2
/*********************************************************************/
3
/* ppmshift - shift lines of a picture left or right by x pixels */
4
/* Frank Neumann, October 1993 */
8
/* V1.0 11.10.1993 first version */
9
/* V1.1 16.11.1993 Rewritten to be NetPBM.programming conforming */
10
/*********************************************************************/
14
/* global variables */
16
static char *version = "$VER: ppmshift 1.1 (16.11.93)"; /* Amiga version identification */
19
/**************************/
20
/* start of main function */
21
/**************************/
28
int argn, rows, cols, format, i = 0, j = 0;
29
pixel *srcrow, *destrow;
30
pixel *pP = NULL, *pP2 = NULL;
33
char *usage = "shift [ppmfile]\n shift: maximum number of pixels to shift a line by\n";
35
/* parse in 'default' parameters */
36
ppm_init(&argc, argv);
40
/* parse in shift number */
43
if (sscanf(argv[argn], "%d", &shift) != 1)
46
pm_error("shift factor must be 0 or more");
49
/* parse in filename (if present, stdin otherwise) */
52
ifp = pm_openr(argv[argn]);
61
/* read first data from file */
62
ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
67
pm_message("shift amount is larger than picture width - reset to %d", shift);
70
/* no error checking required here, ppmlib does it all for us */
71
srcrow = ppm_allocrow(cols);
73
/* allocate a row of pixel data for the new pixels */
74
destrow = ppm_allocrow(cols);
76
ppm_writeppminit(stdout, cols, rows, maxval, 0);
78
/* get time of day to feed the random number generator */
82
/** now do the shifting **/
83
/* the range by which a line is shifted lays in the range from */
84
/* -shift/2 .. +shift/2 pixels; however, within this range it is */
86
for (i = 0; i < rows; i++)
89
nowshift = (rand() % (shift+1)) - ((shift+1) / 2);
93
ppm_readppmrow(ifp, srcrow, cols, maxval, format);
98
/* if the shift value is less than zero, we take the original pixel line and */
99
/* copy it into the destination line translated to the left by x pixels. The */
100
/* empty pixels on the right end of the destination line are filled up with */
101
/* the pixel that is the right-most in the original pixel line. */
105
for (j = 0; j < cols; j++)
107
PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
109
if (j < (cols+nowshift)-1)
113
/* if the shift value is 0 or positive, the first <nowshift> pixels of the */
114
/* destination line are filled with the first pixel from the source line, */
115
/* and the rest of the source line is copied to the dest line */
118
for (j = 0; j < cols; j++)
120
PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
127
/* write out one line of graphic data */
128
ppm_writeppmrow(stdout, destrow, cols, maxval, 0);
133
ppm_freerow(destrow);