File: rwTable.c

package info (click to toggle)
xpaint 2.9.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,552 kB
  • sloc: ansic: 73,017; sh: 492; yacc: 247; lex: 126; sed: 43; makefile: 10
file content (353 lines) | stat: -rw-r--r-- 8,161 bytes parent folder | download | duplicates (7)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* +-------------------------------------------------------------------+ */
/* | Copyright 1993, David Koblas (koblas@netcom.com)		       | */
/* |								       | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.	 There is no	       | */
/* | representations about the suitability of this software for	       | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.					       | */
/* |								       | */
/* +-------------------------------------------------------------------+ */

/* $Id: rwTable.c,v 1.22 2005/03/20 20:15:34 demailly Exp $ */

#if defined(HAVE_PARAM_H)
#include <sys/param.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>

#include <stdio.h>
#include "image.h"
#include "rwTable.h"
#include <string.h>
#include <errno.h>

#ifdef MISSING_STDARG_H
#include <varargs.h>
#else
#include <stdarg.h>
#endif

typedef char *String;
#include "../messages.h"

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif

typedef struct {
    char *name;
    RWreadFunc read;
    RWwriteFunc write;
    RWtestFunc test;
} ImageTypes;

static char RWtableMsg[512];

/*
**  Define all the read/write functions here.
**    [ unfortunately most compilers won't take
**	the "variable" name version of these. ]
 */

extern Image *ReadBMP(char *);
extern Image *ReadXBM(char *);
extern Image *ReadLXP(char *);
extern Image *ReadXWD(char *);
extern Image *ReadPNM(char *);
extern Image *ReadPNG(char *);
extern Image *ReadGIF(char *);
extern Image *ReadSGI(char *);
extern Image *ReadXPM(char *);
extern Image *ReadTIFF(char *);
extern Image *ReadJPEG(char *);
extern Image *ReadICO(char *);
extern Image *ReadScriptC(char *);
extern Image *ReadPS_(char *);
extern int WriteBMP(char *, Image *);
extern int WriteXBM(char *, Image *);
extern int WriteLXP(char *, Image *);
extern int WriteXWD(char *, Image *);
extern int WritePNM(char *, Image *);
extern int WriteXPM(char *, Image *);
extern int WritePNGn(char *, Image *);
extern int WritePNGi(char *, Image *);
extern int WriteGIF(char *, Image *);
extern int WriteSGI(char *, Image *);
extern int WriteTIFF(char *, Image *);
extern int WriteJPEG(char *, Image *);
extern int WriteICO(char *, Image *);
extern int WritePDF(char *, Image *);
extern int WritePS(char *, Image *);
extern int TestBMP(char *);
extern int TestICO(char *);
extern int TestPNM(char *);
extern int TestLXP(char *);
extern int TestXWD(char *);
extern int TestXBM(char *);
extern int TestXPM(char *);
extern int TestPNG(char *);
extern int TestGIF(char *);
extern int TestSGI(char *);
extern int TestTIFF(char *);
extern int TestJPEG(char *);
extern int TestScriptC(char *);
extern int TestPS_(char *);
extern int TestTEX(char *);

#define DEF_READ_ENTRY	0
#define DEF_WRITE_ENTRY	0

/* GRR 960219:  added PNG, alphabetized image types: */
static ImageTypes RWtable[] =
{
    {"Auto_Detect",      readMagic, writeMagic, NULL },
#ifdef HAVE_PNG   /* ReadPNG does all PNG files; no need for two entries */
    {"PNG_Format",       ReadPNG,   WritePNGn, TestPNG },
    {"PNG_Interlaced",	 NULL,      WritePNGi, TestPNG },
#endif
    {"GIF_Format",       ReadGIF,   WriteGIF,  TestGIF },
#ifdef HAVE_JPEG
    {"JPEG_Format",      ReadJPEG,  WriteJPEG, TestJPEG},
#endif
#ifdef HAVE_TIFF
    {"TIFF_Format",      ReadTIFF,  WriteTIFF, TestTIFF},
#endif
    {"BMP_Format",       ReadBMP,   WriteBMP,  TestBMP },
    {"ICO_Format",       ReadICO,   WriteICO,  TestICO },
    {"PPM_Format",       ReadPNM,   WritePNM,  TestPNM },
#ifdef HAVE_SGI
    {"SGI_Format",       ReadSGI,   WriteSGI,  TestSGI },
#endif
    {"XBM_Format",       ReadXBM,   WriteXBM,  TestXBM },
    {"XPM_Format",       ReadXPM,   WriteXPM,  TestXPM },
    {"XWD_Format",       ReadXWD,   WriteXWD,  TestXWD },
    {"LXP_Format",       ReadLXP,   WriteLXP,  TestLXP },
    {"PS_Format",        ReadPS_,   WritePS,   TestPS_ },
    {"PDF_Format",       ReadPS_,   WritePDF,  TestPS_ },
    {"TEX_Format",       ReadPS_,   NULL,      TestPS_ },
    {"CSC_Format",       ReadScriptC,   NULL,  TestScriptC }
};

