JpegDecode.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. int use_jcs_extensions = 0;
  46. #ifdef JCS_EXTENSIONS
  47. #if defined(LIBJPEG_TURBO_VERSION_NUMBER)
  48. #if LIBJPEG_TURBO_VERSION_NUMBER >= 1002010
  49. use_jcs_extensions = 1;
  50. #endif
  51. #else
  52. if (libjpeg_turbo_version) {
  53. use_jcs_extensions = strcmp(libjpeg_turbo_version, "1.2.1") >= 0;
  54. }
  55. #endif
  56. #endif
  57. return use_jcs_extensions;
  58. }
  59. /* -------------------------------------------------------------------- */
  60. /* Suspending input handler */
  61. /* -------------------------------------------------------------------- */
  62. METHODDEF(void)
  63. stub(j_decompress_ptr cinfo) { /* empty */ }
  64. METHODDEF(boolean)
  65. fill_input_buffer(j_decompress_ptr cinfo) {
  66. /* Suspension */
  67. return FALSE;
  68. }
  69. METHODDEF(void)
  70. skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
  71. JPEGSOURCE *source = (JPEGSOURCE *)cinfo->src;
  72. if (num_bytes > (long)source->pub.bytes_in_buffer) {
  73. /* We need to skip more data than we have in the buffer.
  74. This will force the JPEG library to suspend decoding. */
  75. source->skip = num_bytes - source->pub.bytes_in_buffer;
  76. source->pub.next_input_byte += source->pub.bytes_in_buffer;
  77. source->pub.bytes_in_buffer = 0;
  78. } else {
  79. /* Skip portion of the buffer */
  80. source->pub.bytes_in_buffer -= num_bytes;
  81. source->pub.next_input_byte += num_bytes;
  82. source->skip = 0;
  83. }
  84. }
  85. GLOBAL(void)
  86. jpeg_buffer_src(j_decompress_ptr cinfo, JPEGSOURCE *source) {
  87. cinfo->src = (void *)source;
  88. /* Prepare for suspending reader */
  89. source->pub.init_source = stub;
  90. source->pub.fill_input_buffer = fill_input_buffer;
  91. source->pub.skip_input_data = skip_input_data;
  92. source->pub.resync_to_restart = jpeg_resync_to_restart;
  93. source->pub.term_source = stub;
  94. source->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
  95. source->skip = 0;
  96. }
  97. /* -------------------------------------------------------------------- */
  98. /* Error handler */
  99. /* -------------------------------------------------------------------- */
  100. METHODDEF(void)
  101. error(j_common_ptr cinfo) {
  102. JPEGERROR *error;
  103. error = (JPEGERROR *)cinfo->err;
  104. longjmp(error->setjmp_buffer, 1);
  105. }
  106. METHODDEF(void)
  107. output(j_common_ptr cinfo) { /* nothing */ }
  108. /* -------------------------------------------------------------------- */
  109. /* Decoder */
  110. /* -------------------------------------------------------------------- */
  111. int
  112. ImagingJpegDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
  113. JPEGSTATE *context = (JPEGSTATE *)state->context;
  114. int ok;
  115. if (setjmp(context->error.setjmp_buffer)) {
  116. /* JPEG error handler */
  117. jpeg_destroy_decompress(&context->cinfo);
  118. state->errcode = IMAGING_CODEC_BROKEN;
  119. return -1;
  120. }
  121. if (!state->state) {
  122. /* Setup decompression context */
  123. context->cinfo.err = jpeg_std_error(&context->error.pub);
  124. context->error.pub.error_exit = error;
  125. context->error.pub.output_message = output;
  126. jpeg_create_decompress(&context->cinfo);
  127. jpeg_buffer_src(&context->cinfo, &context->source);
  128. /* Ready to decode */
  129. state->state = 1;
  130. }
  131. /* Load the source buffer */
  132. context->source.pub.next_input_byte = buf;
  133. context->source.pub.bytes_in_buffer = bytes;
  134. if (context->source.skip > 0) {
  135. skip_input_data(&context->cinfo, context->source.skip);
  136. if (context->source.skip > 0) {
  137. return context->source.pub.next_input_byte - buf;
  138. }
  139. }
  140. switch (state->state) {
  141. case 1:
  142. /* Read JPEG header, until we find an image body. */
  143. do {
  144. /* Note that we cannot return unless we have decoded
  145. as much data as possible. */
  146. ok = jpeg_read_header(&context->cinfo, FALSE);
  147. } while (ok == JPEG_HEADER_TABLES_ONLY);
  148. if (ok == JPEG_SUSPENDED) {
  149. break;
  150. }
  151. /* Decoder settings */
  152. /* jpegmode indicates whats in the file; if not set, we'll
  153. trust the decoder */
  154. if (strcmp(context->jpegmode, "L") == 0) {
  155. context->cinfo.jpeg_color_space = JCS_GRAYSCALE;
  156. } else if (strcmp(context->jpegmode, "RGB") == 0) {
  157. context->cinfo.jpeg_color_space = JCS_RGB;
  158. } else if (strcmp(context->jpegmode, "CMYK") == 0) {
  159. context->cinfo.jpeg_color_space = JCS_CMYK;
  160. } else if (strcmp(context->jpegmode, "YCbCr") == 0) {
  161. context->cinfo.jpeg_color_space = JCS_YCbCr;
  162. } else if (strcmp(context->jpegmode, "YCbCrK") == 0) {
  163. context->cinfo.jpeg_color_space = JCS_YCCK;
  164. }
  165. /* rawmode indicates what we want from the decoder. if not
  166. set, conversions are disabled */
  167. if (strcmp(context->rawmode, "L") == 0) {
  168. context->cinfo.out_color_space = JCS_GRAYSCALE;
  169. } else if (strcmp(context->rawmode, "RGB") == 0) {
  170. context->cinfo.out_color_space = JCS_RGB;
  171. }
  172. #ifdef JCS_EXTENSIONS
  173. else if (strcmp(context->rawmode, "RGBX") == 0) {
  174. context->cinfo.out_color_space = JCS_EXT_RGBX;
  175. }
  176. #endif
  177. else if (
  178. strcmp(context->rawmode, "CMYK") == 0 ||
  179. strcmp(context->rawmode, "CMYK;I") == 0) {
  180. context->cinfo.out_color_space = JCS_CMYK;
  181. } else if (strcmp(context->rawmode, "YCbCr") == 0) {
  182. context->cinfo.out_color_space = JCS_YCbCr;
  183. } else if (strcmp(context->rawmode, "YCbCrK") == 0) {
  184. context->cinfo.out_color_space = JCS_YCCK;
  185. } else {
  186. /* Disable decoder conversions */
  187. context->cinfo.jpeg_color_space = JCS_UNKNOWN;
  188. context->cinfo.out_color_space = JCS_UNKNOWN;
  189. }
  190. if (context->scale > 1) {
  191. context->cinfo.scale_num = 1;
  192. context->cinfo.scale_denom = context->scale;
  193. }
  194. if (context->draft) {
  195. context->cinfo.do_fancy_upsampling = FALSE;
  196. context->cinfo.dct_method = JDCT_FASTEST;
  197. }
  198. state->state++;
  199. /* fall through */
  200. case 2:
  201. /* Set things up for decompression (this processes the entire
  202. file if necessary to return data line by line) */
  203. if (!jpeg_start_decompress(&context->cinfo)) {
  204. break;
  205. }
  206. state->state++;
  207. /* fall through */
  208. case 3:
  209. /* Decompress a single line of data */
  210. ok = 1;
  211. while (state->y < state->ysize) {
  212. ok = jpeg_read_scanlines(&context->cinfo, &state->buffer, 1);
  213. if (ok != 1) {
  214. break;
  215. }
  216. state->shuffle(
  217. (UINT8 *)im->image[state->y + state->yoff] +
  218. state->xoff * im->pixelsize,
  219. state->buffer,
  220. state->xsize);
  221. state->y++;
  222. }
  223. if (ok != 1) {
  224. break;
  225. }
  226. state->state++;
  227. /* fall through */
  228. case 4:
  229. /* Finish decompression */
  230. if (!jpeg_finish_decompress(&context->cinfo)) {
  231. /* FIXME: add strictness mode test */
  232. if (state->y < state->ysize) {
  233. break;
  234. }
  235. }
  236. /* Clean up */
  237. jpeg_destroy_decompress(&context->cinfo);
  238. /* if (jerr.pub.num_warnings) return BROKEN; */
  239. return -1;
  240. }
  241. /* Return number of bytes consumed */
  242. return context->source.pub.next_input_byte - buf;
  243. }
  244. /* -------------------------------------------------------------------- */
  245. /* Cleanup */
  246. /* -------------------------------------------------------------------- */
  247. int
  248. 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