1.1 --- a/tumble.c Wed Jan 02 10:17:48 2002 +0000 1.2 +++ b/tumble.c Wed Jan 02 10:18:13 2002 +0000 1.3 @@ -5,7 +5,7 @@ 1.4 * encoding. 1.5 * 1.6 * Main program 1.7 - * $Id: tumble.c,v 1.12 2002/01/01 02:16:50 eric Exp $ 1.8 + * $Id: tumble.c,v 1.13 2002/01/02 02:18:13 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 @@ -179,21 +179,37 @@ 1.13 } 1.14 1.15 1.16 -static Bitmap *rotate_bitmap (Bitmap *src, int rotation) 1.17 +static Bitmap *rotate_bitmap (Bitmap *src, 1.18 + float x_resolution, 1.19 + float y_resolution, 1.20 + input_attributes_t input_attributes) 1.21 { 1.22 Rect src_rect; 1.23 Point dest_upper_left; 1.24 int scan; 1.25 1.26 - src_rect.upper_left.x = 0; 1.27 - src_rect.upper_left.y = 0; 1.28 - src_rect.lower_right.x = src->width; 1.29 - src_rect.lower_right.y = src->height; 1.30 + if (input_attributes.has_page_size) 1.31 + { 1.32 + int width_pixels = input_attributes.page_size.width * x_resolution; 1.33 + int height_pixels = input_attributes.page_size.height * y_resolution; 1.34 + 1.35 + src_rect.upper_left.x = (src->width - width_pixels) / 2; 1.36 + src_rect.upper_left.y = (src->height - height_pixels) / 2; 1.37 + src_rect.lower_right.x = src_rect.upper_left.x + width_pixels; 1.38 + src_rect.lower_right.y = src_rect.upper_left.y + height_pixels; 1.39 + } 1.40 + else 1.41 + { 1.42 + src_rect.upper_left.x = 0; 1.43 + src_rect.upper_left.y = 0; 1.44 + src_rect.lower_right.x = src->width; 1.45 + src_rect.lower_right.y = src->height; 1.46 + } 1.47 1.48 dest_upper_left.x = 0; 1.49 dest_upper_left.y = 0; 1.50 1.51 - switch (rotation) 1.52 + switch (input_attributes.rotation) 1.53 { 1.54 case 0: scan = ROT_0; break; 1.55 case 90: scan = ROT_90; break; 1.56 @@ -293,17 +309,6 @@ 1.57 if (1 != TIFFGetField (in, TIFFTAG_PLANARCONFIG, & planar_config)) 1.58 planar_config = 1; 1.59 1.60 - printf ("image length %u width %u, " 1.61 -#ifdef CHECK_DEPTH 1.62 - "depth %u, " 1.63 -#endif 1.64 - "planar config %u\n", 1.65 - image_length, image_width, 1.66 -#ifdef CHECK_DEPTH 1.67 - image_depth, 1.68 -#endif 1.69 - planar_config); 1.70 - 1.71 if (1 != TIFFGetField (in, TIFFTAG_RESOLUTIONUNIT, & resolution_unit)) 1.72 resolution_unit = 2; 1.73 if (1 != TIFFGetField (in, TIFFTAG_XRESOLUTION, & x_resolution)) 1.74 @@ -311,9 +316,6 @@ 1.75 if (1 != TIFFGetField (in, TIFFTAG_YRESOLUTION, & y_resolution)) 1.76 y_resolution = 300; 1.77 1.78 - printf ("resolution unit %u, x resolution %f, y resolution %f\n", 1.79 - resolution_unit, x_resolution, y_resolution); 1.80 - 1.81 if (samples_per_pixel != 1) 1.82 { 1.83 fprintf (stderr, "samples per pixel %u, must be 1\n", samples_per_pixel); 1.84 @@ -340,33 +342,12 @@ 1.85 goto fail; 1.86 } 1.87 1.88 - width_points = (image_width / x_resolution) * POINTS_PER_INCH; 1.89 - height_points = (image_length / y_resolution) * POINTS_PER_INCH; 1.90 - 1.91 - if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS)) 1.92 + if (input_attributes.has_resolution) 1.93 { 1.94 - fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES); 1.95 - goto fail; 1.96 + x_resolution = input_attributes.x_resolution; 1.97 + y_resolution = input_attributes.y_resolution; 1.98 } 1.99 1.100 - printf ("height_points %d, width_points %d\n", height_points, width_points); 1.101 - 1.102 - tiff_temp_fd = mkstemp (tiff_temp_fn); 1.103 - if (tiff_temp_fd < 0) 1.104 - { 1.105 - fprintf (stderr, "can't create temporary TIFF file\n"); 1.106 - goto fail; 1.107 - } 1.108 - 1.109 - tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w"); 1.110 - if (! out) 1.111 - { 1.112 - fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn); 1.113 - goto fail; 1.114 - } 1.115 - 1.116 - printf ("rotation %d\n", input_attributes.rotation); 1.117 - 1.118 if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270)) 1.119 { 1.120 dest_image_width = image_length; 1.121 @@ -383,21 +364,6 @@ 1.122 dest_y_resolution = y_resolution; 1.123 } 1.124 1.125 - TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, dest_image_length); 1.126 - TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, dest_image_width); 1.127 - TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config); 1.128 - 1.129 - TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, dest_image_length); 1.130 - 1.131 - TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit); 1.132 - TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution); 1.133 - TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution); 1.134 - 1.135 - TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel); 1.136 - TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample); 1.137 - TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); 1.138 - TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); 1.139 - 1.140 scanline_size = TIFFScanlineSize (in); 1.141 1.142 src_bitmap = create_bitmap (image_width, image_length); 1.143 @@ -430,13 +396,45 @@ 1.144 goto fail; 1.145 } 1.146 1.147 - dest_bitmap = rotate_bitmap (src_bitmap, input_attributes.rotation); 1.148 + dest_bitmap = rotate_bitmap (src_bitmap, 1.149 + x_resolution, 1.150 + y_resolution, 1.151 + input_attributes); 1.152 if (! dest_bitmap) 1.153 { 1.154 fprintf (stderr, "can't allocate bitmap\n"); 1.155 goto fail; 1.156 } 1.157 1.158 + tiff_temp_fd = mkstemp (tiff_temp_fn); 1.159 + if (tiff_temp_fd < 0) 1.160 + { 1.161 + fprintf (stderr, "can't create temporary TIFF file\n"); 1.162 + goto fail; 1.163 + } 1.164 + 1.165 + tiff_temp = TIFFFdOpen (tiff_temp_fd, tiff_temp_fn, "w"); 1.166 + if (! out) 1.167 + { 1.168 + fprintf (stderr, "can't open temporary TIFF file '%s'\n", tiff_temp_fn); 1.169 + goto fail; 1.170 + } 1.171 + 1.172 + TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, dest_bitmap->height); 1.173 + TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, dest_bitmap->width); 1.174 + TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config); 1.175 + 1.176 + TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, dest_bitmap->height); 1.177 + 1.178 + TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit); 1.179 + TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution); 1.180 + TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution); 1.181 + 1.182 + TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel); 1.183 + TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample); 1.184 + TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); 1.185 + TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); 1.186 + 1.187 for (row = 0; row < dest_bitmap->height; row++) 1.188 if (1 != TIFFWriteScanline (tiff_temp, 1.189 dest_bitmap->bits + row * dest_bitmap->rowbytes, 1.190 @@ -449,9 +447,18 @@ 1.191 1.192 TIFFClose (tiff_temp); 1.193 1.194 + width_points = (dest_bitmap->width / dest_x_resolution) * POINTS_PER_INCH; 1.195 + height_points = (dest_bitmap->height / dest_y_resolution) * POINTS_PER_INCH; 1.196 + 1.197 free_bitmap (dest_bitmap); 1.198 free_bitmap (src_bitmap); 1.199 1.200 + if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS)) 1.201 + { 1.202 + fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES); 1.203 + goto fail; 1.204 + } 1.205 + 1.206 sprintf (pagesize, "[0 0 %d %d]", width_points, height_points); 1.207 1.208 page = panda_newpage (out->pdf, pagesize);