This is 24bit.c in view mode; [Download] [Up]
/* * Copyright (c) 1992 The Regents of the University of California. * All rights reserved. * * Permission to use, copy, modify, and distribute this software and its * documentation for any purpose, without fee, and without written agreement is * hereby granted, provided that the above copyright notice and the following * two paragraphs appear in all copies of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */ #include "video.h" #include "dither.h" /* * We'll define the "ConvertColor" macro here to do fixed point arithmetic * that'll convert from YCrCb to RGB using: * R = L + 1.40200*Cr; * G = L - 0.34414*Cb - 0.71414*Cr * B = L + 1.77200*Cb; * * We'll use fixed point by adding two extra bits after the decimal. */ #define BITS 8 #define ONE ((int) 1) #define CONST_SCALE (ONE << BITS) #define ROUND_FACTOR (ONE << (BITS-1)) /* Macro to convert integer to fixed. */ #define UP(x) (((int)(x)) << BITS) /* Macro to convert fixed to integer (with rounding). */ #define DOWN(x) (((x) + ROUND_FACTOR) >> BITS) /* Macro to convert a float to a fixed */ #define FIX(x) ((int) ((x)*CONST_SCALE + 0.5)) #define CLAMP(ll,x,ul) ( ((x)<(ll)) ?(ll):( ((x)>(ul)) ?(ul):(x))) static int *Cb_r_tab, *Cr_g_tab, *Cb_g_tab, *Cr_b_tab; /* *-------------------------------------------------------------- * * InitColorDither -- * * To get rid of the multiply and other conversions in color * dither, we use a lookup table. * * Results: * None. * * Side effects: * The lookup tables are initialized. * *-------------------------------------------------------------- */ void InitColorDither() { int CR, CB, i; Cr_b_tab = (int *)malloc(256*sizeof(int)); Cr_g_tab = (int *)malloc(256*sizeof(int)); Cb_g_tab = (int *)malloc(256*sizeof(int)); Cb_r_tab = (int *)malloc(256*sizeof(int)); for (i=0; i<256; i++) { CB = CR = i; CB -= 128; CR -= 128; Cb_r_tab[i] = FIX(1.40200) * CB; Cr_g_tab[i] = -FIX(0.34414) * CR; Cb_g_tab[i] = -FIX(0.71414) * CB; Cr_b_tab[i] = FIX(1.77200) * CR; } } /* *-------------------------------------------------------------- * * ColorDitherImage -- * * Converts image into 24 bit color. * * Results: * None. * * Side effects: * None. * *-------------------------------------------------------------- */ void ColorDitherImage(lum, cr, cb, out, rows, cols) unsigned char *lum; unsigned char *cr; unsigned char *cb; unsigned char *out; int cols, rows; { int L, CR, CB; unsigned char *row1, *row2; unsigned char *lum2; int x, y, colsT3; unsigned int r, b, g; int cb_r, cr_g, cb_g, cr_b, R, G, B; row1 = out; colsT3 = 3 * cols; row2 = row1 + colsT3; lum2 = lum + cols; for (y=0; y<rows; y+=2) { for (x=0; x<cols; x+=2) { cb_r = Cb_r_tab[*cb]; cr_g = Cr_g_tab[*cr]; cb_g = Cb_g_tab[*cb++]; cr_b = Cr_b_tab[*cr++]; L = UP(*lum++); R = (L + cb_r)>>BITS; G = (L + cr_g + cb_g)>>BITS; B = (L + cr_b)>>BITS; *row1++ = CLAMP(0,R,255); *row1++ = CLAMP(0,G,255); *row1++ = CLAMP(0,B,255); L = UP(*lum++); R = (L + cb_r)>>BITS; G = (L + cr_g + cb_g)>>BITS; B = (L + cr_b)>>BITS; *row1++ = CLAMP(0,R,255); *row1++ = CLAMP(0,G,255); *row1++ = CLAMP(0,B,255); /* Now, do second row */ L = UP(*lum2++); R = (L + cb_r)>>BITS; G = (L + cr_g + cb_g)>>BITS; B = (L + cr_b)>>BITS; *row2++ = CLAMP(0,R,255); *row2++ = CLAMP(0,G,255); *row2++ = CLAMP(0,B,255); L = UP(*lum2++); R = (L + cb_r)>>BITS; G = (L + cr_g + cb_g)>>BITS; B = (L + cr_b)>>BITS; *row2++ = CLAMP(0,R,255); *row2++ = CLAMP(0,G,255); *row2++ = CLAMP(0,B,255); } lum += cols; lum2 += cols; row1 += colsT3; row2 += colsT3; } }
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.