1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/pdf_jp2.c Mon Dec 14 15:51:53 2009 +0000 1.3 @@ -0,0 +1,161 @@ 1.4 +/* 1.5 + * tumble: build a PDF file from image files 1.6 + * 1.7 + * PDF routines 1.8 + * Copyright 2004 Daniel Gloeckner 1.9 + * 1.10 + * Derived from pdf_jpeg.c written 2003 by Eric Smith <eric at brouhaha.com> 1.11 + * 1.12 + * This program is free software; you can redistribute it and/or modify 1.13 + * it under the terms of the GNU General Public License version 2 as 1.14 + * published by the Free Software Foundation. Note that permission is 1.15 + * not granted to redistribute this program under the terms of any 1.16 + * other version of the General Public License. 1.17 + * 1.18 + * This program is distributed in the hope that it will be useful, 1.19 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 1.20 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.21 + * GNU General Public License for more details. 1.22 + * 1.23 + * You should have received a copy of the GNU General Public License 1.24 + * along with this program; if not, write to the Free Software 1.25 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA 1.26 + */ 1.27 + 1.28 + 1.29 +#include <stdbool.h> 1.30 +#include <stdint.h> 1.31 +#include <stdio.h> 1.32 +#include <stdlib.h> 1.33 +#include <string.h> 1.34 + 1.35 + 1.36 +#include "bitblt.h" 1.37 +#include "pdf.h" 1.38 +#include "pdf_util.h" 1.39 +#include "pdf_prim.h" 1.40 +#include "pdf_private.h" 1.41 + 1.42 + 1.43 +struct pdf_jp2_image 1.44 +{ 1.45 + double width, height; 1.46 + double x, y; 1.47 + uint32_t width_samples, height_samples; 1.48 + FILE *f; 1.49 + char XObject_name [4]; 1.50 +}; 1.51 + 1.52 + 1.53 +static void pdf_write_jp2_content_callback (pdf_file_handle pdf_file, 1.54 + struct pdf_obj *stream, 1.55 + void *app_data) 1.56 +{ 1.57 + struct pdf_jp2_image *image = app_data; 1.58 + 1.59 + /* transformation matrix is: width 0 0 height x y cm */ 1.60 + pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ", 1.61 + image->width, image->height, 1.62 + image->x, image->y); 1.63 + pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n", 1.64 + image->XObject_name); 1.65 +} 1.66 + 1.67 + 1.68 +#define JPEG_BUFFER_SIZE 8192 1.69 + 1.70 +static void pdf_write_jp2_image_callback (pdf_file_handle pdf_file, 1.71 + struct pdf_obj *stream, 1.72 + void *app_data) 1.73 +{ 1.74 + struct pdf_jp2_image *image = app_data; 1.75 + int rlen, wlen; 1.76 + uint8_t *wp; 1.77 + uint8_t buffer [8192]; 1.78 + 1.79 + while (! feof (image->f)) 1.80 + { 1.81 + rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, image->f); 1.82 + wp = & buffer [0]; 1.83 + while (rlen) 1.84 + { 1.85 + wlen = fwrite (wp, 1, rlen, pdf_file->f); 1.86 + if (feof (pdf_file->f)) 1.87 + pdf_fatal ("unexpected EOF on output file\n"); 1.88 + if (ferror (pdf_file->f)) 1.89 + pdf_fatal ("error on output file\n"); 1.90 + rlen -= wlen; 1.91 + wp += wlen; 1.92 + } 1.93 + if (ferror (image->f)) 1.94 + pdf_fatal ("error on input file\n"); 1.95 + } 1.96 +} 1.97 + 1.98 + 1.99 +void pdf_write_jp2_image (pdf_page_handle pdf_page, 1.100 + double x, 1.101 + double y, 1.102 + double width, 1.103 + double height, 1.104 + uint32_t width_samples, 1.105 + uint32_t height_samples, 1.106 + FILE *f) 1.107 +{ 1.108 + struct pdf_jp2_image *image; 1.109 + 1.110 + struct pdf_obj *stream; 1.111 + struct pdf_obj *stream_dict; 1.112 + 1.113 + struct pdf_obj *content_stream; 1.114 + 1.115 + image = pdf_calloc (1, sizeof (struct pdf_jp2_image)); 1.116 + 1.117 + image->width = width; 1.118 + image->height = height; 1.119 + image->x = x; 1.120 + image->y = y; 1.121 + 1.122 + image->f = f; 1.123 + 1.124 + image->width_samples = width_samples; 1.125 + image->height_samples = height_samples; 1.126 + 1.127 + stream_dict = pdf_new_obj (PT_DICTIONARY); 1.128 + 1.129 + stream = pdf_new_ind_ref (pdf_page->pdf_file, 1.130 + pdf_new_stream (pdf_page->pdf_file, 1.131 + stream_dict, 1.132 + & pdf_write_jp2_image_callback, 1.133 + image)); 1.134 + 1.135 + strcpy (& image->XObject_name [0], "Im "); 1.136 + image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); 1.137 + 1.138 + pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); 1.139 + pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); 1.140 +// Name is required in PDF 1.0 but obsoleted in later PDF versions 1.141 +// pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); 1.142 + pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->width_samples)); 1.143 + pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->height_samples)); 1.144 + 1.145 + // not required for JPXDecode, but 1.146 + pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8)); 1.147 + 1.148 + pdf_stream_add_filter (stream, "JPXDecode", NULL); 1.149 + 1.150 + /* the following will write the stream, using our callback function to 1.151 + get the actual data */ 1.152 + pdf_write_ind_obj (pdf_page->pdf_file, stream); 1.153 + 1.154 + content_stream = pdf_new_ind_ref (pdf_page->pdf_file, 1.155 + pdf_new_stream (pdf_page->pdf_file, 1.156 + pdf_new_obj (PT_DICTIONARY), 1.157 + & pdf_write_jp2_content_callback, 1.158 + image)); 1.159 + 1.160 + pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); 1.161 + 1.162 + pdf_write_ind_obj (pdf_page->pdf_file, content_stream); 1.163 + 1.164 +}