1.1 diff -r 2350a4b7393c -r 9c85a4cd88a3 tumble.c 1.2 --- a/tumble.c Wed Jan 02 16:39:20 2002 +0000 1.3 +++ b/tumble.c Wed Jan 02 16:39:39 2002 +0000 1.4 @@ -5,7 +5,7 @@ 1.5 * encoding. 1.6 * 1.7 * Main program 1.8 - * $Id: tumble.c,v 1.13 2002/01/02 02:18:13 eric Exp $ 1.9 + * $Id: tumble.c,v 1.14 2002/01/02 08:39:39 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 @@ -179,53 +179,46 @@ 1.14 } 1.15 1.16 1.17 -static Bitmap *rotate_bitmap (Bitmap *src, 1.18 +/* frees original! */ 1.19 +static Bitmap *resize_bitmap (Bitmap *src, 1.20 float x_resolution, 1.21 float y_resolution, 1.22 input_attributes_t input_attributes) 1.23 { 1.24 Rect src_rect; 1.25 - Point dest_upper_left; 1.26 - int scan; 1.27 + Point dest_min; 1.28 + Bitmap *dest; 1.29 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 + int width_pixels = input_attributes.page_size.width * x_resolution; 1.35 + int height_pixels = input_attributes.page_size.height * y_resolution; 1.36 + 1.37 + src_rect.min.x = (rect_width (& src->rect) - width_pixels) / 2; 1.38 + src_rect.min.y = (rect_height (& src->rect) - height_pixels) / 2; 1.39 + src_rect.max.x = src_rect.min.x + width_pixels; 1.40 + src_rect.max.y = src_rect.min.y + height_pixels; 1.41 1.42 - src_rect.upper_left.x = (src->width - width_pixels) / 2; 1.43 - src_rect.upper_left.y = (src->height - height_pixels) / 2; 1.44 - src_rect.lower_right.x = src_rect.upper_left.x + width_pixels; 1.45 - src_rect.lower_right.y = src_rect.upper_left.y + height_pixels; 1.46 - } 1.47 - else 1.48 - { 1.49 - src_rect.upper_left.x = 0; 1.50 - src_rect.upper_left.y = 0; 1.51 - src_rect.lower_right.x = src->width; 1.52 - src_rect.lower_right.y = src->height; 1.53 - } 1.54 + dest_min.x = 0; 1.55 + dest_min.y = 0; 1.56 1.57 - dest_upper_left.x = 0; 1.58 - dest_upper_left.y = 0; 1.59 + dest = bitblt (src, & src_rect, NULL, & dest_min, TF_SRC); 1.60 + free_bitmap (src); 1.61 + return (dest); 1.62 +} 1.63 1.64 + 1.65 +/* "in place" rotation */ 1.66 +static void rotate_bitmap (Bitmap *src, 1.67 + input_attributes_t input_attributes) 1.68 +{ 1.69 switch (input_attributes.rotation) 1.70 { 1.71 - case 0: scan = ROT_0; break; 1.72 - case 90: scan = ROT_90; break; 1.73 - case 180: scan = ROT_180; break; 1.74 - case 270: scan = ROT_270; break; 1.75 + case 0: break; 1.76 + case 90: rot_90 (src); break; 1.77 + case 180: rot_180 (src); break; 1.78 + case 270: rot_270 (src); break; 1.79 default: 1.80 fprintf (stderr, "rotation must be 0, 90, 180, or 270\n"); 1.81 - return (NULL); 1.82 } 1.83 - 1.84 - return (bitblt (src, 1.85 - src_rect, 1.86 - NULL, /* dest_bitmap */ 1.87 - dest_upper_left, 1.88 - scan, 1.89 - TF_SRC)); 1.90 } 1.91 1.92 1.93 @@ -256,8 +249,9 @@ 1.94 int width_points, height_points; /* really 1/72 inch units rather than 1.95 points */ 1.96 1.97 - Bitmap *src_bitmap; 1.98 - Bitmap *dest_bitmap; 1.99 + Rect rect; 1.100 + Bitmap *bitmap; 1.101 + 1.102 int row; 1.103 1.104 panda_page *page; 1.105 @@ -366,29 +360,35 @@ 1.106 1.107 scanline_size = TIFFScanlineSize (in); 1.108 1.109 - src_bitmap = create_bitmap (image_width, image_length); 1.110 - if (! src_bitmap) 1.111 + rect.min.x = 0; 1.112 + rect.min.y = 0; 1.113 + rect.max.x = image_width; 1.114 + rect.max.y = image_length; 1.115 + 1.116 + bitmap = create_bitmap (& rect); 1.117 + 1.118 + if (! bitmap) 1.119 { 1.120 fprintf (stderr, "can't allocate bitmap\n"); 1.121 goto fail; 1.122 } 1.123 1.124 - if (src_bitmap->rowbytes != scanline_size) 1.125 + if (bitmap->rowbytes != scanline_size) 1.126 { 1.127 printf ("image_width %d\n", image_width); 1.128 - printf ("rowbytes %d\n", src_bitmap->rowbytes); 1.129 + printf ("rowbytes %d\n", bitmap->rowbytes); 1.130 printf ("TIFFScanlineSize %d\n", scanline_size); 1.131 } 1.132 1.133 for (row = 0; row < image_length; row++) 1.134 TIFFReadScanline (in, 1.135 - src_bitmap->bits + row * src_bitmap->rowbytes, 1.136 + bitmap->bits + row * bitmap->rowbytes, 1.137 row, 1.138 0); 1.139 1.140 for (row = 0; row < dest_image_length; row++) 1.141 if (1 != TIFFReadScanline (in, 1.142 - src_bitmap->bits + row * src_bitmap->rowbytes, 1.143 + bitmap->bits + row * bitmap->rowbytes, 1.144 row, 1.145 0)) 1.146 { 1.147 @@ -396,15 +396,13 @@ 1.148 goto fail; 1.149 } 1.150 1.151 - dest_bitmap = rotate_bitmap (src_bitmap, 1.152 - x_resolution, 1.153 - y_resolution, 1.154 - input_attributes); 1.155 - if (! dest_bitmap) 1.156 - { 1.157 - fprintf (stderr, "can't allocate bitmap\n"); 1.158 - goto fail; 1.159 - } 1.160 + bitmap = resize_bitmap (bitmap, 1.161 + x_resolution, 1.162 + y_resolution, 1.163 + input_attributes); 1.164 + 1.165 + rotate_bitmap (bitmap, 1.166 + input_attributes); 1.167 1.168 tiff_temp_fd = mkstemp (tiff_temp_fn); 1.169 if (tiff_temp_fd < 0) 1.170 @@ -420,11 +418,11 @@ 1.171 goto fail; 1.172 } 1.173 1.174 - TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, dest_bitmap->height); 1.175 - TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, dest_bitmap->width); 1.176 + TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, rect_height (& bitmap->rect)); 1.177 + TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, rect_width (& bitmap->rect)); 1.178 TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config); 1.179 1.180 - TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, dest_bitmap->height); 1.181 + TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, rect_height (& bitmap->rect)); 1.182 1.183 TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit); 1.184 TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution); 1.185 @@ -435,9 +433,9 @@ 1.186 TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); 1.187 TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE); 1.188 1.189 - for (row = 0; row < dest_bitmap->height; row++) 1.190 + for (row = 0; row < rect_height (& bitmap->rect); row++) 1.191 if (1 != TIFFWriteScanline (tiff_temp, 1.192 - dest_bitmap->bits + row * dest_bitmap->rowbytes, 1.193 + bitmap->bits + row * bitmap->rowbytes, 1.194 row, 1.195 0)) 1.196 { 1.197 @@ -447,11 +445,10 @@ 1.198 1.199 TIFFClose (tiff_temp); 1.200 1.201 - width_points = (dest_bitmap->width / dest_x_resolution) * POINTS_PER_INCH; 1.202 - height_points = (dest_bitmap->height / dest_y_resolution) * POINTS_PER_INCH; 1.203 + width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH; 1.204 + height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH; 1.205 1.206 - free_bitmap (dest_bitmap); 1.207 - free_bitmap (src_bitmap); 1.208 + free_bitmap (bitmap); 1.209 1.210 if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS)) 1.211 {