Jpeg2KDecode.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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 <stdlib.h>
  18. #include "Jpeg2K.h"
  19. typedef struct {
  20. OPJ_UINT32 tile_index;
  21. OPJ_UINT32 data_size;
  22. OPJ_INT32 x0, y0, x1, y1;
  23. OPJ_UINT32 nb_comps;
  24. } JPEG2KTILEINFO;
  25. /* -------------------------------------------------------------------- */
  26. /* Error handler */
  27. /* -------------------------------------------------------------------- */
  28. static void
  29. j2k_error(const char *msg, void *client_data)
  30. {
  31. JPEG2KDECODESTATE *state = (JPEG2KDECODESTATE *) client_data;
  32. free((void *)state->error_msg);
  33. state->error_msg = strdup(msg);
  34. }
  35. /* -------------------------------------------------------------------- */
  36. /* Buffer input stream */
  37. /* -------------------------------------------------------------------- */
  38. static OPJ_SIZE_T
  39. j2k_read(void *p_buffer, OPJ_SIZE_T p_nb_bytes, void *p_user_data)
  40. {
  41. ImagingCodecState state = (ImagingCodecState)p_user_data;
  42. size_t len = _imaging_read_pyFd(state->fd, p_buffer, p_nb_bytes);
  43. return len ? len : (OPJ_SIZE_T)-1;
  44. }
  45. static OPJ_OFF_T
  46. j2k_skip(OPJ_OFF_T p_nb_bytes, void *p_user_data)
  47. {
  48. off_t pos;
  49. ImagingCodecState state = (ImagingCodecState)p_user_data;
  50. _imaging_seek_pyFd(state->fd, p_nb_bytes, SEEK_CUR);
  51. pos = _imaging_tell_pyFd(state->fd);
  52. return pos ? pos : (OPJ_OFF_T)-1;
  53. }
  54. /* -------------------------------------------------------------------- */
  55. /* Unpackers */
  56. /* -------------------------------------------------------------------- */
  57. typedef void (*j2k_unpacker_t)(opj_image_t *in,
  58. const JPEG2KTILEINFO *tileInfo,
  59. const UINT8 *data,
  60. Imaging im);
  61. struct j2k_decode_unpacker {
  62. const char *mode;
  63. OPJ_COLOR_SPACE color_space;
  64. unsigned components;
  65. j2k_unpacker_t unpacker;
  66. };
  67. static inline
  68. unsigned j2ku_shift(unsigned x, int n)
  69. {
  70. if (n < 0)
  71. return x >> -n;
  72. else
  73. return x << n;
  74. }
  75. static void
  76. j2ku_gray_l(opj_image_t *in, const JPEG2KTILEINFO *tileinfo,
  77. const UINT8 *tiledata, Imaging im)
  78. {
  79. unsigned x0 = tileinfo->x0 - in->x0, y0 = tileinfo->y0 - in->y0;
  80. unsigned w = tileinfo->x1 - tileinfo->x0;
  81. unsigned h = tileinfo->y1 - tileinfo->y0;
  82. int shift = 8 - in->comps[0].prec;
  83. int offset = in->comps[0].sgnd ? 1 << (in->comps[0].prec - 1) : 0;
  84. int csiz = (in->comps[0].prec + 7) >> 3;
  85. unsigned x, y;
  86. if (csiz == 3)
  87. csiz = 4;
  88. if (shift < 0)
  89. offset += 1 << (-shift - 1);
  90. switch (csiz) {
  91. case 1:
  92. for (y = 0; y < h; ++y) {
  93. const UINT8 *data = &tiledata[y * w];
  94. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0;
  95. for (x = 0; x < w; ++x)
  96. *row++ = j2ku_shift(offset + *data++, shift);
  97. }
  98. break;
  99. case 2:
  100. for (y = 0; y < h; ++y) {
  101. const UINT16 *data = (const UINT16 *)&tiledata[2 * y * w];
  102. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0;
  103. for (x = 0; x < w; ++x)
  104. *row++ = j2ku_shift(offset + *data++, shift);
  105. }
  106. break;
  107. case 4:
  108. for (y = 0; y < h; ++y) {
  109. const UINT32 *data = (const UINT32 *)&tiledata[4 * y * w];
  110. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0;
  111. for (x = 0; x < w; ++x)
  112. *row++ = j2ku_shift(offset + *data++, shift);
  113. }
  114. break;
  115. }
  116. }
  117. static void
  118. j2ku_gray_i(opj_image_t *in, const JPEG2KTILEINFO *tileinfo,
  119. const UINT8 *tiledata, Imaging im)
  120. {
  121. unsigned x0 = tileinfo->x0 - in->x0, y0 = tileinfo->y0 - in->y0;
  122. unsigned w = tileinfo->x1 - tileinfo->x0;
  123. unsigned h = tileinfo->y1 - tileinfo->y0;
  124. int shift = 16 - in->comps[0].prec;
  125. int offset = in->comps[0].sgnd ? 1 << (in->comps[0].prec - 1) : 0;
  126. int csiz = (in->comps[0].prec + 7) >> 3;
  127. unsigned x, y;
  128. if (csiz == 3)
  129. csiz = 4;
  130. if (shift < 0)
  131. offset += 1 << (-shift - 1);
  132. switch (csiz) {
  133. case 1:
  134. for (y = 0; y < h; ++y) {
  135. const UINT8 *data = &tiledata[y * w];
  136. UINT16 *row = (UINT16 *)im->image[y0 + y] + x0;
  137. for (x = 0; x < w; ++x)
  138. *row++ = j2ku_shift(offset + *data++, shift);
  139. }
  140. break;
  141. case 2:
  142. for (y = 0; y < h; ++y) {
  143. const UINT16 *data = (const UINT16 *)&tiledata[2 * y * w];
  144. UINT16 *row = (UINT16 *)im->image[y0 + y] + x0;
  145. for (x = 0; x < w; ++x)
  146. *row++ = j2ku_shift(offset + *data++, shift);
  147. }
  148. break;
  149. case 4:
  150. for (y = 0; y < h; ++y) {
  151. const UINT32 *data = (const UINT32 *)&tiledata[4 * y * w];
  152. UINT16 *row = (UINT16 *)im->image[y0 + y] + x0;
  153. for (x = 0; x < w; ++x)
  154. *row++ = j2ku_shift(offset + *data++, shift);
  155. }
  156. break;
  157. }
  158. }
  159. static void
  160. j2ku_gray_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo,
  161. const UINT8 *tiledata, Imaging im)
  162. {
  163. unsigned x0 = tileinfo->x0 - in->x0, y0 = tileinfo->y0 - in->y0;
  164. unsigned w = tileinfo->x1 - tileinfo->x0;
  165. unsigned h = tileinfo->y1 - tileinfo->y0;
  166. int shift = 8 - in->comps[0].prec;
  167. int offset = in->comps[0].sgnd ? 1 << (in->comps[0].prec - 1) : 0;
  168. int csiz = (in->comps[0].prec + 7) >> 3;
  169. unsigned x, y;
  170. if (shift < 0)
  171. offset += 1 << (-shift - 1);
  172. if (csiz == 3)
  173. csiz = 4;
  174. switch (csiz) {
  175. case 1:
  176. for (y = 0; y < h; ++y) {
  177. const UINT8 *data = &tiledata[y * w];
  178. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0;
  179. for (x = 0; x < w; ++x) {
  180. UINT8 byte = j2ku_shift(offset + *data++, shift);
  181. row[0] = row[1] = row[2] = byte;
  182. row[3] = 0xff;
  183. row += 4;
  184. }
  185. }
  186. break;
  187. case 2:
  188. for (y = 0; y < h; ++y) {
  189. const UINT16 *data = (UINT16 *)&tiledata[2 * y * w];
  190. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0;
  191. for (x = 0; x < w; ++x) {
  192. UINT8 byte = j2ku_shift(offset + *data++, shift);
  193. row[0] = row[1] = row[2] = byte;
  194. row[3] = 0xff;
  195. row += 4;
  196. }
  197. }
  198. break;
  199. case 4:
  200. for (y = 0; y < h; ++y) {
  201. const UINT32 *data = (UINT32 *)&tiledata[4 * y * w];
  202. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0;
  203. for (x = 0; x < w; ++x) {
  204. UINT8 byte = j2ku_shift(offset + *data++, shift);
  205. row[0] = row[1] = row[2] = byte;
  206. row[3] = 0xff;
  207. row += 4;
  208. }
  209. }
  210. break;
  211. }
  212. }
  213. static void
  214. j2ku_graya_la(opj_image_t *in, const JPEG2KTILEINFO *tileinfo,
  215. const UINT8 *tiledata, Imaging im)
  216. {
  217. unsigned x0 = tileinfo->x0 - in->x0, y0 = tileinfo->y0 - in->y0;
  218. unsigned w = tileinfo->x1 - tileinfo->x0;
  219. unsigned h = tileinfo->y1 - tileinfo->y0;
  220. int shift = 8 - in->comps[0].prec;
  221. int offset = in->comps[0].sgnd ? 1 << (in->comps[0].prec - 1) : 0;
  222. int csiz = (in->comps[0].prec + 7) >> 3;
  223. int ashift = 8 - in->comps[1].prec;
  224. int aoffset = in->comps[1].sgnd ? 1 << (in->comps[1].prec - 1) : 0;
  225. int acsiz = (in->comps[1].prec + 7) >> 3;
  226. const UINT8 *atiledata;
  227. unsigned x, y;
  228. if (csiz == 3)
  229. csiz = 4;
  230. if (acsiz == 3)
  231. acsiz = 4;
  232. if (shift < 0)
  233. offset += 1 << (-shift - 1);
  234. if (ashift < 0)
  235. aoffset += 1 << (-ashift - 1);
  236. atiledata = tiledata + csiz * w * h;
  237. for (y = 0; y < h; ++y) {
  238. const UINT8 *data = &tiledata[csiz * y * w];
  239. const UINT8 *adata = &atiledata[acsiz * y * w];
  240. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4;
  241. for (x = 0; x < w; ++x) {
  242. UINT32 word = 0, aword = 0, byte;
  243. switch (csiz) {
  244. case 1: word = *data++; break;
  245. case 2: word = *(const UINT16 *)data; data += 2; break;
  246. case 4: word = *(const UINT32 *)data; data += 4; break;
  247. }
  248. switch (acsiz) {
  249. case 1: aword = *adata++; break;
  250. case 2: aword = *(const UINT16 *)adata; adata += 2; break;
  251. case 4: aword = *(const UINT32 *)adata; adata += 4; break;
  252. }
  253. byte = j2ku_shift(offset + word, shift);
  254. row[0] = row[1] = row[2] = byte;
  255. row[3] = j2ku_shift(aoffset + aword, ashift);
  256. row += 4;
  257. }
  258. }
  259. }
  260. static void
  261. j2ku_srgb_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo,
  262. const UINT8 *tiledata, Imaging im)
  263. {
  264. unsigned x0 = tileinfo->x0 - in->x0, y0 = tileinfo->y0 - in->y0;
  265. unsigned w = tileinfo->x1 - tileinfo->x0;
  266. unsigned h = tileinfo->y1 - tileinfo->y0;
  267. int shifts[3], offsets[3], csiz[3];
  268. const UINT8 *cdata[3];
  269. const UINT8 *cptr = tiledata;
  270. unsigned n, x, y;
  271. for (n = 0; n < 3; ++n) {
  272. cdata[n] = cptr;
  273. shifts[n] = 8 - in->comps[n].prec;
  274. offsets[n] = in->comps[n].sgnd ? 1 << (in->comps[n].prec - 1) : 0;
  275. csiz[n] = (in->comps[n].prec + 7) >> 3;
  276. if (csiz[n] == 3)
  277. csiz[n] = 4;
  278. if (shifts[n] < 0)
  279. offsets[n] += 1 << (-shifts[n] - 1);
  280. cptr += csiz[n] * w * h;
  281. }
  282. for (y = 0; y < h; ++y) {
  283. const UINT8 *data[3];
  284. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4;
  285. for (n = 0; n < 3; ++n)
  286. data[n] = &cdata[n][csiz[n] * y * w];
  287. for (x = 0; x < w; ++x) {
  288. for (n = 0; n < 3; ++n) {
  289. UINT32 word = 0;
  290. switch (csiz[n]) {
  291. case 1: word = *data[n]++; break;
  292. case 2: word = *(const UINT16 *)data[n]; data[n] += 2; break;
  293. case 4: word = *(const UINT32 *)data[n]; data[n] += 4; break;
  294. }
  295. row[n] = j2ku_shift(offsets[n] + word, shifts[n]);
  296. }
  297. row[3] = 0xff;
  298. row += 4;
  299. }
  300. }
  301. }
  302. static void
  303. j2ku_sycc_rgb(opj_image_t *in, const JPEG2KTILEINFO *tileinfo,
  304. const UINT8 *tiledata, Imaging im)
  305. {
  306. unsigned x0 = tileinfo->x0 - in->x0, y0 = tileinfo->y0 - in->y0;
  307. unsigned w = tileinfo->x1 - tileinfo->x0;
  308. unsigned h = tileinfo->y1 - tileinfo->y0;
  309. int shifts[3], offsets[3], csiz[3];
  310. const UINT8 *cdata[3];
  311. const UINT8 *cptr = tiledata;
  312. unsigned n, x, y;
  313. for (n = 0; n < 3; ++n) {
  314. cdata[n] = cptr;
  315. shifts[n] = 8 - in->comps[n].prec;
  316. offsets[n] = in->comps[n].sgnd ? 1 << (in->comps[n].prec - 1) : 0;
  317. csiz[n] = (in->comps[n].prec + 7) >> 3;
  318. if (csiz[n] == 3)
  319. csiz[n] = 4;
  320. if (shifts[n] < 0)
  321. offsets[n] += 1 << (-shifts[n] - 1);
  322. cptr += csiz[n] * w * h;
  323. }
  324. for (y = 0; y < h; ++y) {
  325. const UINT8 *data[3];
  326. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4;
  327. UINT8 *row_start = row;
  328. for (n = 0; n < 3; ++n)
  329. data[n] = &cdata[n][csiz[n] * y * w];
  330. for (x = 0; x < w; ++x) {
  331. for (n = 0; n < 3; ++n) {
  332. UINT32 word = 0;
  333. switch (csiz[n]) {
  334. case 1: word = *data[n]++; break;
  335. case 2: word = *(const UINT16 *)data[n]; data[n] += 2; break;
  336. case 4: word = *(const UINT32 *)data[n]; data[n] += 4; break;
  337. }
  338. row[n] = j2ku_shift(offsets[n] + word, shifts[n]);
  339. }
  340. row[3] = 0xff;
  341. row += 4;
  342. }
  343. ImagingConvertYCbCr2RGB(row_start, row_start, w);
  344. }
  345. }
  346. static void
  347. j2ku_srgba_rgba(opj_image_t *in, const JPEG2KTILEINFO *tileinfo,
  348. const UINT8 *tiledata, Imaging im)
  349. {
  350. unsigned x0 = tileinfo->x0 - in->x0, y0 = tileinfo->y0 - in->y0;
  351. unsigned w = tileinfo->x1 - tileinfo->x0;
  352. unsigned h = tileinfo->y1 - tileinfo->y0;
  353. int shifts[4], offsets[4], csiz[4];
  354. const UINT8 *cdata[4];
  355. const UINT8 *cptr = tiledata;
  356. unsigned n, x, y;
  357. for (n = 0; n < 4; ++n) {
  358. cdata[n] = cptr;
  359. shifts[n] = 8 - in->comps[n].prec;
  360. offsets[n] = in->comps[n].sgnd ? 1 << (in->comps[n].prec - 1) : 0;
  361. csiz[n] = (in->comps[n].prec + 7) >> 3;
  362. if (csiz[n] == 3)
  363. csiz[n] = 4;
  364. if (shifts[n] < 0)
  365. offsets[n] += 1 << (-shifts[n] - 1);
  366. cptr += csiz[n] * w * h;
  367. }
  368. for (y = 0; y < h; ++y) {
  369. const UINT8 *data[4];
  370. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4;
  371. for (n = 0; n < 4; ++n)
  372. data[n] = &cdata[n][csiz[n] * y * w];
  373. for (x = 0; x < w; ++x) {
  374. for (n = 0; n < 4; ++n) {
  375. UINT32 word = 0;
  376. switch (csiz[n]) {
  377. case 1: word = *data[n]++; break;
  378. case 2: word = *(const UINT16 *)data[n]; data[n] += 2; break;
  379. case 4: word = *(const UINT32 *)data[n]; data[n] += 4; break;
  380. }
  381. row[n] = j2ku_shift(offsets[n] + word, shifts[n]);
  382. }
  383. row += 4;
  384. }
  385. }
  386. }
  387. static void
  388. j2ku_sycca_rgba(opj_image_t *in, const JPEG2KTILEINFO *tileinfo,
  389. const UINT8 *tiledata, Imaging im)
  390. {
  391. unsigned x0 = tileinfo->x0 - in->x0, y0 = tileinfo->y0 - in->y0;
  392. unsigned w = tileinfo->x1 - tileinfo->x0;
  393. unsigned h = tileinfo->y1 - tileinfo->y0;
  394. int shifts[4], offsets[4], csiz[4];
  395. const UINT8 *cdata[4];
  396. const UINT8 *cptr = tiledata;
  397. unsigned n, x, y;
  398. for (n = 0; n < 4; ++n) {
  399. cdata[n] = cptr;
  400. shifts[n] = 8 - in->comps[n].prec;
  401. offsets[n] = in->comps[n].sgnd ? 1 << (in->comps[n].prec - 1) : 0;
  402. csiz[n] = (in->comps[n].prec + 7) >> 3;
  403. if (csiz[n] == 3)
  404. csiz[n] = 4;
  405. if (shifts[n] < 0)
  406. offsets[n] += 1 << (-shifts[n] - 1);
  407. cptr += csiz[n] * w * h;
  408. }
  409. for (y = 0; y < h; ++y) {
  410. const UINT8 *data[4];
  411. UINT8 *row = (UINT8 *)im->image[y0 + y] + x0 * 4;
  412. UINT8 *row_start = row;
  413. for (n = 0; n < 4; ++n)
  414. data[n] = &cdata[n][csiz[n] * y * w];
  415. for (x = 0; x < w; ++x) {
  416. for (n = 0; n < 4; ++n) {
  417. UINT32 word = 0;
  418. switch (csiz[n]) {
  419. case 1: word = *data[n]++; break;
  420. case 2: word = *(const UINT16 *)data[n]; data[n] += 2; break;
  421. case 4: word = *(const UINT32 *)data[n]; data[n] += 4; break;
  422. }
  423. row[n] = j2ku_shift(offsets[n] + word, shifts[n]);
  424. }
  425. row += 4;
  426. }
  427. ImagingConvertYCbCr2RGB(row_start, row_start, w);
  428. }
  429. }
  430. static const struct j2k_decode_unpacker j2k_unpackers[] = {
  431. { "L", OPJ_CLRSPC_GRAY, 1, j2ku_gray_l },
  432. { "I;16", OPJ_CLRSPC_GRAY, 1, j2ku_gray_i },
  433. { "I;16B", OPJ_CLRSPC_GRAY, 1, j2ku_gray_i },
  434. { "LA", OPJ_CLRSPC_GRAY, 2, j2ku_graya_la },
  435. { "RGB", OPJ_CLRSPC_GRAY, 1, j2ku_gray_rgb },
  436. { "RGB", OPJ_CLRSPC_GRAY, 2, j2ku_gray_rgb },
  437. { "RGB", OPJ_CLRSPC_SRGB, 3, j2ku_srgb_rgb },
  438. { "RGB", OPJ_CLRSPC_SYCC, 3, j2ku_sycc_rgb },
  439. { "RGB", OPJ_CLRSPC_SRGB, 4, j2ku_srgb_rgb },
  440. { "RGB", OPJ_CLRSPC_SYCC, 4, j2ku_sycc_rgb },
  441. { "RGBA", OPJ_CLRSPC_GRAY, 1, j2ku_gray_rgb },
  442. { "RGBA", OPJ_CLRSPC_GRAY, 2, j2ku_graya_la },
  443. { "RGBA", OPJ_CLRSPC_SRGB, 3, j2ku_srgb_rgb },
  444. { "RGBA", OPJ_CLRSPC_SYCC, 3, j2ku_sycc_rgb },
  445. { "RGBA", OPJ_CLRSPC_SRGB, 4, j2ku_srgba_rgba },
  446. { "RGBA", OPJ_CLRSPC_SYCC, 4, j2ku_sycca_rgba },
  447. };
  448. /* -------------------------------------------------------------------- */
  449. /* Decoder */
  450. /* -------------------------------------------------------------------- */
  451. enum {
  452. J2K_STATE_START = 0,
  453. J2K_STATE_DECODING = 1,
  454. J2K_STATE_DONE = 2,
  455. J2K_STATE_FAILED = 3,
  456. };
  457. static int
  458. j2k_decode_entry(Imaging im, ImagingCodecState state)
  459. {
  460. JPEG2KDECODESTATE *context = (JPEG2KDECODESTATE *) state->context;
  461. opj_stream_t *stream = NULL;
  462. opj_image_t *image = NULL;
  463. opj_codec_t *codec = NULL;
  464. opj_dparameters_t params;
  465. OPJ_COLOR_SPACE color_space;
  466. j2k_unpacker_t unpack = NULL;
  467. size_t buffer_size = 0;
  468. unsigned n;
  469. stream = opj_stream_create(BUFFER_SIZE, OPJ_TRUE);
  470. if (!stream) {
  471. state->errcode = IMAGING_CODEC_BROKEN;
  472. state->state = J2K_STATE_FAILED;
  473. goto quick_exit;
  474. }
  475. opj_stream_set_read_function(stream, j2k_read);
  476. opj_stream_set_skip_function(stream, j2k_skip);
  477. /* OpenJPEG 2.0 doesn't have OPJ_VERSION_MAJOR */
  478. #ifndef OPJ_VERSION_MAJOR
  479. opj_stream_set_user_data(stream, state);
  480. #else
  481. opj_stream_set_user_data(stream, state, NULL);
  482. /* Hack: if we don't know the length, the largest file we can
  483. possibly support is 4GB. We can't go larger than this, because
  484. OpenJPEG truncates this value for the final box in the file, and
  485. the box lengths in OpenJPEG are currently 32 bit. */
  486. if (context->length < 0)
  487. opj_stream_set_user_data_length(stream, 0xffffffff);
  488. else
  489. opj_stream_set_user_data_length(stream, context->length);
  490. #endif
  491. /* Setup decompression context */
  492. context->error_msg = NULL;
  493. opj_set_default_decoder_parameters(&params);
  494. params.cp_reduce = context->reduce;
  495. params.cp_layer = context->layers;
  496. codec = opj_create_decompress(context->format);
  497. if (!codec) {
  498. state->errcode = IMAGING_CODEC_BROKEN;
  499. state->state = J2K_STATE_FAILED;
  500. goto quick_exit;
  501. }
  502. opj_set_error_handler(codec, j2k_error, context);
  503. opj_setup_decoder(codec, &params);
  504. if (!opj_read_header(stream, codec, &image)) {
  505. state->errcode = IMAGING_CODEC_BROKEN;
  506. state->state = J2K_STATE_FAILED;
  507. goto quick_exit;
  508. }
  509. /* Check that this image is something we can handle */
  510. if (image->numcomps < 1 || image->numcomps > 4
  511. || image->color_space == OPJ_CLRSPC_UNKNOWN) {
  512. state->errcode = IMAGING_CODEC_BROKEN;
  513. state->state = J2K_STATE_FAILED;
  514. goto quick_exit;
  515. }
  516. for (n = 1; n < image->numcomps; ++n) {
  517. if (image->comps[n].dx != 1 || image->comps[n].dy != 1) {
  518. state->errcode = IMAGING_CODEC_BROKEN;
  519. state->state = J2K_STATE_FAILED;
  520. goto quick_exit;
  521. }
  522. }
  523. /*
  524. Colorspace Number of components PIL mode
  525. ------------------------------------------------------
  526. sRGB 3 RGB
  527. sRGB 4 RGBA
  528. gray 1 L or I
  529. gray 2 LA
  530. YCC 3 YCbCr
  531. If colorspace is unspecified, we assume:
  532. Number of components Colorspace
  533. -----------------------------------------
  534. 1 gray
  535. 2 gray (+ alpha)
  536. 3 sRGB
  537. 4 sRGB (+ alpha)
  538. */
  539. /* Find the correct unpacker */
  540. color_space = image->color_space;
  541. if (color_space == OPJ_CLRSPC_UNSPECIFIED) {
  542. switch (image->numcomps) {
  543. case 1: case 2: color_space = OPJ_CLRSPC_GRAY; break;
  544. case 3: case 4: color_space = OPJ_CLRSPC_SRGB; break;
  545. }
  546. }
  547. for (n = 0; n < sizeof(j2k_unpackers) / sizeof (j2k_unpackers[0]); ++n) {
  548. if (color_space == j2k_unpackers[n].color_space
  549. && image->numcomps == j2k_unpackers[n].components
  550. && strcmp (im->mode, j2k_unpackers[n].mode) == 0) {
  551. unpack = j2k_unpackers[n].unpacker;
  552. break;
  553. }
  554. }
  555. if (!unpack) {
  556. state->errcode = IMAGING_CODEC_BROKEN;
  557. state->state = J2K_STATE_FAILED;
  558. goto quick_exit;
  559. }
  560. /* Decode the image tile-by-tile; this means we only need use as much
  561. memory as is required for one tile's worth of components. */
  562. for (;;) {
  563. JPEG2KTILEINFO tile_info;
  564. OPJ_BOOL should_continue;
  565. unsigned correction = (1 << params.cp_reduce) - 1;
  566. if (!opj_read_tile_header(codec,
  567. stream,
  568. &tile_info.tile_index,
  569. &tile_info.data_size,
  570. &tile_info.x0, &tile_info.y0,
  571. &tile_info.x1, &tile_info.y1,
  572. &tile_info.nb_comps,
  573. &should_continue)) {
  574. state->errcode = IMAGING_CODEC_BROKEN;
  575. state->state = J2K_STATE_FAILED;
  576. goto quick_exit;
  577. }
  578. if (!should_continue)
  579. break;
  580. /* Adjust the tile co-ordinates based on the reduction (OpenJPEG
  581. doesn't do this for us) */
  582. tile_info.x0 = (tile_info.x0 + correction) >> context->reduce;
  583. tile_info.y0 = (tile_info.y0 + correction) >> context->reduce;
  584. tile_info.x1 = (tile_info.x1 + correction) >> context->reduce;
  585. tile_info.y1 = (tile_info.y1 + correction) >> context->reduce;
  586. if (buffer_size < tile_info.data_size) {
  587. /* malloc check ok, tile_info.data_size from openjpeg */
  588. UINT8 *new = realloc (state->buffer, tile_info.data_size);
  589. if (!new) {
  590. state->errcode = IMAGING_CODEC_MEMORY;
  591. state->state = J2K_STATE_FAILED;
  592. goto quick_exit;
  593. }
  594. state->buffer = new;
  595. buffer_size = tile_info.data_size;
  596. }
  597. if (!opj_decode_tile_data(codec,
  598. tile_info.tile_index,
  599. (OPJ_BYTE *)state->buffer,
  600. tile_info.data_size,
  601. stream)) {
  602. state->errcode = IMAGING_CODEC_BROKEN;
  603. state->state = J2K_STATE_FAILED;
  604. goto quick_exit;
  605. }
  606. /* Check the tile bounds; if the tile is outside the image area,
  607. or if it has a negative width or height (i.e. the coordinates are
  608. swapped), bail. */
  609. if (tile_info.x0 >= tile_info.x1
  610. || tile_info.y0 >= tile_info.y1
  611. || tile_info.x0 < image->x0
  612. || tile_info.y0 < image->y0
  613. || tile_info.x1 - image->x0 > im->xsize
  614. || tile_info.y1 - image->y0 > im->ysize) {
  615. state->errcode = IMAGING_CODEC_BROKEN;
  616. state->state = J2K_STATE_FAILED;
  617. goto quick_exit;
  618. }
  619. unpack(image, &tile_info, state->buffer, im);
  620. }
  621. if (!opj_end_decompress(codec, stream)) {
  622. state->errcode = IMAGING_CODEC_BROKEN;
  623. state->state = J2K_STATE_FAILED;
  624. goto quick_exit;
  625. }
  626. state->state = J2K_STATE_DONE;
  627. state->errcode = IMAGING_CODEC_END;
  628. if (context->pfile) {
  629. if(fclose(context->pfile)){
  630. context->pfile = NULL;
  631. }
  632. }
  633. quick_exit:
  634. if (codec)
  635. opj_destroy_codec(codec);
  636. if (image)
  637. opj_image_destroy(image);
  638. if (stream)
  639. opj_stream_destroy(stream);
  640. return -1;
  641. }
  642. int
  643. ImagingJpeg2KDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
  644. {
  645. if (bytes){
  646. state->errcode = IMAGING_CODEC_BROKEN;
  647. state->state = J2K_STATE_FAILED;
  648. return -1;
  649. }
  650. if (state->state == J2K_STATE_DONE || state->state == J2K_STATE_FAILED)
  651. return -1;
  652. if (state->state == J2K_STATE_START) {
  653. state->state = J2K_STATE_DECODING;
  654. return j2k_decode_entry(im, state);
  655. }
  656. if (state->state == J2K_STATE_DECODING) {
  657. state->errcode = IMAGING_CODEC_BROKEN;
  658. state->state = J2K_STATE_FAILED;
  659. return -1;
  660. }
  661. return -1;
  662. }
  663. /* -------------------------------------------------------------------- */
  664. /* Cleanup */
  665. /* -------------------------------------------------------------------- */
  666. int
  667. ImagingJpeg2KDecodeCleanup(ImagingCodecState state) {
  668. JPEG2KDECODESTATE *context = (JPEG2KDECODESTATE *)state->context;
  669. if (context->error_msg) {
  670. free ((void *)context->error_msg);
  671. }
  672. context->error_msg = NULL;
  673. return -1;
  674. }
  675. const char *
  676. ImagingJpeg2KVersion(void)
  677. {
  678. return opj_version();
  679. }
  680. #endif /* HAVE_OPENJPEG */
  681. /*
  682. * Local Variables:
  683. * c-basic-offset: 4
  684. * End:
  685. *
  686. */