Fri, 14 Mar 2003 08:56:38 +0000
remove debug output.
eric@106 | 1 | /* |
eric@125 | 2 | * tumble: build a PDF file from image files |
eric@106 | 3 | * |
eric@106 | 4 | * PDF routines |
eric@125 | 5 | * $Id: pdf_jpeg.c,v 1.2 2003/03/13 00:57:05 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@106 | 43 | FILE *f; |
eric@106 | 44 | unsigned long Columns; |
eric@106 | 45 | unsigned long Rows; |
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 | FILE *f; |
eric@106 | 73 | int rlen, wlen; |
eric@106 | 74 | uint8_t *wp; |
eric@106 | 75 | uint8_t buffer [8192]; |
eric@106 | 76 | |
eric@106 | 77 | while (! feof (image->f)) |
eric@106 | 78 | { |
eric@106 | 79 | rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, f); |
eric@106 | 80 | wp = & buffer [0]; |
eric@106 | 81 | while (rlen) |
eric@106 | 82 | { |
eric@106 | 83 | wlen = fwrite (wp, 1, rlen, pdf_file->f); |
eric@106 | 84 | if (feof (pdf_file->f)) |
eric@106 | 85 | pdf_fatal ("unexpected EOF on output file\n"); |
eric@106 | 86 | if (ferror (pdf_file->f)) |
eric@106 | 87 | pdf_fatal ("error on output file\n"); |
eric@106 | 88 | rlen -= wlen; |
eric@106 | 89 | wp += wlen; |
eric@106 | 90 | } |
eric@106 | 91 | if (ferror (f)) |
eric@106 | 92 | pdf_fatal ("error on input file\n"); |
eric@106 | 93 | } |
eric@106 | 94 | } |
eric@106 | 95 | |
eric@106 | 96 | |
eric@106 | 97 | void pdf_write_jpeg_image (pdf_page_handle pdf_page, |
eric@106 | 98 | double x, |
eric@106 | 99 | double y, |
eric@106 | 100 | double width, |
eric@106 | 101 | double height, |
eric@106 | 102 | FILE *f) |
eric@106 | 103 | { |
eric@106 | 104 | struct pdf_jpeg_image *image; |
eric@106 | 105 | |
eric@106 | 106 | struct pdf_obj *stream; |
eric@106 | 107 | struct pdf_obj *stream_dict; |
eric@106 | 108 | struct pdf_obj *decode_parms; |
eric@106 | 109 | |
eric@106 | 110 | struct pdf_obj *content_stream; |
eric@106 | 111 | |
eric@106 | 112 | image = pdf_calloc (1, sizeof (struct pdf_jpeg_image)); |
eric@106 | 113 | |
eric@106 | 114 | image->width = width; |
eric@106 | 115 | image->height = height; |
eric@106 | 116 | image->x = x; |
eric@106 | 117 | image->y = y; |
eric@106 | 118 | |
eric@106 | 119 | image->f = f; |
eric@106 | 120 | #if 0 |
eric@106 | 121 | image->Columns = bitmap->rect.max.x - bitmap->rect.min.x; |
eric@106 | 122 | image->Rows = bitmap->rect.max.y - bitmap->rect.min.y; |
eric@106 | 123 | #endif |
eric@106 | 124 | |
eric@106 | 125 | stream_dict = pdf_new_obj (PT_DICTIONARY); |
eric@106 | 126 | |
eric@106 | 127 | stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@106 | 128 | pdf_new_stream (pdf_page->pdf_file, |
eric@106 | 129 | stream_dict, |
eric@106 | 130 | & pdf_write_jpeg_image_callback, |
eric@106 | 131 | image)); |
eric@106 | 132 | |
eric@106 | 133 | strcpy (& image->XObject_name [0], "Im "); |
eric@106 | 134 | image->XObject_name [2] = pdf_new_XObject (pdf_page, stream); |
eric@106 | 135 | |
eric@106 | 136 | pdf_set_dict_entry (stream_dict, "Type", pdf_new_name ("XObject")); |
eric@106 | 137 | pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image")); |
eric@106 | 138 | pdf_set_dict_entry (stream_dict, "Name", pdf_new_name (& image->XObject_name [0])); |
eric@106 | 139 | pdf_set_dict_entry (stream_dict, "Width", pdf_new_integer (image->Columns)); |
eric@106 | 140 | pdf_set_dict_entry (stream_dict, "Height", pdf_new_integer (image->Rows)); |
eric@106 | 141 | pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8)); |
eric@106 | 142 | |
eric@106 | 143 | decode_parms = pdf_new_obj (PT_DICTIONARY); |
eric@106 | 144 | |
eric@106 | 145 | pdf_set_dict_entry (decode_parms, |
eric@106 | 146 | "K", |
eric@106 | 147 | pdf_new_integer (-1)); |
eric@106 | 148 | |
eric@106 | 149 | pdf_set_dict_entry (decode_parms, |
eric@106 | 150 | "Columns", |
eric@106 | 151 | pdf_new_integer (image->Columns)); |
eric@106 | 152 | |
eric@106 | 153 | pdf_set_dict_entry (decode_parms, |
eric@106 | 154 | "Rows", |
eric@106 | 155 | pdf_new_integer (image->Rows)); |
eric@106 | 156 | |
eric@106 | 157 | pdf_stream_add_filter (stream, "DCTDecode", decode_parms); |
eric@106 | 158 | |
eric@106 | 159 | /* the following will write the stream, using our callback function to |
eric@106 | 160 | get the actual data */ |
eric@106 | 161 | pdf_write_ind_obj (pdf_page->pdf_file, stream); |
eric@106 | 162 | |
eric@106 | 163 | content_stream = pdf_new_ind_ref (pdf_page->pdf_file, |
eric@106 | 164 | pdf_new_stream (pdf_page->pdf_file, |
eric@106 | 165 | pdf_new_obj (PT_DICTIONARY), |
eric@106 | 166 | & pdf_write_jpeg_content_callback, |
eric@106 | 167 | image)); |
eric@106 | 168 | |
eric@106 | 169 | pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream); |
eric@106 | 170 | |
eric@106 | 171 | pdf_write_ind_obj (pdf_page->pdf_file, content_stream); |
eric@106 | 172 | } |
eric@106 | 173 |