PageRenderTime 69ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/cabalistic/ogredeps/
C | 2534 lines | 1799 code | 348 blank | 387 comment | 376 complexity | 7c3951d52c2de93f0d3640ef51d5ada6 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  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_mstabent_t *e;
  1488. for (e = j2k_dec_mstab; e->id != 0; e++) {
  1489. if (e->id == id) {
  1490. break;
  1491. }
  1492. }
  1493. return e;
  1494. }
  1495. /* ----------------------------------------------------------------------- */
  1496. /* J2K / JPT decoder interface */
  1497. /* ----------------------------------------------------------------------- */
  1498. opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
  1499. opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
  1500. if(!j2k)
  1501. return NULL;
  1502. j2k->default_tcp = (opj_tcp_t*) opj_calloc(1, sizeof(opj_tcp_t));
  1503. if(!j2k->default_tcp) {
  1504. opj_free(j2k);
  1505. return NULL;
  1506. }
  1507. j2k->cinfo = cinfo;
  1508. j2k->tile_data = NULL;
  1509. return j2k;
  1510. }
  1511. void j2k_destroy_decompress(opj_j2k_t *j2k) {
  1512. int i = 0;
  1513. if(j2k->tile_len != NULL) {
  1514. opj_free(j2k->tile_len);
  1515. }
  1516. if(j2k->tile_data != NULL) {
  1517. opj_free(j2k->tile_data);
  1518. }
  1519. if(j2k->default_tcp != NULL) {
  1520. opj_tcp_t *default_tcp = j2k->default_tcp;
  1521. if(default_tcp->ppt_data_first != NULL) {
  1522. opj_free(default_tcp->ppt_data_first);
  1523. }
  1524. if(j2k->default_tcp->tccps != NULL) {
  1525. opj_free(j2k->default_tcp->tccps);
  1526. }
  1527. opj_free(j2k->default_tcp);
  1528. }
  1529. if(j2k->cp != NULL) {
  1530. opj_cp_t *cp = j2k->cp;
  1531. if(cp->tcps != NULL) {
  1532. for(i = 0; i < cp->tw * cp->th; i++) {
  1533. if(cp->tcps[i].ppt_data_first != NULL) {
  1534. opj_free(cp->tcps[i].ppt_data_first);
  1535. }
  1536. if(cp->tcps[i].tccps != NULL) {
  1537. opj_free(cp->tcps[i].tccps);
  1538. }
  1539. }
  1540. opj_free(cp->tcps);
  1541. }
  1542. if(cp->ppm_data_first != NULL) {
  1543. opj_free(cp->ppm_data_first);
  1544. }
  1545. if(cp->tileno != NULL) {
  1546. opj_free(cp->tileno);
  1547. }
  1548. if(cp->comment != NULL) {
  1549. opj_free(cp->comment);
  1550. }
  1551. opj_free(cp);
  1552. }
  1553. opj_free(j2k);
  1554. }
  1555. void j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters) {
  1556. if(j2k && parameters) {
  1557. /* create and initialize the coding parameters structure */
  1558. opj_cp_t *cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
  1559. cp->reduce = parameters->cp_reduce;
  1560. cp->layer = parameters->cp_layer;
  1561. cp->limit_decoding = parameters->cp_limit_decoding;
  1562. #ifdef USE_JPWL
  1563. cp->correct = parameters->jpwl_correct;
  1564. cp->exp_comps = parameters->jpwl_exp_comps;
  1565. cp->max_tiles = parameters->jpwl_max_tiles;
  1566. #endif /* USE_JPWL */
  1567. /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
  1568. j2k->cp = cp;
  1569. }
  1570. }
  1571. opj_image_t* j2k_decode(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
  1572. opj_image_t *image = NULL;
  1573. opj_common_ptr cinfo = j2k->cinfo;
  1574. j2k->cio = cio;
  1575. j2k->cstr_info = cstr_info;
  1576. if (cstr_info)
  1577. memset(cstr_info, 0, sizeof(opj_codestream_info_t));
  1578. /* create an empty image */
  1579. image = opj_image_create0();
  1580. j2k->image = image;
  1581. j2k->state = J2K_STATE_MHSOC;
  1582. for (;;) {
  1583. opj_dec_mstabent_t *e;
  1584. int id = cio_read(cio, 2);
  1585. #ifdef USE_JPWL
  1586. /* we try to honor JPWL correction power */
  1587. if (j2k->cp->correct) {
  1588. int orig_pos = cio_tell(cio);
  1589. opj_bool status;
  1590. /* call the corrector */
  1591. status = jpwl_correct(j2k);
  1592. /* go back to where you were */
  1593. cio_seek(cio, orig_pos - 2);
  1594. /* re-read the marker */
  1595. id = cio_read(cio, 2);
  1596. /* check whether it begins with ff */
  1597. if (id >> 8 != 0xff) {
  1598. opj_event_msg(cinfo, EVT_ERROR,
  1599. "JPWL: possible bad marker %x at %d\n",
  1600. id, cio_tell(cio) - 2);
  1601. if (!JPWL_ASSUME) {
  1602. opj_image_destroy(image);
  1603. opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
  1604. return 0;
  1605. }
  1606. /* we try to correct */
  1607. id = id | 0xff00;
  1608. cio_seek(cio, cio_tell(cio) - 2);
  1609. cio_write(cio, id, 2);
  1610. opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
  1611. "- setting marker to %x\n",
  1612. id);
  1613. }
  1614. }
  1615. #endif /* USE_JPWL */
  1616. if (id >> 8 != 0xff) {
  1617. opj_image_destroy(image);
  1618. opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
  1619. return 0;
  1620. }
  1621. e = j2k_dec_mstab_lookup(id);
  1622. /* Check if the marker is known*/
  1623. if (!(j2k->state & e->states)) {
  1624. opj_image_destroy(image);
  1625. opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
  1626. return 0;
  1627. }
  1628. /* Check if the decoding is limited to the main header*/
  1629. if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
  1630. opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
  1631. return image;
  1632. }
  1633. if (e->handler) {
  1634. (*e->handler)(j2k);
  1635. }
  1636. if (j2k->state & J2K_STATE_ERR)
  1637. return NULL;
  1638. if (j2k->state == J2K_STATE_MT) {
  1639. break;
  1640. }
  1641. if (j2k->state == J2K_STATE_NEOC) {
  1642. break;
  1643. }
  1644. }
  1645. if (j2k->state == J2K_STATE_NEOC) {
  1646. j2k_read_eoc(j2k);
  1647. }
  1648. if (j2k->state != J2K_STATE_MT) {
  1649. opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
  1650. }
  1651. return image;
  1652. }
  1653. /*
  1654. * Read a JPT-stream and decode file
  1655. *
  1656. */
  1657. opj_image_t* j2k_decode_jpt_stream(opj_j2k_t *j2k, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
  1658. opj_image_t *image = NULL;
  1659. opj_jpt_msg_header_t header;
  1660. int position;
  1661. opj_common_ptr cinfo = j2k->cinfo;
  1662. OPJ_ARG_NOT_USED(cstr_info);
  1663. j2k->cio = cio;
  1664. /* create an empty image */
  1665. image = opj_image_create0();
  1666. j2k->image = image;
  1667. j2k->state = J2K_STATE_MHSOC;
  1668. /* Initialize the header */
  1669. jpt_init_msg_header(&header);
  1670. /* Read the first header of the message */
  1671. jpt_read_msg_header(cinfo, cio, &header);
  1672. position = cio_tell(cio);
  1673. if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
  1674. opj_image_destroy(image);
  1675. opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
  1676. return 0;
  1677. }
  1678. for (;;) {
  1679. opj_dec_mstabent_t *e = NULL;
  1680. int id;
  1681. if (!cio_numbytesleft(cio)) {
  1682. j2k_read_eoc(j2k);
  1683. return image;
  1684. }
  1685. /* data-bin read -> need to read a new header */
  1686. if ((unsigned int) (cio_tell(cio) - position) == header.Msg_length) {
  1687. jpt_read_msg_header(cinfo, cio, &header);
  1688. position = cio_tell(cio);
  1689. if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
  1690. opj_image_destroy(image);
  1691. opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
  1692. return 0;
  1693. }
  1694. }
  1695. id = cio_read(cio, 2);
  1696. if (id >> 8 != 0xff) {
  1697. opj_image_destroy(image);
  1698. opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
  1699. return 0;
  1700. }
  1701. e = j2k_dec_mstab_lookup(id);
  1702. if (!(j2k->state & e->states)) {
  1703. opj_image_destroy(image);
  1704. opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
  1705. return 0;
  1706. }
  1707. if (e->handler) {
  1708. (*e->handler)(j2k);
  1709. }
  1710. if (j2k->state == J2K_STATE_MT) {
  1711. break;
  1712. }
  1713. if (j2k->state == J2K_STATE_NEOC) {
  1714. break;
  1715. }
  1716. }
  1717. if (j2k->state == J2K_STATE_NEOC) {
  1718. j2k_read_eoc(j2k);
  1719. }
  1720. if (j2k->state != J2K_STATE_MT) {
  1721. opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
  1722. }
  1723. return image;
  1724. }
  1725. /* ----------------------------------------------------------------------- */
  1726. /* J2K encoder interface */
  1727. /* ----------------------------------------------------------------------- */
  1728. opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
  1729. opj_j2k_t *j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
  1730. if(j2k) {
  1731. j2k->cinfo = cinfo;
  1732. }
  1733. return j2k;
  1734. }
  1735. void j2k_destroy_compress(opj_j2k_t *j2k) {
  1736. int tileno;
  1737. if(!j2k) return;
  1738. if(j2k->cp != NULL) {
  1739. opj_cp_t *cp = j2k->cp;
  1740. if(cp->comment) {
  1741. opj_free(cp->comment);
  1742. }
  1743. if(cp->matrice) {
  1744. opj_free(cp->matrice);
  1745. }
  1746. for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  1747. opj_free(cp->tcps[tileno].tccps);
  1748. }
  1749. opj_free(cp->tcps);
  1750. opj_free(cp);
  1751. }
  1752. opj_free(j2k);
  1753. }
  1754. void j2k_setup_encoder(opj_j2k_t *j2k, opj_cparameters_t *parameters, opj_image_t *image) {
  1755. int i, j, tileno, numpocs_tile;
  1756. opj_cp_t *cp = NULL;
  1757. if(!j2k || !parameters || ! image) {
  1758. return;
  1759. }
  1760. /* create and initialize the coding parameters structure */
  1761. cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
  1762. /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
  1763. j2k->cp = cp;
  1764. /* set default values for cp */
  1765. cp->tw = 1;
  1766. cp->th = 1;
  1767. /*
  1768. copy user encoding parameters
  1769. */
  1770. cp->cinema = parameters->cp_cinema;
  1771. cp->max_comp_size = parameters->max_comp_size;
  1772. cp->rsiz = parameters->cp_rsiz;
  1773. cp->disto_alloc = parameters->cp_disto_alloc;
  1774. cp->fixed_alloc = parameters->cp_fixed_alloc;
  1775. cp->fixed_quality = parameters->cp_fixed_quality;
  1776. /* mod fixed_quality */
  1777. if(parameters->cp_matrice) {
  1778. size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
  1779. cp->matrice = (int *) opj_malloc(array_size);
  1780. memcpy(cp->matrice, parameters->cp_matrice, array_size);
  1781. }
  1782. /* tiles */
  1783. cp->tdx = parameters->cp_tdx;
  1784. cp->tdy = parameters->cp_tdy;
  1785. /* tile offset */
  1786. cp->tx0 = parameters->cp_tx0;
  1787. cp->ty0 = parameters->cp_ty0;
  1788. /* comment string */
  1789. if(parameters->cp_comment) {
  1790. cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
  1791. if(cp->comment) {
  1792. strcpy(cp->comment, parameters->cp_comment);
  1793. }
  1794. }
  1795. /*
  1796. calculate other encoding parameters
  1797. */
  1798. if (parameters->tile_size_on) {
  1799. cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
  1800. cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
  1801. } else {
  1802. cp->tdx = image->x1 - cp->tx0;
  1803. cp->tdy = image->y1 - cp->ty0;
  1804. }
  1805. if(parameters->tp_on){
  1806. cp->tp_flag = parameters->tp_flag;
  1807. cp->tp_on = 1;
  1808. }
  1809. cp->img_size = 0;
  1810. for(i=0;i<image->numcomps ;i++){
  1811. cp->img_size += (image->comps[i].w *image->comps[i].h * image->comps[i].prec);
  1812. }
  1813. #ifdef USE_JPWL
  1814. /*
  1815. calculate JPWL encoding parameters
  1816. */
  1817. if (parameters->jpwl_epc_on) {
  1818. int i;
  1819. /* set JPWL on */
  1820. cp->epc_on = OPJ_TRUE;
  1821. cp->info_on = OPJ_FALSE; /* no informative technique */
  1822. /* set EPB on */
  1823. if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
  1824. cp->epb_on = OPJ_TRUE;
  1825. cp->hprot_MH = parameters->jpwl_hprot_MH;
  1826. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  1827. cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
  1828. cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
  1829. }
  1830. /* if tile specs are not specified, copy MH specs */
  1831. if (cp->hprot_TPH[0] == -1) {
  1832. cp->hprot_TPH_tileno[0] = 0;
  1833. cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
  1834. }
  1835. for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
  1836. cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
  1837. cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
  1838. cp->pprot[i] = parameters->jpwl_pprot[i];
  1839. }
  1840. }
  1841. /* set ESD writing */
  1842. if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
  1843. cp->esd_on = OPJ_TRUE;
  1844. cp->sens_size = parameters->jpwl_sens_size;
  1845. cp->sens_addr = parameters->jpwl_sens_addr;
  1846. cp->sens_range = parameters->jpwl_sens_range;
  1847. cp->sens_MH = parameters->jpwl_sens_MH;
  1848. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  1849. cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
  1850. cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
  1851. }
  1852. }
  1853. /* always set RED writing to false: we are at the encoder */
  1854. cp->red_on = OPJ_FALSE;
  1855. } else {
  1856. cp->epc_on = OPJ_FALSE;
  1857. }
  1858. #endif /* USE_JPWL */
  1859. /* initialize the mutiple tiles */
  1860. /* ---------------------------- */
  1861. cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
  1862. for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  1863. opj_tcp_t *tcp = &cp->tcps[tileno];
  1864. tcp->numlayers = parameters->tcp_numlayers;
  1865. for (j = 0; j < tcp->numlayers; j++) {
  1866. if(cp->cinema){
  1867. if (cp->fixed_quality) {
  1868. tcp->distoratio[j] = parameters->tcp_distoratio[j];
  1869. }
  1870. tcp->rates[j] = parameters->tcp_rates[j];
  1871. }else{
  1872. if (cp->fixed_quality) { /* add fixed_quality */
  1873. tcp->distoratio[j] = parameters->tcp_distoratio[j];
  1874. } else {
  1875. tcp->rates[j] = parameters->tcp_rates[j];
  1876. }
  1877. }
  1878. }
  1879. tcp->csty = parameters->csty;
  1880. tcp->prg = parameters->prog_order;
  1881. tcp->mct = parameters->tcp_mct;
  1882. numpocs_tile = 0;
  1883. tcp->POC = 0;
  1884. if (parameters->numpocs) {
  1885. /* initialisation of POC */
  1886. tcp->POC = 1;
  1887. for (i = 0; i < parameters->numpocs; i++) {
  1888. if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
  1889. opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
  1890. tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
  1891. tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
  1892. tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
  1893. tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
  1894. tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
  1895. tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
  1896. tcp_poc->tile = parameters->POC[numpocs_tile].tile;
  1897. numpocs_tile++;
  1898. }
  1899. }
  1900. tcp->numpocs = numpocs_tile -1 ;
  1901. }else{
  1902. tcp->numpocs = 0;
  1903. }
  1904. tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
  1905. for (i = 0; i < image->numcomps; i++) {
  1906. opj_tccp_t *tccp = &tcp->tccps[i];
  1907. tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
  1908. tccp->numresolutions = parameters->numresolution;
  1909. tccp->cblkw = int_floorlog2(parameters->cblockw_init);
  1910. tccp->cblkh = int_floorlog2(parameters->cblockh_init);
  1911. tccp->cblksty = parameters->mode;
  1912. tccp->qmfbid = parameters->irreversible ? 0 : 1;
  1913. tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
  1914. tccp->numgbits = 2;
  1915. if (i == parameters->roi_compno) {
  1916. tccp->roishift = parameters->roi_shift;
  1917. } else {
  1918. tccp->roishift = 0;
  1919. }
  1920. if(parameters->cp_cinema)
  1921. {
  1922. /*Precinct size for lowest frequency subband=128*/
  1923. tccp->prcw[0] = 7;
  1924. tccp->prch[0] = 7;
  1925. /*Precinct size at all other resolutions = 256*/
  1926. for (j = 1; j < tccp->numresolutions; j++) {
  1927. tccp->prcw[j] = 8;
  1928. tccp->prch[j] = 8;
  1929. }
  1930. }else{
  1931. if (parameters->csty & J2K_CCP_CSTY_PRT) {
  1932. int p = 0;
  1933. for (j = tccp->numresolutions - 1; j >= 0; j--) {
  1934. if (p < parameters->res_spec) {
  1935. if (parameters->prcw_init[p] < 1) {
  1936. tccp->prcw[j] = 1;
  1937. } else {
  1938. tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
  1939. }
  1940. if (parameters->prch_init[p] < 1) {
  1941. tccp->prch[j] = 1;
  1942. }else {
  1943. tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
  1944. }
  1945. } else {
  1946. int res_spec = parameters->res_spec;
  1947. int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
  1948. int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
  1949. if (size_prcw < 1) {
  1950. tccp->prcw[j] = 1;
  1951. } else {
  1952. tccp->prcw[j] = int_floorlog2(size_prcw);
  1953. }
  1954. if (size_prch < 1) {
  1955. tccp->prch[j] = 1;
  1956. } else {
  1957. tccp->prch[j] = int_floorlog2(size_prch);
  1958. }
  1959. }
  1960. p++;
  1961. /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
  1962. } /*end for*/
  1963. } else {
  1964. for (j = 0; j < tccp->numresolutions; j++) {
  1965. tccp->prcw[j] = 15;
  1966. tccp->prch[j] = 15;
  1967. }
  1968. }
  1969. }
  1970. dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
  1971. }
  1972. }
  1973. }
  1974. opj_bool j2k_encode(opj_j2k_t *j2k, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
  1975. int tileno, compno;
  1976. opj_cp_t *cp = NULL;
  1977. opj_tcd_t *tcd = NULL; /* TCD component */
  1978. j2k->cio = cio;
  1979. j2k->image = image;
  1980. cp = j2k->cp;
  1981. /* INDEX >> */
  1982. j2k->cstr_info = cstr_info;
  1983. if (cstr_info) {
  1984. int compno;
  1985. cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
  1986. cstr_info->image_w = image->x1 - image->x0;
  1987. cstr_info->image_h = image->y1 - image->y0;
  1988. cstr_info->prog = (&cp->tcps[0])->prg;
  1989. cstr_info->tw = cp->tw;
  1990. cstr_info->th = cp->th;
  1991. cstr_info->tile_x = cp->tdx; /* new version parser */
  1992. cstr_info->tile_y = cp->tdy; /* new version parser */
  1993. cstr_info->tile_Ox = cp->tx0; /* new version parser */
  1994. cstr_info->tile_Oy = cp->ty0; /* new version parser */
  1995. cstr_info->numcomps = image->numcomps;
  1996. cstr_info->numlayers = (&cp->tcps[0])->numlayers;
  1997. cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
  1998. for (compno=0; compno < image->numcomps; compno++) {
  1999. cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
  2000. }
  2001. cstr_info->D_max = 0.0; /* ADD Marcela */
  2002. cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
  2003. cstr_info->maxmarknum = 100;
  2004. cstr_info->marker = (opj_marker_info_t *) opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
  2005. cstr_info->marknum = 0;
  2006. }
  2007. /* << INDEX */
  2008. j2k_write_soc(j2k);
  2009. j2k_write_siz(j2k);
  2010. j2k_write_cod(j2k);
  2011. j2k_write_qcd(j2k);
  2012. if(cp->cinema){
  2013. for (compno = 1; compno < image->numcomps; compno++) {
  2014. j2k_write_coc(j2k, compno);
  2015. j2k_write_qcc(j2k, compno);
  2016. }
  2017. }
  2018. for (compno = 0; compno < image->numcomps; compno++) {
  2019. opj_tcp_t *tcp = &cp->tcps[0];
  2020. if (tcp->tccps[compno].roishift)
  2021. j2k_write_rgn(j2k, compno, 0);
  2022. }
  2023. if (cp->comment != NULL) {
  2024. j2k_write_com(j2k);
  2025. }
  2026. j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
  2027. /* TLM Marker*/
  2028. if(cp->cinema){
  2029. j2k_write_tlm(j2k);
  2030. if (cp->cinema == CINEMA4K_24) {
  2031. j2k_write_poc(j2k);
  2032. }
  2033. }
  2034. /* uncomment only for testing JPSEC marker writing */
  2035. /* j2k_write_sec(j2k); */
  2036. /* INDEX >> */
  2037. if(cstr_info) {
  2038. cstr_info->main_head_end = cio_tell(cio) - 1;
  2039. }
  2040. /* << INDEX */
  2041. /**** Main Header ENDS here ***/
  2042. /* create the tile encoder */
  2043. tcd = tcd_create(j2k->cinfo);
  2044. /* encode each tile */
  2045. for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  2046. int pino;
  2047. int tilepartno=0;
  2048. /* UniPG>> */
  2049. int acc_pack_num = 0;
  2050. /* <<UniPG */
  2051. opj_tcp_t *tcp = &cp->tcps[tileno];
  2052. opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
  2053. j2k->curtileno = tileno;
  2054. j2k->cur_tp_num = 0;
  2055. tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
  2056. /* initialisation before tile encoding */
  2057. if (tileno == 0) {
  2058. tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
  2059. } else {
  2060. tcd_init_encode(tcd, image, cp, j2k->curtileno);
  2061. }
  2062. /* INDEX >> */
  2063. if(cstr_info) {
  2064. cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
  2065. cstr_info->tile[j2k->curtileno].maxmarknum = 10;
  2066. cstr_info->tile[j2k->curtileno].marker = (opj_marker_info_t *) opj_malloc(cstr_info->tile[j2k->curtileno].maxmarknum * sizeof(opj_marker_info_t));
  2067. cstr_info->tile[j2k->curtileno].marknum = 0;
  2068. }
  2069. /* << INDEX */
  2070. for(pino = 0; pino <= tcp->numpocs; pino++) {
  2071. int tot_num_tp;
  2072. tcd->cur_pino=pino;
  2073. /*Get number of tile parts*/
  2074. tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
  2075. tcd->tp_pos = cp->tp_pos;
  2076. for(tilepartno = 0; tilepartno < tot_num_tp ; tilepartno++){
  2077. j2k->tp_num = tilepartno;
  2078. /* INDEX >> */
  2079. if(cstr_info)
  2080. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
  2081. cio_tell(cio) + j2k->pos_correction;
  2082. /* << INDEX */
  2083. j2k_write_sot(j2k);
  2084. if(j2k->cur_tp_num == 0 && cp->cinema == 0){
  2085. for (compno = 1; compno < image->numcomps; compno++) {
  2086. j2k_write_coc(j2k, compno);
  2087. j2k_write_qcc(j2k, compno);
  2088. }
  2089. if (cp->tcps[tileno].numpocs) {
  2090. j2k_write_poc(j2k);
  2091. }
  2092. }
  2093. /* INDEX >> */
  2094. if(cstr_info)
  2095. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
  2096. cio_tell(cio) + j2k->pos_correction + 1;
  2097. /* << INDEX */
  2098. j2k_write_sod(j2k, tcd);
  2099. /* INDEX >> */
  2100. if(cstr_info) {
  2101. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
  2102. cio_tell(cio) + j2k->pos_correction - 1;
  2103. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
  2104. acc_pack_num;
  2105. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
  2106. cstr_info->packno - acc_pack_num;
  2107. acc_pack_num = cstr_info->packno;
  2108. }
  2109. /* << INDEX */
  2110. j2k->cur_tp_num++;
  2111. }
  2112. }
  2113. if(cstr_info) {
  2114. cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
  2115. }
  2116. /*
  2117. if (tile->PPT) { // BAD PPT !!!
  2118. FILE *PPT_file;
  2119. int i;
  2120. PPT_file=fopen("PPT","rb");
  2121. fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
  2122. for (i=0;i<tile->len_ppt;i++) {
  2123. unsigned char elmt;
  2124. fread(&elmt, 1, 1, PPT_file);
  2125. fwrite(&elmt,1,1,f);
  2126. }
  2127. fclose(PPT_file);
  2128. unlink("PPT");
  2129. }
  2130. */
  2131. }
  2132. /* destroy the tile encoder */
  2133. tcd_free_encode(tcd);
  2134. tcd_destroy(tcd);
  2135. opj_free(j2k->cur_totnum_tp);
  2136. j2k_write_eoc(j2k);
  2137. if(cstr_info) {
  2138. cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
  2139. /* UniPG>> */
  2140. /* The following adjustment is done to adjust the codestream size */
  2141. /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
  2142. /* the first bunch of bytes is not in the codestream */
  2143. cstr_info->codestream_size -= cstr_info->main_head_start;
  2144. /* <<UniPG */
  2145. }
  2146. #ifdef USE_JPWL
  2147. /*
  2148. preparation of JPWL marker segments
  2149. */
  2150. if(cp->epc_on) {
  2151. /* encode according to JPWL */
  2152. jpwl_encode(j2k, cio, image);
  2153. }
  2154. #endif /* USE_JPWL */
  2155. return OPJ_TRUE;
  2156. }
  2157. static void j2k_add_mhmarker(opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
  2158. if (!cstr_info)
  2159. return;
  2160. /* expand the list? */
  2161. if ((cstr_info->marknum + 1) > cstr_info->maxmarknum) {
  2162. cstr_info->maxmarknum = 100 + (int) ((float) cstr_info->maxmarknum * 1.0F);
  2163. cstr_info->marker = (opj_marker_info_t*)opj_realloc(cstr_info->marker, cstr_info->maxmarknum);
  2164. }
  2165. /* add the marker */
  2166. cstr_info->marker[cstr_info->marknum].type = type;
  2167. cstr_info->marker[cstr_info->marknum].pos = pos;
  2168. cstr_info->marker[cstr_info->marknum].len = len;
  2169. cstr_info->marknum++;
  2170. }
  2171. static void j2k_add_tlmarker( int tileno, opj_codestream_info_t *cstr_info, unsigned short int type, int pos, int len) {
  2172. opj_marker_info_t *marker;
  2173. if (!cstr_info)
  2174. return;
  2175. /* expand the list? */
  2176. if ((cstr_info->tile[tileno].marknum + 1) > cstr_info->tile[tileno].maxmarknum) {
  2177. cstr_info->tile[tileno].maxmarknum = 100 + (int) ((float) cstr_info->tile[tileno].maxmarknum * 1.0F);
  2178. cstr_info->tile[tileno].marker = (opj_marker_info_t*)opj_realloc(cstr_info->tile[tileno].marker, cstr_info->maxmarknum);
  2179. }
  2180. marker = &(cstr_info->tile[tileno].marker[cstr_info->tile[tileno].marknum]);
  2181. /* add the marker */
  2182. marker->type = type;
  2183. marker->pos = pos;
  2184. marker->len = len;
  2185. cstr_info->tile[tileno].marknum++;
  2186. }