home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / mpegplay.zip / 24BIT.C < prev    next >
C/C++ Source or Header  |  1993-02-02  |  5KB  |  219 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.  
  22. #include "video.h"
  23. #include "dither.h"
  24. #include "proto.h"
  25.  
  26. /*
  27.  * We'll define the "ConvertColor" macro here to do fixed point arithmetic
  28.  * that'll convert from YCrCb to RGB using:
  29.  *    R = L + 1.40200*Cr;
  30.  *    G = L - 0.34414*Cb - 0.71414*Cr
  31.  *    B = L + 1.77200*Cb;
  32.  *
  33.  * We'll use fixed point by adding two extra bits after the decimal.
  34.  */
  35.  
  36. #define BITS    8
  37. #define ONE     ((int) 1)
  38. #define CONST_SCALE    (ONE << BITS)
  39. #define ROUND_FACTOR    (ONE << (BITS-1))
  40.  
  41. /* Macro to convert integer to fixed. */
  42. #define UP(x)    (((int)(x)) << BITS)
  43.  
  44. /* Macro to convert fixed to integer (with rounding). */
  45. #define DOWN(x)    (((x) + ROUND_FACTOR) >> BITS)
  46.  
  47. /* Macro to convert a float to a fixed */
  48. #define FIX(x)  ((int) ((x)*CONST_SCALE + 0.5))
  49.  
  50. #define CLAMP(ll,x,ul)    ( ((x)<(ll)) ?(ll):( ((x)>(ul)) ?(ul):(x)))
  51.  
  52. static int *Cb_r_tab, *Cr_g_tab, *Cb_g_tab, *Cr_b_tab;
  53.  
  54. /*
  55.  *--------------------------------------------------------------
  56.  *
  57.  * InitColorDither --
  58.  *
  59.  *    To get rid of the multiply and other conversions in color
  60.  *    dither, we use a lookup table.
  61.  *
  62.  * Results:
  63.  *    None.
  64.  *
  65.  * Side effects:
  66.  *    The lookup tables are initialized.
  67.  *
  68.  *--------------------------------------------------------------
  69.  */
  70.  
  71. void
  72. InitColorDither()
  73. {
  74.     int CR, CB, i;
  75.  
  76.     Cr_b_tab = (int *)malloc(256*sizeof(int));
  77.     Cr_g_tab = (int *)malloc(256*sizeof(int));
  78.     Cb_g_tab = (int *)malloc(256*sizeof(int));
  79.     Cb_r_tab = (int *)malloc(256*sizeof(int));
  80.  
  81.     for (i=0; i<256; i++) {
  82.     CB = CR = i;
  83.  
  84.     CB -= 128; CR -= 128;
  85.  
  86.     Cb_r_tab[i] = FIX(1.40200) * CB;
  87.     Cr_g_tab[i] = -FIX(0.34414) * CR;
  88.     Cb_g_tab[i] = -FIX(0.71414) * CB;   
  89.     Cr_b_tab[i] = FIX(1.77200) * CR;
  90.     }
  91. }
  92.  
  93.  
  94. /*
  95.  *--------------------------------------------------------------
  96.  *
  97.  * ColorDitherImage --
  98.  *
  99.  *    Converts image into 24 bit color.
  100.  *
  101.  * Results:
  102.  *    None.
  103.  *
  104.  * Side effects:
  105.  *    None.
  106.  *
  107.  *--------------------------------------------------------------
  108.  */
  109.  
  110. void
  111. ColorDitherImage(lum, cr, cb, out, rows, cols)
  112.   unsigned char *lum;
  113.   unsigned char *cr;
  114.   unsigned char *cb;
  115.   unsigned char *out;
  116.   int cols, rows;
  117.  
  118. {
  119.     int L, CR, CB;
  120.     unsigned int *row1, *row2;
  121.     unsigned char *lum2;
  122.     int x, y;
  123.     unsigned int r, b, g;
  124.     int cb_r;
  125.     int cr_g;
  126.     int cb_g;
  127.     int cr_b;
  128.  
  129.     row1 = (unsigned int *)out;
  130.     row2 = row1 + cols;
  131.     lum2 = lum + cols;
  132.     for (y=0; y<rows; y+=2) {
  133.     for (x=0; x<cols; x+=2) {
  134.         int R, G, B;
  135.  
  136.         CR = *cr++;
  137.         CB = *cb++;
  138.         cb_r = Cb_r_tab[CB];
  139.         cr_g = Cr_g_tab[CR];
  140.         cb_g = Cb_g_tab[CB];
  141.         cr_b = Cr_b_tab[CR];
  142.  
  143.         L = *lum++;
  144.         L = UP(L);
  145.         R = L + cb_r;
  146.         G = L + cr_g + cb_g;
  147.         B = L + cr_b;
  148. #ifndef RS6000
  149.         r = CLAMP(0,R,UP(255)) >> BITS;
  150.         g = CLAMP(0,G,UP(255)) & 0xff00;
  151.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  152. #else
  153.         b = CLAMP(0,B,UP(255)) >> BITS;
  154.         g = CLAMP(0,G,UP(255)) & 0xff00;
  155.         r = (CLAMP(0,R,UP(255)) & 0xff00) << BITS;
  156. #endif
  157.         *row1++ = r | g | b;
  158.  
  159.         L = *lum++;
  160.         L = UP(L);
  161.         R = L + cb_r;
  162.         G = L + cr_g + cb_g;
  163.         B = L + cr_b;
  164. #ifndef RS6000
  165.         r = CLAMP(0,R,UP(255)) >> BITS;
  166.         g = CLAMP(0,G,UP(255)) & 0xff00;
  167.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  168. #else
  169.         b = CLAMP(0,B,UP(255)) >> BITS;
  170.         g = CLAMP(0,G,UP(255)) & 0xff00;
  171.         r = (CLAMP(0,R,UP(255)) & 0xff00) << BITS;
  172. #endif
  173.         *row1++ = r | g | b;
  174.  
  175.         /*
  176.          * Now, do second row.
  177.          */
  178.         L = *lum2++;
  179.         L = UP(L);
  180.         R = L + cb_r;
  181.         G = L + cr_g + cb_g;
  182.         B = L + cr_b;
  183. #ifndef RS6000
  184.         r = CLAMP(0,R,UP(255)) >> BITS;
  185.         g = CLAMP(0,G,UP(255)) & 0xff00;
  186.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  187. #else
  188.         b = CLAMP(0,B,UP(255)) >> BITS;
  189.         g = CLAMP(0,G,UP(255)) & 0xff00;
  190.         r = (CLAMP(0,R,UP(255)) & 0xff00) << BITS;
  191. #endif
  192.         *row2++ = r | g | b;
  193.  
  194.         L = *lum2++;
  195.         L = UP(L);
  196.         R = L + cb_r;
  197.         G = L + cr_g + cb_g;
  198.         B = L + cr_b;
  199. #ifndef RS6000
  200.         r = CLAMP(0,R,UP(255)) >> BITS;
  201.         g = CLAMP(0,G,UP(255)) & 0xff00;
  202.         b = (CLAMP(0,B,UP(255)) & 0xff00) << BITS;
  203. #else
  204.         b = CLAMP(0,B,UP(255)) >> BITS;
  205.         g = CLAMP(0,G,UP(255)) & 0xff00;
  206.         r = (CLAMP(0,R,UP(255)) & 0xff00) << BITS;
  207. #endif
  208.         *row2++ = r | g | b;
  209.     }
  210.     lum += cols;
  211.     lum2 += cols;
  212.     row1 += cols;
  213.     row2 += cols;
  214.     }
  215. }
  216.  
  217.  
  218.  
  219.