pi.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  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. if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
  390. (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
  391. continue;
  392. }
  393. trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
  394. ((OPJ_UINT64)comp->dx << levelno));
  395. try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
  396. ((OPJ_UINT64)comp->dy << levelno));
  397. trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
  398. ((OPJ_UINT64)comp->dx << levelno));
  399. try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
  400. ((OPJ_UINT64)comp->dy << levelno));
  401. rpx = res->pdx + levelno;
  402. rpy = res->pdy + levelno;
  403. if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
  404. (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
  405. continue;
  406. }
  407. /* See ISO-15441. B.12.1.3 Resolution level-position-component-layer progression */
  408. if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
  409. ((pi->y == pi->ty0) &&
  410. (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
  411. continue;
  412. }
  413. if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
  414. ((pi->x == pi->tx0) &&
  415. (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
  416. continue;
  417. }
  418. if ((res->pw == 0) || (res->ph == 0)) {
  419. continue;
  420. }
  421. if ((trx0 == trx1) || (try0 == try1)) {
  422. continue;
  423. }
  424. prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
  425. ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
  426. - opj_uint_floordivpow2(trx0, res->pdx);
  427. prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
  428. ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
  429. - opj_uint_floordivpow2(try0, res->pdy);
  430. pi->precno = prci + prcj * res->pw;
  431. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  432. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
  433. pi->step_c + pi->precno * pi->step_p;
  434. if (index >= pi->include_size) {
  435. opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
  436. return OPJ_FALSE;
  437. }
  438. if (!pi->include[index]) {
  439. pi->include[index] = 1;
  440. return OPJ_TRUE;
  441. }
  442. LABEL_SKIP:
  443. ;
  444. }
  445. }
  446. }
  447. }
  448. }
  449. return OPJ_FALSE;
  450. }
  451. static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi)
  452. {
  453. opj_pi_comp_t *comp = NULL;
  454. opj_pi_resolution_t *res = NULL;
  455. OPJ_UINT32 index = 0;
  456. if (pi->poc.compno0 >= pi->numcomps ||
  457. pi->poc.compno1 >= pi->numcomps + 1) {
  458. opj_event_msg(pi->manager, EVT_ERROR,
  459. "opj_pi_next_pcrl(): invalid compno0/compno1\n");
  460. return OPJ_FALSE;
  461. }
  462. if (!pi->first) {
  463. comp = &pi->comps[pi->compno];
  464. goto LABEL_SKIP;
  465. } else {
  466. OPJ_UINT32 compno, resno;
  467. pi->first = 0;
  468. pi->dx = 0;
  469. pi->dy = 0;
  470. for (compno = 0; compno < pi->numcomps; compno++) {
  471. comp = &pi->comps[compno];
  472. for (resno = 0; resno < comp->numresolutions; resno++) {
  473. OPJ_UINT32 dx, dy;
  474. res = &comp->resolutions[resno];
  475. if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
  476. comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
  477. dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
  478. pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
  479. }
  480. if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
  481. comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
  482. dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
  483. pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
  484. }
  485. }
  486. }
  487. if (pi->dx == 0 || pi->dy == 0) {
  488. return OPJ_FALSE;
  489. }
  490. }
  491. if (!pi->tp_on) {
  492. pi->poc.ty0 = pi->ty0;
  493. pi->poc.tx0 = pi->tx0;
  494. pi->poc.ty1 = pi->ty1;
  495. pi->poc.tx1 = pi->tx1;
  496. }
  497. for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
  498. pi->y += (pi->dy - (pi->y % pi->dy))) {
  499. for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
  500. pi->x += (pi->dx - (pi->x % pi->dx))) {
  501. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  502. comp = &pi->comps[pi->compno];
  503. for (pi->resno = pi->poc.resno0;
  504. pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
  505. OPJ_UINT32 levelno;
  506. OPJ_UINT32 trx0, try0;
  507. OPJ_UINT32 trx1, try1;
  508. OPJ_UINT32 rpx, rpy;
  509. OPJ_UINT32 prci, prcj;
  510. res = &comp->resolutions[pi->resno];
  511. levelno = comp->numresolutions - 1 - pi->resno;
  512. if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
  513. (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
  514. continue;
  515. }
  516. trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
  517. ((OPJ_UINT64)comp->dx << levelno));
  518. try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
  519. ((OPJ_UINT64)comp->dy << levelno));
  520. trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
  521. ((OPJ_UINT64)comp->dx << levelno));
  522. try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
  523. ((OPJ_UINT64)comp->dy << levelno));
  524. rpx = res->pdx + levelno;
  525. rpy = res->pdy + levelno;
  526. if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
  527. (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
  528. continue;
  529. }
  530. /* See ISO-15441. B.12.1.4 Position-component-resolution level-layer progression */
  531. if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
  532. ((pi->y == pi->ty0) &&
  533. (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
  534. continue;
  535. }
  536. if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
  537. ((pi->x == pi->tx0) &&
  538. (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
  539. continue;
  540. }
  541. if ((res->pw == 0) || (res->ph == 0)) {
  542. continue;
  543. }
  544. if ((trx0 == trx1) || (try0 == try1)) {
  545. continue;
  546. }
  547. prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
  548. ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
  549. - opj_uint_floordivpow2(trx0, res->pdx);
  550. prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
  551. ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
  552. - opj_uint_floordivpow2(try0, res->pdy);
  553. pi->precno = prci + prcj * res->pw;
  554. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  555. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
  556. pi->step_c + pi->precno * pi->step_p;
  557. if (index >= pi->include_size) {
  558. opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
  559. return OPJ_FALSE;
  560. }
  561. if (!pi->include[index]) {
  562. pi->include[index] = 1;
  563. return OPJ_TRUE;
  564. }
  565. LABEL_SKIP:
  566. ;
  567. }
  568. }
  569. }
  570. }
  571. }
  572. return OPJ_FALSE;
  573. }
  574. static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi)
  575. {
  576. opj_pi_comp_t *comp = NULL;
  577. opj_pi_resolution_t *res = NULL;
  578. OPJ_UINT32 index = 0;
  579. if (pi->poc.compno0 >= pi->numcomps ||
  580. pi->poc.compno1 >= pi->numcomps + 1) {
  581. opj_event_msg(pi->manager, EVT_ERROR,
  582. "opj_pi_next_cprl(): invalid compno0/compno1\n");
  583. return OPJ_FALSE;
  584. }
  585. if (!pi->first) {
  586. comp = &pi->comps[pi->compno];
  587. goto LABEL_SKIP;
  588. } else {
  589. pi->first = 0;
  590. }
  591. for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1; pi->compno++) {
  592. OPJ_UINT32 resno;
  593. comp = &pi->comps[pi->compno];
  594. pi->dx = 0;
  595. pi->dy = 0;
  596. for (resno = 0; resno < comp->numresolutions; resno++) {
  597. OPJ_UINT32 dx, dy;
  598. res = &comp->resolutions[resno];
  599. if (res->pdx + comp->numresolutions - 1 - resno < 32 &&
  600. comp->dx <= UINT_MAX / (1u << (res->pdx + comp->numresolutions - 1 - resno))) {
  601. dx = comp->dx * (1u << (res->pdx + comp->numresolutions - 1 - resno));
  602. pi->dx = !pi->dx ? dx : opj_uint_min(pi->dx, dx);
  603. }
  604. if (res->pdy + comp->numresolutions - 1 - resno < 32 &&
  605. comp->dy <= UINT_MAX / (1u << (res->pdy + comp->numresolutions - 1 - resno))) {
  606. dy = comp->dy * (1u << (res->pdy + comp->numresolutions - 1 - resno));
  607. pi->dy = !pi->dy ? dy : opj_uint_min(pi->dy, dy);
  608. }
  609. }
  610. if (pi->dx == 0 || pi->dy == 0) {
  611. return OPJ_FALSE;
  612. }
  613. if (!pi->tp_on) {
  614. pi->poc.ty0 = pi->ty0;
  615. pi->poc.tx0 = pi->tx0;
  616. pi->poc.ty1 = pi->ty1;
  617. pi->poc.tx1 = pi->tx1;
  618. }
  619. for (pi->y = (OPJ_UINT32)pi->poc.ty0; pi->y < (OPJ_UINT32)pi->poc.ty1;
  620. pi->y += (pi->dy - (pi->y % pi->dy))) {
  621. for (pi->x = (OPJ_UINT32)pi->poc.tx0; pi->x < (OPJ_UINT32)pi->poc.tx1;
  622. pi->x += (pi->dx - (pi->x % pi->dx))) {
  623. for (pi->resno = pi->poc.resno0;
  624. pi->resno < opj_uint_min(pi->poc.resno1, comp->numresolutions); pi->resno++) {
  625. OPJ_UINT32 levelno;
  626. OPJ_UINT32 trx0, try0;
  627. OPJ_UINT32 trx1, try1;
  628. OPJ_UINT32 rpx, rpy;
  629. OPJ_UINT32 prci, prcj;
  630. res = &comp->resolutions[pi->resno];
  631. levelno = comp->numresolutions - 1 - pi->resno;
  632. if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << levelno) >> levelno) != comp->dx ||
  633. (OPJ_UINT32)(((OPJ_UINT64)comp->dy << levelno) >> levelno) != comp->dy) {
  634. continue;
  635. }
  636. trx0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx0,
  637. ((OPJ_UINT64)comp->dx << levelno));
  638. try0 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty0,
  639. ((OPJ_UINT64)comp->dy << levelno));
  640. trx1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->tx1,
  641. ((OPJ_UINT64)comp->dx << levelno));
  642. try1 = opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->ty1,
  643. ((OPJ_UINT64)comp->dy << levelno));
  644. rpx = res->pdx + levelno;
  645. rpy = res->pdy + levelno;
  646. if ((OPJ_UINT32)(((OPJ_UINT64)comp->dx << rpx) >> rpx) != comp->dx ||
  647. (OPJ_UINT32)(((OPJ_UINT64)comp->dy << rpy) >> rpy) != comp->dy) {
  648. continue;
  649. }
  650. /* See ISO-15441. B.12.1.5 Component-position-resolution level-layer progression */
  651. if (!(((OPJ_UINT64)pi->y % ((OPJ_UINT64)comp->dy << rpy) == 0) ||
  652. ((pi->y == pi->ty0) &&
  653. (((OPJ_UINT64)try0 << levelno) % ((OPJ_UINT64)1U << rpy))))) {
  654. continue;
  655. }
  656. if (!(((OPJ_UINT64)pi->x % ((OPJ_UINT64)comp->dx << rpx) == 0) ||
  657. ((pi->x == pi->tx0) &&
  658. (((OPJ_UINT64)trx0 << levelno) % ((OPJ_UINT64)1U << rpx))))) {
  659. continue;
  660. }
  661. if ((res->pw == 0) || (res->ph == 0)) {
  662. continue;
  663. }
  664. if ((trx0 == trx1) || (try0 == try1)) {
  665. continue;
  666. }
  667. prci = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->x,
  668. ((OPJ_UINT64)comp->dx << levelno)), res->pdx)
  669. - opj_uint_floordivpow2(trx0, res->pdx);
  670. prcj = opj_uint_floordivpow2(opj_uint64_ceildiv_res_uint32((OPJ_UINT64)pi->y,
  671. ((OPJ_UINT64)comp->dy << levelno)), res->pdy)
  672. - opj_uint_floordivpow2(try0, res->pdy);
  673. pi->precno = (OPJ_UINT32)(prci + prcj * res->pw);
  674. for (pi->layno = pi->poc.layno0; pi->layno < pi->poc.layno1; pi->layno++) {
  675. index = pi->layno * pi->step_l + pi->resno * pi->step_r + pi->compno *
  676. pi->step_c + pi->precno * pi->step_p;
  677. if (index >= pi->include_size) {
  678. opj_event_msg(pi->manager, EVT_ERROR, "Invalid access to pi->include");
  679. return OPJ_FALSE;
  680. }
  681. if (!pi->include[index]) {
  682. pi->include[index] = 1;
  683. return OPJ_TRUE;
  684. }
  685. LABEL_SKIP:
  686. ;
  687. }
  688. }
  689. }
  690. }
  691. }
  692. return OPJ_FALSE;
  693. }
  694. static void opj_get_encoding_parameters(const opj_image_t *p_image,
  695. const opj_cp_t *p_cp,
  696. OPJ_UINT32 p_tileno,
  697. OPJ_UINT32 * p_tx0,
  698. OPJ_UINT32 * p_tx1,
  699. OPJ_UINT32 * p_ty0,
  700. OPJ_UINT32 * p_ty1,
  701. OPJ_UINT32 * p_dx_min,
  702. OPJ_UINT32 * p_dy_min,
  703. OPJ_UINT32 * p_max_prec,
  704. OPJ_UINT32 * p_max_res)
  705. {
  706. /* loop */
  707. OPJ_UINT32 compno, resno;
  708. /* pointers */
  709. const opj_tcp_t *l_tcp = 00;
  710. const opj_tccp_t * l_tccp = 00;
  711. const opj_image_comp_t * l_img_comp = 00;
  712. /* position in x and y of tile */
  713. OPJ_UINT32 p, q;
  714. /* non-corrected (in regard to image offset) tile offset */
  715. OPJ_UINT32 l_tx0, l_ty0;
  716. /* preconditions */
  717. assert(p_cp != 00);
  718. assert(p_image != 00);
  719. assert(p_tileno < p_cp->tw * p_cp->th);
  720. /* initializations */
  721. l_tcp = &p_cp->tcps [p_tileno];
  722. l_img_comp = p_image->comps;
  723. l_tccp = l_tcp->tccps;
  724. /* here calculation of tx0, tx1, ty0, ty1, maxprec, dx and dy */
  725. p = p_tileno % p_cp->tw;
  726. q = p_tileno / p_cp->tw;
  727. /* find extent of tile */
  728. l_tx0 = p_cp->tx0 + p *
  729. p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
  730. *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
  731. *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
  732. l_ty0 = p_cp->ty0 + q *
  733. p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
  734. *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
  735. *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
  736. /* max precision is 0 (can only grow) */
  737. *p_max_prec = 0;
  738. *p_max_res = 0;
  739. /* take the largest value for dx_min and dy_min */
  740. *p_dx_min = 0x7fffffff;
  741. *p_dy_min = 0x7fffffff;
  742. for (compno = 0; compno < p_image->numcomps; ++compno) {
  743. /* arithmetic variables to calculate */
  744. OPJ_UINT32 l_level_no;
  745. OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
  746. OPJ_UINT32 l_px0, l_py0, l_px1, py1;
  747. OPJ_UINT32 l_pdx, l_pdy;
  748. OPJ_UINT32 l_pw, l_ph;
  749. OPJ_UINT32 l_product;
  750. OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
  751. l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
  752. l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
  753. l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
  754. l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
  755. if (l_tccp->numresolutions > *p_max_res) {
  756. *p_max_res = l_tccp->numresolutions;
  757. }
  758. /* use custom size for precincts */
  759. for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
  760. OPJ_UINT64 l_dx, l_dy;
  761. /* precinct width and height */
  762. l_pdx = l_tccp->prcw[resno];
  763. l_pdy = l_tccp->prch[resno];
  764. l_dx = l_img_comp->dx * ((OPJ_UINT64)1u << (l_pdx + l_tccp->numresolutions - 1 -
  765. resno));
  766. l_dy = l_img_comp->dy * ((OPJ_UINT64)1u << (l_pdy + l_tccp->numresolutions - 1 -
  767. resno));
  768. /* take the minimum size for dx for each comp and resolution */
  769. if (l_dx <= UINT_MAX) {
  770. *p_dx_min = opj_uint_min(*p_dx_min, (OPJ_UINT32)l_dx);
  771. }
  772. if (l_dy <= UINT_MAX) {
  773. *p_dy_min = opj_uint_min(*p_dy_min, (OPJ_UINT32)l_dy);
  774. }
  775. /* various calculations of extents */
  776. l_level_no = l_tccp->numresolutions - 1 - resno;
  777. l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
  778. l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
  779. l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
  780. l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
  781. l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
  782. l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
  783. l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
  784. py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
  785. l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
  786. l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
  787. l_product = l_pw * l_ph;
  788. /* update precision */
  789. if (l_product > *p_max_prec) {
  790. *p_max_prec = l_product;
  791. }
  792. }
  793. ++l_img_comp;
  794. ++l_tccp;
  795. }
  796. }
  797. static void opj_get_all_encoding_parameters(const opj_image_t *p_image,
  798. const opj_cp_t *p_cp,
  799. OPJ_UINT32 tileno,
  800. OPJ_UINT32 * p_tx0,
  801. OPJ_UINT32 * p_tx1,
  802. OPJ_UINT32 * p_ty0,
  803. OPJ_UINT32 * p_ty1,
  804. OPJ_UINT32 * p_dx_min,
  805. OPJ_UINT32 * p_dy_min,
  806. OPJ_UINT32 * p_max_prec,
  807. OPJ_UINT32 * p_max_res,
  808. OPJ_UINT32 ** p_resolutions)
  809. {
  810. /* loop*/
  811. OPJ_UINT32 compno, resno;
  812. /* pointers*/
  813. const opj_tcp_t *tcp = 00;
  814. const opj_tccp_t * l_tccp = 00;
  815. const opj_image_comp_t * l_img_comp = 00;
  816. /* to store l_dx, l_dy, w and h for each resolution and component.*/
  817. OPJ_UINT32 * lResolutionPtr;
  818. /* position in x and y of tile*/
  819. OPJ_UINT32 p, q;
  820. /* non-corrected (in regard to image offset) tile offset */
  821. OPJ_UINT32 l_tx0, l_ty0;
  822. /* preconditions in debug*/
  823. assert(p_cp != 00);
  824. assert(p_image != 00);
  825. assert(tileno < p_cp->tw * p_cp->th);
  826. /* initializations*/
  827. tcp = &p_cp->tcps [tileno];
  828. l_tccp = tcp->tccps;
  829. l_img_comp = p_image->comps;
  830. /* position in x and y of tile*/
  831. p = tileno % p_cp->tw;
  832. q = tileno / p_cp->tw;
  833. /* here calculation of tx0, tx1, ty0, ty1, maxprec, l_dx and l_dy */
  834. l_tx0 = p_cp->tx0 + p *
  835. p_cp->tdx; /* can't be greater than p_image->x1 so won't overflow */
  836. *p_tx0 = opj_uint_max(l_tx0, p_image->x0);
  837. *p_tx1 = opj_uint_min(opj_uint_adds(l_tx0, p_cp->tdx), p_image->x1);
  838. l_ty0 = p_cp->ty0 + q *
  839. p_cp->tdy; /* can't be greater than p_image->y1 so won't overflow */
  840. *p_ty0 = opj_uint_max(l_ty0, p_image->y0);
  841. *p_ty1 = opj_uint_min(opj_uint_adds(l_ty0, p_cp->tdy), p_image->y1);
  842. /* max precision and resolution is 0 (can only grow)*/
  843. *p_max_prec = 0;
  844. *p_max_res = 0;
  845. /* take the largest value for dx_min and dy_min*/
  846. *p_dx_min = 0x7fffffff;
  847. *p_dy_min = 0x7fffffff;
  848. for (compno = 0; compno < p_image->numcomps; ++compno) {
  849. /* arithmetic variables to calculate*/
  850. OPJ_UINT32 l_level_no;
  851. OPJ_UINT32 l_rx0, l_ry0, l_rx1, l_ry1;
  852. OPJ_UINT32 l_px0, l_py0, l_px1, py1;
  853. OPJ_UINT32 l_product;
  854. OPJ_UINT32 l_tcx0, l_tcy0, l_tcx1, l_tcy1;
  855. OPJ_UINT32 l_pdx, l_pdy, l_pw, l_ph;
  856. lResolutionPtr = p_resolutions ? p_resolutions[compno] : NULL;
  857. l_tcx0 = opj_uint_ceildiv(*p_tx0, l_img_comp->dx);
  858. l_tcy0 = opj_uint_ceildiv(*p_ty0, l_img_comp->dy);
  859. l_tcx1 = opj_uint_ceildiv(*p_tx1, l_img_comp->dx);
  860. l_tcy1 = opj_uint_ceildiv(*p_ty1, l_img_comp->dy);
  861. if (l_tccp->numresolutions > *p_max_res) {
  862. *p_max_res = l_tccp->numresolutions;
  863. }
  864. /* use custom size for precincts*/
  865. l_level_no = l_tccp->numresolutions;
  866. for (resno = 0; resno < l_tccp->numresolutions; ++resno) {
  867. OPJ_UINT32 l_dx, l_dy;
  868. --l_level_no;
  869. /* precinct width and height*/
  870. l_pdx = l_tccp->prcw[resno];
  871. l_pdy = l_tccp->prch[resno];
  872. if (lResolutionPtr) {
  873. *lResolutionPtr++ = l_pdx;
  874. *lResolutionPtr++ = l_pdy;
  875. }
  876. if (l_pdx + l_level_no < 32 &&
  877. l_img_comp->dx <= UINT_MAX / (1u << (l_pdx + l_level_no))) {
  878. l_dx = l_img_comp->dx * (1u << (l_pdx + l_level_no));
  879. /* take the minimum size for l_dx for each comp and resolution*/
  880. *p_dx_min = opj_uint_min(*p_dx_min, l_dx);
  881. }
  882. if (l_pdy + l_level_no < 32 &&
  883. l_img_comp->dy <= UINT_MAX / (1u << (l_pdy + l_level_no))) {
  884. l_dy = l_img_comp->dy * (1u << (l_pdy + l_level_no));
  885. *p_dy_min = opj_uint_min(*p_dy_min, l_dy);
  886. }
  887. /* various calculations of extents*/
  888. l_rx0 = opj_uint_ceildivpow2(l_tcx0, l_level_no);
  889. l_ry0 = opj_uint_ceildivpow2(l_tcy0, l_level_no);
  890. l_rx1 = opj_uint_ceildivpow2(l_tcx1, l_level_no);
  891. l_ry1 = opj_uint_ceildivpow2(l_tcy1, l_level_no);
  892. l_px0 = opj_uint_floordivpow2(l_rx0, l_pdx) << l_pdx;
  893. l_py0 = opj_uint_floordivpow2(l_ry0, l_pdy) << l_pdy;
  894. l_px1 = opj_uint_ceildivpow2(l_rx1, l_pdx) << l_pdx;
  895. py1 = opj_uint_ceildivpow2(l_ry1, l_pdy) << l_pdy;
  896. l_pw = (l_rx0 == l_rx1) ? 0 : ((l_px1 - l_px0) >> l_pdx);
  897. l_ph = (l_ry0 == l_ry1) ? 0 : ((py1 - l_py0) >> l_pdy);
  898. if (lResolutionPtr) {
  899. *lResolutionPtr++ = l_pw;
  900. *lResolutionPtr++ = l_ph;
  901. }
  902. l_product = l_pw * l_ph;
  903. /* update precision*/
  904. if (l_product > *p_max_prec) {
  905. *p_max_prec = l_product;
  906. }
  907. }
  908. ++l_tccp;
  909. ++l_img_comp;
  910. }
  911. }
  912. static opj_pi_iterator_t * opj_pi_create(const opj_image_t *image,
  913. const opj_cp_t *cp,
  914. OPJ_UINT32 tileno,
  915. opj_event_mgr_t* manager)
  916. {
  917. /* loop*/
  918. OPJ_UINT32 pino, compno;
  919. /* number of poc in the p_pi*/
  920. OPJ_UINT32 l_poc_bound;
  921. /* pointers to tile coding parameters and components.*/
  922. opj_pi_iterator_t *l_pi = 00;
  923. opj_tcp_t *tcp = 00;
  924. const opj_tccp_t *tccp = 00;
  925. /* current packet iterator being allocated*/
  926. opj_pi_iterator_t *l_current_pi = 00;
  927. /* preconditions in debug*/
  928. assert(cp != 00);
  929. assert(image != 00);
  930. assert(tileno < cp->tw * cp->th);
  931. /* initializations*/
  932. tcp = &cp->tcps[tileno];
  933. l_poc_bound = tcp->numpocs + 1;
  934. /* memory allocations*/
  935. l_pi = (opj_pi_iterator_t*) opj_calloc((l_poc_bound),
  936. sizeof(opj_pi_iterator_t));
  937. if (!l_pi) {
  938. return NULL;
  939. }
  940. l_current_pi = l_pi;
  941. for (pino = 0; pino < l_poc_bound ; ++pino) {
  942. l_current_pi->manager = manager;
  943. l_current_pi->comps = (opj_pi_comp_t*) opj_calloc(image->numcomps,
  944. sizeof(opj_pi_comp_t));
  945. if (! l_current_pi->comps) {
  946. opj_pi_destroy(l_pi, l_poc_bound);
  947. return NULL;
  948. }
  949. l_current_pi->numcomps = image->numcomps;
  950. for (compno = 0; compno < image->numcomps; ++compno) {
  951. opj_pi_comp_t *comp = &l_current_pi->comps[compno];
  952. tccp = &tcp->tccps[compno];
  953. comp->resolutions = (opj_pi_resolution_t*) opj_calloc(tccp->numresolutions,
  954. sizeof(opj_pi_resolution_t));
  955. if (!comp->resolutions) {
  956. opj_pi_destroy(l_pi, l_poc_bound);
  957. return 00;
  958. }
  959. comp->numresolutions = tccp->numresolutions;
  960. }
  961. ++l_current_pi;
  962. }
  963. return l_pi;
  964. }
  965. static void opj_pi_update_encode_poc_and_final(opj_cp_t *p_cp,
  966. OPJ_UINT32 p_tileno,
  967. OPJ_UINT32 p_tx0,
  968. OPJ_UINT32 p_tx1,
  969. OPJ_UINT32 p_ty0,
  970. OPJ_UINT32 p_ty1,
  971. OPJ_UINT32 p_max_prec,
  972. OPJ_UINT32 p_max_res,
  973. OPJ_UINT32 p_dx_min,
  974. OPJ_UINT32 p_dy_min)
  975. {
  976. /* loop*/
  977. OPJ_UINT32 pino;
  978. /* tile coding parameter*/
  979. opj_tcp_t *l_tcp = 00;
  980. /* current poc being updated*/
  981. opj_poc_t * l_current_poc = 00;
  982. /* number of pocs*/
  983. OPJ_UINT32 l_poc_bound;
  984. OPJ_ARG_NOT_USED(p_max_res);
  985. /* preconditions in debug*/
  986. assert(p_cp != 00);
  987. assert(p_tileno < p_cp->tw * p_cp->th);
  988. /* initializations*/
  989. l_tcp = &p_cp->tcps [p_tileno];
  990. /* number of iterations in the loop */
  991. l_poc_bound = l_tcp->numpocs + 1;
  992. /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
  993. store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
  994. l_current_poc = l_tcp->pocs;
  995. l_current_poc->compS = l_current_poc->compno0;
  996. l_current_poc->compE = l_current_poc->compno1;
  997. l_current_poc->resS = l_current_poc->resno0;
  998. l_current_poc->resE = l_current_poc->resno1;
  999. l_current_poc->layE = l_current_poc->layno1;
  1000. /* special treatment for the first element*/
  1001. l_current_poc->layS = 0;
  1002. l_current_poc->prg = l_current_poc->prg1;
  1003. l_current_poc->prcS = 0;
  1004. l_current_poc->prcE = p_max_prec;
  1005. l_current_poc->txS = (OPJ_UINT32)p_tx0;
  1006. l_current_poc->txE = (OPJ_UINT32)p_tx1;
  1007. l_current_poc->tyS = (OPJ_UINT32)p_ty0;
  1008. l_current_poc->tyE = (OPJ_UINT32)p_ty1;
  1009. l_current_poc->dx = p_dx_min;
  1010. l_current_poc->dy = p_dy_min;
  1011. ++ l_current_poc;
  1012. for (pino = 1; pino < l_poc_bound ; ++pino) {
  1013. l_current_poc->compS = l_current_poc->compno0;
  1014. l_current_poc->compE = l_current_poc->compno1;
  1015. l_current_poc->resS = l_current_poc->resno0;
  1016. l_current_poc->resE = l_current_poc->resno1;
  1017. l_current_poc->layE = l_current_poc->layno1;
  1018. l_current_poc->prg = l_current_poc->prg1;
  1019. l_current_poc->prcS = 0;
  1020. /* special treatment here different from the first element*/
  1021. l_current_poc->layS = (l_current_poc->layE > (l_current_poc - 1)->layE) ?
  1022. l_current_poc->layE : 0;
  1023. l_current_poc->prcE = p_max_prec;
  1024. l_current_poc->txS = (OPJ_UINT32)p_tx0;
  1025. l_current_poc->txE = (OPJ_UINT32)p_tx1;
  1026. l_current_poc->tyS = (OPJ_UINT32)p_ty0;
  1027. l_current_poc->tyE = (OPJ_UINT32)p_ty1;
  1028. l_current_poc->dx = p_dx_min;
  1029. l_current_poc->dy = p_dy_min;
  1030. ++ l_current_poc;
  1031. }
  1032. }
  1033. static void opj_pi_update_encode_not_poc(opj_cp_t *p_cp,
  1034. OPJ_UINT32 p_num_comps,
  1035. OPJ_UINT32 p_tileno,
  1036. OPJ_UINT32 p_tx0,
  1037. OPJ_UINT32 p_tx1,
  1038. OPJ_UINT32 p_ty0,
  1039. OPJ_UINT32 p_ty1,
  1040. OPJ_UINT32 p_max_prec,
  1041. OPJ_UINT32 p_max_res,
  1042. OPJ_UINT32 p_dx_min,
  1043. OPJ_UINT32 p_dy_min)
  1044. {
  1045. /* loop*/
  1046. OPJ_UINT32 pino;
  1047. /* tile coding parameter*/
  1048. opj_tcp_t *l_tcp = 00;
  1049. /* current poc being updated*/
  1050. opj_poc_t * l_current_poc = 00;
  1051. /* number of pocs*/
  1052. OPJ_UINT32 l_poc_bound;
  1053. /* preconditions in debug*/
  1054. assert(p_cp != 00);
  1055. assert(p_tileno < p_cp->tw * p_cp->th);
  1056. /* initializations*/
  1057. l_tcp = &p_cp->tcps [p_tileno];
  1058. /* number of iterations in the loop */
  1059. l_poc_bound = l_tcp->numpocs + 1;
  1060. /* start at first element, and to make sure the compiler will not make a calculation each time in the loop
  1061. store a pointer to the current element to modify rather than l_tcp->pocs[i]*/
  1062. l_current_poc = l_tcp->pocs;
  1063. for (pino = 0; pino < l_poc_bound ; ++pino) {
  1064. l_current_poc->compS = 0;
  1065. l_current_poc->compE = p_num_comps;/*p_image->numcomps;*/
  1066. l_current_poc->resS = 0;
  1067. l_current_poc->resE = p_max_res;
  1068. l_current_poc->layS = 0;
  1069. l_current_poc->layE = l_tcp->numlayers;
  1070. l_current_poc->prg = l_tcp->prg;
  1071. l_current_poc->prcS = 0;
  1072. l_current_poc->prcE = p_max_prec;
  1073. l_current_poc->txS = p_tx0;
  1074. l_current_poc->txE = p_tx1;
  1075. l_current_poc->tyS = p_ty0;
  1076. l_current_poc->tyE = p_ty1;
  1077. l_current_poc->dx = p_dx_min;
  1078. l_current_poc->dy = p_dy_min;
  1079. ++ l_current_poc;
  1080. }
  1081. }
  1082. static void opj_pi_update_decode_poc(opj_pi_iterator_t * p_pi,
  1083. opj_tcp_t * p_tcp,
  1084. OPJ_UINT32 p_max_precision,
  1085. OPJ_UINT32 p_max_res)
  1086. {
  1087. /* loop*/
  1088. OPJ_UINT32 pino;
  1089. /* encoding parameters to set*/
  1090. OPJ_UINT32 l_bound;
  1091. opj_pi_iterator_t * l_current_pi = 00;
  1092. opj_poc_t* l_current_poc = 0;
  1093. OPJ_ARG_NOT_USED(p_max_res);
  1094. /* preconditions in debug*/
  1095. assert(p_pi != 00);
  1096. assert(p_tcp != 00);
  1097. /* initializations*/
  1098. l_bound = p_tcp->numpocs + 1;
  1099. l_current_pi = p_pi;
  1100. l_current_poc = p_tcp->pocs;
  1101. for (pino = 0; pino < l_bound; ++pino) {
  1102. l_current_pi->poc.prg = l_current_poc->prg; /* Progression Order #0 */
  1103. l_current_pi->first = 1;
  1104. l_current_pi->poc.resno0 =
  1105. l_current_poc->resno0; /* Resolution Level Index #0 (Start) */
  1106. l_current_pi->poc.compno0 =
  1107. l_current_poc->compno0; /* Component Index #0 (Start) */
  1108. l_current_pi->poc.layno0 = 0;
  1109. l_current_pi->poc.precno0 = 0;
  1110. l_current_pi->poc.resno1 =
  1111. l_current_poc->resno1; /* Resolution Level Index #0 (End) */
  1112. l_current_pi->poc.compno1 =
  1113. l_current_poc->compno1; /* Component Index #0 (End) */
  1114. l_current_pi->poc.layno1 = opj_uint_min(l_current_poc->layno1,
  1115. p_tcp->numlayers); /* Layer Index #0 (End) */
  1116. l_current_pi->poc.precno1 = p_max_precision;
  1117. ++l_current_pi;
  1118. ++l_current_poc;
  1119. }
  1120. }
  1121. static void opj_pi_update_decode_not_poc(opj_pi_iterator_t * p_pi,
  1122. opj_tcp_t * p_tcp,
  1123. OPJ_UINT32 p_max_precision,
  1124. OPJ_UINT32 p_max_res)
  1125. {
  1126. /* loop*/
  1127. OPJ_UINT32 pino;
  1128. /* encoding parameters to set*/
  1129. OPJ_UINT32 l_bound;
  1130. opj_pi_iterator_t * l_current_pi = 00;
  1131. /* preconditions in debug*/
  1132. assert(p_tcp != 00);
  1133. assert(p_pi != 00);
  1134. /* initializations*/
  1135. l_bound = p_tcp->numpocs + 1;
  1136. l_current_pi = p_pi;
  1137. for (pino = 0; pino < l_bound; ++pino) {
  1138. l_current_pi->poc.prg = p_tcp->prg;
  1139. l_current_pi->first = 1;
  1140. l_current_pi->poc.resno0 = 0;
  1141. l_current_pi->poc.compno0 = 0;
  1142. l_current_pi->poc.layno0 = 0;
  1143. l_current_pi->poc.precno0 = 0;
  1144. l_current_pi->poc.resno1 = p_max_res;
  1145. l_current_pi->poc.compno1 = l_current_pi->numcomps;
  1146. l_current_pi->poc.layno1 = p_tcp->numlayers;
  1147. l_current_pi->poc.precno1 = p_max_precision;
  1148. ++l_current_pi;
  1149. }
  1150. }
  1151. static OPJ_BOOL opj_pi_check_next_level(OPJ_INT32 pos,
  1152. opj_cp_t *cp,
  1153. OPJ_UINT32 tileno,
  1154. OPJ_UINT32 pino,
  1155. const OPJ_CHAR *prog)
  1156. {
  1157. OPJ_INT32 i;
  1158. opj_tcp_t *tcps = &cp->tcps[tileno];
  1159. opj_poc_t *tcp = &tcps->pocs[pino];
  1160. if (pos >= 0) {
  1161. for (i = pos; i >= 0; i--) {
  1162. switch (prog[i]) {
  1163. case 'R':
  1164. if (tcp->res_t == tcp->resE) {
  1165. if (opj_pi_check_next_level(pos - 1, cp, tileno, pino, prog)) {
  1166. return OPJ_TRUE;
  1167. } else {
  1168. return OPJ_FALSE;
  1169. }
  1170. } else {
  1171. return OPJ_TRUE;
  1172. }
  1173. break;
  1174. case 'C':
  1175. if (tcp->comp_t == tcp->compE) {
  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 'L':
  1186. if (tcp->lay_t == tcp->layE) {
  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 'P':
  1197. switch (tcp->prg) {
  1198. case OPJ_LRCP: /* fall through */
  1199. case OPJ_RLCP:
  1200. if (tcp->prc_t == tcp->prcE) {
  1201. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1202. return OPJ_TRUE;
  1203. } else {
  1204. return OPJ_FALSE;
  1205. }
  1206. } else {
  1207. return OPJ_TRUE;
  1208. }
  1209. break;
  1210. default:
  1211. if (tcp->tx0_t == tcp->txE) {
  1212. /*TY*/
  1213. if (tcp->ty0_t == tcp->tyE) {
  1214. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1215. return OPJ_TRUE;
  1216. } else {
  1217. return OPJ_FALSE;
  1218. }
  1219. } else {
  1220. return OPJ_TRUE;
  1221. }/*TY*/
  1222. } else {
  1223. return OPJ_TRUE;
  1224. }
  1225. break;
  1226. }/*end case P*/
  1227. }/*end switch*/
  1228. }/*end for*/
  1229. }/*end if*/
  1230. return OPJ_FALSE;
  1231. }
  1232. /*
  1233. ==========================================================
  1234. Packet iterator interface
  1235. ==========================================================
  1236. */
  1237. opj_pi_iterator_t *opj_pi_create_decode(opj_image_t *p_image,
  1238. opj_cp_t *p_cp,
  1239. OPJ_UINT32 p_tile_no,
  1240. opj_event_mgr_t* manager)
  1241. {
  1242. OPJ_UINT32 numcomps = p_image->numcomps;
  1243. /* loop */
  1244. OPJ_UINT32 pino;
  1245. OPJ_UINT32 compno, resno;
  1246. /* to store w, h, dx and dy for all components and resolutions */
  1247. OPJ_UINT32 * l_tmp_data;
  1248. OPJ_UINT32 ** l_tmp_ptr;
  1249. /* encoding parameters to set */
  1250. OPJ_UINT32 l_max_res;
  1251. OPJ_UINT32 l_max_prec;
  1252. OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
  1253. OPJ_UINT32 l_dx_min, l_dy_min;
  1254. OPJ_UINT32 l_bound;
  1255. OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
  1256. OPJ_UINT32 l_data_stride;
  1257. /* pointers */
  1258. opj_pi_iterator_t *l_pi = 00;
  1259. opj_tcp_t *l_tcp = 00;
  1260. const opj_tccp_t *l_tccp = 00;
  1261. opj_pi_comp_t *l_current_comp = 00;
  1262. opj_image_comp_t * l_img_comp = 00;
  1263. opj_pi_iterator_t * l_current_pi = 00;
  1264. OPJ_UINT32 * l_encoding_value_ptr = 00;
  1265. /* preconditions in debug */
  1266. assert(p_cp != 00);
  1267. assert(p_image != 00);
  1268. assert(p_tile_no < p_cp->tw * p_cp->th);
  1269. /* initializations */
  1270. l_tcp = &p_cp->tcps[p_tile_no];
  1271. l_bound = l_tcp->numpocs + 1;
  1272. l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
  1273. l_tmp_data = (OPJ_UINT32*)opj_malloc(
  1274. l_data_stride * numcomps * sizeof(OPJ_UINT32));
  1275. if
  1276. (! l_tmp_data) {
  1277. return 00;
  1278. }
  1279. l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
  1280. numcomps * sizeof(OPJ_UINT32 *));
  1281. if
  1282. (! l_tmp_ptr) {
  1283. opj_free(l_tmp_data);
  1284. return 00;
  1285. }
  1286. /* memory allocation for pi */
  1287. l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
  1288. if (!l_pi) {
  1289. opj_free(l_tmp_data);
  1290. opj_free(l_tmp_ptr);
  1291. return 00;
  1292. }
  1293. l_encoding_value_ptr = l_tmp_data;
  1294. /* update pointer array */
  1295. for
  1296. (compno = 0; compno < numcomps; ++compno) {
  1297. l_tmp_ptr[compno] = l_encoding_value_ptr;
  1298. l_encoding_value_ptr += l_data_stride;
  1299. }
  1300. /* get encoding parameters */
  1301. opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
  1302. &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
  1303. /* step calculations */
  1304. l_step_p = 1;
  1305. l_step_c = l_max_prec * l_step_p;
  1306. l_step_r = numcomps * l_step_c;
  1307. l_step_l = l_max_res * l_step_r;
  1308. /* set values for first packet iterator */
  1309. l_current_pi = l_pi;
  1310. /* memory allocation for include */
  1311. /* prevent an integer overflow issue */
  1312. /* 0 < l_tcp->numlayers < 65536 c.f. opj_j2k_read_cod in j2k.c */
  1313. l_current_pi->include = 00;
  1314. if (l_step_l <= (UINT_MAX / (l_tcp->numlayers + 1U))) {
  1315. l_current_pi->include_size = (l_tcp->numlayers + 1U) * l_step_l;
  1316. l_current_pi->include = (OPJ_INT16*) opj_calloc(
  1317. l_current_pi->include_size, sizeof(OPJ_INT16));
  1318. }
  1319. if (!l_current_pi->include) {
  1320. opj_free(l_tmp_data);
  1321. opj_free(l_tmp_ptr);
  1322. opj_pi_destroy(l_pi, l_bound);
  1323. return 00;
  1324. }
  1325. /* special treatment for the first packet iterator */
  1326. l_current_comp = l_current_pi->comps;
  1327. l_img_comp = p_image->comps;
  1328. l_tccp = l_tcp->tccps;
  1329. l_current_pi->tx0 = l_tx0;
  1330. l_current_pi->ty0 = l_ty0;
  1331. l_current_pi->tx1 = l_tx1;
  1332. l_current_pi->ty1 = l_ty1;
  1333. /*l_current_pi->dx = l_img_comp->dx;*/
  1334. /*l_current_pi->dy = l_img_comp->dy;*/
  1335. l_current_pi->step_p = l_step_p;
  1336. l_current_pi->step_c = l_step_c;
  1337. l_current_pi->step_r = l_step_r;
  1338. l_current_pi->step_l = l_step_l;
  1339. /* allocation for components and number of components has already been calculated by opj_pi_create */
  1340. for
  1341. (compno = 0; compno < numcomps; ++compno) {
  1342. opj_pi_resolution_t *l_res = l_current_comp->resolutions;
  1343. l_encoding_value_ptr = l_tmp_ptr[compno];
  1344. l_current_comp->dx = l_img_comp->dx;
  1345. l_current_comp->dy = l_img_comp->dy;
  1346. /* resolutions have already been initialized */
  1347. for
  1348. (resno = 0; resno < l_current_comp->numresolutions; resno++) {
  1349. l_res->pdx = *(l_encoding_value_ptr++);
  1350. l_res->pdy = *(l_encoding_value_ptr++);
  1351. l_res->pw = *(l_encoding_value_ptr++);
  1352. l_res->ph = *(l_encoding_value_ptr++);
  1353. ++l_res;
  1354. }
  1355. ++l_current_comp;
  1356. ++l_img_comp;
  1357. ++l_tccp;
  1358. }
  1359. ++l_current_pi;
  1360. for (pino = 1 ; pino < l_bound ; ++pino) {
  1361. l_current_comp = l_current_pi->comps;
  1362. l_img_comp = p_image->comps;
  1363. l_tccp = l_tcp->tccps;
  1364. l_current_pi->tx0 = l_tx0;
  1365. l_current_pi->ty0 = l_ty0;
  1366. l_current_pi->tx1 = l_tx1;
  1367. l_current_pi->ty1 = l_ty1;
  1368. /*l_current_pi->dx = l_dx_min;*/
  1369. /*l_current_pi->dy = l_dy_min;*/
  1370. l_current_pi->step_p = l_step_p;
  1371. l_current_pi->step_c = l_step_c;
  1372. l_current_pi->step_r = l_step_r;
  1373. l_current_pi->step_l = l_step_l;
  1374. /* allocation for components and number of components has already been calculated by opj_pi_create */
  1375. for
  1376. (compno = 0; compno < numcomps; ++compno) {
  1377. opj_pi_resolution_t *l_res = l_current_comp->resolutions;
  1378. l_encoding_value_ptr = l_tmp_ptr[compno];
  1379. l_current_comp->dx = l_img_comp->dx;
  1380. l_current_comp->dy = l_img_comp->dy;
  1381. /* resolutions have already been initialized */
  1382. for
  1383. (resno = 0; resno < l_current_comp->numresolutions; resno++) {
  1384. l_res->pdx = *(l_encoding_value_ptr++);
  1385. l_res->pdy = *(l_encoding_value_ptr++);
  1386. l_res->pw = *(l_encoding_value_ptr++);
  1387. l_res->ph = *(l_encoding_value_ptr++);
  1388. ++l_res;
  1389. }
  1390. ++l_current_comp;
  1391. ++l_img_comp;
  1392. ++l_tccp;
  1393. }
  1394. /* special treatment*/
  1395. l_current_pi->include = (l_current_pi - 1)->include;
  1396. l_current_pi->include_size = (l_current_pi - 1)->include_size;
  1397. ++l_current_pi;
  1398. }
  1399. opj_free(l_tmp_data);
  1400. l_tmp_data = 00;
  1401. opj_free(l_tmp_ptr);
  1402. l_tmp_ptr = 00;
  1403. if
  1404. (l_tcp->POC) {
  1405. opj_pi_update_decode_poc(l_pi, l_tcp, l_max_prec, l_max_res);
  1406. } else {
  1407. opj_pi_update_decode_not_poc(l_pi, l_tcp, l_max_prec, l_max_res);
  1408. }
  1409. return l_pi;
  1410. }
  1411. OPJ_UINT32 opj_get_encoding_packet_count(const opj_image_t *p_image,
  1412. const opj_cp_t *p_cp,
  1413. OPJ_UINT32 p_tile_no)
  1414. {
  1415. OPJ_UINT32 l_max_res;
  1416. OPJ_UINT32 l_max_prec;
  1417. OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
  1418. OPJ_UINT32 l_dx_min, l_dy_min;
  1419. /* preconditions in debug*/
  1420. assert(p_cp != 00);
  1421. assert(p_image != 00);
  1422. assert(p_tile_no < p_cp->tw * p_cp->th);
  1423. /* get encoding parameters*/
  1424. opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
  1425. &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, NULL);
  1426. return p_cp->tcps[p_tile_no].numlayers * l_max_prec * p_image->numcomps *
  1427. l_max_res;
  1428. }
  1429. opj_pi_iterator_t *opj_pi_initialise_encode(const opj_image_t *p_image,
  1430. opj_cp_t *p_cp,
  1431. OPJ_UINT32 p_tile_no,
  1432. J2K_T2_MODE p_t2_mode,
  1433. opj_event_mgr_t* manager)
  1434. {
  1435. OPJ_UINT32 numcomps = p_image->numcomps;
  1436. /* loop*/
  1437. OPJ_UINT32 pino;
  1438. OPJ_UINT32 compno, resno;
  1439. /* to store w, h, dx and dy for all components and resolutions*/
  1440. OPJ_UINT32 * l_tmp_data;
  1441. OPJ_UINT32 ** l_tmp_ptr;
  1442. /* encoding parameters to set*/
  1443. OPJ_UINT32 l_max_res;
  1444. OPJ_UINT32 l_max_prec;
  1445. OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
  1446. OPJ_UINT32 l_dx_min, l_dy_min;
  1447. OPJ_UINT32 l_bound;
  1448. OPJ_UINT32 l_step_p, l_step_c, l_step_r, l_step_l ;
  1449. OPJ_UINT32 l_data_stride;
  1450. /* pointers*/
  1451. opj_pi_iterator_t *l_pi = 00;
  1452. opj_tcp_t *l_tcp = 00;
  1453. const opj_tccp_t *l_tccp = 00;
  1454. opj_pi_comp_t *l_current_comp = 00;
  1455. opj_image_comp_t * l_img_comp = 00;
  1456. opj_pi_iterator_t * l_current_pi = 00;
  1457. OPJ_UINT32 * l_encoding_value_ptr = 00;
  1458. /* preconditions in debug*/
  1459. assert(p_cp != 00);
  1460. assert(p_image != 00);
  1461. assert(p_tile_no < p_cp->tw * p_cp->th);
  1462. /* initializations*/
  1463. l_tcp = &p_cp->tcps[p_tile_no];
  1464. l_bound = l_tcp->numpocs + 1;
  1465. l_data_stride = 4 * OPJ_J2K_MAXRLVLS;
  1466. l_tmp_data = (OPJ_UINT32*)opj_malloc(
  1467. l_data_stride * numcomps * sizeof(OPJ_UINT32));
  1468. if (! l_tmp_data) {
  1469. return 00;
  1470. }
  1471. l_tmp_ptr = (OPJ_UINT32**)opj_malloc(
  1472. numcomps * sizeof(OPJ_UINT32 *));
  1473. if (! l_tmp_ptr) {
  1474. opj_free(l_tmp_data);
  1475. return 00;
  1476. }
  1477. /* memory allocation for pi*/
  1478. l_pi = opj_pi_create(p_image, p_cp, p_tile_no, manager);
  1479. if (!l_pi) {
  1480. opj_free(l_tmp_data);
  1481. opj_free(l_tmp_ptr);
  1482. return 00;
  1483. }
  1484. l_encoding_value_ptr = l_tmp_data;
  1485. /* update pointer array*/
  1486. for (compno = 0; compno < numcomps; ++compno) {
  1487. l_tmp_ptr[compno] = l_encoding_value_ptr;
  1488. l_encoding_value_ptr += l_data_stride;
  1489. }
  1490. /* get encoding parameters*/
  1491. opj_get_all_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1,
  1492. &l_ty0, &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res, l_tmp_ptr);
  1493. /* step calculations*/
  1494. l_step_p = 1;
  1495. l_step_c = l_max_prec * l_step_p;
  1496. l_step_r = numcomps * l_step_c;
  1497. l_step_l = l_max_res * l_step_r;
  1498. /* set values for first packet iterator*/
  1499. l_pi->tp_on = (OPJ_BYTE)p_cp->m_specific_param.m_enc.m_tp_on;
  1500. l_current_pi = l_pi;
  1501. /* memory allocation for include*/
  1502. l_current_pi->include_size = l_tcp->numlayers * l_step_l;
  1503. l_current_pi->include = (OPJ_INT16*) opj_calloc(l_current_pi->include_size,
  1504. sizeof(OPJ_INT16));
  1505. if (!l_current_pi->include) {
  1506. opj_free(l_tmp_data);
  1507. opj_free(l_tmp_ptr);
  1508. opj_pi_destroy(l_pi, l_bound);
  1509. return 00;
  1510. }
  1511. /* special treatment for the first packet iterator*/
  1512. l_current_comp = l_current_pi->comps;
  1513. l_img_comp = p_image->comps;
  1514. l_tccp = l_tcp->tccps;
  1515. l_current_pi->tx0 = l_tx0;
  1516. l_current_pi->ty0 = l_ty0;
  1517. l_current_pi->tx1 = l_tx1;
  1518. l_current_pi->ty1 = l_ty1;
  1519. l_current_pi->dx = l_dx_min;
  1520. l_current_pi->dy = l_dy_min;
  1521. l_current_pi->step_p = l_step_p;
  1522. l_current_pi->step_c = l_step_c;
  1523. l_current_pi->step_r = l_step_r;
  1524. l_current_pi->step_l = l_step_l;
  1525. /* allocation for components and number of components has already been calculated by opj_pi_create */
  1526. for (compno = 0; compno < numcomps; ++compno) {
  1527. opj_pi_resolution_t *l_res = l_current_comp->resolutions;
  1528. l_encoding_value_ptr = l_tmp_ptr[compno];
  1529. l_current_comp->dx = l_img_comp->dx;
  1530. l_current_comp->dy = l_img_comp->dy;
  1531. /* resolutions have already been initialized */
  1532. for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
  1533. l_res->pdx = *(l_encoding_value_ptr++);
  1534. l_res->pdy = *(l_encoding_value_ptr++);
  1535. l_res->pw = *(l_encoding_value_ptr++);
  1536. l_res->ph = *(l_encoding_value_ptr++);
  1537. ++l_res;
  1538. }
  1539. ++l_current_comp;
  1540. ++l_img_comp;
  1541. ++l_tccp;
  1542. }
  1543. ++l_current_pi;
  1544. for (pino = 1 ; pino < l_bound ; ++pino) {
  1545. l_current_comp = l_current_pi->comps;
  1546. l_img_comp = p_image->comps;
  1547. l_tccp = l_tcp->tccps;
  1548. l_current_pi->tx0 = l_tx0;
  1549. l_current_pi->ty0 = l_ty0;
  1550. l_current_pi->tx1 = l_tx1;
  1551. l_current_pi->ty1 = l_ty1;
  1552. l_current_pi->dx = l_dx_min;
  1553. l_current_pi->dy = l_dy_min;
  1554. l_current_pi->step_p = l_step_p;
  1555. l_current_pi->step_c = l_step_c;
  1556. l_current_pi->step_r = l_step_r;
  1557. l_current_pi->step_l = l_step_l;
  1558. /* allocation for components and number of components has already been calculated by opj_pi_create */
  1559. for (compno = 0; compno < numcomps; ++compno) {
  1560. opj_pi_resolution_t *l_res = l_current_comp->resolutions;
  1561. l_encoding_value_ptr = l_tmp_ptr[compno];
  1562. l_current_comp->dx = l_img_comp->dx;
  1563. l_current_comp->dy = l_img_comp->dy;
  1564. /* resolutions have already been initialized */
  1565. for (resno = 0; resno < l_current_comp->numresolutions; resno++) {
  1566. l_res->pdx = *(l_encoding_value_ptr++);
  1567. l_res->pdy = *(l_encoding_value_ptr++);
  1568. l_res->pw = *(l_encoding_value_ptr++);
  1569. l_res->ph = *(l_encoding_value_ptr++);
  1570. ++l_res;
  1571. }
  1572. ++l_current_comp;
  1573. ++l_img_comp;
  1574. ++l_tccp;
  1575. }
  1576. /* special treatment*/
  1577. l_current_pi->include = (l_current_pi - 1)->include;
  1578. l_current_pi->include_size = (l_current_pi - 1)->include_size;
  1579. ++l_current_pi;
  1580. }
  1581. opj_free(l_tmp_data);
  1582. l_tmp_data = 00;
  1583. opj_free(l_tmp_ptr);
  1584. l_tmp_ptr = 00;
  1585. if (l_tcp->POC && (OPJ_IS_CINEMA(p_cp->rsiz) || p_t2_mode == FINAL_PASS)) {
  1586. opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
  1587. l_max_prec, l_max_res, l_dx_min, l_dy_min);
  1588. } else {
  1589. opj_pi_update_encode_not_poc(p_cp, numcomps, p_tile_no, l_tx0, l_tx1,
  1590. l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
  1591. }
  1592. return l_pi;
  1593. }
  1594. void opj_pi_create_encode(opj_pi_iterator_t *pi,
  1595. opj_cp_t *cp,
  1596. OPJ_UINT32 tileno,
  1597. OPJ_UINT32 pino,
  1598. OPJ_UINT32 tpnum,
  1599. OPJ_INT32 tppos,
  1600. J2K_T2_MODE t2_mode)
  1601. {
  1602. const OPJ_CHAR *prog;
  1603. OPJ_INT32 i;
  1604. OPJ_UINT32 incr_top = 1, resetX = 0;
  1605. opj_tcp_t *tcps = &cp->tcps[tileno];
  1606. opj_poc_t *tcp = &tcps->pocs[pino];
  1607. prog = opj_j2k_convert_progression_order(tcp->prg);
  1608. pi[pino].first = 1;
  1609. pi[pino].poc.prg = tcp->prg;
  1610. if (!(cp->m_specific_param.m_enc.m_tp_on && ((!OPJ_IS_CINEMA(cp->rsiz) &&
  1611. !OPJ_IS_IMF(cp->rsiz) &&
  1612. (t2_mode == FINAL_PASS)) || OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)))) {
  1613. pi[pino].poc.resno0 = tcp->resS;
  1614. pi[pino].poc.resno1 = tcp->resE;
  1615. pi[pino].poc.compno0 = tcp->compS;
  1616. pi[pino].poc.compno1 = tcp->compE;
  1617. pi[pino].poc.layno0 = tcp->layS;
  1618. pi[pino].poc.layno1 = tcp->layE;
  1619. pi[pino].poc.precno0 = tcp->prcS;
  1620. pi[pino].poc.precno1 = tcp->prcE;
  1621. pi[pino].poc.tx0 = tcp->txS;
  1622. pi[pino].poc.ty0 = tcp->tyS;
  1623. pi[pino].poc.tx1 = tcp->txE;
  1624. pi[pino].poc.ty1 = tcp->tyE;
  1625. } else {
  1626. for (i = tppos + 1; i < 4; i++) {
  1627. switch (prog[i]) {
  1628. case 'R':
  1629. pi[pino].poc.resno0 = tcp->resS;
  1630. pi[pino].poc.resno1 = tcp->resE;
  1631. break;
  1632. case 'C':
  1633. pi[pino].poc.compno0 = tcp->compS;
  1634. pi[pino].poc.compno1 = tcp->compE;
  1635. break;
  1636. case 'L':
  1637. pi[pino].poc.layno0 = tcp->layS;
  1638. pi[pino].poc.layno1 = tcp->layE;
  1639. break;
  1640. case 'P':
  1641. switch (tcp->prg) {
  1642. case OPJ_LRCP:
  1643. case OPJ_RLCP:
  1644. pi[pino].poc.precno0 = tcp->prcS;
  1645. pi[pino].poc.precno1 = tcp->prcE;
  1646. break;
  1647. default:
  1648. pi[pino].poc.tx0 = tcp->txS;
  1649. pi[pino].poc.ty0 = tcp->tyS;
  1650. pi[pino].poc.tx1 = tcp->txE;
  1651. pi[pino].poc.ty1 = tcp->tyE;
  1652. break;
  1653. }
  1654. break;
  1655. }
  1656. }
  1657. if (tpnum == 0) {
  1658. for (i = tppos; i >= 0; i--) {
  1659. switch (prog[i]) {
  1660. case 'C':
  1661. tcp->comp_t = tcp->compS;
  1662. pi[pino].poc.compno0 = tcp->comp_t;
  1663. pi[pino].poc.compno1 = tcp->comp_t + 1;
  1664. tcp->comp_t += 1;
  1665. break;
  1666. case 'R':
  1667. tcp->res_t = tcp->resS;
  1668. pi[pino].poc.resno0 = tcp->res_t;
  1669. pi[pino].poc.resno1 = tcp->res_t + 1;
  1670. tcp->res_t += 1;
  1671. break;
  1672. case 'L':
  1673. tcp->lay_t = tcp->layS;
  1674. pi[pino].poc.layno0 = tcp->lay_t;
  1675. pi[pino].poc.layno1 = tcp->lay_t + 1;
  1676. tcp->lay_t += 1;
  1677. break;
  1678. case 'P':
  1679. switch (tcp->prg) {
  1680. case OPJ_LRCP:
  1681. case OPJ_RLCP:
  1682. tcp->prc_t = tcp->prcS;
  1683. pi[pino].poc.precno0 = tcp->prc_t;
  1684. pi[pino].poc.precno1 = tcp->prc_t + 1;
  1685. tcp->prc_t += 1;
  1686. break;
  1687. default:
  1688. tcp->tx0_t = tcp->txS;
  1689. tcp->ty0_t = tcp->tyS;
  1690. pi[pino].poc.tx0 = tcp->tx0_t;
  1691. pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
  1692. pi[pino].poc.ty0 = tcp->ty0_t;
  1693. pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
  1694. tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
  1695. tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
  1696. break;
  1697. }
  1698. break;
  1699. }
  1700. }
  1701. incr_top = 1;
  1702. } else {
  1703. for (i = tppos; i >= 0; i--) {
  1704. switch (prog[i]) {
  1705. case 'C':
  1706. pi[pino].poc.compno0 = tcp->comp_t - 1;
  1707. pi[pino].poc.compno1 = tcp->comp_t;
  1708. break;
  1709. case 'R':
  1710. pi[pino].poc.resno0 = tcp->res_t - 1;
  1711. pi[pino].poc.resno1 = tcp->res_t;
  1712. break;
  1713. case 'L':
  1714. pi[pino].poc.layno0 = tcp->lay_t - 1;
  1715. pi[pino].poc.layno1 = tcp->lay_t;
  1716. break;
  1717. case 'P':
  1718. switch (tcp->prg) {
  1719. case OPJ_LRCP:
  1720. case OPJ_RLCP:
  1721. pi[pino].poc.precno0 = tcp->prc_t - 1;
  1722. pi[pino].poc.precno1 = tcp->prc_t;
  1723. break;
  1724. default:
  1725. pi[pino].poc.tx0 = tcp->tx0_t - tcp->dx - (tcp->tx0_t % tcp->dx);
  1726. pi[pino].poc.tx1 = tcp->tx0_t ;
  1727. pi[pino].poc.ty0 = tcp->ty0_t - tcp->dy - (tcp->ty0_t % tcp->dy);
  1728. pi[pino].poc.ty1 = tcp->ty0_t ;
  1729. break;
  1730. }
  1731. break;
  1732. }
  1733. if (incr_top == 1) {
  1734. switch (prog[i]) {
  1735. case 'R':
  1736. if (tcp->res_t == tcp->resE) {
  1737. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1738. tcp->res_t = tcp->resS;
  1739. pi[pino].poc.resno0 = tcp->res_t;
  1740. pi[pino].poc.resno1 = tcp->res_t + 1;
  1741. tcp->res_t += 1;
  1742. incr_top = 1;
  1743. } else {
  1744. incr_top = 0;
  1745. }
  1746. } else {
  1747. pi[pino].poc.resno0 = tcp->res_t;
  1748. pi[pino].poc.resno1 = tcp->res_t + 1;
  1749. tcp->res_t += 1;
  1750. incr_top = 0;
  1751. }
  1752. break;
  1753. case 'C':
  1754. if (tcp->comp_t == tcp->compE) {
  1755. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1756. tcp->comp_t = tcp->compS;
  1757. pi[pino].poc.compno0 = tcp->comp_t;
  1758. pi[pino].poc.compno1 = tcp->comp_t + 1;
  1759. tcp->comp_t += 1;
  1760. incr_top = 1;
  1761. } else {
  1762. incr_top = 0;
  1763. }
  1764. } else {
  1765. pi[pino].poc.compno0 = tcp->comp_t;
  1766. pi[pino].poc.compno1 = tcp->comp_t + 1;
  1767. tcp->comp_t += 1;
  1768. incr_top = 0;
  1769. }
  1770. break;
  1771. case 'L':
  1772. if (tcp->lay_t == tcp->layE) {
  1773. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1774. tcp->lay_t = tcp->layS;
  1775. pi[pino].poc.layno0 = tcp->lay_t;
  1776. pi[pino].poc.layno1 = tcp->lay_t + 1;
  1777. tcp->lay_t += 1;
  1778. incr_top = 1;
  1779. } else {
  1780. incr_top = 0;
  1781. }
  1782. } else {
  1783. pi[pino].poc.layno0 = tcp->lay_t;
  1784. pi[pino].poc.layno1 = tcp->lay_t + 1;
  1785. tcp->lay_t += 1;
  1786. incr_top = 0;
  1787. }
  1788. break;
  1789. case 'P':
  1790. switch (tcp->prg) {
  1791. case OPJ_LRCP:
  1792. case OPJ_RLCP:
  1793. if (tcp->prc_t == tcp->prcE) {
  1794. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1795. tcp->prc_t = tcp->prcS;
  1796. pi[pino].poc.precno0 = tcp->prc_t;
  1797. pi[pino].poc.precno1 = tcp->prc_t + 1;
  1798. tcp->prc_t += 1;
  1799. incr_top = 1;
  1800. } else {
  1801. incr_top = 0;
  1802. }
  1803. } else {
  1804. pi[pino].poc.precno0 = tcp->prc_t;
  1805. pi[pino].poc.precno1 = tcp->prc_t + 1;
  1806. tcp->prc_t += 1;
  1807. incr_top = 0;
  1808. }
  1809. break;
  1810. default:
  1811. if (tcp->tx0_t >= tcp->txE) {
  1812. if (tcp->ty0_t >= tcp->tyE) {
  1813. if (opj_pi_check_next_level(i - 1, cp, tileno, pino, prog)) {
  1814. tcp->ty0_t = tcp->tyS;
  1815. pi[pino].poc.ty0 = tcp->ty0_t;
  1816. pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
  1817. tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
  1818. incr_top = 1;
  1819. resetX = 1;
  1820. } else {
  1821. incr_top = 0;
  1822. resetX = 0;
  1823. }
  1824. } else {
  1825. pi[pino].poc.ty0 = tcp->ty0_t;
  1826. pi[pino].poc.ty1 = tcp->ty0_t + tcp->dy - (tcp->ty0_t % tcp->dy);
  1827. tcp->ty0_t = (OPJ_UINT32)pi[pino].poc.ty1;
  1828. incr_top = 0;
  1829. resetX = 1;
  1830. }
  1831. if (resetX == 1) {
  1832. tcp->tx0_t = tcp->txS;
  1833. pi[pino].poc.tx0 = tcp->tx0_t;
  1834. pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
  1835. tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
  1836. }
  1837. } else {
  1838. pi[pino].poc.tx0 = tcp->tx0_t;
  1839. pi[pino].poc.tx1 = tcp->tx0_t + tcp->dx - (tcp->tx0_t % tcp->dx);
  1840. tcp->tx0_t = (OPJ_UINT32)pi[pino].poc.tx1;
  1841. incr_top = 0;
  1842. }
  1843. break;
  1844. }
  1845. break;
  1846. }
  1847. }
  1848. }
  1849. }
  1850. }
  1851. }
  1852. void opj_pi_destroy(opj_pi_iterator_t *p_pi,
  1853. OPJ_UINT32 p_nb_elements)
  1854. {
  1855. OPJ_UINT32 compno, pino;
  1856. opj_pi_iterator_t *l_current_pi = p_pi;
  1857. if (p_pi) {
  1858. if (p_pi->include) {
  1859. opj_free(p_pi->include);
  1860. p_pi->include = 00;
  1861. }
  1862. for (pino = 0; pino < p_nb_elements; ++pino) {
  1863. if (l_current_pi->comps) {
  1864. opj_pi_comp_t *l_current_component = l_current_pi->comps;
  1865. for (compno = 0; compno < l_current_pi->numcomps; compno++) {
  1866. if (l_current_component->resolutions) {
  1867. opj_free(l_current_component->resolutions);
  1868. l_current_component->resolutions = 00;
  1869. }
  1870. ++l_current_component;
  1871. }
  1872. opj_free(l_current_pi->comps);
  1873. l_current_pi->comps = 0;
  1874. }
  1875. ++l_current_pi;
  1876. }
  1877. opj_free(p_pi);
  1878. }
  1879. }
  1880. void opj_pi_update_encoding_parameters(const opj_image_t *p_image,
  1881. opj_cp_t *p_cp,
  1882. OPJ_UINT32 p_tile_no)
  1883. {
  1884. /* encoding parameters to set */
  1885. OPJ_UINT32 l_max_res;
  1886. OPJ_UINT32 l_max_prec;
  1887. OPJ_UINT32 l_tx0, l_tx1, l_ty0, l_ty1;
  1888. OPJ_UINT32 l_dx_min, l_dy_min;
  1889. /* pointers */
  1890. opj_tcp_t *l_tcp = 00;
  1891. /* preconditions */
  1892. assert(p_cp != 00);
  1893. assert(p_image != 00);
  1894. assert(p_tile_no < p_cp->tw * p_cp->th);
  1895. l_tcp = &(p_cp->tcps[p_tile_no]);
  1896. /* get encoding parameters */
  1897. opj_get_encoding_parameters(p_image, p_cp, p_tile_no, &l_tx0, &l_tx1, &l_ty0,
  1898. &l_ty1, &l_dx_min, &l_dy_min, &l_max_prec, &l_max_res);
  1899. if (l_tcp->POC) {
  1900. opj_pi_update_encode_poc_and_final(p_cp, p_tile_no, l_tx0, l_tx1, l_ty0, l_ty1,
  1901. l_max_prec, l_max_res, l_dx_min, l_dy_min);
  1902. } else {
  1903. opj_pi_update_encode_not_poc(p_cp, p_image->numcomps, p_tile_no, l_tx0, l_tx1,
  1904. l_ty0, l_ty1, l_max_prec, l_max_res, l_dx_min, l_dy_min);
  1905. }
  1906. }
  1907. OPJ_BOOL opj_pi_next(opj_pi_iterator_t * pi)
  1908. {
  1909. switch (pi->poc.prg) {
  1910. case OPJ_LRCP:
  1911. return opj_pi_next_lrcp(pi);
  1912. case OPJ_RLCP:
  1913. return opj_pi_next_rlcp(pi);
  1914. case OPJ_RPCL:
  1915. return opj_pi_next_rpcl(pi);
  1916. case OPJ_PCRL:
  1917. return opj_pi_next_pcrl(pi);
  1918. case OPJ_CPRL:
  1919. return opj_pi_next_cprl(pi);
  1920. case OPJ_PROG_UNKNOWN:
  1921. return OPJ_FALSE;
  1922. }
  1923. return OPJ_FALSE;
  1924. }