pdf_g4.c

Wed, 12 Mar 2003 10:59:37 +0000

author
eric
date
Wed, 12 Mar 2003 10:59:37 +0000
changeset 111
966310797665
parent 102
f6c042f4dadb
child 118
c22e1c0a64fd
permissions
-rw-r--r--

*** empty log message ***

     1 /*
     2  * t2p: Create a PDF file from the contents of one or more TIFF
     3  *      bilevel image files.  The images in the resulting PDF file
     4  *      will be compressed using ITU-T T.6 (G4) fax encoding.
     5  *
     6  * PDF routines
     7  * $Id: pdf_g4.c,v 1.12 2003/03/11 23:53:55 eric Exp $
     8  * Copyright 2003 Eric Smith <eric@brouhaha.com>
     9  *
    10  * This program is free software; you can redistribute it and/or modify
    11  * it under the terms of the GNU General Public License version 2 as
    12  * published by the Free Software Foundation.  Note that permission is
    13  * not granted to redistribute this program under the terms of any
    14  * other version of the General Public License.
    15  *
    16  * This program is distributed in the hope that it will be useful,
    17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19  * GNU General Public License for more details.
    20  *
    21  * You should have received a copy of the GNU General Public License
    22  * along with this program; if not, write to the Free Software
    23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    24  */
    27 #include <stdbool.h>
    28 #include <stdint.h>
    29 #include <stdio.h>
    30 #include <stdlib.h>
    31 #include <string.h>
    34 #include "bitblt.h"
    35 #include "pdf.h"
    36 #include "pdf_util.h"
    37 #include "pdf_prim.h"
    38 #include "pdf_private.h"
    41 struct pdf_g4_image
    42 {
    43   double width, height;
    44   double x, y;
    45   double r, g, b;  /* fill color, only for ImageMask */
    46   unsigned long Columns;
    47   unsigned long Rows;
    48   bool ImageMask;
    49   bool BlackIs1;
    50   Bitmap *bitmap;
    51   char XObject_name [4];
    52 };
    55 static void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
    56 					   struct pdf_obj *stream,
    57 					   void *app_data)
    58 {
    59   struct pdf_g4_image *image = app_data;
    61   /* transformation matrix is: width 0 0 height x y cm */
    62   pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
    63 		     image->width, image->height,
    64 		     image->x, image->y);
    65   if (image->ImageMask)
    66     pdf_stream_printf (pdf_file, stream, "%g %g %g rg ",
    67 		       image->r, image->g, image->b);
    69   pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
    70 		     image->XObject_name);
    71 }
    74 static void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
    75 					     struct pdf_obj *stream,
    76 					     void *app_data)
    77 {
    78   struct pdf_g4_image *image = app_data;
    80   bitblt_write_g4 (image->bitmap, pdf_file->f);
    81 }
    84 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
    85 			     double x,
    86 			     double y,
    87 			     double width,
    88 			     double height,
    89 			     Bitmap *bitmap,
    90 			     bool ImageMask,
    91 			     double r, /* RGB fill color, only for ImageMask */
    92 			     double g,
    93 			     double b,
    94 			     bool BlackIs1)          /* boolean, typ. false */
    95 {
    96   struct pdf_g4_image *image;
    98   struct pdf_obj *stream;
    99   struct pdf_obj *stream_dict;
   100   struct pdf_obj *decode_parms;
   102   struct pdf_obj *content_stream;
   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 }