tumble.c

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