Skip to content

Latest commit

 

History

History
287 lines (246 loc) · 6.87 KB

IMG_tif.c

File metadata and controls

287 lines (246 loc) · 6.87 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 14, 2001
Dec 14, 2001
2
SDL_image: An example image loading library for use with SDL
Feb 4, 2006
Feb 4, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Aug 10, 2000
Aug 10, 2000
4
5
This library is free software; you can redistribute it and/or
Feb 4, 2006
Feb 4, 2006
6
modify it under the terms of the GNU Lesser General Public
Aug 10, 2000
Aug 10, 2000
7
License as published by the Free Software Foundation; either
Feb 4, 2006
Feb 4, 2006
8
version 2.1 of the License, or (at your option) any later version.
Aug 10, 2000
Aug 10, 2000
9
10
11
12
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
Feb 4, 2006
Feb 4, 2006
13
Lesser General Public License for more details.
Aug 10, 2000
Aug 10, 2000
14
Feb 4, 2006
Feb 4, 2006
15
16
17
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Aug 10, 2000
Aug 10, 2000
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Aug 10, 2000
Aug 10, 2000
21
22
23
24
25
26
27
28
29
30
31
32
*/
/* This is a TIFF image file loading framework */
#include <stdio.h>
#include "SDL_image.h"
#ifdef LOAD_TIF
#include <tiffio.h>
May 12, 2006
May 12, 2006
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
static struct {
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);
} lib;
#ifdef LOAD_TIF_DYNAMIC
int IMG_InitTIF()
{
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;
}
void IMG_QuitTIF()
{
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
SDL_UnloadObject(lib.handle);
}
--lib.loaded;
}
#else
int IMG_InitTIF()
{
if ( lib.loaded == 0 ) {
lib.TIFFClientOpen = TIFFClientOpen;
lib.TIFFClose = TIFFClose;
lib.TIFFGetField = TIFFGetField;
lib.TIFFReadRGBAImage = TIFFReadRGBAImage;
lib.TIFFSetErrorHandler = TIFFSetErrorHandler;
}
++lib.loaded;
May 13, 2006
May 13, 2006
112
113
return 0;
May 12, 2006
May 12, 2006
114
115
116
117
118
119
120
121
122
123
124
125
}
void IMG_QuitTIF()
{
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
}
--lib.loaded;
}
#endif /* LOAD_TIF_DYNAMIC */
Aug 10, 2000
Aug 10, 2000
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
/*
* 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)
{
return SDL_RWread((SDL_RWops*)fd, buf, 1, size);
}
static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
{
return SDL_RWseek((SDL_RWops*)fd, offset, origin);
}
static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
{
return SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
}
static int tiff_close(thandle_t fd)
{
/*
* We don't want libtiff closing our SDL_RWops*, but if it's not given
* a routine to try, and if the image isn't a TIFF, it'll segfault.
*/
return 0;
}
static toff_t tiff_size(thandle_t fd)
{
Uint32 save_pos;
toff_t size;
save_pos = SDL_RWtell((SDL_RWops*)fd);
SDL_RWseek((SDL_RWops*)fd, 0, SEEK_END);
size = SDL_RWtell((SDL_RWops*)fd);
SDL_RWseek((SDL_RWops*)fd, save_pos, SEEK_SET);
return size;
}
int IMG_isTIF(SDL_RWops* src)
{
Feb 4, 2006
Feb 4, 2006
169
170
int start;
int is_TIF;
Aug 10, 2000
Aug 10, 2000
171
172
173
TIFF* tiff;
TIFFErrorHandler prev_handler;
May 12, 2006
May 12, 2006
174
175
176
if ( IMG_InitTIF() < 0 ) {
return 0;
}
Feb 4, 2006
Feb 4, 2006
177
178
179
start = SDL_RWtell(src);
is_TIF = 0;
Aug 10, 2000
Aug 10, 2000
180
/* Suppress output from libtiff */
May 12, 2006
May 12, 2006
181
prev_handler = lib.TIFFSetErrorHandler(NULL);
Aug 10, 2000
Aug 10, 2000
182
183
184
/* Attempt to process the given file data */
/* turn off memory mapped access with the m flag */
May 12, 2006
May 12, 2006
185
tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src,
Aug 10, 2000
Aug 10, 2000
186
187
188
tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
/* Reset the default error handler, since it can be useful for info */
May 12, 2006
May 12, 2006
189
lib.TIFFSetErrorHandler(prev_handler);
Aug 10, 2000
Aug 10, 2000
190
191
/* If it's not a TIFF, then tiff will be NULL. */
Feb 4, 2006
Feb 4, 2006
192
193
if ( tiff ) {
is_TIF = 1;
Aug 10, 2000
Aug 10, 2000
194
Feb 4, 2006
Feb 4, 2006
195
/* Free up any dynamically allocated memory libtiff uses */
May 12, 2006
May 12, 2006
196
lib.TIFFClose(tiff);
Feb 4, 2006
Feb 4, 2006
197
198
}
SDL_RWseek(src, start, SEEK_SET);
May 12, 2006
May 12, 2006
199
IMG_QuitTIF();
Feb 4, 2006
Feb 4, 2006
200
return(is_TIF);
Aug 10, 2000
Aug 10, 2000
201
202
203
204
}
SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
{
Feb 4, 2006
Feb 4, 2006
205
int start;
Aug 10, 2000
Aug 10, 2000
206
207
208
TIFF* tiff;
SDL_Surface* surface = NULL;
Uint32 img_width, img_height;
May 12, 2006
May 12, 2006
209
Uint32 Rmask, Gmask, Bmask, Amask;
Aug 10, 2000
Aug 10, 2000
210
211
212
Uint32 x, y;
Uint32 half;
Jan 4, 2004
Jan 4, 2004
213
214
215
216
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Feb 4, 2006
Feb 4, 2006
217
start = SDL_RWtell(src);
Aug 10, 2000
Aug 10, 2000
218
May 12, 2006
May 12, 2006
219
220
221
222
if ( IMG_InitTIF() < 0 ) {
return NULL;
}
Aug 10, 2000
Aug 10, 2000
223
/* turn off memory mapped access with the m flag */
May 12, 2006
May 12, 2006
224
tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src,
Aug 10, 2000
Aug 10, 2000
225
226
tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
if(!tiff)
Feb 4, 2006
Feb 4, 2006
227
goto error;
Aug 10, 2000
Aug 10, 2000
228
229
/* Retrieve the dimensions of the image from the TIFF tags */
May 12, 2006
May 12, 2006
230
231
lib.TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
lib.TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
Aug 10, 2000
Aug 10, 2000
232
Sep 1, 2000
Sep 1, 2000
233
234
235
236
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Amask = 0xFF000000;
Aug 10, 2000
Aug 10, 2000
237
238
239
surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
Rmask, Gmask, Bmask, Amask);
if(!surface)
Feb 4, 2006
Feb 4, 2006
240
goto error;
Aug 10, 2000
Aug 10, 2000
241
May 12, 2006
May 12, 2006
242
if(!lib.TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
Feb 4, 2006
Feb 4, 2006
243
goto error;
Aug 10, 2000
Aug 10, 2000
244
Sep 1, 2000
Sep 1, 2000
245
/* libtiff loads the image upside-down, flip it back */
Aug 10, 2000
Aug 10, 2000
246
half = img_height / 2;
Sep 1, 2000
Sep 1, 2000
247
for(y = 0; y < half; y++)
Aug 10, 2000
Aug 10, 2000
248
{
Sep 1, 2000
Sep 1, 2000
249
250
251
Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
Uint32 *bot = (Uint32 *)surface->pixels
+ (img_height - y - 1) * surface->pitch/4;
Aug 10, 2000
Aug 10, 2000
252
253
for(x = 0; x < img_width; x++)
{
Sep 1, 2000
Sep 1, 2000
254
255
256
Uint32 tmp = top[x];
top[x] = bot[x];
bot[x] = tmp;
Aug 10, 2000
Aug 10, 2000
257
258
}
}
May 12, 2006
May 12, 2006
259
260
lib.TIFFClose(tiff);
IMG_QuitTIF();
Aug 10, 2000
Aug 10, 2000
261
262
return surface;
Feb 4, 2006
Feb 4, 2006
263
264
265
266
267
268
error:
SDL_RWseek(src, start, SEEK_SET);
if ( surface ) {
SDL_FreeSurface(surface);
}
May 12, 2006
May 12, 2006
269
IMG_QuitTIF();
Feb 4, 2006
Feb 4, 2006
270
return NULL;
Aug 10, 2000
Aug 10, 2000
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
}
#else
/* See if an image is contained in a data source */
int IMG_isTIF(SDL_RWops *src)
{
return(0);
}
/* Load a TIFF type image from an SDL datasource */
SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
{
return(NULL);
}
#endif /* LOAD_TIF */