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