I'm sorry, I didn't mean to be cryptic. Below is the latest version of my awk
script. For non-Unix/Linux people, "awk" is a scripting language written many,
many years ago and named for the initials of the three guys who initially
developed it (Aho, Weinberger and Kernighan - names that should be familiar to
old CS types).
Awk is part of the base user installation for Unix & Linux, of pretty much all
flavors. I can't speak for OS/X but I'd guess that if you have a shell in a
window, you've got awk. For the Windows crowd, I guess your best option is to
install Cygwin and make sure you get awk with it.
The addition of the first line is just an old Unix/Linux trick based on the
concept of a "magic number" which is a signature in the first few bytes used to
identify the contents of files. Note that this concept is foreign to MS where
the file name extension (after the '.') is supposed to provide all the
information about the file type. Since this is prone to error, and really
ineffective at documenting file contents, I prefer the Unix/Linux "magic
number" route. In this case, the magic number is "#!" ("shebang") which
indicates that it is a script file, followed by the program to be used to
interpret the script file. By marking the file as executable you can execute
it from the command line. When you try to run the script, the loader first
checks the magic number to determine what kind of executable it is (for
example, it will fail to load a binary program compiled for a different
computer or operating system), it sees the signature for the script and
executes the specified interpreter instead (in this case /usr/bin/awk), passing
the script to it.
What does this long winded historical explanation mean? I was just trying to
save you from having to type:
awk -f reformat.awk logo.bmp > logo.tmp
and reduce it to:
./reformat.awk logo.bmp > logo.tmp
(For the observant, the "./" in front of the script is because we all know that
you never include "." in your PATH environment variable - to avoid this, place
the script in a convenient directory where you keep your other executables).
I'll attach my latest version below, which includes a few changes to deal with
extra whitespace allowed by the informal specification of PNG files. I hope
that this is able to help a few people. I've been plinking away at a Perl
script but my Perl is far more rusty than my awk. Sorry about End Of Line
(EOL) problems - it's not something that I worry about since I left Windoze
far, far behind me.
raoul
--- In [email protected], "josh_eeg" <josh...@...> wrote:
>
> I am confused now... does that make things run with less user input or make
> it output the logo correctly?
> If it is a working script could it be uploaded here or somewhere for others?
>
> Do I run that line in the command prompt?
>
> --- In [email protected], "raoulduke_esq" <raoulduke_esq@> wrote:
> >
> > For fun, make the first line of the awk script:
> >
> > #!/usr/bin/awk -f
> >
> > (obviously use the path to your awk). Change the (I call it reformat.awk)
> > script mode to executable and you can now:
> >
> > >./reformat.awk logo.pbm >> logo.mod
> >
> > And you're half way there - just edit to move the DS lines before
> > $EndMODULE and you're done.
> >
> >
> > raoul
---------------------------- Cut Here -------------------------
#!/usr/bin/awk -f
# This script will take an ASCII (also called "plain") PBM image file and
# convert it to a series of "DS" (Draw Segment) statements in PCBNEW syntax.
# The X & Y axis step size is defined in "step" which is in uints of 1/10
# mil (from the PCBNEW spec). The PCB layer for the DS segments is currently
# set to 21, the component layer silkscreen but you can change that. You
# can also 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) {
if (NF < 2) next; # Skip empty lines too
width = $1;
height = $2;
if ((width < 1) || (height < 1)) {
printf("Invalid width or height\n");
exit 1
}
buff = "";
state = 2;
Y = - ((step * height) / 2);
initX = - ((step * width) / 2);
next;
}
}
{if (state == 2) {
gsub( "[[:space:]]", "" );
buff = buff $0;
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 -------------------------