tumble_jpeg.c

changeset 141
752599b50ff3
child 147
090af3709920
     1.1 diff -r 3fe049d83e22 -r 752599b50ff3 tumble_jpeg.c
     1.2 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 +++ b/tumble_jpeg.c	Thu Mar 20 06:54:08 2003 +0000
     1.4 @@ -0,0 +1,176 @@
     1.5 +/*
     1.6 + * tumble: build a PDF file from image files
     1.7 + *
     1.8 + * $Id: tumble_jpeg.c,v 1.1 2003/03/19 22:54:08 eric Exp $
     1.9 + * Copyright 2003 Eric Smith <eric@brouhaha.com>
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify
    1.12 + * it under the terms of the GNU General Public License version 2 as
    1.13 + * published by the Free Software Foundation.  Note that permission is
    1.14 + * not granted to redistribute this program under the terms of any
    1.15 + * other version of the General Public License.
    1.16 + *
    1.17 + * This program is distributed in the hope that it will be useful,
    1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.20 + * GNU General Public License for more details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU General Public License
    1.23 + * along with this program; if not, write to the Free Software
    1.24 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    1.25 + */
    1.26 +
    1.27 +
    1.28 +#include <stdbool.h>
    1.29 +#include <stdint.h>
    1.30 +#include <stdio.h>
    1.31 +#include <jpeglib.h>
    1.32 +
    1.33 +
    1.34 +#include "semantics.h"
    1.35 +#include "tumble.h"
    1.36 +#include "bitblt.h"
    1.37 +#include "pdf.h"
    1.38 +#include "tumble_input.h"
    1.39 +
    1.40 +
    1.41 +static FILE *jpeg_f;
    1.42 +
    1.43 +static struct jpeg_decompress_struct cinfo;
    1.44 +static struct jpeg_error_mgr jerr;
    1.45 +
    1.46 +
    1.47 +bool close_jpeg_input_file (void)
    1.48 +{
    1.49 +  return (1);
    1.50 +}
    1.51 +
    1.52 +
    1.53 +bool open_jpeg_input_file (FILE *f, char *name)
    1.54 +{
    1.55 +  uint8_t buf [2];
    1.56 +  size_t l;
    1.57 +
    1.58 +  l = fread (& buf [0], 1, sizeof (buf), f);
    1.59 +  if (l != sizeof (buf))
    1.60 +    return (0);
    1.61 +
    1.62 +  rewind (f);
    1.63 +
    1.64 +  if ((buf [0] != 0xff) || (buf [1] != 0xd8))
    1.65 +    return (0);
    1.66 +
    1.67 +  cinfo.err = jpeg_std_error (& jerr);
    1.68 +  jpeg_create_decompress (& cinfo);
    1.69 +
    1.70 +  jpeg_stdio_src (& cinfo, f);
    1.71 +
    1.72 +  jpeg_read_header (& cinfo, TRUE);
    1.73 +
    1.74 +  rewind (f);
    1.75 +
    1.76 +  jpeg_f = f;
    1.77 +
    1.78 +  return (1);
    1.79 +}
    1.80 +
    1.81 +
    1.82 +bool last_jpeg_input_page (void)
    1.83 +{
    1.84 +  return (1);
    1.85 +}
    1.86 +
    1.87 +
    1.88 +bool get_jpeg_image_info (int image,
    1.89 +			  input_attributes_t input_attributes,
    1.90 +			  image_info_t *image_info)
    1.91 +{
    1.92 +  double unit;
    1.93 +
    1.94 +  switch (cinfo.jpeg_color_space)
    1.95 +    {
    1.96 +    case JCS_GRAYSCALE:
    1.97 +      if (cinfo.num_components != 1)
    1.98 +	{
    1.99 +	  fprintf (stderr, "JPEG grayscale image has %d components, should have 1\n",
   1.100 +		   cinfo.num_components);
   1.101 +	  return (0);
   1.102 +	}
   1.103 +      image_info->color = 0;
   1.104 +      break;
   1.105 +    case JCS_RGB:
   1.106 +    case JCS_YCbCr:
   1.107 +      if (cinfo.num_components != 3)
   1.108 +	{
   1.109 +	  fprintf (stderr, "JPEG RGB or YCbCr image has %d components, should have 3\n",
   1.110 +		   cinfo.num_components);
   1.111 +	  return (0);
   1.112 +	}
   1.113 +      image_info->color = 1;
   1.114 +      break;
   1.115 +    default:
   1.116 +      fprintf (stderr, "JPEG color space %d not supported\n", cinfo.jpeg_color_space);
   1.117 +      return (0);
   1.118 +    }
   1.119 +  image_info->width_samples = cinfo.image_width;
   1.120 +  image_info->height_samples = cinfo.image_height;
   1.121 +
   1.122 +  if (cinfo.saw_JFIF_marker & cinfo.density_unit)
   1.123 +    {
   1.124 +      switch (cinfo.density_unit)
   1.125 +	{
   1.126 +	case 1:  /* samples per inch */
   1.127 +	  unit = 1.0;
   1.128 +	  break;
   1.129 +	case 2:  /* samples per cm */
   1.130 +	  unit = 2.54;
   1.131 +	  break;
   1.132 +	default:
   1.133 +	  fprintf (stderr, "JFIF density unit %d not supported\n", cinfo.density_unit);
   1.134 +	  return (0);
   1.135 +	}
   1.136 +      image_info->width_points = ((image_info->width_samples * POINTS_PER_INCH) /
   1.137 +				  (cinfo.X_density * unit));
   1.138 +      image_info->height_points = ((image_info->height_samples * POINTS_PER_INCH) /
   1.139 +				   (cinfo.Y_density * unit));
   1.140 +    }
   1.141 +  else
   1.142 +    {
   1.143 +      /* assume 300 DPI - not great, but what else can we do? */
   1.144 +      image_info->width_points = (image_info->width_samples * POINTS_PER_INCH) / 300.0;
   1.145 +      image_info->height_points = (image_info->height_samples * POINTS_PER_INCH) / 300.0;
   1.146 +    }
   1.147 +
   1.148 +  return (1);
   1.149 +}
   1.150 +
   1.151 +
   1.152 +bool process_jpeg_image (int image,  /* range 1 .. n */
   1.153 +			 input_attributes_t input_attributes,
   1.154 +			 image_info_t *image_info,
   1.155 +			 pdf_page_handle page)
   1.156 +{
   1.157 +  pdf_write_jpeg_image (page,
   1.158 +			0, 0,  /* x, y */
   1.159 +			image_info->width_points,
   1.160 +			image_info->height_points,
   1.161 +			jpeg_f);
   1.162 +
   1.163 +  return (page);
   1.164 +}
   1.165 +
   1.166 +
   1.167 +input_handler_t jpeg_handler =
   1.168 +  {
   1.169 +    open_jpeg_input_file,
   1.170 +    close_jpeg_input_file,
   1.171 +    last_jpeg_input_page,
   1.172 +    get_jpeg_image_info,
   1.173 +    process_jpeg_image
   1.174 +  };
   1.175 +
   1.176 +
   1.177 +void init_jpeg_handler (void)
   1.178 +{
   1.179 +  install_input_handler (& jpeg_handler);
   1.180 +}