1 /* input.c: interface for input handlers
2
3 Copyright (C) 1999, 2000, 2001 Bernhard Herzog.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License
7 as published by the Free Software Foundation; either version 2.1 of
8 the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 USA. */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif /* Def: HAVE_CONFIG_H */
23
24 #include "autotrace.h"
25 #include "input.h"
26 #include "input-pnm.h"
27 #include "input-bmp.h"
28 #include "input-tga.h"
29 #ifdef HAVE_LIBPNG
30 #include "input-png.h"
31 #endif /* HAVE_LIBPNG */
32 #if HAVE_MAGICK
33 #include <sys/types.h> /* Needed for correct interpretation of magick/api.h */
34 #include <magick/api.h>
35 #include "input-magick.h"
36 #endif /* HAVE_MAGICK */
37
38 #include "xstd.h"
39 #include "filename.h"
40 #include "strgicmp.h"
41 #include <string.h>
42
43 struct input_format_entry {
44 const char * name;
45 const char * descr;
46 at_input_read_func reader;
47 };
48
49 #define END {NULL, NULL, NULL}
50 static struct input_format_entry input_formats[] = {
51 #ifdef HAVE_LIBPNG
52 { "PNG", "Portable network graphics", input_png_reader},
53 #endif /* HAVE_LIBPNG */
54 { "TGA", "Truevision Targa image", input_tga_reader },
55 { "PBM", "Portable bitmap format", input_pnm_reader },
56 { "PNM", "Portable anymap format", input_pnm_reader },
57 { "PGM", "Portable graymap format", input_pnm_reader },
58 { "PPM", "Portable pixmap format", input_pnm_reader },
59 { "BMP", "Microsoft Windows bitmap image", input_bmp_reader },
60 END
61 };
62
63 at_input_read_func
at_input_get_handler(at_string filename)64 at_input_get_handler (at_string filename)
65 {
66 char * ext = find_suffix (filename);
67 if (ext == NULL)
68 ext = "";
69
70 return at_input_get_handler_by_suffix (ext);
71 }
72
73 at_input_read_func
at_input_get_handler_by_suffix(at_string suffix)74 at_input_get_handler_by_suffix (at_string suffix)
75 {
76 struct input_format_entry * format;
77
78 if (!suffix || suffix[0] == '\0')
79 return NULL;
80
81 for (format = input_formats ; format->name; format++)
82 {
83 if (strgicmp (suffix, format->name))
84 {
85 return format->reader;
86 }
87 }
88 #if HAVE_MAGICK
89 return (at_input_read_func)input_magick_reader;
90 #else
91 return NULL;
92 #endif /* HAVE_MAGICK */
93 }
94
95 char **
at_input_list_new(void)96 at_input_list_new (void)
97 {
98 char ** list;
99 int count, count_int = 0;
100 int i;
101 #if HAVE_MAGICK
102 ExceptionInfo exception;
103 #if (MagickLibVersion < 0x0540)
104 MagickInfo *info, *magickinfo;
105 #else
106 const MagickInfo *info, *magickinfo;
107 #endif
108 #endif
109
110 struct input_format_entry * entry;
111 for (entry = input_formats; entry->name; entry++)
112 count_int++;
113 #if HAVE_MAGICK
114 #if (MagickLibVersion < 0x0538)
115 MagickIncarnate("");
116 #else
117 InitializeMagick("");
118 #endif
119 GetExceptionInfo(&exception);
120 #if (MagickLibVersion < 0x0534)
121 magickinfo = info = GetMagickInfo(NULL);
122 #else
123 info = GetMagickInfo(NULL, &exception);
124 if (info && !info->next)
125 info = GetMagickInfo("*", &exception);
126 magickinfo = info;
127 #endif
128 #endif
129 count = count_int;
130 #if HAVE_MAGICK
131 while (info)
132 {
133 #if (MagickLibVersion < 0x0537)
134 if (info->tag && info->description)
135 #else
136 if (info->name && info->description)
137 #endif
138 count ++;
139 info = info->next ;
140 }
141 #endif
142
143 XMALLOC(list, sizeof(char*)*((2*count)+1));
144
145 entry = input_formats;
146 for (i = 0; i < count_int; i++)
147 {
148 list[2*i] = (char *)entry[i].name;
149 list[2*i+1] = (char *)entry[i].descr;
150 }
151
152 #if HAVE_MAGICK
153 info = magickinfo;
154
155 while (info)
156 {
157 #if (MagickLibVersion < 0x0537)
158 if (info->tag && info->description)
159 #else
160 if (info->name && info->description)
161 #endif
162 {
163 #if (MagickLibVersion < 0x0537)
164 list[2*i] = info->tag;
165 #else
166 list[2*i] = info->name;
167 #endif
168 list[2*i+1] = info->description;
169 i++;
170 }
171 info = info->next ;
172 }
173 #endif
174 list[2*i] = NULL;
175 return list;
176 }
177
178 void
at_input_list_free(char ** list)179 at_input_list_free(char ** list)
180 {
181 free(list);
182 }
183
184 char *
at_input_shortlist(void)185 at_input_shortlist (void)
186 {
187 char * list;
188 int count_int = 0;
189 size_t length = 0;
190 int i;
191 #if HAVE_MAGICK
192 ExceptionInfo exception;
193 #if (MagickLibVersion < 0x0540)
194 MagickInfo *info, *magickinfo;
195 #else
196 const MagickInfo *info, *magickinfo;
197 #endif
198 #endif
199
200 struct input_format_entry * entry;
201 for (entry = input_formats; entry->name; entry++)
202 {
203 count_int++;
204 length += strlen (entry->name) + 2;
205 }
206
207 #if HAVE_MAGICK
208 #if (MagickLibVersion < 0x0538)
209 MagickIncarnate("");
210 #else
211 InitializeMagick("");
212 #endif
213 GetExceptionInfo(&exception);
214 #if (MagickLibVersion < 0x0534)
215 magickinfo = info = GetMagickInfo(NULL);
216 #else
217 magickinfo = info = GetMagickInfo(NULL, &exception);
218 #endif
219 #endif
220 #if HAVE_MAGICK
221 while (info)
222 {
223 #if (MagickLibVersion < 0x0537)
224 if (info->tag && info->description)
225 #else
226 if (info->name && info->description)
227 #endif
228 {
229 #if (MagickLibVersion < 0x0537)
230 length += strlen (info->tag) + 2;
231 #else
232 length += strlen (info->name) + 2;
233 #endif
234 }
235 info = info->next ;
236 }
237 #endif
238
239 XMALLOC(list, sizeof (char) * (length + 1 + 2));
240
241 entry = input_formats;
242 strcpy (list, (char *) entry[0].name);
243 for (i = 1; i < count_int - 1; i++)
244 {
245 strcat (list, ", ");
246 strcat (list, (char *) entry[i].name);
247 }
248 #if HAVE_MAGICK
249 info = magickinfo;
250 while (info)
251 {
252 #if (MagickLibVersion < 0x0537)
253 if (info->tag && info->description)
254 #else
255 if (info->name && info->description)
256 #endif
257 {
258 strcat (list, ", ");
259 #if (MagickLibVersion < 0x0537)
260 strcat (list, info->tag);
261 #else
262 strcat (list, info->name);
263 #endif
264 }
265 info = info->next ;
266 }
267 #endif
268 strcat (list, " or ");
269 strcat (list, (char *) entry[i].name);
270 return list;
271 }
272
273 int
at_input_add_handler(at_string suffix,at_string description,at_input_read_func func)274 at_input_add_handler (at_string suffix,
275 at_string description,
276 at_input_read_func func)
277 {
278 return 0;
279 }
280