t2p.c

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