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