bitblt_g4.c

Fri, 21 Feb 2003 09:12:05 +0000

author
eric
date
Fri, 21 Feb 2003 09:12:05 +0000
changeset 65
5acb5b549729
parent 64
151394412eba
child 66
6e0551b59dba
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: bitblt_g4.c,v 1.4 2003/02/21 01:01:33 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   unsigned long Columns;
    45   unsigned long Rows;
    46   int BlackIs1;
    47   Bitmap *bitmap;
    48   char XObject_name [4];
    49 };
    52 char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref)
    53 {
    54   char XObject_name [4] = "Im ";
    56   XObject_name [2] = ++pdf_page->last_XObject_name;
    58   if (! pdf_page->XObject_dict)
    59     {
    60       pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY);
    61       pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict);
    62     }
    64   pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref);
    66   return (pdf_page->last_XObject_name);
    67 }
    70 void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
    71 				    struct pdf_obj *stream,
    72 				    void *app_data)
    73 {
    74   struct pdf_g4_image *image = app_data;
    76   char str1 [100];
    77   char *str2 = "/";
    78   char *str3 = " Do Q\r\n";
    80   /* width 0 0 height x y cm */
    81   sprintf (str1, "q %g 0 0 %g %g %g cm\r\n",
    82 	   image->width, image->height,
    83 	   image->x, image->y);
    85   pdf_stream_write_data (pdf_file, stream, str1, strlen (str1));
    86   pdf_stream_write_data (pdf_file, stream, str2, strlen (str2));
    87   pdf_stream_write_data (pdf_file, stream, & image->XObject_name [0],
    88 			 strlen (& image->XObject_name [0]));
    89   pdf_stream_write_data (pdf_file, stream, str3, strlen (str3));
    90 }
    93 void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
    94 				      struct pdf_obj *stream,
    95 				      void *app_data)
    96 {
    97   struct pdf_g4_image *image = app_data;
    99 #if 0
   100   pdf_stream_write_data (pdf_file, stream, image->data, image->len);
   101 #else
   102   unsigned long row = 0;
   103   word_type *ref;
   104   word_type *raw;
   106   ref = NULL;
   107   raw = image->bitmap->bits;
   109   while (row < image->Rows)
   110     {
   111       pdf_stream_write_data (pdf_file, stream, (uint8_t *) raw,
   112 			     image->bitmap->row_words * sizeof (word_type));
   114       row++;
   115       ref = raw;
   116       raw += image->bitmap->row_words;
   117     }
   118   /* $$$ generate and write EOFB code */
   119   /* $$$ flush any remaining buffered bits */
   120 #endif
   121 }
   124 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
   125 			     double x,
   126 			     double y,
   127 			     double width,
   128 			     double height,
   129 			     Bitmap *bitmap,
   130 			     int ImageMask,
   131 			     int BlackIs1)          /* boolean, typ. false */
   132 {
   133   struct pdf_g4_image *image;
   135   struct pdf_obj *stream;
   136   struct pdf_obj *stream_dict;
   137   struct pdf_obj *decode_parms;
   139   struct pdf_obj *content_stream;
   141   image = pdf_calloc (sizeof (struct pdf_g4_image));
   143   image->width = width;
   144   image->height = height;
   145   image->x = x;
   146   image->y = y;
   148   image->bitmap = bitmap;
   149   image->Columns = bitmap->rect.max.x - bitmap->rect.min.x;
   150   image->Rows = bitmap->rect.max.y - bitmap->rect.min.y;
   151   image->BlackIs1 = BlackIs1;
   153   stream_dict = pdf_new_obj (PT_DICTIONARY);
   155   stream = pdf_new_ind_ref (pdf_page->pdf_file,
   156 			    pdf_new_stream (pdf_page->pdf_file,
   157 					    stream_dict,
   158 					    & pdf_write_g4_fax_image_callback,
   159 					    image));
   161   strcpy (& image->XObject_name [0], "Im ");
   162   image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
   164   pdf_set_dict_entry (stream_dict, "Type",    pdf_new_name ("XObject"));
   165   pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
   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->Columns));
   168   pdf_set_dict_entry (stream_dict, "Height",  pdf_new_integer (image->Rows));
   169   pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
   170   if (ImageMask)
   171     pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
   172   else
   173     pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
   175   decode_parms = pdf_new_obj (PT_DICTIONARY);
   177   pdf_set_dict_entry (decode_parms,
   178 		      "K",
   179 		      pdf_new_integer (-1));
   181   pdf_set_dict_entry (decode_parms,
   182 		      "Columns",
   183 		      pdf_new_integer (image->Columns));
   185   pdf_set_dict_entry (decode_parms,
   186 		      "Rows",
   187 		      pdf_new_integer (image->Rows));
   189   if (BlackIs1)
   190     pdf_set_dict_entry (decode_parms,
   191 			"BlackIs1",
   192 			pdf_new_bool (BlackIs1));
   194   pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
   196   /* the following will write the stream, using our callback function to
   197      get the actual data */
   198   pdf_write_ind_obj (pdf_page->pdf_file, stream);
   200   content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
   201 				    pdf_new_stream (pdf_page->pdf_file,
   202 						    pdf_new_obj (PT_DICTIONARY),
   203 						    & pdf_write_g4_content_callback,
   204 						    image));
   206   pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
   208   pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
   209 }