Debian Codegraph @

converter/ppm/vidtoppm.c

raw symbols SCIP
1 /* Bryan got this from mm.ftp-cs.berkeley.edu from the package
2 mpeg-encode-1.5b-src under the name vidtoppm.c on March 30, 2000.
3 The file was dated January 19, 1995.
4
5 This program does not use the netpbm libraries, but generates its
6 output via the program rawtoppm. If any work is ever done on it
7 (or, more to the point, any interest ever expressed in it), it
8 should be converted just to call ppmwrite(), etc. directly.
9
10 There was no attached documentation, but the program appears to
11 convert from Parallax XVideo JPEG format to a sequence of PPM files. It
12 does this conversion by putting each frame in a window and then
13 reading it out of the window, using libXvid.
14
15 Because it requires special libraries and there is no known
16 requirement for it today, we are not including this program in the
17 standard netpbm build. But the source code is here in case someone
18 is interested in it later.
19
20 Bryan added the 'squote' function and its use to generate the shell command
21 because without it, the shell command can do arbitrary things if the output
22 filename base argument contains shell metacharacters. This is for form
23 only, since the program is not being built. This bug was noticed by
24 someone scanning the entire source tree for bugs. 25.04.06.
25 */
26
27 /*
28 * Copyright (c) 1995 The Regents of the University of California.
29 * All rights reserved.
30 *
31 * Permission to use, copy, modify, and distribute this software and its
32 * documentation for any purpose, without fee, and without written agreement is
33 * hereby granted, provided that the above copyright notice and the following
34 * two paragraphs appear in all copies of this software.
35 *
36 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
37 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
38 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
39 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
42 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
43 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
44 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
45 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
46 */
47
48 /* gcc -o playone playone.c -lX11 -lXvid -I/n/picasso/project/mm/xvideo/include
49 */
50 #include <stdio.h>
51 #include <X11/Xlib.h>
52 #include <X11/Xutil.h>
53 #include <XPlxExt.h>
54
55 #include "ppm.h"
56 #include "mallocvar.h"
57
58 usage (p)
59 char *p;
60
61 {
62 fprintf (stderr, "Usage: %s filename width height start end outbase [quality]\n", p);
63 exit (1);
64 }
65
66
67
68 static char buffer[300000];
69
70 static const char *
71 squote(const char * const arg) {
72 /*----------------------------------------------------------------------------
73 'arg' escaped so that it forms a single safe token in a shell command.
74
75 E.g.
76
77 arg return value
78 --- ------------
79 foo 'foo'
80 don't 'don'\\''t'
81
82 Value is in newly malloc'ed storage.
83
84 Abort the program if we can't allocate storage.
85 -----------------------------------------------------------------------------*/
86 size_t const maxEscapedLen = strlen(arg)*4 + 3;
87 /* 4 characters for every apostrophe, plus opening and closing
88 apostrophes, plus NUL terminator
89 */
90
91 char * buffer;
92 unsigned int inCursor;
93 unsigned int outCursor;
94
95 MALLOCARRAY_NOFAIL(buffer, maxEscapedLen);
96
97 outCursor = 0; /* initial value */
98
99 buffer[outCursor++] = '\''; /* opening quote */
100
101 for (inCursor = 0; arg[inCursor]; ++inCursor) {
102 if (arg[inCursor] == '\'') {
103 buffer[outCursor++] = '\'';
104 buffer[outCursor++] = '\\';
105 buffer[outCursor++] = '\'';
106 buffer[outCursor++] = '\'';
107 } else {
108 buffer[outCursor++] = arg[inCursor];
109 }
110 }
111 buffer[outCursor++] = '\''; /* closing quote */
112
113 buffer[outCursor++] = '\0'; /* terminating NUL */
114
115 return buffer;
116 }
117
118
119
120 Visual *
121 FindFullColorVisual (dpy, depth)
122 Display *dpy;
123 int *depth;
124 {
125 XVisualInfo vinfo;
126 XVisualInfo *vinfo_ret;
127 int numitems, maxdepth;
128
129 vinfo.class = TrueColor;
130
131 vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
132
133 if (numitems == 0) return NULL;
134
135 maxdepth = 0;
136 while(numitems > 0) {
137 if (vinfo_ret[numitems-1].depth > maxdepth) {
138 maxdepth = vinfo_ret[numitems-1 ].depth;
139 }
140 numitems--;
141 }
142 XFree(vinfo_ret);
143
144 if (maxdepth < 24) return NULL;
145
146 if (XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth,
147 TrueColor, &vinfo)) {
148 *depth = maxdepth;
149 return vinfo.visual;
150 }
151
152 return NULL;
153 }
154
155
156
157 Window
158 CreateFullColorWindow (dpy, x, y, w, h)
159 Display *dpy;
160 int x, y, w, h;
161 {
162 int depth;
163 Visual *visual;
164 XSetWindowAttributes xswa;
165 unsigned int mask;
166 unsigned int class;
167 int screen;
168
169 screen = XDefaultScreen(dpy);
170 class = InputOutput; /* Could be InputOnly */
171 visual = FindFullColorVisual (dpy, &depth);
172 if (visual == NULL) {
173 return 0;
174 }
175 mask = CWBackPixel | CWColormap | CWBorderPixel;
176 xswa.colormap = XCreateColormap(dpy, XRootWindow(dpy, screen),
177 visual, AllocNone);
178 xswa.background_pixel = BlackPixel(dpy, DefaultScreen(dpy));
179 xswa.border_pixel = WhitePixel(dpy, DefaultScreen(dpy));
180
181 return XCreateWindow(dpy, RootWindow(dpy, screen), x, y, w, h,
182 1, depth, class, visual, mask, &xswa);
183 }
184
185
186
187 main (argc, argv)
188 int argc;
189 char **argv;
190
191 {
192 char *filename;
193 Display *dpy;
194 int screen;
195 Window root;
196 XEvent event;
197 GC gc;
198 Window win;
199 XPlxCImage image;
200 int size;
201 char *qTable;
202 FILE *inFile;
203 FILE *outFile;
204 extern char *malloc();
205 int fd, r1, r2, i, j, r;
206 int quality;
207 int start, end;
208 XImage *ximage;
209 char *tdata;
210 char *obase;
211 char ofname[256];
212 int height, width;
213 char command[256];
214
215 ppm_init(&argc, argv);
216
217 if ((argc != 7) && (argc != 8))usage (argv[0]);
218 filename = argv[1];
219
220 width = atoi(argv[2]);
221 height = atoi(argv[3]);
222
223 start = atoi(argv[4]);
224 end = atoi(argv[5]);
225
226 if ((start < 1) || (end < start)) {
227 perror ("Bad start and end values.");
228 exit();
229 }
230
231 obase = argv[6];
232
233 quality = 100;
234
235 if (argc > 7)
236 quality = atoi (argv[7]);
237
238 dpy = XOpenDisplay (NULL);
239 screen = DefaultScreen(dpy);
240 root = DefaultRootWindow(dpy);
241 /* gc = DefaultGC(dpy, screen); */
242 /* win = XCreateSimpleWindow (dpy, root, 0, 0, width, height,
243 0, NULL, NULL);
244 */
245 win = CreateFullColorWindow(dpy, 0, 0, width+4, height+4);
246 gc = XCreateGC(dpy, win, 0, NULL);
247
248 if (!win ) {
249 perror ("Unable to create window");
250 exit(1);
251 }
252
253 XMapWindow (dpy, win);
254 XSelectInput (dpy, win, ExposureMask |ButtonPressMask);
255
256 size = MakeQTables(quality, &qTable);
257 XPlxPutTable(dpy, win, gc, qTable, size, 0);
258 XPlxPutTable(dpy, win, gc, qTable, size, 1);
259 XPlxVideoTag (dpy, win, gc, PLX_VIDEO);
260
261 inFile = fopen(filename, "rb");
262 if (inFile == NULL) {
263 perror (filename);
264 exit (1);
265 }
266 fd = fileno(inFile);
267 wait(2);
268
269 for (i=0; i<start; i++) {
270 if (read (fd, &image, sizeof(image)) != sizeof(image)) {
271 perror("End of file.");
272 exit();
273 }
274 image.data = buffer;
275 if (read (fd, buffer, image.size) != image.size) {
276 perror("End of file.");
277 exit();
278 }
279 }
280
281 for (i=start; i<=end; i++) {
282 fprintf(stdout, "GRABBING FRAME %d\n", i);
283
284 if (read (fd, &image, sizeof(image)) != sizeof(image)) {
285 perror("End of file.");
286 exit();
287 }
288 image.data = buffer;
289 if (read (fd, buffer, image.size) != image.size) {
290 perror("End of file.");
291 exit();
292 }
293
294 XPlxPutCImage (dpy, win, gc, &image, 0, 0, image.width,
295 image.height, 0, 0, width+2, height+2, 1);
296
297 XFlush(dpy);
298
299 ximage = XGetImage(dpy, win, 0, 0, width, height, 0x00ffffff,
300 ZPixmap);
301
302 if (i == 0) {
303 fprintf(stderr, "Depth %d\n", ximage->depth);
304 fprintf(stderr, "Height: %d Width: %d\n", height, width );
305 }
306 tdata = ximage->data;
307
308
309 sprintf(ofname, "%s%d.ppm", obase, i);
310 outFile = fopen("/tmp/foobar", "wb");
311 if (!outFile) {
312 perror("Couldn't open output file.");
313 }
314
315 for (r=0; r<height; r++) {
316 for (j=0; j<width; j++) {
317 fputc(*(tdata+3), outFile);
318 fputc(*(tdata+2), outFile);
319 fputc(*(tdata+1), outFile);
320 tdata += 4;
321 }
322 }
323
324 fclose(outFile);
325
326 free(tdata);
327
328 sprintf(command, "rawtoppm %d %d < /tmp/foobar > %s",
329 width, height, squote(ofname));
330 system(command);
331 }
332 }
333
334
335