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