pdf_png.c

Mon, 14 Dec 2009 15:51:53 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 14 Dec 2009 15:51:53 +0000
changeset 166
301f6f17c364
permissions
-rw-r--r--

Patched to add PNG and JP2 support.

Created-By: Daniel Glöckner <daniel-gl at gmx.net>


The attached patch adds PNG and JP2 support to tumble.

PNG:
As the deflated data is directly copied into the PDF, there are some
limitations to the list of supported images:
- bit depth <= 8
- no alpha channel
- no interlace

JP2:
The PDF Reference says JP2 is just a subset of the allowed JPX
format. I don't have a copy of the official standard, so I don't know
what to change to cover JPXes as well.
You'll need the Adobe Acrobat Reader 6 to display those images.
Xpdf and Ghostscript are missing the ColorSpace key in the image
dictionary, which is optional for JPXDecode and IMHO not just a matter
of a few lines of code.
One thing left to do is to change the PDF version to 1.5 if a JP2 file
has been given to tumble - maybe using the Version key in the Catalog
if seeking is not possible.
Using the resolution info in a JP2 (resc/resd boxes) is implemented but
untested. Jasper doesn't write those boxes.

I had to change the string handling to allow black in PNG palettes.
And there was a double free in tumble_input.c which happens when not
using control files.

Daniel

