Debian Codegraph @

converter/ppm/ppmtoeyuv.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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 MALLOCARRAY_NOFAIL(mult299 , maxval+1);
121 MALLOCARRAY_NOFAIL(mult587 , maxval+1);
122 MALLOCARRAY_NOFAIL(mult114 , maxval+1);
123 MALLOCARRAY_NOFAIL(mult16874 , maxval+1);
124 MALLOCARRAY_NOFAIL(mult33126 , maxval+1);
125 MALLOCARRAY_NOFAIL(mult5 , maxval+1);
126 MALLOCARRAY_NOFAIL(mult41869 , maxval+1);
127 MALLOCARRAY_NOFAIL(mult08131 , maxval+1);
128
129 if (maxval == YUVMAXVAL) {
130 /* fast path */
131 unsigned int index;
132
133 for (index = 0; index <= maxval; ++index) {
134 mult299[index] = 0.29900*index;
135 mult587[index] = 0.58700*index;
136 mult114[index] = 0.11400*index;
137 mult5[index] = 0.50000*index;
138 mult41869[index] = -0.41869*index;
139 mult08131[index] = -0.08131*index;
140 mult16874[index] = -0.16874*index;
141 mult33126[index] = -0.33126*index;
142 }
143 } else {
144 unsigned int index;
145 for (index = 0; index <= maxval; ++index) {
146 mult299[index] = 0.29900*index*(maxval/YUVMAXVAL);
147 mult587[index] = 0.58700*index*(maxval/YUVMAXVAL);
148 mult114[index] = 0.11400*index*(maxval/YUVMAXVAL);
149 mult5[index] = 0.50000*index*(maxval/YUVMAXVAL);
150 mult41869[index] = -0.41869*index*(maxval/YUVMAXVAL);
151 mult08131[index] = -0.08131*index*(maxval/YUVMAXVAL);
152 mult16874[index] = -0.16874*index*(maxval/YUVMAXVAL);
153 mult33126[index] = -0.33126*index*(maxval/YUVMAXVAL);
154 }
155 }
156 }
157
158
159
160 static void
161 freeMultiplicationTables(void) {
162 free(mult299 );
163 free(mult587 );
164 free(mult114 );
165 free(mult16874 );
166 free(mult33126 );
167 free(mult5 );
168 free(mult41869 );
169 free(mult08131 );
170 }
171
172
173
174 /*===========================================================================*
175 *
176 * PPMtoYUV
177 *
178 * convert PPM data into YUV data
179 * assumes that ydivisor = 1
180 *
181 * RETURNS: nothing
182 *
183 * SIDE EFFECTS: none
184 *
185 * This function processes the input file in 4 pixel squares. If the
186 * Image does not have an even number of rows and columns, the rightmost
187 * column or the bottom row gets ignored and output has one fewer row
188 * or column than the input.
189 *
190 *===========================================================================*/
191 static void
192 ppmToYuv(pixel ** const ppmImage,
193 unsigned int const width,
194 unsigned int const height,
195 uint8 *** const orig_yP,
196 uint8 *** const orig_crP,
197 uint8 *** const orig_cbP) {
198
199 int y;
200 uint8 ** orig_y;
201 uint8 ** orig_cr;
202 uint8 ** orig_cb;
203
204 orig_y = *orig_yP;
205 orig_cr = *orig_crP;
206 orig_cb = *orig_cbP;
207
208 for (y = 0; y + 1 < height; y += 2) {
209 uint8 *dy0, *dy1;
210 uint8 *dcr, *dcb;
211 const pixel *src0, *src1;
212 /* Pair of contiguous rows of the ppm input image we are
213 converting */
214 unsigned int x;
215
216 src0 = ppmImage[y];
217 src1 = ppmImage[y + 1];
218
219 dy0 = orig_y[y];
220 dy1 = orig_y[y + 1];
221 dcr = orig_cr[y / 2];
222 dcb = orig_cb[y / 2];
223
224 for (x = 0; x + 1 < width; x += 2) {
225 dy0[x] = luminance(src0[x]);
226 dy1[x] = luminance(src1[x]);
227
228 dy0[x+1] = luminance(src0[x+1]);
229 dy1[x+1] = luminance(src1[x+1]);
230
231 dcr[x/2] = ((
232 chrominanceRed(src0[x]) +
233 chrominanceRed(src1[x]) +
234 chrominanceRed(src0[x+1]) +
235 chrominanceRed(src1[x+1])
236 ) / 4) + HALFYUVMAXVAL;
237
238 dcb[x/2] = ((
239 chrominanceBlue(src0[x]) +
240 chrominanceBlue(src1[x]) +
241 chrominanceBlue(src0[x+1]) +
242 chrominanceBlue(src1[x+1])
243 ) / 4) + HALFYUVMAXVAL;
244 }
245 }
246 }
247
248
249
250 static void
251 writeYUV(FILE * const ofP,
252 unsigned int const width,
253 unsigned int const height,
254 uint8 ** const orig_y,
255 uint8 ** const orig_cr,
256 uint8 ** const orig_cb) {
257
258 unsigned int y;
259
260 for (y = 0; y < height; ++y) /* Y */
261 fwrite(orig_y[y], 1, width, ofP);
262
263 for (y = 0; y < height / 2; ++y) /* U */
264 fwrite(orig_cb[y], 1, width / 2, ofP);
265
266 for (y = 0; y < height / 2; ++y) /* V */
267 fwrite(orig_cr[y], 1, width / 2, ofP);
268 }
269
270
271
272 static void
273 allocYUV(int const width,
274 int const height,
275 uint8 *** const orig_yP,
276 uint8 *** const orig_crP,
277 uint8 *** const orig_cbP) {
278
279 unsigned int y;
280 uint8 ** orig_y;
281 uint8 ** orig_cr;
282 uint8 ** orig_cb;
283
284 MALLOCARRAY_NOFAIL(*orig_yP, height);
285 orig_y = *orig_yP;
286 for (y = 0; y < height; ++y)
287 MALLOCARRAY_NOFAIL(orig_y[y], width);
288
289 MALLOCARRAY_NOFAIL(*orig_crP, height / 2);
290 orig_cr = *orig_crP;
291 for (y = 0; y < height / 2; ++y)
292 MALLOCARRAY_NOFAIL(orig_cr[y], width / 2);
293
294 MALLOCARRAY_NOFAIL(*orig_cbP, height / 2);
295 orig_cb = *orig_cbP;
296 for (y = 0; y < height / 2; ++y)
297 MALLOCARRAY_NOFAIL(orig_cb[y], width / 2);
298 }
299
300
301
302 static void
303 freeYUV(unsigned int const width,
304 unsigned int const height,
305 uint8 ** const orig_y,
306 uint8 ** const orig_cr,
307 uint8 ** const orig_cb){
308
309 unsigned int y;
310
311 if (orig_y) {
312 for (y = 0; y < height; ++y)
313 free(orig_y[y]);
314 free(orig_y);
315 }
316
317 if (orig_cr) {
318 for (y = 0; y < height / 2; ++y)
319 free(orig_cr[y]);
320 free(orig_cr);
321 }
322
323 if (orig_cb) {
324 for (y = 0; y < height / 2; ++y)
325 free(orig_cb[y]);
326 free(orig_cb);
327 }
328 }
329
330
331
332 int
333 main(int argc, const char **argv) {
334
335 const char *inputFilename; /* NULL for stdin */
336 FILE * ifP;
337 int width, height;
338 pixval maxval;
339 pixel ** ppmImage; /* malloc'ed */
340 uint8 **orig_y, **orig_cr, **orig_cb;
341 /* orig_y is the height x width array of individual pixel luminances
342 orig_cr and orig_cb are the height/2 x width/2 arrays of average
343 red and blue chrominance values over each 4 pixel square.
344 */
345 int eof;
346
347 /* The following are width, height, and maxval of the image we
348 processed before this one. Zero if there was no image before
349 this one.
350 */
351 int lastWidth, lastHeight;
352 pixval lastMaxval;
353
354 pm_proginit(&argc, argv);
355
356 if (argc-1 > 1) {
357 pm_error("Program takes either one argument -- "
358 "the input filename -- or no arguments (input is stdin)");
359 exit(1);
360 } else if (argc-1 > 0)
361 inputFilename = argv[1];
362 else
363 inputFilename = "-";
364
365 ifP = pm_openr(inputFilename);
366
367 eof = false; /* EOF not yet encountered */
368 lastMaxval = 0; /* No previous maxval */
369 lastWidth = 0; /* No previous width */
370 lastHeight = 0; /* No previous height */
371 orig_y = orig_cr = orig_cb = 0;
372
373 while (!eof) {
374 ppmImage = ppm_readppm(ifP, &width, &height, &maxval);
375
376 if (width % 2 != 0)
377 pm_message("Input image has odd number of columns. The rightmost "
378 "column will be omitted from the output.");
379 if (height % 2 != 0)
380 pm_message("Input image has odd number of rows. The bottom "
381 "row will be omitted from the output.");
382
383 if (maxval != lastMaxval) {
384 /* We're going to need all new multiplication tables. */
385 freeMultiplicationTables();
386 createMultiplicationTables(maxval);
387 }
388 lastMaxval = maxval;
389
390 if (height != lastHeight || width != lastWidth) {
391 freeYUV(width, height, orig_y, orig_cr, orig_cb);
392 /* Need new YUV buffers for different size */
393 allocYUV(width, height, &orig_y, &orig_cr, &orig_cb);
394 }
395 lastHeight = height;
396 lastWidth = width;
397
398 ppmToYuv(ppmImage, width, height, &orig_y, &orig_cr, &orig_cb);
399
400 writeYUV(stdout, (width/2)*2, (height/2)*2, orig_y, orig_cr, orig_cb);
401
402 ppm_freearray(ppmImage, height);
403 ppm_nextimage(ifP, &eof);
404 }
405 freeYUV(width, height, orig_y, orig_cr, orig_cb);
406 freeMultiplicationTables();
407 pm_close(ifP);
408
409 return 0;
410 }
411
412
413