pdf_png.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 166
301f6f17c364
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  * Copyright 2004 Daniel Gloeckner
     6  *
     7  * Derived from pdf_jpeg.c written 2003 by Eric Smith <eric at brouhaha.com>
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License version 2 as
    11  * published by the Free Software Foundation.  Note that permission is
    12  * not granted to redistribute this program under the terms of any
    13  * other version of the General Public License.
    14  *
    15  * This program is distributed in the hope that it will be useful,
    16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    18  * GNU General Public License for more details.
    19  *
    20  * You should have received a copy of the GNU General Public License
    21  * along with this program; if not, write to the Free Software
    22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    23  */
    26 #include <stdbool.h>
    27 #include <stdint.h>
    28 #include <stdio.h>
    29 #include <stdlib.h>
    30 #include <string.h>
    33 #include "bitblt.h"
    34 #include "pdf.h"
    35 #include "pdf_util.h"
    36 #include "pdf_prim.h"
    37 #include "pdf_private.h"
    40 struct pdf_png_image
    41 {
    42   double width, height;
    43   double x, y;
    44   bool color;  /* false for grayscale */
    45   uint32_t width_samples, height_samples;
    46   FILE *f;
    47   char XObject_name [4];
    48 };
    51 static void pdf_write_png_content_callback (pdf_file_handle pdf_file,
    52 					     struct pdf_obj *stream,
    53 					     void *app_data)
    54 {
    55   struct pdf_png_image *image = app_data;
    57   /* transformation matrix is: width 0 0 height x y cm */
    58   pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
    59 		     image->width, image->height,
    60 		     image->x, image->y);
    61   pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
    62 		     image->XObject_name);
    63 }
    66 static void pdf_write_png_image_callback (pdf_file_handle pdf_file,
    67 					   struct pdf_obj *stream,
    68 					   void *app_data)
    69 {
    70   struct pdf_png_image *image = app_data;
    71   int rlen, wlen;
    72   uint8_t *wp;
    73   uint8_t buffer [8192];
    75   while (! feof (image->f))
    76     {
    77       uint32_t clen;
    78       rlen = fread (buffer, 1, 8, image->f);
    79       if (rlen != 8)
    80 	pdf_fatal ("unexpected EOF on input file\n");
    81       clen=(buffer[0]<<24)+(buffer[1]<<16)+(buffer[2]<<8)+buffer[3];
    82       if (!memcmp(buffer+4,"IEND",4))
    83 	break;
    84       if (memcmp(buffer+4,"IDAT",4)) {
    85 	fseek(image->f, clen+4, SEEK_CUR);
    86 	continue;
    87       }
    88       while (clen)
    89       {
    90 	rlen = fread (buffer, 1, (clen<sizeof(buffer))?clen:sizeof(buffer), image->f);
    91 	if(!rlen)
    92 	  pdf_fatal ("unexpected EOF on input file\n");
    93 	clen -= rlen;
    94         wp = buffer;
    95         while (rlen)
    96 	{
    97 	  wlen = fwrite (wp, 1, rlen, pdf_file->f);
    98 	  if (feof (pdf_file->f))
    99 	    pdf_fatal ("unexpected EOF on output file\n");
   100 	  if (ferror (pdf_file->f))
   101 	    pdf_fatal ("error on output file\n");
   102 	  rlen -= wlen;
   103 	  wp += wlen;
   104 	}
   105         if (ferror (image->f))
   106 	  pdf_fatal ("error on input file\n");
   107       }
   108       fseek(image->f, 4, SEEK_CUR);
   109     }
   110 }
   113 void pdf_write_png_image (pdf_page_handle pdf_page,
   114 			   double x,
   115 			   double y,
   116 			   double width,
   117 			   double height,
   118 			   int color,
   119 			   char *indexed,
   120 			   int palent,
   121 			   int bpp,
   122 			   uint32_t width_samples,
   123 			   uint32_t height_samples,
   124 			   FILE *f)
   125 {
   126   struct pdf_png_image *image;
   128   struct pdf_obj *stream;
   129   struct pdf_obj *stream_dict;
   130   struct pdf_obj *flateparams;
   132   struct pdf_obj *content_stream;
   134   image = pdf_calloc (1, sizeof (struct pdf_png_image));
   136   image->width = width;
   137   image->height = height;
   138   image->x = x;
   139   image->y = y;
   141   image->f = f;
   143   image->color = color;
   144   image->width_samples = width_samples;
   145   image->height_samples = height_samples;
   147   pdf_add_array_elem_unique (pdf_page->procset,
   148 			     pdf_new_name (palent ? "ImageI" : image->color ? "ImageC" : "ImageB"));
   150   stream_dict = pdf_new_obj (PT_DICTIONARY);
   152   stream = pdf_new_ind_ref (pdf_page->pdf_file,
   153 			    pdf_new_stream (pdf_page->pdf_file,
   154 					    stream_dict,
   155 					    & pdf_write_png_image_callback,
   156 					    image));
   158   strcpy (& image->XObject_name [0], "Im ");
   159   image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
   161   flateparams = pdf_new_obj (PT_DICTIONARY);
   163   pdf_set_dict_entry (stream_dict, "Type",    pdf_new_name ("XObject"));
   164   pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
   165 // Name is required in PDF 1.0 but obsoleted in later PDF versions
   166 //  pdf_set_dict_entry (stream_dict, "Name",    pdf_new_name (& image->XObject_name [0]));
   167   pdf_set_dict_entry (stream_dict, "Width",   pdf_new_integer (image->width_samples));
   168   pdf_set_dict_entry (flateparams, "Columns",   pdf_new_integer (image->width_samples));
   169   pdf_set_dict_entry (stream_dict, "Height",  pdf_new_integer (image->height_samples));
   170   if(palent) {
   171     struct pdf_obj *space;
   172     space = pdf_new_obj (PT_ARRAY);
   173     pdf_add_array_elem (space, pdf_new_name ("Indexed"));
   174     pdf_add_array_elem (space, pdf_new_name ("DeviceRGB"));
   175     pdf_add_array_elem (space, pdf_new_integer (palent-1));
   176     pdf_add_array_elem (space, pdf_new_string_n (indexed,3*palent));
   177     pdf_set_dict_entry (stream_dict, "ColorSpace", space);
   178   } else
   179     pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name (image->color ? "DeviceRGB" : "DeviceGray"));
   180   pdf_set_dict_entry (flateparams, "Colors", pdf_new_integer ((!indexed && image->color) ? 3 : 1));
   181   pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (bpp));
   182   pdf_set_dict_entry (flateparams, "BitsPerComponent", pdf_new_integer (bpp));
   183   pdf_set_dict_entry (flateparams, "Predictor", pdf_new_integer (15));
   185   pdf_stream_add_filter (stream, "FlateDecode", flateparams);
   187   /* the following will write the stream, using our callback function to
   188      get the actual data */
   189   pdf_write_ind_obj (pdf_page->pdf_file, stream);
   191   content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
   192 				    pdf_new_stream (pdf_page->pdf_file,
   193 						    pdf_new_obj (PT_DICTIONARY),
   194 						    & pdf_write_png_content_callback,
   195 						    image));
   197   pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
   199   pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
   200 }