/project/jni/sdl_image/IMG.c

https://github.com/aichunyu/FFPlayer · C · 247 lines · 187 code · 25 blank · 35 comment · 42 complexity · e20e8bf7720a41390810fe9b96863368 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. /* A simple library to load images of various formats as SDL surfaces */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include "SDL_image.h"
  23. #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
  24. /* Table of image detection and loading functions */
  25. static struct {
  26. char *type;
  27. int (SDLCALL *is)(SDL_RWops *src);
  28. SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
  29. } supported[] = {
  30. /* keep magicless formats first */
  31. { "TGA", NULL, IMG_LoadTGA_RW },
  32. { "CUR", IMG_isCUR, IMG_LoadCUR_RW },
  33. { "ICO", IMG_isICO, IMG_LoadICO_RW },
  34. { "BMP", IMG_isBMP, IMG_LoadBMP_RW },
  35. { "GIF", IMG_isGIF, IMG_LoadGIF_RW },
  36. { "JPG", IMG_isJPG, IMG_LoadJPG_RW },
  37. { "LBM", IMG_isLBM, IMG_LoadLBM_RW },
  38. { "PCX", IMG_isPCX, IMG_LoadPCX_RW },
  39. { "PNG", IMG_isPNG, IMG_LoadPNG_RW },
  40. { "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
  41. { "TIF", IMG_isTIF, IMG_LoadTIF_RW },
  42. { "XCF", IMG_isXCF, IMG_LoadXCF_RW },
  43. { "XPM", IMG_isXPM, IMG_LoadXPM_RW },
  44. { "XV", IMG_isXV, IMG_LoadXV_RW },
  45. { "WEBP", IMG_isWEBP, IMG_LoadWEBP_RW },
  46. };
  47. const SDL_version *IMG_Linked_Version(void)
  48. {
  49. static SDL_version linked_version;
  50. SDL_IMAGE_VERSION(&linked_version);
  51. return(&linked_version);
  52. }
  53. extern int IMG_InitJPG();
  54. extern void IMG_QuitJPG();
  55. extern int IMG_InitPNG();
  56. extern void IMG_QuitPNG();
  57. extern int IMG_InitTIF();
  58. extern void IMG_QuitTIF();
  59. extern int IMG_InitWEBP();
  60. extern void IMG_QuitWEBP();
  61. static int initialized = 0;
  62. int IMG_Init(int flags)
  63. {
  64. int result = 0;
  65. if (flags & IMG_INIT_JPG) {
  66. if ((initialized & IMG_INIT_JPG) || IMG_InitJPG() == 0) {
  67. result |= IMG_INIT_JPG;
  68. }
  69. }
  70. if (flags & IMG_INIT_PNG) {
  71. if ((initialized & IMG_INIT_PNG) || IMG_InitPNG() == 0) {
  72. result |= IMG_INIT_PNG;
  73. }
  74. }
  75. if (flags & IMG_INIT_TIF) {
  76. if ((initialized & IMG_INIT_TIF) || IMG_InitTIF() == 0) {
  77. result |= IMG_INIT_TIF;
  78. }
  79. }
  80. if (flags & IMG_INIT_WEBP) {
  81. if ((initialized & IMG_INIT_WEBP) || IMG_InitWEBP() == 0) {
  82. result |= IMG_INIT_WEBP;
  83. }
  84. }
  85. initialized |= result;
  86. return (initialized);
  87. }
  88. void IMG_Quit()
  89. {
  90. if (initialized & IMG_INIT_JPG) {
  91. IMG_QuitJPG();
  92. }
  93. if (initialized & IMG_INIT_PNG) {
  94. IMG_QuitPNG();
  95. }
  96. if (initialized & IMG_INIT_TIF) {
  97. IMG_QuitTIF();
  98. }
  99. if (initialized & IMG_INIT_WEBP) {
  100. IMG_QuitWEBP();
  101. }
  102. initialized = 0;
  103. }
  104. #if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
  105. /* Load an image from a file */
  106. SDL_Surface *IMG_Load(const char *file)
  107. {
  108. SDL_RWops *src = SDL_RWFromFile(file, "rb");
  109. char *ext = strrchr(file, '.');
  110. if(ext) {
  111. ext++;
  112. }
  113. if(!src) {
  114. /* The error message has been set in SDL_RWFromFile */
  115. return NULL;
  116. }
  117. return IMG_LoadTyped_RW(src, 1, ext);
  118. }
  119. #endif
  120. /* Load an image from an SDL datasource (for compatibility) */
  121. SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
  122. {
  123. return IMG_LoadTyped_RW(src, freesrc, NULL);
  124. }
  125. /* Portable case-insensitive string compare function */
  126. static int IMG_string_equals(const char *str1, const char *str2)
  127. {
  128. while ( *str1 && *str2 ) {
  129. if ( toupper((unsigned char)*str1) !=
  130. toupper((unsigned char)*str2) )
  131. break;
  132. ++str1;
  133. ++str2;
  134. }
  135. return (!*str1 && !*str2);
  136. }
  137. /* Load an image from an SDL datasource, optionally specifying the type */
  138. SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, const char *type)
  139. {
  140. int i;
  141. SDL_Surface *image;
  142. /* Make sure there is something to do.. */
  143. if ( src == NULL ) {
  144. IMG_SetError("Passed a NULL data source");
  145. return(NULL);
  146. }
  147. /* See whether or not this data source can handle seeking */
  148. if ( SDL_RWseek(src, 0, RW_SEEK_CUR) < 0 ) {
  149. IMG_SetError("Can't seek in this data source");
  150. if(freesrc)
  151. SDL_RWclose(src);
  152. return(NULL);
  153. }
  154. /* Detect the type of image being loaded */
  155. image = NULL;
  156. for ( i=0; i < ARRAYSIZE(supported); ++i ) {
  157. if(supported[i].is) {
  158. if(!supported[i].is(src))
  159. continue;
  160. } else {
  161. /* magicless format */
  162. if(!type
  163. || !IMG_string_equals(type, supported[i].type))
  164. continue;
  165. }
  166. #ifdef DEBUG_IMGLIB
  167. fprintf(stderr, "IMGLIB: Loading image as %s\n",
  168. supported[i].type);
  169. #endif
  170. image = supported[i].load(src);
  171. if(freesrc)
  172. SDL_RWclose(src);
  173. return image;
  174. }
  175. if ( freesrc ) {
  176. SDL_RWclose(src);
  177. }
  178. IMG_SetError("Unsupported image format");
  179. return NULL;
  180. }
  181. #if (SDL_VERSION_ATLEAST(1,3,0))
  182. SDL_Texture *IMG_LoadTexture(SDL_Renderer *renderer, const char *file)
  183. {
  184. SDL_Texture *texture = NULL;
  185. SDL_Surface *surface = IMG_Load(file);
  186. if (surface) {
  187. texture = SDL_CreateTextureFromSurface(renderer, surface);
  188. SDL_FreeSurface(surface);
  189. }
  190. return texture;
  191. }
  192. SDL_Texture *IMG_LoadTexture_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc)
  193. {
  194. SDL_Texture *texture = NULL;
  195. SDL_Surface *surface = IMG_Load_RW(src, freesrc);
  196. if (surface) {
  197. texture = SDL_CreateTextureFromSurface(renderer, surface);
  198. SDL_FreeSurface(surface);
  199. }
  200. return texture;
  201. }
  202. SDL_Texture *IMG_LoadTextureTyped_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc, const char *type)
  203. {
  204. SDL_Texture *texture = NULL;
  205. SDL_Surface *surface = IMG_LoadTyped_RW(src, freesrc, type);
  206. if (surface) {
  207. texture = SDL_CreateTextureFromSurface(renderer, surface);
  208. SDL_FreeSurface(surface);
  209. }
  210. return texture;
  211. }
  212. #endif
  213. /* Invert the alpha of a surface for use with OpenGL
  214. This function is a no-op and only kept for backwards compatibility.
  215. */
  216. int IMG_InvertAlpha(int on)
  217. {
  218. return 1;
  219. }