home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / amiga / mpeg / mpgplyr1.lha / src / gdith.c < prev    next >
C/C++ Source or Header  |  1992-12-08  |  11KB  |  521 lines

  1. /*
  2.  * Copyright (c) 1992 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. #include <math.h>
  22. #include "video.h"
  23. #include "dither.h"
  24.  
  25. /* Array that remaps color numbers to actual pixel values used by X server. */
  26.  
  27. unsigned char pixel[256];
  28.  
  29. /* Arrays holding quantized value ranged for lum, cr, and cb. */
  30.  
  31. int lum_values[LUM_RANGE];
  32. int cr_values[CR_RANGE];
  33. int cb_values[CB_RANGE];
  34.  
  35. /* Declaration of global variable containing dither type. */
  36.  
  37. extern int ditherType;
  38.  
  39. /* Structures used by the X server. */
  40.  
  41. Display *display;
  42.  
  43. static XImage *ximage = NULL;
  44. static Colormap cmap;
  45. static Window window;
  46. static GC gc;
  47.  
  48.  
  49.  
  50. /*
  51.  *--------------------------------------------------------------
  52.  *
  53.  * InitColor --
  54.  *
  55.  *    Initialized lum, cr, and cb quantized range value arrays.
  56.  *
  57.  * Results: 
  58.  *      None.
  59.  *
  60.  * Side effects:
  61.  *      None.
  62.  *
  63.  *--------------------------------------------------------------
  64.  */
  65.  
  66. void
  67. InitColor()
  68. {
  69.   int i;
  70.  
  71.   for (i=0; i<LUM_RANGE; i++) {
  72.     lum_values[i] = ((i * 256) / (LUM_RANGE)) + (256/(LUM_RANGE*2));
  73.   }
  74.  
  75.   for (i=0; i<CR_RANGE; i++) {
  76.     cr_values[i] = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2));
  77.   }
  78.  
  79.   for (i=0; i<CB_RANGE; i++) {
  80.     cb_values[i] = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2));
  81.   }
  82.  
  83. }
  84.  
  85.  
  86. /*
  87.  *--------------------------------------------------------------
  88.  *
  89.  * ConvertColor --
  90.  *
  91.  *    Given a l, cr, cb tuple, converts it to r,g,b.
  92.  *
  93.  * Results:
  94.  *    r,g,b values returned in pointers passed as parameters.
  95.  *
  96.  * Side effects:
  97.  *      None.
  98.  *
  99.  *--------------------------------------------------------------
  100.  */
  101.  
  102. static void
  103. ConvertColor(l, cr, cb, r, g, b)
  104.      unsigned char l, cr, cb;
  105.      unsigned char *r, *g, *b;
  106. {
  107.   double fl, fcr, fcb, fr, fg, fb;
  108.  
  109.   fl = (double) l;
  110.   fcr =  ((double) cr) - 128.0;
  111.   fcb =  ((double) cb) - 128.0;
  112.  
  113.  
  114.   fr = fl + (1.40200 * fcb);
  115.   fg = fl - (0.71414 * fcb) - (0.34414 * fcr);
  116.   fb = fl + (1.77200 * fcr);
  117.  
  118.   if (fr < 0.0) fr = 0.0;
  119.   else if (fr > 255.0) fr = 255.0;
  120.  
  121.   if (fg < 0.0) fg = 0.0;
  122.   else if (fg > 255.0) fg = 255.0;
  123.  
  124.   if (fb < 0.0) fb = 0.0;
  125.   else if (fb > 255.0) fb = 255.0;
  126.  
  127.   *r = (unsigned char) fr;
  128.   *g = (unsigned char) fg;
  129.   *b = (unsigned char) fb;
  130.  
  131. }
  132.  
  133. /* Array back mapping pixel value to color number. Used for debugging and dumping
  134.    purposes. 
  135. */
  136.  
  137. static char backpixel[256];
  138.  
  139. #ifdef SH_MEM
  140.  
  141. int gXErrorFlag = 0;
  142.  
  143. int HandleXError(dpy, event)
  144.      Display *dpy;
  145.      XErrorEvent *event;
  146. {
  147.   gXErrorFlag = 1;
  148.  
  149.   return 0;
  150. }
  151.  
  152. void InstallXErrorHandler()
  153. {
  154.   int HandleXError();
  155.  
  156.   XSetErrorHandler(HandleXError);
  157.   XFlush(display);
  158. }
  159.  
  160. void DeInstallXErrorHandler()
  161. {
  162.   XSetErrorHandler(NULL);
  163.   XFlush(display);
  164. }
  165. #endif
  166.  
  167.  
  168. /*
  169.  *--------------------------------------------------------------
  170.  *
  171.  * ResizeDisplay --
  172.  *
  173.  *    Resizes display window.
  174.  *
  175.  * Results:
  176.  *    None.
  177.  *
  178.  * Side effects:
  179.  *      None.
  180.  *
  181.  *--------------------------------------------------------------
  182.  */
  183.  
  184. void ResizeDisplay(w, h)
  185.      int w, h;
  186. {
  187.  
  188.   if (ditherType == NO_DITHER) return;
  189.  
  190.   XResizeWindow(display, window, w, h);
  191.   XFlush(display);
  192. }
  193.  
  194.  
  195. /*
  196.  *--------------------------------------------------------------
  197.  *
  198.  * MakeWindow --
  199.  *
  200.  *    Create X Window
  201.  *
  202.  * Results:
  203.  *    Read the code.
  204.  *
  205.  * Side effects:
  206.  *      None.
  207.  *
  208.  *--------------------------------------------------------------
  209.  */
  210.  
  211. static void 
  212. MakeWindow() 
  213. {
  214.   
  215.   XSizeHints hint;
  216.   unsigned int fg, bg;
  217.   char *hello = "MPEG Play";
  218.   int screen;
  219.   Window CreateFullColorWindow();
  220.  
  221.   if (ditherType == NO_DITHER) return;
  222.  
  223.   display = XOpenDisplay("");
  224.   if (display == NULL) {
  225.     fprintf(stderr, "Can not open display\n");
  226.     exit(-2);
  227.   }
  228.  
  229.   screen = DefaultScreen (display);
  230.   
  231.   /* Fill in hint structure */
  232.  
  233.   hint.x = 200;
  234.   hint.y = 300;
  235.   hint.width = 150;
  236.   hint.height = 150;
  237.   hint.flags = PPosition | PSize;
  238.   
  239.   /* Get some colors */
  240.   
  241.   bg = WhitePixel (display, screen);
  242.   fg = BlackPixel (display, screen);
  243.   
  244.   /* Make the window */
  245.   
  246.   if (ditherType == FULL_COLOR_DITHER) {
  247.     window = CreateFullColorWindow (display, hint.x, hint.y, hint.width, hint.height);
  248.     if (window == 0) {
  249.       fprintf (stderr, "-color option only valid on full color display\n");
  250.       exit (-1);
  251.     }
  252.   } else if (ditherType == MONO_DITHER || ditherType == MONO_THRESHOLD) {
  253.     window = XCreateSimpleWindow (display,
  254.                   DefaultRootWindow (display),
  255.                   hint.x, hint.y,
  256.                   hint.width, hint.height,
  257.                   4, fg, bg);
  258.   } else {
  259.     XVisualInfo vinfo;
  260.     
  261.     if (!XMatchVisualInfo (display, screen, 8, PseudoColor, 
  262.                &vinfo)) {
  263.  
  264.       if (!XMatchVisualInfo(display, screen, 8, GrayScale, 
  265.                 &vinfo)) {
  266.  
  267.     fprintf(stderr, "-requires 8 bit display\n");
  268.     exit(-1);
  269.       }
  270.     }
  271.  
  272.     window = XCreateSimpleWindow (display,
  273.                  DefaultRootWindow (display),
  274.                  hint.x, hint.y,
  275.                  hint.width, hint.height,
  276.                  4, fg, bg);
  277.   }
  278.   
  279.   /* Tell other applications about this window */
  280.   
  281.   XSetStandardProperties (display, window, hello, hello, None, NULL, 0, &hint);
  282.   
  283.   /* Map window. */
  284.  
  285.   XMapWindow(display, window);
  286. }
  287.   
  288.  
  289. /*
  290.  *--------------------------------------------------------------
  291.  *
  292.  * InitDisplay --
  293.  *
  294.  *    Initialized display, sets up colormap, etc.
  295.  *
  296.  * Results:
  297.  *      None.
  298.  *
  299.  * Side effects:
  300.  *      None.
  301.  *
  302.  *--------------------------------------------------------------
  303.  */
  304.  
  305. void InitDisplay()
  306. {
  307.  
  308.   int ncolors = LUM_RANGE*CB_RANGE*CR_RANGE;
  309.   XColor xcolor;
  310.   int i, lum_num, cr_num, cb_num;
  311.   unsigned char r, g, b;
  312.  
  313.   if (ditherType == NO_DITHER) return;
  314.  
  315.   MakeWindow();
  316.  
  317.   gc = XCreateGC(display, window, 0, 0);
  318.  
  319.   cmap = XDefaultColormap(display, DefaultScreen(display));
  320.  
  321.   xcolor.flags = DoRed | DoGreen | DoBlue;
  322.  
  323.   for (i=0; i<ncolors; i++) {
  324.  
  325.     lum_num = (i / (CR_RANGE*CB_RANGE))%LUM_RANGE;
  326.     cr_num = (i / CB_RANGE)%CR_RANGE;
  327.     cb_num = i % CB_RANGE;
  328.  
  329.     ConvertColor(lum_values[lum_num], cr_values[cr_num], cb_values[cb_num], &r, &g, &b);
  330.  
  331.     xcolor.red = r * 256;
  332.     xcolor.green = g * 256;
  333.     xcolor.blue = b * 256;
  334.  
  335.     XAllocColor(display, cmap, &xcolor);
  336.     pixel[i] = xcolor.pixel;
  337.     backpixel[xcolor.pixel] = i;
  338.   }
  339.  
  340.   ximage = NULL;
  341. }
  342.  
  343.  
  344. /*
  345.  *--------------------------------------------------------------
  346.  *
  347.  * InitGrayDisplay --
  348.  *
  349.  *    Initialized display for gray scale dither.
  350.  *
  351.  * Results:
  352.  *      None.
  353.  *
  354.  * Side effects:
  355.  *      None.
  356.  *
  357.  *--------------------------------------------------------------
  358.  */
  359.  
  360. #define NUM_COLORS 128
  361.  
  362. void InitGrayDisplay()
  363. {
  364.   int ncolors = NUM_COLORS;
  365.   XColor xcolor;
  366.   int i;
  367.  
  368.   MakeWindow();
  369.  
  370.   gc = XCreateGC(display, window, 0, 0);
  371.  
  372.   cmap = XDefaultColormap(display, DefaultScreen(display));
  373.  
  374.   xcolor.flags = DoRed | DoGreen | DoBlue;
  375.  
  376.   for (i=0; i<ncolors; i++) {
  377.  
  378.     xcolor.red = (i*2) * 256;
  379.     xcolor.green = (i*2) * 256;
  380.     xcolor.blue = (i*2) * 256;
  381.  
  382.     XAllocColor(display, cmap, &xcolor);
  383.     pixel[(i*2)] = xcolor.pixel;
  384.     pixel[(i*2)+1] = xcolor.pixel;
  385.     backpixel[xcolor.pixel] = i;
  386.   }
  387.  
  388.   ximage = NULL;
  389. }
  390.  
  391.  
  392. /*
  393.  *--------------------------------------------------------------
  394.  *
  395.  * InitMonoDisplay --
  396.  *
  397.  *    Initialized display for monochrome dither.
  398.  *
  399.  * Results:
  400.  *      None.
  401.  *
  402.  * Side effects:
  403.  *      None.
  404.  *
  405.  *--------------------------------------------------------------
  406.  */
  407.  
  408. void InitMonoDisplay()
  409. {
  410.   XGCValues xgcv;
  411.  
  412.   MakeWindow();
  413.  
  414.   xgcv.background = BlackPixel(display, DefaultScreen(display));
  415.   xgcv.foreground = WhitePixel(display, DefaultScreen(display));
  416.  
  417.   gc = XCreateGC(display, window, GCForeground | GCBackground, &xgcv);
  418.  
  419.   ximage = NULL;
  420. }
  421.  
  422.  
  423. /*
  424.  *--------------------------------------------------------------
  425.  *
  426.  * InitColorDisplay --
  427.  *
  428.  *    Initialized display for full color output.
  429.  *
  430.  * Results:
  431.  *      None.
  432.  *
  433.  * Side effects:
  434.  *      None.
  435.  *
  436.  *--------------------------------------------------------------
  437.  */
  438.  
  439. void InitColorDisplay()
  440. {
  441.  
  442.   MakeWindow();
  443.  
  444.   gc = XCreateGC(display, window, 0, 0);
  445.   ximage = NULL;
  446. }
  447.  
  448.  
  449. /*
  450.  *--------------------------------------------------------------
  451.  *
  452.  * ExecuteDisplay --
  453.  *
  454.  *    Actually displays display plane in previously created window.
  455.  *
  456.  * Results:
  457.  *    None.
  458.  *
  459.  * Side effects:
  460.  *    None.
  461.  *
  462.  *--------------------------------------------------------------
  463.  */
  464.  
  465. void
  466. ExecuteDisplay(vid_stream)
  467.      VidStream *vid_stream;
  468. {
  469.   char dummy;
  470.   Visual *FindFullColorVisual();
  471.   Visual *fc_visual;
  472.   int depth;
  473.  
  474.   totNumFrames++;
  475.   fprintf (stderr, "%d\r", totNumFrames);
  476.  
  477.   if (ditherType == NO_DITHER) return;
  478.  
  479.   if (ximage == NULL) {
  480.  
  481.     if (ditherType == Twox2_DITHER) {
  482.       ximage = XCreateImage(display, None, 8, ZPixmap, 0, &dummy,
  483.                 vid_stream->mb_width * 32,
  484.                 vid_stream->mb_height * 32, 8, 0);
  485.     } else if (ditherType == FULL_COLOR_DITHER) {
  486.       fc_visual = FindFullColorVisual(display, &depth);
  487.       ximage = XCreateImage (display, fc_visual, depth, ZPixmap,
  488.                  0, &dummy, vid_stream->mb_width * 16,
  489.                  vid_stream->mb_height * 16, 32, 0);
  490.     } else if (ditherType == MONO_DITHER || ditherType == MONO_THRESHOLD) {
  491.       ximage = XCreateImage (display, None, 1, XYBitmap, 0, &dummy,
  492.                  vid_stream->mb_width * 16,
  493.                  vid_stream->mb_height * 16, 8, 0);
  494.     } else {
  495.       ximage = XCreateImage(display, None, 8, ZPixmap, 0, &dummy,
  496.                 vid_stream->mb_width * 16,
  497.                 vid_stream->mb_height * 16, 8, 0);
  498.     }
  499.   }
  500.  
  501. #ifdef SH_MEM
  502.   if (shmemFlag) {
  503.     XShmPutImage(display, window, gc, vid_stream->current->ximage, 
  504.          0, 0, 0, 0,
  505.          vid_stream->current->ximage->width, 
  506.          vid_stream->current->ximage->height, False);
  507.     XFlush(display);
  508.  
  509.   }
  510.   else 
  511. #endif
  512.  
  513.     {
  514.       ximage->data = (char *) vid_stream->current->display; 
  515.  
  516.       XPutImage(display, window, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
  517.     }
  518. }
  519.  
  520.  
  521.