/src/FreeImage/Source/LibOpenJPEG/jp2.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 1210 lines · 811 code · 218 blank · 181 comment · 168 complexity · f2709592cd610c26b6f5915451bbee6e MD5 · raw file

  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * Copyright (c) 2010-2011, Kaori Hagihara
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "opj_includes.h"
  33. /** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
  34. /*@{*/
  35. /** @name Local static functions */
  36. /*@{*/
  37. /**
  38. Read box headers
  39. @param cinfo Codec context info
  40. @param cio Input stream
  41. @param box
  42. @return Returns true if successful, returns false otherwise
  43. */
  44. static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box);
  45. /*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
  46. /**
  47. Read the IHDR box - Image Header box
  48. @param jp2 JP2 handle
  49. @param cio Input buffer stream
  50. @return Returns true if successful, returns false otherwise
  51. */
  52. static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
  53. static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio);
  54. static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
  55. static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio);
  56. static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio);
  57. /**
  58. Write the FTYP box - File type box
  59. @param jp2 JP2 handle
  60. @param cio Output buffer stream
  61. */
  62. static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
  63. /**
  64. Read the FTYP box - File type box
  65. @param jp2 JP2 handle
  66. @param cio Input buffer stream
  67. @return Returns true if successful, returns false otherwise
  68. */
  69. static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio);
  70. static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info);
  71. static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset);
  72. static void jp2_write_jp(opj_cio_t *cio);
  73. /**
  74. Read the JP box - JPEG 2000 signature
  75. @param jp2 JP2 handle
  76. @param cio Input buffer stream
  77. @return Returns true if successful, returns false otherwise
  78. */
  79. static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio);
  80. /**
  81. Decode the structure of a JP2 file
  82. @param jp2 JP2 handle
  83. @param cio Input buffer stream
  84. @param color Collector for profile, cdef and pclr data
  85. @return Returns true if successful, returns false otherwise
  86. */
  87. static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
  88. opj_jp2_color_t *color);
  89. /**
  90. Apply collected palette data
  91. @param color Collector for profile, cdef and pclr data
  92. @param image
  93. */
  94. static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image, opj_common_ptr cinfo);
  95. /**
  96. Collect palette data
  97. @param jp2 JP2 handle
  98. @param cio Input buffer stream
  99. @param box
  100. @param color Collector for profile, cdef and pclr data
  101. @return Returns true if successful, returns false otherwise
  102. */
  103. static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
  104. opj_jp2_box_t *box, opj_jp2_color_t *color);
  105. /**
  106. Collect component mapping data
  107. @param jp2 JP2 handle
  108. @param cio Input buffer stream
  109. @param box
  110. @param color Collector for profile, cdef and pclr data
  111. @return Returns true if successful, returns false otherwise
  112. */
  113. static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
  114. opj_jp2_box_t *box, opj_jp2_color_t *color);
  115. /**
  116. Collect colour specification data
  117. @param jp2 JP2 handle
  118. @param cio Input buffer stream
  119. @param box
  120. @param color Collector for profile, cdef and pclr data
  121. @return Returns true if successful, returns false otherwise
  122. */
  123. static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
  124. opj_jp2_box_t *box, opj_jp2_color_t *color);
  125. /**
  126. Write file Index (superbox)
  127. @param[in] offset_jp2c offset of jp2c box
  128. @param[in] length_jp2c length of jp2c box
  129. @param[in] offset_idx offset of cidx box
  130. @param[in] length_idx length of cidx box
  131. @param[in] cio file output handle
  132. @return length of fidx box
  133. */
  134. static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
  135. /**
  136. Write index Finder box
  137. @param[in] offset offset of fidx box
  138. @param[in] length length of fidx box
  139. @param[in] cio file output handle
  140. */
  141. static void write_iptr( int offset, int length, opj_cio_t *cio);
  142. /**
  143. Write proxy box
  144. @param[in] offset_jp2c offset of jp2c box
  145. @param[in] length_jp2c length of jp2c box
  146. @param[in] offset_idx offset of cidx box
  147. @param[in] length_idx length of cidx box
  148. @param[in] cio file output handle
  149. */
  150. static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio);
  151. /*@}*/
  152. /*@}*/
  153. /* ----------------------------------------------------------------------- */
  154. static opj_bool jp2_read_boxhdr(opj_common_ptr cinfo, opj_cio_t *cio, opj_jp2_box_t *box) {
  155. box->init_pos = cio_tell(cio);
  156. box->length = cio_read(cio, 4);
  157. box->type = cio_read(cio, 4);
  158. if (box->length == 1) {
  159. if (cio_read(cio, 4) != 0) {
  160. opj_event_msg(cinfo, EVT_ERROR, "Cannot handle box sizes higher than 2^32\n");
  161. return OPJ_FALSE;
  162. }
  163. box->length = cio_read(cio, 4);
  164. if (box->length == 0)
  165. box->length = cio_numbytesleft(cio) + 12;
  166. }
  167. else if (box->length == 0) {
  168. box->length = cio_numbytesleft(cio) + 8;
  169. }
  170. return OPJ_TRUE;
  171. }
  172. #if 0
  173. static void jp2_write_url(opj_cio_t *cio, char *Idx_file) {
  174. unsigned int i;
  175. opj_jp2_box_t box;
  176. box.init_pos = cio_tell(cio);
  177. cio_skip(cio, 4);
  178. cio_write(cio, JP2_URL, 4); /* DBTL */
  179. cio_write(cio, 0, 1); /* VERS */
  180. cio_write(cio, 0, 3); /* FLAG */
  181. if(Idx_file) {
  182. for (i = 0; i < strlen(Idx_file); i++) {
  183. cio_write(cio, Idx_file[i], 1);
  184. }
  185. }
  186. box.length = cio_tell(cio) - box.init_pos;
  187. cio_seek(cio, box.init_pos);
  188. cio_write(cio, box.length, 4); /* L */
  189. cio_seek(cio, box.init_pos + box.length);
  190. }
  191. #endif
  192. static opj_bool jp2_read_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
  193. opj_jp2_box_t box;
  194. opj_common_ptr cinfo = jp2->cinfo;
  195. jp2_read_boxhdr(cinfo, cio, &box);
  196. if (JP2_IHDR != box.type) {
  197. opj_event_msg(cinfo, EVT_ERROR, "Expected IHDR Marker\n");
  198. return OPJ_FALSE;
  199. }
  200. jp2->h = cio_read(cio, 4); /* HEIGHT */
  201. jp2->w = cio_read(cio, 4); /* WIDTH */
  202. jp2->numcomps = cio_read(cio, 2); /* NC */
  203. jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
  204. jp2->bpc = cio_read(cio, 1); /* BPC */
  205. jp2->C = cio_read(cio, 1); /* C */
  206. jp2->UnkC = cio_read(cio, 1); /* UnkC */
  207. jp2->IPR = cio_read(cio, 1); /* IPR */
  208. if (cio_tell(cio) - box.init_pos != box.length) {
  209. opj_event_msg(cinfo, EVT_ERROR, "Error with IHDR Box\n");
  210. return OPJ_FALSE;
  211. }
  212. return OPJ_TRUE;
  213. }
  214. static void jp2_write_ihdr(opj_jp2_t *jp2, opj_cio_t *cio) {
  215. opj_jp2_box_t box;
  216. box.init_pos = cio_tell(cio);
  217. cio_skip(cio, 4);
  218. cio_write(cio, JP2_IHDR, 4); /* IHDR */
  219. cio_write(cio, jp2->h, 4); /* HEIGHT */
  220. cio_write(cio, jp2->w, 4); /* WIDTH */
  221. cio_write(cio, jp2->numcomps, 2); /* NC */
  222. cio_write(cio, jp2->bpc, 1); /* BPC */
  223. cio_write(cio, jp2->C, 1); /* C : Always 7 */
  224. cio_write(cio, jp2->UnkC, 1); /* UnkC, colorspace unknown */
  225. cio_write(cio, jp2->IPR, 1); /* IPR, no intellectual property */
  226. box.length = cio_tell(cio) - box.init_pos;
  227. cio_seek(cio, box.init_pos);
  228. cio_write(cio, box.length, 4); /* L */
  229. cio_seek(cio, box.init_pos + box.length);
  230. }
  231. static void jp2_write_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
  232. unsigned int i;
  233. opj_jp2_box_t box;
  234. box.init_pos = cio_tell(cio);
  235. cio_skip(cio, 4);
  236. cio_write(cio, JP2_BPCC, 4); /* BPCC */
  237. for (i = 0; i < jp2->numcomps; i++) {
  238. cio_write(cio, jp2->comps[i].bpcc, 1);
  239. }
  240. box.length = cio_tell(cio) - box.init_pos;
  241. cio_seek(cio, box.init_pos);
  242. cio_write(cio, box.length, 4); /* L */
  243. cio_seek(cio, box.init_pos + box.length);
  244. }
  245. static opj_bool jp2_read_bpcc(opj_jp2_t *jp2, opj_cio_t *cio) {
  246. unsigned int i;
  247. opj_jp2_box_t box;
  248. opj_common_ptr cinfo = jp2->cinfo;
  249. jp2_read_boxhdr(cinfo, cio, &box);
  250. if (JP2_BPCC != box.type) {
  251. opj_event_msg(cinfo, EVT_ERROR, "Expected BPCC Marker\n");
  252. return OPJ_FALSE;
  253. }
  254. for (i = 0; i < jp2->numcomps; i++) {
  255. jp2->comps[i].bpcc = cio_read(cio, 1);
  256. }
  257. if (cio_tell(cio) - box.init_pos != box.length) {
  258. opj_event_msg(cinfo, EVT_ERROR, "Error with BPCC Box\n");
  259. return OPJ_FALSE;
  260. }
  261. return OPJ_TRUE;
  262. }
  263. static void jp2_write_colr(opj_jp2_t *jp2, opj_cio_t *cio) {
  264. opj_jp2_box_t box;
  265. box.init_pos = cio_tell(cio);
  266. cio_skip(cio, 4);
  267. cio_write(cio, JP2_COLR, 4); /* COLR */
  268. cio_write(cio, jp2->meth, 1); /* METH */
  269. cio_write(cio, jp2->precedence, 1); /* PRECEDENCE */
  270. cio_write(cio, jp2->approx, 1); /* APPROX */
  271. if(jp2->meth == 2)
  272. jp2->enumcs = 0;
  273. cio_write(cio, jp2->enumcs, 4); /* EnumCS */
  274. box.length = cio_tell(cio) - box.init_pos;
  275. cio_seek(cio, box.init_pos);
  276. cio_write(cio, box.length, 4); /* L */
  277. cio_seek(cio, box.init_pos + box.length);
  278. }
  279. static void jp2_free_pclr(opj_jp2_color_t *color)
  280. {
  281. opj_free(color->jp2_pclr->channel_sign);
  282. opj_free(color->jp2_pclr->channel_size);
  283. opj_free(color->jp2_pclr->entries);
  284. if(color->jp2_pclr->cmap) opj_free(color->jp2_pclr->cmap);
  285. opj_free(color->jp2_pclr); color->jp2_pclr = NULL;
  286. }
  287. static void free_color_data(opj_jp2_color_t *color)
  288. {
  289. if(color->jp2_pclr)
  290. {
  291. jp2_free_pclr(color);
  292. }
  293. if(color->jp2_cdef)
  294. {
  295. if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
  296. opj_free(color->jp2_cdef);
  297. }
  298. if(color->icc_profile_buf) opj_free(color->icc_profile_buf);
  299. }
  300. static void jp2_apply_pclr(opj_jp2_color_t *color, opj_image_t *image, opj_common_ptr cinfo)
  301. {
  302. opj_image_comp_t *old_comps, *new_comps;
  303. unsigned char *channel_size, *channel_sign;
  304. unsigned int *entries;
  305. opj_jp2_cmap_comp_t *cmap;
  306. int *src, *dst;
  307. unsigned int j, max;
  308. unsigned short i, nr_channels, cmp, pcol;
  309. int k, top_k;
  310. channel_size = color->jp2_pclr->channel_size;
  311. channel_sign = color->jp2_pclr->channel_sign;
  312. entries = color->jp2_pclr->entries;
  313. cmap = color->jp2_pclr->cmap;
  314. nr_channels = color->jp2_pclr->nr_channels;
  315. old_comps = image->comps;
  316. new_comps = (opj_image_comp_t*)
  317. opj_malloc(nr_channels * sizeof(opj_image_comp_t));
  318. for(i = 0; i < nr_channels; ++i)
  319. {
  320. pcol = cmap[i].pcol; cmp = cmap[i].cmp;
  321. if( pcol < nr_channels )
  322. new_comps[pcol] = old_comps[cmp];
  323. else
  324. {
  325. opj_event_msg(cinfo, EVT_ERROR, "Error with pcol value %d (max: %d). skipping\n", pcol, nr_channels);
  326. continue;
  327. }
  328. if(cmap[i].mtyp == 0) /* Direct use */
  329. {
  330. old_comps[cmp].data = NULL; continue;
  331. }
  332. /* Palette mapping: */
  333. new_comps[pcol].data = (int*)
  334. opj_malloc(old_comps[cmp].w * old_comps[cmp].h * sizeof(int));
  335. new_comps[pcol].prec = channel_size[i];
  336. new_comps[pcol].sgnd = channel_sign[i];
  337. }
  338. top_k = color->jp2_pclr->nr_entries - 1;
  339. for(i = 0; i < nr_channels; ++i)
  340. {
  341. /* Direct use: */
  342. if(cmap[i].mtyp == 0) continue;
  343. /* Palette mapping: */
  344. cmp = cmap[i].cmp; pcol = cmap[i].pcol;
  345. src = old_comps[cmp].data;
  346. dst = new_comps[pcol].data;
  347. max = new_comps[pcol].w * new_comps[pcol].h;
  348. for(j = 0; j < max; ++j)
  349. {
  350. /* The index */
  351. if((k = src[j]) < 0) k = 0; else if(k > top_k) k = top_k;
  352. /* The colour */
  353. dst[j] = entries[k * nr_channels + pcol];
  354. }
  355. }
  356. max = image->numcomps;
  357. for(i = 0; i < max; ++i)
  358. {
  359. if(old_comps[i].data) opj_free(old_comps[i].data);
  360. }
  361. opj_free(old_comps);
  362. image->comps = new_comps;
  363. image->numcomps = nr_channels;
  364. jp2_free_pclr(color);
  365. }/* apply_pclr() */
  366. static opj_bool jp2_read_pclr(opj_jp2_t *jp2, opj_cio_t *cio,
  367. opj_jp2_box_t *box, opj_jp2_color_t *color)
  368. {
  369. opj_jp2_pclr_t *jp2_pclr;
  370. unsigned char *channel_size, *channel_sign;
  371. unsigned int *entries;
  372. unsigned short nr_entries, nr_channels;
  373. unsigned short i, j;
  374. unsigned char uc;
  375. OPJ_ARG_NOT_USED(box);
  376. OPJ_ARG_NOT_USED(jp2);
  377. /* Part 1, I.5.3.4: 'There shall be at most one Palette box inside
  378. * a JP2 Header box' :
  379. */
  380. if(color->jp2_pclr) return OPJ_FALSE;
  381. nr_entries = (unsigned short)cio_read(cio, 2); /* NE */
  382. nr_channels = (unsigned short)cio_read(cio, 1);/* NPC */
  383. entries = (unsigned int*)
  384. opj_malloc(nr_channels * nr_entries * sizeof(unsigned int));
  385. channel_size = (unsigned char*)opj_malloc(nr_channels);
  386. channel_sign = (unsigned char*)opj_malloc(nr_channels);
  387. jp2_pclr = (opj_jp2_pclr_t*)opj_malloc(sizeof(opj_jp2_pclr_t));
  388. jp2_pclr->channel_sign = channel_sign;
  389. jp2_pclr->channel_size = channel_size;
  390. jp2_pclr->entries = entries;
  391. jp2_pclr->nr_entries = nr_entries;
  392. jp2_pclr->nr_channels = nr_channels;
  393. jp2_pclr->cmap = NULL;
  394. color->jp2_pclr = jp2_pclr;
  395. for(i = 0; i < nr_channels; ++i)
  396. {
  397. uc = cio_read(cio, 1); /* Bi */
  398. channel_size[i] = (uc & 0x7f) + 1;
  399. channel_sign[i] = (uc & 0x80)?1:0;
  400. }
  401. for(j = 0; j < nr_entries; ++j)
  402. {
  403. for(i = 0; i < nr_channels; ++i)
  404. {
  405. /* Cji */
  406. *entries++ = cio_read(cio, channel_size[i]>>3);
  407. }
  408. }
  409. return OPJ_TRUE;
  410. }/* jp2_read_pclr() */
  411. static opj_bool jp2_read_cmap(opj_jp2_t *jp2, opj_cio_t *cio,
  412. opj_jp2_box_t *box, opj_jp2_color_t *color)
  413. {
  414. opj_jp2_cmap_comp_t *cmap;
  415. unsigned short i, nr_channels;
  416. OPJ_ARG_NOT_USED(box);
  417. OPJ_ARG_NOT_USED(jp2);
  418. /* Need nr_channels: */
  419. if(color->jp2_pclr == NULL) return OPJ_FALSE;
  420. /* Part 1, I.5.3.5: 'There shall be at most one Component Mapping box
  421. * inside a JP2 Header box' :
  422. */
  423. if(color->jp2_pclr->cmap) return OPJ_FALSE;
  424. nr_channels = color->jp2_pclr->nr_channels;
  425. cmap = (opj_jp2_cmap_comp_t*)
  426. opj_malloc(nr_channels * sizeof(opj_jp2_cmap_comp_t));
  427. for(i = 0; i < nr_channels; ++i)
  428. {
  429. cmap[i].cmp = (unsigned short)cio_read(cio, 2);
  430. cmap[i].mtyp = cio_read(cio, 1);
  431. cmap[i].pcol = cio_read(cio, 1);
  432. }
  433. color->jp2_pclr->cmap = cmap;
  434. return OPJ_TRUE;
  435. }/* jp2_read_cmap() */
  436. static void jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
  437. {
  438. opj_jp2_cdef_info_t *info;
  439. int color_space;
  440. unsigned short i, n, cn, typ, asoc, acn;
  441. color_space = image->color_space;
  442. info = color->jp2_cdef->info;
  443. n = color->jp2_cdef->n;
  444. for(i = 0; i < n; ++i)
  445. {
  446. /* WATCH: acn = asoc - 1 ! */
  447. if((asoc = info[i].asoc) == 0) continue;
  448. cn = info[i].cn; typ = info[i].typ; acn = asoc - 1;
  449. if(cn != acn)
  450. {
  451. opj_image_comp_t saved;
  452. memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_t));
  453. memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj_image_comp_t));
  454. memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp_t));
  455. info[i].asoc = cn + 1;
  456. info[acn].asoc = info[acn].cn + 1;
  457. }
  458. }
  459. if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
  460. opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
  461. }/* jp2_apply_cdef() */
  462. static opj_bool jp2_read_cdef(opj_jp2_t *jp2, opj_cio_t *cio,
  463. opj_jp2_box_t *box, opj_jp2_color_t *color)
  464. {
  465. opj_jp2_cdef_info_t *info;
  466. unsigned short i, n;
  467. OPJ_ARG_NOT_USED(box);
  468. OPJ_ARG_NOT_USED(jp2);
  469. /* Part 1, I.5.3.6: 'The shall be at most one Channel Definition box
  470. * inside a JP2 Header box.'
  471. */
  472. if(color->jp2_cdef) return OPJ_FALSE;
  473. if((n = (unsigned short)cio_read(cio, 2)) == 0) return OPJ_FALSE; /* szukw000: FIXME */
  474. info = (opj_jp2_cdef_info_t*)
  475. opj_malloc(n * sizeof(opj_jp2_cdef_info_t));
  476. color->jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2_cdef_t));
  477. color->jp2_cdef->info = info;
  478. color->jp2_cdef->n = n;
  479. for(i = 0; i < n; ++i)
  480. {
  481. info[i].cn = (unsigned short)cio_read(cio, 2);
  482. info[i].typ = (unsigned short)cio_read(cio, 2);
  483. info[i].asoc = (unsigned short)cio_read(cio, 2);
  484. }
  485. return OPJ_TRUE;
  486. }/* jp2_read_cdef() */
  487. static opj_bool jp2_read_colr(opj_jp2_t *jp2, opj_cio_t *cio,
  488. opj_jp2_box_t *box, opj_jp2_color_t *color)
  489. {
  490. int skip_len;
  491. opj_common_ptr cinfo;
  492. /* Part 1, I.5.3.3 : 'A conforming JP2 reader shall ignore all Colour
  493. * Specification boxes after the first.'
  494. */
  495. if(color->jp2_has_colr) return OPJ_FALSE;
  496. cinfo = jp2->cinfo;
  497. jp2->meth = cio_read(cio, 1); /* METH */
  498. jp2->precedence = cio_read(cio, 1); /* PRECEDENCE */
  499. jp2->approx = cio_read(cio, 1); /* APPROX */
  500. if (jp2->meth == 1)
  501. {
  502. jp2->enumcs = cio_read(cio, 4); /* EnumCS */
  503. }
  504. else
  505. if (jp2->meth == 2)
  506. {
  507. /* skip PROFILE */
  508. skip_len = box->init_pos + box->length - cio_tell(cio);
  509. if (skip_len < 0)
  510. {
  511. opj_event_msg(cinfo, EVT_ERROR, "Error with COLR box size\n");
  512. return OPJ_FALSE;
  513. }
  514. if(skip_len > 0)
  515. {
  516. unsigned char *start;
  517. start = cio_getbp(cio);
  518. color->icc_profile_buf = (unsigned char*)opj_malloc(skip_len);
  519. color->icc_profile_len = skip_len;
  520. cio_skip(cio, box->init_pos + box->length - cio_tell(cio));
  521. memcpy(color->icc_profile_buf, start, skip_len);
  522. }
  523. }
  524. if (cio_tell(cio) - box->init_pos != box->length)
  525. {
  526. opj_event_msg(cinfo, EVT_ERROR, "Error with COLR Box\n");
  527. return OPJ_FALSE;
  528. }
  529. color->jp2_has_colr = 1;
  530. return OPJ_TRUE;
  531. }/* jp2_read_colr() */
  532. opj_bool jp2_read_jp2h(opj_jp2_t *jp2, opj_cio_t *cio, opj_jp2_color_t *color)
  533. {
  534. opj_jp2_box_t box;
  535. int jp2h_end;
  536. opj_common_ptr cinfo = jp2->cinfo;
  537. jp2_read_boxhdr(cinfo, cio, &box);
  538. do
  539. {
  540. if (JP2_JP2H != box.type)
  541. {
  542. if (box.type == JP2_JP2C)
  543. {
  544. opj_event_msg(cinfo, EVT_ERROR, "Expected JP2H Marker\n");
  545. return OPJ_FALSE;
  546. }
  547. cio_skip(cio, box.length - 8);
  548. if(cio->bp >= cio->end) return OPJ_FALSE;
  549. jp2_read_boxhdr(cinfo, cio, &box);
  550. }
  551. } while(JP2_JP2H != box.type);
  552. if (!jp2_read_ihdr(jp2, cio))
  553. return OPJ_FALSE;
  554. jp2h_end = box.init_pos + box.length;
  555. if (jp2->bpc == 255)
  556. {
  557. if (!jp2_read_bpcc(jp2, cio))
  558. return OPJ_FALSE;
  559. }
  560. jp2_read_boxhdr(cinfo, cio, &box);
  561. while(cio_tell(cio) < jp2h_end)
  562. {
  563. if(box.type == JP2_COLR)
  564. {
  565. if( !jp2_read_colr(jp2, cio, &box, color))
  566. {
  567. cio_seek(cio, box.init_pos + 8);
  568. cio_skip(cio, box.length - 8);
  569. }
  570. jp2_read_boxhdr(cinfo, cio, &box);
  571. continue;
  572. }
  573. if(box.type == JP2_CDEF && !jp2->ignore_pclr_cmap_cdef)
  574. {
  575. if( !jp2_read_cdef(jp2, cio, &box, color))
  576. {
  577. cio_seek(cio, box.init_pos + 8);
  578. cio_skip(cio, box.length - 8);
  579. }
  580. jp2_read_boxhdr(cinfo, cio, &box);
  581. continue;
  582. }
  583. if(box.type == JP2_PCLR && !jp2->ignore_pclr_cmap_cdef)
  584. {
  585. if( !jp2_read_pclr(jp2, cio, &box, color))
  586. {
  587. cio_seek(cio, box.init_pos + 8);
  588. cio_skip(cio, box.length - 8);
  589. }
  590. jp2_read_boxhdr(cinfo, cio, &box);
  591. continue;
  592. }
  593. if(box.type == JP2_CMAP && !jp2->ignore_pclr_cmap_cdef)
  594. {
  595. if( !jp2_read_cmap(jp2, cio, &box, color))
  596. {
  597. cio_seek(cio, box.init_pos + 8);
  598. cio_skip(cio, box.length - 8);
  599. }
  600. jp2_read_boxhdr(cinfo, cio, &box);
  601. continue;
  602. }
  603. cio_seek(cio, box.init_pos + 8);
  604. cio_skip(cio, box.length - 8);
  605. jp2_read_boxhdr(cinfo, cio, &box);
  606. }/* while(cio_tell(cio) < box_end) */
  607. cio_seek(cio, jp2h_end);
  608. /* Part 1, I.5.3.3 : 'must contain at least one' */
  609. return (color->jp2_has_colr == 1);
  610. }/* jp2_read_jp2h() */
  611. opj_image_t* opj_jp2_decode(opj_jp2_t *jp2, opj_cio_t *cio,
  612. opj_codestream_info_t *cstr_info)
  613. {
  614. opj_common_ptr cinfo;
  615. opj_image_t *image = NULL;
  616. opj_jp2_color_t color;
  617. if(!jp2 || !cio)
  618. {
  619. return NULL;
  620. }
  621. memset(&color, 0, sizeof(opj_jp2_color_t));
  622. cinfo = jp2->cinfo;
  623. /* JP2 decoding */
  624. if(!jp2_read_struct(jp2, cio, &color))
  625. {
  626. free_color_data(&color);
  627. opj_event_msg(cinfo, EVT_ERROR, "Failed to decode jp2 structure\n");
  628. return NULL;
  629. }
  630. /* J2K decoding */
  631. image = j2k_decode(jp2->j2k, cio, cstr_info);
  632. if(!image)
  633. {
  634. free_color_data(&color);
  635. opj_event_msg(cinfo, EVT_ERROR, "Failed to decode J2K image\n");
  636. return NULL;
  637. }
  638. if (!jp2->ignore_pclr_cmap_cdef){
  639. /* Set Image Color Space */
  640. if (jp2->enumcs == 16)
  641. image->color_space = CLRSPC_SRGB;
  642. else if (jp2->enumcs == 17)
  643. image->color_space = CLRSPC_GRAY;
  644. else if (jp2->enumcs == 18)
  645. image->color_space = CLRSPC_SYCC;
  646. else
  647. image->color_space = CLRSPC_UNKNOWN;
  648. if(color.jp2_cdef)
  649. {
  650. jp2_apply_cdef(image, &color);
  651. }
  652. if(color.jp2_pclr)
  653. {
  654. /* Part 1, I.5.3.4: Either both or none : */
  655. if( !color.jp2_pclr->cmap)
  656. jp2_free_pclr(&color);
  657. else
  658. jp2_apply_pclr(&color, image, cinfo);
  659. }
  660. if(color.icc_profile_buf)
  661. {
  662. image->icc_profile_buf = color.icc_profile_buf;
  663. color.icc_profile_buf = NULL;
  664. image->icc_profile_len = color.icc_profile_len;
  665. }
  666. }
  667. return image;
  668. }/* opj_jp2_decode() */
  669. void jp2_write_jp2h(opj_jp2_t *jp2, opj_cio_t *cio) {
  670. opj_jp2_box_t box;
  671. box.init_pos = cio_tell(cio);
  672. cio_skip(cio, 4);
  673. cio_write(cio, JP2_JP2H, 4); /* JP2H */
  674. jp2_write_ihdr(jp2, cio);
  675. if (jp2->bpc == 255) {
  676. jp2_write_bpcc(jp2, cio);
  677. }
  678. jp2_write_colr(jp2, cio);
  679. box.length = cio_tell(cio) - box.init_pos;
  680. cio_seek(cio, box.init_pos);
  681. cio_write(cio, box.length, 4); /* L */
  682. cio_seek(cio, box.init_pos + box.length);
  683. }
  684. static void jp2_write_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
  685. unsigned int i;
  686. opj_jp2_box_t box;
  687. box.init_pos = cio_tell(cio);
  688. cio_skip(cio, 4);
  689. cio_write(cio, JP2_FTYP, 4); /* FTYP */
  690. cio_write(cio, jp2->brand, 4); /* BR */
  691. cio_write(cio, jp2->minversion, 4); /* MinV */
  692. for (i = 0; i < jp2->numcl; i++) {
  693. cio_write(cio, jp2->cl[i], 4); /* CL */
  694. }
  695. box.length = cio_tell(cio) - box.init_pos;
  696. cio_seek(cio, box.init_pos);
  697. cio_write(cio, box.length, 4); /* L */
  698. cio_seek(cio, box.init_pos + box.length);
  699. }
  700. static opj_bool jp2_read_ftyp(opj_jp2_t *jp2, opj_cio_t *cio) {
  701. int i;
  702. opj_jp2_box_t box;
  703. opj_common_ptr cinfo = jp2->cinfo;
  704. jp2_read_boxhdr(cinfo, cio, &box);
  705. if (JP2_FTYP != box.type) {
  706. opj_event_msg(cinfo, EVT_ERROR, "Expected FTYP Marker\n");
  707. return OPJ_FALSE;
  708. }
  709. jp2->brand = cio_read(cio, 4); /* BR */
  710. jp2->minversion = cio_read(cio, 4); /* MinV */
  711. jp2->numcl = (box.length - 16) / 4;
  712. jp2->cl = (unsigned int *) opj_malloc(jp2->numcl * sizeof(unsigned int));
  713. for (i = 0; i < (int)jp2->numcl; i++) {
  714. jp2->cl[i] = cio_read(cio, 4); /* CLi */
  715. }
  716. if (cio_tell(cio) - box.init_pos != box.length) {
  717. opj_event_msg(cinfo, EVT_ERROR, "Error with FTYP Box\n");
  718. return OPJ_FALSE;
  719. }
  720. return OPJ_TRUE;
  721. }
  722. static int jp2_write_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
  723. unsigned int j2k_codestream_offset, j2k_codestream_length;
  724. opj_jp2_box_t box;
  725. opj_j2k_t *j2k = jp2->j2k;
  726. box.init_pos = cio_tell(cio);
  727. cio_skip(cio, 4);
  728. cio_write(cio, JP2_JP2C, 4); /* JP2C */
  729. /* J2K encoding */
  730. j2k_codestream_offset = cio_tell(cio);
  731. if(!j2k_encode(j2k, cio, image, cstr_info)) {
  732. opj_event_msg(j2k->cinfo, EVT_ERROR, "Failed to encode image\n");
  733. return 0;
  734. }
  735. j2k_codestream_length = cio_tell(cio) - j2k_codestream_offset;
  736. jp2->j2k_codestream_offset = j2k_codestream_offset;
  737. jp2->j2k_codestream_length = j2k_codestream_length;
  738. box.length = 8 + jp2->j2k_codestream_length;
  739. cio_seek(cio, box.init_pos);
  740. cio_write(cio, box.length, 4); /* L */
  741. cio_seek(cio, box.init_pos + box.length);
  742. return box.length;
  743. }
  744. static opj_bool jp2_read_jp2c(opj_jp2_t *jp2, opj_cio_t *cio, unsigned int *j2k_codestream_length, unsigned int *j2k_codestream_offset) {
  745. opj_jp2_box_t box;
  746. opj_common_ptr cinfo = jp2->cinfo;
  747. jp2_read_boxhdr(cinfo, cio, &box);
  748. do {
  749. if(JP2_JP2C != box.type) {
  750. cio_skip(cio, box.length - 8);
  751. jp2_read_boxhdr(cinfo, cio, &box);
  752. }
  753. } while(JP2_JP2C != box.type);
  754. *j2k_codestream_offset = cio_tell(cio);
  755. *j2k_codestream_length = box.length - 8;
  756. return OPJ_TRUE;
  757. }
  758. static void jp2_write_jp(opj_cio_t *cio) {
  759. opj_jp2_box_t box;
  760. box.init_pos = cio_tell(cio);
  761. cio_skip(cio, 4);
  762. cio_write(cio, JP2_JP, 4); /* JP2 signature */
  763. cio_write(cio, 0x0d0a870a, 4);
  764. box.length = cio_tell(cio) - box.init_pos;
  765. cio_seek(cio, box.init_pos);
  766. cio_write(cio, box.length, 4); /* L */
  767. cio_seek(cio, box.init_pos + box.length);
  768. }
  769. static opj_bool jp2_read_jp(opj_jp2_t *jp2, opj_cio_t *cio) {
  770. opj_jp2_box_t box;
  771. opj_common_ptr cinfo = jp2->cinfo;
  772. jp2_read_boxhdr(cinfo, cio, &box);
  773. if (JP2_JP != box.type) {
  774. opj_event_msg(cinfo, EVT_ERROR, "Expected JP Marker\n");
  775. return OPJ_FALSE;
  776. }
  777. if (0x0d0a870a != cio_read(cio, 4)) {
  778. opj_event_msg(cinfo, EVT_ERROR, "Error with JP Marker\n");
  779. return OPJ_FALSE;
  780. }
  781. if (cio_tell(cio) - box.init_pos != box.length) {
  782. opj_event_msg(cinfo, EVT_ERROR, "Error with JP Box size\n");
  783. return OPJ_FALSE;
  784. }
  785. return OPJ_TRUE;
  786. }
  787. static opj_bool jp2_read_struct(opj_jp2_t *jp2, opj_cio_t *cio,
  788. opj_jp2_color_t *color) {
  789. if (!jp2_read_jp(jp2, cio))
  790. return OPJ_FALSE;
  791. if (!jp2_read_ftyp(jp2, cio))
  792. return OPJ_FALSE;
  793. if (!jp2_read_jp2h(jp2, cio, color))
  794. return OPJ_FALSE;
  795. if (!jp2_read_jp2c(jp2, cio, &jp2->j2k_codestream_length, &jp2->j2k_codestream_offset))
  796. return OPJ_FALSE;
  797. return OPJ_TRUE;
  798. }
  799. static int write_fidx( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
  800. {
  801. int len, lenp;
  802. lenp = cio_tell( cio);
  803. cio_skip( cio, 4); /* L [at the end] */
  804. cio_write( cio, JPIP_FIDX, 4); /* IPTR */
  805. write_prxy( offset_jp2c, length_jp2c, offset_idx, length_idx, cio);
  806. len = cio_tell( cio)-lenp;
  807. cio_seek( cio, lenp);
  808. cio_write( cio, len, 4); /* L */
  809. cio_seek( cio, lenp+len);
  810. return len;
  811. }
  812. static void write_prxy( int offset_jp2c, int length_jp2c, int offset_idx, int length_idx, opj_cio_t *cio)
  813. {
  814. int len, lenp;
  815. lenp = cio_tell( cio);
  816. cio_skip( cio, 4); /* L [at the end] */
  817. cio_write( cio, JPIP_PRXY, 4); /* IPTR */
  818. cio_write( cio, offset_jp2c, 8); /* OOFF */
  819. cio_write( cio, length_jp2c, 4); /* OBH part 1 */
  820. cio_write( cio, JP2_JP2C, 4); /* OBH part 2 */
  821. cio_write( cio, 1,1); /* NI */
  822. cio_write( cio, offset_idx, 8); /* IOFF */
  823. cio_write( cio, length_idx, 4); /* IBH part 1 */
  824. cio_write( cio, JPIP_CIDX, 4); /* IBH part 2 */
  825. len = cio_tell( cio)-lenp;
  826. cio_seek( cio, lenp);
  827. cio_write( cio, len, 4); /* L */
  828. cio_seek( cio, lenp+len);
  829. }
  830. static void write_iptr( int offset, int length, opj_cio_t *cio)
  831. {
  832. int len, lenp;
  833. lenp = cio_tell( cio);
  834. cio_skip( cio, 4); /* L [at the end] */
  835. cio_write( cio, JPIP_IPTR, 4); /* IPTR */
  836. cio_write( cio, offset, 8);
  837. cio_write( cio, length, 8);
  838. len = cio_tell( cio)-lenp;
  839. cio_seek( cio, lenp);
  840. cio_write( cio, len, 4); /* L */
  841. cio_seek( cio, lenp+len);
  842. }
  843. /* ----------------------------------------------------------------------- */
  844. /* JP2 decoder interface */
  845. /* ----------------------------------------------------------------------- */
  846. opj_jp2_t* jp2_create_decompress(opj_common_ptr cinfo) {
  847. opj_jp2_t *jp2 = (opj_jp2_t*) opj_calloc(1, sizeof(opj_jp2_t));
  848. if(jp2) {
  849. jp2->cinfo = cinfo;
  850. /* create the J2K codec */
  851. jp2->j2k = j2k_create_decompress(cinfo);
  852. if(jp2->j2k == NULL) {
  853. jp2_destroy_decompress(jp2);
  854. return NULL;
  855. }
  856. }
  857. return jp2;
  858. }
  859. void jp2_destroy_decompress(opj_jp2_t *jp2) {
  860. if(jp2) {
  861. /* destroy the J2K codec */
  862. j2k_destroy_decompress(jp2->j2k);
  863. if(jp2->comps) {
  864. opj_free(jp2->comps);
  865. }
  866. if(jp2->cl) {
  867. opj_free(jp2->cl);
  868. }
  869. opj_free(jp2);
  870. }
  871. }
  872. void jp2_setup_decoder(opj_jp2_t *jp2, opj_dparameters_t *parameters) {
  873. /* setup the J2K codec */
  874. j2k_setup_decoder(jp2->j2k, parameters);
  875. /* further JP2 initializations go here */
  876. jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG;
  877. }
  878. /* ----------------------------------------------------------------------- */
  879. /* JP2 encoder interface */
  880. /* ----------------------------------------------------------------------- */
  881. opj_jp2_t* jp2_create_compress(opj_common_ptr cinfo) {
  882. opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t));
  883. if(jp2) {
  884. jp2->cinfo = cinfo;
  885. /* create the J2K codec */
  886. jp2->j2k = j2k_create_compress(cinfo);
  887. if(jp2->j2k == NULL) {
  888. jp2_destroy_compress(jp2);
  889. return NULL;
  890. }
  891. }
  892. return jp2;
  893. }
  894. void jp2_destroy_compress(opj_jp2_t *jp2) {
  895. if(jp2) {
  896. /* destroy the J2K codec */
  897. j2k_destroy_compress(jp2->j2k);
  898. if(jp2->comps) {
  899. opj_free(jp2->comps);
  900. }
  901. if(jp2->cl) {
  902. opj_free(jp2->cl);
  903. }
  904. opj_free(jp2);
  905. }
  906. }
  907. void jp2_setup_encoder(opj_jp2_t *jp2, opj_cparameters_t *parameters, opj_image_t *image) {
  908. int i;
  909. int depth_0, sign;
  910. if(!jp2 || !parameters || !image)
  911. return;
  912. /* setup the J2K codec */
  913. /* ------------------- */
  914. /* Check if number of components respects standard */
  915. if (image->numcomps < 1 || image->numcomps > 16384) {
  916. opj_event_msg(jp2->cinfo, EVT_ERROR, "Invalid number of components specified while setting up JP2 encoder\n");
  917. return;
  918. }
  919. j2k_setup_encoder(jp2->j2k, parameters, image);
  920. /* setup the JP2 codec */
  921. /* ------------------- */
  922. /* Profile box */
  923. jp2->brand = JP2_JP2; /* BR */
  924. jp2->minversion = 0; /* MinV */
  925. jp2->numcl = 1;
  926. jp2->cl = (unsigned int*) opj_malloc(jp2->numcl * sizeof(unsigned int));
  927. jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */
  928. /* Image Header box */
  929. jp2->numcomps = image->numcomps; /* NC */
  930. jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp2_comps_t));
  931. jp2->h = image->y1 - image->y0; /* HEIGHT */
  932. jp2->w = image->x1 - image->x0; /* WIDTH */
  933. /* BPC */
  934. depth_0 = image->comps[0].prec - 1;
  935. sign = image->comps[0].sgnd;
  936. jp2->bpc = depth_0 + (sign << 7);
  937. for (i = 1; i < image->numcomps; i++) {
  938. int depth = image->comps[i].prec - 1;
  939. sign = image->comps[i].sgnd;
  940. if (depth_0 != depth)
  941. jp2->bpc = 255;
  942. }
  943. jp2->C = 7; /* C : Always 7 */
  944. jp2->UnkC = 0; /* UnkC, colorspace specified in colr box */
  945. jp2->IPR = 0; /* IPR, no intellectual property */
  946. /* BitsPerComponent box */
  947. for (i = 0; i < image->numcomps; i++) {
  948. jp2->comps[i].bpcc = image->comps[i].prec - 1 + (image->comps[i].sgnd << 7);
  949. }
  950. jp2->meth = 1;
  951. if (image->color_space == 1)
  952. jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2.1 */
  953. else if (image->color_space == 2)
  954. jp2->enumcs = 17; /* greyscale */
  955. else if (image->color_space == 3)
  956. jp2->enumcs = 18; /* YUV */
  957. jp2->precedence = 0; /* PRECEDENCE */
  958. jp2->approx = 0; /* APPROX */
  959. jp2->jpip_on = parameters->jpip_on;
  960. }
  961. opj_bool opj_jp2_encode(opj_jp2_t *jp2, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
  962. int pos_iptr, pos_cidx, pos_jp2c, len_jp2c, len_cidx, end_pos, pos_fidx, len_fidx;
  963. pos_jp2c = pos_iptr = -1; /* remove a warning */
  964. /* JP2 encoding */
  965. /* JPEG 2000 Signature box */
  966. jp2_write_jp(cio);
  967. /* File Type box */
  968. jp2_write_ftyp(jp2, cio);
  969. /* JP2 Header box */
  970. jp2_write_jp2h(jp2, cio);
  971. if( jp2->jpip_on){
  972. pos_iptr = cio_tell( cio);
  973. cio_skip( cio, 24); /* IPTR further ! */
  974. pos_jp2c = cio_tell( cio);
  975. }
  976. /* J2K encoding */
  977. if(!(len_jp2c = jp2_write_jp2c( jp2, cio, image, cstr_info))){
  978. opj_event_msg(jp2->cinfo, EVT_ERROR, "Failed to encode image\n");
  979. return OPJ_FALSE;
  980. }
  981. if( jp2->jpip_on){
  982. pos_cidx = cio_tell( cio);
  983. len_cidx = write_cidx( pos_jp2c+8, cio, image, *cstr_info, len_jp2c-8);
  984. pos_fidx = cio_tell( cio);
  985. len_fidx = write_fidx( pos_jp2c, len_jp2c, pos_cidx, len_cidx, cio);
  986. end_pos = cio_tell( cio);
  987. cio_seek( cio, pos_iptr);
  988. write_iptr( pos_fidx, len_fidx, cio);
  989. cio_seek( cio, end_pos);
  990. }
  991. return OPJ_TRUE;
  992. }