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
|
/*
* 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 "video.h"
#include "proto.h"
#include "dither.h"
/* Arrays containing error values for floyd-steinberg dithering. */
static int deltay[256];
static int deltau[256];
static int deltav[256];
static int deltay2[256];
static int deltau2[256];
static int deltav2[256];
/* Definitions governing number of bits used for luminance, cr, and cb. */
#define L_BITS 3
#define CR_BITS 2
#define CB_BITS 2
/* Masks for proper quantization of lum, cr, and cb values. */
#define L_MASK 0xe0
#define CR_MASK 0xc0
#define CB_MASK 0xc0
/*
*--------------------------------------------------------------
*
* InitFS2FastDither --
*
* Initializes structures and arrays neeeded for fast implementation
* of two error F-S dithering.
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
void InitFS2FastDither()
{
int i;
int lum_num, cr_num, cb_num;
for (i=0; i<256; i++) {
lum_num = (i >> (8-L_BITS));
cr_num = (i >> (8-CR_BITS));
cb_num = (i >> (8-CB_BITS));
/* These arrays contain the error values propogated for each pixel value
for each channel.
*/
deltay[i] = (i - ((int) lum_values[lum_num])) / 2;
deltau[i] = (i-((int) cr_values[cr_num])) / 2;
deltav[i] = (i-((int) cb_values[cb_num])) / 2;
deltay2[i] = (i - ((int) lum_values[lum_num])) - deltay[i];
deltau2[i] = (i - ((int) cr_values[cr_num])) - deltau[i];
deltav2[i] = (i - ((int) cb_values[cb_num])) - deltav[i];
}
}
/*
*--------------------------------------------------------------
*
* DitherImage --
*
* Dithers an image using floyd-steinberg.
* Assumptions made:
* 1) The color space is allocated y:cr:cb = 8:4:4
* 2) The spatial resolution of y:cr:cb is 4:1:1
*
* Results:
* None.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
void
FS2FastDitherImage (lum, cr, cb, out, h, w)
unsigned char *lum;
unsigned char *cr;
unsigned char *cb;
unsigned char *out;
int w, h;
{
int i, j, idx, idx2;
int y, u, v;
int dy, du, dv;
int code;
static int *yerr1;
static int *yerr2;
static int *uerr1;
static int *uerr2;
static int *verr1;
static int *verr2;
int *ye1, *ue1, *ve1;
int *ye2, *ue2, *ve2;
unsigned char *o, *l, *r, *b;
static int first = 1;
/* If first time called, allocate error arrays. */
if (first) {
first = 0;
yerr1 = (int *) malloc((w+5)*sizeof(int));
yerr2 = (int *) malloc((w+5)*sizeof(int));
uerr1 = (int *) malloc((w+5)*sizeof(int));
uerr2 = (int *) malloc((w+5)*sizeof(int));
verr1 = (int *) malloc((w+5)*sizeof(int));
verr2 = (int *) malloc((w+5)*sizeof(int));
}
/*
* Init error arrays and variables.
*/
memset ((char *)yerr1, 0, (w+5)*sizeof(int));
memset ((char *)yerr2, 0, (w+5)*sizeof(int));
memset ((char *)uerr1, 0, (w+5)*sizeof(int));
memset ((char *)uerr2, 0, (w+5)*sizeof(int));
memset ((char *)verr1, 0, (w+5)*sizeof(int));
memset ((char *)verr2, 0, (w+5)*sizeof(int));
du = dv = dy = 0;
for (j=0; j<h; j+=2) {
ye1 = yerr1;
ue1 = uerr1;
ve1 = verr1;
ye2 = yerr2;
ue2 = uerr2;
ve2 = verr2;
idx = j*w;
idx2 = idx/4;
o = out+idx;
l = lum+idx;
r = cr+idx2;
b = cb+idx2;
/* Do the top row in forward order. */
for (i=0; i<w; i+=2) {
/* Do left side of this pair... */
y = *l++ + dy + *ye1++;
u = *r + du + *ue1++;
v = *b + dv + *ve1++;
if (y < 0) y = 0;
else if (y > 255) y = 255;
if (u < 0) u = 0;
else if (u > 255) u = 255;
if (v < 0) v = 0;
else if (v > 255) v = 255;
/*
* Construct a code using:
* high order 3 bits of y,
* high order 2 bits of u,
* high order 2 bits of v
*/
code = (((y & L_MASK) | ((u & CR_MASK) >> L_BITS) | (v >> (L_BITS+CR_BITS)))
>> (8-(L_BITS+CR_BITS+CB_BITS)));
*o++ = pixel[code];
*ye2++ = deltay[y];
*ue2++ = deltau[u];
*ve2++ = deltav[v];
dy = deltay2[y];
du = deltau2[u];
dv = deltav2[v];
/* Do right side of this pair... */
y = *l++ + dy + *ye1++;
u = *r++ + du + *ue1++;
v = *b++ + dv + *ve1++;
if (y < 0) y = 0;
else if (y > 255) y = 255;
if (u < 0) u = 0;
else if (u > 255) u = 255;
if (v < 0) v = 0;
else if (v > 255) v = 255;
code = (((y & L_MASK) | ((u & CR_MASK) >> L_BITS) | (v >> (L_BITS+CR_BITS)))
>> (8-(L_BITS+CR_BITS+CB_BITS)));
*o++ = pixel[code];
*ye2++ = deltay[y];
*ue2++ = deltau[u];
*ve2++ = deltav[v];
dy = deltay2[y];
du = deltau2[u];
dv = deltav2[v];
}
ye1 = yerr1+w-1;
ue1 = uerr1+w-1;
ve1 = verr1+w-1;
ye2 = yerr2+w-1;
ue2 = uerr2+w-1;
ve2 = verr2+w-1;
l += w-1;
o += w-1;
r--;
b--;
dy = du = dv = 0;
/* Do bottom part of row, in right to left order. */
for (i=w-1; i>0; i-=2) {
/* Do right side of this pair... */
y = *l-- + dy + *ye2--;
u = *r + du + *ue2--;
v = *b + dv + *ve2--;
if (y < 0) y = 0;
else if (y > 255) y = 255;
if (u < 0) u = 0;
else if (u > 255) u = 255;
if (v < 0) v = 0;
else if (v > 255) v = 255;
/*
* Construct a code using:
* high order 3 bits of y,
* high order 2 bits of u,
* high order 2 bits of v
*/
code = (((y & L_MASK) | ((u & CR_MASK) >> L_BITS) | (v >> (L_BITS+CR_BITS)))
>> (8-(L_BITS+CR_BITS+CB_BITS)));
*o-- = pixel[code];
*ye1-- = deltay[y];
*ue1-- = deltau[u];
*ve1-- = deltav[v];
dy = deltay2[y];
du = deltau2[u];
dv = deltav2[v];
/* Do left side of this pair... */
y = *l-- + dy + *ye2--;
u = *r-- + du + *ue2--;
v = *b-- + dv + *ve2--;
if (y < 0) y = 0;
else if (y > 255) y = 255;
if (u < 0) u = 0;
else if (u > 255) u = 255;
if (v < 0) v = 0;
else if (v > 255) v = 255;
code = (((y & L_MASK) | ((u & CR_MASK) >> L_BITS) | (v >> (L_BITS+CR_BITS)))
>> (8-(L_BITS+CR_BITS+CB_BITS)));
*o-- = pixel[code];
*ye1-- = deltay[y];
*ue1-- = deltau[u];
*ve1-- = deltav[v];
dy = deltay2[y];
du = deltau2[u];
dv = deltav2[v];
}
}
}
|