philpem@166 1 /*
philpem@166 2 * tumble: build a PDF file from image files
philpem@166 3 *
philpem@166 4 * PDF routines
philpem@166 5 * Copyright 2004 Daniel Gloeckner
philpem@166 6 *
philpem@166 7 * Derived from pdf_jpeg.c written 2003 by Eric Smith <eric at brouhaha.com>
philpem@166 8 *
philpem@166 9 * This program is free software; you can redistribute it and/or modify
philpem@166 10 * it under the terms of the GNU General Public License version 2 as
philpem@166 11 * published by the Free Software Foundation. Note that permission is
philpem@166 12 * not granted to redistribute this program under the terms of any
philpem@166 13 * other version of the General Public License.
philpem@166 14 *
philpem@166 15 * This program is distributed in the hope that it will be useful,
philpem@166 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
philpem@166 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
philpem@166 18 * GNU General Public License for more details.
philpem@166 19 *
philpem@166 20 * You should have received a copy of the GNU General Public License
philpem@166 21 * along with this program; if not, write to the Free Software
philpem@166 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
philpem@166 23 */
philpem@166 24
philpem@166 25
philpem@166 26 #include <stdbool.h>
philpem@166 27 #include <stdint.h>
philpem@166 28 #include <stdio.h>
philpem@166 29 #include <stdlib.h>
philpem@166 30 #include <string.h>
philpem@166 31
philpem@166 32
philpem@166 33 #include "bitblt.h"
philpem@166 34 #include "pdf.h"
philpem@166 35 #include "pdf_util.h"
philpem@166 36 #include "pdf_prim.h"
philpem@166 37 #include "pdf_private.h"
philpem@166 38
philpem@166 39
philpem@166 40 struct pdf_png_image
philpem@166 41 {
philpem@166 42 double width, height;
philpem@166 43 double x, y;
philpem@166 44 bool color; /* false for grayscale */
philpem@166 45 uint32_t width_samples, height_samples;
philpem@166 46 FILE *f;
philpem@166 47 char XObject_name [4];
philpem@166 48 };
philpem@166 49
philpem@166 50
philpem@166 51 static void pdf_write_png_content_callback (pdf_file_handle pdf_file,
philpem@166 52 struct pdf_obj *stream,
philpem@166 53 void *app_data)
philpem@166 54 {
philpem@166 55 struct pdf_png_image *image = app_data;
philpem@166 56
philpem@166 57 /* transformation matrix is: width 0 0 height x y cm */
philpem@166 58 pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
philpem@166 59 image->width, image->height,
philpem@166 60 image->x, image->y);
philpem@166 61 pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
philpem@166 62 image->XObject_name);
philpem@166 63 }
philpem@166 64
philpem@166 65
philpem@166 66 static void pdf_write_png_image_callback (pdf_file_handle pdf_file,
philpem@166 67 struct pdf_obj *stream,
philpem@166 68 void *app_data)
philpem@166 69 {
philpem@166 70 struct pdf_png_image *image = app_data;
philpem@166 71 int rlen, wlen;
philpem@166 72 uint8_t *wp;
philpem@166 73 uint8_t buffer [8192];
philpem@166 74
philpem@166 75 while (! feof (image->f))
philpem@166 76 {
philpem@166 77 uint32_t clen;
philpem@166 78 rlen = fread (buffer, 1, 8, image->f);
philpem@166 79 if (rlen != 8)
philpem@166 80 pdf_fatal ("unexpected EOF on input file\n");
philpem@166 81 clen=(buffer[0]<<24)+(buffer[1]<<16)+(buffer[2]<<8)+buffer[3];
philpem@166 82 if (!memcmp(buffer+4,"IEND",4))
philpem@166 83 break;
philpem@166 84 if (memcmp(buffer+4,"IDAT",4)) {
philpem@166 85 fseek(image->f, clen+4, SEEK_CUR);
philpem@166 86 continue;
philpem@166 87 }
philpem@166 88 while (clen)
philpem@166 89 {
philpem@166 90 rlen = fread (buffer, 1, (clen<sizeof(buffer))?clen:sizeof(buffer), image->f);
philpem@166 91 if(!rlen)
philpem@166 92 pdf_fatal ("unexpected EOF on input file\n");
philpem@166 93 clen -= rlen;
philpem@166 94 wp = buffer;
philpem@166 95 while (rlen)
philpem@166 96 {
philpem@166 97 wlen = fwrite (wp, 1, rlen, pdf_file->f);
philpem@166 98 if (feof (pdf_file->f))
philpem@166 99 pdf_fatal ("unexpected EOF on output file\n");
philpem@166 100 if (ferror (pdf_file->f))
philpem@166 101 pdf_fatal ("error on output file\n");
philpem@166 102 rlen -= wlen;
philpem@166 103 wp += wlen;
philpem@166 104 }
philpem@166 105 if (ferror (image->f))
philpem@166 106 pdf_fatal ("error on input file\n");
philpem@166 107 }
philpem@166 108 fseek(image->f, 4, SEEK_CUR);
philpem@166 109 }
philpem@166 110 }
philpem@166 111
philpem@166 112
philpem@166 113 void pdf_write_png_image (pdf_page_handle pdf_page,
philpem@166 114 double x,
philpem@166 115 double y,
philpem@166 116 double width,
philpem@166 117 double height,
philpem@166 118 int color,
philpem@166 119 char *indexed,
philpem@166 120 int palent,
philpem@166 121 int bpp,
philpem@166 122 uint32_t width_samples,
philpem@166 123 uint32_t height_samples,
philpem@166 124 FILE *f)
philpem@166 125 {
philpem@166 126 struct pdf_png_image *image;
philpem@166 127
philpem@166 128 struct pdf_obj *stream;
philpem@166 129 struct pdf_obj *stream_dict;
philpem@166 130 struct pdf_obj *flateparams;
philpem@166 131
philpem@166 132 struct pdf_obj *content_stream;
philpem@166 133
philpem@166 134 image = pdf_calloc (1, sizeof (struct pdf_png_image));
philpem@166 135
philpem@166 136 image->width = width;
philpem@166 137 image->height = height;
philpem@166 138 image->x = x;
philpem@166 139 image->y = y;
philpem@166 140
philpem@166 141 image->f = f;
philpem@166 142
philpem@166 143 image->color = color;
philpem@166 144 image->width_samples = width_samples;
philpem@166 145 image->height_samples = height_samples;
philpem@166 146
philpem@166 147 pdf_add_array_elem_unique (pdf_page->procset,
philpem@166 148 pdf_new_name (palent ? "ImageI" : image->color ? "ImageC" : "ImageB"));
philpem@166 149
philpem@166 150 stream_dict = pdf_new_obj (PT_DICTIONARY);
philpem@166 151
philpem@166 152 stream = pdf_new_ind_ref (pdf_page->pdf_file,
philpem@166 153 pdf_new_stream (pdf_page->pdf_file,
philpem@166 154 stream_dict,
philpem@166 155 & pdf_write_png_image_callback,
philpem@166 156 image));
philpem@166 157
philpem@166 158 strcpy (& image->XObject_name [0], "Im ");
philpem@166 159 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
philpem@166 160
philpem@166 161 flateparams = pdf_new_obj (PT_DICTIONARY);
philpem@166 162
philpem@166 163 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
philpem@166 164 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
philpem@166 165 // Name is required in PDF 1.0 but obsoleted in later PDF versions
philpem@166 166 // pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
philpem@166 167 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->width_samples));
philpem@166 168 pdf_set_dict_entry (flateparams, "Columns", pdf_new_integer (image->width_samples));
philpem@166 169 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->height_samples));
philpem@166 170 if(palent) {
philpem@166 171 struct pdf_obj *space;
philpem@166 172 space = pdf_new_obj (PT_ARRAY);
philpem@166 173 pdf_add_array_elem (space, pdf_new_name ("Indexed"));
philpem@166 174 pdf_add_array_elem (space, pdf_new_name ("DeviceRGB"));
philpem@166 175 pdf_add_array_elem (space, pdf_new_integer (palent-1));
philpem@166 176 pdf_add_array_elem (space, pdf_new_string_n (indexed,3*palent));
philpem@166 177 pdf_set_dict_entry (stream_dict, "ColorSpace", space);
philpem@166 178 } else
philpem@166 179 pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name (image->color ? "DeviceRGB" : "DeviceGray"));
philpem@166 180 pdf_set_dict_entry (flateparams, "Colors", pdf_new_integer ((!indexed && image->color) ? 3 : 1));
philpem@166 181 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (bpp));
philpem@166 182 pdf_set_dict_entry (flateparams, "BitsPerComponent", pdf_new_integer (bpp));
philpem@166 183 pdf_set_dict_entry (flateparams, "Predictor", pdf_new_integer (15));
philpem@166 184
philpem@166 185 pdf_stream_add_filter (stream, "FlateDecode", flateparams);
philpem@166 186
philpem@166 187 /* the following will write the stream, using our callback function to
philpem@166 188 get the actual data */
philpem@166 189 pdf_write_ind_obj (pdf_page->pdf_file, stream);
philpem@166 190
philpem@166 191 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
philpem@166 192 pdf_new_stream (pdf_page->pdf_file,
philpem@166 193 pdf_new_obj (PT_DICTIONARY),
philpem@166 194 & pdf_write_png_content_callback,
philpem@166 195 image));
philpem@166 196
philpem@166 197 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
philpem@166 198
philpem@166 199 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
philpem@166 200 }