Mon, 14 Dec 2009 16:18:21 +0000
remove erroneous 0.33-philpem1 tag
1 /*
2 * tumble: build a PDF file from image files
3 *
4 * bitblt routines
5 * $Id: bitblt.c,v 1.17 2003/03/16 07:27:06 eric Exp $
6 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. Note that permission is
11 * not granted to redistribute this program under the terms of any
12 * other version of the General Public License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22 */
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include "bitblt.h"
34 #include "bitblt_tables.h"
37 #define DIV_ROUND_UP(count,pow2) (((count) - 1) / (pow2) + 1)
40 void reverse_bits (uint8_t *p, int byte_count)
41 {
42 while (byte_count--)
43 {
44 (*p) = bit_reverse_byte [*p];
45 p++;
46 }
47 }
50 static word_t bit_reverse_word (word_t d)
51 {
52 return (bit_reverse_byte [d >> 24] |
53 (bit_reverse_byte [(d >> 16) & 0xff] << 8) |
54 (bit_reverse_byte [(d >> 8) & 0xff] << 16) |
55 (bit_reverse_byte [d & 0xff] << 24));
56 }
59 static void reverse_range_of_bytes (uint8_t *b, uint32_t count)
60 {
61 uint8_t *b2 = b + count - 1;
63 while (b < b2)
64 {
65 uint8_t t = bit_reverse_byte [*b];
66 *b = bit_reverse_byte [*b2];
67 *b2 = t;
68 b++;
69 b2--;
70 }
72 if (b == b2)
73 *b = bit_reverse_byte [*b];
74 }
77 static word_t *temp_buffer;
78 static word_t temp_buffer_size;
80 static void realloc_temp_buffer (uint32_t size)
81 {
82 if (size <= temp_buffer_size)
83 return;
84 temp_buffer = realloc (temp_buffer, size);
85 if (! temp_buffer)
86 {
87 fprintf (stderr, "realloc failed in bitblt library\n");
88 exit (2);
89 }
90 temp_buffer_size = size;
91 }
94 static inline word_t pixel_mask (int x)
95 {
96 #if defined (MIXED_ENDIAN) /* disgusting hack for mixed-endian */
97 word_t m;
98 m = 0x80 >> (x & 7);
99 m <<= (x & 24);
100 return (m);
101 #elif defined (LSB_RIGHT)
102 return (1U << ((BITS_PER_WORD - 1) - x));
103 #else
104 return (1U << x);
105 #endif
106 };
109 /* mask for range of bits left..right, inclusive */
110 static inline word_t pixel_range_mask (int left, int right)
111 {
112 word_t m1, m2, val;
114 /* $$$ one of these cases is wrong! */
115 #if defined (LSB_RIGHT)
116 m1 = (~ 0U) >> left;
117 m2 = (~ 0U) << (BITS_PER_WORD - 1 - right);
118 #else
119 m1 = (~ 0U) << left;
120 m2 = (~ 0U) >> (BITS_PER_WORD - 1 - right);
121 #endif
122 val = m1 & m2;
124 printf ("left %d, right %d, mask %08x\n", left, right, val);
125 return (val);
126 };
129 Bitmap *create_bitmap (Rect *rect)
130 {
131 Bitmap *bitmap;
132 uint32_t width = rect_width (rect);
133 uint32_t height = rect_height (rect);
135 if ((width <= 0) || (height <= 0))
136 return (NULL);
138 bitmap = calloc (1, sizeof (Bitmap));
139 if (! bitmap)
140 return (NULL);
141 bitmap->rect = * rect;
142 bitmap->row_words = DIV_ROUND_UP (width, BITS_PER_WORD);
143 bitmap->bits = calloc (1, height * bitmap->row_words * sizeof (word_t));
144 if (! bitmap->bits)
145 {
146 free (bitmap);
147 return (NULL);
148 }
149 return (bitmap);
150 }
152 void free_bitmap (Bitmap *bitmap)
153 {
154 free (bitmap->bits);
155 free (bitmap);
156 }
158 bool get_pixel (Bitmap *bitmap, Point coord)
159 {
160 word_t *p;
161 int w,b;
163 if ((coord.x < bitmap->rect.min.x) ||
164 (coord.x >= bitmap->rect.max.x) ||
165 (coord.y < bitmap->rect.min.y) ||
166 (coord.y >= bitmap->rect.max.y))
167 return (0);
168 coord.y -= bitmap->rect.min.y;
169 coord.x -= bitmap->rect.min.x;
170 w = coord.x / BITS_PER_WORD;
171 b = coord.x & (BITS_PER_WORD - 1);
172 p = bitmap->bits + coord.y * bitmap->row_words + w;
173 return (((*p) & pixel_mask (b)) != 0);
174 }
176 void set_pixel (Bitmap *bitmap, Point coord, bool value)
177 {
178 word_t *p;
179 int w,b;
181 if ((coord.x < bitmap->rect.min.x) ||
182 (coord.x >= bitmap->rect.max.x) ||
183 (coord.y < bitmap->rect.min.y) ||
184 (coord.y >= bitmap->rect.max.y))
185 return;
186 coord.y -= bitmap->rect.min.y;
187 coord.x -= bitmap->rect.min.x;
188 w = coord.x / BITS_PER_WORD;
189 b = coord.x & (BITS_PER_WORD - 1);
190 p = bitmap->bits + coord.y * bitmap->row_words + w;
191 if (value)
192 (*p) |= pixel_mask (b);
193 else
194 (*p) &= ~pixel_mask (b);
195 }
198 /* modifies rect1 to be the intersection of rect1 and rect2;
199 returns true if intersection is non-null */
200 static bool clip_rect (Rect *rect1, Rect *rect2)
201 {
202 if (rect1->min.y > rect2->max.y)
203 goto empty;
204 if (rect1->min.y < rect2->min.y)
205 {
206 if (rect1->max.y < rect2->max.y)
207 goto empty;
208 rect1->min.y = rect2->min.y;
209 }
210 if (rect1->max.y > rect2->max.y)
211 rect1->max.y = rect2->max.y;
213 if (rect1->min.x > rect2->max.x)
214 goto empty;
215 if (rect1->min.x < rect2->min.x)
216 {
217 if (rect1->max.x < rect2->max.x)
218 goto empty;
219 rect1->min.x = rect2->min.x;
220 }
221 if (rect1->max.x > rect2->max.x)
222 rect1->max.x = rect2->max.x;
224 empty:
225 rect1->min.x = rect1->min.y =
226 rect1->max.x = rect1->max.y = 0;
227 return (0);
228 }
231 static void blt_background (Bitmap *dest_bitmap,
232 Rect dest_rect)
233 {
234 uint32_t y;
235 word_t *rp;
236 uint32_t left_bit, left_word;
237 uint32_t right_bit, right_word;
238 word_t left_mask, right_mask;
239 int32_t word_count;
241 /* This function requires a non-null dest rect */
242 assert (dest_rect.min.x < dest_rect.max.x);
243 assert (dest_rect.min.y < dest_rect.max.y);
245 /* and that the rows of the dest rect lie entirely within the dest bitmap */
246 assert (dest_rect.min.y >= dest_bitmap->rect.min.y);
247 assert (dest_rect.max.y <= dest_bitmap->rect.max.y);
249 /* clip the x axis of the dest_rect to the bounds of the dest bitmap */
250 if (dest_rect.min.x < dest_bitmap->rect.min.x)
251 dest_rect.min.x = dest_bitmap->rect.min.x;
252 if (dest_rect.max.x > dest_bitmap->rect.max.x)
253 dest_rect.max.x = dest_bitmap->rect.max.x;
255 rp = dest_bitmap->bits +
256 (dest_rect.min.y - dest_bitmap->rect.min.y) * dest_bitmap->row_words +
257 (dest_rect.min.x - dest_bitmap->rect.min.x) / BITS_PER_WORD;
259 left_bit = dest_rect.min.x % BITS_PER_WORD;
260 left_word = dest_rect.min.x / BITS_PER_WORD;
262 right_bit = (dest_rect.max.x - 1) % BITS_PER_WORD;
263 right_word = (dest_rect.max.x - 1) / BITS_PER_WORD;
265 word_count = right_word + 1 - left_word;
267 /* special case if entire horizontal range fits in a single word */
268 if (word_count == 1)
269 {
270 left_mask = 0;
271 right_mask = ~ pixel_range_mask (left_bit, right_bit);
272 word_count = 0;
273 }
274 else
275 {
276 if (left_bit)
277 {
278 left_mask = ~ pixel_range_mask (left_bit, BITS_PER_WORD - 1);
279 word_count--;
280 }
282 if (right_bit != (BITS_PER_WORD - 1))
283 {
284 right_mask = ~ pixel_range_mask (0, right_bit);
285 word_count--;
286 }
287 }
289 for (y = 0; y < rect_height (& dest_rect); y++)
290 {
291 word_t *wp = rp;
293 /* partial word at left, if any */
294 if (left_mask)
295 *(wp++) &= left_mask;
297 /* use Duff's Device for the full words */
298 if (word_count)
299 {
300 int32_t i = word_count;
301 switch (i % 8)
302 {
303 while (i > 0)
304 {
305 *(wp++) = 0;
306 case 7: *(wp++) = 0;
307 case 6: *(wp++) = 0;
308 case 5: *(wp++) = 0;
309 case 4: *(wp++) = 0;
310 case 3: *(wp++) = 0;
311 case 2: *(wp++) = 0;
312 case 1: *(wp++) = 0;
313 case 0: i -= 8;
314 }
315 }
316 }
318 /* partial word at right, if any */
319 if (right_mask)
320 *wp &= right_mask;
322 /* advance to next row */
323 rp += dest_bitmap->row_words;
324 }
325 }
328 #if 0
329 static void blt (Bitmap *src_bitmap,
330 Rect *src_rect,
331 Bitmap *dest_bitmap,
332 Rect *dest_rect)
333 {
334 int32_t y;
335 word_t *rp;
337 /* This function requires a non-null src rect */
338 assert (dest_rect->min.x < dest_rect->max.x);
339 assert (dest_rect->min.y < dest_rect->max.y);
341 /* and a non-null dest rect */
342 assert (dest_rect->min.x < dest_rect->max.x);
343 assert (dest_rect->min.y < dest_rect->max.y);
345 /* and that the widths and heights of the rects match */
346 assert (rect_width (src_rect) == rect_width (dest_rect));
347 assert (rect_height (src_rect) == rect_height (dest_rect));
349 /* and that the rows of the src rect lie entirely within the src bitmap */
350 assert (dest_rect->min.y >= dest_bitmap->rect->min.y);
351 assert (dest_rect->max.y <= dest_bitmap->rect->max.y);
353 /* and that the rows of the dest rect lie entirely within the dest bitmap */
354 assert (dest_rect->min.y >= dest_bitmap->rect->min.y);
355 assert (dest_rect->max.y <= dest_bitmap->rect->max.y);
357 /* clip the x axis of the dest_rect to the bounds of the dest bitmap,
358 and adjust the src_rect to match */
359 if (dest_rect->min.x < dest_bitmap->rect.min.x)
360 {
361 src_rect->min.x += ???;
362 dest_rect->min.x = dest_bitmap->rect.min.x;
363 }
364 if (dest_rect->max.x > dest_bitmap->rect.max.x)
365 {
366 dest_rect->max.x = dest_bitmap->rect.max.x;
367 }
369 rp = ???;
370 for (y = 0; y < rect_height (dest_rect); y++)
371 {
372 ???;
373 rp += dest_bitmap->row_words;
374 }
375 }
378 /*
379 * The destination rectangle is first clipped to the dest bitmap, and
380 * the source rectangle is adjusted in the corresponding manner.
381 * What's left is divided into five sections, any of which may be
382 * null. The portion that actually corresponds to the intersection of
383 * the source rectangle and the source bitmpa is the "middle". The
384 * other four sections will use the background color as the source
385 * operand.
386 *
387 *
388 * y0 -> -------------------------------------------------
389 * | top |
390 * | |
391 * y1 -> -------------------------------------------------
392 * | left | middle | right |
393 * | | | |
394 * y2 -> -------------------------------------------------
395 * | bottom |
396 * | |
397 * y3 -> -------------------------------------------------
398 *
399 * ^ ^ ^ ^
400 * | | | |
401 * x0 x1 x2 x3
402 *
403 * */
404 Bitmap *bitblt (Bitmap *src_bitmap,
405 Rect *src_rect,
406 Bitmap *dest_bitmap,
407 Point *dest_min,
408 int tfn,
409 int background)
410 {
411 Rect sr, dr; /* src and dest rects, clipped to visible portion of
412 dest rect */
413 uint32_t drw, drh; /* dest rect width, height - gets adjusted */
414 Point src_point, dest_point;
416 /* dest coordinates: */
417 uint32_t x0, x1, x2, x3;
418 uint32_t y0, y1, y2, y3;
420 {
421 sr = * src_rect;
423 uint32_t srw = rect_width (& sr);
424 uint32_t srh = rect_height (& sr);
426 if ((srw < 0) || (srh < 0))
427 goto done; /* the source rect is empty! */
429 dr.min.x = dest_min->x;
430 dr.min.y = dest_min->y;
431 dr.max.x = dr.min.x + srw;
432 dr.max.y = dr.min.y + srh;
433 }
435 if (! dest_bitmap)
436 {
437 dest_bitmap = create_bitmap (& dr);
438 if (! dest_bitmap)
439 return (NULL);
440 }
442 if ((dr.min.x >= dest_bitmap->rect.max.x) ||
443 (dr.min.y >= dest_bitmap->rect.max.y))
444 goto done; /* the dest rect isn't even in the dest bitmap! */
446 /* crop dest rect to dest bitmap */
447 delta = dest_bitmap->rect.min.x - dr.min.x;
448 if (delta > 0)
449 {
450 sr.min.x += delta;
451 dr.min.x += delta;
452 }
454 delta = dest_bitmap->rect.min.y - dr.min.y;
455 if (delta > 0)
456 {
457 sr.min.y += delta;
458 dr.min.y += delta;
459 }
461 delta = dr.max.x - dest_bitmap->rect.max.x;
462 if (delta > 0)
463 {
464 sr.max.x -= delta;
465 dr.max.x -= delta;
466 }
468 delta = dr.max.y - dest_bitmap->rect.max.y;
469 if (delta > 0)
470 {
471 sr.max.x -= delta;
472 dr.max.x -= delta;
473 }
475 drw = rect_width (& dr);
476 drh = rect_height (& dh);
478 x0 = dr.min.x;
479 y0 = dr.min.y;
480 x3 = dr.max.x;
481 y3 = dr.max.y;
483 #if 0
484 /* if the source rect min y is >= the source bitmap max y,
485 we transfer background color to the entire dest rect */
486 if (sr.min.y >= src->rect.max.y)
487 {
488 blt_background (dest_bitmap, dr);
489 goto done;
490 }
491 #endif
493 /* top */
494 if (y0 != y1)
495 {
496 dr2.min.x = x0;
497 dr2.max.x = x3;
498 dr2.min.y = y0;
499 dr2.max.y = y1;
500 blt_background (dest_bitmap, & dr2);
501 }
503 /*
504 * top: if the source rect min y is less than the source bitmap min y,
505 * we need to transfer some backgound color to the top part of the dest
506 * rect
507 */
508 if (sr.min.y < src->rect.min.y)
509 {
510 Rect dr2;
511 uint32 bg_height;
513 bg_height = src->rect.min.y - sr.min.y;
514 if (bg_height > sh)
515 bg_height = sh;
517 dr2 = dr;
518 dr2.max.y = dr2.min.y + bg_height;
520 blt_background (dest_bitmap, & dr2);
522 /* now reduce the rect height by the number of lines of background
523 color */
524 sr.min.y += bg_height;
525 dr.min.y += bg_height;
526 sh -= bg_height;
527 dh -= bg_height;
529 if (sr.min.y == sr.max.y)
530 goto done;
531 }
533 if (y1 != y2)
534 {
535 /* left */
536 if (x0 != x1)
537 {
538 dr2.min.x = x1;
539 dr2.max.x = x1;
540 dr2.min.y = y1;
541 dr2.max.y = y2
542 blt_background (dest_bitmap, & dr2);
543 }
545 /* middle */
546 if (x1 != x2)
547 {
548 /* ??? */
549 }
551 /* right */
552 if (x2 != x3)
553 {
554 dr2.min.x = x2;
555 dr2.max.x = x3;
556 dr2.min.y = y1;
557 dr2.max.y = y2
558 blt_background (dest_bitmap, & dr2);
559 }
560 }
562 /* bottom */
563 if (y2 != y3)
564 {
565 dr2.min.x = x0;
566 dr2.max.x = x3;
567 dr2.min.y = y2;
568 dr2.max.y = y3;
569 blt_background (dest_bitmap, & dr2);
570 }
572 done:
573 return (dest_bitmap);
574 }
575 #else
576 Bitmap *bitblt (Bitmap *src_bitmap,
577 Rect *src_rect,
578 Bitmap *dest_bitmap,
579 Point *dest_min,
580 int tfn,
581 int background)
582 {
583 Point src_point, dest_point;
585 if (! dest_bitmap)
586 {
587 Rect dest_rect = {{ 0, 0 }, { dest_min->x + rect_width (src_rect),
588 dest_min->y + rect_height (src_rect) }};
589 dest_bitmap = create_bitmap (& dest_rect);
590 if (! dest_bitmap)
591 return (NULL);
592 }
594 if (tfn == TF_SRC)
595 {
596 for (src_point.y = src_rect->min.y;
597 src_point.y < src_rect->max.y;
598 src_point.y++)
599 {
600 dest_point.y = dest_min->y + src_point.y - src_rect->min.y;
602 for (src_point.x = src_rect->min.x;
603 src_point.x < src_rect->max.x;
604 src_point.x++)
605 {
606 bool a;
608 dest_point.x = dest_min->x + src_point.x - src_rect->min.x;
610 a = get_pixel (src_bitmap, src_point);
611 set_pixel (dest_bitmap, dest_point, a);
612 }
613 }
614 }
615 else
616 {
617 for (src_point.y = src_rect->min.y;
618 src_point.y < src_rect->max.y;
619 src_point.y++)
620 {
621 dest_point.y = dest_min->y + src_point.y - src_rect->min.y;
623 for (src_point.x = src_rect->min.x;
624 src_point.x < src_rect->max.x;
625 src_point.x++)
626 {
627 bool a, b, c;
629 dest_point.x = dest_min->x + src_point.x - src_rect->min.x;
631 a = get_pixel (src_bitmap, src_point);
632 b = get_pixel (dest_bitmap, dest_point);
633 c = (tfn & (1 << (a * 2 + b))) != 0;
635 set_pixel (dest_bitmap, dest_point, c);
636 }
637 }
638 }
639 return (dest_bitmap);
640 }
641 #endif
644 /* in-place transformations */
645 void flip_h (Bitmap *src)
646 {
647 word_t *rp; /* row pointer */
648 int32_t y;
649 int shift1, shift2;
651 rp = src->bits;
652 if ((rect_width (& src->rect) & 7) == 0)
653 {
654 for (y = src->rect.min.y; y < src->rect.max.y; y++)
655 {
656 reverse_range_of_bytes ((uint8_t *) rp, rect_width (& src->rect) / 8);
657 rp += src->row_words;
658 }
659 return;
660 }
662 realloc_temp_buffer ((src->row_words + 1) * sizeof (word_t));
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_t d1, d2;
671 word_t *p1; /* work src ptr */
672 word_t *p2; /* work dest ptr */
674 memcpy (temp_buffer + 1, rp, src->row_words * sizeof (word_t));
675 p1 = temp_buffer + src->row_words;
676 p2 = rp;
678 d2 = *(p1--);
680 while (p1 >= temp_buffer)
681 {
682 word_t t;
683 d1 = *(p1--);
684 t = (d1 >> shift1) | (d2 << shift2);
685 *(p2++) = bit_reverse_word (t);
686 d2 = d1;
687 }
689 rp += src->row_words;
690 }
691 }
694 void flip_v (Bitmap *src)
695 {
696 word_t *p1, *p2;
698 realloc_temp_buffer (src->row_words * sizeof (word_t));
700 p1 = src->bits;
701 p2 = src->bits + src->row_words * (rect_height (& src->rect) - 1);
702 while (p1 < p2)
703 {
704 memcpy (temp_buffer, p1, src->row_words * sizeof (word_t));
705 memcpy (p1, p2, src->row_words * sizeof (word_t));
706 memcpy (p2, temp_buffer, src->row_words * sizeof (word_t));
707 p1 += src->row_words;
708 p2 -= src->row_words;
709 }
710 }
712 void rot_180 (Bitmap *src) /* combination of flip_h and flip_v */
713 {
714 flip_h (src);
715 flip_v (src);
716 }
718 /* "in-place" transformations - will allocate new memory and free old */
719 void transpose (Bitmap *src)
720 {
721 uint32_t new_row_words = DIV_ROUND_UP (rect_height (& src->rect), 32);
722 word_t *new_bits;
724 new_bits = calloc (1, new_row_words * rect_width (& src->rect) * sizeof (word_t));
726 /* $$$ more code needed here */
727 }
729 void rot_90 (Bitmap *src) /* transpose + flip_h */
730 {
731 transpose (src);
732 flip_h (src);
733 }
735 void rot_270 (Bitmap *src) /* transpose + flip_v */
736 {
737 transpose (src);
738 flip_v (src);
739 }