Debian Codegraph @

converter/other/yuy2topam.c

raw
1 /* Convert an YUY2 image to a PAM image
2 *
3 * See
4 * http://msdn.microsoft.com/en-us/library/aa904813%28VS.80%29.aspx#yuvformats_2
5 * and http://www.digitalpreservation.gov/formats/fdd/fdd000364.shtml for
6 * details.
7 *
8 * By Michael Haardt 2014.
9 *
10 * Contributed to the public domain by its author.
11 *
12 * Recoded in Netpbm style by Bryan Henderson
13 */
14
15 #include <stdio.h>
16 #include <string.h>
17
18 #include "pm_c_util.h"
19 #include "mallocvar.h"
20 #include "pm.h"
21 #include "pam.h"
22 #include "shhopt.h"
23
24
25
26 struct CmdlineInfo {
27 const char * inputFileName;
28 unsigned int width;
29 unsigned int height;
30 };
31
32
33
34 static void
35 parseCommandLine(int argc, const char ** argv,
36 struct CmdlineInfo * const cmdlineP) {
37 /* --------------------------------------------------------------------------
38 Parse program command line described in Unix standard form by argc
39 and argv. Return the information in the options as *cmdlineP
40
41 If command line is internally inconsistent (invalid options, etc.),
42 issue error message to stderr and abort program.
43
44 Note that the strings we return are stored in the storage that
45 was passed to us as the argv array. We also trash *argv.
46 --------------------------------------------------------------------------*/
47 optEntry * option_def; /* Used by OPTENT3 */
48 optStruct3 opt;
49
50 unsigned int widthSpec, heightSpec;
51 unsigned int option_def_index;
52
53 MALLOCARRAY_NOFAIL(option_def, 100);
54
55 option_def_index = 0; /* incremented by OPTENT3 */
56 OPTENT3(0, "width", OPT_UINT,
57 &cmdlineP->width, &widthSpec, 0);
58 OPTENT3(0, "height", OPT_UINT,
59 &cmdlineP->height, &heightSpec, 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_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
66 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
67
68 if (!widthSpec)
69 pm_error("You must specify the image width with -width");
70 if (cmdlineP->width == 0)
71 pm_error("-width cannot be zero");
72
73 if (cmdlineP->width % 2 != 0)
74 pm_error("-width %u is odd, but YUY2 images must have an even width.",
75 cmdlineP->width);
76
77 if (!heightSpec)
78 pm_error("You must specify the image height with -height");
79 if (cmdlineP->height == 0)
80 pm_error("-height cannot be zero");
81
82 if (argc-1 < 1)
83 cmdlineP->inputFileName = "-";
84 else {
85 cmdlineP->inputFileName = argv[1];
86
87 if (argc-1 > 1)
88 pm_error("Too many arguments (%u). The only non-option argument "
89 "is the input file name.", argc-1);
90 }
91 }
92
93
94
95 typedef struct {
96 int y0;
97 int y1;
98 int u;
99 int v;
100 } Yuy2Pixel;
101
102
103
104 static Yuy2Pixel
105 readPixel(FILE * const ifP) {
106 /*----------------------------------------------------------------------------
107 Read one pixel from the YUY2 input. YUY2 represents a pixel in 4 bytes.
108 -----------------------------------------------------------------------------*/
109 Yuy2Pixel retval;
110 unsigned char c;
111
112 pm_readcharu(ifP, &c); retval.y0 = c - 16;
113 pm_readcharu(ifP, &c); retval.u = c - 128;
114 pm_readcharu(ifP, &c); retval.y1 = c - 16;
115 pm_readcharu(ifP, &c); retval.v = c - 128;
116
117 return retval;
118 }
119
120
121
122 typedef struct {
123 int a1;
124 int a2;
125 int a3;
126 int a4;
127 } UvCoeff;
128
129 typedef struct {
130 int a0a;
131 int a0b;
132 UvCoeff uv;
133 } Coeff;
134
135
136
137 static Coeff
138 coeffFromYuy2(Yuy2Pixel const yuy2) {
139
140 Coeff retval;
141
142 retval.a0a = 298 * yuy2.y0;
143 retval.a0b = 298 * yuy2.y1;
144 retval.uv.a1 = 409 * yuy2.v;
145 retval.uv.a2 = 100 * yuy2.u;
146 retval.uv.a3 = 208 * yuy2.v;
147 retval.uv.a4 = 516 * yuy2.u;
148
149 return retval;
150 }
151
152
153
154 typedef struct {
155 int r;
156 int g;
157 int b;
158 } Rgb;
159
160
161
162 static Rgb
163 rgbFromCoeff(int const a0,
164 UvCoeff const uv) {
165
166 Rgb retval;
167
168 retval.r = (a0 + uv.a1 + 128) >> 8;
169 retval.g = (a0 - uv.a2 - uv.a3 + 128) >> 8;
170 retval.b = (a0 + uv.a4 + 128) >> 8;
171
172 return retval;
173 }
174
175
176
177 static Rgb
178 rgbFromCoeff0(Coeff const coeff) {
179
180 return rgbFromCoeff(coeff.a0a, coeff.uv);
181 }
182
183
184
185 static Rgb
186 rgbFromCoeff1(Coeff const coeff) {
187
188 return rgbFromCoeff(coeff.a0b, coeff.uv);
189 }
190
191
192
193 static void
194 rgbToTuple(Rgb const rgb,
195 tuple const out) {
196
197 out[PAM_RED_PLANE] = MIN(255, MAX(0, rgb.r));
198 out[PAM_GRN_PLANE] = MIN(255, MAX(0, rgb.g));
199 out[PAM_BLU_PLANE] = MIN(255, MAX(0, rgb.b));
200 }
201
202
203
204 static void
205 yuy2topam(const char * const fileName,
206 unsigned int const width,
207 unsigned int const height) {
208
209 FILE * ifP;
210 struct pam outpam;
211 tuple * tuplerow;
212 unsigned int row;
213
214 outpam.size = sizeof(struct pam);
215 outpam.len = PAM_STRUCT_SIZE(allocation_depth);
216 outpam.file = stdout;
217 outpam.format = PAM_FORMAT;
218 outpam.plainformat = 0;
219 outpam.width = width;
220 outpam.height = height;
221 outpam.depth = 3;
222 outpam.maxval = 255;
223 outpam.bytes_per_sample = 1;
224 strcpy(outpam.tuple_type, PAM_PPM_TUPLETYPE);
225 outpam.allocation_depth = 3;
226
227 ifP = pm_openr(fileName);
228
229 pnm_writepaminit(&outpam);
230
231 tuplerow = pnm_allocpamrow(&outpam);
232
233 for (row = 0; row < outpam.height; ++row) {
234 unsigned int col;
235
236 for (col = 0; col < outpam.width; col += 2) {
237 Yuy2Pixel const yuy2 = readPixel(ifP);
238
239 Coeff const coeff = coeffFromYuy2(yuy2);
240
241 rgbToTuple(rgbFromCoeff0(coeff), tuplerow[col]);
242 rgbToTuple(rgbFromCoeff1(coeff), tuplerow[col+1]);
243 }
244 pnm_writepamrow(&outpam, tuplerow);
245 }
246 pnm_freepamrow(tuplerow);
247
248 pm_closer(ifP);
249 }
250
251
252
253 int
254 main(int argc, const char *argv[]) {
255
256 struct CmdlineInfo cmdline;
257
258 pm_proginit(&argc, argv);
259
260 parseCommandLine(argc, argv, &cmdline);
261
262 yuy2topam(cmdline.inputFileName, cmdline.width, cmdline.height);
263
264 return 0;
265 }
266
267
268