/src/FreeImage/Source/LibOpenJPEG/j2k.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 2534 lines · 1799 code · 348 blank · 387 comment · 377 complexity · 7c3951d52c2de93f0d3640ef51d5ada6 MD5 · raw file

Large files are truncated click here to view the full 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) 2006-2007, Parvatha Elangovan
  9. * Copyright (c) 2010-2011, Kaori Hagihara
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "opj_includes.h"
  34. /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
  35. /*@{*/
  36. /** @name Local static functions */
  37. /*@{*/
  38. /**
  39. Write the SOC marker (Start Of Codestream)
  40. @param j2k J2K handle
  41. */
  42. static void j2k_write_soc(opj_j2k_t *j2k);
  43. /**
  44. Read the SOC marker (Start of Codestream)
  45. @param j2k J2K handle
  46. */
  47. static void j2k_read_soc(opj_j2k_t *j2k);
  48. /**
  49. Write the SIZ marker (image and tile size)
  50. @param j2k J2K handle
  51. */
  52. static void j2k_write_siz(opj_j2k_t *j2k);
  53. /**
  54. Read the SIZ marker (image and tile size)
  55. @param j2k J2K handle
  56. */
  57. static void j2k_read_siz(opj_j2k_t *j2k);
  58. /**
  59. Write the COM marker (comment)
  60. @param j2k J2K handle
  61. */
  62. static void j2k_write_com(opj_j2k_t *j2k);
  63. /**
  64. Read the COM marker (comment)
  65. @param j2k J2K handle
  66. */
  67. static void j2k_read_com(opj_j2k_t *j2k);
  68. /**
  69. Write the value concerning the specified component in the marker COD and COC
  70. @param j2k J2K handle
  71. @param compno Number of the component concerned by the information written
  72. */
  73. static void j2k_write_cox(opj_j2k_t *j2k, int compno);
  74. /**
  75. Read the value concerning the specified component in the marker COD and COC
  76. @param j2k J2K handle
  77. @param compno Number of the component concerned by the information read
  78. */
  79. static void j2k_read_cox(opj_j2k_t *j2k, int compno);
  80. /**
  81. Write the COD marker (coding style default)
  82. @param j2k J2K handle
  83. */
  84. static void j2k_write_cod(opj_j2k_t *j2k);
  85. /**
  86. Read the COD marker (coding style default)
  87. @param j2k J2K handle
  88. */
  89. static void j2k_read_cod(opj_j2k_t *j2k);
  90. /**
  91. Write the COC marker (coding style component)
  92. @param j2k J2K handle
  93. @param compno Number of the component concerned by the information written
  94. */
  95. static void j2k_write_coc(opj_j2k_t *j2k, int compno);
  96. /**
  97. Read the COC marker (coding style component)
  98. @param j2k J2K handle
  99. */
  100. static void j2k_read_coc(opj_j2k_t *j2k);
  101. /**
  102. Write the value concerning the specified component in the marker QCD and QCC
  103. @param j2k J2K handle
  104. @param compno Number of the component concerned by the information written
  105. */
  106. static void j2k_write_qcx(opj_j2k_t *j2k, int compno);
  107. /**
  108. Read the value concerning the specified component in the marker QCD and QCC
  109. @param j2k J2K handle
  110. @param compno Number of the component concern by the information read
  111. @param len Length of the information in the QCX part of the marker QCD/QCC
  112. */
  113. static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len);
  114. /**
  115. Write the QCD marker (quantization default)
  116. @param j2k J2K handle
  117. */
  118. static void j2k_write_qcd(opj_j2k_t *j2k);
  119. /**
  120. Read the QCD marker (quantization default)
  121. @param j2k J2K handle
  122. */
  123. static void j2k_read_qcd(opj_j2k_t *j2k);
  124. /**
  125. Write the QCC marker (quantization component)
  126. @param j2k J2K handle
  127. @param compno Number of the component concerned by the information written
  128. */
  129. static void j2k_write_qcc(opj_j2k_t *j2k, int compno);
  130. /**
  131. Read the QCC marker (quantization component)
  132. @param j2k J2K handle
  133. */
  134. static void j2k_read_qcc(opj_j2k_t *j2k);
  135. /**
  136. Write the POC marker (progression order change)
  137. @param j2k J2K handle
  138. */
  139. static void j2k_write_poc(opj_j2k_t *j2k);
  140. /**
  141. Read the POC marker (progression order change)
  142. @param j2k J2K handle
  143. */
  144. static void j2k_read_poc(opj_j2k_t *j2k);
  145. /**
  146. Read the CRG marker (component registration)
  147. @param j2k J2K handle
  148. */
  149. static void j2k_read_crg(opj_j2k_t *j2k);
  150. /**
  151. Read the TLM marker (tile-part lengths)
  152. @param j2k J2K handle
  153. */
  154. static void j2k_read_tlm(opj_j2k_t *j2k);
  155. /**
  156. Read the PLM marker (packet length, main header)
  157. @param j2k J2K handle
  158. */
  159. static void j2k_read_plm(opj_j2k_t *j2k);
  160. /**
  161. Read the PLT marker (packet length, tile-part header)
  162. @param j2k J2K handle
  163. */
  164. static void j2k_read_plt(opj_j2k_t *j2k);
  165. /**
  166. Read the PPM marker (packet packet headers, main header)
  167. @param j2k J2K handle
  168. */
  169. static void j2k_read_ppm(opj_j2k_t *j2k);
  170. /**
  171. Read the PPT marker (packet packet headers, tile-part header)
  172. @param j2k J2K handle
  173. */
  174. static void j2k_read_ppt(opj_j2k_t *j2k);
  175. /**
  176. Write the TLM marker (Mainheader)
  177. @param j2k J2K handle
  178. */
  179. static void j2k_write_tlm(opj_j2k_t *j2k);
  180. /**
  181. Write the SOT marker (start of tile-part)
  182. @param j2k J2K handle
  183. */
  184. static void j2k_write_sot(opj_j2k_t *j2k);
  185. /**
  186. Read the SOT marker (start of tile-part)
  187. @param j2k J2K handle
  188. */
  189. static void j2k_read_sot(opj_j2k_t *j2k);
  190. /**
  191. Write the SOD marker (start of data)
  192. @param j2k J2K handle
  193. @param tile_coder Pointer to a TCD handle
  194. */
  195. static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder);
  196. /**
  197. Read the SOD marker (start of data)
  198. @param j2k J2K handle
  199. */
  200. static void j2k_read_sod(opj_j2k_t *j2k);
  201. /**
  202. Write the RGN marker (region-of-interest)
  203. @param j2k J2K handle
  204. @param compno Number of the component concerned by the information written
  205. @param tileno Number of the tile concerned by the information written
  206. */
  207. static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno);
  208. /**
  209. Read the RGN marker (region-of-interest)
  210. @param j2k J2K handle
  211. */
  212. static void j2k_read_rgn(opj_j2k_t *j2k);
  213. /**
  214. Write the EOC marker (end of codestream)
  215. @param j2k J2K handle
  216. */
  217. static void j2k_write_eoc(opj_j2k_t *j2k);
  218. /**
  219. Read the EOC marker (end of codestream)
  220. @param j2k J2K handle
  221. */
  222. static void j2k_read_eoc(opj_j2k_t *j2k);
  223. /**
  224. Read an unknown marker
  225. @param j2k J2K handle
  226. */
  227. static void j2k_read_unk(opj_j2k_t *j2k);
  228. /**
  229. Add main header marker information
  230. @param cstr_info Codestream information structure
  231. @param type marker type
  232. @param pos byte offset of marker segment
  233. @param len length of marker segment
  234. */
  235. static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
  236. /**
  237. Add tile header marker information
  238. @param tileno tile index number
  239. @param cstr_info Codestream information structure
  240. @param type marker type
  241. @param pos byte offset of marker segment
  242. @param len length of marker segment
  243. */
  244. static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len);
  245. /*@}*/
  246. /*@}*/
  247. /* ----------------------------------------------------------------------- */
  248. typedef struct j2k_prog_order{
  249. OPJ_PROG_ORDER enum_prog;
  250. char str_prog[5];
  251. }j2k_prog_order_t;
  252. j2k_prog_order_t j2k_prog_order_list[] = {
  253. {CPRL, "CPRL"},
  254. {LRCP, "LRCP"},
  255. {PCRL, "PCRL"},
  256. {RLCP, "RLCP"},
  257. {RPCL, "RPCL"},
  258. {(OPJ_PROG_ORDER)-1, ""}
  259. };
  260. char *j2k_convert_progression_order(OPJ_PROG_ORDER prg_order){
  261. j2k_prog_order_t *po;
  262. for(po = j2k_prog_order_list; po->enum_prog != -1; po++ ){
  263. if(po->enum_prog == prg_order){
  264. break;
  265. }
  266. }
  267. return po->str_prog;
  268. }
  269. /* ----------------------------------------------------------------------- */
  270. static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno){
  271. char *prog;
  272. int i;
  273. int tpnum=1,tpend=0;
  274. opj_tcp_t *tcp = &cp->tcps[tileno];
  275. prog = j2k_convert_progression_order(tcp->prg);
  276. if(cp->tp_on == 1){
  277. for(i=0;i<4;i++){
  278. if(tpend!=1){
  279. if( cp->tp_flag == prog[i] ){
  280. tpend=1;cp->tp_pos=i;
  281. }
  282. switch(prog[i]){
  283. case 'C':
  284. tpnum= tpnum * tcp->pocs[pino].compE;
  285. break;
  286. case 'R':
  287. tpnum= tpnum * tcp->pocs[pino].resE;
  288. break;
  289. case 'P':
  290. tpnum= tpnum * tcp->pocs[pino].prcE;
  291. break;
  292. case 'L':
  293. tpnum= tpnum * tcp->pocs[pino].layE;
  294. break;
  295. }
  296. }
  297. }
  298. }else{
  299. tpnum=1;
  300. }
  301. return tpnum;
  302. }
  303. /** mem allocation for TLM marker*/
  304. int j2k_calculate_tp(opj_cp_t *cp,int img_numcomp,opj_image_t *image,opj_j2k_t *j2k ){
  305. int pino,tileno,totnum_tp=0;
  306. OPJ_ARG_NOT_USED(img_numcomp);
  307. j2k->cur_totnum_tp = (int *) opj_malloc(cp->tw * cp->th * sizeof(int));
  308. for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  309. int cur_totnum_tp = 0;
  310. opj_tcp_t *tcp = &cp->tcps[tileno];
  311. for(pino = 0; pino <= tcp->numpocs; pino++) {
  312. int tp_num=0;
  313. opj_pi_iterator_t *pi = pi_initialise_encode(image, cp, tileno,FINAL_PASS);
  314. if(!pi) { return -1;}
  315. tp_num = j2k_get_num_tp(cp,pino,tileno);
  316. totnum_tp = totnum_tp + tp_num;
  317. cur_totnum_tp = cur_totnum_tp + tp_num;
  318. pi_destroy(pi, cp, tileno);
  319. }
  320. j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
  321. /* INDEX >> */
  322. if (j2k->cstr_info) {
  323. j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
  324. j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
  325. }
  326. /* << INDEX */
  327. }
  328. return totnum_tp;
  329. }
  330. static void j2k_write_soc(opj_j2k_t *j2k) {
  331. opj_cio_t *cio = j2k->cio;
  332. cio_write(cio, J2K_MS_SOC, 2);
  333. if(j2k->cstr_info)
  334. j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio), 0);
  335. /* UniPG>> */
  336. #ifdef USE_JPWL
  337. /* update markers struct */
  338. j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
  339. #endif /* USE_JPWL */
  340. /* <<UniPG */
  341. }
  342. static void j2k_read_soc(opj_j2k_t *j2k) {
  343. j2k->state = J2K_STATE_MHSIZ;
  344. /* Index */
  345. if (j2k->cstr_info) {
  346. j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
  347. j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
  348. }
  349. }
  350. static void j2k_write_siz(opj_j2k_t *j2k) {
  351. int i;
  352. int lenp, len;
  353. opj_cio_t *cio = j2k->cio;
  354. opj_image_t *image = j2k->image;
  355. opj_cp_t *cp = j2k->cp;
  356. cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
  357. lenp = cio_tell(cio);
  358. cio_skip(cio, 2);
  359. cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
  360. cio_write(cio, image->x1, 4); /* Xsiz */
  361. cio_write(cio, image->y1, 4); /* Ysiz */
  362. cio_write(cio, image->x0, 4); /* X0siz */
  363. cio_write(cio, image->y0, 4); /* Y0siz */
  364. cio_write(cio, cp->tdx, 4); /* XTsiz */
  365. cio_write(cio, cp->tdy, 4); /* YTsiz */
  366. cio_write(cio, cp->tx0, 4); /* XT0siz */
  367. cio_write(cio, cp->ty0, 4); /* YT0siz */
  368. cio_write(cio, image->numcomps, 2); /* Csiz */
  369. for (i = 0; i < image->numcomps; i++) {
  370. cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
  371. cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
  372. cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
  373. }
  374. len = cio_tell(cio) - lenp;
  375. cio_seek(cio, lenp);
  376. cio_write(cio, len, 2); /* Lsiz */
  377. cio_seek(cio, lenp + len);
  378. if(j2k->cstr_info)
  379. j2k_add_mhmarker(j2k->cstr_info, J2K_MS_SIZ, lenp, len);
  380. }
  381. static void j2k_read_siz(opj_j2k_t *j2k) {
  382. int len, i;
  383. opj_cio_t *cio = j2k->cio;
  384. opj_image_t *image = j2k->image;
  385. opj_cp_t *cp = j2k->cp;
  386. len = cio_read(cio, 2); /* Lsiz */
  387. cio_read(cio, 2); /* Rsiz (capabilities) */
  388. image->x1 = cio_read(cio, 4); /* Xsiz */
  389. image->y1 = cio_read(cio, 4); /* Ysiz */
  390. image->x0 = cio_read(cio, 4); /* X0siz */
  391. image->y0 = cio_read(cio, 4); /* Y0siz */
  392. cp->tdx = cio_read(cio, 4); /* XTsiz */
  393. cp->tdy = cio_read(cio, 4); /* YTsiz */
  394. cp->tx0 = cio_read(cio, 4); /* XT0siz */
  395. cp->ty0 = cio_read(cio, 4); /* YT0siz */
  396. if ((image->x0<0)||(image->x1<0)||(image->y0<0)||(image->y1<0)) {
  397. opj_event_msg(j2k->cinfo, EVT_ERROR,
  398. "%s: invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
  399. image->x0,image->x1,image->y0,image->y1);
  400. return;
  401. }
  402. image->numcomps = cio_read(cio, 2); /* Csiz */
  403. #ifdef USE_JPWL
  404. if (j2k->cp->correct) {
  405. /* if JPWL is on, we check whether TX errors have damaged
  406. too much the SIZ parameters */
  407. if (!(image->x1 * image->y1)) {
  408. opj_event_msg(j2k->cinfo, EVT_ERROR,
  409. "JPWL: bad image size (%d x %d)\n",
  410. image->x1, image->y1);
  411. if (!JPWL_ASSUME || JPWL_ASSUME) {
  412. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  413. return;
  414. }
  415. }
  416. if (image->numcomps != ((len - 38) / 3)) {
  417. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  418. "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
  419. image->numcomps, ((len - 38) / 3));
  420. if (!JPWL_ASSUME) {
  421. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  422. return;
  423. }
  424. /* we try to correct */
  425. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
  426. if (image->numcomps < ((len - 38) / 3)) {
  427. len = 38 + 3 * image->numcomps;
  428. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
  429. len);
  430. } else {
  431. image->numcomps = ((len - 38) / 3);
  432. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
  433. image->numcomps);
  434. }
  435. }
  436. /* update components number in the jpwl_exp_comps filed */
  437. cp->exp_comps = image->numcomps;
  438. }
  439. #endif /* USE_JPWL */
  440. image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
  441. for (i = 0; i < image->numcomps; i++) {
  442. int tmp, w, h;
  443. tmp = cio_read(cio, 1); /* Ssiz_i */
  444. image->comps[i].prec = (tmp & 0x7f) + 1;
  445. image->comps[i].sgnd = tmp >> 7;
  446. image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
  447. image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
  448. #ifdef USE_JPWL
  449. if (j2k->cp->correct) {
  450. /* if JPWL is on, we check whether TX errors have damaged
  451. too much the SIZ parameters, again */
  452. if (!(image->comps[i].dx * image->comps[i].dy)) {
  453. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  454. "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
  455. i, i, image->comps[i].dx, image->comps[i].dy);
  456. if (!JPWL_ASSUME) {
  457. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  458. return;
  459. }
  460. /* we try to correct */
  461. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
  462. if (!image->comps[i].dx) {
  463. image->comps[i].dx = 1;
  464. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
  465. i, image->comps[i].dx);
  466. }
  467. if (!image->comps[i].dy) {
  468. image->comps[i].dy = 1;
  469. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
  470. i, image->comps[i].dy);
  471. }
  472. }
  473. }
  474. #endif /* USE_JPWL */
  475. /* TODO: unused ? */
  476. w = int_ceildiv(image->x1 - image->x0, image->comps[i].dx);
  477. h = int_ceildiv(image->y1 - image->y0, image->comps[i].dy);
  478. image->comps[i].resno_decoded = 0; /* number of resolution decoded */
  479. image->comps[i].factor = cp->reduce; /* reducing factor per component */
  480. }
  481. cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
  482. cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
  483. #ifdef USE_JPWL
  484. if (j2k->cp->correct) {
  485. /* if JPWL is on, we check whether TX errors have damaged
  486. too much the SIZ parameters */
  487. if ((cp->tw < 1) || (cp->th < 1) || (cp->tw > cp->max_tiles) || (cp->th > cp->max_tiles)) {
  488. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  489. "JPWL: bad number of tiles (%d x %d)\n",
  490. cp->tw, cp->th);
  491. if (!JPWL_ASSUME) {
  492. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  493. return;
  494. }
  495. /* we try to correct */
  496. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
  497. if (cp->tw < 1) {
  498. cp->tw= 1;
  499. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
  500. cp->tw);
  501. }
  502. if (cp->tw > cp->max_tiles) {
  503. cp->tw= 1;
  504. opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
  505. "- setting %d tiles in x => HYPOTHESIS!!!\n",
  506. cp->max_tiles, cp->tw);
  507. }
  508. if (cp->th < 1) {
  509. cp->th= 1;
  510. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
  511. cp->th);
  512. }
  513. if (cp->th > cp->max_tiles) {
  514. cp->th= 1;
  515. opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
  516. "- setting %d tiles in y => HYPOTHESIS!!!\n",
  517. cp->max_tiles, cp->th);
  518. }
  519. }
  520. }
  521. #endif /* USE_JPWL */
  522. cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
  523. cp->tileno = (int*) opj_malloc(cp->tw * cp->th * sizeof(int));
  524. cp->tileno_size = 0;
  525. #ifdef USE_JPWL
  526. if (j2k->cp->correct) {
  527. if (!cp->tcps) {
  528. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  529. "JPWL: could not alloc tcps field of cp\n");
  530. if (!JPWL_ASSUME || JPWL_ASSUME) {
  531. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  532. return;
  533. }
  534. }
  535. }
  536. #endif /* USE_JPWL */
  537. for (i = 0; i < cp->tw * cp->th; i++) {
  538. cp->tcps[i].POC = 0;
  539. cp->tcps[i].numpocs = 0;
  540. cp->tcps[i].first = 1;
  541. }
  542. /* Initialization for PPM marker */
  543. cp->ppm = 0;
  544. cp->ppm_data = NULL;
  545. cp->ppm_data_first = NULL;
  546. cp->ppm_previous = 0;
  547. cp->ppm_store = 0;
  548. j2k->default_tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
  549. for (i = 0; i < cp->tw * cp->th; i++) {
  550. cp->tcps[i].tccps = (opj_tccp_t*) opj_malloc(image->numcomps * sizeof(opj_tccp_t));
  551. }
  552. j2k->tile_data = (unsigned char**) opj_calloc(cp->tw * cp->th, sizeof(unsigned char*));
  553. j2k->tile_len = (int*) opj_calloc(cp->tw * cp->th, sizeof(int));
  554. j2k->state = J2K_STATE_MH;
  555. /* Index */
  556. if (j2k->cstr_info) {
  557. opj_codestream_info_t *cstr_info = j2k->cstr_info;
  558. cstr_info->image_w = image->x1 - image->x0;
  559. cstr_info->image_h = image->y1 - image->y0;
  560. cstr_info->numcomps = image->numcomps;
  561. cstr_info->tw = cp->tw;
  562. cstr_info->th = cp->th;
  563. cstr_info->tile_x = cp->tdx;
  564. cstr_info->tile_y = cp->tdy;
  565. cstr_info->tile_Ox = cp->tx0;
  566. cstr_info->tile_Oy = cp->ty0;
  567. cstr_info->tile = (opj_tile_info_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tile_info_t));
  568. }
  569. }
  570. static void j2k_write_com(opj_j2k_t *j2k) {
  571. unsigned int i;
  572. int lenp, len;
  573. if(j2k->cp->comment) {
  574. opj_cio_t *cio = j2k->cio;
  575. char *comment = j2k->cp->comment;
  576. cio_write(cio, J2K_MS_COM, 2);
  577. lenp = cio_tell(cio);
  578. cio_skip(cio, 2);
  579. cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
  580. for (i = 0; i < strlen(comment); i++) {
  581. cio_write(cio, comment[i], 1);
  582. }
  583. len = cio_tell(cio) - lenp;
  584. cio_seek(cio, lenp);
  585. cio_write(cio, len, 2);
  586. cio_seek(cio, lenp + len);
  587. if(j2k->cstr_info)
  588. j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COM, lenp, len);
  589. }
  590. }
  591. static void j2k_read_com(opj_j2k_t *j2k) {
  592. int len;
  593. opj_cio_t *cio = j2k->cio;
  594. len = cio_read(cio, 2);
  595. cio_skip(cio, len - 2);
  596. }
  597. static void j2k_write_cox(opj_j2k_t *j2k, int compno) {
  598. int i;
  599. opj_cp_t *cp = j2k->cp;
  600. opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
  601. opj_tccp_t *tccp = &tcp->tccps[compno];
  602. opj_cio_t *cio = j2k->cio;
  603. cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
  604. cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
  605. cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
  606. cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
  607. cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
  608. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  609. for (i = 0; i < tccp->numresolutions; i++) {
  610. cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
  611. }
  612. }
  613. }
  614. static void j2k_read_cox(opj_j2k_t *j2k, int compno) {
  615. int i;
  616. opj_cp_t *cp = j2k->cp;
  617. opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  618. opj_tccp_t *tccp = &tcp->tccps[compno];
  619. opj_cio_t *cio = j2k->cio;
  620. tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
  621. /* If user wants to remove more resolutions than the codestream contains, return error*/
  622. if (cp->reduce >= tccp->numresolutions) {
  623. opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
  624. "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
  625. j2k->state |= J2K_STATE_ERR;
  626. }
  627. tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
  628. tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
  629. tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
  630. tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
  631. if (tccp->csty & J2K_CP_CSTY_PRT) {
  632. for (i = 0; i < tccp->numresolutions; i++) {
  633. int tmp = cio_read(cio, 1); /* SPcox (I_i) */
  634. tccp->prcw[i] = tmp & 0xf;
  635. tccp->prch[i] = tmp >> 4;
  636. }
  637. }
  638. /* INDEX >> */
  639. if(j2k->cstr_info && compno == 0) {
  640. for (i = 0; i < tccp->numresolutions; i++) {
  641. if (tccp->csty & J2K_CP_CSTY_PRT) {
  642. j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
  643. j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
  644. }
  645. else {
  646. j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
  647. j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
  648. }
  649. }
  650. }
  651. /* << INDEX */
  652. }
  653. static void j2k_write_cod(opj_j2k_t *j2k) {
  654. opj_cp_t *cp = NULL;
  655. opj_tcp_t *tcp = NULL;
  656. int lenp, len;
  657. opj_cio_t *cio = j2k->cio;
  658. cio_write(cio, J2K_MS_COD, 2); /* COD */
  659. lenp = cio_tell(cio);
  660. cio_skip(cio, 2);
  661. cp = j2k->cp;
  662. tcp = &cp->tcps[j2k->curtileno];
  663. cio_write(cio, tcp->csty, 1); /* Scod */
  664. cio_write(cio, tcp->prg, 1); /* SGcod (A) */
  665. cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
  666. cio_write(cio, tcp->mct, 1); /* SGcod (C) */
  667. j2k_write_cox(j2k, 0);
  668. len = cio_tell(cio) - lenp;
  669. cio_seek(cio, lenp);
  670. cio_write(cio, len, 2); /* Lcod */
  671. cio_seek(cio, lenp + len);
  672. if(j2k->cstr_info)
  673. j2k_add_mhmarker(j2k->cstr_info, J2K_MS_COD, lenp, len);
  674. }
  675. static void j2k_read_cod(opj_j2k_t *j2k) {
  676. int len, i, pos;
  677. opj_cio_t *cio = j2k->cio;
  678. opj_cp_t *cp = j2k->cp;
  679. opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  680. opj_image_t *image = j2k->image;
  681. len = cio_read(cio, 2); /* Lcod */
  682. tcp->csty = cio_read(cio, 1); /* Scod */
  683. tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
  684. tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
  685. tcp->mct = cio_read(cio, 1); /* SGcod (C) */
  686. pos = cio_tell(cio);
  687. for (i = 0; i < image->numcomps; i++) {
  688. tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
  689. cio_seek(cio, pos);
  690. j2k_read_cox(j2k, i);
  691. }
  692. /* Index */
  693. if (j2k->cstr_info) {
  694. opj_codestream_info_t *cstr_info = j2k->cstr_info;
  695. cstr_info->prog = tcp->prg;
  696. cstr_info->numlayers = tcp->numlayers;
  697. cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
  698. for (i = 0; i < image->numcomps; i++) {
  699. cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
  700. }
  701. }
  702. }
  703. static void j2k_write_coc(opj_j2k_t *j2k, int compno) {
  704. int lenp, len;
  705. opj_cp_t *cp = j2k->cp;
  706. opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
  707. opj_image_t *image = j2k->image;
  708. opj_cio_t *cio = j2k->cio;
  709. cio_write(cio, J2K_MS_COC, 2); /* COC */
  710. lenp = cio_tell(cio);
  711. cio_skip(cio, 2);
  712. cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
  713. cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
  714. j2k_write_cox(j2k, compno);
  715. len = cio_tell(cio) - lenp;
  716. cio_seek(cio, lenp);
  717. cio_write(cio, len, 2); /* Lcoc */
  718. cio_seek(cio, lenp + len);
  719. }
  720. static void j2k_read_coc(opj_j2k_t *j2k) {
  721. int len, compno;
  722. opj_cp_t *cp = j2k->cp;
  723. opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  724. opj_image_t *image = j2k->image;
  725. opj_cio_t *cio = j2k->cio;
  726. len = cio_read(cio, 2); /* Lcoc */
  727. compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
  728. tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
  729. j2k_read_cox(j2k, compno);
  730. }
  731. static void j2k_write_qcx(opj_j2k_t *j2k, int compno) {
  732. int bandno, numbands;
  733. int expn, mant;
  734. opj_cp_t *cp = j2k->cp;
  735. opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
  736. opj_tccp_t *tccp = &tcp->tccps[compno];
  737. opj_cio_t *cio = j2k->cio;
  738. cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
  739. numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
  740. for (bandno = 0; bandno < numbands; bandno++) {
  741. expn = tccp->stepsizes[bandno].expn;
  742. mant = tccp->stepsizes[bandno].mant;
  743. if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  744. cio_write(cio, expn << 3, 1); /* SPqcx_i */
  745. } else {
  746. cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
  747. }
  748. }
  749. }
  750. static void j2k_read_qcx(opj_j2k_t *j2k, int compno, int len) {
  751. int tmp;
  752. int bandno, numbands;
  753. opj_cp_t *cp = j2k->cp;
  754. opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  755. opj_tccp_t *tccp = &tcp->tccps[compno];
  756. opj_cio_t *cio = j2k->cio;
  757. tmp = cio_read(cio, 1); /* Sqcx */
  758. tccp->qntsty = tmp & 0x1f;
  759. tccp->numgbits = tmp >> 5;
  760. numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
  761. 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
  762. #ifdef USE_JPWL
  763. if (j2k->cp->correct) {
  764. /* if JPWL is on, we check whether there are too many subbands */
  765. if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
  766. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  767. "JPWL: bad number of subbands in Sqcx (%d)\n",
  768. numbands);
  769. if (!JPWL_ASSUME) {
  770. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  771. return;
  772. }
  773. /* we try to correct */
  774. numbands = 1;
  775. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
  776. "- setting number of bands to %d => HYPOTHESIS!!!\n",
  777. numbands);
  778. };
  779. };
  780. #else
  781. /* We check whether there are too many subbands */
  782. if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
  783. opj_event_msg(j2k->cinfo, EVT_WARNING ,
  784. "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
  785. "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n", numbands, J2K_MAXBANDS);
  786. }
  787. #endif /* USE_JPWL */
  788. for (bandno = 0; bandno < numbands; bandno++) {
  789. int expn, mant;
  790. if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  791. expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
  792. mant = 0;
  793. } else {
  794. tmp = cio_read(cio, 2); /* SPqcx_i */
  795. expn = tmp >> 11;
  796. mant = tmp & 0x7ff;
  797. }
  798. if (bandno < J2K_MAXBANDS){
  799. tccp->stepsizes[bandno].expn = expn;
  800. tccp->stepsizes[bandno].mant = mant;
  801. }
  802. }
  803. /* Add Antonin : if scalar_derived -> compute other stepsizes */
  804. if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
  805. for (bandno = 1; bandno < J2K_MAXBANDS; bandno++) {
  806. tccp->stepsizes[bandno].expn =
  807. ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
  808. (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
  809. tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
  810. }
  811. }
  812. /* ddA */
  813. }
  814. static void j2k_write_qcd(opj_j2k_t *j2k) {
  815. int lenp, len;
  816. opj_cio_t *cio = j2k->cio;
  817. cio_write(cio, J2K_MS_QCD, 2); /* QCD */
  818. lenp = cio_tell(cio);
  819. cio_skip(cio, 2);
  820. j2k_write_qcx(j2k, 0);
  821. len = cio_tell(cio) - lenp;
  822. cio_seek(cio, lenp);
  823. cio_write(cio, len, 2); /* Lqcd */
  824. cio_seek(cio, lenp + len);
  825. if(j2k->cstr_info)
  826. j2k_add_mhmarker(j2k->cstr_info, J2K_MS_QCD, lenp, len);
  827. }
  828. static void j2k_read_qcd(opj_j2k_t *j2k) {
  829. int len, i, pos;
  830. opj_cio_t *cio = j2k->cio;
  831. opj_image_t *image = j2k->image;
  832. len = cio_read(cio, 2); /* Lqcd */
  833. pos = cio_tell(cio);
  834. for (i = 0; i < image->numcomps; i++) {
  835. cio_seek(cio, pos);
  836. j2k_read_qcx(j2k, i, len - 2);
  837. }
  838. }
  839. static void j2k_write_qcc(opj_j2k_t *j2k, int compno) {
  840. int lenp, len;
  841. opj_cio_t *cio = j2k->cio;
  842. cio_write(cio, J2K_MS_QCC, 2); /* QCC */
  843. lenp = cio_tell(cio);
  844. cio_skip(cio, 2);
  845. cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
  846. j2k_write_qcx(j2k, compno);
  847. len = cio_tell(cio) - lenp;
  848. cio_seek(cio, lenp);
  849. cio_write(cio, len, 2); /* Lqcc */
  850. cio_seek(cio, lenp + len);
  851. }
  852. static void j2k_read_qcc(opj_j2k_t *j2k) {
  853. int len, compno;
  854. int numcomp = j2k->image->numcomps;
  855. opj_cio_t *cio = j2k->cio;
  856. len = cio_read(cio, 2); /* Lqcc */
  857. compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
  858. #ifdef USE_JPWL
  859. if (j2k->cp->correct) {
  860. static int backup_compno = 0;
  861. /* compno is negative or larger than the number of components!!! */
  862. if ((compno < 0) || (compno >= numcomp)) {
  863. opj_event_msg(j2k->cinfo, EVT_ERROR,
  864. "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
  865. compno, numcomp);
  866. if (!JPWL_ASSUME) {
  867. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  868. return;
  869. }
  870. /* we try to correct */
  871. compno = backup_compno % numcomp;
  872. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
  873. "- setting component number to %d\n",
  874. compno);
  875. }
  876. /* keep your private count of tiles */
  877. backup_compno++;
  878. };
  879. #endif /* USE_JPWL */
  880. j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
  881. }
  882. static void j2k_write_poc(opj_j2k_t *j2k) {
  883. int len, numpchgs, i;
  884. int numcomps = j2k->image->numcomps;
  885. opj_cp_t *cp = j2k->cp;
  886. opj_tcp_t *tcp = &cp->tcps[j2k->curtileno];
  887. opj_tccp_t *tccp = &tcp->tccps[0];
  888. opj_cio_t *cio = j2k->cio;
  889. numpchgs = 1 + tcp->numpocs;
  890. cio_write(cio, J2K_MS_POC, 2); /* POC */
  891. len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
  892. cio_write(cio, len, 2); /* Lpoc */
  893. for (i = 0; i < numpchgs; i++) {
  894. opj_poc_t *poc = &tcp->pocs[i];
  895. cio_write(cio, poc->resno0, 1); /* RSpoc_i */
  896. cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
  897. cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
  898. poc->layno1 = int_min(poc->layno1, tcp->numlayers);
  899. cio_write(cio, poc->resno1, 1); /* REpoc_i */
  900. poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
  901. cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
  902. poc->compno1 = int_min(poc->compno1, numcomps);
  903. cio_write(cio, poc->prg, 1); /* Ppoc_i */
  904. }
  905. }
  906. static void j2k_read_poc(opj_j2k_t *j2k) {
  907. int len, numpchgs, i, old_poc;
  908. int numcomps = j2k->image->numcomps;
  909. opj_cp_t *cp = j2k->cp;
  910. opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  911. opj_cio_t *cio = j2k->cio;
  912. old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
  913. tcp->POC = 1;
  914. len = cio_read(cio, 2); /* Lpoc */
  915. numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
  916. for (i = old_poc; i < numpchgs + old_poc; i++) {
  917. opj_poc_t *poc;
  918. poc = &tcp->pocs[i];
  919. poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
  920. poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
  921. poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
  922. poc->resno1 = cio_read(cio, 1); /* REpoc_i */
  923. poc->compno1 = int_min(
  924. cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
  925. poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
  926. }
  927. tcp->numpocs = numpchgs + old_poc - 1;
  928. }
  929. static void j2k_read_crg(opj_j2k_t *j2k) {
  930. int len, i, Xcrg_i, Ycrg_i;
  931. opj_cio_t *cio = j2k->cio;
  932. int numcomps = j2k->image->numcomps;
  933. len = cio_read(cio, 2); /* Lcrg */
  934. for (i = 0; i < numcomps; i++) {
  935. Xcrg_i = cio_read(cio, 2); /* Xcrg_i */
  936. Ycrg_i = cio_read(cio, 2); /* Ycrg_i */
  937. }
  938. }
  939. static void j2k_read_tlm(opj_j2k_t *j2k) {
  940. int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
  941. long int Ttlm_i, Ptlm_i;
  942. opj_cio_t *cio = j2k->cio;
  943. len = cio_read(cio, 2); /* Ltlm */
  944. Ztlm = cio_read(cio, 1); /* Ztlm */
  945. Stlm = cio_read(cio, 1); /* Stlm */
  946. ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
  947. SP = (Stlm >> 6) & 0x01;
  948. tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
  949. for (i = 0; i < tile_tlm; i++) {
  950. Ttlm_i = cio_read(cio, ST); /* Ttlm_i */
  951. Ptlm_i = cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
  952. }
  953. }
  954. static void j2k_read_plm(opj_j2k_t *j2k) {
  955. int len, i, Zplm, Nplm, add, packet_len = 0;
  956. opj_cio_t *cio = j2k->cio;
  957. len = cio_read(cio, 2); /* Lplm */
  958. Zplm = cio_read(cio, 1); /* Zplm */
  959. len -= 3;
  960. while (len > 0) {
  961. Nplm = cio_read(cio, 4); /* Nplm */
  962. len -= 4;
  963. for (i = Nplm; i > 0; i--) {
  964. add = cio_read(cio, 1);
  965. len--;
  966. packet_len = (packet_len << 7) + add; /* Iplm_ij */
  967. if ((add & 0x80) == 0) {
  968. /* New packet */
  969. packet_len = 0;
  970. }
  971. if (len <= 0)
  972. break;
  973. }
  974. }
  975. }
  976. static void j2k_read_plt(opj_j2k_t *j2k) {
  977. int len, i, Zplt, packet_len = 0, add;
  978. opj_cio_t *cio = j2k->cio;
  979. len = cio_read(cio, 2); /* Lplt */
  980. Zplt = cio_read(cio, 1); /* Zplt */
  981. for (i = len - 3; i > 0; i--) {
  982. add = cio_read(cio, 1);
  983. packet_len = (packet_len << 7) + add; /* Iplt_i */
  984. if ((add & 0x80) == 0) {
  985. /* New packet */
  986. packet_len = 0;
  987. }
  988. }
  989. }
  990. static void j2k_read_ppm(opj_j2k_t *j2k) {
  991. int len, Z_ppm, i, j;
  992. int N_ppm;
  993. opj_cp_t *cp = j2k->cp;
  994. opj_cio_t *cio = j2k->cio;
  995. len = cio_read(cio, 2);
  996. cp->ppm = 1;
  997. Z_ppm = cio_read(cio, 1); /* Z_ppm */
  998. len -= 3;
  999. while (len > 0) {
  1000. if (cp->ppm_previous == 0) {
  1001. N_ppm = cio_read(cio, 4); /* N_ppm */
  1002. len -= 4;
  1003. } else {
  1004. N_ppm = cp->ppm_previous;
  1005. }
  1006. j = cp->ppm_store;
  1007. if (Z_ppm == 0) { /* First PPM marker */
  1008. cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
  1009. cp->ppm_data_first = cp->ppm_data;
  1010. cp->ppm_len = N_ppm;
  1011. } else { /* NON-first PPM marker */
  1012. cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
  1013. #ifdef USE_JPWL
  1014. /* this memory allocation check could be done even in non-JPWL cases */
  1015. if (cp->correct) {
  1016. if (!cp->ppm_data) {
  1017. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1018. "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
  1019. cio_tell(cio));
  1020. if (!JPWL_ASSUME || JPWL_ASSUME) {
  1021. opj_free(cp->ppm_data);
  1022. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  1023. return;
  1024. }
  1025. }
  1026. }
  1027. #endif
  1028. cp->ppm_data_first = cp->ppm_data;
  1029. cp->ppm_len = N_ppm + cp->ppm_store;
  1030. }
  1031. for (i = N_ppm; i > 0; i--) { /* Read packet header */
  1032. cp->ppm_data[j] = cio_read(cio, 1);
  1033. j++;
  1034. len--;
  1035. if (len == 0)
  1036. break; /* Case of non-finished packet header in present marker but finished in next one */
  1037. }
  1038. cp->ppm_previous = i - 1;
  1039. cp->ppm_store = j;
  1040. }
  1041. }
  1042. static void j2k_read_ppt(opj_j2k_t *j2k) {
  1043. int len, Z_ppt, i, j = 0;
  1044. opj_cp_t *cp = j2k->cp;
  1045. opj_tcp_t *tcp = cp->tcps + j2k->curtileno;
  1046. opj_cio_t *cio = j2k->cio;
  1047. len = cio_read(cio, 2);
  1048. Z_ppt = cio_read(cio, 1);
  1049. tcp->ppt = 1;
  1050. if (Z_ppt == 0) { /* First PPT marker */
  1051. tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
  1052. tcp->ppt_data_first = tcp->ppt_data;
  1053. tcp->ppt_store = 0;
  1054. tcp->ppt_len = len - 3;
  1055. } else { /* NON-first PPT marker */
  1056. tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
  1057. tcp->ppt_data_first = tcp->ppt_data;
  1058. tcp->ppt_len = len - 3 + tcp->ppt_store;
  1059. }
  1060. j = tcp->ppt_store;
  1061. for (i = len - 3; i > 0; i--) {
  1062. tcp->ppt_data[j] = cio_read(cio, 1);
  1063. j++;
  1064. }
  1065. tcp->ppt_store = j;
  1066. }
  1067. static void j2k_write_tlm(opj_j2k_t *j2k){
  1068. int lenp;
  1069. opj_cio_t *cio = j2k->cio;
  1070. j2k->tlm_start = cio_tell(cio);
  1071. cio_write(cio, J2K_MS_TLM, 2);/* TLM */
  1072. lenp = 4 + (5*j2k->totnum_tp);
  1073. cio_write(cio,lenp,2); /* Ltlm */
  1074. cio_write(cio, 0,1); /* Ztlm=0*/
  1075. cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
  1076. cio_skip(cio,5*j2k->totnum_tp);
  1077. }
  1078. static void j2k_write_sot(opj_j2k_t *j2k) {
  1079. int lenp, len;
  1080. opj_cio_t *cio = j2k->cio;
  1081. j2k->sot_start = cio_tell(cio);
  1082. cio_write(cio, J2K_MS_SOT, 2); /* SOT */
  1083. lenp = cio_tell(cio);
  1084. cio_skip(cio, 2); /* Lsot (further) */
  1085. cio_write(cio, j2k->curtileno, 2); /* Isot */
  1086. cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
  1087. cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
  1088. cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
  1089. len = cio_tell(cio) - lenp;
  1090. cio_seek(cio, lenp);
  1091. cio_write(cio, len, 2); /* Lsot */
  1092. cio_seek(cio, lenp + len);
  1093. /* UniPG>> */
  1094. #ifdef USE_JPWL
  1095. /* update markers struct */
  1096. j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
  1097. #endif /* USE_JPWL */
  1098. /* <<UniPG */
  1099. if( j2k->cstr_info && j2k->cur_tp_num==0){
  1100. j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOT, lenp, len);
  1101. }
  1102. }
  1103. static void j2k_read_sot(opj_j2k_t *j2k) {
  1104. int len, tileno, totlen, partno, numparts, i;
  1105. opj_tcp_t *tcp = NULL;
  1106. char status = 0;
  1107. opj_cp_t *cp = j2k->cp;
  1108. opj_cio_t *cio = j2k->cio;
  1109. len = cio_read(cio, 2);
  1110. tileno = cio_read(cio, 2);
  1111. #ifdef USE_JPWL
  1112. if (j2k->cp->correct) {
  1113. static int backup_tileno = 0;
  1114. /* tileno is negative or larger than the number of tiles!!! */
  1115. if ((tileno < 0) || (tileno > (cp->tw * cp->th))) {
  1116. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1117. "JPWL: bad tile number (%d out of a maximum of %d)\n",
  1118. tileno, (cp->tw * cp->th));
  1119. if (!JPWL_ASSUME) {
  1120. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  1121. return;
  1122. }
  1123. /* we try to correct */
  1124. tileno = backup_tileno;
  1125. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
  1126. "- setting tile number to %d\n",
  1127. tileno);
  1128. }
  1129. /* keep your private count of tiles */
  1130. backup_tileno++;
  1131. };
  1132. #endif /* USE_JPWL */
  1133. if (cp->tileno_size == 0) {
  1134. cp->tileno[cp->tileno_size] = tileno;
  1135. cp->tileno_size++;
  1136. } else {
  1137. i = 0;
  1138. while (i < cp->tileno_size && status == 0) {
  1139. status = cp->tileno[i] == tileno ? 1 : 0;
  1140. i++;
  1141. }
  1142. if (status == 0) {
  1143. cp->tileno[cp->tileno_size] = tileno;
  1144. cp->tileno_size++;
  1145. }
  1146. }
  1147. totlen = cio_read(cio, 4);
  1148. #ifdef USE_JPWL
  1149. if (j2k->cp->correct) {
  1150. /* totlen is negative or larger than the bytes left!!! */
  1151. if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
  1152. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1153. "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
  1154. totlen, cio_numbytesleft(cio) + 8);
  1155. if (!JPWL_ASSUME) {
  1156. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  1157. return;
  1158. }
  1159. /* we try to correct */
  1160. totlen = 0;
  1161. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
  1162. "- setting Psot to %d => assuming it is the last tile\n",
  1163. totlen);
  1164. }
  1165. };
  1166. #endif /* USE_JPWL */
  1167. if (!totlen)
  1168. totlen = cio_numbytesleft(cio) + 8;
  1169. partno = cio_read(cio, 1);
  1170. numparts = cio_read(cio, 1);
  1171. if (partno >= numparts) {
  1172. opj_event_msg(j2k->cinfo, EVT_WARNING, "SOT marker inconsistency in tile %d: tile-part index greater (%d) than number of tile-parts (%d)\n", tileno, partno, numparts);
  1173. numparts = partno+1;
  1174. }
  1175. j2k->curtileno = tileno;
  1176. j2k->cur_tp_num = partno;
  1177. j2k->eot = cio_getbp(cio) - 12 + totlen;
  1178. j2k->state = J2K_STATE_TPH;
  1179. tcp = &cp->tcps[j2k->curtileno];
  1180. /* Index */
  1181. if (j2k->cstr_info) {
  1182. if (tcp->first) {
  1183. if (tileno == 0)
  1184. j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
  1185. j2k->cstr_info->tile[tileno].tileno = tileno;
  1186. j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
  1187. j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
  1188. } else {
  1189. j2k->cstr_info->tile[tileno].end_pos += totlen;
  1190. }
  1191. j2k->cstr_info->tile[tileno].num_tps = numparts;
  1192. if (numparts)
  1193. j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, numparts * sizeof(opj_tp_info_t));
  1194. else
  1195. j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_realloc(j2k->cstr_info->tile[tileno].tp, 10 * sizeof(opj_tp_info_t)); /* Fixme (10)*/
  1196. j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
  1197. j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
  1198. j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
  1199. }
  1200. if (tcp->first == 1) {
  1201. /* Initialization PPT */
  1202. opj_tccp_t *tmp = tcp->tccps;
  1203. memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
  1204. tcp->ppt = 0;
  1205. tcp->ppt_data = NULL;
  1206. tcp->ppt_data_first = NULL;
  1207. tcp->tccps = tmp;
  1208. for (i = 0; i < j2k->image->numcomps; i++) {
  1209. tcp->tccps[i] = j2k->default_tcp->tccps[i];
  1210. }
  1211. cp->tcps[j2k->curtileno].first = 0;
  1212. }
  1213. }
  1214. static void j2k_write_sod(opj_j2k_t *j2k, void *tile_coder) {
  1215. int l, layno;
  1216. int totlen;
  1217. opj_tcp_t *tcp = NULL;
  1218. opj_codestream_info_t *cstr_info = NULL;
  1219. opj_tcd_t *tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
  1220. opj_cp_t *cp = j2k->cp;
  1221. opj_cio_t *cio = j2k->cio;
  1222. tcd->tp_num = j2k->tp_num ;
  1223. tcd->cur_tp_num = j2k->cur_tp_num;
  1224. cio_write(cio, J2K_MS_SOD, 2);
  1225. if( j2k->cstr_info && j2k->cur_tp_num==0){
  1226. j2k_add_tlmarker( j2k->curtileno, j2k->cstr_info, J2K_MS_SOD, cio_tell(cio), 0);
  1227. }
  1228. if (j2k->curtileno == 0) {
  1229. j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
  1230. }
  1231. /* INDEX >> */
  1232. cstr_info = j2k->cstr_info;
  1233. if (cstr_info) {
  1234. if (!j2k->cur_tp_num ) {
  1235. cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
  1236. j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
  1237. }
  1238. else{
  1239. if(cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio))
  1240. cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
  1241. }
  1242. /* UniPG>> */
  1243. #ifdef USE_JPWL
  1244. /* update markers struct */
  1245. j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
  1246. #endif /* USE_JPWL */
  1247. /* <<UniPG */
  1248. }
  1249. /* << INDEX */
  1250. tcp = &cp->tcps[j2k->curtileno];
  1251. for (layno = 0; layno < tcp->numlayers; layno++) {
  1252. if (tcp->rates[layno]>(j2k->sod_start / (cp->th * cp->tw))) {
  1253. tcp->rates[layno]-=(j2k->sod_start / (cp->th * cp->tw));
  1254. } else if (tcp->rates[layno]) {
  1255. tcp->rates[layno]=1;
  1256. }
  1257. }
  1258. if(j2k->cur_tp_num == 0){
  1259. tcd->tcd_image->tiles->packno = 0;
  1260. if(cstr_info)
  1261. cstr_info->packno = 0;
  1262. }
  1263. l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
  1264. /* Writing Psot in SOT marker */
  1265. totlen = cio_tell(cio) + l - j2k->sot_start;
  1266. cio_seek(cio, j2k->sot_start + 6);
  1267. cio_write(cio, totlen, 4);
  1268. cio_seek(cio, j2k->sot_start + totlen);
  1269. /* Writing Ttlm and Ptlm in TLM marker */
  1270. if(cp->cinema){
  1271. cio_seek(cio, j2k->tlm_start + 6 + (5*j2k->cur_tp_num));
  1272. cio_write(cio, j2k->curtileno, 1);
  1273. cio_write(cio, totlen, 4);
  1274. }
  1275. cio_seek(cio, j2k->sot_start + totlen);
  1276. }
  1277. static void j2k_read_sod(opj_j2k_t *j2k) {
  1278. int len, truncate = 0, i;
  1279. unsigned char *data = NULL, *data_ptr = NULL;
  1280. opj_cio_t *cio = j2k->cio;
  1281. int curtileno = j2k->curtileno;
  1282. /* Index */
  1283. if (j2k->cstr_info) {
  1284. j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
  1285. cio_tell(cio) + j2k->pos_correction - 1;
  1286. if (j2k->cur_tp_num == 0)
  1287. j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
  1288. j2k->cstr_info->packno = 0;
  1289. }
  1290. len = int_min(j2k->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
  1291. if (len == cio_numbytesleft(cio) + 1) {
  1292. truncate = 1; /* Case of a truncate codestream */
  1293. }
  1294. data = j2k->tile_data[curtileno];
  1295. data = (unsigned char*) opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
  1296. data_ptr = data + j2k->tile_len[curtileno];
  1297. for (i = 0; i < len; i++) {
  1298. data_ptr[i] = cio_read(cio, 1);
  1299. }
  1300. j2k->tile_len[curtileno] += len;
  1301. j2k->tile_data[curtileno] = data;
  1302. if (!truncate) {
  1303. j2k->state = J2K_STATE_TPHSOT;
  1304. } else {
  1305. j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
  1306. }
  1307. j2k->cur_tp_num++;
  1308. }
  1309. static void j2k_write_rgn(opj_j2k_t *j2k, int compno, int tileno) {
  1310. opj_cp_t *cp = j2k->cp;
  1311. opj_tcp_t *tcp = &cp->tcps[tileno];
  1312. opj_cio_t *cio = j2k->cio;
  1313. int numcomps = j2k->image->numcomps;
  1314. cio_write(cio, J2K_MS_RGN, 2); /* RGN */
  1315. cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
  1316. cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
  1317. cio_write(cio, 0, 1); /* Srgn */
  1318. cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
  1319. }
  1320. static void j2k_read_rgn(opj_j2k_t *j2k) {
  1321. int len, compno, roisty;
  1322. opj_cp_t *cp = j2k->cp;
  1323. opj_tcp_t *tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  1324. opj_cio_t *cio = j2k->cio;
  1325. int numcomps = j2k->image->numcomps;
  1326. len = cio_read(cio, 2); /* Lrgn */
  1327. compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
  1328. roisty = cio_read(cio, 1); /* Srgn */
  1329. #ifdef USE_JPWL
  1330. if (j2k->cp->correct) {
  1331. /* totlen is negative or larger than the bytes left!!! */
  1332. if (compno >= numcomps) {
  1333. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1334. "JPWL: bad component number in RGN (%d when there are only %d)\n",
  1335. compno, numcomps);
  1336. if (!JPWL_ASSUME || JPWL_ASSUME) {
  1337. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  1338. return;
  1339. }
  1340. }
  1341. };
  1342. #endif /* USE_JPWL */
  1343. tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
  1344. }
  1345. static void j2k_write_eoc(opj_j2k_t *j2k) {
  1346. opj_cio_t *cio = j2k->cio;
  1347. /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
  1348. cio_write(cio, J2K_MS_EOC, 2);
  1349. /* UniPG>> */
  1350. #ifdef USE_JPWL
  1351. /* update markers struct */
  1352. j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
  1353. #endif /* USE_JPWL */
  1354. /* <<UniPG */
  1355. }
  1356. static void j2k_read_eoc(opj_j2k_t *j2k) {
  1357. int i, tileno;
  1358. opj_bool success;
  1359. /* if packets should be decoded */
  1360. if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
  1361. opj_tcd_t *tcd = tcd_create(j2k->cinfo);
  1362. tcd_malloc_decode(tcd, j2k->image, j2k->cp);
  1363. for (i = 0; i < j2k->cp->tileno_size; i++) {
  1364. tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
  1365. tileno = j2k->cp->tileno[i];
  1366. success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
  1367. opj_free(j2k->tile_data[tileno]);
  1368. j2k->tile_data[tileno] = NULL;
  1369. tcd_free_decode_tile(tcd, i);
  1370. if (success == OPJ_FALSE) {
  1371. j2k->state |= J2K_STATE_ERR;
  1372. break;
  1373. }
  1374. }
  1375. tcd_free_decode(tcd);
  1376. tcd_destroy(tcd);
  1377. }
  1378. /* if packets should not be decoded */
  1379. else {
  1380. for (i = 0; i < j2k->cp->tileno_size; i++) {
  1381. tileno = j2k->cp->tileno[i];
  1382. opj_free(j2k->tile_data[tileno]);
  1383. j2k->tile_data[tileno] = NULL;
  1384. }
  1385. }
  1386. if (j2k->state & J2K_STATE_ERR)
  1387. j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
  1388. else
  1389. j2k->state = J2K_STATE_MT;
  1390. }
  1391. typedef struct opj_dec_mstabent {
  1392. /** marker value */
  1393. int id;
  1394. /** value of the state when the marker can appear */
  1395. int states;
  1396. /** action linked to the marker */
  1397. void (*handler) (opj_j2k_t *j2k);
  1398. } opj_dec_mstabent_t;
  1399. opj_dec_mstabent_t j2k_dec_mstab[] = {
  1400. {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
  1401. {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
  1402. {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
  1403. {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
  1404. {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
  1405. {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
  1406. {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
  1407. {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
  1408. {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
  1409. {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
  1410. {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
  1411. {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
  1412. {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
  1413. {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
  1414. {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
  1415. {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
  1416. {J2K_MS_SOP, 0, 0},
  1417. {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
  1418. {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
  1419. #ifdef USE_JPWL
  1420. {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
  1421. {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
  1422. {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
  1423. {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
  1424. #endif /* USE_JPWL */
  1425. #ifdef USE_JPSEC
  1426. {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
  1427. {J2K_MS_INSEC, 0, j2k_read_insec},
  1428. #endif /* USE_JPSEC */
  1429. {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
  1430. };
  1431. static void j2k_read_unk(opj_j2k_t *j2k) {
  1432. opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
  1433. #ifdef USE_JPWL
  1434. if (j2k->cp->correct) {
  1435. int m = 0, id, i;
  1436. int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
  1437. cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
  1438. id = cio_read(j2k->cio, 2);
  1439. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1440. "JPWL: really don't know this marker %x\n",
  1441. id);
  1442. if (!JPWL_ASSUME) {
  1443. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1444. "- possible synch loss due to uncorrectable codestream errors => giving up\n");
  1445. return;
  1446. }
  1447. /* OK, activate this at your own risk!!! */
  1448. /* we look for the marker at the minimum hamming distance from this */
  1449. while (j2k_dec_mstab[m].id) {
  1450. /* 1's where they differ */
  1451. tmp_id = j2k_dec_mstab[m].id ^ id;
  1452. /* compute the hamming distance between our id and the current */
  1453. cur_dist = 0;
  1454. for (i = 0; i < 16; i++) {
  1455. if ((tmp_id >> i) & 0x0001) {
  1456. cur_dist++;
  1457. }
  1458. }
  1459. /* if current distance is smaller, set the minimum */
  1460. if (cur_dist < min_dist) {
  1461. min_dist = cur_dist;
  1462. min_id = j2k_dec_mstab[m].id;
  1463. }
  1464. /* jump to the next marker */
  1465. m++;
  1466. }
  1467. /* do we substitute the marker? */
  1468. if (min_dist < JPWL_MAXIMUM_HAMMING) {
  1469. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1470. "- marker %x is at distance %d from the read %x\n",
  1471. min_id, min_dist, id);
  1472. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1473. "- trying to substitute in place and crossing fingers!\n");
  1474. cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
  1475. cio_write(j2k->cio, min_id, 2);
  1476. /* rewind */
  1477. cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
  1478. }
  1479. };
  1480. #endif /* USE_JPWL */
  1481. }
  1482. /**
  1483. Read the lookup table containing all the marker, status and action
  1484. @param id Marker value
  1485. */
  1486. static opj_dec_mstabent_t *j2k_dec_mstab_lookup(int id) {
  1487. opj_dec_msta