Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 2001-2003, David Janssens
  3.  * Copyright (c) 2002-2003, Yannick Verschueren
  4.  * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
  5.  * Copyright (c) 2005, Hervé Drolon, FreeImage Team
  6.  * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
  7.  * Copyright (c) 2006, Mónica Díez García, Image Processing Laboratory, University of Valladolid, Spain
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.  * POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. #include "opj_includes.h"
  33.  
  34. /** @defgroup J3D J3D - JPEG-2000 PART 10 codestream reader/writer */
  35. /*@{*/
  36.  
  37. /** @name Funciones locales */
  38. /*@{*/
  39.  
  40. /**
  41. Write the SOC marker (Start Of Codestream)
  42. @param j3d J3D handle
  43. */
  44. static void j3d_write_soc(opj_j3d_t *j3d);
  45. /**
  46. Read the SOC marker (Start of Codestream)
  47. @param j3d J3D handle
  48. */
  49. static void j3d_read_soc(opj_j3d_t *j3d);
  50. /**
  51. Write the SIZ marker (2D volume and tile size)
  52. @param j3d J3D handle
  53. */
  54. static void j3d_write_siz(opj_j3d_t *j3d);
  55. /**
  56. Read the SIZ marker (2D volume and tile size)
  57. @param j3d J3D handle
  58. */
  59. static void j3d_read_siz(opj_j3d_t *j3d);
  60. /**
  61. Write the ZSI marker (3rd volume and tile size)
  62. @param j3d J3D handle
  63. */
  64. static void j3d_write_zsi(opj_j3d_t *j3d);
  65. /**
  66. Read the ZSI marker (3rd volume and tile size)
  67. @param j3d J3D handle
  68. */
  69. static void j3d_read_zsi(opj_j3d_t *j3d);
  70. /**
  71. Write the COM marker (comment)
  72. @param j3d J3D handle
  73. */
  74. static void j3d_write_com(opj_j3d_t *j3d);
  75. /**
  76. Read the COM marker (comment)
  77. @param j3d J3D handle
  78. */
  79. static void j3d_read_com(opj_j3d_t *j3d);
  80. /**
  81. Write the value concerning the specified component in the marker COD and COC
  82. @param j3d J3D handle
  83. @param compno Number of the component concerned by the information written
  84. */
  85. static void j3d_write_cox(opj_j3d_t *j3d, int compno);
  86. /**
  87. Read the value concerning the specified component in the marker COD and COC
  88. @param j3d J3D handle
  89. @param compno Number of the component concerned by the information read
  90. */
  91. static void j3d_read_cox(opj_j3d_t *j3d, int compno);
  92. /**
  93. Write the COD marker (coding style default)
  94. @param j3d J3D handle
  95. */
  96. static void j3d_write_cod(opj_j3d_t *j3d);
  97. /**
  98. Read the COD marker (coding style default)
  99. @param j3d J3D handle
  100. */
  101. static void j3d_read_cod(opj_j3d_t *j3d);
  102. /**
  103. Write the COC marker (coding style component)
  104. @param j3d J3D handle
  105. @param compno Number of the component concerned by the information written
  106. */
  107. static void j3d_write_coc(opj_j3d_t *j3d, int compno);
  108. /**
  109. Read the COC marker (coding style component)
  110. @param j3d J3D handle
  111. */
  112. static void j3d_read_coc(opj_j3d_t *j3d);
  113. /**
  114. Write the value concerning the specified component in the marker QCD and QCC
  115. @param j3d J3D handle
  116. @param compno Number of the component concerned by the information written
  117. */
  118. static void j3d_write_qcx(opj_j3d_t *j3d, int compno);
  119. /**
  120. Read the value concerning the specified component in the marker QCD and QCC
  121. @param j3d J3D handle
  122. @param compno Number of the component concern by the information read
  123. @param len Length of the information in the QCX part of the marker QCD/QCC
  124. */
  125. static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len);
  126. /**
  127. Write the QCD marker (quantization default)
  128. @param j3d J3D handle
  129. */
  130. static void j3d_write_qcd(opj_j3d_t *j3d);
  131. /**
  132. Read the QCD marker (quantization default)
  133. @param j3d J3D handle
  134. */
  135. static void j3d_read_qcd(opj_j3d_t *j3d);
  136. /**
  137. Write the QCC marker (quantization component)
  138. @param j3d J3D handle
  139. @param compno Number of the component concerned by the information written
  140. */
  141. static void j3d_write_qcc(opj_j3d_t *j3d, int compno);
  142. /**
  143. Read the QCC marker (quantization component)
  144. @param j3d J3D handle
  145. */
  146. static void j3d_read_qcc(opj_j3d_t *j3d);
  147. /**
  148. Write the POC marker (progression order change)
  149. @param j3d J3D handle
  150. */
  151. static void j3d_write_poc(opj_j3d_t *j3d);
  152. /**
  153. Read the POC marker (progression order change)
  154. @param j3d J3D handle
  155. */
  156. static void j3d_read_poc(opj_j3d_t *j3d);
  157. /**
  158. Read the CRG marker (component registration)
  159. @param j3d J3D handle
  160. */
  161. static void j3d_read_crg(opj_j3d_t *j3d);
  162. /**
  163. Read the TLM marker (tile-part lengths)
  164. @param j3d J3D handle
  165. */
  166. static void j3d_read_tlm(opj_j3d_t *j3d);
  167. /**
  168. Read the PLM marker (packet length, main header)
  169. @param j3d J3D handle
  170. */
  171. static void j3d_read_plm(opj_j3d_t *j3d);
  172. /**
  173. Read the PLT marker (packet length, tile-part header)
  174. @param j3d J3D handle
  175. */
  176. static void j3d_read_plt(opj_j3d_t *j3d);
  177. /**
  178. Read the PPM marker (packet packet headers, main header)
  179. @param j3d J3D handle
  180. */
  181. static void j3d_read_ppm(opj_j3d_t *j3d);
  182. /**
  183. Read the PPT marker (packet packet headers, tile-part header)
  184. @param j3d J3D handle
  185. */
  186. static void j3d_read_ppt(opj_j3d_t *j3d);
  187. /**
  188. Write the SOT marker (start of tile-part)
  189. @param j3d J3D handle
  190. */
  191. static void j3d_write_sot(opj_j3d_t *j3d);
  192. /**
  193. Read the SOT marker (start of tile-part)
  194. @param j3d J3D handle
  195. */
  196. static void j3d_read_sot(opj_j3d_t *j3d);
  197. /**
  198. Write the SOD marker (start of data)
  199. @param j3d J3D handle
  200. @param tile_coder Pointer to a TCD handle
  201. */
  202. static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder);
  203. /**
  204. Read the SOD marker (start of data)
  205. @param j3d J3D handle
  206. */
  207. static void j3d_read_sod(opj_j3d_t *j3d);
  208. /**
  209. Write the RGN marker (region-of-interest)
  210. @param j3d J3D handle
  211. @param compno Number of the component concerned by the information written
  212. @param tileno Number of the tile concerned by the information written
  213. */
  214. static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno);
  215. /**
  216. Read the RGN marker (region-of-interest)
  217. @param j3d J3D handle
  218. */
  219. static void j3d_read_rgn(opj_j3d_t *j3d);
  220. /**
  221. Write the EOC marker (end of codestream)
  222. @param j3d J3D handle
  223. */
  224. static void j3d_write_eoc(opj_j3d_t *j3d);
  225. /**
  226. Read the EOC marker (end of codestream)
  227. @param j3d J3D handle
  228. */
  229. static void j3d_read_eoc(opj_j3d_t *j3d);
  230. /**
  231. Read an unknown marker
  232. @param j3d J3D handle
  233. */
  234. static void j3d_read_unk(opj_j3d_t *j3d);
  235. /**
  236. Write the CAP marker (extended capabilities)
  237. @param j3d J3D handle
  238. */
  239. static void j3d_write_cap(opj_j3d_t *j3d);
  240. /**
  241. Read the CAP marker (extended capabilities)
  242. @param j3d J3D handle
  243. */
  244. static void j3d_read_cap(opj_j3d_t *j3d);
  245. /**
  246. Write the DCO marker (Variable DC offset)
  247. @param j3d J3D handle
  248. */
  249. static void j3d_write_dco(opj_j3d_t *j3d);
  250. /**
  251. Read the DCO marker (Variable DC offset)
  252. @param j3d J3D handle
  253. */
  254. static void j3d_read_dco(opj_j3d_t *j3d);
  255. /**
  256. Write the ATK marker (arbitrary transformation kernel)
  257. @param j3d J3D handle
  258. */
  259. static void j3d_write_atk(opj_j3d_t *j3d);
  260. /**
  261. Read the ATK marker (arbitrary transformation kernel)
  262. @param j3d J3D handle
  263. */
  264. static void j3d_read_atk(opj_j3d_t *j3d);
  265. /**
  266. Write the CBD marker (component bit depth definition)
  267. @param j3d J3D handle
  268. */
  269. static void j3d_write_cbd(opj_j3d_t *j3d);
  270. /**
  271. Read the CBD marker (component bit depth definition)
  272. @param j3d J3D handle
  273. */
  274. static void j3d_read_cbd(opj_j3d_t *j3d);
  275. /**
  276. Write the MCT marker (multiple component transfomation definition)
  277. @param j3d J3D handle
  278. */
  279. static void j3d_write_mct(opj_j3d_t *j3d);
  280. /**
  281. Read the MCT marker (multiple component transfomation definition)
  282. @param j3d J3D handle
  283. */
  284. static void j3d_read_mct(opj_j3d_t *j3d);
  285. /**
  286. Write the MCC marker (multiple component transfomation collection)
  287. @param j3d J3D handle
  288. */
  289. static void j3d_write_mcc(opj_j3d_t *j3d);
  290. /**
  291. Read the MCC marker (multiple component transfomation collection)
  292. @param j3d J3D handle
  293. */
  294. static void j3d_read_mcc(opj_j3d_t *j3d);
  295. /**
  296. Write the MCO marker (multiple component transfomation ordering)
  297. @param j3d J3D handle
  298. */
  299. static void j3d_write_mco(opj_j3d_t *j3d);
  300. /**
  301. Read the MCO marker (multiple component transfomation ordering)
  302. @param j3d J3D handle
  303. */
  304. static void j3d_read_mco(opj_j3d_t *j3d);
  305. /**
  306. Write the NLT marker (non-linearity point transformation)
  307. @param j3d J3D handle
  308. */
  309. static void j3d_write_nlt(opj_j3d_t *j3d);
  310. /**
  311. Read the NLT marker (non-linearity point transformation)
  312. @param j3d J3D handle
  313. */
  314. static void j3d_read_nlt(opj_j3d_t *j3d);
  315. /*@}*/
  316.  
  317. /* ----------------------------------------------------------------------- */
  318.  
  319. void j3d_dump_volume(FILE *fd, opj_volume_t * vol) {
  320.         int compno;
  321.         fprintf(fd, "volume {\n");
  322.         fprintf(fd, "  x0=%d, y0=%d, z0=%d, x1=%d, y1=%d, z1=%d\n", vol->x0, vol->y0, vol->z0,vol->x1, vol->y1,  vol->z1);
  323.         fprintf(fd, "  numcomps=%d\n", vol->numcomps);
  324.         for (compno = 0; compno < vol->numcomps; compno++) {
  325.                 opj_volume_comp_t *comp = &vol->comps[compno];
  326.                 fprintf(fd, "  comp %d {\n", compno);
  327.                 fprintf(fd, "    dx=%d, dy=%d, dz=%d\n", comp->dx, comp->dy, comp->dz);
  328.                 fprintf(fd, "    prec=%d\n", comp->prec);
  329.                 fprintf(fd, "    sgnd=%d\n", comp->sgnd);
  330.                 fprintf(fd, "  }\n");
  331.         }
  332.         fprintf(fd, "}\n");
  333. }
  334.  
  335. void j3d_dump_cp(FILE *fd, opj_volume_t * vol, opj_cp_t * cp) {
  336.         int tileno, compno, layno, bandno, resno, numbands;
  337.         fprintf(fd, "coding parameters {\n");
  338.         fprintf(fd, "  tx0=%d, ty0=%d, tz0=%d\n", cp->tx0, cp->ty0, cp->tz0);
  339.         fprintf(fd, "  tdx=%d, tdy=%d, tdz=%d\n", cp->tdx, cp->tdy, cp->tdz);
  340.         fprintf(fd, "  tw=%d, th=%d, tl=%d\n", cp->tw, cp->th, cp->tl);
  341.         fprintf(fd, "  transform format: %d\n", cp->transform_format);
  342.         fprintf(fd, "  encoding format: %d\n", cp->encoding_format);
  343.         for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
  344.                 opj_tcp_t *tcp = &cp->tcps[tileno];
  345.                 fprintf(fd, "  tile %d {\n", tileno);
  346.                 fprintf(fd, "    csty=%x\n", tcp->csty);
  347.                 fprintf(fd, "    prg=%d\n", tcp->prg);
  348.                 fprintf(fd, "    numlayers=%d\n", tcp->numlayers);
  349.                 fprintf(fd, "    mct=%d\n", tcp->mct);
  350.                 fprintf(fd, "    rates=");
  351.                 for (layno = 0; layno < tcp->numlayers; layno++) {
  352.                         fprintf(fd, "%f ", tcp->rates[layno]);
  353.                 }
  354.                 fprintf(fd, "\n");
  355.                 fprintf(fd, "    first=%d\n", tcp->first);
  356.                 for (compno = 0; compno < vol->numcomps; compno++) {
  357.                         opj_tccp_t *tccp = &tcp->tccps[compno];
  358.                         fprintf(fd, "    comp %d {\n", compno);
  359.                         fprintf(fd, "      csty=%x\n", tccp->csty);
  360.                         fprintf(fd, "      numresx=%d, numresy=%d, numresz=%d\n", tccp->numresolution[0], tccp->numresolution[1], tccp->numresolution[2]);
  361.                         fprintf(fd, "      cblkw=%d, cblkh=%d, cblkl=%d\n", tccp->cblk[0], tccp->cblk[1], tccp->cblk[2]);
  362.                         fprintf(fd, "      cblksty=%x\n", tccp->cblksty);
  363.                         fprintf(fd, "      qntsty=%d\n", tccp->qntsty);
  364.                         fprintf(fd, "      numgbits=%d\n", tccp->numgbits);
  365.                         fprintf(fd, "      roishift=%d\n", tccp->roishift);
  366.                         fprintf(fd, "      reversible=%d\n", tccp->reversible);
  367.                         fprintf(fd, "      dwtidx=%d dwtidy=%d dwtidz=%d\n", tccp->dwtid[0], tccp->dwtid[1], tccp->dwtid[2]);
  368.                         if (tccp->atk != NULL) {
  369.                 fprintf(fd, "      atk.index=%d\n", tccp->atk->index);
  370.                                 fprintf(fd, "      atk.coeff_typ=%d\n", tccp->atk->coeff_typ);
  371.                                 fprintf(fd, "      atk.filt_cat=%d\n", tccp->atk->filt_cat);
  372.                                 fprintf(fd, "      atk.exten=%d\n", tccp->atk->exten);
  373.                                 fprintf(fd, "      atk.minit=%d\n", tccp->atk->minit);
  374.                                 fprintf(fd, "      atk.wt_typ=%d\n", tccp->atk->wt_typ);
  375.                         }
  376.                         fprintf(fd, "      stepsizes of bands=");
  377.             numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 :
  378.                         ( (cp->transform_format == TRF_2D_DWT) ? (tccp->numresolution[0] * 3 - 2) :
  379.                                 (tccp->numresolution[0] * 7 - 6) - 4 *(tccp->numresolution[0] - tccp->numresolution[2]) );
  380.                         for (bandno = 0; bandno < numbands; bandno++) {
  381.                                 fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,tccp->stepsizes[bandno].expn);
  382.                         }
  383.                         fprintf(fd, "\n");
  384.                        
  385.                         if (tccp->csty & J3D_CCP_CSTY_PRT) {
  386.                                 fprintf(fd, "      prcw=");
  387.                                 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
  388.                                         fprintf(fd, "%d ", tccp->prctsiz[0][resno]);
  389.                                 }
  390.                                 fprintf(fd, "\n");
  391.                                 fprintf(fd, "      prch=");
  392.                                 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
  393.                                         fprintf(fd, "%d ", tccp->prctsiz[1][resno]);
  394.                                 }
  395.                                 fprintf(fd, "\n");
  396.                                 fprintf(fd, "      prcl=");
  397.                                 for (resno = 0; resno < tccp->numresolution[0]; resno++) {
  398.                                         fprintf(fd, "%d ", tccp->prctsiz[2][resno]);
  399.                                 }
  400.                                 fprintf(fd, "\n");
  401.                         }
  402.                         fprintf(fd, "    }\n");
  403.                 }
  404.                 fprintf(fd, "  }\n");
  405.         }
  406.         fprintf(fd, "}\n");
  407. }
  408.  
  409. /* -----------------------------------------------------------------------
  410. Extended capabilities
  411. ------------------------------------------------------------------------*/
  412.  
  413. static void j3d_write_cap(opj_j3d_t *j3d){
  414.         int len,lenp;
  415.  
  416.         opj_cio_t *cio = j3d->cio;
  417.         cio_write(cio, J3D_MS_CAP, 2);  /* CAP */
  418.         lenp = cio_tell(cio);
  419.         cio_skip(cio, 2);
  420.         cio_write(cio,J3D_CAP_10, 4);
  421.         len = cio_tell(cio) - lenp;
  422.         cio_seek(cio, lenp);
  423.         cio_write(cio, len, 2);         /* Lsiz */
  424.         cio_seek(cio, lenp + len);
  425.  
  426. }
  427. static void j3d_read_cap(opj_j3d_t *j3d){
  428.         int len, Cap;
  429.         opj_cio_t *cio = j3d->cio;
  430.         /*cio_read(cio, 2);      CAP */
  431.         len = cio_read(cio, 2);
  432.         Cap = cio_read(cio, 4);
  433. }
  434. static void j3d_write_zsi(opj_j3d_t *j3d) {
  435.         int i;
  436.         int lenp, len;
  437.  
  438.         opj_cio_t *cio = j3d->cio;
  439.         opj_volume_t *volume = j3d->volume;
  440.         opj_cp_t *cp = j3d->cp;
  441.        
  442.         cio_write(cio, J3D_MS_ZSI, 2);  /* ZSI */
  443.         lenp = cio_tell(cio);
  444.         cio_skip(cio, 2);
  445.         cio_write(cio, volume->z1, 4);  /* Zsiz */
  446.         cio_write(cio, volume->z0, 4);  /* Z0siz */
  447.         cio_write(cio, cp->tdz, 4);             /* ZTsiz */
  448.         cio_write(cio, cp->tz0, 4);             /* ZT0siz */
  449.         for (i = 0; i < volume->numcomps; i++) {
  450.                 cio_write(cio, volume->comps[i].dz, 1); /* ZRsiz_i */
  451.         }
  452.         len = cio_tell(cio) - lenp;
  453.         cio_seek(cio, lenp);
  454.         cio_write(cio, len, 2);         /* Lsiz */
  455.         cio_seek(cio, lenp + len);
  456. }
  457.  
  458. static void j3d_read_zsi(opj_j3d_t *j3d) {
  459.         int len, i;
  460.        
  461.         opj_cio_t *cio = j3d->cio;
  462.         opj_volume_t *volume = j3d->volume;
  463.         opj_cp_t *cp = j3d->cp;
  464.        
  465.         len = cio_read(cio, 2);                 /* Lsiz */
  466.         volume->z1 = cio_read(cio, 4);  /* Zsiz */
  467.         volume->z0 = cio_read(cio, 4);  /* Z0siz */
  468.         cp->tdz = cio_read(cio, 4);             /* ZTsiz */
  469.         cp->tz0 = cio_read(cio, 4);             /* ZT0siz */
  470.         for (i = 0; i < volume->numcomps; i++) {
  471.                 volume->comps[i].dz = cio_read(cio, 1); /* ZRsiz_i */
  472.         }
  473.        
  474.         //Initialization of volume
  475.         cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
  476.         cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
  477.         cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
  478.         cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
  479.         cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
  480.         cp->tileno_size = 0;
  481.        
  482.         for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
  483.                 cp->tcps[i].POC = 0;
  484.                 cp->tcps[i].numpocs = 0;
  485.                 cp->tcps[i].first = 1;
  486.         }
  487.        
  488.         /* Initialization for PPM marker (Packets header)*/
  489.         cp->ppm = 0;
  490.         cp->ppm_data = NULL;
  491.         cp->ppm_data_first = NULL;
  492.         cp->ppm_previous = 0;
  493.         cp->ppm_store = 0;
  494.        
  495.         j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
  496.         for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
  497.                 cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
  498.         }
  499.         j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
  500.         j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
  501.         j3d->state = J3D_STATE_MH;
  502.        
  503. }
  504. static void j3d_write_dco(opj_j3d_t *j3d){
  505.         int lenp, len, i;
  506.         int dcotype;   
  507.  
  508.         opj_cio_t *cio = j3d->cio;
  509.         opj_volume_t *volume = j3d->volume;
  510.         opj_cp_t *cp = j3d->cp;
  511.        
  512.         dcotype = 1; /* Offsets are 16bit signed integers Table A21 15444-2 */
  513.         cio_write(cio, J3D_MS_DCO, 2);  /* DCO */
  514.         lenp = cio_tell(cio);
  515.         cio_skip(cio, 2);
  516.         cio_write(cio, dcotype, 1);    
  517.         if (dcotype == 0) {
  518.                 for (i = 0; i < volume->numcomps; i++)
  519.                         cio_write(cio, volume->comps[i].dcoffset, 1);   /* SPdco_i */
  520.         } else if (dcotype == 1) {
  521.                 for (i = 0; i < volume->numcomps; i++){
  522.                         cio_write(cio, volume->comps[i].dcoffset, 1);   /* SPdco_i */
  523.                         opj_event_msg(j3d->cinfo, EVT_INFO, "dcotype %d DCO %d \n",dcotype,volume->comps[i].dcoffset);
  524.                 }
  525.         }
  526.         len = cio_tell(cio) - lenp;
  527.         cio_seek(cio, lenp);
  528.         cio_write(cio, len, 2);         /* Ldco */
  529.         cio_seek(cio, lenp + len);
  530.  
  531. }
  532. static void j3d_read_dco(opj_j3d_t *j3d){
  533.         int len, i;
  534.         int dcotype;
  535.  
  536.         opj_cio_t *cio = j3d->cio;
  537.         opj_volume_t *volume = j3d->volume;
  538.         opj_cp_t *cp = j3d->cp;
  539.        
  540.         len = cio_read(cio, 2);                 /* Lsiz */
  541.         dcotype = cio_read(cio, 1); //offset 8bit unsigned / 16bit signed integers
  542.         if (dcotype == 0) {
  543.                 for (i = 0; i < volume->numcomps; i++) {
  544.                         volume->comps[i].dcoffset = cio_read(cio, 1);
  545.                         if (volume->comps[i].dcoffset > 128)
  546.                                 volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
  547.                 }
  548.         } else if (dcotype == 1) {
  549.                 for (i = 0; i < volume->numcomps; i++) {
  550.                         volume->comps[i].dcoffset = cio_read(cio, 1);
  551.                         if (volume->comps[i].dcoffset > 128)
  552.                                 volume->comps[i].dcoffset = volume->comps[i].dcoffset - 256;
  553.                 }
  554.         }
  555.        
  556. }
  557. static void j3d_write_atk(opj_j3d_t *j3d){
  558.         int lenp, len, s, k;
  559.  
  560.         opj_cio_t *cio = j3d->cio;
  561.         opj_volume_t *volume = j3d->volume;
  562.         opj_atk_t *atk = j3d->cp->tcps->tccps->atk;
  563.        
  564.         cio_write(cio, J3D_MS_ATK, 2);  /* ATK */
  565.         lenp = cio_tell(cio);
  566.         cio_skip(cio, 2);                              
  567.         cio_write(cio, atk->index + (atk->coeff_typ << 8) + (atk->filt_cat << 11)
  568.                 + (atk->wt_typ << 12) + (atk->minit << 13) + (atk->exten << 14), 2);                    /* Satk */
  569.     if (atk->wt_typ == J3D_ATK_IRR)
  570.                 cio_write(cio,(unsigned int) (atk->Katk * 8192.0), 1 << atk->coeff_typ);
  571.         cio_write(cio, atk->Natk, 1);
  572.         for (s = 0; s < atk->Natk; s++){
  573.                 if (atk->filt_cat == J3D_ATK_ARB)
  574.                         cio_write(cio, atk->Oatk[s], 1);
  575.                 if (atk->wt_typ == J3D_ATK_REV){
  576.                         cio_write(cio, atk->Eatk[s], 1);
  577.                         cio_write(cio, atk->Batk[s], 1);
  578.                 }
  579.                 cio_write(cio, atk->LCatk[s], 1);
  580.                 for (k = 0; k < atk->LCatk[s]; k++)
  581.                         cio_write(cio,(unsigned int) (atk->Aatk[s][k] * 8192.0), 1 << atk->coeff_typ);
  582.         }
  583.         len = cio_tell(cio) - lenp;
  584.         cio_seek(cio, lenp);
  585.         cio_write(cio, len, 2);         /* Latk */
  586.         cio_seek(cio, lenp + len);
  587. }
  588. static void j3d_read_atk(opj_j3d_t *j3d){
  589.         int len, i, Satk, k;
  590.        
  591.         opj_cio_t *cio = j3d->cio;
  592.         opj_volume_t *volume = j3d->volume;
  593.         opj_cp_t *cp = j3d->cp;
  594.         opj_atk_t *atk = cp->tcps->tccps->atk;
  595.        
  596.         len = cio_read(cio, 2);                 /* Latk */
  597.         Satk = cio_read(cio, 2);
  598.         atk->index = Satk & 0x00ff;
  599.         atk->coeff_typ = Satk >> 8 & 0x0007;
  600.         atk->filt_cat = Satk >> 11 & 0x0001;
  601.         atk->wt_typ = Satk >> 12  & 0x0001;
  602.         atk->minit = Satk >> 13 & 0x0001;
  603.         atk->exten = Satk >> 14 & 0x0001;
  604.     if (atk->wt_typ == J3D_ATK_IRR)
  605.                 atk->Katk = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
  606.         atk->Natk = cio_read(cio, 1);
  607.         for (i = 0; i < atk->Natk; i++) {
  608.                 if (atk->filt_cat == J3D_ATK_ARB)
  609.                         atk->Oatk[i] = cio_read(cio, 1);
  610.                 if (atk->wt_typ == J3D_ATK_REV){
  611.                         atk->Eatk[i] = cio_read(cio, 1);
  612.                         atk->Batk[i] = cio_read(cio, 1);
  613.                 }
  614.                 atk->LCatk[i] = cio_read(cio, 1);
  615.                 for (k = 0; k < atk->LCatk[i]; k++)
  616.                         atk->Aatk[i][k] = ((double) cio_read(cio, 1 << atk->coeff_typ) / 8192.0);
  617.         }
  618. }
  619. static void j3d_write_cbd(opj_j3d_t *j3d){
  620. }
  621. static void j3d_read_cbd(opj_j3d_t *j3d){
  622. }
  623. static void j3d_write_mct(opj_j3d_t *j3d){
  624. }
  625. static void j3d_read_mct(opj_j3d_t *j3d){
  626. }
  627. static void j3d_write_mcc(opj_j3d_t *j3d){
  628. }
  629. static void j3d_read_mcc(opj_j3d_t *j3d){
  630. }
  631. static void j3d_write_mco(opj_j3d_t *j3d){
  632. }
  633. static void j3d_read_mco(opj_j3d_t *j3d){
  634. }
  635. static void j3d_write_nlt(opj_j3d_t *j3d){
  636. }
  637. static void j3d_read_nlt(opj_j3d_t *j3d){
  638. }
  639. /* -----------------------------------------------------------------------
  640. 15444-1 codestream syntax
  641. ------------------------------------------------------------------------*/
  642. static void j3d_write_soc(opj_j3d_t *j3d) {
  643.         opj_cio_t *cio = j3d->cio;
  644.         cio_write(cio, J3D_MS_SOC, 2);
  645. }
  646.  
  647. static void j3d_read_soc(opj_j3d_t *j3d) {
  648.         j3d->state = J3D_STATE_MHSIZ;
  649. }
  650.  
  651. static void j3d_write_siz(opj_j3d_t *j3d) {
  652.         int i;
  653.         int lenp, len;
  654.         int Rsiz;
  655.  
  656.         opj_cio_t *cio = j3d->cio;
  657.         opj_volume_t *volume = j3d->volume;
  658.         opj_cp_t *cp = j3d->cp;
  659.        
  660.         cio_write(cio, J3D_MS_SIZ, 2);  /* SIZ */
  661.         lenp = cio_tell(cio);
  662.         cio_skip(cio, 2);
  663.         //cio_write(cio, 0, 2);                 /* Rsiz (capabilities of 15444-1 only) */
  664.         Rsiz = J3D_RSIZ_DCO | J3D_RSIZ_ATK; /** | J3D_RSIZ_MCT | J3D_RSIZ_NONLT (not implemented yet)*/
  665.         cio_write(cio, Rsiz, 2); /* capabilities of WDv5.2*/
  666.         cio_write(cio, volume->x1, 4);  /* Xsiz */
  667.         cio_write(cio, volume->y1, 4);  /* Ysiz */
  668.         cio_write(cio, volume->x0, 4);  /* X0siz */
  669.         cio_write(cio, volume->y0, 4);  /* Y0siz */
  670.         cio_write(cio, cp->tdx, 4);             /* XTsiz */
  671.         cio_write(cio, cp->tdy, 4);             /* YTsiz */
  672.         cio_write(cio, cp->tx0, 4);             /* XT0siz */
  673.         cio_write(cio, cp->ty0, 4);             /* YT0siz */
  674.         cio_write(cio, volume->numcomps, 2);    /* Csiz */
  675.         for (i = 0; i < volume->numcomps; i++) {
  676.                 cio_write(cio, volume->comps[i].prec - 1 + (volume->comps[i].sgnd << 7), 1);    /* Ssiz_i */
  677.                 cio_write(cio, volume->comps[i].dx, 1); /* XRsiz_i */
  678.                 cio_write(cio, volume->comps[i].dy, 1); /* YRsiz_i */
  679.         }
  680.         len = cio_tell(cio) - lenp;
  681.         cio_seek(cio, lenp);
  682.         cio_write(cio, len, 2);         /* Lsiz */
  683.         cio_seek(cio, lenp + len);
  684. }
  685.  
  686. static void j3d_read_siz(opj_j3d_t *j3d) {
  687.         int len, i;
  688.        
  689.         opj_cio_t *cio = j3d->cio;
  690.         opj_volume_t *volume = j3d->volume;
  691.         opj_cp_t *cp = j3d->cp;
  692.        
  693.         len = cio_read(cio, 2);                 /* Lsiz */
  694.         cp->rsiz = cio_read(cio, 2);    /* Rsiz (capabilities) */
  695.         volume->x1 = cio_read(cio, 4);  /* Xsiz */
  696.         volume->y1 = cio_read(cio, 4);  /* Ysiz */
  697.         volume->x0 = cio_read(cio, 4);  /* X0siz */
  698.         volume->y0 = cio_read(cio, 4);  /* Y0siz */
  699.         cp->tdx = cio_read(cio, 4);             /* XTsiz */
  700.         cp->tdy = cio_read(cio, 4);             /* YTsiz */
  701.         cp->tx0 = cio_read(cio, 4);             /* XT0siz */
  702.         cp->ty0 = cio_read(cio, 4);             /* YT0siz */
  703.        
  704.         volume->numcomps = cio_read(cio, 2);    /* Csiz */
  705.         volume->comps = (opj_volume_comp_t *) opj_malloc(volume->numcomps * sizeof(opj_volume_comp_t));
  706.         for (i = 0; i < volume->numcomps; i++) {
  707.                 int tmp, j;
  708.                 tmp = cio_read(cio, 1);         /* Ssiz_i */
  709.                 volume->comps[i].prec = (tmp & 0x7f) + 1;
  710.                 volume->comps[i].sgnd = tmp >> 7;
  711.                 volume->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
  712.                 volume->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
  713.                 for (j = 0; j < 3; j++) {
  714.                         volume->comps[i].resno_decoded[j] = 0;          /* number of resolution decoded */
  715.                         volume->comps[i].factor[j] = 0;         /* reducing factor per component */
  716.                 }
  717.         }
  718.  
  719.         if (j3d->cinfo->codec_format == CODEC_J2K){
  720.                 volume->z1 = 1;
  721.                 volume->z0 = 0;
  722.                 volume->numslices = 1;
  723.                 cp->tdz = 1;
  724.                 cp->tz0 = 0;
  725.                 for (i = 0; i < volume->numcomps; i++)
  726.                         volume->comps[i].dz = 1;
  727.  
  728.                 //Initialization of volume
  729.                 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
  730.                 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
  731.                 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
  732.                 cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
  733.                 cp->tileno = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
  734.                 cp->tileno_size = 0;
  735.                
  736.                 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
  737.                         cp->tcps[i].POC = 0;
  738.                         cp->tcps[i].numpocs = 0;
  739.                         cp->tcps[i].first = 1;
  740.                 }
  741.                
  742.                 /* Initialization for PPM marker (Packets header)*/
  743.                 cp->ppm = 0;
  744.                 cp->ppm_data = NULL;
  745.                 cp->ppm_data_first = NULL;
  746.                 cp->ppm_previous = 0;
  747.                 cp->ppm_store = 0;
  748.                
  749.                 j3d->default_tcp->tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
  750.                 for (i = 0; i < cp->tw * cp->th * cp->tl ; i++) {
  751.                         cp->tcps[i].tccps = (opj_tccp_t *) opj_malloc(sizeof(opj_tccp_t) * volume->numcomps);
  752.                 }
  753.                 j3d->tile_data = (unsigned char **) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(unsigned char *));
  754.                 j3d->tile_len = (int *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(int));
  755.                 j3d->state = J3D_STATE_MH;
  756.         }
  757. }
  758.  
  759.  
  760.  
  761. static void j3d_write_com(opj_j3d_t *j3d) {
  762.         unsigned int i;
  763.         int lenp, len;
  764.  
  765.         opj_cio_t *cio = j3d->cio;
  766.  
  767.         cio_write(cio, J3D_MS_COM, 2);
  768.         lenp = cio_tell(cio);
  769.         cio_skip(cio, 2);
  770.         //cio_write(cio, 0, 2);
  771.         cio_write(cio, j3d->cp->transform_format,1);
  772.         cio_write(cio, j3d->cp->encoding_format,1);
  773.         //opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);
  774.         if (j3d->cp->comment != NULL) {
  775.                 char *comment = j3d->cp->comment;
  776.                 for (i = 0; i < strlen(comment); i++) {
  777.             cio_write(cio, comment[i], 1);
  778.                 }
  779.         }
  780.         len = cio_tell(cio) - lenp;
  781.         cio_seek(cio, lenp);
  782.         cio_write(cio, len, 2);
  783.         cio_seek(cio, lenp + len);
  784. }
  785.  
  786. static void j3d_read_com(opj_j3d_t *j3d) {
  787.         int len;
  788.         opj_cio_t *cio = j3d->cio;
  789.  
  790.         len = cio_read(cio, 2);
  791.        
  792.         j3d->cp->transform_format = (OPJ_TRANSFORM) cio_read(cio, 1);
  793.         j3d->cp->encoding_format = (OPJ_ENTROPY_CODING) cio_read(cio, 1);
  794.         //opj_event_msg(j3d->cinfo, EVT_INFO, "TRF %D ENCOD %d\n",j3d->cp->transform_format,j3d->cp->encoding_format);
  795.  
  796.         cio_skip(cio, len - 4);  //posible comments
  797. }
  798.  
  799. static void j3d_write_cox(opj_j3d_t *j3d, int compno) {
  800.         int i;
  801.  
  802.         opj_cp_t *cp = j3d->cp;
  803.         opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
  804.         opj_tccp_t *tccp = &tcp->tccps[compno];
  805.         opj_cio_t *cio = j3d->cio;
  806.        
  807.         cio_write(cio, tccp->numresolution[0] - 1, 1);  /* SPcox (D) No of decomposition levels in x-axis*/
  808.         if (j3d->cinfo->codec_format == CODEC_J3D) {
  809.                 cio_write(cio, tccp->numresolution[1] - 1, 1);  /* SPcox (E) No of decomposition levels in y-axis*/
  810.                 cio_write(cio, tccp->numresolution[2] - 1, 1);  /* SPcox (F) No of decomposition levels in z-axis*/
  811.         }
  812.         /* (cblkw - 2) + (cblkh - 2) + (cblkl - 2) <= 18*/
  813.         cio_write(cio, tccp->cblk[0] - 2, 1);                           /* SPcox (G) Cblk width entre 10 y 2 (8 y 0)*/
  814.         cio_write(cio, tccp->cblk[1] - 2, 1);                           /* SPcox (H) Cblk height*/
  815.         if (j3d->cinfo->codec_format == CODEC_J3D) {
  816.                 cio_write(cio, tccp->cblk[2] - 2, 1);                   /* SPcox (I) Cblk depth*/
  817.         }
  818.         cio_write(cio, tccp->cblksty, 1);                               /* SPcox (J) Cblk style*/
  819.         cio_write(cio, tccp->dwtid[0], 1);                              /* SPcox (K) WT in x-axis 15444-2 Table A10*/
  820.         if (j3d->cinfo->codec_format == CODEC_J3D) {
  821.                 cio_write(cio, tccp->dwtid[1], 1);                              /* SPcox (L) WT in y-axis 15444-2 Table A10*/
  822.                 cio_write(cio, tccp->dwtid[2], 1);                              /* SPcox (M) WT in z-axis 15444-2 Table A10*/
  823.         }
  824.        
  825.         if (tccp->csty & J3D_CCP_CSTY_PRT) {
  826.                 for (i = 0; i < tccp->numresolution[0]; i++) {
  827.                         if (i < tccp->numresolution[2])
  828.                 cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4) + (tccp->prctsiz[2][i] << 8), 2);       /* SPcox (N_i) Table A9*/
  829.                         else
  830.                                 if (j3d->cinfo->codec_format == CODEC_J3D)
  831.                     cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 2);        /* SPcox (N_i) Table A9*/
  832.                                 else
  833.                     cio_write(cio, tccp->prctsiz[0][i] + (tccp->prctsiz[1][i] << 4), 1);        /* SPcox (N_i) Table A9*/               }
  834.         }
  835. }
  836.  
  837. static void j3d_read_cox(opj_j3d_t *j3d, int compno) {
  838.         int i;
  839.  
  840.         opj_cp_t *cp = j3d->cp;
  841.         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
  842.         opj_tccp_t *tccp = &tcp->tccps[compno];
  843.         opj_cio_t *cio = j3d->cio;
  844.  
  845.         tccp->numresolution[0] = cio_read(cio, 1) + 1;  /* SPcox (D) No of decomposition levels in x-axis*/
  846.         if (j3d->cinfo->codec_format == CODEC_J3D) {
  847.                 tccp->numresolution[1] = cio_read(cio, 1) + 1;  /* SPcox (E) No of decomposition levels in y-axis*/
  848.                 tccp->numresolution[2] = cio_read(cio, 1) + 1;  /* SPcox (F) No of decomposition levels in z-axis*/
  849.         }else if (j3d->cinfo->codec_format == CODEC_J2K) {
  850.                 tccp->numresolution[1] = tccp->numresolution[0];       
  851.                 tccp->numresolution[2] = 1;                                                    
  852.         }
  853.         /* check the reduce value */
  854.         cp->reduce[0] = int_min((tccp->numresolution[0])-1, cp->reduce[0]);
  855.         cp->reduce[1] = int_min((tccp->numresolution[1])-1, cp->reduce[1]);
  856.         cp->reduce[2] = int_min((tccp->numresolution[2])-1, cp->reduce[2]);
  857.        
  858.         tccp->cblk[0] = cio_read(cio, 1) + 2;   /* SPcox (G) */
  859.         tccp->cblk[1] = cio_read(cio, 1) + 2;   /* SPcox (H) */
  860.         if (j3d->cinfo->codec_format == CODEC_J3D)
  861.                 tccp->cblk[2] = cio_read(cio, 1) + 2;   /* SPcox (I) */
  862.         else
  863.                 tccp->cblk[2] = tccp->cblk[0];
  864.  
  865.         tccp->cblksty = cio_read(cio, 1);       /* SPcox (J) */
  866.         tccp->dwtid[0] = cio_read(cio, 1);      /* SPcox (K) */
  867.         if (j3d->cinfo->codec_format == CODEC_J3D) {
  868.                 tccp->dwtid[1] = cio_read(cio, 1);      /* SPcox (L) */
  869.                 tccp->dwtid[2] = cio_read(cio, 1);      /* SPcox (M) */
  870.         }else{
  871.                 tccp->dwtid[1] = tccp->dwtid[0];        /* SPcox (L) */
  872.                 tccp->dwtid[2] = tccp->dwtid[0];        /* SPcox (M) */
  873.         }
  874.         tccp->reversible = (tccp->dwtid[0]>=1 && tccp->dwtid[1]>=1 && tccp->dwtid[2]>=1); //TODO: only valid for irreversible 9x7 WTs
  875.         if (tccp->csty & J3D_CP_CSTY_PRT) {
  876.                 for (i = 0; i < tccp->numresolution[0]; i++) {
  877.                         int tmp = cio_read(cio, 2);     /* SPcox (N_i) */
  878.                         tccp->prctsiz[0][i] = tmp & 0xf;
  879.                         tccp->prctsiz[1][i] = tmp >> 4;
  880.                         tccp->prctsiz[2][i] = tmp >> 8;
  881.                 }
  882.         }
  883. }
  884.  
  885. static void j3d_write_cod(opj_j3d_t *j3d) {
  886.         opj_cp_t *cp = NULL;
  887.         opj_tcp_t *tcp = NULL;
  888.         int lenp, len;
  889.  
  890.         opj_cio_t *cio = j3d->cio;
  891.        
  892.         cio_write(cio, J3D_MS_COD, 2);  /* COD */
  893.        
  894.         lenp = cio_tell(cio);
  895.         cio_skip(cio, 2);
  896.        
  897.         cp = j3d->cp;
  898.         tcp = &cp->tcps[j3d->curtileno];
  899.  
  900.         /* Scod : Table A-4*/
  901.         cio_write(cio, tcp->csty, 1);           /* Scod : Coding style parameters */
  902.         /* SGcod : Table A-5*/
  903.         cio_write(cio, tcp->prg, 1);            /* SGcod (A) : Progression order */
  904.         cio_write(cio, tcp->numlayers, 2);      /* SGcod (B) : No of layers */
  905.         cio_write(cio, tcp->mct, 1);            /* SGcod (C) : Multiple component transformation usage */
  906.         /* SPcod : Table A-6*/
  907.         j3d_write_cox(j3d, 0);                         
  908.         len = cio_tell(cio) - lenp;
  909.         cio_seek(cio, lenp);
  910.         cio_write(cio, len, 2);         /* Lcod */
  911.         cio_seek(cio, lenp + len);
  912. }
  913.  
  914. static void j3d_read_cod(opj_j3d_t *j3d) {
  915.         int len, i, pos;
  916.        
  917.         opj_cio_t *cio = j3d->cio;
  918.         opj_cp_t *cp = j3d->cp;
  919.         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
  920.         opj_volume_t *volume = j3d->volume;
  921.  
  922.         /* Lcod */
  923.         len = cio_read(cio, 2);                        
  924.         /* Scod : Table A-4*/
  925.         tcp->csty = cio_read(cio, 1);          
  926.         /* SGcod : Table A-5*/
  927.         tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);
  928.         tcp->numlayers = cio_read(cio, 2);     
  929.         tcp->mct = cio_read(cio, 1);           
  930.        
  931.         pos = cio_tell(cio);
  932.         for (i = 0; i < volume->numcomps; i++) {
  933.                 tcp->tccps[i].csty = tcp->csty & J3D_CP_CSTY_PRT;
  934.                 cio_seek(cio, pos);
  935.                 j3d_read_cox(j3d, i);
  936.         }
  937. }
  938.  
  939. static void j3d_write_coc(opj_j3d_t *j3d, int compno) {
  940.         int lenp, len;
  941.  
  942.         opj_cp_t *cp = j3d->cp;
  943.         opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
  944.         opj_volume_t *volume = j3d->volume;
  945.         opj_cio_t *cio = j3d->cio;
  946.        
  947.         cio_write(cio, J3D_MS_COC, 2);  /* COC */
  948.         lenp = cio_tell(cio);
  949.         cio_skip(cio, 2);
  950.         cio_write(cio, compno, volume->numcomps <= 256 ? 1 : 2);        /* Ccoc */
  951.         cio_write(cio, tcp->tccps[compno].csty, 1);                                     /* Scoc */
  952.        
  953.         j3d_write_cox(j3d, compno);
  954.        
  955.         len = cio_tell(cio) - lenp;
  956.         cio_seek(cio, lenp);
  957.         cio_write(cio, len, 2);                 /* Lcoc */
  958.         cio_seek(cio, lenp + len);
  959. }
  960.  
  961. static void j3d_read_coc(opj_j3d_t *j3d) {
  962.         int len, compno;
  963.  
  964.         opj_cp_t *cp = j3d->cp;
  965.         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
  966.         opj_volume_t *volume = j3d->volume;
  967.         opj_cio_t *cio = j3d->cio;
  968.        
  969.         len = cio_read(cio, 2);         /* Lcoc */
  970.         compno = cio_read(cio, volume->numcomps <= 256 ? 1 : 2);        /* Ccoc */
  971.         tcp->tccps[compno].csty = cio_read(cio, 1);     /* Scoc */
  972.         j3d_read_cox(j3d, compno);
  973. }
  974.  
  975. static void j3d_write_qcx(opj_j3d_t *j3d, int compno) {
  976.         int bandno, numbands;
  977.         int expn, mant;
  978.        
  979.         opj_cp_t *cp = j3d->cp;
  980.         opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
  981.         opj_tccp_t *tccp = &tcp->tccps[compno];
  982.         opj_cio_t *cio = j3d->cio;
  983.        
  984.         cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1);        /* Sqcx : Table A28 de 15444-1*/
  985.        
  986.         if (j3d->cinfo->codec_format == CODEC_J2K)
  987.         numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolution[0] * 3 - 2;
  988.         else if (j3d->cinfo->codec_format == CODEC_J3D) {
  989.                 int diff = tccp->numresolution[0] - tccp->numresolution[2];
  990.         numbands = (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) ? 1 : (tccp->numresolution[0] * 7 - 6) - 4 *diff; /* SIQNT vs. SEQNT */
  991.         }
  992.        
  993.         for (bandno = 0; bandno < numbands; bandno++) {
  994.                 expn = tccp->stepsizes[bandno].expn;
  995.                 mant = tccp->stepsizes[bandno].mant;
  996.                
  997.                 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
  998.                         cio_write(cio, expn << 3, 1);   /* SPqcx_i */
  999.                 } else {
  1000.                         cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
  1001.                 }
  1002.         }
  1003. }
  1004.  
  1005. static void j3d_read_qcx(opj_j3d_t *j3d, int compno, int len) {
  1006.         int tmp;
  1007.         int bandno, numbands;
  1008.  
  1009.         opj_cp_t *cp = j3d->cp;
  1010.         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
  1011.         opj_tccp_t *tccp = &tcp->tccps[compno];
  1012.         opj_cio_t *cio = j3d->cio;
  1013.  
  1014.         tmp = cio_read(cio, 1);         /* Sqcx */
  1015.         tccp->qntsty = tmp & 0x1f;
  1016.         tccp->numgbits = tmp >> 5;
  1017.  
  1018.         /*Numbands = 1                          si SIQNT
  1019.                              len - 1            si NOQNT
  1020.                                  (len - 1) / 2  si SEQNT */
  1021.         numbands = tccp->qntsty == J3D_CCP_QNTSTY_SIQNT ? 1 : ((tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
  1022.  
  1023.         for (bandno = 0; bandno < numbands; bandno++) {
  1024.                 int expn, mant;
  1025.                 if (tccp->qntsty == J3D_CCP_QNTSTY_NOQNT) {
  1026.                         expn = cio_read(cio, 1) >> 3;   /* SPqcx_i */
  1027.                         mant = 0;
  1028.                 } else {
  1029.                         tmp = cio_read(cio, 2);                 /* SPqcx_i */
  1030.                         expn = tmp >> 11;
  1031.                         mant = tmp & 0x7ff;
  1032.                 }
  1033.                 tccp->stepsizes[bandno].expn = expn;
  1034.                 tccp->stepsizes[bandno].mant = mant;
  1035.         }
  1036.        
  1037.         /* Add Antonin : if scalar_derived -> compute other stepsizes */
  1038.         if (tccp->qntsty == J3D_CCP_QNTSTY_SIQNT) {
  1039.                 for (bandno = 1; bandno < J3D_MAXBANDS; bandno++) {
  1040.                         int numbands = (cp->transform_format==TRF_2D_DWT) ? 3 : 7;
  1041.                         tccp->stepsizes[bandno].expn = tccp->stepsizes[0].expn - ((bandno - 1) / numbands);
  1042.                         tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
  1043.                 }
  1044.         }
  1045.         /* ddA */
  1046. }
  1047.  
  1048. static void j3d_write_qcd(opj_j3d_t *j3d) {
  1049.         int lenp, len;
  1050.  
  1051.         opj_cio_t *cio = j3d->cio;
  1052.        
  1053.         cio_write(cio, J3D_MS_QCD, 2);  /* QCD */
  1054.         lenp = cio_tell(cio);
  1055.         cio_skip(cio, 2);
  1056.         j3d_write_qcx(j3d, 0);                  /* Sqcd*/
  1057.         len = cio_tell(cio) - lenp;
  1058.         cio_seek(cio, lenp);
  1059.         cio_write(cio, len, 2);                 /* Lqcd */
  1060.         cio_seek(cio, lenp + len);
  1061. }
  1062.  
  1063. static void j3d_read_qcd(opj_j3d_t *j3d) {
  1064.         int len, i, pos;
  1065.  
  1066.         opj_cio_t *cio = j3d->cio;
  1067.         opj_volume_t *volume = j3d->volume;
  1068.        
  1069.         len = cio_read(cio, 2);         /* Lqcd */
  1070.         pos = cio_tell(cio);
  1071.         for (i = 0; i < volume->numcomps; i++) {
  1072.                 cio_seek(cio, pos);
  1073.                 j3d_read_qcx(j3d, i, len - 2);
  1074.         }
  1075. }
  1076.  
  1077. static void j3d_write_qcc(opj_j3d_t *j3d, int compno) {
  1078.         int lenp, len;
  1079.  
  1080.         opj_cio_t *cio = j3d->cio;
  1081.        
  1082.         cio_write(cio, J3D_MS_QCC, 2);  /* QCC */
  1083.         lenp = cio_tell(cio);
  1084.         cio_skip(cio, 2);
  1085.         cio_write(cio, compno, j3d->volume->numcomps <= 256 ? 1 : 2);   /* Cqcc */
  1086.         j3d_write_qcx(j3d, compno);
  1087.         len = cio_tell(cio) - lenp;
  1088.         cio_seek(cio, lenp);
  1089.         cio_write(cio, len, 2);                 /* Lqcc */
  1090.         cio_seek(cio, lenp + len);
  1091. }
  1092.  
  1093. static void j3d_read_qcc(opj_j3d_t *j3d) {
  1094.         int len, compno;
  1095.         int numcomp = j3d->volume->numcomps;
  1096.         opj_cio_t *cio = j3d->cio;
  1097.        
  1098.         len = cio_read(cio, 2); /* Lqcc */
  1099.         compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
  1100.         j3d_read_qcx(j3d, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
  1101. }
  1102.  
  1103. static void j3d_write_poc(opj_j3d_t *j3d) {
  1104.         int len, numpchgs, i;
  1105.  
  1106.         int numcomps = j3d->volume->numcomps;
  1107.        
  1108.         opj_cp_t *cp = j3d->cp;
  1109.         opj_tcp_t *tcp = &cp->tcps[j3d->curtileno];
  1110.         opj_tccp_t *tccp = &tcp->tccps[0];
  1111.         opj_cio_t *cio = j3d->cio;
  1112.  
  1113.         numpchgs = tcp->numpocs;
  1114.         cio_write(cio, J3D_MS_POC, 2);  /* POC  */
  1115.         len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
  1116.         cio_write(cio, len, 2);         /* Lpoc */
  1117.         for (i = 0; i < numpchgs; i++) {
  1118.                 opj_poc_t *poc = &tcp->pocs[i];
  1119.                 cio_write(cio, poc->resno0, 1); /* RSpoc_i */
  1120.                 cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2));        /* CSpoc_i */
  1121.                 cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
  1122.                 poc->layno1 = int_min(poc->layno1, tcp->numlayers);
  1123.                 cio_write(cio, poc->resno1, 1); /* REpoc_i */
  1124.                 poc->resno1 = int_min(poc->resno1, tccp->numresolution[0]);
  1125.                 cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2));        /* CEpoc_i */
  1126.                 poc->compno1 = int_min(poc->compno1, numcomps);
  1127.                 cio_write(cio, poc->prg, 1);    /* Ppoc_i */
  1128.         }
  1129. }
  1130.  
  1131. static void j3d_read_poc(opj_j3d_t *j3d) {
  1132.         int len, numpchgs, i, old_poc;
  1133.  
  1134.         int numcomps = j3d->volume->numcomps;
  1135.        
  1136.         opj_cp_t *cp = j3d->cp;
  1137.         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
  1138.         opj_tccp_t *tccp = &tcp->tccps[0];
  1139.         opj_cio_t *cio = j3d->cio;
  1140.        
  1141.         old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
  1142.         tcp->POC = 1;
  1143.         len = cio_read(cio, 2);         /* Lpoc */
  1144.         numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
  1145.        
  1146.         for (i = old_poc; i < numpchgs + old_poc; i++) {
  1147.                 opj_poc_t *poc;
  1148.                 poc = &tcp->pocs[i];
  1149.                 poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
  1150.                 poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2);  /* CSpoc_i */
  1151.                 poc->layno1 = int_min(cio_read(cio, 2), (unsigned int) tcp->numlayers); /* LYEpoc_i */
  1152.                 poc->resno1 = int_min(cio_read(cio, 1), (unsigned int) tccp->numresolution[0]); /* REpoc_i */
  1153.                 poc->compno1 = int_min(
  1154.                         cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps);       /* CEpoc_i */
  1155.                 poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1);    /* Ppoc_i */
  1156.         }
  1157.        
  1158.         tcp->numpocs = numpchgs + old_poc - 1;
  1159. }
  1160.  
  1161. static void j3d_read_crg(opj_j3d_t *j3d) {
  1162.         int len, i, Xcrg_i, Ycrg_i, Zcrg_i;
  1163.        
  1164.         opj_cio_t *cio = j3d->cio;
  1165.         int numcomps = j3d->volume->numcomps;
  1166.        
  1167.         len = cio_read(cio, 2);                 /* Lcrg */
  1168.         for (i = 0; i < numcomps; i++) {
  1169.                 Xcrg_i = cio_read(cio, 2);      /* Xcrg_i */
  1170.                 Ycrg_i = cio_read(cio, 2);      /* Ycrg_i */
  1171.                 Zcrg_i = cio_read(cio, 2);      /* Zcrg_i */
  1172.         }
  1173. }
  1174.  
  1175. static void j3d_read_tlm(opj_j3d_t *j3d) {
  1176.         int len, Ztlm, Stlm, ST, SP, tile_tlm, i;
  1177.         long int Ttlm_i, Ptlm_i;
  1178.  
  1179.         opj_cio_t *cio = j3d->cio;
  1180.        
  1181.         len = cio_read(cio, 2);         /* Ltlm */
  1182.         Ztlm = cio_read(cio, 1);        /* Ztlm */
  1183.         Stlm = cio_read(cio, 1);        /* Stlm */
  1184.         ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
  1185.         SP = (Stlm >> 6) & 0x01;
  1186.         tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
  1187.         for (i = 0; i < tile_tlm; i++) {
  1188.                 Ttlm_i = cio_read(cio, ST);     /* Ttlm_i */
  1189.                 Ptlm_i = cio_read(cio, SP ? 4 : 2);     /* Ptlm_i */
  1190.         }
  1191. }
  1192.  
  1193. static void j3d_read_plm(opj_j3d_t *j3d) {
  1194.         int len, i, Zplm, Nplm, add, packet_len = 0;
  1195.        
  1196.         opj_cio_t *cio = j3d->cio;
  1197.  
  1198.         len = cio_read(cio, 2);         /* Lplm */
  1199.         Zplm = cio_read(cio, 1);        /* Zplm */
  1200.         len -= 3;
  1201.         while (len > 0) {
  1202.                 Nplm = cio_read(cio, 4);                /* Nplm */
  1203.                 len -= 4;
  1204.                 for (i = Nplm; i > 0; i--) {
  1205.                         add = cio_read(cio, 1);
  1206.                         len--;
  1207.                         packet_len = (packet_len << 7) + add;   /* Iplm_ij */
  1208.                         if ((add & 0x80) == 0) {
  1209.                                 /* New packet */
  1210.                                 packet_len = 0;
  1211.                         }
  1212.                         if (len <= 0)
  1213.                                 break;
  1214.                 }
  1215.         }
  1216. }
  1217.  
  1218. static void j3d_read_plt(opj_j3d_t *j3d) {
  1219.         int len, i, Zplt, packet_len = 0, add;
  1220.        
  1221.         opj_cio_t *cio = j3d->cio;
  1222.        
  1223.         len = cio_read(cio, 2);         /* Lplt */
  1224.         Zplt = cio_read(cio, 1);        /* Zplt */
  1225.         for (i = len - 3; i > 0; i--) {
  1226.                 add = cio_read(cio, 1);
  1227.                 packet_len = (packet_len << 7) + add;   /* Iplt_i */
  1228.                 if ((add & 0x80) == 0) {
  1229.                         /* New packet */
  1230.                         packet_len = 0;
  1231.                 }
  1232.         }
  1233. }
  1234.  
  1235. static void j3d_read_ppm(opj_j3d_t *j3d) {
  1236.         int len, Z_ppm, i, j;
  1237.         int N_ppm;
  1238.  
  1239.         opj_cp_t *cp = j3d->cp;
  1240.         opj_cio_t *cio = j3d->cio;
  1241.        
  1242.         len = cio_read(cio, 2);
  1243.         cp->ppm = 1;
  1244.        
  1245.         Z_ppm = cio_read(cio, 1);       /* Z_ppm */
  1246.         len -= 3;
  1247.         while (len > 0) {
  1248.                 if (cp->ppm_previous == 0) {
  1249.                         N_ppm = cio_read(cio, 4);       /* N_ppm */
  1250.                         len -= 4;
  1251.                 } else {
  1252.                         N_ppm = cp->ppm_previous;
  1253.                 }
  1254.                 j = cp->ppm_store;
  1255.                 if (Z_ppm == 0) {       /* First PPM marker */
  1256.                         cp->ppm_data = (unsigned char *) opj_malloc(N_ppm * sizeof(unsigned char));
  1257.                         cp->ppm_data_first = cp->ppm_data;
  1258.                         cp->ppm_len = N_ppm;
  1259.                 } else {                        /* NON-first PPM marker */
  1260.                         cp->ppm_data = (unsigned char *) opj_realloc(cp->ppm_data, (N_ppm +     cp->ppm_store) * sizeof(unsigned char));
  1261.                         cp->ppm_data_first = cp->ppm_data;
  1262.                         cp->ppm_len = N_ppm + cp->ppm_store;
  1263.                 }
  1264.                 for (i = N_ppm; i > 0; i--) {   /* Read packet header */
  1265.                         cp->ppm_data[j] = cio_read(cio, 1);
  1266.                         j++;
  1267.                         len--;
  1268.                         if (len == 0)
  1269.                                 break;                  /* Case of non-finished packet header in present marker but finished in next one */
  1270.                 }
  1271.                 cp->ppm_previous = i - 1;
  1272.                 cp->ppm_store = j;
  1273.         }
  1274. }
  1275.  
  1276. static void j3d_read_ppt(opj_j3d_t *j3d) {
  1277.         int len, Z_ppt, i, j = 0;
  1278.  
  1279.         opj_cp_t *cp = j3d->cp;
  1280.         opj_tcp_t *tcp = cp->tcps + j3d->curtileno;
  1281.         opj_cio_t *cio = j3d->cio;
  1282.  
  1283.         len = cio_read(cio, 2);
  1284.         Z_ppt = cio_read(cio, 1);
  1285.         tcp->ppt = 1;
  1286.         if (Z_ppt == 0) {               /* First PPT marker */
  1287.                 tcp->ppt_data = (unsigned char *) opj_malloc((len - 3) * sizeof(unsigned char));
  1288.                 tcp->ppt_data_first = tcp->ppt_data;
  1289.                 tcp->ppt_store = 0;
  1290.                 tcp->ppt_len = len - 3;
  1291.         } else {                        /* NON-first PPT marker */
  1292.                 tcp->ppt_data = (unsigned char *) opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
  1293.                 tcp->ppt_data_first = tcp->ppt_data;
  1294.                 tcp->ppt_len = len - 3 + tcp->ppt_store;
  1295.         }
  1296.         j = tcp->ppt_store;
  1297.         for (i = len - 3; i > 0; i--) {
  1298.                 tcp->ppt_data[j] = cio_read(cio, 1);
  1299.                 j++;
  1300.         }
  1301.         tcp->ppt_store = j;
  1302. }
  1303.  
  1304. static void j3d_write_sot(opj_j3d_t *j3d) {
  1305.         int lenp, len;
  1306.  
  1307.         opj_cio_t *cio = j3d->cio;
  1308.  
  1309.         j3d->sot_start = cio_tell(cio);
  1310.         cio_write(cio, J3D_MS_SOT, 2);          /* SOT */
  1311.         lenp = cio_tell(cio);
  1312.         cio_skip(cio, 2);                                       /* Lsot (further) */
  1313.         cio_write(cio, j3d->curtileno, 2);      /* Isot */
  1314.         cio_skip(cio, 4);                                       /* Psot (further in j3d_write_sod) */
  1315.         cio_write(cio, 0, 1);                           /* TPsot */
  1316.         cio_write(cio, 1, 1);                           /* TNsot (no of tile-parts of this tile in this codestream)*/
  1317.         len = cio_tell(cio) - lenp;
  1318.         cio_seek(cio, lenp);
  1319.         cio_write(cio, len, 2);                         /* Lsot */
  1320.         cio_seek(cio, lenp + len);
  1321. }
  1322.  
  1323. static void j3d_read_sot(opj_j3d_t *j3d) {
  1324.         int len, tileno, totlen, partno, numparts, i;
  1325.         opj_tcp_t *tcp = NULL;
  1326.         char status = 0;
  1327.  
  1328.         opj_cp_t *cp = j3d->cp;
  1329.         opj_cio_t *cio = j3d->cio;
  1330.        
  1331.         len = cio_read(cio, 2);
  1332.         tileno = cio_read(cio, 2);
  1333.        
  1334.         if (cp->tileno_size == 0) {
  1335.                 cp->tileno[cp->tileno_size] = tileno;
  1336.                 cp->tileno_size++;
  1337.         } else {
  1338.                 i = 0;
  1339.                 while (i < cp->tileno_size && status == 0) {
  1340.                         status = cp->tileno[i] == tileno ? 1 : 0;
  1341.                         i++;
  1342.                 }
  1343.                 if (status == 0) {
  1344.                         cp->tileno[cp->tileno_size] = tileno;
  1345.                         cp->tileno_size++;
  1346.                 }
  1347.         }
  1348.        
  1349.         totlen = cio_read(cio, 4);
  1350.         if (!totlen)
  1351.                 totlen = cio_numbytesleft(cio) + 8;
  1352.        
  1353.         partno = cio_read(cio, 1);
  1354.         numparts = cio_read(cio, 1);
  1355.        
  1356.         j3d->curtileno = tileno;
  1357.         j3d->eot = cio_getbp(cio) - 12 + totlen;
  1358.         j3d->state = J3D_STATE_TPH;
  1359.         tcp = &cp->tcps[j3d->curtileno];
  1360.        
  1361.         if (tcp->first == 1) {
  1362.                
  1363.                 /* Initialization PPT */
  1364.                 opj_tccp_t *tmp = tcp->tccps;
  1365.                 memcpy(tcp, j3d->default_tcp, sizeof(opj_tcp_t));
  1366.                 tcp->ppt = 0;
  1367.                 tcp->ppt_data = NULL;
  1368.                 tcp->ppt_data_first = NULL;
  1369.                 tcp->tccps = tmp;
  1370.  
  1371.                 for (i = 0; i < j3d->volume->numcomps; i++) {
  1372.                         tcp->tccps[i] = j3d->default_tcp->tccps[i];
  1373.                 }
  1374.                 cp->tcps[j3d->curtileno].first = 0;
  1375.         }
  1376. }
  1377.  
  1378. static void j3d_write_sod(opj_j3d_t *j3d, void *tile_coder) {
  1379.         int l, layno;
  1380.         int totlen;
  1381.         opj_tcp_t *tcp = NULL;
  1382.         opj_volume_info_t *volume_info = NULL;
  1383.        
  1384.         opj_tcd_t *tcd = (opj_tcd_t*)tile_coder;        /* cast is needed because of conflicts in header inclusions */
  1385.         opj_cp_t *cp = j3d->cp;
  1386.         opj_cio_t *cio = j3d->cio;
  1387.        
  1388.         cio_write(cio, J3D_MS_SOD, 2);
  1389.         if (j3d->curtileno == 0) {
  1390.                 j3d->sod_start = cio_tell(cio) + j3d->pos_correction;
  1391.         }
  1392.        
  1393.         /* INDEX >> */
  1394.         volume_info = j3d->volume_info;
  1395.         if (volume_info && volume_info->index_on) {
  1396.                 volume_info->tile[j3d->curtileno].end_header = cio_tell(cio) + j3d->pos_correction - 1;
  1397.         }
  1398.         /* << INDEX */
  1399.        
  1400.         tcp = &cp->tcps[j3d->curtileno];
  1401.         for (layno = 0; layno < tcp->numlayers; layno++) {
  1402.                 tcp->rates[layno] -= tcp->rates[layno] ? (j3d->sod_start / (cp->th * cp->tw * cp->tl)) : 0;
  1403.         }
  1404.        
  1405.         if(volume_info) {
  1406.                 volume_info->num = 0;
  1407.         }
  1408.  
  1409.         l = tcd_encode_tile(tcd, j3d->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, volume_info);
  1410.        
  1411.         /* Writing Psot in SOT marker */
  1412.         totlen = cio_tell(cio) + l - j3d->sot_start;
  1413.         cio_seek(cio, j3d->sot_start + 6);
  1414.         cio_write(cio, totlen, 4);
  1415.         cio_seek(cio, j3d->sot_start + totlen);
  1416. }
  1417.  
  1418. static void j3d_read_sod(opj_j3d_t *j3d) {
  1419.         int len, truncate = 0, i;
  1420.         unsigned char *data = NULL, *data_ptr = NULL;
  1421.  
  1422.         opj_cio_t *cio = j3d->cio;
  1423.         int curtileno = j3d->curtileno;
  1424.        
  1425.         len = int_min(j3d->eot - cio_getbp(cio), cio_numbytesleft(cio) + 1);
  1426.        
  1427.         if (len == cio_numbytesleft(cio) + 1) {
  1428.                 truncate = 1;           /* Case of a truncate codestream */
  1429.         }
  1430.        
  1431.         data = (unsigned char *) opj_malloc((j3d->tile_len[curtileno] + len) * sizeof(unsigned char));
  1432.  
  1433.         for (i = 0; i < j3d->tile_len[curtileno]; i++) {
  1434.                 data[i] = j3d->tile_data[curtileno][i];
  1435.         }
  1436.  
  1437.         data_ptr = data + j3d->tile_len[curtileno];
  1438.         for (i = 0; i < len; i++) {
  1439.                 data_ptr[i] = cio_read(cio, 1);
  1440.         }
  1441.        
  1442.         j3d->tile_len[curtileno] += len;
  1443.         opj_free(j3d->tile_data[curtileno]);
  1444.         j3d->tile_data[curtileno] = data;
  1445.        
  1446.         if (!truncate) {
  1447.                 j3d->state = J3D_STATE_TPHSOT;
  1448.         } else {
  1449.                 j3d->state = J3D_STATE_NEOC;    /* RAJOUTE !! */
  1450.         }
  1451. }
  1452.  
  1453. static void j3d_write_rgn(opj_j3d_t *j3d, int compno, int tileno) {
  1454.        
  1455.         opj_cp_t *cp = j3d->cp;
  1456.         opj_tcp_t *tcp = &cp->tcps[tileno];
  1457.         opj_cio_t *cio = j3d->cio;
  1458.         int numcomps = j3d->volume->numcomps;
  1459.        
  1460.         cio_write(cio, J3D_MS_RGN, 2);                                          /* RGN  */
  1461.         cio_write(cio, numcomps <= 256 ? 5 : 6, 2);                     /* Lrgn */
  1462.         cio_write(cio, compno, numcomps <= 256 ? 1 : 2);        /* Crgn */
  1463.         cio_write(cio, 0, 1);                                                           /* Srgn */
  1464.         cio_write(cio, tcp->tccps[compno].roishift, 1);         /* SPrgn */
  1465. }
  1466.  
  1467. static void j3d_read_rgn(opj_j3d_t *j3d) {
  1468.         int len, compno, roisty;
  1469.  
  1470.         opj_cp_t *cp = j3d->cp;
  1471.         opj_tcp_t *tcp = j3d->state == J3D_STATE_TPH ? &cp->tcps[j3d->curtileno] : j3d->default_tcp;
  1472.         opj_cio_t *cio = j3d->cio;
  1473.         int numcomps = j3d->volume->numcomps;
  1474.  
  1475.         len = cio_read(cio, 2);                                                                         /* Lrgn */
  1476.         compno = cio_read(cio, numcomps <= 256 ? 1 : 2);                        /* Crgn */
  1477.         roisty = cio_read(cio, 1);                                                                      /* Srgn */
  1478.         tcp->tccps[compno].roishift = cio_read(cio, 1);                         /* SPrgn */
  1479. }
  1480.  
  1481. static void j3d_write_eoc(opj_j3d_t *j3d) {
  1482.         opj_cio_t *cio = j3d->cio;
  1483.         /* opj_event_msg(j3d->cinfo, "%.8x: EOC\n", cio_tell(cio) + j3d->pos_correction); */
  1484.         cio_write(cio, J3D_MS_EOC, 2);
  1485. }
  1486.  
  1487. static void j3d_read_eoc(opj_j3d_t *j3d) {
  1488.         int i, tileno;
  1489.  
  1490. #ifndef NO_PACKETS_DECODING  
  1491.         opj_tcd_t *tcd = tcd_create(j3d->cinfo);
  1492.     tcd_malloc_decode(tcd, j3d->volume, j3d->cp);
  1493.         /*j3d_dump_volume(stdout, tcd->volume);
  1494.         j3d_dump_cp(stdout, tcd->volume, tcd->cp);*/
  1495.         for (i = 0; i < j3d->cp->tileno_size; i++) {
  1496.                 tileno = j3d->cp->tileno[i];
  1497.                 //opj_event_msg(j3d->cinfo, EVT_INFO, "tcd_decode_tile \n");
  1498.                 tcd_decode_tile(tcd, j3d->tile_data[tileno], j3d->tile_len[tileno], tileno);
  1499.                 opj_free(j3d->tile_data[tileno]);
  1500.                 j3d->tile_data[tileno] = NULL;
  1501.         }
  1502.         tcd_free_decode(tcd);
  1503.         tcd_destroy(tcd);
  1504. #else
  1505.         for (i = 0; i < j3d->cp->tileno_size; i++) {
  1506.                 tileno = j3d->cp->tileno[i];
  1507.                 opj_free(j3d->tile_data[tileno]);
  1508.                 j3d->tile_data[tileno] = NULL;
  1509.         }
  1510. #endif
  1511.        
  1512.         j3d->state = J3D_STATE_MT;
  1513. }
  1514.  
  1515. static void j3d_read_unk(opj_j3d_t *j3d) {
  1516.         opj_event_msg(j3d->cinfo, EVT_WARNING, "Unknown marker\n");
  1517. }
  1518.  
  1519. static opj_atk_t atk_info_wt[] = {
  1520.         {0, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1.230174104, 4, {0}, {0}, {0}, {1,1,1,1}, {-1.586134342059924, -0.052980118572961, 0.882911075530934, 0.443506852043971}},/* WT 9-7 IRR*/
  1521.         {1, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {1,2}, {1,2}, {1,1}, {-1.0,1.0}},/* WT 5-3 REV*/
  1522.         {2, 0, J3D_ATK_ARB, J3D_ATK_REV, 0, J3D_ATK_CON, 0, 2, {0,0}, {0,1}, {0,1}, {1,1}, {{-1.0},{1.0}}}, /* WT 2-2 REV*/
  1523.         {3, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-1}, {0,1,2}, {0,1,2}, {1,1,3}, {{-1.0},{1.0},{1.0,0.0,-1.0}}}, /* WT 2-6 REV*/
  1524.         {4, 0, J3D_ATK_ARB, J3D_ATK_REV, 1, J3D_ATK_CON, 0, 3, {0,0,-2}, {0,1,6}, {0,1,32}, {1,1,5}, {{-1},{1},{-3.0,22.0,0.0,-22.0,3.0}}}, /* WT 2-10 REV*/
  1525.         {5, 1, J3D_ATK_ARB, J3D_ATK_IRR, 1, J3D_ATK_WS, 1, 7, {0}, {0}, {0}, {1,1,2,1,2,1,3},{{-1},{1.58613434206},{-0.460348209828, 0.460348209828},{0.25},{0.374213867768,-0.374213867768},{-1.33613434206},{0.29306717103,0,-0.29306717103}}}, /* WT 6-10 IRR*/
  1526.         {6, 1, J3D_ATK_ARB, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 11, {0}, {0}, {0}, {1,1,2,1,2,1,2,1,2,1,5},{{-1},{0,99715069105},{-1.00573127827, 1.00573127827},{-0.27040357631},{2.20509972343, -2.20509972343},{0.08059995736},
  1527.                 {-1.62682532350, 1.62682532350},{0.52040357631},{0.60404664250, -0.60404664250},{-0.82775064841},{-0.06615812964, 0.29402137720, 0, -0.29402137720, 0.06615812964}}}, /* WT 10-18 IRR*/
  1528.         {7, 1, J3D_ATK_WS, J3D_ATK_IRR, 0, J3D_ATK_WS, 1, 2, {0}, {0}, {0}, {1,1}, {-0.5, 0.25}},       /* WT 5-3 IRR*/
  1529.         {8, 0, J3D_ATK_WS, J3D_ATK_REV, 0, J3D_ATK_WS, 0, 2, {0}, {4,4}, {8,8}, {2,2}, {{-9,1},{5,-1}}}         /* WT 13-7 REV*/
  1530. };
  1531.  
  1532. typedef struct opj_dec_mstabent {
  1533.         /** marker value */
  1534.         int id;
  1535.         /** value of the state when the marker can appear */
  1536.         int states;
  1537.         /** action linked to the marker */
  1538.         void (*handler) (opj_j3d_t *j3d);
  1539. } opj_dec_mstabent_t;
  1540.  
  1541. opj_dec_mstabent_t j3d_dec_mstab[] = {
  1542.   {J3D_MS_SOC, J3D_STATE_MHSOC, j3d_read_soc},
  1543.   {J3D_MS_SOT, J3D_STATE_MH | J3D_STATE_TPHSOT, j3d_read_sot},
  1544.   {J3D_MS_SOD, J3D_STATE_TPH, j3d_read_sod},
  1545.   {J3D_MS_EOC, J3D_STATE_TPHSOT, j3d_read_eoc},
  1546.   {J3D_MS_CAP, J3D_STATE_MHSIZ, j3d_read_cap},
  1547.   {J3D_MS_SIZ, J3D_STATE_MHSIZ, j3d_read_siz},
  1548.   {J3D_MS_ZSI, J3D_STATE_MHSIZ, j3d_read_zsi},
  1549.   {J3D_MS_COD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cod},
  1550.   {J3D_MS_COC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_coc},
  1551.   {J3D_MS_RGN, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_rgn},
  1552.   {J3D_MS_QCD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcd},
  1553.   {J3D_MS_QCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_qcc},
  1554.   {J3D_MS_POC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_poc},
  1555.   {J3D_MS_TLM, J3D_STATE_MH, j3d_read_tlm},
  1556.   {J3D_MS_PLM, J3D_STATE_MH, j3d_read_plm},
  1557.   {J3D_MS_PLT, J3D_STATE_TPH, j3d_read_plt},
  1558.   {J3D_MS_PPM, J3D_STATE_MH, j3d_read_ppm},
  1559.   {J3D_MS_PPT, J3D_STATE_TPH, j3d_read_ppt},
  1560.   {J3D_MS_SOP, 0, 0},
  1561.   {J3D_MS_CRG, J3D_STATE_MH, j3d_read_crg},
  1562.   {J3D_MS_COM, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_com},
  1563.   {J3D_MS_DCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_dco},
  1564.   {J3D_MS_ATK, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_atk},
  1565.   {0, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_unk}
  1566.   /*, -->must define the j3d_read functions
  1567.   {J3D_MS_CBD, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_cbd},
  1568.   {J3D_MS_MCT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mct},
  1569.   {J3D_MS_MCC, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mcc},
  1570.   {J3D_MS_MCO, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_mco},
  1571.   {J3D_MS_NLT, J3D_STATE_MH | J3D_STATE_TPH, j3d_read_nlt},
  1572.   {J3D_MS_VMS, J3D_STATE_MH, j3d_read_vms},
  1573.   {J3D_MS_DFS, J3D_STATE_MH, j3d_read_dfs},
  1574.   {J3D_MS_ADS, J3D_STATE_MH, j3d_read_ads},
  1575.   {J3D_MS_QPD, J3D_STATE_MH, j3d_read_qpd},
  1576.   {J3D_MS_QPC, J3D_STATE_TPH, j3d_read_qpc}*/
  1577. };
  1578.  
  1579. /**
  1580. Read the lookup table containing all the marker, status and action
  1581. @param id Marker value
  1582. */
  1583. static opj_dec_mstabent_t *j3d_dec_mstab_lookup(int id) {
  1584.         opj_dec_mstabent_t *e;
  1585.         for (e = j3d_dec_mstab; e->id != 0; e++) {
  1586.                 if (e->id == id) {
  1587.                         break;
  1588.                 }
  1589.         }
  1590.         return e;
  1591. }
  1592.  
  1593. /* ----------------------------------------------------------------------- */
  1594. /* J3D / JPT decoder interface                                             */
  1595. /* ----------------------------------------------------------------------- */
  1596.  
  1597. opj_j3d_t* j3d_create_decompress(opj_common_ptr cinfo) {
  1598.         opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
  1599.         if(j3d) {
  1600.                 j3d->cinfo = cinfo;
  1601.                 j3d->default_tcp = (opj_tcp_t*)opj_malloc(sizeof(opj_tcp_t));
  1602.                 if(!j3d->default_tcp) {
  1603.                         opj_free(j3d);
  1604.                         return NULL;
  1605.                 }
  1606.         }
  1607.         return j3d;
  1608. }
  1609.  
  1610. void j3d_destroy_decompress(opj_j3d_t *j3d) {
  1611.         int i = 0;
  1612.  
  1613.         if(j3d->tile_len != NULL) {
  1614.                 opj_free(j3d->tile_len);
  1615.         }
  1616.         if(j3d->tile_data != NULL) {
  1617.                 opj_free(j3d->tile_data);
  1618.         }
  1619.         if(j3d->default_tcp != NULL) {
  1620.                 opj_tcp_t *default_tcp = j3d->default_tcp;
  1621.                 if(default_tcp->ppt_data_first != NULL) {
  1622.                         opj_free(default_tcp->ppt_data_first);
  1623.                 }
  1624.                 if(j3d->default_tcp->tccps != NULL) {
  1625.                         opj_free(j3d->default_tcp->tccps);
  1626.                 }
  1627.                 opj_free(j3d->default_tcp);
  1628.         }
  1629.         if(j3d->cp != NULL) {
  1630.                 opj_cp_t *cp = j3d->cp;
  1631.                 if(cp->tcps != NULL) {
  1632.                         for(i = 0; i < cp->tw * cp->th * cp->tl; i++) {
  1633.                                 if(cp->tcps[i].ppt_data_first != NULL) {
  1634.                                         opj_free(cp->tcps[i].ppt_data_first);
  1635.                                 }
  1636.                                 if(cp->tcps[i].tccps != NULL) {
  1637.                                         opj_free(cp->tcps[i].tccps);
  1638.                                 }
  1639.                         }
  1640.                         opj_free(cp->tcps);
  1641.                 }
  1642.                 if(cp->ppm_data_first != NULL) {
  1643.                         opj_free(cp->ppm_data_first);
  1644.                 }
  1645.                 if(cp->tileno != NULL) {
  1646.                         opj_free(cp->tileno);  
  1647.                 }
  1648.                 if(cp->comment != NULL) {
  1649.                         opj_free(cp->comment);
  1650.                 }
  1651.  
  1652.                 opj_free(cp);
  1653.         }
  1654.  
  1655.         opj_free(j3d);
  1656. }
  1657.  
  1658. void j3d_setup_decoder(opj_j3d_t *j3d, opj_dparameters_t *parameters) {
  1659.         if(j3d && parameters) {
  1660.                 /* create and initialize the coding parameters structure */
  1661.                 opj_cp_t *cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
  1662.                 cp->reduce[0] = parameters->cp_reduce[0];
  1663.                 cp->reduce[1] = parameters->cp_reduce[1];
  1664.                 cp->reduce[2] = parameters->cp_reduce[2];
  1665.                 cp->layer = parameters->cp_layer;
  1666.                 cp->bigendian = parameters->bigendian;
  1667.                
  1668.                
  1669.                 cp->encoding_format = ENCOD_2EB;
  1670.                 cp->transform_format = TRF_2D_DWT;
  1671.                
  1672.                 /* keep a link to cp so that we can destroy it later in j3d_destroy_decompress */
  1673.                 j3d->cp = cp;
  1674.         }
  1675. }
  1676.  
  1677. opj_volume_t* j3d_decode(opj_j3d_t *j3d, opj_cio_t *cio) {
  1678.         opj_volume_t *volume = NULL;
  1679.  
  1680.         opj_common_ptr cinfo = j3d->cinfo;
  1681.  
  1682.         j3d->cio = cio;
  1683.  
  1684.         /* create an empty volume */
  1685.         volume = (opj_volume_t*)opj_malloc(sizeof(opj_volume_t));
  1686.         j3d->volume = volume;
  1687.  
  1688.         j3d->state = J3D_STATE_MHSOC;
  1689.        
  1690.         for (;;) {
  1691.                 opj_dec_mstabent_t *e;
  1692.                 int id = cio_read(cio, 2);
  1693.                 if (id >> 8 != 0xff) {
  1694.                         opj_volume_destroy(volume);
  1695.                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
  1696.                         return 0;
  1697.                 }
  1698.                 e = j3d_dec_mstab_lookup(id);
  1699.                 //opj_event_msg(cinfo, EVT_INFO, "MARKER %x PREVSTATE %d E->STATE %d\n",e->id,j3d->state,e->states);
  1700.                 if (!(j3d->state & e->states)) {
  1701.                         opj_volume_destroy(volume);
  1702.                         opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
  1703.                         return 0;
  1704.                 }
  1705.                 if (e->handler) {
  1706.                         (*e->handler)(j3d);
  1707.                 }
  1708.                 //opj_event_msg(cinfo, EVT_INFO, "POSTSTATE %d\n",j3d->state);
  1709.                 if (j3d->state == J3D_STATE_MT) {
  1710.                         break;
  1711.                 }
  1712.                 if (j3d->state == J3D_STATE_NEOC) {
  1713.                         break;
  1714.                 }
  1715.         }
  1716.         if (j3d->state == J3D_STATE_NEOC) {
  1717.                 j3d_read_eoc(j3d);
  1718.         }
  1719.  
  1720.         if (j3d->state != J3D_STATE_MT) {
  1721.                 opj_event_msg(cinfo, EVT_WARNING, "Incomplete bitstream\n");
  1722.         }
  1723.        
  1724.         return volume;
  1725. }
  1726.  
  1727. /* ----------------------------------------------------------------------- */
  1728. /* J3D encoder interface                                                       */
  1729. /* ----------------------------------------------------------------------- */
  1730.  
  1731. opj_j3d_t* j3d_create_compress(opj_common_ptr cinfo) {
  1732.         opj_j3d_t *j3d = (opj_j3d_t*)opj_malloc(sizeof(opj_j3d_t));
  1733.         if(j3d) {
  1734.                 j3d->cinfo = cinfo;
  1735.         }
  1736.         return j3d;
  1737. }
  1738.  
  1739. void j3d_destroy_compress(opj_j3d_t *j3d) {
  1740.         int tileno;
  1741.  
  1742.         if(!j3d) return;
  1743.  
  1744.         if(j3d->volume_info != NULL) {
  1745.                 opj_volume_info_t *volume_info = j3d->volume_info;
  1746.                 if (volume_info->index_on && j3d->cp) {
  1747.                         opj_cp_t *cp = j3d->cp;
  1748.                         for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  1749.                                 opj_tile_info_t *tile_info = &volume_info->tile[tileno];
  1750.                                 opj_free(tile_info->thresh);
  1751.                                 opj_free(tile_info->packet);
  1752.                         }
  1753.                         opj_free(volume_info->tile);
  1754.                 }
  1755.                 opj_free(volume_info);
  1756.         }
  1757.         if(j3d->cp != NULL) {
  1758.                 opj_cp_t *cp = j3d->cp;
  1759.  
  1760.                 if(cp->comment) {
  1761.                         opj_free(cp->comment);
  1762.                 }
  1763.                 if(cp->matrice) {
  1764.                         opj_free(cp->matrice);
  1765.                 }
  1766.                 for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  1767.                         opj_free(cp->tcps[tileno].tccps);
  1768.                 }
  1769.                 opj_free(cp->tcps);
  1770.                 opj_free(cp);
  1771.         }
  1772.  
  1773.         opj_free(j3d);
  1774. }
  1775.  
  1776. void j3d_setup_encoder(opj_j3d_t *j3d, opj_cparameters_t *parameters, opj_volume_t *volume) {
  1777.         int i, j, tileno, numpocs_tile;
  1778.         opj_cp_t *cp = NULL;
  1779.  
  1780.         if(!j3d || !parameters || ! volume) {
  1781.                 return;
  1782.         }
  1783.  
  1784.         /* create and initialize the coding parameters structure */
  1785.         cp = (opj_cp_t*)opj_malloc(sizeof(opj_cp_t));
  1786.  
  1787.         /* keep a link to cp so that we can destroy it later in j3d_destroy_compress */
  1788.         j3d->cp = cp;
  1789.  
  1790.         /* set default values for cp */
  1791.         cp->tw = 1;
  1792.         cp->th = 1;
  1793.         cp->tl = 1;
  1794.  
  1795.         /* copy user encoding parameters */
  1796.         cp->disto_alloc = parameters->cp_disto_alloc;
  1797.         cp->fixed_alloc = parameters->cp_fixed_alloc;
  1798.         cp->fixed_quality = parameters->cp_fixed_quality;
  1799.  
  1800.         /* transform and coding method */
  1801.         cp->transform_format = parameters->transform_format;
  1802.         cp->encoding_format = parameters->encoding_format;
  1803.  
  1804.         /* mod fixed_quality */
  1805.         if(parameters->cp_matrice) {
  1806.                 size_t array_size = parameters->tcp_numlayers * 3 * parameters->numresolution[0] * sizeof(int);
  1807.                 cp->matrice = (int *) opj_malloc(array_size);
  1808.                 memcpy(cp->matrice, parameters->cp_matrice, array_size);
  1809.         }
  1810.  
  1811.         /* creation of an index file ? */
  1812.         cp->index_on = parameters->index_on;
  1813.         if(cp->index_on) {
  1814.                 j3d->volume_info = (opj_volume_info_t*)opj_malloc(sizeof(opj_volume_info_t));
  1815.         }
  1816.        
  1817.         /* tiles */
  1818.         cp->tdx = parameters->cp_tdx;
  1819.         cp->tdy = parameters->cp_tdy;
  1820.         cp->tdz = parameters->cp_tdz;
  1821.         /* tile offset */
  1822.         cp->tx0 = parameters->cp_tx0;
  1823.         cp->ty0 = parameters->cp_ty0;
  1824.         cp->tz0 = parameters->cp_tz0;
  1825.         /* comment string */
  1826.         if(parameters->cp_comment) {
  1827.                 cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
  1828.                 if(cp->comment) {
  1829.                         strcpy(cp->comment, parameters->cp_comment);
  1830.                 }
  1831.         }
  1832.  
  1833.         /*calculate other encoding parameters*/
  1834.         if (parameters->tile_size_on) {
  1835.                 cp->tw = int_ceildiv(volume->x1 - cp->tx0, cp->tdx);
  1836.                 cp->th = int_ceildiv(volume->y1 - cp->ty0, cp->tdy);
  1837.                 cp->tl = int_ceildiv(volume->z1 - cp->tz0, cp->tdz);
  1838.         } else {
  1839.                 cp->tdx = volume->x1 - cp->tx0;
  1840.                 cp->tdy = volume->y1 - cp->ty0;
  1841.                 cp->tdz = volume->z1 - cp->tz0;
  1842.         }
  1843.  
  1844.         /* initialize the multiple tiles */
  1845.         /* ---------------------------- */
  1846.         cp->tcps = (opj_tcp_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tcp_t));
  1847.  
  1848.         for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
  1849.                 opj_tcp_t *tcp = &cp->tcps[tileno];
  1850.                 tcp->numlayers = parameters->tcp_numlayers;
  1851.                 for (j = 0; j < tcp->numlayers; j++) {
  1852.                         if (cp->fixed_quality) {        /* add fixed_quality */
  1853.                                 tcp->distoratio[j] = parameters->tcp_distoratio[j];
  1854.                         } else {
  1855.                                 tcp->rates[j] = parameters->tcp_rates[j];
  1856.                         }
  1857.                 }
  1858.                 tcp->csty = parameters->csty;
  1859.                 tcp->prg = parameters->prog_order;
  1860.                 tcp->mct = volume->numcomps == 3 ? 1 : 0;
  1861.  
  1862.                 numpocs_tile = 0;
  1863.                 tcp->POC = 0;
  1864.                 if (parameters->numpocs) {
  1865.                         /* initialisation of POC */
  1866.                         tcp->POC = 1;
  1867.                         for (i = 0; i < parameters->numpocs; i++) {
  1868.                                 if((tileno == parameters->POC[i].tile - 1) || (parameters->POC[i].tile == -1)) {
  1869.                                         opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
  1870.                                         tcp_poc->resno0         = parameters->POC[numpocs_tile].resno0;
  1871.                                         tcp_poc->compno0        = parameters->POC[numpocs_tile].compno0;
  1872.                                         tcp_poc->layno1         = parameters->POC[numpocs_tile].layno1;
  1873.                                         tcp_poc->resno1         = parameters->POC[numpocs_tile].resno1;
  1874.                                         tcp_poc->compno1        = parameters->POC[numpocs_tile].compno1;
  1875.                                         tcp_poc->prg            = parameters->POC[numpocs_tile].prg;
  1876.                                         tcp_poc->tile           = parameters->POC[numpocs_tile].tile;
  1877.                                         numpocs_tile++;
  1878.                                 }
  1879.                         }
  1880.                 }
  1881.                 tcp->numpocs = numpocs_tile;
  1882.  
  1883.                 tcp->tccps = (opj_tccp_t *) opj_malloc(volume->numcomps * sizeof(opj_tccp_t));
  1884.                
  1885.                 for (i = 0; i < volume->numcomps; i++) {
  1886.                         opj_tccp_t *tccp = &tcp->tccps[i];
  1887.                         tccp->csty = parameters->csty & J3D_CCP_CSTY_PRT;       /* 0 => standard precint || 1 => custom-defined precinct  */
  1888.                         tccp->numresolution[0] = parameters->numresolution[0];
  1889.                         tccp->numresolution[1] = parameters->numresolution[1];
  1890.                         tccp->numresolution[2] = parameters->numresolution[2];
  1891.                                                 assert (parameters->cblock_init[0] <= T1_MAXCBLKW);
  1892.                                                 assert (parameters->cblock_init[0] >= T1_MINCBLKW);
  1893.                                                 assert (parameters->cblock_init[1] <= T1_MAXCBLKH);
  1894.                                                 assert (parameters->cblock_init[1] >= T1_MINCBLKH);
  1895.                                                 assert (parameters->cblock_init[2] <= T1_MAXCBLKD);
  1896.                                                 assert (parameters->cblock_init[2] >= T1_MINCBLKD);
  1897.                         tccp->cblk[0] = int_floorlog2(parameters->cblock_init[0]);
  1898.                         tccp->cblk[1] = int_floorlog2(parameters->cblock_init[1]);
  1899.                         tccp->cblk[2] = int_floorlog2(parameters->cblock_init[2]);
  1900.                                                 assert (tccp->cblk[0]+tccp->cblk[1]+tccp->cblk[1] <= T1_MAXWHD);
  1901.                         tccp->cblksty = parameters->mode; //Codeblock style --> Table A.19 (default 0)
  1902.  
  1903.                         /*ATK / transform */
  1904.                         tccp->reversible = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */
  1905.                         for (j = 0; j < 3; j++) {
  1906.                                         tccp->dwtid[j] = parameters->irreversible ? 0 : 1; /* 0 => DWT 9-7 || 1 => DWT 5-3  */
  1907.                         }
  1908.                                                
  1909.                         /* Quantification: SEQNT (Scalar Expounded, value for each subband) / NOQNT (no quant)*/
  1910.                         tccp->qntsty = parameters->irreversible ? J3D_CCP_QNTSTY_SEQNT : J3D_CCP_QNTSTY_NOQNT;
  1911.                         tccp->numgbits = 2;
  1912.                         if (i == parameters->roi_compno) {
  1913.                                 tccp->roishift = parameters->roi_shift;
  1914.                         } else {
  1915.                                 tccp->roishift = 0;
  1916.                         }
  1917.                         /* Custom defined precints */
  1918.                         if (parameters->csty & J3D_CCP_CSTY_PRT) {
  1919.                                 int k;
  1920.                                 for (k = 0; k < 3; k++) {
  1921.                                         int p = 0;
  1922.                                         for (j = tccp->numresolution[k] - 1; j >= 0; j--) {
  1923.                                                 if (p < parameters->res_spec) {/* p < number of precinct size specifications */
  1924.                                                         if (parameters->prct_init[k][p] < 1) {
  1925.                                                                 tccp->prctsiz[k][j] = 1;
  1926.                                                         } else {
  1927.                                                                 tccp->prctsiz[k][j] = int_floorlog2(parameters->prct_init[k][p]);
  1928.                                                         }
  1929.                                                 } else {
  1930.                                                         int res_spec = parameters->res_spec;
  1931.                                                         int size_prct = parameters->prct_init[k][res_spec - 1] >> (p - (res_spec - 1));
  1932.                                                         if (size_prct < 1) {
  1933.                                                                 tccp->prctsiz[k][j] = 1;
  1934.                                                         } else {
  1935.                                                                 tccp->prctsiz[k][j] = int_floorlog2(size_prct);
  1936.                                                         }
  1937.                                                 }
  1938.                                         }
  1939.                                         p++;
  1940.                                 }
  1941.                         } else {
  1942.                                 int k;
  1943.                                 for (k = 0; k < 3; k++) {
  1944.                     for (j = 0; j < tccp->numresolution[k]; j++) {
  1945.                         tccp->prctsiz[k][j] = 15;
  1946.                                         }
  1947.                                 }
  1948.                         }
  1949.                         //Calcular stepsize for each subband (if NOQNT -->stepsize = 1.0)
  1950.                         dwt_calc_explicit_stepsizes(tccp, volume->comps[i].prec);
  1951.                 }
  1952.         }
  1953. }
  1954.  
  1955. /**
  1956. Create an index file
  1957. @param j3d
  1958. @param cio
  1959. @param volume_info
  1960. @param index Index filename
  1961. @return Returns 1 if successful, returns 0 otherwise
  1962. */
  1963. static int j3d_create_index(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_info_t *volume_info, char *index) {
  1964.        
  1965.         int tileno, compno, layno, resno, precno, pack_nb, x, y, z;
  1966.         FILE *stream = NULL;
  1967.         double total_disto = 0;
  1968.  
  1969.         volume_info->codestream_size = cio_tell(cio) + j3d->pos_correction;     /* Correction 14/4/03 suite rmq de Patrick */
  1970.  
  1971.         stream = fopen(index, "w");
  1972.         if (!stream) {
  1973.                 opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to open %s for writing\n", index);
  1974.                 return 0;
  1975.         }
  1976.        
  1977.         fprintf(stream, "w %d\t h %d\t l %d\n", volume_info->volume_w, volume_info->volume_h, volume_info->volume_l);
  1978.         fprintf(stream, "TRASNFORM\t%d\n", volume_info->transform_format);
  1979.         fprintf(stream, "ENTROPY CODING\t%d\n", volume_info->encoding_format);
  1980.         fprintf(stream, "PROG\t%d\n", volume_info->prog);
  1981.         fprintf(stream, "TILE\tx %d y %d z %d\n", volume_info->tile_x, volume_info->tile_y, volume_info->tile_z);
  1982.         fprintf(stream, "NOTILE\tx %d y %d z %d\n", volume_info->tw, volume_info->th, volume_info->tl);
  1983.         fprintf(stream, "COMPONENTS\t%d\n", volume_info->comp);
  1984.         fprintf(stream, "LAYER\t%d\n", volume_info->layer);
  1985.         fprintf(stream, "RESOLUTIONS\tx %d y %d z %d\n", volume_info->decomposition[0], volume_info->decomposition[1], volume_info->decomposition[2]);
  1986.        
  1987.         fprintf(stream, "Precint sizes for each resolution:\n");
  1988.         for (resno = volume_info->decomposition[0]; resno >= 0; resno--) {
  1989.                 fprintf(stream, "Resno %d \t [%d,%d,%d] \n", resno,
  1990.                         (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[0][resno]), (1 << volume_info->tile[0].prctsiz[2][resno]));   /* based on tile 0 */
  1991.         }
  1992.         fprintf(stream, "HEADER_END\t%d\n", volume_info->main_head_end);
  1993.         fprintf(stream, "CODESTREAM\t%d\n", volume_info->codestream_size);
  1994.         fprintf(stream, "Num_tile Start_pos End_header End_pos Distotile Nbpix Ratio\n");
  1995.         for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
  1996.                 fprintf(stream, "%4d\t%9d\t%9d\t%9d\t%9e\t%9d\t%9e\n",
  1997.                         volume_info->tile[tileno].num_tile,
  1998.                         volume_info->tile[tileno].start_pos,
  1999.                         volume_info->tile[tileno].end_header,
  2000.                         volume_info->tile[tileno].end_pos,
  2001.                         volume_info->tile[tileno].distotile, volume_info->tile[tileno].nbpix,
  2002.                         volume_info->tile[tileno].distotile / volume_info->tile[tileno].nbpix);
  2003.         }
  2004.        
  2005.         for (tileno = 0; tileno < (volume_info->tw * volume_info->th * volume_info->tl); tileno++) {
  2006.                 int start_pos, end_pos;
  2007.                 double disto = 0;
  2008.                 pack_nb = 0;
  2009.                 if (volume_info->prog == LRCP) {        /* LRCP */
  2010.                         fprintf(stream, "pack_nb tileno layno resno compno precno start_pos  end_pos   disto\n");
  2011.                         for (layno = 0; layno < volume_info->layer; layno++) {
  2012.                                 for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
  2013.                                         for (compno = 0; compno < volume_info->comp; compno++) {
  2014.                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
  2015.                                                 for (precno = 0; precno < prec_max; precno++) {
  2016.                                                         start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
  2017.                                                         end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
  2018.                                                         disto = volume_info->tile[tileno].packet[pack_nb].disto;
  2019.                                                         fprintf(stream, "%4d %6d %7d %5d %6d %6d %9d %9d %8e\n",pack_nb, tileno, layno, resno, compno, precno, start_pos, end_pos, disto);
  2020.                                                         total_disto += disto;
  2021.                                                         pack_nb++;
  2022.                                                 }
  2023.                                         }
  2024.                                 }
  2025.                         }
  2026.                 } /* LRCP */
  2027.                 else if (volume_info->prog == RLCP) {   /* RLCP */
  2028.                         /*
  2029.                         fprintf(stream, "pack_nb tileno resno layno compno precno start_pos  end_pos   disto");
  2030.                         */
  2031.                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
  2032.                                 for (layno = 0; layno < volume_info->layer; layno++) {
  2033.                                         for (compno = 0; compno < volume_info->comp; compno++) {
  2034.                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno]* volume_info->tile[tileno].prctno[2][resno];
  2035.                                                 for (precno = 0; precno < prec_max; precno++) {
  2036.                                                         start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
  2037.                                                         end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
  2038.                                                         disto = volume_info->tile[tileno].packet[pack_nb].disto;
  2039.                                                         fprintf(stream, "%4d %6d %5d %7d %6d %6d %9d %9d %8e\n",
  2040.                                                                 pack_nb, tileno, resno, layno, compno, precno, start_pos, end_pos, disto);
  2041.                                                         total_disto += disto;
  2042.                                                         pack_nb++;
  2043.                                                 }
  2044.                                         }
  2045.                                 }
  2046.                         }
  2047.                 } /* RLCP */
  2048.                 else if (volume_info->prog == RPCL) {   /* RPCL */
  2049.                         /*
  2050.                         fprintf(stream, "\npack_nb tileno resno precno compno layno start_pos  end_pos   disto\n");
  2051.                         */
  2052.                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
  2053.                                 /* I suppose components have same XRsiz, YRsiz */
  2054.                                 //int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
  2055.                                 //int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
  2056.                                 int x0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_x;
  2057.                                 int y0 = volume_info->tile_Oy + (int)floor( (float)tileno/(float)volume_info->th ) * volume_info->tile_y;
  2058.                                 int z0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tl ) * volume_info->tile_z;
  2059.                                 int x1 = x0 + volume_info->tile_x;
  2060.                                 int y1 = y0 + volume_info->tile_y;
  2061.                                 int z1 = z0 + volume_info->tile_z;
  2062.                                 for(z = z0; z < z1; z++) {
  2063.                                         for(y = y0; y < y1; y++) {
  2064.                                                 for(x = x0; x < x1; x++) {
  2065.                                                         for (compno = 0; compno < volume_info->comp; compno++) {
  2066.                                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
  2067.                                                                 for (precno = 0; precno < prec_max; precno++) {
  2068.                                                                         int pcnx = volume_info->tile[tileno].prctno[0][resno];
  2069.                                                                         int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
  2070.                                                                         int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
  2071.                                                                         int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
  2072.                                                                         int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
  2073.                                                                         int precno_y = (int) floor( (float)precno/(float)pcnx );
  2074.                                                                         if (precno_y*pcy == y ) {
  2075.                                                                                 if (precno_x*pcx == x ) {
  2076.                                                                                         for (layno = 0; layno < volume_info->layer; layno++) {
  2077.                                                                                                 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
  2078.                                                                                                 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
  2079.                                                                                                 disto = volume_info->tile[tileno].packet[pack_nb].disto;
  2080.                                                                                                 fprintf(stream, "%4d %6d %5d %6d %6d %7d %9d %9d %8e\n",
  2081.                                                                                                         pack_nb, tileno, resno, precno, compno, layno, start_pos, end_pos, disto);
  2082.                                                                                                 total_disto += disto;
  2083.                                                                                                 pack_nb++;
  2084.                                                                                         }
  2085.                                                                                 }
  2086.                                                                         }
  2087.                                                                 } /* precno */
  2088.                                                         } /* compno */
  2089.                                                 } /* x = x0..x1 */
  2090.                                         } /* y = y0..y1 */
  2091.                                 } /* z = z0..z1 */
  2092.                         } /* resno */
  2093.                 } /* RPCL */
  2094.                 else if (volume_info->prog == PCRL) {   /* PCRL */
  2095.                         /* I suppose components have same XRsiz, YRsiz */
  2096.                         int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
  2097.                         int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
  2098.                         int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
  2099.                         int x1 = x0 + volume_info->tile_x;
  2100.                         int y1 = y0 + volume_info->tile_y;
  2101.                         int z1 = z0 + volume_info->tile_z;
  2102.                         /*
  2103.                         fprintf(stream, "\npack_nb tileno precno compno resno layno start_pos  end_pos   disto\n");
  2104.                         */
  2105.                         for(z = z0; z < z1; z++) {
  2106.                                 for(y = y0; y < y1; y++) {
  2107.                                         for(x = x0; x < x1; x++) {
  2108.                                                 for (compno = 0; compno < volume_info->comp; compno++) {
  2109.                                                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
  2110.                                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno];
  2111.                                                                 for (precno = 0; precno < prec_max; precno++) {
  2112.                                                                 int pcnx = volume_info->tile[tileno].prctno[0][resno];
  2113.                                                                 int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
  2114.                                                                 int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
  2115.                                                                 int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
  2116.                                                                 int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
  2117.                                                                 int precno_y = (int) floor( (float)precno/(float)pcnx );
  2118.                                                                 int precno_z = (int) floor( (float)precno/(float)pcnx );
  2119.                                                                 if (precno_z*pcz == z ) {
  2120.                                                                         if (precno_y*pcy == y ) {
  2121.                                                                                 if (precno_x*pcx == x ) {
  2122.                                                                                         for (layno = 0; layno < volume_info->layer; layno++) {
  2123.                                                                                                 start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
  2124.                                                                                                 end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
  2125.                                                                                                 disto = volume_info->tile[tileno].packet[pack_nb].disto;
  2126.                                                                                                 fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
  2127.                                                                                                         pack_nb, tileno, precno, compno, resno, layno, start_pos, end_pos, disto);
  2128.                                                                                                 total_disto += disto;
  2129.                                                                                                 pack_nb++;
  2130.                                                                                         }
  2131.                                                                                 }
  2132.                                                                         }
  2133.                                                                 }
  2134.                                                         } /* precno */
  2135.                                                 } /* resno */
  2136.                                         } /* compno */
  2137.                                 } /* x = x0..x1 */
  2138.                         } /* y = y0..y1 */
  2139.                         }
  2140.                 } /* PCRL */
  2141.                 else {  /* CPRL */
  2142.                         /*
  2143.                         fprintf(stream, "\npack_nb tileno compno precno resno layno start_pos  end_pos   disto\n");
  2144.                         */
  2145.                         for (compno = 0; compno < volume_info->comp; compno++) {
  2146.                                 /* I suppose components have same XRsiz, YRsiz */
  2147.                                 int x0 = volume_info->tile_Ox + tileno - (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tw * volume_info->tile_x;
  2148.                                 int y0 = volume_info->tile_Ox + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_y;
  2149.                                 int z0 = volume_info->tile_Oz + (int)floor( (float)tileno/(float)volume_info->tw ) * volume_info->tile_z;
  2150.                                 int x1 = x0 + volume_info->tile_x;
  2151.                                 int y1 = y0 + volume_info->tile_y;
  2152.                                 int z1 = z0 + volume_info->tile_z;
  2153.                                 for(z = z0; z < z1; z++) {
  2154.                                         for(y = y0; y < y1; y++) {
  2155.                                                 for(x = x0; x < x1; x++) {
  2156.                                                         for (resno = 0; resno < volume_info->decomposition[0] + 1; resno++) {
  2157.                                                                 int prec_max = volume_info->tile[tileno].prctno[0][resno] * volume_info->tile[tileno].prctno[1][resno] * volume_info->tile[tileno].prctno[2][resno];
  2158.                                                                 for (precno = 0; precno < prec_max; precno++) {
  2159.                                                                         int pcnx = volume_info->tile[tileno].prctno[0][resno];
  2160.                                                                         int pcny = volume_info->tile[tileno].prctno[1][resno];
  2161.                                                                         int pcx = (int) pow( 2, volume_info->tile[tileno].prctsiz[0][resno] + volume_info->decomposition[0] - resno );
  2162.                                                                         int pcy = (int) pow( 2, volume_info->tile[tileno].prctsiz[1][resno] + volume_info->decomposition[1] - resno );
  2163.                                                                         int pcz = (int) pow( 2, volume_info->tile[tileno].prctsiz[2][resno] + volume_info->decomposition[2] - resno );
  2164.                                                                         int precno_x = precno - (int) floor( (float)precno/(float)pcnx ) * pcnx;
  2165.                                                                         int precno_y = (int) floor( (float)precno/(float)pcnx );
  2166.                                                                         int precno_z = 0; /*???*/
  2167.                                                                         if (precno_z*pcz == z ) {
  2168.                                                                                 if (precno_y*pcy == y ) {
  2169.                                                                                         if (precno_x*pcx == x ) {
  2170.                                                                                                 for (layno = 0; layno < volume_info->layer; layno++) {
  2171.                                                                                                         start_pos = volume_info->tile[tileno].packet[pack_nb].start_pos;
  2172.                                                                                                         end_pos = volume_info->tile[tileno].packet[pack_nb].end_pos;
  2173.                                                                                                         disto = volume_info->tile[tileno].packet[pack_nb].disto;
  2174.                                                                                                         fprintf(stream, "%4d %6d %6d %6d %5d %7d %9d %9d %8e\n",
  2175.                                                                                                                 pack_nb, tileno, compno, precno, resno, layno, start_pos, end_pos, disto);
  2176.                                                                                                         total_disto += disto;
  2177.                                                                                                         pack_nb++;
  2178.                                                                                                 }
  2179.                                                                                         }
  2180.                                                                                 }
  2181.                                                                         }
  2182.                                                                 } /* precno */
  2183.                                                         } /* resno */
  2184.                                                 } /* x = x0..x1 */
  2185.                                         } /* y = y0..y1 */
  2186.                                 } /* z = z0..z1 */
  2187.                         } /* comno */
  2188.                 } /* CPRL */  
  2189.         } /* tileno */
  2190.        
  2191.         fprintf(stream, "SE_MAX\t%8e\n", volume_info->D_max);   /* SE max */
  2192.         fprintf(stream, "SE_TOTAL\t%.8e\n", total_disto);                       /* SE totale */
  2193.        
  2194.  
  2195.         fclose(stream);
  2196.  
  2197.         return 1;
  2198. }
  2199.  
  2200. bool j3d_encode(opj_j3d_t *j3d, opj_cio_t *cio, opj_volume_t *volume, char *index) {
  2201.         int tileno, compno;
  2202.         opj_volume_info_t *volume_info = NULL;
  2203.         opj_cp_t *cp = NULL;
  2204.         opj_tcd_t *tcd = NULL;  /* TCD component */
  2205.  
  2206.         j3d->cio = cio;
  2207.         j3d->volume = volume;
  2208.         cp = j3d->cp;
  2209.  
  2210.         /*j3d_dump_volume(stdout, volume);
  2211.         j3d_dump_cp(stdout, volume, cp);*/
  2212.  
  2213.         /* INDEX >> */
  2214.         volume_info = j3d->volume_info;
  2215.         if (volume_info && cp->index_on) {
  2216.                 volume_info->index_on = cp->index_on;
  2217.                 volume_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * cp->tl * sizeof(opj_tile_info_t));
  2218.                 volume_info->volume_w = volume->x1 - volume->x0;
  2219.                 volume_info->volume_h = volume->y1 - volume->y0;
  2220.                 volume_info->volume_l = volume->z1 - volume->z0;
  2221.                 volume_info->prog = (&cp->tcps[0])->prg;
  2222.                 volume_info->tw = cp->tw;
  2223.                 volume_info->th = cp->th;
  2224.                 volume_info->tl = cp->tl;
  2225.                 volume_info->tile_x = cp->tdx;  /* new version parser */
  2226.                 volume_info->tile_y = cp->tdy;  /* new version parser */
  2227.                 volume_info->tile_z = cp->tdz;  /* new version parser */
  2228.                 volume_info->tile_Ox = cp->tx0; /* new version parser */
  2229.                 volume_info->tile_Oy = cp->ty0; /* new version parser */
  2230.                 volume_info->tile_Oz = cp->tz0; /* new version parser */
  2231.                 volume_info->transform_format = cp->transform_format;
  2232.                 volume_info->encoding_format = cp->encoding_format;
  2233.                 volume_info->comp = volume->numcomps;
  2234.                 volume_info->layer = (&cp->tcps[0])->numlayers;
  2235.                 volume_info->decomposition[0] = (&cp->tcps[0])->tccps->numresolution[0] - 1;
  2236.                 volume_info->decomposition[1] = (&cp->tcps[0])->tccps->numresolution[1] - 1;
  2237.                 volume_info->decomposition[2] = (&cp->tcps[0])->tccps->numresolution[2] - 1;
  2238.                 volume_info->D_max = 0;         /* ADD Marcela */
  2239.         }
  2240.         /* << INDEX */
  2241.  
  2242.         j3d_write_soc(j3d);
  2243.         j3d_write_siz(j3d);
  2244.         if (j3d->cinfo->codec_format == CODEC_J3D) {
  2245.                 j3d_write_cap(j3d);
  2246.                 j3d_write_zsi(j3d);
  2247.         }
  2248.         j3d_write_cod(j3d);
  2249.         j3d_write_qcd(j3d);
  2250.         for (compno = 0; compno < volume->numcomps; compno++) {
  2251.                 opj_tcp_t *tcp = &cp->tcps[0];
  2252.                 if (tcp->tccps[compno].roishift)
  2253.                         j3d_write_rgn(j3d, compno, 0);                 
  2254.         }
  2255.         /*Optional 15444-2 markers*/
  2256.         if (j3d->cp->tcps->tccps[0].atk != NULL)
  2257.         j3d_write_atk(j3d);
  2258.         if (j3d->volume->comps[0].dcoffset != 0)
  2259.         j3d_write_dco(j3d);
  2260.  
  2261.         if (j3d->cp->transform_format != TRF_2D_DWT || j3d->cp->encoding_format != ENCOD_2EB)
  2262.                 j3d_write_com(j3d);
  2263.        
  2264.         /* INDEX >> */
  2265.         if(volume_info && volume_info->index_on) {
  2266.                 volume_info->main_head_end = cio_tell(cio) - 1;
  2267.         }
  2268.         /* << INDEX */
  2269.  
  2270.         /* create the tile encoder */
  2271.         tcd = tcd_create(j3d->cinfo);
  2272.  
  2273.         /* encode each tile */
  2274.         for (tileno = 0; tileno < cp->tw * cp->th * cp->tl; tileno++) {
  2275.                 opj_event_msg(j3d->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th * cp->tl);
  2276.                
  2277.                 j3d->curtileno = tileno;
  2278.  
  2279.                 /* initialisation before tile encoding  */
  2280.                 if (tileno == 0) {     
  2281.                         tcd_malloc_encode(tcd, volume, cp, j3d->curtileno);
  2282.                 } else {
  2283.                         tcd_init_encode(tcd, volume, cp, j3d->curtileno);
  2284.                 }
  2285.                
  2286.                 /* INDEX >> */
  2287.                 if(volume_info && volume_info->index_on) {
  2288.                         volume_info->tile[j3d->curtileno].num_tile = j3d->curtileno;
  2289.                         volume_info->tile[j3d->curtileno].start_pos = cio_tell(cio) + j3d->pos_correction;
  2290.                 }
  2291.                 /* << INDEX */
  2292.                
  2293.                 j3d_write_sot(j3d);
  2294.        
  2295.                 for (compno = 1; compno < volume->numcomps; compno++) {
  2296.                         j3d_write_coc(j3d, compno);
  2297.                         j3d_write_qcc(j3d, compno);
  2298.                 }
  2299.  
  2300.                 if (cp->tcps[tileno].numpocs) {
  2301.                         j3d_write_poc(j3d);
  2302.                 }
  2303.                 j3d_write_sod(j3d, tcd); //--> tcd_encode_tile
  2304.  
  2305.                 /* INDEX >> */
  2306.                 if(volume_info && volume_info->index_on) {
  2307.                         volume_info->tile[j3d->curtileno].end_pos = cio_tell(cio) + j3d->pos_correction - 1;
  2308.                 }
  2309.                 /* << INDEX */         
  2310.         }
  2311.        
  2312.         /* destroy the tile encoder */
  2313.         tcd_free_encode(tcd);
  2314.         tcd_destroy(tcd);
  2315.  
  2316.         j3d_write_eoc(j3d);
  2317.        
  2318.         /* Creation of the index file */
  2319.         if(volume_info && volume_info->index_on) {
  2320.                 if(!j3d_create_index(j3d, cio, volume_info, index)) {
  2321.                         opj_event_msg(j3d->cinfo, EVT_ERROR, "failed to create index file %s\n", index);
  2322.                         return false;
  2323.                 }
  2324.         }
  2325.          
  2326.         return true;
  2327. }
  2328.  
  2329.