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
|
/* ppmrelief.c - generate a relief map of a PPM image
**
** Copyright (C) 1990 by Wilson H. Bent, Jr.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
#include <stdio.h>
#include "pm_c_util.h"
#include "ppm.h"
static pixval
clip(int const p,
pixval const maxval) {
return MAX(0, MIN(maxval, p));
}
int
main(int argc, const char * argv[]) {
FILE * ifP;
pixel ** inputbuf;
pixel * outputrow;
int argn, format, rows, cols;
unsigned int row, col;
pixval maxval;
const char* const usage = "[ppmfile]";
pm_proginit(&argc, argv);
argn = 1;
if ( argn != argc ) {
ifP = pm_openr( argv[argn] );
++argn;
} else
ifP = stdin;
if ( argn != argc )
pm_usage( usage );
ppm_readppminit(ifP, &cols, &rows, &maxval, &format );
if (cols < 3 || rows < 3 )
pm_error("Input image too small: %u x %u. Must be at least 3x3",
cols, rows);
/* Allocate space for 3 input rows, plus an output row. */
inputbuf = ppm_allocarray(cols, 3);
outputrow = ppm_allocrow(cols);
ppm_writeppminit(stdout, cols, rows, maxval, 0);
/* Read in the first two rows. */
ppm_readppmrow(ifP, inputbuf[0], cols, maxval, format);
ppm_readppmrow(ifP, inputbuf[1], cols, maxval, format);
/* Write out the first row, all zeros. */
for (col = 0; col < cols; ++col)
PPM_ASSIGN( outputrow[col], 0, 0, 0 );
ppm_writeppmrow(stdout, outputrow, cols, maxval, 0);
/* Now the rest of the image - read in the 3rd row of inputbuf,
and convolve with the first row into the output buffer.
*/
for (row = 2 ; row < rows; ++row) {
pixval const mv2 = maxval / 2;
unsigned int const rowa = row % 3;
unsigned int const rowb = (rowa + 2) % 3;
ppm_readppmrow(ifP, inputbuf[rowa], cols, maxval, format);
for (col = 0; col < cols - 2; ++col) {
pixel const inputA = inputbuf[rowa][col];
pixel const inputB = inputbuf[rowb][col + 2];
pixval const r =
clip(PPM_GETR(inputA) + (mv2 - PPM_GETR(inputB)), maxval);
pixval const g =
clip(PPM_GETG(inputA) + (mv2 - PPM_GETG(inputB)), maxval);
pixval const b =
clip(PPM_GETB(inputA) + (mv2 - PPM_GETB(inputB)), maxval);
PPM_ASSIGN(outputrow[col + 1], r, g, b);
}
ppm_writeppmrow(stdout, outputrow, cols, maxval, 0);
}
/* And write the last row, zeros again. */
for (col = 0; col < cols; ++col)
PPM_ASSIGN(outputrow[col], 0, 0, 0);
ppm_writeppmrow(stdout, outputrow, cols, maxval, 0);
ppm_freerow(outputrow);
ppm_freearray(inputbuf, 3);
pm_close(ifP);
pm_close(stdout);
return 0;
}
|