Mon, 14 Dec 2009 16:18:21 +0000
remove erroneous 0.33-philpem1 tag
1 /*
2 * tumble: build a PDF file from image files
3 *
4 * PDF routines
5 * Copyright 2004 Daniel Gloeckner
6 *
7 * Derived from pdf_jpeg.c written 2003 by Eric Smith <eric at brouhaha.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation. Note that permission is
12 * not granted to redistribute this program under the terms of any
13 * other version of the General Public License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
23 */
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
33 #include "bitblt.h"
34 #include "pdf.h"
35 #include "pdf_util.h"
36 #include "pdf_prim.h"
37 #include "pdf_private.h"
40 struct pdf_jp2_image
41 {
42 double width, height;
43 double x, y;
44 uint32_t width_samples, height_samples;
45 FILE *f;
46 char XObject_name [4];
47 };
50 static void pdf_write_jp2_content_callback (pdf_file_handle pdf_file,
51 struct pdf_obj *stream,
52 void *app_data)
53 {
54 struct pdf_jp2_image *image = app_data;
56 /* transformation matrix is: width 0 0 height x y cm */
57 pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
58 image->width, image->height,
59 image->x, image->y);
60 pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
61 image->XObject_name);
62 }
65 #define JPEG_BUFFER_SIZE 8192
67 static void pdf_write_jp2_image_callback (pdf_file_handle pdf_file,
68 struct pdf_obj *stream,
69 void *app_data)
70 {
71 struct pdf_jp2_image *image = app_data;
72 int rlen, wlen;
73 uint8_t *wp;
74 uint8_t buffer [8192];
76 while (! feof (image->f))
77 {
78 rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, image->f);
79 wp = & buffer [0];
80 while (rlen)
81 {
82 wlen = fwrite (wp, 1, rlen, pdf_file->f);
83 if (feof (pdf_file->f))
84 pdf_fatal ("unexpected EOF on output file\n");
85 if (ferror (pdf_file->f))
86 pdf_fatal ("error on output file\n");
87 rlen -= wlen;
88 wp += wlen;
89 }
90 if (ferror (image->f))
91 pdf_fatal ("error on input file\n");
92 }
93 }
96 void pdf_write_jp2_image (pdf_page_handle pdf_page,
97 double x,
98 double y,
99 double width,
100 double height,
101 uint32_t width_samples,
102 uint32_t height_samples,
103 FILE *f)
104 {
105 struct pdf_jp2_image *image;
107 struct pdf_obj *stream;
108 struct pdf_obj *stream_dict;
110 struct pdf_obj *content_stream;
112 image = pdf_calloc (1, sizeof (struct pdf_jp2_image));
114 image->width = width;
115 image->height = height;
116 image->x = x;
117 image->y = y;
119 image->f = f;
121 image->width_samples = width_samples;
122 image->height_samples = height_samples;
124 stream_dict = pdf_new_obj (PT_DICTIONARY);
126 stream = pdf_new_ind_ref (pdf_page->pdf_file,
127 pdf_new_stream (pdf_page->pdf_file,
128 stream_dict,
129 & pdf_write_jp2_image_callback,
130 image));
132 strcpy (& image->XObject_name [0], "Im ");
133 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
135 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
136 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
137 // Name is required in PDF 1.0 but obsoleted in later PDF versions
138 // pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
139 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->width_samples));
140 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->height_samples));
142 // not required for JPXDecode, but
143 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8));
145 pdf_stream_add_filter (stream, "JPXDecode", NULL);
147 /* the following will write the stream, using our callback function to
148 get the actual data */
149 pdf_write_ind_obj (pdf_page->pdf_file, stream);
151 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
152 pdf_new_stream (pdf_page->pdf_file,
153 pdf_new_obj (PT_DICTIONARY),
154 & pdf_write_jp2_content_callback,
155 image));
157 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
159 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
161 }