#! /usr/bin/gawk -f # Last edited on 2014-01-16 20:44:33 by stolfilocal # Reads an ascii PGM image of a sideways plot, outputs the # plotted data. # Assumes Y is horizontal to the right, X is vertical down. # Output lines are "{IV} {IHLO} {IHI}" # where # {IV} is a row number. # {IHLO} is the column index of the first nonzero sample in that row. # {IHHI} is the column index of the last nonzero sample in that row. # Indices start at 0 at top (resp left) and BEGIN \ { abort = -1; nh = -1; # Number of columns. nv = -1 # Number of rows. maxval = -1; # Max sample value. np = 4; # Count of header parameters (incl. "P2") remaining. # Interval of nonzero pixels in each row: split("", ihlo) # Min nonzero sample of each row. split("", ihhi) # Max nonzero sample of each row. } (abort >=0) { exit abort; } # Skip comment and blank lines: /^ *([#]|$)/ { next; } # Skip header lines: /^P2 *$/ { if (np != 4) { data_error(("P2 out of place")); } np--; next; } /^[0-9 ]+$/ { for (i = 1; i <= NF; i++) { vi = $(i) + 0; if (np > 0) { # still in header: vi += 0; if (vi <= 0) { data_error(("invalid param in header " vi)); } if (nh < 0) { if(np != 3) { data_error(("beh?")); } nh = vi; } else if (nv < 0) { if(np != 2) { data_error(("beuh?")); } nv = vi; } else if (maxval < 0) { if(np != 1) { data_error(("booh?")); } maxval = vi; } else { data_error(("too many parameters in header " vi)); } np--; if(np == 0) { # Initialize variables for the image scan: */ for (iv = 0; iv < nv; iv++) { ihlo[iv] = nh; ihhi[iv] = -1; } ih = 0; # Row index of next pixel. iv = 0; # Column index of next pixel. } } else { # In image body: if ((vi < 0) || (vi > maxval)) { data_error(("invalid sample in image " vi)); } if (iv >= nv) { data_error(("too many samples in image")); } if ((iv < 0) || (ih < 0) || (ih >= nh)) { data_error(("duh?")); } if (vi > 0) { if (ih < ihlo[iv]) { ihlo[iv] = ih; } if ((ihhi[iv] >= 0) && (ihhi[iv] != ih-1)) { data_warning(("break in scanline " iv " column " ih)); } ihhi[iv] = ih; } ih++; if (ih >= nh) { iv++; ih = 0; } } } next; } // { data_error(("invalid line format")); } END \ { if (abort >= 0) { exit(abort); } for (iv = 0; iv < nv; iv++) { if (ihlo[iv] <= ihhi[iv]) { printf "%7d %7d %7d\n", iv, ihlo[iv], ihhi[iv]; } else { # Print exactly one blank line if there is a gap of one or more # scanlines without nonzero samples: if ((iv > 0) && (ihlo[iv-1] <= ihhi[iv-1])) { printf "\n"; } } } } function data_error(msg) { printf "%s:%s: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; printf " «%s»\n", $0 > "/dev/stderr"; abort = 1; exit(abort); } function data_warning(msg) { printf "%s:%s: !! warning - %s\n", FILENAME, FNR, msg > "/dev/stderr"; printf " «%s»\n", $0 > "/dev/stderr"; } function arg_error(msg) { printf "** %s\n", msg > "/dev/stderr"; abort = 1; exit(abort); }