#! /usr/bin/gawk -f # Last edited on 2025-08-18 08:53:27 by stolfi # Writes to {stdout} everything read from {stdin}, # after replacing pixel measurements made on the BL2014 images, # by the equivalent measure in millimeters or panel width/height fractions. # # Sepcifically, each occurrence of /[%][0-9]+/ in the input is assumed to be # a dimesnion in pixels measured on the BL2014 images, and is replaced # by the equivalent measure in millimeters, "~{N.NN} mm". # # Also each occurrence f /[0-9]+[/][/][0-9]+/ is assumed to be a fraction # and is replaced by the equivalent decimal fraction, as "~{N.NN}" BEGIN { # ??? Is the height 3500 px valid also for fold-outs? ??? if (vsize == "") { vsize = 3500; } else { vsize += 0; } scale = 235.0/vsize; # Assumed scale for "%" data. } // { lin = $0; while (1) { if (match(lin, /[%][0-9.]+/)) { pref = substr(lin, 1, RSTART-1) pixs = substr(lin, RSTART+1, RLENGTH-1) suff = substr(lin, RSTART+RLENGTH) if (! match(pixs, /^[0-9.]+$/)) { prog_error("bug %"); } dval = scale * pixs; if (dval >= 5.0) { fmt = "%.0f"; } else if (dval >= 0.5) { fmt = "%.1f"; } else { fmt = "%.2f"; } unit = " mm"; } else if (match(lin, /[0-9]+[\/][\/][0-9]+/)) { pref = substr(lin, 1, RSTART-1) frac = substr(lin, RSTART, RLENGTH) suff = substr(lin, RSTART+RLENGTH) if (! match(frac, /[\/][\/]/)) { prog_error("bug //"); } fnum = substr(frac, 1, RSTART-1) fden = substr(frac, RSTART+RLENGTH) if (! match(fnum, /^[0-9]+$/)) { prog_error("bug fnum"); } if (! match(fden, /^[0-9]+$/)) { prog_error("bug fden"); } dval = (fnum + 0)/(fden + 0); if ((dval <= 0.2 ) || (dval >= 0.8)) { fmt = "%.2f"; } else { fmt = "%.1f"; } unit = ""; } else { break; } dval = sprintf(fmt, dval); lin = (pref "~" dval unit suff); } print lin; next } function prog_error(msg) { printf "** %s\n", msg > "/dev/stderr" exit(1) }