pdf_jpeg.c

changeset 106
311726c12122
child 125
e2ef1c2f9eca
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pdf_jpeg.c	Wed Mar 12 10:57:06 2003 +0000
     1.3 @@ -0,0 +1,175 @@
     1.4 +/*
     1.5 + * t2p: Create a PDF file from the contents of one or more TIFF
     1.6 + *      bilevel image files.  The images in the resulting PDF file
     1.7 + *      will be compressed using ITU-T T.6 (G4) fax encoding.
     1.8 + *
     1.9 + * PDF routines
    1.10 + * $Id: pdf_jpeg.c,v 1.1 2003/03/12 02:57:06 eric Exp $
    1.11 + * Copyright 2003 Eric Smith <eric@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_jpeg_image
    1.45 +{
    1.46 +  double width, height;
    1.47 +  double x, y;
    1.48 +  FILE *f;
    1.49 +  unsigned long Columns;
    1.50 +  unsigned long Rows;
    1.51 +  char XObject_name [4];
    1.52 +};
    1.53 +
    1.54 +
    1.55 +static void pdf_write_jpeg_content_callback (pdf_file_handle pdf_file,
    1.56 +					     struct pdf_obj *stream,
    1.57 +					     void *app_data)
    1.58 +{
    1.59 +  struct pdf_jpeg_image *image = app_data;
    1.60 +
    1.61 +  /* transformation matrix is: width 0 0 height x y cm */
    1.62 +  pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
    1.63 +		     image->width, image->height,
    1.64 +		     image->x, image->y);
    1.65 +  pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
    1.66 +		     image->XObject_name);
    1.67 +}
    1.68 +
    1.69 +
    1.70 +#define JPEG_BUFFER_SIZE 8192
    1.71 +
    1.72 +static void pdf_write_jpeg_image_callback (pdf_file_handle pdf_file,
    1.73 +					   struct pdf_obj *stream,
    1.74 +					   void *app_data)
    1.75 +{
    1.76 +  struct pdf_jpeg_image *image = app_data;
    1.77 +  FILE *f;
    1.78 +  int rlen, wlen;
    1.79 +  uint8_t *wp;
    1.80 +  uint8_t buffer [8192];
    1.81 +
    1.82 +  while (! feof (image->f))
    1.83 +    {
    1.84 +      rlen = fread (& buffer [0], 1, JPEG_BUFFER_SIZE, f);
    1.85 +      wp = & buffer [0];
    1.86 +      while (rlen)
    1.87 +	{
    1.88 +	  wlen = fwrite (wp, 1, rlen, pdf_file->f);
    1.89 +	  if (feof (pdf_file->f))
    1.90 +	    pdf_fatal ("unexpected EOF on output file\n");
    1.91 +	  if (ferror (pdf_file->f))
    1.92 +	    pdf_fatal ("error on output file\n");
    1.93 +	  rlen -= wlen;
    1.94 +	  wp += wlen;
    1.95 +	}
    1.96 +      if (ferror (f))
    1.97 +	pdf_fatal ("error on input file\n");
    1.98 +    }
    1.99 +}
   1.100 +
   1.101 +
   1.102 +void pdf_write_jpeg_image (pdf_page_handle pdf_page,
   1.103 +			   double x,
   1.104 +			   double y,
   1.105 +			   double width,
   1.106 +			   double height,
   1.107 +			   FILE *f)
   1.108 +{
   1.109 +  struct pdf_jpeg_image *image;
   1.110 +
   1.111 +  struct pdf_obj *stream;
   1.112 +  struct pdf_obj *stream_dict;
   1.113 +  struct pdf_obj *decode_parms;
   1.114 +
   1.115 +  struct pdf_obj *content_stream;
   1.116 +
   1.117 +  image = pdf_calloc (1, sizeof (struct pdf_jpeg_image));
   1.118 +
   1.119 +  image->width = width;
   1.120 +  image->height = height;
   1.121 +  image->x = x;
   1.122 +  image->y = y;
   1.123 +
   1.124 +  image->f = f;
   1.125 +#if 0
   1.126 +  image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
   1.127 +  image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
   1.128 +#endif
   1.129 +
   1.130 +  stream_dict = pdf_new_obj (PT_DICTIONARY);
   1.131 +
   1.132 +  stream = pdf_new_ind_ref (pdf_page->pdf_file,
   1.133 +			    pdf_new_stream (pdf_page->pdf_file,
   1.134 +					    stream_dict,
   1.135 +					    & pdf_write_jpeg_image_callback,
   1.136 +					    image));
   1.137 +
   1.138 +  strcpy (& image->XObject_name [0], "Im ");
   1.139 +  image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
   1.140 +
   1.141 +  pdf_set_dict_entry (stream_dict, "Type",    pdf_new_name ("XObject"));
   1.142 +  pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
   1.143 +  pdf_set_dict_entry (stream_dict, "Name",    pdf_new_name (& image->XObject_name [0]));
   1.144 +  pdf_set_dict_entry (stream_dict, "Width",   pdf_new_integer (image->Columns));
   1.145 +  pdf_set_dict_entry (stream_dict, "Height",  pdf_new_integer (image->Rows));
   1.146 +  pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (8));
   1.147 +
   1.148 +  decode_parms = pdf_new_obj (PT_DICTIONARY);
   1.149 +
   1.150 +  pdf_set_dict_entry (decode_parms,
   1.151 +		      "K",
   1.152 +		      pdf_new_integer (-1));
   1.153 +
   1.154 +  pdf_set_dict_entry (decode_parms,
   1.155 +		      "Columns",
   1.156 +		      pdf_new_integer (image->Columns));
   1.157 +
   1.158 +  pdf_set_dict_entry (decode_parms,
   1.159 +		      "Rows",
   1.160 +		      pdf_new_integer (image->Rows));
   1.161 +
   1.162 +  pdf_stream_add_filter (stream, "DCTDecode", decode_parms);
   1.163 +
   1.164 +  /* the following will write the stream, using our callback function to
   1.165 +     get the actual data */
   1.166 +  pdf_write_ind_obj (pdf_page->pdf_file, stream);
   1.167 +
   1.168 +  content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
   1.169 +				    pdf_new_stream (pdf_page->pdf_file,
   1.170 +						    pdf_new_obj (PT_DICTIONARY),
   1.171 +						    & pdf_write_jpeg_content_callback,
   1.172 +						    image));
   1.173 +
   1.174 +  pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
   1.175 +
   1.176 +  pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
   1.177 +}
   1.178 +