j2k.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * Copyright (c) 2006-2007, Parvatha Elangovan
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "opj_includes.h"
  33. /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
  34. /*@{*/
  35. /** @name Local static functions */
  36. /*@{*/
  37. /**
  38. Write the SOC marker (Start Of Codestream)
  39. @param j2k J2K handle
  40. */
  41. static void j2k_write_soc(opj_j2k_t* j2k);
  42. /**
  43. Read the SOC marker (Start of Codestream)
  44. @param j2k J2K handle
  45. */
  46. static void j2k_read_soc(opj_j2k_t* j2k);
  47. /**
  48. Write the SIZ marker (image and tile size)
  49. @param j2k J2K handle
  50. */
  51. static void j2k_write_siz(opj_j2k_t* j2k);
  52. /**
  53. Read the SIZ marker (image and tile size)
  54. @param j2k J2K handle
  55. */
  56. static void j2k_read_siz(opj_j2k_t* j2k);
  57. /**
  58. Write the COM marker (comment)
  59. @param j2k J2K handle
  60. */
  61. static void j2k_write_com(opj_j2k_t* j2k);
  62. /**
  63. Read the COM marker (comment)
  64. @param j2k J2K handle
  65. */
  66. static void j2k_read_com(opj_j2k_t* j2k);
  67. /**
  68. Write the value concerning the specified component in the marker COD and COC
  69. @param j2k J2K handle
  70. @param compno Number of the component concerned by the information written
  71. */
  72. static void j2k_write_cox(opj_j2k_t* j2k, int compno);
  73. /**
  74. Read the value concerning the specified component in the marker COD and COC
  75. @param j2k J2K handle
  76. @param compno Number of the component concerned by the information read
  77. */
  78. static void j2k_read_cox(opj_j2k_t* j2k, int compno);
  79. /**
  80. Write the COD marker (coding style default)
  81. @param j2k J2K handle
  82. */
  83. static void j2k_write_cod(opj_j2k_t* j2k);
  84. /**
  85. Read the COD marker (coding style default)
  86. @param j2k J2K handle
  87. */
  88. static void j2k_read_cod(opj_j2k_t* j2k);
  89. /**
  90. Write the COC marker (coding style component)
  91. @param j2k J2K handle
  92. @param compno Number of the component concerned by the information written
  93. */
  94. static void j2k_write_coc(opj_j2k_t* j2k, int compno);
  95. /**
  96. Read the COC marker (coding style component)
  97. @param j2k J2K handle
  98. */
  99. static void j2k_read_coc(opj_j2k_t* j2k);
  100. /**
  101. Write the value concerning the specified component in the marker QCD and QCC
  102. @param j2k J2K handle
  103. @param compno Number of the component concerned by the information written
  104. */
  105. static void j2k_write_qcx(opj_j2k_t* j2k, int compno);
  106. /**
  107. Read the value concerning the specified component in the marker QCD and QCC
  108. @param j2k J2K handle
  109. @param compno Number of the component concern by the information read
  110. @param len Length of the information in the QCX part of the marker QCD/QCC
  111. */
  112. static void j2k_read_qcx(opj_j2k_t* j2k, int compno, int len);
  113. /**
  114. Write the QCD marker (quantization default)
  115. @param j2k J2K handle
  116. */
  117. static void j2k_write_qcd(opj_j2k_t* j2k);
  118. /**
  119. Read the QCD marker (quantization default)
  120. @param j2k J2K handle
  121. */
  122. static void j2k_read_qcd(opj_j2k_t* j2k);
  123. /**
  124. Write the QCC marker (quantization component)
  125. @param j2k J2K handle
  126. @param compno Number of the component concerned by the information written
  127. */
  128. static void j2k_write_qcc(opj_j2k_t* j2k, int compno);
  129. /**
  130. Read the QCC marker (quantization component)
  131. @param j2k J2K handle
  132. */
  133. static void j2k_read_qcc(opj_j2k_t* j2k);
  134. /**
  135. Write the POC marker (progression order change)
  136. @param j2k J2K handle
  137. */
  138. static void j2k_write_poc(opj_j2k_t* j2k);
  139. /**
  140. Read the POC marker (progression order change)
  141. @param j2k J2K handle
  142. */
  143. static void j2k_read_poc(opj_j2k_t* j2k);
  144. /**
  145. Read the CRG marker (component registration)
  146. @param j2k J2K handle
  147. */
  148. static void j2k_read_crg(opj_j2k_t* j2k);
  149. /**
  150. Read the TLM marker (tile-part lengths)
  151. @param j2k J2K handle
  152. */
  153. static void j2k_read_tlm(opj_j2k_t* j2k);
  154. /**
  155. Read the PLM marker (packet length, main header)
  156. @param j2k J2K handle
  157. */
  158. static void j2k_read_plm(opj_j2k_t* j2k);
  159. /**
  160. Read the PLT marker (packet length, tile-part header)
  161. @param j2k J2K handle
  162. */
  163. static void j2k_read_plt(opj_j2k_t* j2k);
  164. /**
  165. Read the PPM marker (packet packet headers, main header)
  166. @param j2k J2K handle
  167. */
  168. static void j2k_read_ppm(opj_j2k_t* j2k);
  169. /**
  170. Read the PPT marker (packet packet headers, tile-part header)
  171. @param j2k J2K handle
  172. */
  173. static void j2k_read_ppt(opj_j2k_t* j2k);
  174. /**
  175. Write the TLM marker (Mainheader)
  176. @param j2k J2K handle
  177. */
  178. static void j2k_write_tlm(opj_j2k_t* j2k);
  179. /**
  180. Write the SOT marker (start of tile-part)
  181. @param j2k J2K handle
  182. */
  183. static void j2k_write_sot(opj_j2k_t* j2k);
  184. /**
  185. Read the SOT marker (start of tile-part)
  186. @param j2k J2K handle
  187. */
  188. static void j2k_read_sot(opj_j2k_t* j2k);
  189. /**
  190. Write the SOD marker (start of data)
  191. @param j2k J2K handle
  192. @param tile_coder Pointer to a TCD handle
  193. */
  194. static void j2k_write_sod(opj_j2k_t* j2k, void* tile_coder);
  195. /**
  196. Read the SOD marker (start of data)
  197. @param j2k J2K handle
  198. */
  199. static void j2k_read_sod(opj_j2k_t* j2k);
  200. /**
  201. Write the RGN marker (region-of-interest)
  202. @param j2k J2K handle
  203. @param compno Number of the component concerned by the information written
  204. @param tileno Number of the tile concerned by the information written
  205. */
  206. static void j2k_write_rgn(opj_j2k_t* j2k, int compno, int tileno);
  207. /**
  208. Read the RGN marker (region-of-interest)
  209. @param j2k J2K handle
  210. */
  211. static void j2k_read_rgn(opj_j2k_t* j2k);
  212. /**
  213. Write the EOC marker (end of codestream)
  214. @param j2k J2K handle
  215. */
  216. static void j2k_write_eoc(opj_j2k_t* j2k);
  217. /**
  218. Read the EOC marker (end of codestream)
  219. @param j2k J2K handle
  220. */
  221. static void j2k_read_eoc(opj_j2k_t* j2k);
  222. /**
  223. Read an unknown marker
  224. @param j2k J2K handle
  225. */
  226. static void j2k_read_unk(opj_j2k_t* j2k);
  227. /*@}*/
  228. /*@}*/
  229. /* ----------------------------------------------------------------------- */
  230. typedef struct j2k_prog_order{
  231. OPJ_PROG_ORDER enum_prog;
  232. char str_prog[5];
  233. }j2k_prog_order_t;
  234. j2k_prog_order_t j2k_prog_order_list[] = {
  235. {CPRL, "CPRL"},
  236. {LRCP, "LRCP"},
  237. {PCRL, "PCRL"},
  238. {RLCP, "RLCP"},
  239. {RPCL, "RPCL"},
  240. {(OPJ_PROG_ORDER)-1, ""}
  241. };
  242. char* j2k_convert_progression_order(OPJ_PROG_ORDER prg_order) {
  243. j2k_prog_order_t* po;
  244. for (po = j2k_prog_order_list; po->enum_prog != -1; ++po) {
  245. if (po->enum_prog == prg_order) {
  246. break;
  247. }
  248. }
  249. return po->str_prog;
  250. }
  251. void j2k_dump_image(FILE* fd, opj_image_t* img) {
  252. int compno;
  253. fprintf(fd, "image {\n");
  254. fprintf(fd, " x0=%d, y0=%d, x1=%d, y1=%d\n", img->x0, img->y0, img->x1, img->y1);
  255. fprintf(fd, " numcomps=%d\n", img->numcomps);
  256. for (compno = 0; compno < img->numcomps; ++compno) {
  257. opj_image_comp_t *comp = &img->comps[compno];
  258. fprintf(fd, " comp %d {\n", compno);
  259. fprintf(fd, " dx=%d, dy=%d\n", comp->dx, comp->dy);
  260. fprintf(fd, " prec=%d\n", comp->prec);
  261. //fprintf(fd, " bpp=%d\n", comp->bpp);
  262. fprintf(fd, " sgnd=%d\n", comp->sgnd);
  263. fprintf(fd, " }\n");
  264. }
  265. fprintf(fd, "}\n");
  266. }
  267. void j2k_dump_cp(FILE* fd, opj_image_t* img, opj_cp_t* cp) {
  268. int tileno, compno, layno, bandno, resno, numbands;
  269. fprintf(fd, "coding parameters {\n");
  270. fprintf(fd, " tx0=%d, ty0=%d\n", cp->tx0, cp->ty0);
  271. fprintf(fd, " tdx=%d, tdy=%d\n", cp->tdx, cp->tdy);
  272. fprintf(fd, " tw=%d, th=%d\n", cp->tw, cp->th);
  273. for (tileno = 0; tileno < cp->tw * cp->th; ++tileno) {
  274. opj_tcp_t *tcp = &cp->tcps[tileno];
  275. fprintf(fd, " tile %d {\n", tileno);
  276. fprintf(fd, " csty=%x\n", tcp->csty);
  277. fprintf(fd, " prg=%d\n", tcp->prg);
  278. fprintf(fd, " numlayers=%d\n", tcp->numlayers);
  279. fprintf(fd, " mct=%d\n", tcp->mct);
  280. fprintf(fd, " rates=");
  281. for (layno = 0; layno < tcp->numlayers; ++layno) {
  282. fprintf(fd, "%.1f ", tcp->rates[layno]);
  283. }
  284. fprintf(fd, "\n");
  285. for (compno = 0; compno < img->numcomps; ++compno) {
  286. opj_tccp_t *tccp = &tcp->tccps[compno];
  287. fprintf(fd, " comp %d {\n", compno);
  288. fprintf(fd, " csty=%x\n", tccp->csty);
  289. fprintf(fd, " numresolutions=%d\n", tccp->numresolutions);
  290. fprintf(fd, " cblkw=%d\n", tccp->cblkw);
  291. fprintf(fd, " cblkh=%d\n", tccp->cblkh);
  292. fprintf(fd, " cblksty=%x\n", tccp->cblksty);
  293. fprintf(fd, " qmfbid=%d\n", tccp->qmfbid);
  294. fprintf(fd, " qntsty=%d\n", tccp->qntsty);
  295. fprintf(fd, " numgbits=%d\n", tccp->numgbits);
  296. fprintf(fd, " roishift=%d\n", tccp->roishift);
  297. fprintf(fd, " stepsizes=");
  298. numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
  299. for (bandno = 0; bandno < numbands; ++bandno) {
  300. fprintf(fd, "(%d,%d) ", tccp->stepsizes[bandno].mant,
  301. tccp->stepsizes[bandno].expn);
  302. }
  303. fprintf(fd, "\n");
  304. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  305. fprintf(fd, " prcw=");
  306. for (resno = 0; resno < tccp->numresolutions; ++resno) {
  307. fprintf(fd, "%d ", tccp->prcw[resno]);
  308. }
  309. fprintf(fd, "\n");
  310. fprintf(fd, " prch=");
  311. for (resno = 0; resno < tccp->numresolutions; ++resno) {
  312. fprintf(fd, "%d ", tccp->prch[resno]);
  313. }
  314. fprintf(fd, "\n");
  315. }
  316. fprintf(fd, " }\n");
  317. }
  318. fprintf(fd, " }\n");
  319. }
  320. fprintf(fd, "}\n");
  321. }
  322. /* ----------------------------------------------------------------------- */
  323. static int j2k_get_num_tp(opj_cp_t *cp,int pino,int tileno) {
  324. int tpnum = 1;
  325. opj_tcp_t* tcp = &cp->tcps[tileno];
  326. char* prog = j2k_convert_progression_order(tcp->prg);
  327. if (cp->tp_on == 1) {
  328. int i;
  329. int tpend = 0;
  330. for (i = 0; i < 4; ++i) {
  331. if (tpend != 1) {
  332. if (cp->tp_flag == prog[i]) {
  333. tpend = 1;
  334. cp->tp_pos = i;
  335. }
  336. switch (prog[i]) {
  337. case 'C':
  338. tpnum *= tcp->pocs[pino].compE;
  339. break;
  340. case 'R':
  341. tpnum *= tcp->pocs[pino].resE;
  342. break;
  343. case 'P':
  344. tpnum *= tcp->pocs[pino].prcE;
  345. break;
  346. case 'L':
  347. tpnum *= tcp->pocs[pino].layE;
  348. }
  349. }
  350. }
  351. }
  352. return tpnum;
  353. }
  354. /** mem allocation for TLM marker*/
  355. int j2k_calculate_tp(opj_cp_t* cp, int img_numcomp, opj_image_t* image, opj_j2k_t* j2k ) {
  356. j2k->cur_totnum_tp = (int*)opj_malloc(cp->tw * cp->th * sizeof(int));
  357. if (!j2k->cur_totnum_tp) {
  358. /* Memory allocation failure... */
  359. return 0;
  360. }
  361. int totnum_tp = 0;
  362. int pino, tileno;
  363. for (tileno = 0; tileno < cp->tw * cp->th; ++tileno) {
  364. int cur_totnum_tp = 0;
  365. opj_tcp_t* tcp = &cp->tcps[tileno];
  366. for (pino = 0; pino <= tcp->numpocs; ++pino) {
  367. int tp_num=0;
  368. opj_pi_iterator_t* pi = pi_initialise_encode(image, cp, tileno, FINAL_PASS);
  369. if (!pi) {
  370. return -1;
  371. }
  372. tp_num = j2k_get_num_tp(cp,pino,tileno);
  373. totnum_tp = totnum_tp + tp_num;
  374. cur_totnum_tp = cur_totnum_tp + tp_num;
  375. pi_destroy(pi, cp, tileno);
  376. }
  377. j2k->cur_totnum_tp[tileno] = cur_totnum_tp;
  378. /* INDEX >> */
  379. if (j2k->cstr_info) {
  380. j2k->cstr_info->tile[tileno].num_tps = cur_totnum_tp;
  381. j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t*)opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
  382. if (!j2k->cstr_info->tile[tileno].tp) {
  383. /* Memory allocation failure... */
  384. return 0;
  385. }
  386. }
  387. /* << INDEX */
  388. }
  389. return totnum_tp;
  390. }
  391. static void j2k_write_soc(opj_j2k_t* j2k) {
  392. opj_cio_t* cio = j2k->cio;
  393. cio_write(cio, J2K_MS_SOC, 2);
  394. /* UniPG>> */
  395. #ifdef USE_JPWL
  396. /* update markers struct */
  397. j2k_add_marker(j2k->cstr_info, J2K_MS_SOC, cio_tell(cio) - 2, 2);
  398. #endif /* USE_JPWL */
  399. /* <<UniPG */
  400. }
  401. static void j2k_read_soc(opj_j2k_t* j2k) {
  402. j2k->state = J2K_STATE_MHSIZ;
  403. /* Index */
  404. if (j2k->cstr_info) {
  405. j2k->cstr_info->main_head_start = cio_tell(j2k->cio) - 2;
  406. j2k->cstr_info->codestream_size = cio_numbytesleft(j2k->cio) + 2 - j2k->cstr_info->main_head_start;
  407. }
  408. }
  409. static void j2k_write_siz(opj_j2k_t* j2k) {
  410. opj_cio_t* cio = j2k->cio;
  411. opj_image_t* image = j2k->image;
  412. opj_cp_t* cp = j2k->cp;
  413. cio_write(cio, J2K_MS_SIZ, 2); /* SIZ */
  414. int lenp = cio_tell(cio);
  415. cio_skip(cio, 2);
  416. cio_write(cio, cp->rsiz, 2); /* Rsiz (capabilities) */
  417. cio_write(cio, image->x1, 4); /* Xsiz */
  418. cio_write(cio, image->y1, 4); /* Ysiz */
  419. cio_write(cio, image->x0, 4); /* X0siz */
  420. cio_write(cio, image->y0, 4); /* Y0siz */
  421. cio_write(cio, cp->tdx, 4); /* XTsiz */
  422. cio_write(cio, cp->tdy, 4); /* YTsiz */
  423. cio_write(cio, cp->tx0, 4); /* XT0siz */
  424. cio_write(cio, cp->ty0, 4); /* YT0siz */
  425. cio_write(cio, image->numcomps, 2); /* Csiz */
  426. int i;
  427. for (i = 0; i < image->numcomps; ++i) {
  428. cio_write(cio, image->comps[i].prec - 1 + (image->comps[i].sgnd << 7), 1); /* Ssiz_i */
  429. cio_write(cio, image->comps[i].dx, 1); /* XRsiz_i */
  430. cio_write(cio, image->comps[i].dy, 1); /* YRsiz_i */
  431. }
  432. int len = cio_tell(cio) - lenp;
  433. cio_seek(cio, lenp);
  434. cio_write(cio, len, 2); /* Lsiz */
  435. cio_seek(cio, lenp + len);
  436. }
  437. static void j2k_read_siz(opj_j2k_t* j2k) {
  438. opj_cio_t* cio = j2k->cio;
  439. opj_image_t* image = j2k->image;
  440. opj_cp_t* cp = j2k->cp;
  441. cio_read(cio, 2); /* Lsiz */
  442. cio_read(cio, 2); /* Rsiz (capabilities) */
  443. image->x1 = cio_read(cio, 4); /* Xsiz */
  444. image->y1 = cio_read(cio, 4); /* Ysiz */
  445. image->x0 = cio_read(cio, 4); /* X0siz */
  446. image->y0 = cio_read(cio, 4); /* Y0siz */
  447. cp->tdx = cio_read(cio, 4); /* XTsiz */
  448. cp->tdy = cio_read(cio, 4); /* YTsiz */
  449. cp->tx0 = cio_read(cio, 4); /* XT0siz */
  450. cp->ty0 = cio_read(cio, 4); /* YT0siz */
  451. if (image->x0 < 0 || image->x1 < 0 || image->y0 < 0 || image->y1 < 0) {
  452. opj_event_msg(j2k->cinfo, EVT_ERROR,
  453. "invalid image size (x0:%d, x1:%d, y0:%d, y1:%d)\n",
  454. image->x0, image->x1, image->y0, image->y1);
  455. return;
  456. }
  457. image->numcomps = cio_read(cio, 2); /* Csiz */
  458. #ifdef USE_JPWL
  459. if (j2k->cp->correct) {
  460. /* if JPWL is on, we check whether TX errors have damaged
  461. too much the SIZ parameters */
  462. if (!(image->x1 * image->y1)) {
  463. opj_event_msg(j2k->cinfo, EVT_ERROR,
  464. "JPWL: bad image size (%d x %d)\n",
  465. image->x1, image->y1);
  466. if (!JPWL_ASSUME || JPWL_ASSUME) {
  467. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  468. return;
  469. }
  470. }
  471. if (image->numcomps != ((len - 38) / 3)) {
  472. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  473. "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
  474. image->numcomps, ((len - 38) / 3));
  475. if (!JPWL_ASSUME) {
  476. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  477. return;
  478. }
  479. /* we try to correct */
  480. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n");
  481. if (image->numcomps < ((len - 38) / 3)) {
  482. len = 38 + 3 * image->numcomps;
  483. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
  484. len);
  485. } else {
  486. image->numcomps = ((len - 38) / 3);
  487. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
  488. image->numcomps);
  489. }
  490. }
  491. /* update components number in the jpwl_exp_comps filed */
  492. cp->exp_comps = image->numcomps;
  493. }
  494. #endif /* USE_JPWL */
  495. image->comps = (opj_image_comp_t*) opj_calloc(image->numcomps, sizeof(opj_image_comp_t));
  496. if (!image->comps) {
  497. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  498. j2k->state |= J2K_STATE_ERR;
  499. return;
  500. }
  501. int i;
  502. for (i = 0; i < image->numcomps; ++i) {
  503. int tmp = cio_read(cio, 1); /* Ssiz_i */
  504. image->comps[i].prec = (tmp & 0x7f) + 1;
  505. image->comps[i].sgnd = tmp >> 7;
  506. image->comps[i].dx = cio_read(cio, 1); /* XRsiz_i */
  507. image->comps[i].dy = cio_read(cio, 1); /* YRsiz_i */
  508. #ifdef USE_JPWL
  509. if (j2k->cp->correct) {
  510. /* if JPWL is on, we check whether TX errors have damaged
  511. too much the SIZ parameters, again */
  512. if (!(image->comps[i].dx * image->comps[i].dy)) {
  513. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  514. "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
  515. i, i, image->comps[i].dx, image->comps[i].dy);
  516. if (!JPWL_ASSUME) {
  517. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  518. return;
  519. }
  520. /* we try to correct */
  521. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
  522. if (!image->comps[i].dx) {
  523. image->comps[i].dx = 1;
  524. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
  525. i, image->comps[i].dx);
  526. }
  527. if (!image->comps[i].dy) {
  528. image->comps[i].dy = 1;
  529. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
  530. i, image->comps[i].dy);
  531. }
  532. }
  533. }
  534. #endif /* USE_JPWL */
  535. if (image->comps[i].dx * image->comps[i].dy == 0) {
  536. opj_event_msg(j2k->cinfo, EVT_ERROR,
  537. "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
  538. i, i, image->comps[i].dx, image->comps[i].dy);
  539. return;
  540. }
  541. image->comps[i].resno_decoded = 0; /* number of resolution decoded */
  542. image->comps[i].factor = cp->reduce; /* reducing factor per component */
  543. }
  544. cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
  545. cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
  546. #ifdef USE_JPWL
  547. if (j2k->cp->correct) {
  548. /* if JPWL is on, we check whether TX errors have damaged
  549. too much the SIZ parameters */
  550. if (cp->tw < 1 || cp->th < 1 || cp->tw > cp->max_tiles || cp->th > cp->max_tiles) {
  551. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  552. "JPWL: bad number of tiles (%d x %d)\n",
  553. cp->tw, cp->th);
  554. if (!JPWL_ASSUME) {
  555. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  556. return;
  557. }
  558. /* we try to correct */
  559. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n");
  560. if (cp->tw < 1) {
  561. cp->tw= 1;
  562. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in x => HYPOTHESIS!!!\n",
  563. cp->tw);
  564. }
  565. if (cp->tw > cp->max_tiles) {
  566. cp->tw= 1;
  567. opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large x, increase expectance of %d\n"
  568. "- setting %d tiles in x => HYPOTHESIS!!!\n",
  569. cp->max_tiles, cp->tw);
  570. }
  571. if (cp->th < 1) {
  572. cp->th= 1;
  573. opj_event_msg(j2k->cinfo, EVT_WARNING, "- setting %d tiles in y => HYPOTHESIS!!!\n",
  574. cp->th);
  575. }
  576. if (cp->th > cp->max_tiles) {
  577. cp->th= 1;
  578. opj_event_msg(j2k->cinfo, EVT_WARNING, "- too large y, increase expectance of %d to continue\n",
  579. "- setting %d tiles in y => HYPOTHESIS!!!\n",
  580. cp->max_tiles, cp->th);
  581. }
  582. }
  583. }
  584. #endif /* USE_JPWL */
  585. cp->tcps = (opj_tcp_t*)opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
  586. cp->tileno = (int*)opj_malloc(cp->tw * cp->th * sizeof(int));
  587. cp->tileno_size = 0;
  588. if (!cp->tcps || !cp->tileno) {
  589. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  590. j2k->state |= J2K_STATE_ERR;
  591. return;
  592. }
  593. #ifdef USE_JPWL
  594. if (j2k->cp->correct) {
  595. if (!cp->tcps) {
  596. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  597. "JPWL: could not alloc tcps field of cp\n");
  598. if (!JPWL_ASSUME || JPWL_ASSUME) {
  599. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  600. return;
  601. }
  602. }
  603. }
  604. #endif /* USE_JPWL */
  605. int cnt = cp->tw * cp->th;
  606. for (i = 0; i < cnt; ++i) {
  607. cp->tcps[i].POC = 0;
  608. cp->tcps[i].numpocs = 0;
  609. cp->tcps[i].first = 1;
  610. }
  611. /* Initialization for PPM marker */
  612. cp->ppm = 0;
  613. cp->ppm_data = NULL;
  614. cp->ppm_data_first = NULL;
  615. cp->ppm_previous = 0;
  616. cp->ppm_store = 0;
  617. j2k->default_tcp->tccps = (opj_tccp_t*)opj_calloc(image->numcomps, sizeof(opj_tccp_t));
  618. if (!j2k->default_tcp->tccps) {
  619. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  620. j2k->state |= J2K_STATE_ERR;
  621. return;
  622. }
  623. for (i = 0; i < cnt; ++i) {
  624. cp->tcps[i].tccps = (opj_tccp_t*)opj_malloc(image->numcomps * sizeof(opj_tccp_t));
  625. if (!cp->tcps[i].tccps) {
  626. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  627. j2k->state |= J2K_STATE_ERR;
  628. return;
  629. }
  630. }
  631. j2k->tile_data = (unsigned char**)opj_calloc(cnt, sizeof(unsigned char*));
  632. j2k->tile_len = (int*)opj_calloc(cnt, sizeof(int));
  633. if (!j2k->tile_data || !j2k->tile_len) {
  634. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  635. j2k->state |= J2K_STATE_ERR;
  636. return;
  637. }
  638. j2k->state = J2K_STATE_MH;
  639. /* Index */
  640. if (j2k->cstr_info) {
  641. opj_codestream_info_t* cstr_info = j2k->cstr_info;
  642. cstr_info->image_w = image->x1 - image->x0;
  643. cstr_info->image_h = image->y1 - image->y0;
  644. cstr_info->numcomps = image->numcomps;
  645. cstr_info->tw = cp->tw;
  646. cstr_info->th = cp->th;
  647. cstr_info->tile_x = cp->tdx;
  648. cstr_info->tile_y = cp->tdy;
  649. cstr_info->tile_Ox = cp->tx0;
  650. cstr_info->tile_Oy = cp->ty0;
  651. cstr_info->tile = (opj_tile_info_t*)opj_calloc(cnt, sizeof(opj_tile_info_t));
  652. if (!cstr_info->tile) {
  653. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  654. j2k->state |= J2K_STATE_ERR;
  655. }
  656. }
  657. }
  658. static void j2k_write_com(opj_j2k_t* j2k) {
  659. if (j2k->cp->comment) {
  660. opj_cio_t* cio = j2k->cio;
  661. char* comment = j2k->cp->comment;
  662. cio_write(cio, J2K_MS_COM, 2);
  663. int lenp = cio_tell(cio);
  664. cio_skip(cio, 2);
  665. cio_write(cio, 1, 2); /* General use (IS 8859-15:1999 (Latin) values) */
  666. unsigned int i;
  667. for (i = 0; i < strlen(comment); ++i) {
  668. cio_write(cio, comment[i], 1);
  669. }
  670. int len = cio_tell(cio) - lenp;
  671. cio_seek(cio, lenp);
  672. cio_write(cio, len, 2);
  673. cio_seek(cio, lenp + len);
  674. }
  675. }
  676. static void j2k_read_com(opj_j2k_t* j2k) {
  677. opj_cio_t* cio = j2k->cio;
  678. int len = cio_read(cio, 2);
  679. cio_skip(cio, len - 2);
  680. }
  681. static void j2k_write_cox(opj_j2k_t* j2k, int compno) {
  682. opj_cp_t* cp = j2k->cp;
  683. opj_tcp_t* tcp = &cp->tcps[j2k->curtileno];
  684. opj_tccp_t* tccp = &tcp->tccps[compno];
  685. opj_cio_t* cio = j2k->cio;
  686. cio_write(cio, tccp->numresolutions - 1, 1); /* SPcox (D) */
  687. cio_write(cio, tccp->cblkw - 2, 1); /* SPcox (E) */
  688. cio_write(cio, tccp->cblkh - 2, 1); /* SPcox (F) */
  689. cio_write(cio, tccp->cblksty, 1); /* SPcox (G) */
  690. cio_write(cio, tccp->qmfbid, 1); /* SPcox (H) */
  691. if (tccp->csty & J2K_CCP_CSTY_PRT) {
  692. int i;
  693. for (i = 0; i < tccp->numresolutions; ++i) {
  694. cio_write(cio, tccp->prcw[i] + (tccp->prch[i] << 4), 1); /* SPcox (I_i) */
  695. }
  696. }
  697. }
  698. static void j2k_read_cox(opj_j2k_t* j2k, int compno) {
  699. opj_cp_t* cp = j2k->cp;
  700. opj_tcp_t* tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  701. opj_tccp_t* tccp = &tcp->tccps[compno];
  702. opj_cio_t* cio = j2k->cio;
  703. tccp->numresolutions = cio_read(cio, 1) + 1; /* SPcox (D) */
  704. // If user wants to remove more resolutions than the codestream contains, return error
  705. if (cp->reduce >= tccp->numresolutions) {
  706. opj_event_msg(j2k->cinfo, EVT_ERROR, "Error decoding component %d.\nThe number of resolutions to remove is higher than the number "
  707. "of resolutions of this component\nModify the cp_reduce parameter.\n\n", compno);
  708. j2k->state |= J2K_STATE_ERR;
  709. }
  710. tccp->cblkw = cio_read(cio, 1) + 2; /* SPcox (E) */
  711. tccp->cblkh = cio_read(cio, 1) + 2; /* SPcox (F) */
  712. tccp->cblksty = cio_read(cio, 1); /* SPcox (G) */
  713. tccp->qmfbid = cio_read(cio, 1); /* SPcox (H) */
  714. if (tccp->csty & J2K_CP_CSTY_PRT) {
  715. int i;
  716. for (i = 0; i < tccp->numresolutions; ++i) {
  717. int tmp = cio_read(cio, 1); /* SPcox (I_i) */
  718. tccp->prcw[i] = tmp & 0xf;
  719. tccp->prch[i] = tmp >> 4;
  720. }
  721. }
  722. /* INDEX >> */
  723. if (j2k->cstr_info && compno == 0) {
  724. int i;
  725. for (i = 0; i < tccp->numresolutions; ++i) {
  726. if (tccp->csty & J2K_CP_CSTY_PRT) {
  727. j2k->cstr_info->tile[j2k->curtileno].pdx[i] = tccp->prcw[i];
  728. j2k->cstr_info->tile[j2k->curtileno].pdy[i] = tccp->prch[i];
  729. }
  730. else {
  731. j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
  732. j2k->cstr_info->tile[j2k->curtileno].pdx[i] = 15;
  733. }
  734. }
  735. }
  736. /* << INDEX */
  737. }
  738. static void j2k_write_cod(opj_j2k_t* j2k) {
  739. opj_cio_t* cio = j2k->cio;
  740. cio_write(cio, J2K_MS_COD, 2); /* COD */
  741. int lenp = cio_tell(cio);
  742. cio_skip(cio, 2);
  743. opj_cp_t* cp = j2k->cp;
  744. opj_tcp_t* tcp = &cp->tcps[j2k->curtileno];
  745. cio_write(cio, tcp->csty, 1); /* Scod */
  746. cio_write(cio, tcp->prg, 1); /* SGcod (A) */
  747. cio_write(cio, tcp->numlayers, 2); /* SGcod (B) */
  748. cio_write(cio, tcp->mct, 1); /* SGcod (C) */
  749. j2k_write_cox(j2k, 0);
  750. int len = cio_tell(cio) - lenp;
  751. cio_seek(cio, lenp);
  752. cio_write(cio, len, 2); /* Lcod */
  753. cio_seek(cio, lenp + len);
  754. }
  755. static void j2k_read_cod(opj_j2k_t* j2k) {
  756. opj_cio_t* cio = j2k->cio;
  757. opj_cp_t* cp = j2k->cp;
  758. opj_tcp_t* tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  759. opj_image_t* image = j2k->image;
  760. cio_read(cio, 2); /* Lcod */
  761. tcp->csty = cio_read(cio, 1); /* Scod */
  762. tcp->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* SGcod (A) */
  763. tcp->numlayers = cio_read(cio, 2); /* SGcod (B) */
  764. tcp->mct = cio_read(cio, 1); /* SGcod (C) */
  765. int pos = cio_tell(cio);
  766. int i;
  767. for (i = 0; i < image->numcomps; ++i) {
  768. tcp->tccps[i].csty = tcp->csty & J2K_CP_CSTY_PRT;
  769. cio_seek(cio, pos);
  770. j2k_read_cox(j2k, i);
  771. }
  772. /* Index */
  773. if (j2k->cstr_info) {
  774. opj_codestream_info_t* cstr_info = j2k->cstr_info;
  775. cstr_info->prog = tcp->prg;
  776. cstr_info->numlayers = tcp->numlayers;
  777. cstr_info->numdecompos = (int*)opj_malloc(image->numcomps * sizeof(int));
  778. if (!cstr_info->numdecompos) {
  779. /* Memory allocation failure... */
  780. return;
  781. }
  782. for (i = 0; i < image->numcomps; ++i) {
  783. cstr_info->numdecompos[i] = tcp->tccps[i].numresolutions - 1;
  784. }
  785. }
  786. }
  787. static void j2k_write_coc(opj_j2k_t* j2k, int compno) {
  788. opj_cp_t* cp = j2k->cp;
  789. opj_tcp_t* tcp = &cp->tcps[j2k->curtileno];
  790. opj_image_t* image = j2k->image;
  791. opj_cio_t* cio = j2k->cio;
  792. cio_write(cio, J2K_MS_COC, 2); /* COC */
  793. int lenp = cio_tell(cio);
  794. cio_skip(cio, 2);
  795. cio_write(cio, compno, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
  796. cio_write(cio, tcp->tccps[compno].csty, 1); /* Scoc */
  797. j2k_write_cox(j2k, compno);
  798. int len = cio_tell(cio) - lenp;
  799. cio_seek(cio, lenp);
  800. cio_write(cio, len, 2); /* Lcoc */
  801. cio_seek(cio, lenp + len);
  802. }
  803. static void j2k_read_coc(opj_j2k_t* j2k) {
  804. opj_cp_t* cp = j2k->cp;
  805. opj_tcp_t* tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  806. opj_image_t* image = j2k->image;
  807. opj_cio_t* cio = j2k->cio;
  808. cio_read(cio, 2); /* Lcoc */
  809. int compno = cio_read(cio, image->numcomps <= 256 ? 1 : 2); /* Ccoc */
  810. if (compno < 0 || compno >= image->numcomps) {
  811. opj_event_msg(j2k->cinfo, EVT_ERROR ,
  812. "bad component number in COC (%d out of a maximum of %d)\n",
  813. compno, image->numcomps);
  814. } else {
  815. tcp->tccps[compno].csty = cio_read(cio, 1); /* Scoc */
  816. j2k_read_cox(j2k, compno);
  817. }
  818. }
  819. static void j2k_write_qcx(opj_j2k_t* j2k, int compno) {
  820. opj_cp_t* cp = j2k->cp;
  821. opj_tcp_t* tcp = &cp->tcps[j2k->curtileno];
  822. opj_tccp_t* tccp = &tcp->tccps[compno];
  823. opj_cio_t* cio = j2k->cio;
  824. cio_write(cio, tccp->qntsty + (tccp->numgbits << 5), 1); /* Sqcx */
  825. int numbands = tccp->qntsty == J2K_CCP_QNTSTY_SIQNT ? 1 : tccp->numresolutions * 3 - 2;
  826. int bandno;
  827. for (bandno = 0; bandno < numbands; ++bandno) {
  828. int expn = tccp->stepsizes[bandno].expn;
  829. int mant = tccp->stepsizes[bandno].mant;
  830. if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  831. cio_write(cio, expn << 3, 1); /* SPqcx_i */
  832. } else {
  833. cio_write(cio, (expn << 11) + mant, 2); /* SPqcx_i */
  834. }
  835. }
  836. }
  837. static void j2k_read_qcx(opj_j2k_t* j2k, int compno, int len) {
  838. opj_cp_t* cp = j2k->cp;
  839. opj_tcp_t* tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  840. opj_tccp_t* tccp = &tcp->tccps[compno];
  841. opj_cio_t* cio = j2k->cio;
  842. int tmp = cio_read(cio, 1); /* Sqcx */
  843. tccp->qntsty = tmp & 0x1f;
  844. tccp->numgbits = tmp >> 5;
  845. int numbands = (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ?
  846. 1 : ((tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ? len - 1 : (len - 1) / 2);
  847. #ifdef USE_JPWL
  848. if (j2k->cp->correct) {
  849. /* if JPWL is on, we check whether there are too many subbands */
  850. if ((numbands < 0) || (numbands >= J2K_MAXBANDS)) {
  851. opj_event_msg(j2k->cinfo, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  852. "JPWL: bad number of subbands in Sqcx (%d)\n",
  853. numbands);
  854. if (!JPWL_ASSUME) {
  855. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  856. return;
  857. }
  858. /* we try to correct */
  859. numbands = 1;
  860. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust them\n"
  861. "- setting number of bands to %d => HYPOTHESIS!!!\n",
  862. numbands);
  863. };
  864. };
  865. #else
  866. /* We check whether there are too many subbands */
  867. if (numbands < 0 || numbands >= J2K_MAXBANDS) {
  868. opj_event_msg(j2k->cinfo, EVT_WARNING,
  869. "bad number of subbands in Sqcx (%d) regarding to J2K_MAXBANDS (%d) \n"
  870. "- limiting number of bands to J2K_MAXBANDS and try to move to the next markers\n",
  871. numbands, J2K_MAXBANDS);
  872. }
  873. #endif /* USE_JPWL */
  874. int bandno;
  875. for (bandno = 0; bandno < numbands; ++bandno) {
  876. int expn, mant;
  877. if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  878. expn = cio_read(cio, 1) >> 3; /* SPqcx_i */
  879. mant = 0;
  880. } else {
  881. tmp = cio_read(cio, 2); /* SPqcx_i */
  882. expn = tmp >> 11;
  883. mant = tmp & 0x7ff;
  884. }
  885. if (bandno < J2K_MAXBANDS) {
  886. tccp->stepsizes[bandno].expn = expn;
  887. tccp->stepsizes[bandno].mant = mant;
  888. }
  889. }
  890. /* Add Antonin : if scalar_derived -> compute other stepsizes */
  891. if (tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
  892. for (bandno = 1; bandno < J2K_MAXBANDS; ++bandno) {
  893. tccp->stepsizes[bandno].expn =
  894. ((tccp->stepsizes[0].expn) - ((bandno - 1) / 3) > 0) ?
  895. (tccp->stepsizes[0].expn) - ((bandno - 1) / 3) : 0;
  896. tccp->stepsizes[bandno].mant = tccp->stepsizes[0].mant;
  897. }
  898. }
  899. /* ddA */
  900. }
  901. static void j2k_write_qcd(opj_j2k_t* j2k) {
  902. opj_cio_t* cio = j2k->cio;
  903. cio_write(cio, J2K_MS_QCD, 2); /* QCD */
  904. int lenp = cio_tell(cio);
  905. cio_skip(cio, 2);
  906. j2k_write_qcx(j2k, 0);
  907. int len = cio_tell(cio) - lenp;
  908. cio_seek(cio, lenp);
  909. cio_write(cio, len, 2); /* Lqcd */
  910. cio_seek(cio, lenp + len);
  911. }
  912. static void j2k_read_qcd(opj_j2k_t* j2k) {
  913. opj_cio_t* cio = j2k->cio;
  914. opj_image_t* image = j2k->image;
  915. int len = cio_read(cio, 2); /* Lqcd */
  916. int pos = cio_tell(cio);
  917. int i;
  918. for (i = 0; i < image->numcomps; ++i) {
  919. cio_seek(cio, pos);
  920. j2k_read_qcx(j2k, i, len - 2);
  921. }
  922. }
  923. static void j2k_write_qcc(opj_j2k_t* j2k, int compno) {
  924. opj_cio_t* cio = j2k->cio;
  925. cio_write(cio, J2K_MS_QCC, 2); /* QCC */
  926. int lenp = cio_tell(cio);
  927. cio_skip(cio, 2);
  928. cio_write(cio, compno, j2k->image->numcomps <= 256 ? 1 : 2); /* Cqcc */
  929. j2k_write_qcx(j2k, compno);
  930. int len = cio_tell(cio) - lenp;
  931. cio_seek(cio, lenp);
  932. cio_write(cio, len, 2); /* Lqcc */
  933. cio_seek(cio, lenp + len);
  934. }
  935. static void j2k_read_qcc(opj_j2k_t* j2k) {
  936. int len, compno;
  937. int numcomp = j2k->image->numcomps;
  938. opj_cio_t *cio = j2k->cio;
  939. len = cio_read(cio, 2); /* Lqcc */
  940. compno = cio_read(cio, numcomp <= 256 ? 1 : 2); /* Cqcc */
  941. #ifdef USE_JPWL
  942. if (j2k->cp->correct) {
  943. static int backup_compno = 0;
  944. /* compno is negative or larger than the number of components!!! */
  945. if (compno < 0 || compno >= numcomp) {
  946. opj_event_msg(j2k->cinfo, EVT_ERROR,
  947. "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
  948. compno, numcomp);
  949. if (!JPWL_ASSUME) {
  950. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  951. return;
  952. }
  953. /* we try to correct */
  954. compno = backup_compno % numcomp;
  955. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
  956. "- setting component number to %d\n",
  957. compno);
  958. }
  959. /* keep your private count of tiles */
  960. ++backup_compno;
  961. }
  962. else
  963. #endif /* USE_JPWL */
  964. {
  965. /* compno is negative or larger than the number of components!!! */
  966. if (compno < 0 || compno >= numcomp) {
  967. opj_event_msg(j2k->cinfo, EVT_ERROR,
  968. "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
  969. compno, numcomp);
  970. return;
  971. }
  972. }
  973. j2k_read_qcx(j2k, compno, len - 2 - (numcomp <= 256 ? 1 : 2));
  974. }
  975. static void j2k_write_poc(opj_j2k_t* j2k) {
  976. int numcomps = j2k->image->numcomps;
  977. opj_cp_t* cp = j2k->cp;
  978. opj_tcp_t* tcp = &cp->tcps[j2k->curtileno];
  979. opj_tccp_t* tccp = &tcp->tccps[0];
  980. opj_cio_t* cio = j2k->cio;
  981. int numpchgs = 1 + tcp->numpocs;
  982. cio_write(cio, J2K_MS_POC, 2); /* POC */
  983. int len = 2 + (5 + 2 * (numcomps <= 256 ? 1 : 2)) * numpchgs;
  984. cio_write(cio, len, 2); /* Lpoc */
  985. int i;
  986. for (i = 0; i < numpchgs; ++i) {
  987. opj_poc_t *poc = &tcp->pocs[i];
  988. cio_write(cio, poc->resno0, 1); /* RSpoc_i */
  989. cio_write(cio, poc->compno0, (numcomps <= 256 ? 1 : 2)); /* CSpoc_i */
  990. cio_write(cio, poc->layno1, 2); /* LYEpoc_i */
  991. poc->layno1 = int_min(poc->layno1, tcp->numlayers);
  992. cio_write(cio, poc->resno1, 1); /* REpoc_i */
  993. poc->resno1 = int_min(poc->resno1, tccp->numresolutions);
  994. cio_write(cio, poc->compno1, (numcomps <= 256 ? 1 : 2)); /* CEpoc_i */
  995. poc->compno1 = int_min(poc->compno1, numcomps);
  996. cio_write(cio, poc->prg, 1); /* Ppoc_i */
  997. }
  998. }
  999. static void j2k_read_poc(opj_j2k_t* j2k) {
  1000. int numcomps = j2k->image->numcomps;
  1001. opj_cp_t* cp = j2k->cp;
  1002. opj_tcp_t* tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  1003. opj_cio_t* cio = j2k->cio;
  1004. int old_poc = tcp->POC ? tcp->numpocs + 1 : 0;
  1005. tcp->POC = 1;
  1006. int len = cio_read(cio, 2); /* Lpoc */
  1007. int numpchgs = (len - 2) / (5 + 2 * (numcomps <= 256 ? 1 : 2));
  1008. /* old_poc < 0 "just in case" */
  1009. int maxpocs = sizeof(tcp->pocs) / sizeof(tcp->pocs[0]);
  1010. if (old_poc < 0 || numpchgs + old_poc >= maxpocs) {
  1011. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1012. "JPWL: bad number of progression order changes (%d out of a maximum of %d)\n",
  1013. numpchgs + old_poc, maxpocs);
  1014. return;
  1015. }
  1016. int i;
  1017. for (i = old_poc; i < numpchgs + old_poc; ++i) {
  1018. opj_poc_t *poc;
  1019. poc = &tcp->pocs[i];
  1020. poc->resno0 = cio_read(cio, 1); /* RSpoc_i */
  1021. poc->compno0 = cio_read(cio, numcomps <= 256 ? 1 : 2); /* CSpoc_i */
  1022. poc->layno1 = cio_read(cio, 2); /* LYEpoc_i */
  1023. poc->resno1 = cio_read(cio, 1); /* REpoc_i */
  1024. poc->compno1 = int_min(
  1025. cio_read(cio, numcomps <= 256 ? 1 : 2), (unsigned int) numcomps); /* CEpoc_i */
  1026. poc->prg = (OPJ_PROG_ORDER)cio_read(cio, 1); /* Ppoc_i */
  1027. }
  1028. tcp->numpocs = numpchgs + old_poc - 1;
  1029. }
  1030. static void j2k_read_crg(opj_j2k_t* j2k) {
  1031. opj_cio_t* cio = j2k->cio;
  1032. cio_read(cio, 2); /* Lcrg */
  1033. int i;
  1034. int numcomps = j2k->image->numcomps;
  1035. for (i = 0; i < numcomps; ++i) {
  1036. cio_read(cio, 2); /* Xcrg_i */
  1037. cio_read(cio, 2); /* Ycrg_i */
  1038. }
  1039. }
  1040. static void j2k_read_tlm(opj_j2k_t* j2k) {
  1041. opj_cio_t* cio = j2k->cio;
  1042. int len = cio_read(cio, 2); /* Ltlm */
  1043. cio_read(cio, 1); /* Ztlm */
  1044. int Stlm = cio_read(cio, 1); /* Stlm */
  1045. int ST = ((Stlm >> 4) & 0x01) + ((Stlm >> 4) & 0x02);
  1046. int SP = (Stlm >> 6) & 0x01;
  1047. int tile_tlm = (len - 4) / ((SP + 1) * 2 + ST);
  1048. int i;
  1049. for (i = 0; i < tile_tlm; ++i) {
  1050. cio_read(cio, ST); /* Ttlm_i */
  1051. cio_read(cio, SP ? 4 : 2); /* Ptlm_i */
  1052. }
  1053. }
  1054. static void j2k_read_plm(opj_j2k_t* j2k) {
  1055. opj_cio_t* cio = j2k->cio;
  1056. int len = cio_read(cio, 2) - 3; /* Lplm */
  1057. cio_read(cio, 1); /* Zplm */
  1058. len -= 3;
  1059. int i;
  1060. int packet_len = 0;
  1061. while (len > 0) {
  1062. int Nplm = cio_read(cio, 4); /* Nplm */
  1063. len -= 4;
  1064. for (i = Nplm; i > 0; --i) {
  1065. int add = cio_read(cio, 1);
  1066. --len;
  1067. packet_len = (packet_len << 7) + add; /* Iplm_ij */
  1068. if ((add & 0x80) == 0) {
  1069. /* New packet */
  1070. packet_len = 0;
  1071. }
  1072. if (len <= 0) {
  1073. break;
  1074. }
  1075. }
  1076. }
  1077. }
  1078. static void j2k_read_plt(opj_j2k_t* j2k) {
  1079. opj_cio_t* cio = j2k->cio;
  1080. int len = cio_read(cio, 2); /* Lplt */
  1081. cio_read(cio, 1); /* Zplt */
  1082. int packet_len = 0;
  1083. int i;
  1084. for (i = len - 3; i > 0; --i) {
  1085. int add = cio_read(cio, 1);
  1086. packet_len = (packet_len << 7) + add; /* Iplt_i */
  1087. if ((add & 0x80) == 0) {
  1088. /* New packet */
  1089. packet_len = 0;
  1090. }
  1091. }
  1092. }
  1093. static void j2k_read_ppm(opj_j2k_t* j2k) {
  1094. opj_cp_t* cp = j2k->cp;
  1095. opj_cio_t* cio = j2k->cio;
  1096. int len = cio_read(cio, 2) - 3;
  1097. cp->ppm = 1;
  1098. int Z_ppm = cio_read(cio, 1); /* Z_ppm */
  1099. int i, j, N_ppm;
  1100. while (len > 0) {
  1101. if (cp->ppm_previous == 0) {
  1102. N_ppm = cio_read(cio, 4); /* N_ppm */
  1103. len -= 4;
  1104. } else {
  1105. N_ppm = cp->ppm_previous;
  1106. }
  1107. j = cp->ppm_store;
  1108. if (Z_ppm == 0) { /* First PPM marker */
  1109. cp->ppm_data = (unsigned char*)opj_malloc(N_ppm * sizeof(unsigned char));
  1110. cp->ppm_data_first = cp->ppm_data;
  1111. cp->ppm_len = N_ppm;
  1112. } else { /* NON-first PPM marker */
  1113. cp->ppm_data = (unsigned char*)opj_realloc(cp->ppm_data, (N_ppm + cp->ppm_store) * sizeof(unsigned char));
  1114. #ifdef USE_JPWL
  1115. /* this memory allocation check could be done even in non-JPWL cases */
  1116. if (cp->correct) {
  1117. if (!cp->ppm_data) {
  1118. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1119. "JPWL: failed memory allocation during PPM marker parsing (pos. %x)\n",
  1120. cio_tell(cio));
  1121. if (!JPWL_ASSUME || JPWL_ASSUME) {
  1122. opj_free(cp->ppm_data);
  1123. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  1124. return;
  1125. }
  1126. }
  1127. }
  1128. #else
  1129. if (!cp->ppm_data) {
  1130. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  1131. return;
  1132. }
  1133. #endif
  1134. cp->ppm_data_first = cp->ppm_data;
  1135. cp->ppm_len = N_ppm + cp->ppm_store;
  1136. }
  1137. for (i = N_ppm; i > 0; --i) { /* Read packet header */
  1138. cp->ppm_data[j] = cio_read(cio, 1);
  1139. ++j;
  1140. if (--len == 0) {
  1141. /* Case of non-finished packet header in present marker but finished in next one */
  1142. break;
  1143. }
  1144. }
  1145. cp->ppm_previous = i - 1;
  1146. cp->ppm_store = j;
  1147. }
  1148. }
  1149. static void j2k_read_ppt(opj_j2k_t* j2k) {
  1150. opj_cp_t* cp = j2k->cp;
  1151. opj_tcp_t* tcp = cp->tcps + j2k->curtileno;
  1152. opj_cio_t* cio = j2k->cio;
  1153. int len = cio_read(cio, 2);
  1154. int Z_ppt = cio_read(cio, 1);
  1155. tcp->ppt = 1;
  1156. if (Z_ppt == 0) { /* First PPT marker */
  1157. tcp->ppt_data = (unsigned char*)opj_malloc((len - 3) * sizeof(unsigned char));
  1158. tcp->ppt_data_first = tcp->ppt_data;
  1159. tcp->ppt_store = 0;
  1160. tcp->ppt_len = len - 3;
  1161. } else { /* NON-first PPT marker */
  1162. tcp->ppt_data = (unsigned char*)opj_realloc(tcp->ppt_data, (len - 3 + tcp->ppt_store) * sizeof(unsigned char));
  1163. tcp->ppt_data_first = tcp->ppt_data;
  1164. tcp->ppt_len = len - 3 + tcp->ppt_store;
  1165. }
  1166. if (!cp->ppm_data) {
  1167. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  1168. return;
  1169. }
  1170. int j = tcp->ppt_store;
  1171. int i;
  1172. for (i = len - 3; i > 0; --i) {
  1173. tcp->ppt_data[j++] = cio_read(cio, 1);
  1174. }
  1175. tcp->ppt_store = j;
  1176. }
  1177. static void j2k_write_tlm(opj_j2k_t* j2k) {
  1178. opj_cio_t *cio = j2k->cio;
  1179. j2k->tlm_start = cio_tell(cio);
  1180. cio_write(cio, J2K_MS_TLM, 2); /* TLM */
  1181. int lenp = 4 + 5 * j2k->totnum_tp;
  1182. cio_write(cio,lenp,2); /* Ltlm */
  1183. cio_write(cio, 0,1); /* Ztlm=0*/
  1184. cio_write(cio,80,1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
  1185. cio_skip(cio,5*j2k->totnum_tp);
  1186. }
  1187. static void j2k_write_sot(opj_j2k_t* j2k) {
  1188. opj_cio_t* cio = j2k->cio;
  1189. j2k->sot_start = cio_tell(cio);
  1190. cio_write(cio, J2K_MS_SOT, 2); /* SOT */
  1191. int lenp = cio_tell(cio);
  1192. cio_skip(cio, 2); /* Lsot (further) */
  1193. cio_write(cio, j2k->curtileno, 2); /* Isot */
  1194. cio_skip(cio, 4); /* Psot (further in j2k_write_sod) */
  1195. cio_write(cio, j2k->cur_tp_num , 1); /* TPsot */
  1196. cio_write(cio, j2k->cur_totnum_tp[j2k->curtileno], 1); /* TNsot */
  1197. int len = cio_tell(cio) - lenp;
  1198. cio_seek(cio, lenp);
  1199. cio_write(cio, len, 2); /* Lsot */
  1200. cio_seek(cio, lenp + len);
  1201. /* UniPG>> */
  1202. #ifdef USE_JPWL
  1203. /* update markers struct */
  1204. j2k_add_marker(j2k->cstr_info, J2K_MS_SOT, j2k->sot_start, len + 2);
  1205. #endif /* USE_JPWL */
  1206. /* <<UniPG */
  1207. }
  1208. static void j2k_read_sot(opj_j2k_t* j2k) {
  1209. opj_cp_t* cp = j2k->cp;
  1210. opj_cio_t* cio = j2k->cio;
  1211. cio_read(cio, 2);
  1212. int tileno = cio_read(cio, 2);
  1213. /* note: FIX to v1.4.0 (CVE-2012-3358) applied */
  1214. #ifdef USE_JPWL
  1215. if (j2k->cp->correct) {
  1216. static int backup_tileno = 0;
  1217. /* tileno is negative or larger than the number of tiles!!! */
  1218. if (tileno < 0 || tileno >= cp->tw * cp->th) {
  1219. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1220. "JPWL: bad tile number (%d out of a maximum of %d)\n",
  1221. tileno, (cp->tw * cp->th));
  1222. if (!JPWL_ASSUME) {
  1223. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  1224. return;
  1225. }
  1226. /* we try to correct */
  1227. tileno = backup_tileno;
  1228. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
  1229. "- setting tile number to %d\n",
  1230. tileno);
  1231. }
  1232. /* keep your private count of tiles */
  1233. ++backup_tileno;
  1234. }
  1235. else
  1236. #endif /* USE_JPWL */
  1237. {
  1238. /* tileno is negative or larger than the number of tiles!!! */
  1239. if ((tileno < 0) || (tileno >= (cp->tw * cp->th))) {
  1240. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1241. "JPWL: bad tile number (%d out of a maximum of %d)\n",
  1242. tileno, (cp->tw * cp->th));
  1243. return;
  1244. }
  1245. }
  1246. int i;
  1247. if (cp->tileno_size == 0) {
  1248. cp->tileno[cp->tileno_size++] = tileno;
  1249. } else {
  1250. i = 0;
  1251. char status = 0;
  1252. while (i < cp->tileno_size && status == 0) {
  1253. status = cp->tileno[i++] == tileno ? 1 : 0;
  1254. }
  1255. if (status == 0) {
  1256. cp->tileno[cp->tileno_size++] = tileno;
  1257. }
  1258. }
  1259. int totlen = cio_read(cio, 4);
  1260. #ifdef USE_JPWL
  1261. if (j2k->cp->correct) {
  1262. /* totlen is negative or larger than the bytes left!!! */
  1263. if (totlen < 0 || totlen > cio_numbytesleft(cio) + 8) {
  1264. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1265. "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
  1266. totlen, cio_numbytesleft(cio) + 8);
  1267. if (!JPWL_ASSUME) {
  1268. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  1269. return;
  1270. }
  1271. /* we try to correct */
  1272. totlen = 0;
  1273. opj_event_msg(j2k->cinfo, EVT_WARNING, "- trying to adjust this\n"
  1274. "- setting Psot to %d => assuming it is the last tile\n",
  1275. totlen);
  1276. }
  1277. }
  1278. else
  1279. {
  1280. /* totlen is negative or larger than the bytes left!!! */
  1281. if ((totlen < 0) || (totlen > (cio_numbytesleft(cio) + 8))) {
  1282. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1283. "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
  1284. totlen, cio_numbytesleft(cio) + 8);
  1285. return;
  1286. }
  1287. }
  1288. #else
  1289. /* totlen is negative ! */
  1290. /* NOTE: it CAN be rightfully larger than the bytes left when decoding
  1291. at a non-zero discard level. Using the same test as in
  1292. USE_JPWL case above breaks OpenJPEG for SecondLife... HB */
  1293. if (totlen < 0) {
  1294. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1295. "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
  1296. totlen, cio_numbytesleft(cio) + 8);
  1297. return;
  1298. }
  1299. #endif /* USE_JPWL */
  1300. if (!totlen) {
  1301. totlen = cio_numbytesleft(cio) + 8;
  1302. }
  1303. int partno = cio_read(cio, 1);
  1304. int numparts = cio_read(cio, 1);
  1305. j2k->curtileno = tileno;
  1306. j2k->cur_tp_num = partno;
  1307. j2k->eot = cio_getbp(cio) - 12 + totlen;
  1308. j2k->state = J2K_STATE_TPH;
  1309. opj_tcp_t* tcp = &cp->tcps[j2k->curtileno];
  1310. /* Index */
  1311. if (j2k->cstr_info) {
  1312. if (tcp->first) {
  1313. if (tileno == 0) {
  1314. j2k->cstr_info->main_head_end = cio_tell(cio) - 13;
  1315. }
  1316. j2k->cstr_info->tile[tileno].tileno = tileno;
  1317. j2k->cstr_info->tile[tileno].start_pos = cio_tell(cio) - 12;
  1318. j2k->cstr_info->tile[tileno].end_pos = j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
  1319. j2k->cstr_info->tile[tileno].num_tps = numparts;
  1320. if (numparts) {
  1321. j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t*)opj_malloc(numparts * sizeof(opj_tp_info_t));
  1322. } else {
  1323. j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t*)opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
  1324. }
  1325. if (!j2k->cstr_info->tile[tileno].tp) {
  1326. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  1327. return;
  1328. }
  1329. } else {
  1330. j2k->cstr_info->tile[tileno].end_pos += totlen;
  1331. }
  1332. j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = cio_tell(cio) - 12;
  1333. j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
  1334. j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
  1335. }
  1336. if (tcp->first == 1) {
  1337. /* Initialization PPT */
  1338. opj_tccp_t* tmp = tcp->tccps;
  1339. memcpy(tcp, j2k->default_tcp, sizeof(opj_tcp_t));
  1340. tcp->ppt = 0;
  1341. tcp->ppt_data = NULL;
  1342. tcp->ppt_data_first = NULL;
  1343. tcp->tccps = tmp;
  1344. for (i = 0; i < j2k->image->numcomps; ++i) {
  1345. tcp->tccps[i] = j2k->default_tcp->tccps[i];
  1346. }
  1347. cp->tcps[j2k->curtileno].first = 0;
  1348. }
  1349. }
  1350. static void j2k_write_sod(opj_j2k_t* j2k, void* tile_coder) {
  1351. opj_tcd_t* tcd = (opj_tcd_t*)tile_coder; /* cast is needed because of conflicts in header inclusions */
  1352. opj_cp_t* cp = j2k->cp;
  1353. opj_cio_t* cio = j2k->cio;
  1354. tcd->tp_num = j2k->tp_num ;
  1355. tcd->cur_tp_num = j2k->cur_tp_num;
  1356. cio_write(cio, J2K_MS_SOD, 2);
  1357. if (j2k->curtileno == 0) {
  1358. j2k->sod_start = cio_tell(cio) + j2k->pos_correction;
  1359. }
  1360. /* INDEX >> */
  1361. opj_codestream_info_t* cstr_info = j2k->cstr_info;
  1362. if (cstr_info) {
  1363. if (!j2k->cur_tp_num ) {
  1364. cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
  1365. j2k->cstr_info->tile[j2k->curtileno].tileno = j2k->curtileno;
  1366. } else if (cstr_info->tile[j2k->curtileno].packet[cstr_info->packno - 1].end_pos < cio_tell(cio)) {
  1367. cstr_info->tile[j2k->curtileno].packet[cstr_info->packno].start_pos = cio_tell(cio);
  1368. }
  1369. /* UniPG>> */
  1370. #ifdef USE_JPWL
  1371. /* update markers struct */
  1372. j2k_add_marker(j2k->cstr_info, J2K_MS_SOD, j2k->sod_start, 2);
  1373. #endif /* USE_JPWL */
  1374. /* <<UniPG */
  1375. }
  1376. /* << INDEX */
  1377. opj_tcp_t* tcp = &cp->tcps[j2k->curtileno];
  1378. int layno;
  1379. int cnt = cp->th * cp->tw;
  1380. for (layno = 0; layno < tcp->numlayers; ++layno) {
  1381. if (tcp->rates[layno] > j2k->sod_start / cnt) {
  1382. tcp->rates[layno] -= j2k->sod_start / cnt;
  1383. } else if (tcp->rates[layno]) {
  1384. tcp->rates[layno] = 1;
  1385. }
  1386. }
  1387. if (j2k->cur_tp_num == 0) {
  1388. tcd->tcd_image->tiles->packno = 0;
  1389. if (cstr_info) {
  1390. cstr_info->packno = 0;
  1391. }
  1392. }
  1393. /* Writing Psot in SOT marker */
  1394. int l = tcd_encode_tile(tcd, j2k->curtileno, cio_getbp(cio), cio_numbytesleft(cio) - 2, cstr_info);
  1395. int totlen = cio_tell(cio) + l - j2k->sot_start;
  1396. cio_seek(cio, j2k->sot_start + 6);
  1397. cio_write(cio, totlen, 4);
  1398. cio_seek(cio, j2k->sot_start + totlen);
  1399. /* Writing Ttlm and Ptlm in TLM marker */
  1400. if (cp->cinema) {
  1401. cio_seek(cio, j2k->tlm_start + 6 + 5 * j2k->cur_tp_num);
  1402. cio_write(cio, j2k->curtileno, 1);
  1403. cio_write(cio, totlen, 4);
  1404. }
  1405. cio_seek(cio, j2k->sot_start + totlen);
  1406. }
  1407. static void j2k_read_sod(opj_j2k_t* j2k) {
  1408. opj_cio_t* cio = j2k->cio;
  1409. int curtileno = j2k->curtileno;
  1410. /* Index */
  1411. if (j2k->cstr_info) {
  1412. j2k->cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
  1413. cio_tell(cio) + j2k->pos_correction - 1;
  1414. if (j2k->cur_tp_num == 0)
  1415. j2k->cstr_info->tile[j2k->curtileno].end_header = cio_tell(cio) + j2k->pos_correction - 1;
  1416. j2k->cstr_info->packno = 0;
  1417. }
  1418. int len = int_min((int)(j2k->eot - cio_getbp(cio)), cio_numbytesleft(cio) + 1);
  1419. int truncate = 0;
  1420. if (len == cio_numbytesleft(cio) + 1) {
  1421. truncate = 1; /* Case of a truncate codestream */
  1422. }
  1423. unsigned char* data = j2k->tile_data[curtileno];
  1424. data = (unsigned char*)opj_realloc(data, (j2k->tile_len[curtileno] + len) * sizeof(unsigned char));
  1425. if (!data) {
  1426. opj_event_msg(j2k->cinfo, EVT_ERROR, "out of memory\n");
  1427. return;
  1428. }
  1429. unsigned char* data_ptr = data + j2k->tile_len[curtileno];
  1430. int i;
  1431. for (i = 0; i < len; ++i) {
  1432. data_ptr[i] = cio_read(cio, 1);
  1433. }
  1434. j2k->tile_len[curtileno] += len;
  1435. j2k->tile_data[curtileno] = data;
  1436. if (!truncate) {
  1437. j2k->state = J2K_STATE_TPHSOT;
  1438. } else {
  1439. j2k->state = J2K_STATE_NEOC; /* RAJOUTE !! */
  1440. }
  1441. ++j2k->cur_tp_num;
  1442. }
  1443. static void j2k_write_rgn(opj_j2k_t* j2k, int compno, int tileno) {
  1444. opj_cp_t* cp = j2k->cp;
  1445. opj_tcp_t* tcp = &cp->tcps[tileno];
  1446. opj_cio_t* cio = j2k->cio;
  1447. int numcomps = j2k->image->numcomps;
  1448. cio_write(cio, J2K_MS_RGN, 2); /* RGN */
  1449. cio_write(cio, numcomps <= 256 ? 5 : 6, 2); /* Lrgn */
  1450. cio_write(cio, compno, numcomps <= 256 ? 1 : 2); /* Crgn */
  1451. cio_write(cio, 0, 1); /* Srgn */
  1452. cio_write(cio, tcp->tccps[compno].roishift, 1); /* SPrgn */
  1453. }
  1454. static void j2k_read_rgn(opj_j2k_t* j2k) {
  1455. opj_cp_t* cp = j2k->cp;
  1456. opj_tcp_t* tcp = j2k->state == J2K_STATE_TPH ? &cp->tcps[j2k->curtileno] : j2k->default_tcp;
  1457. opj_cio_t* cio = j2k->cio;
  1458. int numcomps = j2k->image->numcomps;
  1459. cio_read(cio, 2); /* Lrgn */
  1460. int compno = cio_read(cio, numcomps <= 256 ? 1 : 2); /* Crgn */
  1461. cio_read(cio, 1); /* Srgn */
  1462. #ifdef USE_JPWL
  1463. if (j2k->cp->correct) {
  1464. /* totlen is negative or larger than the bytes left!!! */
  1465. if (compno >= numcomps) {
  1466. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1467. "JPWL: bad component number in RGN (%d when there are only %d)\n",
  1468. compno, numcomps);
  1469. if (!JPWL_ASSUME || JPWL_ASSUME) {
  1470. opj_event_msg(j2k->cinfo, EVT_ERROR, "JPWL: giving up\n");
  1471. return;
  1472. }
  1473. }
  1474. }
  1475. #endif /* USE_JPWL */
  1476. /* totlen is negative or larger than the bytes left!!! */
  1477. if (compno >= numcomps) {
  1478. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1479. "JPWL: bad component number in RGN (%d when there are only %d)\n",
  1480. compno, numcomps);
  1481. } else {
  1482. tcp->tccps[compno].roishift = cio_read(cio, 1); /* SPrgn */
  1483. }
  1484. }
  1485. static void j2k_write_eoc(opj_j2k_t* j2k) {
  1486. opj_cio_t* cio = j2k->cio;
  1487. /* opj_event_msg(j2k->cinfo, "%.8x: EOC\n", cio_tell(cio) + j2k->pos_correction); */
  1488. cio_write(cio, J2K_MS_EOC, 2);
  1489. /* UniPG>> */
  1490. #ifdef USE_JPWL
  1491. /* update markers struct */
  1492. j2k_add_marker(j2k->cstr_info, J2K_MS_EOC, cio_tell(cio) - 2, 2);
  1493. #endif /* USE_JPWL */
  1494. /* <<UniPG */
  1495. }
  1496. static void j2k_read_eoc(opj_j2k_t* j2k) {
  1497. int i;
  1498. /* if packets should be decoded */
  1499. if (j2k->cp->limit_decoding != DECODE_ALL_BUT_PACKETS) {
  1500. opj_tcd_t* tcd = tcd_create(j2k->cinfo);
  1501. tcd_malloc_decode(tcd, j2k->image, j2k->cp);
  1502. for (i = 0; i < j2k->cp->tileno_size; ++i) {
  1503. tcd_malloc_decode_tile(tcd, j2k->image, j2k->cp, i, j2k->cstr_info);
  1504. int tileno = j2k->cp->tileno[i];
  1505. bool success = tcd_decode_tile(tcd, j2k->tile_data[tileno], j2k->tile_len[tileno], tileno, j2k->cstr_info);
  1506. opj_free(j2k->tile_data[tileno]);
  1507. j2k->tile_data[tileno] = NULL;
  1508. tcd_free_decode_tile(tcd, i);
  1509. if (success == false) {
  1510. j2k->state |= J2K_STATE_ERR;
  1511. break;
  1512. }
  1513. }
  1514. tcd_free_decode(tcd);
  1515. tcd_destroy(tcd);
  1516. } else {
  1517. /* if packets should not be decoded */
  1518. for (i = 0; i < j2k->cp->tileno_size; ++i) {
  1519. int tileno = j2k->cp->tileno[i];
  1520. /* not sure if this can actually happen */
  1521. if (tileno != -1) {
  1522. opj_free(j2k->tile_data[tileno]);
  1523. j2k->tile_data[tileno] = NULL;
  1524. }
  1525. }
  1526. }
  1527. if (j2k->state & J2K_STATE_ERR) {
  1528. j2k->state = J2K_STATE_MT + J2K_STATE_ERR;
  1529. } else {
  1530. j2k->state = J2K_STATE_MT;
  1531. }
  1532. }
  1533. typedef struct opj_dec_mstabent {
  1534. /** marker value */
  1535. int id;
  1536. /** value of the state when the marker can appear */
  1537. int states;
  1538. /** action linked to the marker */
  1539. void (*handler)(opj_j2k_t* j2k);
  1540. } opj_dec_mstabent_t;
  1541. opj_dec_mstabent_t j2k_dec_mstab[] = {
  1542. {J2K_MS_SOC, J2K_STATE_MHSOC, j2k_read_soc},
  1543. {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, j2k_read_sot},
  1544. {J2K_MS_SOD, J2K_STATE_TPH, j2k_read_sod},
  1545. {J2K_MS_EOC, J2K_STATE_TPHSOT, j2k_read_eoc},
  1546. {J2K_MS_SIZ, J2K_STATE_MHSIZ, j2k_read_siz},
  1547. {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_cod},
  1548. {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_coc},
  1549. {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_rgn},
  1550. {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcd},
  1551. {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_qcc},
  1552. {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_poc},
  1553. {J2K_MS_TLM, J2K_STATE_MH, j2k_read_tlm},
  1554. {J2K_MS_PLM, J2K_STATE_MH, j2k_read_plm},
  1555. {J2K_MS_PLT, J2K_STATE_TPH, j2k_read_plt},
  1556. {J2K_MS_PPM, J2K_STATE_MH, j2k_read_ppm},
  1557. {J2K_MS_PPT, J2K_STATE_TPH, j2k_read_ppt},
  1558. {J2K_MS_SOP, 0, 0},
  1559. {J2K_MS_CRG, J2K_STATE_MH, j2k_read_crg},
  1560. {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_com},
  1561. #ifdef USE_JPWL
  1562. {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
  1563. {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
  1564. {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
  1565. {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
  1566. #endif /* USE_JPWL */
  1567. #ifdef USE_JPSEC
  1568. {J2K_MS_SEC, J2K_STATE_MH, j2k_read_sec},
  1569. {J2K_MS_INSEC, 0, j2k_read_insec},
  1570. #endif /* USE_JPSEC */
  1571. {0, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_unk}
  1572. };
  1573. static void j2k_read_unk(opj_j2k_t* j2k) {
  1574. opj_event_msg(j2k->cinfo, EVT_WARNING, "Unknown marker\n");
  1575. #ifdef USE_JPWL
  1576. if (j2k->cp->correct) {
  1577. int min_id = 0, min_dist = 17, cur_dist = 0, tmp_id;
  1578. cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
  1579. id = cio_read(j2k->cio, 2);
  1580. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1581. "JPWL: really don't know this marker %x\n",
  1582. id);
  1583. if (!JPWL_ASSUME) {
  1584. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1585. "- possible synch loss due to uncorrectable codestream errors => giving up\n");
  1586. return;
  1587. }
  1588. /* OK, activate this at your own risk!!! */
  1589. /* we look for the marker at the minimum hamming distance from this */
  1590. int m = 0, id, i;
  1591. while (j2k_dec_mstab[m].id) {
  1592. /* 1's where they differ */
  1593. tmp_id = j2k_dec_mstab[m].id ^ id;
  1594. /* compute the hamming distance between our id and the current */
  1595. cur_dist = 0;
  1596. for (i = 0; i < 16; ++i) {
  1597. if ((tmp_id >> i) & 0x0001) {
  1598. ++cur_dist;
  1599. }
  1600. }
  1601. /* if current distance is smaller, set the minimum */
  1602. if (cur_dist < min_dist) {
  1603. min_dist = cur_dist;
  1604. min_id = j2k_dec_mstab[m].id;
  1605. }
  1606. /* jump to the next marker */
  1607. ++m;
  1608. }
  1609. /* do we substitute the marker? */
  1610. if (min_dist < JPWL_MAXIMUM_HAMMING) {
  1611. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1612. "- marker %x is at distance %d from the read %x\n",
  1613. min_id, min_dist, id);
  1614. opj_event_msg(j2k->cinfo, EVT_ERROR,
  1615. "- trying to substitute in place and crossing fingers!\n");
  1616. cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
  1617. cio_write(j2k->cio, min_id, 2);
  1618. /* rewind */
  1619. cio_seek(j2k->cio, cio_tell(j2k->cio) - 2);
  1620. }
  1621. }
  1622. #endif /* USE_JPWL */
  1623. }
  1624. /**
  1625. Read the lookup table containing all the marker, status and action
  1626. @param id Marker value
  1627. */
  1628. static opj_dec_mstabent_t* j2k_dec_mstab_lookup(int id) {
  1629. opj_dec_mstabent_t* e;
  1630. for (e = j2k_dec_mstab; e->id != 0; ++e) {
  1631. if (e->id == id) {
  1632. break;
  1633. }
  1634. }
  1635. return e;
  1636. }
  1637. /* ----------------------------------------------------------------------- */
  1638. /* J2K / JPT decoder interface */
  1639. /* ----------------------------------------------------------------------- */
  1640. opj_j2k_t* j2k_create_decompress(opj_common_ptr cinfo) {
  1641. opj_j2k_t* j2k = (opj_j2k_t*)opj_calloc(1, sizeof(opj_j2k_t));
  1642. if (!j2k) {
  1643. return NULL;
  1644. }
  1645. j2k->default_tcp = (opj_tcp_t*)opj_calloc(1, sizeof(opj_tcp_t));
  1646. if (!j2k->default_tcp) {
  1647. opj_free(j2k);
  1648. return NULL;
  1649. }
  1650. j2k->cinfo = cinfo;
  1651. j2k->tile_data = NULL;
  1652. return j2k;
  1653. }
  1654. void j2k_destroy_decompress(opj_j2k_t* j2k) {
  1655. if (j2k->tile_len) {
  1656. opj_free(j2k->tile_len);
  1657. }
  1658. if (j2k->tile_data) {
  1659. opj_free(j2k->tile_data);
  1660. }
  1661. if (j2k->default_tcp) {
  1662. opj_tcp_t* default_tcp = j2k->default_tcp;
  1663. if (default_tcp->ppt_data_first) {
  1664. opj_free(default_tcp->ppt_data_first);
  1665. }
  1666. if (j2k->default_tcp->tccps) {
  1667. opj_free(j2k->default_tcp->tccps);
  1668. }
  1669. opj_free(j2k->default_tcp);
  1670. }
  1671. if (j2k->cp) {
  1672. opj_cp_t* cp = j2k->cp;
  1673. if (cp->tcps) {
  1674. int i;
  1675. for (i = 0; i < cp->tw * cp->th; ++i) {
  1676. if (cp->tcps[i].ppt_data_first) {
  1677. opj_free(cp->tcps[i].ppt_data_first);
  1678. }
  1679. if (cp->tcps[i].tccps) {
  1680. opj_free(cp->tcps[i].tccps);
  1681. }
  1682. }
  1683. opj_free(cp->tcps);
  1684. }
  1685. if (cp->ppm_data_first) {
  1686. opj_free(cp->ppm_data_first);
  1687. }
  1688. if (cp->tileno) {
  1689. opj_free(cp->tileno);
  1690. }
  1691. if (cp->comment) {
  1692. opj_free(cp->comment);
  1693. }
  1694. opj_free(cp);
  1695. }
  1696. opj_free(j2k);
  1697. }
  1698. void j2k_setup_decoder(opj_j2k_t* j2k, opj_dparameters_t* parameters) {
  1699. if (j2k && parameters) {
  1700. /* create and initialize the coding parameters structure */
  1701. opj_cp_t* cp = (opj_cp_t*)opj_calloc(1, sizeof(opj_cp_t));
  1702. if (!cp) {
  1703. /* Memory allocation failure... */
  1704. return;
  1705. }
  1706. cp->reduce = parameters->cp_reduce;
  1707. cp->layer = parameters->cp_layer;
  1708. cp->limit_decoding = parameters->cp_limit_decoding;
  1709. #ifdef USE_JPWL
  1710. cp->correct = parameters->jpwl_correct;
  1711. cp->exp_comps = parameters->jpwl_exp_comps;
  1712. cp->max_tiles = parameters->jpwl_max_tiles;
  1713. #endif /* USE_JPWL */
  1714. /* keep a link to cp so that we can destroy it later in j2k_destroy_decompress */
  1715. j2k->cp = cp;
  1716. }
  1717. }
  1718. opj_image_t* j2k_decode(opj_j2k_t* j2k, opj_cio_t* cio, opj_codestream_info_t* cstr_info) {
  1719. opj_common_ptr cinfo = j2k->cinfo;
  1720. j2k->cio = cio;
  1721. j2k->cstr_info = cstr_info;
  1722. if (cstr_info) {
  1723. memset(cstr_info, 0, sizeof(opj_codestream_info_t));
  1724. }
  1725. /* create an empty image */
  1726. opj_image_t* image = opj_image_create0();
  1727. j2k->image = image;
  1728. j2k->state = J2K_STATE_MHSOC;
  1729. if (!image) {
  1730. opj_event_msg(cinfo, EVT_ERROR, "out of memory\n");
  1731. j2k->state |= J2K_STATE_ERR;
  1732. return NULL;
  1733. }
  1734. for (;;) {
  1735. opj_dec_mstabent_t *e;
  1736. int id = cio_read(cio, 2);
  1737. #ifdef USE_JPWL
  1738. /* we try to honor JPWL correction power */
  1739. if (j2k->cp->correct) {
  1740. int orig_pos = cio_tell(cio);
  1741. bool status;
  1742. /* call the corrector */
  1743. status = jpwl_correct(j2k);
  1744. /* go back to where you were */
  1745. cio_seek(cio, orig_pos - 2);
  1746. /* re-read the marker */
  1747. id = cio_read(cio, 2);
  1748. /* check whether it begins with ff */
  1749. if ((id >> 8) != 0xff) {
  1750. opj_event_msg(cinfo, EVT_ERROR,
  1751. "JPWL: possible bad marker %x at %d\n",
  1752. id, cio_tell(cio) - 2);
  1753. if (!JPWL_ASSUME) {
  1754. opj_image_destroy(image);
  1755. opj_event_msg(cinfo, EVT_ERROR, "JPWL: giving up\n");
  1756. return NULL;
  1757. }
  1758. /* we try to correct */
  1759. id = id | 0xff00;
  1760. cio_seek(cio, cio_tell(cio) - 2);
  1761. cio_write(cio, id, 2);
  1762. opj_event_msg(cinfo, EVT_WARNING, "- trying to adjust this\n"
  1763. "- setting marker to %x\n",
  1764. id);
  1765. }
  1766. }
  1767. #endif /* USE_JPWL */
  1768. if ((id >> 8) != 0xff) {
  1769. opj_image_destroy(image);
  1770. opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
  1771. return NULL;
  1772. }
  1773. e = j2k_dec_mstab_lookup(id);
  1774. // Check if the marker is known
  1775. if (!(j2k->state & e->states)) {
  1776. opj_image_destroy(image);
  1777. opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
  1778. return NULL;
  1779. }
  1780. // Check if the decoding is limited to the main header
  1781. if (e->id == J2K_MS_SOT && j2k->cp->limit_decoding == LIMIT_TO_MAIN_HEADER) {
  1782. opj_event_msg(cinfo, EVT_INFO, "Main Header decoded.\n");
  1783. return image;
  1784. }
  1785. if (e->handler) {
  1786. (*e->handler)(j2k);
  1787. }
  1788. if (j2k->state & J2K_STATE_ERR) {
  1789. opj_image_destroy(image);
  1790. return NULL;
  1791. }
  1792. if (j2k->state == J2K_STATE_MT) {
  1793. break;
  1794. }
  1795. if (j2k->state == J2K_STATE_NEOC) {
  1796. break;
  1797. }
  1798. }
  1799. if (j2k->state == J2K_STATE_NEOC) {
  1800. j2k_read_eoc(j2k);
  1801. #if 0
  1802. /* Check one last time for errors during decoding before returning */
  1803. if (j2k->state & J2K_STATE_ERR) {
  1804. opj_image_destroy(image);
  1805. return NULL;
  1806. }
  1807. #endif
  1808. }
  1809. if (j2k->state != J2K_STATE_MT) {
  1810. opj_event_msg(cinfo, EVT_INFO, "Incomplete bitstream\n");
  1811. }
  1812. return image;
  1813. }
  1814. /*
  1815. * Read a JPT-stream and decode file
  1816. *
  1817. */
  1818. opj_image_t* j2k_decode_jpt_stream(opj_j2k_t* j2k, opj_cio_t* cio, opj_codestream_info_t* cstr_info) {
  1819. opj_common_ptr cinfo = j2k->cinfo;
  1820. j2k->cio = cio;
  1821. /* create an empty image */
  1822. opj_image_t* image = opj_image_create0();
  1823. j2k->image = image;
  1824. j2k->state = J2K_STATE_MHSOC;
  1825. if (!image) {
  1826. opj_event_msg(cinfo, EVT_ERROR, "out of memory\n");
  1827. j2k->state |= J2K_STATE_ERR;
  1828. return NULL;
  1829. }
  1830. /* Initialize the header */
  1831. opj_jpt_msg_header_t header;
  1832. jpt_init_msg_header(&header);
  1833. /* Read the first header of the message */
  1834. jpt_read_msg_header(cinfo, cio, &header);
  1835. int position = cio_tell(cio);
  1836. if (header.Class_Id != 6) { /* 6 : Main header data-bin message */
  1837. opj_image_destroy(image);
  1838. opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Main header first [class_Id %d] !\n", header.Class_Id);
  1839. return NULL;
  1840. }
  1841. for ( ; ; ) {
  1842. opj_dec_mstabent_t* e = NULL;
  1843. int id;
  1844. if (!cio_numbytesleft(cio)) {
  1845. j2k_read_eoc(j2k);
  1846. return image;
  1847. }
  1848. /* data-bin read -> need to read a new header */
  1849. if ((unsigned int)(cio_tell(cio) - position) == header.Msg_length) {
  1850. jpt_read_msg_header(cinfo, cio, &header);
  1851. position = cio_tell(cio);
  1852. if (header.Class_Id != 4) { /* 4 : Tile data-bin message */
  1853. opj_image_destroy(image);
  1854. opj_event_msg(cinfo, EVT_ERROR, "[JPT-stream] : Expecting Tile info !\n");
  1855. return NULL;
  1856. }
  1857. }
  1858. id = cio_read(cio, 2);
  1859. if (id >> 8 != 0xff) {
  1860. opj_image_destroy(image);
  1861. opj_event_msg(cinfo, EVT_ERROR, "%.8x: expected a marker instead of %x\n", cio_tell(cio) - 2, id);
  1862. return NULL;
  1863. }
  1864. e = j2k_dec_mstab_lookup(id);
  1865. if (!(j2k->state & e->states)) {
  1866. opj_image_destroy(image);
  1867. opj_event_msg(cinfo, EVT_ERROR, "%.8x: unexpected marker %x\n", cio_tell(cio) - 2, id);
  1868. return NULL;
  1869. }
  1870. if (e->handler) {
  1871. (*e->handler)(j2k);
  1872. }
  1873. if (j2k->state == J2K_STATE_MT) {
  1874. break;
  1875. }
  1876. if (j2k->state == J2K_STATE_NEOC) {
  1877. break;
  1878. }
  1879. }
  1880. if (j2k->state == J2K_STATE_NEOC) {
  1881. j2k_read_eoc(j2k);
  1882. }
  1883. if (j2k->state != J2K_STATE_MT) {
  1884. opj_event_msg(cinfo, EVT_INFO, "Incomplete bitstream\n");
  1885. }
  1886. return image;
  1887. }
  1888. /* ----------------------------------------------------------------------- */
  1889. /* J2K encoder interface */
  1890. /* ----------------------------------------------------------------------- */
  1891. opj_j2k_t* j2k_create_compress(opj_common_ptr cinfo) {
  1892. opj_j2k_t* j2k = (opj_j2k_t*)opj_calloc(1, sizeof(opj_j2k_t));
  1893. if (j2k) {
  1894. j2k->cinfo = cinfo;
  1895. }
  1896. return j2k;
  1897. }
  1898. void j2k_destroy_compress(opj_j2k_t* j2k) {
  1899. int tileno;
  1900. if (!j2k) return;
  1901. if (j2k->cp != NULL) {
  1902. opj_cp_t* cp = j2k->cp;
  1903. if (cp->comment) {
  1904. opj_free(cp->comment);
  1905. }
  1906. if (cp->matrice) {
  1907. opj_free(cp->matrice);
  1908. }
  1909. for (tileno = 0; tileno < cp->tw * cp->th; ++tileno) {
  1910. opj_free(cp->tcps[tileno].tccps);
  1911. }
  1912. opj_free(cp->tcps);
  1913. opj_free(cp);
  1914. }
  1915. opj_free(j2k);
  1916. }
  1917. void j2k_setup_encoder(opj_j2k_t* j2k, opj_cparameters_t* parameters, opj_image_t* image) {
  1918. int i, j, tileno, numpocs_tile;
  1919. opj_cp_t* cp = NULL;
  1920. if (!j2k || !parameters || ! image) {
  1921. return;
  1922. }
  1923. /* create and initialize the coding parameters structure */
  1924. cp = (opj_cp_t*) opj_calloc(1, sizeof(opj_cp_t));
  1925. if (!cp) {
  1926. /* Memory allocation failure... */
  1927. return;
  1928. }
  1929. /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
  1930. j2k->cp = cp;
  1931. /* set default values for cp */
  1932. cp->tw = 1;
  1933. cp->th = 1;
  1934. /* copy user encoding parameters */
  1935. cp->cinema = parameters->cp_cinema;
  1936. cp->max_comp_size = parameters->max_comp_size;
  1937. cp->rsiz = parameters->cp_rsiz;
  1938. cp->disto_alloc = parameters->cp_disto_alloc;
  1939. cp->fixed_alloc = parameters->cp_fixed_alloc;
  1940. cp->fixed_quality = parameters->cp_fixed_quality;
  1941. /* mod fixed_quality */
  1942. if (parameters->cp_matrice) {
  1943. size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(int);
  1944. cp->matrice = (int *) opj_malloc(array_size);
  1945. if (cp->matrice) {
  1946. memcpy(cp->matrice, parameters->cp_matrice, array_size);
  1947. }
  1948. }
  1949. /* tiles */
  1950. cp->tdx = parameters->cp_tdx;
  1951. cp->tdy = parameters->cp_tdy;
  1952. /* tile offset */
  1953. cp->tx0 = parameters->cp_tx0;
  1954. cp->ty0 = parameters->cp_ty0;
  1955. /* comment string */
  1956. if (parameters->cp_comment) {
  1957. cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
  1958. if (cp->comment) {
  1959. strcpy(cp->comment, parameters->cp_comment);
  1960. }
  1961. }
  1962. /* calculate other encoding parameters */
  1963. if (parameters->tile_size_on) {
  1964. cp->tw = int_ceildiv(image->x1 - cp->tx0, cp->tdx);
  1965. cp->th = int_ceildiv(image->y1 - cp->ty0, cp->tdy);
  1966. } else {
  1967. cp->tdx = image->x1 - cp->tx0;
  1968. cp->tdy = image->y1 - cp->ty0;
  1969. }
  1970. if (parameters->tp_on) {
  1971. cp->tp_flag = parameters->tp_flag;
  1972. cp->tp_on = 1;
  1973. }
  1974. cp->img_size = 0;
  1975. for (i = 0; i < image->numcomps; ++i) {
  1976. cp->img_size += image->comps[i].w * image->comps[i].h * image->comps[i].prec;
  1977. }
  1978. #ifdef USE_JPWL
  1979. /*
  1980. calculate JPWL encoding parameters
  1981. */
  1982. if (parameters->jpwl_epc_on) {
  1983. int i;
  1984. /* set JPWL on */
  1985. cp->epc_on = true;
  1986. cp->info_on = false; /* no informative technique */
  1987. /* set EPB on */
  1988. if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
  1989. cp->epb_on = true;
  1990. cp->hprot_MH = parameters->jpwl_hprot_MH;
  1991. for (i = 0; i < JPWL_MAX_NO_TILESPECS; ++i) {
  1992. cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
  1993. cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
  1994. }
  1995. /* if tile specs are not specified, copy MH specs */
  1996. if (cp->hprot_TPH[0] == -1) {
  1997. cp->hprot_TPH_tileno[0] = 0;
  1998. cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
  1999. }
  2000. for (i = 0; i < JPWL_MAX_NO_PACKSPECS; ++i) {
  2001. cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
  2002. cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
  2003. cp->pprot[i] = parameters->jpwl_pprot[i];
  2004. }
  2005. }
  2006. /* set ESD writing */
  2007. if (parameters->jpwl_sens_size == 1 || parameters->jpwl_sens_size == 2) {
  2008. cp->esd_on = true;
  2009. cp->sens_size = parameters->jpwl_sens_size;
  2010. cp->sens_addr = parameters->jpwl_sens_addr;
  2011. cp->sens_range = parameters->jpwl_sens_range;
  2012. cp->sens_MH = parameters->jpwl_sens_MH;
  2013. for (i = 0; i < JPWL_MAX_NO_TILESPECS; ++i) {
  2014. cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
  2015. cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
  2016. }
  2017. }
  2018. /* always set RED writing to false: we are at the encoder */
  2019. cp->red_on = false;
  2020. } else {
  2021. cp->epc_on = false;
  2022. }
  2023. #endif /* USE_JPWL */
  2024. /* initialize the mutiple tiles */
  2025. /* ---------------------------- */
  2026. cp->tcps = (opj_tcp_t*)opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
  2027. if (!cp->tcps) {
  2028. /* Memory allocation failure... */
  2029. return;
  2030. }
  2031. for (tileno = 0; tileno < cp->tw * cp->th; ++tileno) {
  2032. opj_tcp_t *tcp = &cp->tcps[tileno];
  2033. tcp->numlayers = parameters->tcp_numlayers;
  2034. for (j = 0; j < tcp->numlayers; ++j) {
  2035. if (cp->cinema) {
  2036. if (cp->fixed_quality) {
  2037. tcp->distoratio[j] = parameters->tcp_distoratio[j];
  2038. }
  2039. tcp->rates[j] = parameters->tcp_rates[j];
  2040. } else if (cp->fixed_quality) { /* add fixed_quality */
  2041. tcp->distoratio[j] = parameters->tcp_distoratio[j];
  2042. } else {
  2043. tcp->rates[j] = parameters->tcp_rates[j];
  2044. }
  2045. }
  2046. tcp->csty = parameters->csty;
  2047. tcp->prg = parameters->prog_order;
  2048. tcp->mct = parameters->tcp_mct;
  2049. numpocs_tile = 0;
  2050. tcp->POC = 0;
  2051. if (parameters->numpocs) {
  2052. /* initialisation of POC */
  2053. tcp->POC = 1;
  2054. for (i = 0; i < parameters->numpocs; ++i) {
  2055. if (tileno == parameters->POC[i].tile - 1 || parameters->POC[i].tile == -1) {
  2056. opj_poc_t* tcp_poc = &tcp->pocs[numpocs_tile];
  2057. tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
  2058. tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
  2059. tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
  2060. tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
  2061. tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
  2062. tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
  2063. tcp_poc->tile = parameters->POC[numpocs_tile++].tile;
  2064. }
  2065. }
  2066. tcp->numpocs = numpocs_tile -1 ;
  2067. } else {
  2068. tcp->numpocs = 0;
  2069. }
  2070. tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
  2071. if (!tcp->tccps) {
  2072. /* Memory allocation failure... */
  2073. return;
  2074. }
  2075. for (i = 0; i < image->numcomps; ++i) {
  2076. opj_tccp_t *tccp = &tcp->tccps[i];
  2077. tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
  2078. tccp->numresolutions = parameters->numresolution;
  2079. tccp->cblkw = int_floorlog2(parameters->cblockw_init);
  2080. tccp->cblkh = int_floorlog2(parameters->cblockh_init);
  2081. tccp->cblksty = parameters->mode;
  2082. tccp->qmfbid = parameters->irreversible ? 0 : 1;
  2083. tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
  2084. tccp->numgbits = 2;
  2085. if (i == parameters->roi_compno) {
  2086. tccp->roishift = parameters->roi_shift;
  2087. } else {
  2088. tccp->roishift = 0;
  2089. }
  2090. if (parameters->cp_cinema)
  2091. {
  2092. //Precinct size for lowest frequency subband = 128
  2093. tccp->prcw[0] = 7;
  2094. tccp->prch[0] = 7;
  2095. //Precinct size at all other resolutions = 256
  2096. for (j = 1; j < tccp->numresolutions; ++j) {
  2097. tccp->prcw[j] = 8;
  2098. tccp->prch[j] = 8;
  2099. }
  2100. }else{
  2101. if (parameters->csty & J2K_CCP_CSTY_PRT) {
  2102. int p = 0;
  2103. for (j = tccp->numresolutions - 1; j >= 0; --j) {
  2104. if (p < parameters->res_spec) {
  2105. if (parameters->prcw_init[p] < 1) {
  2106. tccp->prcw[j] = 1;
  2107. } else {
  2108. tccp->prcw[j] = int_floorlog2(parameters->prcw_init[p]);
  2109. }
  2110. if (parameters->prch_init[p] < 1) {
  2111. tccp->prch[j] = 1;
  2112. } else {
  2113. tccp->prch[j] = int_floorlog2(parameters->prch_init[p]);
  2114. }
  2115. } else {
  2116. int res_spec = parameters->res_spec;
  2117. int size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
  2118. int size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
  2119. if (size_prcw < 1) {
  2120. tccp->prcw[j] = 1;
  2121. } else {
  2122. tccp->prcw[j] = int_floorlog2(size_prcw);
  2123. }
  2124. if (size_prch < 1) {
  2125. tccp->prch[j] = 1;
  2126. } else {
  2127. tccp->prch[j] = int_floorlog2(size_prch);
  2128. }
  2129. }
  2130. ++p;
  2131. /*printf("\nsize precinct for level %d : %d,%d\n", j,tccp->prcw[j], tccp->prch[j]); */
  2132. } //end for
  2133. } else {
  2134. for (j = 0; j < tccp->numresolutions; ++j) {
  2135. tccp->prcw[j] = 15;
  2136. tccp->prch[j] = 15;
  2137. }
  2138. }
  2139. }
  2140. dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
  2141. }
  2142. }
  2143. }
  2144. bool j2k_encode(opj_j2k_t* j2k, opj_cio_t* cio, opj_image_t* image, opj_codestream_info_t* cstr_info) {
  2145. int tileno, compno;
  2146. opj_cp_t* cp = NULL;
  2147. opj_tcd_t *tcd = NULL; /* TCD component */
  2148. j2k->cio = cio;
  2149. j2k->image = image;
  2150. cp = j2k->cp;
  2151. /* j2k_dump_cp(stdout, image, cp); */
  2152. /* INDEX >> */
  2153. j2k->cstr_info = cstr_info;
  2154. if (cstr_info) {
  2155. cstr_info->tile = (opj_tile_info_t *) opj_malloc(cp->tw * cp->th * sizeof(opj_tile_info_t));
  2156. if (!cstr_info->tile) {
  2157. /* Memory allocation failure... */
  2158. return false;
  2159. }
  2160. cstr_info->image_w = image->x1 - image->x0;
  2161. cstr_info->image_h = image->y1 - image->y0;
  2162. cstr_info->prog = (&cp->tcps[0])->prg;
  2163. cstr_info->tw = cp->tw;
  2164. cstr_info->th = cp->th;
  2165. cstr_info->tile_x = cp->tdx; /* new version parser */
  2166. cstr_info->tile_y = cp->tdy; /* new version parser */
  2167. cstr_info->tile_Ox = cp->tx0; /* new version parser */
  2168. cstr_info->tile_Oy = cp->ty0; /* new version parser */
  2169. cstr_info->numcomps = image->numcomps;
  2170. cstr_info->numlayers = (&cp->tcps[0])->numlayers;
  2171. cstr_info->numdecompos = (int*) opj_malloc(image->numcomps * sizeof(int));
  2172. if (!cstr_info->numdecompos) {
  2173. /* Memory allocation failure... */
  2174. return false;
  2175. }
  2176. for (compno = 0; compno < image->numcomps; ++compno) {
  2177. cstr_info->numdecompos[compno] = (&cp->tcps[0])->tccps->numresolutions - 1;
  2178. }
  2179. cstr_info->D_max = 0.0; /* ADD Marcela */
  2180. cstr_info->main_head_start = cio_tell(cio); /* position of SOC */
  2181. cstr_info->maxmarknum = 100;
  2182. cstr_info->marker = (opj_marker_info_t*)opj_malloc(cstr_info->maxmarknum * sizeof(opj_marker_info_t));
  2183. if (!cstr_info->marker) {
  2184. /* Memory allocation failure... */
  2185. return false;
  2186. }
  2187. cstr_info->marknum = 0;
  2188. }
  2189. /* << INDEX */
  2190. j2k_write_soc(j2k);
  2191. j2k_write_siz(j2k);
  2192. j2k_write_cod(j2k);
  2193. j2k_write_qcd(j2k);
  2194. if (cp->cinema) {
  2195. for (compno = 1; compno < image->numcomps; ++compno) {
  2196. j2k_write_coc(j2k, compno);
  2197. j2k_write_qcc(j2k, compno);
  2198. }
  2199. }
  2200. for (compno = 0; compno < image->numcomps; ++compno) {
  2201. opj_tcp_t *tcp = &cp->tcps[0];
  2202. if (tcp->tccps[compno].roishift)
  2203. j2k_write_rgn(j2k, compno, 0);
  2204. }
  2205. if (cp->comment != NULL) {
  2206. j2k_write_com(j2k);
  2207. }
  2208. j2k->totnum_tp = j2k_calculate_tp(cp,image->numcomps,image,j2k);
  2209. /* TLM Marker*/
  2210. if (cp->cinema) {
  2211. j2k_write_tlm(j2k);
  2212. if (cp->cinema == CINEMA4K_24) {
  2213. j2k_write_poc(j2k);
  2214. }
  2215. }
  2216. /* uncomment only for testing JPSEC marker writing */
  2217. /* j2k_write_sec(j2k); */
  2218. /* INDEX >> */
  2219. if (cstr_info) {
  2220. cstr_info->main_head_end = cio_tell(cio) - 1;
  2221. }
  2222. /* << INDEX */
  2223. /**** Main Header ENDS here ***/
  2224. /* create the tile encoder */
  2225. tcd = tcd_create(j2k->cinfo);
  2226. /* encode each tile */
  2227. for (tileno = 0; tileno < cp->tw * cp->th; ++tileno) {
  2228. int tilepartno = 0;
  2229. /* UniPG>> */
  2230. int acc_pack_num = 0;
  2231. /* <<UniPG */
  2232. opj_tcp_t* tcp = &cp->tcps[tileno];
  2233. opj_event_msg(j2k->cinfo, EVT_INFO, "tile number %d / %d\n", tileno + 1, cp->tw * cp->th);
  2234. j2k->curtileno = tileno;
  2235. j2k->cur_tp_num = 0;
  2236. tcd->cur_totnum_tp = j2k->cur_totnum_tp[j2k->curtileno];
  2237. /* initialisation before tile encoding */
  2238. if (tileno == 0) {
  2239. tcd_malloc_encode(tcd, image, cp, j2k->curtileno);
  2240. } else {
  2241. tcd_init_encode(tcd, image, cp, j2k->curtileno);
  2242. }
  2243. /* INDEX >> */
  2244. if (cstr_info) {
  2245. cstr_info->tile[j2k->curtileno].start_pos = cio_tell(cio) + j2k->pos_correction;
  2246. }
  2247. /* << INDEX */
  2248. int pino;
  2249. for (pino = 0; pino <= tcp->numpocs; ++pino) {
  2250. int tot_num_tp;
  2251. tcd->cur_pino=pino;
  2252. /*Get number of tile parts*/
  2253. tot_num_tp = j2k_get_num_tp(cp,pino,tileno);
  2254. tcd->tp_pos = cp->tp_pos;
  2255. for (tilepartno = 0; tilepartno < tot_num_tp; ++tilepartno) {
  2256. j2k->tp_num = tilepartno;
  2257. /* INDEX >> */
  2258. if (cstr_info)
  2259. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pos =
  2260. cio_tell(cio) + j2k->pos_correction;
  2261. /* << INDEX */
  2262. j2k_write_sot(j2k);
  2263. if (j2k->cur_tp_num == 0 && cp->cinema == 0) {
  2264. for (compno = 1; compno < image->numcomps; ++compno) {
  2265. j2k_write_coc(j2k, compno);
  2266. j2k_write_qcc(j2k, compno);
  2267. }
  2268. if (cp->tcps[tileno].numpocs) {
  2269. j2k_write_poc(j2k);
  2270. }
  2271. }
  2272. /* INDEX >> */
  2273. if (cstr_info) {
  2274. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_header =
  2275. cio_tell(cio) + j2k->pos_correction + 1;
  2276. }
  2277. /* << INDEX */
  2278. j2k_write_sod(j2k, tcd);
  2279. /* INDEX >> */
  2280. if (cstr_info) {
  2281. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_end_pos =
  2282. cio_tell(cio) + j2k->pos_correction - 1;
  2283. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_start_pack =
  2284. acc_pack_num;
  2285. cstr_info->tile[j2k->curtileno].tp[j2k->cur_tp_num].tp_numpacks =
  2286. cstr_info->packno - acc_pack_num;
  2287. acc_pack_num = cstr_info->packno;
  2288. }
  2289. /* << INDEX */
  2290. ++j2k->cur_tp_num;
  2291. }
  2292. }
  2293. if (cstr_info) {
  2294. cstr_info->tile[j2k->curtileno].end_pos = cio_tell(cio) + j2k->pos_correction - 1;
  2295. }
  2296. /*
  2297. if (tile->PPT) { // BAD PPT !!!
  2298. FILE *PPT_file;
  2299. int i;
  2300. PPT_file=fopen("PPT","rb");
  2301. fprintf(stderr,"%c%c%c%c",255,97,tile->len_ppt/256,tile->len_ppt%256);
  2302. for (i=0;i<tile->len_ppt;++i) {
  2303. unsigned char elmt;
  2304. fread(&elmt, 1, 1, PPT_file);
  2305. fwrite(&elmt,1,1,f);
  2306. }
  2307. fclose(PPT_file);
  2308. unlink("PPT");
  2309. }
  2310. */
  2311. }
  2312. /* destroy the tile encoder */
  2313. tcd_free_encode(tcd);
  2314. tcd_destroy(tcd);
  2315. opj_free(j2k->cur_totnum_tp);
  2316. j2k_write_eoc(j2k);
  2317. if (cstr_info) {
  2318. cstr_info->codestream_size = cio_tell(cio) + j2k->pos_correction;
  2319. /* UniPG>> */
  2320. /* The following adjustment is done to adjust the codestream size */
  2321. /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
  2322. /* the first bunch of bytes is not in the codestream */
  2323. cstr_info->codestream_size -= cstr_info->main_head_start;
  2324. /* <<UniPG */
  2325. }
  2326. #ifdef USE_JPWL
  2327. /* preparation of JPWL marker segments */
  2328. if (cp->epc_on) {
  2329. /* encode according to JPWL */
  2330. jpwl_encode(j2k, cio, image);
  2331. }
  2332. #endif /* USE_JPWL */
  2333. return true;
  2334. }