/*********************************************************************/ /* pnmdim - dim a picture down to total blackness */ /* Frank Neumann, October 1993 */ /* V1.5 27.11.1999 */ /* */ /* version history: */ /* V1.0 ~ 15.August 1993 first version */ /* V1.1 03.09.1993 uses ppm libs & header files */ /* V1.2 03.09.1993 integer arithmetics instead of float */ /* (gains about 50 % speed up) */ /* V1.3 10.10.1993 reads only one line at a time - this */ /* saves LOTS of memory on big pictures */ /* V1.4 16.11.1993 Rewritten to be NetPBM.programming con- */ /* forming */ /* V1.5 27.11.1999 Promoted from ppmdim to pnmdim. */ /*********************************************************************/ #include "pnm.h" /* global variables */ #ifdef AMIGA static char *version = "$VER: pnmdim 1.5 (27.11.99)"; /* Amiga version identification */ #endif /**************************/ /* start of main function */ /**************************/ int main( int argc, char* argv[] ) { FILE* ifp; int argn, rows, cols, format, newformat, i = 0, j = 0; xel *srcrow, *destrow; xel *xP = NULL, *xP2 = NULL; xelval maxval; double dimfactor; long longfactor; char *usage = "dimfactor [pnmfile]\n dimfactor: 0.0 = total blackness, 1.0 = original picture\n"; /* parse in 'default' parameters */ pnm_init(&argc, argv); argn = 1; /* parse in dim factor */ if (argn == argc) pm_usage(usage); if (sscanf(argv[argn], "%lf", &dimfactor) != 1) pm_usage(usage); if (dimfactor < 0.0 || dimfactor > 1.0) pm_error("dim factor must be in the range from 0.0 to 1.0 "); ++argn; /* parse in filename (if present, stdin otherwise) */ if (argn != argc) { ifp = pm_openr(argv[argn]); ++argn; } else ifp = stdin; if (argn != argc) pm_usage(usage); /* read first data from file */ pnm_pbmmaxval = PNM_MAXMAXVAL; /* use larger value for better results */ pnm_readpnminit(ifp, &cols, &rows, &maxval, &format); /* no error checking required here, pnmlib does it all for us */ srcrow = pnm_allocrow(cols); /* Promote PBM files to PGM. */ if ( PNM_FORMAT_TYPE(format) == PBM_TYPE ) { newformat = PGM_TYPE; pm_message( "promoting from PBM to PGM" ); } else newformat = format; longfactor = (long)(dimfactor * 65536); /* allocate a row of xel data for the new xels */ destrow = pnm_allocrow(cols); pnm_writepnminit(stdout, cols, rows, maxval, newformat, 0); /** now do the dim'ing **/ /* the 'float' parameter for dimming is sort of faked - in fact, we */ /* convert it to a range from 0 to 65536 for integer math. Shouldn't */ /* be something you'll have to worry about, though. */ for (i = 0; i < rows; i++) { pnm_readpnmrow(ifp, srcrow, cols, maxval, format); xP = srcrow; xP2 = destrow; for (j = 0; j < cols; j++) { switch ( PNM_FORMAT_TYPE(format) ) { case PPM_TYPE: PPM_ASSIGN(*xP2, (PPM_GETR(*xP) * longfactor) >> 16, (PPM_GETG(*xP) * longfactor) >> 16, (PPM_GETB(*xP) * longfactor) >> 16); break; default: PNM_ASSIGN1(*xP2, (PNM_GET1(*xP) * longfactor) >> 16); break; } xP++; xP2++; } /* write out one line of graphic data */ pnm_writepnmrow(stdout, destrow, cols, maxval, newformat, 0); } pm_closer(ifp); pm_closew(stdout); pnm_freerow(srcrow); pnm_freerow(destrow); exit(0); }