Thu, 27 Dec 2001 11:04:43 +0000
Initial revision
1 #include <stdlib.h>
3 #include "bitblt.h"
5 static inline u8 pixel_mask (x)
6 {
7 #ifdef LSB_LEFT
8 return (1 << x);
9 #else
10 return (1 << (7 - x));
11 #endif
12 }
14 static inline u32 rect_width (Rect r)
15 {
16 return (r.lower_right.x - r.upper_left.x);
17 }
19 static inline u32 rect_height (Rect r)
20 {
21 return (r.lower_right.y - r.upper_left.y);
22 }
24 Bitmap *create_bitmap (u32 width, u32 height)
25 {
26 Bitmap *bitmap;
28 bitmap = calloc (1, sizeof (Bitmap));
29 if (! bitmap)
30 return (NULL);
31 bitmap->width = width;
32 bitmap->height = height;
33 bitmap->rowbytes = (width - 1) / 8 + 1;
34 bitmap->bits = calloc (height * bitmap->rowbytes, 1);
35 if (! bitmap->bits)
36 {
37 free (bitmap);
38 return (NULL);
39 }
40 return (bitmap);
41 }
43 void free_bitmap (Bitmap *bitmap)
44 {
45 free (bitmap->bits);
46 free (bitmap);
47 }
49 boolean get_pixel (Bitmap *bitmap, Point coord)
50 {
51 u8 *p;
52 if ((coord.x >= bitmap->width) || (coord.y >= bitmap->height))
53 return (0);
54 p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8;
55 return ((*p & pixel_mask (coord.x & 7)) != 0);
56 }
58 void set_pixel (Bitmap *bitmap, Point coord, boolean value)
59 {
60 u8 *p;
61 if ((coord.x >= bitmap->width) || (coord.y >= bitmap->height))
62 return;
63 p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8;
64 if (value)
65 *p |= pixel_mask (coord.x & 7);
66 else
67 *p &= (0xff ^ pixel_mask (coord.x & 7));
68 }
71 Bitmap *bitblt (Bitmap *src_bitmap,
72 Rect src_rect,
73 Bitmap *dest_bitmap,
74 Point dest_upper_left,
75 boolean flip_horizontal,
76 boolean flip_vertical,
77 boolean transpose,
78 int tfn)
79 {
80 Point src_point, dest_point;
81 boolean src_pixel, dest_pixel;
83 if (! dest_bitmap)
84 {
85 if (transpose)
86 dest_bitmap = create_bitmap (rect_height (src_rect),
87 rect_width (src_rect));
88 else
89 dest_bitmap = create_bitmap (rect_width (src_rect),
90 rect_height (src_rect));
91 if (! dest_bitmap)
92 return (NULL);
93 }
95 for (src_point.y = src_rect.upper_left.y;
96 src_point.y < src_rect.lower_right.y;
97 src_point.y++)
98 {
99 for (src_point.x = src_rect.upper_left.x;
100 src_point.x < src_rect.lower_right.x;
101 src_point.x++)
102 {
103 boolean a, b, c;
105 if (transpose)
106 {
107 dest_point.x = dest_upper_left.x + (src_point.y - src_rect.upper_left.y);
108 dest_point.y = dest_upper_left.y + (src_point.x - src_rect.upper_left.x);
109 }
110 else
111 {
112 dest_point.x = dest_upper_left.x + (src_point.x - src_rect.upper_left.x);
113 dest_point.y = dest_upper_left.y + (src_point.y - src_rect.upper_left.y);
114 }
116 a = get_pixel (src_bitmap, src_point);
117 b = get_pixel (dest_bitmap, dest_point);
118 c = (tfn & (1 << (a * 2 + b))) != 0;
120 set_pixel (dest_bitmap, dest_point, c);
121 }
122 }
123 return (dest_bitmap);
124 }