/project/jni/sdl_image/IMG_webp.c

https://github.com/aichunyu/FFPlayer · C · 296 lines · 215 code · 45 blank · 36 comment · 55 complexity · f51d8bb628ef27458181e20b18ce54b0 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. /* This is a WEBP image file loading framework */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include "SDL_image.h"
  22. #ifdef LOAD_WEBP
  23. /*=============================================================================
  24. File: SDL_webp.c
  25. Purpose: A WEBP loader for the SDL library
  26. Revision:
  27. Created by: Michael Bonfils (Murlock) (26 November 2011)
  28. murlock42@gmail.com
  29. =============================================================================*/
  30. #include "SDL_endian.h"
  31. #ifdef macintosh
  32. #define MACOS
  33. #endif
  34. #include <webp/decode.h>
  35. static struct {
  36. int loaded;
  37. void *handle;
  38. int/*VP8StatuCode*/ (*webp_get_features_internal) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version);
  39. uint8_t* (*webp_decode_rgb_into) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
  40. uint8_t* (*webp_decode_rgba_into) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
  41. } lib;
  42. #ifdef LOAD_WEBP_DYNAMIC
  43. int IMG_InitWEBP()
  44. {
  45. if ( lib.loaded == 0 ) {
  46. lib.handle = SDL_LoadObject(LOAD_WEBP_DYNAMIC);
  47. if ( lib.handle == NULL ) {
  48. return -1;
  49. }
  50. lib.webp_get_features_internal =
  51. ( int (*) (const uint8_t *, uint32_t, WebPBitstreamFeatures* const, int) )
  52. SDL_LoadFunction(lib.handle, "WebPGetFeaturesInternal" );
  53. if ( lib.webp_get_features_internal == NULL ) {
  54. SDL_UnloadObject(lib.handle);
  55. return -1;
  56. }
  57. lib.webp_decode_rgb_into =
  58. ( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
  59. SDL_LoadFunction(lib.handle, "WebPDecodeRGBInto" );
  60. if ( lib.webp_decode_rgb_into == NULL ) {
  61. SDL_UnloadObject(lib.handle);
  62. return -1;
  63. }
  64. lib.webp_decode_rgba_into =
  65. ( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
  66. SDL_LoadFunction(lib.handle, "WebPDecodeRGBInto" );
  67. if ( lib.webp_decode_rgba_into == NULL ) {
  68. SDL_UnloadObject(lib.handle);
  69. return -1;
  70. }
  71. }
  72. ++lib.loaded;
  73. return 0;
  74. }
  75. void IMG_QuitWEBP()
  76. {
  77. if ( lib.loaded == 0 ) {
  78. return;
  79. }
  80. if ( lib.loaded == 1 ) {
  81. SDL_UnloadObject(lib.handle);
  82. }
  83. --lib.loaded;
  84. }
  85. #else
  86. int IMG_InitWEBP()
  87. {
  88. if ( lib.loaded == 0 ) {
  89. lib.webp_get_features_internal = WebPGetFeaturesInternal;
  90. lib.webp_decode_rgb_into = WebPDecodeRGBInto;
  91. lib.webp_decode_rgba_into = WebPDecodeRGBAInto;
  92. }
  93. ++lib.loaded;
  94. return 0;
  95. }
  96. void IMG_QuitWEBP()
  97. {
  98. if ( lib.loaded == 0 ) {
  99. return;
  100. }
  101. if ( lib.loaded == 1 ) {
  102. }
  103. --lib.loaded;
  104. }
  105. #endif /* LOAD_WEBP_DYNAMIC */
  106. static int webp_getinfo( SDL_RWops *src, int *datasize ) {
  107. int start;
  108. int is_WEBP;
  109. int data;
  110. Uint8 magic[20];
  111. if ( !src )
  112. return 0;
  113. start = SDL_RWtell(src);
  114. is_WEBP = 0;
  115. if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
  116. if ( magic[ 0] == 'R' &&
  117. magic[ 1] == 'I' &&
  118. magic[ 2] == 'F' &&
  119. magic[ 3] == 'F' &&
  120. magic[ 8] == 'W' &&
  121. magic[ 9] == 'E' &&
  122. magic[10] == 'B' &&
  123. magic[11] == 'P' &&
  124. magic[12] == 'V' &&
  125. magic[13] == 'P' &&
  126. magic[14] == '8' &&
  127. magic[15] == ' ' ) {
  128. is_WEBP = 1;
  129. data = magic[16] | magic[17]<<8 | magic[18]<<16 | magic[19]<<24;
  130. if ( datasize )
  131. *datasize = data;
  132. }
  133. }
  134. SDL_RWseek(src, start, RW_SEEK_SET);
  135. return(is_WEBP);
  136. }
  137. /* See if an image is contained in a data source */
  138. int IMG_isWEBP(SDL_RWops *src)
  139. {
  140. return webp_getinfo( src, NULL );
  141. }
  142. SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
  143. {
  144. int start;
  145. const char *error = NULL;
  146. SDL_Surface *volatile surface = NULL;
  147. Uint32 Rmask;
  148. Uint32 Gmask;
  149. Uint32 Bmask;
  150. Uint32 Amask;
  151. WebPBitstreamFeatures features;
  152. int raw_data_size;
  153. uint8_t *raw_data;
  154. int r;
  155. uint8_t *ret;
  156. if ( !src ) {
  157. /* The error message has been set in SDL_RWFromFile */
  158. return NULL;
  159. }
  160. start = SDL_RWtell(src);
  161. if ( !IMG_Init(IMG_INIT_WEBP) ) {
  162. goto error;
  163. }
  164. raw_data_size = -1;
  165. if ( !webp_getinfo( src, &raw_data_size ) ) {
  166. error = "Invalid WEBP";
  167. goto error;
  168. }
  169. // skip header
  170. SDL_RWseek(src, start+20, RW_SEEK_SET );
  171. raw_data = (uint8_t*) malloc( raw_data_size );
  172. if ( raw_data == NULL ) {
  173. error = "Failed to allocate enought buffer for WEBP";
  174. goto error;
  175. }
  176. r = SDL_RWread(src, raw_data, 1, raw_data_size );
  177. if ( r != raw_data_size ) {
  178. error = "Failed to read WEBP";
  179. goto error;
  180. }
  181. #if 0
  182. // extract size of picture, not interesting since we don't know about alpha channel
  183. int width = -1, height = -1;
  184. if ( !WebPGetInfo( raw_data, raw_data_size, &width, &height ) ) {
  185. printf("WebPGetInfo has failed\n" );
  186. return NULL;
  187. }
  188. #endif
  189. if ( lib.webp_get_features_internal( raw_data, raw_data_size, &features, WEBP_DECODER_ABI_VERSION ) != VP8_STATUS_OK ) {
  190. error = "WebPGetFeatures has failed";
  191. return NULL;
  192. }
  193. /* Check if it's ok !*/
  194. Rmask = 0x000000FF;
  195. Gmask = 0x0000FF00;
  196. Bmask = 0x00FF0000;
  197. Amask = features.has_alpha?0xFF000001:0;
  198. surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
  199. features.width, features.height,
  200. features.has_alpha?32:24, Rmask,Gmask,Bmask,Amask);
  201. if ( surface == NULL ) {
  202. error = "Failed to allocate SDL_Surface";
  203. goto error;
  204. }
  205. if ( features.has_alpha ) {
  206. ret = lib.webp_decode_rgba_into( raw_data, raw_data_size, surface->pixels, surface->pitch * surface->h, surface->pitch );
  207. } else {
  208. ret = lib.webp_decode_rgb_into( raw_data, raw_data_size, surface->pixels, surface->pitch * surface->h, surface->pitch );
  209. }
  210. if ( !ret ) {
  211. error = "Failed to decode WEBP";
  212. goto error;
  213. }
  214. return surface;
  215. error:
  216. if ( surface ) {
  217. SDL_FreeSurface( surface );
  218. }
  219. if ( raw_data ) {
  220. free( raw_data );
  221. }
  222. if ( error ) {
  223. IMG_SetError( error );
  224. }
  225. SDL_RWseek(src, start, RW_SEEK_SET);
  226. return(NULL);
  227. }
  228. #else
  229. int IMG_InitWEBP()
  230. {
  231. IMG_SetError("WEBP images are not supported");
  232. return(-1);
  233. }
  234. void IMG_QuitWEBP()
  235. {
  236. }
  237. /* See if an image is contained in a data source */
  238. int IMG_isWEBP(SDL_RWops *src)
  239. {
  240. return(0);
  241. }
  242. /* Load a WEBP type image from an SDL datasource */
  243. SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
  244. {
  245. return(NULL);
  246. }
  247. #endif /* LOAD_WEBP */