Thu, 20 Mar 2003 07:06:35 +0000
*** empty log message ***
eric@62 | 1 | /* |
eric@125 | 2 | * tumble: build a PDF file from image files |
eric@62 | 3 | * |
eric@62 | 4 | * PDF routines |
eric@125 | 5 | * $Id: pdf_g4.c,v 1.15 2003/03/13 00:57:05 eric Exp $ |
eric@78 | 6 | * Copyright 2003 Eric Smith <eric@brouhaha.com> |
eric@62 | 7 | * |
eric@62 | 8 | * This program is free software; you can redistribute it and/or modify |
eric@62 | 9 | * it under the terms of the GNU General Public License version 2 as |
eric@62 | 10 | * published by the Free Software Foundation. Note that permission is |
eric@62 | 11 | * not granted to redistribute this program under the terms of any |
eric@62 | 12 | * other version of the General Public License. |
eric@62 | 13 | * |
eric@62 | 14 | * This program is distributed in the hope that it will be useful, |
eric@62 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
eric@62 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eric@62 | 17 | * GNU General Public License for more details. |
eric@62 | 18 | * |
eric@62 | 19 | * You should have received a copy of the GNU General Public License |
eric@62 | 20 | * along with this program; if not, write to the Free Software |
eric@62 | 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA |
eric@62 | 22 | */ |
eric@62 | 23 | |
eric@62 | 24 | |
eric@62 | 25 | #include <stdbool.h> |
eric@62 | 26 | #include <stdint.h> |
eric@59 | 27 | #include <stdio.h> |
eric@67 | 28 | #include <stdlib.h> |
eric@59 | 29 | #include <string.h> |
eric@59 | 30 | |
eric@59 | 31 | |
eric@62 | 32 | #include "bitblt.h" |
eric@60 | 33 | #include "pdf.h" |
eric@60 | 34 | #include "pdf_util.h" |
eric@60 | 35 | #include "pdf_prim.h" |
eric@60 | 36 | #include "pdf_private.h" |
eric@59 | 37 | |
eric@59 | 38 | |
eric@59 | 39 | struct pdf_g4_image |
eric@59 | 40 | { |
eric@64 | 41 | double width, height; |
eric@64 | 42 | double x, y; |
eric@66 | 43 | double r, g, b; /* fill color, only for ImageMask */ |
eric@59 | 44 | unsigned long Columns; |
eric@59 | 45 | unsigned long Rows; |
eric@66 | 46 | bool ImageMask; |
eric@66 | 47 | bool BlackIs1; |
eric@62 | 48 | Bitmap *bitmap; |
eric@59 | 49 | char XObject_name [4]; |
eric@59 | 50 | }; |
eric@59 | 51 | |
eric@59 | 52 | |
eric@102 | 53 | static void pdf_write_g4_content_callback (pdf_file_handle pdf_file, |
eric@102 | 54 | struct pdf_obj *stream, |
eric@102 | 55 | void *app_data) |
eric@59 | 56 | { |
eric@59 | 57 | struct pdf_g4_image *image = app_data; |
eric@59 | 58 | |
eric@66 | 59 | /* transformation matrix is: width 0 0 height x y cm */ |
eric@66 | 60 | pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ", |
eric@66 | 61 | image->width, image->height, |
eric@66 | 62 | image->x, image->y); |
eric@66 | 63 | if (image->ImageMask) |
eric@66 | 64 | pdf_stream_printf (pdf_file, stream, "%g %g %g rg ", |
eric@66 | 65 | image->r, image->g, image->b); |
eric@59 | 66 | |
eric@66 | 67 | pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n", |
eric@66 | 68 | image->XObject_name); |
eric@59 | 69 | } |
eric@59 | 70 | |
eric@59 | 71 | |
eric@102 | 72 | static void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file, |
eric@102 | 73 | struct pdf_obj *stream, |
eric@102 | 74 | void *app_data) |
eric@59 | 75 | { |
eric@59 | 76 | struct pdf_g4_image *image = app_data; |
eric@59 | 77 | |
eric@91 | 78 | bitblt_write_g4 (image->bitmap, pdf_file->f); |
eric@122 | 79 | pdf_stream_printf (pdf_file, stream, "\r\n"); |
eric@59 | 80 | } |
eric@59 | 81 | |
eric@59 | 82 | |
eric@59 | 83 | void pdf_write_g4_fax_image (pdf_page_handle pdf_page, |
eric@64 | 84 | double x, |
eric@64 | 85 | double y, |
eric@64 | 86 | double width, |
eric@64 | 87 | double height, |
eric@62 | 88 | Bitmap *bitmap, |
eric@66 | 89 | bool ImageMask, |
eric@66 | 90 | double r, /* RGB fill color, only for ImageMask */ |
eric@66 | 91 | double g, |
eric@66 | 92 | double b, |
eric@66 | 93 | bool BlackIs1) /* boolean, typ. false */ |
eric@59 | 94 | { |
eric@59 | 95 | struct pdf_g4_image *image; |
eric@59 | 96 | |
eric@59 | 97 | struct pdf_obj *stream; |
eric@59 | 98 | struct pdf_obj *stream_dict; |
eric@59 | 99 | struct pdf_obj *decode_parms; |
eric@59 | 100 | |
eric@59 | 101 | struct pdf_obj *content_stream; |
eric@59 | 102 | |
eric@118 | 103 | pdf_add_array_elem_unique (pdf_page->procset, pdf_new_name ("ImageB")); |
eric@118 | 104 | |
eric@67 | 105 | image = pdf_calloc (1, sizeof (struct pdf_g4_image)); |
eric@59 | 106 | |
eric@64 | 107 | image->width = width; |
eric@64 | 108 | image->height = height; |
eric@64 | 109 | image->x = x; |
eric@64 | 110 | image->y = y; |
eric@66 | 111 | image->r = r; |
eric@66 | 112 | image->g = g; |
eric@66 | 113 | image->b = b; |
eric@64 | 114 | |
eric@62 | 115 | image->bitmap = bitmap; |
eric@62 | 116 | image->Columns = bitmap->rect.max.x - bitmap->rect.min.x; |
eric@62 | 117 | image->Rows = bitmap->rect.max.y - bitmap->rect.min.y; |
eric@66 | 118 | image->ImageMask = ImageMask; |
eric@59 | 119 | image->BlackIs1 = BlackIs1; |
eric@59 | 120 | |
eric@59 | 121 | stream_dict = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 122 | |
eric@59 | 123 | stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 124 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 125 | stream_dict, |
eric@59 | 126 | & pdf_write_g4_fax_image_callback, |
eric@59 | 127 | image)); |
eric@59 | 128 | |
eric@59 | 129 | strcpy (& image->XObject_name [0], "Im "); |
eric@59 | 130 | image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); |
eric@59 | 131 | |
eric@59 | 132 | pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); |
eric@59 | 133 | pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); |
eric@59 | 134 | pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); |
eric@62 | 135 | pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns)); |
eric@62 | 136 | pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows)); |
eric@59 | 137 | pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1)); |
eric@59 | 138 | if (ImageMask) |
eric@59 | 139 | pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask)); |
eric@59 | 140 | else |
eric@59 | 141 | pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray")); |
eric@59 | 142 | |
eric@59 | 143 | decode_parms = pdf_new_obj (PT_DICTIONARY); |
eric@59 | 144 | |
eric@59 | 145 | pdf_set_dict_entry (decode_parms, |
eric@59 | 146 | "K", |
eric@59 | 147 | pdf_new_integer (-1)); |
eric@59 | 148 | |
eric@59 | 149 | pdf_set_dict_entry (decode_parms, |
eric@59 | 150 | "Columns", |
eric@62 | 151 | pdf_new_integer (image->Columns)); |
eric@59 | 152 | |
eric@59 | 153 | pdf_set_dict_entry (decode_parms, |
eric@59 | 154 | "Rows", |
eric@62 | 155 | pdf_new_integer (image->Rows)); |
eric@59 | 156 | |
eric@59 | 157 | if (BlackIs1) |
eric@59 | 158 | pdf_set_dict_entry (decode_parms, |
eric@59 | 159 | "BlackIs1", |
eric@59 | 160 | pdf_new_bool (BlackIs1)); |
eric@59 | 161 | |
eric@59 | 162 | pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms); |
eric@59 | 163 | |
eric@59 | 164 | /* the following will write the stream, using our callback function to |
eric@59 | 165 | get the actual data */ |
eric@59 | 166 | pdf_write_ind_obj (pdf_page->pdf_file, stream); |
eric@59 | 167 | |
eric@59 | 168 | content_stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@59 | 169 | pdf_new_stream (pdf_page->pdf_file, |
eric@59 | 170 | pdf_new_obj (PT_DICTIONARY), |
eric@59 | 171 | & pdf_write_g4_content_callback, |
eric@59 | 172 | image)); |
eric@59 | 173 | |
eric@59 | 174 | pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); |
eric@59 | 175 | |
eric@59 | 176 | pdf_write_ind_obj (pdf_page->pdf_file, content_stream); |
eric@59 | 177 | } |
eric@59 | 178 |