Thu, 10 Apr 2003 09:02:12 +0000
include tumble version in usage message.
1 /*
2 * tumble: build a PDF file from image files
3 *
4 * $Id: tumble_pbm.c,v 1.1 2003/04/10 00:47:30 eric Exp $
5 * Copyright 2001, 2002, 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 <stdlib.h>
28 #include <strings.h> /* strcasecmp() is a BSDism */
30 #include <pbm.h>
31 /*
32 * pbm_readpbmrow_packed always uses big-endian bit ordering.
33 * On little-endian processors (such as the x86), we want little-endian
34 * bit order, so we must reverse the bits ourselves after we read in the
35 * file.
36 */
37 #define PBM_REVERSE_BITS
40 #include "semantics.h"
41 #include "tumble.h"
42 #include "bitblt.h"
43 #include "pdf.h"
44 #include "tumble_input.h"
47 typedef struct
48 {
49 FILE *f;
50 int rows;
51 int cols;
52 int format;
53 } pbm_info_t;
55 static pbm_info_t pbm;
58 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
61 static bool match_pbm_suffix (char *suffix)
62 {
63 return (strcasecmp (suffix, ".pbm") == 0);
64 }
67 static bool close_pbm_input_file (void)
68 {
69 pbm.f = NULL;
70 return (1);
71 }
74 static bool open_pbm_input_file (FILE *f, char *name)
75 {
76 uint8_t buf [2];
77 size_t l;
79 l = fread (& buf [0], 1, sizeof (buf), f);
80 if (l != sizeof (buf))
81 return (0);
83 rewind (f);
85 if (! (((buf [0] == 'P') && (buf [1] == '1')) ||
86 ((buf [0] == 'P') && (buf [1] == '4'))))
87 return (0);
89 pbm.f = f;
91 pbm_readpbminit (f, & pbm.cols, & pbm.rows, & pbm.format);
93 return (1);
94 }
97 static bool last_pbm_input_page (void)
98 {
99 /* only handle single-page PBM files for now */
100 return (1);
101 }
104 static bool get_pbm_image_info (int image,
105 input_attributes_t input_attributes,
106 image_info_t *image_info)
107 {
108 double x_resolution = 300;
109 double y_resolution = 300;
111 /* $$$ need to handle rotation! */
112 if (input_attributes.has_resolution)
113 {
114 x_resolution = input_attributes.x_resolution;
115 y_resolution = input_attributes.y_resolution;
116 }
118 image_info->width_points = (pbm.cols / x_resolution) * POINTS_PER_INCH;
119 image_info->height_points = (pbm.rows / y_resolution) * POINTS_PER_INCH;
121 if ((image_info->height_points > PAGE_MAX_POINTS) ||
122 (image_info->width_points > PAGE_MAX_POINTS))
123 {
124 fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
125 return (0);
126 }
128 return (1);
129 }
132 static bool process_pbm_image (int image, /* range 1 .. n */
133 input_attributes_t input_attributes,
134 image_info_t *image_info,
135 pdf_page_handle page)
136 {
137 bool result = 0;
138 Rect rect;
139 Bitmap *bitmap = NULL;
141 int row;
143 rect.min.x = 0;
144 rect.min.y = 0;
146 if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
147 {
148 rect.max.x = image_info->height_samples;
149 rect.max.y = image_info->width_samples;
150 }
151 else
152 {
153 rect.max.x = image_info->width_samples;
154 rect.max.y = image_info->height_samples;
155 }
157 bitmap = create_bitmap (& rect);
159 if (! bitmap)
160 {
161 fprintf (stderr, "can't allocate bitmap\n");
162 fprintf (stderr, "width %d height %d\n", image_info->width_samples, image_info->height_samples);
163 goto fail;
164 }
166 for (row = 0; row < rect.max.y; row++)
167 {
168 pbm_readpbmrow_packed (pbm.f,
169 (unsigned char *) (bitmap->bits + row * bitmap->row_words),
170 pbm.cols,
171 pbm.format);
172 }
174 #ifdef PBM_REVERSE_BITS
175 reverse_bits ((uint8_t *) bitmap->bits,
176 rect.max.y * bitmap->row_words * sizeof (word_t));
177 #endif /* PBM_REVERSE_BITS */
179 /* $$$ need to invert bits here */
181 #if 0
182 if (input_attributes.has_page_size)
183 bitmap = resize_bitmap (bitmap,
184 x_resolution,
185 y_resolution,
186 input_attributes);
187 #endif
189 #if 0
190 rotate_bitmap (bitmap,
191 input_attributes);
192 #endif
194 pdf_write_g4_fax_image (page,
195 0, 0, /* x, y */
196 image_info->width_points, image_info->height_points,
197 bitmap,
198 0, /* ImageMask */
199 0, 0, 0, /* r, g, b */
200 0); /* BlackIs1 */
202 result = 1;
204 fail:
205 if (bitmap)
206 free_bitmap (bitmap);
207 return (result);
208 }
211 input_handler_t pbm_handler =
212 {
213 match_pbm_suffix,
214 open_pbm_input_file,
215 close_pbm_input_file,
216 last_pbm_input_page,
217 get_pbm_image_info,
218 process_pbm_image
219 };
222 void init_pbm_handler (void)
223 {
224 /* why should we let libpbm look at the real args? */
225 int fake_argc = 1;
226 char *fake_argv [] = { "tumble" };
228 pbm_init (& fake_argc, fake_argv);
229 install_input_handler (& pbm_handler);
230 }