pdf_g4.c

changeset 59
e8821eb2fb08
child 60
ffb5b1e54eb2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pdf_g4.c	Thu Feb 20 12:16:00 2003 +0000
     1.3 @@ -0,0 +1,179 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +
     1.7 +
     1.8 +#include "libpdf.h"
     1.9 +#include "libpdf_util.h"
    1.10 +#include "libpdf_prim.h"
    1.11 +#include "libpdf_private.h"
    1.12 +
    1.13 +
    1.14 +struct pdf_g4_image
    1.15 +{
    1.16 +  unsigned long Columns;
    1.17 +  unsigned long Rows;
    1.18 +  unsigned long rowbytes;
    1.19 +  int BlackIs1;
    1.20 +  unsigned char *data;
    1.21 +  unsigned long len;
    1.22 +  char XObject_name [4];
    1.23 +};
    1.24 +
    1.25 +
    1.26 +char pdf_new_XObject (pdf_page_handle pdf_page, struct pdf_obj *ind_ref)
    1.27 +{
    1.28 +  char XObject_name [4] = "Im ";
    1.29 +
    1.30 +  XObject_name [2] = ++pdf_page->last_XObject_name;
    1.31 +  
    1.32 +  if (! pdf_page->XObject_dict)
    1.33 +    {
    1.34 +      pdf_page->XObject_dict = pdf_new_obj (PT_DICTIONARY);
    1.35 +      pdf_set_dict_entry (pdf_page->resources, "XObject", pdf_page->XObject_dict);
    1.36 +    }
    1.37 +
    1.38 +  pdf_set_dict_entry (pdf_page->XObject_dict, & XObject_name [0], ind_ref);
    1.39 +
    1.40 +  return (pdf_page->last_XObject_name);
    1.41 +}
    1.42 +
    1.43 +
    1.44 +void pdf_write_g4_content_callback (pdf_file_handle pdf_file,
    1.45 +				    struct pdf_obj *stream,
    1.46 +				    void *app_data)
    1.47 +{
    1.48 +  unsigned long width = (8.5 * 72);  /* full width of page */
    1.49 +  unsigned long height = (11 * 72);  /* full height of page */
    1.50 +  unsigned long x = 0;  /* 0 is left edge */
    1.51 +  unsigned long y = 0;  /* 0 is bottom edge */
    1.52 +  struct pdf_g4_image *image = app_data;
    1.53 +
    1.54 +  char str1 [100];
    1.55 +  char *str2 = "/";
    1.56 +  char *str3 = " Do\r\n";
    1.57 +
    1.58 +  /* width 0 0 height x y cm */
    1.59 +  sprintf (str1, "q %ld 0 0 %ld %ld %ld cm\r\n", width, height, x, y);
    1.60 +
    1.61 +  pdf_stream_write_data (pdf_file, stream, str1, strlen (str1));
    1.62 +  pdf_stream_write_data (pdf_file, stream, str2, strlen (str2));
    1.63 +  pdf_stream_write_data (pdf_file, stream, & image->XObject_name [0],
    1.64 +			 strlen (& image->XObject_name [0]));
    1.65 +  pdf_stream_write_data (pdf_file, stream, str3, strlen (str3));
    1.66 +}
    1.67 +
    1.68 +
    1.69 +void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file,
    1.70 +				      struct pdf_obj *stream,
    1.71 +				      void *app_data)
    1.72 +{
    1.73 +  struct pdf_g4_image *image = app_data;
    1.74 +
    1.75 +#if 1
    1.76 +  pdf_stream_write_data (pdf_file, stream, image->data, image->len);
    1.77 +#else
    1.78 +  unsigned long row = 0;
    1.79 +  unsigned char *ref;
    1.80 +  unsigned char *raw;
    1.81 +
    1.82 +  ref = NULL;
    1.83 +  raw = image->data;
    1.84 +
    1.85 +  while (row < image->Rows)
    1.86 +    {
    1.87 +      pdf_stream_write_data (pdf_file, stream, raw, image->rowbytes);
    1.88 +
    1.89 +      row++;
    1.90 +      ref = raw;
    1.91 +      raw += image->rowbytes;
    1.92 +    }
    1.93 +  /* $$$ generate and write EOFB code */
    1.94 +  /* $$$ flush any remaining buffered bits */
    1.95 +#endif
    1.96 +}
    1.97 +
    1.98 +
    1.99 +void pdf_write_g4_fax_image (pdf_page_handle pdf_page,
   1.100 +			     unsigned long Columns,
   1.101 +			     unsigned long Rows,
   1.102 +			     unsigned long rowbytes,
   1.103 +			     int ImageMask,
   1.104 +			     int BlackIs1,          /* boolean, typ. false */
   1.105 +			     unsigned char *data,
   1.106 +			     unsigned long len)
   1.107 +{
   1.108 +  struct pdf_g4_image *image;
   1.109 +
   1.110 +  struct pdf_obj *stream;
   1.111 +  struct pdf_obj *stream_dict;
   1.112 +  struct pdf_obj *decode_parms;
   1.113 +
   1.114 +  struct pdf_obj *content_stream;
   1.115 +
   1.116 +  image = pdf_calloc (sizeof (struct pdf_g4_image));
   1.117 +
   1.118 +  image->Columns = Columns;
   1.119 +  image->Rows = Rows;
   1.120 +  image->rowbytes = rowbytes;
   1.121 +  image->BlackIs1 = BlackIs1;
   1.122 +  image->data = data;
   1.123 +  image->len = len;
   1.124 +
   1.125 +  stream_dict = pdf_new_obj (PT_DICTIONARY);
   1.126 +
   1.127 +  stream = pdf_new_ind_ref (pdf_page->pdf_file,
   1.128 +			    pdf_new_stream (pdf_page->pdf_file,
   1.129 +					    stream_dict,
   1.130 +					    & pdf_write_g4_fax_image_callback,
   1.131 +					    image));
   1.132 +
   1.133 +  strcpy (& image->XObject_name [0], "Im ");
   1.134 +  image->XObject_name [2] = pdf_new_XObject (pdf_page, stream);
   1.135 +
   1.136 +  pdf_set_dict_entry (stream_dict, "Type",    pdf_new_name ("XObject"));
   1.137 +  pdf_set_dict_entry (stream_dict, "Subtype", pdf_new_name ("Image"));
   1.138 +  pdf_set_dict_entry (stream_dict, "Name",    pdf_new_name (& image->XObject_name [0]));
   1.139 +  pdf_set_dict_entry (stream_dict, "Width",   pdf_new_integer (Columns));
   1.140 +  pdf_set_dict_entry (stream_dict, "Height",  pdf_new_integer (Rows));
   1.141 +  pdf_set_dict_entry (stream_dict, "BitsPerComponent", pdf_new_integer (1));
   1.142 +  if (ImageMask)
   1.143 +    pdf_set_dict_entry (stream_dict, "ImageMask", pdf_new_bool (ImageMask));
   1.144 +  else
   1.145 +    pdf_set_dict_entry (stream_dict, "ColorSpace", pdf_new_name ("DeviceGray"));
   1.146 +
   1.147 +  decode_parms = pdf_new_obj (PT_DICTIONARY);
   1.148 +
   1.149 +  pdf_set_dict_entry (decode_parms,
   1.150 +		      "K",
   1.151 +		      pdf_new_integer (-1));
   1.152 +
   1.153 +  pdf_set_dict_entry (decode_parms,
   1.154 +		      "Columns",
   1.155 +		      pdf_new_integer (Columns));
   1.156 +
   1.157 +  pdf_set_dict_entry (decode_parms,
   1.158 +		      "Rows",
   1.159 +		      pdf_new_integer (Rows));
   1.160 +
   1.161 +  if (BlackIs1)
   1.162 +    pdf_set_dict_entry (decode_parms,
   1.163 +			"BlackIs1",
   1.164 +			pdf_new_bool (BlackIs1));
   1.165 +
   1.166 +  pdf_stream_add_filter (stream, "CCITTFaxDecode", decode_parms);
   1.167 +
   1.168 +  /* the following will write the stream, using our callback function to
   1.169 +     get the actual data */
   1.170 +  pdf_write_ind_obj (pdf_page->pdf_file, stream);
   1.171 +
   1.172 +  content_stream = pdf_new_ind_ref (pdf_page->pdf_file,
   1.173 +				    pdf_new_stream (pdf_page->pdf_file,
   1.174 +						    pdf_new_obj (PT_DICTIONARY),
   1.175 +						    & pdf_write_g4_content_callback,
   1.176 +						    image));
   1.177 +
   1.178 +  pdf_set_dict_entry (pdf_page->page_dict, "Contents", content_stream);
   1.179 +
   1.180 +  pdf_write_ind_obj (pdf_page->pdf_file, content_stream);
   1.181 +}
   1.182 +