pdf_jpeg.c

Thu, 13 Mar 2003 07:59:10 +0000

author
eric
date
Thu, 13 Mar 2003 07:59:10 +0000
changeset 121
e50c7f76f2f6
parent 106
311726c12122
child 125
e2ef1c2f9eca
permissions
-rw-r--r--

don't use page mode USE_OUTLINES if there are no outline entries.

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