Debian Codegraph @

editor/specialty/ppm3d.c

raw
1 /*=============================================================================
2 ppmto3d
3 ===============================================================================
4 This program converts two PPM images into an anaglyph stereogram image PPM.
5 (for viewing with red/blue 3D glasses).
6
7 =============================================================================*/
8
9 #include <assert.h>
10
11 #include "pm_c_util.h"
12 #include "shhopt.h"
13 #include "mallocvar.h"
14 #include "ppm.h"
15 #include "lum.h"
16
17
18
19 struct cmdlineInfo {
20 /* All the information the user supplied in the command line,
21 in a form easy for the program to use.
22 */
23 const char * leftInputFileName; /* '-' if stdin */
24 const char * rghtInputFileName; /* '-' if stdin */
25 int offset;
26 unsigned int color;
27 };
28
29
30
31 static void
32 parseCommandLine(int argc, char ** argv,
33 struct cmdlineInfo * const cmdlineP) {
34 /*----------------------------------------------------------------------------
35 parse program command line described in Unix standard form by argc
36 and argv. Return the information in the options as *cmdlineP.
37
38 If command line is internally inconsistent (invalid options, etc.),
39 issue error message to stderr and abort program.
40
41 Note that the strings we return are stored in the storage that
42 was passed to us as the argv array. We also trash *argv.
43 -----------------------------------------------------------------------------*/
44 optEntry * option_def;
45 /* Instructions to pm_optParseOptions3 on how to parse our options.
46 */
47 optStruct3 opt;
48
49 unsigned int option_def_index;
50 unsigned int offsetSpec;
51 const char * offsetArg;
52
53 MALLOCARRAY_NOFAIL(option_def, 100);
54
55 option_def_index = 0; /* incremented by OPTENT3 */
56 OPTENT3(0, "color", OPT_FLAG, NULL,
57 &cmdlineP->color, 0);
58 OPTENT3(0, "offset", OPT_INT, &cmdlineP->offset,
59 &offsetSpec, 0);
60
61 opt.opt_table = option_def;
62 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
63 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
64
65 pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0);
66 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
67
68 if (argc-1 < 2)
69 pm_error("You must specify at least two arguments: left and right "
70 "input file names. You specified %u", argc-1);
71 else {
72 cmdlineP->leftInputFileName = argv[1];
73 cmdlineP->rghtInputFileName = argv[2];
74
75 if (argc-1 > 2) {
76 offsetArg = argv[3];
77
78 if (argc-1 > 3)
79 pm_error("Program takes at most 3 arguments: left and "
80 "right input file names and offset. "
81 "You specified %u", argc-1);
82 } else
83 offsetArg = NULL;
84 }
85
86 if (offsetArg && offsetSpec)
87 pm_error("You cannot specify both -offset and the offset "
88 "argument (i.e. with -offset, there is at most "
89 "two arguments: left and right input file names");
90 else if (!offsetArg && !offsetSpec)
91 cmdlineP->offset = 0;
92 else if (offsetArg)
93 cmdlineP->offset = atoi(offsetArg);
94 }
95
96
97
98 static void
99 computeGrayscaleRow(const pixel * const inputRow,
100 gray * const outputRow,
101 pixval const maxval,
102 unsigned int const cols) {
103
104 if (maxval <= 255) {
105 unsigned int col;
106 /* Use fast approximation to 0.299 r + 0.587 g + 0.114 b. */
107 for (col = 0; col < cols; ++col)
108 outputRow[col] = ppm_fastlumin(inputRow[col]);
109 } else {
110 unsigned int col;
111 /* Can't use fast approximation, so fall back on floats. */
112 for (col = 0; col < cols; ++col)
113 outputRow[col] = PPM_LUMIN(inputRow[col]) + 0.5;
114 }
115 }
116
117
118
119 static void
120 compute3dRowMono(gray * const lGrayrow,
121 gray * const rGrayrow,
122 pixel * const pixelrow,
123 unsigned int const cols,
124 int const offset) {
125
126 unsigned int col;
127 gray * lgP;
128 gray * rgP;
129 pixel * pP;
130
131 assert(abs(offset) <= cols);
132
133 for (col = 0, pP = pixelrow, lgP = lGrayrow, rgP = rGrayrow;
134 col < cols + offset;
135 ++col) {
136
137 if ((int)col < offset/2)
138 ++lgP;
139 else if ((int)col < offset) {
140 PPM_ASSIGN(*pP, 0, *lgP, *lgP);
141 ++lgP;
142 ++pP;
143 } else if (col < cols) {
144 PPM_ASSIGN(*pP, *rgP, *lgP, *lgP);
145 ++lgP;
146 ++rgP;
147 ++pP;
148 } else if (col < cols + offset/2) {
149 PPM_ASSIGN(*pP, *rgP, 0, 0);
150 ++rgP;
151 ++pP;
152 } else {
153 assert(col < cols + offset);
154 ++rgP;
155 }
156 }
157 }
158
159
160
161 static void
162 compute3dRowColor(pixel * const lPixelrow,
163 pixel * const rPixelrow,
164 pixel * const pixelrow,
165 unsigned int const cols,
166 unsigned int const offset) {
167
168 unsigned int col;
169 pixel * lP;
170 pixel * rP;
171 pixel * pP;
172
173 assert(abs(offset) <= cols);
174
175 for (col = 0, pP = pixelrow, lP = lPixelrow, rP = rPixelrow;
176 col < cols + offset;
177 ++col) {
178
179 if ((int)col < offset/2)
180 ++lP;
181 else if ((int)col < offset) {
182 PPM_ASSIGN(*pP, 0, PPM_GETG(*lP), PPM_GETB(*lP));
183 ++lP;
184 ++pP;
185 } else if (col < cols) {
186 PPM_ASSIGN(*pP, PPM_GETR(*rP), PPM_GETG(*lP), PPM_GETB(*lP));
187 ++lP;
188 ++rP;
189 ++pP;
190 } else if (col < cols + offset/2) {
191 PPM_ASSIGN(*pP, PPM_GETR(*rP), 0, 0);
192 ++rP;
193 ++pP;
194 } else {
195 assert(col < cols + offset);
196 ++rP;
197 }
198 }
199 }
200
201
202
203 static void
204 write3dRaster(FILE * const ofP,
205 FILE * const lIfP,
206 FILE * const rIfP,
207 unsigned int const cols,
208 unsigned int const rows,
209 pixval const maxval,
210 int const lFormat,
211 int const rFormat,
212 int const offset,
213 bool const color) {
214
215 pixel * lPixelrow;
216 gray * lGrayrow;
217 pixel * rPixelrow;
218 gray * rGrayrow;
219 pixel * pixelrow;
220
221 unsigned int row;
222
223 assert(abs(offset) < cols);
224
225 lPixelrow = ppm_allocrow (cols);
226 lGrayrow = pgm_allocrow (cols);
227 rPixelrow = ppm_allocrow (cols);
228 rGrayrow = pgm_allocrow (cols);
229 pixelrow = ppm_allocrow (cols);
230
231 for (row = 0; row < rows; ++row) {
232 ppm_readppmrow(lIfP, lPixelrow, cols, maxval, lFormat);
233 ppm_readppmrow(rIfP, rPixelrow, cols, maxval, rFormat);
234
235 computeGrayscaleRow(lPixelrow, lGrayrow, maxval, cols);
236 computeGrayscaleRow(rPixelrow, rGrayrow, maxval, cols);
237
238 if (color)
239 compute3dRowColor(lPixelrow, rPixelrow, pixelrow, cols, offset);
240 else
241 compute3dRowMono(lGrayrow, rGrayrow, pixelrow, cols, offset);
242
243 ppm_writeppmrow(ofP, pixelrow, cols, maxval, 0);
244 }
245
246 ppm_freerow(pixelrow);
247 pgm_freerow(rGrayrow);
248 ppm_freerow(rPixelrow);
249 pgm_freerow(lGrayrow);
250 ppm_freerow(lPixelrow);
251 }
252
253
254
255 int
256 main(int argc, char *argv[]) {
257
258 struct cmdlineInfo cmdline;
259 FILE * lIfP;
260 FILE * rIfP;
261
262 int cols, rows;
263 pixval maxval;
264
265 int lRows, lCols;
266 int lFormat;
267 pixval lMaxval;
268
269 int rRows, rCols;
270 int rFormat;
271 pixval rMaxval;
272
273 ppm_init(&argc, argv);
274
275 parseCommandLine(argc, argv, &cmdline);
276
277 lIfP = pm_openr(cmdline.leftInputFileName);
278 rIfP = pm_openr(cmdline.rghtInputFileName);
279
280 ppm_readppminit(lIfP, &lCols, &lRows, &lMaxval, &lFormat);
281 ppm_readppminit(rIfP, &rCols, &rRows, &rMaxval, &rFormat);
282
283 if ((lCols != rCols) || (lRows != rRows) ||
284 (lMaxval != rMaxval) ||
285 (PPM_FORMAT_TYPE(lFormat) != PPM_FORMAT_TYPE(rFormat)))
286 pm_error ("Pictures are not of same size and format");
287
288 cols = lCols;
289 rows = lRows;
290 maxval = lMaxval;
291
292 if (abs(cmdline.offset) >= cols)
293 pm_error("Magnitude of -offset (%u columns) is not less than "
294 "width of images "
295 "(%u columns)", abs(cmdline.offset), cols);
296
297 ppm_writeppminit(stdout, cols, rows, maxval, 0);
298
299 write3dRaster(stdout, lIfP, rIfP, cols, rows, maxval,
300 lFormat, rFormat, cmdline.offset, cmdline.color);
301
302 pm_close(lIfP);
303 pm_close(rIfP);
304 pm_close(stdout);
305
306 return 0;
307 }
308