pdf_jp2.c

Mon, 14 Dec 2009 16:18:21 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 14 Dec 2009 16:18:21 +0000
changeset 172
2fae6df568f6
parent 166
301f6f17c364
permissions
-rw-r--r--

remove erroneous 0.33-philpem1 tag

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_jp2_image
philpem@166 41 {
philpem@166 42 double width, height;
philpem@166 43 double x, y;
philpem@166 44 uint32_t width_samples, height_samples;
philpem@166 45 FILE *f;
philpem@166 46 char XObject_name [4];
philpem@166 47 };
philpem@166 48
philpem@166 49
philpem@166 50 static void pdf_write_jp2_content_callback (pdf_file_handle pdf_file,
philpem@166 51 struct pdf_obj *stream,
philpem@166 52 void *app_data)
philpem@166 53 {
philpem@166 54 struct pdf_jp2_image *image = app_data;
philpem@166 55
philpem@166 56 /* transformation matrix is: width 0 0 height x y cm */
philpem@166 57 pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
philpem@166 58 image->width, image->height,
philpem@166 59 image->x, image->y);
philpem@166 60 pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
philpem@166 61 image->XObject_name);
philpem@166 62 }
philpem@166 63
philpem@166 64
philpem@166 65 #define JPEG_BUFFER_SIZE 8192
philpem@166 66
philpem@166 67 static void pdf_write_jp2_image_callback (pdf_file_handle pdf_file,
philpem@166 68 struct pdf_obj *stream,
philpem@166 69 void *app_data)
philpem@166 70 {
philpem@166 71 struct pdf_jp2_image *image = app_data;
philpem@166 72 int rlen, wlen;
philpem@166 73 uint8_t *wp;
philpem@166 74 uint8_t buffer [8192];
philpem@166 75
philpem@166 76 while (! feof (image->f))
philpem@166 77 {
philpem@166 78 rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, image->f);
philpem@166 79 wp = & buffer [0];
philpem@166 80 while (rlen)
philpem@166 81 {
philpem@166 82 wlen = fwrite (wp, 1, rlen, pdf_file->f);
philpem@166 83 if (feof (pdf_file->f))
philpem@166 84 pdf_fatal ("unexpected EOF on output file\n");
philpem@166 85 if (ferror (pdf_file->f))
philpem@166 86 pdf_fatal ("error on output file\n");
philpem@166 87 rlen -= wlen;
philpem@166 88 wp += wlen;
philpem@166 89 }
philpem@166 90 if (ferror (image->f))
philpem@166 91 pdf_fatal ("error on input file\n");
philpem@166 92 }
philpem@166 93 }
philpem@166 94
philpem@166 95
philpem@166 96 void pdf_write_jp2_image (pdf_page_handle pdf_page,
philpem@166 97 double x,
philpem@166 98 double y,
philpem@166 99 double width,
philpem@166 100 double height,
philpem@166 101 uint32_t width_samples,
philpem@166 102 uint32_t height_samples,
philpem@166 103 FILE *f)
philpem@166 104 {
philpem@166 105 struct pdf_jp2_image *image;
philpem@166 106
philpem@166 107 struct pdf_obj *stream;
philpem@166 108 struct pdf_obj *stream_dict;
philpem@166 109
philpem@166 110 struct pdf_obj *content_stream;
philpem@166 111
philpem@166 112 image = pdf_calloc (1, sizeof (struct pdf_jp2_image));
philpem@166 113
philpem@166 114 image->width = width;
philpem@166 115 image->height = height;
philpem@166 116 image->x = x;
philpem@166 117 image->y = y;
philpem@166 118
philpem@166 119 image->f = f;
philpem@166 120
philpem@166 121 image->width_samples = width_samples;
philpem@166 122 image->height_samples = height_samples;
philpem@166 123
philpem@166 124 stream_dict = pdf_new_obj (PT_DICTIONARY);
philpem@166 125
philpem@166 126 stream = pdf_new_ind_ref (pdf_page->pdf_file,
philpem@166 127 pdf_new_stream (pdf_page->pdf_file,
philpem@166 128 stream_dict,
philpem@166 129 & pdf_write_jp2_image_callback,
philpem@166 130 image));
philpem@166 131
philpem@166 132 strcpy (& image->XObject_name [0], "Im ");
philpem@166 133 image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
philpem@166 134
philpem@166 135 pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject"));
philpem@166 136 pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
philpem@166 137 // Name is required in PDF 1.0 but obsoleted in later PDF versions
philpem@166 138 // pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0]));
philpem@166 139 pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->width_samples));
philpem@166 140 pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->height_samples));
philpem@166 141
philpem@166 142 // not required for JPXDecode, but
philpem@166 143 pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8));
philpem@166 144
philpem@166 145 pdf_stream_add_filter (stream, "JPXDecode", NULL);
philpem@166 146
philpem@166 147 /* the following will write the stream, using our callback function to
philpem@166 148 get the actual data */
philpem@166 149 pdf_write_ind_obj (pdf_page->pdf_file, stream);
philpem@166 150
philpem@166 151 content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
philpem@166 152 pdf_new_stream (pdf_page->pdf_file,
philpem@166 153 pdf_new_obj (PT_DICTIONARY),
philpem@166 154 & pdf_write_jp2_content_callback,
philpem@166 155 image));
philpem@166 156
philpem@166 157 pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
philpem@166 158
philpem@166 159 pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
philpem@166 160
philpem@166 161 }