summaryrefslogtreecommitdiff
path: root/render/thscreen.h
blob: f14a8a44e004730fef7d9ac52d1f9cd9714ad0d3 (plain)
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

#ifndef THSCREEN_H
#define THSCREEN_H

/*
 * render2d
 *
 * Threshold or Error diffusion screen pixel processing object.
 * (Simplified from DPS code)
 *
 * Author:  Graeme W. Gill
 * Date:    11/7/2005
 * Version: 1.00
 *
 * Copyright 2005, 2012, 2014 Graeme W. Gill
 * All rights reserved.
 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
 * see the License.txt file for licencing details.
 *
 */

#define THMXCH2D  8           /* Maximum color channels */

/* Light Separation in screening flag */
typedef enum {
	scls_false = 0,		/* Don't do light ink separation during screening. */
	scls_true  = 0		/* Do light ink separation during screening. */
} sc_lightsep;

/* Input encoding */
typedef enum {
	scie_8  = 0,		/*  8 bit per component */
	scie_16 = 1			/* 16 bit per component */
} sc_iencoding;

/* Output bit order within byte */
typedef enum {
	scoo_l = 0,		/* Little endian */
	scoo_b = 1		/* Big endian */
} sc_oorder;


/* ---------------------------- */
/* Setup a set of screens */

struct _thscreens {
	int np;						/* Number of planes */
	struct _thscreen **sc;		/* List of screens */

	int oebpc;					/* Output encoding bits per component, 1,2,4 or 8 */
	int oelev;					/* Output encoding levels. Must be <= 2 ^ oebpc */
	int oevalues[256];			/* Output encoding values for each level */

	int edif;					/* nz if using error diffusion */
	int **luts;					/* Lookup tables */
	int mxwidth;				/* max width in pixels of raster to be screened */ 
	int lastyoff;				/* Last y offset */
	float **ebuf;				/* Error buffer for each plane */
								/* ebuf[][-1] is used for next pixel error */
	
	void (*quant)(void *qcntx, double *out, double *in); /* optional quantization func. for edif */
	void *qcntx;				/* Context for quant */
	double mxerr;				/* if != 0, max error to propogate */

	sobol *so;					/* Random number generator for error diffusion */

	/* Screen pixel values */
	void (* screen)(			/* Pointer to dither function */
		struct _thscreens *t,	/* Screening object pointer */
		int width, int height,	/* Width and height to screen in pixels */
		int xoff, int yoff,		/* Offset into screening pattern */
		unsigned char *out,		/* Output pixel buffer */
		unsigned long opitch,	/* Increment between output lines in components */
		unsigned char *in,		/* Input pixel buffer */
		unsigned long ipitch);	/* Increment between input lines in components */

	void (* del)(				/* Destructor */
		struct _thscreens *t);	/* Screening objects pointer */

}; typedef struct _thscreens thscreens;


/* Return a thscreens object */
/* Screen data is used that best matches the requested parameters. */
/* Return NULL on error */
thscreens *new_thscreens(
	int exact,				/* Return only exact matches */
	int nplanes,			/* Number of planes to screen */
	double asp,				/* Target aspect ratio (== dpiX/dpiY) */
	int size,				/* Target screen size */
	sc_iencoding ie,		/* Input encoding - must be scie_16 */
	int oebpc,				/* Output encoding bits per component - must be 8 */
	int oelev,				/* Output encoding levels. Must be <= 2 ^ oebpc */
	int *oevalues,			/* Optional output encoding values for each level */
							/* Must be oelev entries. Default is 0 .. oelev-1 */
	sc_oorder oo,			/* Output bit ordering */
	double overlap,			/* Overlap between levels, 0 - 1.0 */
	int mxwidth,			/* max width in pixels of raster to be screened */ 
	void   **cntx,			/* List of contexts for lookup table callback */
	double (**lutfunc)(void *cntx, double in),	/* List of callback function, NULL if none */
	int edif,				/* nz if using error diffusion */
	void (*quant)(void *qcntx, double *out, double *in), /* optional quantization func. for edif */
	void *qcntx,
	double mxerr			/* If error diffusion anf != 0, max error to propogate */
);

/* ---------------------------- */
/* Screen defintion information */

typedef struct {
	int x;
	int y;
} ccoord;

typedef struct {
	int size;			/* General size */
	int width;			/* width in pixels */
	int height;			/* Height in pixels */
	double asp;			/* Aspect ratio (== dpiX/dpiY) */
	int joint;			/* na for joint screens */
	ccoord **list;		/* Pointer to list of pointers to threshold coordinates */
} thscdef;

/* ------------------------ */
/* Setup of a single screen */

struct _thscreen {
	sc_iencoding ie;			/* Input encoding */
	int oebpc;					/* Output encoding bits per component, 1,2,4 or 8 */
	int oelev;					/* Output encoding levels. Must be <= 2 ^ oebpc */
	int oevalues[256];			/* Output encoding values for each level */
	sc_oorder oo;				/* Output bit ordering */
	double asp;					/* Aspect ratio (== dpiX/dpiY) */
	double overlap;				/* Overlap between levels, 0 - 1.0 */
	int *lut;					/* Lookup table */
	unsigned char _tht[65536 * 3];/* Threshold table */
	unsigned char *tht;			/* Pointer to base of threshold table */
	unsigned char **thp;		/* Pointers to threshold table (offset int _tht) */
	int swidth;					/* Given screen width */
	int sheight;				/* Given screen height */
	int twidth;					/* Rounded up screen table width & stride */
	int theight;				/* Screen table height */ 

	void (* screen)(			/* Pointer to dither function */
		struct _thscreen *t,	/* Screening object pointer */
		int width, int height,	/* Width and height to screen in pixels */
		int xoff, int yoff,		/* Offset into screening pattern */
		unsigned char *out,		/* Output pixel buffer */
		unsigned long opinc,	/* Increment between output pixels in components */
		unsigned long opitch,	/* Increment between output lines in components */
		unsigned char *in,		/* Input pixel buffer */
		unsigned long ipinc,	/* Increment between input pixels in components */
		unsigned long ipitch);	/* Increment between input lines in components */

	void (* del)(				/* Destructor */
		struct _thscreen *t);	/* Screening object pointer */

}; typedef struct _thscreen thscreen;

/* Create a new thscreen object */
/* Return NULL on error */
thscreen *new_thscreen(
	int width,					/* width in pixels of screen */
	int height,					/* Height in pixels of screen */
	int xoff, int yoff,			/* Pattern offsets into width & height (must be +ve) */
	double asp,					/* Aspect ratio (== dpiX/dpiY) */
	int swap,					/* Swap X & Y to invert aspect ratio */
	ccoord *thli,				/* List of screen initialisation threshold coordinates */
	sc_iencoding ie,			/* Input encoding - must be scie_16 */
	int oebpc,					/* Output encoding bits per component - must be 8 */
	int oelev,					/* Output encoding levels. Must be <= 2 ^ oebpc */
	int *oevalues,				/* Optional output encoding values for each level */
								/* Must be oelev entries. Default is 0 .. oelev-1 */
	sc_oorder oo,				/* Output bit ordering */
	double olap,				/* Overlap between levels, 0 - 1.0 */
	void   *cntx,				/* Context for LUT table callback */
	double (*lutfunc)(void *cntx, double in)	/* Callback function, NULL if none */
);

#endif /* THSCREEN_H */