/
IMG_tif.c
298 lines (257 loc) · 7.82 KB
1
/*
2
SDL_image: An example image loading library for use with SDL
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
8
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
12
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
*/
22
23
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
24
25
26
27
28
29
30
31
32
33
/* This is a TIFF image file loading framework */
#include <stdio.h>
#include "SDL_image.h"
#ifdef LOAD_TIF
#include <tiffio.h>
34
static struct {
35
36
37
38
39
40
41
int loaded;
void *handle;
TIFF* (*TIFFClientOpen)(const char*, const char*, thandle_t, TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc);
void (*TIFFClose)(TIFF*);
int (*TIFFGetField)(TIFF*, ttag_t, ...);
int (*TIFFReadRGBAImage)(TIFF*, uint32, uint32, uint32*, int);
TIFFErrorHandler (*TIFFSetErrorHandler)(TIFFErrorHandler);
42
43
44
} lib;
#ifdef LOAD_TIF_DYNAMIC
45
int IMG_InitTIF()
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
if ( lib.loaded == 0 ) {
lib.handle = SDL_LoadObject(LOAD_TIF_DYNAMIC);
if ( lib.handle == NULL ) {
return -1;
}
lib.TIFFClientOpen =
(TIFF* (*)(const char*, const char*, thandle_t, TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc))
SDL_LoadFunction(lib.handle, "TIFFClientOpen");
if ( lib.TIFFClientOpen == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.TIFFClose =
(void (*)(TIFF*))
SDL_LoadFunction(lib.handle, "TIFFClose");
if ( lib.TIFFClose == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.TIFFGetField =
(int (*)(TIFF*, ttag_t, ...))
SDL_LoadFunction(lib.handle, "TIFFGetField");
if ( lib.TIFFGetField == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.TIFFReadRGBAImage =
(int (*)(TIFF*, uint32, uint32, uint32*, int))
SDL_LoadFunction(lib.handle, "TIFFReadRGBAImage");
if ( lib.TIFFReadRGBAImage == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.TIFFSetErrorHandler =
(TIFFErrorHandler (*)(TIFFErrorHandler))
SDL_LoadFunction(lib.handle, "TIFFSetErrorHandler");
if ( lib.TIFFSetErrorHandler == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
}
++lib.loaded;
return 0;
91
}
92
void IMG_QuitTIF()
93
{
94
95
96
97
98
99
100
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
SDL_UnloadObject(lib.handle);
}
--lib.loaded;
101
102
}
#else
103
int IMG_InitTIF()
104
{
105
106
107
108
109
110
111
112
113
114
if ( lib.loaded == 0 ) {
lib.TIFFClientOpen = TIFFClientOpen;
lib.TIFFClose = TIFFClose;
lib.TIFFGetField = TIFFGetField;
lib.TIFFReadRGBAImage = TIFFReadRGBAImage;
lib.TIFFSetErrorHandler = TIFFSetErrorHandler;
}
++lib.loaded;
return 0;
115
}
116
void IMG_QuitTIF()
117
{
118
119
120
121
122
123
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
}
--lib.loaded;
124
125
126
}
#endif /* LOAD_TIF_DYNAMIC */
127
128
129
130
131
132
133
/*
* These are the thunking routine to use the SDL_RWops* routines from
* libtiff's internals.
*/
static tsize_t tiff_read(thandle_t fd, tdata_t buf, tsize_t size)
{
134
return SDL_RWread((SDL_RWops*)fd, buf, 1, size);
135
136
137
138
}
static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
{
139
return SDL_RWseek((SDL_RWops*)fd, offset, origin);
140
141
142
143
}
static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
{
144
return SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
145
146
147
148
}
static int tiff_close(thandle_t fd)
{
149
150
/*
* We don't want libtiff closing our SDL_RWops*, but if it's not given
151
* a routine to try, and if the image isn't a TIFF, it'll segfault.
152
153
*/
return 0;
154
155
}
156
157
static int tiff_map(thandle_t fd, tdata_t* pbase, toff_t* psize)
{
158
return (0);
159
160
161
162
}
static void tiff_unmap(thandle_t fd, tdata_t base, toff_t size)
{
163
return;
164
165
}
166
167
static toff_t tiff_size(thandle_t fd)
{
168
169
Sint64 save_pos;
toff_t size;
170
171
172
save_pos = SDL_RWtell((SDL_RWops*)fd);
SDL_RWseek((SDL_RWops*)fd, 0, RW_SEEK_END);
173
size = SDL_RWtell((SDL_RWops*)fd);
174
175
SDL_RWseek((SDL_RWops*)fd, save_pos, RW_SEEK_SET);
return size;
176
177
178
179
}
int IMG_isTIF(SDL_RWops* src)
{
180
181
182
183
184
185
186
187
188
189
Sint64 start;
int is_TIF;
Uint8 magic[4];
if ( !src )
return 0;
start = SDL_RWtell(src);
is_TIF = 0;
if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
if ( (magic[0] == 'I' &&
190
magic[1] == 'I' &&
191
magic[2] == 0x2a &&
192
magic[3] == 0x00) ||
193
(magic[0] == 'M' &&
194
magic[1] == 'M' &&
195
magic[2] == 0x00 &&
196
magic[3] == 0x2a) ) {
197
198
199
200
201
is_TIF = 1;
}
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_TIF);
202
203
204
205
}
SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
{
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
Sint64 start;
TIFF* tiff;
SDL_Surface* surface = NULL;
Uint32 img_width, img_height;
Uint32 Rmask, Gmask, Bmask, Amask;
Uint32 x, y;
Uint32 half;
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);
if ( !IMG_Init(IMG_INIT_TIF) ) {
return NULL;
}
/* turn off memory mapped access with the m flag */
tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src,
tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, tiff_map, tiff_unmap);
if(!tiff)
goto error;
/* Retrieve the dimensions of the image from the TIFF tags */
lib.TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
lib.TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Amask = 0xFF000000;
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, img_width, img_height, 32,
Rmask, Gmask, Bmask, Amask);
if(!surface)
goto error;
if(!lib.TIFFReadRGBAImage(tiff, img_width, img_height, (uint32 *)surface->pixels, 0))
goto error;
/* libtiff loads the image upside-down, flip it back */
half = img_height / 2;
for(y = 0; y < half; y++)
{
Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
Uint32 *bot = (Uint32 *)surface->pixels
+ (img_height - y - 1) * surface->pitch/4;
for(x = 0; x < img_width; x++)
{
Uint32 tmp = top[x];
top[x] = bot[x];
bot[x] = tmp;
}
}
lib.TIFFClose(tiff);
return surface;
263
264
error:
265
266
267
268
269
SDL_RWseek(src, start, RW_SEEK_SET);
if ( surface ) {
SDL_FreeSurface(surface);
}
return NULL;
270
271
272
273
}
#else
274
275
int IMG_InitTIF()
{
276
277
IMG_SetError("TIFF images are not supported");
return(-1);
278
279
280
281
282
283
}
void IMG_QuitTIF()
{
}
284
285
286
/* See if an image is contained in a data source */
int IMG_isTIF(SDL_RWops *src)
{
287
return(0);
288
289
290
291
292
}
/* Load a TIFF type image from an SDL datasource */
SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
{
293
return(NULL);
294
295
296
}
#endif /* LOAD_TIF */
297
298
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */