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