/*********************************************************************/ /* pnmntsc - make a 'look-alike ntsc' picture from a PNM file */ /* Frank Neumann, October 1993 */ /* V1.2 27.11.1999 */ /* */ /* version history: */ /* V1.0 12.10.1993 first version */ /* V1.1 16.11.1993 Rewritten to be NetPBM.programming conforming */ /* V1.2 27.11.1999 Promoted from ppmntsc to pnmntsc. */ /*********************************************************************/ #include "pnm.h" /* global variables */ #ifdef AMIGA static char *version = "$VER: pnmntsc 1.2 (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; pixel *srcrow, *destrow; pixel *xP = NULL, *xP2 = NULL; pixval 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 pixel data for the new pixels */ destrow = pnm_allocrow(cols); pnm_writepnminit(stdout, cols, rows, maxval, newformat, 0); /** now do the ntsc'ing (actually very similar to pnmdim) **/ for (i = 0; i < rows; i++) { pnm_readpnmrow(ifp, srcrow, cols, maxval, format); xP = srcrow; xP2 = destrow; for (j = 0; j < cols; j++) { /* every alternating row is left in unchanged condition */ if (i & 1) { *xP2 = *xP; } /* and the other lines are dimmed to the specified factor */ else { 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); }