bitblt_g4.c

Fri, 21 Feb 2003 09:25:47 +0000

author
eric
date
Fri, 21 Feb 2003 09:25:47 +0000
changeset 66
6e0551b59dba
parent 64
151394412eba
child 67
7add7411c1c2
permissions
-rw-r--r--

implemented ImageMask fill color arguments to pdf_write_g4_fax_image().

     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: bitblt_g4.c,v 1.5 2003/02/21 01:25:47 eric Exp $
     8  * Copyright 2001, 2002, 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 <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_g4_image
    41 {
    42   double width, height;
    43   double x, y;
    44   double r, g, b;  /* fill color, only for ImageMask */
    45   unsigned long Columns;
    46   unsigned long Rows;
    47   bool ImageMask;
    48   bool BlackIs1;
    49   Bitmap *bitmap;
    50   char XObject_name [4];
    51 };
    54 char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref)
    55 {
    56   char XObject_name [4] = "Im ";
    58   XObject_name [2] = ++pdf_page->last_XObject_name;
    60   if (! pdf_page->XObject_dict)
    61     {
    62       pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY);
    63       pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict);
    64     }
    66   pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref);
    68   return (pdf_page->last_XObject_name);
    69 }
    72 void pdf_write_g4_content_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   /* transformation matrix is: width 0 0 height x y cm */
    79   pdf_stream_printf (pdf_file, stream, "q %g 0 0 %g %g %g cm ",
    80 		     image->width, image->height,
    81 		     image->x, image->y);
    82   if (image->ImageMask)
    83     pdf_stream_printf (pdf_file, stream, "%g %g %g rg ",
    84 		       image->r, image->g, image->b);
    86   pdf_stream_printf (pdf_file, stream, "/%s Do Q\r\n",
    87 		     image->XObject_name);
    88 }
    91 void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
    92 				      struct pdf_obj *stream,
    93 				      void *app_data)
    94 {
    95   struct pdf_g4_image *image = app_data;
    97 #if 0
    98   pdf_stream_write_data (pdf_file, stream, image->data, image->len);
    99 #else
   100   unsigned long row = 0;
   101   word_type *ref;
   102   word_type *raw;
   104   ref = NULL;
   105   raw = image->bitmap->bits;
   107   while (row < image->Rows)
   108     {
   109       pdf_stream_write_data (pdf_file, stream, (uint8_t *) raw,
   110 			     image->bitmap->row_words * sizeof (word_type));
   112       row++;
   113       ref = raw;
   114       raw += image->bitmap->row_words;
   115     }
   116   /* $$$ generate and write EOFB code */
   117   /* $$$ flush any remaining buffered bits */
   118 #endif
   119 }
   122 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
   123 			     double x,
   124 			     double y,
   125 			     double width,
   126 			     double height,
   127 			     Bitmap *bitmap,
   128 			     bool ImageMask,
   129 			     double r, /* RGB fill color, only for ImageMask */
   130 			     double g,
   131 			     double b,
   132 			     bool BlackIs1)          /* boolean, typ. false */
   133 {
   134   struct pdf_g4_image *image;
   136   struct pdf_obj *stream;
   137   struct pdf_obj *stream_dict;
   138   struct pdf_obj *decode_parms;
   140   struct pdf_obj *content_stream;
   142   image = pdf_calloc (sizeof (struct pdf_g4_image));
   144   image->width = width;
   145   image->height = height;
   146   image->x = x;
   147   image->y = y;
   148   image->r = r;
   149   image->g = g;
   150   image->b = b;
   152   image->bitmap = bitmap;
   153   image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
   154   image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
   155   image->ImageMask = ImageMask;
   156   image->BlackIs1 = BlackIs1;
   158   stream_dict = pdf_new_obj (PT_DICTIONARY);
   160   stream = pdf_new_ind_ref (pdf_page->pdf_file,
   161 			    pdf_new_stream (pdf_page->pdf_file,
   162 					    stream_dict,
   163 					    & pdf_write_g4_fax_image_callback,
   164 					    image));
   166   strcpy (& image->XObject_name [0], "Im ");
   167   image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
   169   pdf_set_dict_entry (stream_dict, "Type",    pdf_new_name ("XObject"));
   170   pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
   171   pdf_set_dict_entry (stream_dict, "Name",    pdf_new_name (& image->XObject_name [0]));
   172   pdf_set_dict_entry (stream_dict, "Width",   pdf_new_integer (image->Columns));
   173   pdf_set_dict_entry (stream_dict, "Height",  pdf_new_integer (image->Rows));
   174   pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
   175   if (ImageMask)
   176     pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
   177   else
   178     pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
   180   decode_parms = pdf_new_obj (PT_DICTIONARY);
   182   pdf_set_dict_entry (decode_parms,
   183 		      "K",
   184 		      pdf_new_integer (-1));
   186   pdf_set_dict_entry (decode_parms,
   187 		      "Columns",
   188 		      pdf_new_integer (image->Columns));
   190   pdf_set_dict_entry (decode_parms,
   191 		      "Rows",
   192 		      pdf_new_integer (image->Rows));
   194   if (BlackIs1)
   195     pdf_set_dict_entry (decode_parms,
   196 			"BlackIs1",
   197 			pdf_new_bool (BlackIs1));
   199   pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
   201   /* the following will write the stream, using our callback function to
   202      get the actual data */
   203   pdf_write_ind_obj (pdf_page->pdf_file, stream);
   205   content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
   206 				    pdf_new_stream (pdf_page->pdf_file,
   207 						    pdf_new_obj (PT_DICTIONARY),
   208 						    & pdf_write_g4_content_callback,
   209 						    image));
   211   pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
   213   pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
   214 }