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