Mon, 26 Aug 2002 05:43:49 +0000
fixed 'middle-endian' output from TIFFReadScanline
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #include "type.h"
7 #include "bitblt.h"
10 #define DIV_ROUND_UP(count,pow2) (((count) - 1) / (pow2) + 1)
13 static const u8 bit_reverse_byte [0x100] =
14 {
15 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
16 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
17 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
18 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
19 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
20 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
21 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
22 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
23 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
24 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
25 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
26 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
27 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
28 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
29 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
30 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
31 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
32 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
33 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
34 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
35 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
36 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
37 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
38 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
39 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
40 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
41 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
42 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
43 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
44 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
45 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
46 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
47 };
50 void reverse_bits (u8 *p, int byte_count)
51 {
52 while (byte_count--)
53 {
54 (*p) = bit_reverse_byte [*p];
55 p++;
56 }
57 }
60 static word_type bit_reverse_word (word_type d)
61 {
62 return (bit_reverse_byte [d >> 24] |
63 (bit_reverse_byte [(d >> 16) & 0xff] << 8) |
64 (bit_reverse_byte [(d >> 8) & 0xff] << 16) |
65 (bit_reverse_byte [d & 0xff] << 24));
66 }
69 static word_type *temp_buffer;
70 static word_type temp_buffer_size;
72 static void realloc_temp_buffer (u32 size)
73 {
74 if (size <= temp_buffer_size)
75 return;
76 temp_buffer = realloc (temp_buffer, size);
77 if (! temp_buffer)
78 {
79 fprintf (stderr, "realloc failed in bitblt library\n");
80 exit (2);
81 }
82 temp_buffer_size = size;
83 }
86 static inline word_type pixel_mask (int x)
87 {
88 #if defined (MIXED_ENDIAN) /* disgusting hack for mixed-endian */
89 word_type m;
90 m = 0x80 >> (x & 7);
91 m <<= (x & 24);
92 return (m);
93 #elif defined (LSB_RIGHT)
94 return (1U << ((BITS_PER_WORD - 1) - x));
95 #else
96 return (1U << x);
97 #endif
98 };
101 /* mask for range of bits left..right, inclusive */
102 static inline word_type pixel_range_mask (int left, int right)
103 {
104 word_type m1, m2, val;
106 /* $$$ one of these cases is wrong! */
107 #if defined (LSB_RIGHT)
108 m1 = (~ 0U) >> left;
109 m2 = (~ 0U) << (BITS_PER_WORD - 1 - right);
110 #else
111 m1 = (~ 0U) << left;
112 m2 = (~ 0U) >> (BITS_PER_WORD - 1 - right);
113 #endif
114 val = m1 & m2;
116 printf ("left %d, right %d, mask %08x\n", left, right, val);
117 return (val);
118 };
121 Bitmap *create_bitmap (Rect *rect)
122 {
123 Bitmap *bitmap;
124 u32 width = rect_width (rect);
125 u32 height = rect_height (rect);
127 if ((width <= 0) || (height <= 0))
128 return (NULL);
130 bitmap = calloc (1, sizeof (Bitmap));
131 if (! bitmap)
132 return (NULL);
133 bitmap->rect = * rect;
134 bitmap->row_words = DIV_ROUND_UP (width, BITS_PER_WORD);
135 bitmap->bits = calloc (1, height * bitmap->row_words * sizeof (word_type));
136 if (! bitmap->bits)
137 {
138 free (bitmap);
139 return (NULL);
140 }
141 return (bitmap);
142 }
144 void free_bitmap (Bitmap *bitmap)
145 {
146 free (bitmap->bits);
147 free (bitmap);
148 }
150 boolean get_pixel (Bitmap *bitmap, Point coord)
151 {
152 word_type *p;
153 int w,b;
155 if ((coord.x < bitmap->rect.min.x) ||
156 (coord.x >= bitmap->rect.max.x) ||
157 (coord.y < bitmap->rect.min.y) ||
158 (coord.y >= bitmap->rect.max.y))
159 return (0);
160 coord.y -= bitmap->rect.min.y;
161 coord.x -= bitmap->rect.min.x;
162 w = coord.x / BITS_PER_WORD;
163 b = coord.x & (BITS_PER_WORD - 1);
164 p = bitmap->bits + coord.y * bitmap->row_words + w;
165 return (((*p) & pixel_mask (b)) != 0);
166 }
168 void set_pixel (Bitmap *bitmap, Point coord, boolean value)
169 {
170 word_type *p;
171 int w,b;
173 if ((coord.x < bitmap->rect.min.x) ||
174 (coord.x >= bitmap->rect.max.x) ||
175 (coord.y < bitmap->rect.min.y) ||
176 (coord.y >= bitmap->rect.max.y))
177 return;
178 coord.y -= bitmap->rect.min.y;
179 coord.x -= bitmap->rect.min.x;
180 w = coord.x / BITS_PER_WORD;
181 b = coord.x & (BITS_PER_WORD - 1);
182 p = bitmap->bits + coord.y * bitmap->row_words + w;
183 if (value)
184 (*p) |= pixel_mask (b);
185 else
186 (*p) &= ~pixel_mask (b);
187 }
190 /* modifies rect1 to be the intersection of rect1 and rect2;
191 returns true if intersection is non-null */
192 static boolean clip_rect (Rect *rect1, Rect *rect2)
193 {
194 if (rect1->min.y > rect2->max.y)
195 goto empty;
196 if (rect1->min.y < rect2->min.y)
197 {
198 if (rect1->max.y < rect2->max.y)
199 goto empty;
200 rect1->min.y = rect2->min.y;
201 }
202 if (rect1->max.y > rect2->max.y)
203 rect1->max.y = rect2->max.y;
205 if (rect1->min.x > rect2->max.x)
206 goto empty;
207 if (rect1->min.x < rect2->min.x)
208 {
209 if (rect1->max.x < rect2->max.x)
210 goto empty;
211 rect1->min.x = rect2->min.x;
212 }
213 if (rect1->max.x > rect2->max.x)
214 rect1->max.x = rect2->max.x;
216 empty:
217 rect1->min.x = rect1->min.y =
218 rect1->max.x = rect1->max.y = 0;
219 return (0);
220 }
223 static void blt_background (Bitmap *dest_bitmap,
224 Rect dest_rect)
225 {
226 u32 y;
227 word_type *rp;
228 u32 left_bit, left_word;
229 u32 right_bit, right_word;
230 word_type left_mask, right_mask;
231 s32 word_count;
233 /* This function requires a non-null dest rect */
234 assert (dest_rect.min.x < dest_rect.max.x);
235 assert (dest_rect.min.y < dest_rect.max.y);
237 /* and that the rows of the dest rect lie entirely within the dest bitmap */
238 assert (dest_rect.min.y >= dest_bitmap->rect.min.y);
239 assert (dest_rect.max.y <= dest_bitmap->rect.max.y);
241 /* clip the x axis of the dest_rect to the bounds of the dest bitmap */
242 if (dest_rect.min.x < dest_bitmap->rect.min.x)
243 dest_rect.min.x = dest_bitmap->rect.min.x;
244 if (dest_rect.max.x > dest_bitmap->rect.max.x)
245 dest_rect.max.x = dest_bitmap->rect.max.x;
247 rp = dest_bitmap->bits +
248 (dest_rect.min.y - dest_bitmap->rect.min.y) * dest_bitmap->row_words +
249 (dest_rect.min.x - dest_bitmap->rect.min.x) / BITS_PER_WORD;
251 left_bit = dest_rect.min.x % BITS_PER_WORD;
252 left_word = dest_rect.min.x / BITS_PER_WORD;
254 right_bit = (dest_rect.max.x - 1) % BITS_PER_WORD;
255 right_word = (dest_rect.max.x - 1) / BITS_PER_WORD;
257 word_count = right_word + 1 - left_word;
259 /* special case if entire horizontal range fits in a single word */
260 if (word_count == 1)
261 {
262 left_mask = 0;
263 right_mask = ~ pixel_range_mask (left_bit, right_bit);
264 word_count = 0;
265 }
266 else
267 {
268 if (left_bit)
269 {
270 left_mask = ~ pixel_range_mask (left_bit, BITS_PER_WORD - 1);
271 word_count--;
272 }
274 if (right_bit != (BITS_PER_WORD - 1))
275 {
276 right_mask = ~ pixel_range_mask (0, right_bit);
277 word_count--;
278 }
279 }
281 for (y = 0; y < rect_height (& dest_rect); y++)
282 {
283 word_type *wp = rp;
285 /* partial word at left, if any */
286 if (left_mask)
287 *(wp++) &= left_mask;
289 /* use Duff's Device for the full words */
290 if (word_count)
291 {
292 s32 i = word_count;
293 switch (i % 8)
294 {
295 while (i > 0)
296 {
297 *(wp++) = 0;
298 case 7: *(wp++) = 0;
299 case 6: *(wp++) = 0;
300 case 5: *(wp++) = 0;
301 case 4: *(wp++) = 0;
302 case 3: *(wp++) = 0;
303 case 2: *(wp++) = 0;
304 case 1: *(wp++) = 0;
305 case 0: i -= 8;
306 }
307 }
308 }
310 /* partial word at right, if any */
311 if (right_mask)
312 *wp &= right_mask;
314 /* advance to next row */
315 rp += dest_bitmap->row_words;
316 }
317 }
320 #if 0
321 static void blt (Bitmap *src_bitmap,
322 Rect *src_rect,
323 Bitmap *dest_bitmap,
324 Rect *dest_rect)
325 {
326 s32 y;
327 word_type *rp;
329 /* This function requires a non-null src rect */
330 assert (dest_rect->min.x < dest_rect->max.x);
331 assert (dest_rect->min.y < dest_rect->max.y);
333 /* and a non-null dest rect */
334 assert (dest_rect->min.x < dest_rect->max.x);
335 assert (dest_rect->min.y < dest_rect->max.y);
337 /* and that the widths and heights of the rects match */
338 assert (rect_width (src_rect) == rect_width (dest_rect));
339 assert (rect_height (src_rect) == rect_height (dest_rect));
341 /* and that the rows of the src rect lie entirely within the src bitmap */
342 assert (dest_rect->min.y >= dest_bitmap->rect->min.y);
343 assert (dest_rect->max.y <= dest_bitmap->rect->max.y);
345 /* and that the rows of the dest rect lie entirely within the dest bitmap */
346 assert (dest_rect->min.y >= dest_bitmap->rect->min.y);
347 assert (dest_rect->max.y <= dest_bitmap->rect->max.y);
349 /* clip the x axis of the dest_rect to the bounds of the dest bitmap,
350 and adjust the src_rect to match */
351 if (dest_rect->min.x < dest_bitmap->rect.min.x)
352 {
353 src_rect->min.x += ???;
354 dest_rect->min.x = dest_bitmap->rect.min.x;
355 }
356 if (dest_rect->max.x > dest_bitmap->rect.max.x)
357 {
358 dest_rect->max.x = dest_bitmap->rect.max.x;
359 }
361 rp = ???;
362 for (y = 0; y < rect_height (dest_rect); y++)
363 {
364 ???;
365 rp += dest_bitmap->row_words;
366 }
367 }
370 /*
371 * The destination rectangle is first clipped to the dest bitmap, and
372 * the source rectangle is adjusted in the corresponding manner.
373 * What's left is divided into five sections, any of which may be
374 * null. The portion that actually corresponds to the intersection of
375 * the source rectangle and the source bitmpa is the "middle". The
376 * other four sections will use the background color as the source
377 * operand.
378 *
379 *
380 * y0 -> -------------------------------------------------
381 * | top |
382 * | |
383 * y1 -> -------------------------------------------------
384 * | left | middle | right |
385 * | | | |
386 * y2 -> -------------------------------------------------
387 * | bottom |
388 * | |
389 * y3 -> -------------------------------------------------
390 *
391 * ^ ^ ^ ^
392 * | | | |
393 * x0 x1 x2 x3
394 *
395 * */
396 Bitmap *bitblt (Bitmap *src_bitmap,
397 Rect *src_rect,
398 Bitmap *dest_bitmap,
399 Point *dest_min,
400 int tfn,
401 int background)
402 {
403 Rect sr, dr; /* src and dest rects, clipped to visible portion of
404 dest rect */
405 u32 drw, drh; /* dest rect width, height - gets adjusted */
406 Point src_point, dest_point;
408 /* dest coordinates: */
409 u32 x0, x1, x2, x3;
410 u32 y0, y1, y2, y3;
412 {
413 sr = * src_rect;
415 u32 srw = rect_width (& sr);
416 u32 srh = rect_height (& sr);
418 if ((srw < 0) || (srh < 0))
419 goto done; /* the source rect is empty! */
421 dr.min.x = dest_min->x;
422 dr.min.y = dest_min->y;
423 dr.max.x = dr.min.x + srw;
424 dr.max.y = dr.min.y + srh;
425 }
427 if (! dest_bitmap)
428 {
429 dest_bitmap = create_bitmap (& dr);
430 if (! dest_bitmap)
431 return (NULL);
432 }
434 if ((dr.min.x >= dest_bitmap->rect.max.x) ||
435 (dr.min.y >= dest_bitmap->rect.max.y))
436 goto done; /* the dest rect isn't even in the dest bitmap! */
438 /* crop dest rect to dest bitmap */
439 delta = dest_bitmap->rect.min.x - dr.min.x;
440 if (delta > 0)
441 {
442 sr.min.x += delta;
443 dr.min.x += delta;
444 }
446 delta = dest_bitmap->rect.min.y - dr.min.y;
447 if (delta > 0)
448 {
449 sr.min.y += delta;
450 dr.min.y += delta;
451 }
453 delta = dr.max.x - dest_bitmap->rect.max.x;
454 if (delta > 0)
455 {
456 sr.max.x -= delta;
457 dr.max.x -= delta;
458 }
460 delta = dr.max.y - dest_bitmap->rect.max.y;
461 if (delta > 0)
462 {
463 sr.max.x -= delta;
464 dr.max.x -= delta;
465 }
467 drw = rect_width (& dr);
468 drh = rect_height (& dh);
470 x0 = dr.min.x;
471 y0 = dr.min.y;
472 x3 = dr.max.x;
473 y3 = dr.max.y;
475 #if 0
476 /* if the source rect min y is >= the source bitmap max y,
477 we transfer background color to the entire dest rect */
478 if (sr.min.y >= src->rect.max.y)
479 {
480 blt_background (dest_bitmap, dr);
481 goto done;
482 }
483 #endif
485 /* top */
486 if (y0 != y1)
487 {
488 dr2.min.x = x0;
489 dr2.max.x = x3;
490 dr2.min.y = y0;
491 dr2.max.y = y1;
492 blt_background (dest_bitmap, & dr2);
493 }
495 /*
496 * top: if the source rect min y is less than the source bitmap min y,
497 * we need to transfer some backgound color to the top part of the dest
498 * rect
499 */
500 if (sr.min.y < src->rect.min.y)
501 {
502 Rect dr2;
503 uint32 bg_height;
505 bg_height = src->rect.min.y - sr.min.y;
506 if (bg_height > sh)
507 bg_height = sh;
509 dr2 = dr;
510 dr2.max.y = dr2.min.y + bg_height;
512 blt_background (dest_bitmap, & dr2);
514 /* now reduce the rect height by the number of lines of background
515 color */
516 sr.min.y += bg_height;
517 dr.min.y += bg_height;
518 sh -= bg_height;
519 dh -= bg_height;
521 if (sr.min.y == sr.max.y)
522 goto done;
523 }
525 if (y1 != y2)
526 {
527 /* left */
528 if (x0 != x1)
529 {
530 dr2.min.x = x1;
531 dr2.max.x = x1;
532 dr2.min.y = y1;
533 dr2.max.y = y2
534 blt_background (dest_bitmap, & dr2);
535 }
537 /* middle */
538 if (x1 != x2)
539 {
540 /* ??? */
541 }
543 /* right */
544 if (x2 != x3)
545 {
546 dr2.min.x = x2;
547 dr2.max.x = x3;
548 dr2.min.y = y1;
549 dr2.max.y = y2
550 blt_background (dest_bitmap, & dr2);
551 }
552 }
554 /* bottom */
555 if (y2 != y3)
556 {
557 dr2.min.x = x0;
558 dr2.max.x = x3;
559 dr2.min.y = y2;
560 dr2.max.y = y3;
561 blt_background (dest_bitmap, & dr2);
562 }
564 done:
565 return (dest_bitmap);
566 }
567 #else
568 Bitmap *bitblt (Bitmap *src_bitmap,
569 Rect *src_rect,
570 Bitmap *dest_bitmap,
571 Point *dest_min,
572 int tfn,
573 int background)
574 {
575 Point src_point, dest_point;
577 if (! dest_bitmap)
578 {
579 Rect dest_rect = {{ 0, 0 }, { dest_min->x + rect_width (src_rect),
580 dest_min->y + rect_height (src_rect) }};
581 dest_bitmap = create_bitmap (& dest_rect);
582 if (! dest_bitmap)
583 return (NULL);
584 }
586 if (tfn == TF_SRC)
587 {
588 for (src_point.y = src_rect->min.y;
589 src_point.y < src_rect->max.y;
590 src_point.y++)
591 {
592 dest_point.y = dest_min->y + src_point.y - src_rect->min.y;
594 for (src_point.x = src_rect->min.x;
595 src_point.x < src_rect->max.x;
596 src_point.x++)
597 {
598 boolean a;
600 dest_point.x = dest_min->x + src_point.x - src_rect->min.x;
602 a = get_pixel (src_bitmap, src_point);
603 set_pixel (dest_bitmap, dest_point, a);
604 }
605 }
606 }
607 else
608 {
609 for (src_point.y = src_rect->min.y;
610 src_point.y < src_rect->max.y;
611 src_point.y++)
612 {
613 dest_point.y = dest_min->y + src_point.y - src_rect->min.y;
615 for (src_point.x = src_rect->min.x;
616 src_point.x < src_rect->max.x;
617 src_point.x++)
618 {
619 boolean a, b, c;
621 dest_point.x = dest_min->x + src_point.x - src_rect->min.x;
623 a = get_pixel (src_bitmap, src_point);
624 b = get_pixel (dest_bitmap, dest_point);
625 c = (tfn & (1 << (a * 2 + b))) != 0;
627 set_pixel (dest_bitmap, dest_point, c);
628 }
629 }
630 }
631 return (dest_bitmap);
632 }
633 #endif
636 /* in-place transformations */
637 void flip_h (Bitmap *src)
638 {
639 word_type *rp; /* row pointer */
640 word_type *p1; /* work src ptr */
641 word_type *p2; /* work dest ptr */
642 s32 y;
643 int shift1, shift2;
645 realloc_temp_buffer ((src->row_words + 1) * sizeof (word_type));
647 rp = src->bits;
648 if ((rect_width (& src->rect) & 7) == 0)
649 {
650 for (y = src->rect.min.y; y < src->rect.max.y; y++)
651 {
652 memcpy (temp_buffer, rp, src->row_words * sizeof (word_type));
653 p1 = temp_buffer + src->row_words;
654 p2 = rp;
656 while (p1 >= temp_buffer)
657 *(p2++) = bit_reverse_word (*(p1--));
659 rp += src->row_words;
660 }
661 return;
662 }
664 temp_buffer [0] = 0;
665 shift1 = rect_width (& src->rect) & (BITS_PER_WORD - 1);
666 shift2 = BITS_PER_WORD - shift1;
668 for (y = src->rect.min.y; y < src->rect.max.y; y++)
669 {
670 word_type d1, d2;
672 memcpy (temp_buffer + 1, rp, src->row_words * sizeof (word_type));
673 p1 = temp_buffer + src->row_words;
674 p2 = rp;
676 d2 = *(p1--);
678 while (p1 >= temp_buffer)
679 {
680 d1 = *(p1--);
681 *(p2++) = bit_reverse_word ((d1 << shift1) | (d2 >> shift2));
682 d2 = d1;
683 }
685 rp += src->row_words;
686 }
687 }
689 void flip_v (Bitmap *src)
690 {
691 word_type *p1, *p2;
693 realloc_temp_buffer (src->row_words * sizeof (word_type));
695 p1 = src->bits;
696 p2 = src->bits + src->row_words * (rect_height (& src->rect) - 1);
697 while (p1 < p2)
698 {
699 memcpy (temp_buffer, p1, src->row_words * sizeof (word_type));
700 memcpy (p1, p2, src->row_words * sizeof (word_type));
701 memcpy (p2, temp_buffer, src->row_words * sizeof (word_type));
702 p1 += src->row_words;
703 p2 -= src->row_words;
704 }
705 }
707 void rot_180 (Bitmap *src) /* combination of flip_h and flip_v */
708 {
709 flip_h (src);
710 flip_v (src);
711 }
713 /* "in-place" transformations - will allocate new memory and free old */
714 void transpose (Bitmap *src)
715 {
716 u32 new_row_words = DIV_ROUND_UP (rect_height (& src->rect), 32);
717 word_type *new_bits;
719 new_bits = calloc (1, new_row_words * rect_width (& src->rect) * sizeof (word_type));
721 /* $$$ more code needed here */
722 }
724 void rot_90 (Bitmap *src) /* transpose + flip_h */
725 {
726 transpose (src);
727 flip_h (src);
728 }
730 void rot_270 (Bitmap *src) /* transpose + flip_v */
731 {
732 transpose (src);
733 flip_v (src);
734 }