Debian Codegraph @

editor/specialty/ppmrelief.c

raw
1 /* ppmrelief.c - generate a relief map of a PPM image
2 **
3 ** Copyright (C) 1990 by Wilson H. Bent, Jr.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12
13 #include <stdio.h>
14
15 #include "pm_c_util.h"
16 #include "ppm.h"
17
18
19
20 static pixval
21 clip(int const p,
22 pixval const maxval) {
23
24 return MAX(0, MIN(maxval, p));
25 }
26
27
28
29 int
30 main(int argc, const char * argv[]) {
31
32 FILE * ifP;
33 pixel ** inputbuf;
34 pixel * outputrow;
35 int argn, format, rows, cols;
36 unsigned int row, col;
37 pixval maxval;
38 const char* const usage = "[ppmfile]";
39
40 pm_proginit(&argc, argv);
41
42 argn = 1;
43
44 if ( argn != argc ) {
45 ifP = pm_openr( argv[argn] );
46 ++argn;
47 } else
48 ifP = stdin;
49
50 if ( argn != argc )
51 pm_usage( usage );
52
53 ppm_readppminit(ifP, &cols, &rows, &maxval, &format );
54
55 if (cols < 3 || rows < 3 )
56 pm_error("Input image too small: %u x %u. Must be at least 3x3",
57 cols, rows);
58
59 /* Allocate space for 3 input rows, plus an output row. */
60 inputbuf = ppm_allocarray(cols, 3);
61 outputrow = ppm_allocrow(cols);
62
63 ppm_writeppminit(stdout, cols, rows, maxval, 0);
64
65 /* Read in the first two rows. */
66 ppm_readppmrow(ifP, inputbuf[0], cols, maxval, format);
67 ppm_readppmrow(ifP, inputbuf[1], cols, maxval, format);
68
69 /* Write out the first row, all zeros. */
70 for (col = 0; col < cols; ++col)
71 PPM_ASSIGN( outputrow[col], 0, 0, 0 );
72
73 ppm_writeppmrow(stdout, outputrow, cols, maxval, 0);
74
75 /* Now the rest of the image - read in the 3rd row of inputbuf,
76 and convolve with the first row into the output buffer.
77 */
78 for (row = 2 ; row < rows; ++row) {
79 pixval const mv2 = maxval / 2;
80 unsigned int const rowa = row % 3;
81 unsigned int const rowb = (rowa + 2) % 3;
82
83 ppm_readppmrow(ifP, inputbuf[rowa], cols, maxval, format);
84
85 for (col = 0; col < cols - 2; ++col) {
86 pixel const inputA = inputbuf[rowa][col];
87 pixel const inputB = inputbuf[rowb][col + 2];
88
89 pixval const r =
90 clip(PPM_GETR(inputA) + (mv2 - PPM_GETR(inputB)), maxval);
91 pixval const g =
92 clip(PPM_GETG(inputA) + (mv2 - PPM_GETG(inputB)), maxval);
93 pixval const b =
94 clip(PPM_GETB(inputA) + (mv2 - PPM_GETB(inputB)), maxval);
95
96 PPM_ASSIGN(outputrow[col + 1], r, g, b);
97 }
98 ppm_writeppmrow(stdout, outputrow, cols, maxval, 0);
99 }
100
101 /* And write the last row, zeros again. */
102 for (col = 0; col < cols; ++col)
103 PPM_ASSIGN(outputrow[col], 0, 0, 0);
104
105 ppm_writeppmrow(stdout, outputrow, cols, maxval, 0);
106
107 ppm_freerow(outputrow);
108 ppm_freearray(inputbuf, 3);
109
110 pm_close(ifP);
111 pm_close(stdout);
112
113 return 0;
114 }