j2k.c 485 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
  15. * Copyright (c) 2006-2007, Parvatha Elangovan
  16. * Copyright (c) 2010-2011, Kaori Hagihara
  17. * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
  18. * Copyright (c) 2012, CS Systemes d'Information, France
  19. * Copyright (c) 2017, IntoPIX SA <support@intopix.com>
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the above copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  32. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  35. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  38. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  39. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. #include "opj_includes.h"
  44. /** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
  45. /*@{*/
  46. /** @name Local static functions */
  47. /*@{*/
  48. /**
  49. * Sets up the procedures to do on reading header. Developers wanting to extend the library can add their own reading procedures.
  50. */
  51. static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k,
  52. opj_event_mgr_t * p_manager);
  53. /**
  54. * The read header procedure.
  55. */
  56. static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k,
  57. opj_stream_private_t *p_stream,
  58. opj_event_mgr_t * p_manager);
  59. /**
  60. * The default encoding validation procedure without any extension.
  61. *
  62. * @param p_j2k the jpeg2000 codec to validate.
  63. * @param p_stream the input stream to validate.
  64. * @param p_manager the user event manager.
  65. *
  66. * @return true if the parameters are correct.
  67. */
  68. static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k,
  69. opj_stream_private_t *p_stream,
  70. opj_event_mgr_t * p_manager);
  71. /**
  72. * The default decoding validation procedure without any extension.
  73. *
  74. * @param p_j2k the jpeg2000 codec to validate.
  75. * @param p_stream the input stream to validate.
  76. * @param p_manager the user event manager.
  77. *
  78. * @return true if the parameters are correct.
  79. */
  80. static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t * p_j2k,
  81. opj_stream_private_t *p_stream,
  82. opj_event_mgr_t * p_manager);
  83. /**
  84. * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
  85. * are valid. Developers wanting to extend the library can add their own validation procedures.
  86. */
  87. static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k,
  88. opj_event_mgr_t * p_manager);
  89. /**
  90. * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
  91. * are valid. Developers wanting to extend the library can add their own validation procedures.
  92. */
  93. static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k,
  94. opj_event_mgr_t * p_manager);
  95. /**
  96. * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
  97. * are valid. Developers wanting to extend the library can add their own validation procedures.
  98. */
  99. static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k,
  100. opj_event_mgr_t * p_manager);
  101. /**
  102. * The mct encoding validation procedure.
  103. *
  104. * @param p_j2k the jpeg2000 codec to validate.
  105. * @param p_stream the input stream to validate.
  106. * @param p_manager the user event manager.
  107. *
  108. * @return true if the parameters are correct.
  109. */
  110. static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k,
  111. opj_stream_private_t *p_stream,
  112. opj_event_mgr_t * p_manager);
  113. /**
  114. * Builds the tcd decoder to use to decode tile.
  115. */
  116. static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k,
  117. opj_stream_private_t *p_stream,
  118. opj_event_mgr_t * p_manager);
  119. /**
  120. * Builds the tcd encoder to use to encode tile.
  121. */
  122. static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k,
  123. opj_stream_private_t *p_stream,
  124. opj_event_mgr_t * p_manager);
  125. /**
  126. * Creates a tile-coder encoder.
  127. *
  128. * @param p_stream the stream to write data to.
  129. * @param p_j2k J2K codec.
  130. * @param p_manager the user event manager.
  131. */
  132. static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k,
  133. opj_stream_private_t *p_stream,
  134. opj_event_mgr_t * p_manager);
  135. /**
  136. * Executes the given procedures on the given codec.
  137. *
  138. * @param p_procedure_list the list of procedures to execute
  139. * @param p_j2k the jpeg2000 codec to execute the procedures on.
  140. * @param p_stream the stream to execute the procedures on.
  141. * @param p_manager the user manager.
  142. *
  143. * @return true if all the procedures were successfully executed.
  144. */
  145. static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k,
  146. opj_procedure_list_t * p_procedure_list,
  147. opj_stream_private_t *p_stream,
  148. opj_event_mgr_t * p_manager);
  149. /**
  150. * Updates the rates of the tcp.
  151. *
  152. * @param p_stream the stream to write data to.
  153. * @param p_j2k J2K codec.
  154. * @param p_manager the user event manager.
  155. */
  156. static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k,
  157. opj_stream_private_t *p_stream,
  158. opj_event_mgr_t * p_manager);
  159. /**
  160. * Copies the decoding tile parameters onto all the tile parameters.
  161. * Creates also the tile decoder.
  162. */
  163. static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k,
  164. opj_stream_private_t *p_stream,
  165. opj_event_mgr_t * p_manager);
  166. /**
  167. * Destroys the memory associated with the decoding of headers.
  168. */
  169. static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k,
  170. opj_stream_private_t *p_stream,
  171. opj_event_mgr_t * p_manager);
  172. /**
  173. * Reads the lookup table containing all the marker, status and action, and returns the handler associated
  174. * with the marker value.
  175. * @param p_id Marker value to look up
  176. *
  177. * @return the handler associated with the id.
  178. */
  179. static const struct opj_dec_memory_marker_handler * opj_j2k_get_marker_handler(
  180. OPJ_UINT32 p_id);
  181. /**
  182. * Destroys a tile coding parameter structure.
  183. *
  184. * @param p_tcp the tile coding parameter to destroy.
  185. */
  186. static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp);
  187. /**
  188. * Destroys the data inside a tile coding parameter structure.
  189. *
  190. * @param p_tcp the tile coding parameter which contain data to destroy.
  191. */
  192. static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp);
  193. /**
  194. * Destroys a coding parameter structure.
  195. *
  196. * @param p_cp the coding parameter to destroy.
  197. */
  198. static void opj_j2k_cp_destroy(opj_cp_t *p_cp);
  199. /**
  200. * Compare 2 a SPCod/ SPCoc elements, i.e. the coding style of a given component of a tile.
  201. *
  202. * @param p_j2k J2K codec.
  203. * @param p_tile_no Tile number
  204. * @param p_first_comp_no The 1st component number to compare.
  205. * @param p_second_comp_no The 1st component number to compare.
  206. *
  207. * @return OPJ_TRUE if SPCdod are equals.
  208. */
  209. static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k,
  210. OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no);
  211. /**
  212. * Writes a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
  213. *
  214. * @param p_j2k J2K codec.
  215. * @param p_tile_no FIXME DOC
  216. * @param p_comp_no the component number to output.
  217. * @param p_data FIXME DOC
  218. * @param p_header_size FIXME DOC
  219. * @param p_manager the user event manager.
  220. *
  221. * @return FIXME DOC
  222. */
  223. static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k,
  224. OPJ_UINT32 p_tile_no,
  225. OPJ_UINT32 p_comp_no,
  226. OPJ_BYTE * p_data,
  227. OPJ_UINT32 * p_header_size,
  228. opj_event_mgr_t * p_manager);
  229. /**
  230. * Gets the size taken by writing a SPCod or SPCoc for the given tile and component.
  231. *
  232. * @param p_j2k the J2K codec.
  233. * @param p_tile_no the tile index.
  234. * @param p_comp_no the component being outputted.
  235. *
  236. * @return the number of bytes taken by the SPCod element.
  237. */
  238. static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k,
  239. OPJ_UINT32 p_tile_no,
  240. OPJ_UINT32 p_comp_no);
  241. /**
  242. * Reads a SPCod or SPCoc element, i.e. the coding style of a given component of a tile.
  243. * @param p_j2k the jpeg2000 codec.
  244. * @param compno FIXME DOC
  245. * @param p_header_data the data contained in the COM box.
  246. * @param p_header_size the size of the data contained in the COM marker.
  247. * @param p_manager the user event manager.
  248. */
  249. static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k,
  250. OPJ_UINT32 compno,
  251. OPJ_BYTE * p_header_data,
  252. OPJ_UINT32 * p_header_size,
  253. opj_event_mgr_t * p_manager);
  254. /**
  255. * Gets the size taken by writing SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
  256. *
  257. * @param p_tile_no the tile index.
  258. * @param p_comp_no the component being outputted.
  259. * @param p_j2k the J2K codec.
  260. *
  261. * @return the number of bytes taken by the SPCod element.
  262. */
  263. static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k,
  264. OPJ_UINT32 p_tile_no,
  265. OPJ_UINT32 p_comp_no);
  266. /**
  267. * Compares 2 SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
  268. *
  269. * @param p_j2k J2K codec.
  270. * @param p_tile_no the tile to output.
  271. * @param p_first_comp_no the first component number to compare.
  272. * @param p_second_comp_no the second component number to compare.
  273. *
  274. * @return OPJ_TRUE if equals.
  275. */
  276. static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k,
  277. OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no);
  278. /**
  279. * Writes a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
  280. *
  281. * @param p_tile_no the tile to output.
  282. * @param p_comp_no the component number to output.
  283. * @param p_data the data buffer.
  284. * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
  285. * @param p_j2k J2K codec.
  286. * @param p_manager the user event manager.
  287. *
  288. */
  289. static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
  290. OPJ_UINT32 p_tile_no,
  291. OPJ_UINT32 p_comp_no,
  292. OPJ_BYTE * p_data,
  293. OPJ_UINT32 * p_header_size,
  294. opj_event_mgr_t * p_manager);
  295. /**
  296. * Updates the Tile Length Marker.
  297. */
  298. static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size);
  299. /**
  300. * Reads a SQcd or SQcc element, i.e. the quantization values of a band in the QCD or QCC.
  301. *
  302. * @param p_j2k J2K codec.
  303. * @param compno the component number to output.
  304. * @param p_header_data the data buffer.
  305. * @param p_header_size pointer to the size of the data buffer, it is changed by the function.
  306. * @param p_manager the user event manager.
  307. *
  308. */
  309. static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
  310. OPJ_UINT32 compno,
  311. OPJ_BYTE * p_header_data,
  312. OPJ_UINT32 * p_header_size,
  313. opj_event_mgr_t * p_manager);
  314. /**
  315. * Copies the tile component parameters of all the component from the first tile component.
  316. *
  317. * @param p_j2k the J2k codec.
  318. */
  319. static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k);
  320. /**
  321. * Copies the tile quantization parameters of all the component from the first tile component.
  322. *
  323. * @param p_j2k the J2k codec.
  324. */
  325. static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k);
  326. /**
  327. * Reads the tiles.
  328. */
  329. static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k,
  330. opj_stream_private_t *p_stream,
  331. opj_event_mgr_t * p_manager);
  332. static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k,
  333. OPJ_UINT32 p_tile_index,
  334. opj_stream_private_t *p_stream,
  335. opj_event_mgr_t * p_manager);
  336. static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd,
  337. opj_image_t* p_output_image);
  338. static void opj_get_tile_dimensions(opj_image_t * l_image,
  339. opj_tcd_tilecomp_t * l_tilec,
  340. opj_image_comp_t * l_img_comp,
  341. OPJ_UINT32* l_size_comp,
  342. OPJ_UINT32* l_width,
  343. OPJ_UINT32* l_height,
  344. OPJ_UINT32* l_offset_x,
  345. OPJ_UINT32* l_offset_y,
  346. OPJ_UINT32* l_image_width,
  347. OPJ_UINT32* l_stride,
  348. OPJ_UINT32* l_tile_offset);
  349. static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data);
  350. static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k,
  351. opj_stream_private_t *p_stream,
  352. opj_event_mgr_t * p_manager);
  353. /**
  354. * Sets up the procedures to do on writing header.
  355. * Developers wanting to extend the library can add their own writing procedures.
  356. */
  357. static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k,
  358. opj_event_mgr_t * p_manager);
  359. static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k,
  360. OPJ_BYTE * p_data,
  361. OPJ_UINT32 * p_data_written,
  362. OPJ_UINT32 total_data_size,
  363. opj_stream_private_t *p_stream,
  364. struct opj_event_mgr * p_manager);
  365. static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k,
  366. OPJ_BYTE * p_data,
  367. OPJ_UINT32 * p_data_written,
  368. OPJ_UINT32 total_data_size,
  369. opj_stream_private_t *p_stream,
  370. struct opj_event_mgr * p_manager);
  371. /**
  372. * Gets the offset of the header.
  373. *
  374. * @param p_stream the stream to write data to.
  375. * @param p_j2k J2K codec.
  376. * @param p_manager the user event manager.
  377. */
  378. static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
  379. opj_stream_private_t *p_stream,
  380. opj_event_mgr_t * p_manager);
  381. static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k);
  382. /*
  383. * -----------------------------------------------------------------------
  384. * -----------------------------------------------------------------------
  385. * -----------------------------------------------------------------------
  386. */
  387. /**
  388. * Writes the SOC marker (Start Of Codestream)
  389. *
  390. * @param p_stream the stream to write data to.
  391. * @param p_j2k J2K codec.
  392. * @param p_manager the user event manager.
  393. */
  394. static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k,
  395. opj_stream_private_t *p_stream,
  396. opj_event_mgr_t * p_manager);
  397. /**
  398. * Reads a SOC marker (Start of Codestream)
  399. * @param p_j2k the jpeg2000 file codec.
  400. * @param p_stream XXX needs data
  401. * @param p_manager the user event manager.
  402. */
  403. static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k,
  404. opj_stream_private_t *p_stream,
  405. opj_event_mgr_t * p_manager);
  406. /**
  407. * Writes the SIZ marker (image and tile size)
  408. *
  409. * @param p_j2k J2K codec.
  410. * @param p_stream the stream to write data to.
  411. * @param p_manager the user event manager.
  412. */
  413. static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k,
  414. opj_stream_private_t *p_stream,
  415. opj_event_mgr_t * p_manager);
  416. /**
  417. * Reads a SIZ marker (image and tile size)
  418. * @param p_j2k the jpeg2000 file codec.
  419. * @param p_header_data the data contained in the SIZ box.
  420. * @param p_header_size the size of the data contained in the SIZ marker.
  421. * @param p_manager the user event manager.
  422. */
  423. static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
  424. OPJ_BYTE * p_header_data,
  425. OPJ_UINT32 p_header_size,
  426. opj_event_mgr_t * p_manager);
  427. /**
  428. * Writes the COM marker (comment)
  429. *
  430. * @param p_stream the stream to write data to.
  431. * @param p_j2k J2K codec.
  432. * @param p_manager the user event manager.
  433. */
  434. static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k,
  435. opj_stream_private_t *p_stream,
  436. opj_event_mgr_t * p_manager);
  437. /**
  438. * Reads a COM marker (comments)
  439. * @param p_j2k the jpeg2000 file codec.
  440. * @param p_header_data the data contained in the COM box.
  441. * @param p_header_size the size of the data contained in the COM marker.
  442. * @param p_manager the user event manager.
  443. */
  444. static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k,
  445. OPJ_BYTE * p_header_data,
  446. OPJ_UINT32 p_header_size,
  447. opj_event_mgr_t * p_manager);
  448. /**
  449. * Writes the COD marker (Coding style default)
  450. *
  451. * @param p_stream the stream to write data to.
  452. * @param p_j2k J2K codec.
  453. * @param p_manager the user event manager.
  454. */
  455. static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k,
  456. opj_stream_private_t *p_stream,
  457. opj_event_mgr_t * p_manager);
  458. /**
  459. * Reads a COD marker (Coding style defaults)
  460. * @param p_header_data the data contained in the COD box.
  461. * @param p_j2k the jpeg2000 codec.
  462. * @param p_header_size the size of the data contained in the COD marker.
  463. * @param p_manager the user event manager.
  464. */
  465. static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k,
  466. OPJ_BYTE * p_header_data,
  467. OPJ_UINT32 p_header_size,
  468. opj_event_mgr_t * p_manager);
  469. /**
  470. * Compares 2 COC markers (Coding style component)
  471. *
  472. * @param p_j2k J2K codec.
  473. * @param p_first_comp_no the index of the first component to compare.
  474. * @param p_second_comp_no the index of the second component to compare.
  475. *
  476. * @return OPJ_TRUE if equals
  477. */
  478. static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k,
  479. OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no);
  480. /**
  481. * Writes the COC marker (Coding style component)
  482. *
  483. * @param p_j2k J2K codec.
  484. * @param p_comp_no the index of the component to output.
  485. * @param p_stream the stream to write data to.
  486. * @param p_manager the user event manager.
  487. */
  488. static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k,
  489. OPJ_UINT32 p_comp_no,
  490. opj_stream_private_t *p_stream,
  491. opj_event_mgr_t * p_manager);
  492. /**
  493. * Writes the COC marker (Coding style component)
  494. *
  495. * @param p_j2k J2K codec.
  496. * @param p_comp_no the index of the component to output.
  497. * @param p_data FIXME DOC
  498. * @param p_data_written FIXME DOC
  499. * @param p_manager the user event manager.
  500. */
  501. static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
  502. OPJ_UINT32 p_comp_no,
  503. OPJ_BYTE * p_data,
  504. OPJ_UINT32 * p_data_written,
  505. opj_event_mgr_t * p_manager);
  506. /**
  507. * Gets the maximum size taken by a coc.
  508. *
  509. * @param p_j2k the jpeg2000 codec to use.
  510. */
  511. static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k);
  512. /**
  513. * Reads a COC marker (Coding Style Component)
  514. * @param p_header_data the data contained in the COC box.
  515. * @param p_j2k the jpeg2000 codec.
  516. * @param p_header_size the size of the data contained in the COC marker.
  517. * @param p_manager the user event manager.
  518. */
  519. static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k,
  520. OPJ_BYTE * p_header_data,
  521. OPJ_UINT32 p_header_size,
  522. opj_event_mgr_t * p_manager);
  523. /**
  524. * Writes the QCD marker (quantization default)
  525. *
  526. * @param p_j2k J2K codec.
  527. * @param p_stream the stream to write data to.
  528. * @param p_manager the user event manager.
  529. */
  530. static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k,
  531. opj_stream_private_t *p_stream,
  532. opj_event_mgr_t * p_manager);
  533. /**
  534. * Reads a QCD marker (Quantization defaults)
  535. * @param p_header_data the data contained in the QCD box.
  536. * @param p_j2k the jpeg2000 codec.
  537. * @param p_header_size the size of the data contained in the QCD marker.
  538. * @param p_manager the user event manager.
  539. */
  540. static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k,
  541. OPJ_BYTE * p_header_data,
  542. OPJ_UINT32 p_header_size,
  543. opj_event_mgr_t * p_manager);
  544. /**
  545. * Compare QCC markers (quantization component)
  546. *
  547. * @param p_j2k J2K codec.
  548. * @param p_first_comp_no the index of the first component to compare.
  549. * @param p_second_comp_no the index of the second component to compare.
  550. *
  551. * @return OPJ_TRUE if equals.
  552. */
  553. static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k,
  554. OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no);
  555. /**
  556. * Writes the QCC marker (quantization component)
  557. *
  558. * @param p_comp_no the index of the component to output.
  559. * @param p_stream the stream to write data to.
  560. * @param p_j2k J2K codec.
  561. * @param p_manager the user event manager.
  562. */
  563. static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k,
  564. OPJ_UINT32 p_comp_no,
  565. opj_stream_private_t *p_stream,
  566. opj_event_mgr_t * p_manager);
  567. /**
  568. * Writes the QCC marker (quantization component)
  569. *
  570. * @param p_j2k J2K codec.
  571. * @param p_comp_no the index of the component to output.
  572. * @param p_data FIXME DOC
  573. * @param p_data_written the stream to write data to.
  574. * @param p_manager the user event manager.
  575. */
  576. static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
  577. OPJ_UINT32 p_comp_no,
  578. OPJ_BYTE * p_data,
  579. OPJ_UINT32 * p_data_written,
  580. opj_event_mgr_t * p_manager);
  581. /**
  582. * Gets the maximum size taken by a qcc.
  583. */
  584. static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k);
  585. /**
  586. * Reads a QCC marker (Quantization component)
  587. * @param p_header_data the data contained in the QCC box.
  588. * @param p_j2k the jpeg2000 codec.
  589. * @param p_header_size the size of the data contained in the QCC marker.
  590. * @param p_manager the user event manager.
  591. */
  592. static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k,
  593. OPJ_BYTE * p_header_data,
  594. OPJ_UINT32 p_header_size,
  595. opj_event_mgr_t * p_manager);
  596. /**
  597. * Writes the POC marker (Progression Order Change)
  598. *
  599. * @param p_stream the stream to write data to.
  600. * @param p_j2k J2K codec.
  601. * @param p_manager the user event manager.
  602. */
  603. static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k,
  604. opj_stream_private_t *p_stream,
  605. opj_event_mgr_t * p_manager);
  606. /**
  607. * Writes the POC marker (Progression Order Change)
  608. *
  609. * @param p_j2k J2K codec.
  610. * @param p_data FIXME DOC
  611. * @param p_data_written the stream to write data to.
  612. * @param p_manager the user event manager.
  613. */
  614. static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
  615. OPJ_BYTE * p_data,
  616. OPJ_UINT32 * p_data_written,
  617. opj_event_mgr_t * p_manager);
  618. /**
  619. * Gets the maximum size taken by the writing of a POC.
  620. */
  621. static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k);
  622. /**
  623. * Reads a POC marker (Progression Order Change)
  624. *
  625. * @param p_header_data the data contained in the POC box.
  626. * @param p_j2k the jpeg2000 codec.
  627. * @param p_header_size the size of the data contained in the POC marker.
  628. * @param p_manager the user event manager.
  629. */
  630. static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k,
  631. OPJ_BYTE * p_header_data,
  632. OPJ_UINT32 p_header_size,
  633. opj_event_mgr_t * p_manager);
  634. /**
  635. * Gets the maximum size taken by the toc headers of all the tile parts of any given tile.
  636. */
  637. static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k);
  638. /**
  639. * Gets the maximum size taken by the headers of the SOT.
  640. *
  641. * @param p_j2k the jpeg2000 codec to use.
  642. */
  643. static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k);
  644. /**
  645. * Reads a CRG marker (Component registration)
  646. *
  647. * @param p_header_data the data contained in the TLM box.
  648. * @param p_j2k the jpeg2000 codec.
  649. * @param p_header_size the size of the data contained in the TLM marker.
  650. * @param p_manager the user event manager.
  651. */
  652. static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k,
  653. OPJ_BYTE * p_header_data,
  654. OPJ_UINT32 p_header_size,
  655. opj_event_mgr_t * p_manager);
  656. /**
  657. * Reads a TLM marker (Tile Length Marker)
  658. *
  659. * @param p_header_data the data contained in the TLM box.
  660. * @param p_j2k the jpeg2000 codec.
  661. * @param p_header_size the size of the data contained in the TLM marker.
  662. * @param p_manager the user event manager.
  663. */
  664. static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k,
  665. OPJ_BYTE * p_header_data,
  666. OPJ_UINT32 p_header_size,
  667. opj_event_mgr_t * p_manager);
  668. /**
  669. * Writes the updated tlm.
  670. *
  671. * @param p_stream the stream to write data to.
  672. * @param p_j2k J2K codec.
  673. * @param p_manager the user event manager.
  674. */
  675. static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k,
  676. opj_stream_private_t *p_stream,
  677. opj_event_mgr_t * p_manager);
  678. /**
  679. * Reads a PLM marker (Packet length, main header marker)
  680. *
  681. * @param p_header_data the data contained in the TLM box.
  682. * @param p_j2k the jpeg2000 codec.
  683. * @param p_header_size the size of the data contained in the TLM marker.
  684. * @param p_manager the user event manager.
  685. */
  686. static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k,
  687. OPJ_BYTE * p_header_data,
  688. OPJ_UINT32 p_header_size,
  689. opj_event_mgr_t * p_manager);
  690. /**
  691. * Reads a PLT marker (Packet length, tile-part header)
  692. *
  693. * @param p_header_data the data contained in the PLT box.
  694. * @param p_j2k the jpeg2000 codec.
  695. * @param p_header_size the size of the data contained in the PLT marker.
  696. * @param p_manager the user event manager.
  697. */
  698. static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k,
  699. OPJ_BYTE * p_header_data,
  700. OPJ_UINT32 p_header_size,
  701. opj_event_mgr_t * p_manager);
  702. /**
  703. * Reads a PPM marker (Packed headers, main header)
  704. *
  705. * @param p_header_data the data contained in the POC box.
  706. * @param p_j2k the jpeg2000 codec.
  707. * @param p_header_size the size of the data contained in the POC marker.
  708. * @param p_manager the user event manager.
  709. */
  710. static OPJ_BOOL opj_j2k_read_ppm(
  711. opj_j2k_t *p_j2k,
  712. OPJ_BYTE * p_header_data,
  713. OPJ_UINT32 p_header_size,
  714. opj_event_mgr_t * p_manager);
  715. /**
  716. * Merges all PPM markers read (Packed headers, main header)
  717. *
  718. * @param p_cp main coding parameters.
  719. * @param p_manager the user event manager.
  720. */
  721. static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager);
  722. /**
  723. * Reads a PPT marker (Packed packet headers, tile-part header)
  724. *
  725. * @param p_header_data the data contained in the PPT box.
  726. * @param p_j2k the jpeg2000 codec.
  727. * @param p_header_size the size of the data contained in the PPT marker.
  728. * @param p_manager the user event manager.
  729. */
  730. static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k,
  731. OPJ_BYTE * p_header_data,
  732. OPJ_UINT32 p_header_size,
  733. opj_event_mgr_t * p_manager);
  734. /**
  735. * Merges all PPT markers read (Packed headers, tile-part header)
  736. *
  737. * @param p_tcp the tile.
  738. * @param p_manager the user event manager.
  739. */
  740. static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp,
  741. opj_event_mgr_t * p_manager);
  742. /**
  743. * Writes the TLM marker (Tile Length Marker)
  744. *
  745. * @param p_stream the stream to write data to.
  746. * @param p_j2k J2K codec.
  747. * @param p_manager the user event manager.
  748. */
  749. static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k,
  750. opj_stream_private_t *p_stream,
  751. opj_event_mgr_t * p_manager);
  752. /**
  753. * Writes the SOT marker (Start of tile-part)
  754. *
  755. * @param p_j2k J2K codec.
  756. * @param p_data Output buffer
  757. * @param total_data_size Output buffer size
  758. * @param p_data_written Number of bytes written into stream
  759. * @param p_stream the stream to write data to.
  760. * @param p_manager the user event manager.
  761. */
  762. static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k,
  763. OPJ_BYTE * p_data,
  764. OPJ_UINT32 total_data_size,
  765. OPJ_UINT32 * p_data_written,
  766. const opj_stream_private_t *p_stream,
  767. opj_event_mgr_t * p_manager);
  768. /**
  769. * Reads values from a SOT marker (Start of tile-part)
  770. *
  771. * the j2k decoder state is not affected. No side effects, no checks except for p_header_size.
  772. *
  773. * @param p_header_data the data contained in the SOT marker.
  774. * @param p_header_size the size of the data contained in the SOT marker.
  775. * @param p_tile_no Isot.
  776. * @param p_tot_len Psot.
  777. * @param p_current_part TPsot.
  778. * @param p_num_parts TNsot.
  779. * @param p_manager the user event manager.
  780. */
  781. static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data,
  782. OPJ_UINT32 p_header_size,
  783. OPJ_UINT32* p_tile_no,
  784. OPJ_UINT32* p_tot_len,
  785. OPJ_UINT32* p_current_part,
  786. OPJ_UINT32* p_num_parts,
  787. opj_event_mgr_t * p_manager);
  788. /**
  789. * Reads a SOT marker (Start of tile-part)
  790. *
  791. * @param p_header_data the data contained in the SOT marker.
  792. * @param p_j2k the jpeg2000 codec.
  793. * @param p_header_size the size of the data contained in the PPT marker.
  794. * @param p_manager the user event manager.
  795. */
  796. static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k,
  797. OPJ_BYTE * p_header_data,
  798. OPJ_UINT32 p_header_size,
  799. opj_event_mgr_t * p_manager);
  800. /**
  801. * Writes the SOD marker (Start of data)
  802. *
  803. * This also writes optional PLT markers (before SOD)
  804. *
  805. * @param p_j2k J2K codec.
  806. * @param p_tile_coder FIXME DOC
  807. * @param p_data FIXME DOC
  808. * @param p_data_written FIXME DOC
  809. * @param total_data_size FIXME DOC
  810. * @param p_stream the stream to write data to.
  811. * @param p_manager the user event manager.
  812. */
  813. static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,
  814. opj_tcd_t * p_tile_coder,
  815. OPJ_BYTE * p_data,
  816. OPJ_UINT32 * p_data_written,
  817. OPJ_UINT32 total_data_size,
  818. const opj_stream_private_t *p_stream,
  819. opj_event_mgr_t * p_manager);
  820. /**
  821. * Reads a SOD marker (Start Of Data)
  822. *
  823. * @param p_j2k the jpeg2000 codec.
  824. * @param p_stream FIXME DOC
  825. * @param p_manager the user event manager.
  826. */
  827. static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k,
  828. opj_stream_private_t *p_stream,
  829. opj_event_mgr_t * p_manager);
  830. static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size)
  831. {
  832. if (p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte) {
  833. opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
  834. p_j2k->m_current_tile_number, 1);
  835. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 1;
  836. } else {
  837. opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
  838. p_j2k->m_current_tile_number, 2);
  839. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 2;
  840. }
  841. opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
  842. p_tile_part_size, 4); /* PSOT */
  843. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 4;
  844. }
  845. /**
  846. * Writes the RGN marker (Region Of Interest)
  847. *
  848. * @param p_tile_no the tile to output
  849. * @param p_comp_no the component to output
  850. * @param nb_comps the number of components
  851. * @param p_stream the stream to write data to.
  852. * @param p_j2k J2K codec.
  853. * @param p_manager the user event manager.
  854. */
  855. static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
  856. OPJ_UINT32 p_tile_no,
  857. OPJ_UINT32 p_comp_no,
  858. OPJ_UINT32 nb_comps,
  859. opj_stream_private_t *p_stream,
  860. opj_event_mgr_t * p_manager);
  861. /**
  862. * Reads a RGN marker (Region Of Interest)
  863. *
  864. * @param p_header_data the data contained in the POC box.
  865. * @param p_j2k the jpeg2000 codec.
  866. * @param p_header_size the size of the data contained in the POC marker.
  867. * @param p_manager the user event manager.
  868. */
  869. static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k,
  870. OPJ_BYTE * p_header_data,
  871. OPJ_UINT32 p_header_size,
  872. opj_event_mgr_t * p_manager);
  873. /**
  874. * Writes the EOC marker (End of Codestream)
  875. *
  876. * @param p_stream the stream to write data to.
  877. * @param p_j2k J2K codec.
  878. * @param p_manager the user event manager.
  879. */
  880. static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k,
  881. opj_stream_private_t *p_stream,
  882. opj_event_mgr_t * p_manager);
  883. #if 0
  884. /**
  885. * Reads a EOC marker (End Of Codestream)
  886. *
  887. * @param p_j2k the jpeg2000 codec.
  888. * @param p_stream FIXME DOC
  889. * @param p_manager the user event manager.
  890. */
  891. static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k,
  892. opj_stream_private_t *p_stream,
  893. opj_event_mgr_t * p_manager);
  894. #endif
  895. /**
  896. * Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
  897. *
  898. * @param p_stream the stream to write data to.
  899. * @param p_j2k J2K codec.
  900. * @param p_manager the user event manager.
  901. */
  902. static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k,
  903. opj_stream_private_t *p_stream,
  904. opj_event_mgr_t * p_manager);
  905. /**
  906. * Inits the Info
  907. *
  908. * @param p_stream the stream to write data to.
  909. * @param p_j2k J2K codec.
  910. * @param p_manager the user event manager.
  911. */
  912. static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k,
  913. opj_stream_private_t *p_stream,
  914. opj_event_mgr_t * p_manager);
  915. /**
  916. Add main header marker information
  917. @param cstr_index Codestream information structure
  918. @param type marker type
  919. @param pos byte offset of marker segment
  920. @param len length of marker segment
  921. */
  922. static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index,
  923. OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len) ;
  924. /**
  925. Add tile header marker information
  926. @param tileno tile index number
  927. @param cstr_index Codestream information structure
  928. @param type marker type
  929. @param pos byte offset of marker segment
  930. @param len length of marker segment
  931. */
  932. static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno,
  933. opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos,
  934. OPJ_UINT32 len);
  935. /**
  936. * Reads an unknown marker
  937. *
  938. * @param p_j2k the jpeg2000 codec.
  939. * @param p_stream the stream object to read from.
  940. * @param output_marker FIXME DOC
  941. * @param p_manager the user event manager.
  942. *
  943. * @return true if the marker could be deduced.
  944. */
  945. static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k,
  946. opj_stream_private_t *p_stream,
  947. OPJ_UINT32 *output_marker,
  948. opj_event_mgr_t * p_manager);
  949. /**
  950. * Writes the MCT marker (Multiple Component Transform)
  951. *
  952. * @param p_j2k J2K codec.
  953. * @param p_mct_record FIXME DOC
  954. * @param p_stream the stream to write data to.
  955. * @param p_manager the user event manager.
  956. */
  957. static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k,
  958. opj_mct_data_t * p_mct_record,
  959. opj_stream_private_t *p_stream,
  960. opj_event_mgr_t * p_manager);
  961. /**
  962. * Reads a MCT marker (Multiple Component Transform)
  963. *
  964. * @param p_header_data the data contained in the MCT box.
  965. * @param p_j2k the jpeg2000 codec.
  966. * @param p_header_size the size of the data contained in the MCT marker.
  967. * @param p_manager the user event manager.
  968. */
  969. static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k,
  970. OPJ_BYTE * p_header_data,
  971. OPJ_UINT32 p_header_size,
  972. opj_event_mgr_t * p_manager);
  973. /**
  974. * Writes the MCC marker (Multiple Component Collection)
  975. *
  976. * @param p_j2k J2K codec.
  977. * @param p_mcc_record FIXME DOC
  978. * @param p_stream the stream to write data to.
  979. * @param p_manager the user event manager.
  980. */
  981. static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k,
  982. opj_simple_mcc_decorrelation_data_t * p_mcc_record,
  983. opj_stream_private_t *p_stream,
  984. opj_event_mgr_t * p_manager);
  985. /**
  986. * Reads a MCC marker (Multiple Component Collection)
  987. *
  988. * @param p_header_data the data contained in the MCC box.
  989. * @param p_j2k the jpeg2000 codec.
  990. * @param p_header_size the size of the data contained in the MCC marker.
  991. * @param p_manager the user event manager.
  992. */
  993. static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k,
  994. OPJ_BYTE * p_header_data,
  995. OPJ_UINT32 p_header_size,
  996. opj_event_mgr_t * p_manager);
  997. /**
  998. * Writes the MCO marker (Multiple component transformation ordering)
  999. *
  1000. * @param p_stream the stream to write data to.
  1001. * @param p_j2k J2K codec.
  1002. * @param p_manager the user event manager.
  1003. */
  1004. static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k,
  1005. opj_stream_private_t *p_stream,
  1006. opj_event_mgr_t * p_manager);
  1007. /**
  1008. * Reads a MCO marker (Multiple Component Transform Ordering)
  1009. *
  1010. * @param p_header_data the data contained in the MCO box.
  1011. * @param p_j2k the jpeg2000 codec.
  1012. * @param p_header_size the size of the data contained in the MCO marker.
  1013. * @param p_manager the user event manager.
  1014. */
  1015. static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k,
  1016. OPJ_BYTE * p_header_data,
  1017. OPJ_UINT32 p_header_size,
  1018. opj_event_mgr_t * p_manager);
  1019. static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image,
  1020. OPJ_UINT32 p_index);
  1021. static void opj_j2k_read_int16_to_float(const void * p_src_data,
  1022. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1023. static void opj_j2k_read_int32_to_float(const void * p_src_data,
  1024. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1025. static void opj_j2k_read_float32_to_float(const void * p_src_data,
  1026. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1027. static void opj_j2k_read_float64_to_float(const void * p_src_data,
  1028. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1029. static void opj_j2k_read_int16_to_int32(const void * p_src_data,
  1030. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1031. static void opj_j2k_read_int32_to_int32(const void * p_src_data,
  1032. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1033. static void opj_j2k_read_float32_to_int32(const void * p_src_data,
  1034. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1035. static void opj_j2k_read_float64_to_int32(const void * p_src_data,
  1036. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1037. static void opj_j2k_write_float_to_int16(const void * p_src_data,
  1038. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1039. static void opj_j2k_write_float_to_int32(const void * p_src_data,
  1040. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1041. static void opj_j2k_write_float_to_float(const void * p_src_data,
  1042. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1043. static void opj_j2k_write_float_to_float64(const void * p_src_data,
  1044. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1045. /**
  1046. * Ends the encoding, i.e. frees memory.
  1047. *
  1048. * @param p_stream the stream to write data to.
  1049. * @param p_j2k J2K codec.
  1050. * @param p_manager the user event manager.
  1051. */
  1052. static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k,
  1053. opj_stream_private_t *p_stream,
  1054. opj_event_mgr_t * p_manager);
  1055. /**
  1056. * Writes the CBD marker (Component bit depth definition)
  1057. *
  1058. * @param p_stream the stream to write data to.
  1059. * @param p_j2k J2K codec.
  1060. * @param p_manager the user event manager.
  1061. */
  1062. static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k,
  1063. opj_stream_private_t *p_stream,
  1064. opj_event_mgr_t * p_manager);
  1065. /**
  1066. * Reads a CBD marker (Component bit depth definition)
  1067. * @param p_header_data the data contained in the CBD box.
  1068. * @param p_j2k the jpeg2000 codec.
  1069. * @param p_header_size the size of the data contained in the CBD marker.
  1070. * @param p_manager the user event manager.
  1071. */
  1072. static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k,
  1073. OPJ_BYTE * p_header_data,
  1074. OPJ_UINT32 p_header_size,
  1075. opj_event_mgr_t * p_manager);
  1076. /**
  1077. * Reads a CAP marker (extended capabilities definition). Empty implementation.
  1078. * Found in HTJ2K files
  1079. *
  1080. * @param p_header_data the data contained in the CAP box.
  1081. * @param p_j2k the jpeg2000 codec.
  1082. * @param p_header_size the size of the data contained in the CAP marker.
  1083. * @param p_manager the user event manager.
  1084. */
  1085. static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k,
  1086. OPJ_BYTE * p_header_data,
  1087. OPJ_UINT32 p_header_size,
  1088. opj_event_mgr_t * p_manager);
  1089. /**
  1090. * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files
  1091. * @param p_header_data the data contained in the CPF box.
  1092. * @param p_j2k the jpeg2000 codec.
  1093. * @param p_header_size the size of the data contained in the CPF marker.
  1094. * @param p_manager the user event manager.
  1095. */
  1096. static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k,
  1097. OPJ_BYTE * p_header_data,
  1098. OPJ_UINT32 p_header_size,
  1099. opj_event_mgr_t * p_manager);
  1100. /**
  1101. * Writes COC marker for each component.
  1102. *
  1103. * @param p_stream the stream to write data to.
  1104. * @param p_j2k J2K codec.
  1105. * @param p_manager the user event manager.
  1106. */
  1107. static OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k,
  1108. opj_stream_private_t *p_stream,
  1109. opj_event_mgr_t * p_manager);
  1110. /**
  1111. * Writes QCC marker for each component.
  1112. *
  1113. * @param p_stream the stream to write data to.
  1114. * @param p_j2k J2K codec.
  1115. * @param p_manager the user event manager.
  1116. */
  1117. static OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k,
  1118. opj_stream_private_t *p_stream,
  1119. opj_event_mgr_t * p_manager);
  1120. /**
  1121. * Writes regions of interests.
  1122. *
  1123. * @param p_stream the stream to write data to.
  1124. * @param p_j2k J2K codec.
  1125. * @param p_manager the user event manager.
  1126. */
  1127. static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k,
  1128. opj_stream_private_t *p_stream,
  1129. opj_event_mgr_t * p_manager);
  1130. /**
  1131. * Writes EPC ????
  1132. *
  1133. * @param p_stream the stream to write data to.
  1134. * @param p_j2k J2K codec.
  1135. * @param p_manager the user event manager.
  1136. */
  1137. static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k,
  1138. opj_stream_private_t *p_stream,
  1139. opj_event_mgr_t * p_manager);
  1140. /**
  1141. * Checks the progression order changes values. Tells of the poc given as input are valid.
  1142. * A nice message is outputted at errors.
  1143. *
  1144. * @param p_pocs the progression order changes.
  1145. * @param tileno the tile number of interest
  1146. * @param p_nb_pocs the number of progression order changes.
  1147. * @param p_nb_resolutions the number of resolutions.
  1148. * @param numcomps the number of components
  1149. * @param numlayers the number of layers.
  1150. * @param p_manager the user event manager.
  1151. *
  1152. * @return true if the pocs are valid.
  1153. */
  1154. static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs,
  1155. OPJ_UINT32 tileno,
  1156. OPJ_UINT32 p_nb_pocs,
  1157. OPJ_UINT32 p_nb_resolutions,
  1158. OPJ_UINT32 numcomps,
  1159. OPJ_UINT32 numlayers,
  1160. opj_event_mgr_t * p_manager);
  1161. /**
  1162. * Gets the number of tile parts used for the given change of progression (if any) and the given tile.
  1163. *
  1164. * @param cp the coding parameters.
  1165. * @param pino the offset of the given poc (i.e. its position in the coding parameter).
  1166. * @param tileno the given tile.
  1167. *
  1168. * @return the number of tile parts.
  1169. */
  1170. static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino,
  1171. OPJ_UINT32 tileno);
  1172. /**
  1173. * Calculates the total number of tile parts needed by the encoder to
  1174. * encode such an image. If not enough memory is available, then the function return false.
  1175. *
  1176. * @param p_nb_tiles pointer that will hold the number of tile parts.
  1177. * @param cp the coding parameters for the image.
  1178. * @param image the image to encode.
  1179. * @param p_j2k the p_j2k encoder.
  1180. * @param p_manager the user event manager.
  1181. *
  1182. * @return true if the function was successful, false else.
  1183. */
  1184. static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k,
  1185. opj_cp_t *cp,
  1186. OPJ_UINT32 * p_nb_tiles,
  1187. opj_image_t *image,
  1188. opj_event_mgr_t * p_manager);
  1189. static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream);
  1190. static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream);
  1191. static opj_codestream_index_t* opj_j2k_create_cstr_index(void);
  1192. static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp);
  1193. static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp);
  1194. static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres);
  1195. static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters,
  1196. opj_image_t *image, opj_event_mgr_t *p_manager);
  1197. static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz,
  1198. opj_event_mgr_t *p_manager);
  1199. static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters,
  1200. opj_image_t *image, opj_event_mgr_t *p_manager);
  1201. static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters,
  1202. opj_image_t *image,
  1203. opj_event_mgr_t *p_manager);
  1204. /**
  1205. * Checks for invalid number of tile-parts in SOT marker (TPsot==TNsot). See issue 254.
  1206. *
  1207. * @param p_stream the stream to read data from.
  1208. * @param tile_no tile number we're looking for.
  1209. * @param p_correction_needed output value. if true, non conformant codestream needs TNsot correction.
  1210. * @param p_manager the user event manager.
  1211. *
  1212. * @return true if the function was successful, false else.
  1213. */
  1214. static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t
  1215. *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed,
  1216. opj_event_mgr_t * p_manager);
  1217. /*@}*/
  1218. /*@}*/
  1219. /* ----------------------------------------------------------------------- */
  1220. typedef struct j2k_prog_order {
  1221. OPJ_PROG_ORDER enum_prog;
  1222. char str_prog[5];
  1223. } j2k_prog_order_t;
  1224. static const j2k_prog_order_t j2k_prog_order_list[] = {
  1225. {OPJ_CPRL, "CPRL"},
  1226. {OPJ_LRCP, "LRCP"},
  1227. {OPJ_PCRL, "PCRL"},
  1228. {OPJ_RLCP, "RLCP"},
  1229. {OPJ_RPCL, "RPCL"},
  1230. {(OPJ_PROG_ORDER) - 1, ""}
  1231. };
  1232. /**
  1233. * FIXME DOC
  1234. */
  1235. static const OPJ_UINT32 MCT_ELEMENT_SIZE [] = {
  1236. 2,
  1237. 4,
  1238. 4,
  1239. 8
  1240. };
  1241. typedef void (* opj_j2k_mct_function)(const void * p_src_data,
  1242. void * p_dest_data, OPJ_UINT32 p_nb_elem);
  1243. static const opj_j2k_mct_function j2k_mct_read_functions_to_float [] = {
  1244. opj_j2k_read_int16_to_float,
  1245. opj_j2k_read_int32_to_float,
  1246. opj_j2k_read_float32_to_float,
  1247. opj_j2k_read_float64_to_float
  1248. };
  1249. static const opj_j2k_mct_function j2k_mct_read_functions_to_int32 [] = {
  1250. opj_j2k_read_int16_to_int32,
  1251. opj_j2k_read_int32_to_int32,
  1252. opj_j2k_read_float32_to_int32,
  1253. opj_j2k_read_float64_to_int32
  1254. };
  1255. static const opj_j2k_mct_function j2k_mct_write_functions_from_float [] = {
  1256. opj_j2k_write_float_to_int16,
  1257. opj_j2k_write_float_to_int32,
  1258. opj_j2k_write_float_to_float,
  1259. opj_j2k_write_float_to_float64
  1260. };
  1261. typedef struct opj_dec_memory_marker_handler {
  1262. /** marker value */
  1263. OPJ_UINT32 id;
  1264. /** value of the state when the marker can appear */
  1265. OPJ_UINT32 states;
  1266. /** action linked to the marker */
  1267. OPJ_BOOL(*handler)(opj_j2k_t *p_j2k,
  1268. OPJ_BYTE * p_header_data,
  1269. OPJ_UINT32 p_header_size,
  1270. opj_event_mgr_t * p_manager);
  1271. }
  1272. opj_dec_memory_marker_handler_t;
  1273. static const opj_dec_memory_marker_handler_t j2k_memory_marker_handler_tab [] =
  1274. {
  1275. {J2K_MS_SOT, J2K_STATE_MH | J2K_STATE_TPHSOT, opj_j2k_read_sot},
  1276. {J2K_MS_COD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_cod},
  1277. {J2K_MS_COC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_coc},
  1278. {J2K_MS_RGN, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_rgn},
  1279. {J2K_MS_QCD, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcd},
  1280. {J2K_MS_QCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_qcc},
  1281. {J2K_MS_POC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_poc},
  1282. {J2K_MS_SIZ, J2K_STATE_MHSIZ, opj_j2k_read_siz},
  1283. {J2K_MS_TLM, J2K_STATE_MH, opj_j2k_read_tlm},
  1284. {J2K_MS_PLM, J2K_STATE_MH, opj_j2k_read_plm},
  1285. {J2K_MS_PLT, J2K_STATE_TPH, opj_j2k_read_plt},
  1286. {J2K_MS_PPM, J2K_STATE_MH, opj_j2k_read_ppm},
  1287. {J2K_MS_PPT, J2K_STATE_TPH, opj_j2k_read_ppt},
  1288. {J2K_MS_SOP, 0, 0},
  1289. {J2K_MS_CRG, J2K_STATE_MH, opj_j2k_read_crg},
  1290. {J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
  1291. {J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
  1292. {J2K_MS_CBD, J2K_STATE_MH, opj_j2k_read_cbd},
  1293. {J2K_MS_CAP, J2K_STATE_MH, opj_j2k_read_cap},
  1294. {J2K_MS_CPF, J2K_STATE_MH, opj_j2k_read_cpf},
  1295. {J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
  1296. {J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
  1297. #ifdef USE_JPWL
  1298. #ifdef TODO_MS /* remove these functions which are not compatible with the v2 API */
  1299. {J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
  1300. {J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
  1301. {J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
  1302. {J2K_MS_RED, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_red},
  1303. #endif
  1304. #endif /* USE_JPWL */
  1305. #ifdef USE_JPSEC
  1306. {J2K_MS_SEC, J2K_DEC_STATE_MH, j2k_read_sec},
  1307. {J2K_MS_INSEC, 0, j2k_read_insec}
  1308. #endif /* USE_JPSEC */
  1309. {J2K_MS_UNK, J2K_STATE_MH | J2K_STATE_TPH, 0}/*opj_j2k_read_unk is directly used*/
  1310. };
  1311. static void opj_j2k_read_int16_to_float(const void * p_src_data,
  1312. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1313. {
  1314. OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
  1315. OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
  1316. OPJ_UINT32 i;
  1317. OPJ_UINT32 l_temp;
  1318. for (i = 0; i < p_nb_elem; ++i) {
  1319. opj_read_bytes(l_src_data, &l_temp, 2);
  1320. l_src_data += sizeof(OPJ_INT16);
  1321. *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
  1322. }
  1323. }
  1324. static void opj_j2k_read_int32_to_float(const void * p_src_data,
  1325. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1326. {
  1327. OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
  1328. OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
  1329. OPJ_UINT32 i;
  1330. OPJ_UINT32 l_temp;
  1331. for (i = 0; i < p_nb_elem; ++i) {
  1332. opj_read_bytes(l_src_data, &l_temp, 4);
  1333. l_src_data += sizeof(OPJ_INT32);
  1334. *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
  1335. }
  1336. }
  1337. static void opj_j2k_read_float32_to_float(const void * p_src_data,
  1338. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1339. {
  1340. OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
  1341. OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
  1342. OPJ_UINT32 i;
  1343. OPJ_FLOAT32 l_temp;
  1344. for (i = 0; i < p_nb_elem; ++i) {
  1345. opj_read_float(l_src_data, &l_temp);
  1346. l_src_data += sizeof(OPJ_FLOAT32);
  1347. *(l_dest_data++) = l_temp;
  1348. }
  1349. }
  1350. static void opj_j2k_read_float64_to_float(const void * p_src_data,
  1351. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1352. {
  1353. OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
  1354. OPJ_FLOAT32 * l_dest_data = (OPJ_FLOAT32 *) p_dest_data;
  1355. OPJ_UINT32 i;
  1356. OPJ_FLOAT64 l_temp;
  1357. for (i = 0; i < p_nb_elem; ++i) {
  1358. opj_read_double(l_src_data, &l_temp);
  1359. l_src_data += sizeof(OPJ_FLOAT64);
  1360. *(l_dest_data++) = (OPJ_FLOAT32) l_temp;
  1361. }
  1362. }
  1363. static void opj_j2k_read_int16_to_int32(const void * p_src_data,
  1364. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1365. {
  1366. OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
  1367. OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
  1368. OPJ_UINT32 i;
  1369. OPJ_UINT32 l_temp;
  1370. for (i = 0; i < p_nb_elem; ++i) {
  1371. opj_read_bytes(l_src_data, &l_temp, 2);
  1372. l_src_data += sizeof(OPJ_INT16);
  1373. *(l_dest_data++) = (OPJ_INT32) l_temp;
  1374. }
  1375. }
  1376. static void opj_j2k_read_int32_to_int32(const void * p_src_data,
  1377. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1378. {
  1379. OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
  1380. OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
  1381. OPJ_UINT32 i;
  1382. OPJ_UINT32 l_temp;
  1383. for (i = 0; i < p_nb_elem; ++i) {
  1384. opj_read_bytes(l_src_data, &l_temp, 4);
  1385. l_src_data += sizeof(OPJ_INT32);
  1386. *(l_dest_data++) = (OPJ_INT32) l_temp;
  1387. }
  1388. }
  1389. static void opj_j2k_read_float32_to_int32(const void * p_src_data,
  1390. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1391. {
  1392. OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
  1393. OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
  1394. OPJ_UINT32 i;
  1395. OPJ_FLOAT32 l_temp;
  1396. for (i = 0; i < p_nb_elem; ++i) {
  1397. opj_read_float(l_src_data, &l_temp);
  1398. l_src_data += sizeof(OPJ_FLOAT32);
  1399. *(l_dest_data++) = (OPJ_INT32) l_temp;
  1400. }
  1401. }
  1402. static void opj_j2k_read_float64_to_int32(const void * p_src_data,
  1403. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1404. {
  1405. OPJ_BYTE * l_src_data = (OPJ_BYTE *) p_src_data;
  1406. OPJ_INT32 * l_dest_data = (OPJ_INT32 *) p_dest_data;
  1407. OPJ_UINT32 i;
  1408. OPJ_FLOAT64 l_temp;
  1409. for (i = 0; i < p_nb_elem; ++i) {
  1410. opj_read_double(l_src_data, &l_temp);
  1411. l_src_data += sizeof(OPJ_FLOAT64);
  1412. *(l_dest_data++) = (OPJ_INT32) l_temp;
  1413. }
  1414. }
  1415. static void opj_j2k_write_float_to_int16(const void * p_src_data,
  1416. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1417. {
  1418. OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
  1419. OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
  1420. OPJ_UINT32 i;
  1421. OPJ_UINT32 l_temp;
  1422. for (i = 0; i < p_nb_elem; ++i) {
  1423. l_temp = (OPJ_UINT32) * (l_src_data++);
  1424. opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT16));
  1425. l_dest_data += sizeof(OPJ_INT16);
  1426. }
  1427. }
  1428. static void opj_j2k_write_float_to_int32(const void * p_src_data,
  1429. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1430. {
  1431. OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
  1432. OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
  1433. OPJ_UINT32 i;
  1434. OPJ_UINT32 l_temp;
  1435. for (i = 0; i < p_nb_elem; ++i) {
  1436. l_temp = (OPJ_UINT32) * (l_src_data++);
  1437. opj_write_bytes(l_dest_data, l_temp, sizeof(OPJ_INT32));
  1438. l_dest_data += sizeof(OPJ_INT32);
  1439. }
  1440. }
  1441. static void opj_j2k_write_float_to_float(const void * p_src_data,
  1442. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1443. {
  1444. OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
  1445. OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
  1446. OPJ_UINT32 i;
  1447. OPJ_FLOAT32 l_temp;
  1448. for (i = 0; i < p_nb_elem; ++i) {
  1449. l_temp = (OPJ_FLOAT32) * (l_src_data++);
  1450. opj_write_float(l_dest_data, l_temp);
  1451. l_dest_data += sizeof(OPJ_FLOAT32);
  1452. }
  1453. }
  1454. static void opj_j2k_write_float_to_float64(const void * p_src_data,
  1455. void * p_dest_data, OPJ_UINT32 p_nb_elem)
  1456. {
  1457. OPJ_BYTE * l_dest_data = (OPJ_BYTE *) p_dest_data;
  1458. OPJ_FLOAT32 * l_src_data = (OPJ_FLOAT32 *) p_src_data;
  1459. OPJ_UINT32 i;
  1460. OPJ_FLOAT64 l_temp;
  1461. for (i = 0; i < p_nb_elem; ++i) {
  1462. l_temp = (OPJ_FLOAT64) * (l_src_data++);
  1463. opj_write_double(l_dest_data, l_temp);
  1464. l_dest_data += sizeof(OPJ_FLOAT64);
  1465. }
  1466. }
  1467. const char *opj_j2k_convert_progression_order(OPJ_PROG_ORDER prg_order)
  1468. {
  1469. const j2k_prog_order_t *po;
  1470. for (po = j2k_prog_order_list; po->enum_prog != -1; po++) {
  1471. if (po->enum_prog == prg_order) {
  1472. return po->str_prog;
  1473. }
  1474. }
  1475. return po->str_prog;
  1476. }
  1477. static OPJ_BOOL opj_j2k_check_poc_val(const opj_poc_t *p_pocs,
  1478. OPJ_UINT32 tileno,
  1479. OPJ_UINT32 p_nb_pocs,
  1480. OPJ_UINT32 p_nb_resolutions,
  1481. OPJ_UINT32 p_num_comps,
  1482. OPJ_UINT32 p_num_layers,
  1483. opj_event_mgr_t * p_manager)
  1484. {
  1485. OPJ_UINT32* packet_array;
  1486. OPJ_UINT32 index, resno, compno, layno;
  1487. OPJ_UINT32 i;
  1488. OPJ_UINT32 step_c = 1;
  1489. OPJ_UINT32 step_r = p_num_comps * step_c;
  1490. OPJ_UINT32 step_l = p_nb_resolutions * step_r;
  1491. OPJ_BOOL loss = OPJ_FALSE;
  1492. assert(p_nb_pocs > 0);
  1493. packet_array = (OPJ_UINT32*) opj_calloc((size_t)step_l * p_num_layers,
  1494. sizeof(OPJ_UINT32));
  1495. if (packet_array == 00) {
  1496. opj_event_msg(p_manager, EVT_ERROR,
  1497. "Not enough memory for checking the poc values.\n");
  1498. return OPJ_FALSE;
  1499. }
  1500. /* iterate through all the pocs that match our tile of interest. */
  1501. for (i = 0; i < p_nb_pocs; ++i) {
  1502. const opj_poc_t *poc = &p_pocs[i];
  1503. if (tileno + 1 == poc->tile) {
  1504. index = step_r * poc->resno0;
  1505. /* take each resolution for each poc */
  1506. for (resno = poc->resno0 ;
  1507. resno < opj_uint_min(poc->resno1, p_nb_resolutions); ++resno) {
  1508. OPJ_UINT32 res_index = index + poc->compno0 * step_c;
  1509. /* take each comp of each resolution for each poc */
  1510. for (compno = poc->compno0 ;
  1511. compno < opj_uint_min(poc->compno1, p_num_comps); ++compno) {
  1512. /* The layer index always starts at zero for every progression. */
  1513. const OPJ_UINT32 layno0 = 0;
  1514. OPJ_UINT32 comp_index = res_index + layno0 * step_l;
  1515. /* and finally take each layer of each res of ... */
  1516. for (layno = layno0; layno < opj_uint_min(poc->layno1, p_num_layers);
  1517. ++layno) {
  1518. packet_array[comp_index] = 1;
  1519. comp_index += step_l;
  1520. }
  1521. res_index += step_c;
  1522. }
  1523. index += step_r;
  1524. }
  1525. }
  1526. }
  1527. index = 0;
  1528. for (layno = 0; layno < p_num_layers ; ++layno) {
  1529. for (resno = 0; resno < p_nb_resolutions; ++resno) {
  1530. for (compno = 0; compno < p_num_comps; ++compno) {
  1531. loss |= (packet_array[index] != 1);
  1532. #ifdef DEBUG_VERBOSE
  1533. if (packet_array[index] != 1) {
  1534. fprintf(stderr,
  1535. "Missing packet in POC: layno=%d resno=%d compno=%d\n",
  1536. layno, resno, compno);
  1537. }
  1538. #endif
  1539. index += step_c;
  1540. }
  1541. }
  1542. }
  1543. if (loss) {
  1544. opj_event_msg(p_manager, EVT_ERROR, "Missing packets possible loss of data\n");
  1545. }
  1546. opj_free(packet_array);
  1547. return !loss;
  1548. }
  1549. /* ----------------------------------------------------------------------- */
  1550. static OPJ_UINT32 opj_j2k_get_num_tp(opj_cp_t *cp, OPJ_UINT32 pino,
  1551. OPJ_UINT32 tileno)
  1552. {
  1553. const OPJ_CHAR *prog = 00;
  1554. OPJ_INT32 i;
  1555. OPJ_UINT32 tpnum = 1;
  1556. opj_tcp_t *tcp = 00;
  1557. opj_poc_t * l_current_poc = 00;
  1558. /* preconditions */
  1559. assert(tileno < (cp->tw * cp->th));
  1560. assert(pino < (cp->tcps[tileno].numpocs + 1));
  1561. /* get the given tile coding parameter */
  1562. tcp = &cp->tcps[tileno];
  1563. assert(tcp != 00);
  1564. l_current_poc = &(tcp->pocs[pino]);
  1565. assert(l_current_poc != 0);
  1566. /* get the progression order as a character string */
  1567. prog = opj_j2k_convert_progression_order(tcp->prg);
  1568. assert(strlen(prog) > 0);
  1569. if (cp->m_specific_param.m_enc.m_tp_on == 1) {
  1570. for (i = 0; i < 4; ++i) {
  1571. switch (prog[i]) {
  1572. /* component wise */
  1573. case 'C':
  1574. tpnum *= l_current_poc->compE;
  1575. break;
  1576. /* resolution wise */
  1577. case 'R':
  1578. tpnum *= l_current_poc->resE;
  1579. break;
  1580. /* precinct wise */
  1581. case 'P':
  1582. tpnum *= l_current_poc->prcE;
  1583. break;
  1584. /* layer wise */
  1585. case 'L':
  1586. tpnum *= l_current_poc->layE;
  1587. break;
  1588. }
  1589. /* would we split here ? */
  1590. if (cp->m_specific_param.m_enc.m_tp_flag == prog[i]) {
  1591. cp->m_specific_param.m_enc.m_tp_pos = i;
  1592. break;
  1593. }
  1594. }
  1595. } else {
  1596. tpnum = 1;
  1597. }
  1598. return tpnum;
  1599. }
  1600. static OPJ_BOOL opj_j2k_calculate_tp(opj_j2k_t *p_j2k,
  1601. opj_cp_t *cp,
  1602. OPJ_UINT32 * p_nb_tiles,
  1603. opj_image_t *image,
  1604. opj_event_mgr_t * p_manager
  1605. )
  1606. {
  1607. OPJ_UINT32 pino, tileno;
  1608. OPJ_UINT32 l_nb_tiles;
  1609. opj_tcp_t *tcp;
  1610. /* preconditions */
  1611. assert(p_nb_tiles != 00);
  1612. assert(cp != 00);
  1613. assert(image != 00);
  1614. assert(p_j2k != 00);
  1615. assert(p_manager != 00);
  1616. OPJ_UNUSED(p_j2k);
  1617. OPJ_UNUSED(p_manager);
  1618. l_nb_tiles = cp->tw * cp->th;
  1619. * p_nb_tiles = 0;
  1620. tcp = cp->tcps;
  1621. /* INDEX >> */
  1622. /* TODO mergeV2: check this part which use cstr_info */
  1623. /*if (p_j2k->cstr_info) {
  1624. opj_tile_info_t * l_info_tile_ptr = p_j2k->cstr_info->tile;
  1625. for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
  1626. OPJ_UINT32 cur_totnum_tp = 0;
  1627. opj_pi_update_encoding_parameters(image,cp,tileno);
  1628. for (pino = 0; pino <= tcp->numpocs; ++pino)
  1629. {
  1630. OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp,pino,tileno);
  1631. *p_nb_tiles = *p_nb_tiles + tp_num;
  1632. cur_totnum_tp += tp_num;
  1633. }
  1634. tcp->m_nb_tile_parts = cur_totnum_tp;
  1635. l_info_tile_ptr->tp = (opj_tp_info_t *) opj_malloc(cur_totnum_tp * sizeof(opj_tp_info_t));
  1636. if (l_info_tile_ptr->tp == 00) {
  1637. return OPJ_FALSE;
  1638. }
  1639. memset(l_info_tile_ptr->tp,0,cur_totnum_tp * sizeof(opj_tp_info_t));
  1640. l_info_tile_ptr->num_tps = cur_totnum_tp;
  1641. ++l_info_tile_ptr;
  1642. ++tcp;
  1643. }
  1644. }
  1645. else */{
  1646. for (tileno = 0; tileno < l_nb_tiles; ++tileno) {
  1647. OPJ_UINT32 cur_totnum_tp = 0;
  1648. opj_pi_update_encoding_parameters(image, cp, tileno);
  1649. for (pino = 0; pino <= tcp->numpocs; ++pino) {
  1650. OPJ_UINT32 tp_num = opj_j2k_get_num_tp(cp, pino, tileno);
  1651. *p_nb_tiles = *p_nb_tiles + tp_num;
  1652. cur_totnum_tp += tp_num;
  1653. }
  1654. tcp->m_nb_tile_parts = cur_totnum_tp;
  1655. ++tcp;
  1656. }
  1657. }
  1658. return OPJ_TRUE;
  1659. }
  1660. static OPJ_BOOL opj_j2k_write_soc(opj_j2k_t *p_j2k,
  1661. opj_stream_private_t *p_stream,
  1662. opj_event_mgr_t * p_manager)
  1663. {
  1664. /* 2 bytes will be written */
  1665. OPJ_BYTE * l_start_stream = 00;
  1666. /* preconditions */
  1667. assert(p_stream != 00);
  1668. assert(p_j2k != 00);
  1669. assert(p_manager != 00);
  1670. l_start_stream = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  1671. /* write SOC identifier */
  1672. opj_write_bytes(l_start_stream, J2K_MS_SOC, 2);
  1673. if (opj_stream_write_data(p_stream, l_start_stream, 2, p_manager) != 2) {
  1674. return OPJ_FALSE;
  1675. }
  1676. /* UniPG>> */
  1677. #ifdef USE_JPWL
  1678. /* update markers struct */
  1679. /*
  1680. OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOC, p_stream_tell(p_stream) - 2, 2);
  1681. */
  1682. assert(0 && "TODO");
  1683. #endif /* USE_JPWL */
  1684. /* <<UniPG */
  1685. return OPJ_TRUE;
  1686. }
  1687. /**
  1688. * Reads a SOC marker (Start of Codestream)
  1689. * @param p_j2k the jpeg2000 file codec.
  1690. * @param p_stream FIXME DOC
  1691. * @param p_manager the user event manager.
  1692. */
  1693. static OPJ_BOOL opj_j2k_read_soc(opj_j2k_t *p_j2k,
  1694. opj_stream_private_t *p_stream,
  1695. opj_event_mgr_t * p_manager
  1696. )
  1697. {
  1698. OPJ_BYTE l_data [2];
  1699. OPJ_UINT32 l_marker;
  1700. /* preconditions */
  1701. assert(p_j2k != 00);
  1702. assert(p_manager != 00);
  1703. assert(p_stream != 00);
  1704. if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) {
  1705. return OPJ_FALSE;
  1706. }
  1707. opj_read_bytes(l_data, &l_marker, 2);
  1708. if (l_marker != J2K_MS_SOC) {
  1709. return OPJ_FALSE;
  1710. }
  1711. /* Next marker should be a SIZ marker in the main header */
  1712. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSIZ;
  1713. /* FIXME move it in a index structure included in p_j2k*/
  1714. p_j2k->cstr_index->main_head_start = opj_stream_tell(p_stream) - 2;
  1715. opj_event_msg(p_manager, EVT_INFO,
  1716. "Start to read j2k main header (%" PRId64 ").\n",
  1717. p_j2k->cstr_index->main_head_start);
  1718. /* Add the marker to the codestream index*/
  1719. if (OPJ_FALSE == opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_SOC,
  1720. p_j2k->cstr_index->main_head_start, 2)) {
  1721. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
  1722. return OPJ_FALSE;
  1723. }
  1724. return OPJ_TRUE;
  1725. }
  1726. static OPJ_BOOL opj_j2k_write_siz(opj_j2k_t *p_j2k,
  1727. opj_stream_private_t *p_stream,
  1728. opj_event_mgr_t * p_manager)
  1729. {
  1730. OPJ_UINT32 i;
  1731. OPJ_UINT32 l_size_len;
  1732. OPJ_BYTE * l_current_ptr;
  1733. opj_image_t * l_image = 00;
  1734. opj_cp_t *cp = 00;
  1735. opj_image_comp_t * l_img_comp = 00;
  1736. /* preconditions */
  1737. assert(p_stream != 00);
  1738. assert(p_j2k != 00);
  1739. assert(p_manager != 00);
  1740. l_image = p_j2k->m_private_image;
  1741. cp = &(p_j2k->m_cp);
  1742. l_size_len = 40 + 3 * l_image->numcomps;
  1743. l_img_comp = l_image->comps;
  1744. if (l_size_len > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  1745. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  1746. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len);
  1747. if (! new_header_tile_data) {
  1748. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  1749. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  1750. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  1751. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory for the SIZ marker\n");
  1752. return OPJ_FALSE;
  1753. }
  1754. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  1755. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_size_len;
  1756. }
  1757. l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  1758. /* write SOC identifier */
  1759. opj_write_bytes(l_current_ptr, J2K_MS_SIZ, 2); /* SIZ */
  1760. l_current_ptr += 2;
  1761. opj_write_bytes(l_current_ptr, l_size_len - 2, 2); /* L_SIZ */
  1762. l_current_ptr += 2;
  1763. opj_write_bytes(l_current_ptr, cp->rsiz, 2); /* Rsiz (capabilities) */
  1764. l_current_ptr += 2;
  1765. opj_write_bytes(l_current_ptr, l_image->x1, 4); /* Xsiz */
  1766. l_current_ptr += 4;
  1767. opj_write_bytes(l_current_ptr, l_image->y1, 4); /* Ysiz */
  1768. l_current_ptr += 4;
  1769. opj_write_bytes(l_current_ptr, l_image->x0, 4); /* X0siz */
  1770. l_current_ptr += 4;
  1771. opj_write_bytes(l_current_ptr, l_image->y0, 4); /* Y0siz */
  1772. l_current_ptr += 4;
  1773. opj_write_bytes(l_current_ptr, cp->tdx, 4); /* XTsiz */
  1774. l_current_ptr += 4;
  1775. opj_write_bytes(l_current_ptr, cp->tdy, 4); /* YTsiz */
  1776. l_current_ptr += 4;
  1777. opj_write_bytes(l_current_ptr, cp->tx0, 4); /* XT0siz */
  1778. l_current_ptr += 4;
  1779. opj_write_bytes(l_current_ptr, cp->ty0, 4); /* YT0siz */
  1780. l_current_ptr += 4;
  1781. opj_write_bytes(l_current_ptr, l_image->numcomps, 2); /* Csiz */
  1782. l_current_ptr += 2;
  1783. for (i = 0; i < l_image->numcomps; ++i) {
  1784. /* TODO here with MCT ? */
  1785. opj_write_bytes(l_current_ptr, l_img_comp->prec - 1 + (l_img_comp->sgnd << 7),
  1786. 1); /* Ssiz_i */
  1787. ++l_current_ptr;
  1788. opj_write_bytes(l_current_ptr, l_img_comp->dx, 1); /* XRsiz_i */
  1789. ++l_current_ptr;
  1790. opj_write_bytes(l_current_ptr, l_img_comp->dy, 1); /* YRsiz_i */
  1791. ++l_current_ptr;
  1792. ++l_img_comp;
  1793. }
  1794. if (opj_stream_write_data(p_stream,
  1795. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_size_len,
  1796. p_manager) != l_size_len) {
  1797. return OPJ_FALSE;
  1798. }
  1799. return OPJ_TRUE;
  1800. }
  1801. /**
  1802. * Reads a SIZ marker (image and tile size)
  1803. * @param p_j2k the jpeg2000 file codec.
  1804. * @param p_header_data the data contained in the SIZ box.
  1805. * @param p_header_size the size of the data contained in the SIZ marker.
  1806. * @param p_manager the user event manager.
  1807. */
  1808. static OPJ_BOOL opj_j2k_read_siz(opj_j2k_t *p_j2k,
  1809. OPJ_BYTE * p_header_data,
  1810. OPJ_UINT32 p_header_size,
  1811. opj_event_mgr_t * p_manager
  1812. )
  1813. {
  1814. OPJ_UINT32 i;
  1815. OPJ_UINT32 l_nb_comp;
  1816. OPJ_UINT32 l_nb_comp_remain;
  1817. OPJ_UINT32 l_remaining_size;
  1818. OPJ_UINT32 l_nb_tiles;
  1819. OPJ_UINT32 l_tmp, l_tx1, l_ty1;
  1820. OPJ_UINT32 l_prec0, l_sgnd0;
  1821. opj_image_t *l_image = 00;
  1822. opj_cp_t *l_cp = 00;
  1823. opj_image_comp_t * l_img_comp = 00;
  1824. opj_tcp_t * l_current_tile_param = 00;
  1825. /* preconditions */
  1826. assert(p_j2k != 00);
  1827. assert(p_manager != 00);
  1828. assert(p_header_data != 00);
  1829. l_image = p_j2k->m_private_image;
  1830. l_cp = &(p_j2k->m_cp);
  1831. /* minimum size == 39 - 3 (= minimum component parameter) */
  1832. if (p_header_size < 36) {
  1833. opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
  1834. return OPJ_FALSE;
  1835. }
  1836. l_remaining_size = p_header_size - 36;
  1837. l_nb_comp = l_remaining_size / 3;
  1838. l_nb_comp_remain = l_remaining_size % 3;
  1839. if (l_nb_comp_remain != 0) {
  1840. opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker size\n");
  1841. return OPJ_FALSE;
  1842. }
  1843. opj_read_bytes(p_header_data, &l_tmp,
  1844. 2); /* Rsiz (capabilities) */
  1845. p_header_data += 2;
  1846. l_cp->rsiz = (OPJ_UINT16) l_tmp;
  1847. opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
  1848. p_header_data += 4;
  1849. opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
  1850. p_header_data += 4;
  1851. opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x0, 4); /* X0siz */
  1852. p_header_data += 4;
  1853. opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y0, 4); /* Y0siz */
  1854. p_header_data += 4;
  1855. opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdx,
  1856. 4); /* XTsiz */
  1857. p_header_data += 4;
  1858. opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tdy,
  1859. 4); /* YTsiz */
  1860. p_header_data += 4;
  1861. opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->tx0,
  1862. 4); /* XT0siz */
  1863. p_header_data += 4;
  1864. opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_cp->ty0,
  1865. 4); /* YT0siz */
  1866. p_header_data += 4;
  1867. opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_tmp,
  1868. 2); /* Csiz */
  1869. p_header_data += 2;
  1870. if (l_tmp < 16385) {
  1871. l_image->numcomps = (OPJ_UINT16) l_tmp;
  1872. } else {
  1873. opj_event_msg(p_manager, EVT_ERROR,
  1874. "Error with SIZ marker: number of component is illegal -> %d\n", l_tmp);
  1875. return OPJ_FALSE;
  1876. }
  1877. if (l_image->numcomps != l_nb_comp) {
  1878. opj_event_msg(p_manager, EVT_ERROR,
  1879. "Error with SIZ marker: number of component is not compatible with the remaining number of parameters ( %d vs %d)\n",
  1880. l_image->numcomps, l_nb_comp);
  1881. return OPJ_FALSE;
  1882. }
  1883. /* testcase 4035.pdf.SIGSEGV.d8b.3375 */
  1884. /* testcase issue427-null-image-size.jp2 */
  1885. if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) {
  1886. opj_event_msg(p_manager, EVT_ERROR,
  1887. "Error with SIZ marker: negative or zero image size (%" PRId64 " x %" PRId64
  1888. ")\n", (OPJ_INT64)l_image->x1 - l_image->x0,
  1889. (OPJ_INT64)l_image->y1 - l_image->y0);
  1890. return OPJ_FALSE;
  1891. }
  1892. /* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
  1893. if ((l_cp->tdx == 0U) || (l_cp->tdy == 0U)) {
  1894. opj_event_msg(p_manager, EVT_ERROR,
  1895. "Error with SIZ marker: invalid tile size (tdx: %d, tdy: %d)\n", l_cp->tdx,
  1896. l_cp->tdy);
  1897. return OPJ_FALSE;
  1898. }
  1899. /* testcase issue427-illegal-tile-offset.jp2 */
  1900. l_tx1 = opj_uint_adds(l_cp->tx0, l_cp->tdx); /* manage overflow */
  1901. l_ty1 = opj_uint_adds(l_cp->ty0, l_cp->tdy); /* manage overflow */
  1902. if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) ||
  1903. (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0)) {
  1904. opj_event_msg(p_manager, EVT_ERROR,
  1905. "Error with SIZ marker: illegal tile offset\n");
  1906. return OPJ_FALSE;
  1907. }
  1908. if (!p_j2k->dump_state) {
  1909. OPJ_UINT32 siz_w, siz_h;
  1910. siz_w = l_image->x1 - l_image->x0;
  1911. siz_h = l_image->y1 - l_image->y0;
  1912. if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0
  1913. && (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) {
  1914. opj_event_msg(p_manager, EVT_ERROR,
  1915. "Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n", p_j2k->ihdr_w,
  1916. p_j2k->ihdr_h, siz_w, siz_h);
  1917. return OPJ_FALSE;
  1918. }
  1919. }
  1920. #ifdef USE_JPWL
  1921. if (l_cp->correct) {
  1922. /* if JPWL is on, we check whether TX errors have damaged
  1923. too much the SIZ parameters */
  1924. if (!(l_image->x1 * l_image->y1)) {
  1925. opj_event_msg(p_manager, EVT_ERROR,
  1926. "JPWL: bad image size (%d x %d)\n",
  1927. l_image->x1, l_image->y1);
  1928. if (!JPWL_ASSUME) {
  1929. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  1930. return OPJ_FALSE;
  1931. }
  1932. }
  1933. /* FIXME check previously in the function so why keep this piece of code ? Need by the norm ?
  1934. if (l_image->numcomps != ((len - 38) / 3)) {
  1935. opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  1936. "JPWL: Csiz is %d => space in SIZ only for %d comps.!!!\n",
  1937. l_image->numcomps, ((len - 38) / 3));
  1938. if (!JPWL_ASSUME) {
  1939. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  1940. return OPJ_FALSE;
  1941. }
  1942. */ /* we try to correct */
  1943. /* opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n");
  1944. if (l_image->numcomps < ((len - 38) / 3)) {
  1945. len = 38 + 3 * l_image->numcomps;
  1946. opj_event_msg(p_manager, EVT_WARNING, "- setting Lsiz to %d => HYPOTHESIS!!!\n",
  1947. len);
  1948. } else {
  1949. l_image->numcomps = ((len - 38) / 3);
  1950. opj_event_msg(p_manager, EVT_WARNING, "- setting Csiz to %d => HYPOTHESIS!!!\n",
  1951. l_image->numcomps);
  1952. }
  1953. }
  1954. */
  1955. /* update components number in the jpwl_exp_comps filed */
  1956. l_cp->exp_comps = l_image->numcomps;
  1957. }
  1958. #endif /* USE_JPWL */
  1959. /* Allocate the resulting image components */
  1960. l_image->comps = (opj_image_comp_t*) opj_calloc(l_image->numcomps,
  1961. sizeof(opj_image_comp_t));
  1962. if (l_image->comps == 00) {
  1963. l_image->numcomps = 0;
  1964. opj_event_msg(p_manager, EVT_ERROR,
  1965. "Not enough memory to take in charge SIZ marker\n");
  1966. return OPJ_FALSE;
  1967. }
  1968. l_img_comp = l_image->comps;
  1969. l_prec0 = 0;
  1970. l_sgnd0 = 0;
  1971. /* Read the component information */
  1972. for (i = 0; i < l_image->numcomps; ++i) {
  1973. OPJ_UINT32 tmp;
  1974. opj_read_bytes(p_header_data, &tmp, 1); /* Ssiz_i */
  1975. ++p_header_data;
  1976. l_img_comp->prec = (tmp & 0x7f) + 1;
  1977. l_img_comp->sgnd = tmp >> 7;
  1978. if (p_j2k->dump_state == 0) {
  1979. if (i == 0) {
  1980. l_prec0 = l_img_comp->prec;
  1981. l_sgnd0 = l_img_comp->sgnd;
  1982. } else if (!l_cp->allow_different_bit_depth_sign
  1983. && (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) {
  1984. opj_event_msg(p_manager, EVT_WARNING,
  1985. "Despite JP2 BPC!=255, precision and/or sgnd values for comp[%d] is different than comp[0]:\n"
  1986. " [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n", i, l_prec0, l_sgnd0,
  1987. i, l_img_comp->prec, l_img_comp->sgnd);
  1988. }
  1989. /* TODO: we should perhaps also check against JP2 BPCC values */
  1990. }
  1991. opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */
  1992. ++p_header_data;
  1993. l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
  1994. opj_read_bytes(p_header_data, &tmp, 1); /* YRsiz_i */
  1995. ++p_header_data;
  1996. l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
  1997. if (l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
  1998. l_img_comp->dy < 1 || l_img_comp->dy > 255) {
  1999. opj_event_msg(p_manager, EVT_ERROR,
  2000. "Invalid values for comp = %d : dx=%u dy=%u (should be between 1 and 255 according to the JPEG2000 norm)\n",
  2001. i, l_img_comp->dx, l_img_comp->dy);
  2002. return OPJ_FALSE;
  2003. }
  2004. /* Avoids later undefined shift in computation of */
  2005. /* p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1
  2006. << (l_image->comps[i].prec - 1); */
  2007. if (l_img_comp->prec > 31) {
  2008. opj_event_msg(p_manager, EVT_ERROR,
  2009. "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n",
  2010. i, l_img_comp->prec);
  2011. return OPJ_FALSE;
  2012. }
  2013. #ifdef USE_JPWL
  2014. if (l_cp->correct) {
  2015. /* if JPWL is on, we check whether TX errors have damaged
  2016. too much the SIZ parameters, again */
  2017. if (!(l_image->comps[i].dx * l_image->comps[i].dy)) {
  2018. opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  2019. "JPWL: bad XRsiz_%d/YRsiz_%d (%d x %d)\n",
  2020. i, i, l_image->comps[i].dx, l_image->comps[i].dy);
  2021. if (!JPWL_ASSUME) {
  2022. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  2023. return OPJ_FALSE;
  2024. }
  2025. /* we try to correct */
  2026. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
  2027. if (!l_image->comps[i].dx) {
  2028. l_image->comps[i].dx = 1;
  2029. opj_event_msg(p_manager, EVT_WARNING,
  2030. "- setting XRsiz_%d to %d => HYPOTHESIS!!!\n",
  2031. i, l_image->comps[i].dx);
  2032. }
  2033. if (!l_image->comps[i].dy) {
  2034. l_image->comps[i].dy = 1;
  2035. opj_event_msg(p_manager, EVT_WARNING,
  2036. "- setting YRsiz_%d to %d => HYPOTHESIS!!!\n",
  2037. i, l_image->comps[i].dy);
  2038. }
  2039. }
  2040. }
  2041. #endif /* USE_JPWL */
  2042. l_img_comp->resno_decoded =
  2043. 0; /* number of resolution decoded */
  2044. l_img_comp->factor =
  2045. l_cp->m_specific_param.m_dec.m_reduce; /* reducing factor per component */
  2046. ++l_img_comp;
  2047. }
  2048. if (l_cp->tdx == 0 || l_cp->tdy == 0) {
  2049. return OPJ_FALSE;
  2050. }
  2051. /* Compute the number of tiles */
  2052. l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0),
  2053. (OPJ_INT32)l_cp->tdx);
  2054. l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0),
  2055. (OPJ_INT32)l_cp->tdy);
  2056. /* Check that the number of tiles is valid */
  2057. if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
  2058. opj_event_msg(p_manager, EVT_ERROR,
  2059. "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
  2060. l_cp->tw, l_cp->th);
  2061. return OPJ_FALSE;
  2062. }
  2063. l_nb_tiles = l_cp->tw * l_cp->th;
  2064. /* Define the tiles which will be decoded */
  2065. if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
  2066. p_j2k->m_specific_param.m_decoder.m_start_tile_x =
  2067. (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;
  2068. p_j2k->m_specific_param.m_decoder.m_start_tile_y =
  2069. (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;
  2070. p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv((
  2071. OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0),
  2072. (OPJ_INT32)l_cp->tdx);
  2073. p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv((
  2074. OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0),
  2075. (OPJ_INT32)l_cp->tdy);
  2076. } else {
  2077. p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
  2078. p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
  2079. p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
  2080. p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
  2081. }
  2082. #ifdef USE_JPWL
  2083. if (l_cp->correct) {
  2084. /* if JPWL is on, we check whether TX errors have damaged
  2085. too much the SIZ parameters */
  2086. if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) ||
  2087. (l_cp->th > l_cp->max_tiles)) {
  2088. opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  2089. "JPWL: bad number of tiles (%d x %d)\n",
  2090. l_cp->tw, l_cp->th);
  2091. if (!JPWL_ASSUME) {
  2092. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  2093. return OPJ_FALSE;
  2094. }
  2095. /* we try to correct */
  2096. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
  2097. if (l_cp->tw < 1) {
  2098. l_cp->tw = 1;
  2099. opj_event_msg(p_manager, EVT_WARNING,
  2100. "- setting %d tiles in x => HYPOTHESIS!!!\n",
  2101. l_cp->tw);
  2102. }
  2103. if (l_cp->tw > l_cp->max_tiles) {
  2104. l_cp->tw = 1;
  2105. opj_event_msg(p_manager, EVT_WARNING,
  2106. "- too large x, increase expectance of %d\n"
  2107. "- setting %d tiles in x => HYPOTHESIS!!!\n",
  2108. l_cp->max_tiles, l_cp->tw);
  2109. }
  2110. if (l_cp->th < 1) {
  2111. l_cp->th = 1;
  2112. opj_event_msg(p_manager, EVT_WARNING,
  2113. "- setting %d tiles in y => HYPOTHESIS!!!\n",
  2114. l_cp->th);
  2115. }
  2116. if (l_cp->th > l_cp->max_tiles) {
  2117. l_cp->th = 1;
  2118. opj_event_msg(p_manager, EVT_WARNING,
  2119. "- too large y, increase expectance of %d to continue\n",
  2120. "- setting %d tiles in y => HYPOTHESIS!!!\n",
  2121. l_cp->max_tiles, l_cp->th);
  2122. }
  2123. }
  2124. }
  2125. #endif /* USE_JPWL */
  2126. /* memory allocations */
  2127. l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));
  2128. if (l_cp->tcps == 00) {
  2129. opj_event_msg(p_manager, EVT_ERROR,
  2130. "Not enough memory to take in charge SIZ marker\n");
  2131. return OPJ_FALSE;
  2132. }
  2133. #ifdef USE_JPWL
  2134. if (l_cp->correct) {
  2135. if (!l_cp->tcps) {
  2136. opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  2137. "JPWL: could not alloc tcps field of cp\n");
  2138. if (!JPWL_ASSUME) {
  2139. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  2140. return OPJ_FALSE;
  2141. }
  2142. }
  2143. }
  2144. #endif /* USE_JPWL */
  2145. p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
  2146. (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
  2147. if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
  2148. opj_event_msg(p_manager, EVT_ERROR,
  2149. "Not enough memory to take in charge SIZ marker\n");
  2150. return OPJ_FALSE;
  2151. }
  2152. p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
  2153. (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS,
  2154. sizeof(opj_mct_data_t));
  2155. if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
  2156. opj_event_msg(p_manager, EVT_ERROR,
  2157. "Not enough memory to take in charge SIZ marker\n");
  2158. return OPJ_FALSE;
  2159. }
  2160. p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records =
  2161. OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  2162. p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
  2163. (opj_simple_mcc_decorrelation_data_t*)
  2164. opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS,
  2165. sizeof(opj_simple_mcc_decorrelation_data_t));
  2166. if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
  2167. opj_event_msg(p_manager, EVT_ERROR,
  2168. "Not enough memory to take in charge SIZ marker\n");
  2169. return OPJ_FALSE;
  2170. }
  2171. p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records =
  2172. OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
  2173. /* set up default dc level shift */
  2174. for (i = 0; i < l_image->numcomps; ++i) {
  2175. if (! l_image->comps[i].sgnd) {
  2176. p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1
  2177. << (l_image->comps[i].prec - 1);
  2178. }
  2179. }
  2180. l_current_tile_param = l_cp->tcps;
  2181. for (i = 0; i < l_nb_tiles; ++i) {
  2182. l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps,
  2183. sizeof(opj_tccp_t));
  2184. if (l_current_tile_param->tccps == 00) {
  2185. opj_event_msg(p_manager, EVT_ERROR,
  2186. "Not enough memory to take in charge SIZ marker\n");
  2187. return OPJ_FALSE;
  2188. }
  2189. ++l_current_tile_param;
  2190. }
  2191. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH;
  2192. opj_image_comp_header_update(l_image, l_cp);
  2193. return OPJ_TRUE;
  2194. }
  2195. static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k,
  2196. opj_stream_private_t *p_stream,
  2197. opj_event_mgr_t * p_manager
  2198. )
  2199. {
  2200. OPJ_UINT32 l_comment_size;
  2201. OPJ_UINT32 l_total_com_size;
  2202. const OPJ_CHAR *l_comment;
  2203. OPJ_BYTE * l_current_ptr = 00;
  2204. /* preconditions */
  2205. assert(p_j2k != 00);
  2206. assert(p_stream != 00);
  2207. assert(p_manager != 00);
  2208. l_comment = p_j2k->m_cp.comment;
  2209. l_comment_size = (OPJ_UINT32)strlen(l_comment);
  2210. l_total_com_size = l_comment_size + 6;
  2211. if (l_total_com_size >
  2212. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2213. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2214. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size);
  2215. if (! new_header_tile_data) {
  2216. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2217. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2218. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2219. opj_event_msg(p_manager, EVT_ERROR,
  2220. "Not enough memory to write the COM marker\n");
  2221. return OPJ_FALSE;
  2222. }
  2223. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2224. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
  2225. }
  2226. l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  2227. opj_write_bytes(l_current_ptr, J2K_MS_COM, 2); /* COM */
  2228. l_current_ptr += 2;
  2229. opj_write_bytes(l_current_ptr, l_total_com_size - 2, 2); /* L_COM */
  2230. l_current_ptr += 2;
  2231. opj_write_bytes(l_current_ptr, 1,
  2232. 2); /* General use (IS 8859-15:1999 (Latin) values) */
  2233. l_current_ptr += 2;
  2234. memcpy(l_current_ptr, l_comment, l_comment_size);
  2235. if (opj_stream_write_data(p_stream,
  2236. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size,
  2237. p_manager) != l_total_com_size) {
  2238. return OPJ_FALSE;
  2239. }
  2240. return OPJ_TRUE;
  2241. }
  2242. /**
  2243. * Reads a COM marker (comments)
  2244. * @param p_j2k the jpeg2000 file codec.
  2245. * @param p_header_data the data contained in the COM box.
  2246. * @param p_header_size the size of the data contained in the COM marker.
  2247. * @param p_manager the user event manager.
  2248. */
  2249. static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k,
  2250. OPJ_BYTE * p_header_data,
  2251. OPJ_UINT32 p_header_size,
  2252. opj_event_mgr_t * p_manager
  2253. )
  2254. {
  2255. /* preconditions */
  2256. assert(p_j2k != 00);
  2257. assert(p_manager != 00);
  2258. assert(p_header_data != 00);
  2259. OPJ_UNUSED(p_j2k);
  2260. OPJ_UNUSED(p_header_data);
  2261. OPJ_UNUSED(p_header_size);
  2262. OPJ_UNUSED(p_manager);
  2263. return OPJ_TRUE;
  2264. }
  2265. static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k,
  2266. opj_stream_private_t *p_stream,
  2267. opj_event_mgr_t * p_manager)
  2268. {
  2269. opj_cp_t *l_cp = 00;
  2270. opj_tcp_t *l_tcp = 00;
  2271. OPJ_UINT32 l_code_size, l_remaining_size;
  2272. OPJ_BYTE * l_current_data = 00;
  2273. /* preconditions */
  2274. assert(p_j2k != 00);
  2275. assert(p_manager != 00);
  2276. assert(p_stream != 00);
  2277. l_cp = &(p_j2k->m_cp);
  2278. l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
  2279. l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
  2280. p_j2k->m_current_tile_number, 0);
  2281. l_remaining_size = l_code_size;
  2282. if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2283. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2284. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
  2285. if (! new_header_tile_data) {
  2286. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2287. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2288. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2289. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
  2290. return OPJ_FALSE;
  2291. }
  2292. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2293. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
  2294. }
  2295. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  2296. opj_write_bytes(l_current_data, J2K_MS_COD, 2); /* COD */
  2297. l_current_data += 2;
  2298. opj_write_bytes(l_current_data, l_code_size - 2, 2); /* L_COD */
  2299. l_current_data += 2;
  2300. opj_write_bytes(l_current_data, l_tcp->csty, 1); /* Scod */
  2301. ++l_current_data;
  2302. opj_write_bytes(l_current_data, (OPJ_UINT32)l_tcp->prg, 1); /* SGcod (A) */
  2303. ++l_current_data;
  2304. opj_write_bytes(l_current_data, l_tcp->numlayers, 2); /* SGcod (B) */
  2305. l_current_data += 2;
  2306. opj_write_bytes(l_current_data, l_tcp->mct, 1); /* SGcod (C) */
  2307. ++l_current_data;
  2308. l_remaining_size -= 9;
  2309. if (! opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0,
  2310. l_current_data, &l_remaining_size, p_manager)) {
  2311. opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
  2312. return OPJ_FALSE;
  2313. }
  2314. if (l_remaining_size != 0) {
  2315. opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
  2316. return OPJ_FALSE;
  2317. }
  2318. if (opj_stream_write_data(p_stream,
  2319. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size,
  2320. p_manager) != l_code_size) {
  2321. return OPJ_FALSE;
  2322. }
  2323. return OPJ_TRUE;
  2324. }
  2325. /**
  2326. * Reads a COD marker (Coding style defaults)
  2327. * @param p_header_data the data contained in the COD box.
  2328. * @param p_j2k the jpeg2000 codec.
  2329. * @param p_header_size the size of the data contained in the COD marker.
  2330. * @param p_manager the user event manager.
  2331. */
  2332. static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k,
  2333. OPJ_BYTE * p_header_data,
  2334. OPJ_UINT32 p_header_size,
  2335. opj_event_mgr_t * p_manager
  2336. )
  2337. {
  2338. /* loop */
  2339. OPJ_UINT32 i;
  2340. OPJ_UINT32 l_tmp;
  2341. opj_cp_t *l_cp = 00;
  2342. opj_tcp_t *l_tcp = 00;
  2343. opj_image_t *l_image = 00;
  2344. /* preconditions */
  2345. assert(p_header_data != 00);
  2346. assert(p_j2k != 00);
  2347. assert(p_manager != 00);
  2348. l_image = p_j2k->m_private_image;
  2349. l_cp = &(p_j2k->m_cp);
  2350. /* If we are in the first tile-part header of the current tile */
  2351. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
  2352. &l_cp->tcps[p_j2k->m_current_tile_number] :
  2353. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  2354. #if 0
  2355. /* This check was added per https://github.com/uclouvain/openjpeg/commit/daed8cc9195555e101ab708a501af2dfe6d5e001 */
  2356. /* but this is no longer necessary to handle issue476.jp2 */
  2357. /* and this actually cause issues on legit files. See https://github.com/uclouvain/openjpeg/issues/1043 */
  2358. /* Only one COD per tile */
  2359. if (l_tcp->cod) {
  2360. opj_event_msg(p_manager, EVT_ERROR,
  2361. "COD marker already read. No more than one COD marker per tile.\n");
  2362. return OPJ_FALSE;
  2363. }
  2364. #endif
  2365. l_tcp->cod = 1;
  2366. /* Make sure room is sufficient */
  2367. if (p_header_size < 5) {
  2368. opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
  2369. return OPJ_FALSE;
  2370. }
  2371. opj_read_bytes(p_header_data, &l_tcp->csty, 1); /* Scod */
  2372. ++p_header_data;
  2373. /* Make sure we know how to decode this */
  2374. if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP |
  2375. J2K_CP_CSTY_EPH)) != 0U) {
  2376. opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n");
  2377. return OPJ_FALSE;
  2378. }
  2379. opj_read_bytes(p_header_data, &l_tmp, 1); /* SGcod (A) */
  2380. ++p_header_data;
  2381. l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
  2382. /* Make sure progression order is valid */
  2383. if (l_tcp->prg > OPJ_CPRL) {
  2384. opj_event_msg(p_manager, EVT_ERROR,
  2385. "Unknown progression order in COD marker\n");
  2386. l_tcp->prg = OPJ_PROG_UNKNOWN;
  2387. }
  2388. opj_read_bytes(p_header_data, &l_tcp->numlayers, 2); /* SGcod (B) */
  2389. p_header_data += 2;
  2390. if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) {
  2391. opj_event_msg(p_manager, EVT_ERROR,
  2392. "Invalid number of layers in COD marker : %d not in range [1-65535]\n",
  2393. l_tcp->numlayers);
  2394. return OPJ_FALSE;
  2395. }
  2396. /* If user didn't set a number layer to decode take the max specify in the codestream. */
  2397. if (l_cp->m_specific_param.m_dec.m_layer) {
  2398. l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
  2399. } else {
  2400. l_tcp->num_layers_to_decode = l_tcp->numlayers;
  2401. }
  2402. opj_read_bytes(p_header_data, &l_tcp->mct, 1); /* SGcod (C) */
  2403. ++p_header_data;
  2404. if (l_tcp->mct > 1) {
  2405. opj_event_msg(p_manager, EVT_ERROR,
  2406. "Invalid multiple component transformation\n");
  2407. return OPJ_FALSE;
  2408. }
  2409. p_header_size -= 5;
  2410. for (i = 0; i < l_image->numcomps; ++i) {
  2411. l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
  2412. }
  2413. if (! opj_j2k_read_SPCod_SPCoc(p_j2k, 0, p_header_data, &p_header_size,
  2414. p_manager)) {
  2415. opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
  2416. return OPJ_FALSE;
  2417. }
  2418. if (p_header_size != 0) {
  2419. opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
  2420. return OPJ_FALSE;
  2421. }
  2422. /* Apply the coding style to other components of the current tile or the m_default_tcp*/
  2423. opj_j2k_copy_tile_component_parameters(p_j2k);
  2424. /* Index */
  2425. #ifdef WIP_REMOVE_MSD
  2426. if (p_j2k->cstr_info) {
  2427. /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
  2428. p_j2k->cstr_info->prog = l_tcp->prg;
  2429. p_j2k->cstr_info->numlayers = l_tcp->numlayers;
  2430. p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(
  2431. l_image->numcomps * sizeof(OPJ_UINT32));
  2432. if (!p_j2k->cstr_info->numdecompos) {
  2433. return OPJ_FALSE;
  2434. }
  2435. for (i = 0; i < l_image->numcomps; ++i) {
  2436. p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
  2437. }
  2438. }
  2439. #endif
  2440. return OPJ_TRUE;
  2441. }
  2442. static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k,
  2443. OPJ_UINT32 p_comp_no,
  2444. opj_stream_private_t *p_stream,
  2445. opj_event_mgr_t * p_manager)
  2446. {
  2447. OPJ_UINT32 l_coc_size, l_remaining_size;
  2448. OPJ_UINT32 l_comp_room;
  2449. /* preconditions */
  2450. assert(p_j2k != 00);
  2451. assert(p_manager != 00);
  2452. assert(p_stream != 00);
  2453. l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
  2454. l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
  2455. p_j2k->m_current_tile_number, p_comp_no);
  2456. if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2457. OPJ_BYTE *new_header_tile_data;
  2458. /*p_j2k->m_specific_param.m_encoder.m_header_tile_data
  2459. = (OPJ_BYTE*)opj_realloc(
  2460. p_j2k->m_specific_param.m_encoder.m_header_tile_data,
  2461. l_coc_size);*/
  2462. new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2463. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
  2464. if (! new_header_tile_data) {
  2465. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2466. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2467. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2468. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
  2469. return OPJ_FALSE;
  2470. }
  2471. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2472. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
  2473. }
  2474. opj_j2k_write_coc_in_memory(p_j2k, p_comp_no,
  2475. p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size,
  2476. p_manager);
  2477. if (opj_stream_write_data(p_stream,
  2478. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size,
  2479. p_manager) != l_coc_size) {
  2480. return OPJ_FALSE;
  2481. }
  2482. return OPJ_TRUE;
  2483. }
  2484. static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k,
  2485. OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
  2486. {
  2487. opj_cp_t *l_cp = NULL;
  2488. opj_tcp_t *l_tcp = NULL;
  2489. /* preconditions */
  2490. assert(p_j2k != 00);
  2491. l_cp = &(p_j2k->m_cp);
  2492. l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
  2493. if (l_tcp->tccps[p_first_comp_no].csty != l_tcp->tccps[p_second_comp_no].csty) {
  2494. return OPJ_FALSE;
  2495. }
  2496. return opj_j2k_compare_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number,
  2497. p_first_comp_no, p_second_comp_no);
  2498. }
  2499. static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
  2500. OPJ_UINT32 p_comp_no,
  2501. OPJ_BYTE * p_data,
  2502. OPJ_UINT32 * p_data_written,
  2503. opj_event_mgr_t * p_manager
  2504. )
  2505. {
  2506. opj_cp_t *l_cp = 00;
  2507. opj_tcp_t *l_tcp = 00;
  2508. OPJ_UINT32 l_coc_size, l_remaining_size;
  2509. OPJ_BYTE * l_current_data = 00;
  2510. opj_image_t *l_image = 00;
  2511. OPJ_UINT32 l_comp_room;
  2512. /* preconditions */
  2513. assert(p_j2k != 00);
  2514. assert(p_manager != 00);
  2515. l_cp = &(p_j2k->m_cp);
  2516. l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
  2517. l_image = p_j2k->m_private_image;
  2518. l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
  2519. l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
  2520. p_j2k->m_current_tile_number, p_comp_no);
  2521. l_remaining_size = l_coc_size;
  2522. l_current_data = p_data;
  2523. opj_write_bytes(l_current_data, J2K_MS_COC,
  2524. 2); /* COC */
  2525. l_current_data += 2;
  2526. opj_write_bytes(l_current_data, l_coc_size - 2,
  2527. 2); /* L_COC */
  2528. l_current_data += 2;
  2529. opj_write_bytes(l_current_data, p_comp_no, l_comp_room); /* Ccoc */
  2530. l_current_data += l_comp_room;
  2531. opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty,
  2532. 1); /* Scoc */
  2533. ++l_current_data;
  2534. l_remaining_size -= (5 + l_comp_room);
  2535. opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0,
  2536. l_current_data, &l_remaining_size, p_manager);
  2537. * p_data_written = l_coc_size;
  2538. }
  2539. static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
  2540. {
  2541. OPJ_UINT32 i, j;
  2542. OPJ_UINT32 l_nb_comp;
  2543. OPJ_UINT32 l_nb_tiles;
  2544. OPJ_UINT32 l_max = 0;
  2545. /* preconditions */
  2546. l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
  2547. l_nb_comp = p_j2k->m_private_image->numcomps;
  2548. for (i = 0; i < l_nb_tiles; ++i) {
  2549. for (j = 0; j < l_nb_comp; ++j) {
  2550. l_max = opj_uint_max(l_max, opj_j2k_get_SPCod_SPCoc_size(p_j2k, i, j));
  2551. }
  2552. }
  2553. return 6 + l_max;
  2554. }
  2555. /**
  2556. * Reads a COC marker (Coding Style Component)
  2557. * @param p_header_data the data contained in the COC box.
  2558. * @param p_j2k the jpeg2000 codec.
  2559. * @param p_header_size the size of the data contained in the COC marker.
  2560. * @param p_manager the user event manager.
  2561. */
  2562. static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k,
  2563. OPJ_BYTE * p_header_data,
  2564. OPJ_UINT32 p_header_size,
  2565. opj_event_mgr_t * p_manager
  2566. )
  2567. {
  2568. opj_cp_t *l_cp = NULL;
  2569. opj_tcp_t *l_tcp = NULL;
  2570. opj_image_t *l_image = NULL;
  2571. OPJ_UINT32 l_comp_room;
  2572. OPJ_UINT32 l_comp_no;
  2573. /* preconditions */
  2574. assert(p_header_data != 00);
  2575. assert(p_j2k != 00);
  2576. assert(p_manager != 00);
  2577. l_cp = &(p_j2k->m_cp);
  2578. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
  2579. ?
  2580. &l_cp->tcps[p_j2k->m_current_tile_number] :
  2581. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  2582. l_image = p_j2k->m_private_image;
  2583. l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
  2584. /* make sure room is sufficient*/
  2585. if (p_header_size < l_comp_room + 1) {
  2586. opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
  2587. return OPJ_FALSE;
  2588. }
  2589. p_header_size -= l_comp_room + 1;
  2590. opj_read_bytes(p_header_data, &l_comp_no,
  2591. l_comp_room); /* Ccoc */
  2592. p_header_data += l_comp_room;
  2593. if (l_comp_no >= l_image->numcomps) {
  2594. opj_event_msg(p_manager, EVT_ERROR,
  2595. "Error reading COC marker (bad number of components)\n");
  2596. return OPJ_FALSE;
  2597. }
  2598. opj_read_bytes(p_header_data, &l_tcp->tccps[l_comp_no].csty,
  2599. 1); /* Scoc */
  2600. ++p_header_data ;
  2601. if (! opj_j2k_read_SPCod_SPCoc(p_j2k, l_comp_no, p_header_data, &p_header_size,
  2602. p_manager)) {
  2603. opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
  2604. return OPJ_FALSE;
  2605. }
  2606. if (p_header_size != 0) {
  2607. opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
  2608. return OPJ_FALSE;
  2609. }
  2610. return OPJ_TRUE;
  2611. }
  2612. static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k,
  2613. opj_stream_private_t *p_stream,
  2614. opj_event_mgr_t * p_manager
  2615. )
  2616. {
  2617. OPJ_UINT32 l_qcd_size, l_remaining_size;
  2618. OPJ_BYTE * l_current_data = 00;
  2619. /* preconditions */
  2620. assert(p_j2k != 00);
  2621. assert(p_manager != 00);
  2622. assert(p_stream != 00);
  2623. l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
  2624. 0);
  2625. l_remaining_size = l_qcd_size;
  2626. if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2627. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2628. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
  2629. if (! new_header_tile_data) {
  2630. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2631. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2632. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2633. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
  2634. return OPJ_FALSE;
  2635. }
  2636. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2637. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
  2638. }
  2639. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  2640. opj_write_bytes(l_current_data, J2K_MS_QCD, 2); /* QCD */
  2641. l_current_data += 2;
  2642. opj_write_bytes(l_current_data, l_qcd_size - 2, 2); /* L_QCD */
  2643. l_current_data += 2;
  2644. l_remaining_size -= 4;
  2645. if (! opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, 0,
  2646. l_current_data, &l_remaining_size, p_manager)) {
  2647. opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
  2648. return OPJ_FALSE;
  2649. }
  2650. if (l_remaining_size != 0) {
  2651. opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
  2652. return OPJ_FALSE;
  2653. }
  2654. if (opj_stream_write_data(p_stream,
  2655. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size,
  2656. p_manager) != l_qcd_size) {
  2657. return OPJ_FALSE;
  2658. }
  2659. return OPJ_TRUE;
  2660. }
  2661. /**
  2662. * Reads a QCD marker (Quantization defaults)
  2663. * @param p_header_data the data contained in the QCD box.
  2664. * @param p_j2k the jpeg2000 codec.
  2665. * @param p_header_size the size of the data contained in the QCD marker.
  2666. * @param p_manager the user event manager.
  2667. */
  2668. static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k,
  2669. OPJ_BYTE * p_header_data,
  2670. OPJ_UINT32 p_header_size,
  2671. opj_event_mgr_t * p_manager
  2672. )
  2673. {
  2674. /* preconditions */
  2675. assert(p_header_data != 00);
  2676. assert(p_j2k != 00);
  2677. assert(p_manager != 00);
  2678. if (! opj_j2k_read_SQcd_SQcc(p_j2k, 0, p_header_data, &p_header_size,
  2679. p_manager)) {
  2680. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
  2681. return OPJ_FALSE;
  2682. }
  2683. if (p_header_size != 0) {
  2684. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
  2685. return OPJ_FALSE;
  2686. }
  2687. /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
  2688. opj_j2k_copy_tile_quantization_parameters(p_j2k);
  2689. return OPJ_TRUE;
  2690. }
  2691. static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k,
  2692. OPJ_UINT32 p_comp_no,
  2693. opj_stream_private_t *p_stream,
  2694. opj_event_mgr_t * p_manager
  2695. )
  2696. {
  2697. OPJ_UINT32 l_qcc_size, l_remaining_size;
  2698. /* preconditions */
  2699. assert(p_j2k != 00);
  2700. assert(p_manager != 00);
  2701. assert(p_stream != 00);
  2702. l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
  2703. p_comp_no);
  2704. l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0 : 1;
  2705. l_remaining_size = l_qcc_size;
  2706. if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2707. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2708. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
  2709. if (! new_header_tile_data) {
  2710. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2711. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2712. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2713. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
  2714. return OPJ_FALSE;
  2715. }
  2716. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2717. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
  2718. }
  2719. opj_j2k_write_qcc_in_memory(p_j2k, p_comp_no,
  2720. p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size,
  2721. p_manager);
  2722. if (opj_stream_write_data(p_stream,
  2723. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size,
  2724. p_manager) != l_qcc_size) {
  2725. return OPJ_FALSE;
  2726. }
  2727. return OPJ_TRUE;
  2728. }
  2729. static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k,
  2730. OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
  2731. {
  2732. return opj_j2k_compare_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number,
  2733. p_first_comp_no, p_second_comp_no);
  2734. }
  2735. static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
  2736. OPJ_UINT32 p_comp_no,
  2737. OPJ_BYTE * p_data,
  2738. OPJ_UINT32 * p_data_written,
  2739. opj_event_mgr_t * p_manager
  2740. )
  2741. {
  2742. OPJ_UINT32 l_qcc_size, l_remaining_size;
  2743. OPJ_BYTE * l_current_data = 00;
  2744. /* preconditions */
  2745. assert(p_j2k != 00);
  2746. assert(p_manager != 00);
  2747. l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
  2748. p_comp_no);
  2749. l_remaining_size = l_qcc_size;
  2750. l_current_data = p_data;
  2751. opj_write_bytes(l_current_data, J2K_MS_QCC, 2); /* QCC */
  2752. l_current_data += 2;
  2753. if (p_j2k->m_private_image->numcomps <= 256) {
  2754. --l_qcc_size;
  2755. opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */
  2756. l_current_data += 2;
  2757. opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
  2758. ++l_current_data;
  2759. /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
  2760. l_remaining_size -= 6;
  2761. } else {
  2762. opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */
  2763. l_current_data += 2;
  2764. opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
  2765. l_current_data += 2;
  2766. l_remaining_size -= 6;
  2767. }
  2768. opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, p_comp_no,
  2769. l_current_data, &l_remaining_size, p_manager);
  2770. *p_data_written = l_qcc_size;
  2771. }
  2772. static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k)
  2773. {
  2774. return opj_j2k_get_max_coc_size(p_j2k);
  2775. }
  2776. /**
  2777. * Reads a QCC marker (Quantization component)
  2778. * @param p_header_data the data contained in the QCC box.
  2779. * @param p_j2k the jpeg2000 codec.
  2780. * @param p_header_size the size of the data contained in the QCC marker.
  2781. * @param p_manager the user event manager.
  2782. */
  2783. static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k,
  2784. OPJ_BYTE * p_header_data,
  2785. OPJ_UINT32 p_header_size,
  2786. opj_event_mgr_t * p_manager
  2787. )
  2788. {
  2789. OPJ_UINT32 l_num_comp, l_comp_no;
  2790. /* preconditions */
  2791. assert(p_header_data != 00);
  2792. assert(p_j2k != 00);
  2793. assert(p_manager != 00);
  2794. l_num_comp = p_j2k->m_private_image->numcomps;
  2795. if (l_num_comp <= 256) {
  2796. if (p_header_size < 1) {
  2797. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
  2798. return OPJ_FALSE;
  2799. }
  2800. opj_read_bytes(p_header_data, &l_comp_no, 1);
  2801. ++p_header_data;
  2802. --p_header_size;
  2803. } else {
  2804. if (p_header_size < 2) {
  2805. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
  2806. return OPJ_FALSE;
  2807. }
  2808. opj_read_bytes(p_header_data, &l_comp_no, 2);
  2809. p_header_data += 2;
  2810. p_header_size -= 2;
  2811. }
  2812. #ifdef USE_JPWL
  2813. if (p_j2k->m_cp.correct) {
  2814. static OPJ_UINT32 backup_compno = 0;
  2815. /* compno is negative or larger than the number of components!!! */
  2816. if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
  2817. opj_event_msg(p_manager, EVT_ERROR,
  2818. "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
  2819. l_comp_no, l_num_comp);
  2820. if (!JPWL_ASSUME) {
  2821. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  2822. return OPJ_FALSE;
  2823. }
  2824. /* we try to correct */
  2825. l_comp_no = backup_compno % l_num_comp;
  2826. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
  2827. "- setting component number to %d\n",
  2828. l_comp_no);
  2829. }
  2830. /* keep your private count of tiles */
  2831. backup_compno++;
  2832. };
  2833. #endif /* USE_JPWL */
  2834. if (l_comp_no >= p_j2k->m_private_image->numcomps) {
  2835. opj_event_msg(p_manager, EVT_ERROR,
  2836. "Invalid component number: %d, regarding the number of components %d\n",
  2837. l_comp_no, p_j2k->m_private_image->numcomps);
  2838. return OPJ_FALSE;
  2839. }
  2840. if (! opj_j2k_read_SQcd_SQcc(p_j2k, l_comp_no, p_header_data, &p_header_size,
  2841. p_manager)) {
  2842. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
  2843. return OPJ_FALSE;
  2844. }
  2845. if (p_header_size != 0) {
  2846. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
  2847. return OPJ_FALSE;
  2848. }
  2849. return OPJ_TRUE;
  2850. }
  2851. static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k,
  2852. opj_stream_private_t *p_stream,
  2853. opj_event_mgr_t * p_manager
  2854. )
  2855. {
  2856. OPJ_UINT32 l_nb_comp;
  2857. OPJ_UINT32 l_nb_poc;
  2858. OPJ_UINT32 l_poc_size;
  2859. OPJ_UINT32 l_written_size = 0;
  2860. opj_tcp_t *l_tcp = 00;
  2861. OPJ_UINT32 l_poc_room;
  2862. /* preconditions */
  2863. assert(p_j2k != 00);
  2864. assert(p_manager != 00);
  2865. assert(p_stream != 00);
  2866. l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
  2867. l_nb_comp = p_j2k->m_private_image->numcomps;
  2868. l_nb_poc = 1 + l_tcp->numpocs;
  2869. if (l_nb_comp <= 256) {
  2870. l_poc_room = 1;
  2871. } else {
  2872. l_poc_room = 2;
  2873. }
  2874. l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
  2875. if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2876. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2877. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
  2878. if (! new_header_tile_data) {
  2879. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2880. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2881. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2882. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
  2883. return OPJ_FALSE;
  2884. }
  2885. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2886. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
  2887. }
  2888. opj_j2k_write_poc_in_memory(p_j2k,
  2889. p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_written_size,
  2890. p_manager);
  2891. if (opj_stream_write_data(p_stream,
  2892. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size,
  2893. p_manager) != l_poc_size) {
  2894. return OPJ_FALSE;
  2895. }
  2896. return OPJ_TRUE;
  2897. }
  2898. static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
  2899. OPJ_BYTE * p_data,
  2900. OPJ_UINT32 * p_data_written,
  2901. opj_event_mgr_t * p_manager
  2902. )
  2903. {
  2904. OPJ_UINT32 i;
  2905. OPJ_BYTE * l_current_data = 00;
  2906. OPJ_UINT32 l_nb_comp;
  2907. OPJ_UINT32 l_nb_poc;
  2908. OPJ_UINT32 l_poc_size;
  2909. opj_image_t *l_image = 00;
  2910. opj_tcp_t *l_tcp = 00;
  2911. opj_tccp_t *l_tccp = 00;
  2912. opj_poc_t *l_current_poc = 00;
  2913. OPJ_UINT32 l_poc_room;
  2914. /* preconditions */
  2915. assert(p_j2k != 00);
  2916. assert(p_manager != 00);
  2917. OPJ_UNUSED(p_manager);
  2918. l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
  2919. l_tccp = &l_tcp->tccps[0];
  2920. l_image = p_j2k->m_private_image;
  2921. l_nb_comp = l_image->numcomps;
  2922. l_nb_poc = 1 + l_tcp->numpocs;
  2923. if (l_nb_comp <= 256) {
  2924. l_poc_room = 1;
  2925. } else {
  2926. l_poc_room = 2;
  2927. }
  2928. l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
  2929. l_current_data = p_data;
  2930. opj_write_bytes(l_current_data, J2K_MS_POC,
  2931. 2); /* POC */
  2932. l_current_data += 2;
  2933. opj_write_bytes(l_current_data, l_poc_size - 2,
  2934. 2); /* Lpoc */
  2935. l_current_data += 2;
  2936. l_current_poc = l_tcp->pocs;
  2937. for (i = 0; i < l_nb_poc; ++i) {
  2938. opj_write_bytes(l_current_data, l_current_poc->resno0,
  2939. 1); /* RSpoc_i */
  2940. ++l_current_data;
  2941. opj_write_bytes(l_current_data, l_current_poc->compno0,
  2942. l_poc_room); /* CSpoc_i */
  2943. l_current_data += l_poc_room;
  2944. opj_write_bytes(l_current_data, l_current_poc->layno1,
  2945. 2); /* LYEpoc_i */
  2946. l_current_data += 2;
  2947. opj_write_bytes(l_current_data, l_current_poc->resno1,
  2948. 1); /* REpoc_i */
  2949. ++l_current_data;
  2950. opj_write_bytes(l_current_data, l_current_poc->compno1,
  2951. l_poc_room); /* CEpoc_i */
  2952. l_current_data += l_poc_room;
  2953. opj_write_bytes(l_current_data, (OPJ_UINT32)l_current_poc->prg,
  2954. 1); /* Ppoc_i */
  2955. ++l_current_data;
  2956. /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
  2957. l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
  2958. l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
  2959. l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
  2960. l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
  2961. l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
  2962. l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
  2963. ++l_current_poc;
  2964. }
  2965. *p_data_written = l_poc_size;
  2966. }
  2967. static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)
  2968. {
  2969. opj_tcp_t * l_tcp = 00;
  2970. OPJ_UINT32 l_nb_tiles = 0;
  2971. OPJ_UINT32 l_max_poc = 0;
  2972. OPJ_UINT32 i;
  2973. l_tcp = p_j2k->m_cp.tcps;
  2974. l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  2975. for (i = 0; i < l_nb_tiles; ++i) {
  2976. l_max_poc = opj_uint_max(l_max_poc, l_tcp->numpocs);
  2977. ++l_tcp;
  2978. }
  2979. ++l_max_poc;
  2980. return 4 + 9 * l_max_poc;
  2981. }
  2982. static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k)
  2983. {
  2984. OPJ_UINT32 i;
  2985. OPJ_UINT32 l_nb_tiles;
  2986. OPJ_UINT32 l_max = 0;
  2987. opj_tcp_t * l_tcp = 00;
  2988. l_tcp = p_j2k->m_cp.tcps;
  2989. l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
  2990. for (i = 0; i < l_nb_tiles; ++i) {
  2991. l_max = opj_uint_max(l_max, l_tcp->m_nb_tile_parts);
  2992. ++l_tcp;
  2993. }
  2994. return 12 * l_max;
  2995. }
  2996. static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
  2997. {
  2998. OPJ_UINT32 l_nb_bytes = 0;
  2999. OPJ_UINT32 l_nb_comps;
  3000. OPJ_UINT32 l_coc_bytes, l_qcc_bytes;
  3001. l_nb_comps = p_j2k->m_private_image->numcomps - 1;
  3002. l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
  3003. if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) {
  3004. l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
  3005. l_nb_bytes += l_nb_comps * l_coc_bytes;
  3006. l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
  3007. l_nb_bytes += l_nb_comps * l_qcc_bytes;
  3008. }
  3009. l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
  3010. if (p_j2k->m_specific_param.m_encoder.m_PLT) {
  3011. /* Reserve space for PLT markers */
  3012. OPJ_UINT32 i;
  3013. const opj_cp_t * l_cp = &(p_j2k->m_cp);
  3014. OPJ_UINT32 l_max_packet_count = 0;
  3015. for (i = 0; i < l_cp->th * l_cp->tw; ++i) {
  3016. l_max_packet_count = opj_uint_max(l_max_packet_count,
  3017. opj_get_encoding_packet_count(p_j2k->m_private_image, l_cp, i));
  3018. }
  3019. /* Minimum 6 bytes per PLT marker, and at a minimum (taking a pessimistic */
  3020. /* estimate of 4 bytes for a packet size), one can write */
  3021. /* (65536-6) / 4 = 16382 paquet sizes per PLT marker */
  3022. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT =
  3023. 6 * opj_uint_ceildiv(l_max_packet_count, 16382);
  3024. /* Maximum 5 bytes per packet to encode a full UINT32 */
  3025. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT +=
  3026. l_nb_bytes += 5 * l_max_packet_count;
  3027. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += 1;
  3028. l_nb_bytes += p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT;
  3029. }
  3030. /*** DEVELOPER CORNER, Add room for your headers ***/
  3031. return l_nb_bytes;
  3032. }
  3033. /**
  3034. * Reads a POC marker (Progression Order Change)
  3035. *
  3036. * @param p_header_data the data contained in the POC box.
  3037. * @param p_j2k the jpeg2000 codec.
  3038. * @param p_header_size the size of the data contained in the POC marker.
  3039. * @param p_manager the user event manager.
  3040. */
  3041. static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k,
  3042. OPJ_BYTE * p_header_data,
  3043. OPJ_UINT32 p_header_size,
  3044. opj_event_mgr_t * p_manager
  3045. )
  3046. {
  3047. OPJ_UINT32 i, l_nb_comp, l_tmp;
  3048. opj_image_t * l_image = 00;
  3049. OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
  3050. OPJ_UINT32 l_chunk_size, l_comp_room;
  3051. opj_cp_t *l_cp = 00;
  3052. opj_tcp_t *l_tcp = 00;
  3053. opj_poc_t *l_current_poc = 00;
  3054. /* preconditions */
  3055. assert(p_header_data != 00);
  3056. assert(p_j2k != 00);
  3057. assert(p_manager != 00);
  3058. l_image = p_j2k->m_private_image;
  3059. l_nb_comp = l_image->numcomps;
  3060. if (l_nb_comp <= 256) {
  3061. l_comp_room = 1;
  3062. } else {
  3063. l_comp_room = 2;
  3064. }
  3065. l_chunk_size = 5 + 2 * l_comp_room;
  3066. l_current_poc_nb = p_header_size / l_chunk_size;
  3067. l_current_poc_remaining = p_header_size % l_chunk_size;
  3068. if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
  3069. opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");
  3070. return OPJ_FALSE;
  3071. }
  3072. l_cp = &(p_j2k->m_cp);
  3073. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
  3074. &l_cp->tcps[p_j2k->m_current_tile_number] :
  3075. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  3076. l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
  3077. l_current_poc_nb += l_old_poc_nb;
  3078. if (l_current_poc_nb >= J2K_MAX_POCS) {
  3079. opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
  3080. return OPJ_FALSE;
  3081. }
  3082. /* now poc is in use.*/
  3083. l_tcp->POC = 1;
  3084. l_current_poc = &l_tcp->pocs[l_old_poc_nb];
  3085. for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
  3086. opj_read_bytes(p_header_data, &(l_current_poc->resno0),
  3087. 1); /* RSpoc_i */
  3088. ++p_header_data;
  3089. opj_read_bytes(p_header_data, &(l_current_poc->compno0),
  3090. l_comp_room); /* CSpoc_i */
  3091. p_header_data += l_comp_room;
  3092. opj_read_bytes(p_header_data, &(l_current_poc->layno1),
  3093. 2); /* LYEpoc_i */
  3094. /* make sure layer end is in acceptable bounds */
  3095. l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
  3096. p_header_data += 2;
  3097. opj_read_bytes(p_header_data, &(l_current_poc->resno1),
  3098. 1); /* REpoc_i */
  3099. ++p_header_data;
  3100. opj_read_bytes(p_header_data, &(l_current_poc->compno1),
  3101. l_comp_room); /* CEpoc_i */
  3102. p_header_data += l_comp_room;
  3103. opj_read_bytes(p_header_data, &l_tmp,
  3104. 1); /* Ppoc_i */
  3105. ++p_header_data;
  3106. l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
  3107. /* make sure comp is in acceptable bounds */
  3108. l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);
  3109. ++l_current_poc;
  3110. }
  3111. l_tcp->numpocs = l_current_poc_nb - 1;
  3112. return OPJ_TRUE;
  3113. }
  3114. /**
  3115. * Reads a CRG marker (Component registration)
  3116. *
  3117. * @param p_header_data the data contained in the TLM box.
  3118. * @param p_j2k the jpeg2000 codec.
  3119. * @param p_header_size the size of the data contained in the TLM marker.
  3120. * @param p_manager the user event manager.
  3121. */
  3122. static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k,
  3123. OPJ_BYTE * p_header_data,
  3124. OPJ_UINT32 p_header_size,
  3125. opj_event_mgr_t * p_manager
  3126. )
  3127. {
  3128. OPJ_UINT32 l_nb_comp;
  3129. /* preconditions */
  3130. assert(p_header_data != 00);
  3131. assert(p_j2k != 00);
  3132. assert(p_manager != 00);
  3133. OPJ_UNUSED(p_header_data);
  3134. l_nb_comp = p_j2k->m_private_image->numcomps;
  3135. if (p_header_size != l_nb_comp * 4) {
  3136. opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");
  3137. return OPJ_FALSE;
  3138. }
  3139. /* Do not care of this at the moment since only local variables are set here */
  3140. /*
  3141. for
  3142. (i = 0; i < l_nb_comp; ++i)
  3143. {
  3144. opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
  3145. p_header_data+=2;
  3146. opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
  3147. p_header_data+=2;
  3148. }
  3149. */
  3150. return OPJ_TRUE;
  3151. }
  3152. /**
  3153. * Reads a TLM marker (Tile Length Marker)
  3154. *
  3155. * @param p_header_data the data contained in the TLM box.
  3156. * @param p_j2k the jpeg2000 codec.
  3157. * @param p_header_size the size of the data contained in the TLM marker.
  3158. * @param p_manager the user event manager.
  3159. */
  3160. static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k,
  3161. OPJ_BYTE * p_header_data,
  3162. OPJ_UINT32 p_header_size,
  3163. opj_event_mgr_t * p_manager
  3164. )
  3165. {
  3166. OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient,
  3167. l_Ptlm_size;
  3168. /* preconditions */
  3169. assert(p_header_data != 00);
  3170. assert(p_j2k != 00);
  3171. assert(p_manager != 00);
  3172. OPJ_UNUSED(p_j2k);
  3173. if (p_header_size < 2) {
  3174. opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
  3175. return OPJ_FALSE;
  3176. }
  3177. p_header_size -= 2;
  3178. opj_read_bytes(p_header_data, &l_Ztlm,
  3179. 1); /* Ztlm */
  3180. ++p_header_data;
  3181. opj_read_bytes(p_header_data, &l_Stlm,
  3182. 1); /* Stlm */
  3183. ++p_header_data;
  3184. l_ST = ((l_Stlm >> 4) & 0x3);
  3185. l_SP = (l_Stlm >> 6) & 0x1;
  3186. l_Ptlm_size = (l_SP + 1) * 2;
  3187. l_quotient = l_Ptlm_size + l_ST;
  3188. l_tot_num_tp_remaining = p_header_size % l_quotient;
  3189. if (l_tot_num_tp_remaining != 0) {
  3190. opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
  3191. return OPJ_FALSE;
  3192. }
  3193. /* FIXME Do not care of this at the moment since only local variables are set here */
  3194. /*
  3195. for
  3196. (i = 0; i < l_tot_num_tp; ++i)
  3197. {
  3198. opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
  3199. p_header_data += l_ST;
  3200. opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
  3201. p_header_data += l_Ptlm_size;
  3202. }*/
  3203. return OPJ_TRUE;
  3204. }
  3205. /**
  3206. * Reads a PLM marker (Packet length, main header marker)
  3207. *
  3208. * @param p_header_data the data contained in the TLM box.
  3209. * @param p_j2k the jpeg2000 codec.
  3210. * @param p_header_size the size of the data contained in the TLM marker.
  3211. * @param p_manager the user event manager.
  3212. */
  3213. static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k,
  3214. OPJ_BYTE * p_header_data,
  3215. OPJ_UINT32 p_header_size,
  3216. opj_event_mgr_t * p_manager
  3217. )
  3218. {
  3219. /* preconditions */
  3220. assert(p_header_data != 00);
  3221. assert(p_j2k != 00);
  3222. assert(p_manager != 00);
  3223. OPJ_UNUSED(p_j2k);
  3224. OPJ_UNUSED(p_header_data);
  3225. if (p_header_size < 1) {
  3226. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
  3227. return OPJ_FALSE;
  3228. }
  3229. /* Do not care of this at the moment since only local variables are set here */
  3230. /*
  3231. opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
  3232. ++p_header_data;
  3233. --p_header_size;
  3234. while
  3235. (p_header_size > 0)
  3236. {
  3237. opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
  3238. ++p_header_data;
  3239. p_header_size -= (1+l_Nplm);
  3240. if
  3241. (p_header_size < 0)
  3242. {
  3243. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
  3244. return false;
  3245. }
  3246. for
  3247. (i = 0; i < l_Nplm; ++i)
  3248. {
  3249. opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
  3250. ++p_header_data;
  3251. // take only the last seven bytes
  3252. l_packet_len |= (l_tmp & 0x7f);
  3253. if
  3254. (l_tmp & 0x80)
  3255. {
  3256. l_packet_len <<= 7;
  3257. }
  3258. else
  3259. {
  3260. // store packet length and proceed to next packet
  3261. l_packet_len = 0;
  3262. }
  3263. }
  3264. if
  3265. (l_packet_len != 0)
  3266. {
  3267. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
  3268. return false;
  3269. }
  3270. }
  3271. */
  3272. return OPJ_TRUE;
  3273. }
  3274. /**
  3275. * Reads a PLT marker (Packet length, tile-part header)
  3276. *
  3277. * @param p_header_data the data contained in the PLT box.
  3278. * @param p_j2k the jpeg2000 codec.
  3279. * @param p_header_size the size of the data contained in the PLT marker.
  3280. * @param p_manager the user event manager.
  3281. */
  3282. static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k,
  3283. OPJ_BYTE * p_header_data,
  3284. OPJ_UINT32 p_header_size,
  3285. opj_event_mgr_t * p_manager
  3286. )
  3287. {
  3288. OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
  3289. /* preconditions */
  3290. assert(p_header_data != 00);
  3291. assert(p_j2k != 00);
  3292. assert(p_manager != 00);
  3293. OPJ_UNUSED(p_j2k);
  3294. if (p_header_size < 1) {
  3295. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
  3296. return OPJ_FALSE;
  3297. }
  3298. opj_read_bytes(p_header_data, &l_Zplt, 1); /* Zplt */
  3299. ++p_header_data;
  3300. --p_header_size;
  3301. for (i = 0; i < p_header_size; ++i) {
  3302. opj_read_bytes(p_header_data, &l_tmp, 1); /* Iplt_ij */
  3303. ++p_header_data;
  3304. /* take only the last seven bytes */
  3305. l_packet_len |= (l_tmp & 0x7f);
  3306. if (l_tmp & 0x80) {
  3307. l_packet_len <<= 7;
  3308. } else {
  3309. /* store packet length and proceed to next packet */
  3310. l_packet_len = 0;
  3311. }
  3312. }
  3313. if (l_packet_len != 0) {
  3314. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
  3315. return OPJ_FALSE;
  3316. }
  3317. return OPJ_TRUE;
  3318. }
  3319. /**
  3320. * Reads a PPM marker (Packed packet headers, main header)
  3321. *
  3322. * @param p_header_data the data contained in the POC box.
  3323. * @param p_j2k the jpeg2000 codec.
  3324. * @param p_header_size the size of the data contained in the POC marker.
  3325. * @param p_manager the user event manager.
  3326. */
  3327. static OPJ_BOOL opj_j2k_read_ppm(
  3328. opj_j2k_t *p_j2k,
  3329. OPJ_BYTE * p_header_data,
  3330. OPJ_UINT32 p_header_size,
  3331. opj_event_mgr_t * p_manager)
  3332. {
  3333. opj_cp_t *l_cp = 00;
  3334. OPJ_UINT32 l_Z_ppm;
  3335. /* preconditions */
  3336. assert(p_header_data != 00);
  3337. assert(p_j2k != 00);
  3338. assert(p_manager != 00);
  3339. /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */
  3340. if (p_header_size < 2) {
  3341. opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
  3342. return OPJ_FALSE;
  3343. }
  3344. l_cp = &(p_j2k->m_cp);
  3345. l_cp->ppm = 1;
  3346. opj_read_bytes(p_header_data, &l_Z_ppm, 1); /* Z_ppm */
  3347. ++p_header_data;
  3348. --p_header_size;
  3349. /* check allocation needed */
  3350. if (l_cp->ppm_markers == NULL) { /* first PPM marker */
  3351. OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
  3352. assert(l_cp->ppm_markers_count == 0U);
  3353. l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
  3354. if (l_cp->ppm_markers == NULL) {
  3355. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
  3356. return OPJ_FALSE;
  3357. }
  3358. l_cp->ppm_markers_count = l_newCount;
  3359. } else if (l_cp->ppm_markers_count <= l_Z_ppm) {
  3360. OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
  3361. opj_ppx *new_ppm_markers;
  3362. new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers,
  3363. l_newCount * sizeof(opj_ppx));
  3364. if (new_ppm_markers == NULL) {
  3365. /* clean up to be done on l_cp destruction */
  3366. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
  3367. return OPJ_FALSE;
  3368. }
  3369. l_cp->ppm_markers = new_ppm_markers;
  3370. memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0,
  3371. (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx));
  3372. l_cp->ppm_markers_count = l_newCount;
  3373. }
  3374. if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) {
  3375. /* clean up to be done on l_cp destruction */
  3376. opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm);
  3377. return OPJ_FALSE;
  3378. }
  3379. l_cp->ppm_markers[l_Z_ppm].m_data = (OPJ_BYTE *) opj_malloc(p_header_size);
  3380. if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) {
  3381. /* clean up to be done on l_cp destruction */
  3382. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
  3383. return OPJ_FALSE;
  3384. }
  3385. l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size;
  3386. memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size);
  3387. return OPJ_TRUE;
  3388. }
  3389. /**
  3390. * Merges all PPM markers read (Packed headers, main header)
  3391. *
  3392. * @param p_cp main coding parameters.
  3393. * @param p_manager the user event manager.
  3394. */
  3395. static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager)
  3396. {
  3397. OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining;
  3398. /* preconditions */
  3399. assert(p_cp != 00);
  3400. assert(p_manager != 00);
  3401. assert(p_cp->ppm_buffer == NULL);
  3402. if (p_cp->ppm == 0U) {
  3403. return OPJ_TRUE;
  3404. }
  3405. l_ppm_data_size = 0U;
  3406. l_N_ppm_remaining = 0U;
  3407. for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
  3408. if (p_cp->ppm_markers[i].m_data !=
  3409. NULL) { /* standard doesn't seem to require contiguous Zppm */
  3410. OPJ_UINT32 l_N_ppm;
  3411. OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
  3412. const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
  3413. if (l_N_ppm_remaining >= l_data_size) {
  3414. l_N_ppm_remaining -= l_data_size;
  3415. l_data_size = 0U;
  3416. } else {
  3417. l_data += l_N_ppm_remaining;
  3418. l_data_size -= l_N_ppm_remaining;
  3419. l_N_ppm_remaining = 0U;
  3420. }
  3421. if (l_data_size > 0U) {
  3422. do {
  3423. /* read Nppm */
  3424. if (l_data_size < 4U) {
  3425. /* clean up to be done on l_cp destruction */
  3426. opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
  3427. return OPJ_FALSE;
  3428. }
  3429. opj_read_bytes(l_data, &l_N_ppm, 4);
  3430. l_data += 4;
  3431. l_data_size -= 4;
  3432. l_ppm_data_size +=
  3433. l_N_ppm; /* can't overflow, max 256 markers of max 65536 bytes, that is when PPM markers are not corrupted which is checked elsewhere */
  3434. if (l_data_size >= l_N_ppm) {
  3435. l_data_size -= l_N_ppm;
  3436. l_data += l_N_ppm;
  3437. } else {
  3438. l_N_ppm_remaining = l_N_ppm - l_data_size;
  3439. l_data_size = 0U;
  3440. }
  3441. } while (l_data_size > 0U);
  3442. }
  3443. }
  3444. }
  3445. if (l_N_ppm_remaining != 0U) {
  3446. /* clean up to be done on l_cp destruction */
  3447. opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n");
  3448. return OPJ_FALSE;
  3449. }
  3450. p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size);
  3451. if (p_cp->ppm_buffer == 00) {
  3452. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
  3453. return OPJ_FALSE;
  3454. }
  3455. p_cp->ppm_len = l_ppm_data_size;
  3456. l_ppm_data_size = 0U;
  3457. l_N_ppm_remaining = 0U;
  3458. for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
  3459. if (p_cp->ppm_markers[i].m_data !=
  3460. NULL) { /* standard doesn't seem to require contiguous Zppm */
  3461. OPJ_UINT32 l_N_ppm;
  3462. OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
  3463. const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
  3464. if (l_N_ppm_remaining >= l_data_size) {
  3465. memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
  3466. l_ppm_data_size += l_data_size;
  3467. l_N_ppm_remaining -= l_data_size;
  3468. l_data_size = 0U;
  3469. } else {
  3470. memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining);
  3471. l_ppm_data_size += l_N_ppm_remaining;
  3472. l_data += l_N_ppm_remaining;
  3473. l_data_size -= l_N_ppm_remaining;
  3474. l_N_ppm_remaining = 0U;
  3475. }
  3476. if (l_data_size > 0U) {
  3477. do {
  3478. /* read Nppm */
  3479. if (l_data_size < 4U) {
  3480. /* clean up to be done on l_cp destruction */
  3481. opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
  3482. return OPJ_FALSE;
  3483. }
  3484. opj_read_bytes(l_data, &l_N_ppm, 4);
  3485. l_data += 4;
  3486. l_data_size -= 4;
  3487. if (l_data_size >= l_N_ppm) {
  3488. memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm);
  3489. l_ppm_data_size += l_N_ppm;
  3490. l_data_size -= l_N_ppm;
  3491. l_data += l_N_ppm;
  3492. } else {
  3493. memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
  3494. l_ppm_data_size += l_data_size;
  3495. l_N_ppm_remaining = l_N_ppm - l_data_size;
  3496. l_data_size = 0U;
  3497. }
  3498. } while (l_data_size > 0U);
  3499. }
  3500. opj_free(p_cp->ppm_markers[i].m_data);
  3501. p_cp->ppm_markers[i].m_data = NULL;
  3502. p_cp->ppm_markers[i].m_data_size = 0U;
  3503. }
  3504. }
  3505. p_cp->ppm_data = p_cp->ppm_buffer;
  3506. p_cp->ppm_data_size = p_cp->ppm_len;
  3507. p_cp->ppm_markers_count = 0U;
  3508. opj_free(p_cp->ppm_markers);
  3509. p_cp->ppm_markers = NULL;
  3510. return OPJ_TRUE;
  3511. }
  3512. /**
  3513. * Reads a PPT marker (Packed packet headers, tile-part header)
  3514. *
  3515. * @param p_header_data the data contained in the PPT box.
  3516. * @param p_j2k the jpeg2000 codec.
  3517. * @param p_header_size the size of the data contained in the PPT marker.
  3518. * @param p_manager the user event manager.
  3519. */
  3520. static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k,
  3521. OPJ_BYTE * p_header_data,
  3522. OPJ_UINT32 p_header_size,
  3523. opj_event_mgr_t * p_manager
  3524. )
  3525. {
  3526. opj_cp_t *l_cp = 00;
  3527. opj_tcp_t *l_tcp = 00;
  3528. OPJ_UINT32 l_Z_ppt;
  3529. /* preconditions */
  3530. assert(p_header_data != 00);
  3531. assert(p_j2k != 00);
  3532. assert(p_manager != 00);
  3533. /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */
  3534. if (p_header_size < 2) {
  3535. opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n");
  3536. return OPJ_FALSE;
  3537. }
  3538. l_cp = &(p_j2k->m_cp);
  3539. if (l_cp->ppm) {
  3540. opj_event_msg(p_manager, EVT_ERROR,
  3541. "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
  3542. return OPJ_FALSE;
  3543. }
  3544. l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
  3545. l_tcp->ppt = 1;
  3546. opj_read_bytes(p_header_data, &l_Z_ppt, 1); /* Z_ppt */
  3547. ++p_header_data;
  3548. --p_header_size;
  3549. /* check allocation needed */
  3550. if (l_tcp->ppt_markers == NULL) { /* first PPT marker */
  3551. OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
  3552. assert(l_tcp->ppt_markers_count == 0U);
  3553. l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
  3554. if (l_tcp->ppt_markers == NULL) {
  3555. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
  3556. return OPJ_FALSE;
  3557. }
  3558. l_tcp->ppt_markers_count = l_newCount;
  3559. } else if (l_tcp->ppt_markers_count <= l_Z_ppt) {
  3560. OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
  3561. opj_ppx *new_ppt_markers;
  3562. new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers,
  3563. l_newCount * sizeof(opj_ppx));
  3564. if (new_ppt_markers == NULL) {
  3565. /* clean up to be done on l_tcp destruction */
  3566. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
  3567. return OPJ_FALSE;
  3568. }
  3569. l_tcp->ppt_markers = new_ppt_markers;
  3570. memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0,
  3571. (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx));
  3572. l_tcp->ppt_markers_count = l_newCount;
  3573. }
  3574. if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) {
  3575. /* clean up to be done on l_tcp destruction */
  3576. opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt);
  3577. return OPJ_FALSE;
  3578. }
  3579. l_tcp->ppt_markers[l_Z_ppt].m_data = (OPJ_BYTE *) opj_malloc(p_header_size);
  3580. if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) {
  3581. /* clean up to be done on l_tcp destruction */
  3582. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
  3583. return OPJ_FALSE;
  3584. }
  3585. l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size;
  3586. memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size);
  3587. return OPJ_TRUE;
  3588. }
  3589. /**
  3590. * Merges all PPT markers read (Packed packet headers, tile-part header)
  3591. *
  3592. * @param p_tcp the tile.
  3593. * @param p_manager the user event manager.
  3594. */
  3595. static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager)
  3596. {
  3597. OPJ_UINT32 i, l_ppt_data_size;
  3598. /* preconditions */
  3599. assert(p_tcp != 00);
  3600. assert(p_manager != 00);
  3601. if (p_tcp->ppt_buffer != NULL) {
  3602. opj_event_msg(p_manager, EVT_ERROR,
  3603. "opj_j2k_merge_ppt() has already been called\n");
  3604. return OPJ_FALSE;
  3605. }
  3606. if (p_tcp->ppt == 0U) {
  3607. return OPJ_TRUE;
  3608. }
  3609. l_ppt_data_size = 0U;
  3610. for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
  3611. l_ppt_data_size +=
  3612. p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
  3613. }
  3614. p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size);
  3615. if (p_tcp->ppt_buffer == 00) {
  3616. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
  3617. return OPJ_FALSE;
  3618. }
  3619. p_tcp->ppt_len = l_ppt_data_size;
  3620. l_ppt_data_size = 0U;
  3621. for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
  3622. if (p_tcp->ppt_markers[i].m_data !=
  3623. NULL) { /* standard doesn't seem to require contiguous Zppt */
  3624. memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data,
  3625. p_tcp->ppt_markers[i].m_data_size);
  3626. l_ppt_data_size +=
  3627. p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
  3628. opj_free(p_tcp->ppt_markers[i].m_data);
  3629. p_tcp->ppt_markers[i].m_data = NULL;
  3630. p_tcp->ppt_markers[i].m_data_size = 0U;
  3631. }
  3632. }
  3633. p_tcp->ppt_markers_count = 0U;
  3634. opj_free(p_tcp->ppt_markers);
  3635. p_tcp->ppt_markers = NULL;
  3636. p_tcp->ppt_data = p_tcp->ppt_buffer;
  3637. p_tcp->ppt_data_size = p_tcp->ppt_len;
  3638. return OPJ_TRUE;
  3639. }
  3640. static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k,
  3641. opj_stream_private_t *p_stream,
  3642. opj_event_mgr_t * p_manager
  3643. )
  3644. {
  3645. OPJ_BYTE * l_current_data = 00;
  3646. OPJ_UINT32 l_tlm_size;
  3647. OPJ_UINT32 size_per_tile_part;
  3648. /* preconditions */
  3649. assert(p_j2k != 00);
  3650. assert(p_manager != 00);
  3651. assert(p_stream != 00);
  3652. /* 10921 = (65535 - header_size) / size_per_tile_part where */
  3653. /* header_size = 4 and size_per_tile_part = 6 */
  3654. if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts > 10921) {
  3655. /* We could do more but it would require writing several TLM markers */
  3656. opj_event_msg(p_manager, EVT_ERROR,
  3657. "A maximum of 10921 tile-parts are supported currently "
  3658. "when writing TLM marker\n");
  3659. return OPJ_FALSE;
  3660. }
  3661. if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts <= 255) {
  3662. size_per_tile_part = 5;
  3663. p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_TRUE;
  3664. } else {
  3665. size_per_tile_part = 6;
  3666. p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_FALSE;
  3667. }
  3668. l_tlm_size = 2 + 4 + (size_per_tile_part *
  3669. p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
  3670. if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  3671. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  3672. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size);
  3673. if (! new_header_tile_data) {
  3674. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  3675. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  3676. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  3677. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n");
  3678. return OPJ_FALSE;
  3679. }
  3680. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  3681. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
  3682. }
  3683. memset(p_j2k->m_specific_param.m_encoder.m_header_tile_data, 0, l_tlm_size);
  3684. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  3685. /* change the way data is written to avoid seeking if possible */
  3686. /* TODO */
  3687. p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
  3688. opj_write_bytes(l_current_data, J2K_MS_TLM,
  3689. 2); /* TLM */
  3690. l_current_data += 2;
  3691. opj_write_bytes(l_current_data, l_tlm_size - 2,
  3692. 2); /* Lpoc */
  3693. l_current_data += 2;
  3694. opj_write_bytes(l_current_data, 0,
  3695. 1); /* Ztlm=0*/
  3696. ++l_current_data;
  3697. /* Stlm 0x50= ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
  3698. /* Stlm 0x60= ST=2(16bits-65535 tiles max),SP=1(Ptlm=32bits) */
  3699. opj_write_bytes(l_current_data,
  3700. size_per_tile_part == 5 ? 0x50 : 0x60,
  3701. 1);
  3702. ++l_current_data;
  3703. /* do nothing on the size_per_tile_part * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
  3704. if (opj_stream_write_data(p_stream,
  3705. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size,
  3706. p_manager) != l_tlm_size) {
  3707. return OPJ_FALSE;
  3708. }
  3709. return OPJ_TRUE;
  3710. }
  3711. static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k,
  3712. OPJ_BYTE * p_data,
  3713. OPJ_UINT32 total_data_size,
  3714. OPJ_UINT32 * p_data_written,
  3715. const opj_stream_private_t *p_stream,
  3716. opj_event_mgr_t * p_manager
  3717. )
  3718. {
  3719. /* preconditions */
  3720. assert(p_j2k != 00);
  3721. assert(p_manager != 00);
  3722. assert(p_stream != 00);
  3723. OPJ_UNUSED(p_stream);
  3724. if (total_data_size < 12) {
  3725. opj_event_msg(p_manager, EVT_ERROR,
  3726. "Not enough bytes in output buffer to write SOT marker\n");
  3727. return OPJ_FALSE;
  3728. }
  3729. opj_write_bytes(p_data, J2K_MS_SOT,
  3730. 2); /* SOT */
  3731. p_data += 2;
  3732. opj_write_bytes(p_data, 10,
  3733. 2); /* Lsot */
  3734. p_data += 2;
  3735. opj_write_bytes(p_data, p_j2k->m_current_tile_number,
  3736. 2); /* Isot */
  3737. p_data += 2;
  3738. /* Psot */
  3739. p_data += 4;
  3740. opj_write_bytes(p_data,
  3741. p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,
  3742. 1); /* TPsot */
  3743. ++p_data;
  3744. opj_write_bytes(p_data,
  3745. p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,
  3746. 1); /* TNsot */
  3747. ++p_data;
  3748. /* UniPG>> */
  3749. #ifdef USE_JPWL
  3750. /* update markers struct */
  3751. /*
  3752. OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
  3753. */
  3754. assert(0 && "TODO");
  3755. #endif /* USE_JPWL */
  3756. * p_data_written = 12;
  3757. return OPJ_TRUE;
  3758. }
  3759. static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data,
  3760. OPJ_UINT32 p_header_size,
  3761. OPJ_UINT32* p_tile_no,
  3762. OPJ_UINT32* p_tot_len,
  3763. OPJ_UINT32* p_current_part,
  3764. OPJ_UINT32* p_num_parts,
  3765. opj_event_mgr_t * p_manager)
  3766. {
  3767. /* preconditions */
  3768. assert(p_header_data != 00);
  3769. assert(p_manager != 00);
  3770. /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
  3771. if (p_header_size != 8) {
  3772. opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
  3773. return OPJ_FALSE;
  3774. }
  3775. opj_read_bytes(p_header_data, p_tile_no, 2); /* Isot */
  3776. p_header_data += 2;
  3777. opj_read_bytes(p_header_data, p_tot_len, 4); /* Psot */
  3778. p_header_data += 4;
  3779. opj_read_bytes(p_header_data, p_current_part, 1); /* TPsot */
  3780. ++p_header_data;
  3781. opj_read_bytes(p_header_data, p_num_parts, 1); /* TNsot */
  3782. ++p_header_data;
  3783. return OPJ_TRUE;
  3784. }
  3785. static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k,
  3786. OPJ_BYTE * p_header_data,
  3787. OPJ_UINT32 p_header_size,
  3788. opj_event_mgr_t * p_manager)
  3789. {
  3790. opj_cp_t *l_cp = 00;
  3791. opj_tcp_t *l_tcp = 00;
  3792. OPJ_UINT32 l_tot_len, l_num_parts = 0;
  3793. OPJ_UINT32 l_current_part;
  3794. OPJ_UINT32 l_tile_x, l_tile_y;
  3795. /* preconditions */
  3796. assert(p_j2k != 00);
  3797. assert(p_manager != 00);
  3798. if (! opj_j2k_get_sot_values(p_header_data, p_header_size,
  3799. &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts,
  3800. p_manager)) {
  3801. opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
  3802. return OPJ_FALSE;
  3803. }
  3804. #ifdef DEBUG_VERBOSE
  3805. fprintf(stderr, "SOT %d %d %d %d\n",
  3806. p_j2k->m_current_tile_number, l_tot_len, l_current_part, l_num_parts);
  3807. #endif
  3808. l_cp = &(p_j2k->m_cp);
  3809. /* testcase 2.pdf.SIGFPE.706.1112 */
  3810. if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) {
  3811. opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n",
  3812. p_j2k->m_current_tile_number);
  3813. return OPJ_FALSE;
  3814. }
  3815. l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
  3816. l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
  3817. l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
  3818. if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec < 0 ||
  3819. p_j2k->m_current_tile_number == (OPJ_UINT32)
  3820. p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec) {
  3821. /* Do only this check if we decode all tile part headers, or if */
  3822. /* we decode one precise tile. Otherwise the m_current_tile_part_number */
  3823. /* might not be valid */
  3824. /* Fixes issue with id_000020,sig_06,src_001958,op_flip4,pos_149 */
  3825. /* of https://github.com/uclouvain/openjpeg/issues/939 */
  3826. /* We must avoid reading twice the same tile part number for a given tile */
  3827. /* so as to avoid various issues, like opj_j2k_merge_ppt being called */
  3828. /* several times. */
  3829. /* ISO 15444-1 A.4.2 Start of tile-part (SOT) mandates that tile parts */
  3830. /* should appear in increasing order. */
  3831. if (l_tcp->m_current_tile_part_number + 1 != (OPJ_INT32)l_current_part) {
  3832. opj_event_msg(p_manager, EVT_ERROR,
  3833. "Invalid tile part index for tile number %d. "
  3834. "Got %d, expected %d\n",
  3835. p_j2k->m_current_tile_number,
  3836. l_current_part,
  3837. l_tcp->m_current_tile_part_number + 1);
  3838. return OPJ_FALSE;
  3839. }
  3840. }
  3841. l_tcp->m_current_tile_part_number = (OPJ_INT32) l_current_part;
  3842. #ifdef USE_JPWL
  3843. if (l_cp->correct) {
  3844. OPJ_UINT32 tileno = p_j2k->m_current_tile_number;
  3845. static OPJ_UINT32 backup_tileno = 0;
  3846. /* tileno is negative or larger than the number of tiles!!! */
  3847. if (tileno > (l_cp->tw * l_cp->th)) {
  3848. opj_event_msg(p_manager, EVT_ERROR,
  3849. "JPWL: bad tile number (%d out of a maximum of %d)\n",
  3850. tileno, (l_cp->tw * l_cp->th));
  3851. if (!JPWL_ASSUME) {
  3852. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  3853. return OPJ_FALSE;
  3854. }
  3855. /* we try to correct */
  3856. tileno = backup_tileno;
  3857. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
  3858. "- setting tile number to %d\n",
  3859. tileno);
  3860. }
  3861. /* keep your private count of tiles */
  3862. backup_tileno++;
  3863. };
  3864. #endif /* USE_JPWL */
  3865. /* look for the tile in the list of already processed tile (in parts). */
  3866. /* Optimization possible here with a more complex data structure and with the removing of tiles */
  3867. /* since the time taken by this function can only grow at the time */
  3868. /* PSot should be equal to zero or >=14 or <= 2^32-1 */
  3869. if ((l_tot_len != 0) && (l_tot_len < 14)) {
  3870. if (l_tot_len ==
  3871. 12) { /* MSD: Special case for the PHR data which are read by kakadu*/
  3872. opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n",
  3873. l_tot_len);
  3874. } else {
  3875. opj_event_msg(p_manager, EVT_ERROR,
  3876. "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len);
  3877. return OPJ_FALSE;
  3878. }
  3879. }
  3880. #ifdef USE_JPWL
  3881. if (l_cp->correct) {
  3882. /* totlen is negative or larger than the bytes left!!! */
  3883. if (/*(l_tot_len < 0) ||*/ (l_tot_len >
  3884. p_header_size)) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
  3885. opj_event_msg(p_manager, EVT_ERROR,
  3886. "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
  3887. l_tot_len,
  3888. p_header_size); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
  3889. if (!JPWL_ASSUME) {
  3890. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  3891. return OPJ_FALSE;
  3892. }
  3893. /* we try to correct */
  3894. l_tot_len = 0;
  3895. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
  3896. "- setting Psot to %d => assuming it is the last tile\n",
  3897. l_tot_len);
  3898. }
  3899. };
  3900. #endif /* USE_JPWL */
  3901. /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
  3902. if (!l_tot_len) {
  3903. opj_event_msg(p_manager, EVT_INFO,
  3904. "Psot value of the current tile-part is equal to zero, "
  3905. "we assuming it is the last tile-part of the codestream.\n");
  3906. p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
  3907. }
  3908. if (l_tcp->m_nb_tile_parts != 0 && l_current_part >= l_tcp->m_nb_tile_parts) {
  3909. /* Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=2851 */
  3910. opj_event_msg(p_manager, EVT_ERROR,
  3911. "In SOT marker, TPSot (%d) is not valid regards to the previous "
  3912. "number of tile-part (%d), giving up\n", l_current_part,
  3913. l_tcp->m_nb_tile_parts);
  3914. p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
  3915. return OPJ_FALSE;
  3916. }
  3917. if (l_num_parts !=
  3918. 0) { /* Number of tile-part header is provided by this tile-part header */
  3919. l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction;
  3920. /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
  3921. * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
  3922. if (l_tcp->m_nb_tile_parts) {
  3923. if (l_current_part >= l_tcp->m_nb_tile_parts) {
  3924. opj_event_msg(p_manager, EVT_ERROR,
  3925. "In SOT marker, TPSot (%d) is not valid regards to the current "
  3926. "number of tile-part (%d), giving up\n", l_current_part,
  3927. l_tcp->m_nb_tile_parts);
  3928. p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
  3929. return OPJ_FALSE;
  3930. }
  3931. }
  3932. if (l_current_part >= l_num_parts) {
  3933. /* testcase 451.pdf.SIGSEGV.ce9.3723 */
  3934. opj_event_msg(p_manager, EVT_ERROR,
  3935. "In SOT marker, TPSot (%d) is not valid regards to the current "
  3936. "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts);
  3937. p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
  3938. return OPJ_FALSE;
  3939. }
  3940. l_tcp->m_nb_tile_parts = l_num_parts;
  3941. }
  3942. /* If know the number of tile part header we will check if we didn't read the last*/
  3943. if (l_tcp->m_nb_tile_parts) {
  3944. if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
  3945. p_j2k->m_specific_param.m_decoder.m_can_decode =
  3946. 1; /* Process the last tile-part header*/
  3947. }
  3948. }
  3949. if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
  3950. /* Keep the size of data to skip after this marker */
  3951. p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len -
  3952. 12; /* SOT_marker_size = 12 */
  3953. } else {
  3954. /* FIXME: need to be computed from the number of bytes remaining in the codestream */
  3955. p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
  3956. }
  3957. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
  3958. /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
  3959. if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
  3960. p_j2k->m_specific_param.m_decoder.m_skip_data =
  3961. (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
  3962. || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
  3963. || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
  3964. || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
  3965. } else {
  3966. assert(p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0);
  3967. p_j2k->m_specific_param.m_decoder.m_skip_data =
  3968. (p_j2k->m_current_tile_number != (OPJ_UINT32)
  3969. p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
  3970. }
  3971. /* Index */
  3972. if (p_j2k->cstr_index) {
  3973. assert(p_j2k->cstr_index->tile_index != 00);
  3974. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno =
  3975. p_j2k->m_current_tile_number;
  3976. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno =
  3977. l_current_part;
  3978. if (l_num_parts != 0) {
  3979. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps =
  3980. l_num_parts;
  3981. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps =
  3982. l_num_parts;
  3983. if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
  3984. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
  3985. (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
  3986. if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
  3987. opj_event_msg(p_manager, EVT_ERROR,
  3988. "Not enough memory to read SOT marker. Tile index allocation failed\n");
  3989. return OPJ_FALSE;
  3990. }
  3991. } else {
  3992. opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
  3993. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
  3994. l_num_parts * sizeof(opj_tp_index_t));
  3995. if (! new_tp_index) {
  3996. opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
  3997. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
  3998. opj_event_msg(p_manager, EVT_ERROR,
  3999. "Not enough memory to read SOT marker. Tile index allocation failed\n");
  4000. return OPJ_FALSE;
  4001. }
  4002. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
  4003. new_tp_index;
  4004. }
  4005. } else {
  4006. /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
  4007. if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
  4008. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
  4009. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
  4010. (opj_tp_index_t*)opj_calloc(
  4011. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
  4012. sizeof(opj_tp_index_t));
  4013. if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
  4014. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
  4015. opj_event_msg(p_manager, EVT_ERROR,
  4016. "Not enough memory to read SOT marker. Tile index allocation failed\n");
  4017. return OPJ_FALSE;
  4018. }
  4019. }
  4020. if (l_current_part >=
  4021. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps) {
  4022. opj_tp_index_t *new_tp_index;
  4023. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps =
  4024. l_current_part + 1;
  4025. new_tp_index = (opj_tp_index_t *) opj_realloc(
  4026. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
  4027. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps *
  4028. sizeof(opj_tp_index_t));
  4029. if (! new_tp_index) {
  4030. opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
  4031. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
  4032. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
  4033. opj_event_msg(p_manager, EVT_ERROR,
  4034. "Not enough memory to read SOT marker. Tile index allocation failed\n");
  4035. return OPJ_FALSE;
  4036. }
  4037. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
  4038. new_tp_index;
  4039. }
  4040. }
  4041. }
  4042. }
  4043. /* FIXME move this onto a separate method to call before reading any SOT, remove part about main_end header, use a index struct inside p_j2k */
  4044. /* if (p_j2k->cstr_info) {
  4045. if (l_tcp->first) {
  4046. if (tileno == 0) {
  4047. p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
  4048. }
  4049. p_j2k->cstr_info->tile[tileno].tileno = tileno;
  4050. p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
  4051. p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
  4052. p_j2k->cstr_info->tile[tileno].num_tps = numparts;
  4053. if (numparts) {
  4054. p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
  4055. }
  4056. else {
  4057. p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
  4058. }
  4059. }
  4060. else {
  4061. p_j2k->cstr_info->tile[tileno].end_pos += totlen;
  4062. }
  4063. p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
  4064. p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
  4065. p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
  4066. }*/
  4067. return OPJ_TRUE;
  4068. }
  4069. /**
  4070. * Write one or more PLT markers in the provided buffer
  4071. */
  4072. static OPJ_BOOL opj_j2k_write_plt_in_memory(opj_j2k_t *p_j2k,
  4073. opj_tcd_marker_info_t* marker_info,
  4074. OPJ_BYTE * p_data,
  4075. OPJ_UINT32 * p_data_written,
  4076. opj_event_mgr_t * p_manager)
  4077. {
  4078. OPJ_BYTE Zplt = 0;
  4079. OPJ_UINT16 Lplt;
  4080. OPJ_BYTE* p_data_start = p_data;
  4081. OPJ_BYTE* p_data_Lplt = p_data + 2;
  4082. OPJ_UINT32 i;
  4083. OPJ_UNUSED(p_j2k);
  4084. opj_write_bytes(p_data, J2K_MS_PLT, 2);
  4085. p_data += 2;
  4086. /* Reserve space for Lplt */
  4087. p_data += 2;
  4088. opj_write_bytes(p_data, Zplt, 1);
  4089. p_data += 1;
  4090. Lplt = 3;
  4091. for (i = 0; i < marker_info->packet_count; i++) {
  4092. OPJ_BYTE var_bytes[5];
  4093. OPJ_UINT8 var_bytes_size = 0;
  4094. OPJ_UINT32 packet_size = marker_info->p_packet_size[i];
  4095. /* Packet size written in variable-length way, starting with LSB */
  4096. var_bytes[var_bytes_size] = (OPJ_BYTE)(packet_size & 0x7f);
  4097. var_bytes_size ++;
  4098. packet_size >>= 7;
  4099. while (packet_size > 0) {
  4100. var_bytes[var_bytes_size] = (OPJ_BYTE)((packet_size & 0x7f) | 0x80);
  4101. var_bytes_size ++;
  4102. packet_size >>= 7;
  4103. }
  4104. /* Check if that can fit in the current PLT marker. If not, finish */
  4105. /* current one, and start a new one */
  4106. if (Lplt + var_bytes_size > 65535) {
  4107. if (Zplt == 255) {
  4108. opj_event_msg(p_manager, EVT_ERROR,
  4109. "More than 255 PLT markers would be needed for current tile-part !\n");
  4110. return OPJ_FALSE;
  4111. }
  4112. /* Patch Lplt */
  4113. opj_write_bytes(p_data_Lplt, Lplt, 2);
  4114. /* Start new segment */
  4115. opj_write_bytes(p_data, J2K_MS_PLT, 2);
  4116. p_data += 2;
  4117. /* Reserve space for Lplt */
  4118. p_data_Lplt = p_data;
  4119. p_data += 2;
  4120. Zplt ++;
  4121. opj_write_bytes(p_data, Zplt, 1);
  4122. p_data += 1;
  4123. Lplt = 3;
  4124. }
  4125. Lplt = (OPJ_UINT16)(Lplt + var_bytes_size);
  4126. /* Serialize variable-length packet size, starting with MSB */
  4127. for (; var_bytes_size > 0; --var_bytes_size) {
  4128. opj_write_bytes(p_data, var_bytes[var_bytes_size - 1], 1);
  4129. p_data += 1;
  4130. }
  4131. }
  4132. *p_data_written = (OPJ_UINT32)(p_data - p_data_start);
  4133. /* Patch Lplt */
  4134. opj_write_bytes(p_data_Lplt, Lplt, 2);
  4135. return OPJ_TRUE;
  4136. }
  4137. static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,
  4138. opj_tcd_t * p_tile_coder,
  4139. OPJ_BYTE * p_data,
  4140. OPJ_UINT32 * p_data_written,
  4141. OPJ_UINT32 total_data_size,
  4142. const opj_stream_private_t *p_stream,
  4143. opj_event_mgr_t * p_manager
  4144. )
  4145. {
  4146. opj_codestream_info_t *l_cstr_info = 00;
  4147. OPJ_UINT32 l_remaining_data;
  4148. opj_tcd_marker_info_t* marker_info = NULL;
  4149. /* preconditions */
  4150. assert(p_j2k != 00);
  4151. assert(p_manager != 00);
  4152. assert(p_stream != 00);
  4153. OPJ_UNUSED(p_stream);
  4154. if (total_data_size < 4) {
  4155. opj_event_msg(p_manager, EVT_ERROR,
  4156. "Not enough bytes in output buffer to write SOD marker\n");
  4157. return OPJ_FALSE;
  4158. }
  4159. opj_write_bytes(p_data, J2K_MS_SOD,
  4160. 2); /* SOD */
  4161. /* make room for the EOF marker */
  4162. l_remaining_data = total_data_size - 4;
  4163. /* update tile coder */
  4164. p_tile_coder->tp_num =
  4165. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
  4166. p_tile_coder->cur_tp_num =
  4167. p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
  4168. /* INDEX >> */
  4169. /* TODO mergeV2: check this part which use cstr_info */
  4170. /*l_cstr_info = p_j2k->cstr_info;
  4171. if (l_cstr_info) {
  4172. if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
  4173. //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
  4174. l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
  4175. }
  4176. else {*/
  4177. /*
  4178. TODO
  4179. if
  4180. (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
  4181. {
  4182. cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
  4183. }*/
  4184. /*}*/
  4185. /* UniPG>> */
  4186. #ifdef USE_JPWL
  4187. /* update markers struct */
  4188. /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
  4189. */
  4190. assert(0 && "TODO");
  4191. #endif /* USE_JPWL */
  4192. /* <<UniPG */
  4193. /*}*/
  4194. /* << INDEX */
  4195. if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
  4196. p_tile_coder->tcd_image->tiles->packno = 0;
  4197. #ifdef deadcode
  4198. if (l_cstr_info) {
  4199. l_cstr_info->packno = 0;
  4200. }
  4201. #endif
  4202. }
  4203. *p_data_written = 0;
  4204. if (p_j2k->m_specific_param.m_encoder.m_PLT) {
  4205. marker_info = opj_tcd_marker_info_create(
  4206. p_j2k->m_specific_param.m_encoder.m_PLT);
  4207. if (marker_info == NULL) {
  4208. opj_event_msg(p_manager, EVT_ERROR,
  4209. "Cannot encode tile: opj_tcd_marker_info_create() failed\n");
  4210. return OPJ_FALSE;
  4211. }
  4212. }
  4213. if (l_remaining_data <
  4214. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT) {
  4215. opj_event_msg(p_manager, EVT_ERROR,
  4216. "Not enough bytes in output buffer to write SOD marker\n");
  4217. opj_tcd_marker_info_destroy(marker_info);
  4218. return OPJ_FALSE;
  4219. }
  4220. l_remaining_data -= p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT;
  4221. if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number,
  4222. p_data + 2,
  4223. p_data_written, l_remaining_data, l_cstr_info,
  4224. marker_info,
  4225. p_manager)) {
  4226. opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");
  4227. opj_tcd_marker_info_destroy(marker_info);
  4228. return OPJ_FALSE;
  4229. }
  4230. /* For SOD */
  4231. *p_data_written += 2;
  4232. if (p_j2k->m_specific_param.m_encoder.m_PLT) {
  4233. OPJ_UINT32 l_data_written_PLT = 0;
  4234. OPJ_BYTE* p_PLT_buffer = (OPJ_BYTE*)opj_malloc(
  4235. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT);
  4236. if (!p_PLT_buffer) {
  4237. opj_event_msg(p_manager, EVT_ERROR, "Cannot allocate memory\n");
  4238. opj_tcd_marker_info_destroy(marker_info);
  4239. return OPJ_FALSE;
  4240. }
  4241. if (!opj_j2k_write_plt_in_memory(p_j2k,
  4242. marker_info,
  4243. p_PLT_buffer,
  4244. &l_data_written_PLT,
  4245. p_manager)) {
  4246. opj_tcd_marker_info_destroy(marker_info);
  4247. opj_free(p_PLT_buffer);
  4248. return OPJ_FALSE;
  4249. }
  4250. assert(l_data_written_PLT <=
  4251. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT);
  4252. /* Move PLT marker(s) before SOD */
  4253. memmove(p_data + l_data_written_PLT, p_data, *p_data_written);
  4254. memcpy(p_data, p_PLT_buffer, l_data_written_PLT);
  4255. opj_free(p_PLT_buffer);
  4256. *p_data_written += l_data_written_PLT;
  4257. }
  4258. opj_tcd_marker_info_destroy(marker_info);
  4259. return OPJ_TRUE;
  4260. }
  4261. static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k,
  4262. opj_stream_private_t *p_stream,
  4263. opj_event_mgr_t * p_manager
  4264. )
  4265. {
  4266. OPJ_SIZE_T l_current_read_size;
  4267. opj_codestream_index_t * l_cstr_index = 00;
  4268. OPJ_BYTE ** l_current_data = 00;
  4269. opj_tcp_t * l_tcp = 00;
  4270. OPJ_UINT32 * l_tile_len = 00;
  4271. OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE;
  4272. /* preconditions */
  4273. assert(p_j2k != 00);
  4274. assert(p_manager != 00);
  4275. assert(p_stream != 00);
  4276. l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
  4277. if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
  4278. /* opj_stream_get_number_byte_left returns OPJ_OFF_T
  4279. // but we are in the last tile part,
  4280. // so its result will fit on OPJ_UINT32 unless we find
  4281. // a file with a single tile part of more than 4 GB...*/
  4282. p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(
  4283. opj_stream_get_number_byte_left(p_stream) - 2);
  4284. } else {
  4285. /* Check to avoid pass the limit of OPJ_UINT32 */
  4286. if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) {
  4287. p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
  4288. } else {
  4289. /* MSD: case commented to support empty SOT marker (PHR data) */
  4290. }
  4291. }
  4292. l_current_data = &(l_tcp->m_data);
  4293. l_tile_len = &l_tcp->m_data_size;
  4294. /* Patch to support new PHR data */
  4295. if (p_j2k->m_specific_param.m_decoder.m_sot_length) {
  4296. /* If we are here, we'll try to read the data after allocation */
  4297. /* Check enough bytes left in stream before allocation */
  4298. if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length >
  4299. opj_stream_get_number_byte_left(p_stream)) {
  4300. if (p_j2k->m_cp.strict) {
  4301. opj_event_msg(p_manager, EVT_ERROR,
  4302. "Tile part length size inconsistent with stream length\n");
  4303. return OPJ_FALSE;
  4304. } else {
  4305. opj_event_msg(p_manager, EVT_WARNING,
  4306. "Tile part length size inconsistent with stream length\n");
  4307. }
  4308. }
  4309. if (p_j2k->m_specific_param.m_decoder.m_sot_length >
  4310. UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) {
  4311. opj_event_msg(p_manager, EVT_ERROR,
  4312. "p_j2k->m_specific_param.m_decoder.m_sot_length > "
  4313. "UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA");
  4314. return OPJ_FALSE;
  4315. }
  4316. /* Add a margin of OPJ_COMMON_CBLK_DATA_EXTRA to the allocation we */
  4317. /* do so that opj_mqc_init_dec_common() can safely add a synthetic */
  4318. /* 0xFFFF marker. */
  4319. if (! *l_current_data) {
  4320. /* LH: oddly enough, in this path, l_tile_len!=0.
  4321. * TODO: If this was consistent, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).
  4322. */
  4323. *l_current_data = (OPJ_BYTE*) opj_malloc(
  4324. p_j2k->m_specific_param.m_decoder.m_sot_length + OPJ_COMMON_CBLK_DATA_EXTRA);
  4325. } else {
  4326. OPJ_BYTE *l_new_current_data;
  4327. if (*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA -
  4328. p_j2k->m_specific_param.m_decoder.m_sot_length) {
  4329. opj_event_msg(p_manager, EVT_ERROR,
  4330. "*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - "
  4331. "p_j2k->m_specific_param.m_decoder.m_sot_length");
  4332. return OPJ_FALSE;
  4333. }
  4334. l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data,
  4335. *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length +
  4336. OPJ_COMMON_CBLK_DATA_EXTRA);
  4337. if (! l_new_current_data) {
  4338. opj_free(*l_current_data);
  4339. /*nothing more is done as l_current_data will be set to null, and just
  4340. afterward we enter in the error path
  4341. and the actual tile_len is updated (committed) at the end of the
  4342. function. */
  4343. }
  4344. *l_current_data = l_new_current_data;
  4345. }
  4346. if (*l_current_data == 00) {
  4347. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n");
  4348. return OPJ_FALSE;
  4349. }
  4350. } else {
  4351. l_sot_length_pb_detected = OPJ_TRUE;
  4352. }
  4353. /* Index */
  4354. l_cstr_index = p_j2k->cstr_index;
  4355. if (l_cstr_index) {
  4356. OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
  4357. OPJ_UINT32 l_current_tile_part =
  4358. l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
  4359. l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header
  4360. =
  4361. l_current_pos;
  4362. l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos
  4363. =
  4364. l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
  4365. if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
  4366. l_cstr_index,
  4367. J2K_MS_SOD,
  4368. l_current_pos,
  4369. p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) {
  4370. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
  4371. return OPJ_FALSE;
  4372. }
  4373. /*l_cstr_index->packno = 0;*/
  4374. }
  4375. /* Patch to support new PHR data */
  4376. if (!l_sot_length_pb_detected) {
  4377. l_current_read_size = opj_stream_read_data(
  4378. p_stream,
  4379. *l_current_data + *l_tile_len,
  4380. p_j2k->m_specific_param.m_decoder.m_sot_length,
  4381. p_manager);
  4382. } else {
  4383. l_current_read_size = 0;
  4384. }
  4385. if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
  4386. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
  4387. } else {
  4388. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
  4389. }
  4390. *l_tile_len += (OPJ_UINT32)l_current_read_size;
  4391. return OPJ_TRUE;
  4392. }
  4393. static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
  4394. OPJ_UINT32 p_tile_no,
  4395. OPJ_UINT32 p_comp_no,
  4396. OPJ_UINT32 nb_comps,
  4397. opj_stream_private_t *p_stream,
  4398. opj_event_mgr_t * p_manager
  4399. )
  4400. {
  4401. OPJ_BYTE * l_current_data = 00;
  4402. OPJ_UINT32 l_rgn_size;
  4403. opj_cp_t *l_cp = 00;
  4404. opj_tcp_t *l_tcp = 00;
  4405. opj_tccp_t *l_tccp = 00;
  4406. OPJ_UINT32 l_comp_room;
  4407. /* preconditions */
  4408. assert(p_j2k != 00);
  4409. assert(p_manager != 00);
  4410. assert(p_stream != 00);
  4411. l_cp = &(p_j2k->m_cp);
  4412. l_tcp = &l_cp->tcps[p_tile_no];
  4413. l_tccp = &l_tcp->tccps[p_comp_no];
  4414. if (nb_comps <= 256) {
  4415. l_comp_room = 1;
  4416. } else {
  4417. l_comp_room = 2;
  4418. }
  4419. l_rgn_size = 6 + l_comp_room;
  4420. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  4421. opj_write_bytes(l_current_data, J2K_MS_RGN,
  4422. 2); /* RGN */
  4423. l_current_data += 2;
  4424. opj_write_bytes(l_current_data, l_rgn_size - 2,
  4425. 2); /* Lrgn */
  4426. l_current_data += 2;
  4427. opj_write_bytes(l_current_data, p_comp_no,
  4428. l_comp_room); /* Crgn */
  4429. l_current_data += l_comp_room;
  4430. opj_write_bytes(l_current_data, 0,
  4431. 1); /* Srgn */
  4432. ++l_current_data;
  4433. opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift,
  4434. 1); /* SPrgn */
  4435. ++l_current_data;
  4436. if (opj_stream_write_data(p_stream,
  4437. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_rgn_size,
  4438. p_manager) != l_rgn_size) {
  4439. return OPJ_FALSE;
  4440. }
  4441. return OPJ_TRUE;
  4442. }
  4443. static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k,
  4444. opj_stream_private_t *p_stream,
  4445. opj_event_mgr_t * p_manager
  4446. )
  4447. {
  4448. /* preconditions */
  4449. assert(p_j2k != 00);
  4450. assert(p_manager != 00);
  4451. assert(p_stream != 00);
  4452. opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,
  4453. J2K_MS_EOC, 2); /* EOC */
  4454. /* UniPG>> */
  4455. #ifdef USE_JPWL
  4456. /* update markers struct */
  4457. /*
  4458. OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
  4459. */
  4460. #endif /* USE_JPWL */
  4461. if (opj_stream_write_data(p_stream,
  4462. p_j2k->m_specific_param.m_encoder.m_header_tile_data, 2, p_manager) != 2) {
  4463. return OPJ_FALSE;
  4464. }
  4465. if (! opj_stream_flush(p_stream, p_manager)) {
  4466. return OPJ_FALSE;
  4467. }
  4468. return OPJ_TRUE;
  4469. }
  4470. /**
  4471. * Reads a RGN marker (Region Of Interest)
  4472. *
  4473. * @param p_header_data the data contained in the POC box.
  4474. * @param p_j2k the jpeg2000 codec.
  4475. * @param p_header_size the size of the data contained in the POC marker.
  4476. * @param p_manager the user event manager.
  4477. */
  4478. static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k,
  4479. OPJ_BYTE * p_header_data,
  4480. OPJ_UINT32 p_header_size,
  4481. opj_event_mgr_t * p_manager
  4482. )
  4483. {
  4484. OPJ_UINT32 l_nb_comp;
  4485. opj_image_t * l_image = 00;
  4486. opj_cp_t *l_cp = 00;
  4487. opj_tcp_t *l_tcp = 00;
  4488. OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
  4489. /* preconditions*/
  4490. assert(p_header_data != 00);
  4491. assert(p_j2k != 00);
  4492. assert(p_manager != 00);
  4493. l_image = p_j2k->m_private_image;
  4494. l_nb_comp = l_image->numcomps;
  4495. if (l_nb_comp <= 256) {
  4496. l_comp_room = 1;
  4497. } else {
  4498. l_comp_room = 2;
  4499. }
  4500. if (p_header_size != 2 + l_comp_room) {
  4501. opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n");
  4502. return OPJ_FALSE;
  4503. }
  4504. l_cp = &(p_j2k->m_cp);
  4505. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
  4506. &l_cp->tcps[p_j2k->m_current_tile_number] :
  4507. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  4508. opj_read_bytes(p_header_data, &l_comp_no, l_comp_room); /* Crgn */
  4509. p_header_data += l_comp_room;
  4510. opj_read_bytes(p_header_data, &l_roi_sty,
  4511. 1); /* Srgn */
  4512. ++p_header_data;
  4513. #ifdef USE_JPWL
  4514. if (l_cp->correct) {
  4515. /* totlen is negative or larger than the bytes left!!! */
  4516. if (l_comp_room >= l_nb_comp) {
  4517. opj_event_msg(p_manager, EVT_ERROR,
  4518. "JPWL: bad component number in RGN (%d when there are only %d)\n",
  4519. l_comp_room, l_nb_comp);
  4520. if (!JPWL_ASSUME) {
  4521. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  4522. return OPJ_FALSE;
  4523. }
  4524. }
  4525. };
  4526. #endif /* USE_JPWL */
  4527. /* testcase 3635.pdf.asan.77.2930 */
  4528. if (l_comp_no >= l_nb_comp) {
  4529. opj_event_msg(p_manager, EVT_ERROR,
  4530. "bad component number in RGN (%d when there are only %d)\n",
  4531. l_comp_no, l_nb_comp);
  4532. return OPJ_FALSE;
  4533. }
  4534. opj_read_bytes(p_header_data,
  4535. (OPJ_UINT32 *)(&(l_tcp->tccps[l_comp_no].roishift)), 1); /* SPrgn */
  4536. ++p_header_data;
  4537. return OPJ_TRUE;
  4538. }
  4539. static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp)
  4540. {
  4541. return (OPJ_FLOAT32)((p_tcp->m_nb_tile_parts - 1) * 14);
  4542. }
  4543. static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp)
  4544. {
  4545. (void)p_tcp;
  4546. return 0;
  4547. }
  4548. static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k,
  4549. opj_stream_private_t *p_stream,
  4550. opj_event_mgr_t * p_manager)
  4551. {
  4552. opj_cp_t * l_cp = 00;
  4553. opj_image_t * l_image = 00;
  4554. opj_tcp_t * l_tcp = 00;
  4555. opj_image_comp_t * l_img_comp = 00;
  4556. OPJ_UINT32 i, j, k;
  4557. OPJ_INT32 l_x0, l_y0, l_x1, l_y1;
  4558. OPJ_FLOAT32 * l_rates = 0;
  4559. OPJ_FLOAT32 l_sot_remove;
  4560. OPJ_UINT32 l_bits_empty, l_size_pixel;
  4561. OPJ_UINT64 l_tile_size = 0;
  4562. OPJ_UINT32 l_last_res;
  4563. OPJ_FLOAT32(* l_tp_stride_func)(opj_tcp_t *) = 00;
  4564. /* preconditions */
  4565. assert(p_j2k != 00);
  4566. assert(p_manager != 00);
  4567. assert(p_stream != 00);
  4568. OPJ_UNUSED(p_manager);
  4569. l_cp = &(p_j2k->m_cp);
  4570. l_image = p_j2k->m_private_image;
  4571. l_tcp = l_cp->tcps;
  4572. l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
  4573. l_size_pixel = l_image->numcomps * l_image->comps->prec;
  4574. l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)(
  4575. l_cp->th * l_cp->tw);
  4576. if (l_cp->m_specific_param.m_enc.m_tp_on) {
  4577. l_tp_stride_func = opj_j2k_get_tp_stride;
  4578. } else {
  4579. l_tp_stride_func = opj_j2k_get_default_stride;
  4580. }
  4581. for (i = 0; i < l_cp->th; ++i) {
  4582. for (j = 0; j < l_cp->tw; ++j) {
  4583. OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) /
  4584. (OPJ_FLOAT32)l_tcp->numlayers;
  4585. /* 4 borders of the tile rescale on the image if necessary */
  4586. l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx),
  4587. (OPJ_INT32)l_image->x0);
  4588. l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy),
  4589. (OPJ_INT32)l_image->y0);
  4590. l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx),
  4591. (OPJ_INT32)l_image->x1);
  4592. l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy),
  4593. (OPJ_INT32)l_image->y1);
  4594. l_rates = l_tcp->rates;
  4595. /* Modification of the RATE >> */
  4596. for (k = 0; k < l_tcp->numlayers; ++k) {
  4597. if (*l_rates > 0.0f) {
  4598. *l_rates = (OPJ_FLOAT32)(((OPJ_FLOAT64)l_size_pixel * (OPJ_UINT32)(
  4599. l_x1 - l_x0) *
  4600. (OPJ_UINT32)(l_y1 - l_y0))
  4601. / ((*l_rates) * (OPJ_FLOAT32)l_bits_empty))
  4602. -
  4603. l_offset;
  4604. }
  4605. ++l_rates;
  4606. }
  4607. ++l_tcp;
  4608. }
  4609. }
  4610. l_tcp = l_cp->tcps;
  4611. for (i = 0; i < l_cp->th; ++i) {
  4612. for (j = 0; j < l_cp->tw; ++j) {
  4613. l_rates = l_tcp->rates;
  4614. if (*l_rates > 0.0f) {
  4615. *l_rates -= l_sot_remove;
  4616. if (*l_rates < 30.0f) {
  4617. *l_rates = 30.0f;
  4618. }
  4619. }
  4620. ++l_rates;
  4621. l_last_res = l_tcp->numlayers - 1;
  4622. for (k = 1; k < l_last_res; ++k) {
  4623. if (*l_rates > 0.0f) {
  4624. *l_rates -= l_sot_remove;
  4625. if (*l_rates < * (l_rates - 1) + 10.0f) {
  4626. *l_rates = (*(l_rates - 1)) + 20.0f;
  4627. }
  4628. }
  4629. ++l_rates;
  4630. }
  4631. if (*l_rates > 0.0f) {
  4632. *l_rates -= (l_sot_remove + 2.f);
  4633. if (*l_rates < * (l_rates - 1) + 10.0f) {
  4634. *l_rates = (*(l_rates - 1)) + 20.0f;
  4635. }
  4636. }
  4637. ++l_tcp;
  4638. }
  4639. }
  4640. l_img_comp = l_image->comps;
  4641. l_tile_size = 0;
  4642. for (i = 0; i < l_image->numcomps; ++i) {
  4643. l_tile_size += (OPJ_UINT64)opj_uint_ceildiv(l_cp->tdx, l_img_comp->dx)
  4644. *
  4645. opj_uint_ceildiv(l_cp->tdy, l_img_comp->dy)
  4646. *
  4647. l_img_comp->prec;
  4648. ++l_img_comp;
  4649. }
  4650. /* TODO: where does this magic value come from ? */
  4651. /* This used to be 1.3 / 8, but with random data and very small code */
  4652. /* block sizes, this is not enough. For example with */
  4653. /* bin/test_tile_encoder 1 256 256 32 32 8 0 reversible_with_precinct.j2k 4 4 3 0 0 1 16 16 */
  4654. /* TODO revise this to take into account the overhead linked to the */
  4655. /* number of packets and number of code blocks in packets */
  4656. l_tile_size = (OPJ_UINT64)((double)l_tile_size * 1.4 / 8);
  4657. /* Arbitrary amount to make the following work: */
  4658. /* bin/test_tile_encoder 1 256 256 17 16 8 0 reversible_no_precinct.j2k 4 4 3 0 0 1 */
  4659. l_tile_size += 500;
  4660. l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
  4661. if (l_tile_size > UINT_MAX) {
  4662. l_tile_size = UINT_MAX;
  4663. }
  4664. p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = (OPJ_UINT32)l_tile_size;
  4665. p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
  4666. (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
  4667. if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
  4668. opj_event_msg(p_manager, EVT_ERROR,
  4669. "Not enough memory to allocate m_encoded_tile_data. %u MB required\n",
  4670. (OPJ_UINT32)(l_tile_size / 1024 / 1024));
  4671. return OPJ_FALSE;
  4672. }
  4673. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  4674. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
  4675. (OPJ_BYTE *) opj_malloc(6 *
  4676. p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
  4677. if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
  4678. return OPJ_FALSE;
  4679. }
  4680. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
  4681. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
  4682. }
  4683. return OPJ_TRUE;
  4684. }
  4685. #if 0
  4686. static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k,
  4687. opj_stream_private_t *p_stream,
  4688. opj_event_mgr_t * p_manager)
  4689. {
  4690. OPJ_UINT32 i;
  4691. opj_tcd_t * l_tcd = 00;
  4692. OPJ_UINT32 l_nb_tiles;
  4693. opj_tcp_t * l_tcp = 00;
  4694. OPJ_BOOL l_success;
  4695. /* preconditions */
  4696. assert(p_j2k != 00);
  4697. assert(p_manager != 00);
  4698. assert(p_stream != 00);
  4699. l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  4700. l_tcp = p_j2k->m_cp.tcps;
  4701. l_tcd = opj_tcd_create(OPJ_TRUE);
  4702. if (l_tcd == 00) {
  4703. opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
  4704. return OPJ_FALSE;
  4705. }
  4706. for (i = 0; i < l_nb_tiles; ++i) {
  4707. if (l_tcp->m_data) {
  4708. if (! opj_tcd_init_decode_tile(l_tcd, i)) {
  4709. opj_tcd_destroy(l_tcd);
  4710. opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
  4711. return OPJ_FALSE;
  4712. }
  4713. l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i,
  4714. p_j2k->cstr_index);
  4715. /* cleanup */
  4716. if (! l_success) {
  4717. p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
  4718. break;
  4719. }
  4720. }
  4721. opj_j2k_tcp_destroy(l_tcp);
  4722. ++l_tcp;
  4723. }
  4724. opj_tcd_destroy(l_tcd);
  4725. return OPJ_TRUE;
  4726. }
  4727. #endif
  4728. static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
  4729. struct opj_stream_private *p_stream,
  4730. struct opj_event_mgr * p_manager)
  4731. {
  4732. /* preconditions */
  4733. assert(p_j2k != 00);
  4734. assert(p_manager != 00);
  4735. assert(p_stream != 00);
  4736. OPJ_UNUSED(p_manager);
  4737. p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
  4738. return OPJ_TRUE;
  4739. }
  4740. static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k,
  4741. struct opj_stream_private *p_stream,
  4742. struct opj_event_mgr * p_manager)
  4743. {
  4744. OPJ_UINT32 i;
  4745. opj_simple_mcc_decorrelation_data_t * l_mcc_record;
  4746. opj_mct_data_t * l_mct_record;
  4747. opj_tcp_t * l_tcp;
  4748. /* preconditions */
  4749. assert(p_j2k != 00);
  4750. assert(p_stream != 00);
  4751. assert(p_manager != 00);
  4752. if (! opj_j2k_write_cbd(p_j2k, p_stream, p_manager)) {
  4753. return OPJ_FALSE;
  4754. }
  4755. l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
  4756. l_mct_record = l_tcp->m_mct_records;
  4757. for (i = 0; i < l_tcp->m_nb_mct_records; ++i) {
  4758. if (! opj_j2k_write_mct_record(p_j2k, l_mct_record, p_stream, p_manager)) {
  4759. return OPJ_FALSE;
  4760. }
  4761. ++l_mct_record;
  4762. }
  4763. l_mcc_record = l_tcp->m_mcc_records;
  4764. for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
  4765. if (! opj_j2k_write_mcc_record(p_j2k, l_mcc_record, p_stream, p_manager)) {
  4766. return OPJ_FALSE;
  4767. }
  4768. ++l_mcc_record;
  4769. }
  4770. if (! opj_j2k_write_mco(p_j2k, p_stream, p_manager)) {
  4771. return OPJ_FALSE;
  4772. }
  4773. return OPJ_TRUE;
  4774. }
  4775. static OPJ_BOOL opj_j2k_write_all_coc(
  4776. opj_j2k_t *p_j2k,
  4777. struct opj_stream_private *p_stream,
  4778. struct opj_event_mgr * p_manager)
  4779. {
  4780. OPJ_UINT32 compno;
  4781. /* preconditions */
  4782. assert(p_j2k != 00);
  4783. assert(p_manager != 00);
  4784. assert(p_stream != 00);
  4785. for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) {
  4786. /* cod is first component of first tile */
  4787. if (! opj_j2k_compare_coc(p_j2k, 0, compno)) {
  4788. if (! opj_j2k_write_coc(p_j2k, compno, p_stream, p_manager)) {
  4789. return OPJ_FALSE;
  4790. }
  4791. }
  4792. }
  4793. return OPJ_TRUE;
  4794. }
  4795. static OPJ_BOOL opj_j2k_write_all_qcc(
  4796. opj_j2k_t *p_j2k,
  4797. struct opj_stream_private *p_stream,
  4798. struct opj_event_mgr * p_manager)
  4799. {
  4800. OPJ_UINT32 compno;
  4801. /* preconditions */
  4802. assert(p_j2k != 00);
  4803. assert(p_manager != 00);
  4804. assert(p_stream != 00);
  4805. for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) {
  4806. /* qcd is first component of first tile */
  4807. if (! opj_j2k_compare_qcc(p_j2k, 0, compno)) {
  4808. if (! opj_j2k_write_qcc(p_j2k, compno, p_stream, p_manager)) {
  4809. return OPJ_FALSE;
  4810. }
  4811. }
  4812. }
  4813. return OPJ_TRUE;
  4814. }
  4815. static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k,
  4816. struct opj_stream_private *p_stream,
  4817. struct opj_event_mgr * p_manager)
  4818. {
  4819. OPJ_UINT32 compno;
  4820. const opj_tccp_t *l_tccp = 00;
  4821. /* preconditions */
  4822. assert(p_j2k != 00);
  4823. assert(p_manager != 00);
  4824. assert(p_stream != 00);
  4825. l_tccp = p_j2k->m_cp.tcps->tccps;
  4826. for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
  4827. if (l_tccp->roishift) {
  4828. if (! opj_j2k_write_rgn(p_j2k, 0, compno, p_j2k->m_private_image->numcomps,
  4829. p_stream, p_manager)) {
  4830. return OPJ_FALSE;
  4831. }
  4832. }
  4833. ++l_tccp;
  4834. }
  4835. return OPJ_TRUE;
  4836. }
  4837. static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k,
  4838. struct opj_stream_private *p_stream,
  4839. struct opj_event_mgr * p_manager)
  4840. {
  4841. opj_codestream_index_t * l_cstr_index = 00;
  4842. /* preconditions */
  4843. assert(p_j2k != 00);
  4844. assert(p_manager != 00);
  4845. assert(p_stream != 00);
  4846. OPJ_UNUSED(p_manager);
  4847. l_cstr_index = p_j2k->cstr_index;
  4848. if (l_cstr_index) {
  4849. l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream);
  4850. /* UniPG>> */
  4851. /* The following adjustment is done to adjust the codestream size */
  4852. /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
  4853. /* the first bunch of bytes is not in the codestream */
  4854. l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start;
  4855. /* <<UniPG */
  4856. }
  4857. #ifdef USE_JPWL
  4858. /* preparation of JPWL marker segments */
  4859. #if 0
  4860. if (cp->epc_on) {
  4861. /* encode according to JPWL */
  4862. jpwl_encode(p_j2k, p_stream, image);
  4863. }
  4864. #endif
  4865. assert(0 && "TODO");
  4866. #endif /* USE_JPWL */
  4867. return OPJ_TRUE;
  4868. }
  4869. static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k,
  4870. opj_stream_private_t *p_stream,
  4871. OPJ_UINT32 *output_marker,
  4872. opj_event_mgr_t * p_manager
  4873. )
  4874. {
  4875. OPJ_UINT32 l_unknown_marker;
  4876. const opj_dec_memory_marker_handler_t * l_marker_handler;
  4877. OPJ_UINT32 l_size_unk = 2;
  4878. /* preconditions*/
  4879. assert(p_j2k != 00);
  4880. assert(p_manager != 00);
  4881. assert(p_stream != 00);
  4882. opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n");
  4883. for (;;) {
  4884. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
  4885. if (opj_stream_read_data(p_stream,
  4886. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  4887. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  4888. return OPJ_FALSE;
  4889. }
  4890. /* read 2 bytes as the new marker ID*/
  4891. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  4892. &l_unknown_marker, 2);
  4893. if (!(l_unknown_marker < 0xff00)) {
  4894. /* Get the marker handler from the marker ID*/
  4895. l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
  4896. if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
  4897. opj_event_msg(p_manager, EVT_ERROR,
  4898. "Marker is not compliant with its position\n");
  4899. return OPJ_FALSE;
  4900. } else {
  4901. if (l_marker_handler->id != J2K_MS_UNK) {
  4902. /* Add the marker to the codestream index*/
  4903. if (l_marker_handler->id != J2K_MS_SOT) {
  4904. OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
  4905. (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
  4906. l_size_unk);
  4907. if (res == OPJ_FALSE) {
  4908. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
  4909. return OPJ_FALSE;
  4910. }
  4911. }
  4912. break; /* next marker is known and well located */
  4913. } else {
  4914. l_size_unk += 2;
  4915. }
  4916. }
  4917. }
  4918. }
  4919. *output_marker = l_marker_handler->id ;
  4920. return OPJ_TRUE;
  4921. }
  4922. static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k,
  4923. opj_mct_data_t * p_mct_record,
  4924. struct opj_stream_private *p_stream,
  4925. struct opj_event_mgr * p_manager)
  4926. {
  4927. OPJ_UINT32 l_mct_size;
  4928. OPJ_BYTE * l_current_data = 00;
  4929. OPJ_UINT32 l_tmp;
  4930. /* preconditions */
  4931. assert(p_j2k != 00);
  4932. assert(p_manager != 00);
  4933. assert(p_stream != 00);
  4934. l_mct_size = 10 + p_mct_record->m_data_size;
  4935. if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  4936. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  4937. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size);
  4938. if (! new_header_tile_data) {
  4939. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  4940. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  4941. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  4942. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n");
  4943. return OPJ_FALSE;
  4944. }
  4945. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  4946. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
  4947. }
  4948. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  4949. opj_write_bytes(l_current_data, J2K_MS_MCT,
  4950. 2); /* MCT */
  4951. l_current_data += 2;
  4952. opj_write_bytes(l_current_data, l_mct_size - 2,
  4953. 2); /* Lmct */
  4954. l_current_data += 2;
  4955. opj_write_bytes(l_current_data, 0,
  4956. 2); /* Zmct */
  4957. l_current_data += 2;
  4958. /* only one marker atm */
  4959. l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) |
  4960. (p_mct_record->m_element_type << 10);
  4961. opj_write_bytes(l_current_data, l_tmp, 2);
  4962. l_current_data += 2;
  4963. opj_write_bytes(l_current_data, 0,
  4964. 2); /* Ymct */
  4965. l_current_data += 2;
  4966. memcpy(l_current_data, p_mct_record->m_data, p_mct_record->m_data_size);
  4967. if (opj_stream_write_data(p_stream,
  4968. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size,
  4969. p_manager) != l_mct_size) {
  4970. return OPJ_FALSE;
  4971. }
  4972. return OPJ_TRUE;
  4973. }
  4974. /**
  4975. * Reads a MCT marker (Multiple Component Transform)
  4976. *
  4977. * @param p_header_data the data contained in the MCT box.
  4978. * @param p_j2k the jpeg2000 codec.
  4979. * @param p_header_size the size of the data contained in the MCT marker.
  4980. * @param p_manager the user event manager.
  4981. */
  4982. static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k,
  4983. OPJ_BYTE * p_header_data,
  4984. OPJ_UINT32 p_header_size,
  4985. opj_event_mgr_t * p_manager
  4986. )
  4987. {
  4988. OPJ_UINT32 i;
  4989. opj_tcp_t *l_tcp = 00;
  4990. OPJ_UINT32 l_tmp;
  4991. OPJ_UINT32 l_indix;
  4992. opj_mct_data_t * l_mct_data;
  4993. /* preconditions */
  4994. assert(p_header_data != 00);
  4995. assert(p_j2k != 00);
  4996. l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
  4997. &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
  4998. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  4999. if (p_header_size < 2) {
  5000. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
  5001. return OPJ_FALSE;
  5002. }
  5003. /* first marker */
  5004. opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmct */
  5005. p_header_data += 2;
  5006. if (l_tmp != 0) {
  5007. opj_event_msg(p_manager, EVT_WARNING,
  5008. "Cannot take in charge mct data within multiple MCT records\n");
  5009. return OPJ_TRUE;
  5010. }
  5011. if (p_header_size <= 6) {
  5012. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
  5013. return OPJ_FALSE;
  5014. }
  5015. /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
  5016. opj_read_bytes(p_header_data, &l_tmp, 2); /* Imct */
  5017. p_header_data += 2;
  5018. l_indix = l_tmp & 0xff;
  5019. l_mct_data = l_tcp->m_mct_records;
  5020. for (i = 0; i < l_tcp->m_nb_mct_records; ++i) {
  5021. if (l_mct_data->m_index == l_indix) {
  5022. break;
  5023. }
  5024. ++l_mct_data;
  5025. }
  5026. /* NOT FOUND */
  5027. if (i == l_tcp->m_nb_mct_records) {
  5028. if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
  5029. opj_mct_data_t *new_mct_records;
  5030. l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  5031. new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records,
  5032. l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
  5033. if (! new_mct_records) {
  5034. opj_free(l_tcp->m_mct_records);
  5035. l_tcp->m_mct_records = NULL;
  5036. l_tcp->m_nb_max_mct_records = 0;
  5037. l_tcp->m_nb_mct_records = 0;
  5038. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n");
  5039. return OPJ_FALSE;
  5040. }
  5041. /* Update m_mcc_records[].m_offset_array and m_decorrelation_array
  5042. * to point to the new addresses */
  5043. if (new_mct_records != l_tcp->m_mct_records) {
  5044. for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
  5045. opj_simple_mcc_decorrelation_data_t* l_mcc_record =
  5046. &(l_tcp->m_mcc_records[i]);
  5047. if (l_mcc_record->m_decorrelation_array) {
  5048. l_mcc_record->m_decorrelation_array =
  5049. new_mct_records +
  5050. (l_mcc_record->m_decorrelation_array -
  5051. l_tcp->m_mct_records);
  5052. }
  5053. if (l_mcc_record->m_offset_array) {
  5054. l_mcc_record->m_offset_array =
  5055. new_mct_records +
  5056. (l_mcc_record->m_offset_array -
  5057. l_tcp->m_mct_records);
  5058. }
  5059. }
  5060. }
  5061. l_tcp->m_mct_records = new_mct_records;
  5062. l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
  5063. memset(l_mct_data, 0, (l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) *
  5064. sizeof(opj_mct_data_t));
  5065. }
  5066. l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
  5067. ++l_tcp->m_nb_mct_records;
  5068. }
  5069. if (l_mct_data->m_data) {
  5070. opj_free(l_mct_data->m_data);
  5071. l_mct_data->m_data = 00;
  5072. l_mct_data->m_data_size = 0;
  5073. }
  5074. l_mct_data->m_index = l_indix;
  5075. l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
  5076. l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
  5077. opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymct */
  5078. p_header_data += 2;
  5079. if (l_tmp != 0) {
  5080. opj_event_msg(p_manager, EVT_WARNING,
  5081. "Cannot take in charge multiple MCT markers\n");
  5082. return OPJ_TRUE;
  5083. }
  5084. p_header_size -= 6;
  5085. l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
  5086. if (! l_mct_data->m_data) {
  5087. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
  5088. return OPJ_FALSE;
  5089. }
  5090. memcpy(l_mct_data->m_data, p_header_data, p_header_size);
  5091. l_mct_data->m_data_size = p_header_size;
  5092. return OPJ_TRUE;
  5093. }
  5094. static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k,
  5095. struct opj_simple_mcc_decorrelation_data * p_mcc_record,
  5096. struct opj_stream_private *p_stream,
  5097. struct opj_event_mgr * p_manager)
  5098. {
  5099. OPJ_UINT32 i;
  5100. OPJ_UINT32 l_mcc_size;
  5101. OPJ_BYTE * l_current_data = 00;
  5102. OPJ_UINT32 l_nb_bytes_for_comp;
  5103. OPJ_UINT32 l_mask;
  5104. OPJ_UINT32 l_tmcc;
  5105. /* preconditions */
  5106. assert(p_j2k != 00);
  5107. assert(p_manager != 00);
  5108. assert(p_stream != 00);
  5109. if (p_mcc_record->m_nb_comps > 255) {
  5110. l_nb_bytes_for_comp = 2;
  5111. l_mask = 0x8000;
  5112. } else {
  5113. l_nb_bytes_for_comp = 1;
  5114. l_mask = 0;
  5115. }
  5116. l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
  5117. if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  5118. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  5119. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size);
  5120. if (! new_header_tile_data) {
  5121. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  5122. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  5123. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  5124. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n");
  5125. return OPJ_FALSE;
  5126. }
  5127. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  5128. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
  5129. }
  5130. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  5131. opj_write_bytes(l_current_data, J2K_MS_MCC,
  5132. 2); /* MCC */
  5133. l_current_data += 2;
  5134. opj_write_bytes(l_current_data, l_mcc_size - 2,
  5135. 2); /* Lmcc */
  5136. l_current_data += 2;
  5137. /* first marker */
  5138. opj_write_bytes(l_current_data, 0,
  5139. 2); /* Zmcc */
  5140. l_current_data += 2;
  5141. opj_write_bytes(l_current_data, p_mcc_record->m_index,
  5142. 1); /* Imcc -> no need for other values, take the first */
  5143. ++l_current_data;
  5144. /* only one marker atm */
  5145. opj_write_bytes(l_current_data, 0,
  5146. 2); /* Ymcc */
  5147. l_current_data += 2;
  5148. opj_write_bytes(l_current_data, 1,
  5149. 2); /* Qmcc -> number of collections -> 1 */
  5150. l_current_data += 2;
  5151. opj_write_bytes(l_current_data, 0x1,
  5152. 1); /* Xmcci type of component transformation -> array based decorrelation */
  5153. ++l_current_data;
  5154. opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask,
  5155. 2); /* Nmcci number of input components involved and size for each component offset = 8 bits */
  5156. l_current_data += 2;
  5157. for (i = 0; i < p_mcc_record->m_nb_comps; ++i) {
  5158. opj_write_bytes(l_current_data, i,
  5159. l_nb_bytes_for_comp); /* Cmccij Component offset*/
  5160. l_current_data += l_nb_bytes_for_comp;
  5161. }
  5162. opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask,
  5163. 2); /* Mmcci number of output components involved and size for each component offset = 8 bits */
  5164. l_current_data += 2;
  5165. for (i = 0; i < p_mcc_record->m_nb_comps; ++i) {
  5166. opj_write_bytes(l_current_data, i,
  5167. l_nb_bytes_for_comp); /* Wmccij Component offset*/
  5168. l_current_data += l_nb_bytes_for_comp;
  5169. }
  5170. l_tmcc = ((!p_mcc_record->m_is_irreversible) & 1U) << 16;
  5171. if (p_mcc_record->m_decorrelation_array) {
  5172. l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
  5173. }
  5174. if (p_mcc_record->m_offset_array) {
  5175. l_tmcc |= ((p_mcc_record->m_offset_array->m_index) << 8);
  5176. }
  5177. opj_write_bytes(l_current_data, l_tmcc,
  5178. 3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
  5179. l_current_data += 3;
  5180. if (opj_stream_write_data(p_stream,
  5181. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size,
  5182. p_manager) != l_mcc_size) {
  5183. return OPJ_FALSE;
  5184. }
  5185. return OPJ_TRUE;
  5186. }
  5187. static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k,
  5188. OPJ_BYTE * p_header_data,
  5189. OPJ_UINT32 p_header_size,
  5190. opj_event_mgr_t * p_manager)
  5191. {
  5192. OPJ_UINT32 i, j;
  5193. OPJ_UINT32 l_tmp;
  5194. OPJ_UINT32 l_indix;
  5195. opj_tcp_t * l_tcp;
  5196. opj_simple_mcc_decorrelation_data_t * l_mcc_record;
  5197. opj_mct_data_t * l_mct_data;
  5198. OPJ_UINT32 l_nb_collections;
  5199. OPJ_UINT32 l_nb_comps;
  5200. OPJ_UINT32 l_nb_bytes_by_comp;
  5201. OPJ_BOOL l_new_mcc = OPJ_FALSE;
  5202. /* preconditions */
  5203. assert(p_header_data != 00);
  5204. assert(p_j2k != 00);
  5205. assert(p_manager != 00);
  5206. l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
  5207. &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
  5208. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  5209. if (p_header_size < 2) {
  5210. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5211. return OPJ_FALSE;
  5212. }
  5213. /* first marker */
  5214. opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmcc */
  5215. p_header_data += 2;
  5216. if (l_tmp != 0) {
  5217. opj_event_msg(p_manager, EVT_WARNING,
  5218. "Cannot take in charge multiple data spanning\n");
  5219. return OPJ_TRUE;
  5220. }
  5221. if (p_header_size < 7) {
  5222. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5223. return OPJ_FALSE;
  5224. }
  5225. opj_read_bytes(p_header_data, &l_indix,
  5226. 1); /* Imcc -> no need for other values, take the first */
  5227. ++p_header_data;
  5228. l_mcc_record = l_tcp->m_mcc_records;
  5229. for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
  5230. if (l_mcc_record->m_index == l_indix) {
  5231. break;
  5232. }
  5233. ++l_mcc_record;
  5234. }
  5235. /** NOT FOUND */
  5236. if (i == l_tcp->m_nb_mcc_records) {
  5237. if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
  5238. opj_simple_mcc_decorrelation_data_t *new_mcc_records;
  5239. l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
  5240. new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
  5241. l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(
  5242. opj_simple_mcc_decorrelation_data_t));
  5243. if (! new_mcc_records) {
  5244. opj_free(l_tcp->m_mcc_records);
  5245. l_tcp->m_mcc_records = NULL;
  5246. l_tcp->m_nb_max_mcc_records = 0;
  5247. l_tcp->m_nb_mcc_records = 0;
  5248. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n");
  5249. return OPJ_FALSE;
  5250. }
  5251. l_tcp->m_mcc_records = new_mcc_records;
  5252. l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
  5253. memset(l_mcc_record, 0, (l_tcp->m_nb_max_mcc_records - l_tcp->m_nb_mcc_records)
  5254. * sizeof(opj_simple_mcc_decorrelation_data_t));
  5255. }
  5256. l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
  5257. l_new_mcc = OPJ_TRUE;
  5258. }
  5259. l_mcc_record->m_index = l_indix;
  5260. /* only one marker atm */
  5261. opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymcc */
  5262. p_header_data += 2;
  5263. if (l_tmp != 0) {
  5264. opj_event_msg(p_manager, EVT_WARNING,
  5265. "Cannot take in charge multiple data spanning\n");
  5266. return OPJ_TRUE;
  5267. }
  5268. opj_read_bytes(p_header_data, &l_nb_collections,
  5269. 2); /* Qmcc -> number of collections -> 1 */
  5270. p_header_data += 2;
  5271. if (l_nb_collections > 1) {
  5272. opj_event_msg(p_manager, EVT_WARNING,
  5273. "Cannot take in charge multiple collections\n");
  5274. return OPJ_TRUE;
  5275. }
  5276. p_header_size -= 7;
  5277. for (i = 0; i < l_nb_collections; ++i) {
  5278. if (p_header_size < 3) {
  5279. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5280. return OPJ_FALSE;
  5281. }
  5282. opj_read_bytes(p_header_data, &l_tmp,
  5283. 1); /* Xmcci type of component transformation -> array based decorrelation */
  5284. ++p_header_data;
  5285. if (l_tmp != 1) {
  5286. opj_event_msg(p_manager, EVT_WARNING,
  5287. "Cannot take in charge collections other than array decorrelation\n");
  5288. return OPJ_TRUE;
  5289. }
  5290. opj_read_bytes(p_header_data, &l_nb_comps, 2);
  5291. p_header_data += 2;
  5292. p_header_size -= 3;
  5293. l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15);
  5294. l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
  5295. if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
  5296. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5297. return OPJ_FALSE;
  5298. }
  5299. p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
  5300. for (j = 0; j < l_mcc_record->m_nb_comps; ++j) {
  5301. opj_read_bytes(p_header_data, &l_tmp,
  5302. l_nb_bytes_by_comp); /* Cmccij Component offset*/
  5303. p_header_data += l_nb_bytes_by_comp;
  5304. if (l_tmp != j) {
  5305. opj_event_msg(p_manager, EVT_WARNING,
  5306. "Cannot take in charge collections with indix shuffle\n");
  5307. return OPJ_TRUE;
  5308. }
  5309. }
  5310. opj_read_bytes(p_header_data, &l_nb_comps, 2);
  5311. p_header_data += 2;
  5312. l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15);
  5313. l_nb_comps &= 0x7fff;
  5314. if (l_nb_comps != l_mcc_record->m_nb_comps) {
  5315. opj_event_msg(p_manager, EVT_WARNING,
  5316. "Cannot take in charge collections without same number of indixes\n");
  5317. return OPJ_TRUE;
  5318. }
  5319. if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
  5320. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5321. return OPJ_FALSE;
  5322. }
  5323. p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
  5324. for (j = 0; j < l_mcc_record->m_nb_comps; ++j) {
  5325. opj_read_bytes(p_header_data, &l_tmp,
  5326. l_nb_bytes_by_comp); /* Wmccij Component offset*/
  5327. p_header_data += l_nb_bytes_by_comp;
  5328. if (l_tmp != j) {
  5329. opj_event_msg(p_manager, EVT_WARNING,
  5330. "Cannot take in charge collections with indix shuffle\n");
  5331. return OPJ_TRUE;
  5332. }
  5333. }
  5334. opj_read_bytes(p_header_data, &l_tmp, 3); /* Wmccij Component offset*/
  5335. p_header_data += 3;
  5336. l_mcc_record->m_is_irreversible = !((l_tmp >> 16) & 1);
  5337. l_mcc_record->m_decorrelation_array = 00;
  5338. l_mcc_record->m_offset_array = 00;
  5339. l_indix = l_tmp & 0xff;
  5340. if (l_indix != 0) {
  5341. l_mct_data = l_tcp->m_mct_records;
  5342. for (j = 0; j < l_tcp->m_nb_mct_records; ++j) {
  5343. if (l_mct_data->m_index == l_indix) {
  5344. l_mcc_record->m_decorrelation_array = l_mct_data;
  5345. break;
  5346. }
  5347. ++l_mct_data;
  5348. }
  5349. if (l_mcc_record->m_decorrelation_array == 00) {
  5350. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5351. return OPJ_FALSE;
  5352. }
  5353. }
  5354. l_indix = (l_tmp >> 8) & 0xff;
  5355. if (l_indix != 0) {
  5356. l_mct_data = l_tcp->m_mct_records;
  5357. for (j = 0; j < l_tcp->m_nb_mct_records; ++j) {
  5358. if (l_mct_data->m_index == l_indix) {
  5359. l_mcc_record->m_offset_array = l_mct_data;
  5360. break;
  5361. }
  5362. ++l_mct_data;
  5363. }
  5364. if (l_mcc_record->m_offset_array == 00) {
  5365. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5366. return OPJ_FALSE;
  5367. }
  5368. }
  5369. }
  5370. if (p_header_size != 0) {
  5371. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5372. return OPJ_FALSE;
  5373. }
  5374. if (l_new_mcc) {
  5375. ++l_tcp->m_nb_mcc_records;
  5376. }
  5377. return OPJ_TRUE;
  5378. }
  5379. static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k,
  5380. struct opj_stream_private *p_stream,
  5381. struct opj_event_mgr * p_manager
  5382. )
  5383. {
  5384. OPJ_BYTE * l_current_data = 00;
  5385. OPJ_UINT32 l_mco_size;
  5386. opj_tcp_t * l_tcp = 00;
  5387. opj_simple_mcc_decorrelation_data_t * l_mcc_record;
  5388. OPJ_UINT32 i;
  5389. /* preconditions */
  5390. assert(p_j2k != 00);
  5391. assert(p_manager != 00);
  5392. assert(p_stream != 00);
  5393. l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
  5394. l_mco_size = 5 + l_tcp->m_nb_mcc_records;
  5395. if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  5396. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  5397. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size);
  5398. if (! new_header_tile_data) {
  5399. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  5400. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  5401. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  5402. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n");
  5403. return OPJ_FALSE;
  5404. }
  5405. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  5406. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
  5407. }
  5408. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  5409. opj_write_bytes(l_current_data, J2K_MS_MCO, 2); /* MCO */
  5410. l_current_data += 2;
  5411. opj_write_bytes(l_current_data, l_mco_size - 2, 2); /* Lmco */
  5412. l_current_data += 2;
  5413. opj_write_bytes(l_current_data, l_tcp->m_nb_mcc_records,
  5414. 1); /* Nmco : only one transform stage*/
  5415. ++l_current_data;
  5416. l_mcc_record = l_tcp->m_mcc_records;
  5417. for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
  5418. opj_write_bytes(l_current_data, l_mcc_record->m_index,
  5419. 1); /* Imco -> use the mcc indicated by 1*/
  5420. ++l_current_data;
  5421. ++l_mcc_record;
  5422. }
  5423. if (opj_stream_write_data(p_stream,
  5424. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size,
  5425. p_manager) != l_mco_size) {
  5426. return OPJ_FALSE;
  5427. }
  5428. return OPJ_TRUE;
  5429. }
  5430. /**
  5431. * Reads a MCO marker (Multiple Component Transform Ordering)
  5432. *
  5433. * @param p_header_data the data contained in the MCO box.
  5434. * @param p_j2k the jpeg2000 codec.
  5435. * @param p_header_size the size of the data contained in the MCO marker.
  5436. * @param p_manager the user event manager.
  5437. */
  5438. static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k,
  5439. OPJ_BYTE * p_header_data,
  5440. OPJ_UINT32 p_header_size,
  5441. opj_event_mgr_t * p_manager
  5442. )
  5443. {
  5444. OPJ_UINT32 l_tmp, i;
  5445. OPJ_UINT32 l_nb_stages;
  5446. opj_tcp_t * l_tcp;
  5447. opj_tccp_t * l_tccp;
  5448. opj_image_t * l_image;
  5449. /* preconditions */
  5450. assert(p_header_data != 00);
  5451. assert(p_j2k != 00);
  5452. assert(p_manager != 00);
  5453. l_image = p_j2k->m_private_image;
  5454. l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
  5455. &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
  5456. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  5457. if (p_header_size < 1) {
  5458. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n");
  5459. return OPJ_FALSE;
  5460. }
  5461. opj_read_bytes(p_header_data, &l_nb_stages,
  5462. 1); /* Nmco : only one transform stage*/
  5463. ++p_header_data;
  5464. if (l_nb_stages > 1) {
  5465. opj_event_msg(p_manager, EVT_WARNING,
  5466. "Cannot take in charge multiple transformation stages.\n");
  5467. return OPJ_TRUE;
  5468. }
  5469. if (p_header_size != l_nb_stages + 1) {
  5470. opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n");
  5471. return OPJ_FALSE;
  5472. }
  5473. l_tccp = l_tcp->tccps;
  5474. for (i = 0; i < l_image->numcomps; ++i) {
  5475. l_tccp->m_dc_level_shift = 0;
  5476. ++l_tccp;
  5477. }
  5478. if (l_tcp->m_mct_decoding_matrix) {
  5479. opj_free(l_tcp->m_mct_decoding_matrix);
  5480. l_tcp->m_mct_decoding_matrix = 00;
  5481. }
  5482. for (i = 0; i < l_nb_stages; ++i) {
  5483. opj_read_bytes(p_header_data, &l_tmp, 1);
  5484. ++p_header_data;
  5485. if (! opj_j2k_add_mct(l_tcp, p_j2k->m_private_image, l_tmp)) {
  5486. return OPJ_FALSE;
  5487. }
  5488. }
  5489. return OPJ_TRUE;
  5490. }
  5491. static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image,
  5492. OPJ_UINT32 p_index)
  5493. {
  5494. OPJ_UINT32 i;
  5495. opj_simple_mcc_decorrelation_data_t * l_mcc_record;
  5496. opj_mct_data_t * l_deco_array, * l_offset_array;
  5497. OPJ_UINT32 l_data_size, l_mct_size, l_offset_size;
  5498. OPJ_UINT32 l_nb_elem;
  5499. OPJ_UINT32 * l_offset_data, * l_current_offset_data;
  5500. opj_tccp_t * l_tccp;
  5501. /* preconditions */
  5502. assert(p_tcp != 00);
  5503. l_mcc_record = p_tcp->m_mcc_records;
  5504. for (i = 0; i < p_tcp->m_nb_mcc_records; ++i) {
  5505. if (l_mcc_record->m_index == p_index) {
  5506. break;
  5507. }
  5508. }
  5509. if (i == p_tcp->m_nb_mcc_records) {
  5510. /** element discarded **/
  5511. return OPJ_TRUE;
  5512. }
  5513. if (l_mcc_record->m_nb_comps != p_image->numcomps) {
  5514. /** do not support number of comps != image */
  5515. return OPJ_TRUE;
  5516. }
  5517. l_deco_array = l_mcc_record->m_decorrelation_array;
  5518. if (l_deco_array) {
  5519. l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps
  5520. * p_image->numcomps;
  5521. if (l_deco_array->m_data_size != l_data_size) {
  5522. return OPJ_FALSE;
  5523. }
  5524. l_nb_elem = p_image->numcomps * p_image->numcomps;
  5525. l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
  5526. p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
  5527. if (! p_tcp->m_mct_decoding_matrix) {
  5528. return OPJ_FALSE;
  5529. }
  5530. j2k_mct_read_functions_to_float[l_deco_array->m_element_type](
  5531. l_deco_array->m_data, p_tcp->m_mct_decoding_matrix, l_nb_elem);
  5532. }
  5533. l_offset_array = l_mcc_record->m_offset_array;
  5534. if (l_offset_array) {
  5535. l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] *
  5536. p_image->numcomps;
  5537. if (l_offset_array->m_data_size != l_data_size) {
  5538. return OPJ_FALSE;
  5539. }
  5540. l_nb_elem = p_image->numcomps;
  5541. l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32);
  5542. l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
  5543. if (! l_offset_data) {
  5544. return OPJ_FALSE;
  5545. }
  5546. j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](
  5547. l_offset_array->m_data, l_offset_data, l_nb_elem);
  5548. l_tccp = p_tcp->tccps;
  5549. l_current_offset_data = l_offset_data;
  5550. for (i = 0; i < p_image->numcomps; ++i) {
  5551. l_tccp->m_dc_level_shift = (OPJ_INT32) * (l_current_offset_data++);
  5552. ++l_tccp;
  5553. }
  5554. opj_free(l_offset_data);
  5555. }
  5556. return OPJ_TRUE;
  5557. }
  5558. static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k,
  5559. struct opj_stream_private *p_stream,
  5560. struct opj_event_mgr * p_manager)
  5561. {
  5562. OPJ_UINT32 i;
  5563. OPJ_UINT32 l_cbd_size;
  5564. OPJ_BYTE * l_current_data = 00;
  5565. opj_image_t *l_image = 00;
  5566. opj_image_comp_t * l_comp = 00;
  5567. /* preconditions */
  5568. assert(p_j2k != 00);
  5569. assert(p_manager != 00);
  5570. assert(p_stream != 00);
  5571. l_image = p_j2k->m_private_image;
  5572. l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
  5573. if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  5574. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  5575. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size);
  5576. if (! new_header_tile_data) {
  5577. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  5578. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  5579. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  5580. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n");
  5581. return OPJ_FALSE;
  5582. }
  5583. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  5584. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
  5585. }
  5586. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  5587. opj_write_bytes(l_current_data, J2K_MS_CBD, 2); /* CBD */
  5588. l_current_data += 2;
  5589. opj_write_bytes(l_current_data, l_cbd_size - 2, 2); /* L_CBD */
  5590. l_current_data += 2;
  5591. opj_write_bytes(l_current_data, l_image->numcomps, 2); /* Ncbd */
  5592. l_current_data += 2;
  5593. l_comp = l_image->comps;
  5594. for (i = 0; i < l_image->numcomps; ++i) {
  5595. opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1),
  5596. 1); /* Component bit depth */
  5597. ++l_current_data;
  5598. ++l_comp;
  5599. }
  5600. if (opj_stream_write_data(p_stream,
  5601. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size,
  5602. p_manager) != l_cbd_size) {
  5603. return OPJ_FALSE;
  5604. }
  5605. return OPJ_TRUE;
  5606. }
  5607. /**
  5608. * Reads a CBD marker (Component bit depth definition)
  5609. * @param p_header_data the data contained in the CBD box.
  5610. * @param p_j2k the jpeg2000 codec.
  5611. * @param p_header_size the size of the data contained in the CBD marker.
  5612. * @param p_manager the user event manager.
  5613. */
  5614. static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k,
  5615. OPJ_BYTE * p_header_data,
  5616. OPJ_UINT32 p_header_size,
  5617. opj_event_mgr_t * p_manager
  5618. )
  5619. {
  5620. OPJ_UINT32 l_nb_comp, l_num_comp;
  5621. OPJ_UINT32 l_comp_def;
  5622. OPJ_UINT32 i;
  5623. opj_image_comp_t * l_comp = 00;
  5624. /* preconditions */
  5625. assert(p_header_data != 00);
  5626. assert(p_j2k != 00);
  5627. assert(p_manager != 00);
  5628. l_num_comp = p_j2k->m_private_image->numcomps;
  5629. if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
  5630. opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
  5631. return OPJ_FALSE;
  5632. }
  5633. opj_read_bytes(p_header_data, &l_nb_comp,
  5634. 2); /* Ncbd */
  5635. p_header_data += 2;
  5636. if (l_nb_comp != l_num_comp) {
  5637. opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
  5638. return OPJ_FALSE;
  5639. }
  5640. l_comp = p_j2k->m_private_image->comps;
  5641. for (i = 0; i < l_num_comp; ++i) {
  5642. opj_read_bytes(p_header_data, &l_comp_def,
  5643. 1); /* Component bit depth */
  5644. ++p_header_data;
  5645. l_comp->sgnd = (l_comp_def >> 7) & 1;
  5646. l_comp->prec = (l_comp_def & 0x7f) + 1;
  5647. if (l_comp->prec > 31) {
  5648. opj_event_msg(p_manager, EVT_ERROR,
  5649. "Invalid values for comp = %d : prec=%u (should be between 1 and 38 according to the JPEG2000 norm. OpenJpeg only supports up to 31)\n",
  5650. i, l_comp->prec);
  5651. return OPJ_FALSE;
  5652. }
  5653. ++l_comp;
  5654. }
  5655. return OPJ_TRUE;
  5656. }
  5657. /**
  5658. * Reads a CAP marker (extended capabilities definition). Empty implementation.
  5659. * Found in HTJ2K files.
  5660. *
  5661. * @param p_header_data the data contained in the CAP box.
  5662. * @param p_j2k the jpeg2000 codec.
  5663. * @param p_header_size the size of the data contained in the CAP marker.
  5664. * @param p_manager the user event manager.
  5665. */
  5666. static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k,
  5667. OPJ_BYTE * p_header_data,
  5668. OPJ_UINT32 p_header_size,
  5669. opj_event_mgr_t * p_manager
  5670. )
  5671. {
  5672. /* preconditions */
  5673. assert(p_header_data != 00);
  5674. assert(p_j2k != 00);
  5675. assert(p_manager != 00);
  5676. (void)p_j2k;
  5677. (void)p_header_data;
  5678. (void)p_header_size;
  5679. (void)p_manager;
  5680. return OPJ_TRUE;
  5681. }
  5682. /**
  5683. * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files
  5684. * @param p_header_data the data contained in the CPF box.
  5685. * @param p_j2k the jpeg2000 codec.
  5686. * @param p_header_size the size of the data contained in the CPF marker.
  5687. * @param p_manager the user event manager.
  5688. */
  5689. static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k,
  5690. OPJ_BYTE * p_header_data,
  5691. OPJ_UINT32 p_header_size,
  5692. opj_event_mgr_t * p_manager
  5693. )
  5694. {
  5695. /* preconditions */
  5696. assert(p_header_data != 00);
  5697. assert(p_j2k != 00);
  5698. assert(p_manager != 00);
  5699. (void)p_j2k;
  5700. (void)p_header_data;
  5701. (void)p_header_size;
  5702. (void)p_manager;
  5703. return OPJ_TRUE;
  5704. }
  5705. /* ----------------------------------------------------------------------- */
  5706. /* J2K / JPT decoder interface */
  5707. /* ----------------------------------------------------------------------- */
  5708. void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)
  5709. {
  5710. if (j2k && parameters) {
  5711. j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
  5712. j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
  5713. j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG);
  5714. #ifdef USE_JPWL
  5715. j2k->m_cp.correct = parameters->jpwl_correct;
  5716. j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
  5717. j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
  5718. #endif /* USE_JPWL */
  5719. }
  5720. }
  5721. void opj_j2k_decoder_set_strict_mode(opj_j2k_t *j2k, OPJ_BOOL strict)
  5722. {
  5723. if (j2k) {
  5724. j2k->m_cp.strict = strict;
  5725. }
  5726. }
  5727. OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads)
  5728. {
  5729. /* Currently we pass the thread-pool to the tcd, so we cannot re-set it */
  5730. /* afterwards */
  5731. if (opj_has_thread_support() && j2k->m_tcd == NULL) {
  5732. opj_thread_pool_destroy(j2k->m_tp);
  5733. j2k->m_tp = NULL;
  5734. if (num_threads <= (OPJ_UINT32)INT_MAX) {
  5735. j2k->m_tp = opj_thread_pool_create((int)num_threads);
  5736. }
  5737. if (j2k->m_tp == NULL) {
  5738. j2k->m_tp = opj_thread_pool_create(0);
  5739. return OPJ_FALSE;
  5740. }
  5741. return OPJ_TRUE;
  5742. }
  5743. return OPJ_FALSE;
  5744. }
  5745. static int opj_j2k_get_default_thread_count()
  5746. {
  5747. const char* num_threads_str = getenv("OPJ_NUM_THREADS");
  5748. int num_cpus;
  5749. int num_threads;
  5750. if (num_threads_str == NULL || !opj_has_thread_support()) {
  5751. return 0;
  5752. }
  5753. num_cpus = opj_get_num_cpus();
  5754. if (strcmp(num_threads_str, "ALL_CPUS") == 0) {
  5755. return num_cpus;
  5756. }
  5757. if (num_cpus == 0) {
  5758. num_cpus = 32;
  5759. }
  5760. num_threads = atoi(num_threads_str);
  5761. if (num_threads < 0) {
  5762. num_threads = 0;
  5763. } else if (num_threads > 2 * num_cpus) {
  5764. num_threads = 2 * num_cpus;
  5765. }
  5766. return num_threads;
  5767. }
  5768. /* ----------------------------------------------------------------------- */
  5769. /* J2K encoder interface */
  5770. /* ----------------------------------------------------------------------- */
  5771. opj_j2k_t* opj_j2k_create_compress(void)
  5772. {
  5773. opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
  5774. if (!l_j2k) {
  5775. return NULL;
  5776. }
  5777. l_j2k->m_is_decoder = 0;
  5778. l_j2k->m_cp.m_is_decoder = 0;
  5779. l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(
  5780. OPJ_J2K_DEFAULT_HEADER_SIZE);
  5781. if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
  5782. opj_j2k_destroy(l_j2k);
  5783. return NULL;
  5784. }
  5785. l_j2k->m_specific_param.m_encoder.m_header_tile_data_size =
  5786. OPJ_J2K_DEFAULT_HEADER_SIZE;
  5787. /* validation list creation*/
  5788. l_j2k->m_validation_list = opj_procedure_list_create();
  5789. if (! l_j2k->m_validation_list) {
  5790. opj_j2k_destroy(l_j2k);
  5791. return NULL;
  5792. }
  5793. /* execution list creation*/
  5794. l_j2k->m_procedure_list = opj_procedure_list_create();
  5795. if (! l_j2k->m_procedure_list) {
  5796. opj_j2k_destroy(l_j2k);
  5797. return NULL;
  5798. }
  5799. l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count());
  5800. if (!l_j2k->m_tp) {
  5801. l_j2k->m_tp = opj_thread_pool_create(0);
  5802. }
  5803. if (!l_j2k->m_tp) {
  5804. opj_j2k_destroy(l_j2k);
  5805. return NULL;
  5806. }
  5807. return l_j2k;
  5808. }
  5809. static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres)
  5810. {
  5811. POC[0].tile = 1;
  5812. POC[0].resno0 = 0;
  5813. POC[0].compno0 = 0;
  5814. POC[0].layno1 = 1;
  5815. POC[0].resno1 = (OPJ_UINT32)(numres - 1);
  5816. POC[0].compno1 = 3;
  5817. POC[0].prg1 = OPJ_CPRL;
  5818. POC[1].tile = 1;
  5819. POC[1].resno0 = (OPJ_UINT32)(numres - 1);
  5820. POC[1].compno0 = 0;
  5821. POC[1].layno1 = 1;
  5822. POC[1].resno1 = (OPJ_UINT32)numres;
  5823. POC[1].compno1 = 3;
  5824. POC[1].prg1 = OPJ_CPRL;
  5825. return 2;
  5826. }
  5827. static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters,
  5828. opj_image_t *image, opj_event_mgr_t *p_manager)
  5829. {
  5830. /* Configure cinema parameters */
  5831. int i;
  5832. /* No tiling */
  5833. parameters->tile_size_on = OPJ_FALSE;
  5834. parameters->cp_tdx = 1;
  5835. parameters->cp_tdy = 1;
  5836. /* One tile part for each component */
  5837. parameters->tp_flag = 'C';
  5838. parameters->tp_on = 1;
  5839. /* Tile and Image shall be at (0,0) */
  5840. parameters->cp_tx0 = 0;
  5841. parameters->cp_ty0 = 0;
  5842. parameters->image_offset_x0 = 0;
  5843. parameters->image_offset_y0 = 0;
  5844. /* Codeblock size= 32*32 */
  5845. parameters->cblockw_init = 32;
  5846. parameters->cblockh_init = 32;
  5847. /* Codeblock style: no mode switch enabled */
  5848. parameters->mode = 0;
  5849. /* No ROI */
  5850. parameters->roi_compno = -1;
  5851. /* No subsampling */
  5852. parameters->subsampling_dx = 1;
  5853. parameters->subsampling_dy = 1;
  5854. /* 9-7 transform */
  5855. parameters->irreversible = 1;
  5856. /* Number of layers */
  5857. if (parameters->tcp_numlayers > 1) {
  5858. opj_event_msg(p_manager, EVT_WARNING,
  5859. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5860. "1 single quality layer"
  5861. "-> Number of layers forced to 1 (rather than %d)\n"
  5862. "-> Rate of the last layer (%3.1f) will be used",
  5863. parameters->tcp_numlayers,
  5864. parameters->tcp_rates[parameters->tcp_numlayers - 1]);
  5865. parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers - 1];
  5866. parameters->tcp_numlayers = 1;
  5867. }
  5868. /* Resolution levels */
  5869. switch (parameters->rsiz) {
  5870. case OPJ_PROFILE_CINEMA_2K:
  5871. if (parameters->numresolution > 6) {
  5872. opj_event_msg(p_manager, EVT_WARNING,
  5873. "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
  5874. "Number of decomposition levels <= 5\n"
  5875. "-> Number of decomposition levels forced to 5 (rather than %d)\n",
  5876. parameters->numresolution + 1);
  5877. parameters->numresolution = 6;
  5878. }
  5879. break;
  5880. case OPJ_PROFILE_CINEMA_4K:
  5881. if (parameters->numresolution < 2) {
  5882. opj_event_msg(p_manager, EVT_WARNING,
  5883. "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
  5884. "Number of decomposition levels >= 1 && <= 6\n"
  5885. "-> Number of decomposition levels forced to 1 (rather than %d)\n",
  5886. parameters->numresolution + 1);
  5887. parameters->numresolution = 1;
  5888. } else if (parameters->numresolution > 7) {
  5889. opj_event_msg(p_manager, EVT_WARNING,
  5890. "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
  5891. "Number of decomposition levels >= 1 && <= 6\n"
  5892. "-> Number of decomposition levels forced to 6 (rather than %d)\n",
  5893. parameters->numresolution + 1);
  5894. parameters->numresolution = 7;
  5895. }
  5896. break;
  5897. default :
  5898. break;
  5899. }
  5900. /* Precincts */
  5901. parameters->csty |= J2K_CP_CSTY_PRT;
  5902. if (parameters->numresolution == 1) {
  5903. parameters->res_spec = 1;
  5904. parameters->prcw_init[0] = 128;
  5905. parameters->prch_init[0] = 128;
  5906. } else {
  5907. parameters->res_spec = parameters->numresolution - 1;
  5908. for (i = 0; i < parameters->res_spec; i++) {
  5909. parameters->prcw_init[i] = 256;
  5910. parameters->prch_init[i] = 256;
  5911. }
  5912. }
  5913. /* The progression order shall be CPRL */
  5914. parameters->prog_order = OPJ_CPRL;
  5915. /* Progression order changes for 4K, disallowed for 2K */
  5916. if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) {
  5917. parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC,
  5918. parameters->numresolution);
  5919. } else {
  5920. parameters->numpocs = 0;
  5921. }
  5922. /* Limited bit-rate */
  5923. parameters->cp_disto_alloc = 1;
  5924. if (parameters->max_cs_size <= 0) {
  5925. /* No rate has been introduced, 24 fps is assumed */
  5926. parameters->max_cs_size = OPJ_CINEMA_24_CS;
  5927. opj_event_msg(p_manager, EVT_WARNING,
  5928. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5929. "Maximum 1302083 compressed bytes @ 24fps\n"
  5930. "As no rate has been given, this limit will be used.\n");
  5931. } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) {
  5932. opj_event_msg(p_manager, EVT_WARNING,
  5933. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5934. "Maximum 1302083 compressed bytes @ 24fps\n"
  5935. "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n");
  5936. parameters->max_cs_size = OPJ_CINEMA_24_CS;
  5937. }
  5938. if (parameters->max_comp_size <= 0) {
  5939. /* No rate has been introduced, 24 fps is assumed */
  5940. parameters->max_comp_size = OPJ_CINEMA_24_COMP;
  5941. opj_event_msg(p_manager, EVT_WARNING,
  5942. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5943. "Maximum 1041666 compressed bytes @ 24fps\n"
  5944. "As no rate has been given, this limit will be used.\n");
  5945. } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) {
  5946. opj_event_msg(p_manager, EVT_WARNING,
  5947. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5948. "Maximum 1041666 compressed bytes @ 24fps\n"
  5949. "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n");
  5950. parameters->max_comp_size = OPJ_CINEMA_24_COMP;
  5951. }
  5952. parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w *
  5953. image->comps[0].h * image->comps[0].prec) /
  5954. (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx *
  5955. image->comps[0].dy);
  5956. }
  5957. static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz,
  5958. opj_event_mgr_t *p_manager)
  5959. {
  5960. OPJ_UINT32 i;
  5961. /* Number of components */
  5962. if (image->numcomps != 3) {
  5963. opj_event_msg(p_manager, EVT_WARNING,
  5964. "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
  5965. "3 components"
  5966. "-> Number of components of input image (%d) is not compliant\n"
  5967. "-> Non-profile-3 codestream will be generated\n",
  5968. image->numcomps);
  5969. return OPJ_FALSE;
  5970. }
  5971. /* Bitdepth */
  5972. for (i = 0; i < image->numcomps; i++) {
  5973. if ((image->comps[i].prec != 12) | (image->comps[i].sgnd)) {
  5974. char signed_str[] = "signed";
  5975. char unsigned_str[] = "unsigned";
  5976. char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str;
  5977. opj_event_msg(p_manager, EVT_WARNING,
  5978. "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
  5979. "Precision of each component shall be 12 bits unsigned"
  5980. "-> At least component %d of input image (%d bits, %s) is not compliant\n"
  5981. "-> Non-profile-3 codestream will be generated\n",
  5982. i, image->comps[i].prec, tmp_str);
  5983. return OPJ_FALSE;
  5984. }
  5985. }
  5986. /* Image size */
  5987. switch (rsiz) {
  5988. case OPJ_PROFILE_CINEMA_2K:
  5989. if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))) {
  5990. opj_event_msg(p_manager, EVT_WARNING,
  5991. "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
  5992. "width <= 2048 and height <= 1080\n"
  5993. "-> Input image size %d x %d is not compliant\n"
  5994. "-> Non-profile-3 codestream will be generated\n",
  5995. image->comps[0].w, image->comps[0].h);
  5996. return OPJ_FALSE;
  5997. }
  5998. break;
  5999. case OPJ_PROFILE_CINEMA_4K:
  6000. if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))) {
  6001. opj_event_msg(p_manager, EVT_WARNING,
  6002. "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
  6003. "width <= 4096 and height <= 2160\n"
  6004. "-> Image size %d x %d is not compliant\n"
  6005. "-> Non-profile-4 codestream will be generated\n",
  6006. image->comps[0].w, image->comps[0].h);
  6007. return OPJ_FALSE;
  6008. }
  6009. break;
  6010. default :
  6011. break;
  6012. }
  6013. return OPJ_TRUE;
  6014. }
  6015. static int opj_j2k_get_imf_max_NL(opj_cparameters_t *parameters,
  6016. opj_image_t *image)
  6017. {
  6018. /* Decomposition levels */
  6019. const OPJ_UINT16 rsiz = parameters->rsiz;
  6020. const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz);
  6021. const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32)
  6022. parameters->cp_tdx : image->x1;
  6023. switch (profile) {
  6024. case OPJ_PROFILE_IMF_2K:
  6025. return 5;
  6026. case OPJ_PROFILE_IMF_4K:
  6027. return 6;
  6028. case OPJ_PROFILE_IMF_8K:
  6029. return 7;
  6030. case OPJ_PROFILE_IMF_2K_R: {
  6031. if (XTsiz >= 2048) {
  6032. return 5;
  6033. } else if (XTsiz >= 1024) {
  6034. return 4;
  6035. }
  6036. break;
  6037. }
  6038. case OPJ_PROFILE_IMF_4K_R: {
  6039. if (XTsiz >= 4096) {
  6040. return 6;
  6041. } else if (XTsiz >= 2048) {
  6042. return 5;
  6043. } else if (XTsiz >= 1024) {
  6044. return 4;
  6045. }
  6046. break;
  6047. }
  6048. case OPJ_PROFILE_IMF_8K_R: {
  6049. if (XTsiz >= 8192) {
  6050. return 7;
  6051. } else if (XTsiz >= 4096) {
  6052. return 6;
  6053. } else if (XTsiz >= 2048) {
  6054. return 5;
  6055. } else if (XTsiz >= 1024) {
  6056. return 4;
  6057. }
  6058. break;
  6059. }
  6060. default:
  6061. break;
  6062. }
  6063. return -1;
  6064. }
  6065. static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters,
  6066. opj_image_t *image, opj_event_mgr_t *p_manager)
  6067. {
  6068. const OPJ_UINT16 rsiz = parameters->rsiz;
  6069. const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz);
  6070. OPJ_UNUSED(p_manager);
  6071. /* Override defaults set by opj_set_default_encoder_parameters */
  6072. if (parameters->cblockw_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKW &&
  6073. parameters->cblockh_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKH) {
  6074. parameters->cblockw_init = 32;
  6075. parameters->cblockh_init = 32;
  6076. }
  6077. /* One tile part for each component */
  6078. parameters->tp_flag = 'C';
  6079. parameters->tp_on = 1;
  6080. if (parameters->prog_order == OPJ_COMP_PARAM_DEFAULT_PROG_ORDER) {
  6081. parameters->prog_order = OPJ_CPRL;
  6082. }
  6083. if (profile == OPJ_PROFILE_IMF_2K ||
  6084. profile == OPJ_PROFILE_IMF_4K ||
  6085. profile == OPJ_PROFILE_IMF_8K) {
  6086. /* 9-7 transform */
  6087. parameters->irreversible = 1;
  6088. }
  6089. /* Adjust the number of resolutions if set to its defaults */
  6090. if (parameters->numresolution == OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION &&
  6091. image->x0 == 0 &&
  6092. image->y0 == 0) {
  6093. const int max_NL = opj_j2k_get_imf_max_NL(parameters, image);
  6094. if (max_NL >= 0 && parameters->numresolution > max_NL) {
  6095. parameters->numresolution = max_NL + 1;
  6096. }
  6097. /* Note: below is generic logic */
  6098. if (!parameters->tile_size_on) {
  6099. while (parameters->numresolution > 0) {
  6100. if (image->x1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) {
  6101. parameters->numresolution --;
  6102. continue;
  6103. }
  6104. if (image->y1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) {
  6105. parameters->numresolution --;
  6106. continue;
  6107. }
  6108. break;
  6109. }
  6110. }
  6111. }
  6112. /* Set defaults precincts */
  6113. if (parameters->csty == 0) {
  6114. parameters->csty |= J2K_CP_CSTY_PRT;
  6115. if (parameters->numresolution == 1) {
  6116. parameters->res_spec = 1;
  6117. parameters->prcw_init[0] = 128;
  6118. parameters->prch_init[0] = 128;
  6119. } else {
  6120. int i;
  6121. parameters->res_spec = parameters->numresolution - 1;
  6122. for (i = 0; i < parameters->res_spec; i++) {
  6123. parameters->prcw_init[i] = 256;
  6124. parameters->prch_init[i] = 256;
  6125. }
  6126. }
  6127. }
  6128. }
  6129. /* Table A.53 from JPEG2000 standard */
  6130. static const OPJ_UINT16 tabMaxSubLevelFromMainLevel[] = {
  6131. 15, /* unspecified */
  6132. 1,
  6133. 1,
  6134. 1,
  6135. 2,
  6136. 3,
  6137. 4,
  6138. 5,
  6139. 6,
  6140. 7,
  6141. 8,
  6142. 9
  6143. };
  6144. static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters,
  6145. opj_image_t *image,
  6146. opj_event_mgr_t *p_manager)
  6147. {
  6148. OPJ_UINT32 i;
  6149. const OPJ_UINT16 rsiz = parameters->rsiz;
  6150. const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz);
  6151. const OPJ_UINT16 mainlevel = OPJ_GET_IMF_MAINLEVEL(rsiz);
  6152. const OPJ_UINT16 sublevel = OPJ_GET_IMF_SUBLEVEL(rsiz);
  6153. const int NL = parameters->numresolution - 1;
  6154. const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32)
  6155. parameters->cp_tdx : image->x1;
  6156. OPJ_BOOL ret = OPJ_TRUE;
  6157. /* Validate mainlevel */
  6158. if (mainlevel > OPJ_IMF_MAINLEVEL_MAX) {
  6159. opj_event_msg(p_manager, EVT_WARNING,
  6160. "IMF profile require mainlevel <= 11.\n"
  6161. "-> %d is thus not compliant\n"
  6162. "-> Non-IMF codestream will be generated\n",
  6163. mainlevel);
  6164. ret = OPJ_FALSE;
  6165. } else {
  6166. /* Validate sublevel */
  6167. assert(sizeof(tabMaxSubLevelFromMainLevel) ==
  6168. (OPJ_IMF_MAINLEVEL_MAX + 1) * sizeof(tabMaxSubLevelFromMainLevel[0]));
  6169. if (sublevel > tabMaxSubLevelFromMainLevel[mainlevel]) {
  6170. opj_event_msg(p_manager, EVT_WARNING,
  6171. "IMF profile require sublevel <= %d for mainlevel = %d.\n"
  6172. "-> %d is thus not compliant\n"
  6173. "-> Non-IMF codestream will be generated\n",
  6174. tabMaxSubLevelFromMainLevel[mainlevel],
  6175. mainlevel,
  6176. sublevel);
  6177. ret = OPJ_FALSE;
  6178. }
  6179. }
  6180. /* Number of components */
  6181. if (image->numcomps > 3) {
  6182. opj_event_msg(p_manager, EVT_WARNING,
  6183. "IMF profiles require at most 3 components.\n"
  6184. "-> Number of components of input image (%d) is not compliant\n"
  6185. "-> Non-IMF codestream will be generated\n",
  6186. image->numcomps);
  6187. ret = OPJ_FALSE;
  6188. }
  6189. if (image->x0 != 0 || image->y0 != 0) {
  6190. opj_event_msg(p_manager, EVT_WARNING,
  6191. "IMF profiles require image origin to be at 0,0.\n"
  6192. "-> %d,%d is not compliant\n"
  6193. "-> Non-IMF codestream will be generated\n",
  6194. image->x0, image->y0 != 0);
  6195. ret = OPJ_FALSE;
  6196. }
  6197. if (parameters->cp_tx0 != 0 || parameters->cp_ty0 != 0) {
  6198. opj_event_msg(p_manager, EVT_WARNING,
  6199. "IMF profiles require tile origin to be at 0,0.\n"
  6200. "-> %d,%d is not compliant\n"
  6201. "-> Non-IMF codestream will be generated\n",
  6202. parameters->cp_tx0, parameters->cp_ty0);
  6203. ret = OPJ_FALSE;
  6204. }
  6205. if (parameters->tile_size_on) {
  6206. if (profile == OPJ_PROFILE_IMF_2K ||
  6207. profile == OPJ_PROFILE_IMF_4K ||
  6208. profile == OPJ_PROFILE_IMF_8K) {
  6209. if ((OPJ_UINT32)parameters->cp_tdx < image->x1 ||
  6210. (OPJ_UINT32)parameters->cp_tdy < image->y1) {
  6211. opj_event_msg(p_manager, EVT_WARNING,
  6212. "IMF 2K/4K/8K single tile profiles require tile to be greater or equal to image size.\n"
  6213. "-> %d,%d is lesser than %d,%d\n"
  6214. "-> Non-IMF codestream will be generated\n",
  6215. parameters->cp_tdx,
  6216. parameters->cp_tdy,
  6217. image->x1,
  6218. image->y1);
  6219. ret = OPJ_FALSE;
  6220. }
  6221. } else {
  6222. if ((OPJ_UINT32)parameters->cp_tdx >= image->x1 &&
  6223. (OPJ_UINT32)parameters->cp_tdy >= image->y1) {
  6224. /* ok */
  6225. } else if (parameters->cp_tdx == 1024 &&
  6226. parameters->cp_tdy == 1024) {
  6227. /* ok */
  6228. } else if (parameters->cp_tdx == 2048 &&
  6229. parameters->cp_tdy == 2048 &&
  6230. (profile == OPJ_PROFILE_IMF_4K ||
  6231. profile == OPJ_PROFILE_IMF_8K)) {
  6232. /* ok */
  6233. } else if (parameters->cp_tdx == 4096 &&
  6234. parameters->cp_tdy == 4096 &&
  6235. profile == OPJ_PROFILE_IMF_8K) {
  6236. /* ok */
  6237. } else {
  6238. opj_event_msg(p_manager, EVT_WARNING,
  6239. "IMF 2K_R/4K_R/8K_R single/multiple tile profiles "
  6240. "require tile to be greater or equal to image size,\n"
  6241. "or to be (1024,1024), or (2048,2048) for 4K_R/8K_R "
  6242. "or (4096,4096) for 8K_R.\n"
  6243. "-> %d,%d is non conformant\n"
  6244. "-> Non-IMF codestream will be generated\n",
  6245. parameters->cp_tdx,
  6246. parameters->cp_tdy);
  6247. ret = OPJ_FALSE;
  6248. }
  6249. }
  6250. }
  6251. /* Bitdepth */
  6252. for (i = 0; i < image->numcomps; i++) {
  6253. if (!(image->comps[i].prec >= 8 && image->comps[i].prec <= 16) ||
  6254. (image->comps[i].sgnd)) {
  6255. char signed_str[] = "signed";
  6256. char unsigned_str[] = "unsigned";
  6257. char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str;
  6258. opj_event_msg(p_manager, EVT_WARNING,
  6259. "IMF profiles require precision of each component to b in [8-16] bits unsigned"
  6260. "-> At least component %d of input image (%d bits, %s) is not compliant\n"
  6261. "-> Non-IMF codestream will be generated\n",
  6262. i, image->comps[i].prec, tmp_str);
  6263. ret = OPJ_FALSE;
  6264. }
  6265. }
  6266. /* Sub-sampling */
  6267. for (i = 0; i < image->numcomps; i++) {
  6268. if (i == 0 && image->comps[i].dx != 1) {
  6269. opj_event_msg(p_manager, EVT_WARNING,
  6270. "IMF profiles require XRSiz1 == 1. Here it is set to %d.\n"
  6271. "-> Non-IMF codestream will be generated\n",
  6272. image->comps[i].dx);
  6273. ret = OPJ_FALSE;
  6274. }
  6275. if (i == 1 && image->comps[i].dx != 1 && image->comps[i].dx != 2) {
  6276. opj_event_msg(p_manager, EVT_WARNING,
  6277. "IMF profiles require XRSiz2 == 1 or 2. Here it is set to %d.\n"
  6278. "-> Non-IMF codestream will be generated\n",
  6279. image->comps[i].dx);
  6280. ret = OPJ_FALSE;
  6281. }
  6282. if (i > 1 && image->comps[i].dx != image->comps[i - 1].dx) {
  6283. opj_event_msg(p_manager, EVT_WARNING,
  6284. "IMF profiles require XRSiz%d to be the same as XRSiz2. "
  6285. "Here it is set to %d instead of %d.\n"
  6286. "-> Non-IMF codestream will be generated\n",
  6287. i + 1, image->comps[i].dx, image->comps[i - 1].dx);
  6288. ret = OPJ_FALSE;
  6289. }
  6290. if (image->comps[i].dy != 1) {
  6291. opj_event_msg(p_manager, EVT_WARNING,
  6292. "IMF profiles require YRsiz == 1. "
  6293. "Here it is set to %d for component %d.\n"
  6294. "-> Non-IMF codestream will be generated\n",
  6295. image->comps[i].dy, i);
  6296. ret = OPJ_FALSE;
  6297. }
  6298. }
  6299. /* Image size */
  6300. switch (profile) {
  6301. case OPJ_PROFILE_IMF_2K:
  6302. case OPJ_PROFILE_IMF_2K_R:
  6303. if (((image->comps[0].w > 2048) | (image->comps[0].h > 1556))) {
  6304. opj_event_msg(p_manager, EVT_WARNING,
  6305. "IMF 2K/2K_R profile require:\n"
  6306. "width <= 2048 and height <= 1556\n"
  6307. "-> Input image size %d x %d is not compliant\n"
  6308. "-> Non-IMF codestream will be generated\n",
  6309. image->comps[0].w, image->comps[0].h);
  6310. ret = OPJ_FALSE;
  6311. }
  6312. break;
  6313. case OPJ_PROFILE_IMF_4K:
  6314. case OPJ_PROFILE_IMF_4K_R:
  6315. if (((image->comps[0].w > 4096) | (image->comps[0].h > 3112))) {
  6316. opj_event_msg(p_manager, EVT_WARNING,
  6317. "IMF 4K/4K_R profile require:\n"
  6318. "width <= 4096 and height <= 3112\n"
  6319. "-> Input image size %d x %d is not compliant\n"
  6320. "-> Non-IMF codestream will be generated\n",
  6321. image->comps[0].w, image->comps[0].h);
  6322. ret = OPJ_FALSE;
  6323. }
  6324. break;
  6325. case OPJ_PROFILE_IMF_8K:
  6326. case OPJ_PROFILE_IMF_8K_R:
  6327. if (((image->comps[0].w > 8192) | (image->comps[0].h > 6224))) {
  6328. opj_event_msg(p_manager, EVT_WARNING,
  6329. "IMF 8K/8K_R profile require:\n"
  6330. "width <= 8192 and height <= 6224\n"
  6331. "-> Input image size %d x %d is not compliant\n"
  6332. "-> Non-IMF codestream will be generated\n",
  6333. image->comps[0].w, image->comps[0].h);
  6334. ret = OPJ_FALSE;
  6335. }
  6336. break;
  6337. default :
  6338. assert(0);
  6339. return OPJ_FALSE;
  6340. }
  6341. if (parameters->roi_compno != -1) {
  6342. opj_event_msg(p_manager, EVT_WARNING,
  6343. "IMF profile forbid RGN / region of interest marker.\n"
  6344. "-> Compression parameters specify a ROI\n"
  6345. "-> Non-IMF codestream will be generated\n");
  6346. ret = OPJ_FALSE;
  6347. }
  6348. if (parameters->cblockw_init != 32 || parameters->cblockh_init != 32) {
  6349. opj_event_msg(p_manager, EVT_WARNING,
  6350. "IMF profile require code block size to be 32x32.\n"
  6351. "-> Compression parameters set it to %dx%d.\n"
  6352. "-> Non-IMF codestream will be generated\n",
  6353. parameters->cblockw_init,
  6354. parameters->cblockh_init);
  6355. ret = OPJ_FALSE;
  6356. }
  6357. if (parameters->prog_order != OPJ_CPRL) {
  6358. opj_event_msg(p_manager, EVT_WARNING,
  6359. "IMF profile require progression order to be CPRL.\n"
  6360. "-> Compression parameters set it to %d.\n"
  6361. "-> Non-IMF codestream will be generated\n",
  6362. parameters->prog_order);
  6363. ret = OPJ_FALSE;
  6364. }
  6365. if (parameters->numpocs != 0) {
  6366. opj_event_msg(p_manager, EVT_WARNING,
  6367. "IMF profile forbid POC markers.\n"
  6368. "-> Compression parameters set %d POC.\n"
  6369. "-> Non-IMF codestream will be generated\n",
  6370. parameters->numpocs);
  6371. ret = OPJ_FALSE;
  6372. }
  6373. /* Codeblock style: no mode switch enabled */
  6374. if (parameters->mode != 0) {
  6375. opj_event_msg(p_manager, EVT_WARNING,
  6376. "IMF profile forbid mode switch in code block style.\n"
  6377. "-> Compression parameters set code block style to %d.\n"
  6378. "-> Non-IMF codestream will be generated\n",
  6379. parameters->mode);
  6380. ret = OPJ_FALSE;
  6381. }
  6382. if (profile == OPJ_PROFILE_IMF_2K ||
  6383. profile == OPJ_PROFILE_IMF_4K ||
  6384. profile == OPJ_PROFILE_IMF_8K) {
  6385. /* Expect 9-7 transform */
  6386. if (parameters->irreversible != 1) {
  6387. opj_event_msg(p_manager, EVT_WARNING,
  6388. "IMF 2K/4K/8K profiles require 9-7 Irreversible Transform.\n"
  6389. "-> Compression parameters set it to reversible.\n"
  6390. "-> Non-IMF codestream will be generated\n");
  6391. ret = OPJ_FALSE;
  6392. }
  6393. } else {
  6394. /* Expect 5-3 transform */
  6395. if (parameters->irreversible != 0) {
  6396. opj_event_msg(p_manager, EVT_WARNING,
  6397. "IMF 2K/4K/8K profiles require 5-3 reversible Transform.\n"
  6398. "-> Compression parameters set it to irreversible.\n"
  6399. "-> Non-IMF codestream will be generated\n");
  6400. ret = OPJ_FALSE;
  6401. }
  6402. }
  6403. /* Number of layers */
  6404. if (parameters->tcp_numlayers != 1) {
  6405. opj_event_msg(p_manager, EVT_WARNING,
  6406. "IMF 2K/4K/8K profiles require 1 single quality layer.\n"
  6407. "-> Number of layers is %d.\n"
  6408. "-> Non-IMF codestream will be generated\n",
  6409. parameters->tcp_numlayers);
  6410. ret = OPJ_FALSE;
  6411. }
  6412. /* Decomposition levels */
  6413. switch (profile) {
  6414. case OPJ_PROFILE_IMF_2K:
  6415. if (!(NL >= 1 && NL <= 5)) {
  6416. opj_event_msg(p_manager, EVT_WARNING,
  6417. "IMF 2K profile requires 1 <= NL <= 5:\n"
  6418. "-> Number of decomposition levels is %d.\n"
  6419. "-> Non-IMF codestream will be generated\n",
  6420. NL);
  6421. ret = OPJ_FALSE;
  6422. }
  6423. break;
  6424. case OPJ_PROFILE_IMF_4K:
  6425. if (!(NL >= 1 && NL <= 6)) {
  6426. opj_event_msg(p_manager, EVT_WARNING,
  6427. "IMF 4K profile requires 1 <= NL <= 6:\n"
  6428. "-> Number of decomposition levels is %d.\n"
  6429. "-> Non-IMF codestream will be generated\n",
  6430. NL);
  6431. ret = OPJ_FALSE;
  6432. }
  6433. break;
  6434. case OPJ_PROFILE_IMF_8K:
  6435. if (!(NL >= 1 && NL <= 7)) {
  6436. opj_event_msg(p_manager, EVT_WARNING,
  6437. "IMF 8K profile requires 1 <= NL <= 7:\n"
  6438. "-> Number of decomposition levels is %d.\n"
  6439. "-> Non-IMF codestream will be generated\n",
  6440. NL);
  6441. ret = OPJ_FALSE;
  6442. }
  6443. break;
  6444. case OPJ_PROFILE_IMF_2K_R: {
  6445. if (XTsiz >= 2048) {
  6446. if (!(NL >= 1 && NL <= 5)) {
  6447. opj_event_msg(p_manager, EVT_WARNING,
  6448. "IMF 2K_R profile requires 1 <= NL <= 5 for XTsiz >= 2048:\n"
  6449. "-> Number of decomposition levels is %d.\n"
  6450. "-> Non-IMF codestream will be generated\n",
  6451. NL);
  6452. ret = OPJ_FALSE;
  6453. }
  6454. } else if (XTsiz >= 1024) {
  6455. if (!(NL >= 1 && NL <= 4)) {
  6456. opj_event_msg(p_manager, EVT_WARNING,
  6457. "IMF 2K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n"
  6458. "-> Number of decomposition levels is %d.\n"
  6459. "-> Non-IMF codestream will be generated\n",
  6460. NL);
  6461. ret = OPJ_FALSE;
  6462. }
  6463. }
  6464. break;
  6465. }
  6466. case OPJ_PROFILE_IMF_4K_R: {
  6467. if (XTsiz >= 4096) {
  6468. if (!(NL >= 1 && NL <= 6)) {
  6469. opj_event_msg(p_manager, EVT_WARNING,
  6470. "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz >= 4096:\n"
  6471. "-> Number of decomposition levels is %d.\n"
  6472. "-> Non-IMF codestream will be generated\n",
  6473. NL);
  6474. ret = OPJ_FALSE;
  6475. }
  6476. } else if (XTsiz >= 2048) {
  6477. if (!(NL >= 1 && NL <= 5)) {
  6478. opj_event_msg(p_manager, EVT_WARNING,
  6479. "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n"
  6480. "-> Number of decomposition levels is %d.\n"
  6481. "-> Non-IMF codestream will be generated\n",
  6482. NL);
  6483. ret = OPJ_FALSE;
  6484. }
  6485. } else if (XTsiz >= 1024) {
  6486. if (!(NL >= 1 && NL <= 4)) {
  6487. opj_event_msg(p_manager, EVT_WARNING,
  6488. "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n"
  6489. "-> Number of decomposition levels is %d.\n"
  6490. "-> Non-IMF codestream will be generated\n",
  6491. NL);
  6492. ret = OPJ_FALSE;
  6493. }
  6494. }
  6495. break;
  6496. }
  6497. case OPJ_PROFILE_IMF_8K_R: {
  6498. if (XTsiz >= 8192) {
  6499. if (!(NL >= 1 && NL <= 7)) {
  6500. opj_event_msg(p_manager, EVT_WARNING,
  6501. "IMF 4K_R profile requires 1 <= NL <= 7 for XTsiz >= 8192:\n"
  6502. "-> Number of decomposition levels is %d.\n"
  6503. "-> Non-IMF codestream will be generated\n",
  6504. NL);
  6505. ret = OPJ_FALSE;
  6506. }
  6507. } else if (XTsiz >= 4096) {
  6508. if (!(NL >= 1 && NL <= 6)) {
  6509. opj_event_msg(p_manager, EVT_WARNING,
  6510. "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz in [4096,8192[:\n"
  6511. "-> Number of decomposition levels is %d.\n"
  6512. "-> Non-IMF codestream will be generated\n",
  6513. NL);
  6514. ret = OPJ_FALSE;
  6515. }
  6516. } else if (XTsiz >= 2048) {
  6517. if (!(NL >= 1 && NL <= 5)) {
  6518. opj_event_msg(p_manager, EVT_WARNING,
  6519. "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n"
  6520. "-> Number of decomposition levels is %d.\n"
  6521. "-> Non-IMF codestream will be generated\n",
  6522. NL);
  6523. ret = OPJ_FALSE;
  6524. }
  6525. } else if (XTsiz >= 1024) {
  6526. if (!(NL >= 1 && NL <= 4)) {
  6527. opj_event_msg(p_manager, EVT_WARNING,
  6528. "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n"
  6529. "-> Number of decomposition levels is %d.\n"
  6530. "-> Non-IMF codestream will be generated\n",
  6531. NL);
  6532. ret = OPJ_FALSE;
  6533. }
  6534. }
  6535. break;
  6536. }
  6537. default:
  6538. break;
  6539. }
  6540. if (parameters->numresolution == 1) {
  6541. if (parameters->res_spec != 1 ||
  6542. parameters->prcw_init[0] != 128 ||
  6543. parameters->prch_init[0] != 128) {
  6544. opj_event_msg(p_manager, EVT_WARNING,
  6545. "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n"
  6546. "-> Supplied values are different from that.\n"
  6547. "-> Non-IMF codestream will be generated\n");
  6548. ret = OPJ_FALSE;
  6549. }
  6550. } else {
  6551. int i;
  6552. for (i = 0; i < parameters->res_spec; i++) {
  6553. if (parameters->prcw_init[i] != 256 ||
  6554. parameters->prch_init[i] != 256) {
  6555. opj_event_msg(p_manager, EVT_WARNING,
  6556. "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n"
  6557. "-> Supplied values are different from that.\n"
  6558. "-> Non-IMF codestream will be generated\n");
  6559. ret = OPJ_FALSE;
  6560. }
  6561. }
  6562. }
  6563. return ret;
  6564. }
  6565. OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
  6566. opj_cparameters_t *parameters,
  6567. opj_image_t *image,
  6568. opj_event_mgr_t * p_manager)
  6569. {
  6570. OPJ_UINT32 i, j, tileno, numpocs_tile;
  6571. opj_cp_t *cp = 00;
  6572. OPJ_UINT32 cblkw, cblkh;
  6573. if (!p_j2k || !parameters || ! image) {
  6574. return OPJ_FALSE;
  6575. }
  6576. if ((parameters->numresolution <= 0) ||
  6577. (parameters->numresolution > OPJ_J2K_MAXRLVLS)) {
  6578. opj_event_msg(p_manager, EVT_ERROR,
  6579. "Invalid number of resolutions : %d not in range [1,%d]\n",
  6580. parameters->numresolution, OPJ_J2K_MAXRLVLS);
  6581. return OPJ_FALSE;
  6582. }
  6583. if (parameters->cblockw_init < 4 || parameters->cblockw_init > 1024) {
  6584. opj_event_msg(p_manager, EVT_ERROR,
  6585. "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n",
  6586. parameters->cblockw_init);
  6587. return OPJ_FALSE;
  6588. }
  6589. if (parameters->cblockh_init < 4 || parameters->cblockh_init > 1024) {
  6590. opj_event_msg(p_manager, EVT_ERROR,
  6591. "Invalid value for cblockh_init: %d not a power of 2 not in range [4,1024]\n",
  6592. parameters->cblockh_init);
  6593. return OPJ_FALSE;
  6594. }
  6595. if (parameters->cblockw_init * parameters->cblockh_init > 4096) {
  6596. opj_event_msg(p_manager, EVT_ERROR,
  6597. "Invalid value for cblockw_init * cblockh_init: should be <= 4096\n");
  6598. return OPJ_FALSE;
  6599. }
  6600. cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
  6601. cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
  6602. if (parameters->cblockw_init != (1 << cblkw)) {
  6603. opj_event_msg(p_manager, EVT_ERROR,
  6604. "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n",
  6605. parameters->cblockw_init);
  6606. return OPJ_FALSE;
  6607. }
  6608. if (parameters->cblockh_init != (1 << cblkh)) {
  6609. opj_event_msg(p_manager, EVT_ERROR,
  6610. "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n",
  6611. parameters->cblockh_init);
  6612. return OPJ_FALSE;
  6613. }
  6614. p_j2k->m_specific_param.m_encoder.m_nb_comps = image->numcomps;
  6615. /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
  6616. cp = &(p_j2k->m_cp);
  6617. /* set default values for cp */
  6618. cp->tw = 1;
  6619. cp->th = 1;
  6620. /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */
  6621. if (parameters->rsiz ==
  6622. OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */
  6623. OPJ_BOOL deprecated_used = OPJ_FALSE;
  6624. switch (parameters->cp_cinema) {
  6625. case OPJ_CINEMA2K_24:
  6626. parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
  6627. parameters->max_cs_size = OPJ_CINEMA_24_CS;
  6628. parameters->max_comp_size = OPJ_CINEMA_24_COMP;
  6629. deprecated_used = OPJ_TRUE;
  6630. break;
  6631. case OPJ_CINEMA2K_48:
  6632. parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
  6633. parameters->max_cs_size = OPJ_CINEMA_48_CS;
  6634. parameters->max_comp_size = OPJ_CINEMA_48_COMP;
  6635. deprecated_used = OPJ_TRUE;
  6636. break;
  6637. case OPJ_CINEMA4K_24:
  6638. parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
  6639. parameters->max_cs_size = OPJ_CINEMA_24_CS;
  6640. parameters->max_comp_size = OPJ_CINEMA_24_COMP;
  6641. deprecated_used = OPJ_TRUE;
  6642. break;
  6643. case OPJ_OFF:
  6644. default:
  6645. break;
  6646. }
  6647. switch (parameters->cp_rsiz) {
  6648. case OPJ_CINEMA2K:
  6649. parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
  6650. deprecated_used = OPJ_TRUE;
  6651. break;
  6652. case OPJ_CINEMA4K:
  6653. parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
  6654. deprecated_used = OPJ_TRUE;
  6655. break;
  6656. case OPJ_MCT:
  6657. parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT;
  6658. deprecated_used = OPJ_TRUE;
  6659. case OPJ_STD_RSIZ:
  6660. default:
  6661. break;
  6662. }
  6663. if (deprecated_used) {
  6664. opj_event_msg(p_manager, EVT_WARNING,
  6665. "Deprecated fields cp_cinema or cp_rsiz are used\n"
  6666. "Please consider using only the rsiz field\n"
  6667. "See openjpeg.h documentation for more details\n");
  6668. }
  6669. }
  6670. /* If no explicit layers are provided, use lossless settings */
  6671. if (parameters->tcp_numlayers == 0) {
  6672. parameters->tcp_numlayers = 1;
  6673. parameters->cp_disto_alloc = 1;
  6674. parameters->tcp_rates[0] = 0;
  6675. }
  6676. if (parameters->cp_disto_alloc) {
  6677. /* Emit warnings if tcp_rates are not decreasing */
  6678. for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
  6679. OPJ_FLOAT32 rate_i_corr = parameters->tcp_rates[i];
  6680. OPJ_FLOAT32 rate_i_m_1_corr = parameters->tcp_rates[i - 1];
  6681. if (rate_i_corr <= 1.0) {
  6682. rate_i_corr = 1.0;
  6683. }
  6684. if (rate_i_m_1_corr <= 1.0) {
  6685. rate_i_m_1_corr = 1.0;
  6686. }
  6687. if (rate_i_corr >= rate_i_m_1_corr) {
  6688. if (rate_i_corr != parameters->tcp_rates[i] &&
  6689. rate_i_m_1_corr != parameters->tcp_rates[i - 1]) {
  6690. opj_event_msg(p_manager, EVT_WARNING,
  6691. "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser "
  6692. "than tcp_rates[%d]=%f (corrected as %f)\n",
  6693. i, parameters->tcp_rates[i], rate_i_corr,
  6694. i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr);
  6695. } else if (rate_i_corr != parameters->tcp_rates[i]) {
  6696. opj_event_msg(p_manager, EVT_WARNING,
  6697. "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser "
  6698. "than tcp_rates[%d]=%f\n",
  6699. i, parameters->tcp_rates[i], rate_i_corr,
  6700. i - 1, parameters->tcp_rates[i - 1]);
  6701. } else if (rate_i_m_1_corr != parameters->tcp_rates[i - 1]) {
  6702. opj_event_msg(p_manager, EVT_WARNING,
  6703. "tcp_rates[%d]=%f should be strictly lesser "
  6704. "than tcp_rates[%d]=%f (corrected as %f)\n",
  6705. i, parameters->tcp_rates[i],
  6706. i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr);
  6707. } else {
  6708. opj_event_msg(p_manager, EVT_WARNING,
  6709. "tcp_rates[%d]=%f should be strictly lesser "
  6710. "than tcp_rates[%d]=%f\n",
  6711. i, parameters->tcp_rates[i],
  6712. i - 1, parameters->tcp_rates[i - 1]);
  6713. }
  6714. }
  6715. }
  6716. } else if (parameters->cp_fixed_quality) {
  6717. /* Emit warnings if tcp_distoratio are not increasing */
  6718. for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
  6719. if (parameters->tcp_distoratio[i] < parameters->tcp_distoratio[i - 1] &&
  6720. !(i == (OPJ_UINT32)parameters->tcp_numlayers - 1 &&
  6721. parameters->tcp_distoratio[i] == 0)) {
  6722. opj_event_msg(p_manager, EVT_WARNING,
  6723. "tcp_distoratio[%d]=%f should be strictly greater "
  6724. "than tcp_distoratio[%d]=%f\n",
  6725. i, parameters->tcp_distoratio[i], i - 1,
  6726. parameters->tcp_distoratio[i - 1]);
  6727. }
  6728. }
  6729. }
  6730. /* see if max_codestream_size does limit input rate */
  6731. if (parameters->max_cs_size <= 0) {
  6732. if (parameters->tcp_rates[parameters->tcp_numlayers - 1] > 0) {
  6733. OPJ_FLOAT32 temp_size;
  6734. temp_size = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w *
  6735. image->comps[0].h * image->comps[0].prec) /
  6736. ((double)parameters->tcp_rates[parameters->tcp_numlayers - 1] * 8 *
  6737. image->comps[0].dx * image->comps[0].dy));
  6738. if (temp_size > INT_MAX) {
  6739. parameters->max_cs_size = INT_MAX;
  6740. } else {
  6741. parameters->max_cs_size = (int) floor(temp_size);
  6742. }
  6743. } else {
  6744. parameters->max_cs_size = 0;
  6745. }
  6746. } else {
  6747. OPJ_FLOAT32 temp_rate;
  6748. OPJ_BOOL cap = OPJ_FALSE;
  6749. if (OPJ_IS_IMF(parameters->rsiz) && parameters->max_cs_size > 0 &&
  6750. parameters->tcp_numlayers == 1 && parameters->tcp_rates[0] == 0) {
  6751. parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w *
  6752. image->comps[0].h * image->comps[0].prec) /
  6753. (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx *
  6754. image->comps[0].dy);
  6755. }
  6756. temp_rate = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w *
  6757. image->comps[0].h * image->comps[0].prec) /
  6758. (((double)parameters->max_cs_size) * 8 * image->comps[0].dx *
  6759. image->comps[0].dy));
  6760. for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
  6761. if (parameters->tcp_rates[i] < temp_rate) {
  6762. parameters->tcp_rates[i] = temp_rate;
  6763. cap = OPJ_TRUE;
  6764. }
  6765. }
  6766. if (cap) {
  6767. opj_event_msg(p_manager, EVT_WARNING,
  6768. "The desired maximum codestream size has limited\n"
  6769. "at least one of the desired quality layers\n");
  6770. }
  6771. }
  6772. if (OPJ_IS_CINEMA(parameters->rsiz) || OPJ_IS_IMF(parameters->rsiz)) {
  6773. p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE;
  6774. }
  6775. /* Manage profiles and applications and set RSIZ */
  6776. /* set cinema parameters if required */
  6777. if (OPJ_IS_CINEMA(parameters->rsiz)) {
  6778. if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K)
  6779. || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)) {
  6780. opj_event_msg(p_manager, EVT_WARNING,
  6781. "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n");
  6782. parameters->rsiz = OPJ_PROFILE_NONE;
  6783. } else {
  6784. opj_j2k_set_cinema_parameters(parameters, image, p_manager);
  6785. if (!opj_j2k_is_cinema_compliant(image, parameters->rsiz, p_manager)) {
  6786. parameters->rsiz = OPJ_PROFILE_NONE;
  6787. }
  6788. }
  6789. } else if (OPJ_IS_STORAGE(parameters->rsiz)) {
  6790. opj_event_msg(p_manager, EVT_WARNING,
  6791. "JPEG 2000 Long Term Storage profile not yet supported\n");
  6792. parameters->rsiz = OPJ_PROFILE_NONE;
  6793. } else if (OPJ_IS_BROADCAST(parameters->rsiz)) {
  6794. opj_event_msg(p_manager, EVT_WARNING,
  6795. "JPEG 2000 Broadcast profiles not yet supported\n");
  6796. parameters->rsiz = OPJ_PROFILE_NONE;
  6797. } else if (OPJ_IS_IMF(parameters->rsiz)) {
  6798. opj_j2k_set_imf_parameters(parameters, image, p_manager);
  6799. if (!opj_j2k_is_imf_compliant(parameters, image, p_manager)) {
  6800. parameters->rsiz = OPJ_PROFILE_NONE;
  6801. }
  6802. } else if (OPJ_IS_PART2(parameters->rsiz)) {
  6803. if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) {
  6804. opj_event_msg(p_manager, EVT_WARNING,
  6805. "JPEG 2000 Part-2 profile defined\n"
  6806. "but no Part-2 extension enabled.\n"
  6807. "Profile set to NONE.\n");
  6808. parameters->rsiz = OPJ_PROFILE_NONE;
  6809. } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) {
  6810. opj_event_msg(p_manager, EVT_WARNING,
  6811. "Unsupported Part-2 extension enabled\n"
  6812. "Profile set to NONE.\n");
  6813. parameters->rsiz = OPJ_PROFILE_NONE;
  6814. }
  6815. }
  6816. /*
  6817. copy user encoding parameters
  6818. */
  6819. cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)
  6820. parameters->max_comp_size;
  6821. cp->rsiz = parameters->rsiz;
  6822. cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32)
  6823. parameters->cp_disto_alloc & 1u;
  6824. cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32)
  6825. parameters->cp_fixed_alloc & 1u;
  6826. cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32)
  6827. parameters->cp_fixed_quality & 1u;
  6828. /* mod fixed_quality */
  6829. if (parameters->cp_fixed_alloc && parameters->cp_matrice) {
  6830. size_t array_size = (size_t)parameters->tcp_numlayers *
  6831. (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32);
  6832. cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
  6833. if (!cp->m_specific_param.m_enc.m_matrice) {
  6834. opj_event_msg(p_manager, EVT_ERROR,
  6835. "Not enough memory to allocate copy of user encoding parameters matrix \n");
  6836. return OPJ_FALSE;
  6837. }
  6838. memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice,
  6839. array_size);
  6840. }
  6841. /* tiles */
  6842. cp->tdx = (OPJ_UINT32)parameters->cp_tdx;
  6843. cp->tdy = (OPJ_UINT32)parameters->cp_tdy;
  6844. /* tile offset */
  6845. cp->tx0 = (OPJ_UINT32)parameters->cp_tx0;
  6846. cp->ty0 = (OPJ_UINT32)parameters->cp_ty0;
  6847. /* comment string */
  6848. if (parameters->cp_comment) {
  6849. cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U);
  6850. if (!cp->comment) {
  6851. opj_event_msg(p_manager, EVT_ERROR,
  6852. "Not enough memory to allocate copy of comment string\n");
  6853. return OPJ_FALSE;
  6854. }
  6855. strcpy(cp->comment, parameters->cp_comment);
  6856. } else {
  6857. /* Create default comment for codestream */
  6858. const char comment[] = "Created by OpenJPEG version ";
  6859. const size_t clen = strlen(comment);
  6860. const char *version = opj_version();
  6861. /* UniPG>> */
  6862. #ifdef USE_JPWL
  6863. cp->comment = (char*)opj_malloc(clen + strlen(version) + 11);
  6864. if (!cp->comment) {
  6865. opj_event_msg(p_manager, EVT_ERROR,
  6866. "Not enough memory to allocate comment string\n");
  6867. return OPJ_FALSE;
  6868. }
  6869. sprintf(cp->comment, "%s%s with JPWL", comment, version);
  6870. #else
  6871. cp->comment = (char*)opj_malloc(clen + strlen(version) + 1);
  6872. if (!cp->comment) {
  6873. opj_event_msg(p_manager, EVT_ERROR,
  6874. "Not enough memory to allocate comment string\n");
  6875. return OPJ_FALSE;
  6876. }
  6877. sprintf(cp->comment, "%s%s", comment, version);
  6878. #endif
  6879. /* <<UniPG */
  6880. }
  6881. /*
  6882. calculate other encoding parameters
  6883. */
  6884. if (parameters->tile_size_on) {
  6885. if (cp->tdx == 0) {
  6886. opj_event_msg(p_manager, EVT_ERROR, "Invalid tile width\n");
  6887. return OPJ_FALSE;
  6888. }
  6889. if (cp->tdy == 0) {
  6890. opj_event_msg(p_manager, EVT_ERROR, "Invalid tile height\n");
  6891. return OPJ_FALSE;
  6892. }
  6893. cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->x1 - cp->tx0),
  6894. (OPJ_INT32)cp->tdx);
  6895. cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->y1 - cp->ty0),
  6896. (OPJ_INT32)cp->tdy);
  6897. /* Check that the number of tiles is valid */
  6898. if (cp->tw > 65535 / cp->th) {
  6899. opj_event_msg(p_manager, EVT_ERROR,
  6900. "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
  6901. cp->tw, cp->th);
  6902. return OPJ_FALSE;
  6903. }
  6904. } else {
  6905. cp->tdx = image->x1 - cp->tx0;
  6906. cp->tdy = image->y1 - cp->ty0;
  6907. }
  6908. if (parameters->tp_on) {
  6909. cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag;
  6910. cp->m_specific_param.m_enc.m_tp_on = 1;
  6911. }
  6912. #ifdef USE_JPWL
  6913. /*
  6914. calculate JPWL encoding parameters
  6915. */
  6916. if (parameters->jpwl_epc_on) {
  6917. OPJ_INT32 i;
  6918. /* set JPWL on */
  6919. cp->epc_on = OPJ_TRUE;
  6920. cp->info_on = OPJ_FALSE; /* no informative technique */
  6921. /* set EPB on */
  6922. if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
  6923. cp->epb_on = OPJ_TRUE;
  6924. cp->hprot_MH = parameters->jpwl_hprot_MH;
  6925. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  6926. cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
  6927. cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
  6928. }
  6929. /* if tile specs are not specified, copy MH specs */
  6930. if (cp->hprot_TPH[0] == -1) {
  6931. cp->hprot_TPH_tileno[0] = 0;
  6932. cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
  6933. }
  6934. for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
  6935. cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
  6936. cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
  6937. cp->pprot[i] = parameters->jpwl_pprot[i];
  6938. }
  6939. }
  6940. /* set ESD writing */
  6941. if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
  6942. cp->esd_on = OPJ_TRUE;
  6943. cp->sens_size = parameters->jpwl_sens_size;
  6944. cp->sens_addr = parameters->jpwl_sens_addr;
  6945. cp->sens_range = parameters->jpwl_sens_range;
  6946. cp->sens_MH = parameters->jpwl_sens_MH;
  6947. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  6948. cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
  6949. cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
  6950. }
  6951. }
  6952. /* always set RED writing to false: we are at the encoder */
  6953. cp->red_on = OPJ_FALSE;
  6954. } else {
  6955. cp->epc_on = OPJ_FALSE;
  6956. }
  6957. #endif /* USE_JPWL */
  6958. /* initialize the multiple tiles */
  6959. /* ---------------------------- */
  6960. cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
  6961. if (!cp->tcps) {
  6962. opj_event_msg(p_manager, EVT_ERROR,
  6963. "Not enough memory to allocate tile coding parameters\n");
  6964. return OPJ_FALSE;
  6965. }
  6966. for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  6967. opj_tcp_t *tcp = &cp->tcps[tileno];
  6968. tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;
  6969. for (j = 0; j < tcp->numlayers; j++) {
  6970. if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) {
  6971. if (cp->m_specific_param.m_enc.m_fixed_quality) {
  6972. tcp->distoratio[j] = parameters->tcp_distoratio[j];
  6973. }
  6974. tcp->rates[j] = parameters->tcp_rates[j];
  6975. } else {
  6976. if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
  6977. tcp->distoratio[j] = parameters->tcp_distoratio[j];
  6978. } else {
  6979. tcp->rates[j] = parameters->tcp_rates[j];
  6980. }
  6981. }
  6982. if (!cp->m_specific_param.m_enc.m_fixed_quality &&
  6983. tcp->rates[j] <= 1.0) {
  6984. tcp->rates[j] = 0.0; /* force lossless */
  6985. }
  6986. }
  6987. tcp->csty = (OPJ_UINT32)parameters->csty;
  6988. tcp->prg = parameters->prog_order;
  6989. tcp->mct = (OPJ_UINT32)parameters->tcp_mct;
  6990. numpocs_tile = 0;
  6991. tcp->POC = 0;
  6992. if (parameters->numpocs) {
  6993. /* initialisation of POC */
  6994. for (i = 0; i < parameters->numpocs; i++) {
  6995. if (tileno + 1 == parameters->POC[i].tile) {
  6996. opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
  6997. if (parameters->POC[numpocs_tile].compno0 >= image->numcomps) {
  6998. opj_event_msg(p_manager, EVT_ERROR,
  6999. "Invalid compno0 for POC %d\n", i);
  7000. return OPJ_FALSE;
  7001. }
  7002. tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
  7003. tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
  7004. tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
  7005. tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
  7006. tcp_poc->compno1 = opj_uint_min(parameters->POC[numpocs_tile].compno1,
  7007. image->numcomps);
  7008. tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
  7009. tcp_poc->tile = parameters->POC[numpocs_tile].tile;
  7010. numpocs_tile++;
  7011. }
  7012. }
  7013. if (numpocs_tile) {
  7014. /* TODO MSD use the return value*/
  7015. opj_j2k_check_poc_val(parameters->POC, tileno, parameters->numpocs,
  7016. (OPJ_UINT32)parameters->numresolution, image->numcomps,
  7017. (OPJ_UINT32)parameters->tcp_numlayers, p_manager);
  7018. tcp->POC = 1;
  7019. tcp->numpocs = numpocs_tile - 1 ;
  7020. }
  7021. } else {
  7022. tcp->numpocs = 0;
  7023. }
  7024. tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
  7025. if (!tcp->tccps) {
  7026. opj_event_msg(p_manager, EVT_ERROR,
  7027. "Not enough memory to allocate tile component coding parameters\n");
  7028. return OPJ_FALSE;
  7029. }
  7030. if (parameters->mct_data) {
  7031. OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(
  7032. OPJ_FLOAT32);
  7033. OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
  7034. OPJ_INT32 * l_dc_shift = (OPJ_INT32 *)((OPJ_BYTE *) parameters->mct_data +
  7035. lMctSize);
  7036. if (!lTmpBuf) {
  7037. opj_event_msg(p_manager, EVT_ERROR,
  7038. "Not enough memory to allocate temp buffer\n");
  7039. return OPJ_FALSE;
  7040. }
  7041. tcp->mct = 2;
  7042. tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
  7043. if (! tcp->m_mct_coding_matrix) {
  7044. opj_free(lTmpBuf);
  7045. lTmpBuf = NULL;
  7046. opj_event_msg(p_manager, EVT_ERROR,
  7047. "Not enough memory to allocate encoder MCT coding matrix \n");
  7048. return OPJ_FALSE;
  7049. }
  7050. memcpy(tcp->m_mct_coding_matrix, parameters->mct_data, lMctSize);
  7051. memcpy(lTmpBuf, parameters->mct_data, lMctSize);
  7052. tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
  7053. if (! tcp->m_mct_decoding_matrix) {
  7054. opj_free(lTmpBuf);
  7055. lTmpBuf = NULL;
  7056. opj_event_msg(p_manager, EVT_ERROR,
  7057. "Not enough memory to allocate encoder MCT decoding matrix \n");
  7058. return OPJ_FALSE;
  7059. }
  7060. if (opj_matrix_inversion_f(lTmpBuf, (tcp->m_mct_decoding_matrix),
  7061. image->numcomps) == OPJ_FALSE) {
  7062. opj_free(lTmpBuf);
  7063. lTmpBuf = NULL;
  7064. opj_event_msg(p_manager, EVT_ERROR,
  7065. "Failed to inverse encoder MCT decoding matrix \n");
  7066. return OPJ_FALSE;
  7067. }
  7068. tcp->mct_norms = (OPJ_FLOAT64*)
  7069. opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
  7070. if (! tcp->mct_norms) {
  7071. opj_free(lTmpBuf);
  7072. lTmpBuf = NULL;
  7073. opj_event_msg(p_manager, EVT_ERROR,
  7074. "Not enough memory to allocate encoder MCT norms \n");
  7075. return OPJ_FALSE;
  7076. }
  7077. opj_calculate_norms(tcp->mct_norms, image->numcomps,
  7078. tcp->m_mct_decoding_matrix);
  7079. opj_free(lTmpBuf);
  7080. for (i = 0; i < image->numcomps; i++) {
  7081. opj_tccp_t *tccp = &tcp->tccps[i];
  7082. tccp->m_dc_level_shift = l_dc_shift[i];
  7083. }
  7084. if (opj_j2k_setup_mct_encoding(tcp, image) == OPJ_FALSE) {
  7085. /* free will be handled by opj_j2k_destroy */
  7086. opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n");
  7087. return OPJ_FALSE;
  7088. }
  7089. } else {
  7090. if (tcp->mct == 1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */
  7091. if ((image->comps[0].dx != image->comps[1].dx) ||
  7092. (image->comps[0].dx != image->comps[2].dx) ||
  7093. (image->comps[0].dy != image->comps[1].dy) ||
  7094. (image->comps[0].dy != image->comps[2].dy)) {
  7095. opj_event_msg(p_manager, EVT_WARNING,
  7096. "Cannot perform MCT on components with different sizes. Disabling MCT.\n");
  7097. tcp->mct = 0;
  7098. }
  7099. }
  7100. for (i = 0; i < image->numcomps; i++) {
  7101. opj_tccp_t *tccp = &tcp->tccps[i];
  7102. opj_image_comp_t * l_comp = &(image->comps[i]);
  7103. if (! l_comp->sgnd) {
  7104. tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
  7105. }
  7106. }
  7107. }
  7108. for (i = 0; i < image->numcomps; i++) {
  7109. opj_tccp_t *tccp = &tcp->tccps[i];
  7110. tccp->csty = parameters->csty &
  7111. 0x01; /* 0 => one precinct || 1 => custom precinct */
  7112. tccp->numresolutions = (OPJ_UINT32)parameters->numresolution;
  7113. tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
  7114. tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
  7115. tccp->cblksty = (OPJ_UINT32)parameters->mode;
  7116. tccp->qmfbid = parameters->irreversible ? 0 : 1;
  7117. tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT :
  7118. J2K_CCP_QNTSTY_NOQNT;
  7119. tccp->numgbits = 2;
  7120. if ((OPJ_INT32)i == parameters->roi_compno) {
  7121. tccp->roishift = parameters->roi_shift;
  7122. } else {
  7123. tccp->roishift = 0;
  7124. }
  7125. if (parameters->csty & J2K_CCP_CSTY_PRT) {
  7126. OPJ_INT32 p = 0, it_res;
  7127. assert(tccp->numresolutions > 0);
  7128. for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) {
  7129. if (p < parameters->res_spec) {
  7130. if (parameters->prcw_init[p] < 1) {
  7131. tccp->prcw[it_res] = 1;
  7132. } else {
  7133. tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]);
  7134. }
  7135. if (parameters->prch_init[p] < 1) {
  7136. tccp->prch[it_res] = 1;
  7137. } else {
  7138. tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]);
  7139. }
  7140. } else {
  7141. OPJ_INT32 res_spec = parameters->res_spec;
  7142. OPJ_INT32 size_prcw = 0;
  7143. OPJ_INT32 size_prch = 0;
  7144. assert(res_spec > 0); /* issue 189 */
  7145. size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
  7146. size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
  7147. if (size_prcw < 1) {
  7148. tccp->prcw[it_res] = 1;
  7149. } else {
  7150. tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw);
  7151. }
  7152. if (size_prch < 1) {
  7153. tccp->prch[it_res] = 1;
  7154. } else {
  7155. tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch);
  7156. }
  7157. }
  7158. p++;
  7159. /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
  7160. } /*end for*/
  7161. } else {
  7162. for (j = 0; j < tccp->numresolutions; j++) {
  7163. tccp->prcw[j] = 15;
  7164. tccp->prch[j] = 15;
  7165. }
  7166. }
  7167. opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
  7168. }
  7169. }
  7170. if (parameters->mct_data) {
  7171. opj_free(parameters->mct_data);
  7172. parameters->mct_data = 00;
  7173. }
  7174. return OPJ_TRUE;
  7175. }
  7176. static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index,
  7177. OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
  7178. {
  7179. assert(cstr_index != 00);
  7180. /* expand the list? */
  7181. if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
  7182. opj_marker_info_t *new_marker;
  7183. cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32)
  7184. cstr_index->maxmarknum);
  7185. new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker,
  7186. cstr_index->maxmarknum * sizeof(opj_marker_info_t));
  7187. if (! new_marker) {
  7188. opj_free(cstr_index->marker);
  7189. cstr_index->marker = NULL;
  7190. cstr_index->maxmarknum = 0;
  7191. cstr_index->marknum = 0;
  7192. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */
  7193. return OPJ_FALSE;
  7194. }
  7195. cstr_index->marker = new_marker;
  7196. }
  7197. /* add the marker */
  7198. cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
  7199. cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
  7200. cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
  7201. cstr_index->marknum++;
  7202. return OPJ_TRUE;
  7203. }
  7204. static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno,
  7205. opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos,
  7206. OPJ_UINT32 len)
  7207. {
  7208. assert(cstr_index != 00);
  7209. assert(cstr_index->tile_index != 00);
  7210. /* expand the list? */
  7211. if ((cstr_index->tile_index[tileno].marknum + 1) >
  7212. cstr_index->tile_index[tileno].maxmarknum) {
  7213. opj_marker_info_t *new_marker;
  7214. cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 +
  7215. (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum);
  7216. new_marker = (opj_marker_info_t *) opj_realloc(
  7217. cstr_index->tile_index[tileno].marker,
  7218. cstr_index->tile_index[tileno].maxmarknum * sizeof(opj_marker_info_t));
  7219. if (! new_marker) {
  7220. opj_free(cstr_index->tile_index[tileno].marker);
  7221. cstr_index->tile_index[tileno].marker = NULL;
  7222. cstr_index->tile_index[tileno].maxmarknum = 0;
  7223. cstr_index->tile_index[tileno].marknum = 0;
  7224. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */
  7225. return OPJ_FALSE;
  7226. }
  7227. cstr_index->tile_index[tileno].marker = new_marker;
  7228. }
  7229. /* add the marker */
  7230. cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type
  7231. = (OPJ_UINT16)type;
  7232. cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos
  7233. = (OPJ_INT32)pos;
  7234. cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len
  7235. = (OPJ_INT32)len;
  7236. cstr_index->tile_index[tileno].marknum++;
  7237. if (type == J2K_MS_SOT) {
  7238. OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
  7239. if (cstr_index->tile_index[tileno].tp_index) {
  7240. cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
  7241. }
  7242. }
  7243. return OPJ_TRUE;
  7244. }
  7245. /*
  7246. * -----------------------------------------------------------------------
  7247. * -----------------------------------------------------------------------
  7248. * -----------------------------------------------------------------------
  7249. */
  7250. OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k,
  7251. opj_stream_private_t *p_stream,
  7252. opj_event_mgr_t * p_manager
  7253. )
  7254. {
  7255. (void)p_j2k;
  7256. (void)p_stream;
  7257. (void)p_manager;
  7258. return OPJ_TRUE;
  7259. }
  7260. OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream,
  7261. opj_j2k_t* p_j2k,
  7262. opj_image_t** p_image,
  7263. opj_event_mgr_t* p_manager)
  7264. {
  7265. /* preconditions */
  7266. assert(p_j2k != 00);
  7267. assert(p_stream != 00);
  7268. assert(p_manager != 00);
  7269. /* create an empty image header */
  7270. p_j2k->m_private_image = opj_image_create0();
  7271. if (! p_j2k->m_private_image) {
  7272. return OPJ_FALSE;
  7273. }
  7274. /* customization of the validation */
  7275. if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) {
  7276. opj_image_destroy(p_j2k->m_private_image);
  7277. p_j2k->m_private_image = NULL;
  7278. return OPJ_FALSE;
  7279. }
  7280. /* validation of the parameters codec */
  7281. if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) {
  7282. opj_image_destroy(p_j2k->m_private_image);
  7283. p_j2k->m_private_image = NULL;
  7284. return OPJ_FALSE;
  7285. }
  7286. /* customization of the encoding */
  7287. if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) {
  7288. opj_image_destroy(p_j2k->m_private_image);
  7289. p_j2k->m_private_image = NULL;
  7290. return OPJ_FALSE;
  7291. }
  7292. /* read header */
  7293. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  7294. opj_image_destroy(p_j2k->m_private_image);
  7295. p_j2k->m_private_image = NULL;
  7296. return OPJ_FALSE;
  7297. }
  7298. *p_image = opj_image_create0();
  7299. if (!(*p_image)) {
  7300. return OPJ_FALSE;
  7301. }
  7302. /* Copy codestream image information to the output image */
  7303. opj_copy_image_header(p_j2k->m_private_image, *p_image);
  7304. /*Allocate and initialize some elements of codestrem index*/
  7305. if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) {
  7306. opj_image_destroy(*p_image);
  7307. *p_image = NULL;
  7308. return OPJ_FALSE;
  7309. }
  7310. return OPJ_TRUE;
  7311. }
  7312. static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k,
  7313. opj_event_mgr_t * p_manager)
  7314. {
  7315. /* preconditions*/
  7316. assert(p_j2k != 00);
  7317. assert(p_manager != 00);
  7318. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  7319. (opj_procedure)opj_j2k_read_header_procedure, p_manager)) {
  7320. return OPJ_FALSE;
  7321. }
  7322. /* DEVELOPER CORNER, add your custom procedures */
  7323. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  7324. (opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd, p_manager)) {
  7325. return OPJ_FALSE;
  7326. }
  7327. return OPJ_TRUE;
  7328. }
  7329. static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k,
  7330. opj_event_mgr_t * p_manager)
  7331. {
  7332. /* preconditions*/
  7333. assert(p_j2k != 00);
  7334. assert(p_manager != 00);
  7335. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  7336. (opj_procedure)opj_j2k_build_decoder, p_manager)) {
  7337. return OPJ_FALSE;
  7338. }
  7339. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  7340. (opj_procedure)opj_j2k_decoding_validation, p_manager)) {
  7341. return OPJ_FALSE;
  7342. }
  7343. /* DEVELOPER CORNER, add your custom validation procedure */
  7344. return OPJ_TRUE;
  7345. }
  7346. static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k,
  7347. opj_stream_private_t *p_stream,
  7348. opj_event_mgr_t * p_manager)
  7349. {
  7350. OPJ_BOOL l_is_valid = OPJ_TRUE;
  7351. OPJ_UINT32 i, j;
  7352. /* preconditions */
  7353. assert(p_j2k != 00);
  7354. assert(p_stream != 00);
  7355. assert(p_manager != 00);
  7356. OPJ_UNUSED(p_stream);
  7357. OPJ_UNUSED(p_manager);
  7358. if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
  7359. OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  7360. opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
  7361. for (i = 0; i < l_nb_tiles; ++i) {
  7362. if (l_tcp->mct == 2) {
  7363. opj_tccp_t * l_tccp = l_tcp->tccps;
  7364. l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
  7365. for (j = 0; j < p_j2k->m_private_image->numcomps; ++j) {
  7366. l_is_valid &= !(l_tccp->qmfbid & 1);
  7367. ++l_tccp;
  7368. }
  7369. }
  7370. ++l_tcp;
  7371. }
  7372. }
  7373. return l_is_valid;
  7374. }
  7375. OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)
  7376. {
  7377. OPJ_UINT32 i;
  7378. OPJ_UINT32 l_indix = 1;
  7379. opj_mct_data_t * l_mct_deco_data = 00, * l_mct_offset_data = 00;
  7380. opj_simple_mcc_decorrelation_data_t * l_mcc_data;
  7381. OPJ_UINT32 l_mct_size, l_nb_elem;
  7382. OPJ_FLOAT32 * l_data, * l_current_data;
  7383. opj_tccp_t * l_tccp;
  7384. /* preconditions */
  7385. assert(p_tcp != 00);
  7386. if (p_tcp->mct != 2) {
  7387. return OPJ_TRUE;
  7388. }
  7389. if (p_tcp->m_mct_decoding_matrix) {
  7390. if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
  7391. opj_mct_data_t *new_mct_records;
  7392. p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  7393. new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records,
  7394. p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
  7395. if (! new_mct_records) {
  7396. opj_free(p_tcp->m_mct_records);
  7397. p_tcp->m_mct_records = NULL;
  7398. p_tcp->m_nb_max_mct_records = 0;
  7399. p_tcp->m_nb_mct_records = 0;
  7400. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
  7401. return OPJ_FALSE;
  7402. }
  7403. p_tcp->m_mct_records = new_mct_records;
  7404. l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
  7405. memset(l_mct_deco_data, 0,
  7406. (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(
  7407. opj_mct_data_t));
  7408. }
  7409. l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
  7410. if (l_mct_deco_data->m_data) {
  7411. opj_free(l_mct_deco_data->m_data);
  7412. l_mct_deco_data->m_data = 00;
  7413. }
  7414. l_mct_deco_data->m_index = l_indix++;
  7415. l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
  7416. l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
  7417. l_nb_elem = p_image->numcomps * p_image->numcomps;
  7418. l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
  7419. l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size);
  7420. if (! l_mct_deco_data->m_data) {
  7421. return OPJ_FALSE;
  7422. }
  7423. j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](
  7424. p_tcp->m_mct_decoding_matrix, l_mct_deco_data->m_data, l_nb_elem);
  7425. l_mct_deco_data->m_data_size = l_mct_size;
  7426. ++p_tcp->m_nb_mct_records;
  7427. }
  7428. if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
  7429. opj_mct_data_t *new_mct_records;
  7430. p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  7431. new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records,
  7432. p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
  7433. if (! new_mct_records) {
  7434. opj_free(p_tcp->m_mct_records);
  7435. p_tcp->m_mct_records = NULL;
  7436. p_tcp->m_nb_max_mct_records = 0;
  7437. p_tcp->m_nb_mct_records = 0;
  7438. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
  7439. return OPJ_FALSE;
  7440. }
  7441. p_tcp->m_mct_records = new_mct_records;
  7442. l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
  7443. memset(l_mct_offset_data, 0,
  7444. (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(
  7445. opj_mct_data_t));
  7446. if (l_mct_deco_data) {
  7447. l_mct_deco_data = l_mct_offset_data - 1;
  7448. }
  7449. }
  7450. l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
  7451. if (l_mct_offset_data->m_data) {
  7452. opj_free(l_mct_offset_data->m_data);
  7453. l_mct_offset_data->m_data = 00;
  7454. }
  7455. l_mct_offset_data->m_index = l_indix++;
  7456. l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
  7457. l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
  7458. l_nb_elem = p_image->numcomps;
  7459. l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
  7460. l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size);
  7461. if (! l_mct_offset_data->m_data) {
  7462. return OPJ_FALSE;
  7463. }
  7464. l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
  7465. if (! l_data) {
  7466. opj_free(l_mct_offset_data->m_data);
  7467. l_mct_offset_data->m_data = 00;
  7468. return OPJ_FALSE;
  7469. }
  7470. l_tccp = p_tcp->tccps;
  7471. l_current_data = l_data;
  7472. for (i = 0; i < l_nb_elem; ++i) {
  7473. *(l_current_data++) = (OPJ_FLOAT32)(l_tccp->m_dc_level_shift);
  7474. ++l_tccp;
  7475. }
  7476. j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,
  7477. l_mct_offset_data->m_data, l_nb_elem);
  7478. opj_free(l_data);
  7479. l_mct_offset_data->m_data_size = l_mct_size;
  7480. ++p_tcp->m_nb_mct_records;
  7481. if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
  7482. opj_simple_mcc_decorrelation_data_t *new_mcc_records;
  7483. p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  7484. new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
  7485. p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(
  7486. opj_simple_mcc_decorrelation_data_t));
  7487. if (! new_mcc_records) {
  7488. opj_free(p_tcp->m_mcc_records);
  7489. p_tcp->m_mcc_records = NULL;
  7490. p_tcp->m_nb_max_mcc_records = 0;
  7491. p_tcp->m_nb_mcc_records = 0;
  7492. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
  7493. return OPJ_FALSE;
  7494. }
  7495. p_tcp->m_mcc_records = new_mcc_records;
  7496. l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
  7497. memset(l_mcc_data, 0, (p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) *
  7498. sizeof(opj_simple_mcc_decorrelation_data_t));
  7499. }
  7500. l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
  7501. l_mcc_data->m_decorrelation_array = l_mct_deco_data;
  7502. l_mcc_data->m_is_irreversible = 1;
  7503. l_mcc_data->m_nb_comps = p_image->numcomps;
  7504. l_mcc_data->m_index = l_indix++;
  7505. l_mcc_data->m_offset_array = l_mct_offset_data;
  7506. ++p_tcp->m_nb_mcc_records;
  7507. return OPJ_TRUE;
  7508. }
  7509. static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k,
  7510. opj_stream_private_t *p_stream,
  7511. opj_event_mgr_t * p_manager)
  7512. {
  7513. /* add here initialization of cp
  7514. copy paste of setup_decoder */
  7515. (void)p_j2k;
  7516. (void)p_stream;
  7517. (void)p_manager;
  7518. return OPJ_TRUE;
  7519. }
  7520. static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k,
  7521. opj_stream_private_t *p_stream,
  7522. opj_event_mgr_t * p_manager)
  7523. {
  7524. /* add here initialization of cp
  7525. copy paste of setup_encoder */
  7526. (void)p_j2k;
  7527. (void)p_stream;
  7528. (void)p_manager;
  7529. return OPJ_TRUE;
  7530. }
  7531. static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k,
  7532. opj_stream_private_t *p_stream,
  7533. opj_event_mgr_t * p_manager)
  7534. {
  7535. OPJ_BOOL l_is_valid = OPJ_TRUE;
  7536. /* preconditions */
  7537. assert(p_j2k != 00);
  7538. assert(p_stream != 00);
  7539. assert(p_manager != 00);
  7540. OPJ_UNUSED(p_stream);
  7541. /* STATE checking */
  7542. /* make sure the state is at 0 */
  7543. l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
  7544. /* POINTER validation */
  7545. /* make sure a p_j2k codec is present */
  7546. l_is_valid &= (p_j2k->m_procedure_list != 00);
  7547. /* make sure a validation list is present */
  7548. l_is_valid &= (p_j2k->m_validation_list != 00);
  7549. /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */
  7550. /* 33 (32) would always fail the check below (if a cast to 64bits was done) */
  7551. /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */
  7552. if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) ||
  7553. (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) {
  7554. opj_event_msg(p_manager, EVT_ERROR,
  7555. "Number of resolutions is too high in comparison to the size of tiles\n");
  7556. return OPJ_FALSE;
  7557. }
  7558. if ((p_j2k->m_cp.tdx) < (OPJ_UINT32)(1 <<
  7559. (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
  7560. opj_event_msg(p_manager, EVT_ERROR,
  7561. "Number of resolutions is too high in comparison to the size of tiles\n");
  7562. return OPJ_FALSE;
  7563. }
  7564. if ((p_j2k->m_cp.tdy) < (OPJ_UINT32)(1 <<
  7565. (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
  7566. opj_event_msg(p_manager, EVT_ERROR,
  7567. "Number of resolutions is too high in comparison to the size of tiles\n");
  7568. return OPJ_FALSE;
  7569. }
  7570. /* PARAMETER VALIDATION */
  7571. return l_is_valid;
  7572. }
  7573. static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t *p_j2k,
  7574. opj_stream_private_t *p_stream,
  7575. opj_event_mgr_t * p_manager
  7576. )
  7577. {
  7578. OPJ_BOOL l_is_valid = OPJ_TRUE;
  7579. /* preconditions*/
  7580. assert(p_j2k != 00);
  7581. assert(p_stream != 00);
  7582. assert(p_manager != 00);
  7583. OPJ_UNUSED(p_stream);
  7584. OPJ_UNUSED(p_manager);
  7585. /* STATE checking */
  7586. /* make sure the state is at 0 */
  7587. #ifdef TODO_MSD
  7588. l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
  7589. #endif
  7590. l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
  7591. /* POINTER validation */
  7592. /* make sure a p_j2k codec is present */
  7593. /* make sure a procedure list is present */
  7594. l_is_valid &= (p_j2k->m_procedure_list != 00);
  7595. /* make sure a validation list is present */
  7596. l_is_valid &= (p_j2k->m_validation_list != 00);
  7597. /* PARAMETER VALIDATION */
  7598. return l_is_valid;
  7599. }
  7600. static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k,
  7601. opj_stream_private_t *p_stream,
  7602. opj_event_mgr_t * p_manager)
  7603. {
  7604. OPJ_UINT32 l_current_marker;
  7605. OPJ_UINT32 l_marker_size;
  7606. const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
  7607. OPJ_BOOL l_has_siz = 0;
  7608. OPJ_BOOL l_has_cod = 0;
  7609. OPJ_BOOL l_has_qcd = 0;
  7610. /* preconditions */
  7611. assert(p_stream != 00);
  7612. assert(p_j2k != 00);
  7613. assert(p_manager != 00);
  7614. /* We enter in the main header */
  7615. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
  7616. /* Try to read the SOC marker, the codestream must begin with SOC marker */
  7617. if (! opj_j2k_read_soc(p_j2k, p_stream, p_manager)) {
  7618. opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n");
  7619. return OPJ_FALSE;
  7620. }
  7621. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
  7622. if (opj_stream_read_data(p_stream,
  7623. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  7624. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  7625. return OPJ_FALSE;
  7626. }
  7627. /* Read 2 bytes as the new marker ID */
  7628. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  7629. &l_current_marker, 2);
  7630. /* Try to read until the SOT is detected */
  7631. while (l_current_marker != J2K_MS_SOT) {
  7632. /* Check if the current marker ID is valid */
  7633. if (l_current_marker < 0xff00) {
  7634. opj_event_msg(p_manager, EVT_ERROR,
  7635. "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker);
  7636. return OPJ_FALSE;
  7637. }
  7638. /* Get the marker handler from the marker ID */
  7639. l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
  7640. /* Manage case where marker is unknown */
  7641. if (l_marker_handler->id == J2K_MS_UNK) {
  7642. if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)) {
  7643. opj_event_msg(p_manager, EVT_ERROR,
  7644. "Unknown marker has been detected and generated error.\n");
  7645. return OPJ_FALSE;
  7646. }
  7647. if (l_current_marker == J2K_MS_SOT) {
  7648. break; /* SOT marker is detected main header is completely read */
  7649. } else { /* Get the marker handler from the marker ID */
  7650. l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
  7651. }
  7652. }
  7653. if (l_marker_handler->id == J2K_MS_SIZ) {
  7654. /* Mark required SIZ marker as found */
  7655. l_has_siz = 1;
  7656. }
  7657. if (l_marker_handler->id == J2K_MS_COD) {
  7658. /* Mark required COD marker as found */
  7659. l_has_cod = 1;
  7660. }
  7661. if (l_marker_handler->id == J2K_MS_QCD) {
  7662. /* Mark required QCD marker as found */
  7663. l_has_qcd = 1;
  7664. }
  7665. /* Check if the marker is known and if it is the right place to find it */
  7666. if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
  7667. opj_event_msg(p_manager, EVT_ERROR,
  7668. "Marker is not compliant with its position\n");
  7669. return OPJ_FALSE;
  7670. }
  7671. /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
  7672. if (opj_stream_read_data(p_stream,
  7673. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  7674. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  7675. return OPJ_FALSE;
  7676. }
  7677. /* read 2 bytes as the marker size */
  7678. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size,
  7679. 2);
  7680. if (l_marker_size < 2) {
  7681. opj_event_msg(p_manager, EVT_ERROR, "Invalid marker size\n");
  7682. return OPJ_FALSE;
  7683. }
  7684. l_marker_size -= 2; /* Subtract the size of the marker ID already read */
  7685. /* Check if the marker size is compatible with the header data size */
  7686. if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
  7687. OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(
  7688. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
  7689. if (! new_header_data) {
  7690. opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
  7691. p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
  7692. p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
  7693. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
  7694. return OPJ_FALSE;
  7695. }
  7696. p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
  7697. p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
  7698. }
  7699. /* Try to read the rest of the marker segment from stream and copy them into the buffer */
  7700. if (opj_stream_read_data(p_stream,
  7701. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size,
  7702. p_manager) != l_marker_size) {
  7703. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  7704. return OPJ_FALSE;
  7705. }
  7706. /* Read the marker segment with the correct marker handler */
  7707. if (!(*(l_marker_handler->handler))(p_j2k,
  7708. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) {
  7709. opj_event_msg(p_manager, EVT_ERROR,
  7710. "Marker handler function failed to read the marker segment\n");
  7711. return OPJ_FALSE;
  7712. }
  7713. /* Add the marker to the codestream index*/
  7714. if (OPJ_FALSE == opj_j2k_add_mhmarker(
  7715. p_j2k->cstr_index,
  7716. l_marker_handler->id,
  7717. (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
  7718. l_marker_size + 4)) {
  7719. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
  7720. return OPJ_FALSE;
  7721. }
  7722. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
  7723. if (opj_stream_read_data(p_stream,
  7724. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  7725. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  7726. return OPJ_FALSE;
  7727. }
  7728. /* read 2 bytes as the new marker ID */
  7729. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  7730. &l_current_marker, 2);
  7731. }
  7732. if (l_has_siz == 0) {
  7733. opj_event_msg(p_manager, EVT_ERROR,
  7734. "required SIZ marker not found in main header\n");
  7735. return OPJ_FALSE;
  7736. }
  7737. if (l_has_cod == 0) {
  7738. opj_event_msg(p_manager, EVT_ERROR,
  7739. "required COD marker not found in main header\n");
  7740. return OPJ_FALSE;
  7741. }
  7742. if (l_has_qcd == 0) {
  7743. opj_event_msg(p_manager, EVT_ERROR,
  7744. "required QCD marker not found in main header\n");
  7745. return OPJ_FALSE;
  7746. }
  7747. if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) {
  7748. opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n");
  7749. return OPJ_FALSE;
  7750. }
  7751. opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
  7752. /* Position of the last element if the main header */
  7753. p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
  7754. /* Next step: read a tile-part header */
  7755. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
  7756. return OPJ_TRUE;
  7757. }
  7758. static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k,
  7759. opj_procedure_list_t * p_procedure_list,
  7760. opj_stream_private_t *p_stream,
  7761. opj_event_mgr_t * p_manager)
  7762. {
  7763. OPJ_BOOL(** l_procedure)(opj_j2k_t *, opj_stream_private_t *,
  7764. opj_event_mgr_t *) = 00;
  7765. OPJ_BOOL l_result = OPJ_TRUE;
  7766. OPJ_UINT32 l_nb_proc, i;
  7767. /* preconditions*/
  7768. assert(p_procedure_list != 00);
  7769. assert(p_j2k != 00);
  7770. assert(p_stream != 00);
  7771. assert(p_manager != 00);
  7772. l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
  7773. l_procedure = (OPJ_BOOL(**)(opj_j2k_t *, opj_stream_private_t *,
  7774. opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
  7775. for (i = 0; i < l_nb_proc; ++i) {
  7776. l_result = l_result && ((*l_procedure)(p_j2k, p_stream, p_manager));
  7777. ++l_procedure;
  7778. }
  7779. /* and clear the procedure list at the end.*/
  7780. opj_procedure_list_clear(p_procedure_list);
  7781. return l_result;
  7782. }
  7783. /* FIXME DOC*/
  7784. static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k,
  7785. opj_stream_private_t *p_stream,
  7786. opj_event_mgr_t * p_manager
  7787. )
  7788. {
  7789. opj_tcp_t * l_tcp = 00;
  7790. opj_tcp_t * l_default_tcp = 00;
  7791. OPJ_UINT32 l_nb_tiles;
  7792. OPJ_UINT32 i, j;
  7793. opj_tccp_t *l_current_tccp = 00;
  7794. OPJ_UINT32 l_tccp_size;
  7795. OPJ_UINT32 l_mct_size;
  7796. opj_image_t * l_image;
  7797. OPJ_UINT32 l_mcc_records_size, l_mct_records_size;
  7798. opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
  7799. opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
  7800. OPJ_UINT32 l_offset;
  7801. /* preconditions */
  7802. assert(p_j2k != 00);
  7803. assert(p_stream != 00);
  7804. assert(p_manager != 00);
  7805. OPJ_UNUSED(p_stream);
  7806. l_image = p_j2k->m_private_image;
  7807. l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  7808. l_tcp = p_j2k->m_cp.tcps;
  7809. l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t);
  7810. l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
  7811. l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof(
  7812. OPJ_FLOAT32);
  7813. /* For each tile */
  7814. for (i = 0; i < l_nb_tiles; ++i) {
  7815. /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
  7816. l_current_tccp = l_tcp->tccps;
  7817. /*Copy default coding parameters into the current tile coding parameters*/
  7818. memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t));
  7819. /* Initialize some values of the current tile coding parameters*/
  7820. l_tcp->cod = 0;
  7821. l_tcp->ppt = 0;
  7822. l_tcp->ppt_data = 00;
  7823. l_tcp->m_current_tile_part_number = -1;
  7824. /* Remove memory not owned by this tile in case of early error return. */
  7825. l_tcp->m_mct_decoding_matrix = 00;
  7826. l_tcp->m_nb_max_mct_records = 0;
  7827. l_tcp->m_mct_records = 00;
  7828. l_tcp->m_nb_max_mcc_records = 0;
  7829. l_tcp->m_mcc_records = 00;
  7830. /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
  7831. l_tcp->tccps = l_current_tccp;
  7832. /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
  7833. if (l_default_tcp->m_mct_decoding_matrix) {
  7834. l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
  7835. if (! l_tcp->m_mct_decoding_matrix) {
  7836. return OPJ_FALSE;
  7837. }
  7838. memcpy(l_tcp->m_mct_decoding_matrix, l_default_tcp->m_mct_decoding_matrix,
  7839. l_mct_size);
  7840. }
  7841. /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
  7842. l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof(
  7843. opj_mct_data_t);
  7844. l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
  7845. if (! l_tcp->m_mct_records) {
  7846. return OPJ_FALSE;
  7847. }
  7848. memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records, l_mct_records_size);
  7849. /* Copy the mct record data from dflt_tile_cp to the current tile*/
  7850. l_src_mct_rec = l_default_tcp->m_mct_records;
  7851. l_dest_mct_rec = l_tcp->m_mct_records;
  7852. for (j = 0; j < l_default_tcp->m_nb_mct_records; ++j) {
  7853. if (l_src_mct_rec->m_data) {
  7854. l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
  7855. if (! l_dest_mct_rec->m_data) {
  7856. return OPJ_FALSE;
  7857. }
  7858. memcpy(l_dest_mct_rec->m_data, l_src_mct_rec->m_data,
  7859. l_src_mct_rec->m_data_size);
  7860. }
  7861. ++l_src_mct_rec;
  7862. ++l_dest_mct_rec;
  7863. /* Update with each pass to free exactly what has been allocated on early return. */
  7864. l_tcp->m_nb_max_mct_records += 1;
  7865. }
  7866. /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
  7867. l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof(
  7868. opj_simple_mcc_decorrelation_data_t);
  7869. l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(
  7870. l_mcc_records_size);
  7871. if (! l_tcp->m_mcc_records) {
  7872. return OPJ_FALSE;
  7873. }
  7874. memcpy(l_tcp->m_mcc_records, l_default_tcp->m_mcc_records, l_mcc_records_size);
  7875. l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records;
  7876. /* Copy the mcc record data from dflt_tile_cp to the current tile*/
  7877. l_src_mcc_rec = l_default_tcp->m_mcc_records;
  7878. l_dest_mcc_rec = l_tcp->m_mcc_records;
  7879. for (j = 0; j < l_default_tcp->m_nb_max_mcc_records; ++j) {
  7880. if (l_src_mcc_rec->m_decorrelation_array) {
  7881. l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array -
  7882. l_default_tcp->m_mct_records);
  7883. l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
  7884. }
  7885. if (l_src_mcc_rec->m_offset_array) {
  7886. l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array -
  7887. l_default_tcp->m_mct_records);
  7888. l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
  7889. }
  7890. ++l_src_mcc_rec;
  7891. ++l_dest_mcc_rec;
  7892. }
  7893. /* Copy all the dflt_tile_compo_cp to the current tile cp */
  7894. memcpy(l_current_tccp, l_default_tcp->tccps, l_tccp_size);
  7895. /* Move to next tile cp*/
  7896. ++l_tcp;
  7897. }
  7898. /* Create the current tile decoder*/
  7899. p_j2k->m_tcd = opj_tcd_create(OPJ_TRUE);
  7900. if (! p_j2k->m_tcd) {
  7901. return OPJ_FALSE;
  7902. }
  7903. if (!opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp), p_j2k->m_tp)) {
  7904. opj_tcd_destroy(p_j2k->m_tcd);
  7905. p_j2k->m_tcd = 00;
  7906. opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
  7907. return OPJ_FALSE;
  7908. }
  7909. return OPJ_TRUE;
  7910. }
  7911. static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler(
  7912. OPJ_UINT32 p_id)
  7913. {
  7914. const opj_dec_memory_marker_handler_t *e;
  7915. for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
  7916. if (e->id == p_id) {
  7917. break; /* we find a handler corresponding to the marker ID*/
  7918. }
  7919. }
  7920. return e;
  7921. }
  7922. void opj_j2k_destroy(opj_j2k_t *p_j2k)
  7923. {
  7924. if (p_j2k == 00) {
  7925. return;
  7926. }
  7927. if (p_j2k->m_is_decoder) {
  7928. if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
  7929. opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
  7930. opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
  7931. p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
  7932. }
  7933. if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
  7934. opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
  7935. p_j2k->m_specific_param.m_decoder.m_header_data = 00;
  7936. p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
  7937. }
  7938. opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode);
  7939. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = 00;
  7940. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0;
  7941. } else {
  7942. if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
  7943. opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
  7944. p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
  7945. }
  7946. if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
  7947. opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
  7948. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
  7949. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
  7950. }
  7951. if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
  7952. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  7953. p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
  7954. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  7955. }
  7956. }
  7957. opj_tcd_destroy(p_j2k->m_tcd);
  7958. opj_j2k_cp_destroy(&(p_j2k->m_cp));
  7959. memset(&(p_j2k->m_cp), 0, sizeof(opj_cp_t));
  7960. opj_procedure_list_destroy(p_j2k->m_procedure_list);
  7961. p_j2k->m_procedure_list = 00;
  7962. opj_procedure_list_destroy(p_j2k->m_validation_list);
  7963. p_j2k->m_procedure_list = 00;
  7964. j2k_destroy_cstr_index(p_j2k->cstr_index);
  7965. p_j2k->cstr_index = NULL;
  7966. opj_image_destroy(p_j2k->m_private_image);
  7967. p_j2k->m_private_image = NULL;
  7968. opj_image_destroy(p_j2k->m_output_image);
  7969. p_j2k->m_output_image = NULL;
  7970. opj_thread_pool_destroy(p_j2k->m_tp);
  7971. p_j2k->m_tp = NULL;
  7972. opj_free(p_j2k);
  7973. }
  7974. void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind)
  7975. {
  7976. if (p_cstr_ind) {
  7977. if (p_cstr_ind->marker) {
  7978. opj_free(p_cstr_ind->marker);
  7979. p_cstr_ind->marker = NULL;
  7980. }
  7981. if (p_cstr_ind->tile_index) {
  7982. OPJ_UINT32 it_tile = 0;
  7983. for (it_tile = 0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
  7984. if (p_cstr_ind->tile_index[it_tile].packet_index) {
  7985. opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
  7986. p_cstr_ind->tile_index[it_tile].packet_index = NULL;
  7987. }
  7988. if (p_cstr_ind->tile_index[it_tile].tp_index) {
  7989. opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
  7990. p_cstr_ind->tile_index[it_tile].tp_index = NULL;
  7991. }
  7992. if (p_cstr_ind->tile_index[it_tile].marker) {
  7993. opj_free(p_cstr_ind->tile_index[it_tile].marker);
  7994. p_cstr_ind->tile_index[it_tile].marker = NULL;
  7995. }
  7996. }
  7997. opj_free(p_cstr_ind->tile_index);
  7998. p_cstr_ind->tile_index = NULL;
  7999. }
  8000. opj_free(p_cstr_ind);
  8001. }
  8002. }
  8003. static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp)
  8004. {
  8005. if (p_tcp == 00) {
  8006. return;
  8007. }
  8008. if (p_tcp->ppt_markers != 00) {
  8009. OPJ_UINT32 i;
  8010. for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
  8011. if (p_tcp->ppt_markers[i].m_data != NULL) {
  8012. opj_free(p_tcp->ppt_markers[i].m_data);
  8013. }
  8014. }
  8015. p_tcp->ppt_markers_count = 0U;
  8016. opj_free(p_tcp->ppt_markers);
  8017. p_tcp->ppt_markers = NULL;
  8018. }
  8019. if (p_tcp->ppt_buffer != 00) {
  8020. opj_free(p_tcp->ppt_buffer);
  8021. p_tcp->ppt_buffer = 00;
  8022. }
  8023. if (p_tcp->tccps != 00) {
  8024. opj_free(p_tcp->tccps);
  8025. p_tcp->tccps = 00;
  8026. }
  8027. if (p_tcp->m_mct_coding_matrix != 00) {
  8028. opj_free(p_tcp->m_mct_coding_matrix);
  8029. p_tcp->m_mct_coding_matrix = 00;
  8030. }
  8031. if (p_tcp->m_mct_decoding_matrix != 00) {
  8032. opj_free(p_tcp->m_mct_decoding_matrix);
  8033. p_tcp->m_mct_decoding_matrix = 00;
  8034. }
  8035. if (p_tcp->m_mcc_records) {
  8036. opj_free(p_tcp->m_mcc_records);
  8037. p_tcp->m_mcc_records = 00;
  8038. p_tcp->m_nb_max_mcc_records = 0;
  8039. p_tcp->m_nb_mcc_records = 0;
  8040. }
  8041. if (p_tcp->m_mct_records) {
  8042. opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
  8043. OPJ_UINT32 i;
  8044. for (i = 0; i < p_tcp->m_nb_mct_records; ++i) {
  8045. if (l_mct_data->m_data) {
  8046. opj_free(l_mct_data->m_data);
  8047. l_mct_data->m_data = 00;
  8048. }
  8049. ++l_mct_data;
  8050. }
  8051. opj_free(p_tcp->m_mct_records);
  8052. p_tcp->m_mct_records = 00;
  8053. }
  8054. if (p_tcp->mct_norms != 00) {
  8055. opj_free(p_tcp->mct_norms);
  8056. p_tcp->mct_norms = 00;
  8057. }
  8058. opj_j2k_tcp_data_destroy(p_tcp);
  8059. }
  8060. static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp)
  8061. {
  8062. if (p_tcp->m_data) {
  8063. opj_free(p_tcp->m_data);
  8064. p_tcp->m_data = NULL;
  8065. p_tcp->m_data_size = 0;
  8066. }
  8067. }
  8068. static void opj_j2k_cp_destroy(opj_cp_t *p_cp)
  8069. {
  8070. OPJ_UINT32 l_nb_tiles;
  8071. opj_tcp_t * l_current_tile = 00;
  8072. if (p_cp == 00) {
  8073. return;
  8074. }
  8075. if (p_cp->tcps != 00) {
  8076. OPJ_UINT32 i;
  8077. l_current_tile = p_cp->tcps;
  8078. l_nb_tiles = p_cp->th * p_cp->tw;
  8079. for (i = 0U; i < l_nb_tiles; ++i) {
  8080. opj_j2k_tcp_destroy(l_current_tile);
  8081. ++l_current_tile;
  8082. }
  8083. opj_free(p_cp->tcps);
  8084. p_cp->tcps = 00;
  8085. }
  8086. if (p_cp->ppm_markers != 00) {
  8087. OPJ_UINT32 i;
  8088. for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
  8089. if (p_cp->ppm_markers[i].m_data != NULL) {
  8090. opj_free(p_cp->ppm_markers[i].m_data);
  8091. }
  8092. }
  8093. p_cp->ppm_markers_count = 0U;
  8094. opj_free(p_cp->ppm_markers);
  8095. p_cp->ppm_markers = NULL;
  8096. }
  8097. opj_free(p_cp->ppm_buffer);
  8098. p_cp->ppm_buffer = 00;
  8099. p_cp->ppm_data =
  8100. NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */
  8101. opj_free(p_cp->comment);
  8102. p_cp->comment = 00;
  8103. if (! p_cp->m_is_decoder) {
  8104. opj_free(p_cp->m_specific_param.m_enc.m_matrice);
  8105. p_cp->m_specific_param.m_enc.m_matrice = 00;
  8106. }
  8107. }
  8108. static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t
  8109. *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed,
  8110. opj_event_mgr_t * p_manager)
  8111. {
  8112. OPJ_BYTE l_header_data[10];
  8113. OPJ_OFF_T l_stream_pos_backup;
  8114. OPJ_UINT32 l_current_marker;
  8115. OPJ_UINT32 l_marker_size;
  8116. OPJ_UINT32 l_tile_no, l_tot_len, l_current_part, l_num_parts;
  8117. /* initialize to no correction needed */
  8118. *p_correction_needed = OPJ_FALSE;
  8119. if (!opj_stream_has_seek(p_stream)) {
  8120. /* We can't do much in this case, seek is needed */
  8121. return OPJ_TRUE;
  8122. }
  8123. l_stream_pos_backup = opj_stream_tell(p_stream);
  8124. if (l_stream_pos_backup == -1) {
  8125. /* let's do nothing */
  8126. return OPJ_TRUE;
  8127. }
  8128. for (;;) {
  8129. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
  8130. if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) {
  8131. /* assume all is OK */
  8132. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8133. return OPJ_FALSE;
  8134. }
  8135. return OPJ_TRUE;
  8136. }
  8137. /* Read 2 bytes from buffer as the new marker ID */
  8138. opj_read_bytes(l_header_data, &l_current_marker, 2);
  8139. if (l_current_marker != J2K_MS_SOT) {
  8140. /* assume all is OK */
  8141. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8142. return OPJ_FALSE;
  8143. }
  8144. return OPJ_TRUE;
  8145. }
  8146. /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
  8147. if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) {
  8148. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8149. return OPJ_FALSE;
  8150. }
  8151. /* Read 2 bytes from the buffer as the marker size */
  8152. opj_read_bytes(l_header_data, &l_marker_size, 2);
  8153. /* Check marker size for SOT Marker */
  8154. if (l_marker_size != 10) {
  8155. opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n");
  8156. return OPJ_FALSE;
  8157. }
  8158. l_marker_size -= 2;
  8159. if (opj_stream_read_data(p_stream, l_header_data, l_marker_size,
  8160. p_manager) != l_marker_size) {
  8161. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8162. return OPJ_FALSE;
  8163. }
  8164. if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no,
  8165. &l_tot_len, &l_current_part, &l_num_parts, p_manager)) {
  8166. return OPJ_FALSE;
  8167. }
  8168. if (l_tile_no == tile_no) {
  8169. /* we found what we were looking for */
  8170. break;
  8171. }
  8172. if (l_tot_len < 14U) {
  8173. /* last SOT until EOC or invalid Psot value */
  8174. /* assume all is OK */
  8175. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8176. return OPJ_FALSE;
  8177. }
  8178. return OPJ_TRUE;
  8179. }
  8180. l_tot_len -= 12U;
  8181. /* look for next SOT marker */
  8182. if (opj_stream_skip(p_stream, (OPJ_OFF_T)(l_tot_len),
  8183. p_manager) != (OPJ_OFF_T)(l_tot_len)) {
  8184. /* assume all is OK */
  8185. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8186. return OPJ_FALSE;
  8187. }
  8188. return OPJ_TRUE;
  8189. }
  8190. }
  8191. /* check for correction */
  8192. if (l_current_part == l_num_parts) {
  8193. *p_correction_needed = OPJ_TRUE;
  8194. }
  8195. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8196. return OPJ_FALSE;
  8197. }
  8198. return OPJ_TRUE;
  8199. }
  8200. OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k,
  8201. OPJ_UINT32 * p_tile_index,
  8202. OPJ_UINT32 * p_data_size,
  8203. OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
  8204. OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
  8205. OPJ_UINT32 * p_nb_comps,
  8206. OPJ_BOOL * p_go_on,
  8207. opj_stream_private_t *p_stream,
  8208. opj_event_mgr_t * p_manager)
  8209. {
  8210. OPJ_UINT32 l_current_marker = J2K_MS_SOT;
  8211. OPJ_UINT32 l_marker_size;
  8212. const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
  8213. opj_tcp_t * l_tcp = NULL;
  8214. const OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
  8215. /* preconditions */
  8216. assert(p_stream != 00);
  8217. assert(p_j2k != 00);
  8218. assert(p_manager != 00);
  8219. /* Reach the End Of Codestream ?*/
  8220. if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) {
  8221. l_current_marker = J2K_MS_EOC;
  8222. }
  8223. /* We need to encounter a SOT marker (a new tile-part header) */
  8224. else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) {
  8225. return OPJ_FALSE;
  8226. }
  8227. /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
  8228. while ((!p_j2k->m_specific_param.m_decoder.m_can_decode) &&
  8229. (l_current_marker != J2K_MS_EOC)) {
  8230. /* Try to read until the Start Of Data is detected */
  8231. while (l_current_marker != J2K_MS_SOD) {
  8232. if (opj_stream_get_number_byte_left(p_stream) == 0) {
  8233. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
  8234. break;
  8235. }
  8236. /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
  8237. if (opj_stream_read_data(p_stream,
  8238. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  8239. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8240. return OPJ_FALSE;
  8241. }
  8242. /* Read 2 bytes from the buffer as the marker size */
  8243. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size,
  8244. 2);
  8245. /* Check marker size (does not include marker ID but includes marker size) */
  8246. if (l_marker_size < 2) {
  8247. opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n");
  8248. return OPJ_FALSE;
  8249. }
  8250. /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */
  8251. if (l_current_marker == 0x8080 &&
  8252. opj_stream_get_number_byte_left(p_stream) == 0) {
  8253. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
  8254. break;
  8255. }
  8256. /* Why this condition? FIXME */
  8257. if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH) {
  8258. p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
  8259. }
  8260. l_marker_size -= 2; /* Subtract the size of the marker ID already read */
  8261. /* Get the marker handler from the marker ID */
  8262. l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
  8263. /* Check if the marker is known and if it is the right place to find it */
  8264. if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
  8265. opj_event_msg(p_manager, EVT_ERROR,
  8266. "Marker is not compliant with its position\n");
  8267. return OPJ_FALSE;
  8268. }
  8269. /* FIXME manage case of unknown marker as in the main header ? */
  8270. /* Check if the marker size is compatible with the header data size */
  8271. if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
  8272. OPJ_BYTE *new_header_data = NULL;
  8273. /* If we are here, this means we consider this marker as known & we will read it */
  8274. /* Check enough bytes left in stream before allocation */
  8275. if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) {
  8276. opj_event_msg(p_manager, EVT_ERROR,
  8277. "Marker size inconsistent with stream length\n");
  8278. return OPJ_FALSE;
  8279. }
  8280. new_header_data = (OPJ_BYTE *) opj_realloc(
  8281. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
  8282. if (! new_header_data) {
  8283. opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
  8284. p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
  8285. p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
  8286. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
  8287. return OPJ_FALSE;
  8288. }
  8289. p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
  8290. p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
  8291. }
  8292. /* Try to read the rest of the marker segment from stream and copy them into the buffer */
  8293. if (opj_stream_read_data(p_stream,
  8294. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size,
  8295. p_manager) != l_marker_size) {
  8296. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8297. return OPJ_FALSE;
  8298. }
  8299. if (!l_marker_handler->handler) {
  8300. /* See issue #175 */
  8301. opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n");
  8302. return OPJ_FALSE;
  8303. }
  8304. /* Read the marker segment with the correct marker handler */
  8305. if (!(*(l_marker_handler->handler))(p_j2k,
  8306. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) {
  8307. opj_event_msg(p_manager, EVT_ERROR,
  8308. "Fail to read the current marker segment (%#x)\n", l_current_marker);
  8309. return OPJ_FALSE;
  8310. }
  8311. /* Add the marker to the codestream index*/
  8312. if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
  8313. p_j2k->cstr_index,
  8314. l_marker_handler->id,
  8315. (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
  8316. l_marker_size + 4)) {
  8317. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
  8318. return OPJ_FALSE;
  8319. }
  8320. /* Keep the position of the last SOT marker read */
  8321. if (l_marker_handler->id == J2K_MS_SOT) {
  8322. OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4
  8323. ;
  8324. if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) {
  8325. p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
  8326. }
  8327. }
  8328. if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
  8329. /* Skip the rest of the tile part header*/
  8330. if (opj_stream_skip(p_stream, p_j2k->m_specific_param.m_decoder.m_sot_length,
  8331. p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) {
  8332. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8333. return OPJ_FALSE;
  8334. }
  8335. l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
  8336. } else {
  8337. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
  8338. if (opj_stream_read_data(p_stream,
  8339. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  8340. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8341. return OPJ_FALSE;
  8342. }
  8343. /* Read 2 bytes from the buffer as the new marker ID */
  8344. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  8345. &l_current_marker, 2);
  8346. }
  8347. }
  8348. if (opj_stream_get_number_byte_left(p_stream) == 0
  8349. && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) {
  8350. break;
  8351. }
  8352. /* If we didn't skip data before, we need to read the SOD marker*/
  8353. if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
  8354. /* Try to read the SOD marker and skip data ? FIXME */
  8355. if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
  8356. return OPJ_FALSE;
  8357. }
  8358. if (p_j2k->m_specific_param.m_decoder.m_can_decode &&
  8359. !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) {
  8360. /* Issue 254 */
  8361. OPJ_BOOL l_correction_needed;
  8362. p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1;
  8363. if (!opj_j2k_need_nb_tile_parts_correction(p_stream,
  8364. p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) {
  8365. opj_event_msg(p_manager, EVT_ERROR,
  8366. "opj_j2k_apply_nb_tile_parts_correction error\n");
  8367. return OPJ_FALSE;
  8368. }
  8369. if (l_correction_needed) {
  8370. OPJ_UINT32 l_tile_no;
  8371. p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
  8372. p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction = 1;
  8373. /* correct tiles */
  8374. for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) {
  8375. if (p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts != 0U) {
  8376. p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts += 1;
  8377. }
  8378. }
  8379. opj_event_msg(p_manager, EVT_WARNING,
  8380. "Non conformant codestream TPsot==TNsot.\n");
  8381. }
  8382. }
  8383. } else {
  8384. /* Indicate we will try to read a new tile-part header*/
  8385. p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
  8386. p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
  8387. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
  8388. }
  8389. if (! p_j2k->m_specific_param.m_decoder.m_can_decode) {
  8390. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
  8391. if (opj_stream_read_data(p_stream,
  8392. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  8393. /* Deal with likely non conformant SPOT6 files, where the last */
  8394. /* row of tiles have TPsot == 0 and TNsot == 0, and missing EOC, */
  8395. /* but no other tile-parts were found. */
  8396. if (p_j2k->m_current_tile_number + 1 == l_nb_tiles) {
  8397. OPJ_UINT32 l_tile_no;
  8398. for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) {
  8399. if (p_j2k->m_cp.tcps[l_tile_no].m_current_tile_part_number == 0 &&
  8400. p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts == 0) {
  8401. break;
  8402. }
  8403. }
  8404. if (l_tile_no < l_nb_tiles) {
  8405. opj_event_msg(p_manager, EVT_INFO,
  8406. "Tile %u has TPsot == 0 and TNsot == 0, "
  8407. "but no other tile-parts were found. "
  8408. "EOC is also missing.\n",
  8409. l_tile_no);
  8410. p_j2k->m_current_tile_number = l_tile_no;
  8411. l_current_marker = J2K_MS_EOC;
  8412. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
  8413. break;
  8414. }
  8415. }
  8416. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8417. return OPJ_FALSE;
  8418. }
  8419. /* Read 2 bytes from buffer as the new marker ID */
  8420. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  8421. &l_current_marker, 2);
  8422. }
  8423. }
  8424. /* Current marker is the EOC marker ?*/
  8425. if (l_current_marker == J2K_MS_EOC) {
  8426. if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) {
  8427. p_j2k->m_current_tile_number = 0;
  8428. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
  8429. }
  8430. }
  8431. /* Deal with tiles that have a single tile-part with TPsot == 0 and TNsot == 0 */
  8432. if (! p_j2k->m_specific_param.m_decoder.m_can_decode) {
  8433. l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
  8434. while ((p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00)) {
  8435. ++p_j2k->m_current_tile_number;
  8436. ++l_tcp;
  8437. }
  8438. if (p_j2k->m_current_tile_number == l_nb_tiles) {
  8439. *p_go_on = OPJ_FALSE;
  8440. return OPJ_TRUE;
  8441. }
  8442. }
  8443. if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number,
  8444. p_manager)) {
  8445. opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n");
  8446. return OPJ_FALSE;
  8447. }
  8448. /*FIXME ???*/
  8449. if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number,
  8450. p_manager)) {
  8451. opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
  8452. return OPJ_FALSE;
  8453. }
  8454. opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
  8455. p_j2k->m_current_tile_number + 1, (p_j2k->m_cp.th * p_j2k->m_cp.tw));
  8456. *p_tile_index = p_j2k->m_current_tile_number;
  8457. *p_go_on = OPJ_TRUE;
  8458. if (p_data_size) {
  8459. /* For internal use in j2k.c, we don't need this */
  8460. /* This is just needed for folks using the opj_read_tile_header() / opj_decode_tile_data() combo */
  8461. *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd, OPJ_FALSE);
  8462. if (*p_data_size == UINT_MAX) {
  8463. return OPJ_FALSE;
  8464. }
  8465. }
  8466. *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
  8467. *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
  8468. *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
  8469. *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
  8470. *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
  8471. p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA;
  8472. return OPJ_TRUE;
  8473. }
  8474. OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k,
  8475. OPJ_UINT32 p_tile_index,
  8476. OPJ_BYTE * p_data,
  8477. OPJ_UINT32 p_data_size,
  8478. opj_stream_private_t *p_stream,
  8479. opj_event_mgr_t * p_manager)
  8480. {
  8481. OPJ_UINT32 l_current_marker;
  8482. OPJ_BYTE l_data [2];
  8483. opj_tcp_t * l_tcp;
  8484. opj_image_t* l_image_for_bounds;
  8485. /* preconditions */
  8486. assert(p_stream != 00);
  8487. assert(p_j2k != 00);
  8488. assert(p_manager != 00);
  8489. if (!(p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_DATA)
  8490. || (p_tile_index != p_j2k->m_current_tile_number)) {
  8491. return OPJ_FALSE;
  8492. }
  8493. l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
  8494. if (! l_tcp->m_data) {
  8495. opj_j2k_tcp_destroy(l_tcp);
  8496. return OPJ_FALSE;
  8497. }
  8498. /* When using the opj_read_tile_header / opj_decode_tile_data API */
  8499. /* such as in test_tile_decoder, m_output_image is NULL, so fall back */
  8500. /* to the full image dimension. This is a bit surprising that */
  8501. /* opj_set_decode_area() is only used to determine intersecting tiles, */
  8502. /* but full tile decoding is done */
  8503. l_image_for_bounds = p_j2k->m_output_image ? p_j2k->m_output_image :
  8504. p_j2k->m_private_image;
  8505. if (! opj_tcd_decode_tile(p_j2k->m_tcd,
  8506. l_image_for_bounds->x0,
  8507. l_image_for_bounds->y0,
  8508. l_image_for_bounds->x1,
  8509. l_image_for_bounds->y1,
  8510. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode,
  8511. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode,
  8512. l_tcp->m_data,
  8513. l_tcp->m_data_size,
  8514. p_tile_index,
  8515. p_j2k->cstr_index, p_manager)) {
  8516. opj_j2k_tcp_destroy(l_tcp);
  8517. p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
  8518. opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");
  8519. return OPJ_FALSE;
  8520. }
  8521. /* p_data can be set to NULL when the call will take care of using */
  8522. /* itself the TCD data. This is typically the case for whole single */
  8523. /* tile decoding optimization. */
  8524. if (p_data != NULL) {
  8525. if (! opj_tcd_update_tile_data(p_j2k->m_tcd, p_data, p_data_size)) {
  8526. return OPJ_FALSE;
  8527. }
  8528. /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
  8529. * we destroy just the data which will be re-read in read_tile_header*/
  8530. /*opj_j2k_tcp_destroy(l_tcp);
  8531. p_j2k->m_tcd->tcp = 0;*/
  8532. opj_j2k_tcp_data_destroy(l_tcp);
  8533. }
  8534. p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
  8535. p_j2k->m_specific_param.m_decoder.m_state &= (~(OPJ_UINT32)J2K_STATE_DATA);
  8536. if (opj_stream_get_number_byte_left(p_stream) == 0
  8537. && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) {
  8538. return OPJ_TRUE;
  8539. }
  8540. if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) {
  8541. if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) {
  8542. opj_event_msg(p_manager, p_j2k->m_cp.strict ? EVT_ERROR : EVT_WARNING,
  8543. "Stream too short\n");
  8544. return p_j2k->m_cp.strict ? OPJ_FALSE : OPJ_TRUE;
  8545. }
  8546. opj_read_bytes(l_data, &l_current_marker, 2);
  8547. if (l_current_marker == J2K_MS_EOC) {
  8548. p_j2k->m_current_tile_number = 0;
  8549. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
  8550. } else if (l_current_marker != J2K_MS_SOT) {
  8551. if (opj_stream_get_number_byte_left(p_stream) == 0) {
  8552. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
  8553. opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n");
  8554. return OPJ_TRUE;
  8555. }
  8556. opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
  8557. return OPJ_FALSE;
  8558. }
  8559. }
  8560. return OPJ_TRUE;
  8561. }
  8562. static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd,
  8563. opj_image_t* p_output_image)
  8564. {
  8565. OPJ_UINT32 i, j;
  8566. OPJ_UINT32 l_width_src, l_height_src;
  8567. OPJ_UINT32 l_width_dest, l_height_dest;
  8568. OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
  8569. OPJ_SIZE_T l_start_offset_src;
  8570. OPJ_UINT32 l_start_x_dest, l_start_y_dest;
  8571. OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
  8572. OPJ_SIZE_T l_start_offset_dest;
  8573. opj_image_comp_t * l_img_comp_src = 00;
  8574. opj_image_comp_t * l_img_comp_dest = 00;
  8575. opj_tcd_tilecomp_t * l_tilec = 00;
  8576. opj_image_t * l_image_src = 00;
  8577. OPJ_INT32 * l_dest_ptr;
  8578. l_tilec = p_tcd->tcd_image->tiles->comps;
  8579. l_image_src = p_tcd->image;
  8580. l_img_comp_src = l_image_src->comps;
  8581. l_img_comp_dest = p_output_image->comps;
  8582. for (i = 0; i < l_image_src->numcomps;
  8583. i++, ++l_img_comp_dest, ++l_img_comp_src, ++l_tilec) {
  8584. OPJ_INT32 res_x0, res_x1, res_y0, res_y1;
  8585. OPJ_UINT32 src_data_stride;
  8586. const OPJ_INT32* p_src_data;
  8587. /* Copy info from decoded comp image to output image */
  8588. l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
  8589. if (p_tcd->whole_tile_decoding) {
  8590. opj_tcd_resolution_t* l_res = l_tilec->resolutions +
  8591. l_img_comp_src->resno_decoded;
  8592. res_x0 = l_res->x0;
  8593. res_y0 = l_res->y0;
  8594. res_x1 = l_res->x1;
  8595. res_y1 = l_res->y1;
  8596. src_data_stride = (OPJ_UINT32)(
  8597. l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x1 -
  8598. l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0);
  8599. p_src_data = l_tilec->data;
  8600. } else {
  8601. opj_tcd_resolution_t* l_res = l_tilec->resolutions +
  8602. l_img_comp_src->resno_decoded;
  8603. res_x0 = (OPJ_INT32)l_res->win_x0;
  8604. res_y0 = (OPJ_INT32)l_res->win_y0;
  8605. res_x1 = (OPJ_INT32)l_res->win_x1;
  8606. res_y1 = (OPJ_INT32)l_res->win_y1;
  8607. src_data_stride = l_res->win_x1 - l_res->win_x0;
  8608. p_src_data = l_tilec->data_win;
  8609. }
  8610. if (p_src_data == NULL) {
  8611. /* Happens for partial component decoding */
  8612. continue;
  8613. }
  8614. l_width_src = (OPJ_UINT32)(res_x1 - res_x0);
  8615. l_height_src = (OPJ_UINT32)(res_y1 - res_y0);
  8616. /* Current tile component size*/
  8617. /*if (i == 0) {
  8618. fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
  8619. res_x0, res_x1, res_y0, res_y1);
  8620. }*/
  8621. /* Border of the current output component*/
  8622. l_x0_dest = opj_uint_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
  8623. l_y0_dest = opj_uint_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
  8624. l_x1_dest = l_x0_dest +
  8625. l_img_comp_dest->w; /* can't overflow given that image->x1 is uint32 */
  8626. l_y1_dest = l_y0_dest + l_img_comp_dest->h;
  8627. /*if (i == 0) {
  8628. fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
  8629. l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
  8630. }*/
  8631. /*-----*/
  8632. /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
  8633. * of the input buffer (decoded tile component) which will be move
  8634. * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
  8635. * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
  8636. * by this input area.
  8637. * */
  8638. assert(res_x0 >= 0);
  8639. assert(res_x1 >= 0);
  8640. if (l_x0_dest < (OPJ_UINT32)res_x0) {
  8641. l_start_x_dest = (OPJ_UINT32)res_x0 - l_x0_dest;
  8642. l_offset_x0_src = 0;
  8643. if (l_x1_dest >= (OPJ_UINT32)res_x1) {
  8644. l_width_dest = l_width_src;
  8645. l_offset_x1_src = 0;
  8646. } else {
  8647. l_width_dest = l_x1_dest - (OPJ_UINT32)res_x0 ;
  8648. l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest);
  8649. }
  8650. } else {
  8651. l_start_x_dest = 0U;
  8652. l_offset_x0_src = (OPJ_INT32)l_x0_dest - res_x0;
  8653. if (l_x1_dest >= (OPJ_UINT32)res_x1) {
  8654. l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src;
  8655. l_offset_x1_src = 0;
  8656. } else {
  8657. l_width_dest = l_img_comp_dest->w ;
  8658. l_offset_x1_src = res_x1 - (OPJ_INT32)l_x1_dest;
  8659. }
  8660. }
  8661. if (l_y0_dest < (OPJ_UINT32)res_y0) {
  8662. l_start_y_dest = (OPJ_UINT32)res_y0 - l_y0_dest;
  8663. l_offset_y0_src = 0;
  8664. if (l_y1_dest >= (OPJ_UINT32)res_y1) {
  8665. l_height_dest = l_height_src;
  8666. l_offset_y1_src = 0;
  8667. } else {
  8668. l_height_dest = l_y1_dest - (OPJ_UINT32)res_y0 ;
  8669. l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest);
  8670. }
  8671. } else {
  8672. l_start_y_dest = 0U;
  8673. l_offset_y0_src = (OPJ_INT32)l_y0_dest - res_y0;
  8674. if (l_y1_dest >= (OPJ_UINT32)res_y1) {
  8675. l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src;
  8676. l_offset_y1_src = 0;
  8677. } else {
  8678. l_height_dest = l_img_comp_dest->h ;
  8679. l_offset_y1_src = res_y1 - (OPJ_INT32)l_y1_dest;
  8680. }
  8681. }
  8682. if ((l_offset_x0_src < 0) || (l_offset_y0_src < 0) || (l_offset_x1_src < 0) ||
  8683. (l_offset_y1_src < 0)) {
  8684. return OPJ_FALSE;
  8685. }
  8686. /* testcase 2977.pdf.asan.67.2198 */
  8687. if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) {
  8688. return OPJ_FALSE;
  8689. }
  8690. /*-----*/
  8691. /* Compute the input buffer offset */
  8692. l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src
  8693. * (OPJ_SIZE_T)src_data_stride;
  8694. /* Compute the output buffer offset */
  8695. l_start_offset_dest = (OPJ_SIZE_T)l_start_x_dest + (OPJ_SIZE_T)l_start_y_dest
  8696. * (OPJ_SIZE_T)l_img_comp_dest->w;
  8697. /* Allocate output component buffer if necessary */
  8698. if (l_img_comp_dest->data == NULL &&
  8699. l_start_offset_src == 0 && l_start_offset_dest == 0 &&
  8700. src_data_stride == l_img_comp_dest->w &&
  8701. l_width_dest == l_img_comp_dest->w &&
  8702. l_height_dest == l_img_comp_dest->h) {
  8703. /* If the final image matches the tile buffer, then borrow it */
  8704. /* directly to save a copy */
  8705. if (p_tcd->whole_tile_decoding) {
  8706. l_img_comp_dest->data = l_tilec->data;
  8707. l_tilec->data = NULL;
  8708. } else {
  8709. l_img_comp_dest->data = l_tilec->data_win;
  8710. l_tilec->data_win = NULL;
  8711. }
  8712. continue;
  8713. } else if (l_img_comp_dest->data == NULL) {
  8714. OPJ_SIZE_T l_width = l_img_comp_dest->w;
  8715. OPJ_SIZE_T l_height = l_img_comp_dest->h;
  8716. if ((l_height == 0U) || (l_width > (SIZE_MAX / l_height)) ||
  8717. l_width * l_height > SIZE_MAX / sizeof(OPJ_INT32)) {
  8718. /* would overflow */
  8719. return OPJ_FALSE;
  8720. }
  8721. l_img_comp_dest->data = (OPJ_INT32*) opj_image_data_alloc(l_width * l_height *
  8722. sizeof(OPJ_INT32));
  8723. if (! l_img_comp_dest->data) {
  8724. return OPJ_FALSE;
  8725. }
  8726. if (l_img_comp_dest->w != l_width_dest ||
  8727. l_img_comp_dest->h != l_height_dest) {
  8728. memset(l_img_comp_dest->data, 0,
  8729. (OPJ_SIZE_T)l_img_comp_dest->w * l_img_comp_dest->h * sizeof(OPJ_INT32));
  8730. }
  8731. }
  8732. /* Move the output buffer to the first place where we will write*/
  8733. l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
  8734. {
  8735. const OPJ_INT32 * l_src_ptr = p_src_data;
  8736. l_src_ptr += l_start_offset_src;
  8737. for (j = 0; j < l_height_dest; ++j) {
  8738. memcpy(l_dest_ptr, l_src_ptr, l_width_dest * sizeof(OPJ_INT32));
  8739. l_dest_ptr += l_img_comp_dest->w;
  8740. l_src_ptr += src_data_stride;
  8741. }
  8742. }
  8743. }
  8744. return OPJ_TRUE;
  8745. }
  8746. static OPJ_BOOL opj_j2k_update_image_dimensions(opj_image_t* p_image,
  8747. opj_event_mgr_t * p_manager)
  8748. {
  8749. OPJ_UINT32 it_comp;
  8750. OPJ_INT32 l_comp_x1, l_comp_y1;
  8751. opj_image_comp_t* l_img_comp = NULL;
  8752. l_img_comp = p_image->comps;
  8753. for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
  8754. OPJ_INT32 l_h, l_w;
  8755. if (p_image->x0 > (OPJ_UINT32)INT_MAX ||
  8756. p_image->y0 > (OPJ_UINT32)INT_MAX ||
  8757. p_image->x1 > (OPJ_UINT32)INT_MAX ||
  8758. p_image->y1 > (OPJ_UINT32)INT_MAX) {
  8759. opj_event_msg(p_manager, EVT_ERROR,
  8760. "Image coordinates above INT_MAX are not supported\n");
  8761. return OPJ_FALSE;
  8762. }
  8763. l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0,
  8764. (OPJ_INT32)l_img_comp->dx);
  8765. l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0,
  8766. (OPJ_INT32)l_img_comp->dy);
  8767. l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
  8768. l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
  8769. l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor)
  8770. - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor);
  8771. if (l_w < 0) {
  8772. opj_event_msg(p_manager, EVT_ERROR,
  8773. "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
  8774. it_comp, l_w);
  8775. return OPJ_FALSE;
  8776. }
  8777. l_img_comp->w = (OPJ_UINT32)l_w;
  8778. l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor)
  8779. - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor);
  8780. if (l_h < 0) {
  8781. opj_event_msg(p_manager, EVT_ERROR,
  8782. "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
  8783. it_comp, l_h);
  8784. return OPJ_FALSE;
  8785. }
  8786. l_img_comp->h = (OPJ_UINT32)l_h;
  8787. l_img_comp++;
  8788. }
  8789. return OPJ_TRUE;
  8790. }
  8791. OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k,
  8792. OPJ_UINT32 numcomps,
  8793. const OPJ_UINT32* comps_indices,
  8794. opj_event_mgr_t * p_manager)
  8795. {
  8796. OPJ_UINT32 i;
  8797. OPJ_BOOL* already_mapped;
  8798. if (p_j2k->m_private_image == NULL) {
  8799. opj_event_msg(p_manager, EVT_ERROR,
  8800. "opj_read_header() should be called before "
  8801. "opj_set_decoded_components().\n");
  8802. return OPJ_FALSE;
  8803. }
  8804. already_mapped = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
  8805. p_j2k->m_private_image->numcomps);
  8806. if (already_mapped == NULL) {
  8807. return OPJ_FALSE;
  8808. }
  8809. for (i = 0; i < numcomps; i++) {
  8810. if (comps_indices[i] >= p_j2k->m_private_image->numcomps) {
  8811. opj_event_msg(p_manager, EVT_ERROR,
  8812. "Invalid component index: %u\n",
  8813. comps_indices[i]);
  8814. opj_free(already_mapped);
  8815. return OPJ_FALSE;
  8816. }
  8817. if (already_mapped[comps_indices[i]]) {
  8818. opj_event_msg(p_manager, EVT_ERROR,
  8819. "Component index %u used several times\n",
  8820. comps_indices[i]);
  8821. opj_free(already_mapped);
  8822. return OPJ_FALSE;
  8823. }
  8824. already_mapped[comps_indices[i]] = OPJ_TRUE;
  8825. }
  8826. opj_free(already_mapped);
  8827. opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode);
  8828. if (numcomps) {
  8829. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode =
  8830. (OPJ_UINT32*) opj_malloc(numcomps * sizeof(OPJ_UINT32));
  8831. if (p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode == NULL) {
  8832. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0;
  8833. return OPJ_FALSE;
  8834. }
  8835. memcpy(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode,
  8836. comps_indices,
  8837. numcomps * sizeof(OPJ_UINT32));
  8838. } else {
  8839. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = NULL;
  8840. }
  8841. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = numcomps;
  8842. return OPJ_TRUE;
  8843. }
  8844. OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k,
  8845. opj_image_t* p_image,
  8846. OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
  8847. OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
  8848. opj_event_mgr_t * p_manager)
  8849. {
  8850. opj_cp_t * l_cp = &(p_j2k->m_cp);
  8851. opj_image_t * l_image = p_j2k->m_private_image;
  8852. OPJ_BOOL ret;
  8853. OPJ_UINT32 it_comp;
  8854. if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
  8855. p_j2k->m_cp.tcps[0].m_data != NULL) {
  8856. /* In the case of a single-tiled image whose codestream we have already */
  8857. /* ingested, go on */
  8858. }
  8859. /* Check if we are read the main header */
  8860. else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) {
  8861. opj_event_msg(p_manager, EVT_ERROR,
  8862. "Need to decode the main header before begin to decode the remaining codestream.\n");
  8863. return OPJ_FALSE;
  8864. }
  8865. /* Update the comps[].factor member of the output image with the one */
  8866. /* of m_reduce */
  8867. for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
  8868. p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce;
  8869. }
  8870. if (!p_start_x && !p_start_y && !p_end_x && !p_end_y) {
  8871. opj_event_msg(p_manager, EVT_INFO,
  8872. "No decoded area parameters, set the decoded area to the whole image\n");
  8873. p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
  8874. p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
  8875. p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
  8876. p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
  8877. p_image->x0 = l_image->x0;
  8878. p_image->y0 = l_image->y0;
  8879. p_image->x1 = l_image->x1;
  8880. p_image->y1 = l_image->y1;
  8881. return opj_j2k_update_image_dimensions(p_image, p_manager);
  8882. }
  8883. /* ----- */
  8884. /* Check if the positions provided by the user are correct */
  8885. /* Left */
  8886. if (p_start_x < 0) {
  8887. opj_event_msg(p_manager, EVT_ERROR,
  8888. "Left position of the decoded area (region_x0=%d) should be >= 0.\n",
  8889. p_start_x);
  8890. return OPJ_FALSE;
  8891. } else if ((OPJ_UINT32)p_start_x > l_image->x1) {
  8892. opj_event_msg(p_manager, EVT_ERROR,
  8893. "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
  8894. p_start_x, l_image->x1);
  8895. return OPJ_FALSE;
  8896. } else if ((OPJ_UINT32)p_start_x < l_image->x0) {
  8897. opj_event_msg(p_manager, EVT_WARNING,
  8898. "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
  8899. p_start_x, l_image->x0);
  8900. p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
  8901. p_image->x0 = l_image->x0;
  8902. } else {
  8903. p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x -
  8904. l_cp->tx0) / l_cp->tdx;
  8905. p_image->x0 = (OPJ_UINT32)p_start_x;
  8906. }
  8907. /* Up */
  8908. if (p_start_y < 0) {
  8909. opj_event_msg(p_manager, EVT_ERROR,
  8910. "Up position of the decoded area (region_y0=%d) should be >= 0.\n",
  8911. p_start_y);
  8912. return OPJ_FALSE;
  8913. } else if ((OPJ_UINT32)p_start_y > l_image->y1) {
  8914. opj_event_msg(p_manager, EVT_ERROR,
  8915. "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
  8916. p_start_y, l_image->y1);
  8917. return OPJ_FALSE;
  8918. } else if ((OPJ_UINT32)p_start_y < l_image->y0) {
  8919. opj_event_msg(p_manager, EVT_WARNING,
  8920. "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
  8921. p_start_y, l_image->y0);
  8922. p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
  8923. p_image->y0 = l_image->y0;
  8924. } else {
  8925. p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y -
  8926. l_cp->ty0) / l_cp->tdy;
  8927. p_image->y0 = (OPJ_UINT32)p_start_y;
  8928. }
  8929. /* Right */
  8930. if (p_end_x <= 0) {
  8931. opj_event_msg(p_manager, EVT_ERROR,
  8932. "Right position of the decoded area (region_x1=%d) should be > 0.\n",
  8933. p_end_x);
  8934. return OPJ_FALSE;
  8935. } else if ((OPJ_UINT32)p_end_x < l_image->x0) {
  8936. opj_event_msg(p_manager, EVT_ERROR,
  8937. "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
  8938. p_end_x, l_image->x0);
  8939. return OPJ_FALSE;
  8940. } else if ((OPJ_UINT32)p_end_x > l_image->x1) {
  8941. opj_event_msg(p_manager, EVT_WARNING,
  8942. "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
  8943. p_end_x, l_image->x1);
  8944. p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
  8945. p_image->x1 = l_image->x1;
  8946. } else {
  8947. p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(
  8948. p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx);
  8949. p_image->x1 = (OPJ_UINT32)p_end_x;
  8950. }
  8951. /* Bottom */
  8952. if (p_end_y <= 0) {
  8953. opj_event_msg(p_manager, EVT_ERROR,
  8954. "Bottom position of the decoded area (region_y1=%d) should be > 0.\n",
  8955. p_end_y);
  8956. return OPJ_FALSE;
  8957. } else if ((OPJ_UINT32)p_end_y < l_image->y0) {
  8958. opj_event_msg(p_manager, EVT_ERROR,
  8959. "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
  8960. p_end_y, l_image->y0);
  8961. return OPJ_FALSE;
  8962. }
  8963. if ((OPJ_UINT32)p_end_y > l_image->y1) {
  8964. opj_event_msg(p_manager, EVT_WARNING,
  8965. "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
  8966. p_end_y, l_image->y1);
  8967. p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
  8968. p_image->y1 = l_image->y1;
  8969. } else {
  8970. p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(
  8971. p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy);
  8972. p_image->y1 = (OPJ_UINT32)p_end_y;
  8973. }
  8974. /* ----- */
  8975. p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
  8976. ret = opj_j2k_update_image_dimensions(p_image, p_manager);
  8977. if (ret) {
  8978. opj_event_msg(p_manager, EVT_INFO, "Setting decoding area to %d,%d,%d,%d\n",
  8979. p_image->x0, p_image->y0, p_image->x1, p_image->y1);
  8980. }
  8981. return ret;
  8982. }
  8983. opj_j2k_t* opj_j2k_create_decompress(void)
  8984. {
  8985. opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
  8986. if (!l_j2k) {
  8987. return 00;
  8988. }
  8989. l_j2k->m_is_decoder = 1;
  8990. l_j2k->m_cp.m_is_decoder = 1;
  8991. /* in the absence of JP2 boxes, consider different bit depth / sign */
  8992. /* per component is allowed */
  8993. l_j2k->m_cp.allow_different_bit_depth_sign = 1;
  8994. /* Default to using strict mode. */
  8995. l_j2k->m_cp.strict = OPJ_TRUE;
  8996. #ifdef OPJ_DISABLE_TPSOT_FIX
  8997. l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1;
  8998. #endif
  8999. l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1,
  9000. sizeof(opj_tcp_t));
  9001. if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
  9002. opj_j2k_destroy(l_j2k);
  9003. return 00;
  9004. }
  9005. l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1,
  9006. OPJ_J2K_DEFAULT_HEADER_SIZE);
  9007. if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
  9008. opj_j2k_destroy(l_j2k);
  9009. return 00;
  9010. }
  9011. l_j2k->m_specific_param.m_decoder.m_header_data_size =
  9012. OPJ_J2K_DEFAULT_HEADER_SIZE;
  9013. l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
  9014. l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
  9015. /* codestream index creation */
  9016. l_j2k->cstr_index = opj_j2k_create_cstr_index();
  9017. if (!l_j2k->cstr_index) {
  9018. opj_j2k_destroy(l_j2k);
  9019. return 00;
  9020. }
  9021. /* validation list creation */
  9022. l_j2k->m_validation_list = opj_procedure_list_create();
  9023. if (! l_j2k->m_validation_list) {
  9024. opj_j2k_destroy(l_j2k);
  9025. return 00;
  9026. }
  9027. /* execution list creation */
  9028. l_j2k->m_procedure_list = opj_procedure_list_create();
  9029. if (! l_j2k->m_procedure_list) {
  9030. opj_j2k_destroy(l_j2k);
  9031. return 00;
  9032. }
  9033. l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count());
  9034. if (!l_j2k->m_tp) {
  9035. l_j2k->m_tp = opj_thread_pool_create(0);
  9036. }
  9037. if (!l_j2k->m_tp) {
  9038. opj_j2k_destroy(l_j2k);
  9039. return NULL;
  9040. }
  9041. return l_j2k;
  9042. }
  9043. static opj_codestream_index_t* opj_j2k_create_cstr_index(void)
  9044. {
  9045. opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
  9046. opj_calloc(1, sizeof(opj_codestream_index_t));
  9047. if (!cstr_index) {
  9048. return NULL;
  9049. }
  9050. cstr_index->maxmarknum = 100;
  9051. cstr_index->marknum = 0;
  9052. cstr_index->marker = (opj_marker_info_t*)
  9053. opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
  9054. if (!cstr_index-> marker) {
  9055. opj_free(cstr_index);
  9056. return NULL;
  9057. }
  9058. cstr_index->tile_index = NULL;
  9059. return cstr_index;
  9060. }
  9061. static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k,
  9062. OPJ_UINT32 p_tile_no,
  9063. OPJ_UINT32 p_comp_no)
  9064. {
  9065. opj_cp_t *l_cp = 00;
  9066. opj_tcp_t *l_tcp = 00;
  9067. opj_tccp_t *l_tccp = 00;
  9068. /* preconditions */
  9069. assert(p_j2k != 00);
  9070. l_cp = &(p_j2k->m_cp);
  9071. l_tcp = &l_cp->tcps[p_tile_no];
  9072. l_tccp = &l_tcp->tccps[p_comp_no];
  9073. /* preconditions again */
  9074. assert(p_tile_no < (l_cp->tw * l_cp->th));
  9075. assert(p_comp_no < p_j2k->m_private_image->numcomps);
  9076. if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
  9077. return 5 + l_tccp->numresolutions;
  9078. } else {
  9079. return 5;
  9080. }
  9081. }
  9082. static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k,
  9083. OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
  9084. {
  9085. OPJ_UINT32 i;
  9086. opj_cp_t *l_cp = NULL;
  9087. opj_tcp_t *l_tcp = NULL;
  9088. opj_tccp_t *l_tccp0 = NULL;
  9089. opj_tccp_t *l_tccp1 = NULL;
  9090. /* preconditions */
  9091. assert(p_j2k != 00);
  9092. l_cp = &(p_j2k->m_cp);
  9093. l_tcp = &l_cp->tcps[p_tile_no];
  9094. l_tccp0 = &l_tcp->tccps[p_first_comp_no];
  9095. l_tccp1 = &l_tcp->tccps[p_second_comp_no];
  9096. if (l_tccp0->numresolutions != l_tccp1->numresolutions) {
  9097. return OPJ_FALSE;
  9098. }
  9099. if (l_tccp0->cblkw != l_tccp1->cblkw) {
  9100. return OPJ_FALSE;
  9101. }
  9102. if (l_tccp0->cblkh != l_tccp1->cblkh) {
  9103. return OPJ_FALSE;
  9104. }
  9105. if (l_tccp0->cblksty != l_tccp1->cblksty) {
  9106. return OPJ_FALSE;
  9107. }
  9108. if (l_tccp0->qmfbid != l_tccp1->qmfbid) {
  9109. return OPJ_FALSE;
  9110. }
  9111. if ((l_tccp0->csty & J2K_CCP_CSTY_PRT) != (l_tccp1->csty & J2K_CCP_CSTY_PRT)) {
  9112. return OPJ_FALSE;
  9113. }
  9114. for (i = 0U; i < l_tccp0->numresolutions; ++i) {
  9115. if (l_tccp0->prcw[i] != l_tccp1->prcw[i]) {
  9116. return OPJ_FALSE;
  9117. }
  9118. if (l_tccp0->prch[i] != l_tccp1->prch[i]) {
  9119. return OPJ_FALSE;
  9120. }
  9121. }
  9122. return OPJ_TRUE;
  9123. }
  9124. static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k,
  9125. OPJ_UINT32 p_tile_no,
  9126. OPJ_UINT32 p_comp_no,
  9127. OPJ_BYTE * p_data,
  9128. OPJ_UINT32 * p_header_size,
  9129. struct opj_event_mgr * p_manager)
  9130. {
  9131. OPJ_UINT32 i;
  9132. opj_cp_t *l_cp = 00;
  9133. opj_tcp_t *l_tcp = 00;
  9134. opj_tccp_t *l_tccp = 00;
  9135. /* preconditions */
  9136. assert(p_j2k != 00);
  9137. assert(p_header_size != 00);
  9138. assert(p_manager != 00);
  9139. assert(p_data != 00);
  9140. l_cp = &(p_j2k->m_cp);
  9141. l_tcp = &l_cp->tcps[p_tile_no];
  9142. l_tccp = &l_tcp->tccps[p_comp_no];
  9143. /* preconditions again */
  9144. assert(p_tile_no < (l_cp->tw * l_cp->th));
  9145. assert(p_comp_no < (p_j2k->m_private_image->numcomps));
  9146. if (*p_header_size < 5) {
  9147. opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
  9148. return OPJ_FALSE;
  9149. }
  9150. opj_write_bytes(p_data, l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
  9151. ++p_data;
  9152. opj_write_bytes(p_data, l_tccp->cblkw - 2, 1); /* SPcoc (E) */
  9153. ++p_data;
  9154. opj_write_bytes(p_data, l_tccp->cblkh - 2, 1); /* SPcoc (F) */
  9155. ++p_data;
  9156. opj_write_bytes(p_data, l_tccp->cblksty,
  9157. 1); /* SPcoc (G) */
  9158. ++p_data;
  9159. opj_write_bytes(p_data, l_tccp->qmfbid,
  9160. 1); /* SPcoc (H) */
  9161. ++p_data;
  9162. *p_header_size = *p_header_size - 5;
  9163. if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
  9164. if (*p_header_size < l_tccp->numresolutions) {
  9165. opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
  9166. return OPJ_FALSE;
  9167. }
  9168. for (i = 0; i < l_tccp->numresolutions; ++i) {
  9169. opj_write_bytes(p_data, l_tccp->prcw[i] + (l_tccp->prch[i] << 4),
  9170. 1); /* SPcoc (I_i) */
  9171. ++p_data;
  9172. }
  9173. *p_header_size = *p_header_size - l_tccp->numresolutions;
  9174. }
  9175. return OPJ_TRUE;
  9176. }
  9177. static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k,
  9178. OPJ_UINT32 compno,
  9179. OPJ_BYTE * p_header_data,
  9180. OPJ_UINT32 * p_header_size,
  9181. opj_event_mgr_t * p_manager)
  9182. {
  9183. OPJ_UINT32 i, l_tmp;
  9184. opj_cp_t *l_cp = NULL;
  9185. opj_tcp_t *l_tcp = NULL;
  9186. opj_tccp_t *l_tccp = NULL;
  9187. OPJ_BYTE * l_current_ptr = NULL;
  9188. /* preconditions */
  9189. assert(p_j2k != 00);
  9190. assert(p_manager != 00);
  9191. assert(p_header_data != 00);
  9192. l_cp = &(p_j2k->m_cp);
  9193. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
  9194. &l_cp->tcps[p_j2k->m_current_tile_number] :
  9195. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9196. /* precondition again */
  9197. assert(compno < p_j2k->m_private_image->numcomps);
  9198. l_tccp = &l_tcp->tccps[compno];
  9199. l_current_ptr = p_header_data;
  9200. /* make sure room is sufficient */
  9201. if (*p_header_size < 5) {
  9202. opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
  9203. return OPJ_FALSE;
  9204. }
  9205. /* SPcod (D) / SPcoc (A) */
  9206. opj_read_bytes(l_current_ptr, &l_tccp->numresolutions, 1);
  9207. ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
  9208. if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) {
  9209. opj_event_msg(p_manager, EVT_ERROR,
  9210. "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n",
  9211. l_tccp->numresolutions, OPJ_J2K_MAXRLVLS);
  9212. return OPJ_FALSE;
  9213. }
  9214. ++l_current_ptr;
  9215. /* If user wants to remove more resolutions than the codestream contains, return error */
  9216. if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
  9217. opj_event_msg(p_manager, EVT_ERROR,
  9218. "Error decoding component %d.\nThe number of resolutions "
  9219. "to remove (%d) is greater or equal than the number "
  9220. "of resolutions of this component (%d)\nModify the cp_reduce parameter.\n\n",
  9221. compno, l_cp->m_specific_param.m_dec.m_reduce, l_tccp->numresolutions);
  9222. p_j2k->m_specific_param.m_decoder.m_state |=
  9223. 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
  9224. return OPJ_FALSE;
  9225. }
  9226. /* SPcod (E) / SPcoc (B) */
  9227. opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1);
  9228. ++l_current_ptr;
  9229. l_tccp->cblkw += 2;
  9230. /* SPcod (F) / SPcoc (C) */
  9231. opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1);
  9232. ++l_current_ptr;
  9233. l_tccp->cblkh += 2;
  9234. if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) ||
  9235. ((l_tccp->cblkw + l_tccp->cblkh) > 12)) {
  9236. opj_event_msg(p_manager, EVT_ERROR,
  9237. "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n");
  9238. return OPJ_FALSE;
  9239. }
  9240. /* SPcod (G) / SPcoc (D) */
  9241. opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1);
  9242. ++l_current_ptr;
  9243. if ((l_tccp->cblksty & J2K_CCP_CBLKSTY_HTMIXED) != 0) {
  9244. /* We do not support HT mixed mode yet. For conformance, it should be supported.*/
  9245. opj_event_msg(p_manager, EVT_ERROR,
  9246. "Error reading SPCod SPCoc element. Unsupported Mixed HT code-block style found\n");
  9247. return OPJ_FALSE;
  9248. }
  9249. /* SPcod (H) / SPcoc (E) */
  9250. opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1);
  9251. ++l_current_ptr;
  9252. if (l_tccp->qmfbid > 1) {
  9253. opj_event_msg(p_manager, EVT_ERROR,
  9254. "Error reading SPCod SPCoc element, Invalid transformation found\n");
  9255. return OPJ_FALSE;
  9256. }
  9257. *p_header_size = *p_header_size - 5;
  9258. /* use custom precinct size ? */
  9259. if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
  9260. if (*p_header_size < l_tccp->numresolutions) {
  9261. opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
  9262. return OPJ_FALSE;
  9263. }
  9264. /* SPcod (I_i) / SPcoc (F_i) */
  9265. for (i = 0; i < l_tccp->numresolutions; ++i) {
  9266. opj_read_bytes(l_current_ptr, &l_tmp, 1);
  9267. ++l_current_ptr;
  9268. /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */
  9269. if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) {
  9270. opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n");
  9271. return OPJ_FALSE;
  9272. }
  9273. l_tccp->prcw[i] = l_tmp & 0xf;
  9274. l_tccp->prch[i] = l_tmp >> 4;
  9275. }
  9276. *p_header_size = *p_header_size - l_tccp->numresolutions;
  9277. } else {
  9278. /* set default size for the precinct width and height */
  9279. for (i = 0; i < l_tccp->numresolutions; ++i) {
  9280. l_tccp->prcw[i] = 15;
  9281. l_tccp->prch[i] = 15;
  9282. }
  9283. }
  9284. #ifdef WIP_REMOVE_MSD
  9285. /* INDEX >> */
  9286. if (p_j2k->cstr_info && compno == 0) {
  9287. OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
  9288. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh =
  9289. l_tccp->cblkh;
  9290. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw =
  9291. l_tccp->cblkw;
  9292. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions
  9293. = l_tccp->numresolutions;
  9294. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty =
  9295. l_tccp->cblksty;
  9296. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid =
  9297. l_tccp->qmfbid;
  9298. memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx, l_tccp->prcw,
  9299. l_data_size);
  9300. memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy, l_tccp->prch,
  9301. l_data_size);
  9302. }
  9303. /* << INDEX */
  9304. #endif
  9305. return OPJ_TRUE;
  9306. }
  9307. static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k)
  9308. {
  9309. /* loop */
  9310. OPJ_UINT32 i;
  9311. opj_cp_t *l_cp = NULL;
  9312. opj_tcp_t *l_tcp = NULL;
  9313. opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
  9314. OPJ_UINT32 l_prc_size;
  9315. /* preconditions */
  9316. assert(p_j2k != 00);
  9317. l_cp = &(p_j2k->m_cp);
  9318. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
  9319. ?
  9320. &l_cp->tcps[p_j2k->m_current_tile_number] :
  9321. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9322. l_ref_tccp = &l_tcp->tccps[0];
  9323. l_copied_tccp = l_ref_tccp + 1;
  9324. l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32);
  9325. for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) {
  9326. l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
  9327. l_copied_tccp->cblkw = l_ref_tccp->cblkw;
  9328. l_copied_tccp->cblkh = l_ref_tccp->cblkh;
  9329. l_copied_tccp->cblksty = l_ref_tccp->cblksty;
  9330. l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
  9331. memcpy(l_copied_tccp->prcw, l_ref_tccp->prcw, l_prc_size);
  9332. memcpy(l_copied_tccp->prch, l_ref_tccp->prch, l_prc_size);
  9333. ++l_copied_tccp;
  9334. }
  9335. }
  9336. static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k,
  9337. OPJ_UINT32 p_tile_no,
  9338. OPJ_UINT32 p_comp_no)
  9339. {
  9340. OPJ_UINT32 l_num_bands;
  9341. opj_cp_t *l_cp = 00;
  9342. opj_tcp_t *l_tcp = 00;
  9343. opj_tccp_t *l_tccp = 00;
  9344. /* preconditions */
  9345. assert(p_j2k != 00);
  9346. l_cp = &(p_j2k->m_cp);
  9347. l_tcp = &l_cp->tcps[p_tile_no];
  9348. l_tccp = &l_tcp->tccps[p_comp_no];
  9349. /* preconditions again */
  9350. assert(p_tile_no < l_cp->tw * l_cp->th);
  9351. assert(p_comp_no < p_j2k->m_private_image->numcomps);
  9352. l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
  9353. (l_tccp->numresolutions * 3 - 2);
  9354. if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  9355. return 1 + l_num_bands;
  9356. } else {
  9357. return 1 + 2 * l_num_bands;
  9358. }
  9359. }
  9360. static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k,
  9361. OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
  9362. {
  9363. opj_cp_t *l_cp = NULL;
  9364. opj_tcp_t *l_tcp = NULL;
  9365. opj_tccp_t *l_tccp0 = NULL;
  9366. opj_tccp_t *l_tccp1 = NULL;
  9367. OPJ_UINT32 l_band_no, l_num_bands;
  9368. /* preconditions */
  9369. assert(p_j2k != 00);
  9370. l_cp = &(p_j2k->m_cp);
  9371. l_tcp = &l_cp->tcps[p_tile_no];
  9372. l_tccp0 = &l_tcp->tccps[p_first_comp_no];
  9373. l_tccp1 = &l_tcp->tccps[p_second_comp_no];
  9374. if (l_tccp0->qntsty != l_tccp1->qntsty) {
  9375. return OPJ_FALSE;
  9376. }
  9377. if (l_tccp0->numgbits != l_tccp1->numgbits) {
  9378. return OPJ_FALSE;
  9379. }
  9380. if (l_tccp0->qntsty == J2K_CCP_QNTSTY_SIQNT) {
  9381. l_num_bands = 1U;
  9382. } else {
  9383. l_num_bands = l_tccp0->numresolutions * 3U - 2U;
  9384. if (l_num_bands != (l_tccp1->numresolutions * 3U - 2U)) {
  9385. return OPJ_FALSE;
  9386. }
  9387. }
  9388. for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
  9389. if (l_tccp0->stepsizes[l_band_no].expn != l_tccp1->stepsizes[l_band_no].expn) {
  9390. return OPJ_FALSE;
  9391. }
  9392. }
  9393. if (l_tccp0->qntsty != J2K_CCP_QNTSTY_NOQNT) {
  9394. for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
  9395. if (l_tccp0->stepsizes[l_band_no].mant != l_tccp1->stepsizes[l_band_no].mant) {
  9396. return OPJ_FALSE;
  9397. }
  9398. }
  9399. }
  9400. return OPJ_TRUE;
  9401. }
  9402. static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
  9403. OPJ_UINT32 p_tile_no,
  9404. OPJ_UINT32 p_comp_no,
  9405. OPJ_BYTE * p_data,
  9406. OPJ_UINT32 * p_header_size,
  9407. struct opj_event_mgr * p_manager)
  9408. {
  9409. OPJ_UINT32 l_header_size;
  9410. OPJ_UINT32 l_band_no, l_num_bands;
  9411. OPJ_UINT32 l_expn, l_mant;
  9412. opj_cp_t *l_cp = 00;
  9413. opj_tcp_t *l_tcp = 00;
  9414. opj_tccp_t *l_tccp = 00;
  9415. /* preconditions */
  9416. assert(p_j2k != 00);
  9417. assert(p_header_size != 00);
  9418. assert(p_manager != 00);
  9419. assert(p_data != 00);
  9420. l_cp = &(p_j2k->m_cp);
  9421. l_tcp = &l_cp->tcps[p_tile_no];
  9422. l_tccp = &l_tcp->tccps[p_comp_no];
  9423. /* preconditions again */
  9424. assert(p_tile_no < l_cp->tw * l_cp->th);
  9425. assert(p_comp_no < p_j2k->m_private_image->numcomps);
  9426. l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
  9427. (l_tccp->numresolutions * 3 - 2);
  9428. if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  9429. l_header_size = 1 + l_num_bands;
  9430. if (*p_header_size < l_header_size) {
  9431. opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
  9432. return OPJ_FALSE;
  9433. }
  9434. opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5),
  9435. 1); /* Sqcx */
  9436. ++p_data;
  9437. for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
  9438. l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
  9439. opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
  9440. ++p_data;
  9441. }
  9442. } else {
  9443. l_header_size = 1 + 2 * l_num_bands;
  9444. if (*p_header_size < l_header_size) {
  9445. opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
  9446. return OPJ_FALSE;
  9447. }
  9448. opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5),
  9449. 1); /* Sqcx */
  9450. ++p_data;
  9451. for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
  9452. l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
  9453. l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant;
  9454. opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
  9455. p_data += 2;
  9456. }
  9457. }
  9458. *p_header_size = *p_header_size - l_header_size;
  9459. return OPJ_TRUE;
  9460. }
  9461. static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
  9462. OPJ_UINT32 p_comp_no,
  9463. OPJ_BYTE* p_header_data,
  9464. OPJ_UINT32 * p_header_size,
  9465. opj_event_mgr_t * p_manager
  9466. )
  9467. {
  9468. /* loop*/
  9469. OPJ_UINT32 l_band_no;
  9470. opj_cp_t *l_cp = 00;
  9471. opj_tcp_t *l_tcp = 00;
  9472. opj_tccp_t *l_tccp = 00;
  9473. OPJ_BYTE * l_current_ptr = 00;
  9474. OPJ_UINT32 l_tmp, l_num_band;
  9475. /* preconditions*/
  9476. assert(p_j2k != 00);
  9477. assert(p_manager != 00);
  9478. assert(p_header_data != 00);
  9479. l_cp = &(p_j2k->m_cp);
  9480. /* come from tile part header or main header ?*/
  9481. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
  9482. ?
  9483. &l_cp->tcps[p_j2k->m_current_tile_number] :
  9484. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9485. /* precondition again*/
  9486. assert(p_comp_no < p_j2k->m_private_image->numcomps);
  9487. l_tccp = &l_tcp->tccps[p_comp_no];
  9488. l_current_ptr = p_header_data;
  9489. if (*p_header_size < 1) {
  9490. opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
  9491. return OPJ_FALSE;
  9492. }
  9493. *p_header_size -= 1;
  9494. opj_read_bytes(l_current_ptr, &l_tmp, 1); /* Sqcx */
  9495. ++l_current_ptr;
  9496. l_tccp->qntsty = l_tmp & 0x1f;
  9497. l_tccp->numgbits = l_tmp >> 5;
  9498. if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
  9499. l_num_band = 1;
  9500. } else {
  9501. l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
  9502. (*p_header_size) :
  9503. (*p_header_size) / 2;
  9504. if (l_num_band > OPJ_J2K_MAXBANDS) {
  9505. opj_event_msg(p_manager, EVT_WARNING,
  9506. "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
  9507. "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to "
  9508. "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS,
  9509. OPJ_J2K_MAXBANDS);
  9510. /*return OPJ_FALSE;*/
  9511. }
  9512. }
  9513. #ifdef USE_JPWL
  9514. if (l_cp->correct) {
  9515. /* if JPWL is on, we check whether there are too many subbands */
  9516. if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) {
  9517. opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  9518. "JPWL: bad number of subbands in Sqcx (%d)\n",
  9519. l_num_band);
  9520. if (!JPWL_ASSUME) {
  9521. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  9522. return OPJ_FALSE;
  9523. }
  9524. /* we try to correct */
  9525. l_num_band = 1;
  9526. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"
  9527. "- setting number of bands to %d => HYPOTHESIS!!!\n",
  9528. l_num_band);
  9529. };
  9530. };
  9531. #endif /* USE_JPWL */
  9532. if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  9533. for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
  9534. opj_read_bytes(l_current_ptr, &l_tmp, 1); /* SPqcx_i */
  9535. ++l_current_ptr;
  9536. if (l_band_no < OPJ_J2K_MAXBANDS) {
  9537. l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3);
  9538. l_tccp->stepsizes[l_band_no].mant = 0;
  9539. }
  9540. }
  9541. *p_header_size = *p_header_size - l_num_band;
  9542. } else {
  9543. for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
  9544. opj_read_bytes(l_current_ptr, &l_tmp, 2); /* SPqcx_i */
  9545. l_current_ptr += 2;
  9546. if (l_band_no < OPJ_J2K_MAXBANDS) {
  9547. l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11);
  9548. l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
  9549. }
  9550. }
  9551. *p_header_size = *p_header_size - 2 * l_num_band;
  9552. }
  9553. /* Add Antonin : if scalar_derived -> compute other stepsizes */
  9554. if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
  9555. for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) {
  9556. l_tccp->stepsizes[l_band_no].expn =
  9557. ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0)
  9558. ?
  9559. (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0;
  9560. l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
  9561. }
  9562. }
  9563. return OPJ_TRUE;
  9564. }
  9565. static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k)
  9566. {
  9567. OPJ_UINT32 i;
  9568. opj_cp_t *l_cp = NULL;
  9569. opj_tcp_t *l_tcp = NULL;
  9570. opj_tccp_t *l_ref_tccp = NULL;
  9571. opj_tccp_t *l_copied_tccp = NULL;
  9572. OPJ_UINT32 l_size;
  9573. /* preconditions */
  9574. assert(p_j2k != 00);
  9575. l_cp = &(p_j2k->m_cp);
  9576. l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
  9577. &l_cp->tcps[p_j2k->m_current_tile_number] :
  9578. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9579. l_ref_tccp = &l_tcp->tccps[0];
  9580. l_copied_tccp = l_ref_tccp + 1;
  9581. l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t);
  9582. for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) {
  9583. l_copied_tccp->qntsty = l_ref_tccp->qntsty;
  9584. l_copied_tccp->numgbits = l_ref_tccp->numgbits;
  9585. memcpy(l_copied_tccp->stepsizes, l_ref_tccp->stepsizes, l_size);
  9586. ++l_copied_tccp;
  9587. }
  9588. }
  9589. static void opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile,
  9590. OPJ_INT32 numcomps, FILE* out_stream)
  9591. {
  9592. if (l_default_tile) {
  9593. OPJ_INT32 compno;
  9594. fprintf(out_stream, "\t default tile {\n");
  9595. fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
  9596. fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
  9597. fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
  9598. fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
  9599. for (compno = 0; compno < numcomps; compno++) {
  9600. opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
  9601. OPJ_UINT32 resno;
  9602. OPJ_INT32 bandno, numbands;
  9603. /* coding style*/
  9604. fprintf(out_stream, "\t\t comp %d {\n", compno);
  9605. fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
  9606. fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
  9607. fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
  9608. fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
  9609. fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
  9610. fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
  9611. fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
  9612. for (resno = 0; resno < l_tccp->numresolutions; resno++) {
  9613. fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
  9614. }
  9615. fprintf(out_stream, "\n");
  9616. /* quantization style*/
  9617. fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
  9618. fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
  9619. fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
  9620. numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
  9621. (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
  9622. for (bandno = 0; bandno < numbands; bandno++) {
  9623. fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
  9624. l_tccp->stepsizes[bandno].expn);
  9625. }
  9626. fprintf(out_stream, "\n");
  9627. /* RGN value*/
  9628. fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
  9629. fprintf(out_stream, "\t\t }\n");
  9630. } /*end of component of default tile*/
  9631. fprintf(out_stream, "\t }\n"); /*end of default tile*/
  9632. }
  9633. }
  9634. void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
  9635. {
  9636. /* Check if the flag is compatible with j2k file*/
  9637. if ((flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)) {
  9638. fprintf(out_stream, "Wrong flag\n");
  9639. return;
  9640. }
  9641. /* Dump the image_header */
  9642. if (flag & OPJ_IMG_INFO) {
  9643. if (p_j2k->m_private_image) {
  9644. j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
  9645. }
  9646. }
  9647. /* Dump the codestream info from main header */
  9648. if (flag & OPJ_J2K_MH_INFO) {
  9649. if (p_j2k->m_private_image) {
  9650. opj_j2k_dump_MH_info(p_j2k, out_stream);
  9651. }
  9652. }
  9653. /* Dump all tile/codestream info */
  9654. if (flag & OPJ_J2K_TCH_INFO) {
  9655. OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  9656. OPJ_UINT32 i;
  9657. opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
  9658. if (p_j2k->m_private_image) {
  9659. for (i = 0; i < l_nb_tiles; ++i) {
  9660. opj_j2k_dump_tile_info(l_tcp, (OPJ_INT32)p_j2k->m_private_image->numcomps,
  9661. out_stream);
  9662. ++l_tcp;
  9663. }
  9664. }
  9665. }
  9666. /* Dump the codestream info of the current tile */
  9667. if (flag & OPJ_J2K_TH_INFO) {
  9668. }
  9669. /* Dump the codestream index from main header */
  9670. if (flag & OPJ_J2K_MH_IND) {
  9671. opj_j2k_dump_MH_index(p_j2k, out_stream);
  9672. }
  9673. /* Dump the codestream index of the current tile */
  9674. if (flag & OPJ_J2K_TH_IND) {
  9675. }
  9676. }
  9677. static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream)
  9678. {
  9679. opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
  9680. OPJ_UINT32 it_marker, it_tile, it_tile_part;
  9681. fprintf(out_stream, "Codestream index from main header: {\n");
  9682. fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
  9683. "\t Main header end position=%" PRIi64 "\n",
  9684. cstr_index->main_head_start, cstr_index->main_head_end);
  9685. fprintf(out_stream, "\t Marker list: {\n");
  9686. if (cstr_index->marker) {
  9687. for (it_marker = 0; it_marker < cstr_index->marknum ; it_marker++) {
  9688. fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
  9689. cstr_index->marker[it_marker].type,
  9690. cstr_index->marker[it_marker].pos,
  9691. cstr_index->marker[it_marker].len);
  9692. }
  9693. }
  9694. fprintf(out_stream, "\t }\n");
  9695. if (cstr_index->tile_index) {
  9696. /* Simple test to avoid to write empty information*/
  9697. OPJ_UINT32 l_acc_nb_of_tile_part = 0;
  9698. for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) {
  9699. l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
  9700. }
  9701. if (l_acc_nb_of_tile_part) {
  9702. fprintf(out_stream, "\t Tile index: {\n");
  9703. for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) {
  9704. OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
  9705. fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile,
  9706. nb_of_tile_part);
  9707. if (cstr_index->tile_index[it_tile].tp_index) {
  9708. for (it_tile_part = 0; it_tile_part < nb_of_tile_part; it_tile_part++) {
  9709. fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%"
  9710. PRIi64 ", end_pos=%" PRIi64 ".\n",
  9711. it_tile_part,
  9712. cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
  9713. cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
  9714. cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
  9715. }
  9716. }
  9717. if (cstr_index->tile_index[it_tile].marker) {
  9718. for (it_marker = 0; it_marker < cstr_index->tile_index[it_tile].marknum ;
  9719. it_marker++) {
  9720. fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
  9721. cstr_index->tile_index[it_tile].marker[it_marker].type,
  9722. cstr_index->tile_index[it_tile].marker[it_marker].pos,
  9723. cstr_index->tile_index[it_tile].marker[it_marker].len);
  9724. }
  9725. }
  9726. }
  9727. fprintf(out_stream, "\t }\n");
  9728. }
  9729. }
  9730. fprintf(out_stream, "}\n");
  9731. }
  9732. static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream)
  9733. {
  9734. fprintf(out_stream, "Codestream info from main header: {\n");
  9735. fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
  9736. fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
  9737. fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
  9738. opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp,
  9739. (OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
  9740. fprintf(out_stream, "}\n");
  9741. }
  9742. void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag,
  9743. FILE* out_stream)
  9744. {
  9745. char tab[2];
  9746. if (dev_dump_flag) {
  9747. fprintf(stdout, "[DEV] Dump an image_header struct {\n");
  9748. tab[0] = '\0';
  9749. } else {
  9750. fprintf(out_stream, "Image info {\n");
  9751. tab[0] = '\t';
  9752. tab[1] = '\0';
  9753. }
  9754. fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
  9755. fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1,
  9756. img_header->y1);
  9757. fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
  9758. if (img_header->comps) {
  9759. OPJ_UINT32 compno;
  9760. for (compno = 0; compno < img_header->numcomps; compno++) {
  9761. fprintf(out_stream, "%s\t component %d {\n", tab, compno);
  9762. j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag,
  9763. out_stream);
  9764. fprintf(out_stream, "%s}\n", tab);
  9765. }
  9766. }
  9767. fprintf(out_stream, "}\n");
  9768. }
  9769. void j2k_dump_image_comp_header(opj_image_comp_t* comp_header,
  9770. OPJ_BOOL dev_dump_flag, FILE* out_stream)
  9771. {
  9772. char tab[3];
  9773. if (dev_dump_flag) {
  9774. fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
  9775. tab[0] = '\0';
  9776. } else {
  9777. tab[0] = '\t';
  9778. tab[1] = '\t';
  9779. tab[2] = '\0';
  9780. }
  9781. fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
  9782. fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
  9783. fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
  9784. if (dev_dump_flag) {
  9785. fprintf(out_stream, "}\n");
  9786. }
  9787. }
  9788. opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k)
  9789. {
  9790. OPJ_UINT32 compno;
  9791. OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps;
  9792. opj_tcp_t *l_default_tile;
  9793. opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,
  9794. sizeof(opj_codestream_info_v2_t));
  9795. if (!cstr_info) {
  9796. return NULL;
  9797. }
  9798. cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
  9799. cstr_info->tx0 = p_j2k->m_cp.tx0;
  9800. cstr_info->ty0 = p_j2k->m_cp.ty0;
  9801. cstr_info->tdx = p_j2k->m_cp.tdx;
  9802. cstr_info->tdy = p_j2k->m_cp.tdy;
  9803. cstr_info->tw = p_j2k->m_cp.tw;
  9804. cstr_info->th = p_j2k->m_cp.th;
  9805. cstr_info->tile_info = NULL; /* Not fill from the main header*/
  9806. l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9807. cstr_info->m_default_tile_info.csty = l_default_tile->csty;
  9808. cstr_info->m_default_tile_info.prg = l_default_tile->prg;
  9809. cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
  9810. cstr_info->m_default_tile_info.mct = l_default_tile->mct;
  9811. cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(
  9812. cstr_info->nbcomps, sizeof(opj_tccp_info_t));
  9813. if (!cstr_info->m_default_tile_info.tccp_info) {
  9814. opj_destroy_cstr_info(&cstr_info);
  9815. return NULL;
  9816. }
  9817. for (compno = 0; compno < numcomps; compno++) {
  9818. opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
  9819. opj_tccp_info_t *l_tccp_info = &
  9820. (cstr_info->m_default_tile_info.tccp_info[compno]);
  9821. OPJ_INT32 bandno, numbands;
  9822. /* coding style*/
  9823. l_tccp_info->csty = l_tccp->csty;
  9824. l_tccp_info->numresolutions = l_tccp->numresolutions;
  9825. l_tccp_info->cblkw = l_tccp->cblkw;
  9826. l_tccp_info->cblkh = l_tccp->cblkh;
  9827. l_tccp_info->cblksty = l_tccp->cblksty;
  9828. l_tccp_info->qmfbid = l_tccp->qmfbid;
  9829. if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS) {
  9830. memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
  9831. memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
  9832. }
  9833. /* quantization style*/
  9834. l_tccp_info->qntsty = l_tccp->qntsty;
  9835. l_tccp_info->numgbits = l_tccp->numgbits;
  9836. numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
  9837. (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
  9838. if (numbands < OPJ_J2K_MAXBANDS) {
  9839. for (bandno = 0; bandno < numbands; bandno++) {
  9840. l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32)
  9841. l_tccp->stepsizes[bandno].mant;
  9842. l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32)
  9843. l_tccp->stepsizes[bandno].expn;
  9844. }
  9845. }
  9846. /* RGN value*/
  9847. l_tccp_info->roishift = l_tccp->roishift;
  9848. }
  9849. return cstr_info;
  9850. }
  9851. opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k)
  9852. {
  9853. opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
  9854. opj_calloc(1, sizeof(opj_codestream_index_t));
  9855. if (!l_cstr_index) {
  9856. return NULL;
  9857. }
  9858. l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
  9859. l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
  9860. l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
  9861. l_cstr_index->marknum = p_j2k->cstr_index->marknum;
  9862. l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum *
  9863. sizeof(opj_marker_info_t));
  9864. if (!l_cstr_index->marker) {
  9865. opj_free(l_cstr_index);
  9866. return NULL;
  9867. }
  9868. if (p_j2k->cstr_index->marker) {
  9869. memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker,
  9870. l_cstr_index->marknum * sizeof(opj_marker_info_t));
  9871. } else {
  9872. opj_free(l_cstr_index->marker);
  9873. l_cstr_index->marker = NULL;
  9874. }
  9875. l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
  9876. l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(
  9877. l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
  9878. if (!l_cstr_index->tile_index) {
  9879. opj_free(l_cstr_index->marker);
  9880. opj_free(l_cstr_index);
  9881. return NULL;
  9882. }
  9883. if (!p_j2k->cstr_index->tile_index) {
  9884. opj_free(l_cstr_index->tile_index);
  9885. l_cstr_index->tile_index = NULL;
  9886. } else {
  9887. OPJ_UINT32 it_tile = 0;
  9888. for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++) {
  9889. /* Tile Marker*/
  9890. l_cstr_index->tile_index[it_tile].marknum =
  9891. p_j2k->cstr_index->tile_index[it_tile].marknum;
  9892. l_cstr_index->tile_index[it_tile].marker =
  9893. (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum *
  9894. sizeof(opj_marker_info_t));
  9895. if (!l_cstr_index->tile_index[it_tile].marker) {
  9896. OPJ_UINT32 it_tile_free;
  9897. for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) {
  9898. opj_free(l_cstr_index->tile_index[it_tile_free].marker);
  9899. }
  9900. opj_free(l_cstr_index->tile_index);
  9901. opj_free(l_cstr_index->marker);
  9902. opj_free(l_cstr_index);
  9903. return NULL;
  9904. }
  9905. if (p_j2k->cstr_index->tile_index[it_tile].marker)
  9906. memcpy(l_cstr_index->tile_index[it_tile].marker,
  9907. p_j2k->cstr_index->tile_index[it_tile].marker,
  9908. l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t));
  9909. else {
  9910. opj_free(l_cstr_index->tile_index[it_tile].marker);
  9911. l_cstr_index->tile_index[it_tile].marker = NULL;
  9912. }
  9913. /* Tile part index*/
  9914. l_cstr_index->tile_index[it_tile].nb_tps =
  9915. p_j2k->cstr_index->tile_index[it_tile].nb_tps;
  9916. l_cstr_index->tile_index[it_tile].tp_index =
  9917. (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps * sizeof(
  9918. opj_tp_index_t));
  9919. if (!l_cstr_index->tile_index[it_tile].tp_index) {
  9920. OPJ_UINT32 it_tile_free;
  9921. for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) {
  9922. opj_free(l_cstr_index->tile_index[it_tile_free].marker);
  9923. opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
  9924. }
  9925. opj_free(l_cstr_index->tile_index);
  9926. opj_free(l_cstr_index->marker);
  9927. opj_free(l_cstr_index);
  9928. return NULL;
  9929. }
  9930. if (p_j2k->cstr_index->tile_index[it_tile].tp_index) {
  9931. memcpy(l_cstr_index->tile_index[it_tile].tp_index,
  9932. p_j2k->cstr_index->tile_index[it_tile].tp_index,
  9933. l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t));
  9934. } else {
  9935. opj_free(l_cstr_index->tile_index[it_tile].tp_index);
  9936. l_cstr_index->tile_index[it_tile].tp_index = NULL;
  9937. }
  9938. /* Packet index (NOT USED)*/
  9939. l_cstr_index->tile_index[it_tile].nb_packet = 0;
  9940. l_cstr_index->tile_index[it_tile].packet_index = NULL;
  9941. }
  9942. }
  9943. return l_cstr_index;
  9944. }
  9945. static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
  9946. {
  9947. OPJ_UINT32 it_tile = 0;
  9948. p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
  9949. p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(
  9950. p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
  9951. if (!p_j2k->cstr_index->tile_index) {
  9952. return OPJ_FALSE;
  9953. }
  9954. for (it_tile = 0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++) {
  9955. p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
  9956. p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
  9957. p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
  9958. opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum,
  9959. sizeof(opj_marker_info_t));
  9960. if (!p_j2k->cstr_index->tile_index[it_tile].marker) {
  9961. return OPJ_FALSE;
  9962. }
  9963. }
  9964. return OPJ_TRUE;
  9965. }
  9966. static OPJ_BOOL opj_j2k_are_all_used_components_decoded(opj_j2k_t *p_j2k,
  9967. opj_event_mgr_t * p_manager)
  9968. {
  9969. OPJ_UINT32 compno;
  9970. OPJ_BOOL decoded_all_used_components = OPJ_TRUE;
  9971. if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
  9972. for (compno = 0;
  9973. compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) {
  9974. OPJ_UINT32 dec_compno =
  9975. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno];
  9976. if (p_j2k->m_output_image->comps[dec_compno].data == NULL) {
  9977. opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n",
  9978. dec_compno);
  9979. decoded_all_used_components = OPJ_FALSE;
  9980. }
  9981. }
  9982. } else {
  9983. for (compno = 0; compno < p_j2k->m_output_image->numcomps; compno++) {
  9984. if (p_j2k->m_output_image->comps[compno].data == NULL) {
  9985. opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n",
  9986. compno);
  9987. decoded_all_used_components = OPJ_FALSE;
  9988. }
  9989. }
  9990. }
  9991. if (decoded_all_used_components == OPJ_FALSE) {
  9992. opj_event_msg(p_manager, EVT_ERROR, "Failed to decode all used components\n");
  9993. return OPJ_FALSE;
  9994. }
  9995. return OPJ_TRUE;
  9996. }
  9997. static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k,
  9998. opj_stream_private_t *p_stream,
  9999. opj_event_mgr_t * p_manager)
  10000. {
  10001. OPJ_BOOL l_go_on = OPJ_TRUE;
  10002. OPJ_UINT32 l_current_tile_no;
  10003. OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1;
  10004. OPJ_UINT32 l_nb_comps;
  10005. OPJ_UINT32 nr_tiles = 0;
  10006. /* Particular case for whole single tile decoding */
  10007. /* We can avoid allocating intermediate tile buffers */
  10008. if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
  10009. p_j2k->m_cp.tx0 == 0 && p_j2k->m_cp.ty0 == 0 &&
  10010. p_j2k->m_output_image->x0 == 0 &&
  10011. p_j2k->m_output_image->y0 == 0 &&
  10012. p_j2k->m_output_image->x1 == p_j2k->m_cp.tdx &&
  10013. p_j2k->m_output_image->y1 == p_j2k->m_cp.tdy) {
  10014. OPJ_UINT32 i;
  10015. if (! opj_j2k_read_tile_header(p_j2k,
  10016. &l_current_tile_no,
  10017. NULL,
  10018. &l_tile_x0, &l_tile_y0,
  10019. &l_tile_x1, &l_tile_y1,
  10020. &l_nb_comps,
  10021. &l_go_on,
  10022. p_stream,
  10023. p_manager)) {
  10024. return OPJ_FALSE;
  10025. }
  10026. if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0,
  10027. p_stream, p_manager)) {
  10028. opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile 1/1\n");
  10029. return OPJ_FALSE;
  10030. }
  10031. /* Transfer TCD data to output image data */
  10032. for (i = 0; i < p_j2k->m_output_image->numcomps; i++) {
  10033. opj_image_data_free(p_j2k->m_output_image->comps[i].data);
  10034. p_j2k->m_output_image->comps[i].data =
  10035. p_j2k->m_tcd->tcd_image->tiles->comps[i].data;
  10036. p_j2k->m_output_image->comps[i].resno_decoded =
  10037. p_j2k->m_tcd->image->comps[i].resno_decoded;
  10038. p_j2k->m_tcd->tcd_image->tiles->comps[i].data = NULL;
  10039. }
  10040. return OPJ_TRUE;
  10041. }
  10042. for (;;) {
  10043. if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
  10044. p_j2k->m_cp.tcps[0].m_data != NULL) {
  10045. l_current_tile_no = 0;
  10046. p_j2k->m_current_tile_number = 0;
  10047. p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA;
  10048. } else {
  10049. if (! opj_j2k_read_tile_header(p_j2k,
  10050. &l_current_tile_no,
  10051. NULL,
  10052. &l_tile_x0, &l_tile_y0,
  10053. &l_tile_x1, &l_tile_y1,
  10054. &l_nb_comps,
  10055. &l_go_on,
  10056. p_stream,
  10057. p_manager)) {
  10058. return OPJ_FALSE;
  10059. }
  10060. if (! l_go_on) {
  10061. break;
  10062. }
  10063. }
  10064. if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0,
  10065. p_stream, p_manager)) {
  10066. opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n",
  10067. l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
  10068. return OPJ_FALSE;
  10069. }
  10070. opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n",
  10071. l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
  10072. if (! opj_j2k_update_image_data(p_j2k->m_tcd,
  10073. p_j2k->m_output_image)) {
  10074. return OPJ_FALSE;
  10075. }
  10076. if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
  10077. !(p_j2k->m_output_image->x0 == p_j2k->m_private_image->x0 &&
  10078. p_j2k->m_output_image->y0 == p_j2k->m_private_image->y0 &&
  10079. p_j2k->m_output_image->x1 == p_j2k->m_private_image->x1 &&
  10080. p_j2k->m_output_image->y1 == p_j2k->m_private_image->y1)) {
  10081. /* Keep current tcp data */
  10082. } else {
  10083. opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]);
  10084. }
  10085. opj_event_msg(p_manager, EVT_INFO,
  10086. "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
  10087. if (opj_stream_get_number_byte_left(p_stream) == 0
  10088. && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) {
  10089. break;
  10090. }
  10091. if (++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw) {
  10092. break;
  10093. }
  10094. }
  10095. if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) {
  10096. return OPJ_FALSE;
  10097. }
  10098. return OPJ_TRUE;
  10099. }
  10100. /**
  10101. * Sets up the procedures to do on decoding data. Developers wanting to extend the library can add their own reading procedures.
  10102. */
  10103. static OPJ_BOOL opj_j2k_setup_decoding(opj_j2k_t *p_j2k,
  10104. opj_event_mgr_t * p_manager)
  10105. {
  10106. /* preconditions*/
  10107. assert(p_j2k != 00);
  10108. assert(p_manager != 00);
  10109. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10110. (opj_procedure)opj_j2k_decode_tiles, p_manager)) {
  10111. return OPJ_FALSE;
  10112. }
  10113. /* DEVELOPER CORNER, add your custom procedures */
  10114. return OPJ_TRUE;
  10115. }
  10116. /*
  10117. * Read and decode one tile.
  10118. */
  10119. static OPJ_BOOL opj_j2k_decode_one_tile(opj_j2k_t *p_j2k,
  10120. opj_stream_private_t *p_stream,
  10121. opj_event_mgr_t * p_manager)
  10122. {
  10123. OPJ_BOOL l_go_on = OPJ_TRUE;
  10124. OPJ_UINT32 l_current_tile_no;
  10125. OPJ_UINT32 l_tile_no_to_dec;
  10126. OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1;
  10127. OPJ_UINT32 l_nb_comps;
  10128. OPJ_UINT32 l_nb_tiles;
  10129. OPJ_UINT32 i;
  10130. /*Allocate and initialize some elements of codestrem index if not already done*/
  10131. if (!p_j2k->cstr_index->tile_index) {
  10132. if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) {
  10133. return OPJ_FALSE;
  10134. }
  10135. }
  10136. /* Move into the codestream to the first SOT used to decode the desired tile */
  10137. l_tile_no_to_dec = (OPJ_UINT32)
  10138. p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
  10139. if (p_j2k->cstr_index->tile_index)
  10140. if (p_j2k->cstr_index->tile_index->tp_index) {
  10141. if (! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
  10142. /* the index for this tile has not been built,
  10143. * so move to the last SOT read */
  10144. if (!(opj_stream_read_seek(p_stream,
  10145. p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos + 2, p_manager))) {
  10146. opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
  10147. return OPJ_FALSE;
  10148. }
  10149. } else {
  10150. if (!(opj_stream_read_seek(p_stream,
  10151. p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos + 2,
  10152. p_manager))) {
  10153. opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
  10154. return OPJ_FALSE;
  10155. }
  10156. }
  10157. /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
  10158. if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) {
  10159. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
  10160. }
  10161. }
  10162. /* Reset current tile part number for all tiles, and not only the one */
  10163. /* of interest. */
  10164. /* Not completely sure this is always correct but required for */
  10165. /* ./build/bin/j2k_random_tile_access ./build/tests/tte1.j2k */
  10166. l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
  10167. for (i = 0; i < l_nb_tiles; ++i) {
  10168. p_j2k->m_cp.tcps[i].m_current_tile_part_number = -1;
  10169. }
  10170. for (;;) {
  10171. if (! opj_j2k_read_tile_header(p_j2k,
  10172. &l_current_tile_no,
  10173. NULL,
  10174. &l_tile_x0, &l_tile_y0,
  10175. &l_tile_x1, &l_tile_y1,
  10176. &l_nb_comps,
  10177. &l_go_on,
  10178. p_stream,
  10179. p_manager)) {
  10180. return OPJ_FALSE;
  10181. }
  10182. if (! l_go_on) {
  10183. break;
  10184. }
  10185. if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0,
  10186. p_stream, p_manager)) {
  10187. return OPJ_FALSE;
  10188. }
  10189. opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n",
  10190. l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
  10191. if (! opj_j2k_update_image_data(p_j2k->m_tcd,
  10192. p_j2k->m_output_image)) {
  10193. return OPJ_FALSE;
  10194. }
  10195. opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]);
  10196. opj_event_msg(p_manager, EVT_INFO,
  10197. "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
  10198. if (l_current_tile_no == l_tile_no_to_dec) {
  10199. /* move into the codestream to the first SOT (FIXME or not move?)*/
  10200. if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2,
  10201. p_manager))) {
  10202. opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
  10203. return OPJ_FALSE;
  10204. }
  10205. break;
  10206. } else {
  10207. opj_event_msg(p_manager, EVT_WARNING,
  10208. "Tile read, decoded and updated is not the desired one (%d vs %d).\n",
  10209. l_current_tile_no + 1, l_tile_no_to_dec + 1);
  10210. }
  10211. }
  10212. if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) {
  10213. return OPJ_FALSE;
  10214. }
  10215. return OPJ_TRUE;
  10216. }
  10217. /**
  10218. * Sets up the procedures to do on decoding one tile. Developers wanting to extend the library can add their own reading procedures.
  10219. */
  10220. static OPJ_BOOL opj_j2k_setup_decoding_tile(opj_j2k_t *p_j2k,
  10221. opj_event_mgr_t * p_manager)
  10222. {
  10223. /* preconditions*/
  10224. assert(p_j2k != 00);
  10225. assert(p_manager != 00);
  10226. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10227. (opj_procedure)opj_j2k_decode_one_tile, p_manager)) {
  10228. return OPJ_FALSE;
  10229. }
  10230. /* DEVELOPER CORNER, add your custom procedures */
  10231. return OPJ_TRUE;
  10232. }
  10233. static OPJ_BOOL opj_j2k_move_data_from_codec_to_output_image(opj_j2k_t * p_j2k,
  10234. opj_image_t * p_image)
  10235. {
  10236. OPJ_UINT32 compno;
  10237. /* Move data and copy one information from codec to output image*/
  10238. if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode > 0) {
  10239. opj_image_comp_t* newcomps =
  10240. (opj_image_comp_t*) opj_malloc(
  10241. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode *
  10242. sizeof(opj_image_comp_t));
  10243. if (newcomps == NULL) {
  10244. opj_image_destroy(p_j2k->m_private_image);
  10245. p_j2k->m_private_image = NULL;
  10246. return OPJ_FALSE;
  10247. }
  10248. for (compno = 0; compno < p_image->numcomps; compno++) {
  10249. opj_image_data_free(p_image->comps[compno].data);
  10250. p_image->comps[compno].data = NULL;
  10251. }
  10252. for (compno = 0;
  10253. compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) {
  10254. OPJ_UINT32 src_compno =
  10255. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno];
  10256. memcpy(&(newcomps[compno]),
  10257. &(p_j2k->m_output_image->comps[src_compno]),
  10258. sizeof(opj_image_comp_t));
  10259. newcomps[compno].resno_decoded =
  10260. p_j2k->m_output_image->comps[src_compno].resno_decoded;
  10261. newcomps[compno].data = p_j2k->m_output_image->comps[src_compno].data;
  10262. p_j2k->m_output_image->comps[src_compno].data = NULL;
  10263. }
  10264. for (compno = 0; compno < p_image->numcomps; compno++) {
  10265. assert(p_j2k->m_output_image->comps[compno].data == NULL);
  10266. opj_image_data_free(p_j2k->m_output_image->comps[compno].data);
  10267. p_j2k->m_output_image->comps[compno].data = NULL;
  10268. }
  10269. p_image->numcomps = p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode;
  10270. opj_free(p_image->comps);
  10271. p_image->comps = newcomps;
  10272. } else {
  10273. for (compno = 0; compno < p_image->numcomps; compno++) {
  10274. p_image->comps[compno].resno_decoded =
  10275. p_j2k->m_output_image->comps[compno].resno_decoded;
  10276. opj_image_data_free(p_image->comps[compno].data);
  10277. p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
  10278. #if 0
  10279. char fn[256];
  10280. sprintf(fn, "/tmp/%d.raw", compno);
  10281. FILE *debug = fopen(fn, "wb");
  10282. fwrite(p_image->comps[compno].data, sizeof(OPJ_INT32),
  10283. p_image->comps[compno].w * p_image->comps[compno].h, debug);
  10284. fclose(debug);
  10285. #endif
  10286. p_j2k->m_output_image->comps[compno].data = NULL;
  10287. }
  10288. }
  10289. return OPJ_TRUE;
  10290. }
  10291. OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k,
  10292. opj_stream_private_t * p_stream,
  10293. opj_image_t * p_image,
  10294. opj_event_mgr_t * p_manager)
  10295. {
  10296. if (!p_image) {
  10297. return OPJ_FALSE;
  10298. }
  10299. /* Heuristics to detect sequence opj_read_header(), opj_set_decoded_resolution_factor() */
  10300. /* and finally opj_decode_image() without manual setting of comps[].factor */
  10301. /* We could potentially always execute it, if we don't allow people to do */
  10302. /* opj_read_header(), modify x0,y0,x1,y1 of returned image an call opj_decode_image() */
  10303. if (p_j2k->m_cp.m_specific_param.m_dec.m_reduce > 0 &&
  10304. p_j2k->m_private_image != NULL &&
  10305. p_j2k->m_private_image->numcomps > 0 &&
  10306. p_j2k->m_private_image->comps[0].factor ==
  10307. p_j2k->m_cp.m_specific_param.m_dec.m_reduce &&
  10308. p_image->numcomps > 0 &&
  10309. p_image->comps[0].factor == 0 &&
  10310. /* Don't mess with image dimension if the user has allocated it */
  10311. p_image->comps[0].data == NULL) {
  10312. OPJ_UINT32 it_comp;
  10313. /* Update the comps[].factor member of the output image with the one */
  10314. /* of m_reduce */
  10315. for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
  10316. p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce;
  10317. }
  10318. if (!opj_j2k_update_image_dimensions(p_image, p_manager)) {
  10319. return OPJ_FALSE;
  10320. }
  10321. }
  10322. if (p_j2k->m_output_image == NULL) {
  10323. p_j2k->m_output_image = opj_image_create0();
  10324. if (!(p_j2k->m_output_image)) {
  10325. return OPJ_FALSE;
  10326. }
  10327. }
  10328. opj_copy_image_header(p_image, p_j2k->m_output_image);
  10329. /* customization of the decoding */
  10330. if (!opj_j2k_setup_decoding(p_j2k, p_manager)) {
  10331. return OPJ_FALSE;
  10332. }
  10333. /* Decode the codestream */
  10334. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  10335. opj_image_destroy(p_j2k->m_private_image);
  10336. p_j2k->m_private_image = NULL;
  10337. return OPJ_FALSE;
  10338. }
  10339. /* Move data and copy one information from codec to output image*/
  10340. return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image);
  10341. }
  10342. OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k,
  10343. opj_stream_private_t *p_stream,
  10344. opj_image_t* p_image,
  10345. opj_event_mgr_t * p_manager,
  10346. OPJ_UINT32 tile_index)
  10347. {
  10348. OPJ_UINT32 compno;
  10349. OPJ_UINT32 l_tile_x, l_tile_y;
  10350. opj_image_comp_t* l_img_comp;
  10351. if (!p_image) {
  10352. opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n");
  10353. return OPJ_FALSE;
  10354. }
  10355. if (p_image->numcomps < p_j2k->m_private_image->numcomps) {
  10356. opj_event_msg(p_manager, EVT_ERROR,
  10357. "Image has less components than codestream.\n");
  10358. return OPJ_FALSE;
  10359. }
  10360. if (/*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th)) {
  10361. opj_event_msg(p_manager, EVT_ERROR,
  10362. "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index,
  10363. (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);
  10364. return OPJ_FALSE;
  10365. }
  10366. /* Compute the dimension of the desired tile*/
  10367. l_tile_x = tile_index % p_j2k->m_cp.tw;
  10368. l_tile_y = tile_index / p_j2k->m_cp.tw;
  10369. p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
  10370. if (p_image->x0 < p_j2k->m_private_image->x0) {
  10371. p_image->x0 = p_j2k->m_private_image->x0;
  10372. }
  10373. p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
  10374. if (p_image->x1 > p_j2k->m_private_image->x1) {
  10375. p_image->x1 = p_j2k->m_private_image->x1;
  10376. }
  10377. p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
  10378. if (p_image->y0 < p_j2k->m_private_image->y0) {
  10379. p_image->y0 = p_j2k->m_private_image->y0;
  10380. }
  10381. p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
  10382. if (p_image->y1 > p_j2k->m_private_image->y1) {
  10383. p_image->y1 = p_j2k->m_private_image->y1;
  10384. }
  10385. l_img_comp = p_image->comps;
  10386. for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
  10387. OPJ_INT32 l_comp_x1, l_comp_y1;
  10388. l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
  10389. l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0,
  10390. (OPJ_INT32)l_img_comp->dx);
  10391. l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0,
  10392. (OPJ_INT32)l_img_comp->dy);
  10393. l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
  10394. l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
  10395. l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1,
  10396. (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0,
  10397. (OPJ_INT32)l_img_comp->factor));
  10398. l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1,
  10399. (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0,
  10400. (OPJ_INT32)l_img_comp->factor));
  10401. l_img_comp++;
  10402. }
  10403. if (p_image->numcomps > p_j2k->m_private_image->numcomps) {
  10404. /* Can happen when calling repeatdly opj_get_decoded_tile() on an
  10405. * image with a color palette, where color palette expansion is done
  10406. * later in jp2.c */
  10407. for (compno = p_j2k->m_private_image->numcomps; compno < p_image->numcomps;
  10408. ++compno) {
  10409. opj_image_data_free(p_image->comps[compno].data);
  10410. p_image->comps[compno].data = NULL;
  10411. }
  10412. p_image->numcomps = p_j2k->m_private_image->numcomps;
  10413. }
  10414. /* Destroy the previous output image*/
  10415. if (p_j2k->m_output_image) {
  10416. opj_image_destroy(p_j2k->m_output_image);
  10417. }
  10418. /* Create the output image from the information previously computed*/
  10419. p_j2k->m_output_image = opj_image_create0();
  10420. if (!(p_j2k->m_output_image)) {
  10421. return OPJ_FALSE;
  10422. }
  10423. opj_copy_image_header(p_image, p_j2k->m_output_image);
  10424. p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index;
  10425. /* customization of the decoding */
  10426. if (!opj_j2k_setup_decoding_tile(p_j2k, p_manager)) {
  10427. return OPJ_FALSE;
  10428. }
  10429. /* Decode the codestream */
  10430. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  10431. opj_image_destroy(p_j2k->m_private_image);
  10432. p_j2k->m_private_image = NULL;
  10433. return OPJ_FALSE;
  10434. }
  10435. /* Move data and copy one information from codec to output image*/
  10436. return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image);
  10437. }
  10438. OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
  10439. OPJ_UINT32 res_factor,
  10440. opj_event_mgr_t * p_manager)
  10441. {
  10442. OPJ_UINT32 it_comp;
  10443. p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
  10444. if (p_j2k->m_private_image) {
  10445. if (p_j2k->m_private_image->comps) {
  10446. if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
  10447. if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
  10448. for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
  10449. OPJ_UINT32 max_res =
  10450. p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
  10451. if (res_factor >= max_res) {
  10452. opj_event_msg(p_manager, EVT_ERROR,
  10453. "Resolution factor is greater than the maximum resolution in the component.\n");
  10454. return OPJ_FALSE;
  10455. }
  10456. p_j2k->m_private_image->comps[it_comp].factor = res_factor;
  10457. }
  10458. return OPJ_TRUE;
  10459. }
  10460. }
  10461. }
  10462. }
  10463. return OPJ_FALSE;
  10464. }
  10465. /* ----------------------------------------------------------------------- */
  10466. OPJ_BOOL opj_j2k_encoder_set_extra_options(
  10467. opj_j2k_t *p_j2k,
  10468. const char* const* p_options,
  10469. opj_event_mgr_t * p_manager)
  10470. {
  10471. const char* const* p_option_iter;
  10472. if (p_options == NULL) {
  10473. return OPJ_TRUE;
  10474. }
  10475. for (p_option_iter = p_options; *p_option_iter != NULL; ++p_option_iter) {
  10476. if (strncmp(*p_option_iter, "PLT=", 4) == 0) {
  10477. if (strcmp(*p_option_iter, "PLT=YES") == 0) {
  10478. p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_TRUE;
  10479. } else if (strcmp(*p_option_iter, "PLT=NO") == 0) {
  10480. p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_FALSE;
  10481. } else {
  10482. opj_event_msg(p_manager, EVT_ERROR,
  10483. "Invalid value for option: %s.\n", *p_option_iter);
  10484. return OPJ_FALSE;
  10485. }
  10486. } else if (strncmp(*p_option_iter, "TLM=", 4) == 0) {
  10487. if (strcmp(*p_option_iter, "TLM=YES") == 0) {
  10488. p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE;
  10489. } else if (strcmp(*p_option_iter, "TLM=NO") == 0) {
  10490. p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_FALSE;
  10491. } else {
  10492. opj_event_msg(p_manager, EVT_ERROR,
  10493. "Invalid value for option: %s.\n", *p_option_iter);
  10494. return OPJ_FALSE;
  10495. }
  10496. } else if (strncmp(*p_option_iter, "GUARD_BITS=", strlen("GUARD_BITS=")) == 0) {
  10497. OPJ_UINT32 tileno;
  10498. opj_cp_t *cp = cp = &(p_j2k->m_cp);
  10499. int numgbits = atoi(*p_option_iter + strlen("GUARD_BITS="));
  10500. if (numgbits < 0 || numgbits > 7) {
  10501. opj_event_msg(p_manager, EVT_ERROR,
  10502. "Invalid value for option: %s. Should be in [0,7]\n", *p_option_iter);
  10503. return OPJ_FALSE;
  10504. }
  10505. for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  10506. OPJ_UINT32 i;
  10507. opj_tcp_t *tcp = &cp->tcps[tileno];
  10508. for (i = 0; i < p_j2k->m_specific_param.m_encoder.m_nb_comps; i++) {
  10509. opj_tccp_t *tccp = &tcp->tccps[i];
  10510. tccp->numgbits = (OPJ_UINT32)numgbits;
  10511. }
  10512. }
  10513. } else {
  10514. opj_event_msg(p_manager, EVT_ERROR,
  10515. "Invalid option: %s.\n", *p_option_iter);
  10516. return OPJ_FALSE;
  10517. }
  10518. }
  10519. return OPJ_TRUE;
  10520. }
  10521. /* ----------------------------------------------------------------------- */
  10522. OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
  10523. opj_stream_private_t *p_stream,
  10524. opj_event_mgr_t * p_manager)
  10525. {
  10526. OPJ_UINT32 i, j;
  10527. OPJ_UINT32 l_nb_tiles;
  10528. OPJ_SIZE_T l_max_tile_size = 0, l_current_tile_size;
  10529. OPJ_BYTE * l_current_data = 00;
  10530. OPJ_BOOL l_reuse_data = OPJ_FALSE;
  10531. opj_tcd_t* p_tcd = 00;
  10532. /* preconditions */
  10533. assert(p_j2k != 00);
  10534. assert(p_stream != 00);
  10535. assert(p_manager != 00);
  10536. p_tcd = p_j2k->m_tcd;
  10537. l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  10538. if (l_nb_tiles == 1) {
  10539. l_reuse_data = OPJ_TRUE;
  10540. #ifdef __SSE__
  10541. for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) {
  10542. opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
  10543. if (((size_t)l_img_comp->data & 0xFU) !=
  10544. 0U) { /* tile data shall be aligned on 16 bytes */
  10545. l_reuse_data = OPJ_FALSE;
  10546. }
  10547. }
  10548. #endif
  10549. }
  10550. for (i = 0; i < l_nb_tiles; ++i) {
  10551. if (! opj_j2k_pre_write_tile(p_j2k, i, p_stream, p_manager)) {
  10552. if (l_current_data) {
  10553. opj_free(l_current_data);
  10554. }
  10555. return OPJ_FALSE;
  10556. }
  10557. /* if we only have one tile, then simply set tile component data equal to image component data */
  10558. /* otherwise, allocate the data */
  10559. for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) {
  10560. opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j;
  10561. if (l_reuse_data) {
  10562. opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
  10563. l_tilec->data = l_img_comp->data;
  10564. l_tilec->ownsData = OPJ_FALSE;
  10565. } else {
  10566. if (! opj_alloc_tile_component_data(l_tilec)) {
  10567. opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data.");
  10568. if (l_current_data) {
  10569. opj_free(l_current_data);
  10570. }
  10571. return OPJ_FALSE;
  10572. }
  10573. }
  10574. }
  10575. l_current_tile_size = opj_tcd_get_encoder_input_buffer_size(p_j2k->m_tcd);
  10576. if (!l_reuse_data) {
  10577. if (l_current_tile_size > l_max_tile_size) {
  10578. OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data,
  10579. l_current_tile_size);
  10580. if (! l_new_current_data) {
  10581. if (l_current_data) {
  10582. opj_free(l_current_data);
  10583. }
  10584. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
  10585. return OPJ_FALSE;
  10586. }
  10587. l_current_data = l_new_current_data;
  10588. l_max_tile_size = l_current_tile_size;
  10589. }
  10590. if (l_current_data == NULL) {
  10591. /* Should not happen in practice, but will avoid Coverity to */
  10592. /* complain about a null pointer dereference */
  10593. assert(0);
  10594. return OPJ_FALSE;
  10595. }
  10596. /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */
  10597. /* 32 bit components @ 8 bit precision get converted to 8 bit */
  10598. /* 32 bit components @ 16 bit precision get converted to 16 bit */
  10599. opj_j2k_get_tile_data(p_j2k->m_tcd, l_current_data);
  10600. /* now copy this data into the tile component */
  10601. if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, l_current_data,
  10602. l_current_tile_size)) {
  10603. opj_event_msg(p_manager, EVT_ERROR,
  10604. "Size mismatch between tile data and sent data.");
  10605. opj_free(l_current_data);
  10606. return OPJ_FALSE;
  10607. }
  10608. }
  10609. if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) {
  10610. if (l_current_data) {
  10611. opj_free(l_current_data);
  10612. }
  10613. return OPJ_FALSE;
  10614. }
  10615. }
  10616. if (l_current_data) {
  10617. opj_free(l_current_data);
  10618. }
  10619. return OPJ_TRUE;
  10620. }
  10621. OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k,
  10622. opj_stream_private_t *p_stream,
  10623. opj_event_mgr_t * p_manager)
  10624. {
  10625. /* customization of the encoding */
  10626. if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) {
  10627. return OPJ_FALSE;
  10628. }
  10629. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  10630. return OPJ_FALSE;
  10631. }
  10632. return OPJ_TRUE;
  10633. }
  10634. OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
  10635. opj_stream_private_t *p_stream,
  10636. opj_image_t * p_image,
  10637. opj_event_mgr_t * p_manager)
  10638. {
  10639. /* preconditions */
  10640. assert(p_j2k != 00);
  10641. assert(p_stream != 00);
  10642. assert(p_manager != 00);
  10643. p_j2k->m_private_image = opj_image_create0();
  10644. if (! p_j2k->m_private_image) {
  10645. opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header.");
  10646. return OPJ_FALSE;
  10647. }
  10648. opj_copy_image_header(p_image, p_j2k->m_private_image);
  10649. /* TODO_MSD: Find a better way */
  10650. if (p_image->comps) {
  10651. OPJ_UINT32 it_comp;
  10652. for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
  10653. if (p_image->comps[it_comp].data) {
  10654. p_j2k->m_private_image->comps[it_comp].data = p_image->comps[it_comp].data;
  10655. p_image->comps[it_comp].data = NULL;
  10656. }
  10657. }
  10658. }
  10659. /* customization of the validation */
  10660. if (! opj_j2k_setup_encoding_validation(p_j2k, p_manager)) {
  10661. return OPJ_FALSE;
  10662. }
  10663. /* validation of the parameters codec */
  10664. if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) {
  10665. return OPJ_FALSE;
  10666. }
  10667. /* customization of the encoding */
  10668. if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) {
  10669. return OPJ_FALSE;
  10670. }
  10671. /* write header */
  10672. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  10673. return OPJ_FALSE;
  10674. }
  10675. return OPJ_TRUE;
  10676. }
  10677. static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k,
  10678. OPJ_UINT32 p_tile_index,
  10679. opj_stream_private_t *p_stream,
  10680. opj_event_mgr_t * p_manager)
  10681. {
  10682. (void)p_stream;
  10683. if (p_tile_index != p_j2k->m_current_tile_number) {
  10684. opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match.");
  10685. return OPJ_FALSE;
  10686. }
  10687. opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n",
  10688. p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th);
  10689. p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
  10690. p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
  10691. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
  10692. /* initialisation before tile encoding */
  10693. if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number,
  10694. p_manager)) {
  10695. return OPJ_FALSE;
  10696. }
  10697. return OPJ_TRUE;
  10698. }
  10699. static void opj_get_tile_dimensions(opj_image_t * l_image,
  10700. opj_tcd_tilecomp_t * l_tilec,
  10701. opj_image_comp_t * l_img_comp,
  10702. OPJ_UINT32* l_size_comp,
  10703. OPJ_UINT32* l_width,
  10704. OPJ_UINT32* l_height,
  10705. OPJ_UINT32* l_offset_x,
  10706. OPJ_UINT32* l_offset_y,
  10707. OPJ_UINT32* l_image_width,
  10708. OPJ_UINT32* l_stride,
  10709. OPJ_UINT32* l_tile_offset)
  10710. {
  10711. OPJ_UINT32 l_remaining;
  10712. *l_size_comp = l_img_comp->prec >> 3; /* (/8) */
  10713. l_remaining = l_img_comp->prec & 7; /* (%8) */
  10714. if (l_remaining) {
  10715. *l_size_comp += 1;
  10716. }
  10717. if (*l_size_comp == 3) {
  10718. *l_size_comp = 4;
  10719. }
  10720. *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
  10721. *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
  10722. *l_offset_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x0,
  10723. (OPJ_INT32)l_img_comp->dx);
  10724. *l_offset_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->y0,
  10725. (OPJ_INT32)l_img_comp->dy);
  10726. *l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 -
  10727. (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);
  10728. *l_stride = *l_image_width - *l_width;
  10729. *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + ((
  10730. OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width;
  10731. }
  10732. static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
  10733. {
  10734. OPJ_UINT32 i, j, k = 0;
  10735. for (i = 0; i < p_tcd->image->numcomps; ++i) {
  10736. opj_image_t * l_image = p_tcd->image;
  10737. OPJ_INT32 * l_src_ptr;
  10738. opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i;
  10739. opj_image_comp_t * l_img_comp = l_image->comps + i;
  10740. OPJ_UINT32 l_size_comp, l_width, l_height, l_offset_x, l_offset_y,
  10741. l_image_width, l_stride, l_tile_offset;
  10742. opj_get_tile_dimensions(l_image,
  10743. l_tilec,
  10744. l_img_comp,
  10745. &l_size_comp,
  10746. &l_width,
  10747. &l_height,
  10748. &l_offset_x,
  10749. &l_offset_y,
  10750. &l_image_width,
  10751. &l_stride,
  10752. &l_tile_offset);
  10753. l_src_ptr = l_img_comp->data + l_tile_offset;
  10754. switch (l_size_comp) {
  10755. case 1: {
  10756. OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
  10757. if (l_img_comp->sgnd) {
  10758. for (j = 0; j < l_height; ++j) {
  10759. for (k = 0; k < l_width; ++k) {
  10760. *(l_dest_ptr) = (OPJ_CHAR)(*l_src_ptr);
  10761. ++l_dest_ptr;
  10762. ++l_src_ptr;
  10763. }
  10764. l_src_ptr += l_stride;
  10765. }
  10766. } else {
  10767. for (j = 0; j < l_height; ++j) {
  10768. for (k = 0; k < l_width; ++k) {
  10769. *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr) & 0xff);
  10770. ++l_dest_ptr;
  10771. ++l_src_ptr;
  10772. }
  10773. l_src_ptr += l_stride;
  10774. }
  10775. }
  10776. p_data = (OPJ_BYTE*) l_dest_ptr;
  10777. }
  10778. break;
  10779. case 2: {
  10780. OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
  10781. if (l_img_comp->sgnd) {
  10782. for (j = 0; j < l_height; ++j) {
  10783. for (k = 0; k < l_width; ++k) {
  10784. *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++));
  10785. }
  10786. l_src_ptr += l_stride;
  10787. }
  10788. } else {
  10789. for (j = 0; j < l_height; ++j) {
  10790. for (k = 0; k < l_width; ++k) {
  10791. *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
  10792. }
  10793. l_src_ptr += l_stride;
  10794. }
  10795. }
  10796. p_data = (OPJ_BYTE*) l_dest_ptr;
  10797. }
  10798. break;
  10799. case 4: {
  10800. OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
  10801. for (j = 0; j < l_height; ++j) {
  10802. for (k = 0; k < l_width; ++k) {
  10803. *(l_dest_ptr++) = *(l_src_ptr++);
  10804. }
  10805. l_src_ptr += l_stride;
  10806. }
  10807. p_data = (OPJ_BYTE*) l_dest_ptr;
  10808. }
  10809. break;
  10810. }
  10811. }
  10812. }
  10813. static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k,
  10814. opj_stream_private_t *p_stream,
  10815. opj_event_mgr_t * p_manager)
  10816. {
  10817. OPJ_UINT32 l_nb_bytes_written;
  10818. OPJ_BYTE * l_current_data = 00;
  10819. OPJ_UINT32 l_tile_size = 0;
  10820. OPJ_UINT32 l_available_data;
  10821. /* preconditions */
  10822. assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
  10823. l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
  10824. l_available_data = l_tile_size;
  10825. l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
  10826. l_nb_bytes_written = 0;
  10827. if (! opj_j2k_write_first_tile_part(p_j2k, l_current_data, &l_nb_bytes_written,
  10828. l_available_data, p_stream, p_manager)) {
  10829. return OPJ_FALSE;
  10830. }
  10831. l_current_data += l_nb_bytes_written;
  10832. l_available_data -= l_nb_bytes_written;
  10833. l_nb_bytes_written = 0;
  10834. if (! opj_j2k_write_all_tile_parts(p_j2k, l_current_data, &l_nb_bytes_written,
  10835. l_available_data, p_stream, p_manager)) {
  10836. return OPJ_FALSE;
  10837. }
  10838. l_available_data -= l_nb_bytes_written;
  10839. l_nb_bytes_written = l_tile_size - l_available_data;
  10840. if (opj_stream_write_data(p_stream,
  10841. p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
  10842. l_nb_bytes_written, p_manager) != l_nb_bytes_written) {
  10843. return OPJ_FALSE;
  10844. }
  10845. ++p_j2k->m_current_tile_number;
  10846. return OPJ_TRUE;
  10847. }
  10848. static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k,
  10849. opj_event_mgr_t * p_manager)
  10850. {
  10851. /* preconditions */
  10852. assert(p_j2k != 00);
  10853. assert(p_manager != 00);
  10854. /* DEVELOPER CORNER, insert your custom procedures */
  10855. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10856. (opj_procedure)opj_j2k_write_eoc, p_manager)) {
  10857. return OPJ_FALSE;
  10858. }
  10859. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  10860. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10861. (opj_procedure)opj_j2k_write_updated_tlm, p_manager)) {
  10862. return OPJ_FALSE;
  10863. }
  10864. }
  10865. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10866. (opj_procedure)opj_j2k_write_epc, p_manager)) {
  10867. return OPJ_FALSE;
  10868. }
  10869. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10870. (opj_procedure)opj_j2k_end_encoding, p_manager)) {
  10871. return OPJ_FALSE;
  10872. }
  10873. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10874. (opj_procedure)opj_j2k_destroy_header_memory, p_manager)) {
  10875. return OPJ_FALSE;
  10876. }
  10877. return OPJ_TRUE;
  10878. }
  10879. static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k,
  10880. opj_event_mgr_t * p_manager)
  10881. {
  10882. /* preconditions */
  10883. assert(p_j2k != 00);
  10884. assert(p_manager != 00);
  10885. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  10886. (opj_procedure)opj_j2k_build_encoder, p_manager)) {
  10887. return OPJ_FALSE;
  10888. }
  10889. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  10890. (opj_procedure)opj_j2k_encoding_validation, p_manager)) {
  10891. return OPJ_FALSE;
  10892. }
  10893. /* DEVELOPER CORNER, add your custom validation procedure */
  10894. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  10895. (opj_procedure)opj_j2k_mct_validation, p_manager)) {
  10896. return OPJ_FALSE;
  10897. }
  10898. return OPJ_TRUE;
  10899. }
  10900. static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k,
  10901. opj_event_mgr_t * p_manager)
  10902. {
  10903. /* preconditions */
  10904. assert(p_j2k != 00);
  10905. assert(p_manager != 00);
  10906. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10907. (opj_procedure)opj_j2k_init_info, p_manager)) {
  10908. return OPJ_FALSE;
  10909. }
  10910. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10911. (opj_procedure)opj_j2k_write_soc, p_manager)) {
  10912. return OPJ_FALSE;
  10913. }
  10914. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10915. (opj_procedure)opj_j2k_write_siz, p_manager)) {
  10916. return OPJ_FALSE;
  10917. }
  10918. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10919. (opj_procedure)opj_j2k_write_cod, p_manager)) {
  10920. return OPJ_FALSE;
  10921. }
  10922. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10923. (opj_procedure)opj_j2k_write_qcd, p_manager)) {
  10924. return OPJ_FALSE;
  10925. }
  10926. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10927. (opj_procedure)opj_j2k_write_all_coc, p_manager)) {
  10928. return OPJ_FALSE;
  10929. }
  10930. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10931. (opj_procedure)opj_j2k_write_all_qcc, p_manager)) {
  10932. return OPJ_FALSE;
  10933. }
  10934. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  10935. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10936. (opj_procedure)opj_j2k_write_tlm, p_manager)) {
  10937. return OPJ_FALSE;
  10938. }
  10939. if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) {
  10940. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10941. (opj_procedure)opj_j2k_write_poc, p_manager)) {
  10942. return OPJ_FALSE;
  10943. }
  10944. }
  10945. }
  10946. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10947. (opj_procedure)opj_j2k_write_regions, p_manager)) {
  10948. return OPJ_FALSE;
  10949. }
  10950. if (p_j2k->m_cp.comment != 00) {
  10951. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10952. (opj_procedure)opj_j2k_write_com, p_manager)) {
  10953. return OPJ_FALSE;
  10954. }
  10955. }
  10956. /* DEVELOPER CORNER, insert your custom procedures */
  10957. if ((p_j2k->m_cp.rsiz & (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) ==
  10958. (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) {
  10959. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10960. (opj_procedure)opj_j2k_write_mct_data_group, p_manager)) {
  10961. return OPJ_FALSE;
  10962. }
  10963. }
  10964. /* End of Developer Corner */
  10965. if (p_j2k->cstr_index) {
  10966. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10967. (opj_procedure)opj_j2k_get_end_header, p_manager)) {
  10968. return OPJ_FALSE;
  10969. }
  10970. }
  10971. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10972. (opj_procedure)opj_j2k_create_tcd, p_manager)) {
  10973. return OPJ_FALSE;
  10974. }
  10975. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10976. (opj_procedure)opj_j2k_update_rates, p_manager)) {
  10977. return OPJ_FALSE;
  10978. }
  10979. return OPJ_TRUE;
  10980. }
  10981. static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k,
  10982. OPJ_BYTE * p_data,
  10983. OPJ_UINT32 * p_data_written,
  10984. OPJ_UINT32 total_data_size,
  10985. opj_stream_private_t *p_stream,
  10986. struct opj_event_mgr * p_manager)
  10987. {
  10988. OPJ_UINT32 l_nb_bytes_written = 0;
  10989. OPJ_UINT32 l_current_nb_bytes_written;
  10990. OPJ_BYTE * l_begin_data = 00;
  10991. opj_tcd_t * l_tcd = 00;
  10992. opj_cp_t * l_cp = 00;
  10993. l_tcd = p_j2k->m_tcd;
  10994. l_cp = &(p_j2k->m_cp);
  10995. l_tcd->cur_pino = 0;
  10996. /*Get number of tile parts*/
  10997. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
  10998. /* INDEX >> */
  10999. /* << INDEX */
  11000. l_current_nb_bytes_written = 0;
  11001. l_begin_data = p_data;
  11002. if (! opj_j2k_write_sot(p_j2k, p_data, total_data_size,
  11003. &l_current_nb_bytes_written, p_stream,
  11004. p_manager)) {
  11005. return OPJ_FALSE;
  11006. }
  11007. l_nb_bytes_written += l_current_nb_bytes_written;
  11008. p_data += l_current_nb_bytes_written;
  11009. total_data_size -= l_current_nb_bytes_written;
  11010. if (!OPJ_IS_CINEMA(l_cp->rsiz)) {
  11011. #if 0
  11012. for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
  11013. l_current_nb_bytes_written = 0;
  11014. opj_j2k_write_coc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written,
  11015. p_manager);
  11016. l_nb_bytes_written += l_current_nb_bytes_written;
  11017. p_data += l_current_nb_bytes_written;
  11018. total_data_size -= l_current_nb_bytes_written;
  11019. l_current_nb_bytes_written = 0;
  11020. opj_j2k_write_qcc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written,
  11021. p_manager);
  11022. l_nb_bytes_written += l_current_nb_bytes_written;
  11023. p_data += l_current_nb_bytes_written;
  11024. total_data_size -= l_current_nb_bytes_written;
  11025. }
  11026. #endif
  11027. if (l_cp->tcps[p_j2k->m_current_tile_number].POC) {
  11028. l_current_nb_bytes_written = 0;
  11029. opj_j2k_write_poc_in_memory(p_j2k, p_data, &l_current_nb_bytes_written,
  11030. p_manager);
  11031. l_nb_bytes_written += l_current_nb_bytes_written;
  11032. p_data += l_current_nb_bytes_written;
  11033. total_data_size -= l_current_nb_bytes_written;
  11034. }
  11035. }
  11036. l_current_nb_bytes_written = 0;
  11037. if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written,
  11038. total_data_size, p_stream, p_manager)) {
  11039. return OPJ_FALSE;
  11040. }
  11041. l_nb_bytes_written += l_current_nb_bytes_written;
  11042. * p_data_written = l_nb_bytes_written;
  11043. /* Writing Psot in SOT marker */
  11044. opj_write_bytes(l_begin_data + 6, l_nb_bytes_written,
  11045. 4); /* PSOT */
  11046. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  11047. opj_j2k_update_tlm(p_j2k, l_nb_bytes_written);
  11048. }
  11049. return OPJ_TRUE;
  11050. }
  11051. static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k,
  11052. OPJ_BYTE * p_data,
  11053. OPJ_UINT32 * p_data_written,
  11054. OPJ_UINT32 total_data_size,
  11055. opj_stream_private_t *p_stream,
  11056. struct opj_event_mgr * p_manager
  11057. )
  11058. {
  11059. OPJ_UINT32 tilepartno = 0;
  11060. OPJ_UINT32 l_nb_bytes_written = 0;
  11061. OPJ_UINT32 l_current_nb_bytes_written;
  11062. OPJ_UINT32 l_part_tile_size;
  11063. OPJ_UINT32 tot_num_tp;
  11064. OPJ_UINT32 pino;
  11065. OPJ_BYTE * l_begin_data;
  11066. opj_tcp_t *l_tcp = 00;
  11067. opj_tcd_t * l_tcd = 00;
  11068. opj_cp_t * l_cp = 00;
  11069. l_tcd = p_j2k->m_tcd;
  11070. l_cp = &(p_j2k->m_cp);
  11071. l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
  11072. /*Get number of tile parts*/
  11073. tot_num_tp = opj_j2k_get_num_tp(l_cp, 0, p_j2k->m_current_tile_number);
  11074. /* start writing remaining tile parts */
  11075. ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
  11076. for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
  11077. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
  11078. l_current_nb_bytes_written = 0;
  11079. l_part_tile_size = 0;
  11080. l_begin_data = p_data;
  11081. if (! opj_j2k_write_sot(p_j2k, p_data,
  11082. total_data_size,
  11083. &l_current_nb_bytes_written,
  11084. p_stream,
  11085. p_manager)) {
  11086. return OPJ_FALSE;
  11087. }
  11088. l_nb_bytes_written += l_current_nb_bytes_written;
  11089. p_data += l_current_nb_bytes_written;
  11090. total_data_size -= l_current_nb_bytes_written;
  11091. l_part_tile_size += l_current_nb_bytes_written;
  11092. l_current_nb_bytes_written = 0;
  11093. if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written,
  11094. total_data_size, p_stream, p_manager)) {
  11095. return OPJ_FALSE;
  11096. }
  11097. p_data += l_current_nb_bytes_written;
  11098. l_nb_bytes_written += l_current_nb_bytes_written;
  11099. total_data_size -= l_current_nb_bytes_written;
  11100. l_part_tile_size += l_current_nb_bytes_written;
  11101. /* Writing Psot in SOT marker */
  11102. opj_write_bytes(l_begin_data + 6, l_part_tile_size,
  11103. 4); /* PSOT */
  11104. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  11105. opj_j2k_update_tlm(p_j2k, l_part_tile_size);
  11106. }
  11107. ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
  11108. }
  11109. for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
  11110. l_tcd->cur_pino = pino;
  11111. /*Get number of tile parts*/
  11112. tot_num_tp = opj_j2k_get_num_tp(l_cp, pino, p_j2k->m_current_tile_number);
  11113. for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
  11114. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
  11115. l_current_nb_bytes_written = 0;
  11116. l_part_tile_size = 0;
  11117. l_begin_data = p_data;
  11118. if (! opj_j2k_write_sot(p_j2k, p_data,
  11119. total_data_size,
  11120. &l_current_nb_bytes_written, p_stream,
  11121. p_manager)) {
  11122. return OPJ_FALSE;
  11123. }
  11124. l_nb_bytes_written += l_current_nb_bytes_written;
  11125. p_data += l_current_nb_bytes_written;
  11126. total_data_size -= l_current_nb_bytes_written;
  11127. l_part_tile_size += l_current_nb_bytes_written;
  11128. l_current_nb_bytes_written = 0;
  11129. if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written,
  11130. total_data_size, p_stream, p_manager)) {
  11131. return OPJ_FALSE;
  11132. }
  11133. l_nb_bytes_written += l_current_nb_bytes_written;
  11134. p_data += l_current_nb_bytes_written;
  11135. total_data_size -= l_current_nb_bytes_written;
  11136. l_part_tile_size += l_current_nb_bytes_written;
  11137. /* Writing Psot in SOT marker */
  11138. opj_write_bytes(l_begin_data + 6, l_part_tile_size,
  11139. 4); /* PSOT */
  11140. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  11141. opj_j2k_update_tlm(p_j2k, l_part_tile_size);
  11142. }
  11143. ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
  11144. }
  11145. }
  11146. *p_data_written = l_nb_bytes_written;
  11147. return OPJ_TRUE;
  11148. }
  11149. static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k,
  11150. struct opj_stream_private *p_stream,
  11151. struct opj_event_mgr * p_manager)
  11152. {
  11153. OPJ_UINT32 l_tlm_size;
  11154. OPJ_OFF_T l_tlm_position, l_current_position;
  11155. OPJ_UINT32 size_per_tile_part;
  11156. /* preconditions */
  11157. assert(p_j2k != 00);
  11158. assert(p_manager != 00);
  11159. assert(p_stream != 00);
  11160. size_per_tile_part = p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte ? 5 : 6;
  11161. l_tlm_size = size_per_tile_part *
  11162. p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
  11163. l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
  11164. l_current_position = opj_stream_tell(p_stream);
  11165. if (! opj_stream_seek(p_stream, l_tlm_position, p_manager)) {
  11166. return OPJ_FALSE;
  11167. }
  11168. if (opj_stream_write_data(p_stream,
  11169. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer, l_tlm_size,
  11170. p_manager) != l_tlm_size) {
  11171. return OPJ_FALSE;
  11172. }
  11173. if (! opj_stream_seek(p_stream, l_current_position, p_manager)) {
  11174. return OPJ_FALSE;
  11175. }
  11176. return OPJ_TRUE;
  11177. }
  11178. static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k,
  11179. struct opj_stream_private *p_stream,
  11180. struct opj_event_mgr * p_manager)
  11181. {
  11182. /* preconditions */
  11183. assert(p_j2k != 00);
  11184. assert(p_manager != 00);
  11185. assert(p_stream != 00);
  11186. OPJ_UNUSED(p_stream);
  11187. OPJ_UNUSED(p_manager);
  11188. opj_tcd_destroy(p_j2k->m_tcd);
  11189. p_j2k->m_tcd = 00;
  11190. if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
  11191. opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
  11192. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
  11193. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
  11194. }
  11195. if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
  11196. opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
  11197. p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
  11198. }
  11199. p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
  11200. return OPJ_TRUE;
  11201. }
  11202. /**
  11203. * Destroys the memory associated with the decoding of headers.
  11204. */
  11205. static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k,
  11206. opj_stream_private_t *p_stream,
  11207. opj_event_mgr_t * p_manager
  11208. )
  11209. {
  11210. /* preconditions */
  11211. assert(p_j2k != 00);
  11212. assert(p_stream != 00);
  11213. assert(p_manager != 00);
  11214. OPJ_UNUSED(p_stream);
  11215. OPJ_UNUSED(p_manager);
  11216. if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
  11217. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  11218. p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
  11219. }
  11220. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  11221. return OPJ_TRUE;
  11222. }
  11223. static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k,
  11224. struct opj_stream_private *p_stream,
  11225. struct opj_event_mgr * p_manager)
  11226. {
  11227. opj_codestream_info_t * l_cstr_info = 00;
  11228. /* preconditions */
  11229. assert(p_j2k != 00);
  11230. assert(p_manager != 00);
  11231. assert(p_stream != 00);
  11232. (void)l_cstr_info;
  11233. OPJ_UNUSED(p_stream);
  11234. /* TODO mergeV2: check this part which use cstr_info */
  11235. /*l_cstr_info = p_j2k->cstr_info;
  11236. if (l_cstr_info) {
  11237. OPJ_UINT32 compno;
  11238. l_cstr_info->tile = (opj_tile_info_t *) opj_malloc(p_j2k->m_cp.tw * p_j2k->m_cp.th * sizeof(opj_tile_info_t));
  11239. l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
  11240. l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
  11241. l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
  11242. l_cstr_info->tw = p_j2k->m_cp.tw;
  11243. l_cstr_info->th = p_j2k->m_cp.th;
  11244. l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
  11245. /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
  11246. /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
  11247. /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
  11248. /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
  11249. l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
  11250. l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
  11251. for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
  11252. l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
  11253. }
  11254. l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
  11255. /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
  11256. /*l_cstr_info->maxmarknum = 100;
  11257. l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
  11258. l_cstr_info->marknum = 0;
  11259. }*/
  11260. return opj_j2k_calculate_tp(p_j2k, &(p_j2k->m_cp),
  11261. &p_j2k->m_specific_param.m_encoder.m_total_tile_parts, p_j2k->m_private_image,
  11262. p_manager);
  11263. }
  11264. /**
  11265. * Creates a tile-coder encoder.
  11266. *
  11267. * @param p_stream the stream to write data to.
  11268. * @param p_j2k J2K codec.
  11269. * @param p_manager the user event manager.
  11270. */
  11271. static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k,
  11272. opj_stream_private_t *p_stream,
  11273. opj_event_mgr_t * p_manager
  11274. )
  11275. {
  11276. /* preconditions */
  11277. assert(p_j2k != 00);
  11278. assert(p_manager != 00);
  11279. assert(p_stream != 00);
  11280. OPJ_UNUSED(p_stream);
  11281. p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE);
  11282. if (! p_j2k->m_tcd) {
  11283. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
  11284. return OPJ_FALSE;
  11285. }
  11286. if (!opj_tcd_init(p_j2k->m_tcd, p_j2k->m_private_image, &p_j2k->m_cp,
  11287. p_j2k->m_tp)) {
  11288. opj_tcd_destroy(p_j2k->m_tcd);
  11289. p_j2k->m_tcd = 00;
  11290. return OPJ_FALSE;
  11291. }
  11292. return OPJ_TRUE;
  11293. }
  11294. OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k,
  11295. OPJ_UINT32 p_tile_index,
  11296. OPJ_BYTE * p_data,
  11297. OPJ_UINT32 p_data_size,
  11298. opj_stream_private_t *p_stream,
  11299. opj_event_mgr_t * p_manager)
  11300. {
  11301. if (! opj_j2k_pre_write_tile(p_j2k, p_tile_index, p_stream, p_manager)) {
  11302. opj_event_msg(p_manager, EVT_ERROR,
  11303. "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
  11304. return OPJ_FALSE;
  11305. } else {
  11306. OPJ_UINT32 j;
  11307. /* Allocate data */
  11308. for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) {
  11309. opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j;
  11310. if (! opj_alloc_tile_component_data(l_tilec)) {
  11311. opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data.");
  11312. return OPJ_FALSE;
  11313. }
  11314. }
  11315. /* now copy data into the tile component */
  11316. if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, p_data, p_data_size)) {
  11317. opj_event_msg(p_manager, EVT_ERROR,
  11318. "Size mismatch between tile data and sent data.");
  11319. return OPJ_FALSE;
  11320. }
  11321. if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) {
  11322. opj_event_msg(p_manager, EVT_ERROR,
  11323. "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);
  11324. return OPJ_FALSE;
  11325. }
  11326. }
  11327. return OPJ_TRUE;
  11328. }