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