1.1 diff -r 2350a4b7393c -r 9c85a4cd88a3 bitblt.c 1.2 --- a/bitblt.c Wed Jan 02 16:39:20 2002 +0000 1.3 +++ b/bitblt.c Wed Jan 02 16:39:39 2002 +0000 1.4 @@ -1,8 +1,68 @@ 1.5 +#include <stdio.h> 1.6 #include <stdlib.h> 1.7 +#include <string.h> 1.8 1.9 #include "type.h" 1.10 #include "bitblt.h" 1.11 1.12 + 1.13 +#define CALC_ROWBYTES(width) (((width) - 1) / 8 + 1) 1.14 + 1.15 + 1.16 +static const u8 bit_reverse_byte [0x100] = 1.17 +{ 1.18 + 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 1.19 + 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 1.20 + 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 1.21 + 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 1.22 + 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 1.23 + 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 1.24 + 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 1.25 + 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 1.26 + 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 1.27 + 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 1.28 + 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 1.29 + 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 1.30 + 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 1.31 + 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, 1.32 + 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 1.33 + 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 1.34 + 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 1.35 + 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 1.36 + 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 1.37 + 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 1.38 + 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 1.39 + 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 1.40 + 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 1.41 + 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 1.42 + 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 1.43 + 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 1.44 + 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 1.45 + 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 1.46 + 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 1.47 + 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 1.48 + 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 1.49 + 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff 1.50 +}; 1.51 + 1.52 + 1.53 +static u8 *temp_buffer; 1.54 +static u32 temp_buffer_size; 1.55 + 1.56 +static void realloc_temp_buffer (u32 size) 1.57 +{ 1.58 + if (size <= temp_buffer_size) 1.59 + return; 1.60 + temp_buffer = realloc (temp_buffer, size); 1.61 + if (! temp_buffer) 1.62 + { 1.63 + fprintf (stderr, "realloc failed in bitblt library\n"); 1.64 + exit (2); 1.65 + } 1.66 + temp_buffer_size = size; 1.67 +} 1.68 + 1.69 + 1.70 static inline u8 pixel_mask (x) 1.71 { 1.72 #ifdef LSB_LEFT 1.73 @@ -10,21 +70,13 @@ 1.74 #else 1.75 return (1 << (7 - x)); 1.76 #endif 1.77 -} 1.78 - 1.79 -static inline s32 rect_width (Rect r) 1.80 -{ 1.81 - return (r.lower_right.x - r.upper_left.x); 1.82 -} 1.83 +}; 1.84 1.85 -static inline s32 rect_height (Rect r) 1.86 -{ 1.87 - return (r.lower_right.y - r.upper_left.y); 1.88 -} 1.89 - 1.90 -Bitmap *create_bitmap (s32 width, s32 height) 1.91 +Bitmap *create_bitmap (Rect *rect) 1.92 { 1.93 Bitmap *bitmap; 1.94 + u32 width = rect_width (rect); 1.95 + u32 height = rect_height (rect); 1.96 1.97 if ((width <= 0) || (height <= 0)) 1.98 return (NULL); 1.99 @@ -32,9 +84,8 @@ 1.100 bitmap = calloc (1, sizeof (Bitmap)); 1.101 if (! bitmap) 1.102 return (NULL); 1.103 - bitmap->width = width; 1.104 - bitmap->height = height; 1.105 - bitmap->rowbytes = (width - 1) / 8 + 1; 1.106 + bitmap->rect = * rect; 1.107 + bitmap->rowbytes = CALC_ROWBYTES (width); 1.108 bitmap->bits = calloc (height * bitmap->rowbytes, 1); 1.109 if (! bitmap->bits) 1.110 { 1.111 @@ -53,20 +104,28 @@ 1.112 boolean get_pixel (Bitmap *bitmap, Point coord) 1.113 { 1.114 u8 *p; 1.115 - if ((coord.x < 0) || (coord.y < 0) || 1.116 - (coord.x >= bitmap->width) || (coord.y >= bitmap->height)) 1.117 + if ((coord.x < bitmap->rect.min.x) || 1.118 + (coord.x >= bitmap->rect.max.x) || 1.119 + (coord.y < bitmap->rect.min.y) || 1.120 + (coord.y >= bitmap->rect.max.y)) 1.121 return (0); 1.122 - p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8; 1.123 + p = bitmap->bits + 1.124 + (coord.y - bitmap->rect.min.y) * bitmap->rowbytes + 1.125 + (coord.x - bitmap->rect.min.x) / 8; 1.126 return ((*p & pixel_mask (coord.x & 7)) != 0); 1.127 } 1.128 1.129 void set_pixel (Bitmap *bitmap, Point coord, boolean value) 1.130 { 1.131 u8 *p; 1.132 - if ((coord.x < 0) || (coord.y < 0) || 1.133 - (coord.x >= bitmap->width) || (coord.y >= bitmap->height)) 1.134 + if ((coord.x < bitmap->rect.min.x) || 1.135 + (coord.x >= bitmap->rect.max.x) || 1.136 + (coord.y < bitmap->rect.min.y) || 1.137 + (coord.y >= bitmap->rect.max.y)) 1.138 return; 1.139 - p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8; 1.140 + p = bitmap->bits + 1.141 + (coord.y - bitmap->rect.min.y) * bitmap->rowbytes + 1.142 + (coord.x - bitmap->rect.min.x) / 8; 1.143 if (value) 1.144 *p |= pixel_mask (coord.x & 7); 1.145 else 1.146 @@ -74,62 +133,69 @@ 1.147 } 1.148 1.149 1.150 +/* modifies rect1 to be the intersection of rect1 and rect2; 1.151 + returns true if intersection is non-null */ 1.152 +static boolean clip_rect (Rect *rect1, Rect *rect2) 1.153 +{ 1.154 + if (rect1->min.y > rect2->max.y) 1.155 + goto empty; 1.156 + if (rect1->min.y < rect2->min.y) 1.157 + { 1.158 + if (rect1->max.y < rect2->max.y) 1.159 + goto empty; 1.160 + rect1->min.y = rect2->min.y; 1.161 + } 1.162 + if (rect1->max.y > rect2->max.y) 1.163 + rect1->max.y = rect2->max.y; 1.164 + 1.165 + if (rect1->min.x > rect2->max.x) 1.166 + goto empty; 1.167 + if (rect1->min.x < rect2->min.x) 1.168 + { 1.169 + if (rect1->max.x < rect2->max.x) 1.170 + goto empty; 1.171 + rect1->min.x = rect2->min.x; 1.172 + } 1.173 + if (rect1->max.x > rect2->max.x) 1.174 + rect1->max.x = rect2->max.x; 1.175 + 1.176 + empty: 1.177 + rect1->min.x = rect1->min.y = 1.178 + rect1->max.x = rect1->max.y = 0; 1.179 + return (0); 1.180 +} 1.181 + 1.182 + 1.183 Bitmap *bitblt (Bitmap *src_bitmap, 1.184 - Rect src_rect, 1.185 + Rect *src_rect, 1.186 Bitmap *dest_bitmap, 1.187 - Point dest_upper_left, 1.188 - int scan, 1.189 + Point *dest_min, 1.190 int tfn) 1.191 { 1.192 Point src_point, dest_point; 1.193 1.194 if (! dest_bitmap) 1.195 { 1.196 - if (scan & TRANSPOSE) 1.197 - dest_bitmap = create_bitmap (dest_upper_left.x + rect_height (src_rect), 1.198 - dest_upper_left.y + rect_width (src_rect)); 1.199 - else 1.200 - dest_bitmap = create_bitmap (dest_upper_left.x + rect_width (src_rect), 1.201 - dest_upper_left.y + rect_height (src_rect)); 1.202 + Rect dest_rect = {{ 0, 0 }, { dest_min->x + rect_width (src_rect), 1.203 + dest_min->y + rect_height (src_rect) }}; 1.204 + dest_bitmap = create_bitmap (& dest_rect); 1.205 if (! dest_bitmap) 1.206 return (NULL); 1.207 } 1.208 1.209 - for (src_point.y = src_rect.upper_left.y; 1.210 - src_point.y < src_rect.lower_right.y; 1.211 + for (src_point.y = src_rect->min.y; 1.212 + src_point.y < src_rect->max.y; 1.213 src_point.y++) 1.214 { 1.215 - for (src_point.x = src_rect.upper_left.x; 1.216 - src_point.x < src_rect.lower_right.x; 1.217 + dest_point.y = dest_min->y + src_point.y - src_rect->min.y; 1.218 + 1.219 + for (src_point.x = src_rect->min.x; 1.220 + src_point.x < src_rect->max.x; 1.221 src_point.x++) 1.222 { 1.223 boolean a, b, c; 1.224 1.225 - if (scan & TRANSPOSE) 1.226 - { 1.227 - dest_point.x = src_point.y - src_rect.upper_left.y; 1.228 - dest_point.y = src_point.x - src_rect.upper_left.x; 1.229 - 1.230 - if (scan & FLIP_H) 1.231 - dest_point.x = (rect_height (src_rect) - 1) - dest_point.x; 1.232 - 1.233 - if (scan & FLIP_V) 1.234 - dest_point.y = (rect_width (src_rect) - 1) - dest_point.y; 1.235 - } 1.236 - else 1.237 - { 1.238 - dest_point.x = src_point.x - src_rect.upper_left.x; 1.239 - dest_point.y = src_point.y - src_rect.upper_left.y; 1.240 - 1.241 - if (scan & FLIP_H) 1.242 - dest_point.x = (rect_width (src_rect) - 1) - dest_point.x; 1.243 - 1.244 - if (scan & FLIP_V) 1.245 - dest_point.y = (rect_height (src_rect) - 1) - dest_point.y; 1.246 - } 1.247 - 1.248 - dest_point.x += dest_upper_left.x; 1.249 - dest_point.y += dest_upper_left.y; 1.250 + dest_point.x = dest_min->x + src_point.x - src_rect->min.x; 1.251 1.252 a = get_pixel (src_bitmap, src_point); 1.253 b = get_pixel (dest_bitmap, dest_point); 1.254 @@ -140,3 +206,101 @@ 1.255 } 1.256 return (dest_bitmap); 1.257 } 1.258 + 1.259 + 1.260 +/* in-place transformations */ 1.261 +void flip_h (Bitmap *src) 1.262 +{ 1.263 + u8 *rp; /* row pointer */ 1.264 + u8 *p1; /* work src ptr */ 1.265 + u8 *p2; /* work dest ptr */ 1.266 + u16 d; 1.267 + s32 y; 1.268 + int shift; 1.269 + 1.270 + realloc_temp_buffer (src->rowbytes + 1); 1.271 + 1.272 + rp = src->bits; 1.273 + if ((rect_width (& src->rect) & 7) == 0) 1.274 + { 1.275 + for (y = src->rect.min.y; y < src->rect.max.y; y++) 1.276 + { 1.277 + memcpy (temp_buffer, rp, src->rowbytes); 1.278 + p1 = temp_buffer + src->rowbytes - 1; 1.279 + p2 = rp; 1.280 + 1.281 + while (p1 >= temp_buffer) 1.282 + *(p2++) = bit_reverse_byte [*(p1--)]; 1.283 + 1.284 + rp += src->rowbytes; 1.285 + } 1.286 + return; 1.287 + } 1.288 + 1.289 + temp_buffer [0] = 0; 1.290 + shift = 8 - (rect_width (& src->rect) & 7); 1.291 + 1.292 + for (y = src->rect.min.y; y < src->rect.max.y; y++) 1.293 + { 1.294 + memcpy (temp_buffer + 1, rp, src->rowbytes); 1.295 + p1 = temp_buffer + src->rowbytes; 1.296 + p2 = rp; 1.297 + 1.298 + d = *(p1--); 1.299 + 1.300 + while (p1 >= temp_buffer) 1.301 + { 1.302 + d = (d >> 8) | ((*(p1--)) << 8); 1.303 + *(p2++) = bit_reverse_byte [(d >> shift) & 0xff]; 1.304 + } 1.305 + 1.306 + rp += src->rowbytes; 1.307 + } 1.308 +} 1.309 + 1.310 +void flip_v (Bitmap *src) 1.311 +{ 1.312 + u8 *p1, *p2; 1.313 + 1.314 + realloc_temp_buffer (src->rowbytes); 1.315 + 1.316 + p1 = src->bits; 1.317 + p2 = src->bits + src->rowbytes * (rect_height (& src->rect) - 1); 1.318 + while (p1 < p2) 1.319 + { 1.320 + memcpy (temp_buffer, p1, src->rowbytes); 1.321 + memcpy (p1, p2, src->rowbytes); 1.322 + memcpy (p2, temp_buffer, src->rowbytes); 1.323 + p1 += src->rowbytes; 1.324 + p2 -= src->rowbytes; 1.325 + } 1.326 +} 1.327 + 1.328 +void rot_180 (Bitmap *src) /* combination of flip_h and flip_v */ 1.329 +{ 1.330 + flip_h (src); 1.331 + flip_v (src); 1.332 +} 1.333 + 1.334 +/* "in-place" transformations - will allocate new memory and free old */ 1.335 +void transpose (Bitmap *src) 1.336 +{ 1.337 + u32 new_rowbytes = CALC_ROWBYTES (rect_height (& src->rect)); 1.338 + u8 *new_bits; 1.339 + 1.340 + new_bits = calloc (1, new_rowbytes * rect_width (& src->rect)); 1.341 + 1.342 + /* $$$ more code needed here */ 1.343 +} 1.344 + 1.345 +void rot_90 (Bitmap *src) /* transpose + flip_h */ 1.346 +{ 1.347 + transpose (src); 1.348 + flip_h (src); 1.349 +} 1.350 + 1.351 +void rot_270 (Bitmap *src) /* transpose + flip_v */ 1.352 +{ 1.353 + transpose (src); 1.354 + flip_v (src); 1.355 +}