JpegEncode.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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)
  37. {
  38. /* empty */
  39. }
  40. METHODDEF(boolean)
  41. empty_output_buffer (j_compress_ptr cinfo)
  42. {
  43. /* Suspension */
  44. return FALSE;
  45. }
  46. GLOBAL(void)
  47. jpeg_buffer_dest(j_compress_ptr cinfo, JPEGDESTINATION* destination)
  48. {
  49. cinfo->dest = (void*) destination;
  50. destination->pub.init_destination = stub;
  51. destination->pub.empty_output_buffer = empty_output_buffer;
  52. destination->pub.term_destination = stub;
  53. }
  54. /* -------------------------------------------------------------------- */
  55. /* Error handler */
  56. /* -------------------------------------------------------------------- */
  57. METHODDEF(void)
  58. error(j_common_ptr cinfo)
  59. {
  60. JPEGERROR* error;
  61. error = (JPEGERROR*) cinfo->err;
  62. (*cinfo->err->output_message) (cinfo);
  63. longjmp(error->setjmp_buffer, 1);
  64. }
  65. /* -------------------------------------------------------------------- */
  66. /* Encoder */
  67. /* -------------------------------------------------------------------- */
  68. int
  69. ImagingJpegEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
  70. {
  71. JPEGENCODERSTATE* context = (JPEGENCODERSTATE*) state->context;
  72. int ok;
  73. if (setjmp(context->error.setjmp_buffer)) {
  74. /* JPEG error handler */
  75. jpeg_destroy_compress(&context->cinfo);
  76. state->errcode = IMAGING_CODEC_BROKEN;
  77. return -1;
  78. }
  79. if (!state->state) {
  80. /* Setup compression context (very similar to the decoder) */
  81. context->cinfo.err = jpeg_std_error(&context->error.pub);
  82. context->error.pub.error_exit = error;
  83. jpeg_create_compress(&context->cinfo);
  84. jpeg_buffer_dest(&context->cinfo, &context->destination);
  85. context->extra_offset = 0;
  86. /* Ready to encode */
  87. state->state = 1;
  88. }
  89. /* Load the destination buffer */
  90. context->destination.pub.next_output_byte = buf;
  91. context->destination.pub.free_in_buffer = bytes;
  92. switch (state->state) {
  93. case 1:
  94. context->cinfo.image_width = state->xsize;
  95. context->cinfo.image_height = state->ysize;
  96. switch (state->bits) {
  97. case 8:
  98. context->cinfo.input_components = 1;
  99. context->cinfo.in_color_space = JCS_GRAYSCALE;
  100. break;
  101. case 24:
  102. context->cinfo.input_components = 3;
  103. if (strcmp(im->mode, "YCbCr") == 0)
  104. context->cinfo.in_color_space = JCS_YCbCr;
  105. else
  106. context->cinfo.in_color_space = JCS_RGB;
  107. break;
  108. case 32:
  109. context->cinfo.input_components = 4;
  110. context->cinfo.in_color_space = JCS_CMYK;
  111. #ifdef JCS_EXTENSIONS
  112. if (strcmp(context->rawmode, "RGBX") == 0)
  113. context->cinfo.in_color_space = JCS_EXT_RGBX;
  114. #endif
  115. break;
  116. default:
  117. state->errcode = IMAGING_CODEC_CONFIG;
  118. return -1;
  119. }
  120. /* Compressor configuration */
  121. jpeg_set_defaults(&context->cinfo);
  122. /* Use custom quantization tables */
  123. if (context->qtables) {
  124. int i;
  125. int quality = 100;
  126. int last_q = 0;
  127. if (context->quality > 0) {
  128. quality = context->quality;
  129. }
  130. for (i = 0; i < context->qtablesLen; i++) {
  131. // TODO: Should add support for none baseline
  132. jpeg_add_quant_table(&context->cinfo, i, &context->qtables[i * DCTSIZE2],
  133. quality, TRUE);
  134. context->cinfo.comp_info[i].quant_tbl_no = i;
  135. last_q = i;
  136. }
  137. if (context->qtablesLen == 1) {
  138. // jpeg_set_defaults created two qtables internally, but we only wanted one.
  139. jpeg_add_quant_table(&context->cinfo, 1, &context->qtables[0],
  140. quality, TRUE);
  141. }
  142. for (i = last_q; i < context->cinfo.num_components; i++) {
  143. context->cinfo.comp_info[i].quant_tbl_no = last_q;
  144. }
  145. } else if (context->quality > 0) {
  146. jpeg_set_quality(&context->cinfo, context->quality, 1);
  147. }
  148. /* Set subsampling options */
  149. switch (context->subsampling)
  150. {
  151. case 0: /* 1x1 1x1 1x1 (4:4:4) : None */
  152. {
  153. context->cinfo.comp_info[0].h_samp_factor = 1;
  154. context->cinfo.comp_info[0].v_samp_factor = 1;
  155. context->cinfo.comp_info[1].h_samp_factor = 1;
  156. context->cinfo.comp_info[1].v_samp_factor = 1;
  157. context->cinfo.comp_info[2].h_samp_factor = 1;
  158. context->cinfo.comp_info[2].v_samp_factor = 1;
  159. break;
  160. }
  161. case 1: /* 2x1, 1x1, 1x1 (4:2:2) : Medium */
  162. {
  163. context->cinfo.comp_info[0].h_samp_factor = 2;
  164. context->cinfo.comp_info[0].v_samp_factor = 1;
  165. context->cinfo.comp_info[1].h_samp_factor = 1;
  166. context->cinfo.comp_info[1].v_samp_factor = 1;
  167. context->cinfo.comp_info[2].h_samp_factor = 1;
  168. context->cinfo.comp_info[2].v_samp_factor = 1;
  169. break;
  170. }
  171. case 2: /* 2x2, 1x1, 1x1 (4:2:0) : High */
  172. {
  173. context->cinfo.comp_info[0].h_samp_factor = 2;
  174. context->cinfo.comp_info[0].v_samp_factor = 2;
  175. context->cinfo.comp_info[1].h_samp_factor = 1;
  176. context->cinfo.comp_info[1].v_samp_factor = 1;
  177. context->cinfo.comp_info[2].h_samp_factor = 1;
  178. context->cinfo.comp_info[2].v_samp_factor = 1;
  179. break;
  180. }
  181. default:
  182. {
  183. /* Use the lib's default */
  184. break;
  185. }
  186. }
  187. if (context->progressive)
  188. jpeg_simple_progression(&context->cinfo);
  189. context->cinfo.smoothing_factor = context->smooth;
  190. context->cinfo.optimize_coding = (boolean) context->optimize;
  191. if (context->xdpi > 0 && context->ydpi > 0) {
  192. context->cinfo.density_unit = 1; /* dots per inch */
  193. context->cinfo.X_density = context->xdpi;
  194. context->cinfo.Y_density = context->ydpi;
  195. }
  196. switch (context->streamtype) {
  197. case 1:
  198. /* tables only -- not yet implemented */
  199. state->errcode = IMAGING_CODEC_CONFIG;
  200. return -1;
  201. case 2:
  202. /* image only */
  203. jpeg_suppress_tables(&context->cinfo, TRUE);
  204. jpeg_start_compress(&context->cinfo, FALSE);
  205. /* suppress extra section */
  206. context->extra_offset = context->extra_size;
  207. break;
  208. default:
  209. /* interchange stream */
  210. jpeg_start_compress(&context->cinfo, TRUE);
  211. break;
  212. }
  213. state->state++;
  214. /* fall through */
  215. case 2:
  216. // check for exif len + 'APP1' header bytes
  217. if (context->rawExifLen + 5 > context->destination.pub.free_in_buffer){
  218. break;
  219. }
  220. //add exif header
  221. if (context->rawExifLen > 0){
  222. jpeg_write_marker(&context->cinfo, JPEG_APP0+1,
  223. (unsigned char*)context->rawExif, context->rawExifLen);
  224. }
  225. state->state++;
  226. /* fall through */
  227. case 3:
  228. if (context->extra) {
  229. /* copy extra buffer to output buffer */
  230. unsigned int n = context->extra_size - context->extra_offset;
  231. if (n > context->destination.pub.free_in_buffer)
  232. n = context->destination.pub.free_in_buffer;
  233. memcpy(context->destination.pub.next_output_byte,
  234. context->extra + context->extra_offset, n);
  235. context->destination.pub.next_output_byte += n;
  236. context->destination.pub.free_in_buffer -= n;
  237. context->extra_offset += n;
  238. if (context->extra_offset >= context->extra_size)
  239. state->state++;
  240. else
  241. break;
  242. } else
  243. state->state++;
  244. case 4:
  245. if (1024 > context->destination.pub.free_in_buffer){
  246. break;
  247. }
  248. ok = 1;
  249. while (state->y < state->ysize) {
  250. state->shuffle(state->buffer,
  251. (UINT8*) im->image[state->y + state->yoff] +
  252. state->xoff * im->pixelsize, state->xsize);
  253. ok = jpeg_write_scanlines(&context->cinfo, &state->buffer, 1);
  254. if (ok != 1)
  255. break;
  256. state->y++;
  257. }
  258. if (ok != 1)
  259. break;
  260. state->state++;
  261. /* fall through */
  262. case 5:
  263. /* Finish compression */
  264. if (context->destination.pub.free_in_buffer < 100)
  265. break;
  266. jpeg_finish_compress(&context->cinfo);
  267. /* Clean up */
  268. if (context->extra) {
  269. free(context->extra);
  270. context->extra = NULL;
  271. }
  272. if (context->rawExif) {
  273. free(context->rawExif);
  274. context->rawExif = NULL;
  275. }
  276. if (context->qtables) {
  277. free(context->qtables);
  278. context->qtables = NULL;
  279. }
  280. jpeg_destroy_compress(&context->cinfo);
  281. /* if (jerr.pub.num_warnings) return BROKEN; */
  282. state->errcode = IMAGING_CODEC_END;
  283. break;
  284. }
  285. /* Return number of bytes in output buffer */
  286. return context->destination.pub.next_output_byte - buf;
  287. }
  288. const char*
  289. ImagingJpegVersion(void)
  290. {
  291. static char version[20];
  292. sprintf(version, "%d.%d", JPEG_LIB_VERSION / 10, JPEG_LIB_VERSION % 10);
  293. return version;
  294. }
  295. #endif