about summary refs log tree commit diff
path: root/converter/ppm/vidtoppm.c
blob: f3c20404241a8b888b954029336f2bcfd53a5910 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Bryan got this from mm.ftp-cs.berkeley.edu from the package
   mpeg-encode-1.5b-src under the name vidtoppm.c on March 30, 2000.  
   The file was dated January 19, 1995.  

   This program does not use the netpbm libraries, but generates its
   output via the program rawtoppm.  If any work is ever done on it
   (or, more to the point, any interest ever expressed in it), it
   should be converted just to call ppmwrite(), etc. directly.

   There was no attached documentation, but the program appears to 
   convert from Parallax XVideo JPEG format to a sequence of PPM files.  It
   does this conversion by putting each frame in a window and then 
   reading it out of the window, using libXvid.

   Because it requires special libraries and there is no known
   requirement for it today, we are not including this program in the
   standard netpbm build.  But the source code is here in case someone
   is interested in it later.

*/

/*
 * Copyright (c) 1995 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

/* gcc -o playone playone.c -lX11 -lXvid -I/n/picasso/project/mm/xvideo/include
 */
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <XPlxExt.h>

#include "ppm.h"

usage (p)
char *p;

{
    fprintf (stderr, "Usage: %s filename width height start end outbase [quality]\n", p);
    exit (1);
}

static char buffer[300000];

Visual *
FindFullColorVisual (dpy, depth)
    Display *dpy;
    int *depth;
{
  XVisualInfo vinfo;
  XVisualInfo *vinfo_ret;
  int numitems, maxdepth;
  
  vinfo.class = TrueColor;
  
  vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
  
  if (numitems == 0) return NULL;

  maxdepth = 0;
  while(numitems > 0) {
    if (vinfo_ret[numitems-1].depth > maxdepth) {
      maxdepth = vinfo_ret[numitems-1 ].depth;
    }
    numitems--;
  }
  XFree(vinfo_ret);

  if (maxdepth < 24) return NULL;

  if (XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, 
		       TrueColor, &vinfo)) {
    *depth = maxdepth;
    return vinfo.visual;
  }
  
  return NULL;
}

Window
CreateFullColorWindow (dpy, x, y, w, h)
    Display *dpy;
    int x, y, w, h;
{
    int depth;
    Visual *visual;
    XSetWindowAttributes xswa;
    unsigned int mask;
    unsigned int class;
    int screen;

    screen = XDefaultScreen(dpy);
    class = InputOutput;	/* Could be InputOnly */
    visual = FindFullColorVisual (dpy, &depth);
    if (visual == NULL) {
	return 0;
    }
    mask = CWBackPixel | CWColormap | CWBorderPixel;
    xswa.colormap = XCreateColormap(dpy, XRootWindow(dpy, screen),
		    visual, AllocNone);
    xswa.background_pixel = BlackPixel(dpy, DefaultScreen(dpy));
    xswa.border_pixel = WhitePixel(dpy, DefaultScreen(dpy));

    return XCreateWindow(dpy, RootWindow(dpy, screen), x, y, w, h,
	1, depth, class, visual, mask, &xswa);
}

main (argc, argv)
int argc;
char **argv;

{
  char *filename;
  Display *dpy;
  int screen;
  Window root;
  XEvent event;
  GC gc;
  Window win;
  XPlxCImage image;
  int size;
  char *qTable;
  FILE *inFile;
  FILE *outFile;
  extern char *malloc();
  int fd, r1, r2, i, j, r;
  int quality;
  int start, end;
  XImage *ximage;
  char *tdata;
  char *obase;
  char ofname[256];
  int height, width;
  char command[256];

  ppm_init(&argc, argv);

  if ((argc != 7) && (argc != 8))usage (argv[0]);
  filename = argv[1];

  width = atoi(argv[2]);
  height = atoi(argv[3]);

  start = atoi(argv[4]);
  end = atoi(argv[5]);

  if ((start < 1) || (end < start)) {
    perror ("Bad start and end values.");
    exit();
  }

  obase = argv[6];

  quality = 100;
  
  if (argc > 7)
    quality = atoi (argv[7]);
  
  dpy = XOpenDisplay (NULL);
  screen = DefaultScreen(dpy);
  root = DefaultRootWindow(dpy);
/*  gc = DefaultGC(dpy, screen); */
/*  win = XCreateSimpleWindow (dpy, root, 0, 0, width, height,
			     0, NULL, NULL);
*/
  win = CreateFullColorWindow(dpy, 0, 0, width+4, height+4);
  gc = XCreateGC(dpy, win, 0, NULL);

  if (!win ) {
    perror ("Unable to create window");
    exit(1);
  }

  XMapWindow (dpy, win);
  XSelectInput (dpy, win, ExposureMask |ButtonPressMask);

  size = MakeQTables(quality, &qTable);
  XPlxPutTable(dpy, win, gc, qTable, size, 0);
  XPlxPutTable(dpy, win, gc, qTable, size, 1);
  XPlxVideoTag (dpy, win, gc, PLX_VIDEO);
  
  inFile = fopen(filename, "rb");
  if (inFile == NULL) {
    perror (filename);
    exit (1);
  }
  fd = fileno(inFile);
  wait(2);

  for (i=0; i<start; i++) {
    if (read (fd, &image, sizeof(image)) != sizeof(image)) {
      perror("End of file.");
      exit();
    }
    image.data = buffer;
    if (read (fd, buffer, image.size) != image.size) {
      perror("End of file.");
      exit();
    }
  }
    
  for (i=start; i<=end; i++) {
    fprintf(stdout, "GRABBING FRAME %d\n", i);

    if (read (fd, &image, sizeof(image)) != sizeof(image)) {
      perror("End of file.");
      exit();
    }
    image.data = buffer;
    if (read (fd, buffer, image.size) != image.size) {
      perror("End of file.");
      exit();
    }
    
    XPlxPutCImage (dpy, win, gc, &image, 0, 0, image.width,
		   image.height, 0, 0, width+2, height+2, 1);

    XFlush(dpy);

    ximage = XGetImage(dpy, win, 0, 0, width, height, 0x00ffffff,
		       ZPixmap);
    
    if (i == 0) {
      fprintf(stderr, "Depth %d\n", ximage->depth);
      fprintf(stderr, "Height: %d Width: %d\n", height, width );
    }
    tdata = ximage->data;


    sprintf(ofname, "%s%d.ppm", obase, i);
    outFile = fopen("/tmp/foobar", "wb");
    if (!outFile) {
      perror("Couldn't open output file.");
    }

    for (r=0; r<height; r++) {
      for (j=0; j<width; j++) {
	fputc(*(tdata+3), outFile);
	fputc(*(tdata+2), outFile);
	fputc(*(tdata+1), outFile);
	tdata += 4;
      }
    }

    fclose(outFile);

    free(tdata);

    sprintf(command, "rawtoppm %d %d < /tmp/foobar > %s",
	    width, height, ofname);
    system(command);
  }
}