IMG_jpg.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. SDL_image: An example image loading library for use with SDL
  3. Copyright (C) 1999, 2000, 2001 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. */
  18. /* $Id: IMG_jpg.c,v 1.1 2004/07/21 16:24:11 paigoddess Exp $ */
  19. /* This is a JPEG image file loading framework */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <setjmp.h>
  23. #include "SDL_image.h"
  24. #include <jpeglib.h>
  25. /* Define this for fast loading and not as good image quality */
  26. /*#define FAST_JPEG*/
  27. /* See if an image is contained in a data source */
  28. int IMG_isJPG(SDL_RWops *src)
  29. {
  30. int is_JPG;
  31. Uint8 magic[4];
  32. is_JPG = 0;
  33. if ( SDL_RWread(src, magic, 2, 1) ) {
  34. if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
  35. SDL_RWread(src, magic, 4, 1);
  36. SDL_RWread(src, magic, 4, 1);
  37. if ( memcmp((char *)magic, "JFIF", 4) == 0 ||
  38. memcmp((char *)magic, "Exif", 4) == 0 ) {
  39. is_JPG = 1;
  40. }
  41. }
  42. }
  43. return(is_JPG);
  44. }
  45. #define INPUT_BUFFER_SIZE 4096
  46. typedef struct {
  47. struct jpeg_source_mgr pub;
  48. SDL_RWops *ctx;
  49. Uint8 buffer[INPUT_BUFFER_SIZE];
  50. } my_source_mgr;
  51. /*
  52. * Initialize source --- called by jpeg_read_header
  53. * before any data is actually read.
  54. */
  55. static void init_source (j_decompress_ptr cinfo)
  56. {
  57. /* We don't actually need to do anything */
  58. return;
  59. }
  60. /*
  61. * Fill the input buffer --- called whenever buffer is emptied.
  62. */
  63. static int fill_input_buffer (j_decompress_ptr cinfo)
  64. {
  65. my_source_mgr * src = (my_source_mgr *) cinfo->src;
  66. int nbytes;
  67. nbytes = SDL_RWread(src->ctx, src->buffer, 1, INPUT_BUFFER_SIZE);
  68. if (nbytes <= 0) {
  69. /* Insert a fake EOI marker */
  70. src->buffer[0] = (Uint8) 0xFF;
  71. src->buffer[1] = (Uint8) JPEG_EOI;
  72. nbytes = 2;
  73. }
  74. src->pub.next_input_byte = src->buffer;
  75. src->pub.bytes_in_buffer = nbytes;
  76. return TRUE;
  77. }
  78. /*
  79. * Skip data --- used to skip over a potentially large amount of
  80. * uninteresting data (such as an APPn marker).
  81. *
  82. * Writers of suspendable-input applications must note that skip_input_data
  83. * is not granted the right to give a suspension return. If the skip extends
  84. * beyond the data currently in the buffer, the buffer can be marked empty so
  85. * that the next read will cause a fill_input_buffer call that can suspend.
  86. * Arranging for additional bytes to be discarded before reloading the input
  87. * buffer is the application writer's problem.
  88. */
  89. static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
  90. {
  91. my_source_mgr * src = (my_source_mgr *) cinfo->src;
  92. /* Just a dumb implementation for now. Could use fseek() except
  93. * it doesn't work on pipes. Not clear that being smart is worth
  94. * any trouble anyway --- large skips are infrequent.
  95. */
  96. if (num_bytes > 0) {
  97. while (num_bytes > (long) src->pub.bytes_in_buffer) {
  98. num_bytes -= (long) src->pub.bytes_in_buffer;
  99. (void) src->pub.fill_input_buffer(cinfo);
  100. /* note we assume that fill_input_buffer will never
  101. * return FALSE, so suspension need not be handled.
  102. */
  103. }
  104. src->pub.next_input_byte += (size_t) num_bytes;
  105. src->pub.bytes_in_buffer -= (size_t) num_bytes;
  106. }
  107. }
  108. /*
  109. * Terminate source --- called by jpeg_finish_decompress
  110. * after all data has been read.
  111. */
  112. static void term_source (j_decompress_ptr cinfo)
  113. {
  114. /* We don't actually need to do anything */
  115. return;
  116. }
  117. /*
  118. * Prepare for input from a stdio stream.
  119. * The caller must have already opened the stream, and is responsible
  120. * for closing it after finishing decompression.
  121. */
  122. static void jpeg_SDL_RW_src (j_decompress_ptr cinfo, SDL_RWops *ctx)
  123. {
  124. my_source_mgr *src;
  125. /* The source object and input buffer are made permanent so that a series
  126. * of JPEG images can be read from the same file by calling jpeg_stdio_src
  127. * only before the first one. (If we discarded the buffer at the end of
  128. * one image, we'd likely lose the start of the next one.)
  129. * This makes it unsafe to use this manager and a different source
  130. * manager serially with the same JPEG object. Caveat programmer.
  131. */
  132. if (cinfo->src == NULL) { /* first time for this JPEG object? */
  133. cinfo->src = (struct jpeg_source_mgr *)
  134. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
  135. sizeof(my_source_mgr));
  136. src = (my_source_mgr *) cinfo->src;
  137. }
  138. src = (my_source_mgr *) cinfo->src;
  139. src->pub.init_source = init_source;
  140. src->pub.fill_input_buffer = fill_input_buffer;
  141. src->pub.skip_input_data = skip_input_data;
  142. src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  143. src->pub.term_source = term_source;
  144. src->ctx = ctx;
  145. src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  146. src->pub.next_input_byte = NULL; /* until buffer loaded */
  147. }
  148. struct my_error_mgr {
  149. struct jpeg_error_mgr errmgr;
  150. jmp_buf escape;
  151. };
  152. static void my_error_exit(j_common_ptr cinfo)
  153. {
  154. struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
  155. longjmp(err->escape, 1);
  156. }
  157. static void output_no_message(j_common_ptr cinfo)
  158. {
  159. /* do nothing */
  160. }
  161. /* Load a JPEG type image from an SDL datasource */
  162. SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
  163. {
  164. struct jpeg_decompress_struct cinfo;
  165. JSAMPROW rowptr[1];
  166. SDL_Surface *volatile surface = NULL;
  167. struct my_error_mgr jerr;
  168. /* Create a decompression structure and load the JPEG header */
  169. cinfo.err = jpeg_std_error(&jerr.errmgr);
  170. jerr.errmgr.error_exit = my_error_exit;
  171. jerr.errmgr.output_message = output_no_message;
  172. if(setjmp(jerr.escape)) {
  173. /* If we get here, libjpeg found an error */
  174. jpeg_destroy_decompress(&cinfo);
  175. IMG_SetError("JPEG loading error");
  176. SDL_FreeSurface(surface);
  177. return NULL;
  178. }
  179. jpeg_create_decompress(&cinfo);
  180. jpeg_SDL_RW_src(&cinfo, src);
  181. jpeg_read_header(&cinfo, TRUE);
  182. /* Set 24-bit RGB output */
  183. cinfo.out_color_space = JCS_RGB;
  184. cinfo.quantize_colors = FALSE;
  185. #ifdef FAST_JPEG
  186. cinfo.scale_num = 1;
  187. cinfo.scale_denom = 1;
  188. cinfo.dct_method = JDCT_FASTEST;
  189. cinfo.do_fancy_upsampling = FALSE;
  190. #endif
  191. jpeg_calc_output_dimensions(&cinfo);
  192. /* Allocate an output surface to hold the image */
  193. surface = SDL_AllocSurface(SDL_SWSURFACE,
  194. cinfo.output_width, cinfo.output_height, 24,
  195. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  196. 0x0000FF, 0x00FF00, 0xFF0000,
  197. #else
  198. 0xFF0000, 0x00FF00, 0x0000FF,
  199. #endif
  200. 0);
  201. if ( surface == NULL ) {
  202. IMG_SetError("Out of memory");
  203. goto done;
  204. }
  205. /* Decompress the image */
  206. jpeg_start_decompress(&cinfo);
  207. while ( cinfo.output_scanline < cinfo.output_height ) {
  208. rowptr[0] = (JSAMPROW)(Uint8 *)surface->pixels +
  209. cinfo.output_scanline * surface->pitch;
  210. jpeg_read_scanlines(&cinfo, rowptr, (JDIMENSION) 1);
  211. }
  212. jpeg_finish_decompress(&cinfo);
  213. /* Clean up and return */
  214. done:
  215. jpeg_destroy_decompress(&cinfo);
  216. return(surface);
  217. }