Fri, 07 Mar 2003 11:02:31 +0000
added pdf_compare_obj().
eric@62 | 1 | /* |
eric@62 | 2 | * t2p: Create a PDF file from the contents of one or more TIFF |
eric@62 | 3 | * bilevel image files. The images in the resulting PDF file |
eric@62 | 4 | * will be compressed using ITU-T T.6 (G4) fax encoding. |
eric@62 | 5 | * |
eric@62 | 6 | * PDF routines |
eric@78 | 7 | * $Id: bitblt_g4.c,v 1.8 2003/03/05 12:44:33 eric Exp $ |
eric@78 | 8 | * Copyright 2003 Eric Smith <eric@brouhaha.com> |
eric@62 | 9 | * |
eric@62 | 10 | * This program is free software; you can redistribute it and/or modify |
eric@62 | 11 | * it under the terms of the GNU General Public License version 2 as |
eric@62 | 12 | * published by the Free Software Foundation. Note that permission is |
eric@62 | 13 | * not granted to redistribute this program under the terms of any |
eric@62 | 14 | * other version of the General Public License. |
eric@62 | 15 | * |
eric@62 | 16 | * This program is distributed in the hope that it will be useful, |
eric@62 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
eric@62 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eric@62 | 19 | * GNU General Public License for more details. |
eric@62 | 20 | * |
eric@62 | 21 | * You should have received a copy of the GNU General Public License |
eric@62 | 22 | * along with this program; if not, write to the Free Software |
eric@62 | 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA |
eric@62 | 24 | */ |
eric@62 | 25 | |
eric@62 | 26 | |
eric@62 | 27 | #include <stdbool.h> |
eric@62 | 28 | #include <stdint.h> |
eric@59 | 29 | #include <stdio.h> |
eric@67 | 30 | #include <stdlib.h> |
eric@59 | 31 | #include <string.h> |
eric@59 | 32 | |
eric@59 | 33 | |
eric@62 | 34 | #include "bitblt.h" |
eric@60 | 35 | #include "pdf.h" |
eric@60 | 36 | #include "pdf_util.h" |
eric@60 | 37 | #include "pdf_prim.h" |
eric@60 | 38 | #include "pdf_private.h" |
eric@59 | 39 | |
eric@59 | 40 | |
eric@73 | 41 | #include "pdf_g4_tables.h" |
eric@73 | 42 | |
eric@73 | 43 | |
eric@67 | 44 | #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) |
eric@67 | 45 | |
eric@67 | 46 | |
eric@59 | 47 | struct pdf_g4_image |
eric@59 | 48 | { |
eric@64 | 49 | double width, height; |
eric@64 | 50 | double x, y; |
eric@66 | 51 | double r, g, b; /* fill color, only for ImageMask */ |
eric@59 | 52 | unsigned long Columns; |
eric@59 | 53 | unsigned long Rows; |
eric@66 | 54 | bool ImageMask; |
eric@66 | 55 | bool BlackIs1; |
eric@62 | 56 | Bitmap *bitmap; |
eric@59 | 57 | char XObject_name [4]; |
eric@59 | 58 | }; |
eric@59 | 59 | |
eric@59 | 60 | |
eric@59 | 61 | char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref) |
eric@59 | 62 | { |
eric@59 | 63 | char XObject_name [4] = "Im "; |
eric@59 | 64 | |
eric@59 | 65 | XObject_name [2] = ++pdf_page->last_XObject_name; |
eric@59 | 66 | |
eric@59 | 67 | if (! pdf_page->XObject_dict) |
eric@59 | 68 | { |
eric@59 | 69 | pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 70 | pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict); |
eric@59 | 71 | } |
eric@59 | 72 | |
eric@59 | 73 | pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref); |
eric@59 | 74 | |
eric@59 | 75 | return (pdf_page->last_XObject_name); |
eric@59 | 76 | } |
eric@59 | 77 | |
eric@59 | 78 | |
eric@59 | 79 | void pdf_write_g4_content_callback (pdf_file_handle pdf_file, |
eric@59 | 80 | struct pdf_obj *stream, |
eric@59 | 81 | void *app_data) |
eric@59 | 82 | { |
eric@59 | 83 | struct pdf_g4_image *image = app_data; |
eric@59 | 84 | |
eric@66 | 85 | /* transformation matrix is: width 0 0 height x y cm */ |
eric@66 | 86 | pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ", |
eric@66 | 87 | image->width, image->height, |
eric@66 | 88 | image->x, image->y); |
eric@66 | 89 | if (image->ImageMask) |
eric@66 | 90 | pdf_stream_printf (pdf_file, stream, "%g %g %g rg ", |
eric@66 | 91 | image->r, image->g, image->b); |
eric@59 | 92 | |
eric@66 | 93 | pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n", |
eric@66 | 94 | image->XObject_name); |
eric@59 | 95 | } |
eric@59 | 96 | |
eric@59 | 97 | |
eric@73 | 98 | static void pdf_g4_encode_horizontal_run (pdf_file_handle pdf_file, |
eric@73 | 99 | struct pdf_obj *stream, |
eric@73 | 100 | bool black, |
eric@73 | 101 | uint32_t run_length) |
eric@73 | 102 | { |
eric@73 | 103 | uint32_t i; |
eric@73 | 104 | |
eric@73 | 105 | while (run_length >= 2560) |
eric@73 | 106 | { |
eric@73 | 107 | pdf_stream_write_bits (pdf_file, stream, 12, 0x01f); |
eric@73 | 108 | run_length -= 2560; |
eric@73 | 109 | } |
eric@73 | 110 | |
eric@73 | 111 | if (run_length >= 1792) |
eric@73 | 112 | { |
eric@73 | 113 | i = (run_length - 1792) >> 6; |
eric@73 | 114 | pdf_stream_write_bits (pdf_file, stream, |
eric@73 | 115 | g4_long_makeup_code [i].count, |
eric@73 | 116 | g4_long_makeup_code [i].bits); |
eric@73 | 117 | run_length -= (1792 + (i << 6)); |
eric@73 | 118 | } |
eric@73 | 119 | else if (run_length >= 64) |
eric@73 | 120 | { |
eric@73 | 121 | i = (run_length >> 6) - 1; |
eric@73 | 122 | pdf_stream_write_bits (pdf_file, stream, |
eric@73 | 123 | g4_makeup_code [black] [i].count, |
eric@73 | 124 | g4_makeup_code [black] [i].bits); |
eric@73 | 125 | run_length -= (i + 1) << 6; |
eric@73 | 126 | } |
eric@73 | 127 | |
eric@73 | 128 | pdf_stream_write_bits (pdf_file, stream, |
eric@73 | 129 | g4_h_code [black] [run_length].count, |
eric@73 | 130 | g4_h_code [black] [run_length].bits); |
eric@73 | 131 | } |
eric@73 | 132 | |
eric@73 | 133 | |
eric@73 | 134 | uint32_t find_transition (uint8_t *data, |
eric@73 | 135 | uint32_t pos, |
eric@73 | 136 | uint32_t width) |
eric@73 | 137 | { |
eric@73 | 138 | if (! data) |
eric@73 | 139 | return (width); |
eric@73 | 140 | return (0); /* $$$ */ |
eric@73 | 141 | } |
eric@73 | 142 | |
eric@73 | 143 | |
eric@73 | 144 | static void pdf_g4_encode_row (pdf_file_handle pdf_file, |
eric@73 | 145 | struct pdf_obj *stream, |
eric@73 | 146 | uint32_t width, |
eric@73 | 147 | uint8_t *ref, |
eric@73 | 148 | uint8_t *row) |
eric@73 | 149 | { |
eric@73 | 150 | int a0, a1, a2; |
eric@73 | 151 | int b1, b2; |
eric@73 | 152 | |
eric@73 | 153 | a0 = -1; |
eric@73 | 154 | |
eric@73 | 155 | while (a0 < width) |
eric@73 | 156 | { |
eric@73 | 157 | /* find a1, a2 */ |
eric@73 | 158 | a1 = find_transition (row, a0, width); |
eric@73 | 159 | a2 = find_transition (row, a1, width); |
eric@73 | 160 | |
eric@73 | 161 | /* find b1, b2 */ |
eric@73 | 162 | b1 = find_transition (ref, a0, width); |
eric@73 | 163 | if (0) /* $$$ b1 color = a0 color */ |
eric@73 | 164 | b1 = find_transition (ref, b1, width); |
eric@73 | 165 | b2 = find_transition (ref, b2, width); |
eric@73 | 166 | |
eric@73 | 167 | if (b2 < a1) |
eric@73 | 168 | { |
eric@73 | 169 | /* pass mode - 0001 */ |
eric@73 | 170 | pdf_stream_write_bits (pdf_file, stream, 4, 0x1); |
eric@73 | 171 | a0 = b2; |
eric@73 | 172 | } |
eric@73 | 173 | else if (abs (a1 - b1) <= 3) |
eric@73 | 174 | { |
eric@73 | 175 | /* vertical mode */ |
eric@73 | 176 | pdf_stream_write_bits (pdf_file, stream, |
eric@73 | 177 | g4_vert_code [3 + a1 - b1].count, |
eric@73 | 178 | g4_vert_code [3 + a1 - b1].bits); |
eric@73 | 179 | a0 = a1; |
eric@73 | 180 | } |
eric@73 | 181 | else |
eric@73 | 182 | { |
eric@73 | 183 | /* horizontal mode - 001 */ |
eric@73 | 184 | pdf_stream_write_bits (pdf_file, stream, 3, 0x1); |
eric@73 | 185 | pdf_g4_encode_horizontal_run (pdf_file, stream, |
eric@73 | 186 | 0 /* $$$ color (a0) */, a1 - a0); |
eric@73 | 187 | pdf_g4_encode_horizontal_run (pdf_file, stream, |
eric@73 | 188 | 1 /* $$$ color (a1) */, a2 - a1); |
eric@73 | 189 | a0 = a2; |
eric@73 | 190 | } |
eric@73 | 191 | } |
eric@73 | 192 | } |
eric@73 | 193 | |
eric@73 | 194 | |
eric@59 | 195 | void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file, |
eric@59 | 196 | struct pdf_obj *stream, |
eric@59 | 197 | void *app_data) |
eric@59 | 198 | { |
eric@59 | 199 | struct pdf_g4_image *image = app_data; |
eric@59 | 200 | |
eric@67 | 201 | uint32_t row; |
eric@67 | 202 | |
eric@73 | 203 | word_type *ref_line = NULL; /* reference (previous) row */ |
eric@73 | 204 | word_type *line = image->bitmap->bits; |
eric@59 | 205 | |
eric@67 | 206 | for (row = image->bitmap->rect.min.y; |
eric@67 | 207 | row < image->bitmap->rect.max.y; |
eric@67 | 208 | row++) |
eric@59 | 209 | { |
eric@73 | 210 | pdf_g4_encode_row (pdf_file, stream, image->Columns, |
eric@73 | 211 | (uint8_t *) ref_line, |
eric@73 | 212 | (uint8_t *) line); |
eric@73 | 213 | ref_line = line; |
eric@73 | 214 | line += image->bitmap->row_words; |
eric@59 | 215 | } |
eric@67 | 216 | |
eric@67 | 217 | |
eric@73 | 218 | /* write EOFB code */ |
eric@67 | 219 | pdf_stream_write_bits (pdf_file, stream, 24, 0x001001); |
eric@67 | 220 | |
eric@67 | 221 | pdf_stream_flush_bits (pdf_file, stream); |
eric@59 | 222 | } |
eric@59 | 223 | |
eric@59 | 224 | |
eric@59 | 225 | void pdf_write_g4_fax_image (pdf_page_handle pdf_page, |
eric@64 | 226 | double x, |
eric@64 | 227 | double y, |
eric@64 | 228 | double width, |
eric@64 | 229 | double height, |
eric@62 | 230 | Bitmap *bitmap, |
eric@66 | 231 | bool ImageMask, |
eric@66 | 232 | double r, /* RGB fill color, only for ImageMask */ |
eric@66 | 233 | double g, |
eric@66 | 234 | double b, |
eric@66 | 235 | bool BlackIs1) /* boolean, typ. false */ |
eric@59 | 236 | { |
eric@59 | 237 | struct pdf_g4_image *image; |
eric@59 | 238 | |
eric@59 | 239 | struct pdf_obj *stream; |
eric@59 | 240 | struct pdf_obj *stream_dict; |
eric@59 | 241 | struct pdf_obj *decode_parms; |
eric@59 | 242 | |
eric@59 | 243 | struct pdf_obj *content_stream; |
eric@59 | 244 | |
eric@67 | 245 | image = pdf_calloc (1, sizeof (struct pdf_g4_image)); |
eric@59 | 246 | |
eric@64 | 247 | image->width = width; |
eric@64 | 248 | image->height = height; |
eric@64 | 249 | image->x = x; |
eric@64 | 250 | image->y = y; |
eric@66 | 251 | image->r = r; |
eric@66 | 252 | image->g = g; |
eric@66 | 253 | image->b = b; |
eric@64 | 254 | |
eric@62 | 255 | image->bitmap = bitmap; |
eric@62 | 256 | image->Columns = bitmap->rect.max.x - bitmap->rect.min.x; |
eric@62 | 257 | image->Rows = bitmap->rect.max.y - bitmap->rect.min.y; |
eric@66 | 258 | image->ImageMask = ImageMask; |
eric@59 | 259 | image->BlackIs1 = BlackIs1; |
eric@59 | 260 | |
eric@59 | 261 | stream_dict = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 262 | |
eric@59 | 263 | stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 264 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 265 | stream_dict, |
eric@59 | 266 | & pdf_write_g4_fax_image_callback, |
eric@59 | 267 | image)); |
eric@59 | 268 | |
eric@59 | 269 | strcpy (& image->XObject_name [0], "Im "); |
eric@59 | 270 | image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); |
eric@59 | 271 | |
eric@59 | 272 | pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); |
eric@59 | 273 | pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); |
eric@59 | 274 | pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); |
eric@62 | 275 | pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns)); |
eric@62 | 276 | pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows)); |
eric@59 | 277 | pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1)); |
eric@59 | 278 | if (ImageMask) |
eric@59 | 279 | pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask)); |
eric@59 | 280 | else |
eric@59 | 281 | pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray")); |
eric@59 | 282 | |
eric@59 | 283 | decode_parms = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 284 | |
eric@59 | 285 | pdf_set_dict_entry (decode_parms, |
eric@59 | 286 | "K", |
eric@59 | 287 | pdf_new_integer (-1)); |
eric@59 | 288 | |
eric@59 | 289 | pdf_set_dict_entry (decode_parms, |
eric@59 | 290 | "Columns", |
eric@62 | 291 | pdf_new_integer (image->Columns)); |
eric@59 | 292 | |
eric@59 | 293 | pdf_set_dict_entry (decode_parms, |
eric@59 | 294 | "Rows", |
eric@62 | 295 | pdf_new_integer (image->Rows)); |
eric@59 | 296 | |
eric@59 | 297 | if (BlackIs1) |
eric@59 | 298 | pdf_set_dict_entry (decode_parms, |
eric@59 | 299 | "BlackIs1", |
eric@59 | 300 | pdf_new_bool (BlackIs1)); |
eric@59 | 301 | |
eric@59 | 302 | pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms); |
eric@59 | 303 | |
eric@59 | 304 | /* the following will write the stream, using our callback function to |
eric@59 | 305 | get the actual data */ |
eric@59 | 306 | pdf_write_ind_obj (pdf_page->pdf_file, stream); |
eric@59 | 307 | |
eric@59 | 308 | content_stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 309 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 310 | pdf_new_obj (PT_DICTIONARY), |
eric@59 | 311 | & pdf_write_g4_content_callback, |
eric@59 | 312 | image)); |
eric@59 | 313 | |
eric@59 | 314 | pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); |
eric@59 | 315 | |
eric@59 | 316 | pdf_write_ind_obj (pdf_page->pdf_file, content_stream); |
eric@59 | 317 | } |
eric@59 | 318 |