home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 9 / CD_ASCQ_09_1193.iso / news / 4441 / mpegcode / misc / playone.c < prev    next >
C/C++ Source or Header  |  1993-01-01  |  6KB  |  246 lines

  1. /*
  2.  * Copyright (c) 1993 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  *
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. /* gcc -o playone playone.c -lX11 -lXvid -I/n/picasso/project/mm/xvideo/include
  23.  */
  24. #include <stdio.h>
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. #include <XPlxExt.h>
  28.  
  29. usage (p)
  30. char *p;
  31.  
  32. {
  33.     fprintf (stderr, "Usage: %s filename width height start end outbase [quality]\n", p);
  34.     exit (1);
  35. }
  36.  
  37. static char buffer[300000];
  38.  
  39. Visual *
  40. FindFullColorVisual (dpy, depth)
  41.     Display *dpy;
  42.     int *depth;
  43. {
  44.   XVisualInfo vinfo;
  45.   XVisualInfo *vinfo_ret;
  46.   int numitems, maxdepth;
  47.   
  48.   vinfo.class = TrueColor;
  49.   
  50.   vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
  51.   
  52.   if (numitems == 0) return NULL;
  53.  
  54.   maxdepth = 0;
  55.   while(numitems > 0) {
  56.     if (vinfo_ret[numitems-1].depth > maxdepth) {
  57.       maxdepth = vinfo_ret[numitems-1 ].depth;
  58.     }
  59.     numitems--;
  60.   }
  61.   XFree(vinfo_ret);
  62.  
  63.   if (maxdepth < 24) return NULL;
  64.  
  65.   if (XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, 
  66.                TrueColor, &vinfo)) {
  67.     *depth = maxdepth;
  68.     return vinfo.visual;
  69.   }
  70.   
  71.   return NULL;
  72. }
  73.  
  74. Window
  75. CreateFullColorWindow (dpy, x, y, w, h)
  76.     Display *dpy;
  77.     int x, y, w, h;
  78. {
  79.     int depth;
  80.     Visual *visual;
  81.     XSetWindowAttributes xswa;
  82.     unsigned int mask;
  83.     unsigned int class;
  84.     int screen;
  85.  
  86.     screen = XDefaultScreen(dpy);
  87.     class = InputOutput;    /* Could be InputOnly */
  88.     visual = FindFullColorVisual (dpy, &depth);
  89.     if (visual == NULL) {
  90.     return 0;
  91.     }
  92.     mask = CWBackPixel | CWColormap | CWBorderPixel;
  93.     xswa.colormap = XCreateColormap(dpy, XRootWindow(dpy, screen),
  94.             visual, AllocNone);
  95.     xswa.background_pixel = BlackPixel(dpy, DefaultScreen(dpy));
  96.     xswa.border_pixel = WhitePixel(dpy, DefaultScreen(dpy));
  97.  
  98.     return XCreateWindow(dpy, RootWindow(dpy, screen), x, y, w, h,
  99.     1, depth, class, visual, mask, &xswa);
  100. }
  101.  
  102. main (argc, argv)
  103. int argc;
  104. char **argv;
  105.  
  106. {
  107.   char *filename;
  108.   Display *dpy;
  109.   int screen;
  110.   Window root;
  111.   XEvent event;
  112.   GC gc;
  113.   Window win;
  114.   XPlxCImage image;
  115.   int size;
  116.   char *qTable;
  117.   FILE *inFile;
  118.   FILE *outFile;
  119.   extern char *malloc();
  120.   int fd, r1, r2, i, j, r;
  121.   int quality;
  122.   int start, end;
  123.   XImage *ximage;
  124.   char *tdata;
  125.   char *obase;
  126.   char ofname[256];
  127.   int height, width;
  128.   char command[256];
  129.  
  130.   if ((argc != 7) && (argc != 8))usage (argv[0]);
  131.   filename = argv[1];
  132.  
  133.   width = atoi(argv[2]);
  134.   height = atoi(argv[3]);
  135.  
  136.   start = atoi(argv[4]);
  137.   end = atoi(argv[5]);
  138.  
  139.   if ((start < 1) || (end < start)) {
  140.     perror ("Bad start and end values.");
  141.     exit();
  142.   }
  143.  
  144.   obase = argv[6];
  145.  
  146.   quality = 100;
  147.   
  148.   if (argc > 7)
  149.     quality = atoi (argv[7]);
  150.   
  151.   dpy = XOpenDisplay (NULL);
  152.   screen = DefaultScreen(dpy);
  153.   root = DefaultRootWindow(dpy);
  154. /*  gc = DefaultGC(dpy, screen); */
  155. /*  win = XCreateSimpleWindow (dpy, root, 0, 0, width, height,
  156.                  0, NULL, NULL);
  157. */
  158.   win = CreateFullColorWindow(dpy, 0, 0, width+4, height+4);
  159.   gc = XCreateGC(dpy, win, 0, NULL);
  160.  
  161.   if (!win ) {
  162.     perror ("Unable to create window");
  163.     exit(1);
  164.   }
  165.  
  166.   XMapWindow (dpy, win);
  167.   XSelectInput (dpy, win, ExposureMask |ButtonPressMask);
  168.  
  169.   size = MakeQTables(quality, &qTable);
  170.   XPlxPutTable(dpy, win, gc, qTable, size, 0);
  171.   XPlxPutTable(dpy, win, gc, qTable, size, 1);
  172.   XPlxVideoTag (dpy, win, gc, PLX_VIDEO);
  173.   
  174.   inFile = fopen(filename, "r");
  175.   if (inFile == NULL) {
  176.     perror (filename);
  177.     exit (1);
  178.   }
  179.   fd = fileno(inFile);
  180.   wait(2);
  181.  
  182.   for (i=0; i<start; i++) {
  183.     if (read (fd, &image, sizeof(image)) != sizeof(image)) {
  184.       perror("End of file.");
  185.       exit();
  186.     }
  187.     image.data = buffer;
  188.     if (read (fd, buffer, image.size) != image.size) {
  189.       perror("End of file.");
  190.       exit();
  191.     }
  192.   }
  193.     
  194.   for (i=start; i<=end; i++) {
  195.     fprintf(stdout, "GRABBING FRAME %d\n", i);
  196.  
  197.     if (read (fd, &image, sizeof(image)) != sizeof(image)) {
  198.       perror("End of file.");
  199.       exit();
  200.     }
  201.     image.data = buffer;
  202.     if (read (fd, buffer, image.size) != image.size) {
  203.       perror("End of file.");
  204.       exit();
  205.     }
  206.     
  207.     XPlxPutCImage (dpy, win, gc, &image, 0, 0, image.width,
  208.            image.height, 0, 0, width+2, height+2, 1);
  209.  
  210.     XFlush(dpy);
  211.  
  212.     ximage = XGetImage(dpy, win, 0, 0, width, height, 0x00ffffff,
  213.                ZPixmap);
  214.     
  215.     if (i == 0) {
  216.       fprintf(stderr, "Depth %d\n", ximage->depth);
  217.       fprintf(stderr, "Height: %d Width: %d\n", height, width );
  218.     }
  219.     tdata = ximage->data;
  220.  
  221.  
  222.     sprintf(ofname, "%s%d.ppm", obase, i);
  223.     outFile = fopen("/tmp/foobar", "w");
  224.     if (!outFile) {
  225.       perror("Couldn't open output file.");
  226.     }
  227.  
  228.     for (r=0; r<height; r++) {
  229.       for (j=0; j<width; j++) {
  230.     fputc(*(tdata+3), outFile);
  231.     fputc(*(tdata+2), outFile);
  232.     fputc(*(tdata+1), outFile);
  233.     tdata += 4;
  234.       }
  235.     }
  236.  
  237.     fclose(outFile);
  238.  
  239.     free(tdata);
  240.  
  241.     sprintf(command, "rawtoppm %d %d < /tmp/foobar > %s",
  242.         width, height, ofname);
  243.     system(command);
  244.   }
  245. }
  246.