/src/ghostpdl/base/gxifast.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2024 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 | | |
17 | | /* Fast monochrome image rendering */ |
18 | | #include "gx.h" |
19 | | #include "memory_.h" |
20 | | #include "gpcheck.h" |
21 | | #include "gsbittab.h" |
22 | | #include "gserrors.h" |
23 | | #include "gxfixed.h" |
24 | | #include "gxarith.h" |
25 | | #include "gxmatrix.h" |
26 | | #include "gsccolor.h" |
27 | | #include "gspaint.h" |
28 | | #include "gsutil.h" |
29 | | #include "gxdevice.h" |
30 | | #include "gxcmap.h" |
31 | | #include "gxdcolor.h" |
32 | | #include "gxgstate.h" |
33 | | #include "gxdevmem.h" |
34 | | #include "gdevmem.h" /* for mem_mono_device */ |
35 | | #include "gxcpath.h" |
36 | | #include "gximage.h" |
37 | | #include "gzht.h" |
38 | | |
39 | | #include "valgrind.h" |
40 | | |
41 | | /* Conditionally include statistics code. */ |
42 | | /* #define COLLECT_STATS_IFAST */ |
43 | | |
44 | | /* ------ Strategy procedure ------ */ |
45 | | |
46 | | /* Check the prototype. */ |
47 | | iclass_proc(gs_image_class_1_simple); |
48 | | |
49 | | /* Use special fast logic for portrait or landscape black-and-white images. */ |
50 | | static irender_proc(image_render_skip); |
51 | | static irender_proc(image_render_simple); |
52 | | static irender_proc(image_render_landscape); |
53 | | |
54 | | int |
55 | | gs_image_class_1_simple(gx_image_enum * penum, irender_proc_t *render_fn) |
56 | 70.7k | { |
57 | 70.7k | fixed ox = dda_current(penum->dda.pixel0.x); |
58 | 70.7k | fixed oy = dda_current(penum->dda.pixel0.y); |
59 | | |
60 | 70.7k | if (penum->use_rop || penum->spp != 1 || penum->bps != 1) |
61 | 62.9k | return 0; |
62 | 7.79k | switch (penum->posture) { |
63 | 4.08k | case image_portrait: |
64 | 4.08k | { /* Use fast portrait algorithm. */ |
65 | 4.08k | long dev_width = |
66 | 4.08k | fixed2long_pixround(ox + penum->x_extent.x) - |
67 | 4.08k | fixed2long_pixround(ox); |
68 | | |
69 | 4.08k | if (dev_width != penum->rect.w) { |
70 | | /* |
71 | | * Add an extra align_bitmap_mod of padding so that |
72 | | * we can align scaled rows with the device. |
73 | | */ |
74 | 3.29k | long line_size = |
75 | 3.29k | bitmap_raster(any_abs(dev_width)) + align_bitmap_mod; |
76 | | |
77 | 3.29k | if (penum->adjust != 0 || line_size > max_uint) |
78 | 1 | return 0; |
79 | | /* Must buffer a scan line. */ |
80 | 3.29k | penum->line_width = any_abs(dev_width); |
81 | 3.29k | penum->line_size = (uint) line_size; |
82 | 3.29k | penum->line = gs_alloc_bytes(penum->memory, |
83 | 3.29k | penum->line_size, "image line"); |
84 | 3.29k | if (penum->line == 0) { |
85 | 0 | return gs_error_VMerror; |
86 | 0 | } |
87 | 3.29k | } |
88 | 4.08k | if_debug2m('b', penum->memory, "[b]render=simple, unpack=copy; rect.w=%d, dev_width=%ld\n", |
89 | 4.08k | penum->rect.w, dev_width); |
90 | 4.08k | *render_fn = image_render_simple; |
91 | 4.08k | break; |
92 | 4.08k | } |
93 | 3.60k | case image_landscape: |
94 | 3.60k | { /* Use fast landscape algorithm. */ |
95 | 3.60k | long dev_width = |
96 | 3.60k | fixed2long_pixround(oy + penum->x_extent.y) - |
97 | 3.60k | fixed2long_pixround(oy); |
98 | 3.60k | long line_size = |
99 | 3.60k | (dev_width = any_abs(dev_width), |
100 | 3.60k | bitmap_raster(dev_width) * 8 + |
101 | 3.60k | ROUND_UP(dev_width, 8) * align_bitmap_mod); |
102 | | |
103 | 3.60k | if ((dev_width != penum->rect.w && penum->adjust != 0) || |
104 | 3.60k | line_size > max_uint |
105 | 3.60k | ) |
106 | 0 | return 0; |
107 | | /* Must buffer a group of 8N scan lines. */ |
108 | 3.60k | penum->line_width = dev_width; |
109 | 3.60k | penum->line_size = (uint) line_size; |
110 | 3.60k | penum->line = gs_alloc_bytes(penum->memory, |
111 | 3.60k | penum->line_size, "image line"); |
112 | 3.60k | if (penum->line == 0) { |
113 | 0 | return gs_error_VMerror; |
114 | 0 | } |
115 | 3.60k | #ifdef PACIFY_VALGRIND |
116 | 3.60k | memset(penum->line, 0, penum->line_size); /* For the number of scan lined < 8 */ |
117 | 3.60k | #endif |
118 | 3.60k | penum->xi_next = penum->line_xy = fixed2int_var_rounded(ox); |
119 | 3.60k | if_debug3m('b', penum->memory, |
120 | 3.60k | "[b]render=landscape, unpack=copy; rect.w=%d, dev_width=%ld, line_size=%ld\n", |
121 | 3.60k | penum->rect.w, dev_width, line_size); |
122 | 3.60k | *render_fn = image_render_landscape; |
123 | | /* Precompute values needed for rasterizing. */ |
124 | 3.60k | penum->dxy = |
125 | 3.60k | float2fixed(penum->matrix.xy + |
126 | 3.60k | fixed2float(fixed_epsilon) / 2); |
127 | 3.60k | break; |
128 | 3.60k | } |
129 | 96 | default: |
130 | 96 | return 0; |
131 | 7.79k | } |
132 | | /* Precompute values needed for rasterizing. */ |
133 | 7.69k | penum->dxx = |
134 | 7.69k | float2fixed(penum->matrix.xx + fixed2float(fixed_epsilon) / 2); |
135 | | /* |
136 | | * We don't want to spread the samples, but we have to reset unpack_bps |
137 | | * to prevent the buffer pointer from being incremented by 8 bytes per |
138 | | * input byte. |
139 | | */ |
140 | 7.69k | penum->unpack = sample_unpack_copy; |
141 | 7.69k | penum->unpack_bps = 8; |
142 | 7.69k | if (penum->use_mask_color) { |
143 | | /* |
144 | | * Set the masked color as 'no_color' to make it transparent |
145 | | * according to the mask color range and the decoding. |
146 | | */ |
147 | 0 | penum->masked = true; |
148 | 0 | if (penum->mask_color.values[0] == 1) { |
149 | | /* if v0 == 1, 1 is transparent since v1 must be == 1 to be a valid range */ |
150 | 0 | set_nonclient_dev_color(penum->map[0].inverted ? penum->icolor0 : penum->icolor1, |
151 | 0 | gx_no_color_index); |
152 | 0 | } else if (penum->mask_color.values[1] == 0) { |
153 | | /* if v1 == 0, 0 is transparent since v0 must be == 0 to be a valid range */ |
154 | 0 | set_nonclient_dev_color(penum->map[0].inverted ? penum->icolor1 : penum->icolor0, |
155 | 0 | gx_no_color_index); |
156 | 0 | } else { |
157 | | /* |
158 | | * The only other possible in-range value is v0 = 0, v1 = 1. |
159 | | * The image is completely transparent! |
160 | | */ |
161 | 0 | *render_fn = image_render_skip; |
162 | 0 | } |
163 | 0 | penum->map[0].decoding = sd_none; |
164 | 0 | } |
165 | 7.69k | return 0; |
166 | 7.79k | } |
167 | | |
168 | | /* ------ Rendering procedures ------ */ |
169 | | |
170 | | #define DC_IS_NULL(pdc)\ |
171 | 423k | (gx_dc_is_pure(pdc) && (pdc)->colors.pure == gx_no_color_index) |
172 | | |
173 | | /* Skip over a completely transparent image. */ |
174 | | static int |
175 | | image_render_skip(gx_image_enum * penum, const byte * buffer, int data_x, |
176 | | uint w, int h, gx_device * dev) |
177 | 0 | { |
178 | 0 | return h; |
179 | 0 | } |
180 | | |
181 | | /* |
182 | | * Scale (and possibly reverse) one scan line of a monobit image. |
183 | | * This is used for both portrait and landscape image processing. |
184 | | * We pass in an x offset (0 <= line_x < align_bitmap_mod * 8) so that |
185 | | * we can align the result with the eventual device X. |
186 | | * |
187 | | * To be precise, the input to this routine is the w bits starting at |
188 | | * bit data_x in buffer. These w bits expand to abs(x_extent) bits, |
189 | | * either inverted (zero = 0xff) or not (zero = 0), starting at bit |
190 | | * line_x in line which corresponds to coordinate |
191 | | * fixed2int_pixround(xcur + min(x_extent, 0)). Note that the entire |
192 | | * bytes containing the first and last output bits are affected: the |
193 | | * other bits in those bytes are set to zero (i.e., the value of the |
194 | | * 'zero' argument). |
195 | | */ |
196 | | #ifdef COLLECT_STATS_IFAST |
197 | | struct stats_image_fast_s { |
198 | | long |
199 | | calls, all0s, all1s, runs, lbit0, byte00, byte01, byte02, byte03, |
200 | | byte04, rbit0, lbit1, byte1, rbit1, thin, thin2, nwide, bwide, |
201 | | nfill, bfill; |
202 | | } stats_image_fast; |
203 | | # define INCS(stat) ++stats_image_fast.stat |
204 | | # define ADDS(stat, n) stats_image_fast.stat += n |
205 | | #else |
206 | 2.06M | # define INCS(stat) DO_NOTHING |
207 | 24.9k | # define ADDS(stat, n) DO_NOTHING |
208 | | #endif |
209 | | static inline void |
210 | | fill_row(byte *line, int line_x, uint raster, int value) |
211 | 82.0k | { |
212 | 82.0k | memset(line + (line_x >> 3), value, raster - (line_x >> 3)); |
213 | 82.0k | } |
214 | | |
215 | | static void |
216 | | image_simple_expand(byte * line, int line_x, uint raster, |
217 | | const byte * buffer, int data_x, uint w, |
218 | | fixed xcur, fixed x_extent, byte zero /* 0 or 0xff */ ) |
219 | 82.0k | { |
220 | 82.0k | int dbitx = data_x & 7; |
221 | 82.0k | byte sbit = 0x80 >> dbitx; |
222 | 82.0k | byte sbitmask = 0xff >> dbitx; |
223 | 82.0k | uint wx = dbitx + w; |
224 | 82.0k | gx_dda_fixed xl; |
225 | 82.0k | gx_dda_step_fixed dxx4, dxx8, dxx16, dxx24, dxx32; |
226 | 82.0k | register const byte *psrc = buffer + (data_x >> 3); |
227 | | |
228 | | /* |
229 | | * The following 3 variables define the end of the input data row. |
230 | | * We would put them in a struct, except that no compiler that we |
231 | | * know of will optimize individual struct members as though they |
232 | | * were simple variables (e.g., by putting them in registers). |
233 | | * |
234 | | * endp points to the byte that contains the bit just beyond the |
235 | | * end of the row. endx gives the bit number of this bit within |
236 | | * the byte, with 0 being the *least* significant bit. endbit is |
237 | | * a mask for this bit. |
238 | | */ |
239 | 82.0k | const byte *endp = psrc + (wx >> 3); |
240 | 82.0k | int endx = ~wx & 7; |
241 | 82.0k | byte endbit = 1 << endx; |
242 | | |
243 | | /* |
244 | | * The following 3 variables do the same for start of the last run |
245 | | * of the input row (think of it as a pointer to just beyond the |
246 | | * end of the next-to-last run). |
247 | | */ |
248 | 82.0k | const byte *stop = endp; |
249 | 82.0k | int stopx; |
250 | 82.0k | byte stopbit = endbit; |
251 | 82.0k | byte data; |
252 | 82.0k | byte one = ~zero; |
253 | 82.0k | fixed xl0; |
254 | 82.0k | #ifdef PACIFY_VALGRIND |
255 | 82.0k | byte vbits; |
256 | 82.0k | #endif |
257 | | |
258 | 82.0k | if (w == 0) |
259 | 0 | return; |
260 | 82.0k | INCS(calls); |
261 | | |
262 | | /* Scan backward for the last transition. */ |
263 | 82.0k | if (stopbit == 0x80) |
264 | 6.58k | --stop, stopbit = 1; |
265 | 75.4k | else |
266 | 75.4k | stopbit <<= 1; |
267 | | /* Now (stop, stopbit) give the last bit of the row. */ |
268 | 82.0k | #ifdef PACIFY_VALGRIND |
269 | | /* Here, we are dealing with a row of bits, rather than bytes. |
270 | | * If the width of the bits is not a multiple of 8, we don't |
271 | | * fill out the last byte, and valgrind (correctly) tracks the |
272 | | * bits in that last byte as being a mix of defined and undefined. |
273 | | * When we are scanning through the row bitwise, everything works |
274 | | * fine, but our "skip whole bytes" code can confuse valgrind. |
275 | | * We know that we won't match the "data == 0xff" for the final |
276 | | * byte (i.e. the undefinedness of some of the bits doesn't matter |
277 | | * to the correctness of the routine), but valgrind is not smart |
278 | | * enough to realise that we know this. Accordingly, we get a false |
279 | | * positive "undefined memory read". |
280 | | * How do we fix this? Well, one way would be to read in the |
281 | | * partial last byte, and explicitly set the undefined bits to |
282 | | * be 0. |
283 | | * *stop &= ~(stopbit-1); |
284 | | * Unfortunately, stop is a const *, so we can't do that (we could |
285 | | * break const, but it is just conceivable that the caller might |
286 | | * pass the next string of bits out in a later call, and so we |
287 | | * might be corrupting valid data). |
288 | | * Instead, we make a call to a valgrind helper. */ |
289 | 82.0k | VALGRIND_GET_VBITS(stop,&vbits,1); |
290 | 82.0k | if ((vbits & stopbit)==0) { /* At least our stop bit must be addressable already! */ |
291 | 82.0k | byte zero = 0; |
292 | 82.0k | VALGRIND_SET_VBITS(stop,&zero,1); |
293 | 82.0k | } |
294 | 82.0k | #endif |
295 | 82.0k | { |
296 | 82.0k | byte stopmask = (-stopbit << 1) & 0xff; |
297 | 82.0k | byte last = *stop; |
298 | | |
299 | 82.0k | if (stop == psrc) /* only 1 input byte */ |
300 | 53.5k | stopmask &= sbitmask; |
301 | 82.0k | if (last & stopbit) { |
302 | | /* The last bit is a 1: look for a 0-to-1 transition. */ |
303 | 12.8k | if (~last & stopmask) { /* Transition in last byte. */ |
304 | 6.11k | last |= stopbit - 1; |
305 | 6.69k | } else { /* No transition in the last byte. */ |
306 | 57.9k | while (stop > psrc && stop[-1] == 0xff) |
307 | 51.2k | --stop; |
308 | 6.69k | if (stop == psrc || |
309 | 6.69k | (stop == psrc + 1 && !(~*psrc & sbitmask)) |
310 | 6.69k | ) { |
311 | | /* The input is all 1s. Clear the row and exit. */ |
312 | 595 | INCS(all1s); |
313 | 595 | fill_row(line, line_x, raster, one); |
314 | 595 | goto end; |
315 | 595 | } |
316 | 6.10k | last = *--stop; |
317 | 6.10k | } |
318 | 12.2k | stopx = byte_bit_run_length_0[byte_reverse_bits[last]] - 1; |
319 | 69.2k | } else { |
320 | | /* The last bit is a 0: look for a 1-to-0 transition. */ |
321 | 69.2k | if (last & stopmask) { /* Transition in last byte. */ |
322 | 37.7k | last &= -stopbit; |
323 | 37.7k | } else { /* No transition in the last byte. */ |
324 | 246k | while (stop > psrc && stop[-1] == 0) |
325 | 215k | --stop; |
326 | 31.4k | if (stop == psrc || |
327 | 31.4k | (stop == psrc + 1 && !(*psrc & sbitmask)) |
328 | 31.4k | ) { |
329 | | /* The input is all 0s. Clear the row and exit. */ |
330 | 22.9k | INCS(all0s); |
331 | 22.9k | fill_row(line, line_x, raster, zero); |
332 | 22.9k | goto end; |
333 | 22.9k | } |
334 | 8.50k | last = *--stop; |
335 | 8.50k | } |
336 | 46.2k | stopx = byte_bit_run_length_0[byte_reverse_bits[last ^ 0xff]] - 1; |
337 | 46.2k | } |
338 | 58.4k | if (stopx < 0) |
339 | 3.95k | stopx = 7, ++stop; |
340 | 58.4k | stopbit = 1 << stopx; |
341 | 58.4k | } |
342 | | |
343 | | /* Pre-clear the row. */ |
344 | 0 | fill_row(line, line_x, raster, zero); |
345 | | |
346 | | |
347 | | /* Extreme negative values of x_extent cause the xl0 calculation |
348 | | * to explode. Workaround this here. */ |
349 | 58.4k | if (x_extent < min_int + 0x100) |
350 | 0 | x_extent += 0x100; |
351 | | |
352 | | /* Set up the DDAs. */ |
353 | 58.4k | xl0 = |
354 | 58.4k | (x_extent >= 0 ? |
355 | 22.5k | fixed_fraction(fixed_pre_pixround(xcur)) : |
356 | 58.4k | fixed_fraction(fixed_pre_pixround(xcur + x_extent)) - x_extent); |
357 | 58.4k | xl0 += int2fixed(line_x); |
358 | | /* We should never get a negative x10 here. If we do, all bets are off. */ |
359 | 58.4k | if (xl0 < 0) |
360 | 0 | xl0 = 0, x_extent = 0; |
361 | 58.4k | dda_init(xl, xl0, x_extent, w); |
362 | 58.4k | dxx4 = xl.step; |
363 | 58.4k | dda_step_add(dxx4, xl.step); |
364 | | /* egcc - 2.91.66 generates incorrect code for |
365 | | * dda_step_add(dxx4, dxx4); |
366 | | * Using the temp variable. |
367 | | */ |
368 | 58.4k | dxx8 = dxx4; |
369 | 58.4k | dda_step_add(dxx4, dxx8); |
370 | 58.4k | dxx8 = dxx4; |
371 | 58.4k | dda_step_add(dxx8, dxx4); |
372 | 58.4k | dxx16 = dxx8; |
373 | 58.4k | dda_step_add(dxx16, dxx8); |
374 | 58.4k | dxx24 = dxx16; |
375 | 58.4k | dda_step_add(dxx24, dxx8); |
376 | 58.4k | dxx32 = dxx24; |
377 | 58.4k | dda_step_add(dxx32, dxx8); |
378 | | |
379 | | /* |
380 | | * Loop invariants: |
381 | | * data = *psrc; |
382 | | * sbit = 1 << n, 0<=n<=7. |
383 | | */ |
384 | 265k | for (data = *psrc;;) { |
385 | 265k | int x0, n, bit; |
386 | 265k | byte *bp; |
387 | 265k | static const byte lmasks[9] = { |
388 | 265k | 0xff, 0x7f, 0x3f, 0x1f, 0xf, 7, 3, 1, 0 |
389 | 265k | }; |
390 | 265k | static const byte rmasks[9] = { |
391 | 265k | 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff |
392 | 265k | }; |
393 | | |
394 | 265k | INCS(runs); |
395 | | |
396 | | /* Scan a run of zeros. */ |
397 | 265k | data ^= 0xff; /* invert */ |
398 | 838k | while (data & sbit) { |
399 | 573k | dda_next(xl); |
400 | 573k | sbit >>= 1; |
401 | 573k | INCS(lbit0); |
402 | 573k | } |
403 | 265k | if (!sbit) { /* Scan a run of zero bytes. */ |
404 | 111k | sw: if ((data = psrc[1]) != 0) { |
405 | 53.7k | psrc++; |
406 | 53.7k | INCS(byte00); |
407 | 57.8k | } else if ((data = psrc[2]) != 0) { |
408 | 10.4k | dda_state_next(xl.state, dxx8); |
409 | 10.4k | psrc += 2; |
410 | 10.4k | INCS(byte01); |
411 | 47.3k | } else if ((data = psrc[3]) != 0) { |
412 | 6.68k | dda_state_next(xl.state, dxx16); |
413 | 6.68k | psrc += 3; |
414 | 6.68k | INCS(byte02); |
415 | 40.6k | } else if ((data = psrc[4]) != 0) { |
416 | 3.72k | dda_state_next(xl.state, dxx24); |
417 | 3.72k | psrc += 4; |
418 | 3.72k | INCS(byte03); |
419 | 36.9k | } else { |
420 | 36.9k | dda_state_next(xl.state, dxx32); |
421 | 36.9k | psrc += 4; |
422 | 36.9k | INCS(byte04); |
423 | 36.9k | goto sw; |
424 | 36.9k | } |
425 | 74.5k | if (data > 0xf) |
426 | 55.3k | sbit = 0x80; |
427 | 19.2k | else { |
428 | 19.2k | sbit = 0x08; |
429 | 19.2k | dda_state_next(xl.state, dxx4); |
430 | 19.2k | } |
431 | 74.5k | data ^= 0xff; /* invert */ |
432 | 148k | while (data & sbit) { |
433 | 73.9k | dda_next(xl); |
434 | 73.9k | sbit >>= 1; |
435 | 73.9k | INCS(rbit0); |
436 | 73.9k | } |
437 | 74.5k | } |
438 | 265k | x0 = dda_current_fixed2int(xl); |
439 | 265k | if (psrc >= stop && sbit == stopbit) { |
440 | | /* |
441 | | * We've scanned the last run of 0s. |
442 | | * Prepare to fill the final run of 1s. |
443 | | * Use int64_t to avoid overflow. |
444 | | */ |
445 | 12.2k | n = fixed2int((int64_t)xl0 + (int64_t)x_extent) - x0; |
446 | 252k | } else { /* Scan a run of ones. */ |
447 | | /* We know the current bit is a one. */ |
448 | 252k | data ^= 0xff; /* un-invert */ |
449 | 540k | do { |
450 | 540k | dda_next(xl); |
451 | 540k | sbit >>= 1; |
452 | 540k | INCS(lbit1); |
453 | 540k | } |
454 | 540k | while (data & sbit); |
455 | 252k | if (!sbit) { /* Scan a run of 0xff bytes. */ |
456 | 126k | while ((data = *++psrc) == 0xff) { |
457 | 55.3k | dda_state_next(xl.state, dxx8); |
458 | 55.3k | INCS(byte1); |
459 | 55.3k | } |
460 | 71.5k | if (data < 0xf0) |
461 | 52.8k | sbit = 0x80; |
462 | 18.6k | else { |
463 | 18.6k | sbit = 0x08; |
464 | 18.6k | dda_state_next(xl.state, dxx4); |
465 | 18.6k | } |
466 | 144k | while (data & sbit) { |
467 | 72.9k | dda_next(xl); |
468 | 72.9k | sbit >>= 1; |
469 | 72.9k | INCS(rbit1); |
470 | 72.9k | } |
471 | 71.5k | } |
472 | 252k | n = dda_current_fixed2int(xl) - x0; |
473 | 252k | } |
474 | | |
475 | | /* Fill the run in the scan line. */ |
476 | 265k | if (n < 0) |
477 | 51.6k | x0 += n, n = -n; |
478 | 265k | bp = line + (x0 >> 3); |
479 | 265k | bit = x0 & 7; |
480 | 265k | if ((n += bit) <= 8) { |
481 | 171k | *bp ^= lmasks[bit] - lmasks[n]; |
482 | 171k | INCS(thin); |
483 | 171k | } else if ((n -= 8) <= 8) { |
484 | 68.3k | *bp ^= lmasks[bit]; |
485 | 68.3k | bp[1] ^= rmasks[n]; |
486 | 68.3k | INCS(thin2); |
487 | 68.3k | } else { |
488 | 24.9k | *bp++ ^= lmasks[bit]; |
489 | 24.9k | if (n >= 56) { |
490 | 7.45k | int nb = n >> 3; |
491 | | |
492 | 7.45k | memset(bp, one, nb); |
493 | 7.45k | bp += nb; |
494 | 7.45k | INCS(nwide); |
495 | 7.45k | ADDS(bwide, nb); |
496 | 17.5k | } else { |
497 | 17.5k | ADDS(bfill, n >> 3); |
498 | 54.0k | while ((n -= 8) >= 0) |
499 | 36.5k | *bp++ = one; |
500 | 17.5k | INCS(nfill); |
501 | 17.5k | } |
502 | 24.9k | *bp ^= rmasks[n & 7]; |
503 | 24.9k | } |
504 | 265k | if (psrc >= stop && sbit == stopbit) |
505 | 58.4k | break; |
506 | 265k | } |
507 | 82.0k | end: |
508 | 82.0k | { |
509 | 82.0k | #ifdef PACIFY_VALGRIND |
510 | 82.0k | VALGRIND_SET_VBITS(stop,&vbits,1); |
511 | 82.0k | #endif |
512 | 82.0k | } |
513 | 82.0k | } |
514 | | |
515 | | /* Copy one rendered scan line to the device. */ |
516 | | static int |
517 | | copy_portrait(gx_image_enum * penum, const byte * data, int dx, int raster, |
518 | | int x, int y, int w, int h, gx_device * dev) |
519 | 136k | { |
520 | 136k | const gx_device_color *pdc0; |
521 | 136k | const gx_device_color *pdc1; |
522 | 136k | uint align = ALIGNMENT_MOD(data, align_bitmap_mod); |
523 | | |
524 | | /* |
525 | | * We know that the lookup table maps 1 bit to 1 bit, |
526 | | * so it can only have 2 states: straight-through or invert. |
527 | | */ |
528 | 136k | if (penum->map[0].table.lookup4x1to32[0]) |
529 | 25.9k | pdc0 = penum->icolor1, pdc1 = penum->icolor0; |
530 | 110k | else |
531 | 110k | pdc0 = penum->icolor0, pdc1 = penum->icolor1; |
532 | 136k | data -= align; |
533 | 136k | dx += align << 3; |
534 | 136k | if (gx_dc_is_pure(pdc0) && gx_dc_is_pure(pdc1)) { |
535 | | /* Just use copy_mono. */ |
536 | 92.5k | dev_proc_copy_mono((*copy_mono)) = |
537 | 92.5k | (h == 1 || (raster & (align_bitmap_mod - 1)) == 0 ? |
538 | 92.5k | dev_proc(dev, copy_mono) : gx_copy_mono_unaligned); |
539 | 92.5k | return (*copy_mono) |
540 | 92.5k | (dev, data, dx, raster, gx_no_bitmap_id, |
541 | 92.5k | x, y, w, h, pdc0->colors.pure, pdc1->colors.pure); |
542 | 92.5k | } |
543 | | /* |
544 | | * At least one color isn't pure: if the other one is transparent, use |
545 | | * the opaque color's fill_masked procedure. Note that we use a |
546 | | * slightly unusual representation for transparent here (per |
547 | | * gx_begin_image1): a pure color with pixel value gx_no_color_index. |
548 | | */ |
549 | 44.0k | { |
550 | 44.0k | const gx_device_color *pdc; |
551 | 44.0k | bool invert; |
552 | | |
553 | 44.0k | if (DC_IS_NULL(pdc1)) { |
554 | 1.91k | pdc = pdc0; |
555 | 1.91k | invert = true; |
556 | 42.1k | } else { |
557 | 42.1k | if (!DC_IS_NULL(pdc0)) { |
558 | 0 | int code = gx_device_color_fill_rectangle |
559 | 0 | (pdc0, x, y, w, h, dev, lop_default, NULL); |
560 | |
|
561 | 0 | if (code < 0) |
562 | 0 | return code; |
563 | 0 | } |
564 | 42.1k | pdc = pdc1; |
565 | 42.1k | invert = false; |
566 | 42.1k | } |
567 | 44.0k | return (*pdc->type->fill_masked) |
568 | 44.0k | (pdc, data, dx, raster, gx_no_bitmap_id, x, y, w, h, |
569 | 44.0k | dev, lop_default, invert); |
570 | | |
571 | 44.0k | } |
572 | 44.0k | } |
573 | | |
574 | | /* Rendering procedure for a monobit image with no */ |
575 | | /* skew or rotation and pure colors. */ |
576 | | static int |
577 | | image_render_simple(gx_image_enum * penum, const byte * buffer, int data_x, |
578 | | uint w, int h, gx_device * dev) |
579 | 94.8k | { |
580 | 94.8k | dev_proc_copy_mono((*copy_mono)) = dev_proc(dev, copy_mono); |
581 | 94.8k | const fixed dxx = penum->dxx; |
582 | 94.8k | const byte *line; |
583 | 94.8k | uint line_width, line_size; |
584 | 94.8k | int line_x; |
585 | 94.8k | fixed xcur = dda_current(penum->dda.pixel0.x); |
586 | 94.8k | int ix = fixed2int_pixround(xcur); |
587 | 94.8k | int ixr; |
588 | 94.8k | const int iy = penum->yci, ih = penum->hci; |
589 | 94.8k | gx_device_color * const pdc0 = penum->icolor0; |
590 | 94.8k | gx_device_color * const pdc1 = penum->icolor1; |
591 | 94.8k | int dy; |
592 | 94.8k | int code; |
593 | | |
594 | 94.8k | if (h == 0) |
595 | 10.6k | return 0; |
596 | 84.1k | if ((!DC_IS_NULL(pdc0) && |
597 | 84.1k | (code = gx_color_load(pdc0, penum->pgs, dev)) < 0) || |
598 | 84.1k | (!DC_IS_NULL(pdc1) && |
599 | 84.1k | (code = gx_color_load(pdc1, penum->pgs, dev)) < 0) |
600 | 84.1k | ) |
601 | 0 | return code; |
602 | 84.1k | if (penum->line == 0) { /* A direct BitBlt is possible. */ |
603 | 42.0k | line = buffer; |
604 | 42.0k | line_size = (w + 7) >> 3; |
605 | 42.0k | line_width = w; |
606 | 42.0k | line_x = 0; |
607 | 42.1k | } else if (copy_mono == mem_mono_copy_mono && |
608 | 42.1k | dxx > 0 && gx_dc_is_pure(pdc1) && gx_dc_is_pure(pdc0) && |
609 | | /* We know the colors must be (0,1) or (1,0). */ |
610 | 42.1k | (pdc0->colors.pure ^ pdc1->colors.pure) == 1 && |
611 | 42.1k | !penum->clip_image && |
612 | | /* |
613 | | * Even if clip_image is false, the clipping rectangle |
614 | | * might lie partly outside the device coordinate space |
615 | | * if the Margins values are non-zero. |
616 | | */ |
617 | 42.1k | ix >= 0 && |
618 | 42.1k | (ixr = fixed2int_pixround(xcur + penum->x_extent.x) - 1) < |
619 | 0 | dev->width && |
620 | 42.1k | iy >= 0 && iy + ih <= dev->height |
621 | 42.1k | ) { |
622 | | /* Do the operation directly into the memory device bitmap. */ |
623 | 0 | int line_ix; |
624 | 0 | int ib_left = ix >> 3, ib_right = ixr >> 3; |
625 | 0 | byte *scan_line = scan_line_base((gx_device_memory *) dev, iy); |
626 | 0 | byte save_left, save_right, mask; |
627 | |
|
628 | 0 | line_x = ix & (align_bitmap_mod * 8 - 1); |
629 | 0 | line_ix = ix - line_x; |
630 | 0 | line_size = (ixr >> 3) + 1 - (line_ix >> 3); |
631 | 0 | line_width = ixr + 1 - ix; |
632 | | /* We must save and restore any unmodified bits in */ |
633 | | /* the two edge bytes. */ |
634 | 0 | save_left = scan_line[ib_left]; |
635 | 0 | save_right = scan_line[ib_right]; |
636 | 0 | image_simple_expand(scan_line + (line_ix >> 3), line_x, |
637 | 0 | line_size, buffer, data_x, w, xcur, |
638 | 0 | penum->x_extent.x, |
639 | 0 | (byte)((pdc0->colors.pure == 0) != |
640 | 0 | (penum->map[0].table.lookup4x1to32[0] == 0) ? |
641 | 0 | 0xff : 0)); |
642 | 0 | if (ix & 7) |
643 | 0 | mask = (byte) (0xff00 >> (ix & 7)), |
644 | 0 | scan_line[ib_left] = |
645 | 0 | (save_left & mask) + (scan_line[ib_left] & ~mask); |
646 | 0 | if ((ixr + 1) & 7) |
647 | 0 | mask = (byte) (0xff00 >> ((ixr + 1) & 7)), |
648 | 0 | scan_line[ib_right] = |
649 | 0 | (scan_line[ib_right] & mask) + (save_right & ~mask); |
650 | 0 | if (ih <= 1) |
651 | 0 | return 1; |
652 | | /****** MAY BE UNALIGNED ******/ |
653 | 0 | line = scan_line + (line_ix >> 3); |
654 | 0 | for (dy = 1; dy < ih; dy++) { |
655 | 0 | int code = (*copy_mono) |
656 | 0 | (dev, line, line_x, line_size, gx_no_bitmap_id, |
657 | 0 | ix, iy + dy, line_width, 1, |
658 | 0 | (gx_color_index)0, (gx_color_index)1); |
659 | |
|
660 | 0 | if (code < 0) |
661 | 0 | return code; |
662 | 0 | } |
663 | 0 | return 0; |
664 | 42.1k | } else { |
665 | 42.1k | line = penum->line; |
666 | 42.1k | line_size = penum->line_size; |
667 | 42.1k | line_width = penum->line_width; |
668 | 42.1k | line_x = ix & (align_bitmap_mod * 8 - 1); |
669 | 42.1k | image_simple_expand(penum->line, line_x, line_size, |
670 | 42.1k | buffer, data_x, w, xcur, |
671 | 42.1k | penum->x_extent.x, 0); |
672 | 42.1k | } |
673 | | |
674 | | /* Finally, transfer the scan line to the device. */ |
675 | 84.1k | if (dxx < 0) |
676 | 13.2k | ix -= line_width; |
677 | 209k | for (dy = 0; dy < ih; dy++) { |
678 | 125k | int code = copy_portrait(penum, line, line_x, line_size, |
679 | 125k | ix, iy + dy, line_width, 1, dev); |
680 | | |
681 | 125k | if (code < 0) |
682 | 0 | return code; |
683 | 125k | } |
684 | | |
685 | 84.1k | return 1; |
686 | 84.1k | } |
687 | | |
688 | | /* Rendering procedure for a 90 degree rotated monobit image */ |
689 | | /* with pure colors. We buffer and then flip 8 scan lines at a time. */ |
690 | | static int copy_landscape(gx_image_enum *, int, int, bool, gx_device *); |
691 | | static int |
692 | | image_render_landscape(gx_image_enum * penum, const byte * buffer, int data_x, |
693 | | uint w, int h, gx_device * dev) |
694 | 43.6k | { |
695 | 43.6k | byte *line = penum->line; |
696 | 43.6k | uint raster = bitmap_raster(penum->line_width); |
697 | 43.6k | int ix = penum->xci, iw = penum->wci; |
698 | 43.6k | int xinc, xmod; |
699 | 43.6k | byte *row; |
700 | 43.6k | const byte *orig_row = 0; |
701 | 43.6k | bool y_neg = (is_fneg(penum->matrix.xy)); |
702 | | |
703 | 43.6k | if (is_fneg(penum->matrix.yx)) |
704 | 21 | ix += iw, iw = -iw, xinc = -1; |
705 | 43.5k | else |
706 | 43.5k | xinc = 1; |
707 | | /* |
708 | | * Because of clipping, there may be discontinuous jumps in the values |
709 | | * of ix (xci). If this happens, or if we are at the end of the data or |
710 | | * a client has requested flushing, flush the flipping buffer. |
711 | | */ |
712 | 43.6k | if (ix != penum->xi_next || h == 0) { |
713 | 7.21k | int xi = penum->xi_next; |
714 | 7.21k | int code = |
715 | 7.21k | (xinc > 0 ? |
716 | 7.20k | copy_landscape(penum, penum->line_xy, xi, y_neg, dev) : |
717 | 7.21k | copy_landscape(penum, xi, penum->line_xy, y_neg, dev)); |
718 | | |
719 | 7.21k | if (code < 0) |
720 | 0 | return code; |
721 | 7.21k | penum->line_xy = penum->xi_next = ix; |
722 | 7.21k | if (h == 0) |
723 | 7.21k | return code; |
724 | 7.21k | } |
725 | 98.8k | for (; iw != 0; iw -= xinc) { |
726 | 62.4k | if (xinc < 0) |
727 | 304 | --ix; |
728 | 62.4k | xmod = ix & 7; |
729 | 62.4k | row = line + xmod * raster; |
730 | 62.4k | if (orig_row == 0) { |
731 | 39.9k | image_simple_expand(row, 0, raster, |
732 | 39.9k | buffer, data_x, w, |
733 | 39.9k | dda_current(penum->dda.pixel0.y), |
734 | 39.9k | penum->x_extent.y, 0); |
735 | 39.9k | orig_row = row; |
736 | 39.9k | } else |
737 | 22.5k | memcpy(row, orig_row, raster); |
738 | 62.4k | if (xinc > 0) { |
739 | 62.1k | ++ix; |
740 | 62.1k | if (xmod == 7) { |
741 | 7.99k | int code = |
742 | 7.99k | copy_landscape(penum, penum->line_xy, ix, y_neg, dev); |
743 | | |
744 | 7.99k | if (code < 0) |
745 | 0 | return code; |
746 | 7.99k | orig_row = 0; |
747 | 7.99k | penum->line_xy = ix; |
748 | 7.99k | } |
749 | 62.1k | } else { |
750 | 304 | if (xmod == 0) { |
751 | 35 | int code = |
752 | 35 | copy_landscape(penum, ix, penum->line_xy, y_neg, dev); |
753 | | |
754 | 35 | if (code < 0) |
755 | 0 | return code; |
756 | 35 | orig_row = 0; |
757 | 35 | penum->line_xy = ix; |
758 | 35 | } |
759 | 304 | } |
760 | 62.4k | } |
761 | 36.4k | penum->xi_next = ix; |
762 | 36.4k | return 0; |
763 | 36.4k | } |
764 | | |
765 | | /* Flip and copy one group of scan lines. */ |
766 | | static int |
767 | | copy_landscape(gx_image_enum * penum, int x0, int x1, bool y_neg, |
768 | | gx_device * dev) |
769 | 15.2k | { |
770 | 15.2k | byte *line = penum->line; |
771 | 15.2k | uint line_width = penum->line_width; |
772 | 15.2k | uint raster = bitmap_raster(line_width); |
773 | 15.2k | byte *flipped = line + raster * 8; |
774 | 15.2k | int w = x1 - x0; |
775 | 15.2k | int y = fixed2int_pixround(dda_current(penum->dda.pixel0.y)); |
776 | | |
777 | 15.2k | if (w == 0 || line_width == 0) |
778 | 4.39k | return 0; |
779 | | /* Flip the buffered data from raster x 8 to align_bitmap_mod x */ |
780 | | /* line_width. */ |
781 | 10.8k | if (line_width > 0) { |
782 | 10.8k | int i = (line_width-1)>>3; |
783 | | |
784 | 10.8k | #ifdef PACIFY_VALGRIND |
785 | 10.8k | if (line_width & 7) { |
786 | 10.6k | memflip8x8_eol(line + i, raster, |
787 | 10.6k | flipped + (i << (log2_align_bitmap_mod + 3)), |
788 | 10.6k | align_bitmap_mod, |
789 | 10.6k | line_width & 7); |
790 | 10.6k | i--; |
791 | 10.6k | } |
792 | 10.8k | #endif |
793 | | |
794 | 20.8k | for (; i >= 0; --i) |
795 | 10.0k | memflip8x8(line + i, raster, |
796 | 10.0k | flipped + (i << (log2_align_bitmap_mod + 3)), |
797 | 10.0k | align_bitmap_mod); |
798 | 10.8k | } |
799 | | /* Transfer the scan lines to the device. */ |
800 | 10.8k | if (w < 0) |
801 | 0 | x0 = x1, w = -w; |
802 | 10.8k | if (y_neg) |
803 | 10.8k | y -= line_width; |
804 | 10.8k | return copy_portrait(penum, flipped, x0 & 7, align_bitmap_mod, |
805 | 10.8k | x0, y, w, line_width, dev); |
806 | 15.2k | } |