~ubuntu-branches/ubuntu/jaunty/netpbm-free/jaunty

« back to all changes in this revision

Viewing changes to ppm/ppmshift.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2004-07-29 20:25:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040729202546-5x43bcfsdlt7dspl
Tags: upstream-10.0
ImportĀ upstreamĀ versionĀ 10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*********************************************************************/
 
3
/* ppmshift -  shift lines of a picture left or right by x pixels    */
 
4
/* Frank Neumann, October 1993                                       */
 
5
/* V1.1 16.11.1993                                                   */
 
6
/*                                                                   */
 
7
/* version history:                                                  */
 
8
/* V1.0    11.10.1993  first version                                 */
 
9
/* V1.1    16.11.1993  Rewritten to be NetPBM.programming conforming */
 
10
/*********************************************************************/
 
11
 
 
12
#include "ppm.h"
 
13
 
 
14
/* global variables */
 
15
#ifdef AMIGA
 
16
static char *version = "$VER: ppmshift 1.1 (16.11.93)"; /* Amiga version identification */
 
17
#endif
 
18
 
 
19
/**************************/
 
20
/* start of main function */
 
21
/**************************/
 
22
int main(argc, argv)
 
23
int argc;
 
24
char *argv[];
 
25
{
 
26
        FILE* ifp;
 
27
        time_t timenow;
 
28
        int argn, rows, cols, format, i = 0, j = 0;
 
29
        pixel *srcrow, *destrow;
 
30
        pixel *pP = NULL, *pP2 = NULL;
 
31
        pixval maxval;
 
32
        int shift, nowshift;
 
33
        char *usage = "shift [ppmfile]\n        shift: maximum number of pixels to shift a line by\n";
 
34
 
 
35
        /* parse in 'default' parameters */
 
36
        ppm_init(&argc, argv);
 
37
 
 
38
        argn = 1;
 
39
 
 
40
        /* parse in shift number */
 
41
        if (argn == argc)
 
42
                pm_usage(usage);
 
43
        if (sscanf(argv[argn], "%d", &shift) != 1)
 
44
                pm_usage(usage);
 
45
        if (shift < 0)
 
46
                pm_error("shift factor must be 0 or more");
 
47
        ++argn;
 
48
 
 
49
        /* parse in filename (if present, stdin otherwise) */
 
50
        if (argn != argc)
 
51
        {
 
52
                ifp = pm_openr(argv[argn]);
 
53
                ++argn;
 
54
        }
 
55
        else
 
56
                ifp = stdin;
 
57
 
 
58
        if (argn != argc)
 
59
                pm_usage(usage);
 
60
 
 
61
        /* read first data from file */
 
62
        ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
 
63
 
 
64
        if (shift > cols)
 
65
    {
 
66
                shift = cols;
 
67
        pm_message("shift amount is larger than picture width - reset to %d", shift);
 
68
    }
 
69
 
 
70
        /* no error checking required here, ppmlib does it all for us */
 
71
        srcrow = ppm_allocrow(cols);
 
72
 
 
73
        /* allocate a row of pixel data for the new pixels */
 
74
        destrow = ppm_allocrow(cols);
 
75
 
 
76
        ppm_writeppminit(stdout, cols, rows, maxval, 0);
 
77
 
 
78
        /* get time of day to feed the random number generator */
 
79
        timenow = time(NULL);
 
80
        srand(timenow);
 
81
 
 
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 */
 
85
    /* randomly chosen */
 
86
        for (i = 0; i < rows; i++)
 
87
        {
 
88
                if (shift != 0)
 
89
                        nowshift = (rand() % (shift+1)) - ((shift+1) / 2);
 
90
                else
 
91
                        nowshift = 0;
 
92
 
 
93
                ppm_readppmrow(ifp, srcrow, cols, maxval, format);
 
94
 
 
95
                pP = srcrow;
 
96
                pP2 = destrow;
 
97
 
 
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.              */
 
102
                if (nowshift < 0)
 
103
                {
 
104
                        pP+= abs(nowshift);
 
105
                        for (j = 0; j < cols; j++)
 
106
                        {
 
107
                                PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
 
108
                                pP2++;
 
109
                if (j < (cols+nowshift)-1)
 
110
                                        pP++;
 
111
                        }
 
112
                }
 
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              */
 
116
                else
 
117
                {
 
118
                        for (j = 0; j < cols; j++)
 
119
                        {
 
120
                                PPM_ASSIGN(*pP2, PPM_GETR(*pP), PPM_GETG(*pP), PPM_GETB(*pP));
 
121
                                pP2++;
 
122
                if (j >= nowshift)
 
123
                                        pP++;
 
124
                        }
 
125
                }
 
126
 
 
127
                /* write out one line of graphic data */
 
128
                ppm_writeppmrow(stdout, destrow, cols, maxval, 0);
 
129
        }
 
130
 
 
131
        pm_close(ifp);
 
132
        ppm_freerow(srcrow);
 
133
        ppm_freerow(destrow);
 
134
 
 
135
        exit(0);
 
136
}
 
137