about summary refs log tree commit diff
path: root/editor/ppmshift.c
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-10-02 03:42:10 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2006-10-02 03:42:10 +0000
commit0bf54ddfa2a0efcf6142ad1292abbfa26fe0bee3 (patch)
tree5e1255181517b72c3c5670da6be95e5ba8624614 /editor/ppmshift.c
parent9184208ec40610f6856907fa00d814ae093d3e3b (diff)
downloadnetpbm-mirror-0bf54ddfa2a0efcf6142ad1292abbfa26fe0bee3.tar.gz
netpbm-mirror-0bf54ddfa2a0efcf6142ad1292abbfa26fe0bee3.tar.xz
netpbm-mirror-0bf54ddfa2a0efcf6142ad1292abbfa26fe0bee3.zip
cleanup
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@82 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'editor/ppmshift.c')
-rw-r--r--editor/ppmshift.c219
1 files changed, 107 insertions, 112 deletions
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;
+}