/*********************************************************************/ /* pnmspread - randomly displace a PPM's xels by a certain amount */ /* 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 ppmspread to pnmspread. */ /*********************************************************************/ #include #include "pnm.h" /* global variables */ #ifdef AMIGA static char *version = "$VER: pnmspread 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, i, j; int xdis, ydis, xnew, ynew; xel **destarray, **srcarray; xel *xP, *xP2; xelval maxval; xel x; int amount; char *usage = "amount [pnmfile]\n amount: # of xels to displace a xel by at most\n"; /* parse in 'default' parameters */ pnm_init(&argc, argv); argn = 1; /* parse in amount & seed */ if (argn == argc) pm_usage(usage); if (sscanf(argv[argn], "%d", &amount) != 1) pm_usage(usage); if (amount < 0) pm_error("amount should be a positive number"); ++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 entire picture into buffer */ srcarray = pnm_readpnm(ifp, &cols, &rows, &maxval, &format); /* allocate an entire picture buffer for dest picture */ destarray = pnm_allocarray(cols, rows); /* clear out the buffer */ for (i=0; i < rows; i++) bzero(destarray[i], cols * sizeof(xel)); /* set seed for random number generator */ /* get time of day to feed the random number generator */ srandom( time( (time_t*) 0 ) ); /* start displacing xels */ for (i = 0; i < rows; i++) { xP = srcarray[i]; for (j = 0; j < cols; j++) { xdis = (random() % (amount+1)) - ((amount+1) / 2); ydis = (random() % (amount+1)) - ((amount+1) / 2); xnew = j + xdis; ynew = i + ydis; /* only set the displaced xel if it's within the bounds of the image */ if (xnew >= 0 && xnew < cols && ynew >= 0 && ynew < rows) { /* displacing a xel is accomplished by swapping it with another */ /* xel in its vicinity - so, first store other xel's RGB */ xP2 = srcarray[ynew] + xnew; x = *xP2; /* set second xel to new value */ xP2 = destarray[ynew] + xnew; *xP2 = *xP; /* now, set first xel to (old) value of second*/ xP2 = destarray[i] + j; *xP2 = x; } else { /* displaced xel is out of bounds; leave the old xel there */ xP2 = destarray[i] + j; *xP2 = *xP; } xP++; } } /* write out entire dest picture in one go */ pnm_writepnm(stdout, destarray, cols, rows, maxval, format, 0); pm_closer(ifp); pm_closew(stdout); pnm_freearray(srcarray, rows); pnm_freearray(destarray, rows); exit(0); }