Debian Codegraph @

converter/ppm/411toppm.c

raw symbols SCIP
1 /*
2 I recently had a visit from my mom who owns a Sony Mavica camera.
3 This camera produces standard MPEG and JPEG files, but it also
4 creates 64x48 pixel thumbnails for preview/index on its own tiny
5 LCD screen. These files are named with an extension that is
6 ".411".
7
8 Sony appears not to want to document the ".411" file format, but it
9 is clear from various web pages that it is a variant of the
10 CCIR.601 standard YUV encoding used in MPEG. The name indicates
11 that the file content consists of chunks of 6 bytes: 4 bytes of
12 image Y values, followed by 1 bytes of U and one byte of V values
13 that apply to the previous 4 Y pixel values.
14
15 There appear to be some commercial 411 file readers on the net, and
16 there is the Java-based Javica program, but I prefer Open Source
17 command-line utilities. So, I grabbed a copy of netpbm-9.11 from
18 SourceForge and hacked the eyuvtoppm.c file so that it looks like
19 this. While this may not be exactly the right thing to do, it
20 produces results which are close enough for me.
21
22 There are all sorts of user-interface gotchas possible in here that
23 I'm not going to bother changing -- especially not without actual
24 documentation from Sony about what they intend to do with ".411"
25 files in the future. I place my modifications into the public
26 domain, but I ask that my name & e-mail be mentioned in the
27 commentary of any derived version.
28
29 Steve Allen <[email protected]>, 2001-03-01
30
31 Bryan Henderson reworked the program to use the Netpbm libraries to
32 create the PPM output and follow some other Netpbm conventions.
33 2001-03-03. Bryan's contribution is public domain.
34 */
35 /*
36 * Copyright (c) 1995 The Regents of the University of California.
37 * All rights reserved.
38 *
39 * Permission to use, copy, modify, and distribute this software and its
40 * documentation for any purpose, without fee, and without written agreement is
41 * hereby granted, provided that the above copyright notice and the following
42 * two paragraphs appear in all copies of this software.
43 *
44 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
45 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
46 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
47 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 *
49 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
50 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
51 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
52 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
53 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */
54
55 /*==============*
56 * HEADER FILES *
57 *==============*/
58 #include <stdio.h>
59
60 #include "pm_c_util.h"
61 #include "mallocvar.h"
62 #include "shhopt.h"
63 #include "ppm.h"
64
65 typedef unsigned char uint8;
66
67 #define CHOP(x) ((x < 0) ? 0 : ((x > 255) ? 255 : x))
68
69 struct CmdlineInfo {
70 /* All the information the user supplied in the command line,
71 in a form easy for the program to use.
72 */
73 const char * inputFileName;
74 int width;
75 int height;
76 };
77
78
79
80 static void
81 parseCommandLine(int argc, const char ** argv,
82 struct CmdlineInfo *cmdlineP) {
83 /*----------------------------------------------------------------------------
84 Note that the file spec array we return is stored in the storage that
85 was passed to us as the argv array.
86 -----------------------------------------------------------------------------*/
87
88 optEntry * option_def;
89 optStruct3 opt;
90
91 unsigned int option_def_index;
92
93 MALLOCARRAY_NOFAIL(option_def, 100);
94
95 option_def_index = 0; /* incremented by OPTENT3 */
96 OPTENT3(0, "width", OPT_INT, &cmdlineP->width, NULL, 0);
97 OPTENT3(0, "height", OPT_INT, &cmdlineP->height, NULL, 0);
98
99 /* Set the defaults */
100 cmdlineP->width = 64;
101 cmdlineP->height = 48;
102
103 opt.opt_table = option_def;
104 opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
105 opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */
106
107 pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0);
108 /* Uses and sets argc, argv, and some of *cmdlineP and others. */
109
110 if (cmdlineP->width <= 0)
111 pm_error("-width must be positive.");
112 if (cmdlineP->width %4 != 0)
113 pm_error("-width must be a multiple of 4.");
114 if (cmdlineP->height <= 0)
115 pm_error("-height must be positive.");
116
117 if (argc > 2)
118 pm_error("There is at most 1 argument: the input file spec. "
119 "You supplied %d", argc-1);
120 else {
121 if (argc > 1)
122 cmdlineP->inputFileName = argv[1];
123 else
124 cmdlineP->inputFileName = "-";
125 }
126 free(option_def);
127 }
128
129
130
131 static void
132 ReadYUV(FILE * const ifP,
133 uint8 * const inbuff) {
134
135 size_t bytesRead;
136
137 bytesRead = fread(inbuff, 1, 6, ifP);
138
139 if (bytesRead != 6 ) {
140 if (feof(ifP))
141 pm_error("Premature end of input.");
142 else
143 pm_error("Error reading input.");
144 }
145 }
146
147
148
149 static void
150 YUVtoPPM(FILE * const ifP,
151 int const width,
152 int const height,
153 pixel * const pixrow ) {
154
155 unsigned int col;
156
157 for (col = 0; col < width; ++col) {
158
159 uint8 inbuff[6];
160
161 uint8 * const origY = &inbuff[0];
162 uint8 * const origCb = &inbuff[4];
163 uint8 * const origCr = &inbuff[5];
164 int y, u, v;
165 int32_t tempR, tempG, tempB;
166 pixval r, g, b;
167
168 if (col % 4 == 0) {
169 ReadYUV(ifP, inbuff);
170 u = origCb[0] - 128;
171 v = origCr[0] - 128;
172 }
173
174 y = origY[col % 4] - 16;
175
176 tempR = 104635 * v + y * 76310;
177 tempG = -25690 * u + -53294 * v + y * 76310;
178 tempB = 132278 * u + y * 76310;
179
180 r = CHOP((int)(tempR >> 16));
181 g = CHOP((int)(tempG >> 16));
182 b = CHOP((int)(tempB >> 16));
183
184 PPM_ASSIGN(pixrow[col], r, g, b);
185 }
186 }
187
188
189
190 int
191 main(int argc, const char **argv) {
192
193 pixval const maxval = 255;
194 struct CmdlineInfo cmdline;
195 FILE * ifP;
196 pixel * pixrow;
197 unsigned int row;
198
199 pm_proginit(&argc, argv);
200
201 parseCommandLine(argc, argv, &cmdline);
202
203 pixrow = ppm_allocrow(cmdline.width);
204
205 pm_message("Reading (%ux%u): '%s'", cmdline.width, cmdline.height,
206 cmdline.inputFileName);
207
208 ifP = pm_openr(cmdline.inputFileName);
209
210 ppm_writeppminit(stdout, cmdline.width, cmdline.height, maxval, 0);
211
212 for (row = 0; row < cmdline.height; ++row) {
213 YUVtoPPM(ifP, cmdline.width, cmdline.height, pixrow);
214 ppm_writeppmrow(stdout, pixrow, cmdline.width, maxval, 0);
215 }
216
217 if (fgetc(ifP) != EOF)
218 pm_message("Extraneous data at end of image.");
219
220 pm_close(ifP);
221 ppm_freerow(pixrow);
222
223 return 0;
224 }
225
226
227
228 /*
229 By default a .411 file is width=64, height=48, 4608 bytes.
230 There is no header.
231 */