pdf_jpeg.c

Fri, 14 Mar 2003 08:56:38 +0000

author
eric
date
Fri, 14 Mar 2003 08:56:38 +0000
changeset 132
ccf1c28a2940
parent 125
e2ef1c2f9eca
child 139
ec2a06d8a2a6
permissions
-rw-r--r--

remove debug output.

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