home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 9 / CD_ASCQ_09_1193.iso / news / 4441 / mpegcode / misc / xvideoto.c < prev    next >
C/C++ Source or Header  |  1993-01-01  |  6KB  |  252 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.  
  131.   if ((argc != 7) && (argc != 8))usage (argv[0]);
  132.   filename = argv[1];
  133.  
  134.   width = atoi(argv[2]);
  135.   height = atoi(argv[3]);
  136.  
  137.   start = atoi(argv[4]);
  138.   end = atoi(argv[5]);
  139.  
  140.   if ((start < 1) || (end < start)) {
  141.     perror ("Bad start and end values.");
  142.     exit();
  143.   }
  144.  
  145.   obase = argv[6];
  146.  
  147.   quality = 100;
  148.   
  149.   if (argc > 7)
  150.     quality = atoi (argv[7]);
  151.   
  152.   dpy = XOpenDisplay (NULL);
  153.   screen = DefaultScreen(dpy);
  154.   root = DefaultRootWindow(dpy);
  155. /*  gc = DefaultGC(dpy, screen); */
  156. /*  win = XCreateSimpleWindow (dpy, root, 0, 0, width, height,
  157.                  0, NULL, NULL);
  158. */
  159.   win = CreateFullColorWindow(dpy, 0, 0, width+4, height+4);
  160.   gc = XCreateGC(dpy, win, 0, NULL);
  161.  
  162.   if (!win ) {
  163.     perror ("Unable to create window");
  164.     exit(1);
  165.   }
  166.  
  167.   XMapWindow (dpy, win);
  168.   XSelectInput (dpy, win, ExposureMask |ButtonPressMask);
  169.  
  170.   size = MakeQTables(quality, &qTable);
  171.   XPlxPutTable(dpy, win, gc, qTable, size, 0);
  172.   XPlxPutTable(dpy, win, gc, qTable, size, 1);
  173.   XPlxVideoTag (dpy, win, gc, PLX_VIDEO);
  174.   
  175.   inFile = fopen(filename, "r");
  176.   if (inFile == NULL) {
  177.     perror (filename);
  178.     exit (1);
  179.   }
  180.   fd = fileno(inFile);
  181.   wait(2);
  182.  
  183.   for (i=0; i<start; i++) {
  184.     if (read (fd, &image, sizeof(image)) != sizeof(image)) {
  185.       perror("End of file.");
  186.       exit();
  187.     }
  188.     image.data = buffer;
  189.     if (read (fd, buffer, image.size) != image.size) {
  190.       perror("End of file.");
  191.       exit();
  192.     }
  193.   }
  194.     
  195.   for (i=start; i<=end; i++) {
  196.     fprintf(stdout, "GRABBING FRAME %d\n", i);
  197.  
  198.     if (read (fd, &image, sizeof(image)) != sizeof(image)) {
  199.       perror("End of file.");
  200.       exit();
  201.     }
  202.     image.data = buffer;
  203.     if (read (fd, buffer, image.size) != image.size) {
  204.       perror("End of file.");
  205.       exit();
  206.     }
  207.     
  208.     XPlxPutCImage (dpy, win, gc, &image, 0, 0, image.width,
  209.            image.height, 0, 0, width+2, height+2, 1);
  210.  
  211.     XFlush(dpy);
  212.  
  213.     ximage = XGetImage(dpy, win, 0, 0, width, height, 0x00ffffff,
  214.                ZPixmap);
  215.     
  216.     if (i == 0) {
  217.       fprintf(stderr, "Depth %d\n", ximage->depth);
  218.       fprintf(stderr, "Height: %d Width: %d\n", height, width );
  219.     }
  220.     tdata = ximage->data;
  221.  
  222.  
  223.     sprintf(ofname, "%s.%d.jpeg", obase, i);
  224.     outFile = fopen("/tmp/foobar", "w");
  225.     if (!outFile) {
  226.       perror("Couldn't open output file.");
  227.     }
  228.  
  229.     for (r=0; r<height; r++) {
  230.       for (j=0; j<width; j++) {
  231.     fputc(*(tdata+3), outFile);
  232.     fputc(*(tdata+2), outFile);
  233.     fputc(*(tdata+1), outFile);
  234.     tdata += 4;
  235.       }
  236.     }
  237.  
  238.     fclose(outFile);
  239.  
  240.     free(tdata);
  241.  
  242.     sprintf(command, "rawtoppm %d %d < /tmp/foobar | cjpeg > %s",
  243.         width, height, ofname);
  244.     system(command);
  245.   }
  246. }
  247.  
  248.  
  249.  
  250.  
  251.  
  252.