Thu, 20 Mar 2003 06:54:08 +0000
more JPEG support. added input file handler API.
1 /*
2 * tumble: build a PDF file from image files
3 *
4 * Input handler dispatch
5 * $Id: tumble_input.c,v 1.1 2003/03/19 22:54: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 open_input_file (char *name)
61 {
62 int i;
64 if (in)
65 {
66 if (strcmp (name, in_filename) == 0)
67 return (1);
68 close_input_file ();
69 }
70 in_filename = strdup (name);
71 if (! in_filename)
72 {
73 fprintf (stderr, "can't strdup input filename '%s'\n", name);
74 goto fail;
75 }
77 in = fopen (name, "rb");
78 if (! in)
79 goto fail;
81 for (i = 0; i < input_handler_count; i++)
82 {
83 if (input_handlers [i]->open_input_file (in, name))
84 break;
85 }
86 if (i >= input_handler_count)
87 {
88 fprintf (stderr, "unrecognized format for input file '%s'\n", name);
89 goto fail;
90 }
91 current_input_handler = input_handlers [i];
92 return (1);
94 fail:
95 if (in)
96 fclose (in);
97 in = NULL;
98 return (0);
99 }
102 bool close_input_file (void)
103 {
104 bool result;
106 result = current_input_handler->close_input_file ();
107 if (in_filename)
108 free (in_filename);
109 fclose (in);
110 in = NULL;
112 return (result);
113 }
116 bool last_input_page (void)
117 {
118 return (current_input_handler->last_input_page ());
119 }
122 bool get_image_info (int image,
123 input_attributes_t input_attributes,
124 image_info_t *image_info)
125 {
126 return (current_input_handler->get_image_info (image,
127 input_attributes,
128 image_info));
129 }
131 bool process_image (int image,
132 input_attributes_t input_attributes,
133 image_info_t *image_info,
134 pdf_page_handle page)
135 {
136 return (current_input_handler->process_image (image,
137 input_attributes,
138 image_info,
139 page));
140 }