JpegEncode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * coder for JPEG data
  6. *
  7. * history:
  8. * 1996-05-06 fl created
  9. * 1996-07-16 fl don't drop last block of encoded data
  10. * 1996-12-30 fl added quality and progressive settings
  11. * 1997-01-08 fl added streamtype settings
  12. * 1998-01-31 fl adapted to libjpeg 6a
  13. * 1998-07-12 fl added YCbCr support
  14. * 2001-04-16 fl added DPI write support
  15. *
  16. * Copyright (c) 1997-2001 by Secret Labs AB
  17. * Copyright (c) 1995-1997 by Fredrik Lundh
  18. *
  19. * See the README file for details on usage and redistribution.
  20. */
  21. #include "Imaging.h"
  22. #ifdef HAVE_LIBJPEG
  23. #undef HAVE_PROTOTYPES
  24. #undef HAVE_STDLIB_H
  25. #undef HAVE_STDDEF_H
  26. #undef UINT8
  27. #undef UINT16
  28. #undef UINT32
  29. #undef INT16
  30. #undef INT32
  31. #include "Jpeg.h"
  32. /* -------------------------------------------------------------------- */
  33. /* Suspending output handler */
  34. /* -------------------------------------------------------------------- */
  35. METHODDEF(void)
  36. stub(j_compress_ptr cinfo) { /* empty */ }
  37. METHODDEF(boolean)
  38. empty_output_buffer(j_compress_ptr cinfo) {
  39. /* Suspension */
  40. return FALSE;
  41. }
  42. GLOBAL(void)
  43. jpeg_buffer_dest(j_compress_ptr cinfo, JPEGDESTINATION *destination) {
  44. cinfo->dest = (void *)destination;
  45. destination->pub.init_destination = stub;
  46. destination->pub.empty_output_buffer = empty_output_buffer;
  47. destination->pub.term_destination = stub;
  48. }
  49. /* -------------------------------------------------------------------- */
  50. /* Error handler */
  51. /* -------------------------------------------------------------------- */
  52. METHODDEF(void)
  53. error(j_common_ptr cinfo) {
  54. JPEGERROR *error;
  55. error = (JPEGERROR *)cinfo->err;
  56. (*cinfo->err->output_message)(cinfo);
  57. longjmp(error->setjmp_buffer, 1);
  58. }
  59. /* -------------------------------------------------------------------- */
  60. /* Encoder */
  61. /* -------------------------------------------------------------------- */
  62. int
  63. ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes) {
  64. JPEGENCODERSTATE *context = (JPEGENCODERSTATE *)state->context;
  65. int ok;
  66. if (setjmp(context->error.setjmp_buffer)) {
  67. /* JPEG error handler */
  68. jpeg_destroy_compress(&context->cinfo);
  69. state->errcode = IMAGING_CODEC_BROKEN;
  70. return -1;
  71. }
  72. if (!state->state) {
  73. /* Setup compression context (very similar to the decoder) */
  74. context->cinfo.err = jpeg_std_error(&context->error.pub);
  75. context->error.pub.error_exit = error;
  76. jpeg_create_compress(&context->cinfo);
  77. jpeg_buffer_dest(&context->cinfo, &context->destination);
  78. context->extra_offset = 0;
  79. /* Ready to encode */
  80. state->state = 1;
  81. }
  82. /* Load the destination buffer */
  83. context->destination.pub.next_output_byte = buf;
  84. context->destination.pub.free_in_buffer = bytes;
  85. switch (state->state) {
  86. case 1:
  87. context->cinfo.image_width = state->xsize;
  88. context->cinfo.image_height = state->ysize;
  89. switch (state->bits) {
  90. case 8:
  91. context->cinfo.input_components = 1;
  92. context->cinfo.in_color_space = JCS_GRAYSCALE;
  93. break;
  94. case 24:
  95. context->cinfo.input_components = 3;
  96. if (strcmp(im->mode, "YCbCr") == 0) {
  97. context->cinfo.in_color_space = JCS_YCbCr;
  98. } else {
  99. context->cinfo.in_color_space = JCS_RGB;
  100. }
  101. break;
  102. case 32:
  103. context->cinfo.input_components = 4;
  104. context->cinfo.in_color_space = JCS_CMYK;
  105. #ifdef JCS_EXTENSIONS
  106. if (strcmp(context->rawmode, "RGBX") == 0) {
  107. context->cinfo.in_color_space = JCS_EXT_RGBX;
  108. }
  109. #endif
  110. break;
  111. default:
  112. state->errcode = IMAGING_CODEC_CONFIG;
  113. return -1;
  114. }
  115. /* Compressor configuration */
  116. jpeg_set_defaults(&context->cinfo);
  117. /* Prevent RGB -> YCbCr conversion */
  118. if (context->keep_rgb) {
  119. switch (context->cinfo.in_color_space) {
  120. case JCS_RGB:
  121. #ifdef JCS_EXTENSIONS
  122. case JCS_EXT_RGBX:
  123. #endif
  124. switch (context->subsampling) {
  125. case -1: /* Default */
  126. case 0: /* No subsampling */
  127. break;
  128. default:
  129. /* Would subsample the green and blue
  130. channels, which doesn't make sense */
  131. state->errcode = IMAGING_CODEC_CONFIG;
  132. return -1;
  133. }
  134. jpeg_set_colorspace(&context->cinfo, JCS_RGB);
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. /* Use custom quantization tables */
  141. if (context->qtables) {
  142. int i;
  143. int quality = 100;
  144. int last_q = 0;
  145. if (context->quality != -1) {
  146. quality = context->quality;
  147. }
  148. for (i = 0; i < context->qtablesLen; i++) {
  149. jpeg_add_quant_table(
  150. &context->cinfo,
  151. i,
  152. &context->qtables[i * DCTSIZE2],
  153. quality,
  154. FALSE);
  155. context->cinfo.comp_info[i].quant_tbl_no = i;
  156. last_q = i;
  157. }
  158. if (context->qtablesLen == 1) {
  159. // jpeg_set_defaults created two qtables internally, but we only
  160. // wanted one.
  161. jpeg_add_quant_table(
  162. &context->cinfo, 1, &context->qtables[0], quality, FALSE);
  163. }
  164. for (i = last_q; i < context->cinfo.num_components; i++) {
  165. context->cinfo.comp_info[i].quant_tbl_no = last_q;
  166. }
  167. } else if (context->quality != -1) {
  168. jpeg_set_quality(&context->cinfo, context->quality, TRUE);
  169. }
  170. /* Set subsampling options */
  171. switch (context->subsampling) {
  172. case 0: /* 1x1 1x1 1x1 (4:4:4) : None */
  173. {
  174. context->cinfo.comp_info[0].h_samp_factor = 1;
  175. context->cinfo.comp_info[0].v_samp_factor = 1;
  176. context->cinfo.comp_info[1].h_samp_factor = 1;
  177. context->cinfo.comp_info[1].v_samp_factor = 1;
  178. context->cinfo.comp_info[2].h_samp_factor = 1;
  179. context->cinfo.comp_info[2].v_samp_factor = 1;
  180. break;
  181. }
  182. case 1: /* 2x1, 1x1, 1x1 (4:2:2) : Medium */
  183. {
  184. context->cinfo.comp_info[0].h_samp_factor = 2;
  185. context->cinfo.comp_info[0].v_samp_factor = 1;
  186. context->cinfo.comp_info[1].h_samp_factor = 1;
  187. context->cinfo.comp_info[1].v_samp_factor = 1;
  188. context->cinfo.comp_info[2].h_samp_factor = 1;
  189. context->cinfo.comp_info[2].v_samp_factor = 1;
  190. break;
  191. }
  192. case 2: /* 2x2, 1x1, 1x1 (4:2:0) : High */
  193. {
  194. context->cinfo.comp_info[0].h_samp_factor = 2;
  195. context->cinfo.comp_info[0].v_samp_factor = 2;
  196. context->cinfo.comp_info[1].h_samp_factor = 1;
  197. context->cinfo.comp_info[1].v_samp_factor = 1;
  198. context->cinfo.comp_info[2].h_samp_factor = 1;
  199. context->cinfo.comp_info[2].v_samp_factor = 1;
  200. break;
  201. }
  202. default: {
  203. /* Use the lib's default */
  204. break;
  205. }
  206. }
  207. if (context->progressive) {
  208. jpeg_simple_progression(&context->cinfo);
  209. }
  210. context->cinfo.smoothing_factor = context->smooth;
  211. context->cinfo.optimize_coding = (boolean)context->optimize;
  212. context->cinfo.restart_interval = context->restart_marker_blocks;
  213. context->cinfo.restart_in_rows = context->restart_marker_rows;
  214. if (context->xdpi > 0 && context->ydpi > 0) {
  215. context->cinfo.write_JFIF_header = TRUE;
  216. context->cinfo.density_unit = 1; /* dots per inch */
  217. context->cinfo.X_density = context->xdpi;
  218. context->cinfo.Y_density = context->ydpi;
  219. }
  220. switch (context->streamtype) {
  221. case 1:
  222. /* tables only */
  223. jpeg_write_tables(&context->cinfo);
  224. goto cleanup;
  225. case 2:
  226. /* image only */
  227. jpeg_suppress_tables(&context->cinfo, TRUE);
  228. jpeg_start_compress(&context->cinfo, FALSE);
  229. /* suppress extra section */
  230. context->extra_offset = context->extra_size;
  231. break;
  232. default:
  233. /* interchange stream */
  234. jpeg_start_compress(&context->cinfo, TRUE);
  235. break;
  236. }
  237. state->state++;
  238. /* fall through */
  239. case 2:
  240. // check for exif len + 'APP1' header bytes
  241. if (context->rawExifLen + 5 > context->destination.pub.free_in_buffer) {
  242. break;
  243. }
  244. // add exif header
  245. if (context->rawExifLen > 0) {
  246. jpeg_write_marker(
  247. &context->cinfo,
  248. JPEG_APP0 + 1,
  249. (unsigned char *)context->rawExif,
  250. context->rawExifLen);
  251. }
  252. state->state++;
  253. /* fall through */
  254. case 3:
  255. if (context->extra) {
  256. /* copy extra buffer to output buffer */
  257. unsigned int n = context->extra_size - context->extra_offset;
  258. if (n > context->destination.pub.free_in_buffer) {
  259. n = context->destination.pub.free_in_buffer;
  260. }
  261. memcpy(
  262. context->destination.pub.next_output_byte,
  263. context->extra + context->extra_offset,
  264. n);
  265. context->destination.pub.next_output_byte += n;
  266. context->destination.pub.free_in_buffer -= n;
  267. context->extra_offset += n;
  268. if (context->extra_offset >= context->extra_size) {
  269. state->state++;
  270. } else {
  271. break;
  272. }
  273. } else {
  274. state->state++;
  275. }
  276. case 4:
  277. if (context->comment) {
  278. jpeg_write_marker(&context->cinfo, JPEG_COM, (unsigned char *)context->comment, context->comment_size);
  279. }
  280. state->state++;
  281. case 5:
  282. if (1024 > context->destination.pub.free_in_buffer) {
  283. break;
  284. }
  285. ok = 1;
  286. while (state->y < state->ysize) {
  287. state->shuffle(
  288. state->buffer,
  289. (UINT8 *)im->image[state->y + state->yoff] +
  290. state->xoff * im->pixelsize,
  291. state->xsize);
  292. ok = jpeg_write_scanlines(&context->cinfo, &state->buffer, 1);
  293. if (ok != 1) {
  294. break;
  295. }
  296. state->y++;
  297. }
  298. if (ok != 1) {
  299. break;
  300. }
  301. state->state++;
  302. /* fall through */
  303. case 6:
  304. /* Finish compression */
  305. if (context->destination.pub.free_in_buffer < 100) {
  306. break;
  307. }
  308. jpeg_finish_compress(&context->cinfo);
  309. cleanup:
  310. /* Clean up */
  311. if (context->comment) {
  312. free(context->comment);
  313. context->comment = NULL;
  314. }
  315. if (context->extra) {
  316. free(context->extra);
  317. context->extra = NULL;
  318. }
  319. if (context->rawExif) {
  320. free(context->rawExif);
  321. context->rawExif = NULL;
  322. }
  323. if (context->qtables) {
  324. free(context->qtables);
  325. context->qtables = NULL;
  326. }
  327. jpeg_destroy_compress(&context->cinfo);
  328. /* if (jerr.pub.num_warnings) return BROKEN; */
  329. state->errcode = IMAGING_CODEC_END;
  330. break;
  331. }
  332. /* Return number of bytes in output buffer */
  333. return context->destination.pub.next_output_byte - buf;
  334. }
  335. const char *
  336. ImagingJpegVersion(void) {
  337. static char version[20];
  338. sprintf(version, "%d.%d", JPEG_LIB_VERSION / 10, JPEG_LIB_VERSION % 10);
  339. return version;
  340. }
  341. #endif