#define	FMT_NUMBER	(sizeof(RWtable) / sizeof(RWtable[0]))

static char *readList[FMT_NUMBER + 1];
static char *writeList[FMT_NUMBER + 1];

/*
**  Special reader that uses the above information.
 */
static char *usedMagicReader = NULL;
int file_isSpecialImage;
int file_numpages;
int file_force = 1;
int file_bbox = 1;
int file_transparent = 0;
int file_specified_zoom = 0;

RWwriteFunc
RWtableGetWriterFromSuffix(char *suffix)
{
    int i;
    if (!suffix || !*suffix) return WritePNGn;
    if (!strcasecmp(suffix, "C")) suffix = "CSC";
    if (!strcasecmp(suffix, "JPG")) suffix = "JPEG";

    for (i = 1; i < FMT_NUMBER; i++) {
      if (!strncasecmp(RWtable[i].name, suffix, strlen(suffix)) && 
          RWtable[i].write)
	    return RWtable[i].write;
    }
    /* default to WritePNG otherwise */
    return WritePNGn;
}

Image *
readMagic(char *file)
{
    int i;

    errno = 0;
    file_isSpecialImage = 0;
    file_transparent = 0;
    file_numpages = 1;
    file_specified_zoom = 0;

    for (i = 0; i < FMT_NUMBER; i++) {
	if (RWtable[i].read == NULL || RWtable[i].test == NULL)
	    continue;
	if (!RWtable[i].test(file))
	    continue; 
        usedMagicReader = RWtable[i].name;
	return RWtable[i].read(file);
    }

    if (errno == 0)
	RWSetMsg(msgText[UNKNOWN_IMAGE_FORMAT]);

    return NULL;
}

int 
writeMagic(char *file, Image *image)
{
    RWwriteFunc proc;
    char *ptr;
    ptr = strrchr(file, '.');
    if (ptr) {
        ++ptr;
        proc = RWtableGetWriterFromSuffix(ptr);
    } else
        proc = WritePNGn;
    return proc(file, image);
}

void *
RWtableGetReaderID()
{
    return (void *) usedMagicReader;
}


/*
**  Give a name, return an "opaque" handle to some information
 */
void *
RWtableGetEntry(char *name)
{
    int i;

    for (i = 0; i < FMT_NUMBER; i++)
	if (strcmp(name, RWtable[i].name) == 0)
	    return (void *) &RWtable[i];
    return NULL;
}

char *
RWtableGetId(void *v)
{
    ImageTypes *entry = (ImageTypes *) v;

    if (entry == NULL)
	return NULL;

    return entry->name;
}

RWreadFunc
RWtableGetReader(void *entry)
{
    RWtableMsg[0] = '\0';

    if (entry == NULL)
	return RWtable[DEF_READ_ENTRY].read;

    return ((ImageTypes *) entry)->read;
}

RWwriteFunc
RWtableGetWriter(void *entry)
{
    RWtableMsg[0] = '\0';

    if (entry == NULL)
	return RWtable[DEF_WRITE_ENTRY].write;

    return ((ImageTypes *) entry)->write;
}

char **
RWtableGetReaderList()
{
    static int done = FALSE;
    int i, idx = 0;

    if (!done) {
	for (i = 0; i < FMT_NUMBER; i++)
	    if (RWtable[i].read != NULL)
		readList[idx++] = RWtable[i].name;
	readList[idx++] = NULL;
	done = TRUE;
    }
    return readList;
}

char **
RWtableGetWriterList()
{
    static int done = FALSE;
    int i, idx = 0;

    if (!done) {
	for (i = 0; i < FMT_NUMBER; i++)
	    if (RWtable[i].write != NULL)
		writeList[idx++] = RWtable[i].name;
	writeList[idx++] = NULL;
	done = TRUE;
    }
    return writeList;
}

char *
RWGetMsg()
{
#ifndef __NetBSD__
#if defined(BSD4_4)
    __const extern char *__const sys_errlist[];
#else
#ifndef __GLIBC__
#ifndef SYS_ERRLIST_DEFINED
#ifdef __CYGWIN__
#  define sys_errlist _sys_errlist
#else
    extern char *sys_errlist[];
#endif
#endif
#endif
#endif
#endif

    if (RWtableMsg[0] == '\0') {
	if (errno == 0)
	    return "";
#if defined(__STDC__) && !defined(MISSING_STRERROR)
	return strerror(errno);
#else
	return sys_errlist[errno];
#endif
    }
    return RWtableMsg;
}

#ifdef MISSING_STDARG_H
void RWSetMsg(va_alist)
va_dcl
{
    va_list ap;
    char *fmt;

    va_start(ap);
    fmt = va_arg(ap, char *);
    vsprintf(RWtableMsg, fmt, ap);
}

#else
void RWSetMsg(char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vsprintf(RWtableMsg, fmt, ap);
}
#endif

Image *
ImageFromFile(char *file)
{
    return readMagic(file);
}