pdf_g4.c

Thu, 20 Feb 2003 12:16:00 +0000

author
eric
date
Thu, 20 Feb 2003 12:16:00 +0000
changeset 59
e8821eb2fb08
child 60
ffb5b1e54eb2
permissions
-rw-r--r--

my own PDF routines to replace Panda.

     1 #include <stdio.h>
     2 #include <string.h>
     5 #include "libpdf.h"
     6 #include "libpdf_util.h"
     7 #include "libpdf_prim.h"
     8 #include "libpdf_private.h"
    11 struct pdf_g4_image
    12 {
    13   unsigned long Columns;
    14   unsigned long Rows;
    15   unsigned long rowbytes;
    16   int BlackIs1;
    17   unsigned char *data;
    18   unsigned long len;
    19   char XObject_name [4];
    20 };
    23 char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref)
    24 {
    25   char XObject_name [4] = "Im ";
    27   XObject_name [2] = ++pdf_page->last_XObject_name;
    29   if (! pdf_page->XObject_dict)
    30     {
    31       pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY);
    32       pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict);
    33     }
    35   pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref);
    37   return (pdf_page->last_XObject_name);
    38 }
    41 void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
    42 				    struct pdf_obj *stream,
    43 				    void *app_data)
    44 {
    45   unsigned long width = (8.5 * 72);  /* full width of page */
    46   unsigned long height = (11 * 72);  /* full height of page */
    47   unsigned long x = 0;  /* 0 is left edge */
    48   unsigned long y = 0;  /* 0 is bottom edge */
    49   struct pdf_g4_image *image = app_data;
    51   char str1 [100];
    52   char *str2 = "/";
    53   char *str3 = " Do\r\n";
    55   /* width 0 0 height x y cm */
    56   sprintf (str1, "q %ld 0 0 %ld %ld %ld cm\r\n", width, height, x, y);
    58   pdf_stream_write_data (pdf_file, stream, str1, strlen (str1));
    59   pdf_stream_write_data (pdf_file, stream, str2, strlen (str2));
    60   pdf_stream_write_data (pdf_file, stream, & image->XObject_name [0],
    61 			 strlen (& image->XObject_name [0]));
    62   pdf_stream_write_data (pdf_file, stream, str3, strlen (str3));
    63 }
    66 void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
    67 				      struct pdf_obj *stream,
    68 				      void *app_data)
    69 {
    70   struct pdf_g4_image *image = app_data;
    72 #if 1
    73   pdf_stream_write_data (pdf_file, stream, image->data, image->len);
    74 #else
    75   unsigned long row = 0;
    76   unsigned char *ref;
    77   unsigned char *raw;
    79   ref = NULL;
    80   raw = image->data;
    82   while (row < image->Rows)
    83     {
    84       pdf_stream_write_data (pdf_file, stream, raw, image->rowbytes);
    86       row++;
    87       ref = raw;
    88       raw += image->rowbytes;
    89     }
    90   /* $$$ generate and write EOFB code */
    91   /* $$$ flush any remaining buffered bits */
    92 #endif
    93 }
    96 void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
    97 			     unsigned long Columns,
    98 			     unsigned long Rows,
    99 			     unsigned long rowbytes,
   100 			     int ImageMask,
   101 			     int BlackIs1,          /* boolean, typ. false */
   102 			     unsigned char *data,
   103 			     unsigned long len)
   104 {
   105   struct pdf_g4_image *image;
   107   struct pdf_obj *stream;
   108   struct pdf_obj *stream_dict;
   109   struct pdf_obj *decode_parms;
   111   struct pdf_obj *content_stream;
   113   image = pdf_calloc (sizeof (struct pdf_g4_image));
   115   image->Columns = Columns;
   116   image->Rows = Rows;
   117   image->rowbytes = rowbytes;
   118   image->BlackIs1 = BlackIs1;
   119   image->data = data;
   120   image->len = len;
   122   stream_dict = pdf_new_obj (PT_DICTIONARY);
   124   stream = pdf_new_ind_ref (pdf_page->pdf_file,
   125 			    pdf_new_stream (pdf_page->pdf_file,
   126 					    stream_dict,
   127 					    & pdf_write_g4_fax_image_callback,
   128 					    image));
   130   strcpy (& image->XObject_name [0], "Im ");
   131   image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
   133   pdf_set_dict_entry (stream_dict, "Type",    pdf_new_name ("XObject"));
   134   pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
   135   pdf_set_dict_entry (stream_dict, "Name",    pdf_new_name (& image->XObject_name [0]));
   136   pdf_set_dict_entry (stream_dict, "Width",   pdf_new_integer (Columns));
   137   pdf_set_dict_entry (stream_dict, "Height",  pdf_new_integer (Rows));
   138   pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
   139   if (ImageMask)
   140     pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
   141   else
   142     pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
   144   decode_parms = pdf_new_obj (PT_DICTIONARY);
   146   pdf_set_dict_entry (decode_parms,
   147 		      "K",
   148 		      pdf_new_integer (-1));
   150   pdf_set_dict_entry (decode_parms,
   151 		      "Columns",
   152 		      pdf_new_integer (Columns));
   154   pdf_set_dict_entry (decode_parms,
   155 		      "Rows",
   156 		      pdf_new_integer (Rows));
   158   if (BlackIs1)
   159     pdf_set_dict_entry (decode_parms,
   160 			"BlackIs1",
   161 			pdf_new_bool (BlackIs1));
   163   pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
   165   /* the following will write the stream, using our callback function to
   166      get the actual data */
   167   pdf_write_ind_obj (pdf_page->pdf_file, stream);
   169   content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
   170 				    pdf_new_stream (pdf_page->pdf_file,
   171 						    pdf_new_obj (PT_DICTIONARY),
   172 						    & pdf_write_g4_content_callback,
   173 						    image));
   175   pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
   177   pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
   178 }