1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/*
* 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 <config.h>
#include <malloc.h>
#include <memory.h>
#include "mpeg.h"
#include "dither.h"
#include "video.h"
#include "proto.h"
#include "globals.h" /* for global variable ditherType */
#include "my_dmalloc.h"
#define NUM_COLORS 256 /* number of entries in colormap */
/* for gray-scale dithering */
/* Range values for lum, cr, cb. */
int LUM_RANGE = 8;
int CR_RANGE = 4;
int CB_RANGE = 4;
/* Array that remaps color numbers to actual pixel values used by X server. */
unsigned char pixel[256];
/* Arrays holding quantized value ranged for lum, cr, and cb. */
int *lum_values;
int *cr_values;
int *cb_values;
/*
*--------------------------------------------------------------
*
* InitColor --
*
* Initialized lum, cr, and cb quantized range value arrays.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static void
InitColor(void)
{
int i;
for (i=0; i<LUM_RANGE; i++) {
lum_values[i] = ((i * 256) / (LUM_RANGE)) + (256/(LUM_RANGE*2));
}
for (i=0; i<CR_RANGE; i++) {
cr_values[i] = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2));
}
for (i=0; i<CB_RANGE; i++) {
cb_values[i] = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2));
}
}
/*
*--------------------------------------------------------------
*
* ConvertColor --
*
* Given a l, cr, cb tuple, converts it to r,g,b.
*
* Results:
* r,g,b values returned in pointers passed as parameters.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
static void
ConvertColor (unsigned char l,
unsigned char cr,
unsigned char cb,
unsigned char *r,
unsigned char *g,
unsigned char *b)
{
double fl, fcr, fcb, fr, fg, fb;
fl = (double) l;
fcr = ((double) cr) - 128.0;
fcb = ((double) cb) - 128.0;
fr = fl + (1.40200 * fcb);
fg = fl - (0.71414 * fcb) - (0.34414 * fcr);
fb = fl + (1.77200 * fcr);
if (fr < 0.0) fr = 0.0;
else if (fr > 255.0) fr = 255.0;
if (fg < 0.0) fg = 0.0;
else if (fg > 255.0) fg = 255.0;
if (fb < 0.0) fb = 0.0;
else if (fb > 255.0) fb = 255.0;
*r = (unsigned char) fr;
*g = (unsigned char) fg;
*b = (unsigned char) fb;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : InitColormap
@INPUT : (none)
@OUTPUT : *NumColors - number of entries in the newly-created colormap
*Map - an array of colourmap entries; each one contains a
red, green, and blue byte-values (0 .. 255).
*Map[i] gives the colour to display a pixel value i.
@RETURNS : (none)
@DESCRIPTION: Creates a colour map used for most dithering methods
(everything except full-colour, gray, and monochrome).
The colour map itself is pretty self-explanatory -- a
pixel with value i is to be displayed using the red, green
and blue values in *Map[i] after InitColormap() is done.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 95/3/4, Greg Ward: based on InitColorDisplay(), from gdith.c
in the original Berkeley player
@MODIFIED :
---------------------------------------------------------------------------- */
#if (ENABLE_DITHER)
static void
InitColormap (int *NumColors, ColormapEntry **Map)
{
int i, lum_num, cr_num, cb_num;
*NumColors = LUM_RANGE*CB_RANGE*CR_RANGE;
if (ditherType == NO_DITHER) return;
*Map = (ColormapEntry *) malloc (*NumColors * sizeof (ColormapEntry));
for (i = 0; i < *NumColors; i++)
{
lum_num = (i / (CR_RANGE*CB_RANGE))%LUM_RANGE;
cr_num = (i / CB_RANGE)%CR_RANGE;
cb_num = i % CB_RANGE;
ConvertColor(lum_values[lum_num], cr_values[cr_num], cb_values[cb_num],
(unsigned char*)(&((*Map)[i]).red),
(unsigned char*)(&((*Map)[i]).green),
(unsigned char*)(&((*Map)[i]).blue));
pixel[i] = i;
}
}
#endif
/* ----------------------------- MNI Header -----------------------------------
@NAME : InitGrayColormap
@INPUT : (none)
@OUTPUT : *NumColors - number of entries in the newly-created colormap
*Map - an array of colourmap entries
@RETURNS : (none)
@DESCRIPTION: Creates a colour map used for gray-scale dithering, i.e.
the red/green/blue values are the same for any given
pixel value.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : 95/3/4, Greg Ward: based on InitGrayDisplay(), from gdith.c
in the original Berkeley player
@MODIFIED :
---------------------------------------------------------------------------- */
#if (ENABLE_DITHER)
static void
InitGrayColormap (int *NumColors, ColormapEntry **Map)
{
int i;
*NumColors = NUM_COLORS;
if (ditherType == NO_DITHER) return;
*Map = (ColormapEntry *) malloc (*NumColors * sizeof (ColormapEntry));
for (i = 0; i < *NumColors; i++)
{
(*Map)[i].red = (*Map)[i].green = (*Map)[i].blue = i;
pixel[i] = i;
}
}
#endif
/* ----------------------------- MNI Header -----------------------------------
@NAME : InitDither
@INPUT : Image - pointer to the image descriptor for the current MPEG
@OUTPUT : Image->ColormapSize, Image->Colormap - the colour map for
this movie, as initialized by either InitColormap or
InitGrayColormap (unless the current dithering scheme
is full colour, in which case there is no colour map)
@RETURNS : (none)
@DESCRIPTION: Does all initialization particular to the type of dithering
being used. Basically, sets up the internal data structures
needed by the dithering code, and then sets up a colour map
needed to display the pixels output by the ditherers.
@METHOD :
@GLOBALS :
@CALLS : InitColor (for most dithering methods)
InitColormap (for most dithering methods)
InitGrayColormap (for gray-scale dithering)
Init(..)Dither (.. = the current dithering method)
@CREATED : 95/3/3, Greg Ward: taken mostly from main() in the original
Berkeley player
@MODIFIED :
---------------------------------------------------------------------------- */
void
InitDither (ImageDesc *Image)
{
switch (ditherType)
{
#if (ENABLE_DITHER)
case HYBRID_DITHER:
InitColor ();
InitHybridDither ();
InitColormap (&Image->ColormapSize, &Image->Colormap);
break;
case HYBRID2_DITHER:
InitColor ();
InitHybridErrorDither ();
InitColormap (&Image->ColormapSize, &Image->Colormap);
break;
case FS4_DITHER:
InitColor ();
InitFS4Dither ();
InitColormap (&Image->ColormapSize, &Image->Colormap);
break;
case FS2_DITHER:
InitColor ();
InitFS2Dither ();
InitColormap (&Image->ColormapSize, &Image->Colormap);
break;
case FS2FAST_DITHER:
InitColor ();
InitFS2FastDither ();
InitColormap (&Image->ColormapSize, &Image->Colormap);
break;
case Twox2_DITHER:
InitColor ();
Init2x2Dither ();
InitColormap (&Image->ColormapSize, &Image->Colormap);
PostInit2x2Dither ();
break;
case GRAY_DITHER:
InitGrayColormap (&Image->ColormapSize, &Image->Colormap);
break;
#endif
case FULL_COLOR_DITHER:
InitColorDither ();
Image->ColormapSize = -1;
Image->Colormap = NULL;
break;
#if (ENABLE_DITHER)
case NO_DITHER:
break;
case ORDERED_DITHER:
InitColor ();
InitOrderedDither ();
InitColormap (&Image->ColormapSize, &Image->Colormap);
break;
case MONO_DITHER:
case MONO_THRESHOLD:
break;
case ORDERED2_DITHER:
InitColor ();
InitColormap (&Image->ColormapSize, &Image->Colormap);
InitOrdered2Dither ();
break;
case MBORDERED_DITHER:
InitColor ();
InitColormap (&Image->ColormapSize, &Image->Colormap);
InitMBOrderedDither ();
break;
#endif
}
}
|