pi.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. /** @defgroup PI PI - Implementation of a packet iterator */
  34. /*@{*/
  35. /** @name Local static functions */
  36. /*@{*/
  37. /**
  38. Get next packet in layer-resolution-component-precinct order.
  39. @param pi packet iterator to modify
  40. @return returns false if pi pointed to the last packet or else returns true
  41. */
  42. static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi);
  43. /**
  44. Get next packet in resolution-layer-component-precinct order.
  45. @param pi packet iterator to modify
  46. @return returns false if pi pointed to the last packet or else returns true
  47. */
  48. static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi);
  49. /**
  50. Get next packet in resolution-precinct-component-layer order.
  51. @param pi packet iterator to modify
  52. @return returns false if pi pointed to the last packet or else returns true
  53. */
  54. static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi);
  55. /**
  56. Get next packet in precinct-component-resolution-layer order.
  57. @param pi packet iterator to modify
  58. @return returns false if pi pointed to the last packet or else returns true
  59. */
  60. static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi);
  61. /**
  62. Get next packet in component-precinct-resolution-layer order.
  63. @param pi packet iterator to modify
  64. @return returns false if pi pointed to the last packet or else returns true
  65. */
  66. static opj_bool pi_next_cprl(opj_pi_iterator_t * pi);
  67. /*@}*/
  68. /*@}*/
  69. /*
  70. ==========================================================
  71. local functions
  72. ==========================================================
  73. */
  74. static opj_bool pi_next_lrcp(opj_pi_iterator_t * pi) {
  75. opj_pi_comp_t *comp = NULL;
  76. opj_pi_resolution_t *res = NULL;
  77. long index = 0;
  78. if (!pi->first) {
  79. comp = &pi->comps[pi->compno];
  80. res = &comp->resolutions[pi->resno];
  81. goto LABEL_SKIP;
  82. } else {
  83. pi->first = 0;
  84. }
  85. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  86. for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
  87. pi->resno++) {
  88. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  89. comp = &pi->comps[pi->compno];
  90. if (pi->resno >= comp->numresolutions) {
  91. continue;
  92. }
  93. res = &comp->resolutions[pi->resno];
  94. if (!pi->tp_on){
  95. pi->poc.precno1 = res->pw * res->ph;
  96. }
  97. for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
  98. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
  99. if (!pi->include[index]) {
  100. pi->include[index] = 1;
  101. return OPJ_TRUE;
  102. }
  103. LABEL_SKIP:;
  104. }
  105. }
  106. }
  107. }
  108. return OPJ_FALSE;
  109. }
  110. static opj_bool pi_next_rlcp(opj_pi_iterator_t * pi) {
  111. opj_pi_comp_t *comp = NULL;
  112. opj_pi_resolution_t *res = NULL;
  113. long index = 0;
  114. if (!pi->first) {
  115. comp = &pi->comps[pi->compno];
  116. res = &comp->resolutions[pi->resno];
  117. goto LABEL_SKIP;
  118. } else {
  119. pi->first = 0;
  120. }
  121. for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
  122. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  123. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  124. comp = &pi->comps[pi->compno];
  125. if (pi->resno >= comp->numresolutions) {
  126. continue;
  127. }
  128. res = &comp->resolutions[pi->resno];
  129. if(!pi->tp_on){
  130. pi->poc.precno1 = res->pw * res->ph;
  131. }
  132. for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
  133. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
  134. if (!pi->include[index]) {
  135. pi->include[index] = 1;
  136. return OPJ_TRUE;
  137. }
  138. LABEL_SKIP:;
  139. }
  140. }
  141. }
  142. }
  143. return OPJ_FALSE;
  144. }
  145. static opj_bool pi_next_rpcl(opj_pi_iterator_t * pi) {
  146. opj_pi_comp_t *comp = NULL;
  147. opj_pi_resolution_t *res = NULL;
  148. long index = 0;
  149. if (!pi->first) {
  150. goto LABEL_SKIP;
  151. } else {
  152. int compno, resno;
  153. pi->first = 0;
  154. pi->dx = 0;
  155. pi->dy = 0;
  156. for (compno = 0; compno < pi->numcomps; compno++) {
  157. comp = &pi->comps[compno];
  158. for (resno = 0; resno < comp->numresolutions; resno++) {
  159. int dx, dy;
  160. res = &comp->resolutions[resno];
  161. dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
  162. dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
  163. pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
  164. pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
  165. }
  166. }
  167. }
  168. if (!pi->tp_on){
  169. pi->poc.ty0 = pi->ty0;
  170. pi->poc.tx0 = pi->tx0;
  171. pi->poc.ty1 = pi->ty1;
  172. pi->poc.tx1 = pi->tx1;
  173. }
  174. for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
  175. for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
  176. for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
  177. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  178. int levelno;
  179. int trx0, try0;
  180. int trx1, try1;
  181. int rpx, rpy;
  182. int prci, prcj;
  183. comp = &pi->comps[pi->compno];
  184. if (pi->resno >= comp->numresolutions) {
  185. continue;
  186. }
  187. res = &comp->resolutions[pi->resno];
  188. levelno = comp->numresolutions - 1 - pi->resno;
  189. trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
  190. try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
  191. trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
  192. try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
  193. rpx = res->pdx + levelno;
  194. rpy = res->pdy + levelno;
  195. if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
  196. continue;
  197. }
  198. if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
  199. continue;
  200. }
  201. if ((res->pw==0)||(res->ph==0)) continue;
  202. if ((trx0==trx1)||(try0==try1)) continue;
  203. prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
  204. - int_floordivpow2(trx0, res->pdx);
  205. prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
  206. - int_floordivpow2(try0, res->pdy);
  207. pi->precno = prci + prcj * res->pw;
  208. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  209. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
  210. if (!pi->include[index]) {
  211. pi->include[index] = 1;
  212. return OPJ_TRUE;
  213. }
  214. LABEL_SKIP:;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. return OPJ_FALSE;
  221. }
  222. static opj_bool pi_next_pcrl(opj_pi_iterator_t * pi) {
  223. opj_pi_comp_t *comp = NULL;
  224. opj_pi_resolution_t *res = NULL;
  225. long index = 0;
  226. if (!pi->first) {
  227. comp = &pi->comps[pi->compno];
  228. goto LABEL_SKIP;
  229. } else {
  230. int compno, resno;
  231. pi->first = 0;
  232. pi->dx = 0;
  233. pi->dy = 0;
  234. for (compno = 0; compno < pi->numcomps; compno++) {
  235. comp = &pi->comps[compno];
  236. for (resno = 0; resno < comp->numresolutions; resno++) {
  237. int dx, dy;
  238. res = &comp->resolutions[resno];
  239. dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
  240. dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
  241. pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
  242. pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
  243. }
  244. }
  245. }
  246. if (!pi->tp_on){
  247. pi->poc.ty0 = pi->ty0;
  248. pi->poc.tx0 = pi->tx0;
  249. pi->poc.ty1 = pi->ty1;
  250. pi->poc.tx1 = pi->tx1;
  251. }
  252. for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
  253. for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
  254. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  255. comp = &pi->comps[pi->compno];
  256. for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
  257. int levelno;
  258. int trx0, try0;
  259. int trx1, try1;
  260. int rpx, rpy;
  261. int prci, prcj;
  262. res = &comp->resolutions[pi->resno];
  263. levelno = comp->numresolutions - 1 - pi->resno;
  264. trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
  265. try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
  266. trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
  267. try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
  268. rpx = res->pdx + levelno;
  269. rpy = res->pdy + levelno;
  270. if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
  271. continue;
  272. }
  273. if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
  274. continue;
  275. }
  276. if ((res->pw==0)||(res->ph==0)) continue;
  277. if ((trx0==trx1)||(try0==try1)) continue;
  278. prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
  279. - int_floordivpow2(trx0, res->pdx);
  280. prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
  281. - int_floordivpow2(try0, res->pdy);
  282. pi->precno = prci + prcj * res->pw;
  283. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  284. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
  285. if (!pi->include[index]) {
  286. pi->include[index] = 1;
  287. return OPJ_TRUE;
  288. }
  289. LABEL_SKIP:;
  290. }
  291. }
  292. }
  293. }
  294. }
  295. return OPJ_FALSE;
  296. }
  297. static opj_bool pi_next_cprl(opj_pi_iterator_t * pi) {
  298. opj_pi_comp_t *comp = NULL;
  299. opj_pi_resolution_t *res = NULL;
  300. long index = 0;
  301. if (!pi->first) {
  302. comp = &pi->comps[pi->compno];
  303. goto LABEL_SKIP;
  304. } else {
  305. pi->first = 0;
  306. }
  307. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  308. int resno;
  309. comp = &pi->comps[pi->compno];
  310. pi->dx = 0;
  311. pi->dy = 0;
  312. for (resno = 0; resno < comp->numresolutions; resno++) {
  313. int dx, dy;
  314. res = &comp->resolutions[resno];
  315. dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
  316. dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
  317. pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
  318. pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
  319. }
  320. if (!pi->tp_on){
  321. pi->poc.ty0 = pi->ty0;
  322. pi->poc.tx0 = pi->tx0;
  323. pi->poc.ty1 = pi->ty1;
  324. pi->poc.tx1 = pi->tx1;
  325. }
  326. for (pi->y = pi->poc.ty0; pi->y < pi->poc.ty1; pi->y += pi->dy - (pi->y % pi->dy)) {
  327. for (pi->x = pi->poc.tx0; pi->x < pi->poc.tx1; pi->x += pi->dx - (pi->x % pi->dx)) {
  328. for (pi->resno = pi->poc.resno0; pi->resno < int_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
  329. int levelno;
  330. int trx0, try0;
  331. int trx1, try1;
  332. int rpx, rpy;
  333. int prci, prcj;
  334. res = &comp->resolutions[pi->resno];
  335. levelno = comp->numresolutions - 1 - pi->resno;
  336. trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
  337. try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
  338. trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);
  339. try1 = int_ceildiv(pi->ty1, comp->dy << levelno);
  340. rpx = res->pdx + levelno;
  341. rpy = res->pdy + levelno;
  342. if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
  343. continue;
  344. }
  345. if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
  346. continue;
  347. }
  348. if ((res->pw==0)||(res->ph==0)) continue;
  349. if ((trx0==trx1)||(try0==try1)) continue;
  350. prci = int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno), res->pdx)
  351. - int_floordivpow2(trx0, res->pdx);
  352. prcj = int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno), res->pdy)
  353. - int_floordivpow2(try0, res->pdy);
  354. pi->precno = prci + prcj * res->pw;
  355. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  356. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno * pi->step_c + pi->precno * pi->step_p;
  357. if (!pi->include[index]) {
  358. pi->include[index] = 1;
  359. return OPJ_TRUE;
  360. }
  361. LABEL_SKIP:;
  362. }
  363. }
  364. }
  365. }
  366. }
  367. return OPJ_FALSE;
  368. }
  369. /*
  370. ==========================================================
  371. Packet iterator interface
  372. ==========================================================
  373. */
  374. opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
  375. int p, q;
  376. int compno, resno, pino;
  377. opj_pi_iterator_t *pi = NULL;
  378. opj_tcp_t *tcp = NULL;
  379. opj_tccp_t *tccp = NULL;
  380. tcp = &cp->tcps[tileno];
  381. pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
  382. if(!pi) {
  383. /* TODO: throw an error */
  384. return NULL;
  385. }
  386. for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */
  387. int maxres = 0;
  388. int maxprec = 0;
  389. p = tileno % cp->tw;
  390. q = tileno / cp->tw;
  391. pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
  392. pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
  393. pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
  394. pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
  395. pi[pino].numcomps = image->numcomps;
  396. pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
  397. if(!pi[pino].comps) {
  398. /* TODO: throw an error */
  399. pi_destroy(pi, cp, tileno);
  400. return NULL;
  401. }
  402. for (compno = 0; compno < pi->numcomps; compno++) {
  403. int tcx0, tcy0, tcx1, tcy1;
  404. opj_pi_comp_t *comp = &pi[pino].comps[compno];
  405. tccp = &tcp->tccps[compno];
  406. comp->dx = image->comps[compno].dx;
  407. comp->dy = image->comps[compno].dy;
  408. comp->numresolutions = tccp->numresolutions;
  409. comp->resolutions = (opj_pi_resolution_t*) opj_calloc(comp->numresolutions, sizeof(opj_pi_resolution_t));
  410. if(!comp->resolutions) {
  411. /* TODO: throw an error */
  412. pi_destroy(pi, cp, tileno);
  413. return NULL;
  414. }
  415. tcx0 = int_ceildiv(pi->tx0, comp->dx);
  416. tcy0 = int_ceildiv(pi->ty0, comp->dy);
  417. tcx1 = int_ceildiv(pi->tx1, comp->dx);
  418. tcy1 = int_ceildiv(pi->ty1, comp->dy);
  419. if (comp->numresolutions > maxres) {
  420. maxres = comp->numresolutions;
  421. }
  422. for (resno = 0; resno < comp->numresolutions; resno++) {
  423. int levelno;
  424. int rx0, ry0, rx1, ry1;
  425. int px0, py0, px1, py1;
  426. opj_pi_resolution_t *res = &comp->resolutions[resno];
  427. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  428. res->pdx = tccp->prcw[resno];
  429. res->pdy = tccp->prch[resno];
  430. } else {
  431. res->pdx = 15;
  432. res->pdy = 15;
  433. }
  434. levelno = comp->numresolutions - 1 - resno;
  435. rx0 = int_ceildivpow2(tcx0, levelno);
  436. ry0 = int_ceildivpow2(tcy0, levelno);
  437. rx1 = int_ceildivpow2(tcx1, levelno);
  438. ry1 = int_ceildivpow2(tcy1, levelno);
  439. px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
  440. py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
  441. px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
  442. py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
  443. res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
  444. res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
  445. if (res->pw*res->ph > maxprec) {
  446. maxprec = res->pw*res->ph;
  447. }
  448. }
  449. }
  450. tccp = &tcp->tccps[0];
  451. pi[pino].step_p = 1;
  452. pi[pino].step_c = maxprec * pi[pino].step_p;
  453. pi[pino].step_r = image->numcomps * pi[pino].step_c;
  454. pi[pino].step_l = maxres * pi[pino].step_r;
  455. if (pino == 0) {
  456. pi[pino].include = (short int*) opj_calloc(image->numcomps * maxres * tcp->numlayers * maxprec, sizeof(short int));
  457. if(!pi[pino].include) {
  458. /* TODO: throw an error */
  459. pi_destroy(pi, cp, tileno);
  460. return NULL;
  461. }
  462. }
  463. else {
  464. pi[pino].include = pi[pino - 1].include;
  465. }
  466. if (tcp->POC == 0) {
  467. pi[pino].first = 1;
  468. pi[pino].poc.resno0 = 0;
  469. pi[pino].poc.compno0 = 0;
  470. pi[pino].poc.layno1 = tcp->numlayers;
  471. pi[pino].poc.resno1 = maxres;
  472. pi[pino].poc.compno1 = image->numcomps;
  473. pi[pino].poc.prg = tcp->prg;
  474. } else {
  475. pi[pino].first = 1;
  476. pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
  477. pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
  478. pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
  479. pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
  480. pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
  481. pi[pino].poc.prg = tcp->pocs[pino].prg;
  482. }
  483. pi[pino].poc.layno0 = 0;
  484. pi[pino].poc.precno0 = 0;
  485. pi[pino].poc.precno1 = maxprec;
  486. }
  487. return pi;
  488. }
  489. opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){
  490. int p, q, pino;
  491. int compno, resno;
  492. int maxres = 0;
  493. int maxprec = 0;
  494. opj_pi_iterator_t *pi = NULL;
  495. opj_tcp_t *tcp = NULL;
  496. opj_tccp_t *tccp = NULL;
  497. tcp = &cp->tcps[tileno];
  498. pi = (opj_pi_iterator_t*) opj_calloc((tcp->numpocs + 1), sizeof(opj_pi_iterator_t));
  499. if(!pi) { return NULL;}
  500. pi->tp_on = cp->tp_on;
  501. for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
  502. p = tileno % cp->tw;
  503. q = tileno / cp->tw;
  504. pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
  505. pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
  506. pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
  507. pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
  508. pi[pino].numcomps = image->numcomps;
  509. pi[pino].comps = (opj_pi_comp_t*) opj_calloc(image->numcomps, sizeof(opj_pi_comp_t));
  510. if(!pi[pino].comps) {
  511. pi_destroy(pi, cp, tileno);
  512. return NULL;
  513. }
  514. for (compno = 0; compno < pi[pino].numcomps; compno++) {
  515. int tcx0, tcy0, tcx1, tcy1;
  516. opj_pi_comp_t *comp = &pi[pino].comps[compno];
  517. tccp = &tcp->tccps[compno];
  518. comp->dx = image->comps[compno].dx;
  519. comp->dy = image->comps[compno].dy;
  520. comp->numresolutions = tccp->numresolutions;
  521. comp->resolutions = (opj_pi_resolution_t*) opj_malloc(comp->numresolutions * sizeof(opj_pi_resolution_t));
  522. if(!comp->resolutions) {
  523. pi_destroy(pi, cp, tileno);
  524. return NULL;
  525. }
  526. tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
  527. tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
  528. tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
  529. tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
  530. if (comp->numresolutions > maxres) {
  531. maxres = comp->numresolutions;
  532. }
  533. for (resno = 0; resno < comp->numresolutions; resno++) {
  534. int levelno;
  535. int rx0, ry0, rx1, ry1;
  536. int px0, py0, px1, py1;
  537. opj_pi_resolution_t *res = &comp->resolutions[resno];
  538. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  539. res->pdx = tccp->prcw[resno];
  540. res->pdy = tccp->prch[resno];
  541. } else {
  542. res->pdx = 15;
  543. res->pdy = 15;
  544. }
  545. levelno = comp->numresolutions - 1 - resno;
  546. rx0 = int_ceildivpow2(tcx0, levelno);
  547. ry0 = int_ceildivpow2(tcy0, levelno);
  548. rx1 = int_ceildivpow2(tcx1, levelno);
  549. ry1 = int_ceildivpow2(tcy1, levelno);
  550. px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
  551. py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
  552. px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
  553. py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
  554. res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
  555. res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
  556. if (res->pw*res->ph > maxprec) {
  557. maxprec = res->pw * res->ph;
  558. }
  559. }
  560. }
  561. tccp = &tcp->tccps[0];
  562. pi[pino].step_p = 1;
  563. pi[pino].step_c = maxprec * pi[pino].step_p;
  564. pi[pino].step_r = image->numcomps * pi[pino].step_c;
  565. pi[pino].step_l = maxres * pi[pino].step_r;
  566. for (compno = 0; compno < pi->numcomps; compno++) {
  567. opj_pi_comp_t *comp = &pi->comps[compno];
  568. for (resno = 0; resno < comp->numresolutions; resno++) {
  569. int dx, dy;
  570. opj_pi_resolution_t *res = &comp->resolutions[resno];
  571. dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
  572. dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
  573. pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
  574. pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
  575. }
  576. }
  577. if (pino == 0) {
  578. pi[pino].include = (short int*) opj_calloc(tcp->numlayers * pi[pino].step_l, sizeof(short int));
  579. if(!pi[pino].include) {
  580. pi_destroy(pi, cp, tileno);
  581. return NULL;
  582. }
  583. }
  584. else {
  585. pi[pino].include = pi[pino - 1].include;
  586. }
  587. /* Generation of boundaries for each prog flag*/
  588. if(tcp->POC && ( cp->cinema || ((!cp->cinema) && (t2_mode == FINAL_PASS)))){
  589. tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
  590. tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
  591. tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
  592. tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
  593. tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
  594. tcp->pocs[pino].prg = tcp->pocs[pino].prg1;
  595. if (pino > 0)
  596. tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
  597. }else {
  598. tcp->pocs[pino].compS= 0;
  599. tcp->pocs[pino].compE= image->numcomps;
  600. tcp->pocs[pino].resS = 0;
  601. tcp->pocs[pino].resE = maxres;
  602. tcp->pocs[pino].layS = 0;
  603. tcp->pocs[pino].layE = tcp->numlayers;
  604. tcp->pocs[pino].prg = tcp->prg;
  605. }
  606. tcp->pocs[pino].prcS = 0;
  607. tcp->pocs[pino].prcE = maxprec;;
  608. tcp->pocs[pino].txS = pi[pino].tx0;
  609. tcp->pocs[pino].txE = pi[pino].tx1;
  610. tcp->pocs[pino].tyS = pi[pino].ty0;
  611. tcp->pocs[pino].tyE = pi[pino].ty1;
  612. tcp->pocs[pino].dx = pi[pino].dx;
  613. tcp->pocs[pino].dy = pi[pino].dy;
  614. }
  615. return pi;
  616. }
  617. void pi_destroy(opj_pi_iterator_t *pi, opj_cp_t *cp, int tileno) {
  618. int compno, pino;
  619. opj_tcp_t *tcp = &cp->tcps[tileno];
  620. if(pi) {
  621. for (pino = 0; pino < tcp->numpocs + 1; pino++) {
  622. if(pi[pino].comps) {
  623. for (compno = 0; compno < pi->numcomps; compno++) {
  624. opj_pi_comp_t *comp = &pi[pino].comps[compno];
  625. if(comp->resolutions) {
  626. opj_free(comp->resolutions);
  627. }
  628. }
  629. opj_free(pi[pino].comps);
  630. }
  631. }
  632. if(pi->include) {
  633. opj_free(pi->include);
  634. }
  635. opj_free(pi);
  636. }
  637. }
  638. opj_bool pi_next(opj_pi_iterator_t * pi) {
  639. switch (pi->poc.prg) {
  640. case LRCP:
  641. return pi_next_lrcp(pi);
  642. case RLCP:
  643. return pi_next_rlcp(pi);
  644. case RPCL:
  645. return pi_next_rpcl(pi);
  646. case PCRL:
  647. return pi_next_pcrl(pi);
  648. case CPRL:
  649. return pi_next_cprl(pi);
  650. case PROG_UNKNOWN:
  651. return OPJ_FALSE;
  652. }
  653. return OPJ_FALSE;
  654. }
  655. opj_bool pi_create_encode( opj_pi_iterator_t *pi, opj_cp_t *cp,int tileno, int pino,int tpnum, int tppos, J2K_T2_MODE t2_mode,int cur_totnum_tp){
  656. char prog[4];
  657. int i;
  658. int incr_top=1,resetX=0;
  659. opj_tcp_t *tcps =&cp->tcps[tileno];
  660. opj_poc_t *tcp= &tcps->pocs[pino];
  661. pi[pino].first = 1;
  662. pi[pino].poc.prg = tcp->prg;
  663. switch(tcp->prg){
  664. case CPRL: strncpy(prog, "CPRL",4);
  665. break;
  666. case LRCP: strncpy(prog, "LRCP",4);
  667. break;
  668. case PCRL: strncpy(prog, "PCRL",4);
  669. break;
  670. case RLCP: strncpy(prog, "RLCP",4);
  671. break;
  672. case RPCL: strncpy(prog, "RPCL",4);
  673. break;
  674. case PROG_UNKNOWN:
  675. return OPJ_TRUE;
  676. }
  677. if(!(cp->tp_on && ((!cp->cinema && (t2_mode == FINAL_PASS)) || cp->cinema))){
  678. pi[pino].poc.resno0 = tcp->resS;
  679. pi[pino].poc.resno1 = tcp->resE;
  680. pi[pino].poc.compno0 = tcp->compS;
  681. pi[pino].poc.compno1 = tcp->compE;
  682. pi[pino].poc.layno0 = tcp->layS;
  683. pi[pino].poc.layno1 = tcp->layE;
  684. pi[pino].poc.precno0 = tcp->prcS;
  685. pi[pino].poc.precno1 = tcp->prcE;
  686. pi[pino].poc.tx0 = tcp->txS;
  687. pi[pino].poc.ty0 = tcp->tyS;
  688. pi[pino].poc.tx1 = tcp->txE;
  689. pi[pino].poc.ty1 = tcp->tyE;
  690. }else {
  691. if( tpnum < cur_totnum_tp){
  692. for(i=3;i>=0;i--){
  693. switch(prog[i]){
  694. case 'C':
  695. if (i > tppos){
  696. pi[pino].poc.compno0 = tcp->compS;
  697. pi[pino].poc.compno1 = tcp->compE;
  698. }else{
  699. if (tpnum == 0){
  700. tcp->comp_t = tcp->compS;
  701. pi[pino].poc.compno0 = tcp->comp_t;
  702. pi[pino].poc.compno1 = tcp->comp_t+1;
  703. tcp->comp_t+=1;
  704. }else{
  705. if (incr_top == 1){
  706. if(tcp->comp_t ==tcp->compE){
  707. tcp->comp_t = tcp->compS;
  708. pi[pino].poc.compno0 = tcp->comp_t;
  709. pi[pino].poc.compno1 = tcp->comp_t+1;
  710. tcp->comp_t+=1;
  711. incr_top=1;
  712. }else{
  713. pi[pino].poc.compno0 = tcp->comp_t;
  714. pi[pino].poc.compno1 = tcp->comp_t+1;
  715. tcp->comp_t+=1;
  716. incr_top=0;
  717. }
  718. }else{
  719. pi[pino].poc.compno0 = tcp->comp_t-1;
  720. pi[pino].poc.compno1 = tcp->comp_t;
  721. }
  722. }
  723. }
  724. break;
  725. case 'R':
  726. if (i > tppos){
  727. pi[pino].poc.resno0 = tcp->resS;
  728. pi[pino].poc.resno1 = tcp->resE;
  729. }else{
  730. if (tpnum == 0){
  731. tcp->res_t = tcp->resS;
  732. pi[pino].poc.resno0 = tcp->res_t;
  733. pi[pino].poc.resno1 = tcp->res_t+1;
  734. tcp->res_t+=1;
  735. }else{
  736. if (incr_top == 1){
  737. if(tcp->res_t==tcp->resE){
  738. tcp->res_t = tcp->resS;
  739. pi[pino].poc.resno0 = tcp->res_t;
  740. pi[pino].poc.resno1 = tcp->res_t+1;
  741. tcp->res_t+=1;
  742. incr_top=1;
  743. }else{
  744. pi[pino].poc.resno0 = tcp->res_t;
  745. pi[pino].poc.resno1 = tcp->res_t+1;
  746. tcp->res_t+=1;
  747. incr_top=0;
  748. }
  749. }else{
  750. pi[pino].poc.resno0 = tcp->res_t - 1;
  751. pi[pino].poc.resno1 = tcp->res_t;
  752. }
  753. }
  754. }
  755. break;
  756. case 'L':
  757. if (i > tppos){
  758. pi[pino].poc.layno0 = tcp->layS;
  759. pi[pino].poc.layno1 = tcp->layE;
  760. }else{
  761. if (tpnum == 0){
  762. tcp->lay_t = tcp->layS;
  763. pi[pino].poc.layno0 = tcp->lay_t;
  764. pi[pino].poc.layno1 = tcp->lay_t+1;
  765. tcp->lay_t+=1;
  766. }else{
  767. if (incr_top == 1){
  768. if(tcp->lay_t == tcp->layE){
  769. tcp->lay_t = tcp->layS;
  770. pi[pino].poc.layno0 = tcp->lay_t;
  771. pi[pino].poc.layno1 = tcp->lay_t+1;
  772. tcp->lay_t+=1;
  773. incr_top=1;
  774. }else{
  775. pi[pino].poc.layno0 = tcp->lay_t;
  776. pi[pino].poc.layno1 = tcp->lay_t+1;
  777. tcp->lay_t+=1;
  778. incr_top=0;
  779. }
  780. }else{
  781. pi[pino].poc.layno0 = tcp->lay_t - 1;
  782. pi[pino].poc.layno1 = tcp->lay_t;
  783. }
  784. }
  785. }
  786. break;
  787. case 'P':
  788. switch(tcp->prg){
  789. case LRCP:
  790. case RLCP:
  791. if (i > tppos){
  792. pi[pino].poc.precno0 = tcp->prcS;
  793. pi[pino].poc.precno1 = tcp->prcE;
  794. }else{
  795. if (tpnum == 0){
  796. tcp->prc_t = tcp->prcS;
  797. pi[pino].poc.precno0 = tcp->prc_t;
  798. pi[pino].poc.precno1 = tcp->prc_t+1;
  799. tcp->prc_t+=1;
  800. }else{
  801. if (incr_top == 1){
  802. if(tcp->prc_t == tcp->prcE){
  803. tcp->prc_t = tcp->prcS;
  804. pi[pino].poc.precno0 = tcp->prc_t;
  805. pi[pino].poc.precno1 = tcp->prc_t+1;
  806. tcp->prc_t+=1;
  807. incr_top=1;
  808. }else{
  809. pi[pino].poc.precno0 = tcp->prc_t;
  810. pi[pino].poc.precno1 = tcp->prc_t+1;
  811. tcp->prc_t+=1;
  812. incr_top=0;
  813. }
  814. }else{
  815. pi[pino].poc.precno0 = tcp->prc_t - 1;
  816. pi[pino].poc.precno1 = tcp->prc_t;
  817. }
  818. }
  819. }
  820. break;
  821. default:
  822. if (i > tppos){
  823. pi[pino].poc.tx0 = tcp->txS;
  824. pi[pino].poc.ty0 = tcp->tyS;
  825. pi[pino].poc.tx1 = tcp->txE;
  826. pi[pino].poc.ty1 = tcp->tyE;
  827. }else{
  828. if (tpnum == 0){
  829. tcp->tx0_t = tcp->txS;
  830. tcp->ty0_t = tcp->tyS;
  831. pi[pino].poc.tx0 = tcp->tx0_t;
  832. pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
  833. pi[pino].poc.ty0 = tcp->ty0_t;
  834. pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
  835. tcp->tx0_t = pi[pino].poc.tx1;
  836. tcp->ty0_t = pi[pino].poc.ty1;
  837. }else{
  838. if (incr_top == 1){
  839. if(tcp->tx0_t >= tcp->txE){
  840. if(tcp->ty0_t >= tcp->tyE){
  841. tcp->ty0_t = tcp->tyS;
  842. pi[pino].poc.ty0 = tcp->ty0_t;
  843. pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
  844. tcp->ty0_t = pi[pino].poc.ty1;
  845. incr_top=1;resetX=1;
  846. }else{
  847. pi[pino].poc.ty0 = tcp->ty0_t;
  848. pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
  849. tcp->ty0_t = pi[pino].poc.ty1;
  850. incr_top=0;resetX=1;
  851. }
  852. if(resetX==1){
  853. tcp->tx0_t = tcp->txS;
  854. pi[pino].poc.tx0 = tcp->tx0_t;
  855. pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
  856. tcp->tx0_t = pi[pino].poc.tx1;
  857. }
  858. }else{
  859. pi[pino].poc.tx0 = tcp->tx0_t;
  860. pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx- (tcp->tx0_t % tcp->dx);
  861. tcp->tx0_t = pi[pino].poc.tx1;
  862. pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
  863. pi[pino].poc.ty1 = tcp->ty0_t ;
  864. incr_top=0;
  865. }
  866. }else{
  867. pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
  868. pi[pino].poc.tx1 = tcp->tx0_t ;
  869. pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
  870. pi[pino].poc.ty1 = tcp->ty0_t ;
  871. }
  872. }
  873. }
  874. break;
  875. }
  876. break;
  877. }
  878. }
  879. }
  880. }
  881. return OPJ_FALSE;
  882. }