Debian Codegraph @

converter/ppm/yuvsplittoppm.c

raw symbols SCIP
1 /* yuvsplittoppm.c - construct a portable pixmap from 3 raw files:
2 ** - basename.Y : The Luminance chunk at the size of the Image
3 ** - basename.U : The Chrominance chunk U at 1/4
4 ** - basename.V : The Chrominance chunk V at 1/4
5 ** The subsampled U and V values are made by arithmetic mean.
6 **
7 ** If ccir601 is defined, the produced YUV triples have been scaled again
8 ** to fit into the smaller range of values for this standard.
9 **
10 ** by Marcel Wijkstra <[email protected]>
11 **
12 ** Based on ppmtoyuvsplit.c
13 **
14 ** Permission to use, copy, modify, and distribute this software and its
15 ** documentation for any purpose and without fee is hereby granted, provided
16 ** that the above copyright notice appear in all copies and that both that
17 ** copyright notice and this permission notice appear in supporting
18 ** documentation. This software is provided "as is" without express or
19 ** implied warranty.
20 */
21
22 #include <string.h>
23
24 #include "pm_c_util.h"
25 #include "ppm.h"
26 #include "nstring.h"
27 #include "shhopt.h"
28 #include "mallocvar.h"
29
30 /* x must be signed for the following to work correctly */
31 #define limit(x) (((x>0xffffff)?0xff0000:((x<=0xffff)?0:x&0xff0000))>>16)
32
33 struct cmdlineInfo {
34 /* All the information the user supplied in the command line,
35 in a form easy for the program to use.
36 */
37 const char *filenameBase;
38 unsigned int width;
39 unsigned int height;
40 unsigned int ccir601;
41 /* Whether to create YUV in JFIF(JPEG) or CCIR.601(MPEG) scale */
42 };
43
44
45 static void
46 parseCommandLine(int argc,
47 char ** argv,
48 struct cmdlineInfo *cmdlineP ) {
49 /*----------------------------------------------------------------------------
50 Parse program command line described in Unix standard form by argc
51 and argv. Return the information in the options as *cmdlineP.
52
53 If command line is internally inconsistent (invalid options, etc.),
54 issue error message to stderr and abort program.
55
56 Note that the strings we return are stored in the storage that
57 was passed to us as the argv array. We also trash *argv.
58 -----------------------------------------------------------------------------*/
59 optEntry *option_def = malloc(100*sizeof(optEntry));
60 /* Instructions to pm_optParseOptions3 on how to parse our options.
61 */
62 optStruct3 opt;
63
64 unsigned int option_def_index;
65
66 option_def_index = 0; /* incremented by OPTENT3 */
67 OPTENT3(0, "ccir601", OPT_FLAG, NULL,
68 &cmdlineP->ccir601, 0);
69
70 opt.opt_table = option_def;
71 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
72 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
73
74 pm_optParseOptions3( &argc, argv, opt, sizeof(opt), 0);
75 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
76
77
78 if (argc-1 !=3)
79 pm_error("You must specify 3 arguments. "
80 "You specified %d", argc-1);
81 else {
82 int width, height;
83 cmdlineP->filenameBase = argv[1];
84 width = atoi(argv[2]);
85 if (width < 1)
86 pm_error("Width must be at least 1. You specified %d", width);
87 height = atoi(argv[3]);
88 if (height < 1)
89 pm_error("Height must be at least 1. You specified %d", height);
90 cmdlineP->width = width;
91 cmdlineP->height = height;
92 }
93 }
94
95
96
97 static void
98 computeTwoOutputRows(int const cols,
99 bool const ccir601,
100 unsigned char * const y1buf,
101 unsigned char * const y2buf,
102 unsigned char * const ubuf,
103 unsigned char * const vbuf,
104 pixel * const pixelrow1,
105 pixel * const pixelrow2) {
106
107 int col;
108
109 for (col = 0; col < cols; col += 2) {
110 long int r0,g0,b0,r1,g1,b1,r2,g2,b2,r3,g3,b3;
111 long int u,v,y0,y1,y2,y3,u0,u1,u2,u3,v0,v1,v2,v3;
112
113 y0 = y1buf[col];
114 y1 = y1buf[col+1];
115 y2 = y2buf[col];
116 y3 = y2buf[col+1];
117
118 u = ubuf[col/2] - 128;
119 v = vbuf[col/2] - 128;
120
121 if (ccir601) {
122 y0 = ((y0-16)*255)/219;
123 y1 = ((y1-16)*255)/219;
124 y2 = ((y2-16)*255)/219;
125 y3 = ((y3-16)*255)/219;
126
127 u = (u*255)/224 ;
128 v = (v*255)/224 ;
129 }
130 /* mean the chroma for subsampling */
131
132 u0=u1=u2=u3=u;
133 v0=v1=v2=v3=v;
134
135
136 /* The inverse of the JFIF RGB to YUV Matrix for $00010000 = 1.0
137
138 [Y] [65496 0 91880] [R]
139 [U] = [65533 -22580 -46799] [G]
140 [V] [65537 116128 -8] [B]
141
142 */
143
144 r0 = 65536 * y0 + 91880 * v0;
145 g0 = 65536 * y0 - 22580 * u0 - 46799 * v0;
146 b0 = 65536 * y0 + 116128 * u0 ;
147
148 r1 = 65536 * y1 + 91880 * v1;
149 g1 = 65536 * y1 - 22580 * u1 - 46799 * v1;
150 b1 = 65536 * y1 + 116128 * u1 ;
151
152 r2 = 65536 * y2 + 91880 * v2;
153 g2 = 65536 * y2 - 22580 * u2 - 46799 * v2;
154 b2 = 65536 * y2 + 116128 * u2 ;
155
156 r3 = 65536 * y3 + 91880 * v3;
157 g3 = 65536 * y3 - 22580 * u3 - 46799 * v3;
158 b3 = 65536 * y3 + 116128 * u3 ;
159
160 r0 = limit(r0);
161 r1 = limit(r1);
162 r2 = limit(r2);
163 r3 = limit(r3);
164 g0 = limit(g0);
165 g1 = limit(g1);
166 g2 = limit(g2);
167 g3 = limit(g3);
168 b0 = limit(b0);
169 b1 = limit(b1);
170 b2 = limit(b2);
171 b3 = limit(b3);
172
173 PPM_ASSIGN(pixelrow1[col], (pixval)r0, (pixval)g0, (pixval)b0);
174 PPM_ASSIGN(pixelrow1[col+1], (pixval)r1, (pixval)g1, (pixval)b1);
175 PPM_ASSIGN(pixelrow2[col], (pixval)r2, (pixval)g2, (pixval)b2);
176 PPM_ASSIGN(pixelrow2[col+1], (pixval)r3, (pixval)g3, (pixval)b3);
177 }
178 }
179
180
181
182 int
183 main(int argc, char **argv) {
184
185 struct cmdlineInfo cmdline;
186 FILE *vf,*uf,*yf;
187 int cols, rows;
188 pixel *pixelrow1,*pixelrow2;
189 int row;
190 unsigned char *y1buf,*y2buf,*ubuf,*vbuf;
191 const char * ufname;
192 const char * vfname;
193 const char * yfname;
194
195 ppm_init(&argc, argv);
196
197 parseCommandLine(argc, argv, &cmdline);
198
199 pm_asprintf(&ufname, "%s.U", cmdline.filenameBase);
200 pm_asprintf(&vfname, "%s.V", cmdline.filenameBase);
201 pm_asprintf(&yfname, "%s.Y", cmdline.filenameBase);
202
203 uf = pm_openr(ufname);
204 vf = pm_openr(vfname);
205 yf = pm_openr(yfname);
206
207 ppm_writeppminit(stdout, cmdline.width, cmdline.height, 255, 0);
208
209 if (cmdline.width % 2 != 0) {
210 pm_message("Warning: odd width; last column ignored");
211 cols = cmdline.width - 1;
212 } else
213 cols = cmdline.width;
214
215 if (cmdline.height % 2 != 0) {
216 pm_message("Warning: odd height; last row ignored");
217 rows = cmdline.height - 1;
218 } else
219 rows = cmdline.height;
220
221 pixelrow1 = ppm_allocrow(cols);
222 pixelrow2 = ppm_allocrow(cols);
223
224 MALLOCARRAY_NOFAIL(y1buf, cmdline.width);
225 MALLOCARRAY_NOFAIL(y2buf, cmdline.width);
226 MALLOCARRAY_NOFAIL(ubuf, cmdline.width/2);
227 MALLOCARRAY_NOFAIL(vbuf, cmdline.width/2);
228
229 for (row = 0; row < rows; row += 2) {
230 fread(y1buf, cmdline.width, 1, yf);
231 fread(y2buf, cmdline.width, 1, yf);
232 fread(ubuf, cmdline.width/2, 1, uf);
233 fread(vbuf, cmdline.width/2, 1, vf);
234
235 computeTwoOutputRows(cols, cmdline.ccir601,
236 y1buf, y2buf, ubuf, vbuf,
237 pixelrow1, pixelrow2);
238
239 ppm_writeppmrow(stdout, pixelrow1, cols, (pixval) 255, 0);
240 ppm_writeppmrow(stdout, pixelrow2, cols, (pixval) 255, 0);
241 }
242 pm_close(stdout);
243
244 pm_strfree(yfname);
245 pm_strfree(vfname);
246 pm_strfree(ufname);
247
248 pm_close(yf);
249 pm_close(uf);
250 pm_close(vf);
251
252 exit(0);
253 }
254
255
256