bitblt.c

Wed, 19 Feb 2003 10:14:49 +0000

author
eric
date
Wed, 19 Feb 2003 10:14:49 +0000
changeset 53
bb180150e603
parent 48
3d0be1c1c1b2
child 57
b2a2f61135bb
permissions
-rw-r--r--

rename br.c to bitblt_table_gen.c, and integrate use of it into build process

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