1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tumble_pbm.c Thu Apr 10 08:47:30 2003 +0000 1.3 @@ -0,0 +1,230 @@ 1.4 +/* 1.5 + * tumble: build a PDF file from image files 1.6 + * 1.7 + * $Id: tumble_pbm.c,v 1.1 2003/04/10 00:47:30 eric Exp $ 1.8 + * Copyright 2001, 2002, 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 <stdlib.h> 1.31 +#include <strings.h> /* strcasecmp() is a BSDism */ 1.32 + 1.33 +#include <pbm.h> 1.34 +/* 1.35 + * pbm_readpbmrow_packed always uses big-endian bit ordering. 1.36 + * On little-endian processors (such as the x86), we want little-endian 1.37 + * bit order, so we must reverse the bits ourselves after we read in the 1.38 + * file. 1.39 + */ 1.40 +#define PBM_REVERSE_BITS 1.41 + 1.42 + 1.43 +#include "semantics.h" 1.44 +#include "tumble.h" 1.45 +#include "bitblt.h" 1.46 +#include "pdf.h" 1.47 +#include "tumble_input.h" 1.48 + 1.49 + 1.50 +typedef struct 1.51 +{ 1.52 + FILE *f; 1.53 + int rows; 1.54 + int cols; 1.55 + int format; 1.56 +} pbm_info_t; 1.57 + 1.58 +static pbm_info_t pbm; 1.59 + 1.60 + 1.61 +#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) 1.62 + 1.63 + 1.64 +static bool match_pbm_suffix (char *suffix) 1.65 +{ 1.66 + return (strcasecmp (suffix, ".pbm") == 0); 1.67 +} 1.68 + 1.69 + 1.70 +static bool close_pbm_input_file (void) 1.71 +{ 1.72 + pbm.f = NULL; 1.73 + return (1); 1.74 +} 1.75 + 1.76 + 1.77 +static bool open_pbm_input_file (FILE *f, char *name) 1.78 +{ 1.79 + uint8_t buf [2]; 1.80 + size_t l; 1.81 + 1.82 + l = fread (& buf [0], 1, sizeof (buf), f); 1.83 + if (l != sizeof (buf)) 1.84 + return (0); 1.85 + 1.86 + rewind (f); 1.87 + 1.88 + if (! (((buf [0] == 'P') && (buf [1] == '1')) || 1.89 + ((buf [0] == 'P') && (buf [1] == '4')))) 1.90 + return (0); 1.91 + 1.92 + pbm.f = f; 1.93 + 1.94 + pbm_readpbminit (f, & pbm.cols, & pbm.rows, & pbm.format); 1.95 + 1.96 + return (1); 1.97 +} 1.98 + 1.99 + 1.100 +static bool last_pbm_input_page (void) 1.101 +{ 1.102 + /* only handle single-page PBM files for now */ 1.103 + return (1); 1.104 +} 1.105 + 1.106 + 1.107 +static bool get_pbm_image_info (int image, 1.108 + input_attributes_t input_attributes, 1.109 + image_info_t *image_info) 1.110 +{ 1.111 + double x_resolution = 300; 1.112 + double y_resolution = 300; 1.113 + 1.114 + /* $$$ need to handle rotation! */ 1.115 + if (input_attributes.has_resolution) 1.116 + { 1.117 + x_resolution = input_attributes.x_resolution; 1.118 + y_resolution = input_attributes.y_resolution; 1.119 + } 1.120 + 1.121 + image_info->width_points = (pbm.cols / x_resolution) * POINTS_PER_INCH; 1.122 + image_info->height_points = (pbm.rows / y_resolution) * POINTS_PER_INCH; 1.123 + 1.124 + if ((image_info->height_points > PAGE_MAX_POINTS) || 1.125 + (image_info->width_points > PAGE_MAX_POINTS)) 1.126 + { 1.127 + fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES); 1.128 + return (0); 1.129 + } 1.130 + 1.131 + return (1); 1.132 +} 1.133 + 1.134 + 1.135 +static bool process_pbm_image (int image, /* range 1 .. n */ 1.136 + input_attributes_t input_attributes, 1.137 + image_info_t *image_info, 1.138 + pdf_page_handle page) 1.139 +{ 1.140 + bool result = 0; 1.141 + Rect rect; 1.142 + Bitmap *bitmap = NULL; 1.143 + 1.144 + int row; 1.145 + 1.146 + rect.min.x = 0; 1.147 + rect.min.y = 0; 1.148 + 1.149 + if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270)) 1.150 + { 1.151 + rect.max.x = image_info->height_samples; 1.152 + rect.max.y = image_info->width_samples; 1.153 + } 1.154 + else 1.155 + { 1.156 + rect.max.x = image_info->width_samples; 1.157 + rect.max.y = image_info->height_samples; 1.158 + } 1.159 + 1.160 + bitmap = create_bitmap (& rect); 1.161 + 1.162 + if (! bitmap) 1.163 + { 1.164 + fprintf (stderr, "can't allocate bitmap\n"); 1.165 + fprintf (stderr, "width %d height %d\n", image_info->width_samples, image_info->height_samples); 1.166 + goto fail; 1.167 + } 1.168 + 1.169 + for (row = 0; row < rect.max.y; row++) 1.170 + { 1.171 + pbm_readpbmrow_packed (pbm.f, 1.172 + (unsigned char *) (bitmap->bits + row * bitmap->row_words), 1.173 + pbm.cols, 1.174 + pbm.format); 1.175 + } 1.176 + 1.177 +#ifdef PBM_REVERSE_BITS 1.178 + reverse_bits ((uint8_t *) bitmap->bits, 1.179 + rect.max.y * bitmap->row_words * sizeof (word_t)); 1.180 +#endif /* PBM_REVERSE_BITS */ 1.181 + 1.182 + /* $$$ need to invert bits here */ 1.183 + 1.184 +#if 0 1.185 + if (input_attributes.has_page_size) 1.186 + bitmap = resize_bitmap (bitmap, 1.187 + x_resolution, 1.188 + y_resolution, 1.189 + input_attributes); 1.190 +#endif 1.191 + 1.192 +#if 0 1.193 + rotate_bitmap (bitmap, 1.194 + input_attributes); 1.195 +#endif 1.196 + 1.197 + pdf_write_g4_fax_image (page, 1.198 + 0, 0, /* x, y */ 1.199 + image_info->width_points, image_info->height_points, 1.200 + bitmap, 1.201 + 0, /* ImageMask */ 1.202 + 0, 0, 0, /* r, g, b */ 1.203 + 0); /* BlackIs1 */ 1.204 + 1.205 + result = 1; 1.206 + 1.207 + fail: 1.208 + if (bitmap) 1.209 + free_bitmap (bitmap); 1.210 + return (result); 1.211 +} 1.212 + 1.213 + 1.214 +input_handler_t pbm_handler = 1.215 + { 1.216 + match_pbm_suffix, 1.217 + open_pbm_input_file, 1.218 + close_pbm_input_file, 1.219 + last_pbm_input_page, 1.220 + get_pbm_image_info, 1.221 + process_pbm_image 1.222 + }; 1.223 + 1.224 + 1.225 +void init_pbm_handler (void) 1.226 +{ 1.227 + /* why should we let libpbm look at the real args? */ 1.228 + int fake_argc = 1; 1.229 + char *fake_argv [] = { "tumble" }; 1.230 + 1.231 + pbm_init (& fake_argc, fake_argv); 1.232 + install_input_handler (& pbm_handler); 1.233 +}