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