pdf_jpeg.c

Mon, 14 Dec 2009 16:18:21 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 14 Dec 2009 16:18:21 +0000
changeset 172
2fae6df568f6
parent 147
090af3709920
permissions
-rw-r--r--

remove erroneous 0.33-philpem1 tag

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