PageRenderTime 460ms CodeModel.GetById 47ms RepoModel.GetById 10ms app.codeStats 1ms

/src/FreeImage/Source/LibOpenJPEG/tcd.c

https://bitbucket.org/cabalistic/ogredeps/
C | 1524 lines | 1151 code | 237 blank | 136 comment | 274 complexity | 9290d021d70d51991d16f4b8f83f4ab7 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * Copyright (c) 2006-2007, Parvatha Elangovan
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "opj_includes.h"
  33. void tcd_dump(FILE *fd, opj_tcd_t *tcd, opj_tcd_image_t * img) {
  34. int tileno, compno, resno, bandno, precno;/*, cblkno;*/
  35. fprintf(fd, "image {\n");
  36. fprintf(fd, " tw=%d, th=%d x0=%d x1=%d y0=%d y1=%d\n",
  37. img->tw, img->th, tcd->image->x0, tcd->image->x1, tcd->image->y0, tcd->image->y1);
  38. for (tileno = 0; tileno < img->th * img->tw; tileno++) {
  39. opj_tcd_tile_t *tile = &tcd->tcd_image->tiles[tileno];
  40. fprintf(fd, " tile {\n");
  41. fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d, numcomps=%d\n",
  42. tile->x0, tile->y0, tile->x1, tile->y1, tile->numcomps);
  43. for (compno = 0; compno < tile->numcomps; compno++) {
  44. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  45. fprintf(fd, " tilec {\n");
  46. fprintf(fd,
  47. " x0=%d, y0=%d, x1=%d, y1=%d, numresolutions=%d\n",
  48. tilec->x0, tilec->y0, tilec->x1, tilec->y1, tilec->numresolutions);
  49. for (resno = 0; resno < tilec->numresolutions; resno++) {
  50. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  51. fprintf(fd, "\n res {\n");
  52. fprintf(fd,
  53. " x0=%d, y0=%d, x1=%d, y1=%d, pw=%d, ph=%d, numbands=%d\n",
  54. res->x0, res->y0, res->x1, res->y1, res->pw, res->ph, res->numbands);
  55. for (bandno = 0; bandno < res->numbands; bandno++) {
  56. opj_tcd_band_t *band = &res->bands[bandno];
  57. fprintf(fd, " band {\n");
  58. fprintf(fd,
  59. " x0=%d, y0=%d, x1=%d, y1=%d, stepsize=%f, numbps=%d\n",
  60. band->x0, band->y0, band->x1, band->y1, band->stepsize, band->numbps);
  61. for (precno = 0; precno < res->pw * res->ph; precno++) {
  62. opj_tcd_precinct_t *prec = &band->precincts[precno];
  63. fprintf(fd, " prec {\n");
  64. fprintf(fd,
  65. " x0=%d, y0=%d, x1=%d, y1=%d, cw=%d, ch=%d\n",
  66. prec->x0, prec->y0, prec->x1, prec->y1, prec->cw, prec->ch);
  67. /*
  68. for (cblkno = 0; cblkno < prec->cw * prec->ch; cblkno++) {
  69. opj_tcd_cblk_t *cblk = &prec->cblks[cblkno];
  70. fprintf(fd, " cblk {\n");
  71. fprintf(fd,
  72. " x0=%d, y0=%d, x1=%d, y1=%d\n",
  73. cblk->x0, cblk->y0, cblk->x1, cblk->y1);
  74. fprintf(fd, " }\n");
  75. }
  76. */
  77. fprintf(fd, " }\n");
  78. }
  79. fprintf(fd, " }\n");
  80. }
  81. fprintf(fd, " }\n");
  82. }
  83. fprintf(fd, " }\n");
  84. }
  85. fprintf(fd, " }\n");
  86. }
  87. fprintf(fd, "}\n");
  88. }
  89. /* ----------------------------------------------------------------------- */
  90. /**
  91. Create a new TCD handle
  92. */
  93. opj_tcd_t* tcd_create(opj_common_ptr cinfo) {
  94. /* create the tcd structure */
  95. opj_tcd_t *tcd = (opj_tcd_t*)opj_malloc(sizeof(opj_tcd_t));
  96. if(!tcd) return NULL;
  97. tcd->cinfo = cinfo;
  98. tcd->tcd_image = (opj_tcd_image_t*)opj_malloc(sizeof(opj_tcd_image_t));
  99. if(!tcd->tcd_image) {
  100. opj_free(tcd);
  101. return NULL;
  102. }
  103. return tcd;
  104. }
  105. /**
  106. Destroy a previously created TCD handle
  107. */
  108. void tcd_destroy(opj_tcd_t *tcd) {
  109. if(tcd) {
  110. opj_free(tcd->tcd_image);
  111. opj_free(tcd);
  112. }
  113. }
  114. /* ----------------------------------------------------------------------- */
  115. void tcd_malloc_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
  116. int tileno, compno, resno, bandno, precno, cblkno;
  117. tcd->image = image;
  118. tcd->cp = cp;
  119. tcd->tcd_image->tw = cp->tw;
  120. tcd->tcd_image->th = cp->th;
  121. tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(sizeof(opj_tcd_tile_t));
  122. for (tileno = 0; tileno < 1; tileno++) {
  123. opj_tcp_t *tcp = &cp->tcps[curtileno];
  124. int j;
  125. /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  126. int p = curtileno % cp->tw; /* si numerotation matricielle .. */
  127. int q = curtileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
  128. /* opj_tcd_tile_t *tile=&tcd->tcd_image->tiles[tileno]; */
  129. opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
  130. /* 4 borders of the tile rescale on the image if necessary */
  131. tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
  132. tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
  133. tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
  134. tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
  135. tile->numcomps = image->numcomps;
  136. /* tile->PPT=image->PPT; */
  137. /* Modification of the RATE >> */
  138. for (j = 0; j < tcp->numlayers; j++) {
  139. tcp->rates[j] = tcp->rates[j] ?
  140. cp->tp_on ?
  141. (((float) (tile->numcomps
  142. * (tile->x1 - tile->x0)
  143. * (tile->y1 - tile->y0)
  144. * image->comps[0].prec))
  145. /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
  146. :
  147. ((float) (tile->numcomps
  148. * (tile->x1 - tile->x0)
  149. * (tile->y1 - tile->y0)
  150. * image->comps[0].prec))/
  151. (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
  152. : 0;
  153. if (tcp->rates[j]) {
  154. if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
  155. tcp->rates[j] = tcp->rates[j - 1] + 20;
  156. } else {
  157. if (!j && tcp->rates[j] < 30)
  158. tcp->rates[j] = 30;
  159. }
  160. if(j == (tcp->numlayers-1)){
  161. tcp->rates[j] = tcp->rates[j]- 2;
  162. }
  163. }
  164. }
  165. /* << Modification of the RATE */
  166. tile->comps = (opj_tcd_tilecomp_t *) opj_malloc(image->numcomps * sizeof(opj_tcd_tilecomp_t));
  167. for (compno = 0; compno < tile->numcomps; compno++) {
  168. opj_tccp_t *tccp = &tcp->tccps[compno];
  169. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  170. /* border of each tile component (global) */
  171. tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
  172. tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
  173. tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
  174. tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
  175. tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
  176. tilec->numresolutions = tccp->numresolutions;
  177. tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
  178. for (resno = 0; resno < tilec->numresolutions; resno++) {
  179. int pdx, pdy;
  180. int levelno = tilec->numresolutions - 1 - resno;
  181. int tlprcxstart, tlprcystart, brprcxend, brprcyend;
  182. int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
  183. int cbgwidthexpn, cbgheightexpn;
  184. int cblkwidthexpn, cblkheightexpn;
  185. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  186. /* border for each resolution level (global) */
  187. res->x0 = int_ceildivpow2(tilec->x0, levelno);
  188. res->y0 = int_ceildivpow2(tilec->y0, levelno);
  189. res->x1 = int_ceildivpow2(tilec->x1, levelno);
  190. res->y1 = int_ceildivpow2(tilec->y1, levelno);
  191. res->numbands = resno == 0 ? 1 : 3;
  192. /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
  193. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  194. pdx = tccp->prcw[resno];
  195. pdy = tccp->prch[resno];
  196. } else {
  197. pdx = 15;
  198. pdy = 15;
  199. }
  200. /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  201. tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
  202. tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
  203. brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
  204. brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
  205. res->pw = (brprcxend - tlprcxstart) >> pdx;
  206. res->ph = (brprcyend - tlprcystart) >> pdy;
  207. if (resno == 0) {
  208. tlcbgxstart = tlprcxstart;
  209. tlcbgystart = tlprcystart;
  210. brcbgxend = brprcxend;
  211. brcbgyend = brprcyend;
  212. cbgwidthexpn = pdx;
  213. cbgheightexpn = pdy;
  214. } else {
  215. tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
  216. tlcbgystart = int_ceildivpow2(tlprcystart, 1);
  217. brcbgxend = int_ceildivpow2(brprcxend, 1);
  218. brcbgyend = int_ceildivpow2(brprcyend, 1);
  219. cbgwidthexpn = pdx - 1;
  220. cbgheightexpn = pdy - 1;
  221. }
  222. cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
  223. cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
  224. for (bandno = 0; bandno < res->numbands; bandno++) {
  225. int x0b, y0b, i;
  226. int gain, numbps;
  227. opj_stepsize_t *ss = NULL;
  228. opj_tcd_band_t *band = &res->bands[bandno];
  229. band->bandno = resno == 0 ? 0 : bandno + 1;
  230. x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
  231. y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
  232. if (band->bandno == 0) {
  233. /* band border (global) */
  234. band->x0 = int_ceildivpow2(tilec->x0, levelno);
  235. band->y0 = int_ceildivpow2(tilec->y0, levelno);
  236. band->x1 = int_ceildivpow2(tilec->x1, levelno);
  237. band->y1 = int_ceildivpow2(tilec->y1, levelno);
  238. } else {
  239. /* band border (global) */
  240. band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
  241. band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
  242. band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
  243. band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
  244. }
  245. ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
  246. gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
  247. numbps = image->comps[compno].prec + gain;
  248. band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
  249. band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
  250. band->precincts = (opj_tcd_precinct_t *) opj_malloc(3 * res->pw * res->ph * sizeof(opj_tcd_precinct_t));
  251. for (i = 0; i < res->pw * res->ph * 3; i++) {
  252. band->precincts[i].imsbtree = NULL;
  253. band->precincts[i].incltree = NULL;
  254. band->precincts[i].cblks.enc = NULL;
  255. }
  256. for (precno = 0; precno < res->pw * res->ph; precno++) {
  257. int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
  258. int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
  259. int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
  260. int cbgxend = cbgxstart + (1 << cbgwidthexpn);
  261. int cbgyend = cbgystart + (1 << cbgheightexpn);
  262. opj_tcd_precinct_t *prc = &band->precincts[precno];
  263. /* precinct size (global) */
  264. prc->x0 = int_max(cbgxstart, band->x0);
  265. prc->y0 = int_max(cbgystart, band->y0);
  266. prc->x1 = int_min(cbgxend, band->x1);
  267. prc->y1 = int_min(cbgyend, band->y1);
  268. tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
  269. tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
  270. brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
  271. brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
  272. prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
  273. prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
  274. prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc((prc->cw * prc->ch), sizeof(opj_tcd_cblk_enc_t));
  275. prc->incltree = tgt_create(prc->cw, prc->ch);
  276. prc->imsbtree = tgt_create(prc->cw, prc->ch);
  277. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  278. int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
  279. int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
  280. int cblkxend = cblkxstart + (1 << cblkwidthexpn);
  281. int cblkyend = cblkystart + (1 << cblkheightexpn);
  282. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  283. /* code-block size (global) */
  284. cblk->x0 = int_max(cblkxstart, prc->x0);
  285. cblk->y0 = int_max(cblkystart, prc->y0);
  286. cblk->x1 = int_min(cblkxend, prc->x1);
  287. cblk->y1 = int_min(cblkyend, prc->y1);
  288. cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
  289. /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
  290. cblk->data += 2;
  291. cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
  292. cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
  300. }
  301. void tcd_free_encode(opj_tcd_t *tcd) {
  302. int tileno, compno, resno, bandno, precno, cblkno;
  303. for (tileno = 0; tileno < 1; tileno++) {
  304. opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
  305. for (compno = 0; compno < tile->numcomps; compno++) {
  306. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  307. for (resno = 0; resno < tilec->numresolutions; resno++) {
  308. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  309. for (bandno = 0; bandno < res->numbands; bandno++) {
  310. opj_tcd_band_t *band = &res->bands[bandno];
  311. for (precno = 0; precno < res->pw * res->ph; precno++) {
  312. opj_tcd_precinct_t *prc = &band->precincts[precno];
  313. if (prc->incltree != NULL) {
  314. tgt_destroy(prc->incltree);
  315. prc->incltree = NULL;
  316. }
  317. if (prc->imsbtree != NULL) {
  318. tgt_destroy(prc->imsbtree);
  319. prc->imsbtree = NULL;
  320. }
  321. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  322. opj_free(prc->cblks.enc[cblkno].data - 2);
  323. opj_free(prc->cblks.enc[cblkno].layers);
  324. opj_free(prc->cblks.enc[cblkno].passes);
  325. }
  326. opj_free(prc->cblks.enc);
  327. } /* for (precno */
  328. opj_free(band->precincts);
  329. band->precincts = NULL;
  330. } /* for (bandno */
  331. } /* for (resno */
  332. opj_free(tilec->resolutions);
  333. tilec->resolutions = NULL;
  334. } /* for (compno */
  335. opj_free(tile->comps);
  336. tile->comps = NULL;
  337. } /* for (tileno */
  338. opj_free(tcd->tcd_image->tiles);
  339. tcd->tcd_image->tiles = NULL;
  340. }
  341. void tcd_init_encode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int curtileno) {
  342. int tileno, compno, resno, bandno, precno, cblkno;
  343. for (tileno = 0; tileno < 1; tileno++) {
  344. opj_tcp_t *tcp = &cp->tcps[curtileno];
  345. int j;
  346. /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  347. int p = curtileno % cp->tw;
  348. int q = curtileno / cp->tw;
  349. opj_tcd_tile_t *tile = tcd->tcd_image->tiles;
  350. /* 4 borders of the tile rescale on the image if necessary */
  351. tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
  352. tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
  353. tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
  354. tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
  355. tile->numcomps = image->numcomps;
  356. /* tile->PPT=image->PPT; */
  357. /* Modification of the RATE >> */
  358. for (j = 0; j < tcp->numlayers; j++) {
  359. tcp->rates[j] = tcp->rates[j] ?
  360. cp->tp_on ?
  361. (((float) (tile->numcomps
  362. * (tile->x1 - tile->x0)
  363. * (tile->y1 - tile->y0)
  364. * image->comps[0].prec))
  365. /(tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)) - (((tcd->cur_totnum_tp - 1) * 14 )/ tcp->numlayers)
  366. :
  367. ((float) (tile->numcomps
  368. * (tile->x1 - tile->x0)
  369. * (tile->y1 - tile->y0)
  370. * image->comps[0].prec))/
  371. (tcp->rates[j] * 8 * image->comps[0].dx * image->comps[0].dy)
  372. : 0;
  373. if (tcp->rates[j]) {
  374. if (j && tcp->rates[j] < tcp->rates[j - 1] + 10) {
  375. tcp->rates[j] = tcp->rates[j - 1] + 20;
  376. } else {
  377. if (!j && tcp->rates[j] < 30)
  378. tcp->rates[j] = 30;
  379. }
  380. }
  381. }
  382. /* << Modification of the RATE */
  383. /* tile->comps=(opj_tcd_tilecomp_t*)opj_realloc(tile->comps,image->numcomps*sizeof(opj_tcd_tilecomp_t)); */
  384. for (compno = 0; compno < tile->numcomps; compno++) {
  385. opj_tccp_t *tccp = &tcp->tccps[compno];
  386. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  387. /* border of each tile component (global) */
  388. tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
  389. tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
  390. tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
  391. tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
  392. tilec->data = (int *) opj_aligned_malloc((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0) * sizeof(int));
  393. tilec->numresolutions = tccp->numresolutions;
  394. /* tilec->resolutions=(opj_tcd_resolution_t*)opj_realloc(tilec->resolutions,tilec->numresolutions*sizeof(opj_tcd_resolution_t)); */
  395. for (resno = 0; resno < tilec->numresolutions; resno++) {
  396. int pdx, pdy;
  397. int levelno = tilec->numresolutions - 1 - resno;
  398. int tlprcxstart, tlprcystart, brprcxend, brprcyend;
  399. int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
  400. int cbgwidthexpn, cbgheightexpn;
  401. int cblkwidthexpn, cblkheightexpn;
  402. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  403. /* border for each resolution level (global) */
  404. res->x0 = int_ceildivpow2(tilec->x0, levelno);
  405. res->y0 = int_ceildivpow2(tilec->y0, levelno);
  406. res->x1 = int_ceildivpow2(tilec->x1, levelno);
  407. res->y1 = int_ceildivpow2(tilec->y1, levelno);
  408. res->numbands = resno == 0 ? 1 : 3;
  409. /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
  410. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  411. pdx = tccp->prcw[resno];
  412. pdy = tccp->prch[resno];
  413. } else {
  414. pdx = 15;
  415. pdy = 15;
  416. }
  417. /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  418. tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
  419. tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
  420. brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
  421. brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
  422. res->pw = (brprcxend - tlprcxstart) >> pdx;
  423. res->ph = (brprcyend - tlprcystart) >> pdy;
  424. if (resno == 0) {
  425. tlcbgxstart = tlprcxstart;
  426. tlcbgystart = tlprcystart;
  427. brcbgxend = brprcxend;
  428. brcbgyend = brprcyend;
  429. cbgwidthexpn = pdx;
  430. cbgheightexpn = pdy;
  431. } else {
  432. tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
  433. tlcbgystart = int_ceildivpow2(tlprcystart, 1);
  434. brcbgxend = int_ceildivpow2(brprcxend, 1);
  435. brcbgyend = int_ceildivpow2(brprcyend, 1);
  436. cbgwidthexpn = pdx - 1;
  437. cbgheightexpn = pdy - 1;
  438. }
  439. cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
  440. cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
  441. for (bandno = 0; bandno < res->numbands; bandno++) {
  442. int x0b, y0b;
  443. int gain, numbps;
  444. opj_stepsize_t *ss = NULL;
  445. opj_tcd_band_t *band = &res->bands[bandno];
  446. band->bandno = resno == 0 ? 0 : bandno + 1;
  447. x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
  448. y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
  449. if (band->bandno == 0) {
  450. /* band border */
  451. band->x0 = int_ceildivpow2(tilec->x0, levelno);
  452. band->y0 = int_ceildivpow2(tilec->y0, levelno);
  453. band->x1 = int_ceildivpow2(tilec->x1, levelno);
  454. band->y1 = int_ceildivpow2(tilec->y1, levelno);
  455. } else {
  456. band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
  457. band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
  458. band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
  459. band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
  460. }
  461. ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
  462. gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
  463. numbps = image->comps[compno].prec + gain;
  464. band->stepsize = (float)((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn));
  465. band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
  466. for (precno = 0; precno < res->pw * res->ph; precno++) {
  467. int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
  468. int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
  469. int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
  470. int cbgxend = cbgxstart + (1 << cbgwidthexpn);
  471. int cbgyend = cbgystart + (1 << cbgheightexpn);
  472. opj_tcd_precinct_t *prc = &band->precincts[precno];
  473. /* precinct size (global) */
  474. prc->x0 = int_max(cbgxstart, band->x0);
  475. prc->y0 = int_max(cbgystart, band->y0);
  476. prc->x1 = int_min(cbgxend, band->x1);
  477. prc->y1 = int_min(cbgyend, band->y1);
  478. tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
  479. tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
  480. brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
  481. brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
  482. prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
  483. prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
  484. opj_free(prc->cblks.enc);
  485. prc->cblks.enc = (opj_tcd_cblk_enc_t*) opj_calloc(prc->cw * prc->ch, sizeof(opj_tcd_cblk_enc_t));
  486. if (prc->incltree != NULL) {
  487. tgt_destroy(prc->incltree);
  488. }
  489. if (prc->imsbtree != NULL) {
  490. tgt_destroy(prc->imsbtree);
  491. }
  492. prc->incltree = tgt_create(prc->cw, prc->ch);
  493. prc->imsbtree = tgt_create(prc->cw, prc->ch);
  494. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  495. int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
  496. int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
  497. int cblkxend = cblkxstart + (1 << cblkwidthexpn);
  498. int cblkyend = cblkystart + (1 << cblkheightexpn);
  499. opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
  500. /* code-block size (global) */
  501. cblk->x0 = int_max(cblkxstart, prc->x0);
  502. cblk->y0 = int_max(cblkystart, prc->y0);
  503. cblk->x1 = int_min(cblkxend, prc->x1);
  504. cblk->y1 = int_min(cblkyend, prc->y1);
  505. cblk->data = (unsigned char*) opj_calloc(8192+2, sizeof(unsigned char));
  506. /* FIXME: mqc_init_enc and mqc_byteout underrun the buffer if we don't do this. Why? */
  507. cblk->data += 2;
  508. cblk->layers = (opj_tcd_layer_t*) opj_calloc(100, sizeof(opj_tcd_layer_t));
  509. cblk->passes = (opj_tcd_pass_t*) opj_calloc(100, sizeof(opj_tcd_pass_t));
  510. }
  511. } /* precno */
  512. } /* bandno */
  513. } /* resno */
  514. } /* compno */
  515. } /* tileno */
  516. /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
  517. }
  518. void tcd_malloc_decode(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp) {
  519. int i, j, tileno, p, q;
  520. unsigned int x0 = 0, y0 = 0, x1 = 0, y1 = 0, w, h;
  521. tcd->image = image;
  522. tcd->tcd_image->tw = cp->tw;
  523. tcd->tcd_image->th = cp->th;
  524. tcd->tcd_image->tiles = (opj_tcd_tile_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tcd_tile_t));
  525. /*
  526. Allocate place to store the decoded data = final image
  527. Place limited by the tile really present in the codestream
  528. */
  529. for (j = 0; j < cp->tileno_size; j++) {
  530. opj_tcd_tile_t *tile;
  531. tileno = cp->tileno[j];
  532. tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
  533. tile->numcomps = image->numcomps;
  534. tile->comps = (opj_tcd_tilecomp_t*) opj_calloc(image->numcomps, sizeof(opj_tcd_tilecomp_t));
  535. }
  536. for (i = 0; i < image->numcomps; i++) {
  537. for (j = 0; j < cp->tileno_size; j++) {
  538. opj_tcd_tile_t *tile;
  539. opj_tcd_tilecomp_t *tilec;
  540. /* cfr p59 ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  541. tileno = cp->tileno[j];
  542. tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
  543. tilec = &tile->comps[i];
  544. p = tileno % cp->tw; /* si numerotation matricielle .. */
  545. q = tileno / cp->tw; /* .. coordonnees de la tile (q,p) q pour ligne et p pour colonne */
  546. /* 4 borders of the tile rescale on the image if necessary */
  547. tile->x0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
  548. tile->y0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
  549. tile->x1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
  550. tile->y1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
  551. tilec->x0 = int_ceildiv(tile->x0, image->comps[i].dx);
  552. tilec->y0 = int_ceildiv(tile->y0, image->comps[i].dy);
  553. tilec->x1 = int_ceildiv(tile->x1, image->comps[i].dx);
  554. tilec->y1 = int_ceildiv(tile->y1, image->comps[i].dy);
  555. x0 = j == 0 ? tilec->x0 : int_min(x0, (unsigned int) tilec->x0);
  556. y0 = j == 0 ? tilec->y0 : int_min(y0, (unsigned int) tilec->y0);
  557. x1 = j == 0 ? tilec->x1 : int_max(x1, (unsigned int) tilec->x1);
  558. y1 = j == 0 ? tilec->y1 : int_max(y1, (unsigned int) tilec->y1);
  559. }
  560. w = int_ceildivpow2(x1 - x0, image->comps[i].factor);
  561. h = int_ceildivpow2(y1 - y0, image->comps[i].factor);
  562. image->comps[i].w = w;
  563. image->comps[i].h = h;
  564. image->comps[i].x0 = x0;
  565. image->comps[i].y0 = y0;
  566. }
  567. }
  568. void tcd_malloc_decode_tile(opj_tcd_t *tcd, opj_image_t * image, opj_cp_t * cp, int tileno, opj_codestream_info_t *cstr_info) {
  569. int compno, resno, bandno, precno, cblkno;
  570. opj_tcp_t *tcp;
  571. opj_tcd_tile_t *tile;
  572. OPJ_ARG_NOT_USED(cstr_info);
  573. tcd->cp = cp;
  574. tcp = &(cp->tcps[cp->tileno[tileno]]);
  575. tile = &(tcd->tcd_image->tiles[cp->tileno[tileno]]);
  576. tileno = cp->tileno[tileno];
  577. for (compno = 0; compno < tile->numcomps; compno++) {
  578. opj_tccp_t *tccp = &tcp->tccps[compno];
  579. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  580. /* border of each tile component (global) */
  581. tilec->x0 = int_ceildiv(tile->x0, image->comps[compno].dx);
  582. tilec->y0 = int_ceildiv(tile->y0, image->comps[compno].dy);
  583. tilec->x1 = int_ceildiv(tile->x1, image->comps[compno].dx);
  584. tilec->y1 = int_ceildiv(tile->y1, image->comps[compno].dy);
  585. tilec->numresolutions = tccp->numresolutions;
  586. tilec->resolutions = (opj_tcd_resolution_t *) opj_malloc(tilec->numresolutions * sizeof(opj_tcd_resolution_t));
  587. for (resno = 0; resno < tilec->numresolutions; resno++) {
  588. int pdx, pdy;
  589. int levelno = tilec->numresolutions - 1 - resno;
  590. int tlprcxstart, tlprcystart, brprcxend, brprcyend;
  591. int tlcbgxstart, tlcbgystart, brcbgxend, brcbgyend;
  592. int cbgwidthexpn, cbgheightexpn;
  593. int cblkwidthexpn, cblkheightexpn;
  594. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  595. /* border for each resolution level (global) */
  596. res->x0 = int_ceildivpow2(tilec->x0, levelno);
  597. res->y0 = int_ceildivpow2(tilec->y0, levelno);
  598. res->x1 = int_ceildivpow2(tilec->x1, levelno);
  599. res->y1 = int_ceildivpow2(tilec->y1, levelno);
  600. res->numbands = resno == 0 ? 1 : 3;
  601. /* p. 35, table A-23, ISO/IEC FDIS154444-1 : 2000 (18 august 2000) */
  602. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  603. pdx = tccp->prcw[resno];
  604. pdy = tccp->prch[resno];
  605. } else {
  606. pdx = 15;
  607. pdy = 15;
  608. }
  609. /* p. 64, B.6, ISO/IEC FDIS15444-1 : 2000 (18 august 2000) */
  610. tlprcxstart = int_floordivpow2(res->x0, pdx) << pdx;
  611. tlprcystart = int_floordivpow2(res->y0, pdy) << pdy;
  612. brprcxend = int_ceildivpow2(res->x1, pdx) << pdx;
  613. brprcyend = int_ceildivpow2(res->y1, pdy) << pdy;
  614. res->pw = (res->x0 == res->x1) ? 0 : ((brprcxend - tlprcxstart) >> pdx);
  615. res->ph = (res->y0 == res->y1) ? 0 : ((brprcyend - tlprcystart) >> pdy);
  616. if (resno == 0) {
  617. tlcbgxstart = tlprcxstart;
  618. tlcbgystart = tlprcystart;
  619. brcbgxend = brprcxend;
  620. brcbgyend = brprcyend;
  621. cbgwidthexpn = pdx;
  622. cbgheightexpn = pdy;
  623. } else {
  624. tlcbgxstart = int_ceildivpow2(tlprcxstart, 1);
  625. tlcbgystart = int_ceildivpow2(tlprcystart, 1);
  626. brcbgxend = int_ceildivpow2(brprcxend, 1);
  627. brcbgyend = int_ceildivpow2(brprcyend, 1);
  628. cbgwidthexpn = pdx - 1;
  629. cbgheightexpn = pdy - 1;
  630. }
  631. cblkwidthexpn = int_min(tccp->cblkw, cbgwidthexpn);
  632. cblkheightexpn = int_min(tccp->cblkh, cbgheightexpn);
  633. for (bandno = 0; bandno < res->numbands; bandno++) {
  634. int x0b, y0b;
  635. int gain, numbps;
  636. opj_stepsize_t *ss = NULL;
  637. opj_tcd_band_t *band = &res->bands[bandno];
  638. band->bandno = resno == 0 ? 0 : bandno + 1;
  639. x0b = (band->bandno == 1) || (band->bandno == 3) ? 1 : 0;
  640. y0b = (band->bandno == 2) || (band->bandno == 3) ? 1 : 0;
  641. if (band->bandno == 0) {
  642. /* band border (global) */
  643. band->x0 = int_ceildivpow2(tilec->x0, levelno);
  644. band->y0 = int_ceildivpow2(tilec->y0, levelno);
  645. band->x1 = int_ceildivpow2(tilec->x1, levelno);
  646. band->y1 = int_ceildivpow2(tilec->y1, levelno);
  647. } else {
  648. /* band border (global) */
  649. band->x0 = int_ceildivpow2(tilec->x0 - (1 << levelno) * x0b, levelno + 1);
  650. band->y0 = int_ceildivpow2(tilec->y0 - (1 << levelno) * y0b, levelno + 1);
  651. band->x1 = int_ceildivpow2(tilec->x1 - (1 << levelno) * x0b, levelno + 1);
  652. band->y1 = int_ceildivpow2(tilec->y1 - (1 << levelno) * y0b, levelno + 1);
  653. }
  654. ss = &tccp->stepsizes[resno == 0 ? 0 : 3 * (resno - 1) + bandno + 1];
  655. gain = tccp->qmfbid == 0 ? dwt_getgain_real(band->bandno) : dwt_getgain(band->bandno);
  656. numbps = image->comps[compno].prec + gain;
  657. band->stepsize = (float)(((1.0 + ss->mant / 2048.0) * pow(2.0, numbps - ss->expn)) * 0.5);
  658. band->numbps = ss->expn + tccp->numgbits - 1; /* WHY -1 ? */
  659. band->precincts = (opj_tcd_precinct_t *) opj_malloc(res->pw * res->ph * sizeof(opj_tcd_precinct_t));
  660. for (precno = 0; precno < res->pw * res->ph; precno++) {
  661. int tlcblkxstart, tlcblkystart, brcblkxend, brcblkyend;
  662. int cbgxstart = tlcbgxstart + (precno % res->pw) * (1 << cbgwidthexpn);
  663. int cbgystart = tlcbgystart + (precno / res->pw) * (1 << cbgheightexpn);
  664. int cbgxend = cbgxstart + (1 << cbgwidthexpn);
  665. int cbgyend = cbgystart + (1 << cbgheightexpn);
  666. opj_tcd_precinct_t *prc = &band->precincts[precno];
  667. /* precinct size (global) */
  668. prc->x0 = int_max(cbgxstart, band->x0);
  669. prc->y0 = int_max(cbgystart, band->y0);
  670. prc->x1 = int_min(cbgxend, band->x1);
  671. prc->y1 = int_min(cbgyend, band->y1);
  672. tlcblkxstart = int_floordivpow2(prc->x0, cblkwidthexpn) << cblkwidthexpn;
  673. tlcblkystart = int_floordivpow2(prc->y0, cblkheightexpn) << cblkheightexpn;
  674. brcblkxend = int_ceildivpow2(prc->x1, cblkwidthexpn) << cblkwidthexpn;
  675. brcblkyend = int_ceildivpow2(prc->y1, cblkheightexpn) << cblkheightexpn;
  676. prc->cw = (brcblkxend - tlcblkxstart) >> cblkwidthexpn;
  677. prc->ch = (brcblkyend - tlcblkystart) >> cblkheightexpn;
  678. prc->cblks.dec = (opj_tcd_cblk_dec_t*) opj_malloc(prc->cw * prc->ch * sizeof(opj_tcd_cblk_dec_t));
  679. prc->incltree = tgt_create(prc->cw, prc->ch);
  680. prc->imsbtree = tgt_create(prc->cw, prc->ch);
  681. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  682. int cblkxstart = tlcblkxstart + (cblkno % prc->cw) * (1 << cblkwidthexpn);
  683. int cblkystart = tlcblkystart + (cblkno / prc->cw) * (1 << cblkheightexpn);
  684. int cblkxend = cblkxstart + (1 << cblkwidthexpn);
  685. int cblkyend = cblkystart + (1 << cblkheightexpn);
  686. opj_tcd_cblk_dec_t* cblk = &prc->cblks.dec[cblkno];
  687. cblk->data = NULL;
  688. cblk->segs = NULL;
  689. /* code-block size (global) */
  690. cblk->x0 = int_max(cblkxstart, prc->x0);
  691. cblk->y0 = int_max(cblkystart, prc->y0);
  692. cblk->x1 = int_min(cblkxend, prc->x1);
  693. cblk->y1 = int_min(cblkyend, prc->y1);
  694. cblk->numsegs = 0;
  695. }
  696. } /* precno */
  697. } /* bandno */
  698. } /* resno */
  699. } /* compno */
  700. /* tcd_dump(stdout, tcd, &tcd->tcd_image); */
  701. }
  702. void tcd_makelayer_fixed(opj_tcd_t *tcd, int layno, int final) {
  703. int compno, resno, bandno, precno, cblkno;
  704. int value; /*, matrice[tcd_tcp->numlayers][tcd_tile->comps[0].numresolutions][3]; */
  705. int matrice[10][10][3];
  706. int i, j, k;
  707. opj_cp_t *cp = tcd->cp;
  708. opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
  709. opj_tcp_t *tcd_tcp = tcd->tcp;
  710. /*matrice=(int*)opj_malloc(tcd_tcp->numlayers*tcd_tile->comps[0].numresolutions*3*sizeof(int)); */
  711. for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  712. opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  713. for (i = 0; i < tcd_tcp->numlayers; i++) {
  714. for (j = 0; j < tilec->numresolutions; j++) {
  715. for (k = 0; k < 3; k++) {
  716. matrice[i][j][k] =
  717. (int) (cp->matrice[i * tilec->numresolutions * 3 + j * 3 + k]
  718. * (float) (tcd->image->comps[compno].prec / 16.0));
  719. }
  720. }
  721. }
  722. for (resno = 0; resno < tilec->numresolutions; resno++) {
  723. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  724. for (bandno = 0; bandno < res->numbands; bandno++) {
  725. opj_tcd_band_t *band = &res->bands[bandno];
  726. for (precno = 0; precno < res->pw * res->ph; precno++) {
  727. opj_tcd_precinct_t *prc = &band->precincts[precno];
  728. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  729. opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
  730. opj_tcd_layer_t *layer = &cblk->layers[layno];
  731. int n;
  732. int imsb = tcd->image->comps[compno].prec - cblk->numbps; /* number of bit-plan equal to zero */
  733. /* Correction of the matrix of coefficient to include the IMSB information */
  734. if (layno == 0) {
  735. value = matrice[layno][resno][bandno];
  736. if (imsb >= value) {
  737. value = 0;
  738. } else {
  739. value -= imsb;
  740. }
  741. } else {
  742. value = matrice[layno][resno][bandno] - matrice[layno - 1][resno][bandno];
  743. if (imsb >= matrice[layno - 1][resno][bandno]) {
  744. value -= (imsb - matrice[layno - 1][resno][bandno]);
  745. if (value < 0) {
  746. value = 0;
  747. }
  748. }
  749. }
  750. if (layno == 0) {
  751. cblk->numpassesinlayers = 0;
  752. }
  753. n = cblk->numpassesinlayers;
  754. if (cblk->numpassesinlayers == 0) {
  755. if (value != 0) {
  756. n = 3 * value - 2 + cblk->numpassesinlayers;
  757. } else {
  758. n = cblk->numpassesinlayers;
  759. }
  760. } else {
  761. n = 3 * value + cblk->numpassesinlayers;
  762. }
  763. layer->numpasses = n - cblk->numpassesinlayers;
  764. if (!layer->numpasses)
  765. continue;
  766. if (cblk->numpassesinlayers == 0) {
  767. layer->len = cblk->passes[n - 1].rate;
  768. layer->data = cblk->data;
  769. } else {
  770. layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
  771. layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
  772. }
  773. if (final)
  774. cblk->numpassesinlayers = n;
  775. }
  776. }
  777. }
  778. }
  779. }
  780. }
  781. void tcd_rateallocate_fixed(opj_tcd_t *tcd) {
  782. int layno;
  783. for (layno = 0; layno < tcd->tcp->numlayers; layno++) {
  784. tcd_makelayer_fixed(tcd, layno, 1);
  785. }
  786. }
  787. void tcd_makelayer(opj_tcd_t *tcd, int layno, double thresh, int final) {
  788. int compno, resno, bandno, precno, cblkno, passno;
  789. opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
  790. tcd_tile->distolayer[layno] = 0; /* fixed_quality */
  791. for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  792. opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  793. for (resno = 0; resno < tilec->numresolutions; resno++) {
  794. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  795. for (bandno = 0; bandno < res->numbands; bandno++) {
  796. opj_tcd_band_t *band = &res->bands[bandno];
  797. for (precno = 0; precno < res->pw * res->ph; precno++) {
  798. opj_tcd_precinct_t *prc = &band->precincts[precno];
  799. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  800. opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
  801. opj_tcd_layer_t *layer = &cblk->layers[layno];
  802. int n;
  803. if (layno == 0) {
  804. cblk->numpassesinlayers = 0;
  805. }
  806. n = cblk->numpassesinlayers;
  807. for (passno = cblk->numpassesinlayers; passno < cblk->totalpasses; passno++) {
  808. int dr;
  809. double dd;
  810. opj_tcd_pass_t *pass = &cblk->passes[passno];
  811. if (n == 0) {
  812. dr = pass->rate;
  813. dd = pass->distortiondec;
  814. } else {
  815. dr = pass->rate - cblk->passes[n - 1].rate;
  816. dd = pass->distortiondec - cblk->passes[n - 1].distortiondec;
  817. }
  818. if (!dr) {
  819. if (dd != 0)
  820. n = passno + 1;
  821. continue;
  822. }
  823. if (dd / dr >= thresh)
  824. n = passno + 1;
  825. }
  826. layer->numpasses = n - cblk->numpassesinlayers;
  827. if (!layer->numpasses) {
  828. layer->disto = 0;
  829. continue;
  830. }
  831. if (cblk->numpassesinlayers == 0) {
  832. layer->len = cblk->passes[n - 1].rate;
  833. layer->data = cblk->data;
  834. layer->disto = cblk->passes[n - 1].distortiondec;
  835. } else {
  836. layer->len = cblk->passes[n - 1].rate - cblk->passes[cblk->numpassesinlayers - 1].rate;
  837. layer->data = cblk->data + cblk->passes[cblk->numpassesinlayers - 1].rate;
  838. layer->disto = cblk->passes[n - 1].distortiondec - cblk->passes[cblk->numpassesinlayers - 1].distortiondec;
  839. }
  840. tcd_tile->distolayer[layno] += layer->disto; /* fixed_quality */
  841. if (final)
  842. cblk->numpassesinlayers = n;
  843. }
  844. }
  845. }
  846. }
  847. }
  848. }
  849. opj_bool tcd_rateallocate(opj_tcd_t *tcd, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
  850. int compno, resno, bandno, precno, cblkno, passno, layno;
  851. double min, max;
  852. double cumdisto[100]; /* fixed_quality */
  853. const double K = 1; /* 1.1; fixed_quality */
  854. double maxSE = 0;
  855. opj_cp_t *cp = tcd->cp;
  856. opj_tcd_tile_t *tcd_tile = tcd->tcd_tile;
  857. opj_tcp_t *tcd_tcp = tcd->tcp;
  858. min = DBL_MAX;
  859. max = 0;
  860. tcd_tile->numpix = 0; /* fixed_quality */
  861. for (compno = 0; compno < tcd_tile->numcomps; compno++) {
  862. opj_tcd_tilecomp_t *tilec = &tcd_tile->comps[compno];
  863. tilec->numpix = 0;
  864. for (resno = 0; resno < tilec->numresolutions; resno++) {
  865. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  866. for (bandno = 0; bandno < res->numbands; bandno++) {
  867. opj_tcd_band_t *band = &res->bands[bandno];
  868. for (precno = 0; precno < res->pw * res->ph; precno++) {
  869. opj_tcd_precinct_t *prc = &band->precincts[precno];
  870. for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
  871. opj_tcd_cblk_enc_t *cblk = &prc->cblks.enc[cblkno];
  872. for (passno = 0; passno < cblk->totalpasses; passno++) {
  873. opj_tcd_pass_t *pass = &cblk->passes[passno];
  874. int dr;
  875. double dd, rdslope;
  876. if (passno == 0) {
  877. dr = pass->rate;
  878. dd = pass->distortiondec;
  879. } else {
  880. dr = pass->rate - cblk->passes[passno - 1].rate;
  881. dd = pass->distortiondec - cblk->passes[passno - 1].distortiondec;
  882. }
  883. if (dr == 0) {
  884. continue;
  885. }
  886. rdslope = dd / dr;
  887. if (rdslope < min) {
  888. min = rdslope;
  889. }
  890. if (rdslope > max) {
  891. max = rdslope;
  892. }
  893. } /* passno */
  894. /* fixed_quality */
  895. tcd_tile->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
  896. tilec->numpix += ((cblk->x1 - cblk->x0) * (cblk->y1 - cblk->y0));
  897. } /* cbklno */
  898. } /* precno */
  899. } /* bandno */
  900. } /* resno */
  901. maxSE += (((double)(1 << tcd->image->comps[compno].prec) - 1.0)
  902. * ((double)(1 << tcd->image->comps[compno].prec) -1.0))
  903. * ((double)(tilec->numpix));
  904. } /* compno */
  905. /* index file */
  906. if(cstr_info) {
  907. opj_tile_info_t *tile_info = &cstr_info->tile[tcd->tcd_tileno];
  908. tile_info->numpix = tcd_tile->numpix;
  909. tile_info->distotile = tcd_tile->distotile;
  910. tile_info->thresh = (double *) opj_malloc(tcd_tcp->numlayers * sizeof(double));
  911. }
  912. for (layno = 0; layno < tcd_tcp->numlayers; layno++) {
  913. double lo = min;
  914. double hi = max;
  915. int success = 0;
  916. int maxlen = tcd_tcp->rates[layno] ? int_min(((int) ceil(tcd_tcp->rates[layno])), len) : len;
  917. double goodthresh = 0;
  918. double stable_thresh = 0;
  919. int i;
  920. double distotarget; /* fixed_quality */
  921. /* fixed_quality */
  922. distotarget = tcd_tile->distotile - ((K * maxSE) / pow((float)10, tcd_tcp->distoratio[layno] / 10));
  923. /* Don't try to find an optimal threshold but rather take everything not included yet, if
  924. -r xx,yy,zz,0 (disto_alloc == 1 and rates == 0)
  925. -q xx,yy,zz,0 (fixed_quality == 1 and distoratio == 0)
  926. ==> possible to have some lossy layers and the last layer for sure lossless */
  927. if ( ((cp->disto_alloc==1) && (tcd_tcp->rates[layno]>0)) || ((cp->fixed_quality==1) && (tcd_tcp->distoratio[layno]>0))) {
  928. opj_t2_t *t2 = t2_create(tcd->cinfo, tcd->image, cp);
  929. double thresh = 0;
  930. for (i = 0; i < 128; i++) {
  931. int l = 0;
  932. double distoachieved = 0; /* fixed_quality */
  933. thresh = (lo + hi) / 2;
  934. tcd_makelayer(tcd, layno, thresh, 0);
  935. if (cp->fixed_quality) { /* fixed_quality */
  936. if(cp->cinema){
  937. l = t2_encode_packets(t2,tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
  938. if (l == -999) {
  939. lo = thresh;
  940. continue;
  941. }else{
  942. distoachieved = layno == 0 ?
  943. tcd_tile->distolayer[0] : cumdisto[layno - 1] + tcd_tile->distolayer[layno];
  944. if (distoachieved < distotarget) {
  945. hi=thresh;
  946. stable_thresh = thresh;
  947. continue;
  948. }else{
  949. lo=thresh;
  950. }
  951. }
  952. }else{
  953. distoachieved = (layno == 0) ?
  954. tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
  955. if (distoachieved < distotarget) {
  956. hi = thresh;
  957. stable_thresh = thresh;
  958. continue;
  959. }
  960. lo = thresh;
  961. }
  962. } else {
  963. l = t2_encode_packets(t2, tcd->tcd_tileno, tcd_tile, layno + 1, dest, maxlen, cstr_info,tcd->cur_tp_num,tcd->tp_pos,tcd->cur_pino,THRESH_CALC, tcd->cur_totnum_tp);
  964. /* TODO: what to do with l ??? seek / tell ??? */
  965. /* opj_event_msg(tcd->cinfo, EVT_INFO, "rate alloc: len=%d, max=%d\n", l, maxlen); */
  966. if (l == -999) {
  967. lo = thresh;
  968. continue;
  969. }
  970. hi = thresh;
  971. stable_thresh = thresh;
  972. }
  973. }
  974. success = 1;
  975. goodthresh = stable_thresh == 0? thresh : stable_thresh;
  976. t2_destroy(t2);
  977. } else {
  978. success = 1;
  979. goodthresh = min;
  980. }
  981. if (!success) {
  982. return OPJ_FALSE;
  983. }
  984. if(cstr_info) { /* Threshold for Marcela Index */
  985. cstr_info->tile[tcd->tcd_tileno].thresh[layno] = goodthresh;
  986. }
  987. tcd_makelayer(tcd, layno, goodthresh, 1);
  988. /* fixed_quality */
  989. cumdisto[layno] = (layno == 0) ? tcd_tile->distolayer[0] : (cumdisto[layno - 1] + tcd_tile->distolayer[layno]);
  990. }
  991. return OPJ_TRUE;
  992. }
  993. int tcd_encode_tile(opj_tcd_t *tcd, int tileno, unsigned char *dest, int len, opj_codestream_info_t *cstr_info) {
  994. int compno;
  995. int l, i, numpacks = 0;
  996. opj_tcd_tile_t *tile = NULL;
  997. opj_tcp_t *tcd_tcp = NULL;
  998. opj_cp_t *cp = NULL;
  999. opj_tcp_t *tcp = &tcd->cp->tcps[0];
  1000. opj_tccp_t *tccp = &tcp->tccps[0];
  1001. opj_image_t *image = tcd->image;
  1002. opj_t1_t *t1 = NULL; /* T1 component */
  1003. opj_t2_t *t2 = NULL; /* T2 component */
  1004. tcd->tcd_tileno = tileno;
  1005. tcd->tcd_tile = tcd->tcd_image->tiles;
  1006. tcd->tcp = &tcd->cp->tcps[tileno];
  1007. tile = tcd->tcd_tile;
  1008. tcd_tcp = tcd->tcp;
  1009. cp = tcd->cp;
  1010. if(tcd->cur_tp_num == 0){
  1011. tcd->encoding_time = opj_clock(); /* time needed to encode a tile */
  1012. /* INDEX >> "Precinct_nb_X et Precinct_nb_Y" */
  1013. if(cstr_info) {
  1014. opj_tcd_tilecomp_t *tilec_idx = &tile->comps[0]; /* based on component 0 */
  1015. for (i = 0; i < tilec_idx->numresolutions; i++) {
  1016. opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[i];
  1017. cstr_info->tile[tileno].pw[i] = res_idx->pw;
  1018. cstr_info->tile[tileno].ph[i] = res_idx->ph;
  1019. numpacks += res_idx->pw * res_idx->ph;
  1020. cstr_info->tile[tileno].pdx[i] = tccp->prcw[i];
  1021. cstr_info->tile[tileno].pdy[i] = tccp->prch[i];
  1022. }
  1023. cstr_info->tile[tileno].packet = (opj_packet_info_t*) opj_calloc(cstr_info->numcomps * cstr_info->numlayers * numpacks, sizeof(opj_packet_info_t));
  1024. }
  1025. /* << INDEX */
  1026. /*---------------TILE-------------------*/
  1027. for (compno = 0; compno < tile->numcomps; compno++) {
  1028. int x, y;
  1029. int adjust = image->comps[compno].sgnd ? 0 : 1 << (image->comps[compno].prec - 1);
  1030. int offset_x = int_ceildiv(image->x0, image->comps[compno].dx);
  1031. int offset_y = int_ceildiv(image->y0, image->comps[compno].dy);
  1032. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1033. int tw = tilec->x1 - tilec->x0;
  1034. int w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
  1035. /* extract tile data */
  1036. if (tcd_tcp->tccps[compno].qmfbid == 1) {
  1037. for (y = tilec->y0; y < tilec->y1; y++) {
  1038. /* start of the src tile scanline */
  1039. int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
  1040. /* start of the dst tile scanline */
  1041. int *tile_data = &tilec->data[(y - tilec->y0) * tw];
  1042. for (x = tilec->x0; x < tilec->x1; x++) {
  1043. *tile_data++ = *data++ - adjust;
  1044. }
  1045. }
  1046. } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
  1047. for (y = tilec->y0; y < tilec->y1; y++) {
  1048. /* start of the src tile scanline */
  1049. int *data = &image->comps[compno].data[(tilec->x0 - offset_x) + (y - offset_y) * w];
  1050. /* start of the dst tile scanline */
  1051. int *tile_data = &tilec->data[(y - tilec->y0) * tw];
  1052. for (x = tilec->x0; x < tilec->x1; x++) {
  1053. *tile_data++ = (*data++ - adjust) << 11;
  1054. }
  1055. }
  1056. }
  1057. }
  1058. /*----------------MCT-------------------*/
  1059. if (tcd_tcp->mct) {
  1060. int samples = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
  1061. if (tcd_tcp->tccps[0].qmfbid == 0) {
  1062. mct_encode_real(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
  1063. } else {
  1064. mct_encode(tile->comps[0].data, tile->comps[1].data, tile->comps[2].data, samples);
  1065. }
  1066. }
  1067. /*----------------DWT---------------------*/
  1068. for (compno = 0; compno < tile->numcomps; compno++) {
  1069. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1070. if (tcd_tcp->tccps[compno].qmfbid == 1) {
  1071. dwt_encode(tilec);
  1072. } else if (tcd_tcp->tccps[compno].qmfbid == 0) {
  1073. dwt_encode_real(tilec);
  1074. }
  1075. }
  1076. /*------------------TIER1-----------------*/
  1077. t1 = t1_create(tcd->cinfo);
  1078. t1_encode_cblks(t1, tile, tcd_tcp);
  1079. t1_destroy(t1);
  1080. /*-----------RATE-ALLOCATE------------------*/
  1081. /* INDEX */
  1082. if(cstr_info) {
  1083. cstr_info->index_write = 0;
  1084. }
  1085. if (cp->disto_alloc || cp->fixed_quality) { /* fixed_quality */
  1086. /* Normal Rate/distortion allocation */
  1087. tcd_rateallocate(tcd, dest, len, cstr_info);
  1088. } else {
  1089. /* Fixed layer allocation */
  1090. tcd_rateallocate_fixed(tcd);
  1091. }
  1092. }
  1093. /*--------------TIER2------------------*/
  1094. /* INDEX */
  1095. if(cstr_info) {
  1096. cstr_info->index_write = 1;
  1097. }
  1098. t2 = t2_create(tcd->cinfo, image, cp);
  1099. l = t2_encode_packets(t2,tileno, tile, tcd_tcp->numlayers, dest, len, cstr_info,tcd->tp_num,tcd->tp_pos,tcd->cur_pino,FINAL_PASS,tcd->cur_totnum_tp);
  1100. t2_destroy(t2);
  1101. /*---------------CLEAN-------------------*/
  1102. if(tcd->cur_tp_num == tcd->cur_totnum_tp - 1){
  1103. tcd->encoding_time = opj_clock() - tcd->encoding_time;
  1104. opj_event_msg(tcd->cinfo, EVT_INFO, "- tile encoded in %f s\n", tcd->encoding_time);
  1105. /* cleaning memory */
  1106. for (compno = 0; compno < tile->numcomps; compno++) {
  1107. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1108. opj_aligned_free(tilec->data);
  1109. }
  1110. }
  1111. return l;
  1112. }
  1113. opj_bool tcd_decode_tile(opj_tcd_t *tcd, unsigned char *src, int len, int tileno, opj_codestream_info_t *cstr_info) {
  1114. int l;
  1115. int compno;
  1116. int eof = 0;
  1117. double tile_time, t1_time, dwt_time;
  1118. opj_tcd_tile_t *tile = NULL;
  1119. opj_t1_t *t1 = NULL; /* T1 component */
  1120. opj_t2_t *t2 = NULL; /* T2 component */
  1121. tcd->tcd_tileno = tileno;
  1122. tcd->tcd_tile = &(tcd->tcd_image->tiles[tileno]);
  1123. tcd->tcp = &(tcd->cp->tcps[tileno]);
  1124. tile = tcd->tcd_tile;
  1125. tile_time = opj_clock(); /* time needed to decode a tile */
  1126. opj_event_msg(tcd->cinfo, EVT_INFO, "tile %d of %d\n", tileno + 1, tcd->cp->tw * tcd->cp->th);
  1127. /* INDEX >> */
  1128. if(cstr_info) {
  1129. int resno, compno, numprec = 0;
  1130. for (compno = 0; compno < cstr_info->numcomps; compno++) {
  1131. opj_tcp_t *tcp = &tcd->cp->tcps[0];
  1132. opj_tccp_t *tccp = &tcp->tccps[compno];
  1133. opj_tcd_tilecomp_t *tilec_idx = &tile->comps[compno];
  1134. for (resno = 0; resno < tilec_idx->numresolutions; resno++) {
  1135. opj_tcd_resolution_t *res_idx = &tilec_idx->resolutions[resno];
  1136. cstr_info->tile[tileno].pw[resno] = res_idx->pw;
  1137. cstr_info->tile[tileno].ph[resno] = res_idx->ph;
  1138. numprec += res_idx->pw * res_idx->ph;
  1139. if (tccp->csty & J2K_CP_CSTY_PRT) {
  1140. cstr_info->tile[tileno].pdx[resno] = tccp->prcw[resno];
  1141. cstr_info->tile[tileno].pdy[resno] = tccp->prch[resno];
  1142. }
  1143. else {
  1144. cstr_info->tile[tileno].pdx[resno] = 15;
  1145. cstr_info->tile[tileno].pdy[resno] = 15;
  1146. }
  1147. }
  1148. }
  1149. cstr_info->tile[tileno].packet = (opj_packet_info_t *) opj_malloc(cstr_info->numlayers * numprec * sizeof(opj_packet_info_t));
  1150. cstr_info->packno = 0;
  1151. }
  1152. /* << INDEX */
  1153. /*--------------TIER2------------------*/
  1154. t2 = t2_create(tcd->cinfo, tcd->image, tcd->cp);
  1155. l = t2_decode_packets(t2, src, len, tileno, tile, cstr_info);
  1156. t2_destroy(t2);
  1157. if (l == -999) {
  1158. eof = 1;
  1159. opj_event_msg(tcd->cinfo, EVT_ERROR, "tcd_decode: incomplete bistream\n");
  1160. }
  1161. /*------------------TIER1-----------------*/
  1162. t1_time = opj_clock(); /* time needed to decode a tile */
  1163. t1 = t1_create(tcd->cinfo);
  1164. for (compno = 0; compno < tile->numcomps; ++compno) {
  1165. opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
  1166. /* The +3 is headroom required by the vectorized DWT */
  1167. tilec->data = (int*) opj_aligned_malloc((((tilec->x1 - tilec->x0) * (tilec->y1 - tilec->y0))+3) * sizeof(int));
  1168. t1_decode_cblks(t1, tilec, &tcd->tcp->tccps[compno]);
  1169. }
  1170. t1_destroy(t1);
  1171. t1_time = opj_clock() - t1_time;
  1172. opj_event_msg(tcd->cinfo, EVT_INFO, "- tiers-1 took %f s\n", t1_time);
  1173. /*----------------DWT---------------------*/
  1174. dwt_time = opj_clock(); /* time needed to decode a tile */
  1175. for (compno = 0; compno < tile->numcomps; compno++) {
  1176. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1177. int numres2decode;
  1178. if (tcd->cp->reduce != 0) {
  1179. tcd->image->comps[compno].resno_decoded =
  1180. tile->comps[compno].numresolutions - tcd->cp->reduce - 1;
  1181. if (tcd->image->comps[compno].resno_decoded < 0) {
  1182. opj_event_msg(tcd->cinfo, EVT_ERROR, "Error decoding tile. The number of resolutions to remove [%d+1] is higher than the number "
  1183. " of resolutions in the original codestream [%d]\nModify the cp_reduce parameter.\n", tcd->cp->reduce, tile->comps[compno].numresolutions);
  1184. return OPJ_FALSE;
  1185. }
  1186. }
  1187. numres2decode = tcd->image->comps[compno].resno_decoded + 1;
  1188. if(numres2decode > 0){
  1189. if (tcd->tcp->tccps[compno].qmfbid == 1) {
  1190. dwt_decode(tilec, numres2decode);
  1191. } else {
  1192. dwt_decode_real(tilec, numres2decode);
  1193. }
  1194. }
  1195. }
  1196. dwt_time = opj_clock() - dwt_time;
  1197. opj_event_msg(tcd->cinfo, EVT_INFO, "- dwt took %f s\n", dwt_time);
  1198. /*----------------MCT-------------------*/
  1199. if (tcd->tcp->mct) {
  1200. int n = (tile->comps[0].x1 - tile->comps[0].x0) * (tile->comps[0].y1 - tile->comps[0].y0);
  1201. if (tile->numcomps >= 3 ){
  1202. if (tcd->tcp->tccps[0].qmfbid == 1) {
  1203. mct_decode(
  1204. tile->comps[0].data,
  1205. tile->comps[1].data,
  1206. tile->comps[2].data,
  1207. n);
  1208. } else {
  1209. mct_decode_real(
  1210. (float*)tile->comps[0].data,
  1211. (float*)tile->comps[1].data,
  1212. (float*)tile->comps[2].data,
  1213. n);
  1214. }
  1215. } else{
  1216. opj_event_msg(tcd->cinfo, EVT_WARNING,"Number of components (%d) is inconsistent with a MCT. Skip the MCT step.\n",tile->numcomps);
  1217. }
  1218. }
  1219. /*---------------TILE-------------------*/
  1220. for (compno = 0; compno < tile->numcomps; ++compno) {
  1221. opj_tcd_tilecomp_t* tilec = &tile->comps[compno];
  1222. opj_image_comp_t* imagec = &tcd->image->comps[compno];
  1223. opj_tcd_resolution_t* res = &tilec->resolutions[imagec->resno_decoded];
  1224. int adjust = imagec->sgnd ? 0 : 1 << (imagec->prec - 1);
  1225. int min = imagec->sgnd ? -(1 << (imagec->prec - 1)) : 0;
  1226. int max = imagec->sgnd ? (1 << (imagec->prec - 1)) - 1 : (1 << imagec->prec) - 1;
  1227. int tw = tilec->x1 - tilec->x0;
  1228. int w = imagec->w;
  1229. int offset_x = int_ceildivpow2(imagec->x0, imagec->factor);
  1230. int offset_y = int_ceildivpow2(imagec->y0, imagec->factor);
  1231. int i, j;
  1232. if(!imagec->data){
  1233. imagec->data = (int*) opj_malloc(imagec->w * imagec->h * sizeof(int));
  1234. }
  1235. if(tcd->tcp->tccps[compno].qmfbid == 1) {
  1236. for(j = res->y0; j < res->y1; ++j) {
  1237. for(i = res->x0; i < res->x1; ++i) {
  1238. int v = tilec->data[i - res->x0 + (j - res->y0) * tw];
  1239. v += adjust;
  1240. imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
  1241. }
  1242. }
  1243. }else{
  1244. for(j = res->y0; j < res->y1; ++j) {
  1245. for(i = res->x0; i < res->x1; ++i) {
  1246. float tmp = ((float*)tilec->data)[i - res->x0 + (j - res->y0) * tw];
  1247. int v = lrintf(tmp);
  1248. v += adjust;
  1249. imagec->data[(i - offset_x) + (j - offset_y) * w] = int_clamp(v, min, max);
  1250. }
  1251. }
  1252. }
  1253. opj_aligned_free(tilec->data);
  1254. }
  1255. tile_time = opj_clock() - tile_time; /* time needed to decode a tile */
  1256. opj_event_msg(tcd->cinfo, EVT_INFO, "- tile decoded in %f s\n", tile_time);
  1257. if (eof) {
  1258. return OPJ_FALSE;
  1259. }
  1260. return OPJ_TRUE;
  1261. }
  1262. void tcd_free_decode(opj_tcd_t *tcd) {
  1263. opj_tcd_image_t *tcd_image = tcd->tcd_image;
  1264. opj_free(tcd_image->tiles);
  1265. }
  1266. void tcd_free_decode_tile(opj_tcd_t *tcd, int tileno) {
  1267. int compno,resno,bandno,precno;
  1268. opj_tcd_image_t *tcd_image = tcd->tcd_image;
  1269. opj_tcd_tile_t *tile = &tcd_image->tiles[tileno];
  1270. for (compno = 0; compno < tile->numcomps; compno++) {
  1271. opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  1272. for (resno = 0; resno < tilec->numresolutions; resno++) {
  1273. opj_tcd_resolution_t *res = &tilec->resolutions[resno];
  1274. for (bandno = 0; bandno < res->numbands; bandno++) {
  1275. opj_tcd_band_t *band = &res->bands[bandno];
  1276. for (precno = 0; precno < res->ph * res->pw; precno++) {
  1277. opj_tcd_precinct_t *prec = &band->precincts[precno];
  1278. if (prec->imsbtree != NULL) tgt_destroy(prec->imsbtree);
  1279. if (prec->incltree != NULL) tgt_destroy(prec->incltree);
  1280. }
  1281. opj_free(band->precincts);
  1282. }
  1283. }
  1284. opj_free(tilec->resolutions);
  1285. }
  1286. opj_free(tile->comps);
  1287. }