jpc_t2enc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 Encoder
  64. *
  65. * $Id: jpc_t2enc.c,v 1.2 2008-05-26 09:40:52 vp153 Exp $
  66. */
  67. /******************************************************************************\
  68. * Includes.
  69. \******************************************************************************/
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <assert.h>
  73. #include "jasper/jas_fix.h"
  74. #include "jasper/jas_malloc.h"
  75. #include "jasper/jas_math.h"
  76. #include "jasper/jas_debug.h"
  77. #include "jpc_flt.h"
  78. #include "jpc_t2enc.h"
  79. #include "jpc_t2cod.h"
  80. #include "jpc_tagtree.h"
  81. #include "jpc_enc.h"
  82. #include "jpc_math.h"
  83. /******************************************************************************\
  84. * Code.
  85. \******************************************************************************/
  86. static int jpc_putcommacode(jpc_bitstream_t *out, int n)
  87. {
  88. assert(n >= 0);
  89. while (--n >= 0) {
  90. if (jpc_bitstream_putbit(out, 1) == EOF) {
  91. return -1;
  92. }
  93. }
  94. if (jpc_bitstream_putbit(out, 0) == EOF) {
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. static int jpc_putnumnewpasses(jpc_bitstream_t *out, int n)
  100. {
  101. int ret;
  102. if (n <= 0) {
  103. return -1;
  104. } else if (n == 1) {
  105. ret = jpc_bitstream_putbit(out, 0);
  106. } else if (n == 2) {
  107. ret = jpc_bitstream_putbits(out, 2, 2);
  108. } else if (n <= 5) {
  109. ret = jpc_bitstream_putbits(out, 4, 0xc | (n - 3));
  110. } else if (n <= 36) {
  111. ret = jpc_bitstream_putbits(out, 9, 0x1e0 | (n - 6));
  112. } else if (n <= 164) {
  113. ret = jpc_bitstream_putbits(out, 16, 0xff80 | (n - 37));
  114. } else {
  115. /* The standard has no provision for encoding a larger value.
  116. In practice, however, it is highly unlikely that this
  117. limitation will ever be encountered. */
  118. return -1;
  119. }
  120. return (ret != EOF) ? 0 : (-1);
  121. }
  122. int jpc_enc_encpkts(jpc_enc_t *enc, jas_stream_t *out)
  123. {
  124. jpc_enc_tile_t *tile;
  125. jpc_pi_t *pi;
  126. tile = enc->curtile;
  127. jpc_init_t2state(enc, 0);
  128. pi = tile->pi;
  129. jpc_pi_init(pi);
  130. if (!jpc_pi_next(pi)) {
  131. for (;;) {
  132. if (jpc_enc_encpkt(enc, out, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi),
  133. jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) {
  134. return -1;
  135. }
  136. if (jpc_pi_next(pi)) {
  137. break;
  138. }
  139. }
  140. }
  141. return 0;
  142. }
  143. int jpc_enc_encpkt(jpc_enc_t *enc, jas_stream_t *out, int compno, int lvlno, int prcno, int lyrno)
  144. {
  145. jpc_enc_tcmpt_t *comp;
  146. jpc_enc_rlvl_t *lvl;
  147. jpc_enc_band_t *band;
  148. jpc_enc_band_t *endbands;
  149. jpc_enc_cblk_t *cblk;
  150. jpc_enc_cblk_t *endcblks;
  151. jpc_bitstream_t *outb;
  152. jpc_enc_pass_t *pass;
  153. jpc_enc_pass_t *startpass;
  154. jpc_enc_pass_t *lastpass;
  155. jpc_enc_pass_t *endpass;
  156. jpc_enc_pass_t *endpasses;
  157. int i;
  158. int included;
  159. int ret;
  160. jpc_tagtreenode_t *leaf;
  161. int n;
  162. int t1;
  163. int t2;
  164. int adjust;
  165. int maxadjust;
  166. int datalen;
  167. int numnewpasses;
  168. int passcount;
  169. jpc_enc_tile_t *tile;
  170. jpc_enc_prc_t *prc;
  171. jpc_enc_cp_t *cp;
  172. jpc_ms_t *ms;
  173. tile = enc->curtile;
  174. cp = enc->cp;
  175. if (cp->tcp.csty & JPC_COD_SOP) {
  176. if (!(ms = jpc_ms_create(JPC_MS_SOP))) {
  177. return -1;
  178. }
  179. ms->parms.sop.seqno = jpc_pi_getind(tile->pi);
  180. if (jpc_putms(out, enc->cstate, ms)) {
  181. return -1;
  182. }
  183. jpc_ms_destroy(ms);
  184. }
  185. outb = jpc_bitstream_sopen(out, "w+");
  186. assert(outb);
  187. if (jpc_bitstream_putbit(outb, 1) == EOF) {
  188. return -1;
  189. }
  190. JAS_DBGLOG(10, ("\n"));
  191. JAS_DBGLOG(10, ("present. "));
  192. comp = &tile->tcmpts[compno];
  193. lvl = &comp->rlvls[lvlno];
  194. endbands = &lvl->bands[lvl->numbands];
  195. for (band = lvl->bands; band != endbands; ++band) {
  196. if (!band->data) {
  197. continue;
  198. }
  199. prc = &band->prcs[prcno];
  200. if (!prc->cblks) {
  201. continue;
  202. }
  203. endcblks = &prc->cblks[prc->numcblks];
  204. for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
  205. if (!lyrno) {
  206. leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks);
  207. jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs);
  208. }
  209. pass = cblk->curpass;
  210. included = (pass && pass->lyrno == lyrno);
  211. if (included && (!cblk->numencpasses)) {
  212. assert(pass->lyrno == lyrno);
  213. leaf = jpc_tagtree_getleaf(prc->incltree,
  214. cblk - prc->cblks);
  215. jpc_tagtree_setvalue(prc->incltree, leaf, pass->lyrno);
  216. }
  217. }
  218. endcblks = &prc->cblks[prc->numcblks];
  219. for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
  220. pass = cblk->curpass;
  221. included = (pass && pass->lyrno == lyrno);
  222. if (!cblk->numencpasses) {
  223. leaf = jpc_tagtree_getleaf(prc->incltree,
  224. cblk - prc->cblks);
  225. if (jpc_tagtree_encode(prc->incltree, leaf, lyrno
  226. + 1, outb) < 0) {
  227. return -1;
  228. }
  229. } else {
  230. if (jpc_bitstream_putbit(outb, included) == EOF) {
  231. return -1;
  232. }
  233. }
  234. JAS_DBGLOG(10, ("included=%d ", included));
  235. if (!included) {
  236. continue;
  237. }
  238. if (!cblk->numencpasses) {
  239. i = 1;
  240. leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks);
  241. for (;;) {
  242. if ((ret = jpc_tagtree_encode(prc->nlibtree, leaf, i, outb)) < 0) {
  243. return -1;
  244. }
  245. if (ret) {
  246. break;
  247. }
  248. ++i;
  249. }
  250. assert(leaf->known_ && i == leaf->value_ + 1);
  251. }
  252. endpasses = &cblk->passes[cblk->numpasses];
  253. startpass = pass;
  254. endpass = startpass;
  255. while (endpass != endpasses && endpass->lyrno == lyrno){
  256. ++endpass;
  257. }
  258. numnewpasses = endpass - startpass;
  259. if (jpc_putnumnewpasses(outb, numnewpasses)) {
  260. return -1;
  261. }
  262. JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses));
  263. lastpass = endpass - 1;
  264. n = startpass->start;
  265. passcount = 1;
  266. maxadjust = 0;
  267. for (pass = startpass; pass != endpass; ++pass) {
  268. if (pass->term || pass == lastpass) {
  269. datalen = pass->end - n;
  270. t1 = jpc_firstone(datalen) + 1;
  271. t2 = cblk->numlenbits + jpc_floorlog2(passcount);
  272. adjust = JAS_MAX(t1 - t2, 0);
  273. maxadjust = JAS_MAX(adjust, maxadjust);
  274. n += datalen;
  275. passcount = 1;
  276. } else {
  277. ++passcount;
  278. }
  279. }
  280. if (jpc_putcommacode(outb, maxadjust)) {
  281. return -1;
  282. }
  283. cblk->numlenbits += maxadjust;
  284. lastpass = endpass - 1;
  285. n = startpass->start;
  286. passcount = 1;
  287. for (pass = startpass; pass != endpass; ++pass) {
  288. if (pass->term || pass == lastpass) {
  289. datalen = pass->end - n;
  290. assert(jpc_firstone(datalen) < cblk->numlenbits + jpc_floorlog2(passcount));
  291. if (jpc_bitstream_putbits(outb, cblk->numlenbits + jpc_floorlog2(passcount), datalen) == EOF) {
  292. return -1;
  293. }
  294. n += datalen;
  295. passcount = 1;
  296. } else {
  297. ++passcount;
  298. }
  299. }
  300. }
  301. }
  302. jpc_bitstream_outalign(outb, 0);
  303. jpc_bitstream_close(outb);
  304. if (cp->tcp.csty & JPC_COD_EPH) {
  305. if (!(ms = jpc_ms_create(JPC_MS_EPH))) {
  306. return -1;
  307. }
  308. jpc_putms(out, enc->cstate, ms);
  309. jpc_ms_destroy(ms);
  310. }
  311. comp = &tile->tcmpts[compno];
  312. lvl = &comp->rlvls[lvlno];
  313. endbands = &lvl->bands[lvl->numbands];
  314. for (band = lvl->bands; band != endbands; ++band) {
  315. if (!band->data) {
  316. continue;
  317. }
  318. prc = &band->prcs[prcno];
  319. if (!prc->cblks) {
  320. continue;
  321. }
  322. endcblks = &prc->cblks[prc->numcblks];
  323. for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
  324. pass = cblk->curpass;
  325. if (!pass) {
  326. continue;
  327. }
  328. if (pass->lyrno != lyrno) {
  329. assert(pass->lyrno < 0 || pass->lyrno > lyrno);
  330. continue;
  331. }
  332. endpasses = &cblk->passes[cblk->numpasses];
  333. startpass = pass;
  334. endpass = startpass;
  335. while (endpass != endpasses && endpass->lyrno == lyrno){
  336. ++endpass;
  337. }
  338. lastpass = endpass - 1;
  339. numnewpasses = endpass - startpass;
  340. jas_stream_seek(cblk->stream, startpass->start, SEEK_SET);
  341. assert(jas_stream_tell(cblk->stream) == startpass->start);
  342. if (jas_stream_copy(out, cblk->stream, lastpass->end - startpass->start)) {
  343. return -1;
  344. }
  345. cblk->curpass = (endpass != endpasses) ? endpass : 0;
  346. cblk->numencpasses += numnewpasses;
  347. }
  348. }
  349. return 0;
  350. }
  351. void jpc_save_t2state(jpc_enc_t *enc)
  352. {
  353. /* stream pos in embedded T1 stream may be wrong since not saved/restored! */
  354. jpc_enc_tcmpt_t *comp;
  355. jpc_enc_tcmpt_t *endcomps;
  356. jpc_enc_rlvl_t *lvl;
  357. jpc_enc_rlvl_t *endlvls;
  358. jpc_enc_band_t *band;
  359. jpc_enc_band_t *endbands;
  360. jpc_enc_cblk_t *cblk;
  361. jpc_enc_cblk_t *endcblks;
  362. jpc_enc_tile_t *tile;
  363. int prcno;
  364. jpc_enc_prc_t *prc;
  365. tile = enc->curtile;
  366. endcomps = &tile->tcmpts[tile->numtcmpts];
  367. for (comp = tile->tcmpts; comp != endcomps; ++comp) {
  368. endlvls = &comp->rlvls[comp->numrlvls];
  369. for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
  370. if (!lvl->bands) {
  371. continue;
  372. }
  373. endbands = &lvl->bands[lvl->numbands];
  374. for (band = lvl->bands; band != endbands; ++band) {
  375. if (!band->data) {
  376. continue;
  377. }
  378. for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
  379. if (!prc->cblks) {
  380. continue;
  381. }
  382. jpc_tagtree_copy(prc->savincltree, prc->incltree);
  383. jpc_tagtree_copy(prc->savnlibtree, prc->nlibtree);
  384. endcblks = &prc->cblks[prc->numcblks];
  385. for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
  386. cblk->savedcurpass = cblk->curpass;
  387. cblk->savednumencpasses = cblk->numencpasses;
  388. cblk->savednumlenbits = cblk->numlenbits;
  389. }
  390. }
  391. }
  392. }
  393. }
  394. }
  395. void jpc_restore_t2state(jpc_enc_t *enc)
  396. {
  397. jpc_enc_tcmpt_t *comp;
  398. jpc_enc_tcmpt_t *endcomps;
  399. jpc_enc_rlvl_t *lvl;
  400. jpc_enc_rlvl_t *endlvls;
  401. jpc_enc_band_t *band;
  402. jpc_enc_band_t *endbands;
  403. jpc_enc_cblk_t *cblk;
  404. jpc_enc_cblk_t *endcblks;
  405. jpc_enc_tile_t *tile;
  406. int prcno;
  407. jpc_enc_prc_t *prc;
  408. tile = enc->curtile;
  409. endcomps = &tile->tcmpts[tile->numtcmpts];
  410. for (comp = tile->tcmpts; comp != endcomps; ++comp) {
  411. endlvls = &comp->rlvls[comp->numrlvls];
  412. for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
  413. if (!lvl->bands) {
  414. continue;
  415. }
  416. endbands = &lvl->bands[lvl->numbands];
  417. for (band = lvl->bands; band != endbands; ++band) {
  418. if (!band->data) {
  419. continue;
  420. }
  421. for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
  422. if (!prc->cblks) {
  423. continue;
  424. }
  425. jpc_tagtree_copy(prc->incltree, prc->savincltree);
  426. jpc_tagtree_copy(prc->nlibtree, prc->savnlibtree);
  427. endcblks = &prc->cblks[prc->numcblks];
  428. for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
  429. cblk->curpass = cblk->savedcurpass;
  430. cblk->numencpasses = cblk->savednumencpasses;
  431. cblk->numlenbits = cblk->savednumlenbits;
  432. }
  433. }
  434. }
  435. }
  436. }
  437. }
  438. void jpc_init_t2state(jpc_enc_t *enc, int raflag)
  439. {
  440. /* It is assumed that band->numbps and cblk->numbps precomputed */
  441. jpc_enc_tcmpt_t *comp;
  442. jpc_enc_tcmpt_t *endcomps;
  443. jpc_enc_rlvl_t *lvl;
  444. jpc_enc_rlvl_t *endlvls;
  445. jpc_enc_band_t *band;
  446. jpc_enc_band_t *endbands;
  447. jpc_enc_cblk_t *cblk;
  448. jpc_enc_cblk_t *endcblks;
  449. jpc_enc_pass_t *pass;
  450. jpc_enc_pass_t *endpasses;
  451. jpc_tagtreenode_t *leaf;
  452. jpc_enc_tile_t *tile;
  453. int prcno;
  454. jpc_enc_prc_t *prc;
  455. tile = enc->curtile;
  456. endcomps = &tile->tcmpts[tile->numtcmpts];
  457. for (comp = tile->tcmpts; comp != endcomps; ++comp) {
  458. endlvls = &comp->rlvls[comp->numrlvls];
  459. for (lvl = comp->rlvls; lvl != endlvls; ++lvl) {
  460. if (!lvl->bands) {
  461. continue;
  462. }
  463. endbands = &lvl->bands[lvl->numbands];
  464. for (band = lvl->bands; band != endbands; ++band) {
  465. if (!band->data) {
  466. continue;
  467. }
  468. for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) {
  469. if (!prc->cblks) {
  470. continue;
  471. }
  472. jpc_tagtree_reset(prc->incltree);
  473. jpc_tagtree_reset(prc->nlibtree);
  474. endcblks = &prc->cblks[prc->numcblks];
  475. for (cblk = prc->cblks; cblk != endcblks; ++cblk) {
  476. if (jas_stream_rewind(cblk->stream)) {
  477. assert(0);
  478. }
  479. cblk->curpass = (cblk->numpasses > 0) ? cblk->passes : 0;
  480. cblk->numencpasses = 0;
  481. cblk->numlenbits = 3;
  482. cblk->numimsbs = band->numbps - cblk->numbps;
  483. assert(cblk->numimsbs >= 0);
  484. leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks);
  485. jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs);
  486. if (raflag) {
  487. endpasses = &cblk->passes[cblk->numpasses];
  488. for (pass = cblk->passes; pass != endpasses; ++pass) {
  489. pass->lyrno = -1;
  490. pass->lyrno = 0;
  491. }
  492. }
  493. }
  494. }
  495. }
  496. }
  497. }
  498. }
  499. jpc_pi_t *jpc_enc_pi_create(jpc_enc_cp_t *cp, jpc_enc_tile_t *tile)
  500. {
  501. jpc_pi_t *pi;
  502. int compno;
  503. jpc_picomp_t *picomp;
  504. jpc_pirlvl_t *pirlvl;
  505. jpc_enc_tcmpt_t *tcomp;
  506. int rlvlno;
  507. jpc_enc_rlvl_t *rlvl;
  508. int prcno;
  509. int *prclyrno;
  510. if (!(pi = jpc_pi_create0())) {
  511. return 0;
  512. }
  513. pi->pktno = -1;
  514. pi->numcomps = cp->numcmpts;
  515. if (!(pi->picomps = jas_alloc2(pi->numcomps, sizeof(jpc_picomp_t)))) {
  516. jpc_pi_destroy(pi);
  517. return 0;
  518. }
  519. for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno,
  520. ++picomp) {
  521. picomp->pirlvls = 0;
  522. }
  523. for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps;
  524. compno < pi->numcomps; ++compno, ++tcomp, ++picomp) {
  525. picomp->numrlvls = tcomp->numrlvls;
  526. if (!(picomp->pirlvls = jas_alloc2(picomp->numrlvls,
  527. sizeof(jpc_pirlvl_t)))) {
  528. jpc_pi_destroy(pi);
  529. return 0;
  530. }
  531. for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno <
  532. picomp->numrlvls; ++rlvlno, ++pirlvl) {
  533. pirlvl->prclyrnos = 0;
  534. }
  535. for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
  536. rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
  537. /* XXX sizeof(long) should be sizeof different type */
  538. pirlvl->numprcs = rlvl->numprcs;
  539. if (rlvl->numprcs) {
  540. if (!(pirlvl->prclyrnos = jas_alloc2(pirlvl->numprcs,
  541. sizeof(long)))) {
  542. jpc_pi_destroy(pi);
  543. return 0;
  544. }
  545. } else {
  546. pirlvl->prclyrnos = 0;
  547. }
  548. }
  549. }
  550. pi->maxrlvls = 0;
  551. for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps;
  552. compno < pi->numcomps; ++compno, ++tcomp, ++picomp) {
  553. picomp->hsamp = cp->ccps[compno].sampgrdstepx;
  554. picomp->vsamp = cp->ccps[compno].sampgrdstepy;
  555. for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls;
  556. rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) {
  557. pirlvl->prcwidthexpn = rlvl->prcwidthexpn;
  558. pirlvl->prcheightexpn = rlvl->prcheightexpn;
  559. for (prcno = 0, prclyrno = pirlvl->prclyrnos;
  560. prcno < pirlvl->numprcs; ++prcno, ++prclyrno) {
  561. *prclyrno = 0;
  562. }
  563. pirlvl->numhprcs = rlvl->numhprcs;
  564. }
  565. if (pi->maxrlvls < tcomp->numrlvls) {
  566. pi->maxrlvls = tcomp->numrlvls;
  567. }
  568. }
  569. pi->numlyrs = tile->numlyrs;
  570. pi->xstart = tile->tlx;
  571. pi->ystart = tile->tly;
  572. pi->xend = tile->brx;
  573. pi->yend = tile->bry;
  574. pi->picomp = 0;
  575. pi->pirlvl = 0;
  576. pi->x = 0;
  577. pi->y = 0;
  578. pi->compno = 0;
  579. pi->rlvlno = 0;
  580. pi->prcno = 0;
  581. pi->lyrno = 0;
  582. pi->xstep = 0;
  583. pi->ystep = 0;
  584. pi->pchgno = -1;
  585. pi->defaultpchg.prgord = tile->prg;
  586. pi->defaultpchg.compnostart = 0;
  587. pi->defaultpchg.compnoend = pi->numcomps;
  588. pi->defaultpchg.rlvlnostart = 0;
  589. pi->defaultpchg.rlvlnoend = pi->maxrlvls;
  590. pi->defaultpchg.lyrnoend = pi->numlyrs;
  591. pi->pchg = 0;
  592. pi->valid = 0;
  593. return pi;
  594. }