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
|
/*
* "$Id$"
*
* Dithering routines for CUPS.
*
* Copyright 2007 by Apple Inc.
* Copyright 1993-2005 by Easy Software Products.
*
* These coded instructions, statements, and computer programs are the
* property of Apple Inc. and are protected by Federal copyright
* law. Distribution and use rights are outlined in the file "LICENSE.txt"
* which should have been included with this file. If this file is
* file is missing or damaged, see the license at "http://www.cups.org/".
*
* Contents:
*
* cupsDitherDelete() - Free a dithering buffer.
* cupsDitherLine() - Dither a line of pixels...
* cupsDitherNew() - Create a dithering buffer.
*/
/*
* Include necessary headers.
*/
#include "driver.h"
#include <config.h>
/*
* 'cupsDitherDelete()' - Free a dithering buffer.
*
* Returns 0 on success, -1 on failure.
*/
void
cupsDitherDelete(cups_dither_t *d) /* I - Dithering buffer */
{
if (d != NULL)
free(d);
}
/*
* 'cupsDitherLine()' - Dither a line of pixels...
*/
void
cupsDitherLine(cups_dither_t *d, /* I - Dither data */
const cups_lut_t *lut, /* I - Lookup table */
const short *data, /* I - Separation data */
int num_channels,
/* I - Number of components */
unsigned char *p) /* O - Pixels */
{
register int x, /* Horizontal position in line... */
pixel, /* Current adjusted pixel... */
e, /* Current error */
e0,e1,e2; /* Error values */
register int errval0, /* First half of error value */
errval1, /* Second half of error value */
errbase, /* Base multiplier */
errbase0, /* Base multiplier for large values */
errbase1, /* Base multiplier for small values */
errrange; /* Range of random multiplier */
register int *p0, /* Error buffer pointers... */
*p1;
static char logtable[16384]; /* Error magnitude for randomness */
static char loginit = 0; /* Has the table been initialized? */
if (!loginit)
{
/*
* Initialize a logarithmic table for the magnitude of randomness
* that is introduced.
*/
loginit = 1;
logtable[0] = 0;
for (x = 1; x < 2049; x ++)
logtable[x] = (int)(log(x / 16.0) / log(2.0) + 1.0);
for (; x < 16384; x ++)
logtable[x] = logtable[2049];
}
if (d->row == 0)
{
/*
* Dither from left to right:
*
* e0 == p0[0]
* e1 e2 == p1[-1] p1[0]
*/
p0 = d->errors + 2;
p1 = d->errors + 2 + d->width + 4;
e0 = p0[0];
e1 = 0;
e2 = 0;
/*
* Error diffuse each output pixel...
*/
for (x = d->width;
x > 0;
x --, p0 ++, p1 ++, p ++, data += num_channels)
{
/*
* Skip blank pixels...
*/
if (*data == 0)
{
*p = 0;
e0 = p0[1];
p1[-1] = e1;
e1 = e2;
e2 = 0;
continue;
}
/*
* Compute the net pixel brightness and brightness error. Set a dot
* if necessary...
*/
pixel = lut[*data].intensity + e0 / 128;
if (pixel > CUPS_MAX_LUT)
pixel = CUPS_MAX_LUT;
else if (pixel < 0)
pixel = 0;
*p = lut[pixel].pixel;
e = lut[pixel].error;
/*
* Set the randomness factor...
*/
if (e > 0)
errrange = logtable[e];
else
errrange = logtable[-e];
errbase = 8 - errrange;
errrange = errrange * 2 + 1;
/*
* Randomize the error value.
*/
if (errrange > 1)
{
errbase0 = errbase + (CUPS_RAND() % errrange);
errbase1 = errbase + (CUPS_RAND() % errrange);
}
else
errbase0 = errbase1 = errbase;
/*
* X 7/16 = X e0
* 3/16 5/16 1/16 = e1 e2
*/
errval0 = errbase0 * e;
errval1 = (16 - errbase0) * e;
e0 = p0[1] + 7 * errval0;
e1 = e2 + 5 * errval1;
errval0 = errbase1 * e;
errval1 = (16 - errbase1) * e;
e2 = errval0;
p1[-1] = e1 + 3 * errval1;
}
}
else
{
/*
* Dither from right to left:
*
* e0 == p0[0]
* e2 e1 == p1[0] p1[1]
*/
p0 = d->errors + d->width + 1 + d->width + 4;
p1 = d->errors + d->width + 1;
p += d->width - 1;
data += num_channels * (d->width - 1);
e0 = p0[0];
e1 = 0;
e2 = 0;
/*
* Error diffuse each output pixel...
*/
for (x = d->width;
x > 0;
x --, p0 --, p1 --, p --, data -= num_channels)
{
/*
* Skip blank pixels...
*/
if (*data == 0)
{
*p = 0;
e0 = p0[-1];
p1[1] = e1;
e1 = e2;
e2 = 0;
continue;
}
/*
* Compute the net pixel brightness and brightness error. Set a dot
* if necessary...
*/
pixel = lut[*data].intensity + e0 / 128;
if (pixel > CUPS_MAX_LUT)
pixel = CUPS_MAX_LUT;
else if (pixel < 0)
pixel = 0;
*p = lut[pixel].pixel;
e = lut[pixel].error;
/*
* Set the randomness factor...
*/
if (e > 0)
errrange = logtable[e];
else
errrange = logtable[-e];
errbase = 8 - errrange;
errrange = errrange * 2 + 1;
/*
* Randomize the error value.
*/
if (errrange > 1)
{
errbase0 = errbase + (CUPS_RAND() % errrange);
errbase1 = errbase + (CUPS_RAND() % errrange);
}
else
errbase0 = errbase1 = errbase;
/*
* X 7/16 = X e0
* 3/16 5/16 1/16 = e1 e2
*/
errval0 = errbase0 * e;
errval1 = (16 - errbase0) * e;
e0 = p0[-1] + 7 * errval0;
e1 = e2 + 5 * errval1;
errval0 = errbase1 * e;
errval1 = (16 - errbase1) * e;
e2 = errval0;
p1[1] = e1 + 3 * errval1;
}
}
/*
* Update to the next row...
*/
d->row = 1 - d->row;
}
/*
* 'cupsDitherNew()' - Create an error-diffusion dithering buffer.
*/
cups_dither_t * /* O - New state array */
cupsDitherNew(int width) /* I - Width of output in pixels */
{
cups_dither_t *d; /* New dithering buffer */
if ((d = (cups_dither_t *)calloc(1, sizeof(cups_dither_t) +
2 * (width + 4) *
sizeof(int))) == NULL)
return (NULL);
d->width = width;
return (d);
}
/*
* End of "$Id$".
*/
|