Fri, 21 Feb 2003 09:01:33 +0000
added image position and size arguments to pdf_write_g4_fax_image.
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@64 | 7 | * $Id: pdf_g4.c,v 1.4 2003/02/21 01:01:33 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@59 | 30 | #include <string.h> |
eric@59 | 31 | |
eric@59 | 32 | |
eric@62 | 33 | #include "bitblt.h" |
eric@60 | 34 | #include "pdf.h" |
eric@60 | 35 | #include "pdf_util.h" |
eric@60 | 36 | #include "pdf_prim.h" |
eric@60 | 37 | #include "pdf_private.h" |
eric@59 | 38 | |
eric@59 | 39 | |
eric@59 | 40 | struct pdf_g4_image |
eric@59 | 41 | { |
eric@64 | 42 | double width, height; |
eric@64 | 43 | double x, y; |
eric@59 | 44 | unsigned long Columns; |
eric@59 | 45 | unsigned long Rows; |
eric@59 | 46 | int BlackIs1; |
eric@62 | 47 | Bitmap *bitmap; |
eric@59 | 48 | char XObject_name [4]; |
eric@59 | 49 | }; |
eric@59 | 50 | |
eric@59 | 51 | |
eric@59 | 52 | char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref) |
eric@59 | 53 | { |
eric@59 | 54 | char XObject_name [4] = "Im "; |
eric@59 | 55 | |
eric@59 | 56 | XObject_name [2] = ++pdf_page->last_XObject_name; |
eric@59 | 57 | |
eric@59 | 58 | if (! pdf_page->XObject_dict) |
eric@59 | 59 | { |
eric@59 | 60 | pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 61 | pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict); |
eric@59 | 62 | } |
eric@59 | 63 | |
eric@59 | 64 | pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref); |
eric@59 | 65 | |
eric@59 | 66 | return (pdf_page->last_XObject_name); |
eric@59 | 67 | } |
eric@59 | 68 | |
eric@59 | 69 | |
eric@59 | 70 | void pdf_write_g4_content_callback (pdf_file_handle pdf_file, |
eric@59 | 71 | struct pdf_obj *stream, |
eric@59 | 72 | void *app_data) |
eric@59 | 73 | { |
eric@59 | 74 | struct pdf_g4_image *image = app_data; |
eric@59 | 75 | |
eric@59 | 76 | char str1 [100]; |
eric@59 | 77 | char *str2 = "/"; |
eric@64 | 78 | char *str3 = " Do Q\r\n"; |
eric@59 | 79 | |
eric@59 | 80 | /* width 0 0 height x y cm */ |
eric@64 | 81 | sprintf (str1, "q %g 0 0 %g %g %g cm\r\n", |
eric@64 | 82 | image->width, image->height, |
eric@64 | 83 | image->x, image->y); |
eric@59 | 84 | |
eric@59 | 85 | pdf_stream_write_data (pdf_file, stream, str1, strlen (str1)); |
eric@59 | 86 | pdf_stream_write_data (pdf_file, stream, str2, strlen (str2)); |
eric@59 | 87 | pdf_stream_write_data (pdf_file, stream, & image->XObject_name [0], |
eric@59 | 88 | strlen (& image->XObject_name [0])); |
eric@59 | 89 | pdf_stream_write_data (pdf_file, stream, str3, strlen (str3)); |
eric@59 | 90 | } |
eric@59 | 91 | |
eric@59 | 92 | |
eric@59 | 93 | void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file, |
eric@59 | 94 | struct pdf_obj *stream, |
eric@59 | 95 | void *app_data) |
eric@59 | 96 | { |
eric@59 | 97 | struct pdf_g4_image *image = app_data; |
eric@59 | 98 | |
eric@62 | 99 | #if 0 |
eric@59 | 100 | pdf_stream_write_data (pdf_file, stream, image->data, image->len); |
eric@59 | 101 | #else |
eric@59 | 102 | unsigned long row = 0; |
eric@62 | 103 | word_type *ref; |
eric@62 | 104 | word_type *raw; |
eric@59 | 105 | |
eric@59 | 106 | ref = NULL; |
eric@62 | 107 | raw = image->bitmap->bits; |
eric@59 | 108 | |
eric@59 | 109 | while (row < image->Rows) |
eric@59 | 110 | { |
eric@64 | 111 | pdf_stream_write_data (pdf_file, stream, (uint8_t *) raw, |
eric@62 | 112 | image->bitmap->row_words * sizeof (word_type)); |
eric@59 | 113 | |
eric@59 | 114 | row++; |
eric@59 | 115 | ref = raw; |
eric@62 | 116 | raw += image->bitmap->row_words; |
eric@59 | 117 | } |
eric@59 | 118 | /* $$$ generate and write EOFB code */ |
eric@59 | 119 | /* $$$ flush any remaining buffered bits */ |
eric@59 | 120 | #endif |
eric@59 | 121 | } |
eric@59 | 122 | |
eric@59 | 123 | |
eric@59 | 124 | void pdf_write_g4_fax_image (pdf_page_handle pdf_page, |
eric@64 | 125 | double x, |
eric@64 | 126 | double y, |
eric@64 | 127 | double width, |
eric@64 | 128 | double height, |
eric@62 | 129 | Bitmap *bitmap, |
eric@59 | 130 | int ImageMask, |
eric@62 | 131 | int BlackIs1) /* boolean, typ. false */ |
eric@59 | 132 | { |
eric@59 | 133 | struct pdf_g4_image *image; |
eric@59 | 134 | |
eric@59 | 135 | struct pdf_obj *stream; |
eric@59 | 136 | struct pdf_obj *stream_dict; |
eric@59 | 137 | struct pdf_obj *decode_parms; |
eric@59 | 138 | |
eric@59 | 139 | struct pdf_obj *content_stream; |
eric@59 | 140 | |
eric@59 | 141 | image = pdf_calloc (sizeof (struct pdf_g4_image)); |
eric@59 | 142 | |
eric@64 | 143 | image->width = width; |
eric@64 | 144 | image->height = height; |
eric@64 | 145 | image->x = x; |
eric@64 | 146 | image->y = y; |
eric@64 | 147 | |
eric@62 | 148 | image->bitmap = bitmap; |
eric@62 | 149 | image->Columns = bitmap->rect.max.x - bitmap->rect.min.x; |
eric@62 | 150 | image->Rows = bitmap->rect.max.y - bitmap->rect.min.y; |
eric@59 | 151 | image->BlackIs1 = BlackIs1; |
eric@59 | 152 | |
eric@59 | 153 | stream_dict = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 154 | |
eric@59 | 155 | stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 156 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 157 | stream_dict, |
eric@59 | 158 | & pdf_write_g4_fax_image_callback, |
eric@59 | 159 | image)); |
eric@59 | 160 | |
eric@59 | 161 | strcpy (& image->XObject_name [0], "Im "); |
eric@59 | 162 | image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); |
eric@59 | 163 | |
eric@59 | 164 | pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); |
eric@59 | 165 | pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); |
eric@59 | 166 | pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); |
eric@62 | 167 | pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns)); |
eric@62 | 168 | pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows)); |
eric@59 | 169 | pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1)); |
eric@59 | 170 | if (ImageMask) |
eric@59 | 171 | pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask)); |
eric@59 | 172 | else |
eric@59 | 173 | pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray")); |
eric@59 | 174 | |
eric@59 | 175 | decode_parms = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 176 | |
eric@59 | 177 | pdf_set_dict_entry (decode_parms, |
eric@59 | 178 | "K", |
eric@59 | 179 | pdf_new_integer (-1)); |
eric@59 | 180 | |
eric@59 | 181 | pdf_set_dict_entry (decode_parms, |
eric@59 | 182 | "Columns", |
eric@62 | 183 | pdf_new_integer (image->Columns)); |
eric@59 | 184 | |
eric@59 | 185 | pdf_set_dict_entry (decode_parms, |
eric@59 | 186 | "Rows", |
eric@62 | 187 | pdf_new_integer (image->Rows)); |
eric@59 | 188 | |
eric@59 | 189 | if (BlackIs1) |
eric@59 | 190 | pdf_set_dict_entry (decode_parms, |
eric@59 | 191 | "BlackIs1", |
eric@59 | 192 | pdf_new_bool (BlackIs1)); |
eric@59 | 193 | |
eric@59 | 194 | pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms); |
eric@59 | 195 | |
eric@59 | 196 | /* the following will write the stream, using our callback function to |
eric@59 | 197 | get the actual data */ |
eric@59 | 198 | pdf_write_ind_obj (pdf_page->pdf_file, stream); |
eric@59 | 199 | |
eric@59 | 200 | content_stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 201 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 202 | pdf_new_obj (PT_DICTIONARY), |
eric@59 | 203 | & pdf_write_g4_content_callback, |
eric@59 | 204 | image)); |
eric@59 | 205 | |
eric@59 | 206 | pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); |
eric@59 | 207 | |
eric@59 | 208 | pdf_write_ind_obj (pdf_page->pdf_file, content_stream); |
eric@59 | 209 | } |
eric@59 | 210 |