Sat, 22 Feb 2003 10:02:06 +0000
Generate G4 tables from strings of ones and zeros.
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@67 | 7 | * $Id: pdf_g4.c,v 1.6 2003/02/21 02:49:11 eric Exp $ |
eric@62 | 8 | * Copyright 2001, 2002, 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@67 | 41 | #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) |
eric@67 | 42 | |
eric@67 | 43 | |
eric@59 | 44 | struct pdf_g4_image |
eric@59 | 45 | { |
eric@64 | 46 | double width, height; |
eric@64 | 47 | double x, y; |
eric@66 | 48 | double r, g, b; /* fill color, only for ImageMask */ |
eric@59 | 49 | unsigned long Columns; |
eric@59 | 50 | unsigned long Rows; |
eric@66 | 51 | bool ImageMask; |
eric@66 | 52 | bool BlackIs1; |
eric@62 | 53 | Bitmap *bitmap; |
eric@59 | 54 | char XObject_name [4]; |
eric@59 | 55 | }; |
eric@59 | 56 | |
eric@59 | 57 | |
eric@59 | 58 | char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref) |
eric@59 | 59 | { |
eric@59 | 60 | char XObject_name [4] = "Im "; |
eric@59 | 61 | |
eric@59 | 62 | XObject_name [2] = ++pdf_page->last_XObject_name; |
eric@59 | 63 | |
eric@59 | 64 | if (! pdf_page->XObject_dict) |
eric@59 | 65 | { |
eric@59 | 66 | pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 67 | pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict); |
eric@59 | 68 | } |
eric@59 | 69 | |
eric@59 | 70 | pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref); |
eric@59 | 71 | |
eric@59 | 72 | return (pdf_page->last_XObject_name); |
eric@59 | 73 | } |
eric@59 | 74 | |
eric@59 | 75 | |
eric@59 | 76 | void pdf_write_g4_content_callback (pdf_file_handle pdf_file, |
eric@59 | 77 | struct pdf_obj *stream, |
eric@59 | 78 | void *app_data) |
eric@59 | 79 | { |
eric@59 | 80 | struct pdf_g4_image *image = app_data; |
eric@59 | 81 | |
eric@66 | 82 | /* transformation matrix is: width 0 0 height x y cm */ |
eric@66 | 83 | pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ", |
eric@66 | 84 | image->width, image->height, |
eric@66 | 85 | image->x, image->y); |
eric@66 | 86 | if (image->ImageMask) |
eric@66 | 87 | pdf_stream_printf (pdf_file, stream, "%g %g %g rg ", |
eric@66 | 88 | image->r, image->g, image->b); |
eric@59 | 89 | |
eric@66 | 90 | pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n", |
eric@66 | 91 | image->XObject_name); |
eric@59 | 92 | } |
eric@59 | 93 | |
eric@59 | 94 | |
eric@59 | 95 | void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file, |
eric@59 | 96 | struct pdf_obj *stream, |
eric@59 | 97 | void *app_data) |
eric@59 | 98 | { |
eric@59 | 99 | struct pdf_g4_image *image = app_data; |
eric@59 | 100 | |
eric@67 | 101 | uint32_t row; |
eric@67 | 102 | |
eric@67 | 103 | /* reference (previous) row */ |
eric@67 | 104 | uint32_t *ref_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); |
eric@67 | 105 | uint32_t ref_run_count; |
eric@59 | 106 | |
eric@67 | 107 | /* row being converted */ |
eric@67 | 108 | uint32_t *row_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); |
eric@67 | 109 | uint32_t row_run_count; |
eric@67 | 110 | |
eric@67 | 111 | /* initialize reference row - all white */ |
eric@67 | 112 | ref_runs [0] = image->Columns; |
eric@67 | 113 | ref_run_count = 0; |
eric@59 | 114 | |
eric@67 | 115 | for (row = image->bitmap->rect.min.y; |
eric@67 | 116 | row < image->bitmap->rect.max.y; |
eric@67 | 117 | row++) |
eric@59 | 118 | { |
eric@67 | 119 | row_run_count = get_row_run_lengths (image->bitmap, |
eric@67 | 120 | row, |
eric@67 | 121 | image->bitmap->rect.min.x, |
eric@67 | 122 | image->bitmap->rect.max.x - 1, |
eric@67 | 123 | image->Columns, /* max_runs */ |
eric@67 | 124 | row_runs); |
eric@67 | 125 | pdf_assert (row_run_count > 0); |
eric@67 | 126 | |
eric@67 | 127 | /* $$$ G4 encode the runs here */ |
eric@59 | 128 | |
eric@67 | 129 | /* pdf_stream_write_data (pdf_file, stream, image->data, image->len); */ |
eric@67 | 130 | |
eric@67 | 131 | SWAP (uint32_t *, row_runs, ref_runs); |
eric@67 | 132 | ref_run_count = row_run_count; |
eric@59 | 133 | } |
eric@67 | 134 | |
eric@67 | 135 | |
eric@59 | 136 | /* $$$ generate and write EOFB code */ |
eric@67 | 137 | pdf_stream_write_bits (pdf_file, stream, 24, 0x001001); |
eric@67 | 138 | |
eric@67 | 139 | pdf_stream_flush_bits (pdf_file, stream); |
eric@67 | 140 | |
eric@67 | 141 | free (ref_runs); |
eric@67 | 142 | free (row_runs); |
eric@59 | 143 | } |
eric@59 | 144 | |
eric@59 | 145 | |
eric@59 | 146 | void pdf_write_g4_fax_image (pdf_page_handle pdf_page, |
eric@64 | 147 | double x, |
eric@64 | 148 | double y, |
eric@64 | 149 | double width, |
eric@64 | 150 | double height, |
eric@62 | 151 | Bitmap *bitmap, |
eric@66 | 152 | bool ImageMask, |
eric@66 | 153 | double r, /* RGB fill color, only for ImageMask */ |
eric@66 | 154 | double g, |
eric@66 | 155 | double b, |
eric@66 | 156 | bool BlackIs1) /* boolean, typ. false */ |
eric@59 | 157 | { |
eric@59 | 158 | struct pdf_g4_image *image; |
eric@59 | 159 | |
eric@59 | 160 | struct pdf_obj *stream; |
eric@59 | 161 | struct pdf_obj *stream_dict; |
eric@59 | 162 | struct pdf_obj *decode_parms; |
eric@59 | 163 | |
eric@59 | 164 | struct pdf_obj *content_stream; |
eric@59 | 165 | |
eric@67 | 166 | image = pdf_calloc (1, sizeof (struct pdf_g4_image)); |
eric@59 | 167 | |
eric@64 | 168 | image->width = width; |
eric@64 | 169 | image->height = height; |
eric@64 | 170 | image->x = x; |
eric@64 | 171 | image->y = y; |
eric@66 | 172 | image->r = r; |
eric@66 | 173 | image->g = g; |
eric@66 | 174 | image->b = b; |
eric@64 | 175 | |
eric@62 | 176 | image->bitmap = bitmap; |
eric@62 | 177 | image->Columns = bitmap->rect.max.x - bitmap->rect.min.x; |
eric@62 | 178 | image->Rows = bitmap->rect.max.y - bitmap->rect.min.y; |
eric@66 | 179 | image->ImageMask = ImageMask; |
eric@59 | 180 | image->BlackIs1 = BlackIs1; |
eric@59 | 181 | |
eric@59 | 182 | stream_dict = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 183 | |
eric@59 | 184 | stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 185 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 186 | stream_dict, |
eric@59 | 187 | & pdf_write_g4_fax_image_callback, |
eric@59 | 188 | image)); |
eric@59 | 189 | |
eric@59 | 190 | strcpy (& image->XObject_name [0], "Im "); |
eric@59 | 191 | image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); |
eric@59 | 192 | |
eric@59 | 193 | pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); |
eric@59 | 194 | pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); |
eric@59 | 195 | pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); |
eric@62 | 196 | pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns)); |
eric@62 | 197 | pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows)); |
eric@59 | 198 | pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1)); |
eric@59 | 199 | if (ImageMask) |
eric@59 | 200 | pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask)); |
eric@59 | 201 | else |
eric@59 | 202 | pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray")); |
eric@59 | 203 | |
eric@59 | 204 | decode_parms = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 205 | |
eric@59 | 206 | pdf_set_dict_entry (decode_parms, |
eric@59 | 207 | "K", |
eric@59 | 208 | pdf_new_integer (-1)); |
eric@59 | 209 | |
eric@59 | 210 | pdf_set_dict_entry (decode_parms, |
eric@59 | 211 | "Columns", |
eric@62 | 212 | pdf_new_integer (image->Columns)); |
eric@59 | 213 | |
eric@59 | 214 | pdf_set_dict_entry (decode_parms, |
eric@59 | 215 | "Rows", |
eric@62 | 216 | pdf_new_integer (image->Rows)); |
eric@59 | 217 | |
eric@59 | 218 | if (BlackIs1) |
eric@59 | 219 | pdf_set_dict_entry (decode_parms, |
eric@59 | 220 | "BlackIs1", |
eric@59 | 221 | pdf_new_bool (BlackIs1)); |
eric@59 | 222 | |
eric@59 | 223 | pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms); |
eric@59 | 224 | |
eric@59 | 225 | /* the following will write the stream, using our callback function to |
eric@59 | 226 | get the actual data */ |
eric@59 | 227 | pdf_write_ind_obj (pdf_page->pdf_file, stream); |
eric@59 | 228 | |
eric@59 | 229 | content_stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 230 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 231 | pdf_new_obj (PT_DICTIONARY), |
eric@59 | 232 | & pdf_write_g4_content_callback, |
eric@59 | 233 | image)); |
eric@59 | 234 | |
eric@59 | 235 | pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); |
eric@59 | 236 | |
eric@59 | 237 | pdf_write_ind_obj (pdf_page->pdf_file, content_stream); |
eric@59 | 238 | } |
eric@59 | 239 |