#!/bin/sh # ppmtogray 0.1.0 (2004-Jan-19-Mon) # Adam M. Costello # http://www.nicemice.net/amc/ # ppmtogray [file] # # Converts a PPM file to a PGM file. Computes luminance, unlike # ppmtopgm, which computes luma. We assume that the input is sRGB, and # we produce sRGB output. The output maxval is always 65535 (16-bit # samples); pipe through pnmdepth if you want something else. # Formula for luminance: # # 0.21264 R + 0.71517 G + 0.07219 B # # Formula for luma (used by ppmtopgm): # # 0.299 R' + 0.587 G' + 0.114 B' # # (The primes indicate gamma-encoded components.) # Create a filter to be used later: filter=/tmp/ppmtogray-$$.ppm cat > $filter << EOF P3 1 1 65534 51894 65534 49798 EOF # Get 16-bit linear samples: pnmdepth 65535 $1 | pnmgamma -srgbramp -ungamma | # Use preprocessing and postprocessing to effectively alter the # coefficients of ppmtopgm: pnmconvol $filter 2>&- | ppmtopgm | #pgmnorm -bvalue 0 -wvalue 53790 | # As of this writing a bug in pgmnorm prevents it from handling 16-bit # values, but 15-bit is okay. Once the bug is fixed we can remove this # workaround and restore the line above. pnmdepth 32767 | pgmnorm -bvalue 0 -wvalue 26895 | pnmdepth 65535 | # Convert back to nonlinear samples: pnmgamma -srgbramp # Clean up the temporary file: rm $filter