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