tumble_input.c

Thu, 20 Mar 2003 06:54:08 +0000

author
eric
date
Thu, 20 Mar 2003 06:54:08 +0000
changeset 141
752599b50ff3
child 142
cfa664f3129c
permissions
-rw-r--r--

more JPEG support. added input file handler API.

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