Debian Codegraph @

converter/ppm/ppmtoeyuv.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* Bryan got this from mm.ftp-cs.berkeley.edu from the package
2 mpeg-encode-1.5b-src under the name ppmtoeyuv.c on March 30, 2000.
3 The file was dated January 19, 1995.
4
5 Bryan changed the program to take an argument as the input filename
6 and fixed a crash when the input image has an odd number of rows or
7 columns.
8
9 Then Bryan updated the program on March 15, 2001 to use the Netpbm
10 libraries to read the PPM input and handle multi-image PPM files
11 and arbitrary maxvals.
12
13 There was no attached documentation except for this: Encoder/Berkeley
14 YUV format is merely the concatenation of Y, U, and V data in order.
15 Compare with Abekas YUV, which interlaces Y, U, and V data.
16
17 Future enhancement: It may be useful to have an option to do the
18 calculations without multiplication tables to save memory at the
19 expense of execution speed for large maxvals. Actually, a large
20 maxval without a lot of colors might actually make the tables
21 slower.
22
23 */
24
25 /*
26 * Copyright (c) 1995 The Regents of the University of California.
27 * All rights reserved.
28 *
29 * Permission to use, copy, modify, and distribute this software and its
30 * documentation for any purpose, without fee, and without written agreement is
31 * hereby granted, provided that the above copyright notice and the following
32 * two paragraphs appear in all copies of this software.
33 *
34 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
35 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
36 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
37 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
40 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
41 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
42 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
43 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
44 */
45
46 /*
47 * $Header: /n/picasso/users/keving/encode/src/RCS/readframe.c,v 1.1 1993/07/22 22:23:43 keving Exp keving $
48 * $Log: readframe.c,v $
49 * Revision 1.1 1993/07/22 22:23:43 keving
50 * nothing
51 *
52 */
53
54
55 /*==============*
56 * HEADER FILES *
57 *==============*/
58
59 #include <stdbool.h>
60 #include <stdio.h>
61 #include <ctype.h>
62 #include <string.h>
63 #include <stdlib.h>
64
65 #include "pm_c_util.h"
66 #include "ppm.h"
67 #include "mallocvar.h"
68
69 typedef unsigned char uint8;
70
71 /* Multiplication tables */
72
73 #define YUVMAXVAL 255
74 #define HALFYUVMAXVAL 128
75 /* multXXX are multiplication tables used in RGB-YCC calculations for
76 speed. mult299[x] is x * .299, scaled to a maxval of 255. These
77 are malloc'ed and essentially constant.
78
79 We use these tables because it is much faster to do a
80 multiplication once for each possible sample value than once for
81 each pixel in the image.
82 */
83 static float *mult299, *mult587, *mult114;
84 static float *mult16874, *mult33126, *mult5;
85 static float *mult41869, *mult08131;
86
87 static __inline__ float
88 luminance(pixel const p) {
89 return mult299[PPM_GETR(p)]
90 + mult587[PPM_GETG(p)]
91 + mult114[PPM_GETB(p)]
92 ;
93 }
94
95
96
97 static __inline__ float
98 chrominanceRed(pixel const p) {
99 return mult5[PPM_GETR(p)]
100 + mult41869[PPM_GETG(p)]
101 + mult08131[PPM_GETB(p)]
102 ;
103 }
104
105
106
107 static __inline__ float
108 chrominanceBlue(pixel const p) {
109 return mult16874[PPM_GETR(p)]
110 + mult33126[PPM_GETG(p)]
111 + mult5[PPM_GETB(p)]
112 ;
113 }
114
115
116
117 static void
118 createMultiplicationTables(pixval const maxval) {
119
•120 overflow_add(maxval, 1);
121 MALLOCARRAY_NOFAIL(mult299 , maxval+1);
122 MALLOCARRAY_NOFAIL(mult587 , maxval+1);
123 MALLOCARRAY_NOFAIL(mult114 , maxval+1);
124 MALLOCARRAY_NOFAIL(mult16874 , maxval+1);
125 MALLOCARRAY_NOFAIL(mult33126 , maxval+1);
126 MALLOCARRAY_NOFAIL(mult5 , maxval+1);
127 MALLOCARRAY_NOFAIL(mult41869 , maxval+1);
128 MALLOCARRAY_NOFAIL(mult08131 , maxval+1);
129
130 if (maxval == YUVMAXVAL) {
131 /* fast path */
132 unsigned int index;
133
134 for (index = 0; index <= maxval; ++index) {
135 mult299[index] = 0.29900*index;
136 mult587[index] = 0.58700*index;
137 mult114[index] = 0.11400*index;
138 mult5[index] = 0.50000*index;
139 mult41869[index] = -0.41869*index;
140 mult08131[index] = -0.08131*index;
141 mult16874[index] = -0.16874*index;
142 mult33126[index] = -0.33126*index;
143 }
144 } else {
145 unsigned int index;
146 for (index = 0; index <= maxval; ++index) {
147 mult299[index] = 0.29900*index*(maxval/YUVMAXVAL);
148 mult587[index] = 0.58700*index*(maxval/YUVMAXVAL);
149 mult114[index] = 0.11400*index*(maxval/YUVMAXVAL);
150 mult5[index] = 0.50000*index*(maxval/YUVMAXVAL);
151 mult41869[index] = -0.41869*index*(maxval/YUVMAXVAL);
152 mult08131[index] = -0.08131*index*(maxval/YUVMAXVAL);
153 mult16874[index] = -0.16874*index*(maxval/YUVMAXVAL);
154 mult33126[index] = -0.33126*index*(maxval/YUVMAXVAL);
155 }
156 }
157 }
158
159
160
161 static void
162 freeMultiplicationTables(void) {
163 free(mult299 );
164 free(mult587 );
165 free(mult114 );
166 free(mult16874 );
167 free(mult33126 );
168 free(mult5 );
169 free(mult41869 );
170 free(mult08131 );
171 }
172
173
174
175 /*===========================================================================*
176 *
177 * PPMtoYUV
178 *
179 * convert PPM data into YUV data
180 * assumes that ydivisor = 1
181 *
182 * RETURNS: nothing
183 *
184 * SIDE EFFECTS: none
185 *
186 * This function processes the input file in 4 pixel squares. If the
187 * Image does not have an even number of rows and columns, the rightmost
188 * column or the bottom row gets ignored and output has one fewer row
189 * or column than the input.
190 *
191 *===========================================================================*/
192 static void
193 ppmToYuv(pixel ** const ppmImage,
194 unsigned int const width,
195 unsigned int const height,
196 uint8 *** const orig_yP,
197 uint8 *** const orig_crP,
198 uint8 *** const orig_cbP) {
199
200 int y;
201 uint8 ** orig_y;
202 uint8 ** orig_cr;
203 uint8 ** orig_cb;
204
205 orig_y = *orig_yP;
206 orig_cr = *orig_crP;
207 orig_cb = *orig_cbP;
208
209 for (y = 0; y + 1 < height; y += 2) {
210 uint8 *dy0, *dy1;
211 uint8 *dcr, *dcb;
212 const pixel *src0, *src1;
213 /* Pair of contiguous rows of the ppm input image we are
214 converting */
215 unsigned int x;
216
217 src0 = ppmImage[y];
218 src1 = ppmImage[y + 1];
219
220 dy0 = orig_y[y];
221 dy1 = orig_y[y + 1];
222 dcr = orig_cr[y / 2];
223 dcb = orig_cb[y / 2];
224
225 for (x = 0; x + 1 < width; x += 2) {
226 dy0[x] = luminance(src0[x]);
227 dy1[x] = luminance(src1[x]);
228
229 dy0[x+1] = luminance(src0[x+1]);
230 dy1[x+1] = luminance(src1[x+1]);
231
232 dcr[x/2] = ((
233 chrominanceRed(src0[x]) +
234 chrominanceRed(src1[x]) +
235 chrominanceRed(src0[x+1]) +
236 chrominanceRed(src1[x+1])
237 ) / 4) + HALFYUVMAXVAL;
238
239 dcb[x/2] = ((
240 chrominanceBlue(src0[x]) +
241 chrominanceBlue(src1[x]) +
242 chrominanceBlue(src0[x+1]) +
243 chrominanceBlue(src1[x+1])
244 ) / 4) + HALFYUVMAXVAL;
245 }
246 }
247 }
248
249
250
251 static void
252 writeYUV(FILE * const ofP,
253 unsigned int const width,
254 unsigned int const height,
255 uint8 ** const orig_y,
256 uint8 ** const orig_cr,
257 uint8 ** const orig_cb) {
258
259 unsigned int y;
260
261 for (y = 0; y < height; ++y) /* Y */
262 fwrite(orig_y[y], 1, width, ofP);
263
264 for (y = 0; y < height / 2; ++y) /* U */
265 fwrite(orig_cb[y], 1, width / 2, ofP);
266
267 for (y = 0; y < height / 2; ++y) /* V */
268 fwrite(orig_cr[y], 1, width / 2, ofP);
269 }
270
271
272
273 static void
274 allocYUV(int const width,
275 int const height,
276 uint8 *** const orig_yP,
277 uint8 *** const orig_crP,
278 uint8 *** const orig_cbP) {
279
280 unsigned int y;
281 uint8 ** orig_y;
282 uint8 ** orig_cr;
283 uint8 ** orig_cb;
284
285 MALLOCARRAY_NOFAIL(*orig_yP, height);
286 orig_y = *orig_yP;
287 for (y = 0; y < height; ++y)
288 MALLOCARRAY_NOFAIL(orig_y[y], width);
289
290 MALLOCARRAY_NOFAIL(*orig_crP, height / 2);
291 orig_cr = *orig_crP;
292 for (y = 0; y < height / 2; ++y)
293 MALLOCARRAY_NOFAIL(orig_cr[y], width / 2);
294
295 MALLOCARRAY_NOFAIL(*orig_cbP, height / 2);
296 orig_cb = *orig_cbP;
297 for (y = 0; y < height / 2; ++y)
298 MALLOCARRAY_NOFAIL(orig_cb[y], width / 2);
299 }
300
301
302
303 static void
304 freeYUV(unsigned int const width,
305 unsigned int const height,
306 uint8 ** const orig_y,
307 uint8 ** const orig_cr,
308 uint8 ** const orig_cb){
309
310 unsigned int y;
311
312 if (orig_y) {
313 for (y = 0; y < height; ++y)
314 free(orig_y[y]);
315 free(orig_y);
316 }
317
318 if (orig_cr) {
319 for (y = 0; y < height / 2; ++y)
320 free(orig_cr[y]);
321 free(orig_cr);
322 }
323
324 if (orig_cb) {
325 for (y = 0; y < height / 2; ++y)
326 free(orig_cb[y]);
327 free(orig_cb);
328 }
329 }
330
331
332
333 int
334 main(int argc, const char **argv) {
335
336 const char *inputFilename; /* NULL for stdin */
337 FILE * ifP;
338 int width, height;
339 pixval maxval;
340 pixel ** ppmImage; /* malloc'ed */
341 uint8 **orig_y, **orig_cr, **orig_cb;
342 /* orig_y is the height x width array of individual pixel luminances
343 orig_cr and orig_cb are the height/2 x width/2 arrays of average
344 red and blue chrominance values over each 4 pixel square.
345 */
346 int eof;
347
348 /* The following are width, height, and maxval of the image we
349 processed before this one. Zero if there was no image before
350 this one.
351 */
352 int lastWidth, lastHeight;
353 pixval lastMaxval;
354
355 pm_proginit(&argc, argv);
356
357 if (argc-1 > 1) {
358 pm_error("Program takes either one argument -- "
359 "the input filename -- or no arguments (input is stdin)");
360 exit(1);
361 } else if (argc-1 > 0)
362 inputFilename = argv[1];
363 else
364 inputFilename = "-";
365
366 ifP = pm_openr(inputFilename);
367
368 eof = false; /* EOF not yet encountered */
369 lastMaxval = 0; /* No previous maxval */
370 lastWidth = 0; /* No previous width */
371 lastHeight = 0; /* No previous height */
372 orig_y = orig_cr = orig_cb = 0;
373
374 while (!eof) {
375 ppmImage = ppm_readppm(ifP, &width, &height, &maxval);
376
377 if (width % 2 != 0)
378 pm_message("Input image has odd number of columns. The rightmost "
379 "column will be omitted from the output.");
380 if (height % 2 != 0)
381 pm_message("Input image has odd number of rows. The bottom "
382 "row will be omitted from the output.");
383
384 if (maxval != lastMaxval) {
385 /* We're going to need all new multiplication tables. */
386 freeMultiplicationTables();
387 createMultiplicationTables(maxval);
388 }
389 lastMaxval = maxval;
390
391 if (height != lastHeight || width != lastWidth) {
392 freeYUV(width, height, orig_y, orig_cr, orig_cb);
393 /* Need new YUV buffers for different size */
394 allocYUV(width, height, &orig_y, &orig_cr, &orig_cb);
395 }
396 lastHeight = height;
397 lastWidth = width;
398
399 ppmToYuv(ppmImage, width, height, &orig_y, &orig_cr, &orig_cb);
400
401 writeYUV(stdout, (width/2)*2, (height/2)*2, orig_y, orig_cr, orig_cb);
402
403 ppm_freearray(ppmImage, height);
404 ppm_nextimage(ifP, &eof);
405 }
406 freeYUV(width, height, orig_y, orig_cr, orig_cb);
407 freeMultiplicationTables();
408 pm_close(ifP);
409
410 return 0;
411 }
412
413
414