Fri, 14 Mar 2003 08:24:37 +0000
finished implementing page labels.
eric@53 | 1 | /* |
eric@125 | 2 | * tumble: build a PDF file from image files |
eric@53 | 3 | * |
eric@53 | 4 | * bitblt routines |
eric@125 | 5 | * $Id: bitblt.c,v 1.16 2003/03/13 00:57:05 eric Exp $ |
eric@53 | 6 | * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> |
eric@53 | 7 | * |
eric@53 | 8 | * This program is free software; you can redistribute it and/or modify |
eric@53 | 9 | * it under the terms of the GNU General Public License version 2 as |
eric@53 | 10 | * published by the Free Software Foundation. Note that permission is |
eric@53 | 11 | * not granted to redistribute this program under the terms of any |
eric@53 | 12 | * other version of the General Public License. |
eric@53 | 13 | * |
eric@53 | 14 | * This program is distributed in the hope that it will be useful, |
eric@53 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
eric@53 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eric@53 | 17 | * GNU General Public License for more details. |
eric@53 | 18 | * |
eric@53 | 19 | * You should have received a copy of the GNU General Public License |
eric@53 | 20 | * along with this program; if not, write to the Free Software |
eric@53 | 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA |
eric@53 | 22 | */ |
eric@53 | 23 | |
eric@53 | 24 | |
eric@48 | 25 | #include <stdbool.h> |
eric@48 | 26 | #include <stdint.h> |
eric@47 | 27 | #include <assert.h> |
eric@42 | 28 | #include <stdio.h> |
eric@2 | 29 | #include <stdlib.h> |
eric@42 | 30 | #include <string.h> |
eric@2 | 31 | |
eric@2 | 32 | #include "bitblt.h" |
eric@2 | 33 | |
eric@53 | 34 | #include "bitblt_tables.h" |
eric@42 | 35 | |
eric@42 | 36 | |
eric@53 | 37 | #define DIV_ROUND_UP(count,pow2) (((count) - 1) / (pow2) + 1) |
eric@42 | 38 | |
eric@47 | 39 | |
eric@48 | 40 | void reverse_bits (uint8_t *p, int byte_count) |
eric@47 | 41 | { |
eric@47 | 42 | while (byte_count--) |
eric@47 | 43 | { |
eric@47 | 44 | (*p) = bit_reverse_byte [*p]; |
eric@47 | 45 | p++; |
eric@47 | 46 | } |
eric@47 | 47 | } |
eric@47 | 48 | |
eric@47 | 49 | |
eric@109 | 50 | static word_t bit_reverse_word (word_t d) |
eric@43 | 51 | { |
eric@43 | 52 | return (bit_reverse_byte [d >> 24] | |
eric@43 | 53 | (bit_reverse_byte [(d >> 16) & 0xff] << 8) | |
eric@43 | 54 | (bit_reverse_byte [(d >> 8) & 0xff] << 16) | |
eric@43 | 55 | (bit_reverse_byte [d & 0xff] << 24)); |
eric@43 | 56 | } |
eric@42 | 57 | |
eric@43 | 58 | |
eric@109 | 59 | static word_t *temp_buffer; |
eric@109 | 60 | static word_t temp_buffer_size; |
eric@42 | 61 | |
eric@48 | 62 | static void realloc_temp_buffer (uint32_t size) |
eric@42 | 63 | { |
eric@42 | 64 | if (size <= temp_buffer_size) |
eric@42 | 65 | return; |
eric@42 | 66 | temp_buffer = realloc (temp_buffer, size); |
eric@42 | 67 | if (! temp_buffer) |
eric@42 | 68 | { |
eric@42 | 69 | fprintf (stderr, "realloc failed in bitblt library\n"); |
eric@42 | 70 | exit (2); |
eric@42 | 71 | } |
eric@42 | 72 | temp_buffer_size = size; |
eric@42 | 73 | } |
eric@42 | 74 | |
eric@42 | 75 | |
eric@109 | 76 | static inline word_t pixel_mask (int x) |
eric@2 | 77 | { |
eric@47 | 78 | #if defined (MIXED_ENDIAN) /* disgusting hack for mixed-endian */ |
eric@109 | 79 | word_t m; |
eric@47 | 80 | m = 0x80 >> (x & 7); |
eric@47 | 81 | m <<= (x & 24); |
eric@47 | 82 | return (m); |
eric@47 | 83 | #elif defined (LSB_RIGHT) |
eric@47 | 84 | return (1U << ((BITS_PER_WORD - 1) - x)); |
eric@2 | 85 | #else |
eric@47 | 86 | return (1U << x); |
eric@2 | 87 | #endif |
eric@42 | 88 | }; |
eric@2 | 89 | |
eric@47 | 90 | |
eric@47 | 91 | /* mask for range of bits left..right, inclusive */ |
eric@109 | 92 | static inline word_t pixel_range_mask (int left, int right) |
eric@47 | 93 | { |
eric@109 | 94 | word_t m1, m2, val; |
eric@47 | 95 | |
eric@47 | 96 | /* $$$ one of these cases is wrong! */ |
eric@47 | 97 | #if defined (LSB_RIGHT) |
eric@47 | 98 | m1 = (~ 0U) >> left; |
eric@47 | 99 | m2 = (~ 0U) << (BITS_PER_WORD - 1 - right); |
eric@47 | 100 | #else |
eric@47 | 101 | m1 = (~ 0U) << left; |
eric@47 | 102 | m2 = (~ 0U) >> (BITS_PER_WORD - 1 - right); |
eric@47 | 103 | #endif |
eric@47 | 104 | val = m1 & m2; |
eric@47 | 105 | |
eric@47 | 106 | printf ("left %d, right %d, mask %08x\n", left, right, val); |
eric@47 | 107 | return (val); |
eric@47 | 108 | }; |
eric@47 | 109 | |
eric@47 | 110 | |
eric@42 | 111 | Bitmap *create_bitmap (Rect *rect) |
eric@2 | 112 | { |
eric@2 | 113 | Bitmap *bitmap; |
eric@48 | 114 | uint32_t width = rect_width (rect); |
eric@48 | 115 | uint32_t height = rect_height (rect); |
eric@2 | 116 | |
eric@35 | 117 | if ((width <= 0) || (height <= 0)) |
eric@35 | 118 | return (NULL); |
eric@35 | 119 | |
eric@2 | 120 | bitmap = calloc (1, sizeof (Bitmap)); |
eric@2 | 121 | if (! bitmap) |
eric@2 | 122 | return (NULL); |
eric@42 | 123 | bitmap->rect = * rect; |
eric@43 | 124 | bitmap->row_words = DIV_ROUND_UP (width, BITS_PER_WORD); |
eric@109 | 125 | bitmap->bits = calloc (1, height * bitmap->row_words * sizeof (word_t)); |
eric@2 | 126 | if (! bitmap->bits) |
eric@2 | 127 | { |
eric@2 | 128 | free (bitmap); |
eric@2 | 129 | return (NULL); |
eric@2 | 130 | } |
eric@2 | 131 | return (bitmap); |
eric@2 | 132 | } |
eric@2 | 133 | |
eric@2 | 134 | void free_bitmap (Bitmap *bitmap) |
eric@2 | 135 | { |
eric@2 | 136 | free (bitmap->bits); |
eric@2 | 137 | free (bitmap); |
eric@2 | 138 | } |
eric@2 | 139 | |
eric@48 | 140 | bool get_pixel (Bitmap *bitmap, Point coord) |
eric@2 | 141 | { |
eric@109 | 142 | word_t *p; |
eric@47 | 143 | int w,b; |
eric@47 | 144 | |
eric@42 | 145 | if ((coord.x < bitmap->rect.min.x) || |
eric@42 | 146 | (coord.x >= bitmap->rect.max.x) || |
eric@42 | 147 | (coord.y < bitmap->rect.min.y) || |
eric@42 | 148 | (coord.y >= bitmap->rect.max.y)) |
eric@2 | 149 | return (0); |
eric@47 | 150 | coord.y -= bitmap->rect.min.y; |
eric@47 | 151 | coord.x -= bitmap->rect.min.x; |
eric@47 | 152 | w = coord.x / BITS_PER_WORD; |
eric@47 | 153 | b = coord.x & (BITS_PER_WORD - 1); |
eric@47 | 154 | p = bitmap->bits + coord.y * bitmap->row_words + w; |
eric@47 | 155 | return (((*p) & pixel_mask (b)) != 0); |
eric@2 | 156 | } |
eric@2 | 157 | |
eric@48 | 158 | void set_pixel (Bitmap *bitmap, Point coord, bool value) |
eric@2 | 159 | { |
eric@109 | 160 | word_t *p; |
eric@47 | 161 | int w,b; |
eric@47 | 162 | |
eric@42 | 163 | if ((coord.x < bitmap->rect.min.x) || |
eric@42 | 164 | (coord.x >= bitmap->rect.max.x) || |
eric@42 | 165 | (coord.y < bitmap->rect.min.y) || |
eric@42 | 166 | (coord.y >= bitmap->rect.max.y)) |
eric@2 | 167 | return; |
eric@47 | 168 | coord.y -= bitmap->rect.min.y; |
eric@47 | 169 | coord.x -= bitmap->rect.min.x; |
eric@47 | 170 | w = coord.x / BITS_PER_WORD; |
eric@47 | 171 | b = coord.x & (BITS_PER_WORD - 1); |
eric@47 | 172 | p = bitmap->bits + coord.y * bitmap->row_words + w; |
eric@2 | 173 | if (value) |
eric@47 | 174 | (*p) |= pixel_mask (b); |
eric@2 | 175 | else |
eric@47 | 176 | (*p) &= ~pixel_mask (b); |
eric@2 | 177 | } |
eric@2 | 178 | |
eric@2 | 179 | |
eric@42 | 180 | /* modifies rect1 to be the intersection of rect1 and rect2; |
eric@42 | 181 | returns true if intersection is non-null */ |
eric@48 | 182 | static bool clip_rect (Rect *rect1, Rect *rect2) |
eric@42 | 183 | { |
eric@42 | 184 | if (rect1->min.y > rect2->max.y) |
eric@42 | 185 | goto empty; |
eric@42 | 186 | if (rect1->min.y < rect2->min.y) |
eric@42 | 187 | { |
eric@42 | 188 | if (rect1->max.y < rect2->max.y) |
eric@42 | 189 | goto empty; |
eric@42 | 190 | rect1->min.y = rect2->min.y; |
eric@42 | 191 | } |
eric@42 | 192 | if (rect1->max.y > rect2->max.y) |
eric@42 | 193 | rect1->max.y = rect2->max.y; |
eric@42 | 194 | |
eric@42 | 195 | if (rect1->min.x > rect2->max.x) |
eric@42 | 196 | goto empty; |
eric@42 | 197 | if (rect1->min.x < rect2->min.x) |
eric@42 | 198 | { |
eric@42 | 199 | if (rect1->max.x < rect2->max.x) |
eric@42 | 200 | goto empty; |
eric@42 | 201 | rect1->min.x = rect2->min.x; |
eric@42 | 202 | } |
eric@42 | 203 | if (rect1->max.x > rect2->max.x) |
eric@42 | 204 | rect1->max.x = rect2->max.x; |
eric@42 | 205 | |
eric@42 | 206 | empty: |
eric@42 | 207 | rect1->min.x = rect1->min.y = |
eric@42 | 208 | rect1->max.x = rect1->max.y = 0; |
eric@42 | 209 | return (0); |
eric@42 | 210 | } |
eric@42 | 211 | |
eric@42 | 212 | |
eric@43 | 213 | static void blt_background (Bitmap *dest_bitmap, |
eric@47 | 214 | Rect dest_rect) |
eric@43 | 215 | { |
eric@48 | 216 | uint32_t y; |
eric@109 | 217 | word_t *rp; |
eric@48 | 218 | uint32_t left_bit, left_word; |
eric@48 | 219 | uint32_t right_bit, right_word; |
eric@109 | 220 | word_t left_mask, right_mask; |
eric@48 | 221 | int32_t word_count; |
eric@43 | 222 | |
eric@43 | 223 | /* This function requires a non-null dest rect */ |
eric@47 | 224 | assert (dest_rect.min.x < dest_rect.max.x); |
eric@47 | 225 | assert (dest_rect.min.y < dest_rect.max.y); |
eric@43 | 226 | |
eric@43 | 227 | /* and that the rows of the dest rect lie entirely within the dest bitmap */ |
eric@47 | 228 | assert (dest_rect.min.y >= dest_bitmap->rect.min.y); |
eric@47 | 229 | assert (dest_rect.max.y <= dest_bitmap->rect.max.y); |
eric@43 | 230 | |
eric@43 | 231 | /* clip the x axis of the dest_rect to the bounds of the dest bitmap */ |
eric@47 | 232 | if (dest_rect.min.x < dest_bitmap->rect.min.x) |
eric@47 | 233 | dest_rect.min.x = dest_bitmap->rect.min.x; |
eric@47 | 234 | if (dest_rect.max.x > dest_bitmap->rect.max.x) |
eric@47 | 235 | dest_rect.max.x = dest_bitmap->rect.max.x; |
eric@47 | 236 | |
eric@47 | 237 | rp = dest_bitmap->bits + |
eric@47 | 238 | (dest_rect.min.y - dest_bitmap->rect.min.y) * dest_bitmap->row_words + |
eric@47 | 239 | (dest_rect.min.x - dest_bitmap->rect.min.x) / BITS_PER_WORD; |
eric@47 | 240 | |
eric@47 | 241 | left_bit = dest_rect.min.x % BITS_PER_WORD; |
eric@47 | 242 | left_word = dest_rect.min.x / BITS_PER_WORD; |
eric@47 | 243 | |
eric@47 | 244 | right_bit = (dest_rect.max.x - 1) % BITS_PER_WORD; |
eric@47 | 245 | right_word = (dest_rect.max.x - 1) / BITS_PER_WORD; |
eric@47 | 246 | |
eric@47 | 247 | word_count = right_word + 1 - left_word; |
eric@47 | 248 | |
eric@47 | 249 | /* special case if entire horizontal range fits in a single word */ |
eric@47 | 250 | if (word_count == 1) |
eric@47 | 251 | { |
eric@47 | 252 | left_mask = 0; |
eric@47 | 253 | right_mask = ~ pixel_range_mask (left_bit, right_bit); |
eric@47 | 254 | word_count = 0; |
eric@47 | 255 | } |
eric@47 | 256 | else |
eric@47 | 257 | { |
eric@47 | 258 | if (left_bit) |
eric@47 | 259 | { |
eric@47 | 260 | left_mask = ~ pixel_range_mask (left_bit, BITS_PER_WORD - 1); |
eric@47 | 261 | word_count--; |
eric@47 | 262 | } |
eric@43 | 263 | |
eric@47 | 264 | if (right_bit != (BITS_PER_WORD - 1)) |
eric@47 | 265 | { |
eric@47 | 266 | right_mask = ~ pixel_range_mask (0, right_bit); |
eric@47 | 267 | word_count--; |
eric@47 | 268 | } |
eric@47 | 269 | } |
eric@47 | 270 | |
eric@47 | 271 | for (y = 0; y < rect_height (& dest_rect); y++) |
eric@43 | 272 | { |
eric@109 | 273 | word_t *wp = rp; |
eric@47 | 274 | |
eric@47 | 275 | /* partial word at left, if any */ |
eric@47 | 276 | if (left_mask) |
eric@47 | 277 | *(wp++) &= left_mask; |
eric@47 | 278 | |
eric@47 | 279 | /* use Duff's Device for the full words */ |
eric@47 | 280 | if (word_count) |
eric@47 | 281 | { |
eric@48 | 282 | int32_t i = word_count; |
eric@47 | 283 | switch (i % 8) |
eric@47 | 284 | { |
eric@47 | 285 | while (i > 0) |
eric@47 | 286 | { |
eric@47 | 287 | *(wp++) = 0; |
eric@47 | 288 | case 7: *(wp++) = 0; |
eric@47 | 289 | case 6: *(wp++) = 0; |
eric@47 | 290 | case 5: *(wp++) = 0; |
eric@47 | 291 | case 4: *(wp++) = 0; |
eric@47 | 292 | case 3: *(wp++) = 0; |
eric@47 | 293 | case 2: *(wp++) = 0; |
eric@47 | 294 | case 1: *(wp++) = 0; |
eric@47 | 295 | case 0: i -= 8; |
eric@47 | 296 | } |
eric@47 | 297 | } |
eric@47 | 298 | } |
eric@47 | 299 | |
eric@47 | 300 | /* partial word at right, if any */ |
eric@47 | 301 | if (right_mask) |
eric@47 | 302 | *wp &= right_mask; |
eric@47 | 303 | |
eric@47 | 304 | /* advance to next row */ |
eric@43 | 305 | rp += dest_bitmap->row_words; |
eric@43 | 306 | } |
eric@43 | 307 | } |
eric@43 | 308 | |
eric@43 | 309 | |
eric@47 | 310 | #if 0 |
eric@43 | 311 | static void blt (Bitmap *src_bitmap, |
eric@43 | 312 | Rect *src_rect, |
eric@43 | 313 | Bitmap *dest_bitmap, |
eric@43 | 314 | Rect *dest_rect) |
eric@43 | 315 | { |
eric@48 | 316 | int32_t y; |
eric@109 | 317 | word_t *rp; |
eric@43 | 318 | |
eric@43 | 319 | /* This function requires a non-null src rect */ |
eric@43 | 320 | assert (dest_rect->min.x < dest_rect->max.x); |
eric@43 | 321 | assert (dest_rect->min.y < dest_rect->max.y); |
eric@43 | 322 | |
eric@43 | 323 | /* and a non-null dest rect */ |
eric@43 | 324 | assert (dest_rect->min.x < dest_rect->max.x); |
eric@43 | 325 | assert (dest_rect->min.y < dest_rect->max.y); |
eric@43 | 326 | |
eric@43 | 327 | /* and that the widths and heights of the rects match */ |
eric@43 | 328 | assert (rect_width (src_rect) == rect_width (dest_rect)); |
eric@43 | 329 | assert (rect_height (src_rect) == rect_height (dest_rect)); |
eric@43 | 330 | |
eric@43 | 331 | /* and that the rows of the src rect lie entirely within the src bitmap */ |
eric@43 | 332 | assert (dest_rect->min.y >= dest_bitmap->rect->min.y); |
eric@43 | 333 | assert (dest_rect->max.y <= dest_bitmap->rect->max.y); |
eric@43 | 334 | |
eric@43 | 335 | /* and that the rows of the dest rect lie entirely within the dest bitmap */ |
eric@43 | 336 | assert (dest_rect->min.y >= dest_bitmap->rect->min.y); |
eric@43 | 337 | assert (dest_rect->max.y <= dest_bitmap->rect->max.y); |
eric@43 | 338 | |
eric@43 | 339 | /* clip the x axis of the dest_rect to the bounds of the dest bitmap, |
eric@43 | 340 | and adjust the src_rect to match */ |
eric@43 | 341 | if (dest_rect->min.x < dest_bitmap->rect.min.x) |
eric@43 | 342 | { |
eric@43 | 343 | src_rect->min.x += ???; |
eric@43 | 344 | dest_rect->min.x = dest_bitmap->rect.min.x; |
eric@43 | 345 | } |
eric@43 | 346 | if (dest_rect->max.x > dest_bitmap->rect.max.x) |
eric@43 | 347 | { |
eric@43 | 348 | dest_rect->max.x = dest_bitmap->rect.max.x; |
eric@43 | 349 | } |
eric@43 | 350 | |
eric@43 | 351 | rp = ???; |
eric@43 | 352 | for (y = 0; y < rect_height (dest_rect); y++) |
eric@43 | 353 | { |
eric@43 | 354 | ???; |
eric@43 | 355 | rp += dest_bitmap->row_words; |
eric@43 | 356 | } |
eric@43 | 357 | } |
eric@43 | 358 | |
eric@43 | 359 | |
eric@47 | 360 | /* |
eric@47 | 361 | * The destination rectangle is first clipped to the dest bitmap, and |
eric@47 | 362 | * the source rectangle is adjusted in the corresponding manner. |
eric@47 | 363 | * What's left is divided into five sections, any of which may be |
eric@47 | 364 | * null. The portion that actually corresponds to the intersection of |
eric@47 | 365 | * the source rectangle and the source bitmpa is the "middle". The |
eric@47 | 366 | * other four sections will use the background color as the source |
eric@47 | 367 | * operand. |
eric@47 | 368 | * |
eric@47 | 369 | * |
eric@47 | 370 | * y0 -> ------------------------------------------------- |
eric@47 | 371 | * | top | |
eric@47 | 372 | * | | |
eric@47 | 373 | * y1 -> ------------------------------------------------- |
eric@47 | 374 | * | left | middle | right | |
eric@47 | 375 | * | | | | |
eric@47 | 376 | * y2 -> ------------------------------------------------- |
eric@47 | 377 | * | bottom | |
eric@47 | 378 | * | | |
eric@47 | 379 | * y3 -> ------------------------------------------------- |
eric@47 | 380 | * |
eric@47 | 381 | * ^ ^ ^ ^ |
eric@47 | 382 | * | | | | |
eric@47 | 383 | * x0 x1 x2 x3 |
eric@47 | 384 | * |
eric@47 | 385 | * */ |
eric@2 | 386 | Bitmap *bitblt (Bitmap *src_bitmap, |
eric@42 | 387 | Rect *src_rect, |
eric@2 | 388 | Bitmap *dest_bitmap, |
eric@42 | 389 | Point *dest_min, |
eric@43 | 390 | int tfn, |
eric@43 | 391 | int background) |
eric@43 | 392 | { |
eric@43 | 393 | Rect sr, dr; /* src and dest rects, clipped to visible portion of |
eric@43 | 394 | dest rect */ |
eric@48 | 395 | uint32_t drw, drh; /* dest rect width, height - gets adjusted */ |
eric@43 | 396 | Point src_point, dest_point; |
eric@43 | 397 | |
eric@47 | 398 | /* dest coordinates: */ |
eric@48 | 399 | uint32_t x0, x1, x2, x3; |
eric@48 | 400 | uint32_t y0, y1, y2, y3; |
eric@47 | 401 | |
eric@43 | 402 | { |
eric@43 | 403 | sr = * src_rect; |
eric@43 | 404 | |
eric@48 | 405 | uint32_t srw = rect_width (& sr); |
eric@48 | 406 | uint32_t srh = rect_height (& sr); |
eric@43 | 407 | |
eric@43 | 408 | if ((srw < 0) || (srh < 0)) |
eric@43 | 409 | goto done; /* the source rect is empty! */ |
eric@43 | 410 | |
eric@47 | 411 | dr.min.x = dest_min->x; |
eric@47 | 412 | dr.min.y = dest_min->y; |
eric@43 | 413 | dr.max.x = dr.min.x + srw; |
eric@43 | 414 | dr.max.y = dr.min.y + srh; |
eric@43 | 415 | } |
eric@43 | 416 | |
eric@43 | 417 | if (! dest_bitmap) |
eric@43 | 418 | { |
eric@43 | 419 | dest_bitmap = create_bitmap (& dr); |
eric@43 | 420 | if (! dest_bitmap) |
eric@43 | 421 | return (NULL); |
eric@43 | 422 | } |
eric@43 | 423 | |
eric@43 | 424 | if ((dr.min.x >= dest_bitmap->rect.max.x) || |
eric@43 | 425 | (dr.min.y >= dest_bitmap->rect.max.y)) |
eric@43 | 426 | goto done; /* the dest rect isn't even in the dest bitmap! */ |
eric@43 | 427 | |
eric@43 | 428 | /* crop dest rect to dest bitmap */ |
eric@43 | 429 | delta = dest_bitmap->rect.min.x - dr.min.x; |
eric@43 | 430 | if (delta > 0) |
eric@43 | 431 | { |
eric@43 | 432 | sr.min.x += delta; |
eric@43 | 433 | dr.min.x += delta; |
eric@43 | 434 | } |
eric@43 | 435 | |
eric@43 | 436 | delta = dest_bitmap->rect.min.y - dr.min.y; |
eric@43 | 437 | if (delta > 0) |
eric@43 | 438 | { |
eric@43 | 439 | sr.min.y += delta; |
eric@43 | 440 | dr.min.y += delta; |
eric@43 | 441 | } |
eric@43 | 442 | |
eric@43 | 443 | delta = dr.max.x - dest_bitmap->rect.max.x; |
eric@43 | 444 | if (delta > 0) |
eric@43 | 445 | { |
eric@43 | 446 | sr.max.x -= delta; |
eric@43 | 447 | dr.max.x -= delta; |
eric@43 | 448 | } |
eric@43 | 449 | |
eric@43 | 450 | delta = dr.max.y - dest_bitmap->rect.max.y; |
eric@43 | 451 | if (delta > 0) |
eric@43 | 452 | { |
eric@43 | 453 | sr.max.x -= delta; |
eric@43 | 454 | dr.max.x -= delta; |
eric@43 | 455 | } |
eric@43 | 456 | |
eric@43 | 457 | drw = rect_width (& dr); |
eric@43 | 458 | drh = rect_height (& dh); |
eric@43 | 459 | |
eric@47 | 460 | x0 = dr.min.x; |
eric@47 | 461 | y0 = dr.min.y; |
eric@47 | 462 | x3 = dr.max.x; |
eric@47 | 463 | y3 = dr.max.y; |
eric@47 | 464 | |
eric@47 | 465 | #if 0 |
eric@43 | 466 | /* if the source rect min y is >= the source bitmap max y, |
eric@43 | 467 | we transfer background color to the entire dest rect */ |
eric@43 | 468 | if (sr.min.y >= src->rect.max.y) |
eric@43 | 469 | { |
eric@47 | 470 | blt_background (dest_bitmap, dr); |
eric@43 | 471 | goto done; |
eric@43 | 472 | } |
eric@47 | 473 | #endif |
eric@47 | 474 | |
eric@47 | 475 | /* top */ |
eric@47 | 476 | if (y0 != y1) |
eric@47 | 477 | { |
eric@47 | 478 | dr2.min.x = x0; |
eric@47 | 479 | dr2.max.x = x3; |
eric@47 | 480 | dr2.min.y = y0; |
eric@47 | 481 | dr2.max.y = y1; |
eric@47 | 482 | blt_background (dest_bitmap, & dr2); |
eric@47 | 483 | } |
eric@43 | 484 | |
eric@47 | 485 | /* |
eric@47 | 486 | * top: if the source rect min y is less than the source bitmap min y, |
eric@47 | 487 | * we need to transfer some backgound color to the top part of the dest |
eric@47 | 488 | * rect |
eric@47 | 489 | */ |
eric@43 | 490 | if (sr.min.y < src->rect.min.y) |
eric@43 | 491 | { |
eric@43 | 492 | Rect dr2; |
eric@43 | 493 | uint32 bg_height; |
eric@43 | 494 | |
eric@43 | 495 | bg_height = src->rect.min.y - sr.min.y; |
eric@43 | 496 | if (bg_height > sh) |
eric@43 | 497 | bg_height = sh; |
eric@43 | 498 | |
eric@43 | 499 | dr2 = dr; |
eric@43 | 500 | dr2.max.y = dr2.min.y + bg_height; |
eric@43 | 501 | |
eric@43 | 502 | blt_background (dest_bitmap, & dr2); |
eric@43 | 503 | |
eric@43 | 504 | /* now reduce the rect height by the number of lines of background |
eric@43 | 505 | color */ |
eric@43 | 506 | sr.min.y += bg_height; |
eric@43 | 507 | dr.min.y += bg_height; |
eric@43 | 508 | sh -= bg_height; |
eric@43 | 509 | dh -= bg_height; |
eric@43 | 510 | |
eric@43 | 511 | if (sr.min.y == sr.max.y) |
eric@43 | 512 | goto done; |
eric@43 | 513 | } |
eric@43 | 514 | |
eric@47 | 515 | if (y1 != y2) |
eric@47 | 516 | { |
eric@47 | 517 | /* left */ |
eric@47 | 518 | if (x0 != x1) |
eric@47 | 519 | { |
eric@47 | 520 | dr2.min.x = x1; |
eric@47 | 521 | dr2.max.x = x1; |
eric@47 | 522 | dr2.min.y = y1; |
eric@47 | 523 | dr2.max.y = y2 |
eric@47 | 524 | blt_background (dest_bitmap, & dr2); |
eric@47 | 525 | } |
eric@47 | 526 | |
eric@47 | 527 | /* middle */ |
eric@47 | 528 | if (x1 != x2) |
eric@47 | 529 | { |
eric@47 | 530 | /* ??? */ |
eric@47 | 531 | } |
eric@43 | 532 | |
eric@47 | 533 | /* right */ |
eric@47 | 534 | if (x2 != x3) |
eric@47 | 535 | { |
eric@47 | 536 | dr2.min.x = x2; |
eric@47 | 537 | dr2.max.x = x3; |
eric@47 | 538 | dr2.min.y = y1; |
eric@47 | 539 | dr2.max.y = y2 |
eric@47 | 540 | blt_background (dest_bitmap, & dr2); |
eric@47 | 541 | } |
eric@47 | 542 | } |
eric@47 | 543 | |
eric@47 | 544 | /* bottom */ |
eric@47 | 545 | if (y2 != y3) |
eric@43 | 546 | { |
eric@47 | 547 | dr2.min.x = x0; |
eric@47 | 548 | dr2.max.x = x3; |
eric@47 | 549 | dr2.min.y = y2; |
eric@47 | 550 | dr2.max.y = y3; |
eric@47 | 551 | blt_background (dest_bitmap, & dr2); |
eric@43 | 552 | } |
eric@43 | 553 | |
eric@43 | 554 | done: |
eric@43 | 555 | return (dest_bitmap); |
eric@43 | 556 | } |
eric@43 | 557 | #else |
eric@43 | 558 | Bitmap *bitblt (Bitmap *src_bitmap, |
eric@43 | 559 | Rect *src_rect, |
eric@43 | 560 | Bitmap *dest_bitmap, |
eric@43 | 561 | Point *dest_min, |
eric@43 | 562 | int tfn, |
eric@43 | 563 | int background) |
eric@2 | 564 | { |
eric@2 | 565 | Point src_point, dest_point; |
eric@2 | 566 | |
eric@2 | 567 | if (! dest_bitmap) |
eric@2 | 568 | { |
eric@42 | 569 | Rect dest_rect = {{ 0, 0 }, { dest_min->x + rect_width (src_rect), |
eric@42 | 570 | dest_min->y + rect_height (src_rect) }}; |
eric@42 | 571 | dest_bitmap = create_bitmap (& dest_rect); |
eric@2 | 572 | if (! dest_bitmap) |
eric@2 | 573 | return (NULL); |
eric@2 | 574 | } |
eric@2 | 575 | |
eric@47 | 576 | if (tfn == TF_SRC) |
eric@2 | 577 | { |
eric@47 | 578 | for (src_point.y = src_rect->min.y; |
eric@47 | 579 | src_point.y < src_rect->max.y; |
eric@47 | 580 | src_point.y++) |
eric@2 | 581 | { |
eric@47 | 582 | dest_point.y = dest_min->y + src_point.y - src_rect->min.y; |
eric@47 | 583 | |
eric@47 | 584 | for (src_point.x = src_rect->min.x; |
eric@47 | 585 | src_point.x < src_rect->max.x; |
eric@47 | 586 | src_point.x++) |
eric@47 | 587 | { |
eric@48 | 588 | bool a; |
eric@2 | 589 | |
eric@47 | 590 | dest_point.x = dest_min->x + src_point.x - src_rect->min.x; |
eric@3 | 591 | |
eric@47 | 592 | a = get_pixel (src_bitmap, src_point); |
eric@47 | 593 | set_pixel (dest_bitmap, dest_point, a); |
eric@47 | 594 | } |
eric@47 | 595 | } |
eric@47 | 596 | } |
eric@47 | 597 | else |
eric@47 | 598 | { |
eric@47 | 599 | for (src_point.y = src_rect->min.y; |
eric@47 | 600 | src_point.y < src_rect->max.y; |
eric@47 | 601 | src_point.y++) |
eric@47 | 602 | { |
eric@47 | 603 | dest_point.y = dest_min->y + src_point.y - src_rect->min.y; |
eric@47 | 604 | |
eric@47 | 605 | for (src_point.x = src_rect->min.x; |
eric@47 | 606 | src_point.x < src_rect->max.x; |
eric@47 | 607 | src_point.x++) |
eric@47 | 608 | { |
eric@48 | 609 | bool a, b, c; |
eric@2 | 610 | |
eric@47 | 611 | dest_point.x = dest_min->x + src_point.x - src_rect->min.x; |
eric@47 | 612 | |
eric@47 | 613 | a = get_pixel (src_bitmap, src_point); |
eric@47 | 614 | b = get_pixel (dest_bitmap, dest_point); |
eric@47 | 615 | c = (tfn & (1 << (a * 2 + b))) != 0; |
eric@47 | 616 | |
eric@47 | 617 | set_pixel (dest_bitmap, dest_point, c); |
eric@47 | 618 | } |
eric@2 | 619 | } |
eric@2 | 620 | } |
eric@2 | 621 | return (dest_bitmap); |
eric@2 | 622 | } |
eric@43 | 623 | #endif |
eric@42 | 624 | |
eric@42 | 625 | |
eric@42 | 626 | /* in-place transformations */ |
eric@42 | 627 | void flip_h (Bitmap *src) |
eric@42 | 628 | { |
eric@109 | 629 | word_t *rp; /* row pointer */ |
eric@109 | 630 | word_t *p1; /* work src ptr */ |
eric@109 | 631 | word_t *p2; /* work dest ptr */ |
eric@48 | 632 | int32_t y; |
eric@43 | 633 | int shift1, shift2; |
eric@42 | 634 | |
eric@109 | 635 | realloc_temp_buffer ((src->row_words + 1) * sizeof (word_t)); |
eric@42 | 636 | |
eric@42 | 637 | rp = src->bits; |
eric@42 | 638 | if ((rect_width (& src->rect) & 7) == 0) |
eric@42 | 639 | { |
eric@42 | 640 | for (y = src->rect.min.y; y < src->rect.max.y; y++) |
eric@42 | 641 | { |
eric@109 | 642 | memcpy (temp_buffer, rp, src->row_words * sizeof (word_t)); |
eric@43 | 643 | p1 = temp_buffer + src->row_words; |
eric@42 | 644 | p2 = rp; |
eric@42 | 645 | |
eric@42 | 646 | while (p1 >= temp_buffer) |
eric@43 | 647 | *(p2++) = bit_reverse_word (*(p1--)); |
eric@42 | 648 | |
eric@43 | 649 | rp += src->row_words; |
eric@42 | 650 | } |
eric@42 | 651 | return; |
eric@42 | 652 | } |
eric@42 | 653 | |
eric@42 | 654 | temp_buffer [0] = 0; |
eric@43 | 655 | shift1 = rect_width (& src->rect) & (BITS_PER_WORD - 1); |
eric@43 | 656 | shift2 = BITS_PER_WORD - shift1; |
eric@42 | 657 | |
eric@42 | 658 | for (y = src->rect.min.y; y < src->rect.max.y; y++) |
eric@42 | 659 | { |
eric@109 | 660 | word_t d1, d2; |
eric@43 | 661 | |
eric@109 | 662 | memcpy (temp_buffer + 1, rp, src->row_words * sizeof (word_t)); |
eric@43 | 663 | p1 = temp_buffer + src->row_words; |
eric@42 | 664 | p2 = rp; |
eric@42 | 665 | |
eric@43 | 666 | d2 = *(p1--); |
eric@42 | 667 | |
eric@42 | 668 | while (p1 >= temp_buffer) |
eric@42 | 669 | { |
eric@43 | 670 | d1 = *(p1--); |
eric@43 | 671 | *(p2++) = bit_reverse_word ((d1 << shift1) | (d2 >> shift2)); |
eric@43 | 672 | d2 = d1; |
eric@42 | 673 | } |
eric@42 | 674 | |
eric@43 | 675 | rp += src->row_words; |
eric@42 | 676 | } |
eric@42 | 677 | } |
eric@42 | 678 | |
eric@42 | 679 | void flip_v (Bitmap *src) |
eric@42 | 680 | { |
eric@109 | 681 | word_t *p1, *p2; |
eric@42 | 682 | |
eric@109 | 683 | realloc_temp_buffer (src->row_words * sizeof (word_t)); |
eric@42 | 684 | |
eric@42 | 685 | p1 = src->bits; |
eric@43 | 686 | p2 = src->bits + src->row_words * (rect_height (& src->rect) - 1); |
eric@42 | 687 | while (p1 < p2) |
eric@42 | 688 | { |
eric@109 | 689 | memcpy (temp_buffer, p1, src->row_words * sizeof (word_t)); |
eric@109 | 690 | memcpy (p1, p2, src->row_words * sizeof (word_t)); |
eric@109 | 691 | memcpy (p2, temp_buffer, src->row_words * sizeof (word_t)); |
eric@43 | 692 | p1 += src->row_words; |
eric@43 | 693 | p2 -= src->row_words; |
eric@42 | 694 | } |
eric@42 | 695 | } |
eric@42 | 696 | |
eric@42 | 697 | void rot_180 (Bitmap *src) /* combination of flip_h and flip_v */ |
eric@42 | 698 | { |
eric@42 | 699 | flip_h (src); |
eric@42 | 700 | flip_v (src); |
eric@42 | 701 | } |
eric@42 | 702 | |
eric@42 | 703 | /* "in-place" transformations - will allocate new memory and free old */ |
eric@42 | 704 | void transpose (Bitmap *src) |
eric@42 | 705 | { |
eric@48 | 706 | uint32_t new_row_words = DIV_ROUND_UP (rect_height (& src->rect), 32); |
eric@109 | 707 | word_t *new_bits; |
eric@42 | 708 | |
eric@109 | 709 | new_bits = calloc (1, new_row_words * rect_width (& src->rect) * sizeof (word_t)); |
eric@42 | 710 | |
eric@42 | 711 | /* $$$ more code needed here */ |
eric@42 | 712 | } |
eric@42 | 713 | |
eric@42 | 714 | void rot_90 (Bitmap *src) /* transpose + flip_h */ |
eric@42 | 715 | { |
eric@42 | 716 | transpose (src); |
eric@42 | 717 | flip_h (src); |
eric@42 | 718 | } |
eric@42 | 719 | |
eric@42 | 720 | void rot_270 (Bitmap *src) /* transpose + flip_v */ |
eric@42 | 721 | { |
eric@42 | 722 | transpose (src); |
eric@42 | 723 | flip_v (src); |
eric@42 | 724 | } |
eric@53 | 725 | |
eric@53 | 726 |