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