tumble_jpeg.c

Thu, 20 Mar 2003 08:20:52 +0000

author
eric
date
Thu, 20 Mar 2003 08:20:52 +0000
changeset 148
d4a6e303703a
parent 147
090af3709920
child 150
17531d20e477
permissions
-rw-r--r--

fix return values in process_tiff_image().

     1 /*
     2  * tumble: build a PDF file from image files
     3  *
     4  * $Id: tumble_jpeg.c,v 1.2 2003/03/19 23:53:09 eric Exp $
     5  * Copyright 2003 Eric Smith <eric@brouhaha.com>
     6  *
     7  * This program is free software; you can redistribute it and/or modify
     8  * it under the terms of the GNU General Public License version 2 as
     9  * published by the Free Software Foundation.  Note that permission is
    10  * not granted to redistribute this program under the terms of any
    11  * other version of the General Public License.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  *
    18  * You should have received a copy of the GNU General Public License
    19  * along with this program; if not, write to the Free Software
    20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    21  */
    24 #include <stdbool.h>
    25 #include <stdint.h>
    26 #include <stdio.h>
    27 #include <jpeglib.h>
    30 #include "semantics.h"
    31 #include "tumble.h"
    32 #include "bitblt.h"
    33 #include "pdf.h"
    34 #include "tumble_input.h"
    37 static FILE *jpeg_f;
    39 static struct jpeg_decompress_struct cinfo;
    40 static struct jpeg_error_mgr jerr;
    43 bool close_jpeg_input_file (void)
    44 {
    45   return (1);
    46 }
    49 bool open_jpeg_input_file (FILE *f, char *name)
    50 {
    51   uint8_t buf [2];
    52   size_t l;
    54   l = fread (& buf [0], 1, sizeof (buf), f);
    55   if (l != sizeof (buf))
    56     return (0);
    58   rewind (f);
    60   if ((buf [0] != 0xff) || (buf [1] != 0xd8))
    61     return (0);
    63   cinfo.err = jpeg_std_error (& jerr);
    64   jpeg_create_decompress (& cinfo);
    66   jpeg_stdio_src (& cinfo, f);
    68   jpeg_read_header (& cinfo, TRUE);
    70   rewind (f);
    72   jpeg_f = f;
    74   return (1);
    75 }
    78 bool last_jpeg_input_page (void)
    79 {
    80   return (1);
    81 }
    84 bool get_jpeg_image_info (int image,
    85 			  input_attributes_t input_attributes,
    86 			  image_info_t *image_info)
    87 {
    88   double unit;
    90 #ifdef DEBUG_JPEG
    91   printf ("color space: %d\n", cinfo.jpeg_color_space);
    92   printf ("components: %d\n", cinfo.num_components);
    93   printf ("density unit: %d\n", cinfo.density_unit);
    94   printf ("x density: %d\n", cinfo.X_density);
    95   printf ("y density: %d\n", cinfo.Y_density);
    96   printf ("width: %d\n", cinfo.image_width);
    97   printf ("height: %d\n", cinfo.image_height);
    98 #endif
   100   switch (cinfo.jpeg_color_space)
   101     {
   102     case JCS_GRAYSCALE:
   103       if (cinfo.num_components != 1)
   104 	{
   105 	  fprintf (stderr, "JPEG grayscale image has %d components, should have 1\n",
   106 		   cinfo.num_components);
   107 	  return (0);
   108 	}
   109       image_info->color = 0;
   110       break;
   111     case JCS_RGB:
   112     case JCS_YCbCr:
   113       if (cinfo.num_components != 3)
   114 	{
   115 	  fprintf (stderr, "JPEG RGB or YCbCr image has %d components, should have 3\n",
   116 		   cinfo.num_components);
   117 	  return (0);
   118 	}
   119       image_info->color = 1;
   120       break;
   121     default:
   122       fprintf (stderr, "JPEG color space %d not supported\n", cinfo.jpeg_color_space);
   123       return (0);
   124     }
   125   image_info->width_samples = cinfo.image_width;
   126   image_info->height_samples = cinfo.image_height;
   128   if (cinfo.saw_JFIF_marker & cinfo.density_unit)
   129     {
   130       switch (cinfo.density_unit)
   131 	{
   132 	case 1:  /* samples per inch */
   133 	  unit = 1.0;
   134 	  break;
   135 	case 2:  /* samples per cm */
   136 	  unit = 2.54;
   137 	  break;
   138 	default:
   139 	  fprintf (stderr, "JFIF density unit %d not supported\n", cinfo.density_unit);
   140 	  return (0);
   141 	}
   142       image_info->width_points = ((image_info->width_samples * POINTS_PER_INCH) /
   143 				  (cinfo.X_density * unit));
   144       image_info->height_points = ((image_info->height_samples * POINTS_PER_INCH) /
   145 				   (cinfo.Y_density * unit));
   146     }
   147   else
   148     {
   149       /* assume 300 DPI - not great, but what else can we do? */
   150       image_info->width_points = (image_info->width_samples * POINTS_PER_INCH) / 300.0;
   151       image_info->height_points = (image_info->height_samples * POINTS_PER_INCH) / 300.0;
   152     }
   154   return (1);
   155 }
   158 bool process_jpeg_image (int image,  /* range 1 .. n */
   159 			 input_attributes_t input_attributes,
   160 			 image_info_t *image_info,
   161 			 pdf_page_handle page)
   162 {
   163   pdf_write_jpeg_image (page,
   164 			0, 0,  /* x, y */
   165 			image_info->width_points,
   166 			image_info->height_points,
   167 			image_info->color,
   168 			image_info->width_samples,
   169 			image_info->height_samples,
   170 			jpeg_f);
   172   return (page);
   173 }
   176 input_handler_t jpeg_handler =
   177   {
   178     open_jpeg_input_file,
   179     close_jpeg_input_file,
   180     last_jpeg_input_page,
   181     get_jpeg_image_info,
   182     process_jpeg_image
   183   };
   186 void init_jpeg_handler (void)
   187 {
   188   install_input_handler (& jpeg_handler);
   189 }