Skip to content

Latest commit

 

History

History
179 lines (141 loc) · 4.32 KB

IMG_tif.c

File metadata and controls

179 lines (141 loc) · 4.32 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
Jan 4, 2004
Jan 4, 2004
3
Copyright (C) 1999-2004 Sam Lantinga
Aug 10, 2000
Aug 10, 2000
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Aug 10, 2000
Aug 10, 2000
21
22
*/
Dec 14, 2001
Dec 14, 2001
23
24
/* $Id$ */
Aug 10, 2000
Aug 10, 2000
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
/* This is a TIFF image file loading framework */
#include <stdio.h>
#include "SDL_image.h"
#ifdef LOAD_TIF
#include <tiffio.h>
/*
* 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
80
81
int start;
int is_TIF;
Aug 10, 2000
Aug 10, 2000
82
83
84
TIFF* tiff;
TIFFErrorHandler prev_handler;
Feb 4, 2006
Feb 4, 2006
85
86
87
start = SDL_RWtell(src);
is_TIF = 0;
Aug 10, 2000
Aug 10, 2000
88
89
90
91
92
93
94
95
96
97
98
99
/* Suppress output from libtiff */
prev_handler = TIFFSetErrorHandler(NULL);
/* Attempt to process the given file data */
/* turn off memory mapped access with the m flag */
tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src,
tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
/* Reset the default error handler, since it can be useful for info */
TIFFSetErrorHandler(prev_handler);
/* If it's not a TIFF, then tiff will be NULL. */
Feb 4, 2006
Feb 4, 2006
100
101
if ( tiff ) {
is_TIF = 1;
Aug 10, 2000
Aug 10, 2000
102
Feb 4, 2006
Feb 4, 2006
103
104
105
106
107
/* Free up any dynamically allocated memory libtiff uses */
TIFFClose(tiff);
}
SDL_RWseek(src, start, SEEK_SET);
return(is_TIF);
Aug 10, 2000
Aug 10, 2000
108
109
110
111
112
113
114
}
SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
{
TIFF* tiff;
SDL_Surface* surface = NULL;
Uint32 img_width, img_height;
Sep 1, 2000
Sep 1, 2000
115
Uint32 Rmask, Gmask, Bmask, Amask, mask;
Aug 10, 2000
Aug 10, 2000
116
117
118
Uint32 x, y;
Uint32 half;
Jan 4, 2004
Jan 4, 2004
119
120
121
122
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Aug 10, 2000
Aug 10, 2000
123
124
125
126
127
128
129
130
131
132
133
/* turn off memory mapped access with the m flag */
tiff = TIFFClientOpen("SDL_image", "rm", (thandle_t)src,
tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, NULL, NULL);
if(!tiff)
return NULL;
/* Retrieve the dimensions of the image from the TIFF tags */
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
Sep 1, 2000
Sep 1, 2000
134
135
136
137
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Amask = 0xFF000000;
Aug 10, 2000
Aug 10, 2000
138
139
140
141
142
143
144
145
surface = SDL_AllocSurface(SDL_SWSURFACE, img_width, img_height, 32,
Rmask, Gmask, Bmask, Amask);
if(!surface)
return NULL;
if(!TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
return NULL;
Sep 1, 2000
Sep 1, 2000
146
/* libtiff loads the image upside-down, flip it back */
Aug 10, 2000
Aug 10, 2000
147
half = img_height / 2;
Sep 1, 2000
Sep 1, 2000
148
for(y = 0; y < half; y++)
Aug 10, 2000
Aug 10, 2000
149
{
Sep 1, 2000
Sep 1, 2000
150
151
152
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
153
154
for(x = 0; x < img_width; x++)
{
Sep 1, 2000
Sep 1, 2000
155
156
157
Uint32 tmp = top[x];
top[x] = bot[x];
bot[x] = tmp;
Aug 10, 2000
Aug 10, 2000
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
}
}
TIFFClose(tiff);
return surface;
}
#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 */