Jpeg2KEncode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * decoder for JPEG2000 image data.
  6. *
  7. * history:
  8. * 2014-03-12 ajh Created
  9. *
  10. * Copyright (c) 2014 Coriolis Systems Limited
  11. * Copyright (c) 2014 Alastair Houghton
  12. *
  13. * See the README file for details on usage and redistribution.
  14. */
  15. #include "Imaging.h"
  16. #ifdef HAVE_OPENJPEG
  17. #include "Jpeg2K.h"
  18. #define CINEMA_24_CS_LENGTH 1302083
  19. #define CINEMA_48_CS_LENGTH 651041
  20. #define COMP_24_CS_MAX_LENGTH 1041666
  21. #define COMP_48_CS_MAX_LENGTH 520833
  22. /* -------------------------------------------------------------------- */
  23. /* Error handler */
  24. /* -------------------------------------------------------------------- */
  25. static void
  26. j2k_error(const char *msg, void *client_data)
  27. {
  28. JPEG2KENCODESTATE *state = (JPEG2KENCODESTATE *) client_data;
  29. free((void *)state->error_msg);
  30. state->error_msg = strdup(msg);
  31. }
  32. static void
  33. j2k_warn(const char *msg, void *client_data)
  34. {
  35. // Null handler
  36. }
  37. /* -------------------------------------------------------------------- */
  38. /* Buffer output stream */
  39. /* -------------------------------------------------------------------- */
  40. static OPJ_SIZE_T
  41. j2k_write(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
  42. {
  43. ImagingCodecState state = (ImagingCodecState)p_user_data;
  44. int result;
  45. result = _imaging_write_pyFd(state->fd, p_buffer, p_nb_bytes);
  46. return result ? result : (OPJ_SIZE_T)-1;
  47. }
  48. static OPJ_OFF_T
  49. j2k_skip(OPJ_OFF_T p_nb_bytes, void *p_user_data)
  50. {
  51. ImagingCodecState state = (ImagingCodecState)p_user_data;
  52. char *buffer;
  53. int result;
  54. /* Explicitly write zeros */
  55. buffer = calloc(p_nb_bytes,1);
  56. if (!buffer) {
  57. return (OPJ_OFF_T)-1;
  58. }
  59. result = _imaging_write_pyFd(state->fd, buffer, p_nb_bytes);
  60. free(buffer);
  61. return result ? result : p_nb_bytes;
  62. }
  63. static OPJ_BOOL
  64. j2k_seek(OPJ_OFF_T p_nb_bytes, void *p_user_data)
  65. {
  66. ImagingCodecState state = (ImagingCodecState)p_user_data;
  67. off_t pos = 0;
  68. _imaging_seek_pyFd(state->fd, p_nb_bytes, SEEK_SET);
  69. pos = _imaging_tell_pyFd(state->fd);
  70. return pos == p_nb_bytes;
  71. }
  72. /* -------------------------------------------------------------------- */
  73. /* Encoder */
  74. /* -------------------------------------------------------------------- */
  75. typedef void (*j2k_pack_tile_t)(Imaging im, UINT8 *buf,
  76. unsigned x0, unsigned y0,
  77. unsigned w, unsigned h);
  78. static void
  79. j2k_pack_l(Imaging im, UINT8 *buf,
  80. unsigned x0, unsigned y0, unsigned w, unsigned h)
  81. {
  82. UINT8 *ptr = buf;
  83. unsigned x,y;
  84. for (y = 0; y < h; ++y) {
  85. UINT8 *data = (UINT8 *)(im->image[y + y0] + x0);
  86. for (x = 0; x < w; ++x)
  87. *ptr++ = *data++;
  88. }
  89. }
  90. static void
  91. j2k_pack_i16(Imaging im, UINT8 *buf,
  92. unsigned x0, unsigned y0, unsigned w, unsigned h)
  93. {
  94. UINT8 *ptr = buf;
  95. unsigned x,y;
  96. for (y = 0; y < h; ++y) {
  97. UINT8 *data = (UINT8 *)(im->image[y + y0] + x0);
  98. for (x = 0; x < w; ++x) {
  99. *ptr++ = *data++;
  100. *ptr++ = *data++;
  101. }
  102. }
  103. }
  104. static void
  105. j2k_pack_la(Imaging im, UINT8 *buf,
  106. unsigned x0, unsigned y0, unsigned w, unsigned h)
  107. {
  108. UINT8 *ptr = buf;
  109. UINT8 *ptra = buf + w * h;
  110. unsigned x,y;
  111. for (y = 0; y < h; ++y) {
  112. UINT8 *data = (UINT8 *)(im->image[y + y0] + 4 * x0);
  113. for (x = 0; x < w; ++x) {
  114. *ptr++ = data[0];
  115. *ptra++ = data[3];
  116. data += 4;
  117. }
  118. }
  119. }
  120. static void
  121. j2k_pack_rgb(Imaging im, UINT8 *buf,
  122. unsigned x0, unsigned y0, unsigned w, unsigned h)
  123. {
  124. UINT8 *pr = buf;
  125. UINT8 *pg = pr + w * h;
  126. UINT8 *pb = pg + w * h;
  127. unsigned x,y;
  128. for (y = 0; y < h; ++y) {
  129. UINT8 *data = (UINT8 *)(im->image[y + y0] + 4 * x0);
  130. for (x = 0; x < w; ++x) {
  131. *pr++ = data[0];
  132. *pg++ = data[1];
  133. *pb++ = data[2];
  134. data += 4;
  135. }
  136. }
  137. }
  138. static void
  139. j2k_pack_rgba(Imaging im, UINT8 *buf,
  140. unsigned x0, unsigned y0, unsigned w, unsigned h)
  141. {
  142. UINT8 *pr = buf;
  143. UINT8 *pg = pr + w * h;
  144. UINT8 *pb = pg + w * h;
  145. UINT8 *pa = pb + w * h;
  146. unsigned x,y;
  147. for (y = 0; y < h; ++y) {
  148. UINT8 *data = (UINT8 *)(im->image[y + y0] + 4 * x0);
  149. for (x = 0; x < w; ++x) {
  150. *pr++ = *data++;
  151. *pg++ = *data++;
  152. *pb++ = *data++;
  153. *pa++ = *data++;
  154. }
  155. }
  156. }
  157. enum {
  158. J2K_STATE_START = 0,
  159. J2K_STATE_ENCODING = 1,
  160. J2K_STATE_DONE = 2,
  161. J2K_STATE_FAILED = 3,
  162. };
  163. static void
  164. j2k_set_cinema_params(Imaging im, int components, opj_cparameters_t *params)
  165. {
  166. float rate;
  167. int n;
  168. /* These settings have been copied from opj_compress in the OpenJPEG
  169. sources. */
  170. params->tile_size_on = OPJ_FALSE;
  171. params->cp_tdx = params->cp_tdy = 1;
  172. params->tp_flag = 'C';
  173. params->tp_on = 1;
  174. params->cp_tx0 = params->cp_ty0 = 0;
  175. params->image_offset_x0 = params->image_offset_y0 = 0;
  176. params->cblockw_init = 32;
  177. params->cblockh_init = 32;
  178. params->csty |= 0x01;
  179. params->prog_order = OPJ_CPRL;
  180. params->roi_compno = -1;
  181. params->subsampling_dx = params->subsampling_dy = 1;
  182. params->irreversible = 1;
  183. if (params->cp_cinema == OPJ_CINEMA4K_24) {
  184. float max_rate = ((float)(components * im->xsize * im->ysize * 8)
  185. / (CINEMA_24_CS_LENGTH * 8));
  186. params->POC[0].tile = 1;
  187. params->POC[0].resno0 = 0;
  188. params->POC[0].compno0 = 0;
  189. params->POC[0].layno1 = 1;
  190. params->POC[0].resno1 = params->numresolution - 1;
  191. params->POC[0].compno1 = 3;
  192. params->POC[0].prg1 = OPJ_CPRL;
  193. params->POC[1].tile = 1;
  194. params->POC[1].resno0 = 0;
  195. params->POC[1].compno0 = 0;
  196. params->POC[1].layno1 = 1;
  197. params->POC[1].resno1 = params->numresolution - 1;
  198. params->POC[1].compno1 = 3;
  199. params->POC[1].prg1 = OPJ_CPRL;
  200. params->numpocs = 2;
  201. for (n = 0; n < params->tcp_numlayers; ++n) {
  202. rate = 0;
  203. if (params->tcp_rates[0] == 0) {
  204. params->tcp_rates[n] = max_rate;
  205. } else {
  206. rate = ((float)(components * im->xsize * im->ysize * 8)
  207. / (params->tcp_rates[n] * 8));
  208. if (rate > CINEMA_24_CS_LENGTH)
  209. params->tcp_rates[n] = max_rate;
  210. }
  211. }
  212. params->max_comp_size = COMP_24_CS_MAX_LENGTH;
  213. } else {
  214. float max_rate = ((float)(components * im->xsize * im->ysize * 8)
  215. / (CINEMA_48_CS_LENGTH * 8));
  216. for (n = 0; n < params->tcp_numlayers; ++n) {
  217. rate = 0;
  218. if (params->tcp_rates[0] == 0) {
  219. params->tcp_rates[n] = max_rate;
  220. } else {
  221. rate = ((float)(components * im->xsize * im->ysize * 8)
  222. / (params->tcp_rates[n] * 8));
  223. if (rate > CINEMA_48_CS_LENGTH)
  224. params->tcp_rates[n] = max_rate;
  225. }
  226. }
  227. params->max_comp_size = COMP_48_CS_MAX_LENGTH;
  228. }
  229. }
  230. static int
  231. j2k_encode_entry(Imaging im, ImagingCodecState state)
  232. {
  233. JPEG2KENCODESTATE *context = (JPEG2KENCODESTATE *)state->context;
  234. opj_stream_t *stream = NULL;
  235. opj_image_t *image = NULL;
  236. opj_codec_t *codec = NULL;
  237. opj_cparameters_t params;
  238. unsigned components;
  239. OPJ_COLOR_SPACE color_space;
  240. opj_image_cmptparm_t image_params[4];
  241. unsigned xsiz, ysiz;
  242. unsigned tile_width, tile_height;
  243. unsigned tiles_x, tiles_y;
  244. unsigned x, y, tile_ndx;
  245. unsigned n;
  246. j2k_pack_tile_t pack;
  247. int ret = -1;
  248. unsigned prec = 8;
  249. unsigned bpp = 8;
  250. unsigned _overflow_scale_factor;
  251. stream = opj_stream_create(BUFFER_SIZE, OPJ_FALSE);
  252. if (!stream) {
  253. state->errcode = IMAGING_CODEC_BROKEN;
  254. state->state = J2K_STATE_FAILED;
  255. goto quick_exit;
  256. }
  257. opj_stream_set_write_function(stream, j2k_write);
  258. opj_stream_set_skip_function(stream, j2k_skip);
  259. opj_stream_set_seek_function(stream, j2k_seek);
  260. /* OpenJPEG 2.0 doesn't have OPJ_VERSION_MAJOR */
  261. #ifndef OPJ_VERSION_MAJOR
  262. opj_stream_set_user_data(stream, state);
  263. #else
  264. opj_stream_set_user_data(stream, state, NULL);
  265. #endif
  266. /* Setup an opj_image */
  267. if (strcmp (im->mode, "L") == 0) {
  268. components = 1;
  269. color_space = OPJ_CLRSPC_GRAY;
  270. pack = j2k_pack_l;
  271. } else if (strcmp (im->mode, "I;16") == 0){
  272. components = 1;
  273. color_space = OPJ_CLRSPC_GRAY;
  274. pack = j2k_pack_i16;
  275. prec = 16;
  276. bpp = 12;
  277. } else if (strcmp (im->mode, "I;16B") == 0){
  278. components = 1;
  279. color_space = OPJ_CLRSPC_GRAY;
  280. pack = j2k_pack_i16;
  281. prec = 16;
  282. bpp = 12;
  283. } else if (strcmp (im->mode, "LA") == 0) {
  284. components = 2;
  285. color_space = OPJ_CLRSPC_GRAY;
  286. pack = j2k_pack_la;
  287. } else if (strcmp (im->mode, "RGB") == 0) {
  288. components = 3;
  289. color_space = OPJ_CLRSPC_SRGB;
  290. pack = j2k_pack_rgb;
  291. } else if (strcmp (im->mode, "YCbCr") == 0) {
  292. components = 3;
  293. color_space = OPJ_CLRSPC_SYCC;
  294. pack = j2k_pack_rgb;
  295. } else if (strcmp (im->mode, "RGBA") == 0) {
  296. components = 4;
  297. color_space = OPJ_CLRSPC_SRGB;
  298. pack = j2k_pack_rgba;
  299. } else {
  300. state->errcode = IMAGING_CODEC_BROKEN;
  301. state->state = J2K_STATE_FAILED;
  302. goto quick_exit;
  303. }
  304. for (n = 0; n < components; ++n) {
  305. image_params[n].dx = image_params[n].dy = 1;
  306. image_params[n].w = im->xsize;
  307. image_params[n].h = im->ysize;
  308. image_params[n].x0 = image_params[n].y0 = 0;
  309. image_params[n].prec = prec;
  310. image_params[n].bpp = bpp;
  311. image_params[n].sgnd = 0;
  312. }
  313. image = opj_image_create(components, image_params, color_space);
  314. if (!image) {
  315. state->errcode = IMAGING_CODEC_BROKEN;
  316. state->state = J2K_STATE_FAILED;
  317. goto quick_exit;
  318. }
  319. /* Setup compression context */
  320. context->error_msg = NULL;
  321. opj_set_default_encoder_parameters(&params);
  322. params.image_offset_x0 = context->offset_x;
  323. params.image_offset_y0 = context->offset_y;
  324. if (context->tile_size_x && context->tile_size_y) {
  325. params.tile_size_on = OPJ_TRUE;
  326. params.cp_tx0 = context->tile_offset_x;
  327. params.cp_ty0 = context->tile_offset_y;
  328. params.cp_tdx = context->tile_size_x;
  329. params.cp_tdy = context->tile_size_y;
  330. tile_width = params.cp_tdx;
  331. tile_height = params.cp_tdy;
  332. } else {
  333. params.cp_tx0 = 0;
  334. params.cp_ty0 = 0;
  335. params.cp_tdx = 1;
  336. params.cp_tdy = 1;
  337. tile_width = im->xsize;
  338. tile_height = im->ysize;
  339. }
  340. if (context->quality_layers && PySequence_Check(context->quality_layers)) {
  341. Py_ssize_t len = PySequence_Length(context->quality_layers);
  342. Py_ssize_t n;
  343. float *pq;
  344. if (len) {
  345. if (len > sizeof(params.tcp_rates) / sizeof(params.tcp_rates[0]))
  346. len = sizeof(params.tcp_rates)/sizeof(params.tcp_rates[0]);
  347. params.tcp_numlayers = (int)len;
  348. if (context->quality_is_in_db) {
  349. params.cp_disto_alloc = params.cp_fixed_alloc = 0;
  350. params.cp_fixed_quality = 1;
  351. pq = params.tcp_distoratio;
  352. } else {
  353. params.cp_disto_alloc = 1;
  354. params.cp_fixed_alloc = params.cp_fixed_quality = 0;
  355. pq = params.tcp_rates;
  356. }
  357. for (n = 0; n < len; ++n) {
  358. PyObject *obj = PySequence_ITEM(context->quality_layers, n);
  359. pq[n] = PyFloat_AsDouble(obj);
  360. }
  361. }
  362. } else {
  363. params.tcp_numlayers = 1;
  364. params.tcp_rates[0] = 0;
  365. params.cp_disto_alloc = 1;
  366. }
  367. if (context->num_resolutions)
  368. params.numresolution = context->num_resolutions;
  369. if (context->cblk_width >= 4 && context->cblk_width <= 1024
  370. && context->cblk_height >= 4 && context->cblk_height <= 1024
  371. && context->cblk_width * context->cblk_height <= 4096) {
  372. params.cblockw_init = context->cblk_width;
  373. params.cblockh_init = context->cblk_height;
  374. }
  375. if (context->precinct_width >= 4 && context->precinct_height >= 4
  376. && context->precinct_width >= context->cblk_width
  377. && context->precinct_height > context->cblk_height) {
  378. params.prcw_init[0] = context->precinct_width;
  379. params.prch_init[0] = context->precinct_height;
  380. params.res_spec = 1;
  381. params.csty |= 0x01;
  382. }
  383. params.irreversible = context->irreversible;
  384. params.prog_order = context->progression;
  385. params.cp_cinema = context->cinema_mode;
  386. switch (params.cp_cinema) {
  387. case OPJ_OFF:
  388. params.cp_rsiz = OPJ_STD_RSIZ;
  389. break;
  390. case OPJ_CINEMA2K_24:
  391. case OPJ_CINEMA2K_48:
  392. params.cp_rsiz = OPJ_CINEMA2K;
  393. if (params.numresolution > 6)
  394. params.numresolution = 6;
  395. break;
  396. case OPJ_CINEMA4K_24:
  397. params.cp_rsiz = OPJ_CINEMA4K;
  398. if (params.numresolution > 7)
  399. params.numresolution = 7;
  400. break;
  401. }
  402. if (context->cinema_mode != OPJ_OFF)
  403. j2k_set_cinema_params(im, components, &params);
  404. /* Set up the reference grid in the image */
  405. image->x0 = params.image_offset_x0;
  406. image->y0 = params.image_offset_y0;
  407. image->x1 = xsiz = im->xsize + params.image_offset_x0;
  408. image->y1 = ysiz = im->ysize + params.image_offset_y0;
  409. /* Create the compressor */
  410. codec = opj_create_compress(context->format);
  411. if (!codec) {
  412. state->errcode = IMAGING_CODEC_BROKEN;
  413. state->state = J2K_STATE_FAILED;
  414. goto quick_exit;
  415. }
  416. opj_set_error_handler(codec, j2k_error, context);
  417. opj_set_info_handler(codec, j2k_warn, context);
  418. opj_set_warning_handler(codec, j2k_warn, context);
  419. opj_setup_encoder(codec, &params, image);
  420. /* Start encoding */
  421. if (!opj_start_compress(codec, image, stream)) {
  422. state->errcode = IMAGING_CODEC_BROKEN;
  423. state->state = J2K_STATE_FAILED;
  424. goto quick_exit;
  425. }
  426. /* Write each tile */
  427. tiles_x = (im->xsize + (params.image_offset_x0 - params.cp_tx0)
  428. + tile_width - 1) / tile_width;
  429. tiles_y = (im->ysize + (params.image_offset_y0 - params.cp_ty0)
  430. + tile_height - 1) / tile_height;
  431. /* check for integer overflow for the malloc line, checking any expression
  432. that may multiply either tile_width or tile_height */
  433. _overflow_scale_factor = components * prec;
  434. if (( tile_width > UINT_MAX / _overflow_scale_factor ) ||
  435. ( tile_height > UINT_MAX / _overflow_scale_factor ) ||
  436. ( tile_width > UINT_MAX / (tile_height * _overflow_scale_factor )) ||
  437. ( tile_height > UINT_MAX / (tile_width * _overflow_scale_factor ))) {
  438. state->errcode = IMAGING_CODEC_BROKEN;
  439. state->state = J2K_STATE_FAILED;
  440. goto quick_exit;
  441. }
  442. /* malloc check ok, checked for overflow above */
  443. state->buffer = malloc (tile_width * tile_height * components * prec / 8);
  444. if (!state->buffer) {
  445. state->errcode = IMAGING_CODEC_BROKEN;
  446. state->state = J2K_STATE_FAILED;
  447. goto quick_exit;
  448. }
  449. tile_ndx = 0;
  450. for (y = 0; y < tiles_y; ++y) {
  451. int ty0 = params.cp_ty0 + y * tile_height;
  452. unsigned ty1 = ty0 + tile_height;
  453. unsigned pixy, pixh;
  454. if (ty0 < params.image_offset_y0)
  455. ty0 = params.image_offset_y0;
  456. if (ty1 > ysiz)
  457. ty1 = ysiz;
  458. pixy = ty0 - params.image_offset_y0;
  459. pixh = ty1 - ty0;
  460. for (x = 0; x < tiles_x; ++x) {
  461. int tx0 = params.cp_tx0 + x * tile_width;
  462. unsigned tx1 = tx0 + tile_width;
  463. unsigned pixx, pixw;
  464. unsigned data_size;
  465. if (tx0 < params.image_offset_x0)
  466. tx0 = params.image_offset_x0;
  467. if (tx1 > xsiz)
  468. tx1 = xsiz;
  469. pixx = tx0 - params.image_offset_x0;
  470. pixw = tx1 - tx0;
  471. pack(im, state->buffer, pixx, pixy, pixw, pixh);
  472. data_size = pixw * pixh * components * prec / 8;
  473. if (!opj_write_tile(codec, tile_ndx++, state->buffer,
  474. data_size, stream)) {
  475. state->errcode = IMAGING_CODEC_BROKEN;
  476. state->state = J2K_STATE_FAILED;
  477. goto quick_exit;
  478. }
  479. }
  480. }
  481. if (!opj_end_compress(codec, stream)) {
  482. state->errcode = IMAGING_CODEC_BROKEN;
  483. state->state = J2K_STATE_FAILED;
  484. goto quick_exit;
  485. }
  486. state->errcode = IMAGING_CODEC_END;
  487. state->state = J2K_STATE_DONE;
  488. ret = -1;
  489. quick_exit:
  490. if (codec)
  491. opj_destroy_codec(codec);
  492. if (image)
  493. opj_image_destroy(image);
  494. if (stream)
  495. opj_stream_destroy(stream);
  496. return ret;
  497. }
  498. int
  499. ImagingJpeg2KEncode(Imaging im, ImagingCodecState state, UINT8 *buf, int bytes)
  500. {
  501. if (state->state == J2K_STATE_FAILED)
  502. return -1;
  503. if (state->state == J2K_STATE_START) {
  504. state->state = J2K_STATE_ENCODING;
  505. return j2k_encode_entry(im, state);
  506. }
  507. return -1;
  508. }
  509. /* -------------------------------------------------------------------- */
  510. /* Cleanup */
  511. /* -------------------------------------------------------------------- */
  512. int
  513. ImagingJpeg2KEncodeCleanup(ImagingCodecState state) {
  514. JPEG2KENCODESTATE *context = (JPEG2KENCODESTATE *)state->context;
  515. if (context->quality_layers) {
  516. Py_XDECREF(context->quality_layers);
  517. context->quality_layers = NULL;
  518. }
  519. if (context->error_msg)
  520. free ((void *)context->error_msg);
  521. context->error_msg = NULL;
  522. return -1;
  523. }
  524. #endif /* HAVE_OPENJPEG */
  525. /*
  526. * Local Variables:
  527. * c-basic-offset: 4
  528. * End:
  529. *
  530. */