diff options
Diffstat (limited to 'editor')
-rw-r--r-- | editor/pamaddnoise.c | 4 | ||||
-rw-r--r-- | editor/pamditherbw.c | 2 | ||||
-rw-r--r-- | editor/pbmreduce.c | 15 | ||||
-rw-r--r-- | editor/pnmremap.c | 2 | ||||
-rw-r--r-- | editor/ppmshift.c | 219 | ||||
-rw-r--r-- | editor/ppmspread.c | 216 |
6 files changed, 222 insertions, 236 deletions
diff --git a/editor/pamaddnoise.c b/editor/pamaddnoise.c index 9c2d12f7..cf1af815 100644 --- a/editor/pamaddnoise.c +++ b/editor/pamaddnoise.c @@ -208,7 +208,7 @@ main(int argc, char * argv[]) { int argn; const char * inputFilename; int noise_type; - int seed; + unsigned int seed; int i; const char * const usage = "[-type noise_type] [-lsigma x] [-mgsigma x] " "[-sigma1 x] [-sigma2 x] [-lambda x] [-seed n] " @@ -247,7 +247,7 @@ main(int argc, char * argv[]) { pnm_init(&argc, argv); - seed = time(NULL) ^ getpid(); + seed = pm_randseed(); noise_type = GAUSSIAN; argn = 1; diff --git a/editor/pamditherbw.c b/editor/pamditherbw.c index 61c23103..931b475f 100644 --- a/editor/pamditherbw.c +++ b/editor/pamditherbw.c @@ -487,7 +487,7 @@ createFsConverter(struct pam * const graypamP, /* Initialize Floyd-Steinberg error vectors. */ MALLOCARRAY_NOFAIL(stateP->thiserr, graypamP->width + 2); MALLOCARRAY_NOFAIL(stateP->nexterr, graypamP->width + 2); - srand((int)(time(NULL) ^ getpid())); + srand(pm_randseed()); { /* (random errors in [-1/8 .. 1/8]) */ diff --git a/editor/pbmreduce.c b/editor/pbmreduce.c index 15ec2a1b..f49c8d9a 100644 --- a/editor/pbmreduce.c +++ b/editor/pbmreduce.c @@ -91,21 +91,22 @@ main( argc, argv ) pbm_writepbminit( stdout, newcols, newrows, 0 ); newbitrow = pbm_allocrow( newcols ); - if ( halftone == QT_FS ) { + if (halftone == QT_FS) { + unsigned int col; /* Initialize Floyd-Steinberg. */ MALLOCARRAY(thiserr, newcols + 2); MALLOCARRAY(nexterr, newcols + 2); - if ( thiserr == NULL || nexterr == NULL ) - pm_error( "out of memory" ); + if (thiserr == NULL || nexterr == NULL) + pm_error("out of memory"); - srand( (int) ( time( 0 ) ^ getpid( ) ) ); - for ( col = 0; col < newcols + 2; ++col ) - thiserr[col] = ( rand( ) % SCALE - HALFSCALE ) / 4; + srand(pm_randseed()); + for (col = 0; col < newcols + 2; ++col) + thiserr[col] = (rand() % SCALE - HALFSCALE) / 4; /* (random errors in [-SCALE/8 .. SCALE/8]) */ } else { /* These variables are meaningless in this case, and the values should never be used. - */ + */ thiserr = NULL; nexterr = NULL; } diff --git a/editor/pnmremap.c b/editor/pnmremap.c index 2102fe63..e9466fe5 100644 --- a/editor/pnmremap.c +++ b/editor/pnmremap.c @@ -299,7 +299,7 @@ initFserr(struct pam * const pamP, "for Plane %u, size %u", plane, fserrSize); } - srand((int)(time(0) ^ getpid())); + srand(pm_randseed()); { int col; diff --git a/editor/ppmshift.c b/editor/ppmshift.c index 1f8a599b..a765daa5 100644 --- a/editor/ppmshift.c +++ b/editor/ppmshift.c @@ -11,127 +11,122 @@ #include "ppm.h" -/* global variables */ -#ifdef AMIGA -static char *version = "$VER: ppmshift 1.1 (16.11.93)"; /* Amiga version identification */ -#endif - /**************************/ /* start of main function */ /**************************/ -int main(argc, argv) -int argc; -char *argv[]; -{ - FILE* ifp; - time_t timenow; - int argn, rows, cols, format, i = 0, j = 0; - pixel *srcrow, *destrow; - pixel *pP = NULL, *pP2 = NULL; - pixval maxval; - int shift, nowshift; - const char * const usage = "shift [ppmfile]\n shift: maximum number of pixels to shift a line by\n"; - - /* parse in 'default' parameters */ - ppm_init(&argc, argv); - - argn = 1; - - /* parse in shift number */ - if (argn == argc) - pm_usage(usage); - if (sscanf(argv[argn], "%d", &shift) != 1) - pm_usage(usage); - if (shift < 0) - pm_error("shift factor must be 0 or more"); - ++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 */ - ppm_readppminit(ifp, &cols, &rows, &maxval, &format); - - if (shift > cols) +int +main(int argc, + char * argv[]) { + + FILE * ifP; + unsigned int row; + int argn, rows, cols, format; + pixel * srcrow; + pixel * destrow; + pixval maxval; + int shift, nowshift; + int shiftArg; + + const char * const usage = "shift [ppmfile]\n shift: maximum number of pixels to shift a line by\n"; + + /* parse in 'default' parameters */ + ppm_init(&argc, argv); + + argn = 1; + + /* parse in shift number */ + if (argn == argc) + pm_usage(usage); + if (sscanf(argv[argn], "%d", &shiftArg) != 1) + pm_usage(usage); + if (shiftArg < 0) + pm_error("shift factor must be 0 or more"); + ++argn; + + /* parse in filename (if present, stdin otherwise) */ + if (argn != argc) { - shift = cols; - pm_message("shift amount is larger than picture width - reset to %d", shift); + ifP = pm_openr(argv[argn]); + ++argn; } + else + ifP = stdin; + + if (argn != argc) + pm_usage(usage); + + /* read first data from file */ + ppm_readppminit(ifP, &cols, &rows, &maxval, &format); + + if (shiftArg > cols) { + shift = cols; + pm_message("shift amount is larger than picture width - reset to %d", + shift); + } else + shift = shiftArg; - /* no error checking required here, ppmlib does it all for us */ - srcrow = ppm_allocrow(cols); + srcrow = ppm_allocrow(cols); - /* allocate a row of pixel data for the new pixels */ - destrow = ppm_allocrow(cols); + destrow = ppm_allocrow(cols); - ppm_writeppminit(stdout, cols, rows, maxval, 0); + ppm_writeppminit(stdout, cols, rows, maxval, 0); - /* get time of day to feed the random number generator */ - timenow = time(NULL); - srand(timenow); + srand(pm_randseed()); - /** now do the shifting **/ - /* the range by which a line is shifted lays in the range from */ - /* -shift/2 .. +shift/2 pixels; however, within this range it is */ + /** now do the shifting **/ + /* the range by which a line is shifted lays in the range from */ + /* -shift/2 .. +shift/2 pixels; however, within this range it is */ /* randomly chosen */ - for (i = 0; i < rows; i++) - { - if (shift != 0) - nowshift = (rand() % (shift+1)) - ((shift+1) / 2); - else - nowshift = 0; - - ppm_readppmrow(ifp, srcrow, cols, maxval, format); - - pP = srcrow; - pP2 = destrow; - - /* if the shift value is less than zero, we take the original pixel line and */ - /* copy it into the destination line translated to the left by x pixels. The */ - /* empty pixels on the right end of the destination line are filled up with */ - /* the pixel that is the right-most in the original pixel line. */ - if (nowshift < 0) - { - pP+= abs(nowshift); - for (j = 0; j < cols; j++) - { - PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); - pP2++; - if (j < (cols+nowshift)-1) - pP++; - } - } - /* if the shift value is 0 or positive, the first <nowshift> pixels of the */ - /* destination line are filled with the first pixel from the source line, */ - /* and the rest of the source line is copied to the dest line */ - else - { - for (j = 0; j < cols; j++) - { - PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); - pP2++; - if (j >= nowshift) - pP++; - } - } - - /* write out one line of graphic data */ - ppm_writeppmrow(stdout, destrow, cols, maxval, 0); - } - - pm_close(ifp); - ppm_freerow(srcrow); - ppm_freerow(destrow); - - exit(0); -} + for (row = 0; row < rows; ++row) { + pixel * pP; + pixel * pP2; + + if (shift != 0) + nowshift = (rand() % (shift+1)) - ((shift+1) / 2); + else + nowshift = 0; + + ppm_readppmrow(ifP, srcrow, cols, maxval, format); + + pP = &srcrow[0]; + pP2 = &destrow[0]; + + /* if the shift value is less than zero, we take the original + pixel line and copy it into the destination line translated + to the left by x pixels. The empty pixels on the right end + of the destination line are filled up with the pixel that + is the right-most in the original pixel line. + */ + if (nowshift < 0) { + unsigned int col; + pP += abs(nowshift); + for (col = 0; col < cols; ++col) { + PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); + ++pP2; + if (col < (cols + nowshift) - 1) + ++pP; + } + } else { + unsigned int col; + /* The shift value is 0 or positive, so fill the first + <nowshift> pixels of the destination line with the + first pixel from the source line, and copy the rest of + the source line to the dest line + */ + for (col = 0; col < cols; ++col) { + PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); + ++pP2; + if (col >= nowshift) + ++pP; + } + } + + ppm_writeppmrow(stdout, destrow, cols, maxval, 0); + } + pm_close(ifP); + ppm_freerow(srcrow); + ppm_freerow(destrow); + + return 0; +} diff --git a/editor/ppmspread.c b/editor/ppmspread.c index 569d1266..6753f4fe 100644 --- a/editor/ppmspread.c +++ b/editor/ppmspread.c @@ -9,119 +9,109 @@ /*********************************************************************/ #include <string.h> + #include "ppm.h" -/* global variables */ -#ifdef AMIGA -static char *version = "$VER: ppmspread 1.1 (16.11.93)"; /* Amiga version identification */ -#endif - -/**************************/ -/* start of main function */ -/**************************/ -int main(argc, argv) -int argc; -char *argv[]; -{ - FILE* ifp; - int argn, rows, cols, i, j; - int xdis, ydis, xnew, ynew; - pixel **destarray, **srcarray; - pixel *pP, *pP2; - pixval maxval; - pixval r1, g1, b1; - int amount; - time_t timenow; - const char * const usage = "amount [ppmfile]\n amount: # of pixels to displace a pixel by at most\n"; - - /* parse in 'default' parameters */ - ppm_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 = ppm_readppm(ifp, &cols, &rows, &maxval); - - /* allocate an entire picture buffer for dest picture */ - destarray = ppm_allocarray(cols, rows); - - /* clear out the buffer */ - for (i=0; i < rows; i++) - memset(destarray[i], 0, cols * sizeof(pixel)); - - /* set seed for random number generator */ - /* get time of day to feed the random number generator */ - timenow = time(NULL); - srand(timenow); - - /* start displacing pixels */ - for (i = 0; i < rows; i++) - { - pP = srcarray[i]; - - for (j = 0; j < cols; j++) - { - xdis = (rand() % (amount+1)) - ((amount+1) / 2); - ydis = (rand() % (amount+1)) - ((amount+1) / 2); - - xnew = j + xdis; - ynew = i + ydis; - - /* only set the displaced pixel if it's within the bounds of the image */ - if (xnew >= 0 && xnew < cols && ynew >= 0 && ynew < rows) - { - /* displacing a pixel is accomplished by swapping it with another */ - /* pixel in its vicinity - so, first store other pixel's RGB */ - pP2 = srcarray[ynew] + xnew; - r1 = PPM_GETR(*pP2); - g1 = PPM_GETG(*pP2); - b1 = PPM_GETB(*pP2); - /* set second pixel to new value */ - pP2 = destarray[ynew] + xnew; - PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); - - /* now, set first pixel to (old) value of second */ - pP2 = destarray[i] + j; - PPM_ASSIGN(*pP2, r1, g1, b1); - } - else - { - /* displaced pixel is out of bounds; leave the old pixel there */ - pP2 = destarray[i] + j; - PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); - } - pP++; - } - } - - /* write out entire dest picture in one go */ - ppm_writeppm(stdout, destarray, cols, rows, maxval, 0); - - pm_close(ifp); - ppm_freearray(srcarray, rows); - ppm_freearray(destarray, rows); - - exit(0); -} + +int +main(int argc, + char * argv[]) { + + FILE * ifP; + int argn, rows, cols; + unsigned int row; + pixel ** destarray, ** srcarray; + pixel * pP; + pixel * pP2; + pixval maxval; + pixval r1, g1, b1; + int amount; + const char * const usage = "amount [ppmfile]\n amount: # of pixels to displace a pixel by at most\n"; + + /* parse in 'default' parameters */ + ppm_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); + + srcarray = ppm_readppm(ifP, &cols, &rows, &maxval); + + destarray = ppm_allocarray(cols, rows); + + /* clear out the buffer */ + for (row = 0; row < rows; ++row) + memset(destarray[row], 0, cols * sizeof(pixel)); + + srand(pm_randseed()); + + /* start displacing pixels */ + for (row = 0; row < rows; ++row) { + unsigned int col; + pP = &srcarray[row][0]; + + for (col = 0; col < cols; ++col) { + int const xdis = (rand() % (amount+1)) - ((amount+1) / 2); + int const ydis = (rand() % (amount+1)) - ((amount+1) / 2); + + int const xnew = col + xdis; + int const ynew = row + ydis; + + /* only set the displaced pixel if it's within the bounds + of the image + */ + if (xnew >= 0 && xnew < cols && ynew >= 0 && ynew < rows) { + /* displacing a pixel is accomplished by swapping it + with another pixel in its vicinity - so, first + store other pixel's RGB + */ + pP2 = &srcarray[ynew][xnew]; + r1 = PPM_GETR(*pP2); + g1 = PPM_GETG(*pP2); + b1 = PPM_GETB(*pP2); + /* set second pixel to new value */ + pP2 = &destarray[ynew][xnew]; + PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); + + /* now, set first pixel to (old) value of second */ + pP2 = &destarray[row][col]; + PPM_ASSIGN(*pP2, r1, g1, b1); + } else { + /* displaced pixel is out of bounds; leave the old + pixel there + */ + pP2 = &destarray[row][col]; + PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP)); + } + ++pP; + } + } + + ppm_writeppm(stdout, destarray, cols, rows, maxval, 0); + + pm_close(ifP); + ppm_freearray(srcarray, rows); + ppm_freearray(destarray, rows); + + return 0; +} |