Mon, 18 Aug 2003 09:59:41 +0000
added big-endian support
1 /*
2 * tumble: build a PDF file from image files
3 *
4 * Input handler dispatch
5 * $Id: tumble_input.c,v 1.4 2003/03/25 01:38:08 eric Exp $
6 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. Note that permission is
11 * not granted to redistribute this program under the terms of any
12 * other version of the General Public License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
22 */
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
32 #include "semantics.h"
33 #include "tumble.h"
34 #include "bitblt.h"
35 #include "pdf.h"
36 #include "tumble_input.h"
39 #define MAX_INPUT_HANDLERS 10
41 static int input_handler_count = 0;
43 static input_handler_t *input_handlers [MAX_INPUT_HANDLERS];
46 static char *in_filename;
47 static FILE *in;
48 static input_handler_t *current_input_handler;
51 void install_input_handler (input_handler_t *handler)
52 {
53 if (input_handler_count >= MAX_INPUT_HANDLERS)
54 fprintf (stderr, "Too many input handlers, table only has room for %d\n", MAX_INPUT_HANDLERS);
55 else
56 input_handlers [input_handler_count++] = handler;
57 }
60 bool match_input_suffix (char *suffix)
61 {
62 int i;
63 for (i = 0; i < input_handler_count; i++)
64 if (input_handlers [i]->match_suffix (suffix))
65 return (1);
66 return (0);
67 }
69 bool open_input_file (char *name)
70 {
71 int i;
73 if (in)
74 {
75 if (strcmp (name, in_filename) == 0)
76 return (1);
77 close_input_file ();
78 }
79 in_filename = strdup (name);
80 if (! in_filename)
81 {
82 fprintf (stderr, "can't strdup input filename '%s'\n", name);
83 goto fail;
84 }
86 in = fopen (name, "rb");
87 if (! in)
88 goto fail;
90 for (i = 0; i < input_handler_count; i++)
91 {
92 if (input_handlers [i]->open_input_file (in, name))
93 break;
94 }
95 if (i >= input_handler_count)
96 {
97 fprintf (stderr, "unrecognized format for input file '%s'\n", name);
98 goto fail;
99 }
100 current_input_handler = input_handlers [i];
101 return (1);
103 fail:
104 if (in)
105 fclose (in);
106 in = NULL;
107 return (0);
108 }
111 bool close_input_file (void)
112 {
113 bool result = 1;
115 if (current_input_handler)
116 {
117 result = current_input_handler->close_input_file ();
118 current_input_handler = NULL;
119 }
120 if (in_filename)
121 free (in_filename);
122 if (in)
123 {
124 fclose (in);
125 in = NULL;
126 }
128 return (result);
129 }
132 bool last_input_page (void)
133 {
134 if (! current_input_handler)
135 return (0);
136 return (current_input_handler->last_input_page ());
137 }
140 bool get_image_info (int image,
141 input_attributes_t input_attributes,
142 image_info_t *image_info)
143 {
144 if (! current_input_handler)
145 return (0);
146 return (current_input_handler->get_image_info (image,
147 input_attributes,
148 image_info));
149 }
151 bool process_image (int image,
152 input_attributes_t input_attributes,
153 image_info_t *image_info,
154 pdf_page_handle page)
155 {
156 if (! current_input_handler)
157 return (0);
158 return (current_input_handler->process_image (image,
159 input_attributes,
160 image_info,
161 page));
162 }