Jpeg2KDecode.c 29 KB

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