tumble.c

changeset 32
3aac131058da
parent 30
35fad7ec7732
child 36
a338db73c6f4
     1.1 --- a/tumble.c	Tue Jan 01 06:17:39 2002 +0000
     1.2 +++ b/tumble.c	Tue Jan 01 10:16:50 2002 +0000
     1.3 @@ -5,7 +5,7 @@
     1.4   *           encoding.
     1.5   *
     1.6   * Main program
     1.7 - * $Id: tumble.c,v 1.11 2001/12/31 22:11:43 eric Exp $
     1.8 + * $Id: tumble.c,v 1.12 2002/01/01 02:16:50 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,6 +179,42 @@
    1.13  }
    1.14  
    1.15  
    1.16 +static Bitmap *rotate_bitmap (Bitmap *src, int rotation)
    1.17 +{
    1.18 +  Rect src_rect;
    1.19 +  Point dest_upper_left;
    1.20 +  int scan;
    1.21 +
    1.22 +  src_rect.upper_left.x = 0;
    1.23 +  src_rect.upper_left.y = 0;
    1.24 +  src_rect.lower_right.x = src->width;
    1.25 +  src_rect.lower_right.y = src->height;
    1.26 +
    1.27 +  dest_upper_left.x = 0;
    1.28 +  dest_upper_left.y = 0;
    1.29 +
    1.30 +  switch (rotation)
    1.31 +    {
    1.32 +    case 0: scan = ROT_0; break;
    1.33 +    case 90: scan = ROT_90; break;
    1.34 +    case 180: scan = ROT_180; break;
    1.35 +    case 270: scan = ROT_270; break;
    1.36 +    default:
    1.37 +      fprintf (stderr, "rotation must be 0, 90, 180, or 270\n");
    1.38 +      return (NULL);
    1.39 +    }
    1.40 +
    1.41 +  return (bitblt (src,
    1.42 +		  src_rect,
    1.43 +		  NULL,  /* dest_bitmap */
    1.44 +		  dest_upper_left,
    1.45 +		  scan,
    1.46 +		  TF_SRC));
    1.47 +}
    1.48 +
    1.49 +
    1.50 +#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
    1.51 +
    1.52  boolean process_page (int image,  /* range 1 .. n */
    1.53  		      input_attributes_t input_attributes,
    1.54  		      bookmark_t *bookmarks)
    1.55 @@ -186,6 +222,7 @@
    1.56    int result = 0;
    1.57  
    1.58    u32 image_length, image_width;
    1.59 +  u32 dest_image_length, dest_image_width;
    1.60  #ifdef CHECK_DEPTH
    1.61    u32 image_depth;
    1.62  #endif
    1.63 @@ -193,14 +230,19 @@
    1.64    u16 samples_per_pixel;
    1.65    u16 bits_per_sample;
    1.66    u16 planar_config;
    1.67 +
    1.68    u16 resolution_unit;
    1.69    float x_resolution, y_resolution;
    1.70 +  float dest_x_resolution, dest_y_resolution;
    1.71 +
    1.72 +  int scanline_size;
    1.73 +
    1.74    int width_points, height_points;  /* really 1/72 inch units rather than
    1.75  				       points */
    1.76  
    1.77 -
    1.78 -  char *buffer;
    1.79 -  u32 row;
    1.80 +  Bitmap *src_bitmap;
    1.81 +  Bitmap *dest_bitmap;
    1.82 +  int row;
    1.83  
    1.84    panda_page *page;
    1.85  
    1.86 @@ -298,6 +340,17 @@
    1.87        goto fail;
    1.88      }
    1.89  
    1.90 +  width_points = (image_width / x_resolution) * POINTS_PER_INCH;
    1.91 +  height_points = (image_length / y_resolution) * POINTS_PER_INCH;
    1.92 +
    1.93 +  if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
    1.94 +    {
    1.95 +      fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
    1.96 +      goto fail;
    1.97 +    }
    1.98 +
    1.99 +  printf ("height_points %d, width_points %d\n", height_points, width_points);
   1.100 +
   1.101    tiff_temp_fd = mkstemp (tiff_temp_fn);
   1.102    if (tiff_temp_fd < 0)
   1.103      {
   1.104 @@ -312,47 +365,92 @@
   1.105        goto fail;
   1.106      }
   1.107  
   1.108 -  TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, image_length);
   1.109 -  TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, image_width);
   1.110 +  printf ("rotation %d\n", input_attributes.rotation);
   1.111 +
   1.112 +  if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
   1.113 +    {
   1.114 +      dest_image_width  = image_length;
   1.115 +      dest_image_length = image_width;
   1.116 +      dest_x_resolution = y_resolution;
   1.117 +      dest_y_resolution = x_resolution;
   1.118 +      SWAP (int, width_points, height_points);
   1.119 +    }
   1.120 +  else
   1.121 +    {
   1.122 +      dest_image_width = image_width;
   1.123 +      dest_image_length = image_length;
   1.124 +      dest_x_resolution = x_resolution;
   1.125 +      dest_y_resolution = y_resolution;
   1.126 +    }
   1.127 +
   1.128 +  TIFFSetField (tiff_temp, TIFFTAG_IMAGELENGTH, dest_image_length);
   1.129 +  TIFFSetField (tiff_temp, TIFFTAG_IMAGEWIDTH, dest_image_width);
   1.130    TIFFSetField (tiff_temp, TIFFTAG_PLANARCONFIG, planar_config);
   1.131  
   1.132 -  TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, image_length);
   1.133 +  TIFFSetField (tiff_temp, TIFFTAG_ROWSPERSTRIP, dest_image_length);
   1.134  
   1.135    TIFFSetField (tiff_temp, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
   1.136 -  TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, x_resolution);
   1.137 -  TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, y_resolution);
   1.138 +  TIFFSetField (tiff_temp, TIFFTAG_XRESOLUTION, dest_x_resolution);
   1.139 +  TIFFSetField (tiff_temp, TIFFTAG_YRESOLUTION, dest_y_resolution);
   1.140  
   1.141    TIFFSetField (tiff_temp, TIFFTAG_SAMPLESPERPIXEL, samples_per_pixel);
   1.142    TIFFSetField (tiff_temp, TIFFTAG_BITSPERSAMPLE, bits_per_sample);
   1.143    TIFFSetField (tiff_temp, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
   1.144    TIFFSetField (tiff_temp, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
   1.145  
   1.146 -  buffer = _TIFFmalloc (TIFFScanlineSize (in));
   1.147 -  if (! buffer)
   1.148 +  scanline_size = TIFFScanlineSize (in);
   1.149 +
   1.150 +  src_bitmap = create_bitmap (image_width, image_length);
   1.151 +  if (! src_bitmap)
   1.152      {
   1.153 -      fprintf (stderr, "failed to allocate buffer\n");
   1.154 +      fprintf (stderr, "can't allocate bitmap\n");
   1.155        goto fail;
   1.156      }
   1.157  
   1.158 +  if (src_bitmap->rowbytes != scanline_size)
   1.159 +    {
   1.160 +      printf ("image_width %d\n", image_width);
   1.161 +      printf ("rowbytes %d\n", src_bitmap->rowbytes);
   1.162 +      printf ("TIFFScanlineSize %d\n", scanline_size);
   1.163 +    }
   1.164 +
   1.165    for (row = 0; row < image_length; row++)
   1.166 -    {
   1.167 -      TIFFReadScanline (in, buffer, row, 0);
   1.168 -      TIFFWriteScanline (tiff_temp, buffer, row, 0);
   1.169 -    }
   1.170 +    TIFFReadScanline (in,
   1.171 +		      src_bitmap->bits + row * src_bitmap->rowbytes,
   1.172 +		      row,
   1.173 +		      0);
   1.174  
   1.175 -  _TIFFfree (buffer);
   1.176 -  TIFFClose (tiff_temp);
   1.177 +  for (row = 0; row < dest_image_length; row++)
   1.178 +    if (1 != TIFFReadScanline (in,
   1.179 +			       src_bitmap->bits + row * src_bitmap->rowbytes,
   1.180 +			       row,
   1.181 +			       0))
   1.182 +      {
   1.183 +	fprintf (stderr, "can't read TIFF scanline\n");
   1.184 +	goto fail;
   1.185 +      }
   1.186  
   1.187 -  width_points = (image_width / x_resolution) * POINTS_PER_INCH;
   1.188 -  height_points = (image_length / y_resolution) * POINTS_PER_INCH;
   1.189 -
   1.190 -  if ((height_points > PAGE_MAX_POINTS) || (width_points > PAGE_MAX_POINTS))
   1.191 +  dest_bitmap = rotate_bitmap (src_bitmap, input_attributes.rotation);
   1.192 +  if (! dest_bitmap)
   1.193      {
   1.194 -      fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   1.195 +      fprintf (stderr, "can't allocate bitmap\n");
   1.196        goto fail;
   1.197      }
   1.198  
   1.199 -  printf ("height_points %d, width_points %d\n", height_points, width_points);
   1.200 +  for (row = 0; row < dest_bitmap->height; row++)
   1.201 +    if (1 != TIFFWriteScanline (tiff_temp,
   1.202 +				dest_bitmap->bits + row * dest_bitmap->rowbytes,
   1.203 +				row,
   1.204 +				0))
   1.205 +      {
   1.206 +	fprintf (stderr, "can't write TIFF scanline\n");
   1.207 +	goto fail;
   1.208 +      }
   1.209 +
   1.210 +  TIFFClose (tiff_temp);
   1.211 +
   1.212 +  free_bitmap (dest_bitmap);
   1.213 +  free_bitmap (src_bitmap);
   1.214  
   1.215    sprintf (pagesize, "[0 0 %d %d]", width_points, height_points);
   1.216