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