pdf_g4.c

Fri, 21 Feb 2003 08:49:11 +0000

author
eric
date
Fri, 21 Feb 2003 08:49:11 +0000
changeset 63
6eddf63aa517
parent 62
9bd354b83e16
child 64
151394412eba
permissions
-rw-r--r--

GPL.

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@62 7 * $Id: pdf_g4.c,v 1.3 2003/02/20 04:44:17 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@59 42 unsigned long Columns;
eric@59 43 unsigned long Rows;
eric@59 44 int BlackIs1;
eric@62 45 Bitmap *bitmap;
eric@59 46 char XObject_name [4];
eric@59 47 };
eric@59 48
eric@59 49
eric@59 50 char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref)
eric@59 51 {
eric@59 52 char XObject_name [4] = "Im ";
eric@59 53
eric@59 54 XObject_name [2] = ++pdf_page->last_XObject_name;
eric@59 55
eric@59 56 if (! pdf_page->XObject_dict)
eric@59 57 {
eric@59 58 pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY);
eric@59 59 pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict);
eric@59 60 }
eric@59 61
eric@59 62 pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref);
eric@59 63
eric@59 64 return (pdf_page->last_XObject_name);
eric@59 65 }
eric@59 66
eric@59 67
eric@59 68 void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
eric@59 69 struct pdf_obj *stream,
eric@59 70 void *app_data)
eric@59 71 {
eric@59 72 unsigned long width = (8.5 * 72); /* full width of page */
eric@59 73 unsigned long height = (11 * 72); /* full height of page */
eric@59 74 unsigned long x = 0; /* 0 is left edge */
eric@59 75 unsigned long y = 0; /* 0 is bottom edge */
eric@59 76 struct pdf_g4_image *image = app_data;
eric@59 77
eric@59 78 char str1 [100];
eric@59 79 char *str2 = "/";
eric@59 80 char *str3 = " Do\r\n";
eric@59 81
eric@59 82 /* width 0 0 height x y cm */
eric@59 83 sprintf (str1, "q %ld 0 0 %ld %ld %ld cm\r\n", width, height, x, 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@62 111 pdf_stream_write_data (pdf_file, stream, 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@62 125 Bitmap *bitmap,
eric@59 126 int ImageMask,
eric@62 127 int BlackIs1) /* boolean, typ. false */
eric@59 128 {
eric@59 129 struct pdf_g4_image *image;
eric@59 130
eric@59 131 struct pdf_obj *stream;
eric@59 132 struct pdf_obj *stream_dict;
eric@59 133 struct pdf_obj *decode_parms;
eric@59 134
eric@59 135 struct pdf_obj *content_stream;
eric@59 136
eric@59 137 image = pdf_calloc (sizeof (struct pdf_g4_image));
eric@59 138
eric@62 139 image->bitmap = bitmap;
eric@62 140 image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
eric@62 141 image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
eric@59 142 image->BlackIs1 = BlackIs1;
eric@59 143
eric@59 144 stream_dict = pdf_new_obj (PT_DICTIONARY);
eric@59 145
eric@59 146 stream = pdf_new_ind_ref (pdf_page->pdf_file,
eric@59 147 pdf_new_stream (pdf_page->pdf_file,
eric@59 148 stream_dict,
eric@59 149 & pdf_write_g4_fax_image_callback,
eric@59 150 image));
eric@59 151
eric@59 152 strcpy (& image->XObject_name [0], "Im ");
eric@59 153 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
eric@59 154
eric@59 155 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
eric@59 156 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
eric@59 157 pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
eric@62 158 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns));
eric@62 159 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows));
eric@59 160 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
eric@59 161 if (ImageMask)
eric@59 162 pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
eric@59 163 else
eric@59 164 pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
eric@59 165
eric@59 166 decode_parms = pdf_new_obj (PT_DICTIONARY);
eric@59 167
eric@59 168 pdf_set_dict_entry (decode_parms,
eric@59 169 "K",
eric@59 170 pdf_new_integer (-1));
eric@59 171
eric@59 172 pdf_set_dict_entry (decode_parms,
eric@59 173 "Columns",
eric@62 174 pdf_new_integer (image->Columns));
eric@59 175
eric@59 176 pdf_set_dict_entry (decode_parms,
eric@59 177 "Rows",
eric@62 178 pdf_new_integer (image->Rows));
eric@59 179
eric@59 180 if (BlackIs1)
eric@59 181 pdf_set_dict_entry (decode_parms,
eric@59 182 "BlackIs1",
eric@59 183 pdf_new_bool (BlackIs1));
eric@59 184
eric@59 185 pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
eric@59 186
eric@59 187 /* the following will write the stream, using our callback function to
eric@59 188 get the actual data */
eric@59 189 pdf_write_ind_obj (pdf_page->pdf_file, stream);
eric@59 190
eric@59 191 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
eric@59 192 pdf_new_stream (pdf_page->pdf_file,
eric@59 193 pdf_new_obj (PT_DICTIONARY),
eric@59 194 & pdf_write_g4_content_callback,
eric@59 195 image));
eric@59 196
eric@59 197 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
eric@59 198
eric@59 199 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
eric@59 200 }
eric@59 201