pdf_g4.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 145
3ed3f7f32837
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_g4.c,v 1.16 2003/03/19 23:44:53 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_g4_image
    40 {
    41   double width, height;
    42   double x, y;
    43   double r, g, b;  /* fill color, only for ImageMask */
    44   unsigned long Columns;
    45   unsigned long Rows;
    46   bool ImageMask;
    47   bool BlackIs1;
    48   Bitmap *bitmap;
    49   char XObject_name [4];
    50 };
    53 static void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
    54 					   struct pdf_obj *stream,
    55 					   void *app_data)
    56 {
    57   struct pdf_g4_image *image = app_data;
    59   /* transformation matrix is: width 0 0 height x y cm */
    60   pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
    61 		     image->width, image->height,
    62 		     image->x, image->y);
    63   if (image->ImageMask)
    64     pdf_stream_printf (pdf_file, stream, "%g %g %g rg ",
    65 		       image->r, image->g, image->b);
    67   pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
    68 		     image->XObject_name);
    69 }
    72 static void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
    73 					     struct pdf_obj *stream,
    74 					     void *app_data)
    75 {
    76   struct pdf_g4_image *image = app_data;
    78   bitblt_write_g4 (image->bitmap, pdf_file->f);
    79 }
    82 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
    83 			     double x,
    84 			     double y,
    85 			     double width,
    86 			     double height,
    87 			     Bitmap *bitmap,
    88 			     bool ImageMask,
    89 			     double r, /* RGB fill color, only for ImageMask */
    90 			     double g,
    91 			     double b,
    92 			     bool BlackIs1)          /* boolean, typ. false */
    93 {
    94   struct pdf_g4_image *image;
    96   struct pdf_obj *stream;
    97   struct pdf_obj *stream_dict;
    98   struct pdf_obj *decode_parms;
   100   struct pdf_obj *content_stream;
   102   pdf_add_array_elem_unique (pdf_page->procset, pdf_new_name ("ImageB"));
   104   image = pdf_calloc (1, sizeof (struct pdf_g4_image));
   106   image->width = width;
   107   image->height = height;
   108   image->x = x;
   109   image->y = y;
   110   image->r = r;
   111   image->g = g;
   112   image->b = b;
   114   image->bitmap = bitmap;
   115   image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
   116   image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
   117   image->ImageMask = ImageMask;
   118   image->BlackIs1 = BlackIs1;
   120   stream_dict = pdf_new_obj (PT_DICTIONARY);
   122   stream = pdf_new_ind_ref (pdf_page->pdf_file,
   123 			    pdf_new_stream (pdf_page->pdf_file,
   124 					    stream_dict,
   125 					    & pdf_write_g4_fax_image_callback,
   126 					    image));
   128   strcpy (& image->XObject_name [0], "Im ");
   129   image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
   131   pdf_set_dict_entry (stream_dict, "Type",    pdf_new_name ("XObject"));
   132   pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
   133   pdf_set_dict_entry (stream_dict, "Name",    pdf_new_name (& image->XObject_name [0]));
   134   pdf_set_dict_entry (stream_dict, "Width",   pdf_new_integer (image->Columns));
   135   pdf_set_dict_entry (stream_dict, "Height",  pdf_new_integer (image->Rows));
   136   pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
   137   if (ImageMask)
   138     pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
   139   else
   140     pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
   142   decode_parms = pdf_new_obj (PT_DICTIONARY);
   144   pdf_set_dict_entry (decode_parms,
   145 		      "K",
   146 		      pdf_new_integer (-1));
   148   pdf_set_dict_entry (decode_parms,
   149 		      "Columns",
   150 		      pdf_new_integer (image->Columns));
   152   pdf_set_dict_entry (decode_parms,
   153 		      "Rows",
   154 		      pdf_new_integer (image->Rows));
   156   if (BlackIs1)
   157     pdf_set_dict_entry (decode_parms,
   158 			"BlackIs1",
   159 			pdf_new_bool (BlackIs1));
   161   pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
   163   /* the following will write the stream, using our callback function to
   164      get the actual data */
   165   pdf_write_ind_obj (pdf_page->pdf_file, stream);
   167   content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
   168 				    pdf_new_stream (pdf_page->pdf_file,
   169 						    pdf_new_obj (PT_DICTIONARY),
   170 						    & pdf_write_g4_content_callback,
   171 						    image));
   173   pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
   175   pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
   176 }