5
* Threshold screen pixel processing object.
6
* (Simplified from DPS code)
8
* Author: Graeme W. Gill
12
* Copyright 2005, 2012 Graeme W. Gill
13
* All rights reserved.
14
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
15
* see the License.txt file for licencing details.
29
//#include "xcolorants.h"
35
/* ----------------------------------------------------------- */
38
# define DBG(text) printf text ; fflush(stdout);
43
/* ----------------------------------------------------------- */
44
/* Setup a set of screens */
45
/* Screen data is used that best matches the requested parameters. */
47
#include "screens.h" /* Pre-generated screen patterns */
49
/* Screen a single color plane */
50
void screen_thscreens( /* Pointer to dither function */
51
thscreens *t, /* Screening object pointer */
52
int width, int height, /* Width and height to screen in pixels */
53
int xoff, int yoff, /* Offset into screening pattern */
54
unsigned char *in, /* Input pixel buffer */
55
unsigned long ipitch, /* Increment between input lines */
56
unsigned char *out, /* Output pixel buffer */
57
unsigned long opitch /* Increment between output lines */
60
for (i = 0; i < t->np; i++)
61
t->sc[i]->screen(t->sc[i], width, height, xoff, yoff, in + 2 * i, t->np, ipitch,
62
out + i, t->np, opitch);
65
/* Delete a thscreens */
66
void del_thscreens(thscreens *t) {
69
for (i = 0; i < t->np; i++)
70
t->sc[i]->del(t->sc[i]);
75
/* Create a new thscreens object matching the parameters */
76
thscreens *new_thscreens(
77
int exact, /* Return only exact matches */
78
int nplanes, /* Number of planes to screen */
79
double asp, /* Target aspect ratio (== dpiX/dpiY) */
80
int size, /* Target size */
81
sc_iencoding ie, /* Input encoding - must be scie_16 */
82
int oebpc, /* Output encoding bits per component - must be 8 */
83
int oelev, /* Output encoding levels. Must be <= 2 ^ oebpc */
84
int *oevalues, /* Optional output encoding values for each level */
85
/* Must be oelev entries. Default is 0 .. oelev-1 */
86
sc_oorder oo, /* Output bit ordering */
87
double overlap, /* Overlap between levels, 0 - 1.0 */
88
void **cntx, /* List of contexts for lookup table callback */
89
double (**lutfunc)(void *cntx, double in) /* List of callback function, NULL if none */
93
double bamatch; /* Best aspect match */
94
int bsize = 100000; /* Best size match */
95
int swap = 0; /* width and height will need swapping */
97
DBG(("thscreens: new called with:\n"));
98
DBG((" nplanes = 0x%x\n",nplanes));
99
DBG((" asp = %f\n",asp));
100
DBG((" ie = %d\n",ie));
101
DBG((" oebpc = %d\n",oebpc));
102
DBG((" oelev = %d\n",oelev));
103
DBG((" oo = %d\n",oo));
104
DBG((" overlap = %f\n",overlap));
106
if (asp < 1.0) { /* All screens[] have asp >= 1.0 */
109
DBG(("thscreens: aspect swap needed\n"));
112
if ((t = (thscreens *)calloc(1, sizeof(thscreens))) == NULL) {
113
DBG(("thscreens: malloc of thscreens failed\n"));
117
t->np = nplanes; /* Number of planes */
119
DBG(("thscreens no planes = %d\n",t->np));
121
t->screen = screen_thscreens;
122
t->del = del_thscreens;
124
if ((t->sc = malloc(sizeof(thscreen *) * t->np)) == NULL) {
126
DBG(("thscreens: malloc of thscreens->sc[] failed\n"));
130
DBG(("thscreens: searching amongst %d screens, exact = %d\n",NO_SCREENS,exact));
132
DBG(("thscreens: looking for non-exact match\n"));
134
/* Synthesise a set of screens from what's there */
135
/* (Don't bother with matching the colorspace) */
136
for (i = 0;i < NO_SCREENS; i++) {
137
double thamatch; /* This aspect match */
138
int thsize; /* This size match */
140
thamatch = asp/screens[i].asp;
142
thamatch = 1.0/thamatch;
144
if (bi < 0 || (thamatch < bamatch)) { /* No best or new best */
147
DBG(("thscreens: new best with aspmatch %f\n",bamatch));
148
continue; /* On to next */
150
if (thamatch > bamatch) /* Worse aspect match */
152
/* Same on aspect ratio. Check size */
153
thsize = size - screens[i].size;
156
if (thsize < bsize) { /* New better size match */
159
DBG(("thscreens: new best with size %d\n",bsize));
163
if (bi < 0) /* Strange */
166
/* Create each screening object from one defined screen. */
167
/* Use the 0'th plane screen */
168
/* Stagger the screens with a round of 9 offset */
169
for (i = 0; i < t->np; i++) {
170
int xoff = ((i % 3) * screens[bi].width)/3;
171
int yoff = (((i/3) % 3) * screens[bi].height)/3;
173
double (*lf)(void *cntx, double in) = 0;
179
DBG(("thscreens: creating plane %d/%d thscreen, offset %d %d\n",i,t->np,xoff,yoff));
180
if ((t->sc[i] = new_thscreen(screens[bi].width, screens[bi].height, xoff, yoff,
181
screens[bi].asp, swap, screens[bi].list[0],
182
ie, oebpc, oelev, oevalues, oo, overlap,
184
for (--i; i >= 0; i--)
185
t->sc[i]->del(t->sc[i]);
188
DBG(("thscreens: new_thscreen() failed\n"));
192
DBG(("thscreens: returning nonexact match\n"));
196
/* ----------------------------------------------------------- */
197
/* The kernel screening routin */
200
struct _thscreen *t, /* Screening object pointer */
201
int width, int height, /* Width and height to screen in pixels */
202
int xoff, int yoff, /* Offset into screening pattern (must be +ve) */
203
unsigned char *_in, /* Input pixel buffer */
204
unsigned long ipinc, /* Increment between input pixels */
205
unsigned long ipitch, /* Increment between input lines */
206
unsigned char *out, /* Output pixel buffer */
207
unsigned long opinc, /* Increment between output pixels */
208
unsigned long opitch /* Increment between output lines */
210
unsigned short *in = (unsigned short *)_in; /* Pointer to input pixel sized values */
211
int *lut = t->lut; /* Copy of 8 or 16 -> 16 bit lookup table */
212
unsigned char **oth, **eth; /* Current lines start, origin and end in screening table. */
213
int thtsize; /* Overall size of threshold table */
214
unsigned char **eeth; /* Very end of threshold table */
215
unsigned short *ein = in + height * ipitch; /* Vertical end pixel marker */
216
unsigned short *ein1; /* Horizontal end pixel markers */
219
unsigned char **sth; /* Start point of line intable */
220
sth = t->thp + (yoff % t->sheight) * t->twidth;
221
oth = sth + (xoff % t->swidth); /* Orgin of pattern to start from */
222
eth = sth + t->swidth; /* Ending point to wrap back */
223
thtsize = t->twidth * t->theight;
224
eeth = t->thp + thtsize; /* very end of table */
227
ein1 = in + ipinc * width;
230
for (; in < ein; in += ipitch, ein1 += ipitch, out += opitch) {
231
unsigned char **th = oth; /* Threshold table origin */
232
unsigned short *ip = in; /* Horizontal input pointer */
233
unsigned char *op = out; /* Horizontal output pointer */
235
/* Do pixels one output byte at a time */
236
for (; ip < ein1; ip += ipinc, op += opinc) {
238
*op = (unsigned char)th[0][tt];
243
/* Advance screen table pointers with vertical wrap */
253
/* ----------------------------------------------------------- */
255
/* We're done with the screening object */
266
/* Create a new thscreen object */
267
/* Return NULL on error */
268
thscreen *new_thscreen(
269
int width, /* width in pixels of screen */
270
int height, /* Height in pixels of screen */
271
int xoff, int yoff, /* Pattern offsets into width & height */
272
double asp, /* Aspect ratio of screen (== dpiX/dpiY) */
273
int swap, /* Swap X & Y to invert aspect ratio & swap width/height */
274
ccoord *thli, /* Pointer to list of threshold coordinates */
275
sc_iencoding ie, /* Input encoding - must be scie_16 */
276
int oebpc, /* Output encoding bits per component - must be 8 */
277
int oelev, /* Output encoding levels. Must be <= 2 ^ oebpc */
278
int *oevalues, /* Optional output encoding values for each level */
279
/* Must be oelev entries. Default is 0 .. oelev-1 */
280
sc_oorder oo, /* Output bit ordering */
281
double olap, /* Overlap between levels, 0 - 1.0 */
282
void *cntx, /* Context for LUT table callback */
283
double (*lutfunc)(void *cntx, double in) /* Callback function, NULL if none */
285
thscreen *t; /* Object being created */
286
int npix; /* Total pixels in screen */
287
double mrang; /* threshold modulation range */
288
double **fthr; /* Floating point threshold array */
291
DBG(("new_thscreen() called, oebpc = %d\n",oebpc));
292
DBG(("new_thscreen() called, oelev = %d\n",oelev));
294
/* Sanity check overlap */
300
/* Sanity check parameters */
302
DBG(("new_thscreen() ie %d != scie_16\n",ie));
306
DBG(("new_thscreen() oebpc %d != 8\n",oebpc));
310
if (oelev < 2 || oelev > (1 << oebpc)) {
311
DBG(("new_thscreen() oelev %d > 2^%d = %d\n",oelev,1 << oebpc,oebpc));
315
if ((t = (thscreen *)calloc(1, sizeof(thscreen))) == NULL) {
316
DBG(("new_thscreen() calloc failed\n"));
320
/* Instantiation parameters */
324
if (oevalues != NULL) {
325
for (i = 0; i < t->oelev; i++) {
326
if (oevalues[i] >= (1 << t->oebpc)) {
327
DBG(("new_thscreen() oevalues[%d] value %d can't fit in %d bits\n",i,oevalues[i],t->oebpc));
331
t->oevalues[i] = oevalues[i];
334
for (i = 0; i < t->oelev; i++)
341
/* Create a suitable LUT from the given function */
342
/* Input is either 8 or 16 bits, output is always 16 bits */
343
DBG(("new_thscreen() about to create LUT\n"));
344
if ((t->lut = (int *)malloc(sizeof(int) * 65536)) == NULL) {
346
DBG(("new_thscreen() malloc of 16 bit LUT failed\n"));
349
for (i = 0; i < 65536; i++) {
350
if (lutfunc != NULL) {
351
double v = i/65535.0;
352
v = lutfunc(cntx, v);
353
t->lut[i] = (int)(v * 65535.0 + 0.5);
358
/* Screen definition parameters */
368
DBG(("new_thscreen() target width %d, height %d, asp %f\n",t->swidth,t->sheight,t->asp));
369
DBG(("new_thscreen() given width %d, height %d, asp %f\n",width,height,asp));
371
npix = t->swidth * t->sheight; /* Total pixels */
372
/* Allow for read of a words worth of pixels from within screen: */
373
DBG(("new_thscreen() tot pix %d, lev %d, bpp %d\n",npix,t->oelev,t->oebpc));
375
t->twidth = t->swidth + (8/t->oebpc) -1;
376
t->theight = t->sheight;
378
DBG(("new_thscreen() th table size = %d x %d\n",t->twidth,t->theight));
379
DBG(("new_thscreen() about to turn screen list into float threshold matrix\n"));
381
/* Convert the list of screen cells into a floating point threshold array */
382
fthr = dmatrix(0, t->sheight-1, 0, t->swidth-1); /* Temporary matrix */
384
double tt = xoff; /* Swap offsets to align with orientation */
387
for (i = 0; i < npix; i++)
388
fthr[thli[i].x][thli[i].y] = (double)(i/(npix - 1.0));
390
for (i = 0; i < npix; i++)
391
fthr[thli[i].y][thli[i].x] = (double)(i/(npix - 1.0));
394
/* The range that the screen has to modulate */
395
/* over to cross all the thresholds evenly. */
396
mrang = 65535.0/(t->oelev - 1.0);
397
DBG(("new_thscreen() raw modulation rande = %f\n",mrang));
399
/* Modify the modulation range to accomodate any level overlap */
400
if (olap > 0.0 && t->oelev > 2) {
401
mrang = ((t->oelev - 2.0) * olap * mrang + 65535.0)/(t->oelev - 1.0);
402
DBG(("new_thscreen() modulation adjusted for overlap = %f\n",mrang));
405
/* Init the threshold table. It holds the quantized, encoded output */
406
/* values, allowing an input value offset by the screen to be */
407
/* thresholded directly into the output value. We allow a guard band at */
408
/* each end for the effects of the screen modulating the input value. */
410
DBG(("new_thscreen() about to init threshold table\n"));
411
t->tht = &t->_tht[32768]; /* base allows for -ve & +ve range */
412
for (i = -32768; i < (2 * 65536) + 32768; i++) {
413
if (i < mrang) { /* Lower guard band */
414
t->tht[i] = t->oevalues[0];
415
} else if (i >= 65535) { /* Upper guard band */
416
t->tht[i] = t->oevalues[t->oelev-1];
417
} else { /* Middle range */
418
t->tht[i] = t->oevalues[1 + (int)((t->oelev - 2.0) * (i - mrang)/(65535.0 - mrang))];
422
/* Allocate the 2D table of pointers into the */
423
/* threshold table that encodes the screen offset. */
424
if ((t->thp = (unsigned char **)malloc(sizeof(unsigned char *)
425
* t->twidth * t->theight)) == NULL) {
426
free_dmatrix(fthr, 0, t->sheight-1, 0, t->swidth-1);
429
DBG(("new_thscreen() malloc of threshold pointer matrix failed\n"));
433
/* Setup the threshold pointer array to point into the apropriate */
434
/* point into the threshold array itself. This implicitly adds */
435
/* the screen pattern offset value to the input before thresholding it. */
436
/* The input screen offsets are applied at this point too. */
437
DBG(("new_thscreen() about to init threshold pointer table\n"));
438
for (i = 0; i < t->twidth; i++) {
439
for (j = 0; j < t->theight; j++) {
440
double sov = fthr[(j+yoff) % t->sheight][(i+xoff) % t->swidth];
441
int tho = (int)((mrang - 1.0) * (1.0 - sov) + 0.5);
442
t->thp[j * t->twidth + i] = &t->tht[tho];
445
free_dmatrix(fthr, 0, t->sheight-1, 0, t->swidth-1);
447
DBG(("new_thscreen() about to setup method pointers\n"));
450
t->screen = thscreen16_8;
453
DBG(("new_thscreen() done\n"));