j2k.c 486 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217
  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_uint_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
  2053. l_cp->th = opj_uint_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
  2054. /* Check that the number of tiles is valid */
  2055. if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
  2056. opj_event_msg(p_manager, EVT_ERROR,
  2057. "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
  2058. l_cp->tw, l_cp->th);
  2059. return OPJ_FALSE;
  2060. }
  2061. l_nb_tiles = l_cp->tw * l_cp->th;
  2062. /* Define the tiles which will be decoded */
  2063. if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
  2064. p_j2k->m_specific_param.m_decoder.m_start_tile_x =
  2065. (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;
  2066. p_j2k->m_specific_param.m_decoder.m_start_tile_y =
  2067. (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;
  2068. p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv(
  2069. p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0,
  2070. l_cp->tdx);
  2071. p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv(
  2072. p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0,
  2073. l_cp->tdy);
  2074. } else {
  2075. p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
  2076. p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
  2077. p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
  2078. p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
  2079. }
  2080. #ifdef USE_JPWL
  2081. if (l_cp->correct) {
  2082. /* if JPWL is on, we check whether TX errors have damaged
  2083. too much the SIZ parameters */
  2084. if ((l_cp->tw < 1) || (l_cp->th < 1) || (l_cp->tw > l_cp->max_tiles) ||
  2085. (l_cp->th > l_cp->max_tiles)) {
  2086. opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  2087. "JPWL: bad number of tiles (%d x %d)\n",
  2088. l_cp->tw, l_cp->th);
  2089. if (!JPWL_ASSUME) {
  2090. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  2091. return OPJ_FALSE;
  2092. }
  2093. /* we try to correct */
  2094. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n");
  2095. if (l_cp->tw < 1) {
  2096. l_cp->tw = 1;
  2097. opj_event_msg(p_manager, EVT_WARNING,
  2098. "- setting %d tiles in x => HYPOTHESIS!!!\n",
  2099. l_cp->tw);
  2100. }
  2101. if (l_cp->tw > l_cp->max_tiles) {
  2102. l_cp->tw = 1;
  2103. opj_event_msg(p_manager, EVT_WARNING,
  2104. "- too large x, increase expectance of %d\n"
  2105. "- setting %d tiles in x => HYPOTHESIS!!!\n",
  2106. l_cp->max_tiles, l_cp->tw);
  2107. }
  2108. if (l_cp->th < 1) {
  2109. l_cp->th = 1;
  2110. opj_event_msg(p_manager, EVT_WARNING,
  2111. "- setting %d tiles in y => HYPOTHESIS!!!\n",
  2112. l_cp->th);
  2113. }
  2114. if (l_cp->th > l_cp->max_tiles) {
  2115. l_cp->th = 1;
  2116. opj_event_msg(p_manager, EVT_WARNING,
  2117. "- too large y, increase expectance of %d to continue\n",
  2118. "- setting %d tiles in y => HYPOTHESIS!!!\n",
  2119. l_cp->max_tiles, l_cp->th);
  2120. }
  2121. }
  2122. }
  2123. #endif /* USE_JPWL */
  2124. /* memory allocations */
  2125. l_cp->tcps = (opj_tcp_t*) opj_calloc(l_nb_tiles, sizeof(opj_tcp_t));
  2126. if (l_cp->tcps == 00) {
  2127. opj_event_msg(p_manager, EVT_ERROR,
  2128. "Not enough memory to take in charge SIZ marker\n");
  2129. return OPJ_FALSE;
  2130. }
  2131. #ifdef USE_JPWL
  2132. if (l_cp->correct) {
  2133. if (!l_cp->tcps) {
  2134. opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  2135. "JPWL: could not alloc tcps field of cp\n");
  2136. if (!JPWL_ASSUME) {
  2137. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  2138. return OPJ_FALSE;
  2139. }
  2140. }
  2141. }
  2142. #endif /* USE_JPWL */
  2143. p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps =
  2144. (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
  2145. if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps == 00) {
  2146. opj_event_msg(p_manager, EVT_ERROR,
  2147. "Not enough memory to take in charge SIZ marker\n");
  2148. return OPJ_FALSE;
  2149. }
  2150. p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
  2151. (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS,
  2152. sizeof(opj_mct_data_t));
  2153. if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
  2154. opj_event_msg(p_manager, EVT_ERROR,
  2155. "Not enough memory to take in charge SIZ marker\n");
  2156. return OPJ_FALSE;
  2157. }
  2158. p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records =
  2159. OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  2160. p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
  2161. (opj_simple_mcc_decorrelation_data_t*)
  2162. opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS,
  2163. sizeof(opj_simple_mcc_decorrelation_data_t));
  2164. if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
  2165. opj_event_msg(p_manager, EVT_ERROR,
  2166. "Not enough memory to take in charge SIZ marker\n");
  2167. return OPJ_FALSE;
  2168. }
  2169. p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records =
  2170. OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
  2171. /* set up default dc level shift */
  2172. for (i = 0; i < l_image->numcomps; ++i) {
  2173. if (! l_image->comps[i].sgnd) {
  2174. p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[i].m_dc_level_shift = 1
  2175. << (l_image->comps[i].prec - 1);
  2176. }
  2177. }
  2178. l_current_tile_param = l_cp->tcps;
  2179. for (i = 0; i < l_nb_tiles; ++i) {
  2180. l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps,
  2181. sizeof(opj_tccp_t));
  2182. if (l_current_tile_param->tccps == 00) {
  2183. opj_event_msg(p_manager, EVT_ERROR,
  2184. "Not enough memory to take in charge SIZ marker\n");
  2185. return OPJ_FALSE;
  2186. }
  2187. ++l_current_tile_param;
  2188. }
  2189. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MH;
  2190. opj_image_comp_header_update(l_image, l_cp);
  2191. return OPJ_TRUE;
  2192. }
  2193. static OPJ_BOOL opj_j2k_write_com(opj_j2k_t *p_j2k,
  2194. opj_stream_private_t *p_stream,
  2195. opj_event_mgr_t * p_manager
  2196. )
  2197. {
  2198. OPJ_UINT32 l_comment_size;
  2199. OPJ_UINT32 l_total_com_size;
  2200. const OPJ_CHAR *l_comment;
  2201. OPJ_BYTE * l_current_ptr = 00;
  2202. /* preconditions */
  2203. assert(p_j2k != 00);
  2204. assert(p_stream != 00);
  2205. assert(p_manager != 00);
  2206. l_comment = p_j2k->m_cp.comment;
  2207. l_comment_size = (OPJ_UINT32)strlen(l_comment);
  2208. l_total_com_size = l_comment_size + 6;
  2209. if (l_total_com_size >
  2210. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2211. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2212. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size);
  2213. if (! new_header_tile_data) {
  2214. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2215. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2216. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2217. opj_event_msg(p_manager, EVT_ERROR,
  2218. "Not enough memory to write the COM marker\n");
  2219. return OPJ_FALSE;
  2220. }
  2221. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2222. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_total_com_size;
  2223. }
  2224. l_current_ptr = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  2225. opj_write_bytes(l_current_ptr, J2K_MS_COM, 2); /* COM */
  2226. l_current_ptr += 2;
  2227. opj_write_bytes(l_current_ptr, l_total_com_size - 2, 2); /* L_COM */
  2228. l_current_ptr += 2;
  2229. opj_write_bytes(l_current_ptr, 1,
  2230. 2); /* General use (IS 8859-15:1999 (Latin) values) */
  2231. l_current_ptr += 2;
  2232. memcpy(l_current_ptr, l_comment, l_comment_size);
  2233. if (opj_stream_write_data(p_stream,
  2234. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_total_com_size,
  2235. p_manager) != l_total_com_size) {
  2236. return OPJ_FALSE;
  2237. }
  2238. return OPJ_TRUE;
  2239. }
  2240. /**
  2241. * Reads a COM marker (comments)
  2242. * @param p_j2k the jpeg2000 file codec.
  2243. * @param p_header_data the data contained in the COM box.
  2244. * @param p_header_size the size of the data contained in the COM marker.
  2245. * @param p_manager the user event manager.
  2246. */
  2247. static OPJ_BOOL opj_j2k_read_com(opj_j2k_t *p_j2k,
  2248. OPJ_BYTE * p_header_data,
  2249. OPJ_UINT32 p_header_size,
  2250. opj_event_mgr_t * p_manager
  2251. )
  2252. {
  2253. /* preconditions */
  2254. assert(p_j2k != 00);
  2255. assert(p_manager != 00);
  2256. assert(p_header_data != 00);
  2257. OPJ_UNUSED(p_j2k);
  2258. OPJ_UNUSED(p_header_data);
  2259. OPJ_UNUSED(p_header_size);
  2260. OPJ_UNUSED(p_manager);
  2261. return OPJ_TRUE;
  2262. }
  2263. static OPJ_BOOL opj_j2k_write_cod(opj_j2k_t *p_j2k,
  2264. opj_stream_private_t *p_stream,
  2265. opj_event_mgr_t * p_manager)
  2266. {
  2267. opj_cp_t *l_cp = 00;
  2268. opj_tcp_t *l_tcp = 00;
  2269. OPJ_UINT32 l_code_size, l_remaining_size;
  2270. OPJ_BYTE * l_current_data = 00;
  2271. /* preconditions */
  2272. assert(p_j2k != 00);
  2273. assert(p_manager != 00);
  2274. assert(p_stream != 00);
  2275. l_cp = &(p_j2k->m_cp);
  2276. l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
  2277. l_code_size = 9 + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
  2278. p_j2k->m_current_tile_number, 0);
  2279. l_remaining_size = l_code_size;
  2280. if (l_code_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2281. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2282. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size);
  2283. if (! new_header_tile_data) {
  2284. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2285. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2286. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2287. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COD marker\n");
  2288. return OPJ_FALSE;
  2289. }
  2290. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2291. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_code_size;
  2292. }
  2293. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  2294. opj_write_bytes(l_current_data, J2K_MS_COD, 2); /* COD */
  2295. l_current_data += 2;
  2296. opj_write_bytes(l_current_data, l_code_size - 2, 2); /* L_COD */
  2297. l_current_data += 2;
  2298. opj_write_bytes(l_current_data, l_tcp->csty, 1); /* Scod */
  2299. ++l_current_data;
  2300. opj_write_bytes(l_current_data, (OPJ_UINT32)l_tcp->prg, 1); /* SGcod (A) */
  2301. ++l_current_data;
  2302. opj_write_bytes(l_current_data, l_tcp->numlayers, 2); /* SGcod (B) */
  2303. l_current_data += 2;
  2304. opj_write_bytes(l_current_data, l_tcp->mct, 1); /* SGcod (C) */
  2305. ++l_current_data;
  2306. l_remaining_size -= 9;
  2307. if (! opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0,
  2308. l_current_data, &l_remaining_size, p_manager)) {
  2309. opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
  2310. return OPJ_FALSE;
  2311. }
  2312. if (l_remaining_size != 0) {
  2313. opj_event_msg(p_manager, EVT_ERROR, "Error writing COD marker\n");
  2314. return OPJ_FALSE;
  2315. }
  2316. if (opj_stream_write_data(p_stream,
  2317. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_code_size,
  2318. p_manager) != l_code_size) {
  2319. return OPJ_FALSE;
  2320. }
  2321. return OPJ_TRUE;
  2322. }
  2323. /**
  2324. * Reads a COD marker (Coding style defaults)
  2325. * @param p_header_data the data contained in the COD box.
  2326. * @param p_j2k the jpeg2000 codec.
  2327. * @param p_header_size the size of the data contained in the COD marker.
  2328. * @param p_manager the user event manager.
  2329. */
  2330. static OPJ_BOOL opj_j2k_read_cod(opj_j2k_t *p_j2k,
  2331. OPJ_BYTE * p_header_data,
  2332. OPJ_UINT32 p_header_size,
  2333. opj_event_mgr_t * p_manager
  2334. )
  2335. {
  2336. /* loop */
  2337. OPJ_UINT32 i;
  2338. OPJ_UINT32 l_tmp;
  2339. opj_cp_t *l_cp = 00;
  2340. opj_tcp_t *l_tcp = 00;
  2341. opj_image_t *l_image = 00;
  2342. /* preconditions */
  2343. assert(p_header_data != 00);
  2344. assert(p_j2k != 00);
  2345. assert(p_manager != 00);
  2346. l_image = p_j2k->m_private_image;
  2347. l_cp = &(p_j2k->m_cp);
  2348. /* If we are in the first tile-part header of the current tile */
  2349. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
  2350. &l_cp->tcps[p_j2k->m_current_tile_number] :
  2351. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  2352. #if 0
  2353. /* This check was added per https://github.com/uclouvain/openjpeg/commit/daed8cc9195555e101ab708a501af2dfe6d5e001 */
  2354. /* but this is no longer necessary to handle issue476.jp2 */
  2355. /* and this actually cause issues on legit files. See https://github.com/uclouvain/openjpeg/issues/1043 */
  2356. /* Only one COD per tile */
  2357. if (l_tcp->cod) {
  2358. opj_event_msg(p_manager, EVT_ERROR,
  2359. "COD marker already read. No more than one COD marker per tile.\n");
  2360. return OPJ_FALSE;
  2361. }
  2362. #endif
  2363. l_tcp->cod = 1;
  2364. /* Make sure room is sufficient */
  2365. if (p_header_size < 5) {
  2366. opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
  2367. return OPJ_FALSE;
  2368. }
  2369. opj_read_bytes(p_header_data, &l_tcp->csty, 1); /* Scod */
  2370. ++p_header_data;
  2371. /* Make sure we know how to decode this */
  2372. if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP |
  2373. J2K_CP_CSTY_EPH)) != 0U) {
  2374. opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n");
  2375. return OPJ_FALSE;
  2376. }
  2377. opj_read_bytes(p_header_data, &l_tmp, 1); /* SGcod (A) */
  2378. ++p_header_data;
  2379. l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
  2380. /* Make sure progression order is valid */
  2381. if (l_tcp->prg > OPJ_CPRL) {
  2382. opj_event_msg(p_manager, EVT_ERROR,
  2383. "Unknown progression order in COD marker\n");
  2384. l_tcp->prg = OPJ_PROG_UNKNOWN;
  2385. }
  2386. opj_read_bytes(p_header_data, &l_tcp->numlayers, 2); /* SGcod (B) */
  2387. p_header_data += 2;
  2388. if ((l_tcp->numlayers < 1U) || (l_tcp->numlayers > 65535U)) {
  2389. opj_event_msg(p_manager, EVT_ERROR,
  2390. "Invalid number of layers in COD marker : %d not in range [1-65535]\n",
  2391. l_tcp->numlayers);
  2392. return OPJ_FALSE;
  2393. }
  2394. /* If user didn't set a number layer to decode take the max specify in the codestream. */
  2395. if (l_cp->m_specific_param.m_dec.m_layer) {
  2396. l_tcp->num_layers_to_decode = l_cp->m_specific_param.m_dec.m_layer;
  2397. } else {
  2398. l_tcp->num_layers_to_decode = l_tcp->numlayers;
  2399. }
  2400. opj_read_bytes(p_header_data, &l_tcp->mct, 1); /* SGcod (C) */
  2401. ++p_header_data;
  2402. if (l_tcp->mct > 1) {
  2403. opj_event_msg(p_manager, EVT_ERROR,
  2404. "Invalid multiple component transformation\n");
  2405. return OPJ_FALSE;
  2406. }
  2407. p_header_size -= 5;
  2408. for (i = 0; i < l_image->numcomps; ++i) {
  2409. l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
  2410. }
  2411. if (! opj_j2k_read_SPCod_SPCoc(p_j2k, 0, p_header_data, &p_header_size,
  2412. p_manager)) {
  2413. opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
  2414. return OPJ_FALSE;
  2415. }
  2416. if (p_header_size != 0) {
  2417. opj_event_msg(p_manager, EVT_ERROR, "Error reading COD marker\n");
  2418. return OPJ_FALSE;
  2419. }
  2420. /* Apply the coding style to other components of the current tile or the m_default_tcp*/
  2421. opj_j2k_copy_tile_component_parameters(p_j2k);
  2422. /* Index */
  2423. #ifdef WIP_REMOVE_MSD
  2424. if (p_j2k->cstr_info) {
  2425. /*opj_codestream_info_t *l_cstr_info = p_j2k->cstr_info;*/
  2426. p_j2k->cstr_info->prog = l_tcp->prg;
  2427. p_j2k->cstr_info->numlayers = l_tcp->numlayers;
  2428. p_j2k->cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(
  2429. l_image->numcomps * sizeof(OPJ_UINT32));
  2430. if (!p_j2k->cstr_info->numdecompos) {
  2431. return OPJ_FALSE;
  2432. }
  2433. for (i = 0; i < l_image->numcomps; ++i) {
  2434. p_j2k->cstr_info->numdecompos[i] = l_tcp->tccps[i].numresolutions - 1;
  2435. }
  2436. }
  2437. #endif
  2438. return OPJ_TRUE;
  2439. }
  2440. static OPJ_BOOL opj_j2k_write_coc(opj_j2k_t *p_j2k,
  2441. OPJ_UINT32 p_comp_no,
  2442. opj_stream_private_t *p_stream,
  2443. opj_event_mgr_t * p_manager)
  2444. {
  2445. OPJ_UINT32 l_coc_size, l_remaining_size;
  2446. OPJ_UINT32 l_comp_room;
  2447. /* preconditions */
  2448. assert(p_j2k != 00);
  2449. assert(p_manager != 00);
  2450. assert(p_stream != 00);
  2451. l_comp_room = (p_j2k->m_private_image->numcomps <= 256) ? 1 : 2;
  2452. l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
  2453. p_j2k->m_current_tile_number, p_comp_no);
  2454. if (l_coc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2455. OPJ_BYTE *new_header_tile_data;
  2456. /*p_j2k->m_specific_param.m_encoder.m_header_tile_data
  2457. = (OPJ_BYTE*)opj_realloc(
  2458. p_j2k->m_specific_param.m_encoder.m_header_tile_data,
  2459. l_coc_size);*/
  2460. new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2461. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size);
  2462. if (! new_header_tile_data) {
  2463. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2464. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2465. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2466. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write COC marker\n");
  2467. return OPJ_FALSE;
  2468. }
  2469. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2470. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_coc_size;
  2471. }
  2472. opj_j2k_write_coc_in_memory(p_j2k, p_comp_no,
  2473. p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size,
  2474. p_manager);
  2475. if (opj_stream_write_data(p_stream,
  2476. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_coc_size,
  2477. p_manager) != l_coc_size) {
  2478. return OPJ_FALSE;
  2479. }
  2480. return OPJ_TRUE;
  2481. }
  2482. static OPJ_BOOL opj_j2k_compare_coc(opj_j2k_t *p_j2k,
  2483. OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
  2484. {
  2485. opj_cp_t *l_cp = NULL;
  2486. opj_tcp_t *l_tcp = NULL;
  2487. /* preconditions */
  2488. assert(p_j2k != 00);
  2489. l_cp = &(p_j2k->m_cp);
  2490. l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
  2491. if (l_tcp->tccps[p_first_comp_no].csty != l_tcp->tccps[p_second_comp_no].csty) {
  2492. return OPJ_FALSE;
  2493. }
  2494. return opj_j2k_compare_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number,
  2495. p_first_comp_no, p_second_comp_no);
  2496. }
  2497. static void opj_j2k_write_coc_in_memory(opj_j2k_t *p_j2k,
  2498. OPJ_UINT32 p_comp_no,
  2499. OPJ_BYTE * p_data,
  2500. OPJ_UINT32 * p_data_written,
  2501. opj_event_mgr_t * p_manager
  2502. )
  2503. {
  2504. opj_cp_t *l_cp = 00;
  2505. opj_tcp_t *l_tcp = 00;
  2506. OPJ_UINT32 l_coc_size, l_remaining_size;
  2507. OPJ_BYTE * l_current_data = 00;
  2508. opj_image_t *l_image = 00;
  2509. OPJ_UINT32 l_comp_room;
  2510. /* preconditions */
  2511. assert(p_j2k != 00);
  2512. assert(p_manager != 00);
  2513. l_cp = &(p_j2k->m_cp);
  2514. l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
  2515. l_image = p_j2k->m_private_image;
  2516. l_comp_room = (l_image->numcomps <= 256) ? 1 : 2;
  2517. l_coc_size = 5 + l_comp_room + opj_j2k_get_SPCod_SPCoc_size(p_j2k,
  2518. p_j2k->m_current_tile_number, p_comp_no);
  2519. l_remaining_size = l_coc_size;
  2520. l_current_data = p_data;
  2521. opj_write_bytes(l_current_data, J2K_MS_COC,
  2522. 2); /* COC */
  2523. l_current_data += 2;
  2524. opj_write_bytes(l_current_data, l_coc_size - 2,
  2525. 2); /* L_COC */
  2526. l_current_data += 2;
  2527. opj_write_bytes(l_current_data, p_comp_no, l_comp_room); /* Ccoc */
  2528. l_current_data += l_comp_room;
  2529. opj_write_bytes(l_current_data, l_tcp->tccps[p_comp_no].csty,
  2530. 1); /* Scoc */
  2531. ++l_current_data;
  2532. l_remaining_size -= (5 + l_comp_room);
  2533. opj_j2k_write_SPCod_SPCoc(p_j2k, p_j2k->m_current_tile_number, 0,
  2534. l_current_data, &l_remaining_size, p_manager);
  2535. * p_data_written = l_coc_size;
  2536. }
  2537. static OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
  2538. {
  2539. OPJ_UINT32 i, j;
  2540. OPJ_UINT32 l_nb_comp;
  2541. OPJ_UINT32 l_nb_tiles;
  2542. OPJ_UINT32 l_max = 0;
  2543. /* preconditions */
  2544. l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
  2545. l_nb_comp = p_j2k->m_private_image->numcomps;
  2546. for (i = 0; i < l_nb_tiles; ++i) {
  2547. for (j = 0; j < l_nb_comp; ++j) {
  2548. l_max = opj_uint_max(l_max, opj_j2k_get_SPCod_SPCoc_size(p_j2k, i, j));
  2549. }
  2550. }
  2551. return 6 + l_max;
  2552. }
  2553. /**
  2554. * Reads a COC marker (Coding Style Component)
  2555. * @param p_header_data the data contained in the COC box.
  2556. * @param p_j2k the jpeg2000 codec.
  2557. * @param p_header_size the size of the data contained in the COC marker.
  2558. * @param p_manager the user event manager.
  2559. */
  2560. static OPJ_BOOL opj_j2k_read_coc(opj_j2k_t *p_j2k,
  2561. OPJ_BYTE * p_header_data,
  2562. OPJ_UINT32 p_header_size,
  2563. opj_event_mgr_t * p_manager
  2564. )
  2565. {
  2566. opj_cp_t *l_cp = NULL;
  2567. opj_tcp_t *l_tcp = NULL;
  2568. opj_image_t *l_image = NULL;
  2569. OPJ_UINT32 l_comp_room;
  2570. OPJ_UINT32 l_comp_no;
  2571. /* preconditions */
  2572. assert(p_header_data != 00);
  2573. assert(p_j2k != 00);
  2574. assert(p_manager != 00);
  2575. l_cp = &(p_j2k->m_cp);
  2576. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
  2577. ?
  2578. &l_cp->tcps[p_j2k->m_current_tile_number] :
  2579. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  2580. l_image = p_j2k->m_private_image;
  2581. l_comp_room = l_image->numcomps <= 256 ? 1 : 2;
  2582. /* make sure room is sufficient*/
  2583. if (p_header_size < l_comp_room + 1) {
  2584. opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
  2585. return OPJ_FALSE;
  2586. }
  2587. p_header_size -= l_comp_room + 1;
  2588. opj_read_bytes(p_header_data, &l_comp_no,
  2589. l_comp_room); /* Ccoc */
  2590. p_header_data += l_comp_room;
  2591. if (l_comp_no >= l_image->numcomps) {
  2592. opj_event_msg(p_manager, EVT_ERROR,
  2593. "Error reading COC marker (bad number of components)\n");
  2594. return OPJ_FALSE;
  2595. }
  2596. opj_read_bytes(p_header_data, &l_tcp->tccps[l_comp_no].csty,
  2597. 1); /* Scoc */
  2598. ++p_header_data ;
  2599. if (! opj_j2k_read_SPCod_SPCoc(p_j2k, l_comp_no, p_header_data, &p_header_size,
  2600. p_manager)) {
  2601. opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
  2602. return OPJ_FALSE;
  2603. }
  2604. if (p_header_size != 0) {
  2605. opj_event_msg(p_manager, EVT_ERROR, "Error reading COC marker\n");
  2606. return OPJ_FALSE;
  2607. }
  2608. return OPJ_TRUE;
  2609. }
  2610. static OPJ_BOOL opj_j2k_write_qcd(opj_j2k_t *p_j2k,
  2611. opj_stream_private_t *p_stream,
  2612. opj_event_mgr_t * p_manager
  2613. )
  2614. {
  2615. OPJ_UINT32 l_qcd_size, l_remaining_size;
  2616. OPJ_BYTE * l_current_data = 00;
  2617. /* preconditions */
  2618. assert(p_j2k != 00);
  2619. assert(p_manager != 00);
  2620. assert(p_stream != 00);
  2621. l_qcd_size = 4 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
  2622. 0);
  2623. l_remaining_size = l_qcd_size;
  2624. if (l_qcd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2625. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2626. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size);
  2627. if (! new_header_tile_data) {
  2628. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2629. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2630. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2631. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCD marker\n");
  2632. return OPJ_FALSE;
  2633. }
  2634. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2635. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcd_size;
  2636. }
  2637. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  2638. opj_write_bytes(l_current_data, J2K_MS_QCD, 2); /* QCD */
  2639. l_current_data += 2;
  2640. opj_write_bytes(l_current_data, l_qcd_size - 2, 2); /* L_QCD */
  2641. l_current_data += 2;
  2642. l_remaining_size -= 4;
  2643. if (! opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, 0,
  2644. l_current_data, &l_remaining_size, p_manager)) {
  2645. opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
  2646. return OPJ_FALSE;
  2647. }
  2648. if (l_remaining_size != 0) {
  2649. opj_event_msg(p_manager, EVT_ERROR, "Error writing QCD marker\n");
  2650. return OPJ_FALSE;
  2651. }
  2652. if (opj_stream_write_data(p_stream,
  2653. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcd_size,
  2654. p_manager) != l_qcd_size) {
  2655. return OPJ_FALSE;
  2656. }
  2657. return OPJ_TRUE;
  2658. }
  2659. /**
  2660. * Reads a QCD marker (Quantization defaults)
  2661. * @param p_header_data the data contained in the QCD box.
  2662. * @param p_j2k the jpeg2000 codec.
  2663. * @param p_header_size the size of the data contained in the QCD marker.
  2664. * @param p_manager the user event manager.
  2665. */
  2666. static OPJ_BOOL opj_j2k_read_qcd(opj_j2k_t *p_j2k,
  2667. OPJ_BYTE * p_header_data,
  2668. OPJ_UINT32 p_header_size,
  2669. opj_event_mgr_t * p_manager
  2670. )
  2671. {
  2672. /* preconditions */
  2673. assert(p_header_data != 00);
  2674. assert(p_j2k != 00);
  2675. assert(p_manager != 00);
  2676. if (! opj_j2k_read_SQcd_SQcc(p_j2k, 0, p_header_data, &p_header_size,
  2677. p_manager)) {
  2678. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
  2679. return OPJ_FALSE;
  2680. }
  2681. if (p_header_size != 0) {
  2682. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCD marker\n");
  2683. return OPJ_FALSE;
  2684. }
  2685. /* Apply the quantization parameters to other components of the current tile or the m_default_tcp */
  2686. opj_j2k_copy_tile_quantization_parameters(p_j2k);
  2687. return OPJ_TRUE;
  2688. }
  2689. static OPJ_BOOL opj_j2k_write_qcc(opj_j2k_t *p_j2k,
  2690. OPJ_UINT32 p_comp_no,
  2691. opj_stream_private_t *p_stream,
  2692. opj_event_mgr_t * p_manager
  2693. )
  2694. {
  2695. OPJ_UINT32 l_qcc_size, l_remaining_size;
  2696. /* preconditions */
  2697. assert(p_j2k != 00);
  2698. assert(p_manager != 00);
  2699. assert(p_stream != 00);
  2700. l_qcc_size = 5 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
  2701. p_comp_no);
  2702. l_qcc_size += p_j2k->m_private_image->numcomps <= 256 ? 0 : 1;
  2703. l_remaining_size = l_qcc_size;
  2704. if (l_qcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2705. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2706. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size);
  2707. if (! new_header_tile_data) {
  2708. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2709. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2710. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2711. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write QCC marker\n");
  2712. return OPJ_FALSE;
  2713. }
  2714. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2715. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_qcc_size;
  2716. }
  2717. opj_j2k_write_qcc_in_memory(p_j2k, p_comp_no,
  2718. p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_remaining_size,
  2719. p_manager);
  2720. if (opj_stream_write_data(p_stream,
  2721. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_qcc_size,
  2722. p_manager) != l_qcc_size) {
  2723. return OPJ_FALSE;
  2724. }
  2725. return OPJ_TRUE;
  2726. }
  2727. static OPJ_BOOL opj_j2k_compare_qcc(opj_j2k_t *p_j2k,
  2728. OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
  2729. {
  2730. return opj_j2k_compare_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number,
  2731. p_first_comp_no, p_second_comp_no);
  2732. }
  2733. static void opj_j2k_write_qcc_in_memory(opj_j2k_t *p_j2k,
  2734. OPJ_UINT32 p_comp_no,
  2735. OPJ_BYTE * p_data,
  2736. OPJ_UINT32 * p_data_written,
  2737. opj_event_mgr_t * p_manager
  2738. )
  2739. {
  2740. OPJ_UINT32 l_qcc_size, l_remaining_size;
  2741. OPJ_BYTE * l_current_data = 00;
  2742. /* preconditions */
  2743. assert(p_j2k != 00);
  2744. assert(p_manager != 00);
  2745. l_qcc_size = 6 + opj_j2k_get_SQcd_SQcc_size(p_j2k, p_j2k->m_current_tile_number,
  2746. p_comp_no);
  2747. l_remaining_size = l_qcc_size;
  2748. l_current_data = p_data;
  2749. opj_write_bytes(l_current_data, J2K_MS_QCC, 2); /* QCC */
  2750. l_current_data += 2;
  2751. if (p_j2k->m_private_image->numcomps <= 256) {
  2752. --l_qcc_size;
  2753. opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */
  2754. l_current_data += 2;
  2755. opj_write_bytes(l_current_data, p_comp_no, 1); /* Cqcc */
  2756. ++l_current_data;
  2757. /* in the case only one byte is sufficient the last byte allocated is useless -> still do -6 for available */
  2758. l_remaining_size -= 6;
  2759. } else {
  2760. opj_write_bytes(l_current_data, l_qcc_size - 2, 2); /* L_QCC */
  2761. l_current_data += 2;
  2762. opj_write_bytes(l_current_data, p_comp_no, 2); /* Cqcc */
  2763. l_current_data += 2;
  2764. l_remaining_size -= 6;
  2765. }
  2766. opj_j2k_write_SQcd_SQcc(p_j2k, p_j2k->m_current_tile_number, p_comp_no,
  2767. l_current_data, &l_remaining_size, p_manager);
  2768. *p_data_written = l_qcc_size;
  2769. }
  2770. static OPJ_UINT32 opj_j2k_get_max_qcc_size(opj_j2k_t *p_j2k)
  2771. {
  2772. return opj_j2k_get_max_coc_size(p_j2k);
  2773. }
  2774. /**
  2775. * Reads a QCC marker (Quantization component)
  2776. * @param p_header_data the data contained in the QCC box.
  2777. * @param p_j2k the jpeg2000 codec.
  2778. * @param p_header_size the size of the data contained in the QCC marker.
  2779. * @param p_manager the user event manager.
  2780. */
  2781. static OPJ_BOOL opj_j2k_read_qcc(opj_j2k_t *p_j2k,
  2782. OPJ_BYTE * p_header_data,
  2783. OPJ_UINT32 p_header_size,
  2784. opj_event_mgr_t * p_manager
  2785. )
  2786. {
  2787. OPJ_UINT32 l_num_comp, l_comp_no;
  2788. /* preconditions */
  2789. assert(p_header_data != 00);
  2790. assert(p_j2k != 00);
  2791. assert(p_manager != 00);
  2792. l_num_comp = p_j2k->m_private_image->numcomps;
  2793. if (l_num_comp <= 256) {
  2794. if (p_header_size < 1) {
  2795. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
  2796. return OPJ_FALSE;
  2797. }
  2798. opj_read_bytes(p_header_data, &l_comp_no, 1);
  2799. ++p_header_data;
  2800. --p_header_size;
  2801. } else {
  2802. if (p_header_size < 2) {
  2803. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
  2804. return OPJ_FALSE;
  2805. }
  2806. opj_read_bytes(p_header_data, &l_comp_no, 2);
  2807. p_header_data += 2;
  2808. p_header_size -= 2;
  2809. }
  2810. #ifdef USE_JPWL
  2811. if (p_j2k->m_cp.correct) {
  2812. static OPJ_UINT32 backup_compno = 0;
  2813. /* compno is negative or larger than the number of components!!! */
  2814. if (/*(l_comp_no < 0) ||*/ (l_comp_no >= l_num_comp)) {
  2815. opj_event_msg(p_manager, EVT_ERROR,
  2816. "JPWL: bad component number in QCC (%d out of a maximum of %d)\n",
  2817. l_comp_no, l_num_comp);
  2818. if (!JPWL_ASSUME) {
  2819. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  2820. return OPJ_FALSE;
  2821. }
  2822. /* we try to correct */
  2823. l_comp_no = backup_compno % l_num_comp;
  2824. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
  2825. "- setting component number to %d\n",
  2826. l_comp_no);
  2827. }
  2828. /* keep your private count of tiles */
  2829. backup_compno++;
  2830. };
  2831. #endif /* USE_JPWL */
  2832. if (l_comp_no >= p_j2k->m_private_image->numcomps) {
  2833. opj_event_msg(p_manager, EVT_ERROR,
  2834. "Invalid component number: %d, regarding the number of components %d\n",
  2835. l_comp_no, p_j2k->m_private_image->numcomps);
  2836. return OPJ_FALSE;
  2837. }
  2838. if (! opj_j2k_read_SQcd_SQcc(p_j2k, l_comp_no, p_header_data, &p_header_size,
  2839. p_manager)) {
  2840. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
  2841. return OPJ_FALSE;
  2842. }
  2843. if (p_header_size != 0) {
  2844. opj_event_msg(p_manager, EVT_ERROR, "Error reading QCC marker\n");
  2845. return OPJ_FALSE;
  2846. }
  2847. return OPJ_TRUE;
  2848. }
  2849. static OPJ_BOOL opj_j2k_write_poc(opj_j2k_t *p_j2k,
  2850. opj_stream_private_t *p_stream,
  2851. opj_event_mgr_t * p_manager
  2852. )
  2853. {
  2854. OPJ_UINT32 l_nb_comp;
  2855. OPJ_UINT32 l_nb_poc;
  2856. OPJ_UINT32 l_poc_size;
  2857. OPJ_UINT32 l_written_size = 0;
  2858. opj_tcp_t *l_tcp = 00;
  2859. OPJ_UINT32 l_poc_room;
  2860. /* preconditions */
  2861. assert(p_j2k != 00);
  2862. assert(p_manager != 00);
  2863. assert(p_stream != 00);
  2864. l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
  2865. l_nb_comp = p_j2k->m_private_image->numcomps;
  2866. l_nb_poc = 1 + l_tcp->numpocs;
  2867. if (l_nb_comp <= 256) {
  2868. l_poc_room = 1;
  2869. } else {
  2870. l_poc_room = 2;
  2871. }
  2872. l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
  2873. if (l_poc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  2874. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  2875. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size);
  2876. if (! new_header_tile_data) {
  2877. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  2878. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  2879. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  2880. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write POC marker\n");
  2881. return OPJ_FALSE;
  2882. }
  2883. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  2884. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_poc_size;
  2885. }
  2886. opj_j2k_write_poc_in_memory(p_j2k,
  2887. p_j2k->m_specific_param.m_encoder.m_header_tile_data, &l_written_size,
  2888. p_manager);
  2889. if (opj_stream_write_data(p_stream,
  2890. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_poc_size,
  2891. p_manager) != l_poc_size) {
  2892. return OPJ_FALSE;
  2893. }
  2894. return OPJ_TRUE;
  2895. }
  2896. static void opj_j2k_write_poc_in_memory(opj_j2k_t *p_j2k,
  2897. OPJ_BYTE * p_data,
  2898. OPJ_UINT32 * p_data_written,
  2899. opj_event_mgr_t * p_manager
  2900. )
  2901. {
  2902. OPJ_UINT32 i;
  2903. OPJ_BYTE * l_current_data = 00;
  2904. OPJ_UINT32 l_nb_comp;
  2905. OPJ_UINT32 l_nb_poc;
  2906. OPJ_UINT32 l_poc_size;
  2907. opj_image_t *l_image = 00;
  2908. opj_tcp_t *l_tcp = 00;
  2909. opj_tccp_t *l_tccp = 00;
  2910. opj_poc_t *l_current_poc = 00;
  2911. OPJ_UINT32 l_poc_room;
  2912. /* preconditions */
  2913. assert(p_j2k != 00);
  2914. assert(p_manager != 00);
  2915. OPJ_UNUSED(p_manager);
  2916. l_tcp = &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number];
  2917. l_tccp = &l_tcp->tccps[0];
  2918. l_image = p_j2k->m_private_image;
  2919. l_nb_comp = l_image->numcomps;
  2920. l_nb_poc = 1 + l_tcp->numpocs;
  2921. if (l_nb_comp <= 256) {
  2922. l_poc_room = 1;
  2923. } else {
  2924. l_poc_room = 2;
  2925. }
  2926. l_poc_size = 4 + (5 + 2 * l_poc_room) * l_nb_poc;
  2927. l_current_data = p_data;
  2928. opj_write_bytes(l_current_data, J2K_MS_POC,
  2929. 2); /* POC */
  2930. l_current_data += 2;
  2931. opj_write_bytes(l_current_data, l_poc_size - 2,
  2932. 2); /* Lpoc */
  2933. l_current_data += 2;
  2934. l_current_poc = l_tcp->pocs;
  2935. for (i = 0; i < l_nb_poc; ++i) {
  2936. opj_write_bytes(l_current_data, l_current_poc->resno0,
  2937. 1); /* RSpoc_i */
  2938. ++l_current_data;
  2939. opj_write_bytes(l_current_data, l_current_poc->compno0,
  2940. l_poc_room); /* CSpoc_i */
  2941. l_current_data += l_poc_room;
  2942. opj_write_bytes(l_current_data, l_current_poc->layno1,
  2943. 2); /* LYEpoc_i */
  2944. l_current_data += 2;
  2945. opj_write_bytes(l_current_data, l_current_poc->resno1,
  2946. 1); /* REpoc_i */
  2947. ++l_current_data;
  2948. opj_write_bytes(l_current_data, l_current_poc->compno1,
  2949. l_poc_room); /* CEpoc_i */
  2950. l_current_data += l_poc_room;
  2951. opj_write_bytes(l_current_data, (OPJ_UINT32)l_current_poc->prg,
  2952. 1); /* Ppoc_i */
  2953. ++l_current_data;
  2954. /* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
  2955. l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
  2956. l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
  2957. l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
  2958. l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
  2959. l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)
  2960. l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
  2961. ++l_current_poc;
  2962. }
  2963. *p_data_written = l_poc_size;
  2964. }
  2965. static OPJ_UINT32 opj_j2k_get_max_poc_size(opj_j2k_t *p_j2k)
  2966. {
  2967. opj_tcp_t * l_tcp = 00;
  2968. OPJ_UINT32 l_nb_tiles = 0;
  2969. OPJ_UINT32 l_max_poc = 0;
  2970. OPJ_UINT32 i;
  2971. l_tcp = p_j2k->m_cp.tcps;
  2972. l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  2973. for (i = 0; i < l_nb_tiles; ++i) {
  2974. l_max_poc = opj_uint_max(l_max_poc, l_tcp->numpocs);
  2975. ++l_tcp;
  2976. }
  2977. ++l_max_poc;
  2978. return 4 + 9 * l_max_poc;
  2979. }
  2980. static OPJ_UINT32 opj_j2k_get_max_toc_size(opj_j2k_t *p_j2k)
  2981. {
  2982. OPJ_UINT32 i;
  2983. OPJ_UINT32 l_nb_tiles;
  2984. OPJ_UINT32 l_max = 0;
  2985. opj_tcp_t * l_tcp = 00;
  2986. l_tcp = p_j2k->m_cp.tcps;
  2987. l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th ;
  2988. for (i = 0; i < l_nb_tiles; ++i) {
  2989. l_max = opj_uint_max(l_max, l_tcp->m_nb_tile_parts);
  2990. ++l_tcp;
  2991. }
  2992. return 12 * l_max;
  2993. }
  2994. static OPJ_UINT32 opj_j2k_get_specific_header_sizes(opj_j2k_t *p_j2k)
  2995. {
  2996. OPJ_UINT32 l_nb_bytes = 0;
  2997. OPJ_UINT32 l_nb_comps;
  2998. OPJ_UINT32 l_coc_bytes, l_qcc_bytes;
  2999. l_nb_comps = p_j2k->m_private_image->numcomps - 1;
  3000. l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
  3001. if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) {
  3002. l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
  3003. l_nb_bytes += l_nb_comps * l_coc_bytes;
  3004. l_qcc_bytes = opj_j2k_get_max_qcc_size(p_j2k);
  3005. l_nb_bytes += l_nb_comps * l_qcc_bytes;
  3006. }
  3007. l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
  3008. if (p_j2k->m_specific_param.m_encoder.m_PLT) {
  3009. /* Reserve space for PLT markers */
  3010. OPJ_UINT32 i;
  3011. const opj_cp_t * l_cp = &(p_j2k->m_cp);
  3012. OPJ_UINT32 l_max_packet_count = 0;
  3013. for (i = 0; i < l_cp->th * l_cp->tw; ++i) {
  3014. l_max_packet_count = opj_uint_max(l_max_packet_count,
  3015. opj_get_encoding_packet_count(p_j2k->m_private_image, l_cp, i));
  3016. }
  3017. /* Minimum 6 bytes per PLT marker, and at a minimum (taking a pessimistic */
  3018. /* estimate of 4 bytes for a packet size), one can write */
  3019. /* (65536-6) / 4 = 16382 paquet sizes per PLT marker */
  3020. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT =
  3021. 6 * opj_uint_ceildiv(l_max_packet_count, 16382);
  3022. /* Maximum 5 bytes per packet to encode a full UINT32 */
  3023. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT +=
  3024. l_nb_bytes += 5 * l_max_packet_count;
  3025. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += 1;
  3026. l_nb_bytes += p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT;
  3027. }
  3028. /*** DEVELOPER CORNER, Add room for your headers ***/
  3029. return l_nb_bytes;
  3030. }
  3031. /**
  3032. * Reads a POC marker (Progression Order Change)
  3033. *
  3034. * @param p_header_data the data contained in the POC box.
  3035. * @param p_j2k the jpeg2000 codec.
  3036. * @param p_header_size the size of the data contained in the POC marker.
  3037. * @param p_manager the user event manager.
  3038. */
  3039. static OPJ_BOOL opj_j2k_read_poc(opj_j2k_t *p_j2k,
  3040. OPJ_BYTE * p_header_data,
  3041. OPJ_UINT32 p_header_size,
  3042. opj_event_mgr_t * p_manager
  3043. )
  3044. {
  3045. OPJ_UINT32 i, l_nb_comp, l_tmp;
  3046. opj_image_t * l_image = 00;
  3047. OPJ_UINT32 l_old_poc_nb, l_current_poc_nb, l_current_poc_remaining;
  3048. OPJ_UINT32 l_chunk_size, l_comp_room;
  3049. opj_cp_t *l_cp = 00;
  3050. opj_tcp_t *l_tcp = 00;
  3051. opj_poc_t *l_current_poc = 00;
  3052. /* preconditions */
  3053. assert(p_header_data != 00);
  3054. assert(p_j2k != 00);
  3055. assert(p_manager != 00);
  3056. l_image = p_j2k->m_private_image;
  3057. l_nb_comp = l_image->numcomps;
  3058. if (l_nb_comp <= 256) {
  3059. l_comp_room = 1;
  3060. } else {
  3061. l_comp_room = 2;
  3062. }
  3063. l_chunk_size = 5 + 2 * l_comp_room;
  3064. l_current_poc_nb = p_header_size / l_chunk_size;
  3065. l_current_poc_remaining = p_header_size % l_chunk_size;
  3066. if ((l_current_poc_nb <= 0) || (l_current_poc_remaining != 0)) {
  3067. opj_event_msg(p_manager, EVT_ERROR, "Error reading POC marker\n");
  3068. return OPJ_FALSE;
  3069. }
  3070. l_cp = &(p_j2k->m_cp);
  3071. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
  3072. &l_cp->tcps[p_j2k->m_current_tile_number] :
  3073. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  3074. l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
  3075. l_current_poc_nb += l_old_poc_nb;
  3076. if (l_current_poc_nb >= J2K_MAX_POCS) {
  3077. opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
  3078. return OPJ_FALSE;
  3079. }
  3080. /* now poc is in use.*/
  3081. l_tcp->POC = 1;
  3082. l_current_poc = &l_tcp->pocs[l_old_poc_nb];
  3083. for (i = l_old_poc_nb; i < l_current_poc_nb; ++i) {
  3084. opj_read_bytes(p_header_data, &(l_current_poc->resno0),
  3085. 1); /* RSpoc_i */
  3086. ++p_header_data;
  3087. opj_read_bytes(p_header_data, &(l_current_poc->compno0),
  3088. l_comp_room); /* CSpoc_i */
  3089. p_header_data += l_comp_room;
  3090. opj_read_bytes(p_header_data, &(l_current_poc->layno1),
  3091. 2); /* LYEpoc_i */
  3092. /* make sure layer end is in acceptable bounds */
  3093. l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
  3094. p_header_data += 2;
  3095. opj_read_bytes(p_header_data, &(l_current_poc->resno1),
  3096. 1); /* REpoc_i */
  3097. ++p_header_data;
  3098. opj_read_bytes(p_header_data, &(l_current_poc->compno1),
  3099. l_comp_room); /* CEpoc_i */
  3100. p_header_data += l_comp_room;
  3101. opj_read_bytes(p_header_data, &l_tmp,
  3102. 1); /* Ppoc_i */
  3103. ++p_header_data;
  3104. l_current_poc->prg = (OPJ_PROG_ORDER) l_tmp;
  3105. /* make sure comp is in acceptable bounds */
  3106. l_current_poc->compno1 = opj_uint_min(l_current_poc->compno1, l_nb_comp);
  3107. ++l_current_poc;
  3108. }
  3109. l_tcp->numpocs = l_current_poc_nb - 1;
  3110. return OPJ_TRUE;
  3111. }
  3112. /**
  3113. * Reads a CRG marker (Component registration)
  3114. *
  3115. * @param p_header_data the data contained in the TLM box.
  3116. * @param p_j2k the jpeg2000 codec.
  3117. * @param p_header_size the size of the data contained in the TLM marker.
  3118. * @param p_manager the user event manager.
  3119. */
  3120. static OPJ_BOOL opj_j2k_read_crg(opj_j2k_t *p_j2k,
  3121. OPJ_BYTE * p_header_data,
  3122. OPJ_UINT32 p_header_size,
  3123. opj_event_mgr_t * p_manager
  3124. )
  3125. {
  3126. OPJ_UINT32 l_nb_comp;
  3127. /* preconditions */
  3128. assert(p_header_data != 00);
  3129. assert(p_j2k != 00);
  3130. assert(p_manager != 00);
  3131. OPJ_UNUSED(p_header_data);
  3132. l_nb_comp = p_j2k->m_private_image->numcomps;
  3133. if (p_header_size != l_nb_comp * 4) {
  3134. opj_event_msg(p_manager, EVT_ERROR, "Error reading CRG marker\n");
  3135. return OPJ_FALSE;
  3136. }
  3137. /* Do not care of this at the moment since only local variables are set here */
  3138. /*
  3139. for
  3140. (i = 0; i < l_nb_comp; ++i)
  3141. {
  3142. opj_read_bytes(p_header_data,&l_Xcrg_i,2); // Xcrg_i
  3143. p_header_data+=2;
  3144. opj_read_bytes(p_header_data,&l_Ycrg_i,2); // Xcrg_i
  3145. p_header_data+=2;
  3146. }
  3147. */
  3148. return OPJ_TRUE;
  3149. }
  3150. /**
  3151. * Reads a TLM marker (Tile Length Marker)
  3152. *
  3153. * @param p_header_data the data contained in the TLM box.
  3154. * @param p_j2k the jpeg2000 codec.
  3155. * @param p_header_size the size of the data contained in the TLM marker.
  3156. * @param p_manager the user event manager.
  3157. */
  3158. static OPJ_BOOL opj_j2k_read_tlm(opj_j2k_t *p_j2k,
  3159. OPJ_BYTE * p_header_data,
  3160. OPJ_UINT32 p_header_size,
  3161. opj_event_mgr_t * p_manager
  3162. )
  3163. {
  3164. OPJ_UINT32 l_Ztlm, l_Stlm, l_ST, l_SP, l_tot_num_tp_remaining, l_quotient,
  3165. l_Ptlm_size;
  3166. /* preconditions */
  3167. assert(p_header_data != 00);
  3168. assert(p_j2k != 00);
  3169. assert(p_manager != 00);
  3170. OPJ_UNUSED(p_j2k);
  3171. if (p_header_size < 2) {
  3172. opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
  3173. return OPJ_FALSE;
  3174. }
  3175. p_header_size -= 2;
  3176. opj_read_bytes(p_header_data, &l_Ztlm,
  3177. 1); /* Ztlm */
  3178. ++p_header_data;
  3179. opj_read_bytes(p_header_data, &l_Stlm,
  3180. 1); /* Stlm */
  3181. ++p_header_data;
  3182. l_ST = ((l_Stlm >> 4) & 0x3);
  3183. l_SP = (l_Stlm >> 6) & 0x1;
  3184. l_Ptlm_size = (l_SP + 1) * 2;
  3185. l_quotient = l_Ptlm_size + l_ST;
  3186. l_tot_num_tp_remaining = p_header_size % l_quotient;
  3187. if (l_tot_num_tp_remaining != 0) {
  3188. opj_event_msg(p_manager, EVT_ERROR, "Error reading TLM marker\n");
  3189. return OPJ_FALSE;
  3190. }
  3191. /* FIXME Do not care of this at the moment since only local variables are set here */
  3192. /*
  3193. for
  3194. (i = 0; i < l_tot_num_tp; ++i)
  3195. {
  3196. opj_read_bytes(p_header_data,&l_Ttlm_i,l_ST); // Ttlm_i
  3197. p_header_data += l_ST;
  3198. opj_read_bytes(p_header_data,&l_Ptlm_i,l_Ptlm_size); // Ptlm_i
  3199. p_header_data += l_Ptlm_size;
  3200. }*/
  3201. return OPJ_TRUE;
  3202. }
  3203. /**
  3204. * Reads a PLM marker (Packet length, main header marker)
  3205. *
  3206. * @param p_header_data the data contained in the TLM box.
  3207. * @param p_j2k the jpeg2000 codec.
  3208. * @param p_header_size the size of the data contained in the TLM marker.
  3209. * @param p_manager the user event manager.
  3210. */
  3211. static OPJ_BOOL opj_j2k_read_plm(opj_j2k_t *p_j2k,
  3212. OPJ_BYTE * p_header_data,
  3213. OPJ_UINT32 p_header_size,
  3214. opj_event_mgr_t * p_manager
  3215. )
  3216. {
  3217. /* preconditions */
  3218. assert(p_header_data != 00);
  3219. assert(p_j2k != 00);
  3220. assert(p_manager != 00);
  3221. OPJ_UNUSED(p_j2k);
  3222. OPJ_UNUSED(p_header_data);
  3223. if (p_header_size < 1) {
  3224. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
  3225. return OPJ_FALSE;
  3226. }
  3227. /* Do not care of this at the moment since only local variables are set here */
  3228. /*
  3229. opj_read_bytes(p_header_data,&l_Zplm,1); // Zplm
  3230. ++p_header_data;
  3231. --p_header_size;
  3232. while
  3233. (p_header_size > 0)
  3234. {
  3235. opj_read_bytes(p_header_data,&l_Nplm,1); // Nplm
  3236. ++p_header_data;
  3237. p_header_size -= (1+l_Nplm);
  3238. if
  3239. (p_header_size < 0)
  3240. {
  3241. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
  3242. return false;
  3243. }
  3244. for
  3245. (i = 0; i < l_Nplm; ++i)
  3246. {
  3247. opj_read_bytes(p_header_data,&l_tmp,1); // Iplm_ij
  3248. ++p_header_data;
  3249. // take only the last seven bytes
  3250. l_packet_len |= (l_tmp & 0x7f);
  3251. if
  3252. (l_tmp & 0x80)
  3253. {
  3254. l_packet_len <<= 7;
  3255. }
  3256. else
  3257. {
  3258. // store packet length and proceed to next packet
  3259. l_packet_len = 0;
  3260. }
  3261. }
  3262. if
  3263. (l_packet_len != 0)
  3264. {
  3265. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLM marker\n");
  3266. return false;
  3267. }
  3268. }
  3269. */
  3270. return OPJ_TRUE;
  3271. }
  3272. /**
  3273. * Reads a PLT marker (Packet length, tile-part header)
  3274. *
  3275. * @param p_header_data the data contained in the PLT box.
  3276. * @param p_j2k the jpeg2000 codec.
  3277. * @param p_header_size the size of the data contained in the PLT marker.
  3278. * @param p_manager the user event manager.
  3279. */
  3280. static OPJ_BOOL opj_j2k_read_plt(opj_j2k_t *p_j2k,
  3281. OPJ_BYTE * p_header_data,
  3282. OPJ_UINT32 p_header_size,
  3283. opj_event_mgr_t * p_manager
  3284. )
  3285. {
  3286. OPJ_UINT32 l_Zplt, l_tmp, l_packet_len = 0, i;
  3287. /* preconditions */
  3288. assert(p_header_data != 00);
  3289. assert(p_j2k != 00);
  3290. assert(p_manager != 00);
  3291. OPJ_UNUSED(p_j2k);
  3292. if (p_header_size < 1) {
  3293. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
  3294. return OPJ_FALSE;
  3295. }
  3296. opj_read_bytes(p_header_data, &l_Zplt, 1); /* Zplt */
  3297. ++p_header_data;
  3298. --p_header_size;
  3299. for (i = 0; i < p_header_size; ++i) {
  3300. opj_read_bytes(p_header_data, &l_tmp, 1); /* Iplt_ij */
  3301. ++p_header_data;
  3302. /* take only the last seven bytes */
  3303. l_packet_len |= (l_tmp & 0x7f);
  3304. if (l_tmp & 0x80) {
  3305. l_packet_len <<= 7;
  3306. } else {
  3307. /* store packet length and proceed to next packet */
  3308. l_packet_len = 0;
  3309. }
  3310. }
  3311. if (l_packet_len != 0) {
  3312. opj_event_msg(p_manager, EVT_ERROR, "Error reading PLT marker\n");
  3313. return OPJ_FALSE;
  3314. }
  3315. return OPJ_TRUE;
  3316. }
  3317. /**
  3318. * Reads a PPM marker (Packed packet headers, main header)
  3319. *
  3320. * @param p_header_data the data contained in the POC box.
  3321. * @param p_j2k the jpeg2000 codec.
  3322. * @param p_header_size the size of the data contained in the POC marker.
  3323. * @param p_manager the user event manager.
  3324. */
  3325. static OPJ_BOOL opj_j2k_read_ppm(
  3326. opj_j2k_t *p_j2k,
  3327. OPJ_BYTE * p_header_data,
  3328. OPJ_UINT32 p_header_size,
  3329. opj_event_mgr_t * p_manager)
  3330. {
  3331. opj_cp_t *l_cp = 00;
  3332. OPJ_UINT32 l_Z_ppm;
  3333. /* preconditions */
  3334. assert(p_header_data != 00);
  3335. assert(p_j2k != 00);
  3336. assert(p_manager != 00);
  3337. /* We need to have the Z_ppm element + 1 byte of Nppm/Ippm at minimum */
  3338. if (p_header_size < 2) {
  3339. opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
  3340. return OPJ_FALSE;
  3341. }
  3342. l_cp = &(p_j2k->m_cp);
  3343. l_cp->ppm = 1;
  3344. opj_read_bytes(p_header_data, &l_Z_ppm, 1); /* Z_ppm */
  3345. ++p_header_data;
  3346. --p_header_size;
  3347. /* check allocation needed */
  3348. if (l_cp->ppm_markers == NULL) { /* first PPM marker */
  3349. OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
  3350. assert(l_cp->ppm_markers_count == 0U);
  3351. l_cp->ppm_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
  3352. if (l_cp->ppm_markers == NULL) {
  3353. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
  3354. return OPJ_FALSE;
  3355. }
  3356. l_cp->ppm_markers_count = l_newCount;
  3357. } else if (l_cp->ppm_markers_count <= l_Z_ppm) {
  3358. OPJ_UINT32 l_newCount = l_Z_ppm + 1U; /* can't overflow, l_Z_ppm is UINT8 */
  3359. opj_ppx *new_ppm_markers;
  3360. new_ppm_markers = (opj_ppx *) opj_realloc(l_cp->ppm_markers,
  3361. l_newCount * sizeof(opj_ppx));
  3362. if (new_ppm_markers == NULL) {
  3363. /* clean up to be done on l_cp destruction */
  3364. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
  3365. return OPJ_FALSE;
  3366. }
  3367. l_cp->ppm_markers = new_ppm_markers;
  3368. memset(l_cp->ppm_markers + l_cp->ppm_markers_count, 0,
  3369. (l_newCount - l_cp->ppm_markers_count) * sizeof(opj_ppx));
  3370. l_cp->ppm_markers_count = l_newCount;
  3371. }
  3372. if (l_cp->ppm_markers[l_Z_ppm].m_data != NULL) {
  3373. /* clean up to be done on l_cp destruction */
  3374. opj_event_msg(p_manager, EVT_ERROR, "Zppm %u already read\n", l_Z_ppm);
  3375. return OPJ_FALSE;
  3376. }
  3377. l_cp->ppm_markers[l_Z_ppm].m_data = (OPJ_BYTE *) opj_malloc(p_header_size);
  3378. if (l_cp->ppm_markers[l_Z_ppm].m_data == NULL) {
  3379. /* clean up to be done on l_cp destruction */
  3380. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
  3381. return OPJ_FALSE;
  3382. }
  3383. l_cp->ppm_markers[l_Z_ppm].m_data_size = p_header_size;
  3384. memcpy(l_cp->ppm_markers[l_Z_ppm].m_data, p_header_data, p_header_size);
  3385. return OPJ_TRUE;
  3386. }
  3387. /**
  3388. * Merges all PPM markers read (Packed headers, main header)
  3389. *
  3390. * @param p_cp main coding parameters.
  3391. * @param p_manager the user event manager.
  3392. */
  3393. static OPJ_BOOL opj_j2k_merge_ppm(opj_cp_t *p_cp, opj_event_mgr_t * p_manager)
  3394. {
  3395. OPJ_UINT32 i, l_ppm_data_size, l_N_ppm_remaining;
  3396. /* preconditions */
  3397. assert(p_cp != 00);
  3398. assert(p_manager != 00);
  3399. assert(p_cp->ppm_buffer == NULL);
  3400. if (p_cp->ppm == 0U) {
  3401. return OPJ_TRUE;
  3402. }
  3403. l_ppm_data_size = 0U;
  3404. l_N_ppm_remaining = 0U;
  3405. for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
  3406. if (p_cp->ppm_markers[i].m_data !=
  3407. NULL) { /* standard doesn't seem to require contiguous Zppm */
  3408. OPJ_UINT32 l_N_ppm;
  3409. OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
  3410. const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
  3411. if (l_N_ppm_remaining >= l_data_size) {
  3412. l_N_ppm_remaining -= l_data_size;
  3413. l_data_size = 0U;
  3414. } else {
  3415. l_data += l_N_ppm_remaining;
  3416. l_data_size -= l_N_ppm_remaining;
  3417. l_N_ppm_remaining = 0U;
  3418. }
  3419. if (l_data_size > 0U) {
  3420. do {
  3421. /* read Nppm */
  3422. if (l_data_size < 4U) {
  3423. /* clean up to be done on l_cp destruction */
  3424. opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
  3425. return OPJ_FALSE;
  3426. }
  3427. opj_read_bytes(l_data, &l_N_ppm, 4);
  3428. l_data += 4;
  3429. l_data_size -= 4;
  3430. if (l_ppm_data_size > UINT_MAX - l_N_ppm) {
  3431. opj_event_msg(p_manager, EVT_ERROR, "Too large value for Nppm\n");
  3432. return OPJ_FALSE;
  3433. }
  3434. l_ppm_data_size += l_N_ppm;
  3435. if (l_data_size >= l_N_ppm) {
  3436. l_data_size -= l_N_ppm;
  3437. l_data += l_N_ppm;
  3438. } else {
  3439. l_N_ppm_remaining = l_N_ppm - l_data_size;
  3440. l_data_size = 0U;
  3441. }
  3442. } while (l_data_size > 0U);
  3443. }
  3444. }
  3445. }
  3446. if (l_N_ppm_remaining != 0U) {
  3447. /* clean up to be done on l_cp destruction */
  3448. opj_event_msg(p_manager, EVT_ERROR, "Corrupted PPM markers\n");
  3449. return OPJ_FALSE;
  3450. }
  3451. p_cp->ppm_buffer = (OPJ_BYTE *) opj_malloc(l_ppm_data_size);
  3452. if (p_cp->ppm_buffer == 00) {
  3453. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPM marker\n");
  3454. return OPJ_FALSE;
  3455. }
  3456. p_cp->ppm_len = l_ppm_data_size;
  3457. l_ppm_data_size = 0U;
  3458. l_N_ppm_remaining = 0U;
  3459. for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
  3460. if (p_cp->ppm_markers[i].m_data !=
  3461. NULL) { /* standard doesn't seem to require contiguous Zppm */
  3462. OPJ_UINT32 l_N_ppm;
  3463. OPJ_UINT32 l_data_size = p_cp->ppm_markers[i].m_data_size;
  3464. const OPJ_BYTE* l_data = p_cp->ppm_markers[i].m_data;
  3465. if (l_N_ppm_remaining >= l_data_size) {
  3466. memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
  3467. l_ppm_data_size += l_data_size;
  3468. l_N_ppm_remaining -= l_data_size;
  3469. l_data_size = 0U;
  3470. } else {
  3471. memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm_remaining);
  3472. l_ppm_data_size += l_N_ppm_remaining;
  3473. l_data += l_N_ppm_remaining;
  3474. l_data_size -= l_N_ppm_remaining;
  3475. l_N_ppm_remaining = 0U;
  3476. }
  3477. if (l_data_size > 0U) {
  3478. do {
  3479. /* read Nppm */
  3480. if (l_data_size < 4U) {
  3481. /* clean up to be done on l_cp destruction */
  3482. opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes to read Nppm\n");
  3483. return OPJ_FALSE;
  3484. }
  3485. opj_read_bytes(l_data, &l_N_ppm, 4);
  3486. l_data += 4;
  3487. l_data_size -= 4;
  3488. if (l_data_size >= l_N_ppm) {
  3489. memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_N_ppm);
  3490. l_ppm_data_size += l_N_ppm;
  3491. l_data_size -= l_N_ppm;
  3492. l_data += l_N_ppm;
  3493. } else {
  3494. memcpy(p_cp->ppm_buffer + l_ppm_data_size, l_data, l_data_size);
  3495. l_ppm_data_size += l_data_size;
  3496. l_N_ppm_remaining = l_N_ppm - l_data_size;
  3497. l_data_size = 0U;
  3498. }
  3499. } while (l_data_size > 0U);
  3500. }
  3501. opj_free(p_cp->ppm_markers[i].m_data);
  3502. p_cp->ppm_markers[i].m_data = NULL;
  3503. p_cp->ppm_markers[i].m_data_size = 0U;
  3504. }
  3505. }
  3506. p_cp->ppm_data = p_cp->ppm_buffer;
  3507. p_cp->ppm_data_size = p_cp->ppm_len;
  3508. p_cp->ppm_markers_count = 0U;
  3509. opj_free(p_cp->ppm_markers);
  3510. p_cp->ppm_markers = NULL;
  3511. return OPJ_TRUE;
  3512. }
  3513. /**
  3514. * Reads a PPT marker (Packed packet headers, tile-part header)
  3515. *
  3516. * @param p_header_data the data contained in the PPT box.
  3517. * @param p_j2k the jpeg2000 codec.
  3518. * @param p_header_size the size of the data contained in the PPT marker.
  3519. * @param p_manager the user event manager.
  3520. */
  3521. static OPJ_BOOL opj_j2k_read_ppt(opj_j2k_t *p_j2k,
  3522. OPJ_BYTE * p_header_data,
  3523. OPJ_UINT32 p_header_size,
  3524. opj_event_mgr_t * p_manager
  3525. )
  3526. {
  3527. opj_cp_t *l_cp = 00;
  3528. opj_tcp_t *l_tcp = 00;
  3529. OPJ_UINT32 l_Z_ppt;
  3530. /* preconditions */
  3531. assert(p_header_data != 00);
  3532. assert(p_j2k != 00);
  3533. assert(p_manager != 00);
  3534. /* We need to have the Z_ppt element + 1 byte of Ippt at minimum */
  3535. if (p_header_size < 2) {
  3536. opj_event_msg(p_manager, EVT_ERROR, "Error reading PPT marker\n");
  3537. return OPJ_FALSE;
  3538. }
  3539. l_cp = &(p_j2k->m_cp);
  3540. if (l_cp->ppm) {
  3541. opj_event_msg(p_manager, EVT_ERROR,
  3542. "Error reading PPT marker: packet header have been previously found in the main header (PPM marker).\n");
  3543. return OPJ_FALSE;
  3544. }
  3545. l_tcp = &(l_cp->tcps[p_j2k->m_current_tile_number]);
  3546. l_tcp->ppt = 1;
  3547. opj_read_bytes(p_header_data, &l_Z_ppt, 1); /* Z_ppt */
  3548. ++p_header_data;
  3549. --p_header_size;
  3550. /* check allocation needed */
  3551. if (l_tcp->ppt_markers == NULL) { /* first PPT marker */
  3552. OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
  3553. assert(l_tcp->ppt_markers_count == 0U);
  3554. l_tcp->ppt_markers = (opj_ppx *) opj_calloc(l_newCount, sizeof(opj_ppx));
  3555. if (l_tcp->ppt_markers == NULL) {
  3556. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
  3557. return OPJ_FALSE;
  3558. }
  3559. l_tcp->ppt_markers_count = l_newCount;
  3560. } else if (l_tcp->ppt_markers_count <= l_Z_ppt) {
  3561. OPJ_UINT32 l_newCount = l_Z_ppt + 1U; /* can't overflow, l_Z_ppt is UINT8 */
  3562. opj_ppx *new_ppt_markers;
  3563. new_ppt_markers = (opj_ppx *) opj_realloc(l_tcp->ppt_markers,
  3564. l_newCount * sizeof(opj_ppx));
  3565. if (new_ppt_markers == NULL) {
  3566. /* clean up to be done on l_tcp destruction */
  3567. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
  3568. return OPJ_FALSE;
  3569. }
  3570. l_tcp->ppt_markers = new_ppt_markers;
  3571. memset(l_tcp->ppt_markers + l_tcp->ppt_markers_count, 0,
  3572. (l_newCount - l_tcp->ppt_markers_count) * sizeof(opj_ppx));
  3573. l_tcp->ppt_markers_count = l_newCount;
  3574. }
  3575. if (l_tcp->ppt_markers[l_Z_ppt].m_data != NULL) {
  3576. /* clean up to be done on l_tcp destruction */
  3577. opj_event_msg(p_manager, EVT_ERROR, "Zppt %u already read\n", l_Z_ppt);
  3578. return OPJ_FALSE;
  3579. }
  3580. l_tcp->ppt_markers[l_Z_ppt].m_data = (OPJ_BYTE *) opj_malloc(p_header_size);
  3581. if (l_tcp->ppt_markers[l_Z_ppt].m_data == NULL) {
  3582. /* clean up to be done on l_tcp destruction */
  3583. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
  3584. return OPJ_FALSE;
  3585. }
  3586. l_tcp->ppt_markers[l_Z_ppt].m_data_size = p_header_size;
  3587. memcpy(l_tcp->ppt_markers[l_Z_ppt].m_data, p_header_data, p_header_size);
  3588. return OPJ_TRUE;
  3589. }
  3590. /**
  3591. * Merges all PPT markers read (Packed packet headers, tile-part header)
  3592. *
  3593. * @param p_tcp the tile.
  3594. * @param p_manager the user event manager.
  3595. */
  3596. static OPJ_BOOL opj_j2k_merge_ppt(opj_tcp_t *p_tcp, opj_event_mgr_t * p_manager)
  3597. {
  3598. OPJ_UINT32 i, l_ppt_data_size;
  3599. /* preconditions */
  3600. assert(p_tcp != 00);
  3601. assert(p_manager != 00);
  3602. if (p_tcp->ppt_buffer != NULL) {
  3603. opj_event_msg(p_manager, EVT_ERROR,
  3604. "opj_j2k_merge_ppt() has already been called\n");
  3605. return OPJ_FALSE;
  3606. }
  3607. if (p_tcp->ppt == 0U) {
  3608. return OPJ_TRUE;
  3609. }
  3610. l_ppt_data_size = 0U;
  3611. for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
  3612. l_ppt_data_size +=
  3613. p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
  3614. }
  3615. p_tcp->ppt_buffer = (OPJ_BYTE *) opj_malloc(l_ppt_data_size);
  3616. if (p_tcp->ppt_buffer == 00) {
  3617. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
  3618. return OPJ_FALSE;
  3619. }
  3620. p_tcp->ppt_len = l_ppt_data_size;
  3621. l_ppt_data_size = 0U;
  3622. for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
  3623. if (p_tcp->ppt_markers[i].m_data !=
  3624. NULL) { /* standard doesn't seem to require contiguous Zppt */
  3625. memcpy(p_tcp->ppt_buffer + l_ppt_data_size, p_tcp->ppt_markers[i].m_data,
  3626. p_tcp->ppt_markers[i].m_data_size);
  3627. l_ppt_data_size +=
  3628. p_tcp->ppt_markers[i].m_data_size; /* can't overflow, max 256 markers of max 65536 bytes */
  3629. opj_free(p_tcp->ppt_markers[i].m_data);
  3630. p_tcp->ppt_markers[i].m_data = NULL;
  3631. p_tcp->ppt_markers[i].m_data_size = 0U;
  3632. }
  3633. }
  3634. p_tcp->ppt_markers_count = 0U;
  3635. opj_free(p_tcp->ppt_markers);
  3636. p_tcp->ppt_markers = NULL;
  3637. p_tcp->ppt_data = p_tcp->ppt_buffer;
  3638. p_tcp->ppt_data_size = p_tcp->ppt_len;
  3639. return OPJ_TRUE;
  3640. }
  3641. static OPJ_BOOL opj_j2k_write_tlm(opj_j2k_t *p_j2k,
  3642. opj_stream_private_t *p_stream,
  3643. opj_event_mgr_t * p_manager
  3644. )
  3645. {
  3646. OPJ_BYTE * l_current_data = 00;
  3647. OPJ_UINT32 l_tlm_size;
  3648. OPJ_UINT32 size_per_tile_part;
  3649. /* preconditions */
  3650. assert(p_j2k != 00);
  3651. assert(p_manager != 00);
  3652. assert(p_stream != 00);
  3653. /* 10921 = (65535 - header_size) / size_per_tile_part where */
  3654. /* header_size = 4 and size_per_tile_part = 6 */
  3655. if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts > 10921) {
  3656. /* We could do more but it would require writing several TLM markers */
  3657. opj_event_msg(p_manager, EVT_ERROR,
  3658. "A maximum of 10921 tile-parts are supported currently "
  3659. "when writing TLM marker\n");
  3660. return OPJ_FALSE;
  3661. }
  3662. if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts <= 255) {
  3663. size_per_tile_part = 5;
  3664. p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_TRUE;
  3665. } else {
  3666. size_per_tile_part = 6;
  3667. p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_FALSE;
  3668. }
  3669. l_tlm_size = 2 + 4 + (size_per_tile_part *
  3670. p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
  3671. if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  3672. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  3673. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size);
  3674. if (! new_header_tile_data) {
  3675. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  3676. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  3677. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  3678. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write TLM marker\n");
  3679. return OPJ_FALSE;
  3680. }
  3681. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  3682. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
  3683. }
  3684. memset(p_j2k->m_specific_param.m_encoder.m_header_tile_data, 0, l_tlm_size);
  3685. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  3686. /* change the way data is written to avoid seeking if possible */
  3687. /* TODO */
  3688. p_j2k->m_specific_param.m_encoder.m_tlm_start = opj_stream_tell(p_stream);
  3689. opj_write_bytes(l_current_data, J2K_MS_TLM,
  3690. 2); /* TLM */
  3691. l_current_data += 2;
  3692. opj_write_bytes(l_current_data, l_tlm_size - 2,
  3693. 2); /* Lpoc */
  3694. l_current_data += 2;
  3695. opj_write_bytes(l_current_data, 0,
  3696. 1); /* Ztlm=0*/
  3697. ++l_current_data;
  3698. /* Stlm 0x50= ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
  3699. /* Stlm 0x60= ST=2(16bits-65535 tiles max),SP=1(Ptlm=32bits) */
  3700. opj_write_bytes(l_current_data,
  3701. size_per_tile_part == 5 ? 0x50 : 0x60,
  3702. 1);
  3703. ++l_current_data;
  3704. /* do nothing on the size_per_tile_part * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
  3705. if (opj_stream_write_data(p_stream,
  3706. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size,
  3707. p_manager) != l_tlm_size) {
  3708. return OPJ_FALSE;
  3709. }
  3710. return OPJ_TRUE;
  3711. }
  3712. static OPJ_BOOL opj_j2k_write_sot(opj_j2k_t *p_j2k,
  3713. OPJ_BYTE * p_data,
  3714. OPJ_UINT32 total_data_size,
  3715. OPJ_UINT32 * p_data_written,
  3716. const opj_stream_private_t *p_stream,
  3717. opj_event_mgr_t * p_manager
  3718. )
  3719. {
  3720. /* preconditions */
  3721. assert(p_j2k != 00);
  3722. assert(p_manager != 00);
  3723. assert(p_stream != 00);
  3724. OPJ_UNUSED(p_stream);
  3725. if (total_data_size < 12) {
  3726. opj_event_msg(p_manager, EVT_ERROR,
  3727. "Not enough bytes in output buffer to write SOT marker\n");
  3728. return OPJ_FALSE;
  3729. }
  3730. opj_write_bytes(p_data, J2K_MS_SOT,
  3731. 2); /* SOT */
  3732. p_data += 2;
  3733. opj_write_bytes(p_data, 10,
  3734. 2); /* Lsot */
  3735. p_data += 2;
  3736. opj_write_bytes(p_data, p_j2k->m_current_tile_number,
  3737. 2); /* Isot */
  3738. p_data += 2;
  3739. /* Psot */
  3740. p_data += 4;
  3741. opj_write_bytes(p_data,
  3742. p_j2k->m_specific_param.m_encoder.m_current_tile_part_number,
  3743. 1); /* TPsot */
  3744. ++p_data;
  3745. opj_write_bytes(p_data,
  3746. p_j2k->m_cp.tcps[p_j2k->m_current_tile_number].m_nb_tile_parts,
  3747. 1); /* TNsot */
  3748. ++p_data;
  3749. /* UniPG>> */
  3750. #ifdef USE_JPWL
  3751. /* update markers struct */
  3752. /*
  3753. OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOT, p_j2k->sot_start, len + 2);
  3754. */
  3755. assert(0 && "TODO");
  3756. #endif /* USE_JPWL */
  3757. * p_data_written = 12;
  3758. return OPJ_TRUE;
  3759. }
  3760. static OPJ_BOOL opj_j2k_get_sot_values(OPJ_BYTE * p_header_data,
  3761. OPJ_UINT32 p_header_size,
  3762. OPJ_UINT32* p_tile_no,
  3763. OPJ_UINT32* p_tot_len,
  3764. OPJ_UINT32* p_current_part,
  3765. OPJ_UINT32* p_num_parts,
  3766. opj_event_mgr_t * p_manager)
  3767. {
  3768. /* preconditions */
  3769. assert(p_header_data != 00);
  3770. assert(p_manager != 00);
  3771. /* Size of this marker is fixed = 12 (we have already read marker and its size)*/
  3772. if (p_header_size != 8) {
  3773. opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
  3774. return OPJ_FALSE;
  3775. }
  3776. opj_read_bytes(p_header_data, p_tile_no, 2); /* Isot */
  3777. p_header_data += 2;
  3778. opj_read_bytes(p_header_data, p_tot_len, 4); /* Psot */
  3779. p_header_data += 4;
  3780. opj_read_bytes(p_header_data, p_current_part, 1); /* TPsot */
  3781. ++p_header_data;
  3782. opj_read_bytes(p_header_data, p_num_parts, 1); /* TNsot */
  3783. ++p_header_data;
  3784. return OPJ_TRUE;
  3785. }
  3786. static OPJ_BOOL opj_j2k_read_sot(opj_j2k_t *p_j2k,
  3787. OPJ_BYTE * p_header_data,
  3788. OPJ_UINT32 p_header_size,
  3789. opj_event_mgr_t * p_manager)
  3790. {
  3791. opj_cp_t *l_cp = 00;
  3792. opj_tcp_t *l_tcp = 00;
  3793. OPJ_UINT32 l_tot_len, l_num_parts = 0;
  3794. OPJ_UINT32 l_current_part;
  3795. OPJ_UINT32 l_tile_x, l_tile_y;
  3796. /* preconditions */
  3797. assert(p_j2k != 00);
  3798. assert(p_manager != 00);
  3799. if (! opj_j2k_get_sot_values(p_header_data, p_header_size,
  3800. &(p_j2k->m_current_tile_number), &l_tot_len, &l_current_part, &l_num_parts,
  3801. p_manager)) {
  3802. opj_event_msg(p_manager, EVT_ERROR, "Error reading SOT marker\n");
  3803. return OPJ_FALSE;
  3804. }
  3805. #ifdef DEBUG_VERBOSE
  3806. fprintf(stderr, "SOT %d %d %d %d\n",
  3807. p_j2k->m_current_tile_number, l_tot_len, l_current_part, l_num_parts);
  3808. #endif
  3809. l_cp = &(p_j2k->m_cp);
  3810. /* testcase 2.pdf.SIGFPE.706.1112 */
  3811. if (p_j2k->m_current_tile_number >= l_cp->tw * l_cp->th) {
  3812. opj_event_msg(p_manager, EVT_ERROR, "Invalid tile number %d\n",
  3813. p_j2k->m_current_tile_number);
  3814. return OPJ_FALSE;
  3815. }
  3816. l_tcp = &l_cp->tcps[p_j2k->m_current_tile_number];
  3817. l_tile_x = p_j2k->m_current_tile_number % l_cp->tw;
  3818. l_tile_y = p_j2k->m_current_tile_number / l_cp->tw;
  3819. if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec < 0 ||
  3820. p_j2k->m_current_tile_number == (OPJ_UINT32)
  3821. p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec) {
  3822. /* Do only this check if we decode all tile part headers, or if */
  3823. /* we decode one precise tile. Otherwise the m_current_tile_part_number */
  3824. /* might not be valid */
  3825. /* Fixes issue with id_000020,sig_06,src_001958,op_flip4,pos_149 */
  3826. /* of https://github.com/uclouvain/openjpeg/issues/939 */
  3827. /* We must avoid reading twice the same tile part number for a given tile */
  3828. /* so as to avoid various issues, like opj_j2k_merge_ppt being called */
  3829. /* several times. */
  3830. /* ISO 15444-1 A.4.2 Start of tile-part (SOT) mandates that tile parts */
  3831. /* should appear in increasing order. */
  3832. if (l_tcp->m_current_tile_part_number + 1 != (OPJ_INT32)l_current_part) {
  3833. opj_event_msg(p_manager, EVT_ERROR,
  3834. "Invalid tile part index for tile number %d. "
  3835. "Got %d, expected %d\n",
  3836. p_j2k->m_current_tile_number,
  3837. l_current_part,
  3838. l_tcp->m_current_tile_part_number + 1);
  3839. return OPJ_FALSE;
  3840. }
  3841. }
  3842. l_tcp->m_current_tile_part_number = (OPJ_INT32) l_current_part;
  3843. #ifdef USE_JPWL
  3844. if (l_cp->correct) {
  3845. OPJ_UINT32 tileno = p_j2k->m_current_tile_number;
  3846. static OPJ_UINT32 backup_tileno = 0;
  3847. /* tileno is negative or larger than the number of tiles!!! */
  3848. if (tileno > (l_cp->tw * l_cp->th)) {
  3849. opj_event_msg(p_manager, EVT_ERROR,
  3850. "JPWL: bad tile number (%d out of a maximum of %d)\n",
  3851. tileno, (l_cp->tw * l_cp->th));
  3852. if (!JPWL_ASSUME) {
  3853. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  3854. return OPJ_FALSE;
  3855. }
  3856. /* we try to correct */
  3857. tileno = backup_tileno;
  3858. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
  3859. "- setting tile number to %d\n",
  3860. tileno);
  3861. }
  3862. /* keep your private count of tiles */
  3863. backup_tileno++;
  3864. };
  3865. #endif /* USE_JPWL */
  3866. /* look for the tile in the list of already processed tile (in parts). */
  3867. /* Optimization possible here with a more complex data structure and with the removing of tiles */
  3868. /* since the time taken by this function can only grow at the time */
  3869. /* PSot should be equal to zero or >=14 or <= 2^32-1 */
  3870. if ((l_tot_len != 0) && (l_tot_len < 14)) {
  3871. if (l_tot_len ==
  3872. 12) { /* MSD: Special case for the PHR data which are read by kakadu*/
  3873. opj_event_msg(p_manager, EVT_WARNING, "Empty SOT marker detected: Psot=%d.\n",
  3874. l_tot_len);
  3875. } else {
  3876. opj_event_msg(p_manager, EVT_ERROR,
  3877. "Psot value is not correct regards to the JPEG2000 norm: %d.\n", l_tot_len);
  3878. return OPJ_FALSE;
  3879. }
  3880. }
  3881. #ifdef USE_JPWL
  3882. if (l_cp->correct) {
  3883. /* totlen is negative or larger than the bytes left!!! */
  3884. if (/*(l_tot_len < 0) ||*/ (l_tot_len >
  3885. p_header_size)) { /* FIXME it seems correct; for info in V1 -> (p_stream_numbytesleft(p_stream) + 8))) { */
  3886. opj_event_msg(p_manager, EVT_ERROR,
  3887. "JPWL: bad tile byte size (%d bytes against %d bytes left)\n",
  3888. l_tot_len,
  3889. p_header_size); /* FIXME it seems correct; for info in V1 -> p_stream_numbytesleft(p_stream) + 8); */
  3890. if (!JPWL_ASSUME) {
  3891. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  3892. return OPJ_FALSE;
  3893. }
  3894. /* we try to correct */
  3895. l_tot_len = 0;
  3896. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust this\n"
  3897. "- setting Psot to %d => assuming it is the last tile\n",
  3898. l_tot_len);
  3899. }
  3900. };
  3901. #endif /* USE_JPWL */
  3902. /* Ref A.4.2: Psot could be equal zero if it is the last tile-part of the codestream.*/
  3903. if (!l_tot_len) {
  3904. opj_event_msg(p_manager, EVT_INFO,
  3905. "Psot value of the current tile-part is equal to zero, "
  3906. "we assuming it is the last tile-part of the codestream.\n");
  3907. p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
  3908. }
  3909. if (l_tcp->m_nb_tile_parts != 0 && l_current_part >= l_tcp->m_nb_tile_parts) {
  3910. /* Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=2851 */
  3911. opj_event_msg(p_manager, EVT_ERROR,
  3912. "In SOT marker, TPSot (%d) is not valid regards to the previous "
  3913. "number of tile-part (%d), giving up\n", l_current_part,
  3914. l_tcp->m_nb_tile_parts);
  3915. p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
  3916. return OPJ_FALSE;
  3917. }
  3918. if (l_num_parts !=
  3919. 0) { /* Number of tile-part header is provided by this tile-part header */
  3920. l_num_parts += p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction;
  3921. /* Useful to manage the case of textGBR.jp2 file because two values of TNSot are allowed: the correct numbers of
  3922. * tile-parts for that tile and zero (A.4.2 of 15444-1 : 2002). */
  3923. if (l_tcp->m_nb_tile_parts) {
  3924. if (l_current_part >= l_tcp->m_nb_tile_parts) {
  3925. opj_event_msg(p_manager, EVT_ERROR,
  3926. "In SOT marker, TPSot (%d) is not valid regards to the current "
  3927. "number of tile-part (%d), giving up\n", l_current_part,
  3928. l_tcp->m_nb_tile_parts);
  3929. p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
  3930. return OPJ_FALSE;
  3931. }
  3932. }
  3933. if (l_current_part >= l_num_parts) {
  3934. /* testcase 451.pdf.SIGSEGV.ce9.3723 */
  3935. opj_event_msg(p_manager, EVT_ERROR,
  3936. "In SOT marker, TPSot (%d) is not valid regards to the current "
  3937. "number of tile-part (header) (%d), giving up\n", l_current_part, l_num_parts);
  3938. p_j2k->m_specific_param.m_decoder.m_last_tile_part = 1;
  3939. return OPJ_FALSE;
  3940. }
  3941. l_tcp->m_nb_tile_parts = l_num_parts;
  3942. }
  3943. /* If know the number of tile part header we will check if we didn't read the last*/
  3944. if (l_tcp->m_nb_tile_parts) {
  3945. if (l_tcp->m_nb_tile_parts == (l_current_part + 1)) {
  3946. p_j2k->m_specific_param.m_decoder.m_can_decode =
  3947. 1; /* Process the last tile-part header*/
  3948. }
  3949. }
  3950. if (!p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
  3951. /* Keep the size of data to skip after this marker */
  3952. p_j2k->m_specific_param.m_decoder.m_sot_length = l_tot_len -
  3953. 12; /* SOT_marker_size = 12 */
  3954. } else {
  3955. /* FIXME: need to be computed from the number of bytes remaining in the codestream */
  3956. p_j2k->m_specific_param.m_decoder.m_sot_length = 0;
  3957. }
  3958. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPH;
  3959. /* Check if the current tile is outside the area we want decode or not corresponding to the tile index*/
  3960. if (p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec == -1) {
  3961. p_j2k->m_specific_param.m_decoder.m_skip_data =
  3962. (l_tile_x < p_j2k->m_specific_param.m_decoder.m_start_tile_x)
  3963. || (l_tile_x >= p_j2k->m_specific_param.m_decoder.m_end_tile_x)
  3964. || (l_tile_y < p_j2k->m_specific_param.m_decoder.m_start_tile_y)
  3965. || (l_tile_y >= p_j2k->m_specific_param.m_decoder.m_end_tile_y);
  3966. } else {
  3967. assert(p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec >= 0);
  3968. p_j2k->m_specific_param.m_decoder.m_skip_data =
  3969. (p_j2k->m_current_tile_number != (OPJ_UINT32)
  3970. p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec);
  3971. }
  3972. /* Index */
  3973. if (p_j2k->cstr_index) {
  3974. assert(p_j2k->cstr_index->tile_index != 00);
  3975. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tileno =
  3976. p_j2k->m_current_tile_number;
  3977. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno =
  3978. l_current_part;
  3979. if (l_num_parts != 0) {
  3980. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].nb_tps =
  3981. l_num_parts;
  3982. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps =
  3983. l_num_parts;
  3984. if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
  3985. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
  3986. (opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
  3987. if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
  3988. opj_event_msg(p_manager, EVT_ERROR,
  3989. "Not enough memory to read SOT marker. Tile index allocation failed\n");
  3990. return OPJ_FALSE;
  3991. }
  3992. } else {
  3993. opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
  3994. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
  3995. l_num_parts * sizeof(opj_tp_index_t));
  3996. if (! new_tp_index) {
  3997. opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
  3998. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
  3999. opj_event_msg(p_manager, EVT_ERROR,
  4000. "Not enough memory to read SOT marker. Tile index allocation failed\n");
  4001. return OPJ_FALSE;
  4002. }
  4003. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
  4004. new_tp_index;
  4005. }
  4006. } else {
  4007. /*if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index)*/ {
  4008. if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
  4009. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 10;
  4010. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
  4011. (opj_tp_index_t*)opj_calloc(
  4012. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
  4013. sizeof(opj_tp_index_t));
  4014. if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
  4015. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
  4016. opj_event_msg(p_manager, EVT_ERROR,
  4017. "Not enough memory to read SOT marker. Tile index allocation failed\n");
  4018. return OPJ_FALSE;
  4019. }
  4020. }
  4021. if (l_current_part >=
  4022. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps) {
  4023. opj_tp_index_t *new_tp_index;
  4024. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps =
  4025. l_current_part + 1;
  4026. new_tp_index = (opj_tp_index_t *) opj_realloc(
  4027. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index,
  4028. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps *
  4029. sizeof(opj_tp_index_t));
  4030. if (! new_tp_index) {
  4031. opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
  4032. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
  4033. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
  4034. opj_event_msg(p_manager, EVT_ERROR,
  4035. "Not enough memory to read SOT marker. Tile index allocation failed\n");
  4036. return OPJ_FALSE;
  4037. }
  4038. p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
  4039. new_tp_index;
  4040. }
  4041. }
  4042. }
  4043. }
  4044. /* 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 */
  4045. /* if (p_j2k->cstr_info) {
  4046. if (l_tcp->first) {
  4047. if (tileno == 0) {
  4048. p_j2k->cstr_info->main_head_end = p_stream_tell(p_stream) - 13;
  4049. }
  4050. p_j2k->cstr_info->tile[tileno].tileno = tileno;
  4051. p_j2k->cstr_info->tile[tileno].start_pos = p_stream_tell(p_stream) - 12;
  4052. p_j2k->cstr_info->tile[tileno].end_pos = p_j2k->cstr_info->tile[tileno].start_pos + totlen - 1;
  4053. p_j2k->cstr_info->tile[tileno].num_tps = numparts;
  4054. if (numparts) {
  4055. p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(numparts * sizeof(opj_tp_info_t));
  4056. }
  4057. else {
  4058. p_j2k->cstr_info->tile[tileno].tp = (opj_tp_info_t *) opj_malloc(10 * sizeof(opj_tp_info_t)); // Fixme (10)
  4059. }
  4060. }
  4061. else {
  4062. p_j2k->cstr_info->tile[tileno].end_pos += totlen;
  4063. }
  4064. p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos = p_stream_tell(p_stream) - 12;
  4065. p_j2k->cstr_info->tile[tileno].tp[partno].tp_end_pos =
  4066. p_j2k->cstr_info->tile[tileno].tp[partno].tp_start_pos + totlen - 1;
  4067. }*/
  4068. return OPJ_TRUE;
  4069. }
  4070. /**
  4071. * Write one or more PLT markers in the provided buffer
  4072. */
  4073. static OPJ_BOOL opj_j2k_write_plt_in_memory(opj_j2k_t *p_j2k,
  4074. opj_tcd_marker_info_t* marker_info,
  4075. OPJ_BYTE * p_data,
  4076. OPJ_UINT32 * p_data_written,
  4077. opj_event_mgr_t * p_manager)
  4078. {
  4079. OPJ_BYTE Zplt = 0;
  4080. OPJ_UINT16 Lplt;
  4081. OPJ_BYTE* p_data_start = p_data;
  4082. OPJ_BYTE* p_data_Lplt = p_data + 2;
  4083. OPJ_UINT32 i;
  4084. OPJ_UNUSED(p_j2k);
  4085. opj_write_bytes(p_data, J2K_MS_PLT, 2);
  4086. p_data += 2;
  4087. /* Reserve space for Lplt */
  4088. p_data += 2;
  4089. opj_write_bytes(p_data, Zplt, 1);
  4090. p_data += 1;
  4091. Lplt = 3;
  4092. for (i = 0; i < marker_info->packet_count; i++) {
  4093. OPJ_BYTE var_bytes[5];
  4094. OPJ_UINT8 var_bytes_size = 0;
  4095. OPJ_UINT32 packet_size = marker_info->p_packet_size[i];
  4096. /* Packet size written in variable-length way, starting with LSB */
  4097. var_bytes[var_bytes_size] = (OPJ_BYTE)(packet_size & 0x7f);
  4098. var_bytes_size ++;
  4099. packet_size >>= 7;
  4100. while (packet_size > 0) {
  4101. var_bytes[var_bytes_size] = (OPJ_BYTE)((packet_size & 0x7f) | 0x80);
  4102. var_bytes_size ++;
  4103. packet_size >>= 7;
  4104. }
  4105. /* Check if that can fit in the current PLT marker. If not, finish */
  4106. /* current one, and start a new one */
  4107. if (Lplt + var_bytes_size > 65535) {
  4108. if (Zplt == 255) {
  4109. opj_event_msg(p_manager, EVT_ERROR,
  4110. "More than 255 PLT markers would be needed for current tile-part !\n");
  4111. return OPJ_FALSE;
  4112. }
  4113. /* Patch Lplt */
  4114. opj_write_bytes(p_data_Lplt, Lplt, 2);
  4115. /* Start new segment */
  4116. opj_write_bytes(p_data, J2K_MS_PLT, 2);
  4117. p_data += 2;
  4118. /* Reserve space for Lplt */
  4119. p_data_Lplt = p_data;
  4120. p_data += 2;
  4121. Zplt ++;
  4122. opj_write_bytes(p_data, Zplt, 1);
  4123. p_data += 1;
  4124. Lplt = 3;
  4125. }
  4126. Lplt = (OPJ_UINT16)(Lplt + var_bytes_size);
  4127. /* Serialize variable-length packet size, starting with MSB */
  4128. for (; var_bytes_size > 0; --var_bytes_size) {
  4129. opj_write_bytes(p_data, var_bytes[var_bytes_size - 1], 1);
  4130. p_data += 1;
  4131. }
  4132. }
  4133. *p_data_written = (OPJ_UINT32)(p_data - p_data_start);
  4134. /* Patch Lplt */
  4135. opj_write_bytes(p_data_Lplt, Lplt, 2);
  4136. return OPJ_TRUE;
  4137. }
  4138. static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,
  4139. opj_tcd_t * p_tile_coder,
  4140. OPJ_BYTE * p_data,
  4141. OPJ_UINT32 * p_data_written,
  4142. OPJ_UINT32 total_data_size,
  4143. const opj_stream_private_t *p_stream,
  4144. opj_event_mgr_t * p_manager
  4145. )
  4146. {
  4147. opj_codestream_info_t *l_cstr_info = 00;
  4148. OPJ_UINT32 l_remaining_data;
  4149. opj_tcd_marker_info_t* marker_info = NULL;
  4150. /* preconditions */
  4151. assert(p_j2k != 00);
  4152. assert(p_manager != 00);
  4153. assert(p_stream != 00);
  4154. OPJ_UNUSED(p_stream);
  4155. if (total_data_size < 4) {
  4156. opj_event_msg(p_manager, EVT_ERROR,
  4157. "Not enough bytes in output buffer to write SOD marker\n");
  4158. return OPJ_FALSE;
  4159. }
  4160. opj_write_bytes(p_data, J2K_MS_SOD,
  4161. 2); /* SOD */
  4162. /* make room for the EOF marker */
  4163. l_remaining_data = total_data_size - 4;
  4164. /* update tile coder */
  4165. p_tile_coder->tp_num =
  4166. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number ;
  4167. p_tile_coder->cur_tp_num =
  4168. p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
  4169. /* INDEX >> */
  4170. /* TODO mergeV2: check this part which use cstr_info */
  4171. /*l_cstr_info = p_j2k->cstr_info;
  4172. if (l_cstr_info) {
  4173. if (!p_j2k->m_specific_param.m_encoder.m_current_tile_part_number ) {
  4174. //TODO cstr_info->tile[p_j2k->m_current_tile_number].end_header = p_stream_tell(p_stream) + p_j2k->pos_correction - 1;
  4175. l_cstr_info->tile[p_j2k->m_current_tile_number].tileno = p_j2k->m_current_tile_number;
  4176. }
  4177. else {*/
  4178. /*
  4179. TODO
  4180. if
  4181. (cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno - 1].end_pos < p_stream_tell(p_stream))
  4182. {
  4183. cstr_info->tile[p_j2k->m_current_tile_number].packet[cstr_info->packno].start_pos = p_stream_tell(p_stream);
  4184. }*/
  4185. /*}*/
  4186. /* UniPG>> */
  4187. #ifdef USE_JPWL
  4188. /* update markers struct */
  4189. /*OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_SOD, p_j2k->sod_start, 2);
  4190. */
  4191. assert(0 && "TODO");
  4192. #endif /* USE_JPWL */
  4193. /* <<UniPG */
  4194. /*}*/
  4195. /* << INDEX */
  4196. if (p_j2k->m_specific_param.m_encoder.m_current_tile_part_number == 0) {
  4197. p_tile_coder->tcd_image->tiles->packno = 0;
  4198. #ifdef deadcode
  4199. if (l_cstr_info) {
  4200. l_cstr_info->packno = 0;
  4201. }
  4202. #endif
  4203. }
  4204. *p_data_written = 0;
  4205. if (p_j2k->m_specific_param.m_encoder.m_PLT) {
  4206. marker_info = opj_tcd_marker_info_create(
  4207. p_j2k->m_specific_param.m_encoder.m_PLT);
  4208. if (marker_info == NULL) {
  4209. opj_event_msg(p_manager, EVT_ERROR,
  4210. "Cannot encode tile: opj_tcd_marker_info_create() failed\n");
  4211. return OPJ_FALSE;
  4212. }
  4213. }
  4214. if (l_remaining_data <
  4215. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT) {
  4216. opj_event_msg(p_manager, EVT_ERROR,
  4217. "Not enough bytes in output buffer to write SOD marker\n");
  4218. opj_tcd_marker_info_destroy(marker_info);
  4219. return OPJ_FALSE;
  4220. }
  4221. l_remaining_data -= p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT;
  4222. if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number,
  4223. p_data + 2,
  4224. p_data_written, l_remaining_data, l_cstr_info,
  4225. marker_info,
  4226. p_manager)) {
  4227. opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");
  4228. opj_tcd_marker_info_destroy(marker_info);
  4229. return OPJ_FALSE;
  4230. }
  4231. /* For SOD */
  4232. *p_data_written += 2;
  4233. if (p_j2k->m_specific_param.m_encoder.m_PLT) {
  4234. OPJ_UINT32 l_data_written_PLT = 0;
  4235. OPJ_BYTE* p_PLT_buffer = (OPJ_BYTE*)opj_malloc(
  4236. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT);
  4237. if (!p_PLT_buffer) {
  4238. opj_event_msg(p_manager, EVT_ERROR, "Cannot allocate memory\n");
  4239. opj_tcd_marker_info_destroy(marker_info);
  4240. return OPJ_FALSE;
  4241. }
  4242. if (!opj_j2k_write_plt_in_memory(p_j2k,
  4243. marker_info,
  4244. p_PLT_buffer,
  4245. &l_data_written_PLT,
  4246. p_manager)) {
  4247. opj_tcd_marker_info_destroy(marker_info);
  4248. opj_free(p_PLT_buffer);
  4249. return OPJ_FALSE;
  4250. }
  4251. assert(l_data_written_PLT <=
  4252. p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT);
  4253. /* Move PLT marker(s) before SOD */
  4254. memmove(p_data + l_data_written_PLT, p_data, *p_data_written);
  4255. memcpy(p_data, p_PLT_buffer, l_data_written_PLT);
  4256. opj_free(p_PLT_buffer);
  4257. *p_data_written += l_data_written_PLT;
  4258. }
  4259. opj_tcd_marker_info_destroy(marker_info);
  4260. return OPJ_TRUE;
  4261. }
  4262. static OPJ_BOOL opj_j2k_read_sod(opj_j2k_t *p_j2k,
  4263. opj_stream_private_t *p_stream,
  4264. opj_event_mgr_t * p_manager
  4265. )
  4266. {
  4267. OPJ_SIZE_T l_current_read_size;
  4268. opj_codestream_index_t * l_cstr_index = 00;
  4269. OPJ_BYTE ** l_current_data = 00;
  4270. opj_tcp_t * l_tcp = 00;
  4271. OPJ_UINT32 * l_tile_len = 00;
  4272. OPJ_BOOL l_sot_length_pb_detected = OPJ_FALSE;
  4273. /* preconditions */
  4274. assert(p_j2k != 00);
  4275. assert(p_manager != 00);
  4276. assert(p_stream != 00);
  4277. l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
  4278. if (p_j2k->m_specific_param.m_decoder.m_last_tile_part) {
  4279. /* opj_stream_get_number_byte_left returns OPJ_OFF_T
  4280. // but we are in the last tile part,
  4281. // so its result will fit on OPJ_UINT32 unless we find
  4282. // a file with a single tile part of more than 4 GB...*/
  4283. p_j2k->m_specific_param.m_decoder.m_sot_length = (OPJ_UINT32)(
  4284. opj_stream_get_number_byte_left(p_stream) - 2);
  4285. } else {
  4286. /* Check to avoid pass the limit of OPJ_UINT32 */
  4287. if (p_j2k->m_specific_param.m_decoder.m_sot_length >= 2) {
  4288. p_j2k->m_specific_param.m_decoder.m_sot_length -= 2;
  4289. } else {
  4290. /* MSD: case commented to support empty SOT marker (PHR data) */
  4291. }
  4292. }
  4293. l_current_data = &(l_tcp->m_data);
  4294. l_tile_len = &l_tcp->m_data_size;
  4295. /* Patch to support new PHR data */
  4296. if (p_j2k->m_specific_param.m_decoder.m_sot_length) {
  4297. /* If we are here, we'll try to read the data after allocation */
  4298. /* Check enough bytes left in stream before allocation */
  4299. if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length >
  4300. opj_stream_get_number_byte_left(p_stream)) {
  4301. if (p_j2k->m_cp.strict) {
  4302. opj_event_msg(p_manager, EVT_ERROR,
  4303. "Tile part length size inconsistent with stream length\n");
  4304. return OPJ_FALSE;
  4305. } else {
  4306. opj_event_msg(p_manager, EVT_WARNING,
  4307. "Tile part length size inconsistent with stream length\n");
  4308. }
  4309. }
  4310. if (p_j2k->m_specific_param.m_decoder.m_sot_length >
  4311. UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) {
  4312. opj_event_msg(p_manager, EVT_ERROR,
  4313. "p_j2k->m_specific_param.m_decoder.m_sot_length > "
  4314. "UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA");
  4315. return OPJ_FALSE;
  4316. }
  4317. /* Add a margin of OPJ_COMMON_CBLK_DATA_EXTRA to the allocation we */
  4318. /* do so that opj_mqc_init_dec_common() can safely add a synthetic */
  4319. /* 0xFFFF marker. */
  4320. if (! *l_current_data) {
  4321. /* LH: oddly enough, in this path, l_tile_len!=0.
  4322. * TODO: If this was consistent, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).
  4323. */
  4324. *l_current_data = (OPJ_BYTE*) opj_malloc(
  4325. p_j2k->m_specific_param.m_decoder.m_sot_length + OPJ_COMMON_CBLK_DATA_EXTRA);
  4326. } else {
  4327. OPJ_BYTE *l_new_current_data;
  4328. if (*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA -
  4329. p_j2k->m_specific_param.m_decoder.m_sot_length) {
  4330. opj_event_msg(p_manager, EVT_ERROR,
  4331. "*l_tile_len > UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA - "
  4332. "p_j2k->m_specific_param.m_decoder.m_sot_length");
  4333. return OPJ_FALSE;
  4334. }
  4335. l_new_current_data = (OPJ_BYTE *) opj_realloc(*l_current_data,
  4336. *l_tile_len + p_j2k->m_specific_param.m_decoder.m_sot_length +
  4337. OPJ_COMMON_CBLK_DATA_EXTRA);
  4338. if (! l_new_current_data) {
  4339. opj_free(*l_current_data);
  4340. /*nothing more is done as l_current_data will be set to null, and just
  4341. afterward we enter in the error path
  4342. and the actual tile_len is updated (committed) at the end of the
  4343. function. */
  4344. }
  4345. *l_current_data = l_new_current_data;
  4346. }
  4347. if (*l_current_data == 00) {
  4348. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to decode tile\n");
  4349. return OPJ_FALSE;
  4350. }
  4351. } else {
  4352. l_sot_length_pb_detected = OPJ_TRUE;
  4353. }
  4354. /* Index */
  4355. l_cstr_index = p_j2k->cstr_index;
  4356. if (l_cstr_index) {
  4357. OPJ_OFF_T l_current_pos = opj_stream_tell(p_stream) - 2;
  4358. OPJ_UINT32 l_current_tile_part =
  4359. l_cstr_index->tile_index[p_j2k->m_current_tile_number].current_tpsno;
  4360. l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_header
  4361. =
  4362. l_current_pos;
  4363. l_cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index[l_current_tile_part].end_pos
  4364. =
  4365. l_current_pos + p_j2k->m_specific_param.m_decoder.m_sot_length + 2;
  4366. if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
  4367. l_cstr_index,
  4368. J2K_MS_SOD,
  4369. l_current_pos,
  4370. p_j2k->m_specific_param.m_decoder.m_sot_length + 2)) {
  4371. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
  4372. return OPJ_FALSE;
  4373. }
  4374. /*l_cstr_index->packno = 0;*/
  4375. }
  4376. /* Patch to support new PHR data */
  4377. if (!l_sot_length_pb_detected) {
  4378. l_current_read_size = opj_stream_read_data(
  4379. p_stream,
  4380. *l_current_data + *l_tile_len,
  4381. p_j2k->m_specific_param.m_decoder.m_sot_length,
  4382. p_manager);
  4383. } else {
  4384. l_current_read_size = 0;
  4385. }
  4386. if (l_current_read_size != p_j2k->m_specific_param.m_decoder.m_sot_length) {
  4387. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
  4388. } else {
  4389. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
  4390. }
  4391. *l_tile_len += (OPJ_UINT32)l_current_read_size;
  4392. return OPJ_TRUE;
  4393. }
  4394. static OPJ_BOOL opj_j2k_write_rgn(opj_j2k_t *p_j2k,
  4395. OPJ_UINT32 p_tile_no,
  4396. OPJ_UINT32 p_comp_no,
  4397. OPJ_UINT32 nb_comps,
  4398. opj_stream_private_t *p_stream,
  4399. opj_event_mgr_t * p_manager
  4400. )
  4401. {
  4402. OPJ_BYTE * l_current_data = 00;
  4403. OPJ_UINT32 l_rgn_size;
  4404. opj_cp_t *l_cp = 00;
  4405. opj_tcp_t *l_tcp = 00;
  4406. opj_tccp_t *l_tccp = 00;
  4407. OPJ_UINT32 l_comp_room;
  4408. /* preconditions */
  4409. assert(p_j2k != 00);
  4410. assert(p_manager != 00);
  4411. assert(p_stream != 00);
  4412. l_cp = &(p_j2k->m_cp);
  4413. l_tcp = &l_cp->tcps[p_tile_no];
  4414. l_tccp = &l_tcp->tccps[p_comp_no];
  4415. if (nb_comps <= 256) {
  4416. l_comp_room = 1;
  4417. } else {
  4418. l_comp_room = 2;
  4419. }
  4420. l_rgn_size = 6 + l_comp_room;
  4421. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  4422. opj_write_bytes(l_current_data, J2K_MS_RGN,
  4423. 2); /* RGN */
  4424. l_current_data += 2;
  4425. opj_write_bytes(l_current_data, l_rgn_size - 2,
  4426. 2); /* Lrgn */
  4427. l_current_data += 2;
  4428. opj_write_bytes(l_current_data, p_comp_no,
  4429. l_comp_room); /* Crgn */
  4430. l_current_data += l_comp_room;
  4431. opj_write_bytes(l_current_data, 0,
  4432. 1); /* Srgn */
  4433. ++l_current_data;
  4434. opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift,
  4435. 1); /* SPrgn */
  4436. ++l_current_data;
  4437. if (opj_stream_write_data(p_stream,
  4438. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_rgn_size,
  4439. p_manager) != l_rgn_size) {
  4440. return OPJ_FALSE;
  4441. }
  4442. return OPJ_TRUE;
  4443. }
  4444. static OPJ_BOOL opj_j2k_write_eoc(opj_j2k_t *p_j2k,
  4445. opj_stream_private_t *p_stream,
  4446. opj_event_mgr_t * p_manager
  4447. )
  4448. {
  4449. /* preconditions */
  4450. assert(p_j2k != 00);
  4451. assert(p_manager != 00);
  4452. assert(p_stream != 00);
  4453. opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_header_tile_data,
  4454. J2K_MS_EOC, 2); /* EOC */
  4455. /* UniPG>> */
  4456. #ifdef USE_JPWL
  4457. /* update markers struct */
  4458. /*
  4459. OPJ_BOOL res = j2k_add_marker(p_j2k->cstr_info, J2K_MS_EOC, p_stream_tell(p_stream) - 2, 2);
  4460. */
  4461. #endif /* USE_JPWL */
  4462. if (opj_stream_write_data(p_stream,
  4463. p_j2k->m_specific_param.m_encoder.m_header_tile_data, 2, p_manager) != 2) {
  4464. return OPJ_FALSE;
  4465. }
  4466. if (! opj_stream_flush(p_stream, p_manager)) {
  4467. return OPJ_FALSE;
  4468. }
  4469. return OPJ_TRUE;
  4470. }
  4471. /**
  4472. * Reads a RGN marker (Region Of Interest)
  4473. *
  4474. * @param p_header_data the data contained in the POC box.
  4475. * @param p_j2k the jpeg2000 codec.
  4476. * @param p_header_size the size of the data contained in the POC marker.
  4477. * @param p_manager the user event manager.
  4478. */
  4479. static OPJ_BOOL opj_j2k_read_rgn(opj_j2k_t *p_j2k,
  4480. OPJ_BYTE * p_header_data,
  4481. OPJ_UINT32 p_header_size,
  4482. opj_event_mgr_t * p_manager
  4483. )
  4484. {
  4485. OPJ_UINT32 l_nb_comp;
  4486. opj_image_t * l_image = 00;
  4487. opj_cp_t *l_cp = 00;
  4488. opj_tcp_t *l_tcp = 00;
  4489. OPJ_UINT32 l_comp_room, l_comp_no, l_roi_sty;
  4490. /* preconditions*/
  4491. assert(p_header_data != 00);
  4492. assert(p_j2k != 00);
  4493. assert(p_manager != 00);
  4494. l_image = p_j2k->m_private_image;
  4495. l_nb_comp = l_image->numcomps;
  4496. if (l_nb_comp <= 256) {
  4497. l_comp_room = 1;
  4498. } else {
  4499. l_comp_room = 2;
  4500. }
  4501. if (p_header_size != 2 + l_comp_room) {
  4502. opj_event_msg(p_manager, EVT_ERROR, "Error reading RGN marker\n");
  4503. return OPJ_FALSE;
  4504. }
  4505. l_cp = &(p_j2k->m_cp);
  4506. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
  4507. &l_cp->tcps[p_j2k->m_current_tile_number] :
  4508. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  4509. opj_read_bytes(p_header_data, &l_comp_no, l_comp_room); /* Crgn */
  4510. p_header_data += l_comp_room;
  4511. opj_read_bytes(p_header_data, &l_roi_sty,
  4512. 1); /* Srgn */
  4513. ++p_header_data;
  4514. #ifdef USE_JPWL
  4515. if (l_cp->correct) {
  4516. /* totlen is negative or larger than the bytes left!!! */
  4517. if (l_comp_room >= l_nb_comp) {
  4518. opj_event_msg(p_manager, EVT_ERROR,
  4519. "JPWL: bad component number in RGN (%d when there are only %d)\n",
  4520. l_comp_room, l_nb_comp);
  4521. if (!JPWL_ASSUME) {
  4522. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  4523. return OPJ_FALSE;
  4524. }
  4525. }
  4526. };
  4527. #endif /* USE_JPWL */
  4528. /* testcase 3635.pdf.asan.77.2930 */
  4529. if (l_comp_no >= l_nb_comp) {
  4530. opj_event_msg(p_manager, EVT_ERROR,
  4531. "bad component number in RGN (%d when there are only %d)\n",
  4532. l_comp_no, l_nb_comp);
  4533. return OPJ_FALSE;
  4534. }
  4535. opj_read_bytes(p_header_data,
  4536. (OPJ_UINT32 *)(&(l_tcp->tccps[l_comp_no].roishift)), 1); /* SPrgn */
  4537. ++p_header_data;
  4538. return OPJ_TRUE;
  4539. }
  4540. static OPJ_FLOAT32 opj_j2k_get_tp_stride(opj_tcp_t * p_tcp)
  4541. {
  4542. return (OPJ_FLOAT32)((p_tcp->m_nb_tile_parts - 1) * 14);
  4543. }
  4544. static OPJ_FLOAT32 opj_j2k_get_default_stride(opj_tcp_t * p_tcp)
  4545. {
  4546. (void)p_tcp;
  4547. return 0;
  4548. }
  4549. static OPJ_BOOL opj_j2k_update_rates(opj_j2k_t *p_j2k,
  4550. opj_stream_private_t *p_stream,
  4551. opj_event_mgr_t * p_manager)
  4552. {
  4553. opj_cp_t * l_cp = 00;
  4554. opj_image_t * l_image = 00;
  4555. opj_tcp_t * l_tcp = 00;
  4556. opj_image_comp_t * l_img_comp = 00;
  4557. OPJ_UINT32 i, j, k;
  4558. OPJ_INT32 l_x0, l_y0, l_x1, l_y1;
  4559. OPJ_FLOAT32 * l_rates = 0;
  4560. OPJ_FLOAT32 l_sot_remove;
  4561. OPJ_UINT32 l_bits_empty, l_size_pixel;
  4562. OPJ_UINT64 l_tile_size = 0;
  4563. OPJ_UINT32 l_last_res;
  4564. OPJ_FLOAT32(* l_tp_stride_func)(opj_tcp_t *) = 00;
  4565. /* preconditions */
  4566. assert(p_j2k != 00);
  4567. assert(p_manager != 00);
  4568. assert(p_stream != 00);
  4569. OPJ_UNUSED(p_manager);
  4570. l_cp = &(p_j2k->m_cp);
  4571. l_image = p_j2k->m_private_image;
  4572. l_tcp = l_cp->tcps;
  4573. l_bits_empty = 8 * l_image->comps->dx * l_image->comps->dy;
  4574. l_size_pixel = l_image->numcomps * l_image->comps->prec;
  4575. l_sot_remove = (OPJ_FLOAT32) opj_stream_tell(p_stream) / (OPJ_FLOAT32)(
  4576. l_cp->th * l_cp->tw);
  4577. if (l_cp->m_specific_param.m_enc.m_tp_on) {
  4578. l_tp_stride_func = opj_j2k_get_tp_stride;
  4579. } else {
  4580. l_tp_stride_func = opj_j2k_get_default_stride;
  4581. }
  4582. for (i = 0; i < l_cp->th; ++i) {
  4583. for (j = 0; j < l_cp->tw; ++j) {
  4584. OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) /
  4585. (OPJ_FLOAT32)l_tcp->numlayers;
  4586. /* 4 borders of the tile rescale on the image if necessary */
  4587. l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx),
  4588. (OPJ_INT32)l_image->x0);
  4589. l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy),
  4590. (OPJ_INT32)l_image->y0);
  4591. l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx),
  4592. (OPJ_INT32)l_image->x1);
  4593. l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy),
  4594. (OPJ_INT32)l_image->y1);
  4595. l_rates = l_tcp->rates;
  4596. /* Modification of the RATE >> */
  4597. for (k = 0; k < l_tcp->numlayers; ++k) {
  4598. if (*l_rates > 0.0f) {
  4599. *l_rates = (OPJ_FLOAT32)(((OPJ_FLOAT64)l_size_pixel * (OPJ_UINT32)(
  4600. l_x1 - l_x0) *
  4601. (OPJ_UINT32)(l_y1 - l_y0))
  4602. / ((*l_rates) * (OPJ_FLOAT32)l_bits_empty))
  4603. -
  4604. l_offset;
  4605. }
  4606. ++l_rates;
  4607. }
  4608. ++l_tcp;
  4609. }
  4610. }
  4611. l_tcp = l_cp->tcps;
  4612. for (i = 0; i < l_cp->th; ++i) {
  4613. for (j = 0; j < l_cp->tw; ++j) {
  4614. l_rates = l_tcp->rates;
  4615. if (*l_rates > 0.0f) {
  4616. *l_rates -= l_sot_remove;
  4617. if (*l_rates < 30.0f) {
  4618. *l_rates = 30.0f;
  4619. }
  4620. }
  4621. ++l_rates;
  4622. l_last_res = l_tcp->numlayers - 1;
  4623. for (k = 1; k < l_last_res; ++k) {
  4624. if (*l_rates > 0.0f) {
  4625. *l_rates -= l_sot_remove;
  4626. if (*l_rates < * (l_rates - 1) + 10.0f) {
  4627. *l_rates = (*(l_rates - 1)) + 20.0f;
  4628. }
  4629. }
  4630. ++l_rates;
  4631. }
  4632. if (*l_rates > 0.0f) {
  4633. *l_rates -= (l_sot_remove + 2.f);
  4634. if (*l_rates < * (l_rates - 1) + 10.0f) {
  4635. *l_rates = (*(l_rates - 1)) + 20.0f;
  4636. }
  4637. }
  4638. ++l_tcp;
  4639. }
  4640. }
  4641. l_img_comp = l_image->comps;
  4642. l_tile_size = 0;
  4643. for (i = 0; i < l_image->numcomps; ++i) {
  4644. l_tile_size += (OPJ_UINT64)opj_uint_ceildiv(l_cp->tdx, l_img_comp->dx)
  4645. *
  4646. opj_uint_ceildiv(l_cp->tdy, l_img_comp->dy)
  4647. *
  4648. l_img_comp->prec;
  4649. ++l_img_comp;
  4650. }
  4651. /* TODO: where does this magic value come from ? */
  4652. /* This used to be 1.3 / 8, but with random data and very small code */
  4653. /* block sizes, this is not enough. For example with */
  4654. /* bin/test_tile_encoder 1 256 256 32 32 8 0 reversible_with_precinct.j2k 4 4 3 0 0 1 16 16 */
  4655. /* TODO revise this to take into account the overhead linked to the */
  4656. /* number of packets and number of code blocks in packets */
  4657. l_tile_size = (OPJ_UINT64)((double)l_tile_size * 1.4 / 8);
  4658. /* Arbitrary amount to make the following work: */
  4659. /* bin/test_tile_encoder 1 256 256 17 16 8 0 reversible_no_precinct.j2k 4 4 3 0 0 1 */
  4660. l_tile_size += 500;
  4661. l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
  4662. if (l_tile_size > UINT_MAX) {
  4663. l_tile_size = UINT_MAX;
  4664. }
  4665. p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = (OPJ_UINT32)l_tile_size;
  4666. p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
  4667. (OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
  4668. if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
  4669. opj_event_msg(p_manager, EVT_ERROR,
  4670. "Not enough memory to allocate m_encoded_tile_data. %u MB required\n",
  4671. (OPJ_UINT32)(l_tile_size / 1024 / 1024));
  4672. return OPJ_FALSE;
  4673. }
  4674. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  4675. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
  4676. (OPJ_BYTE *) opj_malloc(6 *
  4677. p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
  4678. if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
  4679. return OPJ_FALSE;
  4680. }
  4681. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current =
  4682. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer;
  4683. }
  4684. return OPJ_TRUE;
  4685. }
  4686. #if 0
  4687. static OPJ_BOOL opj_j2k_read_eoc(opj_j2k_t *p_j2k,
  4688. opj_stream_private_t *p_stream,
  4689. opj_event_mgr_t * p_manager)
  4690. {
  4691. OPJ_UINT32 i;
  4692. opj_tcd_t * l_tcd = 00;
  4693. OPJ_UINT32 l_nb_tiles;
  4694. opj_tcp_t * l_tcp = 00;
  4695. OPJ_BOOL l_success;
  4696. /* preconditions */
  4697. assert(p_j2k != 00);
  4698. assert(p_manager != 00);
  4699. assert(p_stream != 00);
  4700. l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  4701. l_tcp = p_j2k->m_cp.tcps;
  4702. l_tcd = opj_tcd_create(OPJ_TRUE);
  4703. if (l_tcd == 00) {
  4704. opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
  4705. return OPJ_FALSE;
  4706. }
  4707. for (i = 0; i < l_nb_tiles; ++i) {
  4708. if (l_tcp->m_data) {
  4709. if (! opj_tcd_init_decode_tile(l_tcd, i)) {
  4710. opj_tcd_destroy(l_tcd);
  4711. opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
  4712. return OPJ_FALSE;
  4713. }
  4714. l_success = opj_tcd_decode_tile(l_tcd, l_tcp->m_data, l_tcp->m_data_size, i,
  4715. p_j2k->cstr_index);
  4716. /* cleanup */
  4717. if (! l_success) {
  4718. p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
  4719. break;
  4720. }
  4721. }
  4722. opj_j2k_tcp_destroy(l_tcp);
  4723. ++l_tcp;
  4724. }
  4725. opj_tcd_destroy(l_tcd);
  4726. return OPJ_TRUE;
  4727. }
  4728. #endif
  4729. static OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
  4730. struct opj_stream_private *p_stream,
  4731. struct opj_event_mgr * p_manager)
  4732. {
  4733. /* preconditions */
  4734. assert(p_j2k != 00);
  4735. assert(p_manager != 00);
  4736. assert(p_stream != 00);
  4737. OPJ_UNUSED(p_manager);
  4738. p_j2k->cstr_index->main_head_end = opj_stream_tell(p_stream);
  4739. return OPJ_TRUE;
  4740. }
  4741. static OPJ_BOOL opj_j2k_write_mct_data_group(opj_j2k_t *p_j2k,
  4742. struct opj_stream_private *p_stream,
  4743. struct opj_event_mgr * p_manager)
  4744. {
  4745. OPJ_UINT32 i;
  4746. opj_simple_mcc_decorrelation_data_t * l_mcc_record;
  4747. opj_mct_data_t * l_mct_record;
  4748. opj_tcp_t * l_tcp;
  4749. /* preconditions */
  4750. assert(p_j2k != 00);
  4751. assert(p_stream != 00);
  4752. assert(p_manager != 00);
  4753. if (! opj_j2k_write_cbd(p_j2k, p_stream, p_manager)) {
  4754. return OPJ_FALSE;
  4755. }
  4756. l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
  4757. l_mct_record = l_tcp->m_mct_records;
  4758. for (i = 0; i < l_tcp->m_nb_mct_records; ++i) {
  4759. if (! opj_j2k_write_mct_record(p_j2k, l_mct_record, p_stream, p_manager)) {
  4760. return OPJ_FALSE;
  4761. }
  4762. ++l_mct_record;
  4763. }
  4764. l_mcc_record = l_tcp->m_mcc_records;
  4765. for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
  4766. if (! opj_j2k_write_mcc_record(p_j2k, l_mcc_record, p_stream, p_manager)) {
  4767. return OPJ_FALSE;
  4768. }
  4769. ++l_mcc_record;
  4770. }
  4771. if (! opj_j2k_write_mco(p_j2k, p_stream, p_manager)) {
  4772. return OPJ_FALSE;
  4773. }
  4774. return OPJ_TRUE;
  4775. }
  4776. static OPJ_BOOL opj_j2k_write_all_coc(
  4777. opj_j2k_t *p_j2k,
  4778. struct opj_stream_private *p_stream,
  4779. struct opj_event_mgr * p_manager)
  4780. {
  4781. OPJ_UINT32 compno;
  4782. /* preconditions */
  4783. assert(p_j2k != 00);
  4784. assert(p_manager != 00);
  4785. assert(p_stream != 00);
  4786. for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) {
  4787. /* cod is first component of first tile */
  4788. if (! opj_j2k_compare_coc(p_j2k, 0, compno)) {
  4789. if (! opj_j2k_write_coc(p_j2k, compno, p_stream, p_manager)) {
  4790. return OPJ_FALSE;
  4791. }
  4792. }
  4793. }
  4794. return OPJ_TRUE;
  4795. }
  4796. static OPJ_BOOL opj_j2k_write_all_qcc(
  4797. opj_j2k_t *p_j2k,
  4798. struct opj_stream_private *p_stream,
  4799. struct opj_event_mgr * p_manager)
  4800. {
  4801. OPJ_UINT32 compno;
  4802. /* preconditions */
  4803. assert(p_j2k != 00);
  4804. assert(p_manager != 00);
  4805. assert(p_stream != 00);
  4806. for (compno = 1; compno < p_j2k->m_private_image->numcomps; ++compno) {
  4807. /* qcd is first component of first tile */
  4808. if (! opj_j2k_compare_qcc(p_j2k, 0, compno)) {
  4809. if (! opj_j2k_write_qcc(p_j2k, compno, p_stream, p_manager)) {
  4810. return OPJ_FALSE;
  4811. }
  4812. }
  4813. }
  4814. return OPJ_TRUE;
  4815. }
  4816. static OPJ_BOOL opj_j2k_write_regions(opj_j2k_t *p_j2k,
  4817. struct opj_stream_private *p_stream,
  4818. struct opj_event_mgr * p_manager)
  4819. {
  4820. OPJ_UINT32 compno;
  4821. const opj_tccp_t *l_tccp = 00;
  4822. /* preconditions */
  4823. assert(p_j2k != 00);
  4824. assert(p_manager != 00);
  4825. assert(p_stream != 00);
  4826. l_tccp = p_j2k->m_cp.tcps->tccps;
  4827. for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
  4828. if (l_tccp->roishift) {
  4829. if (! opj_j2k_write_rgn(p_j2k, 0, compno, p_j2k->m_private_image->numcomps,
  4830. p_stream, p_manager)) {
  4831. return OPJ_FALSE;
  4832. }
  4833. }
  4834. ++l_tccp;
  4835. }
  4836. return OPJ_TRUE;
  4837. }
  4838. static OPJ_BOOL opj_j2k_write_epc(opj_j2k_t *p_j2k,
  4839. struct opj_stream_private *p_stream,
  4840. struct opj_event_mgr * p_manager)
  4841. {
  4842. opj_codestream_index_t * l_cstr_index = 00;
  4843. /* preconditions */
  4844. assert(p_j2k != 00);
  4845. assert(p_manager != 00);
  4846. assert(p_stream != 00);
  4847. OPJ_UNUSED(p_manager);
  4848. l_cstr_index = p_j2k->cstr_index;
  4849. if (l_cstr_index) {
  4850. l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream);
  4851. /* UniPG>> */
  4852. /* The following adjustment is done to adjust the codestream size */
  4853. /* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
  4854. /* the first bunch of bytes is not in the codestream */
  4855. l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start;
  4856. /* <<UniPG */
  4857. }
  4858. #ifdef USE_JPWL
  4859. /* preparation of JPWL marker segments */
  4860. #if 0
  4861. if (cp->epc_on) {
  4862. /* encode according to JPWL */
  4863. jpwl_encode(p_j2k, p_stream, image);
  4864. }
  4865. #endif
  4866. assert(0 && "TODO");
  4867. #endif /* USE_JPWL */
  4868. return OPJ_TRUE;
  4869. }
  4870. static OPJ_BOOL opj_j2k_read_unk(opj_j2k_t *p_j2k,
  4871. opj_stream_private_t *p_stream,
  4872. OPJ_UINT32 *output_marker,
  4873. opj_event_mgr_t * p_manager
  4874. )
  4875. {
  4876. OPJ_UINT32 l_unknown_marker;
  4877. const opj_dec_memory_marker_handler_t * l_marker_handler;
  4878. OPJ_UINT32 l_size_unk = 2;
  4879. /* preconditions*/
  4880. assert(p_j2k != 00);
  4881. assert(p_manager != 00);
  4882. assert(p_stream != 00);
  4883. opj_event_msg(p_manager, EVT_WARNING, "Unknown marker\n");
  4884. for (;;) {
  4885. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
  4886. if (opj_stream_read_data(p_stream,
  4887. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  4888. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  4889. return OPJ_FALSE;
  4890. }
  4891. /* read 2 bytes as the new marker ID*/
  4892. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  4893. &l_unknown_marker, 2);
  4894. if (!(l_unknown_marker < 0xff00)) {
  4895. /* Get the marker handler from the marker ID*/
  4896. l_marker_handler = opj_j2k_get_marker_handler(l_unknown_marker);
  4897. if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
  4898. opj_event_msg(p_manager, EVT_ERROR,
  4899. "Marker is not compliant with its position\n");
  4900. return OPJ_FALSE;
  4901. } else {
  4902. if (l_marker_handler->id != J2K_MS_UNK) {
  4903. /* Add the marker to the codestream index*/
  4904. if (l_marker_handler->id != J2K_MS_SOT) {
  4905. OPJ_BOOL res = opj_j2k_add_mhmarker(p_j2k->cstr_index, J2K_MS_UNK,
  4906. (OPJ_UINT32) opj_stream_tell(p_stream) - l_size_unk,
  4907. l_size_unk);
  4908. if (res == OPJ_FALSE) {
  4909. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
  4910. return OPJ_FALSE;
  4911. }
  4912. }
  4913. break; /* next marker is known and well located */
  4914. } else {
  4915. l_size_unk += 2;
  4916. }
  4917. }
  4918. }
  4919. }
  4920. *output_marker = l_marker_handler->id ;
  4921. return OPJ_TRUE;
  4922. }
  4923. static OPJ_BOOL opj_j2k_write_mct_record(opj_j2k_t *p_j2k,
  4924. opj_mct_data_t * p_mct_record,
  4925. struct opj_stream_private *p_stream,
  4926. struct opj_event_mgr * p_manager)
  4927. {
  4928. OPJ_UINT32 l_mct_size;
  4929. OPJ_BYTE * l_current_data = 00;
  4930. OPJ_UINT32 l_tmp;
  4931. /* preconditions */
  4932. assert(p_j2k != 00);
  4933. assert(p_manager != 00);
  4934. assert(p_stream != 00);
  4935. l_mct_size = 10 + p_mct_record->m_data_size;
  4936. if (l_mct_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  4937. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  4938. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size);
  4939. if (! new_header_tile_data) {
  4940. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  4941. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  4942. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  4943. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCT marker\n");
  4944. return OPJ_FALSE;
  4945. }
  4946. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  4947. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mct_size;
  4948. }
  4949. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  4950. opj_write_bytes(l_current_data, J2K_MS_MCT,
  4951. 2); /* MCT */
  4952. l_current_data += 2;
  4953. opj_write_bytes(l_current_data, l_mct_size - 2,
  4954. 2); /* Lmct */
  4955. l_current_data += 2;
  4956. opj_write_bytes(l_current_data, 0,
  4957. 2); /* Zmct */
  4958. l_current_data += 2;
  4959. /* only one marker atm */
  4960. l_tmp = (p_mct_record->m_index & 0xff) | (p_mct_record->m_array_type << 8) |
  4961. (p_mct_record->m_element_type << 10);
  4962. opj_write_bytes(l_current_data, l_tmp, 2);
  4963. l_current_data += 2;
  4964. opj_write_bytes(l_current_data, 0,
  4965. 2); /* Ymct */
  4966. l_current_data += 2;
  4967. memcpy(l_current_data, p_mct_record->m_data, p_mct_record->m_data_size);
  4968. if (opj_stream_write_data(p_stream,
  4969. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mct_size,
  4970. p_manager) != l_mct_size) {
  4971. return OPJ_FALSE;
  4972. }
  4973. return OPJ_TRUE;
  4974. }
  4975. /**
  4976. * Reads a MCT marker (Multiple Component Transform)
  4977. *
  4978. * @param p_header_data the data contained in the MCT box.
  4979. * @param p_j2k the jpeg2000 codec.
  4980. * @param p_header_size the size of the data contained in the MCT marker.
  4981. * @param p_manager the user event manager.
  4982. */
  4983. static OPJ_BOOL opj_j2k_read_mct(opj_j2k_t *p_j2k,
  4984. OPJ_BYTE * p_header_data,
  4985. OPJ_UINT32 p_header_size,
  4986. opj_event_mgr_t * p_manager
  4987. )
  4988. {
  4989. OPJ_UINT32 i;
  4990. opj_tcp_t *l_tcp = 00;
  4991. OPJ_UINT32 l_tmp;
  4992. OPJ_UINT32 l_indix;
  4993. opj_mct_data_t * l_mct_data;
  4994. /* preconditions */
  4995. assert(p_header_data != 00);
  4996. assert(p_j2k != 00);
  4997. l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
  4998. &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
  4999. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  5000. if (p_header_size < 2) {
  5001. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
  5002. return OPJ_FALSE;
  5003. }
  5004. /* first marker */
  5005. opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmct */
  5006. p_header_data += 2;
  5007. if (l_tmp != 0) {
  5008. opj_event_msg(p_manager, EVT_WARNING,
  5009. "Cannot take in charge mct data within multiple MCT records\n");
  5010. return OPJ_TRUE;
  5011. }
  5012. if (p_header_size <= 6) {
  5013. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
  5014. return OPJ_FALSE;
  5015. }
  5016. /* Imct -> no need for other values, take the first, type is double with decorrelation x0000 1101 0000 0000*/
  5017. opj_read_bytes(p_header_data, &l_tmp, 2); /* Imct */
  5018. p_header_data += 2;
  5019. l_indix = l_tmp & 0xff;
  5020. l_mct_data = l_tcp->m_mct_records;
  5021. for (i = 0; i < l_tcp->m_nb_mct_records; ++i) {
  5022. if (l_mct_data->m_index == l_indix) {
  5023. break;
  5024. }
  5025. ++l_mct_data;
  5026. }
  5027. /* NOT FOUND */
  5028. if (i == l_tcp->m_nb_mct_records) {
  5029. if (l_tcp->m_nb_mct_records == l_tcp->m_nb_max_mct_records) {
  5030. opj_mct_data_t *new_mct_records;
  5031. l_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  5032. new_mct_records = (opj_mct_data_t *) opj_realloc(l_tcp->m_mct_records,
  5033. l_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
  5034. if (! new_mct_records) {
  5035. opj_free(l_tcp->m_mct_records);
  5036. l_tcp->m_mct_records = NULL;
  5037. l_tcp->m_nb_max_mct_records = 0;
  5038. l_tcp->m_nb_mct_records = 0;
  5039. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCT marker\n");
  5040. return OPJ_FALSE;
  5041. }
  5042. /* Update m_mcc_records[].m_offset_array and m_decorrelation_array
  5043. * to point to the new addresses */
  5044. if (new_mct_records != l_tcp->m_mct_records) {
  5045. for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
  5046. opj_simple_mcc_decorrelation_data_t* l_mcc_record =
  5047. &(l_tcp->m_mcc_records[i]);
  5048. if (l_mcc_record->m_decorrelation_array) {
  5049. l_mcc_record->m_decorrelation_array =
  5050. new_mct_records +
  5051. (l_mcc_record->m_decorrelation_array -
  5052. l_tcp->m_mct_records);
  5053. }
  5054. if (l_mcc_record->m_offset_array) {
  5055. l_mcc_record->m_offset_array =
  5056. new_mct_records +
  5057. (l_mcc_record->m_offset_array -
  5058. l_tcp->m_mct_records);
  5059. }
  5060. }
  5061. }
  5062. l_tcp->m_mct_records = new_mct_records;
  5063. l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
  5064. memset(l_mct_data, 0, (l_tcp->m_nb_max_mct_records - l_tcp->m_nb_mct_records) *
  5065. sizeof(opj_mct_data_t));
  5066. }
  5067. l_mct_data = l_tcp->m_mct_records + l_tcp->m_nb_mct_records;
  5068. ++l_tcp->m_nb_mct_records;
  5069. }
  5070. if (l_mct_data->m_data) {
  5071. opj_free(l_mct_data->m_data);
  5072. l_mct_data->m_data = 00;
  5073. l_mct_data->m_data_size = 0;
  5074. }
  5075. l_mct_data->m_index = l_indix;
  5076. l_mct_data->m_array_type = (J2K_MCT_ARRAY_TYPE)((l_tmp >> 8) & 3);
  5077. l_mct_data->m_element_type = (J2K_MCT_ELEMENT_TYPE)((l_tmp >> 10) & 3);
  5078. opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymct */
  5079. p_header_data += 2;
  5080. if (l_tmp != 0) {
  5081. opj_event_msg(p_manager, EVT_WARNING,
  5082. "Cannot take in charge multiple MCT markers\n");
  5083. return OPJ_TRUE;
  5084. }
  5085. p_header_size -= 6;
  5086. l_mct_data->m_data = (OPJ_BYTE*)opj_malloc(p_header_size);
  5087. if (! l_mct_data->m_data) {
  5088. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCT marker\n");
  5089. return OPJ_FALSE;
  5090. }
  5091. memcpy(l_mct_data->m_data, p_header_data, p_header_size);
  5092. l_mct_data->m_data_size = p_header_size;
  5093. return OPJ_TRUE;
  5094. }
  5095. static OPJ_BOOL opj_j2k_write_mcc_record(opj_j2k_t *p_j2k,
  5096. struct opj_simple_mcc_decorrelation_data * p_mcc_record,
  5097. struct opj_stream_private *p_stream,
  5098. struct opj_event_mgr * p_manager)
  5099. {
  5100. OPJ_UINT32 i;
  5101. OPJ_UINT32 l_mcc_size;
  5102. OPJ_BYTE * l_current_data = 00;
  5103. OPJ_UINT32 l_nb_bytes_for_comp;
  5104. OPJ_UINT32 l_mask;
  5105. OPJ_UINT32 l_tmcc;
  5106. /* preconditions */
  5107. assert(p_j2k != 00);
  5108. assert(p_manager != 00);
  5109. assert(p_stream != 00);
  5110. if (p_mcc_record->m_nb_comps > 255) {
  5111. l_nb_bytes_for_comp = 2;
  5112. l_mask = 0x8000;
  5113. } else {
  5114. l_nb_bytes_for_comp = 1;
  5115. l_mask = 0;
  5116. }
  5117. l_mcc_size = p_mcc_record->m_nb_comps * 2 * l_nb_bytes_for_comp + 19;
  5118. if (l_mcc_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  5119. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  5120. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size);
  5121. if (! new_header_tile_data) {
  5122. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  5123. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  5124. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  5125. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCC marker\n");
  5126. return OPJ_FALSE;
  5127. }
  5128. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  5129. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mcc_size;
  5130. }
  5131. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  5132. opj_write_bytes(l_current_data, J2K_MS_MCC,
  5133. 2); /* MCC */
  5134. l_current_data += 2;
  5135. opj_write_bytes(l_current_data, l_mcc_size - 2,
  5136. 2); /* Lmcc */
  5137. l_current_data += 2;
  5138. /* first marker */
  5139. opj_write_bytes(l_current_data, 0,
  5140. 2); /* Zmcc */
  5141. l_current_data += 2;
  5142. opj_write_bytes(l_current_data, p_mcc_record->m_index,
  5143. 1); /* Imcc -> no need for other values, take the first */
  5144. ++l_current_data;
  5145. /* only one marker atm */
  5146. opj_write_bytes(l_current_data, 0,
  5147. 2); /* Ymcc */
  5148. l_current_data += 2;
  5149. opj_write_bytes(l_current_data, 1,
  5150. 2); /* Qmcc -> number of collections -> 1 */
  5151. l_current_data += 2;
  5152. opj_write_bytes(l_current_data, 0x1,
  5153. 1); /* Xmcci type of component transformation -> array based decorrelation */
  5154. ++l_current_data;
  5155. opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask,
  5156. 2); /* Nmcci number of input components involved and size for each component offset = 8 bits */
  5157. l_current_data += 2;
  5158. for (i = 0; i < p_mcc_record->m_nb_comps; ++i) {
  5159. opj_write_bytes(l_current_data, i,
  5160. l_nb_bytes_for_comp); /* Cmccij Component offset*/
  5161. l_current_data += l_nb_bytes_for_comp;
  5162. }
  5163. opj_write_bytes(l_current_data, p_mcc_record->m_nb_comps | l_mask,
  5164. 2); /* Mmcci number of output components involved and size for each component offset = 8 bits */
  5165. l_current_data += 2;
  5166. for (i = 0; i < p_mcc_record->m_nb_comps; ++i) {
  5167. opj_write_bytes(l_current_data, i,
  5168. l_nb_bytes_for_comp); /* Wmccij Component offset*/
  5169. l_current_data += l_nb_bytes_for_comp;
  5170. }
  5171. l_tmcc = ((!p_mcc_record->m_is_irreversible) & 1U) << 16;
  5172. if (p_mcc_record->m_decorrelation_array) {
  5173. l_tmcc |= p_mcc_record->m_decorrelation_array->m_index;
  5174. }
  5175. if (p_mcc_record->m_offset_array) {
  5176. l_tmcc |= ((p_mcc_record->m_offset_array->m_index) << 8);
  5177. }
  5178. opj_write_bytes(l_current_data, l_tmcc,
  5179. 3); /* Tmcci : use MCT defined as number 1 and irreversible array based. */
  5180. l_current_data += 3;
  5181. if (opj_stream_write_data(p_stream,
  5182. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mcc_size,
  5183. p_manager) != l_mcc_size) {
  5184. return OPJ_FALSE;
  5185. }
  5186. return OPJ_TRUE;
  5187. }
  5188. static OPJ_BOOL opj_j2k_read_mcc(opj_j2k_t *p_j2k,
  5189. OPJ_BYTE * p_header_data,
  5190. OPJ_UINT32 p_header_size,
  5191. opj_event_mgr_t * p_manager)
  5192. {
  5193. OPJ_UINT32 i, j;
  5194. OPJ_UINT32 l_tmp;
  5195. OPJ_UINT32 l_indix;
  5196. opj_tcp_t * l_tcp;
  5197. opj_simple_mcc_decorrelation_data_t * l_mcc_record;
  5198. opj_mct_data_t * l_mct_data;
  5199. OPJ_UINT32 l_nb_collections;
  5200. OPJ_UINT32 l_nb_comps;
  5201. OPJ_UINT32 l_nb_bytes_by_comp;
  5202. OPJ_BOOL l_new_mcc = OPJ_FALSE;
  5203. /* preconditions */
  5204. assert(p_header_data != 00);
  5205. assert(p_j2k != 00);
  5206. assert(p_manager != 00);
  5207. l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
  5208. &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
  5209. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  5210. if (p_header_size < 2) {
  5211. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5212. return OPJ_FALSE;
  5213. }
  5214. /* first marker */
  5215. opj_read_bytes(p_header_data, &l_tmp, 2); /* Zmcc */
  5216. p_header_data += 2;
  5217. if (l_tmp != 0) {
  5218. opj_event_msg(p_manager, EVT_WARNING,
  5219. "Cannot take in charge multiple data spanning\n");
  5220. return OPJ_TRUE;
  5221. }
  5222. if (p_header_size < 7) {
  5223. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5224. return OPJ_FALSE;
  5225. }
  5226. opj_read_bytes(p_header_data, &l_indix,
  5227. 1); /* Imcc -> no need for other values, take the first */
  5228. ++p_header_data;
  5229. l_mcc_record = l_tcp->m_mcc_records;
  5230. for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
  5231. if (l_mcc_record->m_index == l_indix) {
  5232. break;
  5233. }
  5234. ++l_mcc_record;
  5235. }
  5236. /** NOT FOUND */
  5237. if (i == l_tcp->m_nb_mcc_records) {
  5238. if (l_tcp->m_nb_mcc_records == l_tcp->m_nb_max_mcc_records) {
  5239. opj_simple_mcc_decorrelation_data_t *new_mcc_records;
  5240. l_tcp->m_nb_max_mcc_records += OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
  5241. new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
  5242. l_tcp->m_mcc_records, l_tcp->m_nb_max_mcc_records * sizeof(
  5243. opj_simple_mcc_decorrelation_data_t));
  5244. if (! new_mcc_records) {
  5245. opj_free(l_tcp->m_mcc_records);
  5246. l_tcp->m_mcc_records = NULL;
  5247. l_tcp->m_nb_max_mcc_records = 0;
  5248. l_tcp->m_nb_mcc_records = 0;
  5249. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read MCC marker\n");
  5250. return OPJ_FALSE;
  5251. }
  5252. l_tcp->m_mcc_records = new_mcc_records;
  5253. l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
  5254. memset(l_mcc_record, 0, (l_tcp->m_nb_max_mcc_records - l_tcp->m_nb_mcc_records)
  5255. * sizeof(opj_simple_mcc_decorrelation_data_t));
  5256. }
  5257. l_mcc_record = l_tcp->m_mcc_records + l_tcp->m_nb_mcc_records;
  5258. l_new_mcc = OPJ_TRUE;
  5259. }
  5260. l_mcc_record->m_index = l_indix;
  5261. /* only one marker atm */
  5262. opj_read_bytes(p_header_data, &l_tmp, 2); /* Ymcc */
  5263. p_header_data += 2;
  5264. if (l_tmp != 0) {
  5265. opj_event_msg(p_manager, EVT_WARNING,
  5266. "Cannot take in charge multiple data spanning\n");
  5267. return OPJ_TRUE;
  5268. }
  5269. opj_read_bytes(p_header_data, &l_nb_collections,
  5270. 2); /* Qmcc -> number of collections -> 1 */
  5271. p_header_data += 2;
  5272. if (l_nb_collections > 1) {
  5273. opj_event_msg(p_manager, EVT_WARNING,
  5274. "Cannot take in charge multiple collections\n");
  5275. return OPJ_TRUE;
  5276. }
  5277. p_header_size -= 7;
  5278. for (i = 0; i < l_nb_collections; ++i) {
  5279. if (p_header_size < 3) {
  5280. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5281. return OPJ_FALSE;
  5282. }
  5283. opj_read_bytes(p_header_data, &l_tmp,
  5284. 1); /* Xmcci type of component transformation -> array based decorrelation */
  5285. ++p_header_data;
  5286. if (l_tmp != 1) {
  5287. opj_event_msg(p_manager, EVT_WARNING,
  5288. "Cannot take in charge collections other than array decorrelation\n");
  5289. return OPJ_TRUE;
  5290. }
  5291. opj_read_bytes(p_header_data, &l_nb_comps, 2);
  5292. p_header_data += 2;
  5293. p_header_size -= 3;
  5294. l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15);
  5295. l_mcc_record->m_nb_comps = l_nb_comps & 0x7fff;
  5296. if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2)) {
  5297. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5298. return OPJ_FALSE;
  5299. }
  5300. p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 2);
  5301. for (j = 0; j < l_mcc_record->m_nb_comps; ++j) {
  5302. opj_read_bytes(p_header_data, &l_tmp,
  5303. l_nb_bytes_by_comp); /* Cmccij Component offset*/
  5304. p_header_data += l_nb_bytes_by_comp;
  5305. if (l_tmp != j) {
  5306. opj_event_msg(p_manager, EVT_WARNING,
  5307. "Cannot take in charge collections with indix shuffle\n");
  5308. return OPJ_TRUE;
  5309. }
  5310. }
  5311. opj_read_bytes(p_header_data, &l_nb_comps, 2);
  5312. p_header_data += 2;
  5313. l_nb_bytes_by_comp = 1 + (l_nb_comps >> 15);
  5314. l_nb_comps &= 0x7fff;
  5315. if (l_nb_comps != l_mcc_record->m_nb_comps) {
  5316. opj_event_msg(p_manager, EVT_WARNING,
  5317. "Cannot take in charge collections without same number of indixes\n");
  5318. return OPJ_TRUE;
  5319. }
  5320. if (p_header_size < (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3)) {
  5321. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5322. return OPJ_FALSE;
  5323. }
  5324. p_header_size -= (l_nb_bytes_by_comp * l_mcc_record->m_nb_comps + 3);
  5325. for (j = 0; j < l_mcc_record->m_nb_comps; ++j) {
  5326. opj_read_bytes(p_header_data, &l_tmp,
  5327. l_nb_bytes_by_comp); /* Wmccij Component offset*/
  5328. p_header_data += l_nb_bytes_by_comp;
  5329. if (l_tmp != j) {
  5330. opj_event_msg(p_manager, EVT_WARNING,
  5331. "Cannot take in charge collections with indix shuffle\n");
  5332. return OPJ_TRUE;
  5333. }
  5334. }
  5335. opj_read_bytes(p_header_data, &l_tmp, 3); /* Wmccij Component offset*/
  5336. p_header_data += 3;
  5337. l_mcc_record->m_is_irreversible = !((l_tmp >> 16) & 1);
  5338. l_mcc_record->m_decorrelation_array = 00;
  5339. l_mcc_record->m_offset_array = 00;
  5340. l_indix = l_tmp & 0xff;
  5341. if (l_indix != 0) {
  5342. l_mct_data = l_tcp->m_mct_records;
  5343. for (j = 0; j < l_tcp->m_nb_mct_records; ++j) {
  5344. if (l_mct_data->m_index == l_indix) {
  5345. l_mcc_record->m_decorrelation_array = l_mct_data;
  5346. break;
  5347. }
  5348. ++l_mct_data;
  5349. }
  5350. if (l_mcc_record->m_decorrelation_array == 00) {
  5351. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5352. return OPJ_FALSE;
  5353. }
  5354. }
  5355. l_indix = (l_tmp >> 8) & 0xff;
  5356. if (l_indix != 0) {
  5357. l_mct_data = l_tcp->m_mct_records;
  5358. for (j = 0; j < l_tcp->m_nb_mct_records; ++j) {
  5359. if (l_mct_data->m_index == l_indix) {
  5360. l_mcc_record->m_offset_array = l_mct_data;
  5361. break;
  5362. }
  5363. ++l_mct_data;
  5364. }
  5365. if (l_mcc_record->m_offset_array == 00) {
  5366. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5367. return OPJ_FALSE;
  5368. }
  5369. }
  5370. }
  5371. if (p_header_size != 0) {
  5372. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCC marker\n");
  5373. return OPJ_FALSE;
  5374. }
  5375. if (l_new_mcc) {
  5376. ++l_tcp->m_nb_mcc_records;
  5377. }
  5378. return OPJ_TRUE;
  5379. }
  5380. static OPJ_BOOL opj_j2k_write_mco(opj_j2k_t *p_j2k,
  5381. struct opj_stream_private *p_stream,
  5382. struct opj_event_mgr * p_manager
  5383. )
  5384. {
  5385. OPJ_BYTE * l_current_data = 00;
  5386. OPJ_UINT32 l_mco_size;
  5387. opj_tcp_t * l_tcp = 00;
  5388. opj_simple_mcc_decorrelation_data_t * l_mcc_record;
  5389. OPJ_UINT32 i;
  5390. /* preconditions */
  5391. assert(p_j2k != 00);
  5392. assert(p_manager != 00);
  5393. assert(p_stream != 00);
  5394. l_tcp = &(p_j2k->m_cp.tcps[p_j2k->m_current_tile_number]);
  5395. l_mco_size = 5 + l_tcp->m_nb_mcc_records;
  5396. if (l_mco_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  5397. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  5398. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size);
  5399. if (! new_header_tile_data) {
  5400. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  5401. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  5402. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  5403. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n");
  5404. return OPJ_FALSE;
  5405. }
  5406. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  5407. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_mco_size;
  5408. }
  5409. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  5410. opj_write_bytes(l_current_data, J2K_MS_MCO, 2); /* MCO */
  5411. l_current_data += 2;
  5412. opj_write_bytes(l_current_data, l_mco_size - 2, 2); /* Lmco */
  5413. l_current_data += 2;
  5414. opj_write_bytes(l_current_data, l_tcp->m_nb_mcc_records,
  5415. 1); /* Nmco : only one transform stage*/
  5416. ++l_current_data;
  5417. l_mcc_record = l_tcp->m_mcc_records;
  5418. for (i = 0; i < l_tcp->m_nb_mcc_records; ++i) {
  5419. opj_write_bytes(l_current_data, l_mcc_record->m_index,
  5420. 1); /* Imco -> use the mcc indicated by 1*/
  5421. ++l_current_data;
  5422. ++l_mcc_record;
  5423. }
  5424. if (opj_stream_write_data(p_stream,
  5425. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size,
  5426. p_manager) != l_mco_size) {
  5427. return OPJ_FALSE;
  5428. }
  5429. return OPJ_TRUE;
  5430. }
  5431. /**
  5432. * Reads a MCO marker (Multiple Component Transform Ordering)
  5433. *
  5434. * @param p_header_data the data contained in the MCO box.
  5435. * @param p_j2k the jpeg2000 codec.
  5436. * @param p_header_size the size of the data contained in the MCO marker.
  5437. * @param p_manager the user event manager.
  5438. */
  5439. static OPJ_BOOL opj_j2k_read_mco(opj_j2k_t *p_j2k,
  5440. OPJ_BYTE * p_header_data,
  5441. OPJ_UINT32 p_header_size,
  5442. opj_event_mgr_t * p_manager
  5443. )
  5444. {
  5445. OPJ_UINT32 l_tmp, i;
  5446. OPJ_UINT32 l_nb_stages;
  5447. opj_tcp_t * l_tcp;
  5448. opj_tccp_t * l_tccp;
  5449. opj_image_t * l_image;
  5450. /* preconditions */
  5451. assert(p_header_data != 00);
  5452. assert(p_j2k != 00);
  5453. assert(p_manager != 00);
  5454. l_image = p_j2k->m_private_image;
  5455. l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
  5456. &p_j2k->m_cp.tcps[p_j2k->m_current_tile_number] :
  5457. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  5458. if (p_header_size < 1) {
  5459. opj_event_msg(p_manager, EVT_ERROR, "Error reading MCO marker\n");
  5460. return OPJ_FALSE;
  5461. }
  5462. opj_read_bytes(p_header_data, &l_nb_stages,
  5463. 1); /* Nmco : only one transform stage*/
  5464. ++p_header_data;
  5465. if (l_nb_stages > 1) {
  5466. opj_event_msg(p_manager, EVT_WARNING,
  5467. "Cannot take in charge multiple transformation stages.\n");
  5468. return OPJ_TRUE;
  5469. }
  5470. if (p_header_size != l_nb_stages + 1) {
  5471. opj_event_msg(p_manager, EVT_WARNING, "Error reading MCO marker\n");
  5472. return OPJ_FALSE;
  5473. }
  5474. l_tccp = l_tcp->tccps;
  5475. for (i = 0; i < l_image->numcomps; ++i) {
  5476. l_tccp->m_dc_level_shift = 0;
  5477. ++l_tccp;
  5478. }
  5479. if (l_tcp->m_mct_decoding_matrix) {
  5480. opj_free(l_tcp->m_mct_decoding_matrix);
  5481. l_tcp->m_mct_decoding_matrix = 00;
  5482. }
  5483. for (i = 0; i < l_nb_stages; ++i) {
  5484. opj_read_bytes(p_header_data, &l_tmp, 1);
  5485. ++p_header_data;
  5486. if (! opj_j2k_add_mct(l_tcp, p_j2k->m_private_image, l_tmp)) {
  5487. return OPJ_FALSE;
  5488. }
  5489. }
  5490. return OPJ_TRUE;
  5491. }
  5492. static OPJ_BOOL opj_j2k_add_mct(opj_tcp_t * p_tcp, opj_image_t * p_image,
  5493. OPJ_UINT32 p_index)
  5494. {
  5495. OPJ_UINT32 i;
  5496. opj_simple_mcc_decorrelation_data_t * l_mcc_record;
  5497. opj_mct_data_t * l_deco_array, * l_offset_array;
  5498. OPJ_UINT32 l_data_size, l_mct_size, l_offset_size;
  5499. OPJ_UINT32 l_nb_elem;
  5500. OPJ_UINT32 * l_offset_data, * l_current_offset_data;
  5501. opj_tccp_t * l_tccp;
  5502. /* preconditions */
  5503. assert(p_tcp != 00);
  5504. l_mcc_record = p_tcp->m_mcc_records;
  5505. for (i = 0; i < p_tcp->m_nb_mcc_records; ++i) {
  5506. if (l_mcc_record->m_index == p_index) {
  5507. break;
  5508. }
  5509. }
  5510. if (i == p_tcp->m_nb_mcc_records) {
  5511. /** element discarded **/
  5512. return OPJ_TRUE;
  5513. }
  5514. if (l_mcc_record->m_nb_comps != p_image->numcomps) {
  5515. /** do not support number of comps != image */
  5516. return OPJ_TRUE;
  5517. }
  5518. l_deco_array = l_mcc_record->m_decorrelation_array;
  5519. if (l_deco_array) {
  5520. l_data_size = MCT_ELEMENT_SIZE[l_deco_array->m_element_type] * p_image->numcomps
  5521. * p_image->numcomps;
  5522. if (l_deco_array->m_data_size != l_data_size) {
  5523. return OPJ_FALSE;
  5524. }
  5525. l_nb_elem = p_image->numcomps * p_image->numcomps;
  5526. l_mct_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
  5527. p_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
  5528. if (! p_tcp->m_mct_decoding_matrix) {
  5529. return OPJ_FALSE;
  5530. }
  5531. j2k_mct_read_functions_to_float[l_deco_array->m_element_type](
  5532. l_deco_array->m_data, p_tcp->m_mct_decoding_matrix, l_nb_elem);
  5533. }
  5534. l_offset_array = l_mcc_record->m_offset_array;
  5535. if (l_offset_array) {
  5536. l_data_size = MCT_ELEMENT_SIZE[l_offset_array->m_element_type] *
  5537. p_image->numcomps;
  5538. if (l_offset_array->m_data_size != l_data_size) {
  5539. return OPJ_FALSE;
  5540. }
  5541. l_nb_elem = p_image->numcomps;
  5542. l_offset_size = l_nb_elem * (OPJ_UINT32)sizeof(OPJ_UINT32);
  5543. l_offset_data = (OPJ_UINT32*)opj_malloc(l_offset_size);
  5544. if (! l_offset_data) {
  5545. return OPJ_FALSE;
  5546. }
  5547. j2k_mct_read_functions_to_int32[l_offset_array->m_element_type](
  5548. l_offset_array->m_data, l_offset_data, l_nb_elem);
  5549. l_tccp = p_tcp->tccps;
  5550. l_current_offset_data = l_offset_data;
  5551. for (i = 0; i < p_image->numcomps; ++i) {
  5552. l_tccp->m_dc_level_shift = (OPJ_INT32) * (l_current_offset_data++);
  5553. ++l_tccp;
  5554. }
  5555. opj_free(l_offset_data);
  5556. }
  5557. return OPJ_TRUE;
  5558. }
  5559. static OPJ_BOOL opj_j2k_write_cbd(opj_j2k_t *p_j2k,
  5560. struct opj_stream_private *p_stream,
  5561. struct opj_event_mgr * p_manager)
  5562. {
  5563. OPJ_UINT32 i;
  5564. OPJ_UINT32 l_cbd_size;
  5565. OPJ_BYTE * l_current_data = 00;
  5566. opj_image_t *l_image = 00;
  5567. opj_image_comp_t * l_comp = 00;
  5568. /* preconditions */
  5569. assert(p_j2k != 00);
  5570. assert(p_manager != 00);
  5571. assert(p_stream != 00);
  5572. l_image = p_j2k->m_private_image;
  5573. l_cbd_size = 6 + p_j2k->m_private_image->numcomps;
  5574. if (l_cbd_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
  5575. OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
  5576. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size);
  5577. if (! new_header_tile_data) {
  5578. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  5579. p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
  5580. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  5581. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write CBD marker\n");
  5582. return OPJ_FALSE;
  5583. }
  5584. p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
  5585. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_cbd_size;
  5586. }
  5587. l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
  5588. opj_write_bytes(l_current_data, J2K_MS_CBD, 2); /* CBD */
  5589. l_current_data += 2;
  5590. opj_write_bytes(l_current_data, l_cbd_size - 2, 2); /* L_CBD */
  5591. l_current_data += 2;
  5592. opj_write_bytes(l_current_data, l_image->numcomps, 2); /* Ncbd */
  5593. l_current_data += 2;
  5594. l_comp = l_image->comps;
  5595. for (i = 0; i < l_image->numcomps; ++i) {
  5596. opj_write_bytes(l_current_data, (l_comp->sgnd << 7) | (l_comp->prec - 1),
  5597. 1); /* Component bit depth */
  5598. ++l_current_data;
  5599. ++l_comp;
  5600. }
  5601. if (opj_stream_write_data(p_stream,
  5602. p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_cbd_size,
  5603. p_manager) != l_cbd_size) {
  5604. return OPJ_FALSE;
  5605. }
  5606. return OPJ_TRUE;
  5607. }
  5608. /**
  5609. * Reads a CBD marker (Component bit depth definition)
  5610. * @param p_header_data the data contained in the CBD box.
  5611. * @param p_j2k the jpeg2000 codec.
  5612. * @param p_header_size the size of the data contained in the CBD marker.
  5613. * @param p_manager the user event manager.
  5614. */
  5615. static OPJ_BOOL opj_j2k_read_cbd(opj_j2k_t *p_j2k,
  5616. OPJ_BYTE * p_header_data,
  5617. OPJ_UINT32 p_header_size,
  5618. opj_event_mgr_t * p_manager
  5619. )
  5620. {
  5621. OPJ_UINT32 l_nb_comp, l_num_comp;
  5622. OPJ_UINT32 l_comp_def;
  5623. OPJ_UINT32 i;
  5624. opj_image_comp_t * l_comp = 00;
  5625. /* preconditions */
  5626. assert(p_header_data != 00);
  5627. assert(p_j2k != 00);
  5628. assert(p_manager != 00);
  5629. l_num_comp = p_j2k->m_private_image->numcomps;
  5630. if (p_header_size != (p_j2k->m_private_image->numcomps + 2)) {
  5631. opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
  5632. return OPJ_FALSE;
  5633. }
  5634. opj_read_bytes(p_header_data, &l_nb_comp,
  5635. 2); /* Ncbd */
  5636. p_header_data += 2;
  5637. if (l_nb_comp != l_num_comp) {
  5638. opj_event_msg(p_manager, EVT_ERROR, "Crror reading CBD marker\n");
  5639. return OPJ_FALSE;
  5640. }
  5641. l_comp = p_j2k->m_private_image->comps;
  5642. for (i = 0; i < l_num_comp; ++i) {
  5643. opj_read_bytes(p_header_data, &l_comp_def,
  5644. 1); /* Component bit depth */
  5645. ++p_header_data;
  5646. l_comp->sgnd = (l_comp_def >> 7) & 1;
  5647. l_comp->prec = (l_comp_def & 0x7f) + 1;
  5648. if (l_comp->prec > 31) {
  5649. opj_event_msg(p_manager, EVT_ERROR,
  5650. "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",
  5651. i, l_comp->prec);
  5652. return OPJ_FALSE;
  5653. }
  5654. ++l_comp;
  5655. }
  5656. return OPJ_TRUE;
  5657. }
  5658. /**
  5659. * Reads a CAP marker (extended capabilities definition). Empty implementation.
  5660. * Found in HTJ2K files.
  5661. *
  5662. * @param p_header_data the data contained in the CAP box.
  5663. * @param p_j2k the jpeg2000 codec.
  5664. * @param p_header_size the size of the data contained in the CAP marker.
  5665. * @param p_manager the user event manager.
  5666. */
  5667. static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k,
  5668. OPJ_BYTE * p_header_data,
  5669. OPJ_UINT32 p_header_size,
  5670. opj_event_mgr_t * p_manager
  5671. )
  5672. {
  5673. /* preconditions */
  5674. assert(p_header_data != 00);
  5675. assert(p_j2k != 00);
  5676. assert(p_manager != 00);
  5677. (void)p_j2k;
  5678. (void)p_header_data;
  5679. (void)p_header_size;
  5680. (void)p_manager;
  5681. return OPJ_TRUE;
  5682. }
  5683. /**
  5684. * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files
  5685. * @param p_header_data the data contained in the CPF box.
  5686. * @param p_j2k the jpeg2000 codec.
  5687. * @param p_header_size the size of the data contained in the CPF marker.
  5688. * @param p_manager the user event manager.
  5689. */
  5690. static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k,
  5691. OPJ_BYTE * p_header_data,
  5692. OPJ_UINT32 p_header_size,
  5693. opj_event_mgr_t * p_manager
  5694. )
  5695. {
  5696. /* preconditions */
  5697. assert(p_header_data != 00);
  5698. assert(p_j2k != 00);
  5699. assert(p_manager != 00);
  5700. (void)p_j2k;
  5701. (void)p_header_data;
  5702. (void)p_header_size;
  5703. (void)p_manager;
  5704. return OPJ_TRUE;
  5705. }
  5706. /* ----------------------------------------------------------------------- */
  5707. /* J2K / JPT decoder interface */
  5708. /* ----------------------------------------------------------------------- */
  5709. void opj_j2k_setup_decoder(opj_j2k_t *j2k, opj_dparameters_t *parameters)
  5710. {
  5711. if (j2k && parameters) {
  5712. j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
  5713. j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
  5714. j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG);
  5715. #ifdef USE_JPWL
  5716. j2k->m_cp.correct = parameters->jpwl_correct;
  5717. j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
  5718. j2k->m_cp.max_tiles = parameters->jpwl_max_tiles;
  5719. #endif /* USE_JPWL */
  5720. }
  5721. }
  5722. void opj_j2k_decoder_set_strict_mode(opj_j2k_t *j2k, OPJ_BOOL strict)
  5723. {
  5724. if (j2k) {
  5725. j2k->m_cp.strict = strict;
  5726. }
  5727. }
  5728. OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads)
  5729. {
  5730. /* Currently we pass the thread-pool to the tcd, so we cannot re-set it */
  5731. /* afterwards */
  5732. if (opj_has_thread_support() && j2k->m_tcd == NULL) {
  5733. opj_thread_pool_destroy(j2k->m_tp);
  5734. j2k->m_tp = NULL;
  5735. if (num_threads <= (OPJ_UINT32)INT_MAX) {
  5736. j2k->m_tp = opj_thread_pool_create((int)num_threads);
  5737. }
  5738. if (j2k->m_tp == NULL) {
  5739. j2k->m_tp = opj_thread_pool_create(0);
  5740. return OPJ_FALSE;
  5741. }
  5742. return OPJ_TRUE;
  5743. }
  5744. return OPJ_FALSE;
  5745. }
  5746. static int opj_j2k_get_default_thread_count(void)
  5747. {
  5748. const char* num_threads_str = getenv("OPJ_NUM_THREADS");
  5749. int num_cpus;
  5750. int num_threads;
  5751. if (num_threads_str == NULL || !opj_has_thread_support()) {
  5752. return 0;
  5753. }
  5754. num_cpus = opj_get_num_cpus();
  5755. if (strcmp(num_threads_str, "ALL_CPUS") == 0) {
  5756. return num_cpus;
  5757. }
  5758. if (num_cpus == 0) {
  5759. num_cpus = 32;
  5760. }
  5761. num_threads = atoi(num_threads_str);
  5762. if (num_threads < 0) {
  5763. num_threads = 0;
  5764. } else if (num_threads > 2 * num_cpus) {
  5765. num_threads = 2 * num_cpus;
  5766. }
  5767. return num_threads;
  5768. }
  5769. /* ----------------------------------------------------------------------- */
  5770. /* J2K encoder interface */
  5771. /* ----------------------------------------------------------------------- */
  5772. opj_j2k_t* opj_j2k_create_compress(void)
  5773. {
  5774. opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
  5775. if (!l_j2k) {
  5776. return NULL;
  5777. }
  5778. l_j2k->m_is_decoder = 0;
  5779. l_j2k->m_cp.m_is_decoder = 0;
  5780. l_j2k->m_specific_param.m_encoder.m_header_tile_data = (OPJ_BYTE *) opj_malloc(
  5781. OPJ_J2K_DEFAULT_HEADER_SIZE);
  5782. if (! l_j2k->m_specific_param.m_encoder.m_header_tile_data) {
  5783. opj_j2k_destroy(l_j2k);
  5784. return NULL;
  5785. }
  5786. l_j2k->m_specific_param.m_encoder.m_header_tile_data_size =
  5787. OPJ_J2K_DEFAULT_HEADER_SIZE;
  5788. /* validation list creation*/
  5789. l_j2k->m_validation_list = opj_procedure_list_create();
  5790. if (! l_j2k->m_validation_list) {
  5791. opj_j2k_destroy(l_j2k);
  5792. return NULL;
  5793. }
  5794. /* execution list creation*/
  5795. l_j2k->m_procedure_list = opj_procedure_list_create();
  5796. if (! l_j2k->m_procedure_list) {
  5797. opj_j2k_destroy(l_j2k);
  5798. return NULL;
  5799. }
  5800. l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count());
  5801. if (!l_j2k->m_tp) {
  5802. l_j2k->m_tp = opj_thread_pool_create(0);
  5803. }
  5804. if (!l_j2k->m_tp) {
  5805. opj_j2k_destroy(l_j2k);
  5806. return NULL;
  5807. }
  5808. return l_j2k;
  5809. }
  5810. static int opj_j2k_initialise_4K_poc(opj_poc_t *POC, int numres)
  5811. {
  5812. POC[0].tile = 1;
  5813. POC[0].resno0 = 0;
  5814. POC[0].compno0 = 0;
  5815. POC[0].layno1 = 1;
  5816. POC[0].resno1 = (OPJ_UINT32)(numres - 1);
  5817. POC[0].compno1 = 3;
  5818. POC[0].prg1 = OPJ_CPRL;
  5819. POC[1].tile = 1;
  5820. POC[1].resno0 = (OPJ_UINT32)(numres - 1);
  5821. POC[1].compno0 = 0;
  5822. POC[1].layno1 = 1;
  5823. POC[1].resno1 = (OPJ_UINT32)numres;
  5824. POC[1].compno1 = 3;
  5825. POC[1].prg1 = OPJ_CPRL;
  5826. return 2;
  5827. }
  5828. static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters,
  5829. opj_image_t *image, opj_event_mgr_t *p_manager)
  5830. {
  5831. /* Configure cinema parameters */
  5832. int i;
  5833. /* No tiling */
  5834. parameters->tile_size_on = OPJ_FALSE;
  5835. parameters->cp_tdx = 1;
  5836. parameters->cp_tdy = 1;
  5837. /* One tile part for each component */
  5838. parameters->tp_flag = 'C';
  5839. parameters->tp_on = 1;
  5840. /* Tile and Image shall be at (0,0) */
  5841. parameters->cp_tx0 = 0;
  5842. parameters->cp_ty0 = 0;
  5843. parameters->image_offset_x0 = 0;
  5844. parameters->image_offset_y0 = 0;
  5845. /* Codeblock size= 32*32 */
  5846. parameters->cblockw_init = 32;
  5847. parameters->cblockh_init = 32;
  5848. /* Codeblock style: no mode switch enabled */
  5849. parameters->mode = 0;
  5850. /* No ROI */
  5851. parameters->roi_compno = -1;
  5852. /* No subsampling */
  5853. parameters->subsampling_dx = 1;
  5854. parameters->subsampling_dy = 1;
  5855. /* 9-7 transform */
  5856. parameters->irreversible = 1;
  5857. /* Number of layers */
  5858. if (parameters->tcp_numlayers > 1) {
  5859. opj_event_msg(p_manager, EVT_WARNING,
  5860. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5861. "1 single quality layer"
  5862. "-> Number of layers forced to 1 (rather than %d)\n"
  5863. "-> Rate of the last layer (%3.1f) will be used",
  5864. parameters->tcp_numlayers,
  5865. parameters->tcp_rates[parameters->tcp_numlayers - 1]);
  5866. parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers - 1];
  5867. parameters->tcp_numlayers = 1;
  5868. }
  5869. /* Resolution levels */
  5870. switch (parameters->rsiz) {
  5871. case OPJ_PROFILE_CINEMA_2K:
  5872. if (parameters->numresolution > 6) {
  5873. opj_event_msg(p_manager, EVT_WARNING,
  5874. "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
  5875. "Number of decomposition levels <= 5\n"
  5876. "-> Number of decomposition levels forced to 5 (rather than %d)\n",
  5877. parameters->numresolution + 1);
  5878. parameters->numresolution = 6;
  5879. }
  5880. break;
  5881. case OPJ_PROFILE_CINEMA_4K:
  5882. if (parameters->numresolution < 2) {
  5883. opj_event_msg(p_manager, EVT_WARNING,
  5884. "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
  5885. "Number of decomposition levels >= 1 && <= 6\n"
  5886. "-> Number of decomposition levels forced to 1 (rather than %d)\n",
  5887. parameters->numresolution + 1);
  5888. parameters->numresolution = 1;
  5889. } else if (parameters->numresolution > 7) {
  5890. opj_event_msg(p_manager, EVT_WARNING,
  5891. "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
  5892. "Number of decomposition levels >= 1 && <= 6\n"
  5893. "-> Number of decomposition levels forced to 6 (rather than %d)\n",
  5894. parameters->numresolution + 1);
  5895. parameters->numresolution = 7;
  5896. }
  5897. break;
  5898. default :
  5899. break;
  5900. }
  5901. /* Precincts */
  5902. parameters->csty |= J2K_CP_CSTY_PRT;
  5903. if (parameters->numresolution == 1) {
  5904. parameters->res_spec = 1;
  5905. parameters->prcw_init[0] = 128;
  5906. parameters->prch_init[0] = 128;
  5907. } else {
  5908. parameters->res_spec = parameters->numresolution - 1;
  5909. for (i = 0; i < parameters->res_spec; i++) {
  5910. parameters->prcw_init[i] = 256;
  5911. parameters->prch_init[i] = 256;
  5912. }
  5913. }
  5914. /* The progression order shall be CPRL */
  5915. parameters->prog_order = OPJ_CPRL;
  5916. /* Progression order changes for 4K, disallowed for 2K */
  5917. if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) {
  5918. parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC,
  5919. parameters->numresolution);
  5920. } else {
  5921. parameters->numpocs = 0;
  5922. }
  5923. /* Limited bit-rate */
  5924. parameters->cp_disto_alloc = 1;
  5925. if (parameters->max_cs_size <= 0) {
  5926. /* No rate has been introduced, 24 fps is assumed */
  5927. parameters->max_cs_size = OPJ_CINEMA_24_CS;
  5928. opj_event_msg(p_manager, EVT_WARNING,
  5929. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5930. "Maximum 1302083 compressed bytes @ 24fps\n"
  5931. "As no rate has been given, this limit will be used.\n");
  5932. } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) {
  5933. opj_event_msg(p_manager, EVT_WARNING,
  5934. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5935. "Maximum 1302083 compressed bytes @ 24fps\n"
  5936. "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n");
  5937. parameters->max_cs_size = OPJ_CINEMA_24_CS;
  5938. }
  5939. if (parameters->max_comp_size <= 0) {
  5940. /* No rate has been introduced, 24 fps is assumed */
  5941. parameters->max_comp_size = OPJ_CINEMA_24_COMP;
  5942. opj_event_msg(p_manager, EVT_WARNING,
  5943. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5944. "Maximum 1041666 compressed bytes @ 24fps\n"
  5945. "As no rate has been given, this limit will be used.\n");
  5946. } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) {
  5947. opj_event_msg(p_manager, EVT_WARNING,
  5948. "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
  5949. "Maximum 1041666 compressed bytes @ 24fps\n"
  5950. "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n");
  5951. parameters->max_comp_size = OPJ_CINEMA_24_COMP;
  5952. }
  5953. parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w *
  5954. image->comps[0].h * image->comps[0].prec) /
  5955. (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx *
  5956. image->comps[0].dy);
  5957. }
  5958. static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz,
  5959. opj_event_mgr_t *p_manager)
  5960. {
  5961. OPJ_UINT32 i;
  5962. /* Number of components */
  5963. if (image->numcomps != 3) {
  5964. opj_event_msg(p_manager, EVT_WARNING,
  5965. "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
  5966. "3 components"
  5967. "-> Number of components of input image (%d) is not compliant\n"
  5968. "-> Non-profile-3 codestream will be generated\n",
  5969. image->numcomps);
  5970. return OPJ_FALSE;
  5971. }
  5972. /* Bitdepth */
  5973. for (i = 0; i < image->numcomps; i++) {
  5974. if ((image->comps[i].prec != 12) | (image->comps[i].sgnd)) {
  5975. char signed_str[] = "signed";
  5976. char unsigned_str[] = "unsigned";
  5977. char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str;
  5978. opj_event_msg(p_manager, EVT_WARNING,
  5979. "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
  5980. "Precision of each component shall be 12 bits unsigned"
  5981. "-> At least component %d of input image (%d bits, %s) is not compliant\n"
  5982. "-> Non-profile-3 codestream will be generated\n",
  5983. i, image->comps[i].prec, tmp_str);
  5984. return OPJ_FALSE;
  5985. }
  5986. }
  5987. /* Image size */
  5988. switch (rsiz) {
  5989. case OPJ_PROFILE_CINEMA_2K:
  5990. if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))) {
  5991. opj_event_msg(p_manager, EVT_WARNING,
  5992. "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
  5993. "width <= 2048 and height <= 1080\n"
  5994. "-> Input image size %d x %d is not compliant\n"
  5995. "-> Non-profile-3 codestream will be generated\n",
  5996. image->comps[0].w, image->comps[0].h);
  5997. return OPJ_FALSE;
  5998. }
  5999. break;
  6000. case OPJ_PROFILE_CINEMA_4K:
  6001. if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))) {
  6002. opj_event_msg(p_manager, EVT_WARNING,
  6003. "JPEG 2000 Profile-4 (4k dc profile) requires:\n"
  6004. "width <= 4096 and height <= 2160\n"
  6005. "-> Image size %d x %d is not compliant\n"
  6006. "-> Non-profile-4 codestream will be generated\n",
  6007. image->comps[0].w, image->comps[0].h);
  6008. return OPJ_FALSE;
  6009. }
  6010. break;
  6011. default :
  6012. break;
  6013. }
  6014. return OPJ_TRUE;
  6015. }
  6016. static int opj_j2k_get_imf_max_NL(opj_cparameters_t *parameters,
  6017. opj_image_t *image)
  6018. {
  6019. /* Decomposition levels */
  6020. const OPJ_UINT16 rsiz = parameters->rsiz;
  6021. const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz);
  6022. const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32)
  6023. parameters->cp_tdx : image->x1;
  6024. switch (profile) {
  6025. case OPJ_PROFILE_IMF_2K:
  6026. return 5;
  6027. case OPJ_PROFILE_IMF_4K:
  6028. return 6;
  6029. case OPJ_PROFILE_IMF_8K:
  6030. return 7;
  6031. case OPJ_PROFILE_IMF_2K_R: {
  6032. if (XTsiz >= 2048) {
  6033. return 5;
  6034. } else if (XTsiz >= 1024) {
  6035. return 4;
  6036. }
  6037. break;
  6038. }
  6039. case OPJ_PROFILE_IMF_4K_R: {
  6040. if (XTsiz >= 4096) {
  6041. return 6;
  6042. } else if (XTsiz >= 2048) {
  6043. return 5;
  6044. } else if (XTsiz >= 1024) {
  6045. return 4;
  6046. }
  6047. break;
  6048. }
  6049. case OPJ_PROFILE_IMF_8K_R: {
  6050. if (XTsiz >= 8192) {
  6051. return 7;
  6052. } else if (XTsiz >= 4096) {
  6053. return 6;
  6054. } else if (XTsiz >= 2048) {
  6055. return 5;
  6056. } else if (XTsiz >= 1024) {
  6057. return 4;
  6058. }
  6059. break;
  6060. }
  6061. default:
  6062. break;
  6063. }
  6064. return -1;
  6065. }
  6066. static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters,
  6067. opj_image_t *image, opj_event_mgr_t *p_manager)
  6068. {
  6069. const OPJ_UINT16 rsiz = parameters->rsiz;
  6070. const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz);
  6071. OPJ_UNUSED(p_manager);
  6072. /* Override defaults set by opj_set_default_encoder_parameters */
  6073. if (parameters->cblockw_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKW &&
  6074. parameters->cblockh_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKH) {
  6075. parameters->cblockw_init = 32;
  6076. parameters->cblockh_init = 32;
  6077. }
  6078. /* One tile part for each component */
  6079. parameters->tp_flag = 'C';
  6080. parameters->tp_on = 1;
  6081. if (parameters->prog_order == OPJ_COMP_PARAM_DEFAULT_PROG_ORDER) {
  6082. parameters->prog_order = OPJ_CPRL;
  6083. }
  6084. if (profile == OPJ_PROFILE_IMF_2K ||
  6085. profile == OPJ_PROFILE_IMF_4K ||
  6086. profile == OPJ_PROFILE_IMF_8K) {
  6087. /* 9-7 transform */
  6088. parameters->irreversible = 1;
  6089. }
  6090. /* Adjust the number of resolutions if set to its defaults */
  6091. if (parameters->numresolution == OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION &&
  6092. image->x0 == 0 &&
  6093. image->y0 == 0) {
  6094. const int max_NL = opj_j2k_get_imf_max_NL(parameters, image);
  6095. if (max_NL >= 0 && parameters->numresolution > max_NL) {
  6096. parameters->numresolution = max_NL + 1;
  6097. }
  6098. /* Note: below is generic logic */
  6099. if (!parameters->tile_size_on) {
  6100. while (parameters->numresolution > 0) {
  6101. if (image->x1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) {
  6102. parameters->numresolution --;
  6103. continue;
  6104. }
  6105. if (image->y1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) {
  6106. parameters->numresolution --;
  6107. continue;
  6108. }
  6109. break;
  6110. }
  6111. }
  6112. }
  6113. /* Set defaults precincts */
  6114. if (parameters->csty == 0) {
  6115. parameters->csty |= J2K_CP_CSTY_PRT;
  6116. if (parameters->numresolution == 1) {
  6117. parameters->res_spec = 1;
  6118. parameters->prcw_init[0] = 128;
  6119. parameters->prch_init[0] = 128;
  6120. } else {
  6121. int i;
  6122. parameters->res_spec = parameters->numresolution - 1;
  6123. for (i = 0; i < parameters->res_spec; i++) {
  6124. parameters->prcw_init[i] = 256;
  6125. parameters->prch_init[i] = 256;
  6126. }
  6127. }
  6128. }
  6129. }
  6130. /* Table A.53 from JPEG2000 standard */
  6131. static const OPJ_UINT16 tabMaxSubLevelFromMainLevel[] = {
  6132. 15, /* unspecified */
  6133. 1,
  6134. 1,
  6135. 1,
  6136. 2,
  6137. 3,
  6138. 4,
  6139. 5,
  6140. 6,
  6141. 7,
  6142. 8,
  6143. 9
  6144. };
  6145. static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters,
  6146. opj_image_t *image,
  6147. opj_event_mgr_t *p_manager)
  6148. {
  6149. OPJ_UINT32 i;
  6150. const OPJ_UINT16 rsiz = parameters->rsiz;
  6151. const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz);
  6152. const OPJ_UINT16 mainlevel = OPJ_GET_IMF_MAINLEVEL(rsiz);
  6153. const OPJ_UINT16 sublevel = OPJ_GET_IMF_SUBLEVEL(rsiz);
  6154. const int NL = parameters->numresolution - 1;
  6155. const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32)
  6156. parameters->cp_tdx : image->x1;
  6157. OPJ_BOOL ret = OPJ_TRUE;
  6158. /* Validate mainlevel */
  6159. if (mainlevel > OPJ_IMF_MAINLEVEL_MAX) {
  6160. opj_event_msg(p_manager, EVT_WARNING,
  6161. "IMF profile require mainlevel <= 11.\n"
  6162. "-> %d is thus not compliant\n"
  6163. "-> Non-IMF codestream will be generated\n",
  6164. mainlevel);
  6165. ret = OPJ_FALSE;
  6166. } else {
  6167. /* Validate sublevel */
  6168. assert(sizeof(tabMaxSubLevelFromMainLevel) ==
  6169. (OPJ_IMF_MAINLEVEL_MAX + 1) * sizeof(tabMaxSubLevelFromMainLevel[0]));
  6170. if (sublevel > tabMaxSubLevelFromMainLevel[mainlevel]) {
  6171. opj_event_msg(p_manager, EVT_WARNING,
  6172. "IMF profile require sublevel <= %d for mainlevel = %d.\n"
  6173. "-> %d is thus not compliant\n"
  6174. "-> Non-IMF codestream will be generated\n",
  6175. tabMaxSubLevelFromMainLevel[mainlevel],
  6176. mainlevel,
  6177. sublevel);
  6178. ret = OPJ_FALSE;
  6179. }
  6180. }
  6181. /* Number of components */
  6182. if (image->numcomps > 3) {
  6183. opj_event_msg(p_manager, EVT_WARNING,
  6184. "IMF profiles require at most 3 components.\n"
  6185. "-> Number of components of input image (%d) is not compliant\n"
  6186. "-> Non-IMF codestream will be generated\n",
  6187. image->numcomps);
  6188. ret = OPJ_FALSE;
  6189. }
  6190. if (image->x0 != 0 || image->y0 != 0) {
  6191. opj_event_msg(p_manager, EVT_WARNING,
  6192. "IMF profiles require image origin to be at 0,0.\n"
  6193. "-> %d,%d is not compliant\n"
  6194. "-> Non-IMF codestream will be generated\n",
  6195. image->x0, image->y0 != 0);
  6196. ret = OPJ_FALSE;
  6197. }
  6198. if (parameters->cp_tx0 != 0 || parameters->cp_ty0 != 0) {
  6199. opj_event_msg(p_manager, EVT_WARNING,
  6200. "IMF profiles require tile origin to be at 0,0.\n"
  6201. "-> %d,%d is not compliant\n"
  6202. "-> Non-IMF codestream will be generated\n",
  6203. parameters->cp_tx0, parameters->cp_ty0);
  6204. ret = OPJ_FALSE;
  6205. }
  6206. if (parameters->tile_size_on) {
  6207. if (profile == OPJ_PROFILE_IMF_2K ||
  6208. profile == OPJ_PROFILE_IMF_4K ||
  6209. profile == OPJ_PROFILE_IMF_8K) {
  6210. if ((OPJ_UINT32)parameters->cp_tdx < image->x1 ||
  6211. (OPJ_UINT32)parameters->cp_tdy < image->y1) {
  6212. opj_event_msg(p_manager, EVT_WARNING,
  6213. "IMF 2K/4K/8K single tile profiles require tile to be greater or equal to image size.\n"
  6214. "-> %d,%d is lesser than %d,%d\n"
  6215. "-> Non-IMF codestream will be generated\n",
  6216. parameters->cp_tdx,
  6217. parameters->cp_tdy,
  6218. image->x1,
  6219. image->y1);
  6220. ret = OPJ_FALSE;
  6221. }
  6222. } else {
  6223. if ((OPJ_UINT32)parameters->cp_tdx >= image->x1 &&
  6224. (OPJ_UINT32)parameters->cp_tdy >= image->y1) {
  6225. /* ok */
  6226. } else if (parameters->cp_tdx == 1024 &&
  6227. parameters->cp_tdy == 1024) {
  6228. /* ok */
  6229. } else if (parameters->cp_tdx == 2048 &&
  6230. parameters->cp_tdy == 2048 &&
  6231. (profile == OPJ_PROFILE_IMF_4K ||
  6232. profile == OPJ_PROFILE_IMF_8K)) {
  6233. /* ok */
  6234. } else if (parameters->cp_tdx == 4096 &&
  6235. parameters->cp_tdy == 4096 &&
  6236. profile == OPJ_PROFILE_IMF_8K) {
  6237. /* ok */
  6238. } else {
  6239. opj_event_msg(p_manager, EVT_WARNING,
  6240. "IMF 2K_R/4K_R/8K_R single/multiple tile profiles "
  6241. "require tile to be greater or equal to image size,\n"
  6242. "or to be (1024,1024), or (2048,2048) for 4K_R/8K_R "
  6243. "or (4096,4096) for 8K_R.\n"
  6244. "-> %d,%d is non conformant\n"
  6245. "-> Non-IMF codestream will be generated\n",
  6246. parameters->cp_tdx,
  6247. parameters->cp_tdy);
  6248. ret = OPJ_FALSE;
  6249. }
  6250. }
  6251. }
  6252. /* Bitdepth */
  6253. for (i = 0; i < image->numcomps; i++) {
  6254. if (!(image->comps[i].prec >= 8 && image->comps[i].prec <= 16) ||
  6255. (image->comps[i].sgnd)) {
  6256. char signed_str[] = "signed";
  6257. char unsigned_str[] = "unsigned";
  6258. char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str;
  6259. opj_event_msg(p_manager, EVT_WARNING,
  6260. "IMF profiles require precision of each component to b in [8-16] bits unsigned"
  6261. "-> At least component %d of input image (%d bits, %s) is not compliant\n"
  6262. "-> Non-IMF codestream will be generated\n",
  6263. i, image->comps[i].prec, tmp_str);
  6264. ret = OPJ_FALSE;
  6265. }
  6266. }
  6267. /* Sub-sampling */
  6268. for (i = 0; i < image->numcomps; i++) {
  6269. if (i == 0 && image->comps[i].dx != 1) {
  6270. opj_event_msg(p_manager, EVT_WARNING,
  6271. "IMF profiles require XRSiz1 == 1. Here it is set to %d.\n"
  6272. "-> Non-IMF codestream will be generated\n",
  6273. image->comps[i].dx);
  6274. ret = OPJ_FALSE;
  6275. }
  6276. if (i == 1 && image->comps[i].dx != 1 && image->comps[i].dx != 2) {
  6277. opj_event_msg(p_manager, EVT_WARNING,
  6278. "IMF profiles require XRSiz2 == 1 or 2. Here it is set to %d.\n"
  6279. "-> Non-IMF codestream will be generated\n",
  6280. image->comps[i].dx);
  6281. ret = OPJ_FALSE;
  6282. }
  6283. if (i > 1 && image->comps[i].dx != image->comps[i - 1].dx) {
  6284. opj_event_msg(p_manager, EVT_WARNING,
  6285. "IMF profiles require XRSiz%d to be the same as XRSiz2. "
  6286. "Here it is set to %d instead of %d.\n"
  6287. "-> Non-IMF codestream will be generated\n",
  6288. i + 1, image->comps[i].dx, image->comps[i - 1].dx);
  6289. ret = OPJ_FALSE;
  6290. }
  6291. if (image->comps[i].dy != 1) {
  6292. opj_event_msg(p_manager, EVT_WARNING,
  6293. "IMF profiles require YRsiz == 1. "
  6294. "Here it is set to %d for component %d.\n"
  6295. "-> Non-IMF codestream will be generated\n",
  6296. image->comps[i].dy, i);
  6297. ret = OPJ_FALSE;
  6298. }
  6299. }
  6300. /* Image size */
  6301. switch (profile) {
  6302. case OPJ_PROFILE_IMF_2K:
  6303. case OPJ_PROFILE_IMF_2K_R:
  6304. if (((image->comps[0].w > 2048) | (image->comps[0].h > 1556))) {
  6305. opj_event_msg(p_manager, EVT_WARNING,
  6306. "IMF 2K/2K_R profile require:\n"
  6307. "width <= 2048 and height <= 1556\n"
  6308. "-> Input image size %d x %d is not compliant\n"
  6309. "-> Non-IMF codestream will be generated\n",
  6310. image->comps[0].w, image->comps[0].h);
  6311. ret = OPJ_FALSE;
  6312. }
  6313. break;
  6314. case OPJ_PROFILE_IMF_4K:
  6315. case OPJ_PROFILE_IMF_4K_R:
  6316. if (((image->comps[0].w > 4096) | (image->comps[0].h > 3112))) {
  6317. opj_event_msg(p_manager, EVT_WARNING,
  6318. "IMF 4K/4K_R profile require:\n"
  6319. "width <= 4096 and height <= 3112\n"
  6320. "-> Input image size %d x %d is not compliant\n"
  6321. "-> Non-IMF codestream will be generated\n",
  6322. image->comps[0].w, image->comps[0].h);
  6323. ret = OPJ_FALSE;
  6324. }
  6325. break;
  6326. case OPJ_PROFILE_IMF_8K:
  6327. case OPJ_PROFILE_IMF_8K_R:
  6328. if (((image->comps[0].w > 8192) | (image->comps[0].h > 6224))) {
  6329. opj_event_msg(p_manager, EVT_WARNING,
  6330. "IMF 8K/8K_R profile require:\n"
  6331. "width <= 8192 and height <= 6224\n"
  6332. "-> Input image size %d x %d is not compliant\n"
  6333. "-> Non-IMF codestream will be generated\n",
  6334. image->comps[0].w, image->comps[0].h);
  6335. ret = OPJ_FALSE;
  6336. }
  6337. break;
  6338. default :
  6339. assert(0);
  6340. return OPJ_FALSE;
  6341. }
  6342. if (parameters->roi_compno != -1) {
  6343. opj_event_msg(p_manager, EVT_WARNING,
  6344. "IMF profile forbid RGN / region of interest marker.\n"
  6345. "-> Compression parameters specify a ROI\n"
  6346. "-> Non-IMF codestream will be generated\n");
  6347. ret = OPJ_FALSE;
  6348. }
  6349. if (parameters->cblockw_init != 32 || parameters->cblockh_init != 32) {
  6350. opj_event_msg(p_manager, EVT_WARNING,
  6351. "IMF profile require code block size to be 32x32.\n"
  6352. "-> Compression parameters set it to %dx%d.\n"
  6353. "-> Non-IMF codestream will be generated\n",
  6354. parameters->cblockw_init,
  6355. parameters->cblockh_init);
  6356. ret = OPJ_FALSE;
  6357. }
  6358. if (parameters->prog_order != OPJ_CPRL) {
  6359. opj_event_msg(p_manager, EVT_WARNING,
  6360. "IMF profile require progression order to be CPRL.\n"
  6361. "-> Compression parameters set it to %d.\n"
  6362. "-> Non-IMF codestream will be generated\n",
  6363. parameters->prog_order);
  6364. ret = OPJ_FALSE;
  6365. }
  6366. if (parameters->numpocs != 0) {
  6367. opj_event_msg(p_manager, EVT_WARNING,
  6368. "IMF profile forbid POC markers.\n"
  6369. "-> Compression parameters set %d POC.\n"
  6370. "-> Non-IMF codestream will be generated\n",
  6371. parameters->numpocs);
  6372. ret = OPJ_FALSE;
  6373. }
  6374. /* Codeblock style: no mode switch enabled */
  6375. if (parameters->mode != 0) {
  6376. opj_event_msg(p_manager, EVT_WARNING,
  6377. "IMF profile forbid mode switch in code block style.\n"
  6378. "-> Compression parameters set code block style to %d.\n"
  6379. "-> Non-IMF codestream will be generated\n",
  6380. parameters->mode);
  6381. ret = OPJ_FALSE;
  6382. }
  6383. if (profile == OPJ_PROFILE_IMF_2K ||
  6384. profile == OPJ_PROFILE_IMF_4K ||
  6385. profile == OPJ_PROFILE_IMF_8K) {
  6386. /* Expect 9-7 transform */
  6387. if (parameters->irreversible != 1) {
  6388. opj_event_msg(p_manager, EVT_WARNING,
  6389. "IMF 2K/4K/8K profiles require 9-7 Irreversible Transform.\n"
  6390. "-> Compression parameters set it to reversible.\n"
  6391. "-> Non-IMF codestream will be generated\n");
  6392. ret = OPJ_FALSE;
  6393. }
  6394. } else {
  6395. /* Expect 5-3 transform */
  6396. if (parameters->irreversible != 0) {
  6397. opj_event_msg(p_manager, EVT_WARNING,
  6398. "IMF 2K/4K/8K profiles require 5-3 reversible Transform.\n"
  6399. "-> Compression parameters set it to irreversible.\n"
  6400. "-> Non-IMF codestream will be generated\n");
  6401. ret = OPJ_FALSE;
  6402. }
  6403. }
  6404. /* Number of layers */
  6405. if (parameters->tcp_numlayers != 1) {
  6406. opj_event_msg(p_manager, EVT_WARNING,
  6407. "IMF 2K/4K/8K profiles require 1 single quality layer.\n"
  6408. "-> Number of layers is %d.\n"
  6409. "-> Non-IMF codestream will be generated\n",
  6410. parameters->tcp_numlayers);
  6411. ret = OPJ_FALSE;
  6412. }
  6413. /* Decomposition levels */
  6414. switch (profile) {
  6415. case OPJ_PROFILE_IMF_2K:
  6416. if (!(NL >= 1 && NL <= 5)) {
  6417. opj_event_msg(p_manager, EVT_WARNING,
  6418. "IMF 2K profile requires 1 <= NL <= 5:\n"
  6419. "-> Number of decomposition levels is %d.\n"
  6420. "-> Non-IMF codestream will be generated\n",
  6421. NL);
  6422. ret = OPJ_FALSE;
  6423. }
  6424. break;
  6425. case OPJ_PROFILE_IMF_4K:
  6426. if (!(NL >= 1 && NL <= 6)) {
  6427. opj_event_msg(p_manager, EVT_WARNING,
  6428. "IMF 4K profile requires 1 <= NL <= 6:\n"
  6429. "-> Number of decomposition levels is %d.\n"
  6430. "-> Non-IMF codestream will be generated\n",
  6431. NL);
  6432. ret = OPJ_FALSE;
  6433. }
  6434. break;
  6435. case OPJ_PROFILE_IMF_8K:
  6436. if (!(NL >= 1 && NL <= 7)) {
  6437. opj_event_msg(p_manager, EVT_WARNING,
  6438. "IMF 8K profile requires 1 <= NL <= 7:\n"
  6439. "-> Number of decomposition levels is %d.\n"
  6440. "-> Non-IMF codestream will be generated\n",
  6441. NL);
  6442. ret = OPJ_FALSE;
  6443. }
  6444. break;
  6445. case OPJ_PROFILE_IMF_2K_R: {
  6446. if (XTsiz >= 2048) {
  6447. if (!(NL >= 1 && NL <= 5)) {
  6448. opj_event_msg(p_manager, EVT_WARNING,
  6449. "IMF 2K_R profile requires 1 <= NL <= 5 for XTsiz >= 2048:\n"
  6450. "-> Number of decomposition levels is %d.\n"
  6451. "-> Non-IMF codestream will be generated\n",
  6452. NL);
  6453. ret = OPJ_FALSE;
  6454. }
  6455. } else if (XTsiz >= 1024) {
  6456. if (!(NL >= 1 && NL <= 4)) {
  6457. opj_event_msg(p_manager, EVT_WARNING,
  6458. "IMF 2K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n"
  6459. "-> Number of decomposition levels is %d.\n"
  6460. "-> Non-IMF codestream will be generated\n",
  6461. NL);
  6462. ret = OPJ_FALSE;
  6463. }
  6464. }
  6465. break;
  6466. }
  6467. case OPJ_PROFILE_IMF_4K_R: {
  6468. if (XTsiz >= 4096) {
  6469. if (!(NL >= 1 && NL <= 6)) {
  6470. opj_event_msg(p_manager, EVT_WARNING,
  6471. "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz >= 4096:\n"
  6472. "-> Number of decomposition levels is %d.\n"
  6473. "-> Non-IMF codestream will be generated\n",
  6474. NL);
  6475. ret = OPJ_FALSE;
  6476. }
  6477. } else if (XTsiz >= 2048) {
  6478. if (!(NL >= 1 && NL <= 5)) {
  6479. opj_event_msg(p_manager, EVT_WARNING,
  6480. "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n"
  6481. "-> Number of decomposition levels is %d.\n"
  6482. "-> Non-IMF codestream will be generated\n",
  6483. NL);
  6484. ret = OPJ_FALSE;
  6485. }
  6486. } else if (XTsiz >= 1024) {
  6487. if (!(NL >= 1 && NL <= 4)) {
  6488. opj_event_msg(p_manager, EVT_WARNING,
  6489. "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n"
  6490. "-> Number of decomposition levels is %d.\n"
  6491. "-> Non-IMF codestream will be generated\n",
  6492. NL);
  6493. ret = OPJ_FALSE;
  6494. }
  6495. }
  6496. break;
  6497. }
  6498. case OPJ_PROFILE_IMF_8K_R: {
  6499. if (XTsiz >= 8192) {
  6500. if (!(NL >= 1 && NL <= 7)) {
  6501. opj_event_msg(p_manager, EVT_WARNING,
  6502. "IMF 4K_R profile requires 1 <= NL <= 7 for XTsiz >= 8192:\n"
  6503. "-> Number of decomposition levels is %d.\n"
  6504. "-> Non-IMF codestream will be generated\n",
  6505. NL);
  6506. ret = OPJ_FALSE;
  6507. }
  6508. } else if (XTsiz >= 4096) {
  6509. if (!(NL >= 1 && NL <= 6)) {
  6510. opj_event_msg(p_manager, EVT_WARNING,
  6511. "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz in [4096,8192[:\n"
  6512. "-> Number of decomposition levels is %d.\n"
  6513. "-> Non-IMF codestream will be generated\n",
  6514. NL);
  6515. ret = OPJ_FALSE;
  6516. }
  6517. } else if (XTsiz >= 2048) {
  6518. if (!(NL >= 1 && NL <= 5)) {
  6519. opj_event_msg(p_manager, EVT_WARNING,
  6520. "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n"
  6521. "-> Number of decomposition levels is %d.\n"
  6522. "-> Non-IMF codestream will be generated\n",
  6523. NL);
  6524. ret = OPJ_FALSE;
  6525. }
  6526. } else if (XTsiz >= 1024) {
  6527. if (!(NL >= 1 && NL <= 4)) {
  6528. opj_event_msg(p_manager, EVT_WARNING,
  6529. "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n"
  6530. "-> Number of decomposition levels is %d.\n"
  6531. "-> Non-IMF codestream will be generated\n",
  6532. NL);
  6533. ret = OPJ_FALSE;
  6534. }
  6535. }
  6536. break;
  6537. }
  6538. default:
  6539. break;
  6540. }
  6541. if (parameters->numresolution == 1) {
  6542. if (parameters->res_spec != 1 ||
  6543. parameters->prcw_init[0] != 128 ||
  6544. parameters->prch_init[0] != 128) {
  6545. opj_event_msg(p_manager, EVT_WARNING,
  6546. "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n"
  6547. "-> Supplied values are different from that.\n"
  6548. "-> Non-IMF codestream will be generated\n");
  6549. ret = OPJ_FALSE;
  6550. }
  6551. } else {
  6552. int i;
  6553. for (i = 0; i < parameters->res_spec; i++) {
  6554. if (parameters->prcw_init[i] != 256 ||
  6555. parameters->prch_init[i] != 256) {
  6556. opj_event_msg(p_manager, EVT_WARNING,
  6557. "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n"
  6558. "-> Supplied values are different from that.\n"
  6559. "-> Non-IMF codestream will be generated\n");
  6560. ret = OPJ_FALSE;
  6561. }
  6562. }
  6563. }
  6564. return ret;
  6565. }
  6566. OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
  6567. opj_cparameters_t *parameters,
  6568. opj_image_t *image,
  6569. opj_event_mgr_t * p_manager)
  6570. {
  6571. OPJ_UINT32 i, j, tileno, numpocs_tile;
  6572. opj_cp_t *cp = 00;
  6573. OPJ_UINT32 cblkw, cblkh;
  6574. if (!p_j2k || !parameters || ! image) {
  6575. return OPJ_FALSE;
  6576. }
  6577. if ((parameters->numresolution <= 0) ||
  6578. (parameters->numresolution > OPJ_J2K_MAXRLVLS)) {
  6579. opj_event_msg(p_manager, EVT_ERROR,
  6580. "Invalid number of resolutions : %d not in range [1,%d]\n",
  6581. parameters->numresolution, OPJ_J2K_MAXRLVLS);
  6582. return OPJ_FALSE;
  6583. }
  6584. if (parameters->cblockw_init < 4 || parameters->cblockw_init > 1024) {
  6585. opj_event_msg(p_manager, EVT_ERROR,
  6586. "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n",
  6587. parameters->cblockw_init);
  6588. return OPJ_FALSE;
  6589. }
  6590. if (parameters->cblockh_init < 4 || parameters->cblockh_init > 1024) {
  6591. opj_event_msg(p_manager, EVT_ERROR,
  6592. "Invalid value for cblockh_init: %d not a power of 2 not in range [4,1024]\n",
  6593. parameters->cblockh_init);
  6594. return OPJ_FALSE;
  6595. }
  6596. if (parameters->cblockw_init * parameters->cblockh_init > 4096) {
  6597. opj_event_msg(p_manager, EVT_ERROR,
  6598. "Invalid value for cblockw_init * cblockh_init: should be <= 4096\n");
  6599. return OPJ_FALSE;
  6600. }
  6601. cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
  6602. cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
  6603. if (parameters->cblockw_init != (1 << cblkw)) {
  6604. opj_event_msg(p_manager, EVT_ERROR,
  6605. "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n",
  6606. parameters->cblockw_init);
  6607. return OPJ_FALSE;
  6608. }
  6609. if (parameters->cblockh_init != (1 << cblkh)) {
  6610. opj_event_msg(p_manager, EVT_ERROR,
  6611. "Invalid value for cblockw_init: %d not a power of 2 in range [4,1024]\n",
  6612. parameters->cblockh_init);
  6613. return OPJ_FALSE;
  6614. }
  6615. if (parameters->cp_fixed_alloc) {
  6616. if (parameters->cp_matrice == NULL) {
  6617. opj_event_msg(p_manager, EVT_ERROR,
  6618. "cp_fixed_alloc set, but cp_matrice missing\n");
  6619. return OPJ_FALSE;
  6620. }
  6621. if (parameters->tcp_numlayers > J2K_TCD_MATRIX_MAX_LAYER_COUNT) {
  6622. opj_event_msg(p_manager, EVT_ERROR,
  6623. "tcp_numlayers when cp_fixed_alloc set should not exceed %d\n",
  6624. J2K_TCD_MATRIX_MAX_LAYER_COUNT);
  6625. return OPJ_FALSE;
  6626. }
  6627. if (parameters->numresolution > J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT) {
  6628. opj_event_msg(p_manager, EVT_ERROR,
  6629. "numresolution when cp_fixed_alloc set should not exceed %d\n",
  6630. J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT);
  6631. return OPJ_FALSE;
  6632. }
  6633. }
  6634. p_j2k->m_specific_param.m_encoder.m_nb_comps = image->numcomps;
  6635. /* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
  6636. cp = &(p_j2k->m_cp);
  6637. /* set default values for cp */
  6638. cp->tw = 1;
  6639. cp->th = 1;
  6640. /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */
  6641. if (parameters->rsiz ==
  6642. OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */
  6643. OPJ_BOOL deprecated_used = OPJ_FALSE;
  6644. switch (parameters->cp_cinema) {
  6645. case OPJ_CINEMA2K_24:
  6646. parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
  6647. parameters->max_cs_size = OPJ_CINEMA_24_CS;
  6648. parameters->max_comp_size = OPJ_CINEMA_24_COMP;
  6649. deprecated_used = OPJ_TRUE;
  6650. break;
  6651. case OPJ_CINEMA2K_48:
  6652. parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
  6653. parameters->max_cs_size = OPJ_CINEMA_48_CS;
  6654. parameters->max_comp_size = OPJ_CINEMA_48_COMP;
  6655. deprecated_used = OPJ_TRUE;
  6656. break;
  6657. case OPJ_CINEMA4K_24:
  6658. parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
  6659. parameters->max_cs_size = OPJ_CINEMA_24_CS;
  6660. parameters->max_comp_size = OPJ_CINEMA_24_COMP;
  6661. deprecated_used = OPJ_TRUE;
  6662. break;
  6663. case OPJ_OFF:
  6664. default:
  6665. break;
  6666. }
  6667. switch (parameters->cp_rsiz) {
  6668. case OPJ_CINEMA2K:
  6669. parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
  6670. deprecated_used = OPJ_TRUE;
  6671. break;
  6672. case OPJ_CINEMA4K:
  6673. parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
  6674. deprecated_used = OPJ_TRUE;
  6675. break;
  6676. case OPJ_MCT:
  6677. parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT;
  6678. deprecated_used = OPJ_TRUE;
  6679. case OPJ_STD_RSIZ:
  6680. default:
  6681. break;
  6682. }
  6683. if (deprecated_used) {
  6684. opj_event_msg(p_manager, EVT_WARNING,
  6685. "Deprecated fields cp_cinema or cp_rsiz are used\n"
  6686. "Please consider using only the rsiz field\n"
  6687. "See openjpeg.h documentation for more details\n");
  6688. }
  6689. }
  6690. /* If no explicit layers are provided, use lossless settings */
  6691. if (parameters->tcp_numlayers == 0) {
  6692. parameters->tcp_numlayers = 1;
  6693. parameters->cp_disto_alloc = 1;
  6694. parameters->tcp_rates[0] = 0;
  6695. }
  6696. if (parameters->cp_disto_alloc) {
  6697. /* Emit warnings if tcp_rates are not decreasing */
  6698. for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
  6699. OPJ_FLOAT32 rate_i_corr = parameters->tcp_rates[i];
  6700. OPJ_FLOAT32 rate_i_m_1_corr = parameters->tcp_rates[i - 1];
  6701. if (rate_i_corr <= 1.0) {
  6702. rate_i_corr = 1.0;
  6703. }
  6704. if (rate_i_m_1_corr <= 1.0) {
  6705. rate_i_m_1_corr = 1.0;
  6706. }
  6707. if (rate_i_corr >= rate_i_m_1_corr) {
  6708. if (rate_i_corr != parameters->tcp_rates[i] &&
  6709. rate_i_m_1_corr != parameters->tcp_rates[i - 1]) {
  6710. opj_event_msg(p_manager, EVT_WARNING,
  6711. "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser "
  6712. "than tcp_rates[%d]=%f (corrected as %f)\n",
  6713. i, parameters->tcp_rates[i], rate_i_corr,
  6714. i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr);
  6715. } else if (rate_i_corr != parameters->tcp_rates[i]) {
  6716. opj_event_msg(p_manager, EVT_WARNING,
  6717. "tcp_rates[%d]=%f (corrected as %f) should be strictly lesser "
  6718. "than tcp_rates[%d]=%f\n",
  6719. i, parameters->tcp_rates[i], rate_i_corr,
  6720. i - 1, parameters->tcp_rates[i - 1]);
  6721. } else if (rate_i_m_1_corr != parameters->tcp_rates[i - 1]) {
  6722. opj_event_msg(p_manager, EVT_WARNING,
  6723. "tcp_rates[%d]=%f should be strictly lesser "
  6724. "than tcp_rates[%d]=%f (corrected as %f)\n",
  6725. i, parameters->tcp_rates[i],
  6726. i - 1, parameters->tcp_rates[i - 1], rate_i_m_1_corr);
  6727. } else {
  6728. opj_event_msg(p_manager, EVT_WARNING,
  6729. "tcp_rates[%d]=%f should be strictly lesser "
  6730. "than tcp_rates[%d]=%f\n",
  6731. i, parameters->tcp_rates[i],
  6732. i - 1, parameters->tcp_rates[i - 1]);
  6733. }
  6734. }
  6735. }
  6736. } else if (parameters->cp_fixed_quality) {
  6737. /* Emit warnings if tcp_distoratio are not increasing */
  6738. for (i = 1; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
  6739. if (parameters->tcp_distoratio[i] < parameters->tcp_distoratio[i - 1] &&
  6740. !(i == (OPJ_UINT32)parameters->tcp_numlayers - 1 &&
  6741. parameters->tcp_distoratio[i] == 0)) {
  6742. opj_event_msg(p_manager, EVT_WARNING,
  6743. "tcp_distoratio[%d]=%f should be strictly greater "
  6744. "than tcp_distoratio[%d]=%f\n",
  6745. i, parameters->tcp_distoratio[i], i - 1,
  6746. parameters->tcp_distoratio[i - 1]);
  6747. }
  6748. }
  6749. }
  6750. /* see if max_codestream_size does limit input rate */
  6751. if (parameters->max_cs_size <= 0) {
  6752. if (parameters->tcp_rates[parameters->tcp_numlayers - 1] > 0) {
  6753. OPJ_FLOAT32 temp_size;
  6754. temp_size = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w *
  6755. image->comps[0].h * image->comps[0].prec) /
  6756. ((double)parameters->tcp_rates[parameters->tcp_numlayers - 1] * 8 *
  6757. image->comps[0].dx * image->comps[0].dy));
  6758. if (temp_size > (OPJ_FLOAT32)INT_MAX) {
  6759. parameters->max_cs_size = INT_MAX;
  6760. } else {
  6761. parameters->max_cs_size = (int) floor(temp_size);
  6762. }
  6763. } else {
  6764. parameters->max_cs_size = 0;
  6765. }
  6766. } else {
  6767. OPJ_FLOAT32 temp_rate;
  6768. OPJ_BOOL cap = OPJ_FALSE;
  6769. if (OPJ_IS_IMF(parameters->rsiz) && parameters->max_cs_size > 0 &&
  6770. parameters->tcp_numlayers == 1 && parameters->tcp_rates[0] == 0) {
  6771. parameters->tcp_rates[0] = (OPJ_FLOAT32)(image->numcomps * image->comps[0].w *
  6772. image->comps[0].h * image->comps[0].prec) /
  6773. (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx *
  6774. image->comps[0].dy);
  6775. }
  6776. temp_rate = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w *
  6777. image->comps[0].h * image->comps[0].prec) /
  6778. (((double)parameters->max_cs_size) * 8 * image->comps[0].dx *
  6779. image->comps[0].dy));
  6780. for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
  6781. if (parameters->tcp_rates[i] < temp_rate) {
  6782. parameters->tcp_rates[i] = temp_rate;
  6783. cap = OPJ_TRUE;
  6784. }
  6785. }
  6786. if (cap) {
  6787. opj_event_msg(p_manager, EVT_WARNING,
  6788. "The desired maximum codestream size has limited\n"
  6789. "at least one of the desired quality layers\n");
  6790. }
  6791. }
  6792. if (OPJ_IS_CINEMA(parameters->rsiz) || OPJ_IS_IMF(parameters->rsiz)) {
  6793. p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE;
  6794. }
  6795. /* Manage profiles and applications and set RSIZ */
  6796. /* set cinema parameters if required */
  6797. if (OPJ_IS_CINEMA(parameters->rsiz)) {
  6798. if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K)
  6799. || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)) {
  6800. opj_event_msg(p_manager, EVT_WARNING,
  6801. "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n");
  6802. parameters->rsiz = OPJ_PROFILE_NONE;
  6803. } else {
  6804. opj_j2k_set_cinema_parameters(parameters, image, p_manager);
  6805. if (!opj_j2k_is_cinema_compliant(image, parameters->rsiz, p_manager)) {
  6806. parameters->rsiz = OPJ_PROFILE_NONE;
  6807. }
  6808. }
  6809. } else if (OPJ_IS_STORAGE(parameters->rsiz)) {
  6810. opj_event_msg(p_manager, EVT_WARNING,
  6811. "JPEG 2000 Long Term Storage profile not yet supported\n");
  6812. parameters->rsiz = OPJ_PROFILE_NONE;
  6813. } else if (OPJ_IS_BROADCAST(parameters->rsiz)) {
  6814. opj_event_msg(p_manager, EVT_WARNING,
  6815. "JPEG 2000 Broadcast profiles not yet supported\n");
  6816. parameters->rsiz = OPJ_PROFILE_NONE;
  6817. } else if (OPJ_IS_IMF(parameters->rsiz)) {
  6818. opj_j2k_set_imf_parameters(parameters, image, p_manager);
  6819. if (!opj_j2k_is_imf_compliant(parameters, image, p_manager)) {
  6820. parameters->rsiz = OPJ_PROFILE_NONE;
  6821. }
  6822. } else if (OPJ_IS_PART2(parameters->rsiz)) {
  6823. if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) {
  6824. opj_event_msg(p_manager, EVT_WARNING,
  6825. "JPEG 2000 Part-2 profile defined\n"
  6826. "but no Part-2 extension enabled.\n"
  6827. "Profile set to NONE.\n");
  6828. parameters->rsiz = OPJ_PROFILE_NONE;
  6829. } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) {
  6830. opj_event_msg(p_manager, EVT_WARNING,
  6831. "Unsupported Part-2 extension enabled\n"
  6832. "Profile set to NONE.\n");
  6833. parameters->rsiz = OPJ_PROFILE_NONE;
  6834. }
  6835. }
  6836. /*
  6837. copy user encoding parameters
  6838. */
  6839. cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)
  6840. parameters->max_comp_size;
  6841. cp->rsiz = parameters->rsiz;
  6842. if (parameters->cp_fixed_alloc) {
  6843. cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = FIXED_LAYER;
  6844. } else if (parameters->cp_fixed_quality) {
  6845. cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy =
  6846. FIXED_DISTORTION_RATIO;
  6847. } else {
  6848. cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy =
  6849. RATE_DISTORTION_RATIO;
  6850. }
  6851. if (parameters->cp_fixed_alloc) {
  6852. size_t array_size = (size_t)parameters->tcp_numlayers *
  6853. (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32);
  6854. cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
  6855. if (!cp->m_specific_param.m_enc.m_matrice) {
  6856. opj_event_msg(p_manager, EVT_ERROR,
  6857. "Not enough memory to allocate copy of user encoding parameters matrix \n");
  6858. return OPJ_FALSE;
  6859. }
  6860. memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice,
  6861. array_size);
  6862. }
  6863. /* tiles */
  6864. cp->tdx = (OPJ_UINT32)parameters->cp_tdx;
  6865. cp->tdy = (OPJ_UINT32)parameters->cp_tdy;
  6866. /* tile offset */
  6867. cp->tx0 = (OPJ_UINT32)parameters->cp_tx0;
  6868. cp->ty0 = (OPJ_UINT32)parameters->cp_ty0;
  6869. /* comment string */
  6870. if (parameters->cp_comment) {
  6871. cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U);
  6872. if (!cp->comment) {
  6873. opj_event_msg(p_manager, EVT_ERROR,
  6874. "Not enough memory to allocate copy of comment string\n");
  6875. return OPJ_FALSE;
  6876. }
  6877. strcpy(cp->comment, parameters->cp_comment);
  6878. } else {
  6879. /* Create default comment for codestream */
  6880. const char comment[] = "Created by OpenJPEG version ";
  6881. const size_t clen = strlen(comment);
  6882. const char *version = opj_version();
  6883. /* UniPG>> */
  6884. #ifdef USE_JPWL
  6885. const size_t cp_comment_buf_size = clen + strlen(version) + 11;
  6886. cp->comment = (char*)opj_malloc(cp_comment_buf_size);
  6887. if (!cp->comment) {
  6888. opj_event_msg(p_manager, EVT_ERROR,
  6889. "Not enough memory to allocate comment string\n");
  6890. return OPJ_FALSE;
  6891. }
  6892. snprintf(cp->comment, cp_comment_buf_size, "%s%s with JPWL",
  6893. comment, version);
  6894. #else
  6895. const size_t cp_comment_buf_size = clen + strlen(version) + 1;
  6896. cp->comment = (char*)opj_malloc(cp_comment_buf_size);
  6897. if (!cp->comment) {
  6898. opj_event_msg(p_manager, EVT_ERROR,
  6899. "Not enough memory to allocate comment string\n");
  6900. return OPJ_FALSE;
  6901. }
  6902. snprintf(cp->comment, cp_comment_buf_size, "%s%s", comment, version);
  6903. #endif
  6904. /* <<UniPG */
  6905. }
  6906. /*
  6907. calculate other encoding parameters
  6908. */
  6909. if (parameters->tile_size_on) {
  6910. if (cp->tdx == 0) {
  6911. opj_event_msg(p_manager, EVT_ERROR, "Invalid tile width\n");
  6912. return OPJ_FALSE;
  6913. }
  6914. if (cp->tdy == 0) {
  6915. opj_event_msg(p_manager, EVT_ERROR, "Invalid tile height\n");
  6916. return OPJ_FALSE;
  6917. }
  6918. cp->tw = opj_uint_ceildiv(image->x1 - cp->tx0, cp->tdx);
  6919. cp->th = opj_uint_ceildiv(image->y1 - cp->ty0, cp->tdy);
  6920. /* Check that the number of tiles is valid */
  6921. if (cp->tw > 65535 / cp->th) {
  6922. opj_event_msg(p_manager, EVT_ERROR,
  6923. "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
  6924. cp->tw, cp->th);
  6925. return OPJ_FALSE;
  6926. }
  6927. } else {
  6928. cp->tdx = image->x1 - cp->tx0;
  6929. cp->tdy = image->y1 - cp->ty0;
  6930. }
  6931. if (parameters->tp_on) {
  6932. cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag;
  6933. cp->m_specific_param.m_enc.m_tp_on = 1;
  6934. }
  6935. #ifdef USE_JPWL
  6936. /*
  6937. calculate JPWL encoding parameters
  6938. */
  6939. if (parameters->jpwl_epc_on) {
  6940. OPJ_INT32 i;
  6941. /* set JPWL on */
  6942. cp->epc_on = OPJ_TRUE;
  6943. cp->info_on = OPJ_FALSE; /* no informative technique */
  6944. /* set EPB on */
  6945. if ((parameters->jpwl_hprot_MH > 0) || (parameters->jpwl_hprot_TPH[0] > 0)) {
  6946. cp->epb_on = OPJ_TRUE;
  6947. cp->hprot_MH = parameters->jpwl_hprot_MH;
  6948. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  6949. cp->hprot_TPH_tileno[i] = parameters->jpwl_hprot_TPH_tileno[i];
  6950. cp->hprot_TPH[i] = parameters->jpwl_hprot_TPH[i];
  6951. }
  6952. /* if tile specs are not specified, copy MH specs */
  6953. if (cp->hprot_TPH[0] == -1) {
  6954. cp->hprot_TPH_tileno[0] = 0;
  6955. cp->hprot_TPH[0] = parameters->jpwl_hprot_MH;
  6956. }
  6957. for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
  6958. cp->pprot_tileno[i] = parameters->jpwl_pprot_tileno[i];
  6959. cp->pprot_packno[i] = parameters->jpwl_pprot_packno[i];
  6960. cp->pprot[i] = parameters->jpwl_pprot[i];
  6961. }
  6962. }
  6963. /* set ESD writing */
  6964. if ((parameters->jpwl_sens_size == 1) || (parameters->jpwl_sens_size == 2)) {
  6965. cp->esd_on = OPJ_TRUE;
  6966. cp->sens_size = parameters->jpwl_sens_size;
  6967. cp->sens_addr = parameters->jpwl_sens_addr;
  6968. cp->sens_range = parameters->jpwl_sens_range;
  6969. cp->sens_MH = parameters->jpwl_sens_MH;
  6970. for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
  6971. cp->sens_TPH_tileno[i] = parameters->jpwl_sens_TPH_tileno[i];
  6972. cp->sens_TPH[i] = parameters->jpwl_sens_TPH[i];
  6973. }
  6974. }
  6975. /* always set RED writing to false: we are at the encoder */
  6976. cp->red_on = OPJ_FALSE;
  6977. } else {
  6978. cp->epc_on = OPJ_FALSE;
  6979. }
  6980. #endif /* USE_JPWL */
  6981. /* initialize the multiple tiles */
  6982. /* ---------------------------- */
  6983. cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
  6984. if (!cp->tcps) {
  6985. opj_event_msg(p_manager, EVT_ERROR,
  6986. "Not enough memory to allocate tile coding parameters\n");
  6987. return OPJ_FALSE;
  6988. }
  6989. for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  6990. opj_tcp_t *tcp = &cp->tcps[tileno];
  6991. const OPJ_BOOL fixed_distoratio =
  6992. cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
  6993. FIXED_DISTORTION_RATIO;
  6994. tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;
  6995. for (j = 0; j < tcp->numlayers; j++) {
  6996. if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) {
  6997. if (fixed_distoratio) {
  6998. tcp->distoratio[j] = parameters->tcp_distoratio[j];
  6999. }
  7000. tcp->rates[j] = parameters->tcp_rates[j];
  7001. } else {
  7002. if (fixed_distoratio) {
  7003. tcp->distoratio[j] = parameters->tcp_distoratio[j];
  7004. } else {
  7005. tcp->rates[j] = parameters->tcp_rates[j];
  7006. }
  7007. }
  7008. if (!fixed_distoratio &&
  7009. tcp->rates[j] <= 1.0) {
  7010. tcp->rates[j] = 0.0; /* force lossless */
  7011. }
  7012. }
  7013. tcp->csty = (OPJ_UINT32)parameters->csty;
  7014. tcp->prg = parameters->prog_order;
  7015. tcp->mct = (OPJ_UINT32)parameters->tcp_mct;
  7016. numpocs_tile = 0;
  7017. tcp->POC = 0;
  7018. if (parameters->numpocs) {
  7019. /* initialisation of POC */
  7020. for (i = 0; i < parameters->numpocs; i++) {
  7021. if (tileno + 1 == parameters->POC[i].tile) {
  7022. opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
  7023. if (parameters->POC[numpocs_tile].compno0 >= image->numcomps) {
  7024. opj_event_msg(p_manager, EVT_ERROR,
  7025. "Invalid compno0 for POC %d\n", i);
  7026. return OPJ_FALSE;
  7027. }
  7028. tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
  7029. tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
  7030. tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
  7031. tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
  7032. tcp_poc->compno1 = opj_uint_min(parameters->POC[numpocs_tile].compno1,
  7033. image->numcomps);
  7034. tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
  7035. tcp_poc->tile = parameters->POC[numpocs_tile].tile;
  7036. numpocs_tile++;
  7037. }
  7038. }
  7039. if (numpocs_tile) {
  7040. /* TODO MSD use the return value*/
  7041. opj_j2k_check_poc_val(parameters->POC, tileno, parameters->numpocs,
  7042. (OPJ_UINT32)parameters->numresolution, image->numcomps,
  7043. (OPJ_UINT32)parameters->tcp_numlayers, p_manager);
  7044. tcp->POC = 1;
  7045. tcp->numpocs = numpocs_tile - 1 ;
  7046. }
  7047. } else {
  7048. tcp->numpocs = 0;
  7049. }
  7050. tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
  7051. if (!tcp->tccps) {
  7052. opj_event_msg(p_manager, EVT_ERROR,
  7053. "Not enough memory to allocate tile component coding parameters\n");
  7054. return OPJ_FALSE;
  7055. }
  7056. if (parameters->mct_data) {
  7057. OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(
  7058. OPJ_FLOAT32);
  7059. OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
  7060. OPJ_INT32 * l_dc_shift = (OPJ_INT32 *)((OPJ_BYTE *) parameters->mct_data +
  7061. lMctSize);
  7062. if (!lTmpBuf) {
  7063. opj_event_msg(p_manager, EVT_ERROR,
  7064. "Not enough memory to allocate temp buffer\n");
  7065. return OPJ_FALSE;
  7066. }
  7067. tcp->mct = 2;
  7068. tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
  7069. if (! tcp->m_mct_coding_matrix) {
  7070. opj_free(lTmpBuf);
  7071. lTmpBuf = NULL;
  7072. opj_event_msg(p_manager, EVT_ERROR,
  7073. "Not enough memory to allocate encoder MCT coding matrix \n");
  7074. return OPJ_FALSE;
  7075. }
  7076. memcpy(tcp->m_mct_coding_matrix, parameters->mct_data, lMctSize);
  7077. memcpy(lTmpBuf, parameters->mct_data, lMctSize);
  7078. tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
  7079. if (! tcp->m_mct_decoding_matrix) {
  7080. opj_free(lTmpBuf);
  7081. lTmpBuf = NULL;
  7082. opj_event_msg(p_manager, EVT_ERROR,
  7083. "Not enough memory to allocate encoder MCT decoding matrix \n");
  7084. return OPJ_FALSE;
  7085. }
  7086. if (opj_matrix_inversion_f(lTmpBuf, (tcp->m_mct_decoding_matrix),
  7087. image->numcomps) == OPJ_FALSE) {
  7088. opj_free(lTmpBuf);
  7089. lTmpBuf = NULL;
  7090. opj_event_msg(p_manager, EVT_ERROR,
  7091. "Failed to inverse encoder MCT decoding matrix \n");
  7092. return OPJ_FALSE;
  7093. }
  7094. tcp->mct_norms = (OPJ_FLOAT64*)
  7095. opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
  7096. if (! tcp->mct_norms) {
  7097. opj_free(lTmpBuf);
  7098. lTmpBuf = NULL;
  7099. opj_event_msg(p_manager, EVT_ERROR,
  7100. "Not enough memory to allocate encoder MCT norms \n");
  7101. return OPJ_FALSE;
  7102. }
  7103. opj_calculate_norms(tcp->mct_norms, image->numcomps,
  7104. tcp->m_mct_decoding_matrix);
  7105. opj_free(lTmpBuf);
  7106. for (i = 0; i < image->numcomps; i++) {
  7107. opj_tccp_t *tccp = &tcp->tccps[i];
  7108. tccp->m_dc_level_shift = l_dc_shift[i];
  7109. }
  7110. if (opj_j2k_setup_mct_encoding(tcp, image) == OPJ_FALSE) {
  7111. /* free will be handled by opj_j2k_destroy */
  7112. opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n");
  7113. return OPJ_FALSE;
  7114. }
  7115. } else {
  7116. if (tcp->mct == 1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */
  7117. if ((image->comps[0].dx != image->comps[1].dx) ||
  7118. (image->comps[0].dx != image->comps[2].dx) ||
  7119. (image->comps[0].dy != image->comps[1].dy) ||
  7120. (image->comps[0].dy != image->comps[2].dy)) {
  7121. opj_event_msg(p_manager, EVT_WARNING,
  7122. "Cannot perform MCT on components with different sizes. Disabling MCT.\n");
  7123. tcp->mct = 0;
  7124. }
  7125. }
  7126. for (i = 0; i < image->numcomps; i++) {
  7127. opj_tccp_t *tccp = &tcp->tccps[i];
  7128. opj_image_comp_t * l_comp = &(image->comps[i]);
  7129. if (! l_comp->sgnd) {
  7130. tccp->m_dc_level_shift = 1 << (l_comp->prec - 1);
  7131. }
  7132. }
  7133. }
  7134. for (i = 0; i < image->numcomps; i++) {
  7135. opj_tccp_t *tccp = &tcp->tccps[i];
  7136. tccp->csty = parameters->csty &
  7137. 0x01; /* 0 => one precinct || 1 => custom precinct */
  7138. tccp->numresolutions = (OPJ_UINT32)parameters->numresolution;
  7139. tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
  7140. tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
  7141. tccp->cblksty = (OPJ_UINT32)parameters->mode;
  7142. tccp->qmfbid = parameters->irreversible ? 0 : 1;
  7143. tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT :
  7144. J2K_CCP_QNTSTY_NOQNT;
  7145. tccp->numgbits = 2;
  7146. if ((OPJ_INT32)i == parameters->roi_compno) {
  7147. tccp->roishift = parameters->roi_shift;
  7148. } else {
  7149. tccp->roishift = 0;
  7150. }
  7151. if (parameters->csty & J2K_CCP_CSTY_PRT) {
  7152. OPJ_INT32 p = 0, it_res;
  7153. assert(tccp->numresolutions > 0);
  7154. for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) {
  7155. if (p < parameters->res_spec) {
  7156. if (parameters->prcw_init[p] < 1) {
  7157. tccp->prcw[it_res] = 1;
  7158. } else {
  7159. tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]);
  7160. }
  7161. if (parameters->prch_init[p] < 1) {
  7162. tccp->prch[it_res] = 1;
  7163. } else {
  7164. tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]);
  7165. }
  7166. } else {
  7167. OPJ_INT32 res_spec = parameters->res_spec;
  7168. OPJ_INT32 size_prcw = 0;
  7169. OPJ_INT32 size_prch = 0;
  7170. assert(res_spec > 0); /* issue 189 */
  7171. size_prcw = parameters->prcw_init[res_spec - 1] >> (p - (res_spec - 1));
  7172. size_prch = parameters->prch_init[res_spec - 1] >> (p - (res_spec - 1));
  7173. if (size_prcw < 1) {
  7174. tccp->prcw[it_res] = 1;
  7175. } else {
  7176. tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw);
  7177. }
  7178. if (size_prch < 1) {
  7179. tccp->prch[it_res] = 1;
  7180. } else {
  7181. tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch);
  7182. }
  7183. }
  7184. p++;
  7185. /*printf("\nsize precinct for level %d : %d,%d\n", it_res,tccp->prcw[it_res], tccp->prch[it_res]); */
  7186. } /*end for*/
  7187. } else {
  7188. for (j = 0; j < tccp->numresolutions; j++) {
  7189. tccp->prcw[j] = 15;
  7190. tccp->prch[j] = 15;
  7191. }
  7192. }
  7193. opj_dwt_calc_explicit_stepsizes(tccp, image->comps[i].prec);
  7194. }
  7195. }
  7196. if (parameters->mct_data) {
  7197. opj_free(parameters->mct_data);
  7198. parameters->mct_data = 00;
  7199. }
  7200. return OPJ_TRUE;
  7201. }
  7202. static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index,
  7203. OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
  7204. {
  7205. assert(cstr_index != 00);
  7206. /* expand the list? */
  7207. if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
  7208. opj_marker_info_t *new_marker;
  7209. cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32)
  7210. cstr_index->maxmarknum);
  7211. new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker,
  7212. cstr_index->maxmarknum * sizeof(opj_marker_info_t));
  7213. if (! new_marker) {
  7214. opj_free(cstr_index->marker);
  7215. cstr_index->marker = NULL;
  7216. cstr_index->maxmarknum = 0;
  7217. cstr_index->marknum = 0;
  7218. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n"); */
  7219. return OPJ_FALSE;
  7220. }
  7221. cstr_index->marker = new_marker;
  7222. }
  7223. /* add the marker */
  7224. cstr_index->marker[cstr_index->marknum].type = (OPJ_UINT16)type;
  7225. cstr_index->marker[cstr_index->marknum].pos = (OPJ_INT32)pos;
  7226. cstr_index->marker[cstr_index->marknum].len = (OPJ_INT32)len;
  7227. cstr_index->marknum++;
  7228. return OPJ_TRUE;
  7229. }
  7230. static OPJ_BOOL opj_j2k_add_tlmarker(OPJ_UINT32 tileno,
  7231. opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos,
  7232. OPJ_UINT32 len)
  7233. {
  7234. assert(cstr_index != 00);
  7235. assert(cstr_index->tile_index != 00);
  7236. /* expand the list? */
  7237. if ((cstr_index->tile_index[tileno].marknum + 1) >
  7238. cstr_index->tile_index[tileno].maxmarknum) {
  7239. opj_marker_info_t *new_marker;
  7240. cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 +
  7241. (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum);
  7242. new_marker = (opj_marker_info_t *) opj_realloc(
  7243. cstr_index->tile_index[tileno].marker,
  7244. cstr_index->tile_index[tileno].maxmarknum * sizeof(opj_marker_info_t));
  7245. if (! new_marker) {
  7246. opj_free(cstr_index->tile_index[tileno].marker);
  7247. cstr_index->tile_index[tileno].marker = NULL;
  7248. cstr_index->tile_index[tileno].maxmarknum = 0;
  7249. cstr_index->tile_index[tileno].marknum = 0;
  7250. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n"); */
  7251. return OPJ_FALSE;
  7252. }
  7253. cstr_index->tile_index[tileno].marker = new_marker;
  7254. }
  7255. /* add the marker */
  7256. cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].type
  7257. = (OPJ_UINT16)type;
  7258. cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].pos
  7259. = (OPJ_INT32)pos;
  7260. cstr_index->tile_index[tileno].marker[cstr_index->tile_index[tileno].marknum].len
  7261. = (OPJ_INT32)len;
  7262. cstr_index->tile_index[tileno].marknum++;
  7263. if (type == J2K_MS_SOT) {
  7264. OPJ_UINT32 l_current_tile_part = cstr_index->tile_index[tileno].current_tpsno;
  7265. if (cstr_index->tile_index[tileno].tp_index) {
  7266. cstr_index->tile_index[tileno].tp_index[l_current_tile_part].start_pos = pos;
  7267. }
  7268. }
  7269. return OPJ_TRUE;
  7270. }
  7271. /*
  7272. * -----------------------------------------------------------------------
  7273. * -----------------------------------------------------------------------
  7274. * -----------------------------------------------------------------------
  7275. */
  7276. OPJ_BOOL opj_j2k_end_decompress(opj_j2k_t *p_j2k,
  7277. opj_stream_private_t *p_stream,
  7278. opj_event_mgr_t * p_manager
  7279. )
  7280. {
  7281. (void)p_j2k;
  7282. (void)p_stream;
  7283. (void)p_manager;
  7284. return OPJ_TRUE;
  7285. }
  7286. OPJ_BOOL opj_j2k_read_header(opj_stream_private_t *p_stream,
  7287. opj_j2k_t* p_j2k,
  7288. opj_image_t** p_image,
  7289. opj_event_mgr_t* p_manager)
  7290. {
  7291. /* preconditions */
  7292. assert(p_j2k != 00);
  7293. assert(p_stream != 00);
  7294. assert(p_manager != 00);
  7295. /* create an empty image header */
  7296. p_j2k->m_private_image = opj_image_create0();
  7297. if (! p_j2k->m_private_image) {
  7298. return OPJ_FALSE;
  7299. }
  7300. /* customization of the validation */
  7301. if (! opj_j2k_setup_decoding_validation(p_j2k, p_manager)) {
  7302. opj_image_destroy(p_j2k->m_private_image);
  7303. p_j2k->m_private_image = NULL;
  7304. return OPJ_FALSE;
  7305. }
  7306. /* validation of the parameters codec */
  7307. if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) {
  7308. opj_image_destroy(p_j2k->m_private_image);
  7309. p_j2k->m_private_image = NULL;
  7310. return OPJ_FALSE;
  7311. }
  7312. /* customization of the encoding */
  7313. if (! opj_j2k_setup_header_reading(p_j2k, p_manager)) {
  7314. opj_image_destroy(p_j2k->m_private_image);
  7315. p_j2k->m_private_image = NULL;
  7316. return OPJ_FALSE;
  7317. }
  7318. /* read header */
  7319. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  7320. opj_image_destroy(p_j2k->m_private_image);
  7321. p_j2k->m_private_image = NULL;
  7322. return OPJ_FALSE;
  7323. }
  7324. *p_image = opj_image_create0();
  7325. if (!(*p_image)) {
  7326. return OPJ_FALSE;
  7327. }
  7328. /* Copy codestream image information to the output image */
  7329. opj_copy_image_header(p_j2k->m_private_image, *p_image);
  7330. /*Allocate and initialize some elements of codestrem index*/
  7331. if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) {
  7332. opj_image_destroy(*p_image);
  7333. *p_image = NULL;
  7334. return OPJ_FALSE;
  7335. }
  7336. return OPJ_TRUE;
  7337. }
  7338. static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k,
  7339. opj_event_mgr_t * p_manager)
  7340. {
  7341. /* preconditions*/
  7342. assert(p_j2k != 00);
  7343. assert(p_manager != 00);
  7344. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  7345. (opj_procedure)opj_j2k_read_header_procedure, p_manager)) {
  7346. return OPJ_FALSE;
  7347. }
  7348. /* DEVELOPER CORNER, add your custom procedures */
  7349. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  7350. (opj_procedure)opj_j2k_copy_default_tcp_and_create_tcd, p_manager)) {
  7351. return OPJ_FALSE;
  7352. }
  7353. return OPJ_TRUE;
  7354. }
  7355. static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k,
  7356. opj_event_mgr_t * p_manager)
  7357. {
  7358. /* preconditions*/
  7359. assert(p_j2k != 00);
  7360. assert(p_manager != 00);
  7361. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  7362. (opj_procedure)opj_j2k_build_decoder, p_manager)) {
  7363. return OPJ_FALSE;
  7364. }
  7365. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  7366. (opj_procedure)opj_j2k_decoding_validation, p_manager)) {
  7367. return OPJ_FALSE;
  7368. }
  7369. /* DEVELOPER CORNER, add your custom validation procedure */
  7370. return OPJ_TRUE;
  7371. }
  7372. static OPJ_BOOL opj_j2k_mct_validation(opj_j2k_t * p_j2k,
  7373. opj_stream_private_t *p_stream,
  7374. opj_event_mgr_t * p_manager)
  7375. {
  7376. OPJ_BOOL l_is_valid = OPJ_TRUE;
  7377. OPJ_UINT32 i, j;
  7378. /* preconditions */
  7379. assert(p_j2k != 00);
  7380. assert(p_stream != 00);
  7381. assert(p_manager != 00);
  7382. OPJ_UNUSED(p_stream);
  7383. OPJ_UNUSED(p_manager);
  7384. if ((p_j2k->m_cp.rsiz & 0x8200) == 0x8200) {
  7385. OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  7386. opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
  7387. for (i = 0; i < l_nb_tiles; ++i) {
  7388. if (l_tcp->mct == 2) {
  7389. opj_tccp_t * l_tccp = l_tcp->tccps;
  7390. l_is_valid &= (l_tcp->m_mct_coding_matrix != 00);
  7391. for (j = 0; j < p_j2k->m_private_image->numcomps; ++j) {
  7392. l_is_valid &= !(l_tccp->qmfbid & 1);
  7393. ++l_tccp;
  7394. }
  7395. }
  7396. ++l_tcp;
  7397. }
  7398. }
  7399. return l_is_valid;
  7400. }
  7401. OPJ_BOOL opj_j2k_setup_mct_encoding(opj_tcp_t * p_tcp, opj_image_t * p_image)
  7402. {
  7403. OPJ_UINT32 i;
  7404. OPJ_UINT32 l_indix = 1;
  7405. opj_mct_data_t * l_mct_deco_data = 00, * l_mct_offset_data = 00;
  7406. opj_simple_mcc_decorrelation_data_t * l_mcc_data;
  7407. OPJ_UINT32 l_mct_size, l_nb_elem;
  7408. OPJ_FLOAT32 * l_data, * l_current_data;
  7409. opj_tccp_t * l_tccp;
  7410. /* preconditions */
  7411. assert(p_tcp != 00);
  7412. if (p_tcp->mct != 2) {
  7413. return OPJ_TRUE;
  7414. }
  7415. if (p_tcp->m_mct_decoding_matrix) {
  7416. if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
  7417. opj_mct_data_t *new_mct_records;
  7418. p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  7419. new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records,
  7420. p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
  7421. if (! new_mct_records) {
  7422. opj_free(p_tcp->m_mct_records);
  7423. p_tcp->m_mct_records = NULL;
  7424. p_tcp->m_nb_max_mct_records = 0;
  7425. p_tcp->m_nb_mct_records = 0;
  7426. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
  7427. return OPJ_FALSE;
  7428. }
  7429. p_tcp->m_mct_records = new_mct_records;
  7430. l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
  7431. memset(l_mct_deco_data, 0,
  7432. (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(
  7433. opj_mct_data_t));
  7434. }
  7435. l_mct_deco_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
  7436. if (l_mct_deco_data->m_data) {
  7437. opj_free(l_mct_deco_data->m_data);
  7438. l_mct_deco_data->m_data = 00;
  7439. }
  7440. l_mct_deco_data->m_index = l_indix++;
  7441. l_mct_deco_data->m_array_type = MCT_TYPE_DECORRELATION;
  7442. l_mct_deco_data->m_element_type = MCT_TYPE_FLOAT;
  7443. l_nb_elem = p_image->numcomps * p_image->numcomps;
  7444. l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_deco_data->m_element_type];
  7445. l_mct_deco_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size);
  7446. if (! l_mct_deco_data->m_data) {
  7447. return OPJ_FALSE;
  7448. }
  7449. j2k_mct_write_functions_from_float[l_mct_deco_data->m_element_type](
  7450. p_tcp->m_mct_decoding_matrix, l_mct_deco_data->m_data, l_nb_elem);
  7451. l_mct_deco_data->m_data_size = l_mct_size;
  7452. ++p_tcp->m_nb_mct_records;
  7453. }
  7454. if (p_tcp->m_nb_mct_records == p_tcp->m_nb_max_mct_records) {
  7455. opj_mct_data_t *new_mct_records;
  7456. p_tcp->m_nb_max_mct_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  7457. new_mct_records = (opj_mct_data_t *) opj_realloc(p_tcp->m_mct_records,
  7458. p_tcp->m_nb_max_mct_records * sizeof(opj_mct_data_t));
  7459. if (! new_mct_records) {
  7460. opj_free(p_tcp->m_mct_records);
  7461. p_tcp->m_mct_records = NULL;
  7462. p_tcp->m_nb_max_mct_records = 0;
  7463. p_tcp->m_nb_mct_records = 0;
  7464. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
  7465. return OPJ_FALSE;
  7466. }
  7467. p_tcp->m_mct_records = new_mct_records;
  7468. l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
  7469. memset(l_mct_offset_data, 0,
  7470. (p_tcp->m_nb_max_mct_records - p_tcp->m_nb_mct_records) * sizeof(
  7471. opj_mct_data_t));
  7472. if (l_mct_deco_data) {
  7473. l_mct_deco_data = l_mct_offset_data - 1;
  7474. }
  7475. }
  7476. l_mct_offset_data = p_tcp->m_mct_records + p_tcp->m_nb_mct_records;
  7477. if (l_mct_offset_data->m_data) {
  7478. opj_free(l_mct_offset_data->m_data);
  7479. l_mct_offset_data->m_data = 00;
  7480. }
  7481. l_mct_offset_data->m_index = l_indix++;
  7482. l_mct_offset_data->m_array_type = MCT_TYPE_OFFSET;
  7483. l_mct_offset_data->m_element_type = MCT_TYPE_FLOAT;
  7484. l_nb_elem = p_image->numcomps;
  7485. l_mct_size = l_nb_elem * MCT_ELEMENT_SIZE[l_mct_offset_data->m_element_type];
  7486. l_mct_offset_data->m_data = (OPJ_BYTE*)opj_malloc(l_mct_size);
  7487. if (! l_mct_offset_data->m_data) {
  7488. return OPJ_FALSE;
  7489. }
  7490. l_data = (OPJ_FLOAT32*)opj_malloc(l_nb_elem * sizeof(OPJ_FLOAT32));
  7491. if (! l_data) {
  7492. opj_free(l_mct_offset_data->m_data);
  7493. l_mct_offset_data->m_data = 00;
  7494. return OPJ_FALSE;
  7495. }
  7496. l_tccp = p_tcp->tccps;
  7497. l_current_data = l_data;
  7498. for (i = 0; i < l_nb_elem; ++i) {
  7499. *(l_current_data++) = (OPJ_FLOAT32)(l_tccp->m_dc_level_shift);
  7500. ++l_tccp;
  7501. }
  7502. j2k_mct_write_functions_from_float[l_mct_offset_data->m_element_type](l_data,
  7503. l_mct_offset_data->m_data, l_nb_elem);
  7504. opj_free(l_data);
  7505. l_mct_offset_data->m_data_size = l_mct_size;
  7506. ++p_tcp->m_nb_mct_records;
  7507. if (p_tcp->m_nb_mcc_records == p_tcp->m_nb_max_mcc_records) {
  7508. opj_simple_mcc_decorrelation_data_t *new_mcc_records;
  7509. p_tcp->m_nb_max_mcc_records += OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
  7510. new_mcc_records = (opj_simple_mcc_decorrelation_data_t *) opj_realloc(
  7511. p_tcp->m_mcc_records, p_tcp->m_nb_max_mcc_records * sizeof(
  7512. opj_simple_mcc_decorrelation_data_t));
  7513. if (! new_mcc_records) {
  7514. opj_free(p_tcp->m_mcc_records);
  7515. p_tcp->m_mcc_records = NULL;
  7516. p_tcp->m_nb_max_mcc_records = 0;
  7517. p_tcp->m_nb_mcc_records = 0;
  7518. /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to setup mct encoding\n"); */
  7519. return OPJ_FALSE;
  7520. }
  7521. p_tcp->m_mcc_records = new_mcc_records;
  7522. l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
  7523. memset(l_mcc_data, 0, (p_tcp->m_nb_max_mcc_records - p_tcp->m_nb_mcc_records) *
  7524. sizeof(opj_simple_mcc_decorrelation_data_t));
  7525. }
  7526. l_mcc_data = p_tcp->m_mcc_records + p_tcp->m_nb_mcc_records;
  7527. l_mcc_data->m_decorrelation_array = l_mct_deco_data;
  7528. l_mcc_data->m_is_irreversible = 1;
  7529. l_mcc_data->m_nb_comps = p_image->numcomps;
  7530. l_mcc_data->m_index = l_indix++;
  7531. l_mcc_data->m_offset_array = l_mct_offset_data;
  7532. ++p_tcp->m_nb_mcc_records;
  7533. return OPJ_TRUE;
  7534. }
  7535. static OPJ_BOOL opj_j2k_build_decoder(opj_j2k_t * p_j2k,
  7536. opj_stream_private_t *p_stream,
  7537. opj_event_mgr_t * p_manager)
  7538. {
  7539. /* add here initialization of cp
  7540. copy paste of setup_decoder */
  7541. (void)p_j2k;
  7542. (void)p_stream;
  7543. (void)p_manager;
  7544. return OPJ_TRUE;
  7545. }
  7546. static OPJ_BOOL opj_j2k_build_encoder(opj_j2k_t * p_j2k,
  7547. opj_stream_private_t *p_stream,
  7548. opj_event_mgr_t * p_manager)
  7549. {
  7550. /* add here initialization of cp
  7551. copy paste of setup_encoder */
  7552. (void)p_j2k;
  7553. (void)p_stream;
  7554. (void)p_manager;
  7555. return OPJ_TRUE;
  7556. }
  7557. static OPJ_BOOL opj_j2k_encoding_validation(opj_j2k_t * p_j2k,
  7558. opj_stream_private_t *p_stream,
  7559. opj_event_mgr_t * p_manager)
  7560. {
  7561. OPJ_BOOL l_is_valid = OPJ_TRUE;
  7562. /* preconditions */
  7563. assert(p_j2k != 00);
  7564. assert(p_stream != 00);
  7565. assert(p_manager != 00);
  7566. OPJ_UNUSED(p_stream);
  7567. /* STATE checking */
  7568. /* make sure the state is at 0 */
  7569. l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NONE);
  7570. /* POINTER validation */
  7571. /* make sure a p_j2k codec is present */
  7572. l_is_valid &= (p_j2k->m_procedure_list != 00);
  7573. /* make sure a validation list is present */
  7574. l_is_valid &= (p_j2k->m_validation_list != 00);
  7575. /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */
  7576. /* 33 (32) would always fail the check below (if a cast to 64bits was done) */
  7577. /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */
  7578. if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) ||
  7579. (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) {
  7580. opj_event_msg(p_manager, EVT_ERROR,
  7581. "Number of resolutions is too high in comparison to the size of tiles\n");
  7582. return OPJ_FALSE;
  7583. }
  7584. if ((p_j2k->m_cp.tdx) < (OPJ_UINT32)(1 <<
  7585. (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
  7586. opj_event_msg(p_manager, EVT_ERROR,
  7587. "Number of resolutions is too high in comparison to the size of tiles\n");
  7588. return OPJ_FALSE;
  7589. }
  7590. if ((p_j2k->m_cp.tdy) < (OPJ_UINT32)(1 <<
  7591. (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
  7592. opj_event_msg(p_manager, EVT_ERROR,
  7593. "Number of resolutions is too high in comparison to the size of tiles\n");
  7594. return OPJ_FALSE;
  7595. }
  7596. /* PARAMETER VALIDATION */
  7597. return l_is_valid;
  7598. }
  7599. static OPJ_BOOL opj_j2k_decoding_validation(opj_j2k_t *p_j2k,
  7600. opj_stream_private_t *p_stream,
  7601. opj_event_mgr_t * p_manager
  7602. )
  7603. {
  7604. OPJ_BOOL l_is_valid = OPJ_TRUE;
  7605. /* preconditions*/
  7606. assert(p_j2k != 00);
  7607. assert(p_stream != 00);
  7608. assert(p_manager != 00);
  7609. OPJ_UNUSED(p_stream);
  7610. OPJ_UNUSED(p_manager);
  7611. /* STATE checking */
  7612. /* make sure the state is at 0 */
  7613. #ifdef TODO_MSD
  7614. l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == J2K_DEC_STATE_NONE);
  7615. #endif
  7616. l_is_valid &= (p_j2k->m_specific_param.m_decoder.m_state == 0x0000);
  7617. /* POINTER validation */
  7618. /* make sure a p_j2k codec is present */
  7619. /* make sure a procedure list is present */
  7620. l_is_valid &= (p_j2k->m_procedure_list != 00);
  7621. /* make sure a validation list is present */
  7622. l_is_valid &= (p_j2k->m_validation_list != 00);
  7623. /* PARAMETER VALIDATION */
  7624. return l_is_valid;
  7625. }
  7626. static OPJ_BOOL opj_j2k_read_header_procedure(opj_j2k_t *p_j2k,
  7627. opj_stream_private_t *p_stream,
  7628. opj_event_mgr_t * p_manager)
  7629. {
  7630. OPJ_UINT32 l_current_marker;
  7631. OPJ_UINT32 l_marker_size;
  7632. const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
  7633. OPJ_BOOL l_has_siz = 0;
  7634. OPJ_BOOL l_has_cod = 0;
  7635. OPJ_BOOL l_has_qcd = 0;
  7636. /* preconditions */
  7637. assert(p_stream != 00);
  7638. assert(p_j2k != 00);
  7639. assert(p_manager != 00);
  7640. /* We enter in the main header */
  7641. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_MHSOC;
  7642. /* Try to read the SOC marker, the codestream must begin with SOC marker */
  7643. if (! opj_j2k_read_soc(p_j2k, p_stream, p_manager)) {
  7644. opj_event_msg(p_manager, EVT_ERROR, "Expected a SOC marker \n");
  7645. return OPJ_FALSE;
  7646. }
  7647. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
  7648. if (opj_stream_read_data(p_stream,
  7649. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  7650. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  7651. return OPJ_FALSE;
  7652. }
  7653. /* Read 2 bytes as the new marker ID */
  7654. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  7655. &l_current_marker, 2);
  7656. /* Try to read until the SOT is detected */
  7657. while (l_current_marker != J2K_MS_SOT) {
  7658. /* Check if the current marker ID is valid */
  7659. if (l_current_marker < 0xff00) {
  7660. opj_event_msg(p_manager, EVT_ERROR,
  7661. "A marker ID was expected (0xff--) instead of %.8x\n", l_current_marker);
  7662. return OPJ_FALSE;
  7663. }
  7664. /* Get the marker handler from the marker ID */
  7665. l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
  7666. /* Manage case where marker is unknown */
  7667. if (l_marker_handler->id == J2K_MS_UNK) {
  7668. if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)) {
  7669. opj_event_msg(p_manager, EVT_ERROR,
  7670. "Unknown marker has been detected and generated error.\n");
  7671. return OPJ_FALSE;
  7672. }
  7673. if (l_current_marker == J2K_MS_SOT) {
  7674. break; /* SOT marker is detected main header is completely read */
  7675. } else { /* Get the marker handler from the marker ID */
  7676. l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
  7677. }
  7678. }
  7679. if (l_marker_handler->id == J2K_MS_SIZ) {
  7680. /* Mark required SIZ marker as found */
  7681. l_has_siz = 1;
  7682. }
  7683. if (l_marker_handler->id == J2K_MS_COD) {
  7684. /* Mark required COD marker as found */
  7685. l_has_cod = 1;
  7686. }
  7687. if (l_marker_handler->id == J2K_MS_QCD) {
  7688. /* Mark required QCD marker as found */
  7689. l_has_qcd = 1;
  7690. }
  7691. /* Check if the marker is known and if it is the right place to find it */
  7692. if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
  7693. opj_event_msg(p_manager, EVT_ERROR,
  7694. "Marker is not compliant with its position\n");
  7695. return OPJ_FALSE;
  7696. }
  7697. /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
  7698. if (opj_stream_read_data(p_stream,
  7699. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  7700. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  7701. return OPJ_FALSE;
  7702. }
  7703. /* read 2 bytes as the marker size */
  7704. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size,
  7705. 2);
  7706. if (l_marker_size < 2) {
  7707. opj_event_msg(p_manager, EVT_ERROR, "Invalid marker size\n");
  7708. return OPJ_FALSE;
  7709. }
  7710. l_marker_size -= 2; /* Subtract the size of the marker ID already read */
  7711. /* Check if the marker size is compatible with the header data size */
  7712. if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
  7713. OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(
  7714. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
  7715. if (! new_header_data) {
  7716. opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
  7717. p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
  7718. p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
  7719. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
  7720. return OPJ_FALSE;
  7721. }
  7722. p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
  7723. p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
  7724. }
  7725. /* Try to read the rest of the marker segment from stream and copy them into the buffer */
  7726. if (opj_stream_read_data(p_stream,
  7727. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size,
  7728. p_manager) != l_marker_size) {
  7729. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  7730. return OPJ_FALSE;
  7731. }
  7732. /* Read the marker segment with the correct marker handler */
  7733. if (!(*(l_marker_handler->handler))(p_j2k,
  7734. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) {
  7735. opj_event_msg(p_manager, EVT_ERROR,
  7736. "Marker handler function failed to read the marker segment\n");
  7737. return OPJ_FALSE;
  7738. }
  7739. /* Add the marker to the codestream index*/
  7740. if (OPJ_FALSE == opj_j2k_add_mhmarker(
  7741. p_j2k->cstr_index,
  7742. l_marker_handler->id,
  7743. (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
  7744. l_marker_size + 4)) {
  7745. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add mh marker\n");
  7746. return OPJ_FALSE;
  7747. }
  7748. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
  7749. if (opj_stream_read_data(p_stream,
  7750. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  7751. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  7752. return OPJ_FALSE;
  7753. }
  7754. /* read 2 bytes as the new marker ID */
  7755. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  7756. &l_current_marker, 2);
  7757. }
  7758. if (l_has_siz == 0) {
  7759. opj_event_msg(p_manager, EVT_ERROR,
  7760. "required SIZ marker not found in main header\n");
  7761. return OPJ_FALSE;
  7762. }
  7763. if (l_has_cod == 0) {
  7764. opj_event_msg(p_manager, EVT_ERROR,
  7765. "required COD marker not found in main header\n");
  7766. return OPJ_FALSE;
  7767. }
  7768. if (l_has_qcd == 0) {
  7769. opj_event_msg(p_manager, EVT_ERROR,
  7770. "required QCD marker not found in main header\n");
  7771. return OPJ_FALSE;
  7772. }
  7773. if (! opj_j2k_merge_ppm(&(p_j2k->m_cp), p_manager)) {
  7774. opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPM data\n");
  7775. return OPJ_FALSE;
  7776. }
  7777. opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
  7778. /* Position of the last element if the main header */
  7779. p_j2k->cstr_index->main_head_end = (OPJ_UINT32) opj_stream_tell(p_stream) - 2;
  7780. /* Next step: read a tile-part header */
  7781. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
  7782. return OPJ_TRUE;
  7783. }
  7784. static OPJ_BOOL opj_j2k_exec(opj_j2k_t * p_j2k,
  7785. opj_procedure_list_t * p_procedure_list,
  7786. opj_stream_private_t *p_stream,
  7787. opj_event_mgr_t * p_manager)
  7788. {
  7789. OPJ_BOOL(** l_procedure)(opj_j2k_t *, opj_stream_private_t *,
  7790. opj_event_mgr_t *) = 00;
  7791. OPJ_BOOL l_result = OPJ_TRUE;
  7792. OPJ_UINT32 l_nb_proc, i;
  7793. /* preconditions*/
  7794. assert(p_procedure_list != 00);
  7795. assert(p_j2k != 00);
  7796. assert(p_stream != 00);
  7797. assert(p_manager != 00);
  7798. l_nb_proc = opj_procedure_list_get_nb_procedures(p_procedure_list);
  7799. l_procedure = (OPJ_BOOL(**)(opj_j2k_t *, opj_stream_private_t *,
  7800. opj_event_mgr_t *)) opj_procedure_list_get_first_procedure(p_procedure_list);
  7801. for (i = 0; i < l_nb_proc; ++i) {
  7802. l_result = l_result && ((*l_procedure)(p_j2k, p_stream, p_manager));
  7803. ++l_procedure;
  7804. }
  7805. /* and clear the procedure list at the end.*/
  7806. opj_procedure_list_clear(p_procedure_list);
  7807. return l_result;
  7808. }
  7809. /* FIXME DOC*/
  7810. static OPJ_BOOL opj_j2k_copy_default_tcp_and_create_tcd(opj_j2k_t * p_j2k,
  7811. opj_stream_private_t *p_stream,
  7812. opj_event_mgr_t * p_manager
  7813. )
  7814. {
  7815. opj_tcp_t * l_tcp = 00;
  7816. opj_tcp_t * l_default_tcp = 00;
  7817. OPJ_UINT32 l_nb_tiles;
  7818. OPJ_UINT32 i, j;
  7819. opj_tccp_t *l_current_tccp = 00;
  7820. OPJ_UINT32 l_tccp_size;
  7821. OPJ_UINT32 l_mct_size;
  7822. opj_image_t * l_image;
  7823. OPJ_UINT32 l_mcc_records_size, l_mct_records_size;
  7824. opj_mct_data_t * l_src_mct_rec, *l_dest_mct_rec;
  7825. opj_simple_mcc_decorrelation_data_t * l_src_mcc_rec, *l_dest_mcc_rec;
  7826. OPJ_UINT32 l_offset;
  7827. /* preconditions */
  7828. assert(p_j2k != 00);
  7829. assert(p_stream != 00);
  7830. assert(p_manager != 00);
  7831. OPJ_UNUSED(p_stream);
  7832. l_image = p_j2k->m_private_image;
  7833. l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  7834. l_tcp = p_j2k->m_cp.tcps;
  7835. l_tccp_size = l_image->numcomps * (OPJ_UINT32)sizeof(opj_tccp_t);
  7836. l_default_tcp = p_j2k->m_specific_param.m_decoder.m_default_tcp;
  7837. l_mct_size = l_image->numcomps * l_image->numcomps * (OPJ_UINT32)sizeof(
  7838. OPJ_FLOAT32);
  7839. /* For each tile */
  7840. for (i = 0; i < l_nb_tiles; ++i) {
  7841. /* keep the tile-compo coding parameters pointer of the current tile coding parameters*/
  7842. l_current_tccp = l_tcp->tccps;
  7843. /*Copy default coding parameters into the current tile coding parameters*/
  7844. memcpy(l_tcp, l_default_tcp, sizeof(opj_tcp_t));
  7845. /* Initialize some values of the current tile coding parameters*/
  7846. l_tcp->cod = 0;
  7847. l_tcp->ppt = 0;
  7848. l_tcp->ppt_data = 00;
  7849. l_tcp->m_current_tile_part_number = -1;
  7850. /* Remove memory not owned by this tile in case of early error return. */
  7851. l_tcp->m_mct_decoding_matrix = 00;
  7852. l_tcp->m_nb_max_mct_records = 0;
  7853. l_tcp->m_mct_records = 00;
  7854. l_tcp->m_nb_max_mcc_records = 0;
  7855. l_tcp->m_mcc_records = 00;
  7856. /* Reconnect the tile-compo coding parameters pointer to the current tile coding parameters*/
  7857. l_tcp->tccps = l_current_tccp;
  7858. /* Get the mct_decoding_matrix of the dflt_tile_cp and copy them into the current tile cp*/
  7859. if (l_default_tcp->m_mct_decoding_matrix) {
  7860. l_tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(l_mct_size);
  7861. if (! l_tcp->m_mct_decoding_matrix) {
  7862. return OPJ_FALSE;
  7863. }
  7864. memcpy(l_tcp->m_mct_decoding_matrix, l_default_tcp->m_mct_decoding_matrix,
  7865. l_mct_size);
  7866. }
  7867. /* Get the mct_record of the dflt_tile_cp and copy them into the current tile cp*/
  7868. l_mct_records_size = l_default_tcp->m_nb_max_mct_records * (OPJ_UINT32)sizeof(
  7869. opj_mct_data_t);
  7870. l_tcp->m_mct_records = (opj_mct_data_t*)opj_malloc(l_mct_records_size);
  7871. if (! l_tcp->m_mct_records) {
  7872. return OPJ_FALSE;
  7873. }
  7874. memcpy(l_tcp->m_mct_records, l_default_tcp->m_mct_records, l_mct_records_size);
  7875. /* Copy the mct record data from dflt_tile_cp to the current tile*/
  7876. l_src_mct_rec = l_default_tcp->m_mct_records;
  7877. l_dest_mct_rec = l_tcp->m_mct_records;
  7878. for (j = 0; j < l_default_tcp->m_nb_mct_records; ++j) {
  7879. if (l_src_mct_rec->m_data) {
  7880. l_dest_mct_rec->m_data = (OPJ_BYTE*) opj_malloc(l_src_mct_rec->m_data_size);
  7881. if (! l_dest_mct_rec->m_data) {
  7882. return OPJ_FALSE;
  7883. }
  7884. memcpy(l_dest_mct_rec->m_data, l_src_mct_rec->m_data,
  7885. l_src_mct_rec->m_data_size);
  7886. }
  7887. ++l_src_mct_rec;
  7888. ++l_dest_mct_rec;
  7889. /* Update with each pass to free exactly what has been allocated on early return. */
  7890. l_tcp->m_nb_max_mct_records += 1;
  7891. }
  7892. /* Get the mcc_record of the dflt_tile_cp and copy them into the current tile cp*/
  7893. l_mcc_records_size = l_default_tcp->m_nb_max_mcc_records * (OPJ_UINT32)sizeof(
  7894. opj_simple_mcc_decorrelation_data_t);
  7895. l_tcp->m_mcc_records = (opj_simple_mcc_decorrelation_data_t*) opj_malloc(
  7896. l_mcc_records_size);
  7897. if (! l_tcp->m_mcc_records) {
  7898. return OPJ_FALSE;
  7899. }
  7900. memcpy(l_tcp->m_mcc_records, l_default_tcp->m_mcc_records, l_mcc_records_size);
  7901. l_tcp->m_nb_max_mcc_records = l_default_tcp->m_nb_max_mcc_records;
  7902. /* Copy the mcc record data from dflt_tile_cp to the current tile*/
  7903. l_src_mcc_rec = l_default_tcp->m_mcc_records;
  7904. l_dest_mcc_rec = l_tcp->m_mcc_records;
  7905. for (j = 0; j < l_default_tcp->m_nb_max_mcc_records; ++j) {
  7906. if (l_src_mcc_rec->m_decorrelation_array) {
  7907. l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_decorrelation_array -
  7908. l_default_tcp->m_mct_records);
  7909. l_dest_mcc_rec->m_decorrelation_array = l_tcp->m_mct_records + l_offset;
  7910. }
  7911. if (l_src_mcc_rec->m_offset_array) {
  7912. l_offset = (OPJ_UINT32)(l_src_mcc_rec->m_offset_array -
  7913. l_default_tcp->m_mct_records);
  7914. l_dest_mcc_rec->m_offset_array = l_tcp->m_mct_records + l_offset;
  7915. }
  7916. ++l_src_mcc_rec;
  7917. ++l_dest_mcc_rec;
  7918. }
  7919. /* Copy all the dflt_tile_compo_cp to the current tile cp */
  7920. memcpy(l_current_tccp, l_default_tcp->tccps, l_tccp_size);
  7921. /* Move to next tile cp*/
  7922. ++l_tcp;
  7923. }
  7924. /* Create the current tile decoder*/
  7925. p_j2k->m_tcd = opj_tcd_create(OPJ_TRUE);
  7926. if (! p_j2k->m_tcd) {
  7927. return OPJ_FALSE;
  7928. }
  7929. if (!opj_tcd_init(p_j2k->m_tcd, l_image, &(p_j2k->m_cp), p_j2k->m_tp)) {
  7930. opj_tcd_destroy(p_j2k->m_tcd);
  7931. p_j2k->m_tcd = 00;
  7932. opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
  7933. return OPJ_FALSE;
  7934. }
  7935. return OPJ_TRUE;
  7936. }
  7937. static const opj_dec_memory_marker_handler_t * opj_j2k_get_marker_handler(
  7938. OPJ_UINT32 p_id)
  7939. {
  7940. const opj_dec_memory_marker_handler_t *e;
  7941. for (e = j2k_memory_marker_handler_tab; e->id != 0; ++e) {
  7942. if (e->id == p_id) {
  7943. break; /* we find a handler corresponding to the marker ID*/
  7944. }
  7945. }
  7946. return e;
  7947. }
  7948. void opj_j2k_destroy(opj_j2k_t *p_j2k)
  7949. {
  7950. if (p_j2k == 00) {
  7951. return;
  7952. }
  7953. if (p_j2k->m_is_decoder) {
  7954. if (p_j2k->m_specific_param.m_decoder.m_default_tcp != 00) {
  7955. opj_j2k_tcp_destroy(p_j2k->m_specific_param.m_decoder.m_default_tcp);
  7956. opj_free(p_j2k->m_specific_param.m_decoder.m_default_tcp);
  7957. p_j2k->m_specific_param.m_decoder.m_default_tcp = 00;
  7958. }
  7959. if (p_j2k->m_specific_param.m_decoder.m_header_data != 00) {
  7960. opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
  7961. p_j2k->m_specific_param.m_decoder.m_header_data = 00;
  7962. p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
  7963. }
  7964. opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode);
  7965. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = 00;
  7966. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0;
  7967. } else {
  7968. if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
  7969. opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
  7970. p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 00;
  7971. }
  7972. if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
  7973. opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
  7974. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 00;
  7975. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 00;
  7976. }
  7977. if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
  7978. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  7979. p_j2k->m_specific_param.m_encoder.m_header_tile_data = 00;
  7980. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  7981. }
  7982. }
  7983. opj_tcd_destroy(p_j2k->m_tcd);
  7984. opj_j2k_cp_destroy(&(p_j2k->m_cp));
  7985. memset(&(p_j2k->m_cp), 0, sizeof(opj_cp_t));
  7986. opj_procedure_list_destroy(p_j2k->m_procedure_list);
  7987. p_j2k->m_procedure_list = 00;
  7988. opj_procedure_list_destroy(p_j2k->m_validation_list);
  7989. p_j2k->m_procedure_list = 00;
  7990. j2k_destroy_cstr_index(p_j2k->cstr_index);
  7991. p_j2k->cstr_index = NULL;
  7992. opj_image_destroy(p_j2k->m_private_image);
  7993. p_j2k->m_private_image = NULL;
  7994. opj_image_destroy(p_j2k->m_output_image);
  7995. p_j2k->m_output_image = NULL;
  7996. opj_thread_pool_destroy(p_j2k->m_tp);
  7997. p_j2k->m_tp = NULL;
  7998. opj_free(p_j2k);
  7999. }
  8000. void j2k_destroy_cstr_index(opj_codestream_index_t *p_cstr_ind)
  8001. {
  8002. if (p_cstr_ind) {
  8003. if (p_cstr_ind->marker) {
  8004. opj_free(p_cstr_ind->marker);
  8005. p_cstr_ind->marker = NULL;
  8006. }
  8007. if (p_cstr_ind->tile_index) {
  8008. OPJ_UINT32 it_tile = 0;
  8009. for (it_tile = 0; it_tile < p_cstr_ind->nb_of_tiles; it_tile++) {
  8010. if (p_cstr_ind->tile_index[it_tile].packet_index) {
  8011. opj_free(p_cstr_ind->tile_index[it_tile].packet_index);
  8012. p_cstr_ind->tile_index[it_tile].packet_index = NULL;
  8013. }
  8014. if (p_cstr_ind->tile_index[it_tile].tp_index) {
  8015. opj_free(p_cstr_ind->tile_index[it_tile].tp_index);
  8016. p_cstr_ind->tile_index[it_tile].tp_index = NULL;
  8017. }
  8018. if (p_cstr_ind->tile_index[it_tile].marker) {
  8019. opj_free(p_cstr_ind->tile_index[it_tile].marker);
  8020. p_cstr_ind->tile_index[it_tile].marker = NULL;
  8021. }
  8022. }
  8023. opj_free(p_cstr_ind->tile_index);
  8024. p_cstr_ind->tile_index = NULL;
  8025. }
  8026. opj_free(p_cstr_ind);
  8027. }
  8028. }
  8029. static void opj_j2k_tcp_destroy(opj_tcp_t *p_tcp)
  8030. {
  8031. if (p_tcp == 00) {
  8032. return;
  8033. }
  8034. if (p_tcp->ppt_markers != 00) {
  8035. OPJ_UINT32 i;
  8036. for (i = 0U; i < p_tcp->ppt_markers_count; ++i) {
  8037. if (p_tcp->ppt_markers[i].m_data != NULL) {
  8038. opj_free(p_tcp->ppt_markers[i].m_data);
  8039. }
  8040. }
  8041. p_tcp->ppt_markers_count = 0U;
  8042. opj_free(p_tcp->ppt_markers);
  8043. p_tcp->ppt_markers = NULL;
  8044. }
  8045. if (p_tcp->ppt_buffer != 00) {
  8046. opj_free(p_tcp->ppt_buffer);
  8047. p_tcp->ppt_buffer = 00;
  8048. }
  8049. if (p_tcp->tccps != 00) {
  8050. opj_free(p_tcp->tccps);
  8051. p_tcp->tccps = 00;
  8052. }
  8053. if (p_tcp->m_mct_coding_matrix != 00) {
  8054. opj_free(p_tcp->m_mct_coding_matrix);
  8055. p_tcp->m_mct_coding_matrix = 00;
  8056. }
  8057. if (p_tcp->m_mct_decoding_matrix != 00) {
  8058. opj_free(p_tcp->m_mct_decoding_matrix);
  8059. p_tcp->m_mct_decoding_matrix = 00;
  8060. }
  8061. if (p_tcp->m_mcc_records) {
  8062. opj_free(p_tcp->m_mcc_records);
  8063. p_tcp->m_mcc_records = 00;
  8064. p_tcp->m_nb_max_mcc_records = 0;
  8065. p_tcp->m_nb_mcc_records = 0;
  8066. }
  8067. if (p_tcp->m_mct_records) {
  8068. opj_mct_data_t * l_mct_data = p_tcp->m_mct_records;
  8069. OPJ_UINT32 i;
  8070. for (i = 0; i < p_tcp->m_nb_mct_records; ++i) {
  8071. if (l_mct_data->m_data) {
  8072. opj_free(l_mct_data->m_data);
  8073. l_mct_data->m_data = 00;
  8074. }
  8075. ++l_mct_data;
  8076. }
  8077. opj_free(p_tcp->m_mct_records);
  8078. p_tcp->m_mct_records = 00;
  8079. }
  8080. if (p_tcp->mct_norms != 00) {
  8081. opj_free(p_tcp->mct_norms);
  8082. p_tcp->mct_norms = 00;
  8083. }
  8084. opj_j2k_tcp_data_destroy(p_tcp);
  8085. }
  8086. static void opj_j2k_tcp_data_destroy(opj_tcp_t *p_tcp)
  8087. {
  8088. if (p_tcp->m_data) {
  8089. opj_free(p_tcp->m_data);
  8090. p_tcp->m_data = NULL;
  8091. p_tcp->m_data_size = 0;
  8092. }
  8093. }
  8094. static void opj_j2k_cp_destroy(opj_cp_t *p_cp)
  8095. {
  8096. OPJ_UINT32 l_nb_tiles;
  8097. opj_tcp_t * l_current_tile = 00;
  8098. if (p_cp == 00) {
  8099. return;
  8100. }
  8101. if (p_cp->tcps != 00) {
  8102. OPJ_UINT32 i;
  8103. l_current_tile = p_cp->tcps;
  8104. l_nb_tiles = p_cp->th * p_cp->tw;
  8105. for (i = 0U; i < l_nb_tiles; ++i) {
  8106. opj_j2k_tcp_destroy(l_current_tile);
  8107. ++l_current_tile;
  8108. }
  8109. opj_free(p_cp->tcps);
  8110. p_cp->tcps = 00;
  8111. }
  8112. if (p_cp->ppm_markers != 00) {
  8113. OPJ_UINT32 i;
  8114. for (i = 0U; i < p_cp->ppm_markers_count; ++i) {
  8115. if (p_cp->ppm_markers[i].m_data != NULL) {
  8116. opj_free(p_cp->ppm_markers[i].m_data);
  8117. }
  8118. }
  8119. p_cp->ppm_markers_count = 0U;
  8120. opj_free(p_cp->ppm_markers);
  8121. p_cp->ppm_markers = NULL;
  8122. }
  8123. opj_free(p_cp->ppm_buffer);
  8124. p_cp->ppm_buffer = 00;
  8125. p_cp->ppm_data =
  8126. NULL; /* ppm_data belongs to the allocated buffer pointed by ppm_buffer */
  8127. opj_free(p_cp->comment);
  8128. p_cp->comment = 00;
  8129. if (! p_cp->m_is_decoder) {
  8130. opj_free(p_cp->m_specific_param.m_enc.m_matrice);
  8131. p_cp->m_specific_param.m_enc.m_matrice = 00;
  8132. }
  8133. }
  8134. static OPJ_BOOL opj_j2k_need_nb_tile_parts_correction(opj_stream_private_t
  8135. *p_stream, OPJ_UINT32 tile_no, OPJ_BOOL* p_correction_needed,
  8136. opj_event_mgr_t * p_manager)
  8137. {
  8138. OPJ_BYTE l_header_data[10];
  8139. OPJ_OFF_T l_stream_pos_backup;
  8140. OPJ_UINT32 l_current_marker;
  8141. OPJ_UINT32 l_marker_size;
  8142. OPJ_UINT32 l_tile_no, l_tot_len, l_current_part, l_num_parts;
  8143. /* initialize to no correction needed */
  8144. *p_correction_needed = OPJ_FALSE;
  8145. if (!opj_stream_has_seek(p_stream)) {
  8146. /* We can't do much in this case, seek is needed */
  8147. return OPJ_TRUE;
  8148. }
  8149. l_stream_pos_backup = opj_stream_tell(p_stream);
  8150. if (l_stream_pos_backup == -1) {
  8151. /* let's do nothing */
  8152. return OPJ_TRUE;
  8153. }
  8154. for (;;) {
  8155. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
  8156. if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) {
  8157. /* assume all is OK */
  8158. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8159. return OPJ_FALSE;
  8160. }
  8161. return OPJ_TRUE;
  8162. }
  8163. /* Read 2 bytes from buffer as the new marker ID */
  8164. opj_read_bytes(l_header_data, &l_current_marker, 2);
  8165. if (l_current_marker != J2K_MS_SOT) {
  8166. /* assume all is OK */
  8167. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8168. return OPJ_FALSE;
  8169. }
  8170. return OPJ_TRUE;
  8171. }
  8172. /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
  8173. if (opj_stream_read_data(p_stream, l_header_data, 2, p_manager) != 2) {
  8174. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8175. return OPJ_FALSE;
  8176. }
  8177. /* Read 2 bytes from the buffer as the marker size */
  8178. opj_read_bytes(l_header_data, &l_marker_size, 2);
  8179. /* Check marker size for SOT Marker */
  8180. if (l_marker_size != 10) {
  8181. opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n");
  8182. return OPJ_FALSE;
  8183. }
  8184. l_marker_size -= 2;
  8185. if (opj_stream_read_data(p_stream, l_header_data, l_marker_size,
  8186. p_manager) != l_marker_size) {
  8187. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8188. return OPJ_FALSE;
  8189. }
  8190. if (! opj_j2k_get_sot_values(l_header_data, l_marker_size, &l_tile_no,
  8191. &l_tot_len, &l_current_part, &l_num_parts, p_manager)) {
  8192. return OPJ_FALSE;
  8193. }
  8194. if (l_tile_no == tile_no) {
  8195. /* we found what we were looking for */
  8196. break;
  8197. }
  8198. if (l_tot_len < 14U) {
  8199. /* last SOT until EOC or invalid Psot value */
  8200. /* assume all is OK */
  8201. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8202. return OPJ_FALSE;
  8203. }
  8204. return OPJ_TRUE;
  8205. }
  8206. l_tot_len -= 12U;
  8207. /* look for next SOT marker */
  8208. if (opj_stream_skip(p_stream, (OPJ_OFF_T)(l_tot_len),
  8209. p_manager) != (OPJ_OFF_T)(l_tot_len)) {
  8210. /* assume all is OK */
  8211. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8212. return OPJ_FALSE;
  8213. }
  8214. return OPJ_TRUE;
  8215. }
  8216. }
  8217. /* check for correction */
  8218. if (l_current_part == l_num_parts) {
  8219. *p_correction_needed = OPJ_TRUE;
  8220. }
  8221. if (! opj_stream_seek(p_stream, l_stream_pos_backup, p_manager)) {
  8222. return OPJ_FALSE;
  8223. }
  8224. return OPJ_TRUE;
  8225. }
  8226. OPJ_BOOL opj_j2k_read_tile_header(opj_j2k_t * p_j2k,
  8227. OPJ_UINT32 * p_tile_index,
  8228. OPJ_UINT32 * p_data_size,
  8229. OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
  8230. OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
  8231. OPJ_UINT32 * p_nb_comps,
  8232. OPJ_BOOL * p_go_on,
  8233. opj_stream_private_t *p_stream,
  8234. opj_event_mgr_t * p_manager)
  8235. {
  8236. OPJ_UINT32 l_current_marker = J2K_MS_SOT;
  8237. OPJ_UINT32 l_marker_size;
  8238. const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
  8239. opj_tcp_t * l_tcp = NULL;
  8240. const OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
  8241. /* preconditions */
  8242. assert(p_stream != 00);
  8243. assert(p_j2k != 00);
  8244. assert(p_manager != 00);
  8245. /* Reach the End Of Codestream ?*/
  8246. if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) {
  8247. l_current_marker = J2K_MS_EOC;
  8248. }
  8249. /* We need to encounter a SOT marker (a new tile-part header) */
  8250. else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) {
  8251. return OPJ_FALSE;
  8252. }
  8253. /* Read into the codestream until reach the EOC or ! can_decode ??? FIXME */
  8254. while ((!p_j2k->m_specific_param.m_decoder.m_can_decode) &&
  8255. (l_current_marker != J2K_MS_EOC)) {
  8256. /* Try to read until the Start Of Data is detected */
  8257. while (l_current_marker != J2K_MS_SOD) {
  8258. if (opj_stream_get_number_byte_left(p_stream) == 0) {
  8259. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
  8260. break;
  8261. }
  8262. /* Try to read 2 bytes (the marker size) from stream and copy them into the buffer */
  8263. if (opj_stream_read_data(p_stream,
  8264. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  8265. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8266. return OPJ_FALSE;
  8267. }
  8268. /* Read 2 bytes from the buffer as the marker size */
  8269. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data, &l_marker_size,
  8270. 2);
  8271. /* Check marker size (does not include marker ID but includes marker size) */
  8272. if (l_marker_size < 2) {
  8273. opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n");
  8274. return OPJ_FALSE;
  8275. }
  8276. /* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */
  8277. if (l_current_marker == 0x8080 &&
  8278. opj_stream_get_number_byte_left(p_stream) == 0) {
  8279. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
  8280. break;
  8281. }
  8282. /* Why this condition? FIXME */
  8283. if (p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_TPH) {
  8284. p_j2k->m_specific_param.m_decoder.m_sot_length -= (l_marker_size + 2);
  8285. }
  8286. l_marker_size -= 2; /* Subtract the size of the marker ID already read */
  8287. /* Get the marker handler from the marker ID */
  8288. l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
  8289. /* Check if the marker is known and if it is the right place to find it */
  8290. if (!(p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states)) {
  8291. opj_event_msg(p_manager, EVT_ERROR,
  8292. "Marker is not compliant with its position\n");
  8293. return OPJ_FALSE;
  8294. }
  8295. /* FIXME manage case of unknown marker as in the main header ? */
  8296. /* Check if the marker size is compatible with the header data size */
  8297. if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
  8298. OPJ_BYTE *new_header_data = NULL;
  8299. /* If we are here, this means we consider this marker as known & we will read it */
  8300. /* Check enough bytes left in stream before allocation */
  8301. if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) {
  8302. opj_event_msg(p_manager, EVT_ERROR,
  8303. "Marker size inconsistent with stream length\n");
  8304. return OPJ_FALSE;
  8305. }
  8306. new_header_data = (OPJ_BYTE *) opj_realloc(
  8307. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
  8308. if (! new_header_data) {
  8309. opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
  8310. p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
  8311. p_j2k->m_specific_param.m_decoder.m_header_data_size = 0;
  8312. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read header\n");
  8313. return OPJ_FALSE;
  8314. }
  8315. p_j2k->m_specific_param.m_decoder.m_header_data = new_header_data;
  8316. p_j2k->m_specific_param.m_decoder.m_header_data_size = l_marker_size;
  8317. }
  8318. /* Try to read the rest of the marker segment from stream and copy them into the buffer */
  8319. if (opj_stream_read_data(p_stream,
  8320. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size,
  8321. p_manager) != l_marker_size) {
  8322. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8323. return OPJ_FALSE;
  8324. }
  8325. if (!l_marker_handler->handler) {
  8326. /* See issue #175 */
  8327. opj_event_msg(p_manager, EVT_ERROR, "Not sure how that happened.\n");
  8328. return OPJ_FALSE;
  8329. }
  8330. /* Read the marker segment with the correct marker handler */
  8331. if (!(*(l_marker_handler->handler))(p_j2k,
  8332. p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size, p_manager)) {
  8333. opj_event_msg(p_manager, EVT_ERROR,
  8334. "Fail to read the current marker segment (%#x)\n", l_current_marker);
  8335. return OPJ_FALSE;
  8336. }
  8337. /* Add the marker to the codestream index*/
  8338. if (OPJ_FALSE == opj_j2k_add_tlmarker(p_j2k->m_current_tile_number,
  8339. p_j2k->cstr_index,
  8340. l_marker_handler->id,
  8341. (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4,
  8342. l_marker_size + 4)) {
  8343. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to add tl marker\n");
  8344. return OPJ_FALSE;
  8345. }
  8346. /* Keep the position of the last SOT marker read */
  8347. if (l_marker_handler->id == J2K_MS_SOT) {
  8348. OPJ_UINT32 sot_pos = (OPJ_UINT32) opj_stream_tell(p_stream) - l_marker_size - 4
  8349. ;
  8350. if (sot_pos > p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos) {
  8351. p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = sot_pos;
  8352. }
  8353. }
  8354. if (p_j2k->m_specific_param.m_decoder.m_skip_data) {
  8355. /* Skip the rest of the tile part header*/
  8356. if (opj_stream_skip(p_stream, p_j2k->m_specific_param.m_decoder.m_sot_length,
  8357. p_manager) != p_j2k->m_specific_param.m_decoder.m_sot_length) {
  8358. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8359. return OPJ_FALSE;
  8360. }
  8361. l_current_marker = J2K_MS_SOD; /* Normally we reached a SOD */
  8362. } else {
  8363. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer*/
  8364. if (opj_stream_read_data(p_stream,
  8365. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  8366. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8367. return OPJ_FALSE;
  8368. }
  8369. /* Read 2 bytes from the buffer as the new marker ID */
  8370. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  8371. &l_current_marker, 2);
  8372. }
  8373. }
  8374. if (opj_stream_get_number_byte_left(p_stream) == 0
  8375. && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) {
  8376. break;
  8377. }
  8378. /* If we didn't skip data before, we need to read the SOD marker*/
  8379. if (! p_j2k->m_specific_param.m_decoder.m_skip_data) {
  8380. /* Try to read the SOD marker and skip data ? FIXME */
  8381. if (! opj_j2k_read_sod(p_j2k, p_stream, p_manager)) {
  8382. return OPJ_FALSE;
  8383. }
  8384. if (p_j2k->m_specific_param.m_decoder.m_can_decode &&
  8385. !p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked) {
  8386. /* Issue 254 */
  8387. OPJ_BOOL l_correction_needed;
  8388. p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1;
  8389. if (!opj_j2k_need_nb_tile_parts_correction(p_stream,
  8390. p_j2k->m_current_tile_number, &l_correction_needed, p_manager)) {
  8391. opj_event_msg(p_manager, EVT_ERROR,
  8392. "opj_j2k_apply_nb_tile_parts_correction error\n");
  8393. return OPJ_FALSE;
  8394. }
  8395. if (l_correction_needed) {
  8396. OPJ_UINT32 l_tile_no;
  8397. p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
  8398. p_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction = 1;
  8399. /* correct tiles */
  8400. for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) {
  8401. if (p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts != 0U) {
  8402. p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts += 1;
  8403. }
  8404. }
  8405. opj_event_msg(p_manager, EVT_WARNING,
  8406. "Non conformant codestream TPsot==TNsot.\n");
  8407. }
  8408. }
  8409. } else {
  8410. /* Indicate we will try to read a new tile-part header*/
  8411. p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
  8412. p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
  8413. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
  8414. }
  8415. if (! p_j2k->m_specific_param.m_decoder.m_can_decode) {
  8416. /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
  8417. if (opj_stream_read_data(p_stream,
  8418. p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
  8419. /* Deal with likely non conformant SPOT6 files, where the last */
  8420. /* row of tiles have TPsot == 0 and TNsot == 0, and missing EOC, */
  8421. /* but no other tile-parts were found. */
  8422. if (p_j2k->m_current_tile_number + 1 == l_nb_tiles) {
  8423. OPJ_UINT32 l_tile_no;
  8424. for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) {
  8425. if (p_j2k->m_cp.tcps[l_tile_no].m_current_tile_part_number == 0 &&
  8426. p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts == 0) {
  8427. break;
  8428. }
  8429. }
  8430. if (l_tile_no < l_nb_tiles) {
  8431. opj_event_msg(p_manager, EVT_INFO,
  8432. "Tile %u has TPsot == 0 and TNsot == 0, "
  8433. "but no other tile-parts were found. "
  8434. "EOC is also missing.\n",
  8435. l_tile_no);
  8436. p_j2k->m_current_tile_number = l_tile_no;
  8437. l_current_marker = J2K_MS_EOC;
  8438. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
  8439. break;
  8440. }
  8441. }
  8442. opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
  8443. return OPJ_FALSE;
  8444. }
  8445. /* Read 2 bytes from buffer as the new marker ID */
  8446. opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
  8447. &l_current_marker, 2);
  8448. }
  8449. }
  8450. /* Current marker is the EOC marker ?*/
  8451. if (l_current_marker == J2K_MS_EOC) {
  8452. if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) {
  8453. p_j2k->m_current_tile_number = 0;
  8454. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
  8455. }
  8456. }
  8457. /* Deal with tiles that have a single tile-part with TPsot == 0 and TNsot == 0 */
  8458. if (! p_j2k->m_specific_param.m_decoder.m_can_decode) {
  8459. l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
  8460. while ((p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00)) {
  8461. ++p_j2k->m_current_tile_number;
  8462. ++l_tcp;
  8463. }
  8464. if (p_j2k->m_current_tile_number == l_nb_tiles) {
  8465. *p_go_on = OPJ_FALSE;
  8466. return OPJ_TRUE;
  8467. }
  8468. }
  8469. if (! opj_j2k_merge_ppt(p_j2k->m_cp.tcps + p_j2k->m_current_tile_number,
  8470. p_manager)) {
  8471. opj_event_msg(p_manager, EVT_ERROR, "Failed to merge PPT data\n");
  8472. return OPJ_FALSE;
  8473. }
  8474. /*FIXME ???*/
  8475. if (! opj_tcd_init_decode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number,
  8476. p_manager)) {
  8477. opj_event_msg(p_manager, EVT_ERROR, "Cannot decode tile, memory error\n");
  8478. return OPJ_FALSE;
  8479. }
  8480. opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
  8481. p_j2k->m_current_tile_number + 1, (p_j2k->m_cp.th * p_j2k->m_cp.tw));
  8482. *p_tile_index = p_j2k->m_current_tile_number;
  8483. *p_go_on = OPJ_TRUE;
  8484. if (p_data_size) {
  8485. /* For internal use in j2k.c, we don't need this */
  8486. /* This is just needed for folks using the opj_read_tile_header() / opj_decode_tile_data() combo */
  8487. *p_data_size = opj_tcd_get_decoded_tile_size(p_j2k->m_tcd, OPJ_FALSE);
  8488. if (*p_data_size == UINT_MAX) {
  8489. return OPJ_FALSE;
  8490. }
  8491. }
  8492. *p_tile_x0 = p_j2k->m_tcd->tcd_image->tiles->x0;
  8493. *p_tile_y0 = p_j2k->m_tcd->tcd_image->tiles->y0;
  8494. *p_tile_x1 = p_j2k->m_tcd->tcd_image->tiles->x1;
  8495. *p_tile_y1 = p_j2k->m_tcd->tcd_image->tiles->y1;
  8496. *p_nb_comps = p_j2k->m_tcd->tcd_image->tiles->numcomps;
  8497. p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA;
  8498. return OPJ_TRUE;
  8499. }
  8500. OPJ_BOOL opj_j2k_decode_tile(opj_j2k_t * p_j2k,
  8501. OPJ_UINT32 p_tile_index,
  8502. OPJ_BYTE * p_data,
  8503. OPJ_UINT32 p_data_size,
  8504. opj_stream_private_t *p_stream,
  8505. opj_event_mgr_t * p_manager)
  8506. {
  8507. OPJ_UINT32 l_current_marker;
  8508. OPJ_BYTE l_data [2];
  8509. opj_tcp_t * l_tcp;
  8510. opj_image_t* l_image_for_bounds;
  8511. /* preconditions */
  8512. assert(p_stream != 00);
  8513. assert(p_j2k != 00);
  8514. assert(p_manager != 00);
  8515. if (!(p_j2k->m_specific_param.m_decoder.m_state & J2K_STATE_DATA)
  8516. || (p_tile_index != p_j2k->m_current_tile_number)) {
  8517. return OPJ_FALSE;
  8518. }
  8519. l_tcp = &(p_j2k->m_cp.tcps[p_tile_index]);
  8520. if (! l_tcp->m_data) {
  8521. opj_j2k_tcp_destroy(l_tcp);
  8522. return OPJ_FALSE;
  8523. }
  8524. /* When using the opj_read_tile_header / opj_decode_tile_data API */
  8525. /* such as in test_tile_decoder, m_output_image is NULL, so fall back */
  8526. /* to the full image dimension. This is a bit surprising that */
  8527. /* opj_set_decode_area() is only used to determine intersecting tiles, */
  8528. /* but full tile decoding is done */
  8529. l_image_for_bounds = p_j2k->m_output_image ? p_j2k->m_output_image :
  8530. p_j2k->m_private_image;
  8531. if (! opj_tcd_decode_tile(p_j2k->m_tcd,
  8532. l_image_for_bounds->x0,
  8533. l_image_for_bounds->y0,
  8534. l_image_for_bounds->x1,
  8535. l_image_for_bounds->y1,
  8536. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode,
  8537. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode,
  8538. l_tcp->m_data,
  8539. l_tcp->m_data_size,
  8540. p_tile_index,
  8541. p_j2k->cstr_index, p_manager)) {
  8542. opj_j2k_tcp_destroy(l_tcp);
  8543. p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_ERR;
  8544. opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");
  8545. return OPJ_FALSE;
  8546. }
  8547. /* p_data can be set to NULL when the call will take care of using */
  8548. /* itself the TCD data. This is typically the case for whole single */
  8549. /* tile decoding optimization. */
  8550. if (p_data != NULL) {
  8551. if (! opj_tcd_update_tile_data(p_j2k->m_tcd, p_data, p_data_size)) {
  8552. return OPJ_FALSE;
  8553. }
  8554. /* To avoid to destroy the tcp which can be useful when we try to decode a tile decoded before (cf j2k_random_tile_access)
  8555. * we destroy just the data which will be re-read in read_tile_header*/
  8556. /*opj_j2k_tcp_destroy(l_tcp);
  8557. p_j2k->m_tcd->tcp = 0;*/
  8558. opj_j2k_tcp_data_destroy(l_tcp);
  8559. }
  8560. p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
  8561. p_j2k->m_specific_param.m_decoder.m_state &= (~(OPJ_UINT32)J2K_STATE_DATA);
  8562. if (opj_stream_get_number_byte_left(p_stream) == 0
  8563. && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) {
  8564. return OPJ_TRUE;
  8565. }
  8566. if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) {
  8567. if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) {
  8568. opj_event_msg(p_manager, p_j2k->m_cp.strict ? EVT_ERROR : EVT_WARNING,
  8569. "Stream too short\n");
  8570. return p_j2k->m_cp.strict ? OPJ_FALSE : OPJ_TRUE;
  8571. }
  8572. opj_read_bytes(l_data, &l_current_marker, 2);
  8573. if (l_current_marker == J2K_MS_EOC) {
  8574. p_j2k->m_current_tile_number = 0;
  8575. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
  8576. } else if (l_current_marker != J2K_MS_SOT) {
  8577. if (opj_stream_get_number_byte_left(p_stream) == 0) {
  8578. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
  8579. opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n");
  8580. return OPJ_TRUE;
  8581. }
  8582. opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
  8583. return OPJ_FALSE;
  8584. }
  8585. }
  8586. return OPJ_TRUE;
  8587. }
  8588. static OPJ_BOOL opj_j2k_update_image_data(opj_tcd_t * p_tcd,
  8589. opj_image_t* p_output_image)
  8590. {
  8591. OPJ_UINT32 i, j;
  8592. OPJ_UINT32 l_width_src, l_height_src;
  8593. OPJ_UINT32 l_width_dest, l_height_dest;
  8594. OPJ_INT32 l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src;
  8595. OPJ_SIZE_T l_start_offset_src;
  8596. OPJ_UINT32 l_start_x_dest, l_start_y_dest;
  8597. OPJ_UINT32 l_x0_dest, l_y0_dest, l_x1_dest, l_y1_dest;
  8598. OPJ_SIZE_T l_start_offset_dest;
  8599. opj_image_comp_t * l_img_comp_src = 00;
  8600. opj_image_comp_t * l_img_comp_dest = 00;
  8601. opj_tcd_tilecomp_t * l_tilec = 00;
  8602. opj_image_t * l_image_src = 00;
  8603. OPJ_INT32 * l_dest_ptr;
  8604. l_tilec = p_tcd->tcd_image->tiles->comps;
  8605. l_image_src = p_tcd->image;
  8606. l_img_comp_src = l_image_src->comps;
  8607. l_img_comp_dest = p_output_image->comps;
  8608. for (i = 0; i < l_image_src->numcomps;
  8609. i++, ++l_img_comp_dest, ++l_img_comp_src, ++l_tilec) {
  8610. OPJ_INT32 res_x0, res_x1, res_y0, res_y1;
  8611. OPJ_UINT32 src_data_stride;
  8612. const OPJ_INT32* p_src_data;
  8613. /* Copy info from decoded comp image to output image */
  8614. l_img_comp_dest->resno_decoded = l_img_comp_src->resno_decoded;
  8615. if (p_tcd->whole_tile_decoding) {
  8616. opj_tcd_resolution_t* l_res = l_tilec->resolutions +
  8617. l_img_comp_src->resno_decoded;
  8618. res_x0 = l_res->x0;
  8619. res_y0 = l_res->y0;
  8620. res_x1 = l_res->x1;
  8621. res_y1 = l_res->y1;
  8622. src_data_stride = (OPJ_UINT32)(
  8623. l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x1 -
  8624. l_tilec->resolutions[l_tilec->minimum_num_resolutions - 1].x0);
  8625. p_src_data = l_tilec->data;
  8626. } else {
  8627. opj_tcd_resolution_t* l_res = l_tilec->resolutions +
  8628. l_img_comp_src->resno_decoded;
  8629. res_x0 = (OPJ_INT32)l_res->win_x0;
  8630. res_y0 = (OPJ_INT32)l_res->win_y0;
  8631. res_x1 = (OPJ_INT32)l_res->win_x1;
  8632. res_y1 = (OPJ_INT32)l_res->win_y1;
  8633. src_data_stride = l_res->win_x1 - l_res->win_x0;
  8634. p_src_data = l_tilec->data_win;
  8635. }
  8636. if (p_src_data == NULL) {
  8637. /* Happens for partial component decoding */
  8638. continue;
  8639. }
  8640. l_width_src = (OPJ_UINT32)(res_x1 - res_x0);
  8641. l_height_src = (OPJ_UINT32)(res_y1 - res_y0);
  8642. /* Current tile component size*/
  8643. /*if (i == 0) {
  8644. fprintf(stdout, "SRC: l_res_x0=%d, l_res_x1=%d, l_res_y0=%d, l_res_y1=%d\n",
  8645. res_x0, res_x1, res_y0, res_y1);
  8646. }*/
  8647. /* Border of the current output component*/
  8648. l_x0_dest = opj_uint_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
  8649. l_y0_dest = opj_uint_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
  8650. l_x1_dest = l_x0_dest +
  8651. l_img_comp_dest->w; /* can't overflow given that image->x1 is uint32 */
  8652. l_y1_dest = l_y0_dest + l_img_comp_dest->h;
  8653. /*if (i == 0) {
  8654. fprintf(stdout, "DEST: l_x0_dest=%d, l_x1_dest=%d, l_y0_dest=%d, l_y1_dest=%d (%d)\n",
  8655. l_x0_dest, l_x1_dest, l_y0_dest, l_y1_dest, l_img_comp_dest->factor );
  8656. }*/
  8657. /*-----*/
  8658. /* Compute the area (l_offset_x0_src, l_offset_y0_src, l_offset_x1_src, l_offset_y1_src)
  8659. * of the input buffer (decoded tile component) which will be move
  8660. * in the output buffer. Compute the area of the output buffer (l_start_x_dest,
  8661. * l_start_y_dest, l_width_dest, l_height_dest) which will be modified
  8662. * by this input area.
  8663. * */
  8664. assert(res_x0 >= 0);
  8665. assert(res_x1 >= 0);
  8666. if (l_x0_dest < (OPJ_UINT32)res_x0) {
  8667. l_start_x_dest = (OPJ_UINT32)res_x0 - l_x0_dest;
  8668. l_offset_x0_src = 0;
  8669. if (l_x1_dest >= (OPJ_UINT32)res_x1) {
  8670. l_width_dest = l_width_src;
  8671. l_offset_x1_src = 0;
  8672. } else {
  8673. l_width_dest = l_x1_dest - (OPJ_UINT32)res_x0 ;
  8674. l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest);
  8675. }
  8676. } else {
  8677. l_start_x_dest = 0U;
  8678. l_offset_x0_src = (OPJ_INT32)l_x0_dest - res_x0;
  8679. if (l_x1_dest >= (OPJ_UINT32)res_x1) {
  8680. l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src;
  8681. l_offset_x1_src = 0;
  8682. } else {
  8683. l_width_dest = l_img_comp_dest->w ;
  8684. l_offset_x1_src = res_x1 - (OPJ_INT32)l_x1_dest;
  8685. }
  8686. }
  8687. if (l_y0_dest < (OPJ_UINT32)res_y0) {
  8688. l_start_y_dest = (OPJ_UINT32)res_y0 - l_y0_dest;
  8689. l_offset_y0_src = 0;
  8690. if (l_y1_dest >= (OPJ_UINT32)res_y1) {
  8691. l_height_dest = l_height_src;
  8692. l_offset_y1_src = 0;
  8693. } else {
  8694. l_height_dest = l_y1_dest - (OPJ_UINT32)res_y0 ;
  8695. l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest);
  8696. }
  8697. } else {
  8698. l_start_y_dest = 0U;
  8699. l_offset_y0_src = (OPJ_INT32)l_y0_dest - res_y0;
  8700. if (l_y1_dest >= (OPJ_UINT32)res_y1) {
  8701. l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src;
  8702. l_offset_y1_src = 0;
  8703. } else {
  8704. l_height_dest = l_img_comp_dest->h ;
  8705. l_offset_y1_src = res_y1 - (OPJ_INT32)l_y1_dest;
  8706. }
  8707. }
  8708. if ((l_offset_x0_src < 0) || (l_offset_y0_src < 0) || (l_offset_x1_src < 0) ||
  8709. (l_offset_y1_src < 0)) {
  8710. return OPJ_FALSE;
  8711. }
  8712. /* testcase 2977.pdf.asan.67.2198 */
  8713. if ((OPJ_INT32)l_width_dest < 0 || (OPJ_INT32)l_height_dest < 0) {
  8714. return OPJ_FALSE;
  8715. }
  8716. /*-----*/
  8717. /* Compute the input buffer offset */
  8718. l_start_offset_src = (OPJ_SIZE_T)l_offset_x0_src + (OPJ_SIZE_T)l_offset_y0_src
  8719. * (OPJ_SIZE_T)src_data_stride;
  8720. /* Compute the output buffer offset */
  8721. l_start_offset_dest = (OPJ_SIZE_T)l_start_x_dest + (OPJ_SIZE_T)l_start_y_dest
  8722. * (OPJ_SIZE_T)l_img_comp_dest->w;
  8723. /* Allocate output component buffer if necessary */
  8724. if (l_img_comp_dest->data == NULL &&
  8725. l_start_offset_src == 0 && l_start_offset_dest == 0 &&
  8726. src_data_stride == l_img_comp_dest->w &&
  8727. l_width_dest == l_img_comp_dest->w &&
  8728. l_height_dest == l_img_comp_dest->h) {
  8729. /* If the final image matches the tile buffer, then borrow it */
  8730. /* directly to save a copy */
  8731. if (p_tcd->whole_tile_decoding) {
  8732. l_img_comp_dest->data = l_tilec->data;
  8733. l_tilec->data = NULL;
  8734. } else {
  8735. l_img_comp_dest->data = l_tilec->data_win;
  8736. l_tilec->data_win = NULL;
  8737. }
  8738. continue;
  8739. } else if (l_img_comp_dest->data == NULL) {
  8740. OPJ_SIZE_T l_width = l_img_comp_dest->w;
  8741. OPJ_SIZE_T l_height = l_img_comp_dest->h;
  8742. if ((l_height == 0U) || (l_width > (SIZE_MAX / l_height)) ||
  8743. l_width * l_height > SIZE_MAX / sizeof(OPJ_INT32)) {
  8744. /* would overflow */
  8745. return OPJ_FALSE;
  8746. }
  8747. l_img_comp_dest->data = (OPJ_INT32*) opj_image_data_alloc(l_width * l_height *
  8748. sizeof(OPJ_INT32));
  8749. if (! l_img_comp_dest->data) {
  8750. return OPJ_FALSE;
  8751. }
  8752. if (l_img_comp_dest->w != l_width_dest ||
  8753. l_img_comp_dest->h != l_height_dest) {
  8754. memset(l_img_comp_dest->data, 0,
  8755. (OPJ_SIZE_T)l_img_comp_dest->w * l_img_comp_dest->h * sizeof(OPJ_INT32));
  8756. }
  8757. }
  8758. /* Move the output buffer to the first place where we will write*/
  8759. l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
  8760. {
  8761. const OPJ_INT32 * l_src_ptr = p_src_data;
  8762. l_src_ptr += l_start_offset_src;
  8763. for (j = 0; j < l_height_dest; ++j) {
  8764. memcpy(l_dest_ptr, l_src_ptr, l_width_dest * sizeof(OPJ_INT32));
  8765. l_dest_ptr += l_img_comp_dest->w;
  8766. l_src_ptr += src_data_stride;
  8767. }
  8768. }
  8769. }
  8770. return OPJ_TRUE;
  8771. }
  8772. static OPJ_BOOL opj_j2k_update_image_dimensions(opj_image_t* p_image,
  8773. opj_event_mgr_t * p_manager)
  8774. {
  8775. OPJ_UINT32 it_comp;
  8776. OPJ_INT32 l_comp_x1, l_comp_y1;
  8777. opj_image_comp_t* l_img_comp = NULL;
  8778. l_img_comp = p_image->comps;
  8779. for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
  8780. OPJ_INT32 l_h, l_w;
  8781. if (p_image->x0 > (OPJ_UINT32)INT_MAX ||
  8782. p_image->y0 > (OPJ_UINT32)INT_MAX ||
  8783. p_image->x1 > (OPJ_UINT32)INT_MAX ||
  8784. p_image->y1 > (OPJ_UINT32)INT_MAX) {
  8785. opj_event_msg(p_manager, EVT_ERROR,
  8786. "Image coordinates above INT_MAX are not supported\n");
  8787. return OPJ_FALSE;
  8788. }
  8789. l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx);
  8790. l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy);
  8791. l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
  8792. l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
  8793. l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor)
  8794. - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor);
  8795. if (l_w < 0) {
  8796. opj_event_msg(p_manager, EVT_ERROR,
  8797. "Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
  8798. it_comp, l_w);
  8799. return OPJ_FALSE;
  8800. }
  8801. l_img_comp->w = (OPJ_UINT32)l_w;
  8802. l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor)
  8803. - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor);
  8804. if (l_h < 0) {
  8805. opj_event_msg(p_manager, EVT_ERROR,
  8806. "Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
  8807. it_comp, l_h);
  8808. return OPJ_FALSE;
  8809. }
  8810. l_img_comp->h = (OPJ_UINT32)l_h;
  8811. l_img_comp++;
  8812. }
  8813. return OPJ_TRUE;
  8814. }
  8815. OPJ_BOOL opj_j2k_set_decoded_components(opj_j2k_t *p_j2k,
  8816. OPJ_UINT32 numcomps,
  8817. const OPJ_UINT32* comps_indices,
  8818. opj_event_mgr_t * p_manager)
  8819. {
  8820. OPJ_UINT32 i;
  8821. OPJ_BOOL* already_mapped;
  8822. if (p_j2k->m_private_image == NULL) {
  8823. opj_event_msg(p_manager, EVT_ERROR,
  8824. "opj_read_header() should be called before "
  8825. "opj_set_decoded_components().\n");
  8826. return OPJ_FALSE;
  8827. }
  8828. already_mapped = (OPJ_BOOL*) opj_calloc(sizeof(OPJ_BOOL),
  8829. p_j2k->m_private_image->numcomps);
  8830. if (already_mapped == NULL) {
  8831. return OPJ_FALSE;
  8832. }
  8833. for (i = 0; i < numcomps; i++) {
  8834. if (comps_indices[i] >= p_j2k->m_private_image->numcomps) {
  8835. opj_event_msg(p_manager, EVT_ERROR,
  8836. "Invalid component index: %u\n",
  8837. comps_indices[i]);
  8838. opj_free(already_mapped);
  8839. return OPJ_FALSE;
  8840. }
  8841. if (already_mapped[comps_indices[i]]) {
  8842. opj_event_msg(p_manager, EVT_ERROR,
  8843. "Component index %u used several times\n",
  8844. comps_indices[i]);
  8845. opj_free(already_mapped);
  8846. return OPJ_FALSE;
  8847. }
  8848. already_mapped[comps_indices[i]] = OPJ_TRUE;
  8849. }
  8850. opj_free(already_mapped);
  8851. opj_free(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode);
  8852. if (numcomps) {
  8853. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode =
  8854. (OPJ_UINT32*) opj_malloc(numcomps * sizeof(OPJ_UINT32));
  8855. if (p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode == NULL) {
  8856. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = 0;
  8857. return OPJ_FALSE;
  8858. }
  8859. memcpy(p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode,
  8860. comps_indices,
  8861. numcomps * sizeof(OPJ_UINT32));
  8862. } else {
  8863. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode = NULL;
  8864. }
  8865. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode = numcomps;
  8866. return OPJ_TRUE;
  8867. }
  8868. OPJ_BOOL opj_j2k_set_decode_area(opj_j2k_t *p_j2k,
  8869. opj_image_t* p_image,
  8870. OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
  8871. OPJ_INT32 p_end_x, OPJ_INT32 p_end_y,
  8872. opj_event_mgr_t * p_manager)
  8873. {
  8874. opj_cp_t * l_cp = &(p_j2k->m_cp);
  8875. opj_image_t * l_image = p_j2k->m_private_image;
  8876. OPJ_BOOL ret;
  8877. OPJ_UINT32 it_comp;
  8878. if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
  8879. p_j2k->m_cp.tcps[0].m_data != NULL) {
  8880. /* In the case of a single-tiled image whose codestream we have already */
  8881. /* ingested, go on */
  8882. }
  8883. /* Check if we are read the main header */
  8884. else if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_TPHSOT) {
  8885. opj_event_msg(p_manager, EVT_ERROR,
  8886. "Need to decode the main header before begin to decode the remaining codestream.\n");
  8887. return OPJ_FALSE;
  8888. }
  8889. /* Update the comps[].factor member of the output image with the one */
  8890. /* of m_reduce */
  8891. for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
  8892. p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce;
  8893. }
  8894. if (!p_start_x && !p_start_y && !p_end_x && !p_end_y) {
  8895. opj_event_msg(p_manager, EVT_INFO,
  8896. "No decoded area parameters, set the decoded area to the whole image\n");
  8897. p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
  8898. p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
  8899. p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
  8900. p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
  8901. p_image->x0 = l_image->x0;
  8902. p_image->y0 = l_image->y0;
  8903. p_image->x1 = l_image->x1;
  8904. p_image->y1 = l_image->y1;
  8905. return opj_j2k_update_image_dimensions(p_image, p_manager);
  8906. }
  8907. /* ----- */
  8908. /* Check if the positions provided by the user are correct */
  8909. /* Left */
  8910. if (p_start_x < 0) {
  8911. opj_event_msg(p_manager, EVT_ERROR,
  8912. "Left position of the decoded area (region_x0=%d) should be >= 0.\n",
  8913. p_start_x);
  8914. return OPJ_FALSE;
  8915. } else if ((OPJ_UINT32)p_start_x > l_image->x1) {
  8916. opj_event_msg(p_manager, EVT_ERROR,
  8917. "Left position of the decoded area (region_x0=%d) is outside the image area (Xsiz=%d).\n",
  8918. p_start_x, l_image->x1);
  8919. return OPJ_FALSE;
  8920. } else if ((OPJ_UINT32)p_start_x < l_image->x0) {
  8921. opj_event_msg(p_manager, EVT_WARNING,
  8922. "Left position of the decoded area (region_x0=%d) is outside the image area (XOsiz=%d).\n",
  8923. p_start_x, l_image->x0);
  8924. p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
  8925. p_image->x0 = l_image->x0;
  8926. } else {
  8927. p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x -
  8928. l_cp->tx0) / l_cp->tdx;
  8929. p_image->x0 = (OPJ_UINT32)p_start_x;
  8930. }
  8931. /* Up */
  8932. if (p_start_y < 0) {
  8933. opj_event_msg(p_manager, EVT_ERROR,
  8934. "Up position of the decoded area (region_y0=%d) should be >= 0.\n",
  8935. p_start_y);
  8936. return OPJ_FALSE;
  8937. } else if ((OPJ_UINT32)p_start_y > l_image->y1) {
  8938. opj_event_msg(p_manager, EVT_ERROR,
  8939. "Up position of the decoded area (region_y0=%d) is outside the image area (Ysiz=%d).\n",
  8940. p_start_y, l_image->y1);
  8941. return OPJ_FALSE;
  8942. } else if ((OPJ_UINT32)p_start_y < l_image->y0) {
  8943. opj_event_msg(p_manager, EVT_WARNING,
  8944. "Up position of the decoded area (region_y0=%d) is outside the image area (YOsiz=%d).\n",
  8945. p_start_y, l_image->y0);
  8946. p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
  8947. p_image->y0 = l_image->y0;
  8948. } else {
  8949. p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y -
  8950. l_cp->ty0) / l_cp->tdy;
  8951. p_image->y0 = (OPJ_UINT32)p_start_y;
  8952. }
  8953. /* Right */
  8954. if (p_end_x <= 0) {
  8955. opj_event_msg(p_manager, EVT_ERROR,
  8956. "Right position of the decoded area (region_x1=%d) should be > 0.\n",
  8957. p_end_x);
  8958. return OPJ_FALSE;
  8959. } else if ((OPJ_UINT32)p_end_x < l_image->x0) {
  8960. opj_event_msg(p_manager, EVT_ERROR,
  8961. "Right position of the decoded area (region_x1=%d) is outside the image area (XOsiz=%d).\n",
  8962. p_end_x, l_image->x0);
  8963. return OPJ_FALSE;
  8964. } else if ((OPJ_UINT32)p_end_x > l_image->x1) {
  8965. opj_event_msg(p_manager, EVT_WARNING,
  8966. "Right position of the decoded area (region_x1=%d) is outside the image area (Xsiz=%d).\n",
  8967. p_end_x, l_image->x1);
  8968. p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
  8969. p_image->x1 = l_image->x1;
  8970. } else {
  8971. p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv((
  8972. OPJ_UINT32)p_end_x - l_cp->tx0, l_cp->tdx);
  8973. p_image->x1 = (OPJ_UINT32)p_end_x;
  8974. }
  8975. /* Bottom */
  8976. if (p_end_y <= 0) {
  8977. opj_event_msg(p_manager, EVT_ERROR,
  8978. "Bottom position of the decoded area (region_y1=%d) should be > 0.\n",
  8979. p_end_y);
  8980. return OPJ_FALSE;
  8981. } else if ((OPJ_UINT32)p_end_y < l_image->y0) {
  8982. opj_event_msg(p_manager, EVT_ERROR,
  8983. "Bottom position of the decoded area (region_y1=%d) is outside the image area (YOsiz=%d).\n",
  8984. p_end_y, l_image->y0);
  8985. return OPJ_FALSE;
  8986. }
  8987. if ((OPJ_UINT32)p_end_y > l_image->y1) {
  8988. opj_event_msg(p_manager, EVT_WARNING,
  8989. "Bottom position of the decoded area (region_y1=%d) is outside the image area (Ysiz=%d).\n",
  8990. p_end_y, l_image->y1);
  8991. p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
  8992. p_image->y1 = l_image->y1;
  8993. } else {
  8994. p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv((
  8995. OPJ_UINT32)p_end_y - l_cp->ty0, l_cp->tdy);
  8996. p_image->y1 = (OPJ_UINT32)p_end_y;
  8997. }
  8998. /* ----- */
  8999. p_j2k->m_specific_param.m_decoder.m_discard_tiles = 1;
  9000. ret = opj_j2k_update_image_dimensions(p_image, p_manager);
  9001. if (ret) {
  9002. opj_event_msg(p_manager, EVT_INFO, "Setting decoding area to %d,%d,%d,%d\n",
  9003. p_image->x0, p_image->y0, p_image->x1, p_image->y1);
  9004. }
  9005. return ret;
  9006. }
  9007. opj_j2k_t* opj_j2k_create_decompress(void)
  9008. {
  9009. opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1, sizeof(opj_j2k_t));
  9010. if (!l_j2k) {
  9011. return 00;
  9012. }
  9013. l_j2k->m_is_decoder = 1;
  9014. l_j2k->m_cp.m_is_decoder = 1;
  9015. /* in the absence of JP2 boxes, consider different bit depth / sign */
  9016. /* per component is allowed */
  9017. l_j2k->m_cp.allow_different_bit_depth_sign = 1;
  9018. /* Default to using strict mode. */
  9019. l_j2k->m_cp.strict = OPJ_TRUE;
  9020. #ifdef OPJ_DISABLE_TPSOT_FIX
  9021. l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1;
  9022. #endif
  9023. l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1,
  9024. sizeof(opj_tcp_t));
  9025. if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
  9026. opj_j2k_destroy(l_j2k);
  9027. return 00;
  9028. }
  9029. l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1,
  9030. OPJ_J2K_DEFAULT_HEADER_SIZE);
  9031. if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
  9032. opj_j2k_destroy(l_j2k);
  9033. return 00;
  9034. }
  9035. l_j2k->m_specific_param.m_decoder.m_header_data_size =
  9036. OPJ_J2K_DEFAULT_HEADER_SIZE;
  9037. l_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = -1 ;
  9038. l_j2k->m_specific_param.m_decoder.m_last_sot_read_pos = 0 ;
  9039. /* codestream index creation */
  9040. l_j2k->cstr_index = opj_j2k_create_cstr_index();
  9041. if (!l_j2k->cstr_index) {
  9042. opj_j2k_destroy(l_j2k);
  9043. return 00;
  9044. }
  9045. /* validation list creation */
  9046. l_j2k->m_validation_list = opj_procedure_list_create();
  9047. if (! l_j2k->m_validation_list) {
  9048. opj_j2k_destroy(l_j2k);
  9049. return 00;
  9050. }
  9051. /* execution list creation */
  9052. l_j2k->m_procedure_list = opj_procedure_list_create();
  9053. if (! l_j2k->m_procedure_list) {
  9054. opj_j2k_destroy(l_j2k);
  9055. return 00;
  9056. }
  9057. l_j2k->m_tp = opj_thread_pool_create(opj_j2k_get_default_thread_count());
  9058. if (!l_j2k->m_tp) {
  9059. l_j2k->m_tp = opj_thread_pool_create(0);
  9060. }
  9061. if (!l_j2k->m_tp) {
  9062. opj_j2k_destroy(l_j2k);
  9063. return NULL;
  9064. }
  9065. return l_j2k;
  9066. }
  9067. static opj_codestream_index_t* opj_j2k_create_cstr_index(void)
  9068. {
  9069. opj_codestream_index_t* cstr_index = (opj_codestream_index_t*)
  9070. opj_calloc(1, sizeof(opj_codestream_index_t));
  9071. if (!cstr_index) {
  9072. return NULL;
  9073. }
  9074. cstr_index->maxmarknum = 100;
  9075. cstr_index->marknum = 0;
  9076. cstr_index->marker = (opj_marker_info_t*)
  9077. opj_calloc(cstr_index->maxmarknum, sizeof(opj_marker_info_t));
  9078. if (!cstr_index-> marker) {
  9079. opj_free(cstr_index);
  9080. return NULL;
  9081. }
  9082. cstr_index->tile_index = NULL;
  9083. return cstr_index;
  9084. }
  9085. static OPJ_UINT32 opj_j2k_get_SPCod_SPCoc_size(opj_j2k_t *p_j2k,
  9086. OPJ_UINT32 p_tile_no,
  9087. OPJ_UINT32 p_comp_no)
  9088. {
  9089. opj_cp_t *l_cp = 00;
  9090. opj_tcp_t *l_tcp = 00;
  9091. opj_tccp_t *l_tccp = 00;
  9092. /* preconditions */
  9093. assert(p_j2k != 00);
  9094. l_cp = &(p_j2k->m_cp);
  9095. l_tcp = &l_cp->tcps[p_tile_no];
  9096. l_tccp = &l_tcp->tccps[p_comp_no];
  9097. /* preconditions again */
  9098. assert(p_tile_no < (l_cp->tw * l_cp->th));
  9099. assert(p_comp_no < p_j2k->m_private_image->numcomps);
  9100. if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
  9101. return 5 + l_tccp->numresolutions;
  9102. } else {
  9103. return 5;
  9104. }
  9105. }
  9106. static OPJ_BOOL opj_j2k_compare_SPCod_SPCoc(opj_j2k_t *p_j2k,
  9107. OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
  9108. {
  9109. OPJ_UINT32 i;
  9110. opj_cp_t *l_cp = NULL;
  9111. opj_tcp_t *l_tcp = NULL;
  9112. opj_tccp_t *l_tccp0 = NULL;
  9113. opj_tccp_t *l_tccp1 = NULL;
  9114. /* preconditions */
  9115. assert(p_j2k != 00);
  9116. l_cp = &(p_j2k->m_cp);
  9117. l_tcp = &l_cp->tcps[p_tile_no];
  9118. l_tccp0 = &l_tcp->tccps[p_first_comp_no];
  9119. l_tccp1 = &l_tcp->tccps[p_second_comp_no];
  9120. if (l_tccp0->numresolutions != l_tccp1->numresolutions) {
  9121. return OPJ_FALSE;
  9122. }
  9123. if (l_tccp0->cblkw != l_tccp1->cblkw) {
  9124. return OPJ_FALSE;
  9125. }
  9126. if (l_tccp0->cblkh != l_tccp1->cblkh) {
  9127. return OPJ_FALSE;
  9128. }
  9129. if (l_tccp0->cblksty != l_tccp1->cblksty) {
  9130. return OPJ_FALSE;
  9131. }
  9132. if (l_tccp0->qmfbid != l_tccp1->qmfbid) {
  9133. return OPJ_FALSE;
  9134. }
  9135. if ((l_tccp0->csty & J2K_CCP_CSTY_PRT) != (l_tccp1->csty & J2K_CCP_CSTY_PRT)) {
  9136. return OPJ_FALSE;
  9137. }
  9138. for (i = 0U; i < l_tccp0->numresolutions; ++i) {
  9139. if (l_tccp0->prcw[i] != l_tccp1->prcw[i]) {
  9140. return OPJ_FALSE;
  9141. }
  9142. if (l_tccp0->prch[i] != l_tccp1->prch[i]) {
  9143. return OPJ_FALSE;
  9144. }
  9145. }
  9146. return OPJ_TRUE;
  9147. }
  9148. static OPJ_BOOL opj_j2k_write_SPCod_SPCoc(opj_j2k_t *p_j2k,
  9149. OPJ_UINT32 p_tile_no,
  9150. OPJ_UINT32 p_comp_no,
  9151. OPJ_BYTE * p_data,
  9152. OPJ_UINT32 * p_header_size,
  9153. struct opj_event_mgr * p_manager)
  9154. {
  9155. OPJ_UINT32 i;
  9156. opj_cp_t *l_cp = 00;
  9157. opj_tcp_t *l_tcp = 00;
  9158. opj_tccp_t *l_tccp = 00;
  9159. /* preconditions */
  9160. assert(p_j2k != 00);
  9161. assert(p_header_size != 00);
  9162. assert(p_manager != 00);
  9163. assert(p_data != 00);
  9164. l_cp = &(p_j2k->m_cp);
  9165. l_tcp = &l_cp->tcps[p_tile_no];
  9166. l_tccp = &l_tcp->tccps[p_comp_no];
  9167. /* preconditions again */
  9168. assert(p_tile_no < (l_cp->tw * l_cp->th));
  9169. assert(p_comp_no < (p_j2k->m_private_image->numcomps));
  9170. if (*p_header_size < 5) {
  9171. opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
  9172. return OPJ_FALSE;
  9173. }
  9174. opj_write_bytes(p_data, l_tccp->numresolutions - 1, 1); /* SPcoc (D) */
  9175. ++p_data;
  9176. opj_write_bytes(p_data, l_tccp->cblkw - 2, 1); /* SPcoc (E) */
  9177. ++p_data;
  9178. opj_write_bytes(p_data, l_tccp->cblkh - 2, 1); /* SPcoc (F) */
  9179. ++p_data;
  9180. opj_write_bytes(p_data, l_tccp->cblksty,
  9181. 1); /* SPcoc (G) */
  9182. ++p_data;
  9183. opj_write_bytes(p_data, l_tccp->qmfbid,
  9184. 1); /* SPcoc (H) */
  9185. ++p_data;
  9186. *p_header_size = *p_header_size - 5;
  9187. if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
  9188. if (*p_header_size < l_tccp->numresolutions) {
  9189. opj_event_msg(p_manager, EVT_ERROR, "Error writing SPCod SPCoc element\n");
  9190. return OPJ_FALSE;
  9191. }
  9192. for (i = 0; i < l_tccp->numresolutions; ++i) {
  9193. opj_write_bytes(p_data, l_tccp->prcw[i] + (l_tccp->prch[i] << 4),
  9194. 1); /* SPcoc (I_i) */
  9195. ++p_data;
  9196. }
  9197. *p_header_size = *p_header_size - l_tccp->numresolutions;
  9198. }
  9199. return OPJ_TRUE;
  9200. }
  9201. static OPJ_BOOL opj_j2k_read_SPCod_SPCoc(opj_j2k_t *p_j2k,
  9202. OPJ_UINT32 compno,
  9203. OPJ_BYTE * p_header_data,
  9204. OPJ_UINT32 * p_header_size,
  9205. opj_event_mgr_t * p_manager)
  9206. {
  9207. OPJ_UINT32 i, l_tmp;
  9208. opj_cp_t *l_cp = NULL;
  9209. opj_tcp_t *l_tcp = NULL;
  9210. opj_tccp_t *l_tccp = NULL;
  9211. OPJ_BYTE * l_current_ptr = NULL;
  9212. /* preconditions */
  9213. assert(p_j2k != 00);
  9214. assert(p_manager != 00);
  9215. assert(p_header_data != 00);
  9216. l_cp = &(p_j2k->m_cp);
  9217. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH) ?
  9218. &l_cp->tcps[p_j2k->m_current_tile_number] :
  9219. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9220. /* precondition again */
  9221. assert(compno < p_j2k->m_private_image->numcomps);
  9222. l_tccp = &l_tcp->tccps[compno];
  9223. l_current_ptr = p_header_data;
  9224. /* make sure room is sufficient */
  9225. if (*p_header_size < 5) {
  9226. opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
  9227. return OPJ_FALSE;
  9228. }
  9229. /* SPcod (D) / SPcoc (A) */
  9230. opj_read_bytes(l_current_ptr, &l_tccp->numresolutions, 1);
  9231. ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
  9232. if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) {
  9233. opj_event_msg(p_manager, EVT_ERROR,
  9234. "Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n",
  9235. l_tccp->numresolutions, OPJ_J2K_MAXRLVLS);
  9236. return OPJ_FALSE;
  9237. }
  9238. ++l_current_ptr;
  9239. /* If user wants to remove more resolutions than the codestream contains, return error */
  9240. if (l_cp->m_specific_param.m_dec.m_reduce >= l_tccp->numresolutions) {
  9241. opj_event_msg(p_manager, EVT_ERROR,
  9242. "Error decoding component %d.\nThe number of resolutions "
  9243. "to remove (%d) is greater or equal than the number "
  9244. "of resolutions of this component (%d)\nModify the cp_reduce parameter.\n\n",
  9245. compno, l_cp->m_specific_param.m_dec.m_reduce, l_tccp->numresolutions);
  9246. p_j2k->m_specific_param.m_decoder.m_state |=
  9247. 0x8000;/* FIXME J2K_DEC_STATE_ERR;*/
  9248. return OPJ_FALSE;
  9249. }
  9250. /* SPcod (E) / SPcoc (B) */
  9251. opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1);
  9252. ++l_current_ptr;
  9253. l_tccp->cblkw += 2;
  9254. /* SPcod (F) / SPcoc (C) */
  9255. opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1);
  9256. ++l_current_ptr;
  9257. l_tccp->cblkh += 2;
  9258. if ((l_tccp->cblkw > 10) || (l_tccp->cblkh > 10) ||
  9259. ((l_tccp->cblkw + l_tccp->cblkh) > 12)) {
  9260. opj_event_msg(p_manager, EVT_ERROR,
  9261. "Error reading SPCod SPCoc element, Invalid cblkw/cblkh combination\n");
  9262. return OPJ_FALSE;
  9263. }
  9264. /* SPcod (G) / SPcoc (D) */
  9265. opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1);
  9266. ++l_current_ptr;
  9267. if ((l_tccp->cblksty & J2K_CCP_CBLKSTY_HTMIXED) != 0) {
  9268. /* We do not support HT mixed mode yet. For conformance, it should be supported.*/
  9269. opj_event_msg(p_manager, EVT_ERROR,
  9270. "Error reading SPCod SPCoc element. Unsupported Mixed HT code-block style found\n");
  9271. return OPJ_FALSE;
  9272. }
  9273. /* SPcod (H) / SPcoc (E) */
  9274. opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1);
  9275. ++l_current_ptr;
  9276. if (l_tccp->qmfbid > 1) {
  9277. opj_event_msg(p_manager, EVT_ERROR,
  9278. "Error reading SPCod SPCoc element, Invalid transformation found\n");
  9279. return OPJ_FALSE;
  9280. }
  9281. *p_header_size = *p_header_size - 5;
  9282. /* use custom precinct size ? */
  9283. if (l_tccp->csty & J2K_CCP_CSTY_PRT) {
  9284. if (*p_header_size < l_tccp->numresolutions) {
  9285. opj_event_msg(p_manager, EVT_ERROR, "Error reading SPCod SPCoc element\n");
  9286. return OPJ_FALSE;
  9287. }
  9288. /* SPcod (I_i) / SPcoc (F_i) */
  9289. for (i = 0; i < l_tccp->numresolutions; ++i) {
  9290. opj_read_bytes(l_current_ptr, &l_tmp, 1);
  9291. ++l_current_ptr;
  9292. /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */
  9293. if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) {
  9294. opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n");
  9295. return OPJ_FALSE;
  9296. }
  9297. l_tccp->prcw[i] = l_tmp & 0xf;
  9298. l_tccp->prch[i] = l_tmp >> 4;
  9299. }
  9300. *p_header_size = *p_header_size - l_tccp->numresolutions;
  9301. } else {
  9302. /* set default size for the precinct width and height */
  9303. for (i = 0; i < l_tccp->numresolutions; ++i) {
  9304. l_tccp->prcw[i] = 15;
  9305. l_tccp->prch[i] = 15;
  9306. }
  9307. }
  9308. #ifdef WIP_REMOVE_MSD
  9309. /* INDEX >> */
  9310. if (p_j2k->cstr_info && compno == 0) {
  9311. OPJ_UINT32 l_data_size = l_tccp->numresolutions * sizeof(OPJ_UINT32);
  9312. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkh =
  9313. l_tccp->cblkh;
  9314. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblkw =
  9315. l_tccp->cblkw;
  9316. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].numresolutions
  9317. = l_tccp->numresolutions;
  9318. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].cblksty =
  9319. l_tccp->cblksty;
  9320. p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].tccp_info[compno].qmfbid =
  9321. l_tccp->qmfbid;
  9322. memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdx, l_tccp->prcw,
  9323. l_data_size);
  9324. memcpy(p_j2k->cstr_info->tile[p_j2k->m_current_tile_number].pdy, l_tccp->prch,
  9325. l_data_size);
  9326. }
  9327. /* << INDEX */
  9328. #endif
  9329. return OPJ_TRUE;
  9330. }
  9331. static void opj_j2k_copy_tile_component_parameters(opj_j2k_t *p_j2k)
  9332. {
  9333. /* loop */
  9334. OPJ_UINT32 i;
  9335. opj_cp_t *l_cp = NULL;
  9336. opj_tcp_t *l_tcp = NULL;
  9337. opj_tccp_t *l_ref_tccp = NULL, *l_copied_tccp = NULL;
  9338. OPJ_UINT32 l_prc_size;
  9339. /* preconditions */
  9340. assert(p_j2k != 00);
  9341. l_cp = &(p_j2k->m_cp);
  9342. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
  9343. ?
  9344. &l_cp->tcps[p_j2k->m_current_tile_number] :
  9345. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9346. l_ref_tccp = &l_tcp->tccps[0];
  9347. l_copied_tccp = l_ref_tccp + 1;
  9348. l_prc_size = l_ref_tccp->numresolutions * (OPJ_UINT32)sizeof(OPJ_UINT32);
  9349. for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) {
  9350. l_copied_tccp->numresolutions = l_ref_tccp->numresolutions;
  9351. l_copied_tccp->cblkw = l_ref_tccp->cblkw;
  9352. l_copied_tccp->cblkh = l_ref_tccp->cblkh;
  9353. l_copied_tccp->cblksty = l_ref_tccp->cblksty;
  9354. l_copied_tccp->qmfbid = l_ref_tccp->qmfbid;
  9355. memcpy(l_copied_tccp->prcw, l_ref_tccp->prcw, l_prc_size);
  9356. memcpy(l_copied_tccp->prch, l_ref_tccp->prch, l_prc_size);
  9357. ++l_copied_tccp;
  9358. }
  9359. }
  9360. static OPJ_UINT32 opj_j2k_get_SQcd_SQcc_size(opj_j2k_t *p_j2k,
  9361. OPJ_UINT32 p_tile_no,
  9362. OPJ_UINT32 p_comp_no)
  9363. {
  9364. OPJ_UINT32 l_num_bands;
  9365. opj_cp_t *l_cp = 00;
  9366. opj_tcp_t *l_tcp = 00;
  9367. opj_tccp_t *l_tccp = 00;
  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_tccp = &l_tcp->tccps[p_comp_no];
  9373. /* preconditions again */
  9374. assert(p_tile_no < l_cp->tw * l_cp->th);
  9375. assert(p_comp_no < p_j2k->m_private_image->numcomps);
  9376. l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
  9377. (l_tccp->numresolutions * 3 - 2);
  9378. if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  9379. return 1 + l_num_bands;
  9380. } else {
  9381. return 1 + 2 * l_num_bands;
  9382. }
  9383. }
  9384. static OPJ_BOOL opj_j2k_compare_SQcd_SQcc(opj_j2k_t *p_j2k,
  9385. OPJ_UINT32 p_tile_no, OPJ_UINT32 p_first_comp_no, OPJ_UINT32 p_second_comp_no)
  9386. {
  9387. opj_cp_t *l_cp = NULL;
  9388. opj_tcp_t *l_tcp = NULL;
  9389. opj_tccp_t *l_tccp0 = NULL;
  9390. opj_tccp_t *l_tccp1 = NULL;
  9391. OPJ_UINT32 l_band_no, l_num_bands;
  9392. /* preconditions */
  9393. assert(p_j2k != 00);
  9394. l_cp = &(p_j2k->m_cp);
  9395. l_tcp = &l_cp->tcps[p_tile_no];
  9396. l_tccp0 = &l_tcp->tccps[p_first_comp_no];
  9397. l_tccp1 = &l_tcp->tccps[p_second_comp_no];
  9398. if (l_tccp0->qntsty != l_tccp1->qntsty) {
  9399. return OPJ_FALSE;
  9400. }
  9401. if (l_tccp0->numgbits != l_tccp1->numgbits) {
  9402. return OPJ_FALSE;
  9403. }
  9404. if (l_tccp0->qntsty == J2K_CCP_QNTSTY_SIQNT) {
  9405. l_num_bands = 1U;
  9406. } else {
  9407. l_num_bands = l_tccp0->numresolutions * 3U - 2U;
  9408. if (l_num_bands != (l_tccp1->numresolutions * 3U - 2U)) {
  9409. return OPJ_FALSE;
  9410. }
  9411. }
  9412. for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
  9413. if (l_tccp0->stepsizes[l_band_no].expn != l_tccp1->stepsizes[l_band_no].expn) {
  9414. return OPJ_FALSE;
  9415. }
  9416. }
  9417. if (l_tccp0->qntsty != J2K_CCP_QNTSTY_NOQNT) {
  9418. for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
  9419. if (l_tccp0->stepsizes[l_band_no].mant != l_tccp1->stepsizes[l_band_no].mant) {
  9420. return OPJ_FALSE;
  9421. }
  9422. }
  9423. }
  9424. return OPJ_TRUE;
  9425. }
  9426. static OPJ_BOOL opj_j2k_write_SQcd_SQcc(opj_j2k_t *p_j2k,
  9427. OPJ_UINT32 p_tile_no,
  9428. OPJ_UINT32 p_comp_no,
  9429. OPJ_BYTE * p_data,
  9430. OPJ_UINT32 * p_header_size,
  9431. struct opj_event_mgr * p_manager)
  9432. {
  9433. OPJ_UINT32 l_header_size;
  9434. OPJ_UINT32 l_band_no, l_num_bands;
  9435. OPJ_UINT32 l_expn, l_mant;
  9436. opj_cp_t *l_cp = 00;
  9437. opj_tcp_t *l_tcp = 00;
  9438. opj_tccp_t *l_tccp = 00;
  9439. /* preconditions */
  9440. assert(p_j2k != 00);
  9441. assert(p_header_size != 00);
  9442. assert(p_manager != 00);
  9443. assert(p_data != 00);
  9444. l_cp = &(p_j2k->m_cp);
  9445. l_tcp = &l_cp->tcps[p_tile_no];
  9446. l_tccp = &l_tcp->tccps[p_comp_no];
  9447. /* preconditions again */
  9448. assert(p_tile_no < l_cp->tw * l_cp->th);
  9449. assert(p_comp_no < p_j2k->m_private_image->numcomps);
  9450. l_num_bands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
  9451. (l_tccp->numresolutions * 3 - 2);
  9452. if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  9453. l_header_size = 1 + l_num_bands;
  9454. if (*p_header_size < l_header_size) {
  9455. opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
  9456. return OPJ_FALSE;
  9457. }
  9458. opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5),
  9459. 1); /* Sqcx */
  9460. ++p_data;
  9461. for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
  9462. l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
  9463. opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
  9464. ++p_data;
  9465. }
  9466. } else {
  9467. l_header_size = 1 + 2 * l_num_bands;
  9468. if (*p_header_size < l_header_size) {
  9469. opj_event_msg(p_manager, EVT_ERROR, "Error writing SQcd SQcc element\n");
  9470. return OPJ_FALSE;
  9471. }
  9472. opj_write_bytes(p_data, l_tccp->qntsty + (l_tccp->numgbits << 5),
  9473. 1); /* Sqcx */
  9474. ++p_data;
  9475. for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
  9476. l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
  9477. l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant;
  9478. opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
  9479. p_data += 2;
  9480. }
  9481. }
  9482. *p_header_size = *p_header_size - l_header_size;
  9483. return OPJ_TRUE;
  9484. }
  9485. static OPJ_BOOL opj_j2k_read_SQcd_SQcc(opj_j2k_t *p_j2k,
  9486. OPJ_UINT32 p_comp_no,
  9487. OPJ_BYTE* p_header_data,
  9488. OPJ_UINT32 * p_header_size,
  9489. opj_event_mgr_t * p_manager
  9490. )
  9491. {
  9492. /* loop*/
  9493. OPJ_UINT32 l_band_no;
  9494. opj_cp_t *l_cp = 00;
  9495. opj_tcp_t *l_tcp = 00;
  9496. opj_tccp_t *l_tccp = 00;
  9497. OPJ_BYTE * l_current_ptr = 00;
  9498. OPJ_UINT32 l_tmp, l_num_band;
  9499. /* preconditions*/
  9500. assert(p_j2k != 00);
  9501. assert(p_manager != 00);
  9502. assert(p_header_data != 00);
  9503. l_cp = &(p_j2k->m_cp);
  9504. /* come from tile part header or main header ?*/
  9505. l_tcp = (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH)
  9506. ?
  9507. &l_cp->tcps[p_j2k->m_current_tile_number] :
  9508. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9509. /* precondition again*/
  9510. assert(p_comp_no < p_j2k->m_private_image->numcomps);
  9511. l_tccp = &l_tcp->tccps[p_comp_no];
  9512. l_current_ptr = p_header_data;
  9513. if (*p_header_size < 1) {
  9514. opj_event_msg(p_manager, EVT_ERROR, "Error reading SQcd or SQcc element\n");
  9515. return OPJ_FALSE;
  9516. }
  9517. *p_header_size -= 1;
  9518. opj_read_bytes(l_current_ptr, &l_tmp, 1); /* Sqcx */
  9519. ++l_current_ptr;
  9520. l_tccp->qntsty = l_tmp & 0x1f;
  9521. l_tccp->numgbits = l_tmp >> 5;
  9522. if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
  9523. l_num_band = 1;
  9524. } else {
  9525. l_num_band = (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) ?
  9526. (*p_header_size) :
  9527. (*p_header_size) / 2;
  9528. if (l_num_band > OPJ_J2K_MAXBANDS) {
  9529. opj_event_msg(p_manager, EVT_WARNING,
  9530. "While reading CCP_QNTSTY element inside QCD or QCC marker segment, "
  9531. "number of subbands (%d) is greater to OPJ_J2K_MAXBANDS (%d). So we limit the number of elements stored to "
  9532. "OPJ_J2K_MAXBANDS (%d) and skip the rest. \n", l_num_band, OPJ_J2K_MAXBANDS,
  9533. OPJ_J2K_MAXBANDS);
  9534. /*return OPJ_FALSE;*/
  9535. }
  9536. }
  9537. #ifdef USE_JPWL
  9538. if (l_cp->correct) {
  9539. /* if JPWL is on, we check whether there are too many subbands */
  9540. if (/*(l_num_band < 0) ||*/ (l_num_band >= OPJ_J2K_MAXBANDS)) {
  9541. opj_event_msg(p_manager, JPWL_ASSUME ? EVT_WARNING : EVT_ERROR,
  9542. "JPWL: bad number of subbands in Sqcx (%d)\n",
  9543. l_num_band);
  9544. if (!JPWL_ASSUME) {
  9545. opj_event_msg(p_manager, EVT_ERROR, "JPWL: giving up\n");
  9546. return OPJ_FALSE;
  9547. }
  9548. /* we try to correct */
  9549. l_num_band = 1;
  9550. opj_event_msg(p_manager, EVT_WARNING, "- trying to adjust them\n"
  9551. "- setting number of bands to %d => HYPOTHESIS!!!\n",
  9552. l_num_band);
  9553. };
  9554. };
  9555. #endif /* USE_JPWL */
  9556. if (l_tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
  9557. for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
  9558. opj_read_bytes(l_current_ptr, &l_tmp, 1); /* SPqcx_i */
  9559. ++l_current_ptr;
  9560. if (l_band_no < OPJ_J2K_MAXBANDS) {
  9561. l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3);
  9562. l_tccp->stepsizes[l_band_no].mant = 0;
  9563. }
  9564. }
  9565. if (*p_header_size < l_num_band) {
  9566. return OPJ_FALSE;
  9567. }
  9568. *p_header_size = *p_header_size - l_num_band;
  9569. } else {
  9570. for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
  9571. opj_read_bytes(l_current_ptr, &l_tmp, 2); /* SPqcx_i */
  9572. l_current_ptr += 2;
  9573. if (l_band_no < OPJ_J2K_MAXBANDS) {
  9574. l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11);
  9575. l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
  9576. }
  9577. }
  9578. if (*p_header_size < 2 * l_num_band) {
  9579. return OPJ_FALSE;
  9580. }
  9581. *p_header_size = *p_header_size - 2 * l_num_band;
  9582. }
  9583. /* Add Antonin : if scalar_derived -> compute other stepsizes */
  9584. if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
  9585. for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) {
  9586. l_tccp->stepsizes[l_band_no].expn =
  9587. ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0)
  9588. ?
  9589. (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0;
  9590. l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
  9591. }
  9592. }
  9593. return OPJ_TRUE;
  9594. }
  9595. static void opj_j2k_copy_tile_quantization_parameters(opj_j2k_t *p_j2k)
  9596. {
  9597. OPJ_UINT32 i;
  9598. opj_cp_t *l_cp = NULL;
  9599. opj_tcp_t *l_tcp = NULL;
  9600. opj_tccp_t *l_ref_tccp = NULL;
  9601. opj_tccp_t *l_copied_tccp = NULL;
  9602. OPJ_UINT32 l_size;
  9603. /* preconditions */
  9604. assert(p_j2k != 00);
  9605. l_cp = &(p_j2k->m_cp);
  9606. l_tcp = p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_TPH ?
  9607. &l_cp->tcps[p_j2k->m_current_tile_number] :
  9608. p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9609. l_ref_tccp = &l_tcp->tccps[0];
  9610. l_copied_tccp = l_ref_tccp + 1;
  9611. l_size = OPJ_J2K_MAXBANDS * sizeof(opj_stepsize_t);
  9612. for (i = 1; i < p_j2k->m_private_image->numcomps; ++i) {
  9613. l_copied_tccp->qntsty = l_ref_tccp->qntsty;
  9614. l_copied_tccp->numgbits = l_ref_tccp->numgbits;
  9615. memcpy(l_copied_tccp->stepsizes, l_ref_tccp->stepsizes, l_size);
  9616. ++l_copied_tccp;
  9617. }
  9618. }
  9619. static void opj_j2k_dump_tile_info(opj_tcp_t * l_default_tile,
  9620. OPJ_INT32 numcomps, FILE* out_stream)
  9621. {
  9622. if (l_default_tile) {
  9623. OPJ_INT32 compno;
  9624. fprintf(out_stream, "\t default tile {\n");
  9625. fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
  9626. fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
  9627. fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
  9628. fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
  9629. for (compno = 0; compno < numcomps; compno++) {
  9630. opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
  9631. OPJ_UINT32 resno;
  9632. OPJ_INT32 bandno, numbands;
  9633. /* coding style*/
  9634. fprintf(out_stream, "\t\t comp %d {\n", compno);
  9635. fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
  9636. fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
  9637. fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
  9638. fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
  9639. fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
  9640. fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
  9641. fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
  9642. for (resno = 0; resno < l_tccp->numresolutions; resno++) {
  9643. fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
  9644. }
  9645. fprintf(out_stream, "\n");
  9646. /* quantization style*/
  9647. fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
  9648. fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
  9649. fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
  9650. numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
  9651. (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
  9652. for (bandno = 0; bandno < numbands; bandno++) {
  9653. fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
  9654. l_tccp->stepsizes[bandno].expn);
  9655. }
  9656. fprintf(out_stream, "\n");
  9657. /* RGN value*/
  9658. fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
  9659. fprintf(out_stream, "\t\t }\n");
  9660. } /*end of component of default tile*/
  9661. fprintf(out_stream, "\t }\n"); /*end of default tile*/
  9662. }
  9663. }
  9664. void j2k_dump(opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
  9665. {
  9666. /* Check if the flag is compatible with j2k file*/
  9667. if ((flag & OPJ_JP2_INFO) || (flag & OPJ_JP2_IND)) {
  9668. fprintf(out_stream, "Wrong flag\n");
  9669. return;
  9670. }
  9671. /* Dump the image_header */
  9672. if (flag & OPJ_IMG_INFO) {
  9673. if (p_j2k->m_private_image) {
  9674. j2k_dump_image_header(p_j2k->m_private_image, 0, out_stream);
  9675. }
  9676. }
  9677. /* Dump the codestream info from main header */
  9678. if (flag & OPJ_J2K_MH_INFO) {
  9679. if (p_j2k->m_private_image) {
  9680. opj_j2k_dump_MH_info(p_j2k, out_stream);
  9681. }
  9682. }
  9683. /* Dump all tile/codestream info */
  9684. if (flag & OPJ_J2K_TCH_INFO) {
  9685. OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  9686. OPJ_UINT32 i;
  9687. opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
  9688. if (p_j2k->m_private_image) {
  9689. for (i = 0; i < l_nb_tiles; ++i) {
  9690. opj_j2k_dump_tile_info(l_tcp, (OPJ_INT32)p_j2k->m_private_image->numcomps,
  9691. out_stream);
  9692. ++l_tcp;
  9693. }
  9694. }
  9695. }
  9696. /* Dump the codestream info of the current tile */
  9697. if (flag & OPJ_J2K_TH_INFO) {
  9698. }
  9699. /* Dump the codestream index from main header */
  9700. if (flag & OPJ_J2K_MH_IND) {
  9701. opj_j2k_dump_MH_index(p_j2k, out_stream);
  9702. }
  9703. /* Dump the codestream index of the current tile */
  9704. if (flag & OPJ_J2K_TH_IND) {
  9705. }
  9706. }
  9707. static void opj_j2k_dump_MH_index(opj_j2k_t* p_j2k, FILE* out_stream)
  9708. {
  9709. opj_codestream_index_t* cstr_index = p_j2k->cstr_index;
  9710. OPJ_UINT32 it_marker, it_tile, it_tile_part;
  9711. fprintf(out_stream, "Codestream index from main header: {\n");
  9712. fprintf(out_stream, "\t Main header start position=%" PRIi64 "\n"
  9713. "\t Main header end position=%" PRIi64 "\n",
  9714. cstr_index->main_head_start, cstr_index->main_head_end);
  9715. fprintf(out_stream, "\t Marker list: {\n");
  9716. if (cstr_index->marker) {
  9717. for (it_marker = 0; it_marker < cstr_index->marknum ; it_marker++) {
  9718. fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
  9719. cstr_index->marker[it_marker].type,
  9720. cstr_index->marker[it_marker].pos,
  9721. cstr_index->marker[it_marker].len);
  9722. }
  9723. }
  9724. fprintf(out_stream, "\t }\n");
  9725. if (cstr_index->tile_index) {
  9726. /* Simple test to avoid to write empty information*/
  9727. OPJ_UINT32 l_acc_nb_of_tile_part = 0;
  9728. for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) {
  9729. l_acc_nb_of_tile_part += cstr_index->tile_index[it_tile].nb_tps;
  9730. }
  9731. if (l_acc_nb_of_tile_part) {
  9732. fprintf(out_stream, "\t Tile index: {\n");
  9733. for (it_tile = 0; it_tile < cstr_index->nb_of_tiles ; it_tile++) {
  9734. OPJ_UINT32 nb_of_tile_part = cstr_index->tile_index[it_tile].nb_tps;
  9735. fprintf(out_stream, "\t\t nb of tile-part in tile [%d]=%d\n", it_tile,
  9736. nb_of_tile_part);
  9737. if (cstr_index->tile_index[it_tile].tp_index) {
  9738. for (it_tile_part = 0; it_tile_part < nb_of_tile_part; it_tile_part++) {
  9739. fprintf(out_stream, "\t\t\t tile-part[%d]: star_pos=%" PRIi64 ", end_header=%"
  9740. PRIi64 ", end_pos=%" PRIi64 ".\n",
  9741. it_tile_part,
  9742. cstr_index->tile_index[it_tile].tp_index[it_tile_part].start_pos,
  9743. cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_header,
  9744. cstr_index->tile_index[it_tile].tp_index[it_tile_part].end_pos);
  9745. }
  9746. }
  9747. if (cstr_index->tile_index[it_tile].marker) {
  9748. for (it_marker = 0; it_marker < cstr_index->tile_index[it_tile].marknum ;
  9749. it_marker++) {
  9750. fprintf(out_stream, "\t\t type=%#x, pos=%" PRIi64 ", len=%d\n",
  9751. cstr_index->tile_index[it_tile].marker[it_marker].type,
  9752. cstr_index->tile_index[it_tile].marker[it_marker].pos,
  9753. cstr_index->tile_index[it_tile].marker[it_marker].len);
  9754. }
  9755. }
  9756. }
  9757. fprintf(out_stream, "\t }\n");
  9758. }
  9759. }
  9760. fprintf(out_stream, "}\n");
  9761. }
  9762. static void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream)
  9763. {
  9764. fprintf(out_stream, "Codestream info from main header: {\n");
  9765. fprintf(out_stream, "\t tx0=%" PRIu32 ", ty0=%" PRIu32 "\n", p_j2k->m_cp.tx0,
  9766. p_j2k->m_cp.ty0);
  9767. fprintf(out_stream, "\t tdx=%" PRIu32 ", tdy=%" PRIu32 "\n", p_j2k->m_cp.tdx,
  9768. p_j2k->m_cp.tdy);
  9769. fprintf(out_stream, "\t tw=%" PRIu32 ", th=%" PRIu32 "\n", p_j2k->m_cp.tw,
  9770. p_j2k->m_cp.th);
  9771. opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp,
  9772. (OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
  9773. fprintf(out_stream, "}\n");
  9774. }
  9775. void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag,
  9776. FILE* out_stream)
  9777. {
  9778. char tab[2];
  9779. if (dev_dump_flag) {
  9780. fprintf(stdout, "[DEV] Dump an image_header struct {\n");
  9781. tab[0] = '\0';
  9782. } else {
  9783. fprintf(out_stream, "Image info {\n");
  9784. tab[0] = '\t';
  9785. tab[1] = '\0';
  9786. }
  9787. fprintf(out_stream, "%s x0=%d, y0=%d\n", tab, img_header->x0, img_header->y0);
  9788. fprintf(out_stream, "%s x1=%d, y1=%d\n", tab, img_header->x1,
  9789. img_header->y1);
  9790. fprintf(out_stream, "%s numcomps=%d\n", tab, img_header->numcomps);
  9791. if (img_header->comps) {
  9792. OPJ_UINT32 compno;
  9793. for (compno = 0; compno < img_header->numcomps; compno++) {
  9794. fprintf(out_stream, "%s\t component %d {\n", tab, compno);
  9795. j2k_dump_image_comp_header(&(img_header->comps[compno]), dev_dump_flag,
  9796. out_stream);
  9797. fprintf(out_stream, "%s}\n", tab);
  9798. }
  9799. }
  9800. fprintf(out_stream, "}\n");
  9801. }
  9802. void j2k_dump_image_comp_header(opj_image_comp_t* comp_header,
  9803. OPJ_BOOL dev_dump_flag, FILE* out_stream)
  9804. {
  9805. char tab[3];
  9806. if (dev_dump_flag) {
  9807. fprintf(stdout, "[DEV] Dump an image_comp_header struct {\n");
  9808. tab[0] = '\0';
  9809. } else {
  9810. tab[0] = '\t';
  9811. tab[1] = '\t';
  9812. tab[2] = '\0';
  9813. }
  9814. fprintf(out_stream, "%s dx=%d, dy=%d\n", tab, comp_header->dx, comp_header->dy);
  9815. fprintf(out_stream, "%s prec=%d\n", tab, comp_header->prec);
  9816. fprintf(out_stream, "%s sgnd=%d\n", tab, comp_header->sgnd);
  9817. if (dev_dump_flag) {
  9818. fprintf(out_stream, "}\n");
  9819. }
  9820. }
  9821. opj_codestream_info_v2_t* j2k_get_cstr_info(opj_j2k_t* p_j2k)
  9822. {
  9823. OPJ_UINT32 compno;
  9824. OPJ_UINT32 numcomps = p_j2k->m_private_image->numcomps;
  9825. opj_tcp_t *l_default_tile;
  9826. opj_codestream_info_v2_t* cstr_info = (opj_codestream_info_v2_t*) opj_calloc(1,
  9827. sizeof(opj_codestream_info_v2_t));
  9828. if (!cstr_info) {
  9829. return NULL;
  9830. }
  9831. cstr_info->nbcomps = p_j2k->m_private_image->numcomps;
  9832. cstr_info->tx0 = p_j2k->m_cp.tx0;
  9833. cstr_info->ty0 = p_j2k->m_cp.ty0;
  9834. cstr_info->tdx = p_j2k->m_cp.tdx;
  9835. cstr_info->tdy = p_j2k->m_cp.tdy;
  9836. cstr_info->tw = p_j2k->m_cp.tw;
  9837. cstr_info->th = p_j2k->m_cp.th;
  9838. cstr_info->tile_info = NULL; /* Not fill from the main header*/
  9839. l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
  9840. cstr_info->m_default_tile_info.csty = l_default_tile->csty;
  9841. cstr_info->m_default_tile_info.prg = l_default_tile->prg;
  9842. cstr_info->m_default_tile_info.numlayers = l_default_tile->numlayers;
  9843. cstr_info->m_default_tile_info.mct = l_default_tile->mct;
  9844. cstr_info->m_default_tile_info.tccp_info = (opj_tccp_info_t*) opj_calloc(
  9845. cstr_info->nbcomps, sizeof(opj_tccp_info_t));
  9846. if (!cstr_info->m_default_tile_info.tccp_info) {
  9847. opj_destroy_cstr_info(&cstr_info);
  9848. return NULL;
  9849. }
  9850. for (compno = 0; compno < numcomps; compno++) {
  9851. opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
  9852. opj_tccp_info_t *l_tccp_info = &
  9853. (cstr_info->m_default_tile_info.tccp_info[compno]);
  9854. OPJ_INT32 bandno, numbands;
  9855. /* coding style*/
  9856. l_tccp_info->csty = l_tccp->csty;
  9857. l_tccp_info->numresolutions = l_tccp->numresolutions;
  9858. l_tccp_info->cblkw = l_tccp->cblkw;
  9859. l_tccp_info->cblkh = l_tccp->cblkh;
  9860. l_tccp_info->cblksty = l_tccp->cblksty;
  9861. l_tccp_info->qmfbid = l_tccp->qmfbid;
  9862. if (l_tccp->numresolutions < OPJ_J2K_MAXRLVLS) {
  9863. memcpy(l_tccp_info->prch, l_tccp->prch, l_tccp->numresolutions);
  9864. memcpy(l_tccp_info->prcw, l_tccp->prcw, l_tccp->numresolutions);
  9865. }
  9866. /* quantization style*/
  9867. l_tccp_info->qntsty = l_tccp->qntsty;
  9868. l_tccp_info->numgbits = l_tccp->numgbits;
  9869. numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 :
  9870. (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
  9871. if (numbands < OPJ_J2K_MAXBANDS) {
  9872. for (bandno = 0; bandno < numbands; bandno++) {
  9873. l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32)
  9874. l_tccp->stepsizes[bandno].mant;
  9875. l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32)
  9876. l_tccp->stepsizes[bandno].expn;
  9877. }
  9878. }
  9879. /* RGN value*/
  9880. l_tccp_info->roishift = l_tccp->roishift;
  9881. }
  9882. return cstr_info;
  9883. }
  9884. opj_codestream_index_t* j2k_get_cstr_index(opj_j2k_t* p_j2k)
  9885. {
  9886. opj_codestream_index_t* l_cstr_index = (opj_codestream_index_t*)
  9887. opj_calloc(1, sizeof(opj_codestream_index_t));
  9888. if (!l_cstr_index) {
  9889. return NULL;
  9890. }
  9891. l_cstr_index->main_head_start = p_j2k->cstr_index->main_head_start;
  9892. l_cstr_index->main_head_end = p_j2k->cstr_index->main_head_end;
  9893. l_cstr_index->codestream_size = p_j2k->cstr_index->codestream_size;
  9894. l_cstr_index->marknum = p_j2k->cstr_index->marknum;
  9895. l_cstr_index->marker = (opj_marker_info_t*)opj_malloc(l_cstr_index->marknum *
  9896. sizeof(opj_marker_info_t));
  9897. if (!l_cstr_index->marker) {
  9898. opj_free(l_cstr_index);
  9899. return NULL;
  9900. }
  9901. if (p_j2k->cstr_index->marker) {
  9902. memcpy(l_cstr_index->marker, p_j2k->cstr_index->marker,
  9903. l_cstr_index->marknum * sizeof(opj_marker_info_t));
  9904. } else {
  9905. opj_free(l_cstr_index->marker);
  9906. l_cstr_index->marker = NULL;
  9907. }
  9908. l_cstr_index->nb_of_tiles = p_j2k->cstr_index->nb_of_tiles;
  9909. l_cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(
  9910. l_cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
  9911. if (!l_cstr_index->tile_index) {
  9912. opj_free(l_cstr_index->marker);
  9913. opj_free(l_cstr_index);
  9914. return NULL;
  9915. }
  9916. if (!p_j2k->cstr_index->tile_index) {
  9917. opj_free(l_cstr_index->tile_index);
  9918. l_cstr_index->tile_index = NULL;
  9919. } else {
  9920. OPJ_UINT32 it_tile = 0;
  9921. for (it_tile = 0; it_tile < l_cstr_index->nb_of_tiles; it_tile++) {
  9922. /* Tile Marker*/
  9923. l_cstr_index->tile_index[it_tile].marknum =
  9924. p_j2k->cstr_index->tile_index[it_tile].marknum;
  9925. l_cstr_index->tile_index[it_tile].marker =
  9926. (opj_marker_info_t*)opj_malloc(l_cstr_index->tile_index[it_tile].marknum *
  9927. sizeof(opj_marker_info_t));
  9928. if (!l_cstr_index->tile_index[it_tile].marker) {
  9929. OPJ_UINT32 it_tile_free;
  9930. for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) {
  9931. opj_free(l_cstr_index->tile_index[it_tile_free].marker);
  9932. }
  9933. opj_free(l_cstr_index->tile_index);
  9934. opj_free(l_cstr_index->marker);
  9935. opj_free(l_cstr_index);
  9936. return NULL;
  9937. }
  9938. if (p_j2k->cstr_index->tile_index[it_tile].marker)
  9939. memcpy(l_cstr_index->tile_index[it_tile].marker,
  9940. p_j2k->cstr_index->tile_index[it_tile].marker,
  9941. l_cstr_index->tile_index[it_tile].marknum * sizeof(opj_marker_info_t));
  9942. else {
  9943. opj_free(l_cstr_index->tile_index[it_tile].marker);
  9944. l_cstr_index->tile_index[it_tile].marker = NULL;
  9945. }
  9946. /* Tile part index*/
  9947. l_cstr_index->tile_index[it_tile].nb_tps =
  9948. p_j2k->cstr_index->tile_index[it_tile].nb_tps;
  9949. l_cstr_index->tile_index[it_tile].tp_index =
  9950. (opj_tp_index_t*)opj_malloc(l_cstr_index->tile_index[it_tile].nb_tps * sizeof(
  9951. opj_tp_index_t));
  9952. if (!l_cstr_index->tile_index[it_tile].tp_index) {
  9953. OPJ_UINT32 it_tile_free;
  9954. for (it_tile_free = 0; it_tile_free < it_tile; it_tile_free++) {
  9955. opj_free(l_cstr_index->tile_index[it_tile_free].marker);
  9956. opj_free(l_cstr_index->tile_index[it_tile_free].tp_index);
  9957. }
  9958. opj_free(l_cstr_index->tile_index);
  9959. opj_free(l_cstr_index->marker);
  9960. opj_free(l_cstr_index);
  9961. return NULL;
  9962. }
  9963. if (p_j2k->cstr_index->tile_index[it_tile].tp_index) {
  9964. memcpy(l_cstr_index->tile_index[it_tile].tp_index,
  9965. p_j2k->cstr_index->tile_index[it_tile].tp_index,
  9966. l_cstr_index->tile_index[it_tile].nb_tps * sizeof(opj_tp_index_t));
  9967. } else {
  9968. opj_free(l_cstr_index->tile_index[it_tile].tp_index);
  9969. l_cstr_index->tile_index[it_tile].tp_index = NULL;
  9970. }
  9971. /* Packet index (NOT USED)*/
  9972. l_cstr_index->tile_index[it_tile].nb_packet = 0;
  9973. l_cstr_index->tile_index[it_tile].packet_index = NULL;
  9974. }
  9975. }
  9976. return l_cstr_index;
  9977. }
  9978. static OPJ_BOOL opj_j2k_allocate_tile_element_cstr_index(opj_j2k_t *p_j2k)
  9979. {
  9980. OPJ_UINT32 it_tile = 0;
  9981. p_j2k->cstr_index->nb_of_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
  9982. p_j2k->cstr_index->tile_index = (opj_tile_index_t*)opj_calloc(
  9983. p_j2k->cstr_index->nb_of_tiles, sizeof(opj_tile_index_t));
  9984. if (!p_j2k->cstr_index->tile_index) {
  9985. return OPJ_FALSE;
  9986. }
  9987. for (it_tile = 0; it_tile < p_j2k->cstr_index->nb_of_tiles; it_tile++) {
  9988. p_j2k->cstr_index->tile_index[it_tile].maxmarknum = 100;
  9989. p_j2k->cstr_index->tile_index[it_tile].marknum = 0;
  9990. p_j2k->cstr_index->tile_index[it_tile].marker = (opj_marker_info_t*)
  9991. opj_calloc(p_j2k->cstr_index->tile_index[it_tile].maxmarknum,
  9992. sizeof(opj_marker_info_t));
  9993. if (!p_j2k->cstr_index->tile_index[it_tile].marker) {
  9994. return OPJ_FALSE;
  9995. }
  9996. }
  9997. return OPJ_TRUE;
  9998. }
  9999. static OPJ_BOOL opj_j2k_are_all_used_components_decoded(opj_j2k_t *p_j2k,
  10000. opj_event_mgr_t * p_manager)
  10001. {
  10002. OPJ_UINT32 compno;
  10003. OPJ_BOOL decoded_all_used_components = OPJ_TRUE;
  10004. if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
  10005. for (compno = 0;
  10006. compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) {
  10007. OPJ_UINT32 dec_compno =
  10008. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno];
  10009. if (p_j2k->m_output_image->comps[dec_compno].data == NULL) {
  10010. opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n",
  10011. dec_compno);
  10012. decoded_all_used_components = OPJ_FALSE;
  10013. }
  10014. }
  10015. } else {
  10016. for (compno = 0; compno < p_j2k->m_output_image->numcomps; compno++) {
  10017. if (p_j2k->m_output_image->comps[compno].data == NULL) {
  10018. opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n",
  10019. compno);
  10020. decoded_all_used_components = OPJ_FALSE;
  10021. }
  10022. }
  10023. }
  10024. if (decoded_all_used_components == OPJ_FALSE) {
  10025. opj_event_msg(p_manager, EVT_ERROR, "Failed to decode all used components\n");
  10026. return OPJ_FALSE;
  10027. }
  10028. return OPJ_TRUE;
  10029. }
  10030. static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k,
  10031. opj_stream_private_t *p_stream,
  10032. opj_event_mgr_t * p_manager)
  10033. {
  10034. OPJ_BOOL l_go_on = OPJ_TRUE;
  10035. OPJ_UINT32 l_current_tile_no;
  10036. OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1;
  10037. OPJ_UINT32 l_nb_comps;
  10038. OPJ_UINT32 nr_tiles = 0;
  10039. /* Particular case for whole single tile decoding */
  10040. /* We can avoid allocating intermediate tile buffers */
  10041. if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
  10042. p_j2k->m_cp.tx0 == 0 && p_j2k->m_cp.ty0 == 0 &&
  10043. p_j2k->m_output_image->x0 == 0 &&
  10044. p_j2k->m_output_image->y0 == 0 &&
  10045. p_j2k->m_output_image->x1 == p_j2k->m_cp.tdx &&
  10046. p_j2k->m_output_image->y1 == p_j2k->m_cp.tdy) {
  10047. OPJ_UINT32 i;
  10048. if (! opj_j2k_read_tile_header(p_j2k,
  10049. &l_current_tile_no,
  10050. NULL,
  10051. &l_tile_x0, &l_tile_y0,
  10052. &l_tile_x1, &l_tile_y1,
  10053. &l_nb_comps,
  10054. &l_go_on,
  10055. p_stream,
  10056. p_manager)) {
  10057. return OPJ_FALSE;
  10058. }
  10059. if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0,
  10060. p_stream, p_manager)) {
  10061. opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile 1/1\n");
  10062. return OPJ_FALSE;
  10063. }
  10064. /* Transfer TCD data to output image data */
  10065. for (i = 0; i < p_j2k->m_output_image->numcomps; i++) {
  10066. opj_image_data_free(p_j2k->m_output_image->comps[i].data);
  10067. p_j2k->m_output_image->comps[i].data =
  10068. p_j2k->m_tcd->tcd_image->tiles->comps[i].data;
  10069. p_j2k->m_output_image->comps[i].resno_decoded =
  10070. p_j2k->m_tcd->image->comps[i].resno_decoded;
  10071. p_j2k->m_tcd->tcd_image->tiles->comps[i].data = NULL;
  10072. }
  10073. return OPJ_TRUE;
  10074. }
  10075. for (;;) {
  10076. if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
  10077. p_j2k->m_cp.tcps[0].m_data != NULL) {
  10078. l_current_tile_no = 0;
  10079. p_j2k->m_current_tile_number = 0;
  10080. p_j2k->m_specific_param.m_decoder.m_state |= J2K_STATE_DATA;
  10081. } else {
  10082. if (! opj_j2k_read_tile_header(p_j2k,
  10083. &l_current_tile_no,
  10084. NULL,
  10085. &l_tile_x0, &l_tile_y0,
  10086. &l_tile_x1, &l_tile_y1,
  10087. &l_nb_comps,
  10088. &l_go_on,
  10089. p_stream,
  10090. p_manager)) {
  10091. return OPJ_FALSE;
  10092. }
  10093. if (! l_go_on) {
  10094. break;
  10095. }
  10096. }
  10097. if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0,
  10098. p_stream, p_manager)) {
  10099. opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n",
  10100. l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
  10101. return OPJ_FALSE;
  10102. }
  10103. opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n",
  10104. l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
  10105. if (! opj_j2k_update_image_data(p_j2k->m_tcd,
  10106. p_j2k->m_output_image)) {
  10107. return OPJ_FALSE;
  10108. }
  10109. if (p_j2k->m_cp.tw == 1 && p_j2k->m_cp.th == 1 &&
  10110. !(p_j2k->m_output_image->x0 == p_j2k->m_private_image->x0 &&
  10111. p_j2k->m_output_image->y0 == p_j2k->m_private_image->y0 &&
  10112. p_j2k->m_output_image->x1 == p_j2k->m_private_image->x1 &&
  10113. p_j2k->m_output_image->y1 == p_j2k->m_private_image->y1)) {
  10114. /* Keep current tcp data */
  10115. } else {
  10116. opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]);
  10117. }
  10118. opj_event_msg(p_manager, EVT_INFO,
  10119. "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
  10120. if (opj_stream_get_number_byte_left(p_stream) == 0
  10121. && p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC) {
  10122. break;
  10123. }
  10124. if (++nr_tiles == p_j2k->m_cp.th * p_j2k->m_cp.tw) {
  10125. break;
  10126. }
  10127. }
  10128. if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) {
  10129. return OPJ_FALSE;
  10130. }
  10131. return OPJ_TRUE;
  10132. }
  10133. /**
  10134. * Sets up the procedures to do on decoding data. Developers wanting to extend the library can add their own reading procedures.
  10135. */
  10136. static OPJ_BOOL opj_j2k_setup_decoding(opj_j2k_t *p_j2k,
  10137. opj_event_mgr_t * p_manager)
  10138. {
  10139. /* preconditions*/
  10140. assert(p_j2k != 00);
  10141. assert(p_manager != 00);
  10142. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10143. (opj_procedure)opj_j2k_decode_tiles, p_manager)) {
  10144. return OPJ_FALSE;
  10145. }
  10146. /* DEVELOPER CORNER, add your custom procedures */
  10147. return OPJ_TRUE;
  10148. }
  10149. /*
  10150. * Read and decode one tile.
  10151. */
  10152. static OPJ_BOOL opj_j2k_decode_one_tile(opj_j2k_t *p_j2k,
  10153. opj_stream_private_t *p_stream,
  10154. opj_event_mgr_t * p_manager)
  10155. {
  10156. OPJ_BOOL l_go_on = OPJ_TRUE;
  10157. OPJ_UINT32 l_current_tile_no;
  10158. OPJ_UINT32 l_tile_no_to_dec;
  10159. OPJ_INT32 l_tile_x0, l_tile_y0, l_tile_x1, l_tile_y1;
  10160. OPJ_UINT32 l_nb_comps;
  10161. OPJ_UINT32 l_nb_tiles;
  10162. OPJ_UINT32 i;
  10163. /*Allocate and initialize some elements of codestrem index if not already done*/
  10164. if (!p_j2k->cstr_index->tile_index) {
  10165. if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) {
  10166. return OPJ_FALSE;
  10167. }
  10168. }
  10169. /* Move into the codestream to the first SOT used to decode the desired tile */
  10170. l_tile_no_to_dec = (OPJ_UINT32)
  10171. p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
  10172. if (p_j2k->cstr_index->tile_index)
  10173. if (p_j2k->cstr_index->tile_index->tp_index) {
  10174. if (! p_j2k->cstr_index->tile_index[l_tile_no_to_dec].nb_tps) {
  10175. /* the index for this tile has not been built,
  10176. * so move to the last SOT read */
  10177. if (!(opj_stream_read_seek(p_stream,
  10178. p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos + 2, p_manager))) {
  10179. opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
  10180. return OPJ_FALSE;
  10181. }
  10182. } else {
  10183. if (!(opj_stream_read_seek(p_stream,
  10184. p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos + 2,
  10185. p_manager))) {
  10186. opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
  10187. return OPJ_FALSE;
  10188. }
  10189. }
  10190. /* Special case if we have previously read the EOC marker (if the previous tile getted is the last ) */
  10191. if (p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_EOC) {
  10192. p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
  10193. }
  10194. }
  10195. /* Reset current tile part number for all tiles, and not only the one */
  10196. /* of interest. */
  10197. /* Not completely sure this is always correct but required for */
  10198. /* ./build/bin/j2k_random_tile_access ./build/tests/tte1.j2k */
  10199. l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
  10200. for (i = 0; i < l_nb_tiles; ++i) {
  10201. p_j2k->m_cp.tcps[i].m_current_tile_part_number = -1;
  10202. }
  10203. for (;;) {
  10204. if (! opj_j2k_read_tile_header(p_j2k,
  10205. &l_current_tile_no,
  10206. NULL,
  10207. &l_tile_x0, &l_tile_y0,
  10208. &l_tile_x1, &l_tile_y1,
  10209. &l_nb_comps,
  10210. &l_go_on,
  10211. p_stream,
  10212. p_manager)) {
  10213. return OPJ_FALSE;
  10214. }
  10215. if (! l_go_on) {
  10216. break;
  10217. }
  10218. if (! opj_j2k_decode_tile(p_j2k, l_current_tile_no, NULL, 0,
  10219. p_stream, p_manager)) {
  10220. return OPJ_FALSE;
  10221. }
  10222. opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n",
  10223. l_current_tile_no + 1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
  10224. if (! opj_j2k_update_image_data(p_j2k->m_tcd,
  10225. p_j2k->m_output_image)) {
  10226. return OPJ_FALSE;
  10227. }
  10228. opj_j2k_tcp_data_destroy(&p_j2k->m_cp.tcps[l_current_tile_no]);
  10229. opj_event_msg(p_manager, EVT_INFO,
  10230. "Image data has been updated with tile %d.\n\n", l_current_tile_no + 1);
  10231. if (l_current_tile_no == l_tile_no_to_dec) {
  10232. /* move into the codestream to the first SOT (FIXME or not move?)*/
  10233. if (!(opj_stream_read_seek(p_stream, p_j2k->cstr_index->main_head_end + 2,
  10234. p_manager))) {
  10235. opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
  10236. return OPJ_FALSE;
  10237. }
  10238. break;
  10239. } else {
  10240. opj_event_msg(p_manager, EVT_WARNING,
  10241. "Tile read, decoded and updated is not the desired one (%d vs %d).\n",
  10242. l_current_tile_no + 1, l_tile_no_to_dec + 1);
  10243. }
  10244. }
  10245. if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) {
  10246. return OPJ_FALSE;
  10247. }
  10248. return OPJ_TRUE;
  10249. }
  10250. /**
  10251. * Sets up the procedures to do on decoding one tile. Developers wanting to extend the library can add their own reading procedures.
  10252. */
  10253. static OPJ_BOOL opj_j2k_setup_decoding_tile(opj_j2k_t *p_j2k,
  10254. opj_event_mgr_t * p_manager)
  10255. {
  10256. /* preconditions*/
  10257. assert(p_j2k != 00);
  10258. assert(p_manager != 00);
  10259. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10260. (opj_procedure)opj_j2k_decode_one_tile, p_manager)) {
  10261. return OPJ_FALSE;
  10262. }
  10263. /* DEVELOPER CORNER, add your custom procedures */
  10264. return OPJ_TRUE;
  10265. }
  10266. static OPJ_BOOL opj_j2k_move_data_from_codec_to_output_image(opj_j2k_t * p_j2k,
  10267. opj_image_t * p_image)
  10268. {
  10269. OPJ_UINT32 compno;
  10270. /* Move data and copy one information from codec to output image*/
  10271. if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode > 0) {
  10272. opj_image_comp_t* newcomps =
  10273. (opj_image_comp_t*) opj_malloc(
  10274. p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode *
  10275. sizeof(opj_image_comp_t));
  10276. if (newcomps == NULL) {
  10277. opj_image_destroy(p_j2k->m_private_image);
  10278. p_j2k->m_private_image = NULL;
  10279. return OPJ_FALSE;
  10280. }
  10281. for (compno = 0; compno < p_image->numcomps; compno++) {
  10282. opj_image_data_free(p_image->comps[compno].data);
  10283. p_image->comps[compno].data = NULL;
  10284. }
  10285. for (compno = 0;
  10286. compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) {
  10287. OPJ_UINT32 src_compno =
  10288. p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno];
  10289. memcpy(&(newcomps[compno]),
  10290. &(p_j2k->m_output_image->comps[src_compno]),
  10291. sizeof(opj_image_comp_t));
  10292. newcomps[compno].resno_decoded =
  10293. p_j2k->m_output_image->comps[src_compno].resno_decoded;
  10294. newcomps[compno].data = p_j2k->m_output_image->comps[src_compno].data;
  10295. p_j2k->m_output_image->comps[src_compno].data = NULL;
  10296. }
  10297. for (compno = 0; compno < p_image->numcomps; compno++) {
  10298. assert(p_j2k->m_output_image->comps[compno].data == NULL);
  10299. opj_image_data_free(p_j2k->m_output_image->comps[compno].data);
  10300. p_j2k->m_output_image->comps[compno].data = NULL;
  10301. }
  10302. p_image->numcomps = p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode;
  10303. opj_free(p_image->comps);
  10304. p_image->comps = newcomps;
  10305. } else {
  10306. for (compno = 0; compno < p_image->numcomps; compno++) {
  10307. p_image->comps[compno].resno_decoded =
  10308. p_j2k->m_output_image->comps[compno].resno_decoded;
  10309. opj_image_data_free(p_image->comps[compno].data);
  10310. p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
  10311. #if 0
  10312. char fn[256];
  10313. snprintf(fn, sizeof fn, "/tmp/%d.raw", compno);
  10314. FILE *debug = fopen(fn, "wb");
  10315. fwrite(p_image->comps[compno].data, sizeof(OPJ_INT32),
  10316. p_image->comps[compno].w * p_image->comps[compno].h, debug);
  10317. fclose(debug);
  10318. #endif
  10319. p_j2k->m_output_image->comps[compno].data = NULL;
  10320. }
  10321. }
  10322. return OPJ_TRUE;
  10323. }
  10324. OPJ_BOOL opj_j2k_decode(opj_j2k_t * p_j2k,
  10325. opj_stream_private_t * p_stream,
  10326. opj_image_t * p_image,
  10327. opj_event_mgr_t * p_manager)
  10328. {
  10329. if (!p_image) {
  10330. return OPJ_FALSE;
  10331. }
  10332. /* Heuristics to detect sequence opj_read_header(), opj_set_decoded_resolution_factor() */
  10333. /* and finally opj_decode_image() without manual setting of comps[].factor */
  10334. /* We could potentially always execute it, if we don't allow people to do */
  10335. /* opj_read_header(), modify x0,y0,x1,y1 of returned image an call opj_decode_image() */
  10336. if (p_j2k->m_cp.m_specific_param.m_dec.m_reduce > 0 &&
  10337. p_j2k->m_private_image != NULL &&
  10338. p_j2k->m_private_image->numcomps > 0 &&
  10339. p_j2k->m_private_image->comps[0].factor ==
  10340. p_j2k->m_cp.m_specific_param.m_dec.m_reduce &&
  10341. p_image->numcomps > 0 &&
  10342. p_image->comps[0].factor == 0 &&
  10343. /* Don't mess with image dimension if the user has allocated it */
  10344. p_image->comps[0].data == NULL) {
  10345. OPJ_UINT32 it_comp;
  10346. /* Update the comps[].factor member of the output image with the one */
  10347. /* of m_reduce */
  10348. for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
  10349. p_image->comps[it_comp].factor = p_j2k->m_cp.m_specific_param.m_dec.m_reduce;
  10350. }
  10351. if (!opj_j2k_update_image_dimensions(p_image, p_manager)) {
  10352. return OPJ_FALSE;
  10353. }
  10354. }
  10355. if (p_j2k->m_output_image == NULL) {
  10356. p_j2k->m_output_image = opj_image_create0();
  10357. if (!(p_j2k->m_output_image)) {
  10358. return OPJ_FALSE;
  10359. }
  10360. }
  10361. opj_copy_image_header(p_image, p_j2k->m_output_image);
  10362. /* customization of the decoding */
  10363. if (!opj_j2k_setup_decoding(p_j2k, p_manager)) {
  10364. return OPJ_FALSE;
  10365. }
  10366. /* Decode the codestream */
  10367. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  10368. opj_image_destroy(p_j2k->m_private_image);
  10369. p_j2k->m_private_image = NULL;
  10370. return OPJ_FALSE;
  10371. }
  10372. /* Move data and copy one information from codec to output image*/
  10373. return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image);
  10374. }
  10375. OPJ_BOOL opj_j2k_get_tile(opj_j2k_t *p_j2k,
  10376. opj_stream_private_t *p_stream,
  10377. opj_image_t* p_image,
  10378. opj_event_mgr_t * p_manager,
  10379. OPJ_UINT32 tile_index)
  10380. {
  10381. OPJ_UINT32 compno;
  10382. OPJ_UINT32 l_tile_x, l_tile_y;
  10383. opj_image_comp_t* l_img_comp;
  10384. if (!p_image) {
  10385. opj_event_msg(p_manager, EVT_ERROR, "We need an image previously created.\n");
  10386. return OPJ_FALSE;
  10387. }
  10388. if (p_image->numcomps < p_j2k->m_private_image->numcomps) {
  10389. opj_event_msg(p_manager, EVT_ERROR,
  10390. "Image has less components than codestream.\n");
  10391. return OPJ_FALSE;
  10392. }
  10393. if (/*(tile_index < 0) &&*/ (tile_index >= p_j2k->m_cp.tw * p_j2k->m_cp.th)) {
  10394. opj_event_msg(p_manager, EVT_ERROR,
  10395. "Tile index provided by the user is incorrect %d (max = %d) \n", tile_index,
  10396. (p_j2k->m_cp.tw * p_j2k->m_cp.th) - 1);
  10397. return OPJ_FALSE;
  10398. }
  10399. /* Compute the dimension of the desired tile*/
  10400. l_tile_x = tile_index % p_j2k->m_cp.tw;
  10401. l_tile_y = tile_index / p_j2k->m_cp.tw;
  10402. p_image->x0 = l_tile_x * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
  10403. if (p_image->x0 < p_j2k->m_private_image->x0) {
  10404. p_image->x0 = p_j2k->m_private_image->x0;
  10405. }
  10406. p_image->x1 = (l_tile_x + 1) * p_j2k->m_cp.tdx + p_j2k->m_cp.tx0;
  10407. if (p_image->x1 > p_j2k->m_private_image->x1) {
  10408. p_image->x1 = p_j2k->m_private_image->x1;
  10409. }
  10410. p_image->y0 = l_tile_y * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
  10411. if (p_image->y0 < p_j2k->m_private_image->y0) {
  10412. p_image->y0 = p_j2k->m_private_image->y0;
  10413. }
  10414. p_image->y1 = (l_tile_y + 1) * p_j2k->m_cp.tdy + p_j2k->m_cp.ty0;
  10415. if (p_image->y1 > p_j2k->m_private_image->y1) {
  10416. p_image->y1 = p_j2k->m_private_image->y1;
  10417. }
  10418. l_img_comp = p_image->comps;
  10419. for (compno = 0; compno < p_j2k->m_private_image->numcomps; ++compno) {
  10420. OPJ_INT32 l_comp_x1, l_comp_y1;
  10421. l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
  10422. l_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx);
  10423. l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, l_img_comp->dy);
  10424. l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
  10425. l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
  10426. l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1,
  10427. (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0,
  10428. (OPJ_INT32)l_img_comp->factor));
  10429. l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1,
  10430. (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0,
  10431. (OPJ_INT32)l_img_comp->factor));
  10432. l_img_comp++;
  10433. }
  10434. if (p_image->numcomps > p_j2k->m_private_image->numcomps) {
  10435. /* Can happen when calling repeatdly opj_get_decoded_tile() on an
  10436. * image with a color palette, where color palette expansion is done
  10437. * later in jp2.c */
  10438. for (compno = p_j2k->m_private_image->numcomps; compno < p_image->numcomps;
  10439. ++compno) {
  10440. opj_image_data_free(p_image->comps[compno].data);
  10441. p_image->comps[compno].data = NULL;
  10442. }
  10443. p_image->numcomps = p_j2k->m_private_image->numcomps;
  10444. }
  10445. /* Destroy the previous output image*/
  10446. if (p_j2k->m_output_image) {
  10447. opj_image_destroy(p_j2k->m_output_image);
  10448. }
  10449. /* Create the output image from the information previously computed*/
  10450. p_j2k->m_output_image = opj_image_create0();
  10451. if (!(p_j2k->m_output_image)) {
  10452. return OPJ_FALSE;
  10453. }
  10454. opj_copy_image_header(p_image, p_j2k->m_output_image);
  10455. p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index;
  10456. /* customization of the decoding */
  10457. if (!opj_j2k_setup_decoding_tile(p_j2k, p_manager)) {
  10458. return OPJ_FALSE;
  10459. }
  10460. /* Decode the codestream */
  10461. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  10462. opj_image_destroy(p_j2k->m_private_image);
  10463. p_j2k->m_private_image = NULL;
  10464. return OPJ_FALSE;
  10465. }
  10466. /* Move data and copy one information from codec to output image*/
  10467. return opj_j2k_move_data_from_codec_to_output_image(p_j2k, p_image);
  10468. }
  10469. OPJ_BOOL opj_j2k_set_decoded_resolution_factor(opj_j2k_t *p_j2k,
  10470. OPJ_UINT32 res_factor,
  10471. opj_event_mgr_t * p_manager)
  10472. {
  10473. OPJ_UINT32 it_comp;
  10474. p_j2k->m_cp.m_specific_param.m_dec.m_reduce = res_factor;
  10475. if (p_j2k->m_private_image) {
  10476. if (p_j2k->m_private_image->comps) {
  10477. if (p_j2k->m_specific_param.m_decoder.m_default_tcp) {
  10478. if (p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps) {
  10479. for (it_comp = 0 ; it_comp < p_j2k->m_private_image->numcomps; it_comp++) {
  10480. OPJ_UINT32 max_res =
  10481. p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps[it_comp].numresolutions;
  10482. if (res_factor >= max_res) {
  10483. opj_event_msg(p_manager, EVT_ERROR,
  10484. "Resolution factor is greater than the maximum resolution in the component.\n");
  10485. return OPJ_FALSE;
  10486. }
  10487. p_j2k->m_private_image->comps[it_comp].factor = res_factor;
  10488. }
  10489. return OPJ_TRUE;
  10490. }
  10491. }
  10492. }
  10493. }
  10494. return OPJ_FALSE;
  10495. }
  10496. /* ----------------------------------------------------------------------- */
  10497. OPJ_BOOL opj_j2k_encoder_set_extra_options(
  10498. opj_j2k_t *p_j2k,
  10499. const char* const* p_options,
  10500. opj_event_mgr_t * p_manager)
  10501. {
  10502. const char* const* p_option_iter;
  10503. if (p_options == NULL) {
  10504. return OPJ_TRUE;
  10505. }
  10506. for (p_option_iter = p_options; *p_option_iter != NULL; ++p_option_iter) {
  10507. if (strncmp(*p_option_iter, "PLT=", 4) == 0) {
  10508. if (strcmp(*p_option_iter, "PLT=YES") == 0) {
  10509. p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_TRUE;
  10510. } else if (strcmp(*p_option_iter, "PLT=NO") == 0) {
  10511. p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_FALSE;
  10512. } else {
  10513. opj_event_msg(p_manager, EVT_ERROR,
  10514. "Invalid value for option: %s.\n", *p_option_iter);
  10515. return OPJ_FALSE;
  10516. }
  10517. } else if (strncmp(*p_option_iter, "TLM=", 4) == 0) {
  10518. if (strcmp(*p_option_iter, "TLM=YES") == 0) {
  10519. p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE;
  10520. } else if (strcmp(*p_option_iter, "TLM=NO") == 0) {
  10521. p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_FALSE;
  10522. } else {
  10523. opj_event_msg(p_manager, EVT_ERROR,
  10524. "Invalid value for option: %s.\n", *p_option_iter);
  10525. return OPJ_FALSE;
  10526. }
  10527. } else if (strncmp(*p_option_iter, "GUARD_BITS=", strlen("GUARD_BITS=")) == 0) {
  10528. OPJ_UINT32 tileno;
  10529. opj_cp_t *cp = cp = &(p_j2k->m_cp);
  10530. int numgbits = atoi(*p_option_iter + strlen("GUARD_BITS="));
  10531. if (numgbits < 0 || numgbits > 7) {
  10532. opj_event_msg(p_manager, EVT_ERROR,
  10533. "Invalid value for option: %s. Should be in [0,7]\n", *p_option_iter);
  10534. return OPJ_FALSE;
  10535. }
  10536. for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
  10537. OPJ_UINT32 i;
  10538. opj_tcp_t *tcp = &cp->tcps[tileno];
  10539. for (i = 0; i < p_j2k->m_specific_param.m_encoder.m_nb_comps; i++) {
  10540. opj_tccp_t *tccp = &tcp->tccps[i];
  10541. tccp->numgbits = (OPJ_UINT32)numgbits;
  10542. }
  10543. }
  10544. } else {
  10545. opj_event_msg(p_manager, EVT_ERROR,
  10546. "Invalid option: %s.\n", *p_option_iter);
  10547. return OPJ_FALSE;
  10548. }
  10549. }
  10550. return OPJ_TRUE;
  10551. }
  10552. /* ----------------------------------------------------------------------- */
  10553. OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
  10554. opj_stream_private_t *p_stream,
  10555. opj_event_mgr_t * p_manager)
  10556. {
  10557. OPJ_UINT32 i, j;
  10558. OPJ_UINT32 l_nb_tiles;
  10559. OPJ_SIZE_T l_max_tile_size = 0, l_current_tile_size;
  10560. OPJ_BYTE * l_current_data = 00;
  10561. OPJ_BOOL l_reuse_data = OPJ_FALSE;
  10562. opj_tcd_t* p_tcd = 00;
  10563. /* preconditions */
  10564. assert(p_j2k != 00);
  10565. assert(p_stream != 00);
  10566. assert(p_manager != 00);
  10567. p_tcd = p_j2k->m_tcd;
  10568. l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
  10569. if (l_nb_tiles == 1) {
  10570. l_reuse_data = OPJ_TRUE;
  10571. #ifdef __SSE__
  10572. for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) {
  10573. opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
  10574. if (((size_t)l_img_comp->data & 0xFU) !=
  10575. 0U) { /* tile data shall be aligned on 16 bytes */
  10576. l_reuse_data = OPJ_FALSE;
  10577. }
  10578. }
  10579. #endif
  10580. }
  10581. for (i = 0; i < l_nb_tiles; ++i) {
  10582. if (! opj_j2k_pre_write_tile(p_j2k, i, p_stream, p_manager)) {
  10583. if (l_current_data) {
  10584. opj_free(l_current_data);
  10585. }
  10586. return OPJ_FALSE;
  10587. }
  10588. /* if we only have one tile, then simply set tile component data equal to image component data */
  10589. /* otherwise, allocate the data */
  10590. for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) {
  10591. opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j;
  10592. if (l_reuse_data) {
  10593. opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
  10594. l_tilec->data = l_img_comp->data;
  10595. l_tilec->ownsData = OPJ_FALSE;
  10596. } else {
  10597. if (! opj_alloc_tile_component_data(l_tilec)) {
  10598. opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data.");
  10599. if (l_current_data) {
  10600. opj_free(l_current_data);
  10601. }
  10602. return OPJ_FALSE;
  10603. }
  10604. }
  10605. }
  10606. l_current_tile_size = opj_tcd_get_encoder_input_buffer_size(p_j2k->m_tcd);
  10607. if (!l_reuse_data) {
  10608. if (l_current_tile_size > l_max_tile_size) {
  10609. OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data,
  10610. l_current_tile_size);
  10611. if (! l_new_current_data) {
  10612. if (l_current_data) {
  10613. opj_free(l_current_data);
  10614. }
  10615. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
  10616. return OPJ_FALSE;
  10617. }
  10618. l_current_data = l_new_current_data;
  10619. l_max_tile_size = l_current_tile_size;
  10620. }
  10621. if (l_current_data == NULL) {
  10622. /* Should not happen in practice, but will avoid Coverity to */
  10623. /* complain about a null pointer dereference */
  10624. assert(0);
  10625. return OPJ_FALSE;
  10626. }
  10627. /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */
  10628. /* 32 bit components @ 8 bit precision get converted to 8 bit */
  10629. /* 32 bit components @ 16 bit precision get converted to 16 bit */
  10630. opj_j2k_get_tile_data(p_j2k->m_tcd, l_current_data);
  10631. /* now copy this data into the tile component */
  10632. if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, l_current_data,
  10633. l_current_tile_size)) {
  10634. opj_event_msg(p_manager, EVT_ERROR,
  10635. "Size mismatch between tile data and sent data.");
  10636. opj_free(l_current_data);
  10637. return OPJ_FALSE;
  10638. }
  10639. }
  10640. if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) {
  10641. if (l_current_data) {
  10642. opj_free(l_current_data);
  10643. }
  10644. return OPJ_FALSE;
  10645. }
  10646. }
  10647. if (l_current_data) {
  10648. opj_free(l_current_data);
  10649. }
  10650. return OPJ_TRUE;
  10651. }
  10652. OPJ_BOOL opj_j2k_end_compress(opj_j2k_t *p_j2k,
  10653. opj_stream_private_t *p_stream,
  10654. opj_event_mgr_t * p_manager)
  10655. {
  10656. /* customization of the encoding */
  10657. if (! opj_j2k_setup_end_compress(p_j2k, p_manager)) {
  10658. return OPJ_FALSE;
  10659. }
  10660. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  10661. return OPJ_FALSE;
  10662. }
  10663. return OPJ_TRUE;
  10664. }
  10665. OPJ_BOOL opj_j2k_start_compress(opj_j2k_t *p_j2k,
  10666. opj_stream_private_t *p_stream,
  10667. opj_image_t * p_image,
  10668. opj_event_mgr_t * p_manager)
  10669. {
  10670. /* preconditions */
  10671. assert(p_j2k != 00);
  10672. assert(p_stream != 00);
  10673. assert(p_manager != 00);
  10674. p_j2k->m_private_image = opj_image_create0();
  10675. if (! p_j2k->m_private_image) {
  10676. opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header.");
  10677. return OPJ_FALSE;
  10678. }
  10679. opj_copy_image_header(p_image, p_j2k->m_private_image);
  10680. /* TODO_MSD: Find a better way */
  10681. if (p_image->comps) {
  10682. OPJ_UINT32 it_comp;
  10683. for (it_comp = 0 ; it_comp < p_image->numcomps; it_comp++) {
  10684. if (p_image->comps[it_comp].data) {
  10685. p_j2k->m_private_image->comps[it_comp].data = p_image->comps[it_comp].data;
  10686. p_image->comps[it_comp].data = NULL;
  10687. }
  10688. }
  10689. }
  10690. /* customization of the validation */
  10691. if (! opj_j2k_setup_encoding_validation(p_j2k, p_manager)) {
  10692. return OPJ_FALSE;
  10693. }
  10694. /* validation of the parameters codec */
  10695. if (! opj_j2k_exec(p_j2k, p_j2k->m_validation_list, p_stream, p_manager)) {
  10696. return OPJ_FALSE;
  10697. }
  10698. /* customization of the encoding */
  10699. if (! opj_j2k_setup_header_writing(p_j2k, p_manager)) {
  10700. return OPJ_FALSE;
  10701. }
  10702. /* write header */
  10703. if (! opj_j2k_exec(p_j2k, p_j2k->m_procedure_list, p_stream, p_manager)) {
  10704. return OPJ_FALSE;
  10705. }
  10706. return OPJ_TRUE;
  10707. }
  10708. static OPJ_BOOL opj_j2k_pre_write_tile(opj_j2k_t * p_j2k,
  10709. OPJ_UINT32 p_tile_index,
  10710. opj_stream_private_t *p_stream,
  10711. opj_event_mgr_t * p_manager)
  10712. {
  10713. (void)p_stream;
  10714. if (p_tile_index != p_j2k->m_current_tile_number) {
  10715. opj_event_msg(p_manager, EVT_ERROR, "The given tile index does not match.");
  10716. return OPJ_FALSE;
  10717. }
  10718. opj_event_msg(p_manager, EVT_INFO, "tile number %d / %d\n",
  10719. p_j2k->m_current_tile_number + 1, p_j2k->m_cp.tw * p_j2k->m_cp.th);
  10720. p_j2k->m_specific_param.m_encoder.m_current_tile_part_number = 0;
  10721. p_j2k->m_tcd->cur_totnum_tp = p_j2k->m_cp.tcps[p_tile_index].m_nb_tile_parts;
  10722. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
  10723. /* initialisation before tile encoding */
  10724. if (! opj_tcd_init_encode_tile(p_j2k->m_tcd, p_j2k->m_current_tile_number,
  10725. p_manager)) {
  10726. return OPJ_FALSE;
  10727. }
  10728. return OPJ_TRUE;
  10729. }
  10730. static void opj_get_tile_dimensions(opj_image_t * l_image,
  10731. opj_tcd_tilecomp_t * l_tilec,
  10732. opj_image_comp_t * l_img_comp,
  10733. OPJ_UINT32* l_size_comp,
  10734. OPJ_UINT32* l_width,
  10735. OPJ_UINT32* l_height,
  10736. OPJ_UINT32* l_offset_x,
  10737. OPJ_UINT32* l_offset_y,
  10738. OPJ_UINT32* l_image_width,
  10739. OPJ_UINT32* l_stride,
  10740. OPJ_UINT32* l_tile_offset)
  10741. {
  10742. OPJ_UINT32 l_remaining;
  10743. *l_size_comp = l_img_comp->prec >> 3; /* (/8) */
  10744. l_remaining = l_img_comp->prec & 7; /* (%8) */
  10745. if (l_remaining) {
  10746. *l_size_comp += 1;
  10747. }
  10748. if (*l_size_comp == 3) {
  10749. *l_size_comp = 4;
  10750. }
  10751. *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
  10752. *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
  10753. *l_offset_x = opj_uint_ceildiv(l_image->x0, l_img_comp->dx);
  10754. *l_offset_y = opj_uint_ceildiv(l_image->y0, l_img_comp->dy);
  10755. *l_image_width = opj_uint_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
  10756. *l_stride = *l_image_width - *l_width;
  10757. *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + ((
  10758. OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width;
  10759. }
  10760. static void opj_j2k_get_tile_data(opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
  10761. {
  10762. OPJ_UINT32 i, j, k = 0;
  10763. for (i = 0; i < p_tcd->image->numcomps; ++i) {
  10764. opj_image_t * l_image = p_tcd->image;
  10765. OPJ_INT32 * l_src_ptr;
  10766. opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i;
  10767. opj_image_comp_t * l_img_comp = l_image->comps + i;
  10768. OPJ_UINT32 l_size_comp, l_width, l_height, l_offset_x, l_offset_y,
  10769. l_image_width, l_stride, l_tile_offset;
  10770. opj_get_tile_dimensions(l_image,
  10771. l_tilec,
  10772. l_img_comp,
  10773. &l_size_comp,
  10774. &l_width,
  10775. &l_height,
  10776. &l_offset_x,
  10777. &l_offset_y,
  10778. &l_image_width,
  10779. &l_stride,
  10780. &l_tile_offset);
  10781. l_src_ptr = l_img_comp->data + l_tile_offset;
  10782. switch (l_size_comp) {
  10783. case 1: {
  10784. OPJ_CHAR * l_dest_ptr = (OPJ_CHAR*) p_data;
  10785. if (l_img_comp->sgnd) {
  10786. for (j = 0; j < l_height; ++j) {
  10787. for (k = 0; k < l_width; ++k) {
  10788. *(l_dest_ptr) = (OPJ_CHAR)(*l_src_ptr);
  10789. ++l_dest_ptr;
  10790. ++l_src_ptr;
  10791. }
  10792. l_src_ptr += l_stride;
  10793. }
  10794. } else {
  10795. for (j = 0; j < l_height; ++j) {
  10796. for (k = 0; k < l_width; ++k) {
  10797. *(l_dest_ptr) = (OPJ_CHAR)((*l_src_ptr) & 0xff);
  10798. ++l_dest_ptr;
  10799. ++l_src_ptr;
  10800. }
  10801. l_src_ptr += l_stride;
  10802. }
  10803. }
  10804. p_data = (OPJ_BYTE*) l_dest_ptr;
  10805. }
  10806. break;
  10807. case 2: {
  10808. OPJ_INT16 * l_dest_ptr = (OPJ_INT16 *) p_data;
  10809. if (l_img_comp->sgnd) {
  10810. for (j = 0; j < l_height; ++j) {
  10811. for (k = 0; k < l_width; ++k) {
  10812. *(l_dest_ptr++) = (OPJ_INT16)(*(l_src_ptr++));
  10813. }
  10814. l_src_ptr += l_stride;
  10815. }
  10816. } else {
  10817. for (j = 0; j < l_height; ++j) {
  10818. for (k = 0; k < l_width; ++k) {
  10819. *(l_dest_ptr++) = (OPJ_INT16)((*(l_src_ptr++)) & 0xffff);
  10820. }
  10821. l_src_ptr += l_stride;
  10822. }
  10823. }
  10824. p_data = (OPJ_BYTE*) l_dest_ptr;
  10825. }
  10826. break;
  10827. case 4: {
  10828. OPJ_INT32 * l_dest_ptr = (OPJ_INT32 *) p_data;
  10829. for (j = 0; j < l_height; ++j) {
  10830. for (k = 0; k < l_width; ++k) {
  10831. *(l_dest_ptr++) = *(l_src_ptr++);
  10832. }
  10833. l_src_ptr += l_stride;
  10834. }
  10835. p_data = (OPJ_BYTE*) l_dest_ptr;
  10836. }
  10837. break;
  10838. }
  10839. }
  10840. }
  10841. static OPJ_BOOL opj_j2k_post_write_tile(opj_j2k_t * p_j2k,
  10842. opj_stream_private_t *p_stream,
  10843. opj_event_mgr_t * p_manager)
  10844. {
  10845. OPJ_UINT32 l_nb_bytes_written;
  10846. OPJ_BYTE * l_current_data = 00;
  10847. OPJ_UINT32 l_tile_size = 0;
  10848. OPJ_UINT32 l_available_data;
  10849. /* preconditions */
  10850. assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
  10851. l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
  10852. l_available_data = l_tile_size;
  10853. l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
  10854. l_nb_bytes_written = 0;
  10855. if (! opj_j2k_write_first_tile_part(p_j2k, l_current_data, &l_nb_bytes_written,
  10856. l_available_data, p_stream, p_manager)) {
  10857. return OPJ_FALSE;
  10858. }
  10859. l_current_data += l_nb_bytes_written;
  10860. l_available_data -= l_nb_bytes_written;
  10861. l_nb_bytes_written = 0;
  10862. if (! opj_j2k_write_all_tile_parts(p_j2k, l_current_data, &l_nb_bytes_written,
  10863. l_available_data, p_stream, p_manager)) {
  10864. return OPJ_FALSE;
  10865. }
  10866. l_available_data -= l_nb_bytes_written;
  10867. l_nb_bytes_written = l_tile_size - l_available_data;
  10868. if (opj_stream_write_data(p_stream,
  10869. p_j2k->m_specific_param.m_encoder.m_encoded_tile_data,
  10870. l_nb_bytes_written, p_manager) != l_nb_bytes_written) {
  10871. return OPJ_FALSE;
  10872. }
  10873. ++p_j2k->m_current_tile_number;
  10874. return OPJ_TRUE;
  10875. }
  10876. static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k,
  10877. opj_event_mgr_t * p_manager)
  10878. {
  10879. /* preconditions */
  10880. assert(p_j2k != 00);
  10881. assert(p_manager != 00);
  10882. /* DEVELOPER CORNER, insert your custom procedures */
  10883. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10884. (opj_procedure)opj_j2k_write_eoc, p_manager)) {
  10885. return OPJ_FALSE;
  10886. }
  10887. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  10888. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10889. (opj_procedure)opj_j2k_write_updated_tlm, p_manager)) {
  10890. return OPJ_FALSE;
  10891. }
  10892. }
  10893. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10894. (opj_procedure)opj_j2k_write_epc, p_manager)) {
  10895. return OPJ_FALSE;
  10896. }
  10897. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10898. (opj_procedure)opj_j2k_end_encoding, p_manager)) {
  10899. return OPJ_FALSE;
  10900. }
  10901. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10902. (opj_procedure)opj_j2k_destroy_header_memory, p_manager)) {
  10903. return OPJ_FALSE;
  10904. }
  10905. return OPJ_TRUE;
  10906. }
  10907. static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k,
  10908. opj_event_mgr_t * p_manager)
  10909. {
  10910. /* preconditions */
  10911. assert(p_j2k != 00);
  10912. assert(p_manager != 00);
  10913. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  10914. (opj_procedure)opj_j2k_build_encoder, p_manager)) {
  10915. return OPJ_FALSE;
  10916. }
  10917. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  10918. (opj_procedure)opj_j2k_encoding_validation, p_manager)) {
  10919. return OPJ_FALSE;
  10920. }
  10921. /* DEVELOPER CORNER, add your custom validation procedure */
  10922. if (! opj_procedure_list_add_procedure(p_j2k->m_validation_list,
  10923. (opj_procedure)opj_j2k_mct_validation, p_manager)) {
  10924. return OPJ_FALSE;
  10925. }
  10926. return OPJ_TRUE;
  10927. }
  10928. static OPJ_BOOL opj_j2k_setup_header_writing(opj_j2k_t *p_j2k,
  10929. opj_event_mgr_t * p_manager)
  10930. {
  10931. /* preconditions */
  10932. assert(p_j2k != 00);
  10933. assert(p_manager != 00);
  10934. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10935. (opj_procedure)opj_j2k_init_info, p_manager)) {
  10936. return OPJ_FALSE;
  10937. }
  10938. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10939. (opj_procedure)opj_j2k_write_soc, p_manager)) {
  10940. return OPJ_FALSE;
  10941. }
  10942. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10943. (opj_procedure)opj_j2k_write_siz, p_manager)) {
  10944. return OPJ_FALSE;
  10945. }
  10946. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10947. (opj_procedure)opj_j2k_write_cod, p_manager)) {
  10948. return OPJ_FALSE;
  10949. }
  10950. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10951. (opj_procedure)opj_j2k_write_qcd, p_manager)) {
  10952. return OPJ_FALSE;
  10953. }
  10954. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10955. (opj_procedure)opj_j2k_write_all_coc, p_manager)) {
  10956. return OPJ_FALSE;
  10957. }
  10958. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10959. (opj_procedure)opj_j2k_write_all_qcc, p_manager)) {
  10960. return OPJ_FALSE;
  10961. }
  10962. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  10963. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10964. (opj_procedure)opj_j2k_write_tlm, p_manager)) {
  10965. return OPJ_FALSE;
  10966. }
  10967. if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) {
  10968. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10969. (opj_procedure)opj_j2k_write_poc, p_manager)) {
  10970. return OPJ_FALSE;
  10971. }
  10972. }
  10973. }
  10974. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10975. (opj_procedure)opj_j2k_write_regions, p_manager)) {
  10976. return OPJ_FALSE;
  10977. }
  10978. if (p_j2k->m_cp.comment != 00) {
  10979. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10980. (opj_procedure)opj_j2k_write_com, p_manager)) {
  10981. return OPJ_FALSE;
  10982. }
  10983. }
  10984. /* DEVELOPER CORNER, insert your custom procedures */
  10985. if ((p_j2k->m_cp.rsiz & (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) ==
  10986. (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) {
  10987. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10988. (opj_procedure)opj_j2k_write_mct_data_group, p_manager)) {
  10989. return OPJ_FALSE;
  10990. }
  10991. }
  10992. /* End of Developer Corner */
  10993. if (p_j2k->cstr_index) {
  10994. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  10995. (opj_procedure)opj_j2k_get_end_header, p_manager)) {
  10996. return OPJ_FALSE;
  10997. }
  10998. }
  10999. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  11000. (opj_procedure)opj_j2k_create_tcd, p_manager)) {
  11001. return OPJ_FALSE;
  11002. }
  11003. if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
  11004. (opj_procedure)opj_j2k_update_rates, p_manager)) {
  11005. return OPJ_FALSE;
  11006. }
  11007. return OPJ_TRUE;
  11008. }
  11009. static OPJ_BOOL opj_j2k_write_first_tile_part(opj_j2k_t *p_j2k,
  11010. OPJ_BYTE * p_data,
  11011. OPJ_UINT32 * p_data_written,
  11012. OPJ_UINT32 total_data_size,
  11013. opj_stream_private_t *p_stream,
  11014. struct opj_event_mgr * p_manager)
  11015. {
  11016. OPJ_UINT32 l_nb_bytes_written = 0;
  11017. OPJ_UINT32 l_current_nb_bytes_written;
  11018. OPJ_BYTE * l_begin_data = 00;
  11019. opj_tcd_t * l_tcd = 00;
  11020. opj_cp_t * l_cp = 00;
  11021. l_tcd = p_j2k->m_tcd;
  11022. l_cp = &(p_j2k->m_cp);
  11023. l_tcd->cur_pino = 0;
  11024. /*Get number of tile parts*/
  11025. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = 0;
  11026. /* INDEX >> */
  11027. /* << INDEX */
  11028. l_current_nb_bytes_written = 0;
  11029. l_begin_data = p_data;
  11030. if (! opj_j2k_write_sot(p_j2k, p_data, total_data_size,
  11031. &l_current_nb_bytes_written, p_stream,
  11032. p_manager)) {
  11033. return OPJ_FALSE;
  11034. }
  11035. l_nb_bytes_written += l_current_nb_bytes_written;
  11036. p_data += l_current_nb_bytes_written;
  11037. total_data_size -= l_current_nb_bytes_written;
  11038. if (!OPJ_IS_CINEMA(l_cp->rsiz)) {
  11039. #if 0
  11040. for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
  11041. l_current_nb_bytes_written = 0;
  11042. opj_j2k_write_coc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written,
  11043. p_manager);
  11044. l_nb_bytes_written += l_current_nb_bytes_written;
  11045. p_data += l_current_nb_bytes_written;
  11046. total_data_size -= l_current_nb_bytes_written;
  11047. l_current_nb_bytes_written = 0;
  11048. opj_j2k_write_qcc_in_memory(p_j2k, compno, p_data, &l_current_nb_bytes_written,
  11049. p_manager);
  11050. l_nb_bytes_written += l_current_nb_bytes_written;
  11051. p_data += l_current_nb_bytes_written;
  11052. total_data_size -= l_current_nb_bytes_written;
  11053. }
  11054. #endif
  11055. if (l_cp->tcps[p_j2k->m_current_tile_number].POC) {
  11056. l_current_nb_bytes_written = 0;
  11057. opj_j2k_write_poc_in_memory(p_j2k, p_data, &l_current_nb_bytes_written,
  11058. p_manager);
  11059. l_nb_bytes_written += l_current_nb_bytes_written;
  11060. p_data += l_current_nb_bytes_written;
  11061. total_data_size -= l_current_nb_bytes_written;
  11062. }
  11063. }
  11064. l_current_nb_bytes_written = 0;
  11065. if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written,
  11066. total_data_size, p_stream, p_manager)) {
  11067. return OPJ_FALSE;
  11068. }
  11069. l_nb_bytes_written += l_current_nb_bytes_written;
  11070. * p_data_written = l_nb_bytes_written;
  11071. /* Writing Psot in SOT marker */
  11072. opj_write_bytes(l_begin_data + 6, l_nb_bytes_written,
  11073. 4); /* PSOT */
  11074. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  11075. opj_j2k_update_tlm(p_j2k, l_nb_bytes_written);
  11076. }
  11077. return OPJ_TRUE;
  11078. }
  11079. static OPJ_BOOL opj_j2k_write_all_tile_parts(opj_j2k_t *p_j2k,
  11080. OPJ_BYTE * p_data,
  11081. OPJ_UINT32 * p_data_written,
  11082. OPJ_UINT32 total_data_size,
  11083. opj_stream_private_t *p_stream,
  11084. struct opj_event_mgr * p_manager
  11085. )
  11086. {
  11087. OPJ_UINT32 tilepartno = 0;
  11088. OPJ_UINT32 l_nb_bytes_written = 0;
  11089. OPJ_UINT32 l_current_nb_bytes_written;
  11090. OPJ_UINT32 l_part_tile_size;
  11091. OPJ_UINT32 tot_num_tp;
  11092. OPJ_UINT32 pino;
  11093. OPJ_BYTE * l_begin_data;
  11094. opj_tcp_t *l_tcp = 00;
  11095. opj_tcd_t * l_tcd = 00;
  11096. opj_cp_t * l_cp = 00;
  11097. l_tcd = p_j2k->m_tcd;
  11098. l_cp = &(p_j2k->m_cp);
  11099. l_tcp = l_cp->tcps + p_j2k->m_current_tile_number;
  11100. /*Get number of tile parts*/
  11101. tot_num_tp = opj_j2k_get_num_tp(l_cp, 0, p_j2k->m_current_tile_number);
  11102. /* start writing remaining tile parts */
  11103. ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
  11104. for (tilepartno = 1; tilepartno < tot_num_tp ; ++tilepartno) {
  11105. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
  11106. l_current_nb_bytes_written = 0;
  11107. l_part_tile_size = 0;
  11108. l_begin_data = p_data;
  11109. if (! opj_j2k_write_sot(p_j2k, p_data,
  11110. total_data_size,
  11111. &l_current_nb_bytes_written,
  11112. p_stream,
  11113. p_manager)) {
  11114. return OPJ_FALSE;
  11115. }
  11116. l_nb_bytes_written += l_current_nb_bytes_written;
  11117. p_data += l_current_nb_bytes_written;
  11118. total_data_size -= l_current_nb_bytes_written;
  11119. l_part_tile_size += l_current_nb_bytes_written;
  11120. l_current_nb_bytes_written = 0;
  11121. if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written,
  11122. total_data_size, p_stream, p_manager)) {
  11123. return OPJ_FALSE;
  11124. }
  11125. p_data += l_current_nb_bytes_written;
  11126. l_nb_bytes_written += l_current_nb_bytes_written;
  11127. total_data_size -= l_current_nb_bytes_written;
  11128. l_part_tile_size += l_current_nb_bytes_written;
  11129. /* Writing Psot in SOT marker */
  11130. opj_write_bytes(l_begin_data + 6, l_part_tile_size,
  11131. 4); /* PSOT */
  11132. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  11133. opj_j2k_update_tlm(p_j2k, l_part_tile_size);
  11134. }
  11135. ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
  11136. }
  11137. for (pino = 1; pino <= l_tcp->numpocs; ++pino) {
  11138. l_tcd->cur_pino = pino;
  11139. /*Get number of tile parts*/
  11140. tot_num_tp = opj_j2k_get_num_tp(l_cp, pino, p_j2k->m_current_tile_number);
  11141. for (tilepartno = 0; tilepartno < tot_num_tp ; ++tilepartno) {
  11142. p_j2k->m_specific_param.m_encoder.m_current_poc_tile_part_number = tilepartno;
  11143. l_current_nb_bytes_written = 0;
  11144. l_part_tile_size = 0;
  11145. l_begin_data = p_data;
  11146. if (! opj_j2k_write_sot(p_j2k, p_data,
  11147. total_data_size,
  11148. &l_current_nb_bytes_written, p_stream,
  11149. p_manager)) {
  11150. return OPJ_FALSE;
  11151. }
  11152. l_nb_bytes_written += l_current_nb_bytes_written;
  11153. p_data += l_current_nb_bytes_written;
  11154. total_data_size -= l_current_nb_bytes_written;
  11155. l_part_tile_size += l_current_nb_bytes_written;
  11156. l_current_nb_bytes_written = 0;
  11157. if (! opj_j2k_write_sod(p_j2k, l_tcd, p_data, &l_current_nb_bytes_written,
  11158. total_data_size, p_stream, p_manager)) {
  11159. return OPJ_FALSE;
  11160. }
  11161. l_nb_bytes_written += l_current_nb_bytes_written;
  11162. p_data += l_current_nb_bytes_written;
  11163. total_data_size -= l_current_nb_bytes_written;
  11164. l_part_tile_size += l_current_nb_bytes_written;
  11165. /* Writing Psot in SOT marker */
  11166. opj_write_bytes(l_begin_data + 6, l_part_tile_size,
  11167. 4); /* PSOT */
  11168. if (p_j2k->m_specific_param.m_encoder.m_TLM) {
  11169. opj_j2k_update_tlm(p_j2k, l_part_tile_size);
  11170. }
  11171. ++p_j2k->m_specific_param.m_encoder.m_current_tile_part_number;
  11172. }
  11173. }
  11174. *p_data_written = l_nb_bytes_written;
  11175. return OPJ_TRUE;
  11176. }
  11177. static OPJ_BOOL opj_j2k_write_updated_tlm(opj_j2k_t *p_j2k,
  11178. struct opj_stream_private *p_stream,
  11179. struct opj_event_mgr * p_manager)
  11180. {
  11181. OPJ_UINT32 l_tlm_size;
  11182. OPJ_OFF_T l_tlm_position, l_current_position;
  11183. OPJ_UINT32 size_per_tile_part;
  11184. /* preconditions */
  11185. assert(p_j2k != 00);
  11186. assert(p_manager != 00);
  11187. assert(p_stream != 00);
  11188. size_per_tile_part = p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte ? 5 : 6;
  11189. l_tlm_size = size_per_tile_part *
  11190. p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
  11191. l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
  11192. l_current_position = opj_stream_tell(p_stream);
  11193. if (! opj_stream_seek(p_stream, l_tlm_position, p_manager)) {
  11194. return OPJ_FALSE;
  11195. }
  11196. if (opj_stream_write_data(p_stream,
  11197. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer, l_tlm_size,
  11198. p_manager) != l_tlm_size) {
  11199. return OPJ_FALSE;
  11200. }
  11201. if (! opj_stream_seek(p_stream, l_current_position, p_manager)) {
  11202. return OPJ_FALSE;
  11203. }
  11204. return OPJ_TRUE;
  11205. }
  11206. static OPJ_BOOL opj_j2k_end_encoding(opj_j2k_t *p_j2k,
  11207. struct opj_stream_private *p_stream,
  11208. struct opj_event_mgr * p_manager)
  11209. {
  11210. /* preconditions */
  11211. assert(p_j2k != 00);
  11212. assert(p_manager != 00);
  11213. assert(p_stream != 00);
  11214. OPJ_UNUSED(p_stream);
  11215. OPJ_UNUSED(p_manager);
  11216. opj_tcd_destroy(p_j2k->m_tcd);
  11217. p_j2k->m_tcd = 00;
  11218. if (p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
  11219. opj_free(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer);
  11220. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer = 0;
  11221. p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current = 0;
  11222. }
  11223. if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data) {
  11224. opj_free(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
  11225. p_j2k->m_specific_param.m_encoder.m_encoded_tile_data = 0;
  11226. }
  11227. p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = 0;
  11228. return OPJ_TRUE;
  11229. }
  11230. /**
  11231. * Destroys the memory associated with the decoding of headers.
  11232. */
  11233. static OPJ_BOOL opj_j2k_destroy_header_memory(opj_j2k_t * p_j2k,
  11234. opj_stream_private_t *p_stream,
  11235. opj_event_mgr_t * p_manager
  11236. )
  11237. {
  11238. /* preconditions */
  11239. assert(p_j2k != 00);
  11240. assert(p_stream != 00);
  11241. assert(p_manager != 00);
  11242. OPJ_UNUSED(p_stream);
  11243. OPJ_UNUSED(p_manager);
  11244. if (p_j2k->m_specific_param.m_encoder.m_header_tile_data) {
  11245. opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
  11246. p_j2k->m_specific_param.m_encoder.m_header_tile_data = 0;
  11247. }
  11248. p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
  11249. return OPJ_TRUE;
  11250. }
  11251. static OPJ_BOOL opj_j2k_init_info(opj_j2k_t *p_j2k,
  11252. struct opj_stream_private *p_stream,
  11253. struct opj_event_mgr * p_manager)
  11254. {
  11255. opj_codestream_info_t * l_cstr_info = 00;
  11256. /* preconditions */
  11257. assert(p_j2k != 00);
  11258. assert(p_manager != 00);
  11259. assert(p_stream != 00);
  11260. (void)l_cstr_info;
  11261. OPJ_UNUSED(p_stream);
  11262. /* TODO mergeV2: check this part which use cstr_info */
  11263. /*l_cstr_info = p_j2k->cstr_info;
  11264. if (l_cstr_info) {
  11265. OPJ_UINT32 compno;
  11266. 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));
  11267. l_cstr_info->image_w = p_j2k->m_image->x1 - p_j2k->m_image->x0;
  11268. l_cstr_info->image_h = p_j2k->m_image->y1 - p_j2k->m_image->y0;
  11269. l_cstr_info->prog = (&p_j2k->m_cp.tcps[0])->prg;
  11270. l_cstr_info->tw = p_j2k->m_cp.tw;
  11271. l_cstr_info->th = p_j2k->m_cp.th;
  11272. l_cstr_info->tile_x = p_j2k->m_cp.tdx;*/ /* new version parser */
  11273. /*l_cstr_info->tile_y = p_j2k->m_cp.tdy;*/ /* new version parser */
  11274. /*l_cstr_info->tile_Ox = p_j2k->m_cp.tx0;*/ /* new version parser */
  11275. /*l_cstr_info->tile_Oy = p_j2k->m_cp.ty0;*/ /* new version parser */
  11276. /*l_cstr_info->numcomps = p_j2k->m_image->numcomps;
  11277. l_cstr_info->numlayers = (&p_j2k->m_cp.tcps[0])->numlayers;
  11278. l_cstr_info->numdecompos = (OPJ_INT32*) opj_malloc(p_j2k->m_image->numcomps * sizeof(OPJ_INT32));
  11279. for (compno=0; compno < p_j2k->m_image->numcomps; compno++) {
  11280. l_cstr_info->numdecompos[compno] = (&p_j2k->m_cp.tcps[0])->tccps->numresolutions - 1;
  11281. }
  11282. l_cstr_info->D_max = 0.0; */ /* ADD Marcela */
  11283. /*l_cstr_info->main_head_start = opj_stream_tell(p_stream);*/ /* position of SOC */
  11284. /*l_cstr_info->maxmarknum = 100;
  11285. l_cstr_info->marker = (opj_marker_info_t *) opj_malloc(l_cstr_info->maxmarknum * sizeof(opj_marker_info_t));
  11286. l_cstr_info->marknum = 0;
  11287. }*/
  11288. return opj_j2k_calculate_tp(p_j2k, &(p_j2k->m_cp),
  11289. &p_j2k->m_specific_param.m_encoder.m_total_tile_parts, p_j2k->m_private_image,
  11290. p_manager);
  11291. }
  11292. /**
  11293. * Creates a tile-coder encoder.
  11294. *
  11295. * @param p_stream the stream to write data to.
  11296. * @param p_j2k J2K codec.
  11297. * @param p_manager the user event manager.
  11298. */
  11299. static OPJ_BOOL opj_j2k_create_tcd(opj_j2k_t *p_j2k,
  11300. opj_stream_private_t *p_stream,
  11301. opj_event_mgr_t * p_manager
  11302. )
  11303. {
  11304. /* preconditions */
  11305. assert(p_j2k != 00);
  11306. assert(p_manager != 00);
  11307. assert(p_stream != 00);
  11308. OPJ_UNUSED(p_stream);
  11309. p_j2k->m_tcd = opj_tcd_create(OPJ_FALSE);
  11310. if (! p_j2k->m_tcd) {
  11311. opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to create Tile Coder\n");
  11312. return OPJ_FALSE;
  11313. }
  11314. if (!opj_tcd_init(p_j2k->m_tcd, p_j2k->m_private_image, &p_j2k->m_cp,
  11315. p_j2k->m_tp)) {
  11316. opj_tcd_destroy(p_j2k->m_tcd);
  11317. p_j2k->m_tcd = 00;
  11318. return OPJ_FALSE;
  11319. }
  11320. return OPJ_TRUE;
  11321. }
  11322. OPJ_BOOL opj_j2k_write_tile(opj_j2k_t * p_j2k,
  11323. OPJ_UINT32 p_tile_index,
  11324. OPJ_BYTE * p_data,
  11325. OPJ_UINT32 p_data_size,
  11326. opj_stream_private_t *p_stream,
  11327. opj_event_mgr_t * p_manager)
  11328. {
  11329. if (! opj_j2k_pre_write_tile(p_j2k, p_tile_index, p_stream, p_manager)) {
  11330. opj_event_msg(p_manager, EVT_ERROR,
  11331. "Error while opj_j2k_pre_write_tile with tile index = %d\n", p_tile_index);
  11332. return OPJ_FALSE;
  11333. } else {
  11334. OPJ_UINT32 j;
  11335. /* Allocate data */
  11336. for (j = 0; j < p_j2k->m_tcd->image->numcomps; ++j) {
  11337. opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j;
  11338. if (! opj_alloc_tile_component_data(l_tilec)) {
  11339. opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data.");
  11340. return OPJ_FALSE;
  11341. }
  11342. }
  11343. /* now copy data into the tile component */
  11344. if (! opj_tcd_copy_tile_data(p_j2k->m_tcd, p_data, p_data_size)) {
  11345. opj_event_msg(p_manager, EVT_ERROR,
  11346. "Size mismatch between tile data and sent data.");
  11347. return OPJ_FALSE;
  11348. }
  11349. if (! opj_j2k_post_write_tile(p_j2k, p_stream, p_manager)) {
  11350. opj_event_msg(p_manager, EVT_ERROR,
  11351. "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);
  11352. return OPJ_FALSE;
  11353. }
  11354. }
  11355. return OPJ_TRUE;
  11356. }