1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*********************************************************************/
/* ppmshift - shift lines of a picture left or right by x pixels */
/* Frank Neumann, October 1993 */
/* V1.1 16.11.1993 */
/* */
/* version history: */
/* V1.0 11.10.1993 first version */
/* V1.1 16.11.1993 Rewritten to be NetPBM.programming conforming */
/*********************************************************************/
#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)
{
shift = cols;
pm_message("shift amount is larger than picture width - reset to %d", shift);
}
/* no error checking required here, ppmlib does it all for us */
srcrow = ppm_allocrow(cols);
/* allocate a row of pixel data for the new pixels */
destrow = ppm_allocrow(cols);
ppm_writeppminit(stdout, cols, rows, maxval, 0);
/* get time of day to feed the random number generator */
timenow = time(NULL);
srand(timenow);
/** 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);
}
|