j2k.c 76 KB

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