t2p.c

changeset 28
ba8313d18bd3
parent 26
4368c5fd9242
child 29
c904ffd6a1cf
     1.1 --- a/t2p.c	Tue Jan 01 03:46:08 2002 +0000
     1.2 +++ b/t2p.c	Tue Jan 01 05:02:44 2002 +0000
     1.3 @@ -1,7 +1,7 @@
     1.4  /*
     1.5   * tiffg4: reencode a bilevel TIFF file as a single-strip TIFF Class F Group 4
     1.6   * Main program
     1.7 - * $Id: t2p.c,v 1.8 2001/12/31 19:44:40 eric Exp $
     1.8 + * $Id: t2p.c,v 1.9 2001/12/31 21:02:44 eric Exp $
     1.9   * Copyright 2001 Eric Smith <eric@brouhaha.com>
    1.10   *
    1.11   * This program is free software; you can redistribute it and/or modify
    1.12 @@ -22,6 +22,8 @@
    1.13  
    1.14  
    1.15  #include <stdio.h>
    1.16 +#include <stdlib.h>
    1.17 +#include <unistd.h>
    1.18  #include <tiffio.h>
    1.19  #include <panda/functions.h>
    1.20  #include <panda/constants.h>
    1.21 @@ -33,6 +35,13 @@
    1.22  #include "tiff2pdf.h"
    1.23  
    1.24  
    1.25 +#define POINTS_PER_INCH 72
    1.26 +
    1.27 +/* page size limited by Acrobat Reader to 45 inches on a side */
    1.28 +#define PAGE_MAX_INCHES 45
    1.29 +#define PAGE_MAX_POINTS (PAGE_MAX_INCHES * POINTS_PER_INCH)
    1.30 +
    1.31 +
    1.32  typedef struct output_file_t
    1.33  {
    1.34    struct output_file_t *next;
    1.35 @@ -166,10 +175,23 @@
    1.36    u16 planar_config;
    1.37    u16 resolution_unit;
    1.38    float x_resolution, y_resolution;
    1.39 +  int width_points, height_points;  /* really 1/72 inch units rather than
    1.40 +				       points */
    1.41 +
    1.42  
    1.43    char *buffer;
    1.44    u32 row;
    1.45  
    1.46 +  panda_page *page;
    1.47 +
    1.48 +  int tiff_temp_fd;
    1.49 +  char tiff_temp_fn [] = "/var/tmp/tiff2pdf-XXXXXX\0";
    1.50 +  TIFF *tiff_temp;
    1.51 +  
    1.52 +  char pagesize [26];  /* Needs to hold two ints of four characters (0..3420),
    1.53 +			  two zeros, three spaces, two brackets, and a NULL.
    1.54 +                          Added an extra ten characters just in case. */
    1.55 +
    1.56    if (! TIFFSetDirectory (in, image - 1))
    1.57      {
    1.58        fprintf (stderr, "can't find page %d of input file\n", image);
    1.59 @@ -243,21 +265,33 @@
    1.60        goto fail;
    1.61      }
    1.62  
    1.63 -#if 0
    1.64 -  TIFFSetField (out->pdf, TIFFTAG_IMAGELENGTH, image_length);
    1.65 -  TIFFSetField (out->pdf, TIFFTAG_IMAGEWIDTH, image_width);
    1.66 -  TIFFSetField (out->pdf, TIFFTAG_PLANARCONFIG, planar_config);
    1.67 +  tiff_temp_fd = mkstemp (tiff_temp_fn);
    1.68 +  if (tiff_temp_fd < 0)
    1.69 +    {
    1.70 +      fprintf (stderr, "can't create temporary TIFF file\n");
    1.71 +      goto fail;
    1.72 +    }
    1.73  
    1.74 -  TIFFSetField (out->pdf, TIFFTAG_ROWSPERSTRIP, image_length);
    1.75 +  tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w");
    1.76 +  if (! out)
    1.77 +    {
    1.78 +      fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn);
    1.79 +      goto fail;
    1.80 +    }
    1.81  
    1.82 -  TIFFSetField (out->pdf, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
    1.83 -  TIFFSetField (out->pdf, TIFFTAG_XRESOLUTION, x_resolution);
    1.84 -  TIFFSetField (out->pdf, TIFFTAG_YRESOLUTION, y_resolution);
    1.85 +  TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, image_length);
    1.86 +  TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, image_width);
    1.87 +  TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config);
    1.88 +
    1.89 +  TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, image_length);
    1.90  
    1.91 -  TIFFSetField (out->pdf, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
    1.92 -  TIFFSetField (out->pdf, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
    1.93 -  TIFFSetField (out->pdf, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
    1.94 -#endif
    1.95 +  TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
    1.96 +  TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, x_resolution);
    1.97 +  TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, y_resolution);
    1.98 +
    1.99 +  TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   1.100 +  TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   1.101 +  TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   1.102  
   1.103    buffer = _TIFFmalloc (TIFFScanlineSize (in));
   1.104    if (! buffer)
   1.105 @@ -269,12 +303,36 @@
   1.106    for (row = 0; row < image_length; row++)
   1.107      {
   1.108        TIFFReadScanline (in, buffer, row, 0);
   1.109 -#if 0
   1.110 -      TIFFWriteScanline (out->pdf, buffer, row, 0);
   1.111 -#endif
   1.112 +      TIFFWriteScanline (tiff_temp, buffer, row, 0);
   1.113      }
   1.114  
   1.115    _TIFFfree (buffer);
   1.116 +  TIFFClose (tiff_temp);
   1.117 +
   1.118 +  height_points = (image_width / x_resolution) * POINTS_PER_INCH;
   1.119 +  width_points = (image_length / y_resolution) * POINTS_PER_INCH;
   1.120 +
   1.121 +  if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
   1.122 +    {
   1.123 +      fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   1.124 +      goto fail;
   1.125 +    }
   1.126 +
   1.127 +  printf ("height_points %d, width_points %d\n", height_points, width_points);
   1.128 +
   1.129 +  sprintf (pagesize, "[0 0 %d %d]", width_points, height_points);
   1.130 +
   1.131 +  page = panda_newpage (out->pdf, pagesize);
   1.132 +  panda_imagebox (out->pdf,
   1.133 +		  page,
   1.134 +		  0, /* top */
   1.135 +		  0, /* left */
   1.136 +		  height_points, /* bottom */
   1.137 +		  width_points, /* right */
   1.138 +		  tiff_temp_fn,
   1.139 +		  panda_image_tiff);
   1.140 +
   1.141 +  unlink (tiff_temp_fn);
   1.142  
   1.143    return (1);
   1.144