Tue, 11 Mar 2003 11:14:39 +0000
many bug fixes including bit ordering of output, search positions and polarities. added G4_DEBUG conditional.
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@94 | 7 | * $Id: bitblt.h,v 1.14 2003/03/10 05:08:25 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@2 | 27 | typedef struct Point |
eric@2 | 28 | { |
eric@48 | 29 | int32_t x; |
eric@48 | 30 | int32_t y; |
eric@2 | 31 | } Point; |
eric@2 | 32 | |
eric@2 | 33 | typedef struct Rect |
eric@2 | 34 | { |
eric@42 | 35 | Point min; |
eric@42 | 36 | Point max; |
eric@2 | 37 | } Rect; |
eric@2 | 38 | |
eric@48 | 39 | static inline int32_t rect_width (Rect *r) |
eric@42 | 40 | { |
eric@42 | 41 | return (r->max.x - r->min.x); |
eric@42 | 42 | } |
eric@42 | 43 | |
eric@48 | 44 | static inline int32_t rect_height (Rect *r) |
eric@42 | 45 | { |
eric@42 | 46 | return (r->max.y - r->min.y); |
eric@42 | 47 | } |
eric@42 | 48 | |
eric@47 | 49 | |
eric@94 | 50 | /* word_type should be the largest native type that can be handled |
eric@94 | 51 | efficiently, so it shouldn't be a 64-bit type on a processor that |
eric@94 | 52 | doesn't have native 64-bit operations. */ |
eric@48 | 53 | typedef uint32_t word_type; |
eric@47 | 54 | #define BITS_PER_WORD (8 * sizeof (word_type)) |
eric@94 | 55 | #define ALL_ONES (~ 0UL) |
eric@47 | 56 | |
eric@43 | 57 | |
eric@2 | 58 | typedef struct Bitmap |
eric@2 | 59 | { |
eric@43 | 60 | word_type *bits; |
eric@42 | 61 | Rect rect; |
eric@48 | 62 | uint32_t row_words; |
eric@2 | 63 | } Bitmap; |
eric@2 | 64 | |
eric@2 | 65 | |
eric@2 | 66 | #define TF_SRC 0xc |
eric@2 | 67 | #define TF_AND 0x8 |
eric@2 | 68 | #define TF_OR 0xe |
eric@2 | 69 | #define TF_XOR 0x6 |
eric@2 | 70 | |
eric@2 | 71 | |
eric@53 | 72 | void bitblt_init (void); |
eric@53 | 73 | |
eric@53 | 74 | |
eric@42 | 75 | Bitmap *create_bitmap (Rect *rect); |
eric@42 | 76 | void free_bitmap (Bitmap *bitmap); |
eric@3 | 77 | |
eric@48 | 78 | bool get_pixel (Bitmap *bitmap, Point coord); |
eric@48 | 79 | void set_pixel (Bitmap *bitmap, Point coord, bool value); |
eric@2 | 80 | |
eric@42 | 81 | |
eric@2 | 82 | Bitmap *bitblt (Bitmap *src_bitmap, |
eric@42 | 83 | Rect *src_rect, |
eric@2 | 84 | Bitmap *dest_bitmap, |
eric@42 | 85 | Point *dest_min, |
eric@43 | 86 | int tfn, |
eric@43 | 87 | int background); |
eric@42 | 88 | |
eric@42 | 89 | |
eric@42 | 90 | /* in-place transformations */ |
eric@42 | 91 | void flip_h (Bitmap *src); |
eric@42 | 92 | void flip_v (Bitmap *src); |
eric@42 | 93 | |
eric@42 | 94 | void rot_180 (Bitmap *src); /* combination of flip_h and flip_v */ |
eric@42 | 95 | |
eric@42 | 96 | /* "in-place" transformations - will allocate new memory and free old */ |
eric@42 | 97 | void transpose (Bitmap *src); |
eric@42 | 98 | |
eric@42 | 99 | void rot_90 (Bitmap *src); /* transpose + flip_h */ |
eric@42 | 100 | void rot_270 (Bitmap *src); /* transpose + flip_v */ |
eric@47 | 101 | |
eric@47 | 102 | |
eric@48 | 103 | void reverse_bits (uint8_t *p, int byte_count); |
eric@57 | 104 | |
eric@57 | 105 | |
eric@57 | 106 | /* |
eric@57 | 107 | * get_row_run_lengths counts the runs of 0 and 1 bits in row |
eric@57 | 108 | * y of a bitmap, from min_x through max_x inclusive. The run lengths |
eric@57 | 109 | * will be stored in the run_length array. The first entry will be |
eric@57 | 110 | * the length of a zero run (which length may be zero, if the first |
eric@57 | 111 | * bit is a one). The next entry will the be the length of a run of |
eric@57 | 112 | * ones, and they will alternate from there, with even entries representing |
eric@57 | 113 | * runs of zeros, and odd entries representing runs of ones. |
eric@57 | 114 | * |
eric@57 | 115 | * max_runs should be set to the maximum number of run lengths that |
eric@57 | 116 | * can be stored in the run_length array. |
eric@57 | 117 | * |
eric@57 | 118 | * Returns the actual number of runs counted, or -max_runs if there |
eric@57 | 119 | * was not enough room in the array. |
eric@57 | 120 | */ |
eric@72 | 121 | |
eric@72 | 122 | typedef struct |
eric@72 | 123 | { |
eric@72 | 124 | bool value; |
eric@72 | 125 | int32_t left; |
eric@72 | 126 | uint32_t width; |
eric@72 | 127 | } run_t; |
eric@72 | 128 | |
eric@57 | 129 | int32_t get_row_run_lengths (Bitmap *src, |
eric@57 | 130 | int32_t y, |
eric@57 | 131 | int32_t min_x, int32_t max_x, |
eric@57 | 132 | int32_t max_runs, |
eric@72 | 133 | run_t *runs); |
eric@91 | 134 | |
eric@91 | 135 | |
eric@91 | 136 | void bitblt_write_g4 (Bitmap *bitmap, FILE *f); |