1 /*
2  * tumble: build a PDF file from image files
3  *
4  * Copyright 2001, 2002, 2003, 2017 Eric Smith <spacewar@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.  Note that permission is
9  * not granted to redistribute this program under the terms of any
10  * other version of the General Public License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
20  *
21  *  2009-03-02 [JDB] Add support for overlay images.
22  */
23 
24 
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <strings.h>  /* strcasecmp() is a BSDism */
30 
31 #include <netpbm/pbm.h>
32 /*
33  * pbm_readpbmrow_packed always uses big-endian bit ordering.
34  * On little-endian processors (such as the x86), we want little-endian
35  * bit order, so we must reverse the bits ourselves after we read in the
36  * file.
37  */
38 #define PBM_REVERSE_BITS
39 
40 
41 #include "semantics.h"
42 #include "tumble.h"
43 #include "bitblt.h"
44 #include "pdf.h"
45 #include "tumble_input.h"
46 
47 
48 typedef struct
49 {
50   FILE *f;
51   int rows;
52   int cols;
53   int format;
54 } pbm_info_t;
55 
56 static pbm_info_t pbm;
57 
58 
59 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
60 
61 
match_pbm_suffix(char * suffix)62 static bool match_pbm_suffix (char *suffix)
63 {
64   return (strcasecmp (suffix, ".pbm") == 0);
65 }
66 
67 
close_pbm_input_file(void)68 static bool close_pbm_input_file (void)
69 {
70   pbm.f = NULL;
71   return (1);
72 }
73 
74 
open_pbm_input_file(FILE * f,char * name)75 static bool open_pbm_input_file (FILE *f, char *name)
76 {
77   uint8_t buf [2];
78   size_t l;
79 
80   l = fread (& buf [0], 1, sizeof (buf), f);
81   if (l != sizeof (buf))
82     return (0);
83 
84   rewind (f);
85 
86   if (! (((buf [0] == 'P') && (buf [1] == '1')) ||
87 	 ((buf [0] == 'P') && (buf [1] == '4'))))
88     return (0);
89 
90   pbm.f = f;
91 
92   pbm_readpbminit (f, & pbm.cols, & pbm.rows, & pbm.format);
93 
94   return (1);
95 }
96 
97 
last_pbm_input_page(void)98 static bool last_pbm_input_page (void)
99 {
100   /* only handle single-page PBM files for now */
101   return (1);
102 }
103 
104 
get_pbm_image_info(int image,input_attributes_t input_attributes,image_info_t * image_info)105 static bool get_pbm_image_info (int image,
106 				input_attributes_t input_attributes,
107 				image_info_t *image_info)
108 {
109   double x_resolution = 300;
110   double y_resolution = 300;
111   double dest_x_resolution, dest_y_resolution;
112 
113   if (input_attributes.has_resolution)
114     {
115       x_resolution = input_attributes.x_resolution;
116       y_resolution = input_attributes.y_resolution;
117     }
118 
119   if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
120     {
121       image_info->width_samples = pbm.rows;
122       image_info->height_samples = pbm.cols;
123       dest_x_resolution = y_resolution;
124       dest_y_resolution = x_resolution;
125     }
126   else
127     {
128       image_info->width_samples = pbm.cols;
129       image_info->height_samples = pbm.rows;
130       dest_x_resolution = x_resolution;
131       dest_y_resolution = y_resolution;
132     }
133 
134 
135   image_info->width_points = (image_info->width_samples / dest_x_resolution) * POINTS_PER_INCH;
136   image_info->height_points = (image_info->height_samples / dest_y_resolution) * POINTS_PER_INCH;
137 
138   if ((image_info->height_points > PAGE_MAX_POINTS) ||
139       (image_info->width_points > PAGE_MAX_POINTS))
140     {
141       fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
142       return (0);
143     }
144 
145   image_info->negative = false;
146 
147   return (1);
148 }
149 
150 
process_pbm_image(int image,input_attributes_t input_attributes,image_info_t * image_info,pdf_page_handle page,position_t position)151 static bool process_pbm_image (int image,  /* range 1 .. n */
152 			       input_attributes_t input_attributes,
153 			       image_info_t *image_info,
154 			       pdf_page_handle page,
155 			       position_t position)
156 {
157   bool result = 0;
158   Rect rect;
159   Bitmap *bitmap = NULL;
160 
161   int row;
162 
163   rect.min.x = 0;
164   rect.min.y = 0;
165 
166   if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
167     {
168       rect.max.x = image_info->height_samples;
169       rect.max.y = image_info->width_samples;
170     }
171   else
172     {
173       rect.max.x = image_info->width_samples;
174       rect.max.y = image_info->height_samples;
175     }
176 
177   bitmap = create_bitmap (& rect);
178 
179   if (! bitmap)
180     {
181       fprintf (stderr, "can't allocate bitmap\n");
182       fprintf (stderr, "width %d height %d\n", image_info->width_samples, image_info->height_samples);
183       goto fail;
184     }
185 
186   for (row = 0; row < rect.max.y; row++)
187     {
188       pbm_readpbmrow_packed (pbm.f,
189 			     (unsigned char *) (bitmap->bits + row * bitmap->row_words),
190 			     pbm.cols,
191 			     pbm.format);
192       }
193 
194 #ifdef PBM_REVERSE_BITS
195   reverse_bits ((uint8_t *) bitmap->bits,
196 		rect.max.y * bitmap->row_words * sizeof (word_t));
197 #endif /* PBM_REVERSE_BITS */
198 
199   /* $$$ need to invert bits here */
200 
201 #if 0
202   if (input_attributes.has_page_size)
203     bitmap = resize_bitmap (bitmap,
204 			    x_resolution,
205 			    y_resolution,
206 			    input_attributes);
207 #endif
208 
209   rotate_bitmap (bitmap, input_attributes.rotation);
210 
211   pdf_write_g4_fax_image (page,
212 			  position.x, position.y,
213 			  image_info->width_points, image_info->height_points,
214 			  image_info->negative,
215 			  bitmap,
216 			  NULL,
217 			  NULL);
218 
219   result = 1;
220 
221  fail:
222   if (bitmap)
223     free_bitmap (bitmap);
224   return (result);
225 }
226 
227 
228 input_handler_t pbm_handler =
229   {
230     match_pbm_suffix,
231     open_pbm_input_file,
232     close_pbm_input_file,
233     last_pbm_input_page,
234     get_pbm_image_info,
235     process_pbm_image
236   };
237 
238 
init_pbm_handler(void)239 void init_pbm_handler (void)
240 {
241   /* why should we let libpbm look at the real args? */
242   int fake_argc = 1;
243   char *fake_argv [] = { "tumble" };
244 
245   pbm_init (& fake_argc, fake_argv);
246   install_input_handler (& pbm_handler);
247 }
248