1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: gdevstc2.c 8022 2007-06-05 22:23:38Z giles $*/
15
/* Epson Stylus-Color Printer-Driver */
18
This file holds two implementations of the Floyd-Steinberg error
19
diffusion-algorithm. This algorithms are intended for high quality
20
printing in conjunction with the PostScript-Header stcolor.ps:
22
gs -sDEVICE=stcolor <other options> stcolor.ps ...
24
Most prominent option is -sDithering=xxx, to select the algorithm:
26
fsmono - monochrome Floyd-Steinberg
27
fsrgb - 3-Component Floyd-Steinberg
28
fsx4 - 4-Component Floyd-Steinberg (Bad results)
30
fscmyk - Modified 4-Component Floyd-Steinberg
31
(Algorithmically identical with hscmyk, but slower)
37
#include <stdlib.h> /* for rand */
40
Both algorithms require an error-buffer of
42
3 + 3*num_components +1*scan long-items.
44
and must consequently set up to work with longs.
45
It is just a Floyd-Steinberg-algorithm applied to each component.
50
* Due to the -selfdefined- ugly coding of the output-data, we need
51
* some conversion. But since this includes the black-separation, I
52
* did not change the definition.
54
* This algorithm stores the 1st component in the LSB, thus it
55
* reverts the order used by the basic driver.
58
static const byte grayvals[2] = { 0, BLACK };
60
static const byte rgbvals[8] = {
61
0, RED, GREEN, RED|GREEN, BLUE, BLUE|RED, BLUE|GREEN, BLUE|RED|GREEN};
63
static const byte cmykvals[16] = {
64
0, CYAN,MAGENTA,CYAN|MAGENTA,YELLOW,YELLOW|CYAN,YELLOW|MAGENTA,BLACK,
65
BLACK,BLACK, BLACK, BLACK, BLACK, BLACK, BLACK,BLACK};
67
static const byte *const pixelconversion[5] = {
68
NULL, grayvals, NULL, rgbvals, cmykvals};
72
stc_fs(stcolor_device *sdev,int npixel,byte *bin,byte *bbuf,byte *out)
75
long *in = (long *) bin;
76
long *buf = (long *) bbuf;
78
/* ============================================================= */
79
if(npixel > 0) { /* npixel > 0 -> scanline-processing */
80
/* ============================================================= */
82
int bstep,pstart,pstop,pstep,p;
83
long spotsize,threshold,*errc,*errv;
84
const byte *pixel2stc;
86
if(buf[0] >= 0) { /* run forward */
89
pstep = sdev->color_info.num_components;
91
pstop = npixel * pstep;
93
} else { /* run backward */
96
pstep = -sdev->color_info.num_components;
98
pstart = (1-npixel) * pstep;
100
} /* forward / backward */
102
/* --------------------------------------------------------------------- */
103
if(in == NULL) return 0; /* almost ignore the 'white calls' */
104
/* --------------------------------------------------------------------- */
109
errv = errc + 2*sdev->color_info.num_components;
110
pixel2stc = pixelconversion[sdev->color_info.num_components];
112
for(p = pstart; p != pstop; p += pstep) { /* loop over pixels */
113
int c; /* component-number */
114
int pixel; /* internal pxel-value */
118
for(c = 0; c < sdev->color_info.num_components; c++) { /* comp */
119
long cv; /* component value */
121
cv = in[p+c] + errv[p+c] + errc[c] - ((errc[c]+4)>>3);
126
errv[p+c-pstep] += ((3*cv+8)>>4); /* 3/16 */
127
errv[p+c ] = ((5*cv )>>4) /* 5/16 */
128
+ ((errc[c]+4)>>3); /* 1/16 (rest) */
129
errc[c] = cv /* 8/16 (neu) */
134
*out = pixel2stc[pixel];
136
} /* loop over pixels */
139
/* ============================================================= */
140
} else { /* npixel <= 0 -> initialisation */
141
/* ============================================================= */
148
* check wether the number of components is valid
150
if((sdev->color_info.num_components < 0) ||
151
(sdev->color_info.num_components >= countof(pixelconversion)) ||
152
(pixelconversion[sdev->color_info.num_components] == NULL)) return -1;
155
* check wether stcdither & TYPE are correct
157
if(( sdev->stc.dither == NULL) ||
158
((sdev->stc.dither->flags & STC_TYPE) != STC_LONG)) return -2;
161
* check wether the buffer-size is sufficiently large
163
if(((sdev->stc.dither->flags/STC_SCAN) < 1) ||
164
( sdev->stc.dither->bufadd <
165
(3 + 3*sdev->color_info.num_components))) return -3;
167
* must neither have STC_DIRECT nor STC_WHITE
169
if(sdev->stc.dither->flags & (STC_DIRECT | STC_WHITE)) return -4;
172
* compute initial values
178
scale = sdev->stc.dither->minmax[1];
179
buf[1] = (long)(scale + (scale > 0.0 ? 0.5 : -0.5));
182
offset = sdev->stc.dither->minmax[0];
184
if((offset+0.5*scale) > 0.0) buf[2] = (long)(offset + 0.5*scale + 0.5);
185
else buf[2] = (long)(offset + 0.5*scale - 0.5);
188
* random values, that do not exceed half of normal value
190
i2do = sdev->color_info.num_components * (3-npixel);
193
if(sdev->stc.flags & STCDFLAG0) {
195
for(i = 0; i < i2do; ++i) buf[i+3] = 0;
199
for(i = 0; i < i2do; ++i) {
201
if(buf[i+3] > rand_max) rand_max = buf[i+3];
204
scale = (double) buf[1] / (double) rand_max;
206
for(i = 0; i < sdev->color_info.num_components; ++ i)
207
buf[i+3] = (long)(0.25000*scale*(buf[i+3]-rand_max/2));
209
for( ; i < i2do; ++i) /* includes 2 additional pixels ! */
210
buf[i+3] = (long)(0.28125*scale*(buf[i+3]-rand_max/2));
214
/* ============================================================= */
215
} /* scanline-processing or initialisation */
216
/* ============================================================= */
222
* Experimental CMYK-Algorithm
226
stc_fscmyk(stcolor_device *sdev,int npixel,byte *bin,byte *bbuf,byte *out)
228
long *in = (long *) bin;
229
long *buf = (long *) bbuf;
231
/* ============================================================= */
232
if(npixel > 0) { /* npixel > 0 -> scanline-processing */
233
/* ============================================================= */
235
int bstep,pstart,pstop,pstep,p;
236
long spotsize,threshold,*errc,*errv;
238
if(buf[0] >= 0) { /* run forward */
243
pstop = npixel * pstep;
245
} else { /* run backward */
250
pstart = (1-npixel) * pstep;
252
} /* forward / backward */
259
for(p = 0; p < 4; ++p) errc[p] = 0;
261
for(p = pstart; p != pstop; p += pstep) { /* loop over pixels */
262
int c; /* component-number */
263
int pixel; /* internal pxel-value */
267
* Black is treated first, with conventional Floyd-Steinberg
270
cv = k + errv[p+3] + errc[3] - ((errc[3]+4)>>3);
279
errv[p+3-pstep] += ((3*cv+8)>>4); /* 3/16 */
280
errv[p+3 ] = ((5*cv )>>4) /* 5/16 */
281
+ ((errc[3]+4)>>3); /* 1/16 (rest) */
282
errc[3] = cv /* 8/16 (neu) */
287
* color-handling changes with black fired or not
291
/* -------- firing of black causes all colors to fire too */
293
for(c = 0; c < 3; ++c) {
294
cv = in[p+c] > k ? in[p+c] : k;
295
cv += errv[p+c] + errc[c] - ((errc[c]+4)>>3)-spotsize;
296
if(cv <= (threshold-spotsize)) cv = threshold-spotsize+1;
298
errv[p+c-pstep] += ((3*cv+8)>>4); /* 3/16 */
299
errv[p+c ] = ((5*cv )>>4) /* 5/16 */
300
+ ((errc[c]+4)>>3); /* 1/16 (rest) */
301
errc[c] = cv /* 8/16 (neu) */
308
/* -------- if black did not fire, only colors w. larger values may fire */
310
for(c = 0; c < 3; ++c) {
314
if(cv > k) { /* May Fire */
315
cv += errv[p+c] + errc[c] - ((errc[c]+4)>>3);
320
} else { /* Must not fire */
321
cv = k + errv[p+c] + errc[c] - ((errc[c]+4)>>3);
322
if(cv > threshold ) cv = threshold;
325
errv[p+c-pstep] += ((3*cv+8)>>4); /* 3/16 */
326
errv[p+c ] = ((5*cv )>>4) /* 5/16 */
327
+ ((errc[c]+4)>>3); /* 1/16 (rest) */
328
errc[c] = cv /* 8/16 (neu) */
336
} /* loop over pixels */
339
/* ============================================================= */
340
} else { /* npixel <= 0 -> initialisation */
341
/* ============================================================= */
348
* check wether the number of components is valid
350
if(sdev->color_info.num_components != 4) return -1;
353
* check wether stcdither & TYPE are correct
355
if(( sdev->stc.dither == NULL) ||
356
((sdev->stc.dither->flags & STC_TYPE) != STC_LONG)) return -2;
359
* check wether the buffer-size is sufficiently large
361
if(((sdev->stc.dither->flags/STC_SCAN) < 1) ||
362
( sdev->stc.dither->bufadd <
363
(3 + 3*sdev->color_info.num_components))) return -3;
365
* must neither have STC_DIRECT nor STC_WHITE
367
if(sdev->stc.dither->flags & (STC_DIRECT | STC_WHITE)) return -4;
370
* compute initial values
376
scale = sdev->stc.dither->minmax[1];
377
buf[1] = (long)(scale + (scale > 0.0 ? 0.5 : -0.5));
380
offset = sdev->stc.dither->minmax[0];
382
if(sdev->stc.flags & STCDFLAG1) {
383
buf[2] = (long)((sdev->stc.extv[0][sdev->stc.sizv[0]-1] -
384
sdev->stc.extv[0][0]) * scale / 2.0 + offset);
386
if((offset+0.5*scale) > 0.0) buf[2] = (long)(offset + 0.5*scale + 0.5);
387
else buf[2] = (long)(offset + 0.5*scale - 0.5);
391
* random values, that do not exceed half of normal value
393
i2do = sdev->color_info.num_components * (3-npixel);
396
if(sdev->stc.flags & STCDFLAG0) {
398
for(i = 0; i < i2do; ++i) buf[i+3] = 0;
402
for(i = 0; i < i2do; ++i) {
404
if(buf[i+3] > rand_max) rand_max = buf[i+3];
407
scale = (double) buf[1] / (double) rand_max;
409
for(i = 0; i < sdev->color_info.num_components; ++ i)
410
buf[i+3] = (long)(0.25000*scale*(buf[i+3]-rand_max/2));
412
for( ; i < i2do; ++i) /* includes 2 additional pixels ! */
413
buf[i+3] = (long)(0.28125*scale*(buf[i+3]-rand_max/2));
417
/* ============================================================= */
418
} /* scanline-processing or initialisation */
419
/* ============================================================= */