bitblt.c

Mon, 14 Dec 2009 16:18:21 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 14 Dec 2009 16:18:21 +0000
changeset 172
2fae6df568f6
parent 135
5d3d2dccb3ff
permissions
-rw-r--r--

remove erroneous 0.33-philpem1 tag

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