jpc_t2cod.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * Copyright (c) 1999-2000 Image Power, Inc. and the University of
  3. * British Columbia.
  4. * Copyright (c) 2001-2003 Michael David Adams.
  5. * All rights reserved.
  6. */
  7. /* __START_OF_JASPER_LICENSE__
  8. *
  9. * JasPer License Version 2.0
  10. *
  11. * Copyright (c) 2001-2006 Michael David Adams
  12. * Copyright (c) 1999-2000 Image Power, Inc.
  13. * Copyright (c) 1999-2000 The University of British Columbia
  14. *
  15. * All rights reserved.
  16. *
  17. * Permission is hereby granted, free of charge, to any person (the
  18. * "User") obtaining a copy of this software and associated documentation
  19. * files (the "Software"), to deal in the Software without restriction,
  20. * including without limitation the rights to use, copy, modify, merge,
  21. * publish, distribute, and/or sell copies of the Software, and to permit
  22. * persons to whom the Software is furnished to do so, subject to the
  23. * following conditions:
  24. *
  25. * 1. The above copyright notices and this permission notice (which
  26. * includes the disclaimer below) shall be included in all copies or
  27. * substantial portions of the Software.
  28. *
  29. * 2. The name of a copyright holder shall not be used to endorse or
  30. * promote products derived from the Software without specific prior
  31. * written permission.
  32. *
  33. * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
  34. * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  35. * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
  36. * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  37. * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  38. * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
  39. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
  40. * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
  41. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  42. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  43. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
  44. * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
  45. * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
  46. * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
  47. * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
  48. * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
  49. * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
  50. * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
  51. * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
  52. * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
  53. * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
  54. * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
  55. * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
  56. * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
  57. * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
  58. * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
  59. *
  60. * __END_OF_JASPER_LICENSE__
  61. */
  62. /*
  63. * Tier-2 Coding Library
  64. *
  65. * $Id: jpc_t2cod.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. #include "jasper/jas_math.h"
  68. #include "jasper/jas_malloc.h"
  69. #include "jasper/jas_math.h"
  70. #include "jpc_cs.h"
  71. #include "jpc_t2cod.h"
  72. #include "jpc_math.h"
  73. static int jpc_pi_nextlrcp(jpc_pi_t *pi);
  74. static int jpc_pi_nextrlcp(jpc_pi_t *pi);
  75. static int jpc_pi_nextrpcl(jpc_pi_t *pi);
  76. static int jpc_pi_nextpcrl(jpc_pi_t *pi);
  77. static int jpc_pi_nextcprl(jpc_pi_t *pi);
  78. int jpc_pi_next(jpc_pi_t *pi)
  79. {
  80. jpc_pchg_t *pchg;
  81. int ret;
  82. for (;;) {
  83. pi->valid = false;
  84. if (!pi->pchg) {
  85. ++pi->pchgno;
  86. pi->compno = 0;
  87. pi->rlvlno = 0;
  88. pi->prcno = 0;
  89. pi->lyrno = 0;
  90. pi->prgvolfirst = true;
  91. if (pi->pchgno < jpc_pchglist_numpchgs(pi->pchglist)) {
  92. pi->pchg = jpc_pchglist_get(pi->pchglist, pi->pchgno);
  93. } else if (pi->pchgno == jpc_pchglist_numpchgs(pi->pchglist)) {
  94. pi->pchg = &pi->defaultpchg;
  95. } else {
  96. return 1;
  97. }
  98. }
  99. pchg = pi->pchg;
  100. switch (pchg->prgord) {
  101. case JPC_COD_LRCPPRG:
  102. ret = jpc_pi_nextlrcp(pi);
  103. break;
  104. case JPC_COD_RLCPPRG:
  105. ret = jpc_pi_nextrlcp(pi);
  106. break;
  107. case JPC_COD_RPCLPRG:
  108. ret = jpc_pi_nextrpcl(pi);
  109. break;
  110. case JPC_COD_PCRLPRG:
  111. ret = jpc_pi_nextpcrl(pi);
  112. break;
  113. case JPC_COD_CPRLPRG:
  114. ret = jpc_pi_nextcprl(pi);
  115. break;
  116. default:
  117. ret = -1;
  118. break;
  119. }
  120. if (!ret) {
  121. pi->valid = true;
  122. ++pi->pktno;
  123. return 0;
  124. }
  125. pi->pchg = 0;
  126. }
  127. }
  128. static int jpc_pi_nextlrcp(register jpc_pi_t *pi)
  129. {
  130. jpc_pchg_t *pchg;
  131. int *prclyrno;
  132. pchg = pi->pchg;
  133. if (!pi->prgvolfirst) {
  134. prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
  135. goto skip;
  136. } else {
  137. pi->prgvolfirst = false;
  138. }
  139. for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno <
  140. JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
  141. for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls &&
  142. pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) {
  143. for (pi->compno = pchg->compnostart, pi->picomp =
  144. &pi->picomps[pi->compno]; pi->compno < pi->numcomps
  145. && pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno,
  146. ++pi->picomp) {
  147. if (pi->rlvlno >= pi->picomp->numrlvls) {
  148. continue;
  149. }
  150. pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
  151. for (pi->prcno = 0, prclyrno =
  152. pi->pirlvl->prclyrnos; pi->prcno <
  153. pi->pirlvl->numprcs; ++pi->prcno,
  154. ++prclyrno) {
  155. if (pi->lyrno >= *prclyrno) {
  156. *prclyrno = pi->lyrno;
  157. ++(*prclyrno);
  158. return 0;
  159. }
  160. skip:
  161. ;
  162. }
  163. }
  164. }
  165. }
  166. return 1;
  167. }
  168. static int jpc_pi_nextrlcp(register jpc_pi_t *pi)
  169. {
  170. jpc_pchg_t *pchg;
  171. int *prclyrno;
  172. pchg = pi->pchg;
  173. if (!pi->prgvolfirst) {
  174. assert(pi->prcno < pi->pirlvl->numprcs);
  175. prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
  176. goto skip;
  177. } else {
  178. pi->prgvolfirst = 0;
  179. }
  180. for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls &&
  181. pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) {
  182. for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno <
  183. JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
  184. for (pi->compno = pchg->compnostart, pi->picomp =
  185. &pi->picomps[pi->compno]; pi->compno < pi->numcomps &&
  186. pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno, ++pi->picomp) {
  187. if (pi->rlvlno >= pi->picomp->numrlvls) {
  188. continue;
  189. }
  190. pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
  191. for (pi->prcno = 0, prclyrno = pi->pirlvl->prclyrnos;
  192. pi->prcno < pi->pirlvl->numprcs; ++pi->prcno, ++prclyrno) {
  193. if (pi->lyrno >= *prclyrno) {
  194. *prclyrno = pi->lyrno;
  195. ++(*prclyrno);
  196. return 0;
  197. }
  198. skip:
  199. ;
  200. }
  201. }
  202. }
  203. }
  204. return 1;
  205. }
  206. static int jpc_pi_nextrpcl(register jpc_pi_t *pi)
  207. {
  208. int rlvlno;
  209. jpc_pirlvl_t *pirlvl;
  210. jpc_pchg_t *pchg;
  211. int prchind;
  212. int prcvind;
  213. int *prclyrno;
  214. int compno;
  215. jpc_picomp_t *picomp;
  216. int xstep;
  217. int ystep;
  218. uint_fast32_t r;
  219. uint_fast32_t rpx;
  220. uint_fast32_t rpy;
  221. uint_fast32_t trx0;
  222. uint_fast32_t try0;
  223. pchg = pi->pchg;
  224. if (!pi->prgvolfirst) {
  225. goto skip;
  226. } else {
  227. pi->xstep = 0;
  228. pi->ystep = 0;
  229. for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
  230. ++compno, ++picomp) {
  231. for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
  232. picomp->numrlvls; ++rlvlno, ++pirlvl) {
  233. xstep = picomp->hsamp * (1 << (pirlvl->prcwidthexpn +
  234. picomp->numrlvls - rlvlno - 1));
  235. ystep = picomp->vsamp * (1 << (pirlvl->prcheightexpn +
  236. picomp->numrlvls - rlvlno - 1));
  237. pi->xstep = (!pi->xstep) ? xstep : JAS_MIN(pi->xstep, xstep);
  238. pi->ystep = (!pi->ystep) ? ystep : JAS_MIN(pi->ystep, ystep);
  239. }
  240. }
  241. pi->prgvolfirst = 0;
  242. }
  243. for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pchg->rlvlnoend &&
  244. pi->rlvlno < pi->maxrlvls; ++pi->rlvlno) {
  245. for (pi->y = pi->ystart; pi->y < pi->yend; pi->y +=
  246. pi->ystep - (pi->y % pi->ystep)) {
  247. for (pi->x = pi->xstart; pi->x < pi->xend; pi->x +=
  248. pi->xstep - (pi->x % pi->xstep)) {
  249. for (pi->compno = pchg->compnostart,
  250. pi->picomp = &pi->picomps[pi->compno];
  251. pi->compno < JAS_CAST(int, pchg->compnoend) && pi->compno <
  252. pi->numcomps; ++pi->compno, ++pi->picomp) {
  253. if (pi->rlvlno >= pi->picomp->numrlvls) {
  254. continue;
  255. }
  256. pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
  257. if (pi->pirlvl->numprcs == 0) {
  258. continue;
  259. }
  260. r = pi->picomp->numrlvls - 1 - pi->rlvlno;
  261. rpx = r + pi->pirlvl->prcwidthexpn;
  262. rpy = r + pi->pirlvl->prcheightexpn;
  263. trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r);
  264. try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r);
  265. if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx)))
  266. || !(pi->x % (1 << rpx))) &&
  267. ((pi->y == pi->ystart && ((try0 << r) % (1 << rpy)))
  268. || !(pi->y % (1 << rpy)))) {
  269. prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp
  270. << r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0,
  271. pi->pirlvl->prcwidthexpn);
  272. prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp
  273. << r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0,
  274. pi->pirlvl->prcheightexpn);
  275. pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind;
  276. assert(pi->prcno < pi->pirlvl->numprcs);
  277. for (pi->lyrno = 0; pi->lyrno <
  278. pi->numlyrs && pi->lyrno < JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
  279. prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
  280. if (pi->lyrno >= *prclyrno) {
  281. ++(*prclyrno);
  282. return 0;
  283. }
  284. skip:
  285. ;
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. return 1;
  293. }
  294. static int jpc_pi_nextpcrl(register jpc_pi_t *pi)
  295. {
  296. int rlvlno;
  297. jpc_pirlvl_t *pirlvl;
  298. jpc_pchg_t *pchg;
  299. int prchind;
  300. int prcvind;
  301. int *prclyrno;
  302. int compno;
  303. jpc_picomp_t *picomp;
  304. int xstep;
  305. int ystep;
  306. uint_fast32_t trx0;
  307. uint_fast32_t try0;
  308. uint_fast32_t r;
  309. uint_fast32_t rpx;
  310. uint_fast32_t rpy;
  311. pchg = pi->pchg;
  312. if (!pi->prgvolfirst) {
  313. goto skip;
  314. } else {
  315. pi->xstep = 0;
  316. pi->ystep = 0;
  317. for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
  318. ++compno, ++picomp) {
  319. for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
  320. picomp->numrlvls; ++rlvlno, ++pirlvl) {
  321. xstep = picomp->hsamp * (1 <<
  322. (pirlvl->prcwidthexpn + picomp->numrlvls -
  323. rlvlno - 1));
  324. ystep = picomp->vsamp * (1 <<
  325. (pirlvl->prcheightexpn + picomp->numrlvls -
  326. rlvlno - 1));
  327. pi->xstep = (!pi->xstep) ? xstep :
  328. JAS_MIN(pi->xstep, xstep);
  329. pi->ystep = (!pi->ystep) ? ystep :
  330. JAS_MIN(pi->ystep, ystep);
  331. }
  332. }
  333. pi->prgvolfirst = 0;
  334. }
  335. for (pi->y = pi->ystart; pi->y < pi->yend; pi->y += pi->ystep -
  336. (pi->y % pi->ystep)) {
  337. for (pi->x = pi->xstart; pi->x < pi->xend; pi->x += pi->xstep -
  338. (pi->x % pi->xstep)) {
  339. for (pi->compno = pchg->compnostart, pi->picomp =
  340. &pi->picomps[pi->compno]; pi->compno < pi->numcomps
  341. && pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno,
  342. ++pi->picomp) {
  343. for (pi->rlvlno = pchg->rlvlnostart,
  344. pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
  345. pi->rlvlno < pi->picomp->numrlvls &&
  346. pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno,
  347. ++pi->pirlvl) {
  348. if (pi->pirlvl->numprcs == 0) {
  349. continue;
  350. }
  351. r = pi->picomp->numrlvls - 1 - pi->rlvlno;
  352. trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r);
  353. try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r);
  354. rpx = r + pi->pirlvl->prcwidthexpn;
  355. rpy = r + pi->pirlvl->prcheightexpn;
  356. if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) ||
  357. !(pi->x % (pi->picomp->hsamp << rpx))) &&
  358. ((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) ||
  359. !(pi->y % (pi->picomp->vsamp << rpy)))) {
  360. prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp
  361. << r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0,
  362. pi->pirlvl->prcwidthexpn);
  363. prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp
  364. << r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0,
  365. pi->pirlvl->prcheightexpn);
  366. pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind;
  367. assert(pi->prcno < pi->pirlvl->numprcs);
  368. for (pi->lyrno = 0; pi->lyrno < pi->numlyrs &&
  369. pi->lyrno < JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
  370. prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
  371. if (pi->lyrno >= *prclyrno) {
  372. ++(*prclyrno);
  373. return 0;
  374. }
  375. skip:
  376. ;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }
  383. return 1;
  384. }
  385. static int jpc_pi_nextcprl(register jpc_pi_t *pi)
  386. {
  387. int rlvlno;
  388. jpc_pirlvl_t *pirlvl;
  389. jpc_pchg_t *pchg;
  390. int prchind;
  391. int prcvind;
  392. int *prclyrno;
  393. uint_fast32_t trx0;
  394. uint_fast32_t try0;
  395. uint_fast32_t r;
  396. uint_fast32_t rpx;
  397. uint_fast32_t rpy;
  398. pchg = pi->pchg;
  399. if (!pi->prgvolfirst) {
  400. goto skip;
  401. } else {
  402. pi->prgvolfirst = 0;
  403. }
  404. for (pi->compno = pchg->compnostart, pi->picomp =
  405. &pi->picomps[pi->compno]; pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno,
  406. ++pi->picomp) {
  407. pirlvl = pi->picomp->pirlvls;
  408. pi->xstep = pi->picomp->hsamp * (1 << (pirlvl->prcwidthexpn +
  409. pi->picomp->numrlvls - 1));
  410. pi->ystep = pi->picomp->vsamp * (1 << (pirlvl->prcheightexpn +
  411. pi->picomp->numrlvls - 1));
  412. for (rlvlno = 1, pirlvl = &pi->picomp->pirlvls[1];
  413. rlvlno < pi->picomp->numrlvls; ++rlvlno, ++pirlvl) {
  414. pi->xstep = JAS_MIN(pi->xstep, pi->picomp->hsamp * (1 <<
  415. (pirlvl->prcwidthexpn + pi->picomp->numrlvls -
  416. rlvlno - 1)));
  417. pi->ystep = JAS_MIN(pi->ystep, pi->picomp->vsamp * (1 <<
  418. (pirlvl->prcheightexpn + pi->picomp->numrlvls -
  419. rlvlno - 1)));
  420. }
  421. for (pi->y = pi->ystart; pi->y < pi->yend;
  422. pi->y += pi->ystep - (pi->y % pi->ystep)) {
  423. for (pi->x = pi->xstart; pi->x < pi->xend;
  424. pi->x += pi->xstep - (pi->x % pi->xstep)) {
  425. for (pi->rlvlno = pchg->rlvlnostart,
  426. pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno];
  427. pi->rlvlno < pi->picomp->numrlvls && pi->rlvlno <
  428. pchg->rlvlnoend; ++pi->rlvlno, ++pi->pirlvl) {
  429. if (pi->pirlvl->numprcs == 0) {
  430. continue;
  431. }
  432. r = pi->picomp->numrlvls - 1 - pi->rlvlno;
  433. trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r);
  434. try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r);
  435. rpx = r + pi->pirlvl->prcwidthexpn;
  436. rpy = r + pi->pirlvl->prcheightexpn;
  437. if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) ||
  438. !(pi->x % (pi->picomp->hsamp << rpx))) &&
  439. ((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) ||
  440. !(pi->y % (pi->picomp->vsamp << rpy)))) {
  441. prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp
  442. << r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0,
  443. pi->pirlvl->prcwidthexpn);
  444. prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp
  445. << r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0,
  446. pi->pirlvl->prcheightexpn);
  447. pi->prcno = prcvind *
  448. pi->pirlvl->numhprcs +
  449. prchind;
  450. assert(pi->prcno <
  451. pi->pirlvl->numprcs);
  452. for (pi->lyrno = 0; pi->lyrno <
  453. pi->numlyrs && pi->lyrno < JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) {
  454. prclyrno = &pi->pirlvl->prclyrnos[pi->prcno];
  455. if (pi->lyrno >= *prclyrno) {
  456. ++(*prclyrno);
  457. return 0;
  458. }
  459. skip:
  460. ;
  461. }
  462. }
  463. }
  464. }
  465. }
  466. }
  467. return 1;
  468. }
  469. static void pirlvl_destroy(jpc_pirlvl_t *rlvl)
  470. {
  471. if (rlvl->prclyrnos) {
  472. jas_free(rlvl->prclyrnos);
  473. }
  474. }
  475. static void jpc_picomp_destroy(jpc_picomp_t *picomp)
  476. {
  477. int rlvlno;
  478. jpc_pirlvl_t *pirlvl;
  479. if (picomp->pirlvls) {
  480. for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
  481. picomp->numrlvls; ++rlvlno, ++pirlvl) {
  482. pirlvl_destroy(pirlvl);
  483. }
  484. jas_free(picomp->pirlvls);
  485. }
  486. }
  487. void jpc_pi_destroy(jpc_pi_t *pi)
  488. {
  489. jpc_picomp_t *picomp;
  490. int compno;
  491. if (pi->picomps) {
  492. for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
  493. ++compno, ++picomp) {
  494. jpc_picomp_destroy(picomp);
  495. }
  496. jas_free(pi->picomps);
  497. }
  498. if (pi->pchglist) {
  499. jpc_pchglist_destroy(pi->pchglist);
  500. }
  501. jas_free(pi);
  502. }
  503. jpc_pi_t *jpc_pi_create0()
  504. {
  505. jpc_pi_t *pi;
  506. if (!(pi = jas_malloc(sizeof(jpc_pi_t)))) {
  507. return 0;
  508. }
  509. pi->picomps = 0;
  510. pi->pchgno = 0;
  511. if (!(pi->pchglist = jpc_pchglist_create())) {
  512. jas_free(pi);
  513. return 0;
  514. }
  515. return pi;
  516. }
  517. int jpc_pi_addpchg(jpc_pi_t *pi, jpc_pocpchg_t *pchg)
  518. {
  519. return jpc_pchglist_insert(pi->pchglist, -1, pchg);
  520. }
  521. jpc_pchglist_t *jpc_pchglist_create()
  522. {
  523. jpc_pchglist_t *pchglist;
  524. if (!(pchglist = jas_malloc(sizeof(jpc_pchglist_t)))) {
  525. return 0;
  526. }
  527. pchglist->numpchgs = 0;
  528. pchglist->maxpchgs = 0;
  529. pchglist->pchgs = 0;
  530. return pchglist;
  531. }
  532. int jpc_pchglist_insert(jpc_pchglist_t *pchglist, int pchgno, jpc_pchg_t *pchg)
  533. {
  534. int i;
  535. int newmaxpchgs;
  536. jpc_pchg_t **newpchgs;
  537. if (pchgno < 0) {
  538. pchgno = pchglist->numpchgs;
  539. }
  540. if (pchglist->numpchgs >= pchglist->maxpchgs) {
  541. newmaxpchgs = pchglist->maxpchgs + 128;
  542. if (!(newpchgs = jas_realloc2(pchglist->pchgs, newmaxpchgs, sizeof(jpc_pchg_t *)))) {
  543. return -1;
  544. }
  545. pchglist->maxpchgs = newmaxpchgs;
  546. pchglist->pchgs = newpchgs;
  547. }
  548. for (i = pchglist->numpchgs; i > pchgno; --i) {
  549. pchglist->pchgs[i] = pchglist->pchgs[i - 1];
  550. }
  551. pchglist->pchgs[pchgno] = pchg;
  552. ++pchglist->numpchgs;
  553. return 0;
  554. }
  555. jpc_pchg_t *jpc_pchglist_remove(jpc_pchglist_t *pchglist, int pchgno)
  556. {
  557. int i;
  558. jpc_pchg_t *pchg;
  559. assert(pchgno < pchglist->numpchgs);
  560. pchg = pchglist->pchgs[pchgno];
  561. for (i = pchgno + 1; i < pchglist->numpchgs; ++i) {
  562. pchglist->pchgs[i - 1] = pchglist->pchgs[i];
  563. }
  564. --pchglist->numpchgs;
  565. return pchg;
  566. }
  567. jpc_pchg_t *jpc_pchg_copy(jpc_pchg_t *pchg)
  568. {
  569. jpc_pchg_t *newpchg;
  570. if (!(newpchg = jas_malloc(sizeof(jpc_pchg_t)))) {
  571. return 0;
  572. }
  573. *newpchg = *pchg;
  574. return newpchg;
  575. }
  576. jpc_pchglist_t *jpc_pchglist_copy(jpc_pchglist_t *pchglist)
  577. {
  578. jpc_pchglist_t *newpchglist;
  579. jpc_pchg_t *newpchg;
  580. int pchgno;
  581. if (!(newpchglist = jpc_pchglist_create())) {
  582. return 0;
  583. }
  584. for (pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) {
  585. if (!(newpchg = jpc_pchg_copy(pchglist->pchgs[pchgno])) ||
  586. jpc_pchglist_insert(newpchglist, -1, newpchg)) {
  587. jpc_pchglist_destroy(newpchglist);
  588. return 0;
  589. }
  590. }
  591. return newpchglist;
  592. }
  593. void jpc_pchglist_destroy(jpc_pchglist_t *pchglist)
  594. {
  595. int pchgno;
  596. if (pchglist->pchgs) {
  597. for (pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) {
  598. jpc_pchg_destroy(pchglist->pchgs[pchgno]);
  599. }
  600. jas_free(pchglist->pchgs);
  601. }
  602. jas_free(pchglist);
  603. }
  604. void jpc_pchg_destroy(jpc_pchg_t *pchg)
  605. {
  606. jas_free(pchg);
  607. }
  608. jpc_pchg_t *jpc_pchglist_get(jpc_pchglist_t *pchglist, int pchgno)
  609. {
  610. return pchglist->pchgs[pchgno];
  611. }
  612. int jpc_pchglist_numpchgs(jpc_pchglist_t *pchglist)
  613. {
  614. return pchglist->numpchgs;
  615. }
  616. int jpc_pi_init(jpc_pi_t *pi)
  617. {
  618. int compno;
  619. int rlvlno;
  620. int prcno;
  621. jpc_picomp_t *picomp;
  622. jpc_pirlvl_t *pirlvl;
  623. int *prclyrno;
  624. pi->prgvolfirst = 0;
  625. pi->valid = 0;
  626. pi->pktno = -1;
  627. pi->pchgno = -1;
  628. pi->pchg = 0;
  629. for (compno = 0, picomp = pi->picomps; compno < pi->numcomps;
  630. ++compno, ++picomp) {
  631. for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
  632. picomp->numrlvls; ++rlvlno, ++pirlvl) {
  633. for (prcno = 0, prclyrno = pirlvl->prclyrnos;
  634. prcno < pirlvl->numprcs; ++prcno, ++prclyrno) {
  635. *prclyrno = 0;
  636. }
  637. }
  638. }
  639. return 0;
  640. }