// Back-engineered version of the Afternow raster script
// which creates the same effect:
// http://scriptographer.com/Gallery/Afternow/
//
// Works with Scriptographer 2.0.
//
// Created by DRGS (hello@drgs.no)

var raster = null;
var sel = null;

sel = activeDocument.getSelectedItems();
for (var i = 0; i < sel.length; i++) {
    obj = sel[i];
    if (raster == null && obj instanceof Raster) raster = obj;
    if (raster != null) break;
}

if (raster != null) {
    values = Dialog.prompt("Enter raster values", [
        { value: 5, description: "X-step (pixels)", width: 50 },
        { value: 10, description: "Y-step (pixels)", width: 50 },
        { value: 50, description: "Height (points)", width: 50 },
        { value: 70, description: "Inclination angle", width: 50 }
    ]);
    if (values) {
        xstep = Math.abs(values[0]);
        ystep = Math.abs(values[1]);
        height = values[2];
        angle = values[3];
        var group = new Group();
        for (var y = 0; y < raster.height; y += ystep) {
            var path = new Path();
            for (var x = 0; x < raster.width; x += xstep) {
                var col = raster.getPixel(x, y).convert(Color.TYPE_GRAY).gray;
                path.segments.add(new Point(
                    x + height * Math.cos(Math.PI * angle / 180) * col,
                    - y + height * Math.sin(Math.PI * angle / 180) * col
                ));
            }
            // path.pointsToCurves();
            path.lineTo(x - xstep, - raster.height);
            path.lineTo(0, - raster.height);
            path.closed = true;
            path.style.stroke.color = new GrayColor(1);
            path.style.fill.color = new GrayColor(0);
            group.appendChild(path);
        }
        group.scale(raster.geometricBounds.width / raster.width); // corrects dimension difference if raster dpi !== document dpi
        group.translate(raster.geometricBounds.topLeft);
        activeDocument.deselectAll();
    }
}