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