PageRenderTime 53ms CodeModel.GetById 12ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/project/jni/sdl_image/IMG_tif.c

https://github.com/aichunyu/FFPlayer
C | 298 lines | 225 code | 38 blank | 35 comment | 45 complexity | 9b4a831c9dd17e586f890d10181e634b MD5 | raw file
  1/*
  2  SDL_image:  An example image loading library for use with SDL
  3  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
  4
  5  This software is provided 'as-is', without any express or implied
  6  warranty.  In no event will the authors be held liable for any damages
  7  arising from the use of this software.
  8
  9  Permission is granted to anyone to use this software for any purpose,
 10  including commercial applications, and to alter it and redistribute it
 11  freely, subject to the following restrictions:
 12
 13  1. The origin of this software must not be misrepresented; you must not
 14     claim that you wrote the original software. If you use this software
 15     in a product, an acknowledgment in the product documentation would be
 16     appreciated but is not required.
 17  2. Altered source versions must be plainly marked as such, and must not be
 18     misrepresented as being the original software.
 19  3. This notice may not be removed or altered from any source distribution.
 20*/
 21
 22#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
 23
 24/* This is a TIFF image file loading framework */
 25
 26#include <stdio.h>
 27
 28#include "SDL_image.h"
 29
 30#ifdef LOAD_TIF
 31
 32#include <tiffio.h>
 33
 34static struct {
 35	int loaded;
 36	void *handle;
 37	TIFF* (*TIFFClientOpen)(const char*, const char*, thandle_t, TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc);
 38	void (*TIFFClose)(TIFF*);
 39	int (*TIFFGetField)(TIFF*, ttag_t, ...);
 40	int (*TIFFReadRGBAImage)(TIFF*, uint32, uint32, uint32*, int);
 41	TIFFErrorHandler (*TIFFSetErrorHandler)(TIFFErrorHandler);
 42} lib;
 43
 44#ifdef LOAD_TIF_DYNAMIC
 45int IMG_InitTIF()
 46{
 47	if ( lib.loaded == 0 ) {
 48		lib.handle = SDL_LoadObject(LOAD_TIF_DYNAMIC);
 49		if ( lib.handle == NULL ) {
 50			return -1;
 51		}
 52		lib.TIFFClientOpen =
 53			(TIFF* (*)(const char*, const char*, thandle_t, TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc, TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc))
 54			SDL_LoadFunction(lib.handle, "TIFFClientOpen");
 55		if ( lib.TIFFClientOpen == NULL ) {
 56			SDL_UnloadObject(lib.handle);
 57			return -1;
 58		}
 59		lib.TIFFClose =
 60			(void (*)(TIFF*))
 61			SDL_LoadFunction(lib.handle, "TIFFClose");
 62		if ( lib.TIFFClose == NULL ) {
 63			SDL_UnloadObject(lib.handle);
 64			return -1;
 65		}
 66		lib.TIFFGetField =
 67			(int (*)(TIFF*, ttag_t, ...))
 68			SDL_LoadFunction(lib.handle, "TIFFGetField");
 69		if ( lib.TIFFGetField == NULL ) {
 70			SDL_UnloadObject(lib.handle);
 71			return -1;
 72		}
 73		lib.TIFFReadRGBAImage =
 74			(int (*)(TIFF*, uint32, uint32, uint32*, int))
 75			SDL_LoadFunction(lib.handle, "TIFFReadRGBAImage");
 76		if ( lib.TIFFReadRGBAImage == NULL ) {
 77			SDL_UnloadObject(lib.handle);
 78			return -1;
 79		}
 80		lib.TIFFSetErrorHandler =
 81			(TIFFErrorHandler (*)(TIFFErrorHandler))
 82			SDL_LoadFunction(lib.handle, "TIFFSetErrorHandler");
 83		if ( lib.TIFFSetErrorHandler == NULL ) {
 84			SDL_UnloadObject(lib.handle);
 85			return -1;
 86		}
 87	}
 88	++lib.loaded;
 89
 90	return 0;
 91}
 92void IMG_QuitTIF()
 93{
 94	if ( lib.loaded == 0 ) {
 95		return;
 96	}
 97	if ( lib.loaded == 1 ) {
 98		SDL_UnloadObject(lib.handle);
 99	}
100	--lib.loaded;
101}
102#else
103int IMG_InitTIF()
104{
105	if ( lib.loaded == 0 ) {
106		lib.TIFFClientOpen = TIFFClientOpen;
107		lib.TIFFClose = TIFFClose;
108		lib.TIFFGetField = TIFFGetField;
109		lib.TIFFReadRGBAImage = TIFFReadRGBAImage;
110		lib.TIFFSetErrorHandler = TIFFSetErrorHandler;
111	}
112	++lib.loaded;
113
114	return 0;
115}
116void IMG_QuitTIF()
117{
118	if ( lib.loaded == 0 ) {
119		return;
120	}
121	if ( lib.loaded == 1 ) {
122	}
123	--lib.loaded;
124}
125#endif /* LOAD_TIF_DYNAMIC */
126
127/*
128 * These are the thunking routine to use the SDL_RWops* routines from
129 * libtiff's internals.
130*/
131
132static tsize_t tiff_read(thandle_t fd, tdata_t buf, tsize_t size)
133{
134	return SDL_RWread((SDL_RWops*)fd, buf, 1, size);
135}
136
137static toff_t tiff_seek(thandle_t fd, toff_t offset, int origin)
138{
139	return SDL_RWseek((SDL_RWops*)fd, offset, origin);
140}
141
142static tsize_t tiff_write(thandle_t fd, tdata_t buf, tsize_t size)
143{
144	return SDL_RWwrite((SDL_RWops*)fd, buf, 1, size);
145}
146
147static int tiff_close(thandle_t fd)
148{
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
156static int tiff_map(thandle_t fd, tdata_t* pbase, toff_t* psize)
157{
158	return (0);
159}
160
161static void tiff_unmap(thandle_t fd, tdata_t base, toff_t size)
162{
163	return;
164}
165
166static toff_t tiff_size(thandle_t fd)
167{
168	Uint32 save_pos;
169	toff_t size;
170
171	save_pos = SDL_RWtell((SDL_RWops*)fd);
172	SDL_RWseek((SDL_RWops*)fd, 0, RW_SEEK_END);
173        size = SDL_RWtell((SDL_RWops*)fd);
174	SDL_RWseek((SDL_RWops*)fd, save_pos, RW_SEEK_SET);
175	return size;
176}
177
178int IMG_isTIF(SDL_RWops* src)
179{
180	int start;
181	int is_TIF;
182	Uint8 magic[4];
183
184	if ( !src )
185		return 0;
186	start = SDL_RWtell(src);
187	is_TIF = 0;
188	if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
189		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			is_TIF = 1;
198		}
199	}
200	SDL_RWseek(src, start, RW_SEEK_SET);
201	return(is_TIF);
202}
203
204SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src)
205{
206	int start;
207	TIFF* tiff;
208	SDL_Surface* surface = NULL;
209	Uint32 img_width, img_height;
210	Uint32 Rmask, Gmask, Bmask, Amask;
211	Uint32 x, y;
212	Uint32 half;
213
214	if ( !src ) {
215		/* The error message has been set in SDL_RWFromFile */
216		return NULL;
217	}
218	start = SDL_RWtell(src);
219
220	if ( !IMG_Init(IMG_INIT_TIF) ) {
221		return NULL;
222	}
223
224	/* turn off memory mapped access with the m flag */
225	tiff = lib.TIFFClientOpen("SDL_image", "rm", (thandle_t)src, 
226		tiff_read, tiff_write, tiff_seek, tiff_close, tiff_size, tiff_map, tiff_unmap);
227	if(!tiff)
228		goto error;
229
230	/* Retrieve the dimensions of the image from the TIFF tags */
231	lib.TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &img_width);
232	lib.TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &img_height);
233
234	Rmask = 0x000000FF;
235	Gmask = 0x0000FF00;
236	Bmask = 0x00FF0000;
237	Amask = 0xFF000000;
238	surface = SDL_CreateRGBSurface(SDL_SWSURFACE, img_width, img_height, 32,
239		Rmask, Gmask, Bmask, Amask);
240	if(!surface)
241		goto error;
242	
243	if(!lib.TIFFReadRGBAImage(tiff, img_width, img_height, surface->pixels, 0))
244		goto error;
245
246	/* libtiff loads the image upside-down, flip it back */
247	half = img_height / 2;
248	for(y = 0; y < half; y++)
249	{
250	        Uint32 *top = (Uint32 *)surface->pixels + y * surface->pitch/4;
251	        Uint32 *bot = (Uint32 *)surface->pixels
252		              + (img_height - y - 1) * surface->pitch/4;
253		for(x = 0; x < img_width; x++)
254		{
255		        Uint32 tmp = top[x];
256			top[x] = bot[x];
257			bot[x] = tmp;
258		}
259	}
260	lib.TIFFClose(tiff);
261	
262	return surface;
263
264error:
265	SDL_RWseek(src, start, RW_SEEK_SET);
266	if ( surface ) {
267		SDL_FreeSurface(surface);
268	}
269	return NULL;
270}
271
272#else
273
274int IMG_InitTIF()
275{
276	IMG_SetError("TIFF images are not supported");
277	return(-1);
278}
279
280void IMG_QuitTIF()
281{
282}
283
284/* See if an image is contained in a data source */
285int IMG_isTIF(SDL_RWops *src)
286{
287	return(0);
288}
289
290/* Load a TIFF type image from an SDL datasource */
291SDL_Surface *IMG_LoadTIF_RW(SDL_RWops *src)
292{
293	return(NULL);
294}
295
296#endif /* LOAD_TIF */
297
298#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */