Thu, 20 Feb 2003 12:20:28 +0000
fix includes.
eric@59 | 1 | #include <stdio.h> |
eric@59 | 2 | #include <string.h> |
eric@59 | 3 | |
eric@59 | 4 | |
eric@60 | 5 | #include "pdf.h" |
eric@60 | 6 | #include "pdf_util.h" |
eric@60 | 7 | #include "pdf_prim.h" |
eric@60 | 8 | #include "pdf_private.h" |
eric@59 | 9 | |
eric@59 | 10 | |
eric@59 | 11 | struct pdf_g4_image |
eric@59 | 12 | { |
eric@59 | 13 | unsigned long Columns; |
eric@59 | 14 | unsigned long Rows; |
eric@59 | 15 | unsigned long rowbytes; |
eric@59 | 16 | int BlackIs1; |
eric@59 | 17 | unsigned char *data; |
eric@59 | 18 | unsigned long len; |
eric@59 | 19 | char XObject_name [4]; |
eric@59 | 20 | }; |
eric@59 | 21 | |
eric@59 | 22 | |
eric@59 | 23 | char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref) |
eric@59 | 24 | { |
eric@59 | 25 | char XObject_name [4] = "Im "; |
eric@59 | 26 | |
eric@59 | 27 | XObject_name [2] = ++pdf_page->last_XObject_name; |
eric@59 | 28 | |
eric@59 | 29 | if (! pdf_page->XObject_dict) |
eric@59 | 30 | { |
eric@59 | 31 | pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 32 | pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict); |
eric@59 | 33 | } |
eric@59 | 34 | |
eric@59 | 35 | pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref); |
eric@59 | 36 | |
eric@59 | 37 | return (pdf_page->last_XObject_name); |
eric@59 | 38 | } |
eric@59 | 39 | |
eric@59 | 40 | |
eric@59 | 41 | void pdf_write_g4_content_callback (pdf_file_handle pdf_file, |
eric@59 | 42 | struct pdf_obj *stream, |
eric@59 | 43 | void *app_data) |
eric@59 | 44 | { |
eric@59 | 45 | unsigned long width = (8.5 * 72); /* full width of page */ |
eric@59 | 46 | unsigned long height = (11 * 72); /* full height of page */ |
eric@59 | 47 | unsigned long x = 0; /* 0 is left edge */ |
eric@59 | 48 | unsigned long y = 0; /* 0 is bottom edge */ |
eric@59 | 49 | struct pdf_g4_image *image = app_data; |
eric@59 | 50 | |
eric@59 | 51 | char str1 [100]; |
eric@59 | 52 | char *str2 = "/"; |
eric@59 | 53 | char *str3 = " Do\r\n"; |
eric@59 | 54 | |
eric@59 | 55 | /* width 0 0 height x y cm */ |
eric@59 | 56 | sprintf (str1, "q %ld 0 0 %ld %ld %ld cm\r\n", width, height, x, y); |
eric@59 | 57 | |
eric@59 | 58 | pdf_stream_write_data (pdf_file, stream, str1, strlen (str1)); |
eric@59 | 59 | pdf_stream_write_data (pdf_file, stream, str2, strlen (str2)); |
eric@59 | 60 | pdf_stream_write_data (pdf_file, stream, & image->XObject_name [0], |
eric@59 | 61 | strlen (& image->XObject_name [0])); |
eric@59 | 62 | pdf_stream_write_data (pdf_file, stream, str3, strlen (str3)); |
eric@59 | 63 | } |
eric@59 | 64 | |
eric@59 | 65 | |
eric@59 | 66 | void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file, |
eric@59 | 67 | struct pdf_obj *stream, |
eric@59 | 68 | void *app_data) |
eric@59 | 69 | { |
eric@59 | 70 | struct pdf_g4_image *image = app_data; |
eric@59 | 71 | |
eric@59 | 72 | #if 1 |
eric@59 | 73 | pdf_stream_write_data (pdf_file, stream, image->data, image->len); |
eric@59 | 74 | #else |
eric@59 | 75 | unsigned long row = 0; |
eric@59 | 76 | unsigned char *ref; |
eric@59 | 77 | unsigned char *raw; |
eric@59 | 78 | |
eric@59 | 79 | ref = NULL; |
eric@59 | 80 | raw = image->data; |
eric@59 | 81 | |
eric@59 | 82 | while (row < image->Rows) |
eric@59 | 83 | { |
eric@59 | 84 | pdf_stream_write_data (pdf_file, stream, raw, image->rowbytes); |
eric@59 | 85 | |
eric@59 | 86 | row++; |
eric@59 | 87 | ref = raw; |
eric@59 | 88 | raw += image->rowbytes; |
eric@59 | 89 | } |
eric@59 | 90 | /* $$$ generate and write EOFB code */ |
eric@59 | 91 | /* $$$ flush any remaining buffered bits */ |
eric@59 | 92 | #endif |
eric@59 | 93 | } |
eric@59 | 94 | |
eric@59 | 95 | |
eric@59 | 96 | void pdf_write_g4_fax_image (pdf_page_handle pdf_page, |
eric@59 | 97 | unsigned long Columns, |
eric@59 | 98 | unsigned long Rows, |
eric@59 | 99 | unsigned long rowbytes, |
eric@59 | 100 | int ImageMask, |
eric@59 | 101 | int BlackIs1, /* boolean, typ. false */ |
eric@59 | 102 | unsigned char *data, |
eric@59 | 103 | unsigned long len) |
eric@59 | 104 | { |
eric@59 | 105 | struct pdf_g4_image *image; |
eric@59 | 106 | |
eric@59 | 107 | struct pdf_obj *stream; |
eric@59 | 108 | struct pdf_obj *stream_dict; |
eric@59 | 109 | struct pdf_obj *decode_parms; |
eric@59 | 110 | |
eric@59 | 111 | struct pdf_obj *content_stream; |
eric@59 | 112 | |
eric@59 | 113 | image = pdf_calloc (sizeof (struct pdf_g4_image)); |
eric@59 | 114 | |
eric@59 | 115 | image->Columns = Columns; |
eric@59 | 116 | image->Rows = Rows; |
eric@59 | 117 | image->rowbytes = rowbytes; |
eric@59 | 118 | image->BlackIs1 = BlackIs1; |
eric@59 | 119 | image->data = data; |
eric@59 | 120 | image->len = len; |
eric@59 | 121 | |
eric@59 | 122 | stream_dict = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 123 | |
eric@59 | 124 | stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 125 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 126 | stream_dict, |
eric@59 | 127 | & pdf_write_g4_fax_image_callback, |
eric@59 | 128 | image)); |
eric@59 | 129 | |
eric@59 | 130 | strcpy (& image->XObject_name [0], "Im "); |
eric@59 | 131 | image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); |
eric@59 | 132 | |
eric@59 | 133 | pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); |
eric@59 | 134 | pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); |
eric@59 | 135 | pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); |
eric@59 | 136 | pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (Columns)); |
eric@59 | 137 | pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (Rows)); |
eric@59 | 138 | pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1)); |
eric@59 | 139 | if (ImageMask) |
eric@59 | 140 | pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask)); |
eric@59 | 141 | else |
eric@59 | 142 | pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray")); |
eric@59 | 143 | |
eric@59 | 144 | decode_parms = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 145 | |
eric@59 | 146 | pdf_set_dict_entry (decode_parms, |
eric@59 | 147 | "K", |
eric@59 | 148 | pdf_new_integer (-1)); |
eric@59 | 149 | |
eric@59 | 150 | pdf_set_dict_entry (decode_parms, |
eric@59 | 151 | "Columns", |
eric@59 | 152 | pdf_new_integer (Columns)); |
eric@59 | 153 | |
eric@59 | 154 | pdf_set_dict_entry (decode_parms, |
eric@59 | 155 | "Rows", |
eric@59 | 156 | pdf_new_integer (Rows)); |
eric@59 | 157 | |
eric@59 | 158 | if (BlackIs1) |
eric@59 | 159 | pdf_set_dict_entry (decode_parms, |
eric@59 | 160 | "BlackIs1", |
eric@59 | 161 | pdf_new_bool (BlackIs1)); |
eric@59 | 162 | |
eric@59 | 163 | pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms); |
eric@59 | 164 | |
eric@59 | 165 | /* the following will write the stream, using our callback function to |
eric@59 | 166 | get the actual data */ |
eric@59 | 167 | pdf_write_ind_obj (pdf_page->pdf_file, stream); |
eric@59 | 168 | |
eric@59 | 169 | content_stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 170 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 171 | pdf_new_obj (PT_DICTIONARY), |
eric@59 | 172 | & pdf_write_g4_content_callback, |
eric@59 | 173 | image)); |
eric@59 | 174 | |
eric@59 | 175 | pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); |
eric@59 | 176 | |
eric@59 | 177 | pdf_write_ind_obj (pdf_page->pdf_file, content_stream); |
eric@59 | 178 | } |
eric@59 | 179 |