OK, I suck.  My first code did not properly handle the case of a line that 
starts with the fg color, much less an entire line of fg color.  Let's try this 
again (sorry that Yahoo eats my indentation):

------------Cut Here------------
# This script will take an ASCII PBM B&W file and convert it to a series
# of "DS" (Draw Segment) statements in PCBNEW syntax.  The deltaX and deltaY
# is defined in "step" which is in uints of 1/10 mil.  The layer is currently
# set to 21, the component layer silkscreen.  Swap bg & fg based on whether
# black or white is the foreground.
#
# State 0 : look for magic number - must be P1 (can be P4 for raw file)
# State 1 : look for height & width
# State 2 : process data
# State 3 : done with data - skip the rest
#
BEGIN { state = 0; step = 40; layer = 21; fg = "1"; bg = "0"; }
{if (NR == 1) {
        state = 1;
        if ($1 != "P1") {
            printf("Must supply an ASCII PBM image file\n");
            exit 1
        }
        next;
    }
}
/^#/    { next }  # Comment line, skip it
{if (state == 1) {
        width = $1;
        height = $2;
        buff = "";
        state = 2;
        Y = - ((step * height) / 2);
        initX = - ((step * width) / 2);
        next;
    }
}
{if (state == 2)  {
        buff = buff $1;
        if (length( buff ) >= width) {
            scanline = substr( buff, 1, width );
            buff = substr( buff, width + 1 );
            Y += step;
            X = initX;
            while ( Z1 = index( scanline, fg )) {
                scanline = substr( scanline, Z1 );
                Z2 = index( scanline, bg );
                if (Z2 == 0)
                    Z2 = length( scanline ) + 1;
                scanline = substr( scanline, Z2 );
                Z1 = step * Z1 + X;
                Z2 = step * Z2 + Z1 - 2 * step;
                X = Z2;
                printf( "DS %d %d %d %d %d %d\n", Z1, Y, Z2, Y, step, layer );
            }
            height--;
            if (height == 0)
                state = 3;
        }
    }
}
{if (state == 3) { nextfile; }}
------------Cut Here------------


Reply via email to