pi.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2006-2007, Parvatha Elangovan
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. * 1. Redistributions of source code must retain the above copyright
  21. * notice, this list of conditions and the following disclaimer.
  22. * 2. Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. #define OPJ_UINT32_SEMANTICALLY_BUT_INT32 OPJ_UINT32
  39. #include "opj_includes.h"
  40. /** @defgroup PI PI - Implementation of a packet iterator */
  41. /*@{*/
  42. /** @name Local static functions */
  43. /*@{*/
  44. /**
  45. Get next packet in layer-resolution-component-precinct order.
  46. @param pi packet iterator to modify
  47. @return returns false if pi pointed to the last packet or else returns true
  48. */
  49. static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi);
  50. /**
  51. Get next packet in resolution-layer-component-precinct order.
  52. @param pi packet iterator to modify
  53. @return returns false if pi pointed to the last packet or else returns true
  54. */
  55. static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi);
  56. /**
  57. Get next packet in resolution-precinct-component-layer order.
  58. @param pi packet iterator to modify
  59. @return returns false if pi pointed to the last packet or else returns true
  60. */
  61. static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi);
  62. /**
  63. Get next packet in precinct-component-resolution-layer order.
  64. @param pi packet iterator to modify
  65. @return returns false if pi pointed to the last packet or else returns true
  66. */
  67. static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi);
  68. /**
  69. Get next packet in component-precinct-resolution-layer order.
  70. @param pi packet iterator to modify
  71. @return returns false if pi pointed to the last packet or else returns true
  72. */
  73. static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi);
  74. /**
  75. * Updates the coding parameters if the encoding is used with Progression order changes and final (or cinema parameters are used).
  76. *
  77. * @param p_cp the coding parameters to modify
  78. * @param p_tileno the tile index being concerned.
  79. * @param p_tx0 X0 parameter for the tile
  80. * @param p_tx1 X1 parameter for the tile
  81. * @param p_ty0 Y0 parameter for the tile
  82. * @param p_ty1 Y1 parameter for the tile
  83. * @param p_max_prec the maximum precision for all the bands of the tile
  84. * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
  85. * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
  86. * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
  87. */
  88. static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
  89. OPJ_UINT32 p_tileno,
  90. OPJ_UINT32 p_tx0,
  91. OPJ_UINT32 p_tx1,
  92. OPJ_UINT32 p_ty0,
  93. OPJ_UINT32 p_ty1,
  94. OPJ_UINT32 p_max_prec,
  95. OPJ_UINT32 p_max_res,
  96. OPJ_UINT32 p_dx_min,
  97. OPJ_UINT32 p_dy_min);
  98. /**
  99. * Updates the coding parameters if the encoding is not used with Progression order changes and final (and cinema parameters are used).
  100. *
  101. * @param p_cp the coding parameters to modify
  102. * @param p_num_comps the number of components
  103. * @param p_tileno the tile index being concerned.
  104. * @param p_tx0 X0 parameter for the tile
  105. * @param p_tx1 X1 parameter for the tile
  106. * @param p_ty0 Y0 parameter for the tile
  107. * @param p_ty1 Y1 parameter for the tile
  108. * @param p_max_prec the maximum precision for all the bands of the tile
  109. * @param p_max_res the maximum number of resolutions for all the poc inside the tile.
  110. * @param p_dx_min the minimum dx of all the components of all the resolutions for the tile.
  111. * @param p_dy_min the minimum dy of all the components of all the resolutions for the tile.
  112. */
  113. static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
  114. OPJ_UINT32 p_num_comps,
  115. OPJ_UINT32 p_tileno,
  116. OPJ_UINT32 p_tx0,
  117. OPJ_UINT32 p_tx1,
  118. OPJ_UINT32 p_ty0,
  119. OPJ_UINT32 p_ty1,
  120. OPJ_UINT32 p_max_prec,
  121. OPJ_UINT32 p_max_res,
  122. OPJ_UINT32 p_dx_min,
  123. OPJ_UINT32 p_dy_min);
  124. /**
  125. * Gets the encoding parameters needed to update the coding parameters and all the pocs.
  126. *
  127. * @param p_image the image being encoded.
  128. * @param p_cp the coding parameters.
  129. * @param tileno the tile index of the tile being encoded.
  130. * @param p_tx0 pointer that will hold the X0 parameter for the tile
  131. * @param p_tx1 pointer that will hold the X1 parameter for the tile
  132. * @param p_ty0 pointer that will hold the Y0 parameter for the tile
  133. * @param p_ty1 pointer that will hold the Y1 parameter for the tile
  134. * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
  135. * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
  136. * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
  137. * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
  138. */
  139. static void opj_get_encoding_parameters(const opj_image_t *p_image,
  140. const opj_cp_t *p_cp,
  141. OPJ_UINT32 tileno,
  142. OPJ_UINT32 * p_tx0,
  143. OPJ_UINT32 * p_tx1,
  144. OPJ_UINT32 * p_ty0,
  145. OPJ_UINT32 * p_ty1,
  146. OPJ_UINT32 * p_dx_min,
  147. OPJ_UINT32 * p_dy_min,
  148. OPJ_UINT32 * p_max_prec,
  149. OPJ_UINT32 * p_max_res);
  150. /**
  151. * Gets the encoding parameters needed to update the coding parameters and all the pocs.
  152. * The precinct widths, heights, dx and dy for each component at each resolution will be stored as well.
  153. * the last parameter of the function should be an array of pointers of size nb components, each pointer leading
  154. * to an area of size 4 * max_res. The data is stored inside this area with the following pattern :
  155. * dx_compi_res0 , dy_compi_res0 , w_compi_res0, h_compi_res0 , dx_compi_res1 , dy_compi_res1 , w_compi_res1, h_compi_res1 , ...
  156. *
  157. * @param p_image the image being encoded.
  158. * @param p_cp the coding parameters.
  159. * @param tileno the tile index of the tile being encoded.
  160. * @param p_tx0 pointer that will hold the X0 parameter for the tile
  161. * @param p_tx1 pointer that will hold the X1 parameter for the tile
  162. * @param p_ty0 pointer that will hold the Y0 parameter for the tile
  163. * @param p_ty1 pointer that will hold the Y1 parameter for the tile
  164. * @param p_max_prec pointer that will hold the maximum precision for all the bands of the tile
  165. * @param p_max_res pointer that will hold the maximum number of resolutions for all the poc inside the tile.
  166. * @param p_dx_min pointer that will hold the minimum dx of all the components of all the resolutions for the tile.
  167. * @param p_dy_min pointer that will hold the minimum dy of all the components of all the resolutions for the tile.
  168. * @param p_resolutions pointer to an area corresponding to the one described above.
  169. */
  170. static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
  171. const opj_cp_t *p_cp,
  172. OPJ_UINT32 tileno,
  173. OPJ_UINT32 * p_tx0,
  174. OPJ_UINT32 * p_tx1,
  175. OPJ_UINT32 * p_ty0,
  176. OPJ_UINT32 * p_ty1,
  177. OPJ_UINT32 * p_dx_min,
  178. OPJ_UINT32 * p_dy_min,
  179. OPJ_UINT32 * p_max_prec,
  180. OPJ_UINT32 * p_max_res,
  181. OPJ_UINT32 ** p_resolutions);
  182. /**
  183. * Allocates memory for a packet iterator. Data and data sizes are set by this operation.
  184. * No other data is set. The include section of the packet iterator is not allocated.
  185. *
  186. * @param p_image the image used to initialize the packet iterator (in fact only the number of components is relevant.
  187. * @param p_cp the coding parameters.
  188. * @param tileno the index of the tile from which creating the packet iterator.
  189. * @param manager Event manager
  190. */
  191. static opj_pi_iterator_t * opj_pi_create(const opj_image_t *p_image,
  192. const opj_cp_t *p_cp,
  193. OPJ_UINT32 tileno,
  194. opj_event_mgr_t* manager);
  195. /**
  196. * FIXME DOC
  197. */
  198. static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
  199. opj_tcp_t * p_tcp,
  200. OPJ_UINT32 p_max_precision,
  201. OPJ_UINT32 p_max_res);
  202. /**
  203. * FIXME DOC
  204. */
  205. static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
  206. opj_tcp_t * p_tcp,
  207. OPJ_UINT32 p_max_precision,
  208. OPJ_UINT32 p_max_res);
  209. /**
  210. * FIXME DOC
  211. */
  212. static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
  213. opj_cp_t *cp,
  214. OPJ_UINT32 tileno,
  215. OPJ_UINT32 pino,
  216. const OPJ_CHAR *prog);
  217. /*@}*/
  218. /*@}*/
  219. /*
  220. ==========================================================
  221. local functions
  222. ==========================================================
  223. */
  224. static OPJ_BOOL opj_pi_next_lrcp(opj_pi_iterator_t * pi)
  225. {
  226. opj_pi_comp_t *comp = NULL;
  227. opj_pi_resolution_t *res = NULL;
  228. OPJ_UINT32 index = 0;
  229. if (pi->poc.compno0 >= pi->numcomps ||
  230. pi->poc.compno1 >= pi->numcomps + 1) {
  231. opj_event_msg(pi->manager, EVT_ERROR,
  232. "opj_pi_next_lrcp(): invalid compno0/compno1\n");
  233. return OPJ_FALSE;
  234. }
  235. if (!pi->first) {
  236. comp = &pi->comps[pi->compno];
  237. res = &comp->resolutions[pi->resno];
  238. goto LABEL_SKIP;
  239. } else {
  240. pi->first = 0;
  241. }
  242. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  243. for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
  244. pi->resno++) {
  245. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  246. comp = &pi->comps[pi->compno];
  247. if (pi->resno >= comp->numresolutions) {
  248. continue;
  249. }
  250. res = &comp->resolutions[pi->resno];
  251. if (!pi->tp_on) {
  252. pi->poc.precno1 = res->pw * res->ph;
  253. }
  254. for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
  255. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
  256. pi->step_c + pi->precno * pi->step_p;
  257. /* Avoids index out of bounds access with */
  258. /* id_000098,sig_11,src_005411,op_havoc,rep_2 of */
  259. /* https://github.com/uclouvain/openjpeg/issues/938 */
  260. /* Not sure if this is the most clever fix. Perhaps */
  261. /* include should be resized when a POC arises, or */
  262. /* the POC should be rejected */
  263. if (index >= pi->include_size) {
  264. opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
  265. return OPJ_FALSE;
  266. }
  267. if (!pi->include[index]) {
  268. pi->include[index] = 1;
  269. return OPJ_TRUE;
  270. }
  271. LABEL_SKIP:
  272. ;
  273. }
  274. }
  275. }
  276. }
  277. return OPJ_FALSE;
  278. }
  279. static OPJ_BOOL opj_pi_next_rlcp(opj_pi_iterator_t * pi)
  280. {
  281. opj_pi_comp_t *comp = NULL;
  282. opj_pi_resolution_t *res = NULL;
  283. OPJ_UINT32 index = 0;
  284. if (pi->poc.compno0 >= pi->numcomps ||
  285. pi->poc.compno1 >= pi->numcomps + 1) {
  286. opj_event_msg(pi->manager, EVT_ERROR,
  287. "opj_pi_next_rlcp(): invalid compno0/compno1\n");
  288. return OPJ_FALSE;
  289. }
  290. if (!pi->first) {
  291. comp = &pi->comps[pi->compno];
  292. res = &comp->resolutions[pi->resno];
  293. goto LABEL_SKIP;
  294. } else {
  295. pi->first = 0;
  296. }
  297. for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
  298. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  299. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  300. comp = &pi->comps[pi->compno];
  301. if (pi->resno >= comp->numresolutions) {
  302. continue;
  303. }
  304. res = &comp->resolutions[pi->resno];
  305. if (!pi->tp_on) {
  306. pi->poc.precno1 = res->pw * res->ph;
  307. }
  308. for (pi->precno = pi->poc.precno0; pi->precno < pi->poc.precno1; pi->precno++) {
  309. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
  310. pi->step_c + pi->precno * pi->step_p;
  311. if (index >= pi->include_size) {
  312. opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
  313. return OPJ_FALSE;
  314. }
  315. if (!pi->include[index]) {
  316. pi->include[index] = 1;
  317. return OPJ_TRUE;
  318. }
  319. LABEL_SKIP:
  320. ;
  321. }
  322. }
  323. }
  324. }
  325. return OPJ_FALSE;
  326. }
  327. static OPJ_BOOL opj_pi_next_rpcl(opj_pi_iterator_t * pi)
  328. {
  329. opj_pi_comp_t *comp = NULL;
  330. opj_pi_resolution_t *res = NULL;
  331. OPJ_UINT32 index = 0;
  332. if (pi->poc.compno0 >= pi->numcomps ||
  333. pi->poc.compno1 >= pi->numcomps + 1) {
  334. opj_event_msg(pi->manager, EVT_ERROR,
  335. "opj_pi_next_rpcl(): invalid compno0/compno1\n");
  336. return OPJ_FALSE;
  337. }
  338. if (!pi->first) {
  339. goto LABEL_SKIP;
  340. } else {
  341. OPJ_UINT32 compno, resno;
  342. pi->first = 0;
  343. pi->dx = 0;
  344. pi->dy = 0;
  345. for (compno = 0; compno < pi->numcomps; compno++) {
  346. comp = &pi->comps[compno];
  347. for (resno = 0; resno < comp->numresolutions; resno++) {
  348. OPJ_UINT32 dx, dy;
  349. res = &comp->resolutions[resno];
  350. if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
  351. comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
  352. dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
  353. pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
  354. }
  355. if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
  356. comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
  357. dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
  358. pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
  359. }
  360. }
  361. }
  362. if (pi->dx == 0 || pi->dy == 0) {
  363. return OPJ_FALSE;
  364. }
  365. }
  366. if (!pi->tp_on) {
  367. pi->poc.ty0 = pi->ty0;
  368. pi->poc.tx0 = pi->tx0;
  369. pi->poc.ty1 = pi->ty1;
  370. pi->poc.tx1 = pi->tx1;
  371. }
  372. for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
  373. for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
  374. pi->y += (pi->dy - (pi->y % pi->dy))) {
  375. for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
  376. pi->x += (pi->dx - (pi->x % pi->dx))) {
  377. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  378. OPJ_UINT32 levelno;
  379. OPJ_UINT32 trx0, try0;
  380. OPJ_UINT32 trx1, try1;
  381. OPJ_UINT32 rpx, rpy;
  382. OPJ_UINT32 prci, prcj;
  383. comp = &pi->comps[pi->compno];
  384. if (pi->resno >= comp->numresolutions) {
  385. continue;
  386. }
  387. res = &comp->resolutions[pi->resno];
  388. levelno = comp->numresolutions - 1 - pi->resno;
  389. /* Avoids division by zero */
  390. /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
  391. /* of https://github.com/uclouvain/openjpeg/issues/938 */
  392. if (levelno >= 32 ||
  393. ((comp->dx << levelno) >> levelno) != comp->dx ||
  394. ((comp->dy << levelno) >> levelno) != comp->dy) {
  395. continue;
  396. }
  397. if ((comp->dx << levelno) > INT_MAX ||
  398. (comp->dy << levelno) > INT_MAX) {
  399. continue;
  400. }
  401. trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno));
  402. try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno));
  403. trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno));
  404. try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno));
  405. rpx = res->pdx + levelno;
  406. rpy = res->pdy + levelno;
  407. /* To avoid divisions by zero / undefined behaviour on shift */
  408. /* in below tests */
  409. /* Fixes reading id:000026,sig:08,src:002419,op:int32,pos:60,val:+32 */
  410. /* of https://github.com/uclouvain/openjpeg/issues/938 */
  411. if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
  412. rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
  413. continue;
  414. }
  415. /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
  416. if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
  417. ((try0 << levelno) % (1U << rpy))))) {
  418. continue;
  419. }
  420. if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
  421. ((trx0 << levelno) % (1U << rpx))))) {
  422. continue;
  423. }
  424. if ((res->pw == 0) || (res->ph == 0)) {
  425. continue;
  426. }
  427. if ((trx0 == trx1) || (try0 == try1)) {
  428. continue;
  429. }
  430. prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x,
  431. (comp->dx << levelno)), res->pdx)
  432. - opj_uint_floordivpow2(trx0, res->pdx);
  433. prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y,
  434. (comp->dy << levelno)), res->pdy)
  435. - opj_uint_floordivpow2(try0, res->pdy);
  436. pi->precno = prci + prcj * res->pw;
  437. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  438. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
  439. pi->step_c + pi->precno * pi->step_p;
  440. if (index >= pi->include_size) {
  441. opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
  442. return OPJ_FALSE;
  443. }
  444. if (!pi->include[index]) {
  445. pi->include[index] = 1;
  446. return OPJ_TRUE;
  447. }
  448. LABEL_SKIP:
  449. ;
  450. }
  451. }
  452. }
  453. }
  454. }
  455. return OPJ_FALSE;
  456. }
  457. static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
  458. {
  459. opj_pi_comp_t *comp = NULL;
  460. opj_pi_resolution_t *res = NULL;
  461. OPJ_UINT32 index = 0;
  462. if (pi->poc.compno0 >= pi->numcomps ||
  463. pi->poc.compno1 >= pi->numcomps + 1) {
  464. opj_event_msg(pi->manager, EVT_ERROR,
  465. "opj_pi_next_pcrl(): invalid compno0/compno1\n");
  466. return OPJ_FALSE;
  467. }
  468. if (!pi->first) {
  469. comp = &pi->comps[pi->compno];
  470. goto LABEL_SKIP;
  471. } else {
  472. OPJ_UINT32 compno, resno;
  473. pi->first = 0;
  474. pi->dx = 0;
  475. pi->dy = 0;
  476. for (compno = 0; compno < pi->numcomps; compno++) {
  477. comp = &pi->comps[compno];
  478. for (resno = 0; resno < comp->numresolutions; resno++) {
  479. OPJ_UINT32 dx, dy;
  480. res = &comp->resolutions[resno];
  481. if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
  482. comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
  483. dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
  484. pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
  485. }
  486. if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
  487. comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
  488. dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
  489. pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
  490. }
  491. }
  492. }
  493. if (pi->dx == 0 || pi->dy == 0) {
  494. return OPJ_FALSE;
  495. }
  496. }
  497. if (!pi->tp_on) {
  498. pi->poc.ty0 = pi->ty0;
  499. pi->poc.tx0 = pi->tx0;
  500. pi->poc.ty1 = pi->ty1;
  501. pi->poc.tx1 = pi->tx1;
  502. }
  503. for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
  504. pi->y += (pi->dy - (pi->y % pi->dy))) {
  505. for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
  506. pi->x += (pi->dx - (pi->x % pi->dx))) {
  507. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  508. comp = &pi->comps[pi->compno];
  509. for (pi->resno = pi->poc.resno0;
  510. pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
  511. OPJ_UINT32 levelno;
  512. OPJ_UINT32 trx0, try0;
  513. OPJ_UINT32 trx1, try1;
  514. OPJ_UINT32 rpx, rpy;
  515. OPJ_UINT32 prci, prcj;
  516. res = &comp->resolutions[pi->resno];
  517. levelno = comp->numresolutions - 1 - pi->resno;
  518. /* Avoids division by zero */
  519. /* Relates to id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
  520. /* of https://github.com/uclouvain/openjpeg/issues/938 */
  521. if (levelno >= 32 ||
  522. ((comp->dx << levelno) >> levelno) != comp->dx ||
  523. ((comp->dy << levelno) >> levelno) != comp->dy) {
  524. continue;
  525. }
  526. if ((comp->dx << levelno) > INT_MAX ||
  527. (comp->dy << levelno) > INT_MAX) {
  528. continue;
  529. }
  530. trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno));
  531. try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno));
  532. trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno));
  533. try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno));
  534. rpx = res->pdx + levelno;
  535. rpy = res->pdy + levelno;
  536. /* To avoid divisions by zero / undefined behaviour on shift */
  537. /* in below tests */
  538. /* Relates to id:000019,sig:08,src:001098,op:flip1,pos:49 */
  539. /* of https://github.com/uclouvain/openjpeg/issues/938 */
  540. if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
  541. rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
  542. continue;
  543. }
  544. /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
  545. if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
  546. ((try0 << levelno) % (1U << rpy))))) {
  547. continue;
  548. }
  549. if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
  550. ((trx0 << levelno) % (1U << rpx))))) {
  551. continue;
  552. }
  553. if ((res->pw == 0) || (res->ph == 0)) {
  554. continue;
  555. }
  556. if ((trx0 == trx1) || (try0 == try1)) {
  557. continue;
  558. }
  559. prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x,
  560. (comp->dx << levelno)), res->pdx)
  561. - opj_uint_floordivpow2(trx0, res->pdx);
  562. prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y,
  563. (comp->dy << levelno)), res->pdy)
  564. - opj_uint_floordivpow2(try0, res->pdy);
  565. pi->precno = prci + prcj * res->pw;
  566. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  567. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
  568. pi->step_c + pi->precno * pi->step_p;
  569. if (index >= pi->include_size) {
  570. opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
  571. return OPJ_FALSE;
  572. }
  573. if (!pi->include[index]) {
  574. pi->include[index] = 1;
  575. return OPJ_TRUE;
  576. }
  577. LABEL_SKIP:
  578. ;
  579. }
  580. }
  581. }
  582. }
  583. }
  584. return OPJ_FALSE;
  585. }
  586. static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
  587. {
  588. opj_pi_comp_t *comp = NULL;
  589. opj_pi_resolution_t *res = NULL;
  590. OPJ_UINT32 index = 0;
  591. if (pi->poc.compno0 >= pi->numcomps ||
  592. pi->poc.compno1 >= pi->numcomps + 1) {
  593. opj_event_msg(pi->manager, EVT_ERROR,
  594. "opj_pi_next_cprl(): invalid compno0/compno1\n");
  595. return OPJ_FALSE;
  596. }
  597. if (!pi->first) {
  598. comp = &pi->comps[pi->compno];
  599. goto LABEL_SKIP;
  600. } else {
  601. pi->first = 0;
  602. }
  603. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  604. OPJ_UINT32 resno;
  605. comp = &pi->comps[pi->compno];
  606. pi->dx = 0;
  607. pi->dy = 0;
  608. for (resno = 0; resno < comp->numresolutions; resno++) {
  609. OPJ_UINT32 dx, dy;
  610. res = &comp->resolutions[resno];
  611. if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
  612. comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
  613. dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
  614. pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
  615. }
  616. if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
  617. comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
  618. dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
  619. pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
  620. }
  621. }
  622. if (pi->dx == 0 || pi->dy == 0) {
  623. return OPJ_FALSE;
  624. }
  625. if (!pi->tp_on) {
  626. pi->poc.ty0 = pi->ty0;
  627. pi->poc.tx0 = pi->tx0;
  628. pi->poc.ty1 = pi->ty1;
  629. pi->poc.tx1 = pi->tx1;
  630. }
  631. for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
  632. pi->y += (pi->dy - (pi->y % pi->dy))) {
  633. for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
  634. pi->x += (pi->dx - (pi->x % pi->dx))) {
  635. for (pi->resno = pi->poc.resno0;
  636. pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
  637. OPJ_UINT32 levelno;
  638. OPJ_UINT32 trx0, try0;
  639. OPJ_UINT32 trx1, try1;
  640. OPJ_UINT32 rpx, rpy;
  641. OPJ_UINT32 prci, prcj;
  642. res = &comp->resolutions[pi->resno];
  643. levelno = comp->numresolutions - 1 - pi->resno;
  644. /* Avoids division by zero on id_000004,sig_06,src_000679,op_arith8,pos_49,val_-17 */
  645. /* of https://github.com/uclouvain/openjpeg/issues/938 */
  646. if (levelno >= 32 ||
  647. ((comp->dx << levelno) >> levelno) != comp->dx ||
  648. ((comp->dy << levelno) >> levelno) != comp->dy) {
  649. continue;
  650. }
  651. if ((comp->dx << levelno) > INT_MAX ||
  652. (comp->dy << levelno) > INT_MAX) {
  653. continue;
  654. }
  655. trx0 = opj_uint_ceildiv(pi->tx0, (comp->dx << levelno));
  656. try0 = opj_uint_ceildiv(pi->ty0, (comp->dy << levelno));
  657. trx1 = opj_uint_ceildiv(pi->tx1, (comp->dx << levelno));
  658. try1 = opj_uint_ceildiv(pi->ty1, (comp->dy << levelno));
  659. rpx = res->pdx + levelno;
  660. rpy = res->pdy + levelno;
  661. /* To avoid divisions by zero / undefined behaviour on shift */
  662. /* in below tests */
  663. /* Fixes reading id:000019,sig:08,src:001098,op:flip1,pos:49 */
  664. /* of https://github.com/uclouvain/openjpeg/issues/938 */
  665. if (rpx >= 31 || ((comp->dx << rpx) >> rpx) != comp->dx ||
  666. rpy >= 31 || ((comp->dy << rpy) >> rpy) != comp->dy) {
  667. continue;
  668. }
  669. /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
  670. if (!((pi->y % (comp->dy << rpy) == 0) || ((pi->y == pi->ty0) &&
  671. ((try0 << levelno) % (1U << rpy))))) {
  672. continue;
  673. }
  674. if (!((pi->x % (comp->dx << rpx) == 0) || ((pi->x == pi->tx0) &&
  675. ((trx0 << levelno) % (1U << rpx))))) {
  676. continue;
  677. }
  678. if ((res->pw == 0) || (res->ph == 0)) {
  679. continue;
  680. }
  681. if ((trx0 == trx1) || (try0 == try1)) {
  682. continue;
  683. }
  684. prci = opj_uint_floordivpow2(opj_uint_ceildiv(pi->x,
  685. (comp->dx << levelno)), res->pdx)
  686. - opj_uint_floordivpow2(trx0, res->pdx);
  687. prcj = opj_uint_floordivpow2(opj_uint_ceildiv(pi->y,
  688. (comp->dy << levelno)), res->pdy)
  689. - opj_uint_floordivpow2(try0, res->pdy);
  690. pi->precno = (OPJ_UINT32)(prci + prcj * res->pw);
  691. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  692. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
  693. pi->step_c + pi->precno * pi->step_p;
  694. if (index >= pi->include_size) {
  695. opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
  696. return OPJ_FALSE;
  697. }
  698. if (!pi->include[index]) {
  699. pi->include[index] = 1;
  700. return OPJ_TRUE;
  701. }
  702. LABEL_SKIP:
  703. ;
  704. }
  705. }
  706. }
  707. }
  708. }
  709. return OPJ_FALSE;
  710. }
  711. static void opj_get_encoding_parameters(const opj_image_t *p_image,
  712. const opj_cp_t *p_cp,
  713. OPJ_UINT32 p_tileno,
  714. OPJ_UINT32 * p_tx0,
  715. OPJ_UINT32 * p_tx1,
  716. OPJ_UINT32 * p_ty0,
  717. OPJ_UINT32 * p_ty1,
  718. OPJ_UINT32 * p_dx_min,
  719. OPJ_UINT32 * p_dy_min,
  720. OPJ_UINT32 * p_max_prec,
  721. OPJ_UINT32 * p_max_res)
  722. {
  723. /* loop */
  724. OPJ_UINT32 compno, resno;
  725. /* pointers */
  726. const opj_tcp_t *l_tcp = 00;
  727. const opj_tccp_t * l_tccp = 00;
  728. const opj_image_comp_t * l_img_comp = 00;
  729. /* position in x and y of tile */
  730. OPJ_UINT32 p, q;
  731. /* non-corrected (in regard to image offset) tile offset */
  732. OPJ_UINT32 l_tx0, l_ty0;
  733. /* preconditions */
  734. assert(p_cp != 00);
  735. assert(p_image != 00);
  736. assert(p_tileno < p_cp->tw * p_cp->th);
  737. /* initializations */
  738. l_tcp = &p_cp->tcps [p_tileno];
  739. l_img_comp = p_image->comps;
  740. l_tccp = l_tcp->tccps;
  741. /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
  742. p = p_tileno % p_cp->tw;
  743. q = p_tileno / p_cp->tw;
  744. /* find extent of tile */
  745. l_tx0 = p_cp->tx0 + p *
  746. p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
  747. *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
  748. *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
  749. l_ty0 = p_cp->ty0 + q *
  750. p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
  751. *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
  752. *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
  753. /* max precision is 0 (can only grow) */
  754. *p_max_prec = 0;
  755. *p_max_res = 0;
  756. /* take the largest value for dx_min and dy_min */
  757. *p_dx_min = 0x7fffffff;
  758. *p_dy_min = 0x7fffffff;
  759. for (compno = 0; compno < p_image->numcomps; ++compno) {
  760. /* arithmetic variables to calculate */
  761. OPJ_UINT32 l_level_no;
  762. OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
  763. OPJ_UINT32 l_px0, l_py0, l_px1, py1;
  764. OPJ_UINT32 l_pdx, l_pdy;
  765. OPJ_UINT32 l_pw, l_ph;
  766. OPJ_UINT32 l_product;
  767. OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
  768. l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
  769. l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
  770. l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
  771. l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
  772. if (l_tccp->numresolutions > *p_max_res) {
  773. *p_max_res = l_tccp->numresolutions;
  774. }
  775. /* use custom size for precincts */
  776. for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
  777. OPJ_UINT32 l_dx, l_dy;
  778. /* precinct width and height */
  779. l_pdx = l_tccp->prcw[resno];
  780. l_pdy = l_tccp->prch[resno];
  781. l_dx = l_img_comp->dx * (1u << (l_pdx + l_tccp->numresolutions - 1 - resno));
  782. l_dy = l_img_comp->dy * (1u << (l_pdy + l_tccp->numresolutions - 1 - resno));
  783. /* take the minimum size for dx for each comp and resolution */
  784. *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
  785. *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
  786. /* various calculations of extents */
  787. l_level_no = l_tccp->numresolutions - 1 - resno;
  788. l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
  789. l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
  790. l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
  791. l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
  792. l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
  793. l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
  794. l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
  795. py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
  796. l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
  797. l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
  798. l_product = l_pw * l_ph;
  799. /* update precision */
  800. if (l_product > *p_max_prec) {
  801. *p_max_prec = l_product;
  802. }
  803. }
  804. ++l_img_comp;
  805. ++l_tccp;
  806. }
  807. }
  808. static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
  809. const opj_cp_t *p_cp,
  810. OPJ_UINT32 tileno,
  811. OPJ_UINT32 * p_tx0,
  812. OPJ_UINT32 * p_tx1,
  813. OPJ_UINT32 * p_ty0,
  814. OPJ_UINT32 * p_ty1,
  815. OPJ_UINT32 * p_dx_min,
  816. OPJ_UINT32 * p_dy_min,
  817. OPJ_UINT32 * p_max_prec,
  818. OPJ_UINT32 * p_max_res,
  819. OPJ_UINT32 ** p_resolutions)
  820. {
  821. /* loop*/
  822. OPJ_UINT32 compno, resno;
  823. /* pointers*/
  824. const opj_tcp_t *tcp = 00;
  825. const opj_tccp_t * l_tccp = 00;
  826. const opj_image_comp_t * l_img_comp = 00;
  827. /* to store l_dx, l_dy, w and h for each resolution and component.*/
  828. OPJ_UINT32 * lResolutionPtr;
  829. /* position in x and y of tile*/
  830. OPJ_UINT32 p, q;
  831. /* non-corrected (in regard to image offset) tile offset */
  832. OPJ_UINT32 l_tx0, l_ty0;
  833. /* preconditions in debug*/
  834. assert(p_cp != 00);
  835. assert(p_image != 00);
  836. assert(tileno < p_cp->tw * p_cp->th);
  837. /* initializations*/
  838. tcp = &p_cp->tcps [tileno];
  839. l_tccp = tcp->tccps;
  840. l_img_comp = p_image->comps;
  841. /* position in x and y of tile*/
  842. p = tileno % p_cp->tw;
  843. q = tileno / p_cp->tw;
  844. /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
  845. l_tx0 = p_cp->tx0 + p *
  846. p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
  847. *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
  848. *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
  849. l_ty0 = p_cp->ty0 + q *
  850. p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
  851. *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
  852. *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
  853. /* max precision and resolution is 0 (can only grow)*/
  854. *p_max_prec = 0;
  855. *p_max_res = 0;
  856. /* take the largest value for dx_min and dy_min*/
  857. *p_dx_min = 0x7fffffff;
  858. *p_dy_min = 0x7fffffff;
  859. for (compno = 0; compno < p_image->numcomps; ++compno) {
  860. /* arithmetic variables to calculate*/
  861. OPJ_UINT32 l_level_no;
  862. OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
  863. OPJ_UINT32 l_px0, l_py0, l_px1, py1;
  864. OPJ_UINT32 l_product;
  865. OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
  866. OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
  867. lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL;
  868. l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
  869. l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
  870. l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
  871. l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
  872. if (l_tccp->numresolutions > *p_max_res) {
  873. *p_max_res = l_tccp->numresolutions;
  874. }
  875. /* use custom size for precincts*/
  876. l_level_no = l_tccp->numresolutions;
  877. for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
  878. OPJ_UINT32 l_dx, l_dy;
  879. --l_level_no;
  880. /* precinct width and height*/
  881. l_pdx = l_tccp->prcw[resno];
  882. l_pdy = l_tccp->prch[resno];
  883. if (lResolutionPtr) {
  884. *lResolutionPtr++ = l_pdx;
  885. *lResolutionPtr++ = l_pdy;
  886. }
  887. if (l_pdx + l_level_no < 32 &&
  888. l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
  889. l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
  890. /* take the minimum size for l_dx for each comp and resolution*/
  891. *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
  892. }
  893. if (l_pdy + l_level_no < 32 &&
  894. l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
  895. l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
  896. *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
  897. }
  898. /* various calculations of extents*/
  899. l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
  900. l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
  901. l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
  902. l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
  903. l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
  904. l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
  905. l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
  906. py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
  907. l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
  908. l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
  909. if (lResolutionPtr) {
  910. *lResolutionPtr++ = l_pw;
  911. *lResolutionPtr++ = l_ph;
  912. }
  913. l_product = l_pw * l_ph;
  914. /* update precision*/
  915. if (l_product > *p_max_prec) {
  916. *p_max_prec = l_product;
  917. }
  918. }
  919. ++l_tccp;
  920. ++l_img_comp;
  921. }
  922. }
  923. static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
  924. const opj_cp_t *cp,
  925. OPJ_UINT32 tileno,
  926. opj_event_mgr_t* manager)
  927. {
  928. /* loop*/
  929. OPJ_UINT32 pino, compno;
  930. /* number of poc in the p_pi*/
  931. OPJ_UINT32 l_poc_bound;
  932. /* pointers to tile coding parameters and components.*/
  933. opj_pi_iterator_t *l_pi = 00;
  934. opj_tcp_t *tcp = 00;
  935. const opj_tccp_t *tccp = 00;
  936. /* current packet iterator being allocated*/
  937. opj_pi_iterator_t *l_current_pi = 00;
  938. /* preconditions in debug*/
  939. assert(cp != 00);
  940. assert(image != 00);
  941. assert(tileno < cp->tw * cp->th);
  942. /* initializations*/
  943. tcp = &cp->tcps[tileno];
  944. l_poc_bound = tcp->numpocs + 1;
  945. /* memory allocations*/
  946. l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
  947. sizeof(opj_pi_iterator_t));
  948. if (!l_pi) {
  949. return NULL;
  950. }
  951. l_current_pi = l_pi;
  952. for (pino = 0; pino < l_poc_bound ; ++pino) {
  953. l_current_pi->manager = manager;
  954. l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
  955. sizeof(opj_pi_comp_t));
  956. if (! l_current_pi->comps) {
  957. opj_pi_destroy(l_pi, l_poc_bound);
  958. return NULL;
  959. }
  960. l_current_pi->numcomps = image->numcomps;
  961. for (compno = 0; compno < image->numcomps; ++compno) {
  962. opj_pi_comp_t *comp = &l_current_pi->comps[compno];
  963. tccp = &tcp->tccps[compno];
  964. comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
  965. sizeof(opj_pi_resolution_t));
  966. if (!comp->resolutions) {
  967. opj_pi_destroy(l_pi, l_poc_bound);
  968. return 00;
  969. }
  970. comp->numresolutions = tccp->numresolutions;
  971. }
  972. ++l_current_pi;
  973. }
  974. return l_pi;
  975. }
  976. static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
  977. OPJ_UINT32 p_tileno,
  978. OPJ_UINT32 p_tx0,
  979. OPJ_UINT32 p_tx1,
  980. OPJ_UINT32 p_ty0,
  981. OPJ_UINT32 p_ty1,
  982. OPJ_UINT32 p_max_prec,
  983. OPJ_UINT32 p_max_res,
  984. OPJ_UINT32 p_dx_min,
  985. OPJ_UINT32 p_dy_min)
  986. {
  987. /* loop*/
  988. OPJ_UINT32 pino;
  989. /* tile coding parameter*/
  990. opj_tcp_t *l_tcp = 00;
  991. /* current poc being updated*/
  992. opj_poc_t * l_current_poc = 00;
  993. /* number of pocs*/
  994. OPJ_UINT32 l_poc_bound;
  995. OPJ_ARG_NOT_USED(p_max_res);
  996. /* preconditions in debug*/
  997. assert(p_cp != 00);
  998. assert(p_tileno < p_cp->tw * p_cp->th);
  999. /* initializations*/
  1000. l_tcp = &p_cp->tcps [p_tileno];
  1001. /* number of iterations in the loop */
  1002. l_poc_bound = l_tcp->numpocs + 1;
  1003. /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
  1004. store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
  1005. l_current_poc = l_tcp->pocs;
  1006. l_current_poc->compS = l_current_poc->compno0;
  1007. l_current_poc->compE = l_current_poc->compno1;
  1008. l_current_poc->resS = l_current_poc->resno0;
  1009. l_current_poc->resE = l_current_poc->resno1;
  1010. l_current_poc->layE = l_current_poc->layno1;
  1011. /* special treatment for the first element*/
  1012. l_current_poc->layS = 0;
  1013. l_current_poc->prg = l_current_poc->prg1;
  1014. l_current_poc->prcS = 0;
  1015. l_current_poc->prcE = p_max_prec;
  1016. l_current_poc->txS = (OPJ_UINT32)p_tx0;
  1017. l_current_poc->txE = (OPJ_UINT32)p_tx1;
  1018. l_current_poc->tyS = (OPJ_UINT32)p_ty0;
  1019. l_current_poc->tyE = (OPJ_UINT32)p_ty1;
  1020. l_current_poc->dx = p_dx_min;
  1021. l_current_poc->dy = p_dy_min;
  1022. ++ l_current_poc;
  1023. for (pino = 1; pino < l_poc_bound ; ++pino) {
  1024. l_current_poc->compS = l_current_poc->compno0;
  1025. l_current_poc->compE = l_current_poc->compno1;
  1026. l_current_poc->resS = l_current_poc->resno0;
  1027. l_current_poc->resE = l_current_poc->resno1;
  1028. l_current_poc->layE = l_current_poc->layno1;
  1029. l_current_poc->prg = l_current_poc->prg1;
  1030. l_current_poc->prcS = 0;
  1031. /* special treatment here different from the first element*/
  1032. l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
  1033. l_current_poc->layE : 0;
  1034. l_current_poc->prcE = p_max_prec;
  1035. l_current_poc->txS = (OPJ_UINT32)p_tx0;
  1036. l_current_poc->txE = (OPJ_UINT32)p_tx1;
  1037. l_current_poc->tyS = (OPJ_UINT32)p_ty0;
  1038. l_current_poc->tyE = (OPJ_UINT32)p_ty1;
  1039. l_current_poc->dx = p_dx_min;
  1040. l_current_poc->dy = p_dy_min;
  1041. ++ l_current_poc;
  1042. }
  1043. }
  1044. static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
  1045. OPJ_UINT32 p_num_comps,
  1046. OPJ_UINT32 p_tileno,
  1047. OPJ_UINT32 p_tx0,
  1048. OPJ_UINT32 p_tx1,
  1049. OPJ_UINT32 p_ty0,
  1050. OPJ_UINT32 p_ty1,
  1051. OPJ_UINT32 p_max_prec,
  1052. OPJ_UINT32 p_max_res,
  1053. OPJ_UINT32 p_dx_min,
  1054. OPJ_UINT32 p_dy_min)
  1055. {
  1056. /* loop*/
  1057. OPJ_UINT32 pino;
  1058. /* tile coding parameter*/
  1059. opj_tcp_t *l_tcp = 00;
  1060. /* current poc being updated*/
  1061. opj_poc_t * l_current_poc = 00;
  1062. /* number of pocs*/
  1063. OPJ_UINT32 l_poc_bound;
  1064. /* preconditions in debug*/
  1065. assert(p_cp != 00);
  1066. assert(p_tileno < p_cp->tw * p_cp->th);
  1067. /* initializations*/
  1068. l_tcp = &p_cp->tcps [p_tileno];
  1069. /* number of iterations in the loop */
  1070. l_poc_bound = l_tcp->numpocs + 1;
  1071. /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
  1072. store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
  1073. l_current_poc = l_tcp->pocs;
  1074. for (pino = 0; pino < l_poc_bound ; ++pino) {
  1075. l_current_poc->compS = 0;
  1076. l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
  1077. l_current_poc->resS = 0;
  1078. l_current_poc->resE = p_max_res;
  1079. l_current_poc->layS = 0;
  1080. l_current_poc->layE = l_tcp->numlayers;
  1081. l_current_poc->prg = l_tcp->prg;
  1082. l_current_poc->prcS = 0;
  1083. l_current_poc->prcE = p_max_prec;
  1084. l_current_poc->txS = p_tx0;
  1085. l_current_poc->txE = p_tx1;
  1086. l_current_poc->tyS = p_ty0;
  1087. l_current_poc->tyE = p_ty1;
  1088. l_current_poc->dx = p_dx_min;
  1089. l_current_poc->dy = p_dy_min;
  1090. ++ l_current_poc;
  1091. }
  1092. }
  1093. static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
  1094. opj_tcp_t * p_tcp,
  1095. OPJ_UINT32 p_max_precision,
  1096. OPJ_UINT32 p_max_res)
  1097. {
  1098. /* loop*/
  1099. OPJ_UINT32 pino;
  1100. /* encoding parameters to set*/
  1101. OPJ_UINT32 l_bound;
  1102. opj_pi_iterator_t * l_current_pi = 00;
  1103. opj_poc_t* l_current_poc = 0;
  1104. OPJ_ARG_NOT_USED(p_max_res);
  1105. /* preconditions in debug*/
  1106. assert(p_pi != 00);
  1107. assert(p_tcp != 00);
  1108. /* initializations*/
  1109. l_bound = p_tcp->numpocs + 1;
  1110. l_current_pi = p_pi;
  1111. l_current_poc = p_tcp->pocs;
  1112. for (pino = 0; pino < l_bound; ++pino) {
  1113. l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
  1114. l_current_pi->first = 1;
  1115. l_current_pi->poc.resno0 =
  1116. l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
  1117. l_current_pi->poc.compno0 =
  1118. l_current_poc->compno0; /* Component Index #0 (Start) */
  1119. l_current_pi->poc.layno0 = 0;
  1120. l_current_pi->poc.precno0 = 0;
  1121. l_current_pi->poc.resno1 =
  1122. l_current_poc->resno1; /* Resolution Level Index #0 (End) */
  1123. l_current_pi->poc.compno1 =
  1124. l_current_poc->compno1; /* Component Index #0 (End) */
  1125. l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
  1126. p_tcp->numlayers); /* Layer Index #0 (End) */
  1127. l_current_pi->poc.precno1 = p_max_precision;
  1128. ++l_current_pi;
  1129. ++l_current_poc;
  1130. }
  1131. }
  1132. static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
  1133. opj_tcp_t * p_tcp,
  1134. OPJ_UINT32 p_max_precision,
  1135. OPJ_UINT32 p_max_res)
  1136. {
  1137. /* loop*/
  1138. OPJ_UINT32 pino;
  1139. /* encoding parameters to set*/
  1140. OPJ_UINT32 l_bound;
  1141. opj_pi_iterator_t * l_current_pi = 00;
  1142. /* preconditions in debug*/
  1143. assert(p_tcp != 00);
  1144. assert(p_pi != 00);
  1145. /* initializations*/
  1146. l_bound = p_tcp->numpocs + 1;
  1147. l_current_pi = p_pi;
  1148. for (pino = 0; pino < l_bound; ++pino) {
  1149. l_current_pi->poc.prg = p_tcp->prg;
  1150. l_current_pi->first = 1;
  1151. l_current_pi->poc.resno0 = 0;
  1152. l_current_pi->poc.compno0 = 0;
  1153. l_current_pi->poc.layno0 = 0;
  1154. l_current_pi->poc.precno0 = 0;
  1155. l_current_pi->poc.resno1 = p_max_res;
  1156. l_current_pi->poc.compno1 = l_current_pi->numcomps;
  1157. l_current_pi->poc.layno1 = p_tcp->numlayers;
  1158. l_current_pi->poc.precno1 = p_max_precision;
  1159. ++l_current_pi;
  1160. }
  1161. }
  1162. static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
  1163. opj_cp_t *cp,
  1164. OPJ_UINT32 tileno,
  1165. OPJ_UINT32 pino,
  1166. const OPJ_CHAR *prog)
  1167. {
  1168. OPJ_INT32 i;
  1169. opj_tcp_t *tcps = &cp->tcps[tileno];
  1170. opj_poc_t *tcp = &tcps->pocs[pino];
  1171. if (pos >= 0) {
  1172. for (i = pos; i >= 0; i--) {
  1173. switch (prog[i]) {
  1174. case 'R':
  1175. if (tcp->res_t == tcp->resE) {
  1176. if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
  1177. return OPJ_TRUE;
  1178. } else {
  1179. return OPJ_FALSE;
  1180. }
  1181. } else {
  1182. return OPJ_TRUE;
  1183. }
  1184. break;
  1185. case 'C':
  1186. if (tcp->comp_t == tcp->compE) {
  1187. if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
  1188. return OPJ_TRUE;
  1189. } else {
  1190. return OPJ_FALSE;
  1191. }
  1192. } else {
  1193. return OPJ_TRUE;
  1194. }
  1195. break;
  1196. case 'L':
  1197. if (tcp->lay_t == tcp->layE) {
  1198. if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
  1199. return OPJ_TRUE;
  1200. } else {
  1201. return OPJ_FALSE;
  1202. }
  1203. } else {
  1204. return OPJ_TRUE;
  1205. }
  1206. break;
  1207. case 'P':
  1208. switch (tcp->prg) {
  1209. case OPJ_LRCP: /* fall through */
  1210. case OPJ_RLCP:
  1211. if (tcp->prc_t == tcp->prcE) {
  1212. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1213. return OPJ_TRUE;
  1214. } else {
  1215. return OPJ_FALSE;
  1216. }
  1217. } else {
  1218. return OPJ_TRUE;
  1219. }
  1220. break;
  1221. default:
  1222. if (tcp->tx0_t == tcp->txE) {
  1223. /*TY*/
  1224. if (tcp->ty0_t == tcp->tyE) {
  1225. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1226. return OPJ_TRUE;
  1227. } else {
  1228. return OPJ_FALSE;
  1229. }
  1230. } else {
  1231. return OPJ_TRUE;
  1232. }/*TY*/
  1233. } else {
  1234. return OPJ_TRUE;
  1235. }
  1236. break;
  1237. }/*end case P*/
  1238. }/*end switch*/
  1239. }/*end for*/
  1240. }/*end if*/
  1241. return OPJ_FALSE;
  1242. }
  1243. /*
  1244. ==========================================================
  1245. Packet iterator interface
  1246. ==========================================================
  1247. */
  1248. opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
  1249. opj_cp_t *p_cp,
  1250. OPJ_UINT32 p_tile_no,
  1251. opj_event_mgr_t* manager)
  1252. {
  1253. OPJ_UINT32 numcomps = p_image->numcomps;
  1254. /* loop */
  1255. OPJ_UINT32 pino;
  1256. OPJ_UINT32 compno, resno;
  1257. /* to store w, h, dx and dy for all components and resolutions */
  1258. OPJ_UINT32 * l_tmp_data;
  1259. OPJ_UINT32 ** l_tmp_ptr;
  1260. /* encoding parameters to set */
  1261. OPJ_UINT32 l_max_res;
  1262. OPJ_UINT32 l_max_prec;
  1263. OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
  1264. OPJ_UINT32 l_dx_min, l_dy_min;
  1265. OPJ_UINT32 l_bound;
  1266. OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
  1267. OPJ_UINT32 l_data_stride;
  1268. /* pointers */
  1269. opj_pi_iterator_t *l_pi = 00;
  1270. opj_tcp_t *l_tcp = 00;
  1271. const opj_tccp_t *l_tccp = 00;
  1272. opj_pi_comp_t *l_current_comp = 00;
  1273. opj_image_comp_t * l_img_comp = 00;
  1274. opj_pi_iterator_t * l_current_pi = 00;
  1275. OPJ_UINT32 * l_encoding_value_ptr = 00;
  1276. /* preconditions in debug */
  1277. assert(p_cp != 00);
  1278. assert(p_image != 00);
  1279. assert(p_tile_no < p_cp->tw * p_cp->th);
  1280. /* initializations */
  1281. l_tcp = &p_cp->tcps[p_tile_no];
  1282. l_bound = l_tcp->numpocs + 1;
  1283. l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
  1284. l_tmp_data = (OPJ_UINT32*)opj_malloc(
  1285. l_data_stride * numcomps * sizeof(OPJ_UINT32));
  1286. if
  1287. (! l_tmp_data) {
  1288. return 00;
  1289. }
  1290. l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
  1291. numcomps * sizeof(OPJ_UINT32 *));
  1292. if
  1293. (! l_tmp_ptr) {
  1294. opj_free(l_tmp_data);
  1295. return 00;
  1296. }
  1297. /* memory allocation for pi */
  1298. l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
  1299. if (!l_pi) {
  1300. opj_free(l_tmp_data);
  1301. opj_free(l_tmp_ptr);
  1302. return 00;
  1303. }
  1304. l_encoding_value_ptr = l_tmp_data;
  1305. /* update pointer array */
  1306. for
  1307. (compno = 0; compno < numcomps; ++compno) {
  1308. l_tmp_ptr[compno] = l_encoding_value_ptr;
  1309. l_encoding_value_ptr += l_data_stride;
  1310. }
  1311. /* get encoding parameters */
  1312. opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
  1313. &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
  1314. /* step calculations */
  1315. l_step_p = 1;
  1316. l_step_c = l_max_prec * l_step_p;
  1317. l_step_r = numcomps * l_step_c;
  1318. l_step_l = l_max_res * l_step_r;
  1319. /* set values for first packet iterator */
  1320. l_current_pi = l_pi;
  1321. /* memory allocation for include */
  1322. /* prevent an integer overflow issue */
  1323. /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
  1324. l_current_pi->include = 00;
  1325. if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
  1326. l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
  1327. l_current_pi->include = (OPJ_INT16*) opj_calloc(
  1328. l_current_pi->include_size, sizeof(OPJ_INT16));
  1329. }
  1330. if (!l_current_pi->include) {
  1331. opj_free(l_tmp_data);
  1332. opj_free(l_tmp_ptr);
  1333. opj_pi_destroy(l_pi, l_bound);
  1334. return 00;
  1335. }
  1336. /* special treatment for the first packet iterator */
  1337. l_current_comp = l_current_pi->comps;
  1338. l_img_comp = p_image->comps;
  1339. l_tccp = l_tcp->tccps;
  1340. l_current_pi->tx0 = l_tx0;
  1341. l_current_pi->ty0 = l_ty0;
  1342. l_current_pi->tx1 = l_tx1;
  1343. l_current_pi->ty1 = l_ty1;
  1344. /*l_current_pi->dx = l_img_comp->dx;*/
  1345. /*l_current_pi->dy = l_img_comp->dy;*/
  1346. l_current_pi->step_p = l_step_p;
  1347. l_current_pi->step_c = l_step_c;
  1348. l_current_pi->step_r = l_step_r;
  1349. l_current_pi->step_l = l_step_l;
  1350. /* allocation for components and number of components has already been calculated by opj_pi_create */
  1351. for
  1352. (compno = 0; compno < numcomps; ++compno) {
  1353. opj_pi_resolution_t *l_res = l_current_comp->resolutions;
  1354. l_encoding_value_ptr = l_tmp_ptr[compno];
  1355. l_current_comp->dx = l_img_comp->dx;
  1356. l_current_comp->dy = l_img_comp->dy;
  1357. /* resolutions have already been initialized */
  1358. for
  1359. (resno = 0; resno < l_current_comp->numresolutions; resno++) {
  1360. l_res->pdx = *(l_encoding_value_ptr++);
  1361. l_res->pdy = *(l_encoding_value_ptr++);
  1362. l_res->pw = *(l_encoding_value_ptr++);
  1363. l_res->ph = *(l_encoding_value_ptr++);
  1364. ++l_res;
  1365. }
  1366. ++l_current_comp;
  1367. ++l_img_comp;
  1368. ++l_tccp;
  1369. }
  1370. ++l_current_pi;
  1371. for (pino = 1 ; pino < l_bound ; ++pino) {
  1372. l_current_comp = l_current_pi->comps;
  1373. l_img_comp = p_image->comps;
  1374. l_tccp = l_tcp->tccps;
  1375. l_current_pi->tx0 = l_tx0;
  1376. l_current_pi->ty0 = l_ty0;
  1377. l_current_pi->tx1 = l_tx1;
  1378. l_current_pi->ty1 = l_ty1;
  1379. /*l_current_pi->dx = l_dx_min;*/
  1380. /*l_current_pi->dy = l_dy_min;*/
  1381. l_current_pi->step_p = l_step_p;
  1382. l_current_pi->step_c = l_step_c;
  1383. l_current_pi->step_r = l_step_r;
  1384. l_current_pi->step_l = l_step_l;
  1385. /* allocation for components and number of components has already been calculated by opj_pi_create */
  1386. for
  1387. (compno = 0; compno < numcomps; ++compno) {
  1388. opj_pi_resolution_t *l_res = l_current_comp->resolutions;
  1389. l_encoding_value_ptr = l_tmp_ptr[compno];
  1390. l_current_comp->dx = l_img_comp->dx;
  1391. l_current_comp->dy = l_img_comp->dy;
  1392. /* resolutions have already been initialized */
  1393. for
  1394. (resno = 0; resno < l_current_comp->numresolutions; resno++) {
  1395. l_res->pdx = *(l_encoding_value_ptr++);
  1396. l_res->pdy = *(l_encoding_value_ptr++);
  1397. l_res->pw = *(l_encoding_value_ptr++);
  1398. l_res->ph = *(l_encoding_value_ptr++);
  1399. ++l_res;
  1400. }
  1401. ++l_current_comp;
  1402. ++l_img_comp;
  1403. ++l_tccp;
  1404. }
  1405. /* special treatment*/
  1406. l_current_pi->include = (l_current_pi - 1)->include;
  1407. l_current_pi->include_size = (l_current_pi - 1)->include_size;
  1408. ++l_current_pi;
  1409. }
  1410. opj_free(l_tmp_data);
  1411. l_tmp_data = 00;
  1412. opj_free(l_tmp_ptr);
  1413. l_tmp_ptr = 00;
  1414. if
  1415. (l_tcp->POC) {
  1416. opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
  1417. } else {
  1418. opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
  1419. }
  1420. return l_pi;
  1421. }
  1422. OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image,
  1423. const opj_cp_t *p_cp,
  1424. OPJ_UINT32 p_tile_no)
  1425. {
  1426. OPJ_UINT32 l_max_res;
  1427. OPJ_UINT32 l_max_prec;
  1428. OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
  1429. OPJ_UINT32 l_dx_min, l_dy_min;
  1430. /* preconditions in debug*/
  1431. assert(p_cp != 00);
  1432. assert(p_image != 00);
  1433. assert(p_tile_no < p_cp->tw * p_cp->th);
  1434. /* get encoding parameters*/
  1435. opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
  1436. &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, NULL);
  1437. return p_cp->tcps[p_tile_no].numlayers * l_max_prec * p_image->numcomps *
  1438. l_max_res;
  1439. }
  1440. opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
  1441. opj_cp_t *p_cp,
  1442. OPJ_UINT32 p_tile_no,
  1443. J2K_T2_MODE p_t2_mode,
  1444. opj_event_mgr_t* manager)
  1445. {
  1446. OPJ_UINT32 numcomps = p_image->numcomps;
  1447. /* loop*/
  1448. OPJ_UINT32 pino;
  1449. OPJ_UINT32 compno, resno;
  1450. /* to store w, h, dx and dy for all components and resolutions*/
  1451. OPJ_UINT32 * l_tmp_data;
  1452. OPJ_UINT32 ** l_tmp_ptr;
  1453. /* encoding parameters to set*/
  1454. OPJ_UINT32 l_max_res;
  1455. OPJ_UINT32 l_max_prec;
  1456. OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
  1457. OPJ_UINT32 l_dx_min, l_dy_min;
  1458. OPJ_UINT32 l_bound;
  1459. OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
  1460. OPJ_UINT32 l_data_stride;
  1461. /* pointers*/
  1462. opj_pi_iterator_t *l_pi = 00;
  1463. opj_tcp_t *l_tcp = 00;
  1464. const opj_tccp_t *l_tccp = 00;
  1465. opj_pi_comp_t *l_current_comp = 00;
  1466. opj_image_comp_t * l_img_comp = 00;
  1467. opj_pi_iterator_t * l_current_pi = 00;
  1468. OPJ_UINT32 * l_encoding_value_ptr = 00;
  1469. /* preconditions in debug*/
  1470. assert(p_cp != 00);
  1471. assert(p_image != 00);
  1472. assert(p_tile_no < p_cp->tw * p_cp->th);
  1473. /* initializations*/
  1474. l_tcp = &p_cp->tcps[p_tile_no];
  1475. l_bound = l_tcp->numpocs + 1;
  1476. l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
  1477. l_tmp_data = (OPJ_UINT32*)opj_malloc(
  1478. l_data_stride * numcomps * sizeof(OPJ_UINT32));
  1479. if (! l_tmp_data) {
  1480. return 00;
  1481. }
  1482. l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
  1483. numcomps * sizeof(OPJ_UINT32 *));
  1484. if (! l_tmp_ptr) {
  1485. opj_free(l_tmp_data);
  1486. return 00;
  1487. }
  1488. /* memory allocation for pi*/
  1489. l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
  1490. if (!l_pi) {
  1491. opj_free(l_tmp_data);
  1492. opj_free(l_tmp_ptr);
  1493. return 00;
  1494. }
  1495. l_encoding_value_ptr = l_tmp_data;
  1496. /* update pointer array*/
  1497. for (compno = 0; compno < numcomps; ++compno) {
  1498. l_tmp_ptr[compno] = l_encoding_value_ptr;
  1499. l_encoding_value_ptr += l_data_stride;
  1500. }
  1501. /* get encoding parameters*/
  1502. opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
  1503. &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
  1504. /* step calculations*/
  1505. l_step_p = 1;
  1506. l_step_c = l_max_prec * l_step_p;
  1507. l_step_r = numcomps * l_step_c;
  1508. l_step_l = l_max_res * l_step_r;
  1509. /* set values for first packet iterator*/
  1510. l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
  1511. l_current_pi = l_pi;
  1512. /* memory allocation for include*/
  1513. l_current_pi->include_size = l_tcp->numlayers * l_step_l;
  1514. l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
  1515. sizeof(OPJ_INT16));
  1516. if (!l_current_pi->include) {
  1517. opj_free(l_tmp_data);
  1518. opj_free(l_tmp_ptr);
  1519. opj_pi_destroy(l_pi, l_bound);
  1520. return 00;
  1521. }
  1522. /* special treatment for the first packet iterator*/
  1523. l_current_comp = l_current_pi->comps;
  1524. l_img_comp = p_image->comps;
  1525. l_tccp = l_tcp->tccps;
  1526. l_current_pi->tx0 = l_tx0;
  1527. l_current_pi->ty0 = l_ty0;
  1528. l_current_pi->tx1 = l_tx1;
  1529. l_current_pi->ty1 = l_ty1;
  1530. l_current_pi->dx = l_dx_min;
  1531. l_current_pi->dy = l_dy_min;
  1532. l_current_pi->step_p = l_step_p;
  1533. l_current_pi->step_c = l_step_c;
  1534. l_current_pi->step_r = l_step_r;
  1535. l_current_pi->step_l = l_step_l;
  1536. /* allocation for components and number of components has already been calculated by opj_pi_create */
  1537. for (compno = 0; compno < numcomps; ++compno) {
  1538. opj_pi_resolution_t *l_res = l_current_comp->resolutions;
  1539. l_encoding_value_ptr = l_tmp_ptr[compno];
  1540. l_current_comp->dx = l_img_comp->dx;
  1541. l_current_comp->dy = l_img_comp->dy;
  1542. /* resolutions have already been initialized */
  1543. for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
  1544. l_res->pdx = *(l_encoding_value_ptr++);
  1545. l_res->pdy = *(l_encoding_value_ptr++);
  1546. l_res->pw = *(l_encoding_value_ptr++);
  1547. l_res->ph = *(l_encoding_value_ptr++);
  1548. ++l_res;
  1549. }
  1550. ++l_current_comp;
  1551. ++l_img_comp;
  1552. ++l_tccp;
  1553. }
  1554. ++l_current_pi;
  1555. for (pino = 1 ; pino < l_bound ; ++pino) {
  1556. l_current_comp = l_current_pi->comps;
  1557. l_img_comp = p_image->comps;
  1558. l_tccp = l_tcp->tccps;
  1559. l_current_pi->tx0 = l_tx0;
  1560. l_current_pi->ty0 = l_ty0;
  1561. l_current_pi->tx1 = l_tx1;
  1562. l_current_pi->ty1 = l_ty1;
  1563. l_current_pi->dx = l_dx_min;
  1564. l_current_pi->dy = l_dy_min;
  1565. l_current_pi->step_p = l_step_p;
  1566. l_current_pi->step_c = l_step_c;
  1567. l_current_pi->step_r = l_step_r;
  1568. l_current_pi->step_l = l_step_l;
  1569. /* allocation for components and number of components has already been calculated by opj_pi_create */
  1570. for (compno = 0; compno < numcomps; ++compno) {
  1571. opj_pi_resolution_t *l_res = l_current_comp->resolutions;
  1572. l_encoding_value_ptr = l_tmp_ptr[compno];
  1573. l_current_comp->dx = l_img_comp->dx;
  1574. l_current_comp->dy = l_img_comp->dy;
  1575. /* resolutions have already been initialized */
  1576. for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
  1577. l_res->pdx = *(l_encoding_value_ptr++);
  1578. l_res->pdy = *(l_encoding_value_ptr++);
  1579. l_res->pw = *(l_encoding_value_ptr++);
  1580. l_res->ph = *(l_encoding_value_ptr++);
  1581. ++l_res;
  1582. }
  1583. ++l_current_comp;
  1584. ++l_img_comp;
  1585. ++l_tccp;
  1586. }
  1587. /* special treatment*/
  1588. l_current_pi->include = (l_current_pi - 1)->include;
  1589. l_current_pi->include_size = (l_current_pi - 1)->include_size;
  1590. ++l_current_pi;
  1591. }
  1592. opj_free(l_tmp_data);
  1593. l_tmp_data = 00;
  1594. opj_free(l_tmp_ptr);
  1595. l_tmp_ptr = 00;
  1596. if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
  1597. opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
  1598. l_max_prec, l_max_res, l_dx_min, l_dy_min);
  1599. } else {
  1600. opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
  1601. l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
  1602. }
  1603. return l_pi;
  1604. }
  1605. void opj_pi_create_encode(opj_pi_iterator_t *pi,
  1606. opj_cp_t *cp,
  1607. OPJ_UINT32 tileno,
  1608. OPJ_UINT32 pino,
  1609. OPJ_UINT32 tpnum,
  1610. OPJ_INT32 tppos,
  1611. J2K_T2_MODE t2_mode)
  1612. {
  1613. const OPJ_CHAR *prog;
  1614. OPJ_INT32 i;
  1615. OPJ_UINT32 incr_top = 1, resetX = 0;
  1616. opj_tcp_t *tcps = &cp->tcps[tileno];
  1617. opj_poc_t *tcp = &tcps->pocs[pino];
  1618. prog = opj_j2k_convert_progression_order(tcp->prg);
  1619. pi[pino].first = 1;
  1620. pi[pino].poc.prg = tcp->prg;
  1621. if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
  1622. !OPJ_IS_IMF(cp->rsiz) &&
  1623. (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) {
  1624. pi[pino].poc.resno0 = tcp->resS;
  1625. pi[pino].poc.resno1 = tcp->resE;
  1626. pi[pino].poc.compno0 = tcp->compS;
  1627. pi[pino].poc.compno1 = tcp->compE;
  1628. pi[pino].poc.layno0 = tcp->layS;
  1629. pi[pino].poc.layno1 = tcp->layE;
  1630. pi[pino].poc.precno0 = tcp->prcS;
  1631. pi[pino].poc.precno1 = tcp->prcE;
  1632. pi[pino].poc.tx0 = tcp->txS;
  1633. pi[pino].poc.ty0 = tcp->tyS;
  1634. pi[pino].poc.tx1 = tcp->txE;
  1635. pi[pino].poc.ty1 = tcp->tyE;
  1636. } else {
  1637. for (i = tppos + 1; i < 4; i++) {
  1638. switch (prog[i]) {
  1639. case 'R':
  1640. pi[pino].poc.resno0 = tcp->resS;
  1641. pi[pino].poc.resno1 = tcp->resE;
  1642. break;
  1643. case 'C':
  1644. pi[pino].poc.compno0 = tcp->compS;
  1645. pi[pino].poc.compno1 = tcp->compE;
  1646. break;
  1647. case 'L':
  1648. pi[pino].poc.layno0 = tcp->layS;
  1649. pi[pino].poc.layno1 = tcp->layE;
  1650. break;
  1651. case 'P':
  1652. switch (tcp->prg) {
  1653. case OPJ_LRCP:
  1654. case OPJ_RLCP:
  1655. pi[pino].poc.precno0 = tcp->prcS;
  1656. pi[pino].poc.precno1 = tcp->prcE;
  1657. break;
  1658. default:
  1659. pi[pino].poc.tx0 = tcp->txS;
  1660. pi[pino].poc.ty0 = tcp->tyS;
  1661. pi[pino].poc.tx1 = tcp->txE;
  1662. pi[pino].poc.ty1 = tcp->tyE;
  1663. break;
  1664. }
  1665. break;
  1666. }
  1667. }
  1668. if (tpnum == 0) {
  1669. for (i = tppos; i >= 0; i--) {
  1670. switch (prog[i]) {
  1671. case 'C':
  1672. tcp->comp_t = tcp->compS;
  1673. pi[pino].poc.compno0 = tcp->comp_t;
  1674. pi[pino].poc.compno1 = tcp->comp_t + 1;
  1675. tcp->comp_t += 1;
  1676. break;
  1677. case 'R':
  1678. tcp->res_t = tcp->resS;
  1679. pi[pino].poc.resno0 = tcp->res_t;
  1680. pi[pino].poc.resno1 = tcp->res_t + 1;
  1681. tcp->res_t += 1;
  1682. break;
  1683. case 'L':
  1684. tcp->lay_t = tcp->layS;
  1685. pi[pino].poc.layno0 = tcp->lay_t;
  1686. pi[pino].poc.layno1 = tcp->lay_t + 1;
  1687. tcp->lay_t += 1;
  1688. break;
  1689. case 'P':
  1690. switch (tcp->prg) {
  1691. case OPJ_LRCP:
  1692. case OPJ_RLCP:
  1693. tcp->prc_t = tcp->prcS;
  1694. pi[pino].poc.precno0 = tcp->prc_t;
  1695. pi[pino].poc.precno1 = tcp->prc_t + 1;
  1696. tcp->prc_t += 1;
  1697. break;
  1698. default:
  1699. tcp->tx0_t = tcp->txS;
  1700. tcp->ty0_t = tcp->tyS;
  1701. pi[pino].poc.tx0 = tcp->tx0_t;
  1702. pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
  1703. pi[pino].poc.ty0 = tcp->ty0_t;
  1704. pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
  1705. tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
  1706. tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
  1707. break;
  1708. }
  1709. break;
  1710. }
  1711. }
  1712. incr_top = 1;
  1713. } else {
  1714. for (i = tppos; i >= 0; i--) {
  1715. switch (prog[i]) {
  1716. case 'C':
  1717. pi[pino].poc.compno0 = tcp->comp_t - 1;
  1718. pi[pino].poc.compno1 = tcp->comp_t;
  1719. break;
  1720. case 'R':
  1721. pi[pino].poc.resno0 = tcp->res_t - 1;
  1722. pi[pino].poc.resno1 = tcp->res_t;
  1723. break;
  1724. case 'L':
  1725. pi[pino].poc.layno0 = tcp->lay_t - 1;
  1726. pi[pino].poc.layno1 = tcp->lay_t;
  1727. break;
  1728. case 'P':
  1729. switch (tcp->prg) {
  1730. case OPJ_LRCP:
  1731. case OPJ_RLCP:
  1732. pi[pino].poc.precno0 = tcp->prc_t - 1;
  1733. pi[pino].poc.precno1 = tcp->prc_t;
  1734. break;
  1735. default:
  1736. pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
  1737. pi[pino].poc.tx1 = tcp->tx0_t ;
  1738. pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
  1739. pi[pino].poc.ty1 = tcp->ty0_t ;
  1740. break;
  1741. }
  1742. break;
  1743. }
  1744. if (incr_top == 1) {
  1745. switch (prog[i]) {
  1746. case 'R':
  1747. if (tcp->res_t == tcp->resE) {
  1748. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1749. tcp->res_t = tcp->resS;
  1750. pi[pino].poc.resno0 = tcp->res_t;
  1751. pi[pino].poc.resno1 = tcp->res_t + 1;
  1752. tcp->res_t += 1;
  1753. incr_top = 1;
  1754. } else {
  1755. incr_top = 0;
  1756. }
  1757. } else {
  1758. pi[pino].poc.resno0 = tcp->res_t;
  1759. pi[pino].poc.resno1 = tcp->res_t + 1;
  1760. tcp->res_t += 1;
  1761. incr_top = 0;
  1762. }
  1763. break;
  1764. case 'C':
  1765. if (tcp->comp_t == tcp->compE) {
  1766. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1767. tcp->comp_t = tcp->compS;
  1768. pi[pino].poc.compno0 = tcp->comp_t;
  1769. pi[pino].poc.compno1 = tcp->comp_t + 1;
  1770. tcp->comp_t += 1;
  1771. incr_top = 1;
  1772. } else {
  1773. incr_top = 0;
  1774. }
  1775. } else {
  1776. pi[pino].poc.compno0 = tcp->comp_t;
  1777. pi[pino].poc.compno1 = tcp->comp_t + 1;
  1778. tcp->comp_t += 1;
  1779. incr_top = 0;
  1780. }
  1781. break;
  1782. case 'L':
  1783. if (tcp->lay_t == tcp->layE) {
  1784. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1785. tcp->lay_t = tcp->layS;
  1786. pi[pino].poc.layno0 = tcp->lay_t;
  1787. pi[pino].poc.layno1 = tcp->lay_t + 1;
  1788. tcp->lay_t += 1;
  1789. incr_top = 1;
  1790. } else {
  1791. incr_top = 0;
  1792. }
  1793. } else {
  1794. pi[pino].poc.layno0 = tcp->lay_t;
  1795. pi[pino].poc.layno1 = tcp->lay_t + 1;
  1796. tcp->lay_t += 1;
  1797. incr_top = 0;
  1798. }
  1799. break;
  1800. case 'P':
  1801. switch (tcp->prg) {
  1802. case OPJ_LRCP:
  1803. case OPJ_RLCP:
  1804. if (tcp->prc_t == tcp->prcE) {
  1805. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1806. tcp->prc_t = tcp->prcS;
  1807. pi[pino].poc.precno0 = tcp->prc_t;
  1808. pi[pino].poc.precno1 = tcp->prc_t + 1;
  1809. tcp->prc_t += 1;
  1810. incr_top = 1;
  1811. } else {
  1812. incr_top = 0;
  1813. }
  1814. } else {
  1815. pi[pino].poc.precno0 = tcp->prc_t;
  1816. pi[pino].poc.precno1 = tcp->prc_t + 1;
  1817. tcp->prc_t += 1;
  1818. incr_top = 0;
  1819. }
  1820. break;
  1821. default:
  1822. if (tcp->tx0_t >= tcp->txE) {
  1823. if (tcp->ty0_t >= tcp->tyE) {
  1824. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1825. tcp->ty0_t = tcp->tyS;
  1826. pi[pino].poc.ty0 = tcp->ty0_t;
  1827. pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
  1828. tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
  1829. incr_top = 1;
  1830. resetX = 1;
  1831. } else {
  1832. incr_top = 0;
  1833. resetX = 0;
  1834. }
  1835. } else {
  1836. pi[pino].poc.ty0 = tcp->ty0_t;
  1837. pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
  1838. tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
  1839. incr_top = 0;
  1840. resetX = 1;
  1841. }
  1842. if (resetX == 1) {
  1843. tcp->tx0_t = tcp->txS;
  1844. pi[pino].poc.tx0 = tcp->tx0_t;
  1845. pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
  1846. tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
  1847. }
  1848. } else {
  1849. pi[pino].poc.tx0 = tcp->tx0_t;
  1850. pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
  1851. tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
  1852. incr_top = 0;
  1853. }
  1854. break;
  1855. }
  1856. break;
  1857. }
  1858. }
  1859. }
  1860. }
  1861. }
  1862. }
  1863. void opj_pi_destroy(opj_pi_iterator_t *p_pi,
  1864. OPJ_UINT32 p_nb_elements)
  1865. {
  1866. OPJ_UINT32 compno, pino;
  1867. opj_pi_iterator_t *l_current_pi = p_pi;
  1868. if (p_pi) {
  1869. if (p_pi->include) {
  1870. opj_free(p_pi->include);
  1871. p_pi->include = 00;
  1872. }
  1873. for (pino = 0; pino < p_nb_elements; ++pino) {
  1874. if (l_current_pi->comps) {
  1875. opj_pi_comp_t *l_current_component = l_current_pi->comps;
  1876. for (compno = 0; compno < l_current_pi->numcomps; compno++) {
  1877. if (l_current_component->resolutions) {
  1878. opj_free(l_current_component->resolutions);
  1879. l_current_component->resolutions = 00;
  1880. }
  1881. ++l_current_component;
  1882. }
  1883. opj_free(l_current_pi->comps);
  1884. l_current_pi->comps = 0;
  1885. }
  1886. ++l_current_pi;
  1887. }
  1888. opj_free(p_pi);
  1889. }
  1890. }
  1891. void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
  1892. opj_cp_t *p_cp,
  1893. OPJ_UINT32 p_tile_no)
  1894. {
  1895. /* encoding parameters to set */
  1896. OPJ_UINT32 l_max_res;
  1897. OPJ_UINT32 l_max_prec;
  1898. OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
  1899. OPJ_UINT32 l_dx_min, l_dy_min;
  1900. /* pointers */
  1901. opj_tcp_t *l_tcp = 00;
  1902. /* preconditions */
  1903. assert(p_cp != 00);
  1904. assert(p_image != 00);
  1905. assert(p_tile_no < p_cp->tw * p_cp->th);
  1906. l_tcp = &(p_cp->tcps[p_tile_no]);
  1907. /* get encoding parameters */
  1908. opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
  1909. &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
  1910. if (l_tcp->POC) {
  1911. opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
  1912. l_max_prec, l_max_res, l_dx_min, l_dy_min);
  1913. } else {
  1914. opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
  1915. l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
  1916. }
  1917. }
  1918. OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
  1919. {
  1920. switch (pi->poc.prg) {
  1921. case OPJ_LRCP:
  1922. return opj_pi_next_lrcp(pi);
  1923. case OPJ_RLCP:
  1924. return opj_pi_next_rlcp(pi);
  1925. case OPJ_RPCL:
  1926. return opj_pi_next_rpcl(pi);
  1927. case OPJ_PCRL:
  1928. return opj_pi_next_pcrl(pi);
  1929. case OPJ_CPRL:
  1930. return opj_pi_next_cprl(pi);
  1931. case OPJ_PROG_UNKNOWN:
  1932. return OPJ_FALSE;
  1933. }
  1934. return OPJ_FALSE;
  1935. }