Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2013-2023 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | #include <stdlib.h> |
17 | | #include <string.h> |
18 | | #include <math.h> |
19 | | #include "ets.h" |
20 | | |
21 | | /* source for threshold matrix - need to improve build process */ |
22 | | #include "ets_tm.h" |
23 | | |
24 | 0 | #define ETS_VERSION 150 |
25 | | |
26 | 0 | #define ETS_SHIFT 16 |
27 | 0 | #define IMO_SHIFT 14 |
28 | | |
29 | | #define FANCY_COUPLING |
30 | | |
31 | | typedef struct { |
32 | | int err; /* Total error carried out of pixel in the line above */ |
33 | | int r; /* expected distance value (see paper for details) */ |
34 | | int a; /* expected distance intermediate value (see paper) */ |
35 | | int b; /* expected distance intermediate value (see paper) */ |
36 | | } ETS_PixelData; |
37 | | |
38 | | typedef struct { |
39 | | int *dst_line; /* Output pointer */ |
40 | | ETS_PixelData *line; /* Internal data for each pixel on the line */ |
41 | | int *lut; /* Table to map from input source value to internal |
42 | | * intensity level. Internal intensity level is 0 to |
43 | | * 1<<ETS_SHIFT. */ |
44 | | int *dist_lut; /* A table of "expected distance between set pixels" |
45 | | * values, stored in fixed point format with (ETS_SHIFT-c1) |
46 | | * fractional bits. Values outside of the 'level 0-1' band |
47 | | * will be set to 0 to avoid ETS weighting being used. */ |
48 | | char *rs_lut; /* Random noise table; values between 0 and 24. x meaning |
49 | | * use 32-x bits of random noise, */ |
50 | | int c1; /* Shift adjustment for the dist_lut. */ |
51 | | unsigned int tm_offset;/* Plane offset within tm data */ |
52 | | int strength; /* Strength */ |
53 | | } ETS_PlaneCtx; |
54 | | |
55 | | typedef unsigned int uint32; |
56 | | |
57 | | typedef void (ETS_LineFn)(ETS_Ctx *etc, unsigned char **dest, const ETS_SrcPixel * const *src); |
58 | | |
59 | | struct _ETS_Ctx { |
60 | | int width; |
61 | | int n_planes; |
62 | | int levels; /* Number of levels on output, <= 256 */ |
63 | | ETS_PlaneCtx ** plane_ctx; |
64 | | int aspect_x; |
65 | | int aspect_y; |
66 | | int elo; |
67 | | int ehi; |
68 | | int *c_line; |
69 | | |
70 | | int ets_bias; |
71 | | int r_style; |
72 | | |
73 | | uint32 seeds[2]; |
74 | | |
75 | | FILE *dump_file; |
76 | | ETS_DumpLevel dump_level; |
77 | | |
78 | | /* Threshold modulation array */ |
79 | | unsigned int y; |
80 | | unsigned int tmwidth; |
81 | | unsigned int tmheight; |
82 | | const signed char *tmmat; |
83 | | |
84 | | ETS_LineFn *line_fn; |
85 | | }; |
86 | | |
87 | | /* Maximum number of planes, but actually we want to dynamically |
88 | | allocate all scratch buffers that depend on this. */ |
89 | | #define M 16 |
90 | | |
91 | | typedef struct { |
92 | | int a; |
93 | | int b; |
94 | | int r; |
95 | | int e_1_0; |
96 | | int e_m1_1; |
97 | | int e_0_1; |
98 | | int e_1_1; |
99 | | } ETS_PixelInternals; |
100 | | |
101 | | /** |
102 | | * ets_line_template: Generic code to perform ETS screening |
103 | | * on an input line. Called to generate optimised versions. |
104 | | */ |
105 | | static inline void |
106 | | ets_line_template(unsigned char * gs_restrict * gs_restrict dest, const ETS_SrcPixel * const gs_restrict * gs_restrict src, int n_planes, int levels, int aspect_x, int aspect_y, int elo, int ehi, int ets_biasing_mode, int r_style, int old_quant, int fancy_coupling, int * gs_restrict c_line, |
107 | | const signed char * gs_restrict tmmat, unsigned int tmwidth, unsigned int tmheight, unsigned int y, int xd, ETS_PlaneCtx * gs_restrict * gs_restrict planes, uint32 *seeds, int in_plane_step, int out_plane_step) |
108 | 0 | { |
109 | 0 | ETS_PixelInternals pi[M]; |
110 | 0 | ETS_PixelInternals * gs_restrict pii; |
111 | 0 | int i; |
112 | 0 | int im; |
113 | 0 | int rg; |
114 | 0 | uint32 seed1, seed2; |
115 | 0 | uint32 sum; |
116 | 0 | int plane_idx; |
117 | 0 | int dith_mul = (old_quant ? levels : levels-1) << 8; |
118 | 0 | int imo_mul = (1 << (ETS_SHIFT + IMO_SHIFT)) / (levels - 1); |
119 | 0 | const int aspect_x2 = aspect_x * aspect_x; |
120 | 0 | const int aspect_y2 = aspect_y * aspect_y; |
121 | 0 | int coupling; |
122 | 0 | int rand_shift; |
123 | 0 | const signed char * gs_restrict tmline = (r_style == ETS_RSTYLE_THRESHOLD) ? (tmmat + (y % tmheight) * tmwidth) : 0; |
124 | | |
125 | | /* Read seeds (but only if we are using them) */ |
126 | 0 | seed1 = (r_style == 2 ? seeds[0] : 0); |
127 | 0 | seed2 = (r_style == 2 ? seeds[1] : 0); |
128 | | |
129 | | /* Setup initial conditions for walking across the scanline. Because we |
130 | | * are dealing with multiple planes, we have arrays of each variable, |
131 | | * indexed by p = plane number. |
132 | | * a[p] = 2x+1 (where x is the horizontal distance to the nearest set pixel) |
133 | | * b[p] = 2y+1 (where y is the vertical distance to the nearest set pixel) |
134 | | * r[p] = distance^2 to the nearest set pixel in this plane. |
135 | | * e_0_1[p] = error from pixel above |
136 | | * e_1_0[p] = error from pixel to the left |
137 | | * e_m1_1[p]= error from pixel above right |
138 | | * e_1_1[p] = error from pixel above left |
139 | | */ |
140 | | |
141 | | /* A potted recap of the distance calculations in the paper for easy |
142 | | * reference. |
143 | | * distance to last dot = SQR( (aspect_y * x)^2 + (aspect_x * y)^2 ) |
144 | | * r = distance^2 = (aspect_y * x)^2 + (aspect_x * y)^2 |
145 | | * = aspect_y^2 * x^2 + aspect_x^2 * y^2 |
146 | | * r_below - r = (aspect_x^2 * (y+1)^2) - (aspect_x^2 * y^2) |
147 | | * = aspect_x^2 * ( (y+1)^2 - y^2 ) |
148 | | * = aspect_x^2 * ( 2y + 1 ) |
149 | | * r_under - r = (aspect_y^2 * (x+1)^2) - (aspect_y^2 * x^2) |
150 | | * = aspect_y^2 * ( (x+1)^2 - x^2 ) |
151 | | * = aspect_y^2 * ( 2x + 1 ) |
152 | | * So, we keep: |
153 | | * a = aspect_y^2 * (2x+1) |
154 | | * b = aspect_x^2 * (2y+1) |
155 | | * And we can then update r by adding either a or b at each stage. |
156 | | */ |
157 | 0 | for (plane_idx = 0; plane_idx < n_planes; plane_idx++) |
158 | 0 | { |
159 | 0 | ETS_PlaneCtx *ctx = planes[plane_idx]; |
160 | |
|
161 | 0 | pi[plane_idx].a = aspect_y2; /* aspect_y^2 * (2x + 1) where x = 0 */ |
162 | 0 | pi[plane_idx].b = aspect_x2; /* aspect_x^2 * (2y + 1) where y = 0 */ |
163 | 0 | pi[plane_idx].r = 0; |
164 | 0 | pi[plane_idx].e_0_1 = 0; |
165 | 0 | pi[plane_idx].e_1_0 = 0; |
166 | 0 | pi[plane_idx].e_m1_1 = ctx->line[0].err; |
167 | 0 | } |
168 | |
|
169 | 0 | coupling = 0; |
170 | |
|
171 | 0 | for (i = 0; i < xd; i++) |
172 | 0 | { |
173 | 0 | if (fancy_coupling) |
174 | 0 | coupling += c_line[i]; |
175 | 0 | else |
176 | 0 | coupling = 0; |
177 | | /* Lookup image data and compute R for all planes. */ |
178 | 0 | pii = pi; |
179 | 0 | for (plane_idx = 0; plane_idx < n_planes; plane_idx++, pii++) |
180 | 0 | { |
181 | 0 | ETS_PlaneCtx *ctx = planes[plane_idx]; |
182 | 0 | ETS_SrcPixel src_pixel = src[plane_idx][i * in_plane_step]; |
183 | 0 | int new_r; |
184 | 0 | int c1 = ctx->c1; |
185 | 0 | int rlimit = 1 << (30 - ETS_SHIFT + c1); |
186 | 0 | unsigned char *dst_ptr = dest[plane_idx]; |
187 | 0 | int new_e_1_0; |
188 | 0 | int achieved_error; |
189 | 0 | int err; |
190 | 0 | int imo; |
191 | 0 | int expected_r; |
192 | 0 | ETS_PixelData * gs_restrict pd = &ctx->line[i]; |
193 | |
|
194 | 0 | im = ctx->lut[src_pixel]; /* image pixel (ink level) */ |
195 | 0 | expected_r = ctx->dist_lut[src_pixel]; /* expected distance */ |
196 | 0 | if (r_style != ETS_RSTYLE_NONE) |
197 | 0 | rand_shift = ctx->rs_lut[src_pixel]; /* random noise shift */ |
198 | | |
199 | | /* Forward pass distance computation; equation 2 from paper */ |
200 | 0 | if (pii->r + pii->a < pd->r) |
201 | 0 | { |
202 | 0 | pii->r += pii->a; |
203 | 0 | pii->a += 2*aspect_y2; |
204 | 0 | } |
205 | 0 | else |
206 | 0 | { |
207 | 0 | pii->a = pd->a; |
208 | 0 | pii->b = pd->b; |
209 | 0 | pii->r = pd->r; |
210 | 0 | } |
211 | | |
212 | | /* Shuffle all the errors and read the next one. */ |
213 | 0 | pii->e_1_1 = pii->e_0_1; |
214 | 0 | pii->e_0_1 = pii->e_m1_1; |
215 | 0 | pii->e_m1_1 = i == xd - 1 ? 0 : pd[1].err; |
216 | | /* Reuse of variables here; new_e_1_0 is the total error passed |
217 | | * into this pixel, with the traditional fs weights. */ |
218 | 0 | new_e_1_0 = ((pii->e_1_0 * 7 + pii->e_m1_1 * 3 + |
219 | 0 | pii->e_0_1 * 5 + pii->e_1_1 * 1) >> 4); |
220 | | |
221 | | /* White pixels stay white */ |
222 | 0 | if (im == 0) |
223 | 0 | { |
224 | 0 | dst_ptr[i * out_plane_step] = 0; |
225 | | /* If we are forcing white pixels to stay white, we should |
226 | | * not propagate errors through them. Or at the very least |
227 | | * we should attenuate such errors. */ |
228 | 0 | new_e_1_0 = 0; |
229 | 0 | } |
230 | 0 | else |
231 | 0 | { |
232 | | /* The guts of ets (Equation 5) */ |
233 | 0 | int ets_bias; |
234 | |
|
235 | 0 | if (expected_r == 0) |
236 | 0 | { |
237 | 0 | ets_bias = 0; |
238 | 0 | } |
239 | 0 | else |
240 | 0 | { |
241 | | /* Read the current distance, and clamp to avoid overflow |
242 | | * in subsequent calculations. */ |
243 | 0 | new_r = pii->r; |
244 | 0 | if (new_r > rlimit) |
245 | 0 | new_r = rlimit; |
246 | | /* Should we store back with the limit? */ |
247 | | |
248 | | /* Change the units on the distance to match our lut |
249 | | * and subtract our actual distance (rg) from the expected |
250 | | * distance (expected_r). */ |
251 | 0 | rg = new_r << (ETS_SHIFT - c1); |
252 | 0 | ets_bias = rg - expected_r; |
253 | | |
254 | | /* So ets_bias is the difference that we want to base our |
255 | | * threshold modulation on (section 2.1 of the paper). |
256 | | * Exactly how do we do that? We present various options |
257 | | * here. |
258 | | * 0 no modulation |
259 | | * 1 what the code did when it came to me. No reference |
260 | | * to this in the paper. |
261 | | * 2 use it unchanged. |
262 | | * 3 like 1, but same shift either side of 0. |
263 | | * 4+ scale the modulation down. |
264 | | */ |
265 | 0 | switch (ets_biasing_mode) |
266 | 0 | { |
267 | 0 | case ETS_BIAS_ZERO: |
268 | 0 | ets_bias = 0; |
269 | 0 | break; |
270 | 0 | case ETS_BIAS_REDUCE_POSITIVE: |
271 | 0 | if (ets_bias > 0) ets_bias >>= 3; |
272 | 0 | break; |
273 | 0 | case ETS_BIAS_NONE: |
274 | 0 | break; |
275 | 0 | case ETS_BIAS_REDUCE: |
276 | 0 | ets_bias >>= 3; |
277 | 0 | break; |
278 | 0 | default: |
279 | 0 | ets_bias /= ets_bias-3; |
280 | 0 | } |
281 | 0 | } |
282 | | |
283 | | /* Non white pixels get biased, and have the error |
284 | | * applied. The error starts from the total error passed |
285 | | * in. */ |
286 | 0 | err = new_e_1_0; |
287 | | |
288 | | /* Plus any ETS bias (calculated above) */ |
289 | 0 | err += ets_bias; |
290 | | |
291 | | /* Plus any random noise. Again various options here: |
292 | | * 0 No random noise |
293 | | * 1 The code as it came to me, using lookup table |
294 | | * 2 commented out when it came to me; using pseudo |
295 | | * random numbers generated from seed. |
296 | | */ |
297 | 0 | switch(r_style) |
298 | 0 | { |
299 | 0 | default: |
300 | 0 | case ETS_RSTYLE_NONE: |
301 | 0 | break; |
302 | 0 | case ETS_RSTYLE_PSEUDO: |
303 | | /* Add the two seeds together */ |
304 | 0 | sum = seed1 + seed2; |
305 | | |
306 | | /* If the add generated a carry, increment |
307 | | * the result of the addition. |
308 | | */ |
309 | 0 | if (sum < seed1 || sum < seed2) sum++; |
310 | | |
311 | | /* Seed2 becomes old seed1, seed1 becomes result */ |
312 | 0 | seed2 = seed1; |
313 | 0 | seed1 = sum; |
314 | |
|
315 | 0 | err -= (sum >> rand_shift) - (0x80000000 >> rand_shift); |
316 | 0 | break; |
317 | 0 | case ETS_RSTYLE_THRESHOLD: |
318 | 0 | err += tmline[((unsigned int)(i+ctx->tm_offset)) % tmwidth] << (24 - rand_shift); |
319 | 0 | break; |
320 | 0 | } |
321 | | |
322 | | /* Clamp the error; this is explained in the paper in |
323 | | * section 6 just after equation 7. */ |
324 | | /* FIXME: Understand this better */ |
325 | 0 | if (err < elo) |
326 | 0 | err = elo; |
327 | 0 | else if (err > ehi) |
328 | 0 | err = ehi; |
329 | | |
330 | | /* Add the coupling to our combined 'error + bias' value */ |
331 | | /* FIXME: Are we sure this shouldn't be clamped? */ |
332 | 0 | err += coupling; |
333 | | |
334 | | /* Calculate imo = the quantised image value (Equation 7) */ |
335 | 0 | imo = ((err + im) * dith_mul + (old_quant ? 0 : (1 << (ETS_SHIFT + 7)))) >> (ETS_SHIFT + 8); |
336 | | |
337 | | /* Clamp to allow for over/underflow due to large errors */ |
338 | 0 | if (imo < 0) imo = 0; |
339 | 0 | else if (imo > levels - 1) imo = levels - 1; |
340 | | |
341 | | /* Store final output pixel */ |
342 | 0 | dst_ptr[i * out_plane_step] = imo; |
343 | | |
344 | | /* Calculate the error between the desired and the obtained |
345 | | * pixel values. */ |
346 | 0 | achieved_error = im - ((imo * imo_mul) >> IMO_SHIFT); |
347 | | |
348 | | /* And the error passed in is updated with the error for |
349 | | * this pixel. */ |
350 | 0 | new_e_1_0 += achieved_error; |
351 | | |
352 | | /* Do the magic coupling here; strengths is 0 when |
353 | | * multiplane optimisation is turned off, hence coupling |
354 | | * remains 0 always. Equation 6. */ |
355 | 0 | coupling += (achieved_error * ctx->strength) >> 8; |
356 | | |
357 | | /* If we output a set pixel, then reset our distances. */ |
358 | 0 | if (imo != 0) |
359 | 0 | { |
360 | 0 | pii->a = aspect_y2; |
361 | 0 | pii->b = aspect_x2; |
362 | 0 | pii->r = 0; |
363 | 0 | } |
364 | 0 | } |
365 | | |
366 | | /* Store the values back for the next pass (Equation 3) */ |
367 | 0 | pd->a = pii->a; |
368 | 0 | pd->b = pii->b; |
369 | 0 | pd->r = pii->r; |
370 | 0 | pd->err = new_e_1_0; |
371 | 0 | pii->e_1_0 = new_e_1_0; |
372 | 0 | } |
373 | 0 | if (fancy_coupling) |
374 | 0 | { |
375 | 0 | coupling = coupling >> 1; |
376 | 0 | c_line[i] = coupling; |
377 | 0 | } |
378 | 0 | } |
379 | | |
380 | | /* Note: this isn't white optimized, but the payoff is probably not |
381 | | that important. */ |
382 | 0 | if (fancy_coupling) |
383 | 0 | { |
384 | 0 | coupling = 0; |
385 | 0 | for (i = xd - 1; i >= 0; i--) |
386 | 0 | { |
387 | 0 | coupling = (coupling + c_line[i]) >> 1; |
388 | 0 | c_line[i] = (coupling - (coupling >> 4)); |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | | /* Update distances. Reverse scanline pass. */ |
393 | 0 | for (plane_idx = 0; plane_idx < n_planes; plane_idx++) |
394 | 0 | { |
395 | 0 | ETS_PlaneCtx *ctx = planes[plane_idx]; |
396 | 0 | int av = aspect_y2; |
397 | 0 | int bv = aspect_x2; |
398 | 0 | int rv = 0; |
399 | 0 | int c1 = ctx->c1; |
400 | 0 | int rlimit = 1 << (30 - ETS_SHIFT + c1); |
401 | 0 | ETS_PixelData * gs_restrict pd = &ctx->line[xd]; |
402 | |
|
403 | 0 | for (i = xd; i > 0; i--) |
404 | 0 | { |
405 | 0 | pd--; |
406 | | /* Equation 4 from the paper */ |
407 | 0 | if (rv + bv + av < pd->r + pd->b) |
408 | 0 | { |
409 | 0 | rv += av; |
410 | 0 | av += (aspect_y2<<1); |
411 | 0 | } |
412 | 0 | else |
413 | 0 | { |
414 | 0 | rv = pd->r; |
415 | 0 | av = pd->a; |
416 | 0 | bv = pd->b; |
417 | 0 | } |
418 | 0 | if (rv > rlimit) rv = rlimit; |
419 | 0 | pd->a = av; |
420 | 0 | pd->b = bv + (aspect_x2 << 1); |
421 | 0 | pd->r = rv + bv; |
422 | 0 | } |
423 | 0 | } |
424 | |
|
425 | 0 | if (r_style == 2) |
426 | 0 | { |
427 | 0 | seeds[0] = seed1; |
428 | 0 | seeds[1] = seed2; |
429 | 0 | } |
430 | 0 | } |
431 | | |
432 | | /** |
433 | | * ets_line: Screen a line using EvenTonedFS screening. |
434 | | * @ctx: An #EBPlaneCtx context. |
435 | | * @dest: Array of destination buffers, 8 bpp pixels each. |
436 | | * @src: Array of source buffer, ET_SrcPixel pixels each. |
437 | | * |
438 | | * Screens a single line using Even ToneFS screening. |
439 | | **/ |
440 | | #ifdef OLD_QUANT |
441 | | #define OLD_QUANT_VAL 1 |
442 | | #else |
443 | 0 | #define OLD_QUANT_VAL 0 |
444 | | #endif |
445 | | #ifdef FANCY_COUPLING |
446 | 0 | #define FANCY_COUPLING_VAL 1 |
447 | | #else |
448 | | #define FANCY_COUPLING_VAL 0 |
449 | | #endif |
450 | | |
451 | | static void |
452 | | ets_line_none(ETS_Ctx *etc, unsigned char **dest, const ETS_SrcPixel * const *src) |
453 | 0 | { |
454 | 0 | ets_line_template(dest, src, etc->n_planes, etc->levels, etc->aspect_x, etc->aspect_y, etc->elo, etc->ehi, etc->ets_bias, ETS_RSTYLE_NONE, |
455 | 0 | OLD_QUANT_VAL, FANCY_COUPLING_VAL, |
456 | 0 | etc->c_line, NULL, 0, 0, etc->y, etc->width, etc->plane_ctx, etc->seeds, etc->n_planes, etc->n_planes); |
457 | 0 | } |
458 | | |
459 | | static void |
460 | | ets_line_threshold(ETS_Ctx *etc, unsigned char **dest, const ETS_SrcPixel * const * src) |
461 | 0 | { |
462 | 0 | ets_line_template(dest, src, etc->n_planes, etc->levels, etc->aspect_x, etc->aspect_y, etc->elo, etc->ehi, etc->ets_bias, ETS_RSTYLE_THRESHOLD, |
463 | 0 | OLD_QUANT_VAL, FANCY_COUPLING_VAL, |
464 | 0 | etc->c_line, etc->tmmat, etc->tmwidth, etc->tmheight, etc->y, etc->width, etc->plane_ctx, etc->seeds, etc->n_planes, etc->n_planes); |
465 | 0 | } |
466 | | |
467 | | static void |
468 | | ets_line_pseudo(ETS_Ctx *etc, unsigned char **dest, const ETS_SrcPixel * const * src) |
469 | 0 | { |
470 | 0 | ets_line_template(dest, src, etc->n_planes, etc->levels, etc->aspect_x, etc->aspect_y, etc->elo, etc->ehi, etc->ets_bias, ETS_RSTYLE_PSEUDO, |
471 | 0 | OLD_QUANT_VAL, FANCY_COUPLING_VAL, |
472 | 0 | etc->c_line, NULL, 0, 0, etc->y, etc->width, etc->plane_ctx, etc->seeds, etc->n_planes, etc->n_planes); |
473 | 0 | } |
474 | | |
475 | | #ifdef UNUSED |
476 | | static void |
477 | | ets_line_default(ETS_Ctx *etc, unsigned char **dest, const ETS_SrcPixel * const * src) |
478 | | { |
479 | | ets_line_template(dest, src, etc->n_planes, etc->levels, etc->aspect_x, etc->aspect_y, etc->elo, etc->ehi, etc->ets_bias, etc->r_style, |
480 | | OLD_QUANT_VAL, FANCY_COUPLING_VAL, |
481 | | etc->c_line, etc->tmmat, etc->tmwidth, etc->tmheight, etc->y, etc->width, etc->plane_ctx, etc->seeds, etc->n_planes, etc->n_planes); |
482 | | } |
483 | | #endif |
484 | | |
485 | | void |
486 | | ets_line(ETS_Ctx *etc, unsigned char **dest, const ETS_SrcPixel * const * gs_restrict src) |
487 | 0 | { |
488 | 0 | etc->line_fn(etc, dest, src); |
489 | 0 | etc->y++; |
490 | 0 | } |
491 | | |
492 | | /** |
493 | | * ets_plane_free: Free an #EBPlaneCtx context. |
494 | | * @ctx: The #EBPlaneCtx context to free. |
495 | | * |
496 | | * Frees @ctx. |
497 | | **/ |
498 | | static void |
499 | | ets_plane_free(void *malloc_arg, ETS_PlaneCtx *ctx) |
500 | 0 | { |
501 | 0 | if (!ctx) |
502 | 0 | return; |
503 | | |
504 | 0 | ets_free(malloc_arg, ctx->line); |
505 | 0 | ets_free(malloc_arg, ctx->lut); |
506 | 0 | ets_free(malloc_arg, ctx->dist_lut); |
507 | 0 | ets_free(malloc_arg, ctx->rs_lut); |
508 | 0 | ets_free(malloc_arg, ctx); |
509 | 0 | } |
510 | | |
511 | | static double |
512 | | compute_distscale(const ETS_Params *params) |
513 | 0 | { |
514 | 0 | double distscale = params->distscale; |
515 | |
|
516 | 0 | if (distscale == 0.0) |
517 | 0 | { |
518 | 0 | distscale = -1; |
519 | 0 | switch(params->aspect_x) |
520 | 0 | { |
521 | 0 | case 1: |
522 | 0 | switch(params->aspect_y) |
523 | 0 | { |
524 | 0 | case 1: |
525 | 0 | distscale = 0.95; |
526 | 0 | break; |
527 | 0 | case 2: |
528 | 0 | distscale = 1.8; |
529 | 0 | break; |
530 | 0 | case 3: |
531 | 0 | distscale = 2.4; /* FIXME */ |
532 | 0 | break; |
533 | 0 | case 4: |
534 | 0 | distscale = 3.6; |
535 | 0 | break; |
536 | 0 | } |
537 | 0 | break; |
538 | 0 | case 2: |
539 | 0 | switch(params->aspect_y) |
540 | 0 | { |
541 | 0 | case 1: |
542 | 0 | distscale = 1.8; |
543 | 0 | break; |
544 | 0 | case 2: |
545 | 0 | break; |
546 | 0 | case 3: |
547 | 0 | distscale = 1.35; /* FIXME */ |
548 | 0 | break; |
549 | 0 | case 4: |
550 | 0 | break; |
551 | 0 | } |
552 | 0 | break; |
553 | 0 | case 3: |
554 | 0 | switch(params->aspect_y) |
555 | 0 | { |
556 | 0 | case 1: |
557 | 0 | distscale = 2.4; /* FIXME */ |
558 | 0 | break; |
559 | 0 | case 2: |
560 | 0 | distscale = 1.35; /* FIXME */ |
561 | 0 | break; |
562 | 0 | case 3: |
563 | 0 | break; |
564 | 0 | case 4: |
565 | 0 | distscale = 0.675; /* FIXME */ |
566 | 0 | break; |
567 | 0 | } |
568 | 0 | break; |
569 | 0 | case 4: |
570 | 0 | switch(params->aspect_y) |
571 | 0 | { |
572 | 0 | case 1: |
573 | 0 | distscale = 3.6; |
574 | 0 | break; |
575 | 0 | case 2: |
576 | 0 | break; |
577 | 0 | case 3: |
578 | 0 | distscale = 0.675; /* FIXME */ |
579 | 0 | break; |
580 | 0 | case 4: |
581 | 0 | break; |
582 | 0 | } |
583 | 0 | break; |
584 | 0 | } |
585 | 0 | if (distscale == -1) |
586 | 0 | { |
587 | 0 | fprintf(stderr, "aspect ratio of %d:%d not supported\n", |
588 | 0 | params->aspect_x, params->aspect_y); |
589 | 0 | exit(1); |
590 | 0 | } |
591 | 0 | } |
592 | 0 | return distscale; |
593 | 0 | } |
594 | | |
595 | | static unsigned int |
596 | | ets_log2(unsigned int x) |
597 | 0 | { |
598 | 0 | unsigned int y = 0; |
599 | 0 | unsigned int z; |
600 | |
|
601 | 0 | for (z = x; z > 1; z = z >> 1) |
602 | 0 | y++; |
603 | 0 | return y; |
604 | 0 | } |
605 | | |
606 | | static unsigned int |
607 | | ets_log2up(unsigned int x) |
608 | 0 | { |
609 | 0 | return ets_log2(x-1)+1; |
610 | 0 | } |
611 | | |
612 | | static int |
613 | | compute_randshift(int nl, int rs_base, int levels) |
614 | 0 | { |
615 | 0 | int rs = rs_base; |
616 | |
|
617 | 0 | if ((nl > (90 << (ETS_SHIFT - 10)) && |
618 | 0 | nl < (129 << (ETS_SHIFT - 10))) || |
619 | 0 | (nl > (162 << (ETS_SHIFT - 10)) && |
620 | 0 | nl < (180 << (ETS_SHIFT - 10)))) |
621 | 0 | rs--; |
622 | 0 | else if (nl > (321 << (ETS_SHIFT - 10)) && |
623 | 0 | nl < (361 << (ETS_SHIFT - 10))) |
624 | 0 | { |
625 | 0 | rs--; |
626 | 0 | if (nl > (331 << (ETS_SHIFT - 10)) && |
627 | 0 | nl < (351 << (ETS_SHIFT - 10))) |
628 | 0 | rs--; |
629 | 0 | } |
630 | 0 | else if ((nl == (levels - 1) << ETS_SHIFT) && |
631 | 0 | nl > (((levels - 1) << ETS_SHIFT) - |
632 | 0 | (1 << (ETS_SHIFT - 2)))) |
633 | 0 | { |
634 | | /* don't add randomness in extreme shadows */ |
635 | 0 | } |
636 | 0 | else if ((nl > (3 << (ETS_SHIFT - 2)))) |
637 | 0 | { |
638 | 0 | nl -= (nl + (1 << (ETS_SHIFT - 2))) & -(1 << (ETS_SHIFT - 1)); |
639 | 0 | if (nl < 0) nl = -nl; |
640 | 0 | if (nl < (1 << (ETS_SHIFT - 4))) rs--; |
641 | 0 | if (nl < (1 << (ETS_SHIFT - 5))) rs--; |
642 | 0 | if (nl < (1 << (ETS_SHIFT - 6))) rs--; |
643 | 0 | } |
644 | 0 | else |
645 | 0 | { |
646 | 0 | if (nl < (3 << (ETS_SHIFT - 3))) nl += 1 << (ETS_SHIFT - 2); |
647 | 0 | nl = nl - (1 << (ETS_SHIFT - 1)); |
648 | 0 | if (nl < 0) nl = -nl; |
649 | 0 | if (nl < (1 << (ETS_SHIFT - 4))) rs--; |
650 | 0 | if (nl < (1 << (ETS_SHIFT - 5))) rs--; |
651 | 0 | if (nl < (1 << (ETS_SHIFT - 6))) rs--; |
652 | 0 | } |
653 | 0 | return rs; |
654 | 0 | } |
655 | | |
656 | | /** |
657 | | * ets_new: Create new Even ToneFS screening context. |
658 | | * @source_width: Width of source buffer. |
659 | | * @dest_width: Width of destination buffer, in pixels. |
660 | | * @lut: Lookup table for gray values. |
661 | | * |
662 | | * Creates a new context for Even ToneFS screening. |
663 | | * |
664 | | * If @dest_width is larger than @source_width, then input lines will |
665 | | * be expanded using nearest-neighbor sampling. |
666 | | * |
667 | | * @lut should be an array of 256 values, one for each possible input |
668 | | * gray value. @lut is a lookup table for gray values. Output is from |
669 | | * 0 for white (no ink) to .... |
670 | | * |
671 | | * |
672 | | * Return value: The new #EBPlaneCtx context. |
673 | | **/ |
674 | | static ETS_PlaneCtx * |
675 | | ets_plane_new(void *malloc_arg, const ETS_Params *params, ETS_Ctx *etc, int plane_idx, int strength) |
676 | 0 | { |
677 | 0 | int width = params->width; |
678 | 0 | int *lut = params->luts[plane_idx]; |
679 | 0 | ETS_PlaneCtx *result; |
680 | 0 | int i; |
681 | 0 | int *new_lut = NULL; |
682 | 0 | int *dist_lut = NULL; |
683 | 0 | char *rs_lut = NULL; |
684 | 0 | double distscale = compute_distscale(params); |
685 | 0 | int c1; |
686 | 0 | int rlimit; |
687 | 0 | int log2_levels, log2_aspect; |
688 | 0 | int rs_base; |
689 | |
|
690 | 0 | result = (ETS_PlaneCtx *)ets_malloc(malloc_arg, sizeof(ETS_PlaneCtx)); |
691 | 0 | if (result == NULL) |
692 | 0 | goto fail; |
693 | | |
694 | 0 | log2_levels = ets_log2(params->levels); |
695 | 0 | log2_aspect = ets_log2(params->aspect_x) + ets_log2(params->aspect_y); /* FIXME */ |
696 | 0 | c1 = 6 + log2_aspect + log2_levels; |
697 | 0 | if (params->c1_scale) |
698 | 0 | c1 -= params->c1_scale[plane_idx]; |
699 | 0 | result->c1 = c1; |
700 | 0 | rlimit = 1 << (30 - ETS_SHIFT + c1); |
701 | 0 | result->tm_offset = TM_WIDTH/ets_log2up(params->n_planes); |
702 | 0 | result->strength = strength; |
703 | | |
704 | | /* Set up a lut to map input values from the source domain to the |
705 | | * amount of ink. Callers can provide a lut of their own, which can be |
706 | | * used for gamma correction etc. In the absence of this, a linear |
707 | | * distribution is assumed. The user supplied lut should map from |
708 | | * 'amount of light' to 'gamma adjusted amount of light', as the code |
709 | | * subtracts the final value from (1<<ETS_SHIFT) (typically 65536) to |
710 | | * get 'amount of ink'. */ |
711 | 0 | new_lut = (int *)ets_malloc(malloc_arg, (ETS_SRC_MAX + 1) * sizeof(int)); |
712 | 0 | if (new_lut == NULL) |
713 | 0 | goto fail; |
714 | 0 | for (i = 0; i < ETS_SRC_MAX + 1; i++) |
715 | 0 | { |
716 | 0 | int nli; |
717 | |
|
718 | 0 | if (lut == NULL) |
719 | 0 | { |
720 | 0 | #if ETS_SRC_MAX == 255 |
721 | 0 | nli = (i * 65793 + (i >> 7)) >> (24 - ETS_SHIFT); |
722 | | #else |
723 | | nli = (i * ((double) (1 << ETS_SHIFT)) / ETS_SRC_MAX) + 0.5; |
724 | | #endif |
725 | 0 | } |
726 | 0 | else |
727 | 0 | nli = lut[i] >> (24 - ETS_SHIFT); |
728 | 0 | if (params->polarity == ETS_BLACK_IS_ZERO) |
729 | 0 | new_lut[i] = (1 << ETS_SHIFT) - nli; |
730 | 0 | else |
731 | 0 | new_lut[i] = nli; |
732 | 0 | } |
733 | | |
734 | | /* Here we calculate 2 more lookup tables. These could be separated out |
735 | | * into 2 different loops, but are done in 1 to avoid a small amount of |
736 | | * recalculation. |
737 | | * dist_lut[i] = expected distance between dots for a greyscale of level i |
738 | | * rs_lut[i] = whacky random noise scale factor. |
739 | | */ |
740 | 0 | dist_lut = (int *)ets_malloc(malloc_arg, (ETS_SRC_MAX + 1) * sizeof(int)); |
741 | 0 | if (dist_lut == NULL) |
742 | 0 | goto fail; |
743 | 0 | rs_lut = (char *)ets_malloc(malloc_arg, (ETS_SRC_MAX + 1) * sizeof(int)); |
744 | 0 | if (rs_lut == NULL) |
745 | 0 | goto fail; |
746 | | |
747 | 0 | rs_base = 35 - ETS_SHIFT + log2_levels - params->rand_scale; |
748 | | |
749 | | /* The paper says that the expected 'value' for a grayshade g is: |
750 | | * d_avg = 0.95 / 0.95/(g^2) |
751 | | * This seems wrong to me. Let's consider some common cases; for a given |
752 | | * greyscale, lay out the 'ideal' dithering, then consider removing each |
753 | | * set pixel in turn and measuring the distance between that pixel and |
754 | | * the closest set pixel. |
755 | | * |
756 | | * g = 1/2 #.#.#.#. visibly, expected distance = SQR(2) |
757 | | * .#.#.#.# |
758 | | * #.#.#.#. |
759 | | * .#.#.#.# |
760 | | * |
761 | | * g = 1/4 #.#.#.#. expected distance = 2 |
762 | | * ........ |
763 | | * #.#.#.#. |
764 | | * ........ |
765 | | * |
766 | | * g = 1/16 #...#... expected distance = 4 |
767 | | * ........ |
768 | | * ........ |
769 | | * ........ |
770 | | * #...#... |
771 | | * ........ |
772 | | * ........ |
773 | | * ........ |
774 | | * |
775 | | * This rough approach leads us to suspect that we should be finding |
776 | | * values roughly proportional to 1/SQR(g). Given the algorithm works in |
777 | | * terms of square distance, this means 1/g. This is at odds with the |
778 | | * value given in the paper. Being charitable and assuming that the paper |
779 | | * means 'squared distance' when it says 'value', we are still a square |
780 | | * off. |
781 | | * |
782 | | * Nonetheless, the code as supplied uses 0.95/g for the squared distance |
783 | | * (i.e. it appears to agree with our logic here). |
784 | | */ |
785 | 0 | for (i = 0; i <= ETS_SRC_MAX; i++) |
786 | 0 | { |
787 | 0 | double dist; |
788 | 0 | int nl = new_lut[i] * (params->levels - 1); |
789 | 0 | int rs; |
790 | | |
791 | | /* This is (or is supposed to be) equation 5 from the paper. If nl |
792 | | * is g, why aren't we dividing by nl*nl ? */ |
793 | 0 | if (nl == 0) |
794 | 0 | { |
795 | | /* The expected distance for an ink level of 0 is infinite. Just |
796 | | * put 0! */ |
797 | 0 | dist = 0; |
798 | 0 | } |
799 | 0 | else if (nl >= ((1<<ETS_SHIFT)/(params->levels-1))) |
800 | 0 | { |
801 | | /* New from RJW: Our distance measurements are only meaningful |
802 | | * within the bottom 'level band' of the output. Do not apply |
803 | | * ETS to higher ink levels. */ |
804 | 0 | dist = 0; |
805 | 0 | } |
806 | 0 | else |
807 | 0 | { |
808 | 0 | dist = (distscale * (1 << (2 * ETS_SHIFT - c1))) / nl; |
809 | 0 | if (dist > rlimit << (ETS_SHIFT - c1)) |
810 | 0 | dist = rlimit << (ETS_SHIFT - c1); |
811 | 0 | } |
812 | |
|
813 | 0 | if (params->rand_scale_luts == NULL) |
814 | 0 | { |
815 | 0 | rs = compute_randshift(nl, rs_base, params->levels); |
816 | 0 | rs_lut[i] = rs; |
817 | 0 | } |
818 | 0 | else |
819 | 0 | { |
820 | 0 | int val = params->rand_scale_luts[plane_idx][i]; |
821 | |
|
822 | 0 | rs_lut[i] = rs_base + 16 - ets_log2(val + (val >> 1)); |
823 | 0 | } |
824 | 0 | dist_lut[i] = (int)dist; |
825 | 0 | } |
826 | |
|
827 | 0 | result->lut = new_lut; |
828 | 0 | result->dist_lut = dist_lut; |
829 | 0 | result->rs_lut = rs_lut; |
830 | |
|
831 | 0 | result->line = (ETS_PixelData *)ets_calloc(malloc_arg, width, sizeof(ETS_PixelData)); |
832 | 0 | if (result->line == NULL) |
833 | 0 | goto fail; |
834 | 0 | for (i = 0; i < width; i++) |
835 | 0 | { |
836 | 0 | result->line[i].a = 1; |
837 | 0 | result->line[i].b = 1; |
838 | | /* Initialize error with a non zero random value to ensure dots don't |
839 | | land on dots when we have same planes with same gray level and |
840 | | the plane interaction option is turned off. Ideally the level |
841 | | of this error should be based upon the values of the first line |
842 | | to ensure that things get primed properly */ |
843 | 0 | result->line[i].err = -((rand () & 0x7fff) << 6) >> (24 - ETS_SHIFT); |
844 | 0 | } |
845 | |
|
846 | 0 | return result; |
847 | 0 | fail: |
848 | 0 | if (result) |
849 | 0 | { |
850 | 0 | ets_free(malloc_arg, new_lut); |
851 | 0 | ets_free(malloc_arg, dist_lut); |
852 | 0 | ets_free(malloc_arg, rs_lut); |
853 | 0 | ets_free(malloc_arg, result->line); |
854 | 0 | } |
855 | 0 | ets_free(malloc_arg, result); |
856 | 0 | return NULL; |
857 | 0 | } |
858 | | |
859 | | |
860 | | /** |
861 | | * ets_destroy: Destroy an #EvenBetterCtx context. |
862 | | * @ctx: The #EvenBetterCtx context to destroy. |
863 | | * |
864 | | * Frees @ctx. |
865 | | **/ |
866 | | void |
867 | | ets_destroy(void *malloc_arg, ETS_Ctx *ctx) |
868 | 0 | { |
869 | 0 | int i; |
870 | 0 | int n_planes; |
871 | |
|
872 | 0 | if (ctx == NULL) |
873 | 0 | return; |
874 | | |
875 | 0 | if (ctx->dump_file) |
876 | 0 | fclose(ctx->dump_file); |
877 | |
|
878 | 0 | n_planes = ctx->n_planes; |
879 | 0 | for (i = 0; i < n_planes; i++) |
880 | 0 | ets_plane_free(malloc_arg, ctx->plane_ctx[i]); |
881 | 0 | ets_free(malloc_arg,ctx->plane_ctx); |
882 | 0 | ets_free(malloc_arg, ctx->c_line); |
883 | |
|
884 | 0 | ets_free(malloc_arg, ctx); |
885 | 0 | } |
886 | | |
887 | | ETS_Ctx * |
888 | | ets_create(void *malloc_arg, const ETS_Params *params) |
889 | 0 | { |
890 | 0 | ETS_Ctx *result = (ETS_Ctx *)ets_malloc(malloc_arg, sizeof(ETS_Ctx)); |
891 | 0 | int n_planes = params->n_planes; |
892 | 0 | int i; |
893 | |
|
894 | 0 | if (result == NULL) |
895 | 0 | return NULL; |
896 | | |
897 | 0 | if (params->dump_file) |
898 | 0 | { |
899 | 0 | int header[5]; |
900 | |
|
901 | 0 | header[0] = 0x70644245; |
902 | 0 | header[1] = 'M' * 0x1010000 + 'I' * 0x101; |
903 | 0 | header[2] = ETS_VERSION; |
904 | 0 | header[3] = ETS_SRC_MAX; |
905 | 0 | header[4] = sizeof(ETS_SrcPixel); |
906 | 0 | fwrite(header, sizeof(int), sizeof(header) / sizeof(header[0]), |
907 | 0 | params->dump_file); |
908 | 0 | if (params->dump_level >= ETS_DUMP_PARAMS) |
909 | 0 | { |
910 | 0 | fwrite(params, 1, sizeof(ETS_Params), params->dump_file); |
911 | 0 | } |
912 | 0 | if (params->dump_level >= ETS_DUMP_LUTS) |
913 | 0 | { |
914 | 0 | for (i = 0; i < params->n_planes; i++) |
915 | 0 | fwrite(params->luts[i], sizeof(int), ETS_SRC_MAX + 1, |
916 | 0 | params->dump_file); |
917 | 0 | } |
918 | 0 | } |
919 | |
|
920 | 0 | result->width = params->width; |
921 | 0 | result->n_planes = n_planes; |
922 | 0 | result->levels = params->levels; |
923 | |
|
924 | 0 | result->aspect_x = params->aspect_x; |
925 | 0 | result->aspect_y = params->aspect_y; |
926 | |
|
927 | 0 | result->ehi = (int)(0.6 * (1 << ETS_SHIFT) / (params->levels - 1)); |
928 | 0 | result->elo = -result->ehi; |
929 | |
|
930 | 0 | result->ets_bias = params->ets_bias; |
931 | 0 | result->r_style = params->r_style; |
932 | |
|
933 | 0 | result->c_line = (int *)ets_calloc(malloc_arg, params->width, sizeof(int)); |
934 | |
|
935 | 0 | result->seeds[0] = 0x5324879f; |
936 | 0 | result->seeds[1] = 0xb78d0945; |
937 | |
|
938 | 0 | result->dump_file = params->dump_file; |
939 | 0 | result->dump_level = params->dump_level; |
940 | |
|
941 | 0 | result->plane_ctx = (ETS_PlaneCtx **)ets_calloc(malloc_arg, n_planes, sizeof(ETS_PlaneCtx *)); |
942 | 0 | if (result->plane_ctx == NULL) |
943 | 0 | goto fail; |
944 | 0 | for (i = 0; i < n_planes; i++) |
945 | 0 | { |
946 | 0 | result->plane_ctx[i] = ets_plane_new(malloc_arg, params, result, i, params->strengths[i]); |
947 | 0 | if (result->plane_ctx[i] == NULL) |
948 | 0 | goto fail; |
949 | 0 | } |
950 | 0 | result->y = 0; |
951 | 0 | result->tmmat = tmmat; |
952 | 0 | result->tmwidth = TM_WIDTH; |
953 | 0 | result->tmheight = TM_HEIGHT; |
954 | | |
955 | | /* Can replace this with optimised versions - for now, just the random ones. */ |
956 | 0 | switch (result->r_style) |
957 | 0 | { |
958 | 0 | default: |
959 | 0 | case ETS_RSTYLE_NONE: |
960 | 0 | result->line_fn = ets_line_none; |
961 | 0 | break; |
962 | 0 | case ETS_RSTYLE_THRESHOLD: |
963 | 0 | result->line_fn = ets_line_threshold; |
964 | 0 | break; |
965 | 0 | case ETS_RSTYLE_PSEUDO: |
966 | 0 | result->line_fn = ets_line_pseudo; |
967 | 0 | break; |
968 | 0 | } |
969 | | |
970 | 0 | return result; |
971 | | |
972 | 0 | fail: |
973 | 0 | ets_destroy(malloc_arg, result); |
974 | 0 | return NULL; |
975 | 0 | } |