74
74
last_bit = width_bits + bit - (chunk_bits + 1);
76
76
if (last_bit < 0) { /* <=1 chunk */
77
set_mono_thin_mask(right_mask, width_bits, bit);
79
FOR_EACH_LINE(*ptr &= ~right_mask;);
80
else if (pattern == (mono_fill_chunk)(-1))
81
FOR_EACH_LINE(*ptr |= right_mask;);
84
*ptr = (*ptr & ~right_mask) | (pattern & right_mask); );
77
set_mono_thin_mask(right_mask, width_bits, bit);
79
FOR_EACH_LINE(*ptr &= ~right_mask;);
80
else if (pattern == (mono_fill_chunk)(-1))
81
FOR_EACH_LINE(*ptr |= right_mask;);
84
*ptr = (*ptr & ~right_mask) | (pattern & right_mask); );
87
int last = last_bit >> chunk_log2_bits;
89
set_mono_left_mask(mask, bit);
90
set_mono_right_mask(right_mask, (last_bit & chunk_bit_mask) + 1);
92
case 0: /* 2 chunks */
94
FOR_EACH_LINE(*ptr &= ~mask; ptr[1] &= ~right_mask;);
95
else if (pattern == (mono_fill_chunk)(-1))
96
FOR_EACH_LINE(*ptr |= mask; ptr[1] |= right_mask;);
99
*ptr = (*ptr & ~mask) | (pattern & mask);
100
ptr[1] = (ptr[1] & ~right_mask) | (pattern & right_mask); );
102
case 1: /* 3 chunks */
104
FOR_EACH_LINE( *ptr &= ~mask;
106
ptr[2] &= ~right_mask; );
107
else if (pattern == (mono_fill_chunk)(-1))
108
FOR_EACH_LINE( *ptr |= mask;
110
ptr[2] |= right_mask; );
112
FOR_EACH_LINE( *ptr = (*ptr & ~mask) | (pattern & mask);
114
ptr[2] = (ptr[2] & ~right_mask) | (pattern & right_mask); );
116
default:{ /* >3 chunks */
117
uint byte_count = (last_bit >> 3) & -chunk_bytes;
120
FOR_EACH_LINE( *ptr &= ~mask;
121
memset(ptr + 1, 0, byte_count);
122
ptr[last + 1] &= ~right_mask; );
123
else if (pattern == (mono_fill_chunk)(-1))
124
FOR_EACH_LINE( *ptr |= mask;
125
memset(ptr + 1, 0xff, byte_count);
126
ptr[last + 1] |= right_mask; );
129
*ptr = (*ptr & ~mask) | (pattern & mask);
130
memset(ptr + 1, (byte) pattern, byte_count);
131
ptr[last + 1] = (ptr[last + 1] & ~right_mask) |
132
(pattern & right_mask); );
87
int last = last_bit >> chunk_log2_bits;
89
set_mono_left_mask(mask, bit);
90
set_mono_right_mask(right_mask, (last_bit & chunk_bit_mask) + 1);
92
case 0: /* 2 chunks */
94
FOR_EACH_LINE(*ptr &= ~mask; ptr[1] &= ~right_mask;);
95
else if (pattern == (mono_fill_chunk)(-1))
96
FOR_EACH_LINE(*ptr |= mask; ptr[1] |= right_mask;);
99
*ptr = (*ptr & ~mask) | (pattern & mask);
100
ptr[1] = (ptr[1] & ~right_mask) | (pattern & right_mask); );
102
case 1: /* 3 chunks */
104
FOR_EACH_LINE( *ptr &= ~mask;
106
ptr[2] &= ~right_mask; );
107
else if (pattern == (mono_fill_chunk)(-1))
108
FOR_EACH_LINE( *ptr |= mask;
110
ptr[2] |= right_mask; );
112
FOR_EACH_LINE( *ptr = (*ptr & ~mask) | (pattern & mask);
114
ptr[2] = (ptr[2] & ~right_mask) | (pattern & right_mask); );
116
default:{ /* >3 chunks */
117
uint byte_count = (last_bit >> 3) & -chunk_bytes;
120
FOR_EACH_LINE( *ptr &= ~mask;
121
memset(ptr + 1, 0, byte_count);
122
ptr[last + 1] &= ~right_mask; );
123
else if (pattern == (mono_fill_chunk)(-1))
124
FOR_EACH_LINE( *ptr |= mask;
125
memset(ptr + 1, 0xff, byte_count);
126
ptr[last + 1] |= right_mask; );
129
*ptr = (*ptr & ~mask) | (pattern & mask);
130
memset(ptr + 1, (byte) pattern, byte_count);
131
ptr[last + 1] = (ptr[last + 1] & ~right_mask) |
132
(pattern & right_mask); );
136
136
#undef FOR_EACH_LINE
162
162
last_bit = width_bits + bit - (chunk_bits + 1);
164
164
if (last_bit < 0) { /* <=1 chunk */
165
set_mono_thin_mask(right_mask, width_bits, bit);
166
right_mask &= ~src_mask;
168
FOR_EACH_LINE(*ptr &= ~right_mask;);
169
else if (pattern == (mono_fill_chunk)(-1))
170
FOR_EACH_LINE(*ptr |= right_mask;);
173
*ptr = (*ptr & ~right_mask) | (pattern & right_mask); );
165
set_mono_thin_mask(right_mask, width_bits, bit);
166
right_mask &= ~src_mask;
168
FOR_EACH_LINE(*ptr &= ~right_mask;);
169
else if (pattern == (mono_fill_chunk)(-1))
170
FOR_EACH_LINE(*ptr |= right_mask;);
173
*ptr = (*ptr & ~right_mask) | (pattern & right_mask); );
176
int last = last_bit >> chunk_log2_bits;
176
int last = last_bit >> chunk_log2_bits;
178
set_mono_left_mask(mask, bit);
179
set_mono_right_mask(right_mask, (last_bit & chunk_bit_mask) + 1);
181
right_mask &= ~src_mask;
183
case 0: /* 2 chunks */
185
FOR_EACH_LINE(*ptr &= ~mask; ptr[1] &= ~right_mask;);
186
else if (pattern == (mono_fill_chunk)(-1))
187
FOR_EACH_LINE(*ptr |= mask; ptr[1] |= right_mask;);
190
*ptr = (*ptr & ~mask) | (pattern & mask);
191
ptr[1] = (ptr[1] & ~right_mask) | (pattern & right_mask); );
193
case 1: /* 3 chunks */
195
FOR_EACH_LINE( *ptr &= ~mask;
197
ptr[2] &= ~right_mask; );
198
else if (pattern == (mono_fill_chunk)(-1))
199
FOR_EACH_LINE( *ptr |= mask;
201
ptr[2] |= right_mask; );
203
FOR_EACH_LINE( *ptr = (*ptr & ~mask) | (pattern & mask);
204
ptr[1] =(ptr[1] & src_mask) | pattern;
205
ptr[2] = (ptr[2] & ~right_mask) | (pattern & right_mask); );
207
default:{ /* >3 chunks */
178
set_mono_left_mask(mask, bit);
179
set_mono_right_mask(right_mask, (last_bit & chunk_bit_mask) + 1);
181
right_mask &= ~src_mask;
183
case 0: /* 2 chunks */
185
FOR_EACH_LINE(*ptr &= ~mask; ptr[1] &= ~right_mask;);
186
else if (pattern == (mono_fill_chunk)(-1))
187
FOR_EACH_LINE(*ptr |= mask; ptr[1] |= right_mask;);
190
*ptr = (*ptr & ~mask) | (pattern & mask);
191
ptr[1] = (ptr[1] & ~right_mask) | (pattern & right_mask); );
193
case 1: /* 3 chunks */
195
FOR_EACH_LINE( *ptr &= ~mask;
197
ptr[2] &= ~right_mask; );
198
else if (pattern == (mono_fill_chunk)(-1))
199
FOR_EACH_LINE( *ptr |= mask;
201
ptr[2] |= right_mask; );
203
FOR_EACH_LINE( *ptr = (*ptr & ~mask) | (pattern & mask);
204
ptr[1] =(ptr[1] & src_mask) | pattern;
205
ptr[2] = (ptr[2] & ~right_mask) | (pattern & right_mask); );
207
default:{ /* >3 chunks */
211
FOR_EACH_LINE( *ptr++ &= ~mask;
212
for (i = 0; i < last; i++)
214
*ptr &= ~right_mask; );
215
else if (pattern == (mono_fill_chunk)(-1))
216
FOR_EACH_LINE( *ptr++ |= mask;
217
for (i = 0; i < last; i++)
219
*ptr |= right_mask; );
222
/* note: we know (pattern & ~src_mask) == pattern */
223
*ptr = (*ptr & ~mask) | (pattern & mask);
225
for (i = 0; i < last; i++, ptr++)
211
FOR_EACH_LINE( *ptr++ &= ~mask;
212
for (i = 0; i < last; i++)
214
*ptr &= ~right_mask; );
215
else if (pattern == (mono_fill_chunk)(-1))
216
FOR_EACH_LINE( *ptr++ |= mask;
217
for (i = 0; i < last; i++)
219
*ptr |= right_mask; );
222
/* note: we know (pattern & ~src_mask) == pattern */
223
*ptr = (*ptr & ~mask) | (pattern & mask);
225
for (i = 0; i < last; i++, ptr++)
226
226
*ptr = (*ptr & src_mask) | pattern;
227
*ptr = (*ptr & ~right_mask) | (pattern & right_mask); );
227
*ptr = (*ptr & ~right_mask) | (pattern & right_mask); );
231
231
#undef FOR_EACH_LINE
244
244
if (!(width & 7)) {
245
uint src_bytes = width >> 3;
246
uint dest_bytes = replicated_width >> 3;
248
for (y = height; y-- > 0;
249
orig_row -= raster, tile_row -= replicated_raster
251
uint move = src_bytes;
252
const byte *from = orig_row;
253
byte *to = tile_row + dest_bytes - src_bytes;
255
memmove(to, from, move);
256
while (to - tile_row >= move) {
259
memmove(to, from, move);
263
memmove(tile_row, to, to - tile_row);
245
uint src_bytes = width >> 3;
246
uint dest_bytes = replicated_width >> 3;
248
for (y = height; y-- > 0;
249
orig_row -= raster, tile_row -= replicated_raster
251
uint move = src_bytes;
252
const byte *from = orig_row;
253
byte *to = tile_row + dest_bytes - src_bytes;
255
memmove(to, from, move);
256
while (to - tile_row >= move) {
259
memmove(to, from, move);
263
memmove(tile_row, to, to - tile_row);
267
* This algorithm is inefficient, but probably not worth improving.
269
uint bit_count = width & (uint)(-(int)width); /* lowest bit: 1, 2, or 4 */
270
uint left_mask = (0xff00 >> bit_count) & 0xff;
272
for (y = height; y-- > 0;
273
orig_row -= raster, tile_row -= replicated_raster
277
for (sx = width; sx > 0;) {
281
bits = (orig_row[sx >> 3] << (sx & 7)) & left_mask;
282
for (dx = sx + replicated_width; dx >= width;) {
288
dp = tile_row + (dx >> 3);
289
*dp = (*dp & ~(left_mask >> dbit)) | (bits >> dbit);
267
* This algorithm is inefficient, but probably not worth improving.
269
uint bit_count = width & (uint)(-(int)width); /* lowest bit: 1, 2, or 4 */
270
uint left_mask = (0xff00 >> bit_count) & 0xff;
272
for (y = height; y-- > 0;
273
orig_row -= raster, tile_row -= replicated_raster
277
for (sx = width; sx > 0;) {
281
bits = (orig_row[sx >> 3] << (sx & 7)) & left_mask;
282
for (dx = sx + replicated_width; dx >= width;) {
288
dp = tile_row + (dx >> 3);
289
*dp = (*dp & ~(left_mask >> dbit)) | (bits >> dbit);
296
296
/* Replicate a bitmap vertically in place. */
298
298
bits_replicate_vertically(byte * data, uint height, uint raster,
299
uint replicated_height)
299
uint replicated_height)
301
301
byte *dest = data;
302
302
uint h = replicated_height;
303
303
uint size = raster * height;
305
305
while (h > height) {
306
memcpy(dest + size, dest, size);
306
memcpy(dest + size, dest, size);
313
313
/* Assume bits beyond the width are zero. */
315
315
bits_bounding_box(const byte * data, uint height, uint raster,
318
318
register const ulong *lp;
319
319
static const byte first_1[16] = {
320
4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
320
4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0
322
322
static const byte last_1[16] = {
323
0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4
323
0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4
326
326
/* Count trailing blank rows. */
341
341
lp = (const ulong *)data;
345
uint n = ((const byte *)lp - data) / raster;
345
uint n = ((const byte *)lp - data) / raster;
349
height -= n, data += n * raster;
349
height -= n, data += n * raster;
352
352
/* Find the left and right edges. */
353
353
/* We know that the first and last rows are non-blank. */
356
uint raster_longs = raster >> arch_log2_sizeof_long;
357
uint left = raster_longs - 1, right = 0;
358
ulong llong = 0, rlong = 0;
362
for (q = data, h = height; h-- > 0; q += raster) { /* Work from the left edge by longs. */
363
for (lp = (const ulong *)q, n = 0;
364
n < left && !*lp; lp++, n++
367
left = n, llong = *lp;
370
/* Work from the right edge by longs. */
371
for (lp = (const ulong *)(q + raster - sizeof(long)),
372
n = raster_longs - 1;
374
n > right && !*lp; lp--, n--
377
right = n, rlong = *lp;
382
/* Do binary subdivision on edge longs. We assume that */
383
/* sizeof(long) = 4 or 8. */
356
uint raster_longs = raster >> arch_log2_sizeof_long;
357
uint left = raster_longs - 1, right = 0;
358
ulong llong = 0, rlong = 0;
362
for (q = data, h = height; h-- > 0; q += raster) { /* Work from the left edge by longs. */
363
for (lp = (const ulong *)q, n = 0;
364
n < left && !*lp; lp++, n++
367
left = n, llong = *lp;
370
/* Work from the right edge by longs. */
371
for (lp = (const ulong *)(q + raster - sizeof(long)),
372
n = raster_longs - 1;
374
n > right && !*lp; lp--, n--
377
right = n, rlong = *lp;
382
/* Do binary subdivision on edge longs. We assume that */
383
/* sizeof(long) = 4 or 8. */
384
384
#if arch_sizeof_long > 8
385
Error_longs_are_too_large();
385
Error_longs_are_too_large();
388
388
#if arch_is_big_endian
395
395
# define right_justify_last(x,n) (x) >>= ((arch_sizeof_long * 8) - (n))
398
left <<= arch_log2_sizeof_long + 3;
399
#if arch_sizeof_long == 8
400
if (llong & ~last_bits(32))
401
shift_out_last(llong, 32);
405
if (llong & ~last_bits(16))
406
shift_out_last(llong, 16);
409
if (llong & ~last_bits(8))
410
shift_out_last(llong, 8);
413
right_justify_last(llong, 8);
415
left += first_1[(byte) llong >> 4];
417
left += first_1[(byte) llong] + 4;
419
right <<= arch_log2_sizeof_long + 3;
420
#if arch_sizeof_long == 8
421
if (!(rlong & last_bits(32)))
422
shift_out_last(rlong, 32);
426
if (!(rlong & last_bits(16)))
427
shift_out_last(rlong, 16);
430
if (!(rlong & last_bits(8)))
431
shift_out_last(rlong, 8);
434
right_justify_last(rlong, 8);
436
right += last_1[(byte) rlong >> 4];
438
right += last_1[(uint) rlong & 0xf] + 4;
398
left <<= arch_log2_sizeof_long + 3;
399
#if arch_sizeof_long == 8
400
if (llong & ~last_bits(32))
401
shift_out_last(llong, 32);
405
if (llong & ~last_bits(16))
406
shift_out_last(llong, 16);
409
if (llong & ~last_bits(8))
410
shift_out_last(llong, 8);
413
right_justify_last(llong, 8);
415
left += first_1[(byte) llong >> 4];
417
left += first_1[(byte) llong] + 4;
419
right <<= arch_log2_sizeof_long + 3;
420
#if arch_sizeof_long == 8
421
if (!(rlong & last_bits(32)))
422
shift_out_last(rlong, 32);
426
if (!(rlong & last_bits(16)))
427
shift_out_last(rlong, 16);
430
if (!(rlong & last_bits(8)))
431
shift_out_last(rlong, 8);
434
right_justify_last(rlong, 8);
436
right += last_1[(byte) rlong >> 4];
438
right += last_1[(uint) rlong & 0xf] + 4;
466
466
/* Check for the fast CMYK cases. */
467
467
if (!(source_bit | dest_bit)) {
468
switch (source_depth) {
471
(dest_depth == 1 && !(source->raster & 3) &&
472
!(source->x & 1) ? EXTRACT_4_TO_1 :
476
if (dest_depth == 8 && !(shift & 7)) {
477
loop_case = EXTRACT_32_TO_8;
478
source_row += 3 - (shift >> 3);
468
switch (source_depth) {
471
(dest_depth == 1 && !(source->raster & 3) &&
472
!(source->x & 1) ? EXTRACT_4_TO_1 :
476
if (dest_depth == 8 && !(shift & 7)) {
477
loop_case = EXTRACT_32_TO_8;
478
source_row += 3 - (shift >> 3);
483
483
for (y = 0; y < height;
484
++y, source_row += source->raster, dest_row += dest->raster
489
case EXTRACT_4_TO_1: {
490
const byte *src = source_row;
491
byte *dst = dest_row;
493
/* Do groups of 8 pixels. */
494
for (x = width; x >= 8; src += 4, x -= 8) {
496
(*(const bits32 *)src >> shift) & 0x11111111;
499
byte_acegbdfh_to_abcdefgh[(
484
++y, source_row += source->raster, dest_row += dest->raster
489
case EXTRACT_4_TO_1: {
490
const byte *src = source_row;
491
byte *dst = dest_row;
493
/* Do groups of 8 pixels. */
494
for (x = width; x >= 8; src += 4, x -= 8) {
496
(*(const bits32 *)src >> shift) & 0x11111111;
499
byte_acegbdfh_to_abcdefgh[(
500
500
#if arch_is_big_endian
501
(sword >> 21) | (sword >> 14) | (sword >> 7) | sword
501
(sword >> 21) | (sword >> 14) | (sword >> 7) | sword
503
(sword << 3) | (sword >> 6) | (sword >> 15) | (sword >> 24)
503
(sword << 3) | (sword >> 6) | (sword >> 15) | (sword >> 24)
508
/* Do the final 1-7 pixels. */
509
uint test = 0x10 << shift, store = 0x80;
512
*dst = (*src & test ? *dst | store : *dst & ~store);
522
case EXTRACT_32_TO_8: {
523
const byte *src = source_row;
524
byte *dst = dest_row;
526
for (x = width; x > 0; src += 4, --x)
531
sample_load_declare_setup(sptr, sbit, source_row, source_bit,
533
sample_store_declare_setup(dptr, dbit, dbbyte, dest_row, dest_bit,
536
sample_store_preload(dbbyte, dptr, dbit, dest_depth);
537
for (x = width; x > 0; --x) {
538
gx_color_index color;
541
sample_load_next_any(color, sptr, sbit, source_depth);
542
pixel = (color >> shift) & plane_mask;
543
sample_store_next8(pixel, dptr, dbit, dest_depth, dbbyte);
545
sample_store_flush(dptr, dbit, dest_depth, dbbyte);
508
/* Do the final 1-7 pixels. */
509
uint test = 0x10 << shift, store = 0x80;
512
*dst = (*src & test ? *dst | store : *dst & ~store);
522
case EXTRACT_32_TO_8: {
523
const byte *src = source_row;
524
byte *dst = dest_row;
526
for (x = width; x > 0; src += 4, --x)
531
sample_load_declare_setup(sptr, sbit, source_row, source_bit,
533
sample_store_declare_setup(dptr, dbit, dbbyte, dest_row, dest_bit,
536
sample_store_preload(dbbyte, dptr, dbit, dest_depth);
537
for (x = width; x > 0; --x) {
538
gx_color_index color;
541
sample_load_next_any(color, sptr, sbit, source_depth);
542
pixel = (color >> shift) & plane_mask;
543
sample_store_next8(pixel, dptr, dbit, dest_depth, dbbyte);
545
sample_store_flush(dptr, dbit, dest_depth, dbbyte);
591
591
#if arch_is_big_endian
592
592
# define word_shift (shift)
594
int word_shift = 24 - shift;
594
int word_shift = 24 - shift;
596
for (y = 0; y < height;
597
++y, source_row += source->raster, dest_row += dest->raster
600
const byte *src = source_row;
601
bits32 *dst = (bits32 *)dest_row;
596
for (y = 0; y < height;
597
++y, source_row += source->raster, dest_row += dest->raster
600
const byte *src = source_row;
601
bits32 *dst = (bits32 *)dest_row;
603
for (x = width; x > 0; --x)
604
*dst++ = (bits32)(*src++) << word_shift;
603
for (x = width; x > 0; --x)
604
*dst++ = (bits32)(*src++) << word_shift;
606
606
#undef word_shift
610
610
case EXPAND_1_TO_4:
612
for (y = 0; y < height;
613
++y, source_row += source->raster, dest_row += dest->raster
616
sample_load_declare_setup(sptr, sbit, source_row, source_bit,
618
sample_store_declare_setup(dptr, dbit, dbbyte, dest_row, dest_bit,
621
sample_store_preload(dbbyte, dptr, dbit, dest_depth);
622
for (x = width; x > 0; --x) {
624
gx_color_index pixel;
626
sample_load_next8(color, sptr, sbit, source_depth);
627
pixel = color << shift;
628
sample_store_next_any(pixel, dptr, dbit, dest_depth, dbbyte);
630
sample_store_flush(dptr, dbit, dest_depth, dbbyte);
612
for (y = 0; y < height;
613
++y, source_row += source->raster, dest_row += dest->raster
616
sample_load_declare_setup(sptr, sbit, source_row, source_bit,
618
sample_store_declare_setup(dptr, dbit, dbbyte, dest_row, dest_bit,
621
sample_store_preload(dbbyte, dptr, dbit, dest_depth);
622
for (x = width; x > 0; --x) {
624
gx_color_index pixel;
626
sample_load_next8(color, sptr, sbit, source_depth);
627
pixel = color << shift;
628
sample_store_next_any(pixel, dptr, dbit, dest_depth, dbbyte);
630
sample_store_flush(dptr, dbit, dest_depth, dbbyte);
640
640
/* Fill a rectangle of bytes. */
642
642
bytes_fill_rectangle(byte * dest, uint raster,
643
byte value, int width_bytes, int height)
643
byte value, int width_bytes, int height)
645
645
while (height-- > 0) {
646
memset(dest, value, width_bytes);
646
memset(dest, value, width_bytes);
651
651
/* Copy a rectangle of bytes. */
653
653
bytes_copy_rectangle(byte * dest, uint dest_raster,
654
const byte * src, uint src_raster, int width_bytes, int height)
654
const byte * src, uint src_raster, int width_bytes, int height)
656
656
while (height-- > 0) {
657
memcpy(dest, src, width_bytes);
657
memcpy(dest, src, width_bytes);
663
663
/* Copy a rectangle of bytes zeroing any padding bytes. */
665
665
bytes_copy_rectangle_zero_padding(byte * dest, uint dest_raster,
666
const byte * src, uint src_raster, int width_bytes, int height)
666
const byte * src, uint src_raster, int width_bytes, int height)
668
668
int padlen = dest_raster;