Tue, 25 Mar 2003 09:38:08 +0000
support both big- and little-endian TIFF files. don't crash in close_input_file() if current_input_handler is NULL.
eric@106 | 1 | /* |
eric@125 | 2 | * tumble: build a PDF file from image files |
eric@106 | 3 | * |
eric@106 | 4 | * PDF routines |
eric@147 | 5 | * $Id: pdf_jpeg.c,v 1.5 2003/03/19 23:53:09 eric Exp $ |
eric@106 | 6 | * Copyright 2003 Eric Smith <eric@brouhaha.com> |
eric@106 | 7 | * |
eric@106 | 8 | * This program is free software; you can redistribute it and/or modify |
eric@106 | 9 | * it under the terms of the GNU General Public License version 2 as |
eric@106 | 10 | * published by the Free Software Foundation. Note that permission is |
eric@106 | 11 | * not granted to redistribute this program under the terms of any |
eric@106 | 12 | * other version of the General Public License. |
eric@106 | 13 | * |
eric@106 | 14 | * This program is distributed in the hope that it will be useful, |
eric@106 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
eric@106 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eric@106 | 17 | * GNU General Public License for more details. |
eric@106 | 18 | * |
eric@106 | 19 | * You should have received a copy of the GNU General Public License |
eric@106 | 20 | * along with this program; if not, write to the Free Software |
eric@106 | 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA |
eric@106 | 22 | */ |
eric@106 | 23 | |
eric@106 | 24 | |
eric@106 | 25 | #include <stdbool.h> |
eric@106 | 26 | #include <stdint.h> |
eric@106 | 27 | #include <stdio.h> |
eric@106 | 28 | #include <stdlib.h> |
eric@106 | 29 | #include <string.h> |
eric@106 | 30 | |
eric@106 | 31 | |
eric@106 | 32 | #include "bitblt.h" |
eric@106 | 33 | #include "pdf.h" |
eric@106 | 34 | #include "pdf_util.h" |
eric@106 | 35 | #include "pdf_prim.h" |
eric@106 | 36 | #include "pdf_private.h" |
eric@106 | 37 | |
eric@106 | 38 | |
eric@106 | 39 | struct pdf_jpeg_image |
eric@106 | 40 | { |
eric@106 | 41 | double width, height; |
eric@106 | 42 | double x, y; |
eric@139 | 43 | bool color; /* false for grayscale */ |
eric@139 | 44 | uint32_t width_samples, height_samples; |
eric@106 | 45 | FILE *f; |
eric@106 | 46 | char XObject_name [4]; |
eric@106 | 47 | }; |
eric@106 | 48 | |
eric@106 | 49 | |
eric@106 | 50 | static void pdf_write_jpeg_content_callback (pdf_file_handle pdf_file, |
eric@106 | 51 | struct pdf_obj *stream, |
eric@106 | 52 | void *app_data) |
eric@106 | 53 | { |
eric@106 | 54 | struct pdf_jpeg_image *image = app_data; |
eric@106 | 55 | |
eric@106 | 56 | /* transformation matrix is: width 0 0 height x y cm */ |
eric@106 | 57 | pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ", |
eric@106 | 58 | image->width, image->height, |
eric@106 | 59 | image->x, image->y); |
eric@106 | 60 | pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n", |
eric@106 | 61 | image->XObject_name); |
eric@106 | 62 | } |
eric@106 | 63 | |
eric@106 | 64 | |
eric@106 | 65 | #define JPEG_BUFFER_SIZE 8192 |
eric@106 | 66 | |
eric@106 | 67 | static void pdf_write_jpeg_image_callback (pdf_file_handle pdf_file, |
eric@106 | 68 | struct pdf_obj *stream, |
eric@106 | 69 | void *app_data) |
eric@106 | 70 | { |
eric@106 | 71 | struct pdf_jpeg_image *image = app_data; |
eric@106 | 72 | int rlen, wlen; |
eric@106 | 73 | uint8_t *wp; |
eric@106 | 74 | uint8_t buffer [8192]; |
eric@106 | 75 | |
eric@106 | 76 | while (! feof (image->f)) |
eric@106 | 77 | { |
eric@139 | 78 | rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, image->f); |
eric@106 | 79 | wp = & buffer [0]; |
eric@106 | 80 | while (rlen) |
eric@106 | 81 | { |
eric@106 | 82 | wlen = fwrite (wp, 1, rlen, pdf_file->f); |
eric@106 | 83 | if (feof (pdf_file->f)) |
eric@106 | 84 | pdf_fatal ("unexpected EOF on output file\n"); |
eric@106 | 85 | if (ferror (pdf_file->f)) |
eric@106 | 86 | pdf_fatal ("error on output file\n"); |
eric@106 | 87 | rlen -= wlen; |
eric@106 | 88 | wp += wlen; |
eric@106 | 89 | } |
eric@139 | 90 | if (ferror (image->f)) |
eric@106 | 91 | pdf_fatal ("error on input file\n"); |
eric@106 | 92 | } |
eric@106 | 93 | } |
eric@106 | 94 | |
eric@106 | 95 | |
eric@106 | 96 | void pdf_write_jpeg_image (pdf_page_handle pdf_page, |
eric@106 | 97 | double x, |
eric@106 | 98 | double y, |
eric@106 | 99 | double width, |
eric@106 | 100 | double height, |
eric@147 | 101 | bool color, |
eric@147 | 102 | uint32_t width_samples, |
eric@147 | 103 | uint32_t height_samples, |
eric@106 | 104 | FILE *f) |
eric@106 | 105 | { |
eric@106 | 106 | struct pdf_jpeg_image *image; |
eric@106 | 107 | |
eric@106 | 108 | struct pdf_obj *stream; |
eric@106 | 109 | struct pdf_obj *stream_dict; |
eric@106 | 110 | |
eric@106 | 111 | struct pdf_obj *content_stream; |
eric@106 | 112 | |
eric@106 | 113 | image = pdf_calloc (1, sizeof (struct pdf_jpeg_image)); |
eric@106 | 114 | |
eric@106 | 115 | image->width = width; |
eric@106 | 116 | image->height = height; |
eric@106 | 117 | image->x = x; |
eric@106 | 118 | image->y = y; |
eric@106 | 119 | |
eric@106 | 120 | image->f = f; |
eric@139 | 121 | |
eric@147 | 122 | image->color = color; |
eric@147 | 123 | image->width_samples = width_samples; |
eric@147 | 124 | image->height_samples = height_samples; |
eric@139 | 125 | |
eric@139 | 126 | pdf_add_array_elem_unique (pdf_page->procset, |
eric@139 | 127 | pdf_new_name (image->color ? "ImageC" : "ImageB")); |
eric@106 | 128 | |
eric@106 | 129 | stream_dict = pdf_new_obj (PT_DICTIONARY); |
eric@106 | 130 | |
eric@106 | 131 | stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@106 | 132 | pdf_new_stream (pdf_page->pdf_file, |
eric@106 | 133 | stream_dict, |
eric@106 | 134 | & pdf_write_jpeg_image_callback, |
eric@106 | 135 | image)); |
eric@106 | 136 | |
eric@106 | 137 | strcpy (& image->XObject_name [0], "Im "); |
eric@106 | 138 | image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); |
eric@106 | 139 | |
eric@106 | 140 | pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); |
eric@106 | 141 | pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); |
eric@139 | 142 | // Name is required in PDF 1.0 but obsoleted in later PDF versions |
eric@139 | 143 | // pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); |
eric@139 | 144 | pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->width_samples)); |
eric@139 | 145 | pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->height_samples)); |
eric@139 | 146 | pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name (image->color ? "DeviceRGB" : "DeviceGray")); |
eric@106 | 147 | pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8)); |
eric@106 | 148 | |
eric@139 | 149 | pdf_stream_add_filter (stream, "DCTDecode", NULL); |
eric@106 | 150 | |
eric@106 | 151 | /* the following will write the stream, using our callback function to |
eric@106 | 152 | get the actual data */ |
eric@106 | 153 | pdf_write_ind_obj (pdf_page->pdf_file, stream); |
eric@106 | 154 | |
eric@106 | 155 | content_stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@106 | 156 | pdf_new_stream (pdf_page->pdf_file, |
eric@106 | 157 | pdf_new_obj (PT_DICTIONARY), |
eric@106 | 158 | & pdf_write_jpeg_content_callback, |
eric@106 | 159 | image)); |
eric@106 | 160 | |
eric@106 | 161 | pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); |
eric@106 | 162 | |
eric@106 | 163 | pdf_write_ind_obj (pdf_page->pdf_file, content_stream); |
eric@106 | 164 | } |