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