1.1 diff -r 3fe049d83e22 -r 752599b50ff3 tumble_tiff.c 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/tumble_tiff.c Thu Mar 20 06:54:08 2003 +0000 1.4 @@ -0,0 +1,351 @@ 1.5 +/* 1.6 + * tumble: build a PDF file from image files 1.7 + * 1.8 + * $Id: tumble_tiff.c,v 1.1 2003/03/19 22:54:08 eric Exp $ 1.9 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 1.10 + * 1.11 + * This program is free software; you can redistribute it and/or modify 1.12 + * it under the terms of the GNU General Public License version 2 as 1.13 + * published by the Free Software Foundation. Note that permission is 1.14 + * not granted to redistribute this program under the terms of any 1.15 + * other version of the General Public License. 1.16 + * 1.17 + * This program is distributed in the hope that it will be useful, 1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.20 + * GNU General Public License for more details. 1.21 + * 1.22 + * You should have received a copy of the GNU General Public License 1.23 + * along with this program; if not, write to the Free Software 1.24 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA 1.25 + */ 1.26 + 1.27 + 1.28 +#include <stdbool.h> 1.29 +#include <stdint.h> 1.30 +#include <stdio.h> 1.31 +#include <stdlib.h> 1.32 +#include <string.h> 1.33 + 1.34 +#include <tiffio.h> 1.35 +#define TIFF_REVERSE_BITS 1.36 + 1.37 + 1.38 +#include "semantics.h" 1.39 +#include "tumble.h" 1.40 +#include "bitblt.h" 1.41 +#include "pdf.h" 1.42 +#include "tumble_input.h" 1.43 + 1.44 + 1.45 +TIFF *tiff_in; 1.46 + 1.47 + 1.48 +#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) 1.49 + 1.50 + 1.51 +bool close_tiff_input_file (void) 1.52 +{ 1.53 + TIFFClose (tiff_in); 1.54 + return (1); 1.55 +} 1.56 + 1.57 + 1.58 +bool open_tiff_input_file (FILE *f, char *name) 1.59 +{ 1.60 + uint8_t buf [2]; 1.61 + size_t l; 1.62 + 1.63 + l = fread (& buf [0], 1, sizeof (buf), f); 1.64 + if (l != sizeof (buf)) 1.65 + return (0); 1.66 + 1.67 + rewind (f); 1.68 + 1.69 + if ((buf [0] != 0x49) || (buf [1] != 0x49)) 1.70 + return (0); 1.71 + 1.72 + /* $$$ should we dup the file descriptor here, so that later closing f 1.73 + won't cause problems? */ 1.74 + tiff_in = TIFFFdOpen (fileno (f), name, "r"); 1.75 + if (! tiff_in) 1.76 + { 1.77 + fprintf (stderr, "can't open input file '%s'\n", name); 1.78 + return (0); 1.79 + } 1.80 + return (1); 1.81 +} 1.82 + 1.83 + 1.84 +bool last_tiff_input_page (void) 1.85 +{ 1.86 + return (TIFFLastDirectory (tiff_in)); 1.87 +} 1.88 + 1.89 + 1.90 +bool get_tiff_image_info (int image, 1.91 + input_attributes_t input_attributes, 1.92 + image_info_t *image_info) 1.93 +{ 1.94 + uint32_t image_height, image_width; 1.95 + uint16_t samples_per_pixel; 1.96 + uint16_t bits_per_sample; 1.97 + uint16_t planar_config; 1.98 + 1.99 + uint16_t resolution_unit; 1.100 + float x_resolution, y_resolution; 1.101 + 1.102 + double dest_x_resolution, dest_y_resolution; 1.103 + 1.104 +#ifdef CHECK_DEPTH 1.105 + uint32_t image_depth; 1.106 +#endif 1.107 + 1.108 + if (! TIFFSetDirectory (tiff_in, image - 1)) 1.109 + { 1.110 + fprintf (stderr, "can't find page %d of input file\n", image); 1.111 + return (0); 1.112 + } 1.113 + if (1 != TIFFGetField (tiff_in, TIFFTAG_IMAGELENGTH, & image_height)) 1.114 + { 1.115 + fprintf (stderr, "can't get image height\n"); 1.116 + return (0); 1.117 + } 1.118 + if (1 != TIFFGetField (tiff_in, TIFFTAG_IMAGEWIDTH, & image_width)) 1.119 + { 1.120 + fprintf (stderr, "can't get image width\n"); 1.121 + return (0); 1.122 + } 1.123 + 1.124 + if (1 != TIFFGetField (tiff_in, TIFFTAG_SAMPLESPERPIXEL, & samples_per_pixel)) 1.125 + { 1.126 + fprintf (stderr, "can't get samples per pixel\n"); 1.127 + return (0); 1.128 + } 1.129 + 1.130 +#ifdef CHECK_DEPTH 1.131 + if (1 != TIFFGetField (tiff_in, TIFFTAG_IMAGEDEPTH, & image_depth)) 1.132 + { 1.133 + fprintf (stderr, "can't get image depth\n"); 1.134 + return (0); 1.135 + } 1.136 +#endif 1.137 + 1.138 + if (1 != TIFFGetField (tiff_in, TIFFTAG_BITSPERSAMPLE, & bits_per_sample)) 1.139 + { 1.140 + fprintf (stderr, "can't get bits per sample\n"); 1.141 + return (0); 1.142 + } 1.143 + 1.144 + if (1 != TIFFGetField (tiff_in, TIFFTAG_PLANARCONFIG, & planar_config)) 1.145 + planar_config = 1; 1.146 + 1.147 + if (1 != TIFFGetField (tiff_in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit)) 1.148 + resolution_unit = 2; 1.149 + if (1 != TIFFGetField (tiff_in, TIFFTAG_XRESOLUTION, & x_resolution)) 1.150 + x_resolution = 300; 1.151 + if (1 != TIFFGetField (tiff_in, TIFFTAG_YRESOLUTION, & y_resolution)) 1.152 + y_resolution = 300; 1.153 + 1.154 + if (samples_per_pixel != 1) 1.155 + { 1.156 + fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel); 1.157 + return (0); 1.158 + } 1.159 + 1.160 +#ifdef CHECK_DEPTH 1.161 + if (image_depth != 1) 1.162 + { 1.163 + fprintf (stderr, "image depth %u, must be 1\n", image_depth); 1.164 + return (0); 1.165 + } 1.166 +#endif 1.167 + 1.168 + if (bits_per_sample != 1) 1.169 + { 1.170 + fprintf (stderr, "bits per sample %u, must be 1\n", bits_per_sample); 1.171 + return (0); 1.172 + } 1.173 + 1.174 + if (planar_config != 1) 1.175 + { 1.176 + fprintf (stderr, "planar config %u, must be 1\n", planar_config); 1.177 + return (0); 1.178 + } 1.179 + 1.180 + if (input_attributes.has_resolution) 1.181 + { 1.182 + x_resolution = input_attributes.x_resolution; 1.183 + y_resolution = input_attributes.y_resolution; 1.184 + } 1.185 + 1.186 + if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270)) 1.187 + { 1.188 + image_info->width_samples = image_height; 1.189 + image_info->height_samples = image_width; 1.190 + dest_x_resolution = y_resolution; 1.191 + dest_y_resolution = x_resolution; 1.192 + SWAP (double, image_info->width_points, image_info->height_points); 1.193 + } 1.194 + else 1.195 + { 1.196 + image_info->width_samples = image_width; 1.197 + image_info->height_samples = image_height; 1.198 + dest_x_resolution = x_resolution; 1.199 + dest_y_resolution = y_resolution; 1.200 + } 1.201 + 1.202 + image_info->width_points = (image_info->width_samples / dest_x_resolution) * POINTS_PER_INCH; 1.203 + image_info->height_points = (image_info->height_samples / dest_y_resolution) * POINTS_PER_INCH; 1.204 + 1.205 + if ((image_info->height_points > PAGE_MAX_POINTS) || 1.206 + (image_info->width_points > PAGE_MAX_POINTS)) 1.207 + { 1.208 + fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES); 1.209 + return (0); 1.210 + } 1.211 + 1.212 + return (1); 1.213 +} 1.214 + 1.215 + 1.216 +/* frees original! */ 1.217 +static Bitmap *resize_bitmap (Bitmap *src, 1.218 + double x_resolution, 1.219 + double y_resolution, 1.220 + input_attributes_t input_attributes) 1.221 +{ 1.222 + Rect src_rect; 1.223 + Point dest_min; 1.224 + Bitmap *dest; 1.225 + 1.226 + int width_pixels = input_attributes.page_size.width * x_resolution; 1.227 + int height_pixels = input_attributes.page_size.height * y_resolution; 1.228 + 1.229 + src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2; 1.230 + src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2; 1.231 + src_rect.max.x = src_rect.min.x + width_pixels; 1.232 + src_rect.max.y = src_rect.min.y + height_pixels; 1.233 + 1.234 + dest_min.x = 0; 1.235 + dest_min.y = 0; 1.236 + 1.237 + dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC, 0); 1.238 + free_bitmap (src); 1.239 + return (dest); 1.240 +} 1.241 + 1.242 + 1.243 +/* "in place" rotation */ 1.244 +static void rotate_bitmap (Bitmap *src, 1.245 + input_attributes_t input_attributes) 1.246 +{ 1.247 + switch (input_attributes.rotation) 1.248 + { 1.249 + case 0: break; 1.250 + case 90: rot_90 (src); break; 1.251 + case 180: rot_180 (src); break; 1.252 + case 270: rot_270 (src); break; 1.253 + default: 1.254 + fprintf (stderr, "rotation must be 0, 90, 180, or 270\n"); 1.255 + } 1.256 +} 1.257 + 1.258 + 1.259 +bool process_tiff_image (int image, /* range 1 .. n */ 1.260 + input_attributes_t input_attributes, 1.261 + image_info_t *image_info, 1.262 + pdf_page_handle page) 1.263 +{ 1.264 + Rect rect; 1.265 + Bitmap *bitmap = NULL; 1.266 + 1.267 + int row; 1.268 + 1.269 + rect.min.x = 0; 1.270 + rect.min.y = 0; 1.271 + 1.272 + if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270)) 1.273 + { 1.274 + rect.max.x = image_info->height_samples; 1.275 + rect.max.y = image_info->width_samples; 1.276 + } 1.277 + else 1.278 + { 1.279 + rect.max.x = image_info->width_samples; 1.280 + rect.max.y = image_info->height_samples; 1.281 + } 1.282 + 1.283 + bitmap = create_bitmap (& rect); 1.284 + 1.285 + if (! bitmap) 1.286 + { 1.287 + fprintf (stderr, "can't allocate bitmap\n"); 1.288 + fprintf (stderr, "width %d height %d\n", image_info->width_samples, image_info->height_samples); 1.289 + goto fail; 1.290 + } 1.291 + 1.292 + for (row = 0; row < rect.max.y; row++) 1.293 + if (1 != TIFFReadScanline (tiff_in, 1.294 + bitmap->bits + row * bitmap->row_words, 1.295 + row, 1.296 + 0)) 1.297 + { 1.298 + fprintf (stderr, "can't read TIFF scanline\n"); 1.299 + goto fail; 1.300 + } 1.301 + 1.302 +#ifdef TIFF_REVERSE_BITS 1.303 + reverse_bits ((uint8_t *) bitmap->bits, 1.304 + rect.max.y * bitmap->row_words * sizeof (word_t)); 1.305 +#endif /* TIFF_REVERSE_BITS */ 1.306 + 1.307 +#if 0 1.308 + if (input_attributes.has_page_size) 1.309 + bitmap = resize_bitmap (bitmap, 1.310 + x_resolution, 1.311 + y_resolution, 1.312 + input_attributes); 1.313 +#endif 1.314 + 1.315 + rotate_bitmap (bitmap, 1.316 + input_attributes); 1.317 + 1.318 +#if 0 1.319 + pdf_write_text (page); 1.320 +#else 1.321 + pdf_write_g4_fax_image (page, 1.322 + 0, 0, /* x, y */ 1.323 + image_info->width_points, image_info->height_points, 1.324 + bitmap, 1.325 + 0, /* ImageMask */ 1.326 + 0, 0, 0, /* r, g, b */ 1.327 + 0); /* BlackIs1 */ 1.328 +#endif 1.329 + 1.330 + if (bitmap) 1.331 + free_bitmap (bitmap); 1.332 + return (page); 1.333 + 1.334 + fail: 1.335 + if (bitmap) 1.336 + free_bitmap (bitmap); 1.337 + 1.338 + return (NULL); 1.339 +} 1.340 + 1.341 + 1.342 +input_handler_t tiff_handler = 1.343 + { 1.344 + open_tiff_input_file, 1.345 + close_tiff_input_file, 1.346 + last_tiff_input_page, 1.347 + get_tiff_image_info, 1.348 + process_tiff_image 1.349 + }; 1.350 + 1.351 + 1.352 +void init_tiff_handler (void) 1.353 +{ 1.354 + install_input_handler (& tiff_handler); 1.355 +}