JpegDecode.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * decoder for JPEG image data.
  6. *
  7. * history:
  8. * 1996-05-02 fl Created
  9. * 1996-05-05 fl Handle small JPEG files correctly
  10. * 1996-05-28 fl Added "draft mode" support
  11. * 1997-01-25 fl Added colour conversion override
  12. * 1998-01-31 fl Adapted to libjpeg 6a
  13. * 1998-07-12 fl Extended YCbCr support
  14. * 1998-12-29 fl Added new state to handle suspension in multipass modes
  15. * 2000-10-12 fl Suppress warnings
  16. * 2000-12-04 fl Suppress errors beyond end of image data
  17. *
  18. * Copyright (c) 1998-2000 Secret Labs AB
  19. * Copyright (c) 1996-2000 Fredrik Lundh
  20. *
  21. * See the README file for details on usage and redistribution.
  22. */
  23. #include "Imaging.h"
  24. #ifdef HAVE_LIBJPEG
  25. #undef HAVE_PROTOTYPES
  26. #undef HAVE_STDLIB_H
  27. #undef HAVE_STDDEF_H
  28. #undef UINT8
  29. #undef UINT16
  30. #undef UINT32
  31. #undef INT16
  32. #undef INT32
  33. #include "Jpeg.h"
  34. #define STRINGIFY(x) #x
  35. #define TOSTRING(x) STRINGIFY(x)
  36. // There is no way to compare versions on compile time,
  37. // so we have to do that in runtime.
  38. #ifdef LIBJPEG_TURBO_VERSION
  39. char *libjpeg_turbo_version = TOSTRING(LIBJPEG_TURBO_VERSION);
  40. #else
  41. char *libjpeg_turbo_version = NULL;
  42. #endif
  43. int
  44. ImagingJpegUseJCSExtensions()
  45. {
  46. int use_jcs_extensions = 0;
  47. #ifdef JCS_EXTENSIONS
  48. #if defined(LIBJPEG_TURBO_VERSION_NUMBER)
  49. #if LIBJPEG_TURBO_VERSION_NUMBER >= 1002010
  50. use_jcs_extensions = 1;
  51. #endif
  52. #else
  53. if (libjpeg_turbo_version) {
  54. use_jcs_extensions = strcmp(libjpeg_turbo_version, "1.2.1") >= 0;
  55. }
  56. #endif
  57. #endif
  58. return use_jcs_extensions;
  59. }
  60. /* -------------------------------------------------------------------- */
  61. /* Suspending input handler */
  62. /* -------------------------------------------------------------------- */
  63. METHODDEF(void)
  64. stub(j_decompress_ptr cinfo)
  65. {
  66. /* empty */
  67. }
  68. METHODDEF(boolean)
  69. fill_input_buffer(j_decompress_ptr cinfo)
  70. {
  71. /* Suspension */
  72. return FALSE;
  73. }
  74. METHODDEF(void)
  75. skip_input_data(j_decompress_ptr cinfo, long num_bytes)
  76. {
  77. JPEGSOURCE* source = (JPEGSOURCE*) cinfo->src;
  78. if (num_bytes > (long) source->pub.bytes_in_buffer) {
  79. /* We need to skip more data than we have in the buffer.
  80. This will force the JPEG library to suspend decoding. */
  81. source->skip = num_bytes - source->pub.bytes_in_buffer;
  82. source->pub.next_input_byte += source->pub.bytes_in_buffer;
  83. source->pub.bytes_in_buffer = 0;
  84. } else {
  85. /* Skip portion of the buffer */
  86. source->pub.bytes_in_buffer -= num_bytes;
  87. source->pub.next_input_byte += num_bytes;
  88. source->skip = 0;
  89. }
  90. }
  91. GLOBAL(void)
  92. jpeg_buffer_src(j_decompress_ptr cinfo, JPEGSOURCE* source)
  93. {
  94. cinfo->src = (void*) source;
  95. /* Prepare for suspending reader */
  96. source->pub.init_source = stub;
  97. source->pub.fill_input_buffer = fill_input_buffer;
  98. source->pub.skip_input_data = skip_input_data;
  99. source->pub.resync_to_restart = jpeg_resync_to_restart;
  100. source->pub.term_source = stub;
  101. source->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  102. source->skip = 0;
  103. }
  104. /* -------------------------------------------------------------------- */
  105. /* Error handler */
  106. /* -------------------------------------------------------------------- */
  107. METHODDEF(void)
  108. error(j_common_ptr cinfo)
  109. {
  110. JPEGERROR* error;
  111. error = (JPEGERROR*) cinfo->err;
  112. longjmp(error->setjmp_buffer, 1);
  113. }
  114. METHODDEF(void)
  115. output(j_common_ptr cinfo)
  116. {
  117. /* nothing */
  118. }
  119. /* -------------------------------------------------------------------- */
  120. /* Decoder */
  121. /* -------------------------------------------------------------------- */
  122. int
  123. ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
  124. {
  125. JPEGSTATE* context = (JPEGSTATE*) state->context;
  126. int ok;
  127. if (setjmp(context->error.setjmp_buffer)) {
  128. /* JPEG error handler */
  129. jpeg_destroy_decompress(&context->cinfo);
  130. state->errcode = IMAGING_CODEC_BROKEN;
  131. return -1;
  132. }
  133. if (!state->state) {
  134. /* Setup decompression context */
  135. context->cinfo.err = jpeg_std_error(&context->error.pub);
  136. context->error.pub.error_exit = error;
  137. context->error.pub.output_message = output;
  138. jpeg_create_decompress(&context->cinfo);
  139. jpeg_buffer_src(&context->cinfo, &context->source);
  140. /* Ready to decode */
  141. state->state = 1;
  142. }
  143. /* Load the source buffer */
  144. context->source.pub.next_input_byte = buf;
  145. context->source.pub.bytes_in_buffer = bytes;
  146. if (context->source.skip > 0) {
  147. skip_input_data(&context->cinfo, context->source.skip);
  148. if (context->source.skip > 0)
  149. return context->source.pub.next_input_byte - buf;
  150. }
  151. switch (state->state) {
  152. case 1:
  153. /* Read JPEG header, until we find an image body. */
  154. do {
  155. /* Note that we cannot return unless we have decoded
  156. as much data as possible. */
  157. ok = jpeg_read_header(&context->cinfo, FALSE);
  158. } while (ok == JPEG_HEADER_TABLES_ONLY);
  159. if (ok == JPEG_SUSPENDED)
  160. break;
  161. /* Decoder settings */
  162. /* jpegmode indicates whats in the file; if not set, we'll
  163. trust the decoder */
  164. if (strcmp(context->jpegmode, "L") == 0)
  165. context->cinfo.jpeg_color_space = JCS_GRAYSCALE;
  166. else if (strcmp(context->jpegmode, "RGB") == 0)
  167. context->cinfo.jpeg_color_space = JCS_RGB;
  168. else if (strcmp(context->jpegmode, "CMYK") == 0)
  169. context->cinfo.jpeg_color_space = JCS_CMYK;
  170. else if (strcmp(context->jpegmode, "YCbCr") == 0)
  171. context->cinfo.jpeg_color_space = JCS_YCbCr;
  172. else if (strcmp(context->jpegmode, "YCbCrK") == 0) {
  173. context->cinfo.jpeg_color_space = JCS_YCCK;
  174. }
  175. /* rawmode indicates what we want from the decoder. if not
  176. set, conversions are disabled */
  177. if (strcmp(context->rawmode, "L") == 0)
  178. context->cinfo.out_color_space = JCS_GRAYSCALE;
  179. else if (strcmp(context->rawmode, "RGB") == 0)
  180. context->cinfo.out_color_space = JCS_RGB;
  181. #ifdef JCS_EXTENSIONS
  182. else if (strcmp(context->rawmode, "RGBX") == 0)
  183. context->cinfo.out_color_space = JCS_EXT_RGBX;
  184. #endif
  185. else if (strcmp(context->rawmode, "CMYK") == 0 ||
  186. strcmp(context->rawmode, "CMYK;I") == 0)
  187. context->cinfo.out_color_space = JCS_CMYK;
  188. else if (strcmp(context->rawmode, "YCbCr") == 0)
  189. context->cinfo.out_color_space = JCS_YCbCr;
  190. else if (strcmp(context->rawmode, "YCbCrK") == 0)
  191. context->cinfo.out_color_space = JCS_YCCK;
  192. else {
  193. /* Disable decoder conversions */
  194. context->cinfo.jpeg_color_space = JCS_UNKNOWN;
  195. context->cinfo.out_color_space = JCS_UNKNOWN;
  196. }
  197. if (context->scale > 1) {
  198. context->cinfo.scale_num = 1;
  199. context->cinfo.scale_denom = context->scale;
  200. }
  201. if (context->draft) {
  202. context->cinfo.do_fancy_upsampling = FALSE;
  203. context->cinfo.dct_method = JDCT_FASTEST;
  204. }
  205. state->state++;
  206. /* fall through */
  207. case 2:
  208. /* Set things up for decompression (this processes the entire
  209. file if necessary to return data line by line) */
  210. if (!jpeg_start_decompress(&context->cinfo))
  211. break;
  212. state->state++;
  213. /* fall through */
  214. case 3:
  215. /* Decompress a single line of data */
  216. ok = 1;
  217. while (state->y < state->ysize) {
  218. ok = jpeg_read_scanlines(&context->cinfo, &state->buffer, 1);
  219. if (ok != 1)
  220. break;
  221. state->shuffle((UINT8*) im->image[state->y + state->yoff] +
  222. state->xoff * im->pixelsize, state->buffer,
  223. state->xsize);
  224. state->y++;
  225. }
  226. if (ok != 1)
  227. break;
  228. state->state++;
  229. /* fall through */
  230. case 4:
  231. /* Finish decompression */
  232. if (!jpeg_finish_decompress(&context->cinfo)) {
  233. /* FIXME: add strictness mode test */
  234. if (state->y < state->ysize)
  235. break;
  236. }
  237. /* Clean up */
  238. jpeg_destroy_decompress(&context->cinfo);
  239. /* if (jerr.pub.num_warnings) return BROKEN; */
  240. return -1;
  241. }
  242. /* Return number of bytes consumed */
  243. return context->source.pub.next_input_byte - buf;
  244. }
  245. /* -------------------------------------------------------------------- */
  246. /* Cleanup */
  247. /* -------------------------------------------------------------------- */
  248. int ImagingJpegDecodeCleanup(ImagingCodecState state){
  249. /* called to free the decompression engine when the decode terminates
  250. due to a corrupt or truncated image
  251. */
  252. JPEGSTATE* context = (JPEGSTATE*) state->context;
  253. /* Clean up */
  254. jpeg_destroy_decompress(&context->cinfo);
  255. return -1;
  256. }
  257. #endif