Skip to content

Latest commit

 

History

History
241 lines (201 loc) · 6.43 KB

IMG_tif.c

File metadata and controls

241 lines (201 loc) · 6.43 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Mar 1, 2018
Mar 1, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
Aug 10, 2000
Aug 10, 2000
4
Dec 31, 2011
Dec 31, 2011
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.
Aug 10, 2000
Aug 10, 2000
8
Dec 31, 2011
Dec 31, 2011
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:
Aug 10, 2000
Aug 10, 2000
12
Dec 31, 2011
Dec 31, 2011
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.
Aug 10, 2000
Aug 10, 2000
20
21
*/
Sep 12, 2017
Sep 12, 2017
22
#if !(defined(__APPLE__) || defined(SDL_IMAGE_USE_WIC_BACKEND)) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
Nov 10, 2009
Nov 10, 2009
23
Aug 10, 2000
Aug 10, 2000
24
25
26
27
28
29
30
31
/* This is a TIFF image file loading framework */
#include "SDL_image.h"
#ifdef LOAD_TIF
#include <tiffio.h>
May 12, 2006
May 12, 2006
32
static struct {
May 22, 2013
May 22, 2013
33
34
35
36
37
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, ...);
Nov 1, 2016
Nov 1, 2016
38
int (*TIFFReadRGBAImageOriented)(TIFF*, uint32, uint32, uint32*, int, int);
May 22, 2013
May 22, 2013
39
TIFFErrorHandler (*TIFFSetErrorHandler)(TIFFErrorHandler);
May 12, 2006
May 12, 2006
40
41
42
} lib;
#ifdef LOAD_TIF_DYNAMIC
Oct 22, 2017
Oct 22, 2017
43
44
45
46
47
48
49
50
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = (SIG) SDL_LoadFunction(lib.handle, #FUNC); \
if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = FUNC;
#endif
Sep 26, 2009
Sep 26, 2009
51
int IMG_InitTIF()
May 12, 2006
May 12, 2006
52
{
May 22, 2013
May 22, 2013
53
if ( lib.loaded == 0 ) {
Oct 22, 2017
Oct 22, 2017
54
#ifdef LOAD_TIF_DYNAMIC
May 22, 2013
May 22, 2013
55
56
57
58
lib.handle = SDL_LoadObject(LOAD_TIF_DYNAMIC);
if ( lib.handle == NULL ) {
return -1;
}
Oct 22, 2017
Oct 22, 2017
59
60
61
62
63
64
#endif
FUNCTION_LOADER(TIFFClientOpen, TIFF * (*)(const char*, const char*, thandle_t, TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc))
FUNCTION_LOADER(TIFFClose, void (*)(TIFF*))
FUNCTION_LOADER(TIFFGetField, int (*)(TIFF*, ttag_t, ...))
FUNCTION_LOADER(TIFFReadRGBAImageOriented, int (*)(TIFF*, uint32, uint32, uint32*, int, int))
FUNCTION_LOADER(TIFFSetErrorHandler, TIFFErrorHandler (*)(TIFFErrorHandler))
May 22, 2013
May 22, 2013
65
66
67
68
}
++lib.loaded;
return 0;
May 12, 2006
May 12, 2006
69
}
Sep 26, 2009
Sep 26, 2009
70
void IMG_QuitTIF()
May 12, 2006
May 12, 2006
71
{
May 22, 2013
May 22, 2013
72
73
74
75
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
Oct 22, 2017
Oct 22, 2017
76
#ifdef LOAD_TIF_DYNAMIC
May 22, 2013
May 22, 2013
77
SDL_UnloadObject(lib.handle);
Oct 22, 2017
Oct 22, 2017
78
#endif
May 22, 2013
May 22, 2013
79
80
}
--lib.loaded;
May 12, 2006
May 12, 2006
81
82
}
Aug 10, 2000
Aug 10, 2000
83
84
85
86
87
88
89
/*
* 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)
{
Oct 22, 2017
Oct 22, 2017
90
return (tsize_t)SDL_RWread((SDL_RWops*)fd, buf, 1, size);
Aug 10, 2000
Aug 10, 2000
91
92
93
94
}
static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
{
May 22, 2013
May 22, 2013
95
return SDL_RWseek((SDL_RWops*)fd, offset, origin);
Aug 10, 2000
Aug 10, 2000
96
97
98
99
}
static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
{
Oct 22, 2017
Oct 22, 2017
100
return (tsize_t)SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
Aug 10, 2000
Aug 10, 2000
101
102
103
104
}
static int tiff_close(thandle_t fd)
{
May 22, 2013
May 22, 2013
105
106
/*
* We don't want libtiff closing our SDL_RWops*, but if it's not given
Aug 10, 2000
Aug 10, 2000
107
* a routine to try, and if the image isn't a TIFF, it'll segfault.
May 22, 2013
May 22, 2013
108
109
*/
return 0;
Aug 10, 2000
Aug 10, 2000
110
111
}
Jul 18, 2007
Jul 18, 2007
112
113
static int tiff_map(thandle_t fd, tdata_t* pbase, toff_t* psize)
{
May 22, 2013
May 22, 2013
114
return (0);
Jul 18, 2007
Jul 18, 2007
115
116
117
118
}
static void tiff_unmap(thandle_t fd, tdata_t base, toff_t size)
{
May 22, 2013
May 22, 2013
119
return;
Jul 18, 2007
Jul 18, 2007
120
121
}
Aug 10, 2000
Aug 10, 2000
122
123
static toff_t tiff_size(thandle_t fd)
{
May 22, 2013
May 22, 2013
124
125
Sint64 save_pos;
toff_t size;
Aug 10, 2000
Aug 10, 2000
126
May 22, 2013
May 22, 2013
127
128
save_pos = SDL_RWtell((SDL_RWops*)fd);
SDL_RWseek((SDL_RWops*)fd, 0, RW_SEEK_END);
Feb 3, 2013
Feb 3, 2013
129
size = SDL_RWtell((SDL_RWops*)fd);
May 22, 2013
May 22, 2013
130
131
SDL_RWseek((SDL_RWops*)fd, save_pos, RW_SEEK_SET);
return size;
Aug 10, 2000
Aug 10, 2000
132
133
134
135
}
int IMG_isTIF(SDL_RWops* src)
{
May 22, 2013
May 22, 2013
136
137
138
139
140
141
142
143
144
145
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' &&
Jul 18, 2007
Jul 18, 2007
146
magic[1] == 'I' &&
May 22, 2013
May 22, 2013
147
magic[2] == 0x2a &&
Jul 18, 2007
Jul 18, 2007
148
magic[3] == 0x00) ||
May 22, 2013
May 22, 2013
149
(magic[0] == 'M' &&
Jul 18, 2007
Jul 18, 2007
150
magic[1] == 'M' &&
May 22, 2013
May 22, 2013
151
magic[2] == 0x00 &&
Jul 18, 2007
Jul 18, 2007
152
magic[3] == 0x2a) ) {
May 22, 2013
May 22, 2013
153
154
155
156
157
is_TIF = 1;
}
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_TIF);
Aug 10, 2000
Aug 10, 2000
158
159
160
161
}
SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
{
May 22, 2013
May 22, 2013
162
Sint64 start;
Nov 1, 2016
Nov 1, 2016
163
TIFF* tiff = NULL;
May 22, 2013
May 22, 2013
164
165
166
167
168
169
170
171
172
173
SDL_Surface* surface = NULL;
Uint32 img_width, img_height;
Uint32 Rmask, Gmask, Bmask, Amask;
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);
Feb 15, 2016
Feb 15, 2016
174
if ( (IMG_Init(IMG_INIT_TIF) & IMG_INIT_TIF) == 0 ) {
May 22, 2013
May 22, 2013
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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;
Nov 1, 2016
Nov 1, 2016
197
if(!lib.TIFFReadRGBAImageOriented(tiff, img_width, img_height, (uint32 *)surface->pixels, ORIENTATION_TOPLEFT, 0))
May 22, 2013
May 22, 2013
198
199
200
201
202
goto error;
lib.TIFFClose(tiff);
return surface;
Feb 4, 2006
Feb 4, 2006
203
204
error:
May 22, 2013
May 22, 2013
205
SDL_RWseek(src, start, RW_SEEK_SET);
Nov 1, 2016
Nov 1, 2016
206
if (surface) {
May 22, 2013
May 22, 2013
207
208
SDL_FreeSurface(surface);
}
Nov 1, 2016
Nov 1, 2016
209
210
211
if (tiff) {
lib.TIFFClose(tiff);
}
May 22, 2013
May 22, 2013
212
return NULL;
Aug 10, 2000
Aug 10, 2000
213
214
215
216
}
#else
Sep 26, 2009
Sep 26, 2009
217
218
int IMG_InitTIF()
{
May 22, 2013
May 22, 2013
219
220
IMG_SetError("TIFF images are not supported");
return(-1);
Sep 26, 2009
Sep 26, 2009
221
222
223
224
225
226
}
void IMG_QuitTIF()
{
}
Aug 10, 2000
Aug 10, 2000
227
228
229
/* See if an image is contained in a data source */
int IMG_isTIF(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
230
return(0);
Aug 10, 2000
Aug 10, 2000
231
232
233
234
235
}
/* Load a TIFF type image from an SDL datasource */
SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
236
return(NULL);
Aug 10, 2000
Aug 10, 2000
237
238
239
}
#endif /* LOAD_TIF */
Nov 10, 2009
Nov 10, 2009
240
241
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */