tinyexr.h 287 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230
  1. #ifndef TINYEXR_H_
  2. #define TINYEXR_H_
  3. /*
  4. Copyright (c) 2014 - 2021, Syoyo Fujita and many contributors.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. * Neither the name of the Syoyo Fujita nor the
  14. names of its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  20. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // TinyEXR contains some OpenEXR code, which is licensed under ------------
  28. ///////////////////////////////////////////////////////////////////////////
  29. //
  30. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  31. // Digital Ltd. LLC
  32. //
  33. // All rights reserved.
  34. //
  35. // Redistribution and use in source and binary forms, with or without
  36. // modification, are permitted provided that the following conditions are
  37. // met:
  38. // * Redistributions of source code must retain the above copyright
  39. // notice, this list of conditions and the following disclaimer.
  40. // * Redistributions in binary form must reproduce the above
  41. // copyright notice, this list of conditions and the following disclaimer
  42. // in the documentation and/or other materials provided with the
  43. // distribution.
  44. // * Neither the name of Industrial Light & Magic nor the names of
  45. // its contributors may be used to endorse or promote products derived
  46. // from this software without specific prior written permission.
  47. //
  48. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  49. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  51. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  52. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  55. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  56. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  57. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  58. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. //
  60. ///////////////////////////////////////////////////////////////////////////
  61. // End of OpenEXR license -------------------------------------------------
  62. //
  63. //
  64. // Do this:
  65. // #define TINYEXR_IMPLEMENTATION
  66. // before you include this file in *one* C or C++ file to create the
  67. // implementation.
  68. //
  69. // // i.e. it should look like this:
  70. // #include ...
  71. // #include ...
  72. // #include ...
  73. // #define TINYEXR_IMPLEMENTATION
  74. // #include "tinyexr.h"
  75. //
  76. //
  77. #include <stddef.h> // for size_t
  78. #include <stdint.h> // guess stdint.h is available(C99)
  79. #ifdef __cplusplus
  80. extern "C" {
  81. #endif
  82. #if defined(_M_IX86) || defined(_M_X64) || defined(__i386__) || \
  83. defined(__i386) || defined(__i486__) || defined(__i486) || \
  84. defined(i386) || defined(__ia64__) || defined(__x86_64__)
  85. #define TINYEXR_X86_OR_X64_CPU 1
  86. #else
  87. #define TINYEXR_X86_OR_X64_CPU 0
  88. #endif
  89. #if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || TINYEXR_X86_OR_X64_CPU
  90. #define TINYEXR_LITTLE_ENDIAN 1
  91. #else
  92. #define TINYEXR_LITTLE_ENDIAN 0
  93. #endif
  94. // Use miniz or not to decode ZIP format pixel. Linking with zlib
  95. // required if this flag is 0 and TINYEXR_USE_STB_ZLIB is 0.
  96. #ifndef TINYEXR_USE_MINIZ
  97. #define TINYEXR_USE_MINIZ (1)
  98. #endif
  99. // Use the ZIP implementation of stb_image.h and stb_image_write.h.
  100. #ifndef TINYEXR_USE_STB_ZLIB
  101. #define TINYEXR_USE_STB_ZLIB (0)
  102. #endif
  103. // Disable PIZ compression when applying cpplint.
  104. #ifndef TINYEXR_USE_PIZ
  105. #define TINYEXR_USE_PIZ (1)
  106. #endif
  107. #ifndef TINYEXR_USE_ZFP
  108. #define TINYEXR_USE_ZFP (0) // TinyEXR extension.
  109. // http://computation.llnl.gov/projects/floating-point-compression
  110. #endif
  111. #ifndef TINYEXR_USE_THREAD
  112. #define TINYEXR_USE_THREAD (0) // No threaded loading.
  113. // http://computation.llnl.gov/projects/floating-point-compression
  114. #endif
  115. #ifndef TINYEXR_USE_OPENMP
  116. #ifdef _OPENMP
  117. #define TINYEXR_USE_OPENMP (1)
  118. #else
  119. #define TINYEXR_USE_OPENMP (0)
  120. #endif
  121. #endif
  122. #define TINYEXR_SUCCESS (0)
  123. #define TINYEXR_ERROR_INVALID_MAGIC_NUMBER (-1)
  124. #define TINYEXR_ERROR_INVALID_EXR_VERSION (-2)
  125. #define TINYEXR_ERROR_INVALID_ARGUMENT (-3)
  126. #define TINYEXR_ERROR_INVALID_DATA (-4)
  127. #define TINYEXR_ERROR_INVALID_FILE (-5)
  128. #define TINYEXR_ERROR_INVALID_PARAMETER (-6)
  129. #define TINYEXR_ERROR_CANT_OPEN_FILE (-7)
  130. #define TINYEXR_ERROR_UNSUPPORTED_FORMAT (-8)
  131. #define TINYEXR_ERROR_INVALID_HEADER (-9)
  132. #define TINYEXR_ERROR_UNSUPPORTED_FEATURE (-10)
  133. #define TINYEXR_ERROR_CANT_WRITE_FILE (-11)
  134. #define TINYEXR_ERROR_SERIALIZATION_FAILED (-12)
  135. #define TINYEXR_ERROR_LAYER_NOT_FOUND (-13)
  136. #define TINYEXR_ERROR_DATA_TOO_LARGE (-14)
  137. // @note { OpenEXR file format: http://www.openexr.com/openexrfilelayout.pdf }
  138. // pixel type: possible values are: UINT = 0 HALF = 1 FLOAT = 2
  139. #define TINYEXR_PIXELTYPE_UINT (0)
  140. #define TINYEXR_PIXELTYPE_HALF (1)
  141. #define TINYEXR_PIXELTYPE_FLOAT (2)
  142. #define TINYEXR_MAX_HEADER_ATTRIBUTES (1024)
  143. #define TINYEXR_MAX_CUSTOM_ATTRIBUTES (128)
  144. #define TINYEXR_COMPRESSIONTYPE_NONE (0)
  145. #define TINYEXR_COMPRESSIONTYPE_RLE (1)
  146. #define TINYEXR_COMPRESSIONTYPE_ZIPS (2)
  147. #define TINYEXR_COMPRESSIONTYPE_ZIP (3)
  148. #define TINYEXR_COMPRESSIONTYPE_PIZ (4)
  149. #define TINYEXR_COMPRESSIONTYPE_ZFP (128) // TinyEXR extension
  150. #define TINYEXR_ZFP_COMPRESSIONTYPE_RATE (0)
  151. #define TINYEXR_ZFP_COMPRESSIONTYPE_PRECISION (1)
  152. #define TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY (2)
  153. #define TINYEXR_TILE_ONE_LEVEL (0)
  154. #define TINYEXR_TILE_MIPMAP_LEVELS (1)
  155. #define TINYEXR_TILE_RIPMAP_LEVELS (2)
  156. #define TINYEXR_TILE_ROUND_DOWN (0)
  157. #define TINYEXR_TILE_ROUND_UP (1)
  158. typedef struct TEXRVersion {
  159. int version; // this must be 2
  160. // tile format image;
  161. // not zero for only a single-part "normal" tiled file (according to spec.)
  162. int tiled;
  163. int long_name; // long name attribute
  164. // deep image(EXR 2.0);
  165. // for a multi-part file, indicates that at least one part is of type deep* (according to spec.)
  166. int non_image;
  167. int multipart; // multi-part(EXR 2.0)
  168. } EXRVersion;
  169. typedef struct TEXRAttribute {
  170. char name[256]; // name and type are up to 255 chars long.
  171. char type[256];
  172. unsigned char *value; // uint8_t*
  173. int size;
  174. int pad0;
  175. } EXRAttribute;
  176. typedef struct TEXRChannelInfo {
  177. char name[256]; // less than 255 bytes long
  178. int pixel_type;
  179. int x_sampling;
  180. int y_sampling;
  181. unsigned char p_linear;
  182. unsigned char pad[3];
  183. } EXRChannelInfo;
  184. typedef struct TEXRTile {
  185. int offset_x;
  186. int offset_y;
  187. int level_x;
  188. int level_y;
  189. int width; // actual width in a tile.
  190. int height; // actual height int a tile.
  191. unsigned char **images; // image[channels][pixels]
  192. } EXRTile;
  193. typedef struct TEXRBox2i {
  194. int min_x;
  195. int min_y;
  196. int max_x;
  197. int max_y;
  198. } EXRBox2i;
  199. typedef struct TEXRHeader {
  200. float pixel_aspect_ratio;
  201. int line_order;
  202. EXRBox2i data_window;
  203. EXRBox2i display_window;
  204. float screen_window_center[2];
  205. float screen_window_width;
  206. int chunk_count;
  207. // Properties for tiled format(`tiledesc`).
  208. int tiled;
  209. int tile_size_x;
  210. int tile_size_y;
  211. int tile_level_mode;
  212. int tile_rounding_mode;
  213. int long_name;
  214. // for a single-part file, agree with the version field bit 11
  215. // for a multi-part file, it is consistent with the type of part
  216. int non_image;
  217. int multipart;
  218. unsigned int header_len;
  219. // Custom attributes(exludes required attributes(e.g. `channels`,
  220. // `compression`, etc)
  221. int num_custom_attributes;
  222. EXRAttribute *custom_attributes; // array of EXRAttribute. size =
  223. // `num_custom_attributes`.
  224. EXRChannelInfo *channels; // [num_channels]
  225. int *pixel_types; // Loaded pixel type(TINYEXR_PIXELTYPE_*) of `images` for
  226. // each channel. This is overwritten with `requested_pixel_types` when
  227. // loading.
  228. int num_channels;
  229. int compression_type; // compression type(TINYEXR_COMPRESSIONTYPE_*)
  230. int *requested_pixel_types; // Filled initially by
  231. // ParseEXRHeaderFrom(Meomory|File), then users
  232. // can edit it(only valid for HALF pixel type
  233. // channel)
  234. // name attribute required for multipart files;
  235. // must be unique and non empty (according to spec.);
  236. // use EXRSetNameAttr for setting value;
  237. // max 255 character allowed - excluding terminating zero
  238. char name[256];
  239. } EXRHeader;
  240. typedef struct TEXRMultiPartHeader {
  241. int num_headers;
  242. EXRHeader *headers;
  243. } EXRMultiPartHeader;
  244. typedef struct TEXRImage {
  245. EXRTile *tiles; // Tiled pixel data. The application must reconstruct image
  246. // from tiles manually. NULL if scanline format.
  247. struct TEXRImage* next_level; // NULL if scanline format or image is the last level.
  248. int level_x; // x level index
  249. int level_y; // y level index
  250. unsigned char **images; // image[channels][pixels]. NULL if tiled format.
  251. int width;
  252. int height;
  253. int num_channels;
  254. // Properties for tile format.
  255. int num_tiles;
  256. } EXRImage;
  257. typedef struct TEXRMultiPartImage {
  258. int num_images;
  259. EXRImage *images;
  260. } EXRMultiPartImage;
  261. typedef struct TDeepImage {
  262. const char **channel_names;
  263. float ***image; // image[channels][scanlines][samples]
  264. int **offset_table; // offset_table[scanline][offsets]
  265. int num_channels;
  266. int width;
  267. int height;
  268. int pad0;
  269. } DeepImage;
  270. // @deprecated { For backward compatibility. Not recommended to use. }
  271. // Loads single-frame OpenEXR image. Assume EXR image contains A(single channel
  272. // alpha) or RGB(A) channels.
  273. // Application must free image data as returned by `out_rgba`
  274. // Result image format is: float x RGBA x width x hight
  275. // Returns negative value and may set error string in `err` when there's an
  276. // error
  277. extern int LoadEXR(float **out_rgba, int *width, int *height,
  278. const char *filename, const char **err);
  279. // Loads single-frame OpenEXR image by specifying layer name. Assume EXR image
  280. // contains A(single channel alpha) or RGB(A) channels. Application must free
  281. // image data as returned by `out_rgba` Result image format is: float x RGBA x
  282. // width x hight Returns negative value and may set error string in `err` when
  283. // there's an error When the specified layer name is not found in the EXR file,
  284. // the function will return `TINYEXR_ERROR_LAYER_NOT_FOUND`.
  285. extern int LoadEXRWithLayer(float **out_rgba, int *width, int *height,
  286. const char *filename, const char *layer_name,
  287. const char **err);
  288. //
  289. // Get layer infos from EXR file.
  290. //
  291. // @param[out] layer_names List of layer names. Application must free memory
  292. // after using this.
  293. // @param[out] num_layers The number of layers
  294. // @param[out] err Error string(will be filled when the function returns error
  295. // code). Free it using FreeEXRErrorMessage after using this value.
  296. //
  297. // @return TINYEXR_SUCCEES upon success.
  298. //
  299. extern int EXRLayers(const char *filename, const char **layer_names[],
  300. int *num_layers, const char **err);
  301. // @deprecated
  302. // Simple wrapper API for ParseEXRHeaderFromFile.
  303. // checking given file is a EXR file(by just look up header)
  304. // @return TINYEXR_SUCCEES for EXR image, TINYEXR_ERROR_INVALID_HEADER for
  305. // others
  306. extern int IsEXR(const char *filename);
  307. // Simple wrapper API for ParseEXRHeaderFromMemory.
  308. // Check if given data is a EXR image(by just looking up a header section)
  309. // @return TINYEXR_SUCCEES for EXR image, TINYEXR_ERROR_INVALID_HEADER for
  310. // others
  311. extern int IsEXRFromMemory(const unsigned char *memory, size_t size);
  312. // @deprecated
  313. // Saves single-frame OpenEXR image to a buffer. Assume EXR image contains RGB(A) channels.
  314. // components must be 1(Grayscale), 3(RGB) or 4(RGBA).
  315. // Input image format is: `float x width x height`, or `float x RGB(A) x width x
  316. // hight`
  317. // Save image as fp16(HALF) format when `save_as_fp16` is positive non-zero
  318. // value.
  319. // Save image as fp32(FLOAT) format when `save_as_fp16` is 0.
  320. // Use ZIP compression by default.
  321. // `buffer` is the pointer to write EXR data.
  322. // Memory for `buffer` is allocated internally in SaveEXRToMemory.
  323. // Returns the data size of EXR file when the value is positive(up to 2GB EXR data).
  324. // Returns negative value and may set error string in `err` when there's an
  325. // error
  326. extern int SaveEXRToMemory(const float *data, const int width, const int height,
  327. const int components, const int save_as_fp16,
  328. const unsigned char **buffer, const char **err);
  329. // @deprecated { Not recommended, but handy to use. }
  330. // Saves single-frame OpenEXR image to a buffer. Assume EXR image contains RGB(A) channels.
  331. // components must be 1(Grayscale), 3(RGB) or 4(RGBA).
  332. // Input image format is: `float x width x height`, or `float x RGB(A) x width x
  333. // hight`
  334. // Save image as fp16(HALF) format when `save_as_fp16` is positive non-zero
  335. // value.
  336. // Save image as fp32(FLOAT) format when `save_as_fp16` is 0.
  337. // Use ZIP compression by default.
  338. // Returns TINYEXR_SUCCEES(0) when success.
  339. // Returns negative value and may set error string in `err` when there's an
  340. // error
  341. extern int SaveEXR(const float *data, const int width, const int height,
  342. const int components, const int save_as_fp16,
  343. const char *filename, const char **err);
  344. // Returns the number of resolution levels of the image (including the base)
  345. extern int EXRNumLevels(const EXRImage* exr_image);
  346. // Initialize EXRHeader struct
  347. extern void InitEXRHeader(EXRHeader *exr_header);
  348. // Set name attribute of EXRHeader struct (it makes a copy)
  349. extern void EXRSetNameAttr(EXRHeader *exr_header, const char* name);
  350. // Initialize EXRImage struct
  351. extern void InitEXRImage(EXRImage *exr_image);
  352. // Frees internal data of EXRHeader struct
  353. extern int FreeEXRHeader(EXRHeader *exr_header);
  354. // Frees internal data of EXRImage struct
  355. extern int FreeEXRImage(EXRImage *exr_image);
  356. // Frees error message
  357. extern void FreeEXRErrorMessage(const char *msg);
  358. // Parse EXR version header of a file.
  359. extern int ParseEXRVersionFromFile(EXRVersion *version, const char *filename);
  360. // Parse EXR version header from memory-mapped EXR data.
  361. extern int ParseEXRVersionFromMemory(EXRVersion *version,
  362. const unsigned char *memory, size_t size);
  363. // Parse single-part OpenEXR header from a file and initialize `EXRHeader`.
  364. // When there was an error message, Application must free `err` with
  365. // FreeEXRErrorMessage()
  366. extern int ParseEXRHeaderFromFile(EXRHeader *header, const EXRVersion *version,
  367. const char *filename, const char **err);
  368. // Parse single-part OpenEXR header from a memory and initialize `EXRHeader`.
  369. // When there was an error message, Application must free `err` with
  370. // FreeEXRErrorMessage()
  371. extern int ParseEXRHeaderFromMemory(EXRHeader *header,
  372. const EXRVersion *version,
  373. const unsigned char *memory, size_t size,
  374. const char **err);
  375. // Parse multi-part OpenEXR headers from a file and initialize `EXRHeader*`
  376. // array.
  377. // When there was an error message, Application must free `err` with
  378. // FreeEXRErrorMessage()
  379. extern int ParseEXRMultipartHeaderFromFile(EXRHeader ***headers,
  380. int *num_headers,
  381. const EXRVersion *version,
  382. const char *filename,
  383. const char **err);
  384. // Parse multi-part OpenEXR headers from a memory and initialize `EXRHeader*`
  385. // array
  386. // When there was an error message, Application must free `err` with
  387. // FreeEXRErrorMessage()
  388. extern int ParseEXRMultipartHeaderFromMemory(EXRHeader ***headers,
  389. int *num_headers,
  390. const EXRVersion *version,
  391. const unsigned char *memory,
  392. size_t size, const char **err);
  393. // Loads single-part OpenEXR image from a file.
  394. // Application must setup `ParseEXRHeaderFromFile` before calling this function.
  395. // Application can free EXRImage using `FreeEXRImage`
  396. // Returns negative value and may set error string in `err` when there's an
  397. // error
  398. // When there was an error message, Application must free `err` with
  399. // FreeEXRErrorMessage()
  400. extern int LoadEXRImageFromFile(EXRImage *image, const EXRHeader *header,
  401. const char *filename, const char **err);
  402. // Loads single-part OpenEXR image from a memory.
  403. // Application must setup `EXRHeader` with
  404. // `ParseEXRHeaderFromMemory` before calling this function.
  405. // Application can free EXRImage using `FreeEXRImage`
  406. // Returns negative value and may set error string in `err` when there's an
  407. // error
  408. // When there was an error message, Application must free `err` with
  409. // FreeEXRErrorMessage()
  410. extern int LoadEXRImageFromMemory(EXRImage *image, const EXRHeader *header,
  411. const unsigned char *memory,
  412. const size_t size, const char **err);
  413. // Loads multi-part OpenEXR image from a file.
  414. // Application must setup `ParseEXRMultipartHeaderFromFile` before calling this
  415. // function.
  416. // Application can free EXRImage using `FreeEXRImage`
  417. // Returns negative value and may set error string in `err` when there's an
  418. // error
  419. // When there was an error message, Application must free `err` with
  420. // FreeEXRErrorMessage()
  421. extern int LoadEXRMultipartImageFromFile(EXRImage *images,
  422. const EXRHeader **headers,
  423. unsigned int num_parts,
  424. const char *filename,
  425. const char **err);
  426. // Loads multi-part OpenEXR image from a memory.
  427. // Application must setup `EXRHeader*` array with
  428. // `ParseEXRMultipartHeaderFromMemory` before calling this function.
  429. // Application can free EXRImage using `FreeEXRImage`
  430. // Returns negative value and may set error string in `err` when there's an
  431. // error
  432. // When there was an error message, Application must free `err` with
  433. // FreeEXRErrorMessage()
  434. extern int LoadEXRMultipartImageFromMemory(EXRImage *images,
  435. const EXRHeader **headers,
  436. unsigned int num_parts,
  437. const unsigned char *memory,
  438. const size_t size, const char **err);
  439. // Saves multi-channel, single-frame OpenEXR image to a file.
  440. // Returns negative value and may set error string in `err` when there's an
  441. // error
  442. // When there was an error message, Application must free `err` with
  443. // FreeEXRErrorMessage()
  444. extern int SaveEXRImageToFile(const EXRImage *image,
  445. const EXRHeader *exr_header, const char *filename,
  446. const char **err);
  447. // Saves multi-channel, single-frame OpenEXR image to a memory.
  448. // Image is compressed using EXRImage.compression value.
  449. // Return the number of bytes if success.
  450. // Return zero and will set error string in `err` when there's an
  451. // error.
  452. // When there was an error message, Application must free `err` with
  453. // FreeEXRErrorMessage()
  454. extern size_t SaveEXRImageToMemory(const EXRImage *image,
  455. const EXRHeader *exr_header,
  456. unsigned char **memory, const char **err);
  457. // Saves multi-channel, multi-frame OpenEXR image to a memory.
  458. // Image is compressed using EXRImage.compression value.
  459. // File global attributes (eg. display_window) must be set in the first header.
  460. // Returns negative value and may set error string in `err` when there's an
  461. // error
  462. // When there was an error message, Application must free `err` with
  463. // FreeEXRErrorMessage()
  464. extern int SaveEXRMultipartImageToFile(const EXRImage *images,
  465. const EXRHeader **exr_headers,
  466. unsigned int num_parts,
  467. const char *filename, const char **err);
  468. // Saves multi-channel, multi-frame OpenEXR image to a memory.
  469. // Image is compressed using EXRImage.compression value.
  470. // File global attributes (eg. display_window) must be set in the first header.
  471. // Return the number of bytes if success.
  472. // Return zero and will set error string in `err` when there's an
  473. // error.
  474. // When there was an error message, Application must free `err` with
  475. // FreeEXRErrorMessage()
  476. extern size_t SaveEXRMultipartImageToMemory(const EXRImage *images,
  477. const EXRHeader **exr_headers,
  478. unsigned int num_parts,
  479. unsigned char **memory, const char **err);
  480. // Loads single-frame OpenEXR deep image.
  481. // Application must free memory of variables in DeepImage(image, offset_table)
  482. // Returns negative value and may set error string in `err` when there's an
  483. // error
  484. // When there was an error message, Application must free `err` with
  485. // FreeEXRErrorMessage()
  486. extern int LoadDeepEXR(DeepImage *out_image, const char *filename,
  487. const char **err);
  488. // NOT YET IMPLEMENTED:
  489. // Saves single-frame OpenEXR deep image.
  490. // Returns negative value and may set error string in `err` when there's an
  491. // error
  492. // extern int SaveDeepEXR(const DeepImage *in_image, const char *filename,
  493. // const char **err);
  494. // NOT YET IMPLEMENTED:
  495. // Loads multi-part OpenEXR deep image.
  496. // Application must free memory of variables in DeepImage(image, offset_table)
  497. // extern int LoadMultiPartDeepEXR(DeepImage **out_image, int num_parts, const
  498. // char *filename,
  499. // const char **err);
  500. // For emscripten.
  501. // Loads single-frame OpenEXR image from memory. Assume EXR image contains
  502. // RGB(A) channels.
  503. // Returns negative value and may set error string in `err` when there's an
  504. // error
  505. // When there was an error message, Application must free `err` with
  506. // FreeEXRErrorMessage()
  507. extern int LoadEXRFromMemory(float **out_rgba, int *width, int *height,
  508. const unsigned char *memory, size_t size,
  509. const char **err);
  510. #ifdef __cplusplus
  511. }
  512. #endif
  513. #endif // TINYEXR_H_
  514. #ifdef TINYEXR_IMPLEMENTATION
  515. #ifndef TINYEXR_IMPLEMENTATION_DEFINED
  516. #define TINYEXR_IMPLEMENTATION_DEFINED
  517. #ifdef _WIN32
  518. #ifndef WIN32_LEAN_AND_MEAN
  519. #define WIN32_LEAN_AND_MEAN
  520. #endif
  521. #ifndef NOMINMAX
  522. #define NOMINMAX
  523. #endif
  524. #include <windows.h> // for UTF-8 and memory-mapping
  525. #define TINYEXR_USE_WIN32_MMAP (1)
  526. #elif defined(__linux__) || defined(__unix__)
  527. #include <fcntl.h> // for open()
  528. #include <sys/mman.h> // for memory-mapping
  529. #include <sys/stat.h> // for stat
  530. #include <unistd.h> // for close()
  531. #define TINYEXR_USE_POSIX_MMAP (1)
  532. #endif
  533. #include <algorithm>
  534. #include <cstdio>
  535. #include <cstdlib>
  536. #include <cstring>
  537. #include <sstream>
  538. //#include <iostream> // debug
  539. #include <limits>
  540. #include <string>
  541. #include <vector>
  542. #include <set>
  543. // https://stackoverflow.com/questions/5047971/how-do-i-check-for-c11-support
  544. #if __cplusplus > 199711L || (defined(_MSC_VER) && _MSC_VER >= 1900)
  545. #define TINYEXR_HAS_CXX11 (1)
  546. // C++11
  547. #include <cstdint>
  548. #if TINYEXR_USE_THREAD
  549. #include <atomic>
  550. #include <thread>
  551. #endif
  552. #else // __cplusplus > 199711L
  553. #define TINYEXR_HAS_CXX11 (0)
  554. #endif // __cplusplus > 199711L
  555. #if TINYEXR_USE_OPENMP
  556. #include <omp.h>
  557. #endif
  558. #if TINYEXR_USE_MINIZ
  559. #include <miniz.h>
  560. #else
  561. // Issue #46. Please include your own zlib-compatible API header before
  562. // including `tinyexr.h`
  563. //#include "zlib.h"
  564. #endif
  565. #if TINYEXR_USE_STB_ZLIB
  566. // Since we don't know where a project has stb_image.h and stb_image_write.h
  567. // and whether they are in the include path, we don't include them here, and
  568. // instead declare the two relevant functions manually.
  569. // from stb_image.h:
  570. extern "C" int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
  571. // from stb_image_write.h:
  572. extern "C" unsigned char *stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality);
  573. #endif
  574. #if TINYEXR_USE_ZFP
  575. #ifdef __clang__
  576. #pragma clang diagnostic push
  577. #pragma clang diagnostic ignored "-Weverything"
  578. #endif
  579. #include "zfp.h"
  580. #ifdef __clang__
  581. #pragma clang diagnostic pop
  582. #endif
  583. #endif
  584. // cond: conditional expression
  585. // msg: std::string
  586. // err: std::string*
  587. #define TINYEXR_CHECK_AND_RETURN_MSG(cond, msg, err) do { \
  588. if (!(cond)) { \
  589. if (!err) { \
  590. std::ostringstream ss_e; \
  591. ss_e << __func__ << "():" << __LINE__ << msg << "\n"; \
  592. (*err) += ss_e.str(); \
  593. } \
  594. return false;\
  595. } \
  596. } while(0)
  597. // no error message.
  598. #define TINYEXR_CHECK_AND_RETURN_C(cond, retcode) do { \
  599. if (!(cond)) { \
  600. return retcode; \
  601. } \
  602. } while(0)
  603. namespace tinyexr {
  604. #if __cplusplus > 199711L
  605. // C++11
  606. typedef uint64_t tinyexr_uint64;
  607. typedef int64_t tinyexr_int64;
  608. #else
  609. // Although `long long` is not a standard type pre C++11, assume it is defined
  610. // as a compiler's extension.
  611. #ifdef __clang__
  612. #pragma clang diagnostic push
  613. #pragma clang diagnostic ignored "-Wc++11-long-long"
  614. #endif
  615. typedef unsigned long long tinyexr_uint64;
  616. typedef long long tinyexr_int64;
  617. #ifdef __clang__
  618. #pragma clang diagnostic pop
  619. #endif
  620. #endif
  621. // static bool IsBigEndian(void) {
  622. // union {
  623. // unsigned int i;
  624. // char c[4];
  625. // } bint = {0x01020304};
  626. //
  627. // return bint.c[0] == 1;
  628. //}
  629. static void SetErrorMessage(const std::string &msg, const char **err) {
  630. if (err) {
  631. #ifdef _WIN32
  632. (*err) = _strdup(msg.c_str());
  633. #else
  634. (*err) = strdup(msg.c_str());
  635. #endif
  636. }
  637. }
  638. #if 0
  639. static void SetWarningMessage(const std::string &msg, const char **warn) {
  640. if (warn) {
  641. #ifdef _WIN32
  642. (*warn) = _strdup(msg.c_str());
  643. #else
  644. (*warn) = strdup(msg.c_str());
  645. #endif
  646. }
  647. }
  648. #endif
  649. static const int kEXRVersionSize = 8;
  650. static void cpy2(unsigned short *dst_val, const unsigned short *src_val) {
  651. unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
  652. const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
  653. dst[0] = src[0];
  654. dst[1] = src[1];
  655. }
  656. static void swap2(unsigned short *val) {
  657. #ifdef TINYEXR_LITTLE_ENDIAN
  658. (void)val;
  659. #else
  660. unsigned short tmp = *val;
  661. unsigned char *dst = reinterpret_cast<unsigned char *>(val);
  662. unsigned char *src = reinterpret_cast<unsigned char *>(&tmp);
  663. dst[0] = src[1];
  664. dst[1] = src[0];
  665. #endif
  666. }
  667. #ifdef __clang__
  668. #pragma clang diagnostic push
  669. #pragma clang diagnostic ignored "-Wunused-function"
  670. #endif
  671. #ifdef __GNUC__
  672. #pragma GCC diagnostic push
  673. #pragma GCC diagnostic ignored "-Wunused-function"
  674. #endif
  675. static void cpy4(int *dst_val, const int *src_val) {
  676. unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
  677. const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
  678. dst[0] = src[0];
  679. dst[1] = src[1];
  680. dst[2] = src[2];
  681. dst[3] = src[3];
  682. }
  683. static void cpy4(unsigned int *dst_val, const unsigned int *src_val) {
  684. unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
  685. const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
  686. dst[0] = src[0];
  687. dst[1] = src[1];
  688. dst[2] = src[2];
  689. dst[3] = src[3];
  690. }
  691. static void cpy4(float *dst_val, const float *src_val) {
  692. unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
  693. const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
  694. dst[0] = src[0];
  695. dst[1] = src[1];
  696. dst[2] = src[2];
  697. dst[3] = src[3];
  698. }
  699. #ifdef __clang__
  700. #pragma clang diagnostic pop
  701. #endif
  702. #ifdef __GNUC__
  703. #pragma GCC diagnostic pop
  704. #endif
  705. static void swap4(unsigned int *val) {
  706. #ifdef TINYEXR_LITTLE_ENDIAN
  707. (void)val;
  708. #else
  709. unsigned int tmp = *val;
  710. unsigned char *dst = reinterpret_cast<unsigned char *>(val);
  711. unsigned char *src = reinterpret_cast<unsigned char *>(&tmp);
  712. dst[0] = src[3];
  713. dst[1] = src[2];
  714. dst[2] = src[1];
  715. dst[3] = src[0];
  716. #endif
  717. }
  718. static void swap4(int *val) {
  719. #ifdef TINYEXR_LITTLE_ENDIAN
  720. (void)val;
  721. #else
  722. int tmp = *val;
  723. unsigned char *dst = reinterpret_cast<unsigned char *>(val);
  724. unsigned char *src = reinterpret_cast<unsigned char *>(&tmp);
  725. dst[0] = src[3];
  726. dst[1] = src[2];
  727. dst[2] = src[1];
  728. dst[3] = src[0];
  729. #endif
  730. }
  731. static void swap4(float *val) {
  732. #ifdef TINYEXR_LITTLE_ENDIAN
  733. (void)val;
  734. #else
  735. float tmp = *val;
  736. unsigned char *dst = reinterpret_cast<unsigned char *>(val);
  737. unsigned char *src = reinterpret_cast<unsigned char *>(&tmp);
  738. dst[0] = src[3];
  739. dst[1] = src[2];
  740. dst[2] = src[1];
  741. dst[3] = src[0];
  742. #endif
  743. }
  744. #if 0
  745. static void cpy8(tinyexr::tinyexr_uint64 *dst_val, const tinyexr::tinyexr_uint64 *src_val) {
  746. unsigned char *dst = reinterpret_cast<unsigned char *>(dst_val);
  747. const unsigned char *src = reinterpret_cast<const unsigned char *>(src_val);
  748. dst[0] = src[0];
  749. dst[1] = src[1];
  750. dst[2] = src[2];
  751. dst[3] = src[3];
  752. dst[4] = src[4];
  753. dst[5] = src[5];
  754. dst[6] = src[6];
  755. dst[7] = src[7];
  756. }
  757. #endif
  758. static void swap8(tinyexr::tinyexr_uint64 *val) {
  759. #ifdef TINYEXR_LITTLE_ENDIAN
  760. (void)val;
  761. #else
  762. tinyexr::tinyexr_uint64 tmp = (*val);
  763. unsigned char *dst = reinterpret_cast<unsigned char *>(val);
  764. unsigned char *src = reinterpret_cast<unsigned char *>(&tmp);
  765. dst[0] = src[7];
  766. dst[1] = src[6];
  767. dst[2] = src[5];
  768. dst[3] = src[4];
  769. dst[4] = src[3];
  770. dst[5] = src[2];
  771. dst[6] = src[1];
  772. dst[7] = src[0];
  773. #endif
  774. }
  775. // https://gist.github.com/rygorous/2156668
  776. union FP32 {
  777. unsigned int u;
  778. float f;
  779. struct {
  780. #if TINYEXR_LITTLE_ENDIAN
  781. unsigned int Mantissa : 23;
  782. unsigned int Exponent : 8;
  783. unsigned int Sign : 1;
  784. #else
  785. unsigned int Sign : 1;
  786. unsigned int Exponent : 8;
  787. unsigned int Mantissa : 23;
  788. #endif
  789. } s;
  790. };
  791. #ifdef __clang__
  792. #pragma clang diagnostic push
  793. #pragma clang diagnostic ignored "-Wpadded"
  794. #endif
  795. union FP16 {
  796. unsigned short u;
  797. struct {
  798. #if TINYEXR_LITTLE_ENDIAN
  799. unsigned int Mantissa : 10;
  800. unsigned int Exponent : 5;
  801. unsigned int Sign : 1;
  802. #else
  803. unsigned int Sign : 1;
  804. unsigned int Exponent : 5;
  805. unsigned int Mantissa : 10;
  806. #endif
  807. } s;
  808. };
  809. #ifdef __clang__
  810. #pragma clang diagnostic pop
  811. #endif
  812. static FP32 half_to_float(FP16 h) {
  813. static const FP32 magic = {113 << 23};
  814. static const unsigned int shifted_exp = 0x7c00
  815. << 13; // exponent mask after shift
  816. FP32 o;
  817. o.u = (h.u & 0x7fffU) << 13U; // exponent/mantissa bits
  818. unsigned int exp_ = shifted_exp & o.u; // just the exponent
  819. o.u += (127 - 15) << 23; // exponent adjust
  820. // handle exponent special cases
  821. if (exp_ == shifted_exp) // Inf/NaN?
  822. o.u += (128 - 16) << 23; // extra exp adjust
  823. else if (exp_ == 0) // Zero/Denormal?
  824. {
  825. o.u += 1 << 23; // extra exp adjust
  826. o.f -= magic.f; // renormalize
  827. }
  828. o.u |= (h.u & 0x8000U) << 16U; // sign bit
  829. return o;
  830. }
  831. static FP16 float_to_half_full(FP32 f) {
  832. FP16 o = {0};
  833. // Based on ISPC reference code (with minor modifications)
  834. if (f.s.Exponent == 0) // Signed zero/denormal (which will underflow)
  835. o.s.Exponent = 0;
  836. else if (f.s.Exponent == 255) // Inf or NaN (all exponent bits set)
  837. {
  838. o.s.Exponent = 31;
  839. o.s.Mantissa = f.s.Mantissa ? 0x200 : 0; // NaN->qNaN and Inf->Inf
  840. } else // Normalized number
  841. {
  842. // Exponent unbias the single, then bias the halfp
  843. int newexp = f.s.Exponent - 127 + 15;
  844. if (newexp >= 31) // Overflow, return signed infinity
  845. o.s.Exponent = 31;
  846. else if (newexp <= 0) // Underflow
  847. {
  848. if ((14 - newexp) <= 24) // Mantissa might be non-zero
  849. {
  850. unsigned int mant = f.s.Mantissa | 0x800000; // Hidden 1 bit
  851. o.s.Mantissa = mant >> (14 - newexp);
  852. if ((mant >> (13 - newexp)) & 1) // Check for rounding
  853. o.u++; // Round, might overflow into exp bit, but this is OK
  854. }
  855. } else {
  856. o.s.Exponent = static_cast<unsigned int>(newexp);
  857. o.s.Mantissa = f.s.Mantissa >> 13;
  858. if (f.s.Mantissa & 0x1000) // Check for rounding
  859. o.u++; // Round, might overflow to inf, this is OK
  860. }
  861. }
  862. o.s.Sign = f.s.Sign;
  863. return o;
  864. }
  865. // NOTE: From OpenEXR code
  866. // #define IMF_INCREASING_Y 0
  867. // #define IMF_DECREASING_Y 1
  868. // #define IMF_RAMDOM_Y 2
  869. //
  870. // #define IMF_NO_COMPRESSION 0
  871. // #define IMF_RLE_COMPRESSION 1
  872. // #define IMF_ZIPS_COMPRESSION 2
  873. // #define IMF_ZIP_COMPRESSION 3
  874. // #define IMF_PIZ_COMPRESSION 4
  875. // #define IMF_PXR24_COMPRESSION 5
  876. // #define IMF_B44_COMPRESSION 6
  877. // #define IMF_B44A_COMPRESSION 7
  878. #ifdef __clang__
  879. #pragma clang diagnostic push
  880. #if __has_warning("-Wzero-as-null-pointer-constant")
  881. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
  882. #endif
  883. #endif
  884. static const char *ReadString(std::string *s, const char *ptr, size_t len) {
  885. // Read untile NULL(\0).
  886. const char *p = ptr;
  887. const char *q = ptr;
  888. while ((size_t(q - ptr) < len) && (*q) != 0) {
  889. q++;
  890. }
  891. if (size_t(q - ptr) >= len) {
  892. (*s).clear();
  893. return NULL;
  894. }
  895. (*s) = std::string(p, q);
  896. return q + 1; // skip '\0'
  897. }
  898. static bool ReadAttribute(std::string *name, std::string *type,
  899. std::vector<unsigned char> *data, size_t *marker_size,
  900. const char *marker, size_t size) {
  901. size_t name_len = strnlen(marker, size);
  902. if (name_len == size) {
  903. // String does not have a terminating character.
  904. return false;
  905. }
  906. *name = std::string(marker, name_len);
  907. marker += name_len + 1;
  908. size -= name_len + 1;
  909. size_t type_len = strnlen(marker, size);
  910. if (type_len == size) {
  911. return false;
  912. }
  913. *type = std::string(marker, type_len);
  914. marker += type_len + 1;
  915. size -= type_len + 1;
  916. if (size < sizeof(uint32_t)) {
  917. return false;
  918. }
  919. uint32_t data_len;
  920. memcpy(&data_len, marker, sizeof(uint32_t));
  921. tinyexr::swap4(reinterpret_cast<unsigned int *>(&data_len));
  922. if (data_len == 0) {
  923. if ((*type).compare("string") == 0) {
  924. // Accept empty string attribute.
  925. marker += sizeof(uint32_t);
  926. size -= sizeof(uint32_t);
  927. *marker_size = name_len + 1 + type_len + 1 + sizeof(uint32_t);
  928. data->resize(1);
  929. (*data)[0] = '\0';
  930. return true;
  931. } else {
  932. return false;
  933. }
  934. }
  935. marker += sizeof(uint32_t);
  936. size -= sizeof(uint32_t);
  937. if (size < data_len) {
  938. return false;
  939. }
  940. data->resize(static_cast<size_t>(data_len));
  941. memcpy(&data->at(0), marker, static_cast<size_t>(data_len));
  942. *marker_size = name_len + 1 + type_len + 1 + sizeof(uint32_t) + data_len;
  943. return true;
  944. }
  945. static void WriteAttributeToMemory(std::vector<unsigned char> *out,
  946. const char *name, const char *type,
  947. const unsigned char *data, int len) {
  948. out->insert(out->end(), name, name + strlen(name) + 1);
  949. out->insert(out->end(), type, type + strlen(type) + 1);
  950. int outLen = len;
  951. tinyexr::swap4(&outLen);
  952. out->insert(out->end(), reinterpret_cast<unsigned char *>(&outLen),
  953. reinterpret_cast<unsigned char *>(&outLen) + sizeof(int));
  954. out->insert(out->end(), data, data + len);
  955. }
  956. typedef struct TChannelInfo {
  957. std::string name; // less than 255 bytes long
  958. int pixel_type;
  959. int requested_pixel_type;
  960. int x_sampling;
  961. int y_sampling;
  962. unsigned char p_linear;
  963. unsigned char pad[3];
  964. } ChannelInfo;
  965. typedef struct {
  966. int min_x;
  967. int min_y;
  968. int max_x;
  969. int max_y;
  970. } Box2iInfo;
  971. struct HeaderInfo {
  972. std::vector<tinyexr::ChannelInfo> channels;
  973. std::vector<EXRAttribute> attributes;
  974. Box2iInfo data_window;
  975. int line_order;
  976. Box2iInfo display_window;
  977. float screen_window_center[2];
  978. float screen_window_width;
  979. float pixel_aspect_ratio;
  980. int chunk_count;
  981. // Tiled format
  982. int tiled; // Non-zero if the part is tiled.
  983. int tile_size_x;
  984. int tile_size_y;
  985. int tile_level_mode;
  986. int tile_rounding_mode;
  987. unsigned int header_len;
  988. int compression_type;
  989. // required for multi-part or non-image files
  990. std::string name;
  991. // required for multi-part or non-image files
  992. std::string type;
  993. void clear() {
  994. channels.clear();
  995. attributes.clear();
  996. data_window.min_x = 0;
  997. data_window.min_y = 0;
  998. data_window.max_x = 0;
  999. data_window.max_y = 0;
  1000. line_order = 0;
  1001. display_window.min_x = 0;
  1002. display_window.min_y = 0;
  1003. display_window.max_x = 0;
  1004. display_window.max_y = 0;
  1005. screen_window_center[0] = 0.0f;
  1006. screen_window_center[1] = 0.0f;
  1007. screen_window_width = 0.0f;
  1008. pixel_aspect_ratio = 0.0f;
  1009. chunk_count = 0;
  1010. // Tiled format
  1011. tiled = 0;
  1012. tile_size_x = 0;
  1013. tile_size_y = 0;
  1014. tile_level_mode = 0;
  1015. tile_rounding_mode = 0;
  1016. header_len = 0;
  1017. compression_type = 0;
  1018. name.clear();
  1019. type.clear();
  1020. }
  1021. };
  1022. static bool ReadChannelInfo(std::vector<ChannelInfo> &channels,
  1023. const std::vector<unsigned char> &data) {
  1024. const char *p = reinterpret_cast<const char *>(&data.at(0));
  1025. for (;;) {
  1026. if ((*p) == 0) {
  1027. break;
  1028. }
  1029. ChannelInfo info;
  1030. info.requested_pixel_type = 0;
  1031. tinyexr_int64 data_len = static_cast<tinyexr_int64>(data.size()) -
  1032. (p - reinterpret_cast<const char *>(data.data()));
  1033. if (data_len < 0) {
  1034. return false;
  1035. }
  1036. p = ReadString(&info.name, p, size_t(data_len));
  1037. if ((p == NULL) && (info.name.empty())) {
  1038. // Buffer overrun. Issue #51.
  1039. return false;
  1040. }
  1041. const unsigned char *data_end =
  1042. reinterpret_cast<const unsigned char *>(p) + 16;
  1043. if (data_end >= (data.data() + data.size())) {
  1044. return false;
  1045. }
  1046. memcpy(&info.pixel_type, p, sizeof(int));
  1047. p += 4;
  1048. info.p_linear = static_cast<unsigned char>(p[0]); // uchar
  1049. p += 1 + 3; // reserved: uchar[3]
  1050. memcpy(&info.x_sampling, p, sizeof(int)); // int
  1051. p += 4;
  1052. memcpy(&info.y_sampling, p, sizeof(int)); // int
  1053. p += 4;
  1054. tinyexr::swap4(&info.pixel_type);
  1055. tinyexr::swap4(&info.x_sampling);
  1056. tinyexr::swap4(&info.y_sampling);
  1057. channels.push_back(info);
  1058. }
  1059. return true;
  1060. }
  1061. static void WriteChannelInfo(std::vector<unsigned char> &data,
  1062. const std::vector<ChannelInfo> &channels) {
  1063. size_t sz = 0;
  1064. // Calculate total size.
  1065. for (size_t c = 0; c < channels.size(); c++) {
  1066. sz += channels[c].name.length() + 1; // +1 for \0
  1067. sz += 16; // 4 * int
  1068. }
  1069. data.resize(sz + 1);
  1070. unsigned char *p = &data.at(0);
  1071. for (size_t c = 0; c < channels.size(); c++) {
  1072. memcpy(p, channels[c].name.c_str(), channels[c].name.length());
  1073. p += channels[c].name.length();
  1074. (*p) = '\0';
  1075. p++;
  1076. int pixel_type = channels[c].requested_pixel_type;
  1077. int x_sampling = channels[c].x_sampling;
  1078. int y_sampling = channels[c].y_sampling;
  1079. tinyexr::swap4(&pixel_type);
  1080. tinyexr::swap4(&x_sampling);
  1081. tinyexr::swap4(&y_sampling);
  1082. memcpy(p, &pixel_type, sizeof(int));
  1083. p += sizeof(int);
  1084. (*p) = channels[c].p_linear;
  1085. p += 4;
  1086. memcpy(p, &x_sampling, sizeof(int));
  1087. p += sizeof(int);
  1088. memcpy(p, &y_sampling, sizeof(int));
  1089. p += sizeof(int);
  1090. }
  1091. (*p) = '\0';
  1092. }
  1093. static bool CompressZip(unsigned char *dst,
  1094. tinyexr::tinyexr_uint64 &compressedSize,
  1095. const unsigned char *src, unsigned long src_size) {
  1096. std::vector<unsigned char> tmpBuf(src_size);
  1097. //
  1098. // Apply EXR-specific? postprocess. Grabbed from OpenEXR's
  1099. // ImfZipCompressor.cpp
  1100. //
  1101. //
  1102. // Reorder the pixel data.
  1103. //
  1104. const char *srcPtr = reinterpret_cast<const char *>(src);
  1105. {
  1106. char *t1 = reinterpret_cast<char *>(&tmpBuf.at(0));
  1107. char *t2 = reinterpret_cast<char *>(&tmpBuf.at(0)) + (src_size + 1) / 2;
  1108. const char *stop = srcPtr + src_size;
  1109. for (;;) {
  1110. if (srcPtr < stop)
  1111. *(t1++) = *(srcPtr++);
  1112. else
  1113. break;
  1114. if (srcPtr < stop)
  1115. *(t2++) = *(srcPtr++);
  1116. else
  1117. break;
  1118. }
  1119. }
  1120. //
  1121. // Predictor.
  1122. //
  1123. {
  1124. unsigned char *t = &tmpBuf.at(0) + 1;
  1125. unsigned char *stop = &tmpBuf.at(0) + src_size;
  1126. int p = t[-1];
  1127. while (t < stop) {
  1128. int d = int(t[0]) - p + (128 + 256);
  1129. p = t[0];
  1130. t[0] = static_cast<unsigned char>(d);
  1131. ++t;
  1132. }
  1133. }
  1134. #if TINYEXR_USE_MINIZ
  1135. //
  1136. // Compress the data using miniz
  1137. //
  1138. mz_ulong outSize = mz_compressBound(src_size);
  1139. int ret = mz_compress(
  1140. dst, &outSize, static_cast<const unsigned char *>(&tmpBuf.at(0)),
  1141. src_size);
  1142. if (ret != MZ_OK) {
  1143. return false;
  1144. }
  1145. compressedSize = outSize;
  1146. #elif TINYEXR_USE_STB_ZLIB
  1147. int outSize;
  1148. unsigned char* ret = stbi_zlib_compress(const_cast<unsigned char*>(&tmpBuf.at(0)), src_size, &outSize, 8);
  1149. if (!ret) {
  1150. return false;
  1151. }
  1152. memcpy(dst, ret, outSize);
  1153. free(ret);
  1154. compressedSize = outSize;
  1155. #else
  1156. uLong outSize = compressBound(static_cast<uLong>(src_size));
  1157. int ret = compress(dst, &outSize, static_cast<const Bytef *>(&tmpBuf.at(0)),
  1158. src_size);
  1159. if (ret != Z_OK) {
  1160. return false;
  1161. }
  1162. compressedSize = outSize;
  1163. #endif
  1164. // Use uncompressed data when compressed data is larger than uncompressed.
  1165. // (Issue 40)
  1166. if (compressedSize >= src_size) {
  1167. compressedSize = src_size;
  1168. memcpy(dst, src, src_size);
  1169. }
  1170. return true;
  1171. }
  1172. static bool DecompressZip(unsigned char *dst,
  1173. unsigned long *uncompressed_size /* inout */,
  1174. const unsigned char *src, unsigned long src_size) {
  1175. if ((*uncompressed_size) == src_size) {
  1176. // Data is not compressed(Issue 40).
  1177. memcpy(dst, src, src_size);
  1178. return true;
  1179. }
  1180. std::vector<unsigned char> tmpBuf(*uncompressed_size);
  1181. #if TINYEXR_USE_MINIZ
  1182. int ret =
  1183. mz_uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size);
  1184. if (MZ_OK != ret) {
  1185. return false;
  1186. }
  1187. #elif TINYEXR_USE_STB_ZLIB
  1188. int ret = stbi_zlib_decode_buffer(reinterpret_cast<char*>(&tmpBuf.at(0)),
  1189. *uncompressed_size, reinterpret_cast<const char*>(src), src_size);
  1190. if (ret < 0) {
  1191. return false;
  1192. }
  1193. #else
  1194. int ret = uncompress(&tmpBuf.at(0), uncompressed_size, src, src_size);
  1195. if (Z_OK != ret) {
  1196. return false;
  1197. }
  1198. #endif
  1199. //
  1200. // Apply EXR-specific? postprocess. Grabbed from OpenEXR's
  1201. // ImfZipCompressor.cpp
  1202. //
  1203. // Predictor.
  1204. {
  1205. unsigned char *t = &tmpBuf.at(0) + 1;
  1206. unsigned char *stop = &tmpBuf.at(0) + (*uncompressed_size);
  1207. while (t < stop) {
  1208. int d = int(t[-1]) + int(t[0]) - 128;
  1209. t[0] = static_cast<unsigned char>(d);
  1210. ++t;
  1211. }
  1212. }
  1213. // Reorder the pixel data.
  1214. {
  1215. const char *t1 = reinterpret_cast<const char *>(&tmpBuf.at(0));
  1216. const char *t2 = reinterpret_cast<const char *>(&tmpBuf.at(0)) +
  1217. (*uncompressed_size + 1) / 2;
  1218. char *s = reinterpret_cast<char *>(dst);
  1219. char *stop = s + (*uncompressed_size);
  1220. for (;;) {
  1221. if (s < stop)
  1222. *(s++) = *(t1++);
  1223. else
  1224. break;
  1225. if (s < stop)
  1226. *(s++) = *(t2++);
  1227. else
  1228. break;
  1229. }
  1230. }
  1231. return true;
  1232. }
  1233. // RLE code from OpenEXR --------------------------------------
  1234. #ifdef __clang__
  1235. #pragma clang diagnostic push
  1236. #pragma clang diagnostic ignored "-Wsign-conversion"
  1237. #if __has_warning("-Wextra-semi-stmt")
  1238. #pragma clang diagnostic ignored "-Wextra-semi-stmt"
  1239. #endif
  1240. #endif
  1241. #ifdef _MSC_VER
  1242. #pragma warning(push)
  1243. #pragma warning(disable : 4204) // nonstandard extension used : non-constant
  1244. // aggregate initializer (also supported by GNU
  1245. // C and C99, so no big deal)
  1246. #pragma warning(disable : 4244) // 'initializing': conversion from '__int64' to
  1247. // 'int', possible loss of data
  1248. #pragma warning(disable : 4267) // 'argument': conversion from '__int64' to
  1249. // 'int', possible loss of data
  1250. #pragma warning(disable : 4996) // 'strdup': The POSIX name for this item is
  1251. // deprecated. Instead, use the ISO C and C++
  1252. // conformant name: _strdup.
  1253. #endif
  1254. const int MIN_RUN_LENGTH = 3;
  1255. const int MAX_RUN_LENGTH = 127;
  1256. //
  1257. // Compress an array of bytes, using run-length encoding,
  1258. // and return the length of the compressed data.
  1259. //
  1260. static int rleCompress(int inLength, const char in[], signed char out[]) {
  1261. const char *inEnd = in + inLength;
  1262. const char *runStart = in;
  1263. const char *runEnd = in + 1;
  1264. signed char *outWrite = out;
  1265. while (runStart < inEnd) {
  1266. while (runEnd < inEnd && *runStart == *runEnd &&
  1267. runEnd - runStart - 1 < MAX_RUN_LENGTH) {
  1268. ++runEnd;
  1269. }
  1270. if (runEnd - runStart >= MIN_RUN_LENGTH) {
  1271. //
  1272. // Compressible run
  1273. //
  1274. *outWrite++ = static_cast<char>(runEnd - runStart) - 1;
  1275. *outWrite++ = *(reinterpret_cast<const signed char *>(runStart));
  1276. runStart = runEnd;
  1277. } else {
  1278. //
  1279. // Uncompressable run
  1280. //
  1281. while (runEnd < inEnd &&
  1282. ((runEnd + 1 >= inEnd || *runEnd != *(runEnd + 1)) ||
  1283. (runEnd + 2 >= inEnd || *(runEnd + 1) != *(runEnd + 2))) &&
  1284. runEnd - runStart < MAX_RUN_LENGTH) {
  1285. ++runEnd;
  1286. }
  1287. *outWrite++ = static_cast<char>(runStart - runEnd);
  1288. while (runStart < runEnd) {
  1289. *outWrite++ = *(reinterpret_cast<const signed char *>(runStart++));
  1290. }
  1291. }
  1292. ++runEnd;
  1293. }
  1294. return static_cast<int>(outWrite - out);
  1295. }
  1296. //
  1297. // Uncompress an array of bytes compressed with rleCompress().
  1298. // Returns the length of the uncompressed data, or 0 if the
  1299. // length of the uncompressed data would be more than maxLength.
  1300. //
  1301. static int rleUncompress(int inLength, int maxLength, const signed char in[],
  1302. char out[]) {
  1303. char *outStart = out;
  1304. while (inLength > 0) {
  1305. if (*in < 0) {
  1306. int count = -(static_cast<int>(*in++));
  1307. inLength -= count + 1;
  1308. // Fixes #116: Add bounds check to in buffer.
  1309. if ((0 > (maxLength -= count)) || (inLength < 0)) return 0;
  1310. memcpy(out, in, count);
  1311. out += count;
  1312. in += count;
  1313. } else {
  1314. int count = *in++;
  1315. inLength -= 2;
  1316. if ((0 > (maxLength -= count + 1)) || (inLength < 0)) return 0;
  1317. memset(out, *reinterpret_cast<const char *>(in), count + 1);
  1318. out += count + 1;
  1319. in++;
  1320. }
  1321. }
  1322. return static_cast<int>(out - outStart);
  1323. }
  1324. #ifdef __clang__
  1325. #pragma clang diagnostic pop
  1326. #endif
  1327. // End of RLE code from OpenEXR -----------------------------------
  1328. static bool CompressRle(unsigned char *dst,
  1329. tinyexr::tinyexr_uint64 &compressedSize,
  1330. const unsigned char *src, unsigned long src_size) {
  1331. std::vector<unsigned char> tmpBuf(src_size);
  1332. //
  1333. // Apply EXR-specific? postprocess. Grabbed from OpenEXR's
  1334. // ImfRleCompressor.cpp
  1335. //
  1336. //
  1337. // Reorder the pixel data.
  1338. //
  1339. const char *srcPtr = reinterpret_cast<const char *>(src);
  1340. {
  1341. char *t1 = reinterpret_cast<char *>(&tmpBuf.at(0));
  1342. char *t2 = reinterpret_cast<char *>(&tmpBuf.at(0)) + (src_size + 1) / 2;
  1343. const char *stop = srcPtr + src_size;
  1344. for (;;) {
  1345. if (srcPtr < stop)
  1346. *(t1++) = *(srcPtr++);
  1347. else
  1348. break;
  1349. if (srcPtr < stop)
  1350. *(t2++) = *(srcPtr++);
  1351. else
  1352. break;
  1353. }
  1354. }
  1355. //
  1356. // Predictor.
  1357. //
  1358. {
  1359. unsigned char *t = &tmpBuf.at(0) + 1;
  1360. unsigned char *stop = &tmpBuf.at(0) + src_size;
  1361. int p = t[-1];
  1362. while (t < stop) {
  1363. int d = int(t[0]) - p + (128 + 256);
  1364. p = t[0];
  1365. t[0] = static_cast<unsigned char>(d);
  1366. ++t;
  1367. }
  1368. }
  1369. // outSize will be (srcSiz * 3) / 2 at max.
  1370. int outSize = rleCompress(static_cast<int>(src_size),
  1371. reinterpret_cast<const char *>(&tmpBuf.at(0)),
  1372. reinterpret_cast<signed char *>(dst));
  1373. TINYEXR_CHECK_AND_RETURN_C(outSize > 0, false);
  1374. compressedSize = static_cast<tinyexr::tinyexr_uint64>(outSize);
  1375. // Use uncompressed data when compressed data is larger than uncompressed.
  1376. // (Issue 40)
  1377. if (compressedSize >= src_size) {
  1378. compressedSize = src_size;
  1379. memcpy(dst, src, src_size);
  1380. }
  1381. return true;
  1382. }
  1383. static bool DecompressRle(unsigned char *dst,
  1384. const unsigned long uncompressed_size,
  1385. const unsigned char *src, unsigned long src_size) {
  1386. if (uncompressed_size == src_size) {
  1387. // Data is not compressed(Issue 40).
  1388. memcpy(dst, src, src_size);
  1389. return true;
  1390. }
  1391. // Workaround for issue #112.
  1392. // TODO(syoyo): Add more robust out-of-bounds check in `rleUncompress`.
  1393. if (src_size <= 2) {
  1394. return false;
  1395. }
  1396. std::vector<unsigned char> tmpBuf(uncompressed_size);
  1397. int ret = rleUncompress(static_cast<int>(src_size),
  1398. static_cast<int>(uncompressed_size),
  1399. reinterpret_cast<const signed char *>(src),
  1400. reinterpret_cast<char *>(&tmpBuf.at(0)));
  1401. if (ret != static_cast<int>(uncompressed_size)) {
  1402. return false;
  1403. }
  1404. //
  1405. // Apply EXR-specific? postprocess. Grabbed from OpenEXR's
  1406. // ImfRleCompressor.cpp
  1407. //
  1408. // Predictor.
  1409. {
  1410. unsigned char *t = &tmpBuf.at(0) + 1;
  1411. unsigned char *stop = &tmpBuf.at(0) + uncompressed_size;
  1412. while (t < stop) {
  1413. int d = int(t[-1]) + int(t[0]) - 128;
  1414. t[0] = static_cast<unsigned char>(d);
  1415. ++t;
  1416. }
  1417. }
  1418. // Reorder the pixel data.
  1419. {
  1420. const char *t1 = reinterpret_cast<const char *>(&tmpBuf.at(0));
  1421. const char *t2 = reinterpret_cast<const char *>(&tmpBuf.at(0)) +
  1422. (uncompressed_size + 1) / 2;
  1423. char *s = reinterpret_cast<char *>(dst);
  1424. char *stop = s + uncompressed_size;
  1425. for (;;) {
  1426. if (s < stop)
  1427. *(s++) = *(t1++);
  1428. else
  1429. break;
  1430. if (s < stop)
  1431. *(s++) = *(t2++);
  1432. else
  1433. break;
  1434. }
  1435. }
  1436. return true;
  1437. }
  1438. #if TINYEXR_USE_PIZ
  1439. #ifdef __clang__
  1440. #pragma clang diagnostic push
  1441. #pragma clang diagnostic ignored "-Wc++11-long-long"
  1442. #pragma clang diagnostic ignored "-Wold-style-cast"
  1443. #pragma clang diagnostic ignored "-Wpadded"
  1444. #pragma clang diagnostic ignored "-Wsign-conversion"
  1445. #pragma clang diagnostic ignored "-Wc++11-extensions"
  1446. #pragma clang diagnostic ignored "-Wconversion"
  1447. #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
  1448. #if __has_warning("-Wcast-qual")
  1449. #pragma clang diagnostic ignored "-Wcast-qual"
  1450. #endif
  1451. #if __has_warning("-Wextra-semi-stmt")
  1452. #pragma clang diagnostic ignored "-Wextra-semi-stmt"
  1453. #endif
  1454. #endif
  1455. //
  1456. // PIZ compress/uncompress, based on OpenEXR's ImfPizCompressor.cpp
  1457. //
  1458. // -----------------------------------------------------------------
  1459. // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
  1460. // Digital Ltd. LLC)
  1461. // (3 clause BSD license)
  1462. //
  1463. struct PIZChannelData {
  1464. unsigned short *start;
  1465. unsigned short *end;
  1466. int nx;
  1467. int ny;
  1468. int ys;
  1469. int size;
  1470. };
  1471. //-----------------------------------------------------------------------------
  1472. //
  1473. // 16-bit Haar Wavelet encoding and decoding
  1474. //
  1475. // The source code in this file is derived from the encoding
  1476. // and decoding routines written by Christian Rouet for his
  1477. // PIZ image file format.
  1478. //
  1479. //-----------------------------------------------------------------------------
  1480. //
  1481. // Wavelet basis functions without modulo arithmetic; they produce
  1482. // the best compression ratios when the wavelet-transformed data are
  1483. // Huffman-encoded, but the wavelet transform works only for 14-bit
  1484. // data (untransformed data values must be less than (1 << 14)).
  1485. //
  1486. inline void wenc14(unsigned short a, unsigned short b, unsigned short &l,
  1487. unsigned short &h) {
  1488. short as = static_cast<short>(a);
  1489. short bs = static_cast<short>(b);
  1490. short ms = (as + bs) >> 1;
  1491. short ds = as - bs;
  1492. l = static_cast<unsigned short>(ms);
  1493. h = static_cast<unsigned short>(ds);
  1494. }
  1495. inline void wdec14(unsigned short l, unsigned short h, unsigned short &a,
  1496. unsigned short &b) {
  1497. short ls = static_cast<short>(l);
  1498. short hs = static_cast<short>(h);
  1499. int hi = hs;
  1500. int ai = ls + (hi & 1) + (hi >> 1);
  1501. short as = static_cast<short>(ai);
  1502. short bs = static_cast<short>(ai - hi);
  1503. a = static_cast<unsigned short>(as);
  1504. b = static_cast<unsigned short>(bs);
  1505. }
  1506. //
  1507. // Wavelet basis functions with modulo arithmetic; they work with full
  1508. // 16-bit data, but Huffman-encoding the wavelet-transformed data doesn't
  1509. // compress the data quite as well.
  1510. //
  1511. const int NBITS = 16;
  1512. const int A_OFFSET = 1 << (NBITS - 1);
  1513. const int M_OFFSET = 1 << (NBITS - 1);
  1514. const int MOD_MASK = (1 << NBITS) - 1;
  1515. inline void wenc16(unsigned short a, unsigned short b, unsigned short &l,
  1516. unsigned short &h) {
  1517. int ao = (a + A_OFFSET) & MOD_MASK;
  1518. int m = ((ao + b) >> 1);
  1519. int d = ao - b;
  1520. if (d < 0) m = (m + M_OFFSET) & MOD_MASK;
  1521. d &= MOD_MASK;
  1522. l = static_cast<unsigned short>(m);
  1523. h = static_cast<unsigned short>(d);
  1524. }
  1525. inline void wdec16(unsigned short l, unsigned short h, unsigned short &a,
  1526. unsigned short &b) {
  1527. int m = l;
  1528. int d = h;
  1529. int bb = (m - (d >> 1)) & MOD_MASK;
  1530. int aa = (d + bb - A_OFFSET) & MOD_MASK;
  1531. b = static_cast<unsigned short>(bb);
  1532. a = static_cast<unsigned short>(aa);
  1533. }
  1534. //
  1535. // 2D Wavelet encoding:
  1536. //
  1537. static void wav2Encode(
  1538. unsigned short *in, // io: values are transformed in place
  1539. int nx, // i : x size
  1540. int ox, // i : x offset
  1541. int ny, // i : y size
  1542. int oy, // i : y offset
  1543. unsigned short mx) // i : maximum in[x][y] value
  1544. {
  1545. bool w14 = (mx < (1 << 14));
  1546. int n = (nx > ny) ? ny : nx;
  1547. int p = 1; // == 1 << level
  1548. int p2 = 2; // == 1 << (level+1)
  1549. //
  1550. // Hierarchical loop on smaller dimension n
  1551. //
  1552. while (p2 <= n) {
  1553. unsigned short *py = in;
  1554. unsigned short *ey = in + oy * (ny - p2);
  1555. int oy1 = oy * p;
  1556. int oy2 = oy * p2;
  1557. int ox1 = ox * p;
  1558. int ox2 = ox * p2;
  1559. unsigned short i00, i01, i10, i11;
  1560. //
  1561. // Y loop
  1562. //
  1563. for (; py <= ey; py += oy2) {
  1564. unsigned short *px = py;
  1565. unsigned short *ex = py + ox * (nx - p2);
  1566. //
  1567. // X loop
  1568. //
  1569. for (; px <= ex; px += ox2) {
  1570. unsigned short *p01 = px + ox1;
  1571. unsigned short *p10 = px + oy1;
  1572. unsigned short *p11 = p10 + ox1;
  1573. //
  1574. // 2D wavelet encoding
  1575. //
  1576. if (w14) {
  1577. wenc14(*px, *p01, i00, i01);
  1578. wenc14(*p10, *p11, i10, i11);
  1579. wenc14(i00, i10, *px, *p10);
  1580. wenc14(i01, i11, *p01, *p11);
  1581. } else {
  1582. wenc16(*px, *p01, i00, i01);
  1583. wenc16(*p10, *p11, i10, i11);
  1584. wenc16(i00, i10, *px, *p10);
  1585. wenc16(i01, i11, *p01, *p11);
  1586. }
  1587. }
  1588. //
  1589. // Encode (1D) odd column (still in Y loop)
  1590. //
  1591. if (nx & p) {
  1592. unsigned short *p10 = px + oy1;
  1593. if (w14)
  1594. wenc14(*px, *p10, i00, *p10);
  1595. else
  1596. wenc16(*px, *p10, i00, *p10);
  1597. *px = i00;
  1598. }
  1599. }
  1600. //
  1601. // Encode (1D) odd line (must loop in X)
  1602. //
  1603. if (ny & p) {
  1604. unsigned short *px = py;
  1605. unsigned short *ex = py + ox * (nx - p2);
  1606. for (; px <= ex; px += ox2) {
  1607. unsigned short *p01 = px + ox1;
  1608. if (w14)
  1609. wenc14(*px, *p01, i00, *p01);
  1610. else
  1611. wenc16(*px, *p01, i00, *p01);
  1612. *px = i00;
  1613. }
  1614. }
  1615. //
  1616. // Next level
  1617. //
  1618. p = p2;
  1619. p2 <<= 1;
  1620. }
  1621. }
  1622. //
  1623. // 2D Wavelet decoding:
  1624. //
  1625. static void wav2Decode(
  1626. unsigned short *in, // io: values are transformed in place
  1627. int nx, // i : x size
  1628. int ox, // i : x offset
  1629. int ny, // i : y size
  1630. int oy, // i : y offset
  1631. unsigned short mx) // i : maximum in[x][y] value
  1632. {
  1633. bool w14 = (mx < (1 << 14));
  1634. int n = (nx > ny) ? ny : nx;
  1635. int p = 1;
  1636. int p2;
  1637. //
  1638. // Search max level
  1639. //
  1640. while (p <= n) p <<= 1;
  1641. p >>= 1;
  1642. p2 = p;
  1643. p >>= 1;
  1644. //
  1645. // Hierarchical loop on smaller dimension n
  1646. //
  1647. while (p >= 1) {
  1648. unsigned short *py = in;
  1649. unsigned short *ey = in + oy * (ny - p2);
  1650. int oy1 = oy * p;
  1651. int oy2 = oy * p2;
  1652. int ox1 = ox * p;
  1653. int ox2 = ox * p2;
  1654. unsigned short i00, i01, i10, i11;
  1655. //
  1656. // Y loop
  1657. //
  1658. for (; py <= ey; py += oy2) {
  1659. unsigned short *px = py;
  1660. unsigned short *ex = py + ox * (nx - p2);
  1661. //
  1662. // X loop
  1663. //
  1664. for (; px <= ex; px += ox2) {
  1665. unsigned short *p01 = px + ox1;
  1666. unsigned short *p10 = px + oy1;
  1667. unsigned short *p11 = p10 + ox1;
  1668. //
  1669. // 2D wavelet decoding
  1670. //
  1671. if (w14) {
  1672. wdec14(*px, *p10, i00, i10);
  1673. wdec14(*p01, *p11, i01, i11);
  1674. wdec14(i00, i01, *px, *p01);
  1675. wdec14(i10, i11, *p10, *p11);
  1676. } else {
  1677. wdec16(*px, *p10, i00, i10);
  1678. wdec16(*p01, *p11, i01, i11);
  1679. wdec16(i00, i01, *px, *p01);
  1680. wdec16(i10, i11, *p10, *p11);
  1681. }
  1682. }
  1683. //
  1684. // Decode (1D) odd column (still in Y loop)
  1685. //
  1686. if (nx & p) {
  1687. unsigned short *p10 = px + oy1;
  1688. if (w14)
  1689. wdec14(*px, *p10, i00, *p10);
  1690. else
  1691. wdec16(*px, *p10, i00, *p10);
  1692. *px = i00;
  1693. }
  1694. }
  1695. //
  1696. // Decode (1D) odd line (must loop in X)
  1697. //
  1698. if (ny & p) {
  1699. unsigned short *px = py;
  1700. unsigned short *ex = py + ox * (nx - p2);
  1701. for (; px <= ex; px += ox2) {
  1702. unsigned short *p01 = px + ox1;
  1703. if (w14)
  1704. wdec14(*px, *p01, i00, *p01);
  1705. else
  1706. wdec16(*px, *p01, i00, *p01);
  1707. *px = i00;
  1708. }
  1709. }
  1710. //
  1711. // Next level
  1712. //
  1713. p2 = p;
  1714. p >>= 1;
  1715. }
  1716. }
  1717. //-----------------------------------------------------------------------------
  1718. //
  1719. // 16-bit Huffman compression and decompression.
  1720. //
  1721. // The source code in this file is derived from the 8-bit
  1722. // Huffman compression and decompression routines written
  1723. // by Christian Rouet for his PIZ image file format.
  1724. //
  1725. //-----------------------------------------------------------------------------
  1726. // Adds some modification for tinyexr.
  1727. const int HUF_ENCBITS = 16; // literal (value) bit length
  1728. const int HUF_DECBITS = 14; // decoding bit size (>= 8)
  1729. const int HUF_ENCSIZE = (1 << HUF_ENCBITS) + 1; // encoding table size
  1730. const int HUF_DECSIZE = 1 << HUF_DECBITS; // decoding table size
  1731. const int HUF_DECMASK = HUF_DECSIZE - 1;
  1732. struct HufDec { // short code long code
  1733. //-------------------------------
  1734. unsigned int len : 8; // code length 0
  1735. unsigned int lit : 24; // lit p size
  1736. unsigned int *p; // 0 lits
  1737. };
  1738. inline long long hufLength(long long code) { return code & 63; }
  1739. inline long long hufCode(long long code) { return code >> 6; }
  1740. inline void outputBits(int nBits, long long bits, long long &c, int &lc,
  1741. char *&out) {
  1742. c <<= nBits;
  1743. lc += nBits;
  1744. c |= bits;
  1745. while (lc >= 8) *out++ = static_cast<char>((c >> (lc -= 8)));
  1746. }
  1747. inline long long getBits(int nBits, long long &c, int &lc, const char *&in) {
  1748. while (lc < nBits) {
  1749. c = (c << 8) | *(reinterpret_cast<const unsigned char *>(in++));
  1750. lc += 8;
  1751. }
  1752. lc -= nBits;
  1753. return (c >> lc) & ((1 << nBits) - 1);
  1754. }
  1755. //
  1756. // ENCODING TABLE BUILDING & (UN)PACKING
  1757. //
  1758. //
  1759. // Build a "canonical" Huffman code table:
  1760. // - for each (uncompressed) symbol, hcode contains the length
  1761. // of the corresponding code (in the compressed data)
  1762. // - canonical codes are computed and stored in hcode
  1763. // - the rules for constructing canonical codes are as follows:
  1764. // * shorter codes (if filled with zeroes to the right)
  1765. // have a numerically higher value than longer codes
  1766. // * for codes with the same length, numerical values
  1767. // increase with numerical symbol values
  1768. // - because the canonical code table can be constructed from
  1769. // symbol lengths alone, the code table can be transmitted
  1770. // without sending the actual code values
  1771. // - see http://www.compressconsult.com/huffman/
  1772. //
  1773. static void hufCanonicalCodeTable(long long hcode[HUF_ENCSIZE]) {
  1774. long long n[59];
  1775. //
  1776. // For each i from 0 through 58, count the
  1777. // number of different codes of length i, and
  1778. // store the count in n[i].
  1779. //
  1780. for (int i = 0; i <= 58; ++i) n[i] = 0;
  1781. for (int i = 0; i < HUF_ENCSIZE; ++i) n[hcode[i]] += 1;
  1782. //
  1783. // For each i from 58 through 1, compute the
  1784. // numerically lowest code with length i, and
  1785. // store that code in n[i].
  1786. //
  1787. long long c = 0;
  1788. for (int i = 58; i > 0; --i) {
  1789. long long nc = ((c + n[i]) >> 1);
  1790. n[i] = c;
  1791. c = nc;
  1792. }
  1793. //
  1794. // hcode[i] contains the length, l, of the
  1795. // code for symbol i. Assign the next available
  1796. // code of length l to the symbol and store both
  1797. // l and the code in hcode[i].
  1798. //
  1799. for (int i = 0; i < HUF_ENCSIZE; ++i) {
  1800. int l = static_cast<int>(hcode[i]);
  1801. if (l > 0) hcode[i] = l | (n[l]++ << 6);
  1802. }
  1803. }
  1804. //
  1805. // Compute Huffman codes (based on frq input) and store them in frq:
  1806. // - code structure is : [63:lsb - 6:msb] | [5-0: bit length];
  1807. // - max code length is 58 bits;
  1808. // - codes outside the range [im-iM] have a null length (unused values);
  1809. // - original frequencies are destroyed;
  1810. // - encoding tables are used by hufEncode() and hufBuildDecTable();
  1811. //
  1812. struct FHeapCompare {
  1813. bool operator()(long long *a, long long *b) { return *a > *b; }
  1814. };
  1815. static bool hufBuildEncTable(
  1816. long long *frq, // io: input frequencies [HUF_ENCSIZE], output table
  1817. int *im, // o: min frq index
  1818. int *iM) // o: max frq index
  1819. {
  1820. //
  1821. // This function assumes that when it is called, array frq
  1822. // indicates the frequency of all possible symbols in the data
  1823. // that are to be Huffman-encoded. (frq[i] contains the number
  1824. // of occurrences of symbol i in the data.)
  1825. //
  1826. // The loop below does three things:
  1827. //
  1828. // 1) Finds the minimum and maximum indices that point
  1829. // to non-zero entries in frq:
  1830. //
  1831. // frq[im] != 0, and frq[i] == 0 for all i < im
  1832. // frq[iM] != 0, and frq[i] == 0 for all i > iM
  1833. //
  1834. // 2) Fills array fHeap with pointers to all non-zero
  1835. // entries in frq.
  1836. //
  1837. // 3) Initializes array hlink such that hlink[i] == i
  1838. // for all array entries.
  1839. //
  1840. std::vector<int> hlink(HUF_ENCSIZE);
  1841. std::vector<long long *> fHeap(HUF_ENCSIZE);
  1842. *im = 0;
  1843. while (!frq[*im]) (*im)++;
  1844. int nf = 0;
  1845. for (int i = *im; i < HUF_ENCSIZE; i++) {
  1846. hlink[i] = i;
  1847. if (frq[i]) {
  1848. fHeap[nf] = &frq[i];
  1849. nf++;
  1850. *iM = i;
  1851. }
  1852. }
  1853. //
  1854. // Add a pseudo-symbol, with a frequency count of 1, to frq;
  1855. // adjust the fHeap and hlink array accordingly. Function
  1856. // hufEncode() uses the pseudo-symbol for run-length encoding.
  1857. //
  1858. (*iM)++;
  1859. frq[*iM] = 1;
  1860. fHeap[nf] = &frq[*iM];
  1861. nf++;
  1862. //
  1863. // Build an array, scode, such that scode[i] contains the number
  1864. // of bits assigned to symbol i. Conceptually this is done by
  1865. // constructing a tree whose leaves are the symbols with non-zero
  1866. // frequency:
  1867. //
  1868. // Make a heap that contains all symbols with a non-zero frequency,
  1869. // with the least frequent symbol on top.
  1870. //
  1871. // Repeat until only one symbol is left on the heap:
  1872. //
  1873. // Take the two least frequent symbols off the top of the heap.
  1874. // Create a new node that has first two nodes as children, and
  1875. // whose frequency is the sum of the frequencies of the first
  1876. // two nodes. Put the new node back into the heap.
  1877. //
  1878. // The last node left on the heap is the root of the tree. For each
  1879. // leaf node, the distance between the root and the leaf is the length
  1880. // of the code for the corresponding symbol.
  1881. //
  1882. // The loop below doesn't actually build the tree; instead we compute
  1883. // the distances of the leaves from the root on the fly. When a new
  1884. // node is added to the heap, then that node's descendants are linked
  1885. // into a single linear list that starts at the new node, and the code
  1886. // lengths of the descendants (that is, their distance from the root
  1887. // of the tree) are incremented by one.
  1888. //
  1889. std::make_heap(&fHeap[0], &fHeap[nf], FHeapCompare());
  1890. std::vector<long long> scode(HUF_ENCSIZE);
  1891. memset(scode.data(), 0, sizeof(long long) * HUF_ENCSIZE);
  1892. while (nf > 1) {
  1893. //
  1894. // Find the indices, mm and m, of the two smallest non-zero frq
  1895. // values in fHeap, add the smallest frq to the second-smallest
  1896. // frq, and remove the smallest frq value from fHeap.
  1897. //
  1898. int mm = fHeap[0] - frq;
  1899. std::pop_heap(&fHeap[0], &fHeap[nf], FHeapCompare());
  1900. --nf;
  1901. int m = fHeap[0] - frq;
  1902. std::pop_heap(&fHeap[0], &fHeap[nf], FHeapCompare());
  1903. frq[m] += frq[mm];
  1904. std::push_heap(&fHeap[0], &fHeap[nf], FHeapCompare());
  1905. //
  1906. // The entries in scode are linked into lists with the
  1907. // entries in hlink serving as "next" pointers and with
  1908. // the end of a list marked by hlink[j] == j.
  1909. //
  1910. // Traverse the lists that start at scode[m] and scode[mm].
  1911. // For each element visited, increment the length of the
  1912. // corresponding code by one bit. (If we visit scode[j]
  1913. // during the traversal, then the code for symbol j becomes
  1914. // one bit longer.)
  1915. //
  1916. // Merge the lists that start at scode[m] and scode[mm]
  1917. // into a single list that starts at scode[m].
  1918. //
  1919. //
  1920. // Add a bit to all codes in the first list.
  1921. //
  1922. for (int j = m;; j = hlink[j]) {
  1923. scode[j]++;
  1924. TINYEXR_CHECK_AND_RETURN_C(scode[j] <= 58, false);
  1925. if (hlink[j] == j) {
  1926. //
  1927. // Merge the two lists.
  1928. //
  1929. hlink[j] = mm;
  1930. break;
  1931. }
  1932. }
  1933. //
  1934. // Add a bit to all codes in the second list
  1935. //
  1936. for (int j = mm;; j = hlink[j]) {
  1937. scode[j]++;
  1938. TINYEXR_CHECK_AND_RETURN_C(scode[j] <= 58, false);
  1939. if (hlink[j] == j) break;
  1940. }
  1941. }
  1942. //
  1943. // Build a canonical Huffman code table, replacing the code
  1944. // lengths in scode with (code, code length) pairs. Copy the
  1945. // code table from scode into frq.
  1946. //
  1947. hufCanonicalCodeTable(scode.data());
  1948. memcpy(frq, scode.data(), sizeof(long long) * HUF_ENCSIZE);
  1949. return true;
  1950. }
  1951. //
  1952. // Pack an encoding table:
  1953. // - only code lengths, not actual codes, are stored
  1954. // - runs of zeroes are compressed as follows:
  1955. //
  1956. // unpacked packed
  1957. // --------------------------------
  1958. // 1 zero 0 (6 bits)
  1959. // 2 zeroes 59
  1960. // 3 zeroes 60
  1961. // 4 zeroes 61
  1962. // 5 zeroes 62
  1963. // n zeroes (6 or more) 63 n-6 (6 + 8 bits)
  1964. //
  1965. const int SHORT_ZEROCODE_RUN = 59;
  1966. const int LONG_ZEROCODE_RUN = 63;
  1967. const int SHORTEST_LONG_RUN = 2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN;
  1968. const int LONGEST_LONG_RUN = 255 + SHORTEST_LONG_RUN;
  1969. static void hufPackEncTable(
  1970. const long long *hcode, // i : encoding table [HUF_ENCSIZE]
  1971. int im, // i : min hcode index
  1972. int iM, // i : max hcode index
  1973. char **pcode) // o: ptr to packed table (updated)
  1974. {
  1975. char *p = *pcode;
  1976. long long c = 0;
  1977. int lc = 0;
  1978. for (; im <= iM; im++) {
  1979. int l = hufLength(hcode[im]);
  1980. if (l == 0) {
  1981. int zerun = 1;
  1982. while ((im < iM) && (zerun < LONGEST_LONG_RUN)) {
  1983. if (hufLength(hcode[im + 1]) > 0) break;
  1984. im++;
  1985. zerun++;
  1986. }
  1987. if (zerun >= 2) {
  1988. if (zerun >= SHORTEST_LONG_RUN) {
  1989. outputBits(6, LONG_ZEROCODE_RUN, c, lc, p);
  1990. outputBits(8, zerun - SHORTEST_LONG_RUN, c, lc, p);
  1991. } else {
  1992. outputBits(6, SHORT_ZEROCODE_RUN + zerun - 2, c, lc, p);
  1993. }
  1994. continue;
  1995. }
  1996. }
  1997. outputBits(6, l, c, lc, p);
  1998. }
  1999. if (lc > 0) *p++ = (unsigned char)(c << (8 - lc));
  2000. *pcode = p;
  2001. }
  2002. //
  2003. // Unpack an encoding table packed by hufPackEncTable():
  2004. //
  2005. static bool hufUnpackEncTable(
  2006. const char **pcode, // io: ptr to packed table (updated)
  2007. int ni, // i : input size (in bytes)
  2008. int im, // i : min hcode index
  2009. int iM, // i : max hcode index
  2010. long long *hcode) // o: encoding table [HUF_ENCSIZE]
  2011. {
  2012. memset(hcode, 0, sizeof(long long) * HUF_ENCSIZE);
  2013. const char *p = *pcode;
  2014. long long c = 0;
  2015. int lc = 0;
  2016. for (; im <= iM; im++) {
  2017. if (p - *pcode >= ni) {
  2018. return false;
  2019. }
  2020. long long l = hcode[im] = getBits(6, c, lc, p); // code length
  2021. if (l == (long long)LONG_ZEROCODE_RUN) {
  2022. if (p - *pcode > ni) {
  2023. return false;
  2024. }
  2025. int zerun = getBits(8, c, lc, p) + SHORTEST_LONG_RUN;
  2026. if (im + zerun > iM + 1) {
  2027. return false;
  2028. }
  2029. while (zerun--) hcode[im++] = 0;
  2030. im--;
  2031. } else if (l >= (long long)SHORT_ZEROCODE_RUN) {
  2032. int zerun = l - SHORT_ZEROCODE_RUN + 2;
  2033. if (im + zerun > iM + 1) {
  2034. return false;
  2035. }
  2036. while (zerun--) hcode[im++] = 0;
  2037. im--;
  2038. }
  2039. }
  2040. *pcode = const_cast<char *>(p);
  2041. hufCanonicalCodeTable(hcode);
  2042. return true;
  2043. }
  2044. //
  2045. // DECODING TABLE BUILDING
  2046. //
  2047. //
  2048. // Clear a newly allocated decoding table so that it contains only zeroes.
  2049. //
  2050. static void hufClearDecTable(HufDec *hdecod) // io: (allocated by caller)
  2051. // decoding table [HUF_DECSIZE]
  2052. {
  2053. for (int i = 0; i < HUF_DECSIZE; i++) {
  2054. hdecod[i].len = 0;
  2055. hdecod[i].lit = 0;
  2056. hdecod[i].p = NULL;
  2057. }
  2058. // memset(hdecod, 0, sizeof(HufDec) * HUF_DECSIZE);
  2059. }
  2060. //
  2061. // Build a decoding hash table based on the encoding table hcode:
  2062. // - short codes (<= HUF_DECBITS) are resolved with a single table access;
  2063. // - long code entry allocations are not optimized, because long codes are
  2064. // unfrequent;
  2065. // - decoding tables are used by hufDecode();
  2066. //
  2067. static bool hufBuildDecTable(const long long *hcode, // i : encoding table
  2068. int im, // i : min index in hcode
  2069. int iM, // i : max index in hcode
  2070. HufDec *hdecod) // o: (allocated by caller)
  2071. // decoding table [HUF_DECSIZE]
  2072. {
  2073. //
  2074. // Init hashtable & loop on all codes.
  2075. // Assumes that hufClearDecTable(hdecod) has already been called.
  2076. //
  2077. for (; im <= iM; im++) {
  2078. long long c = hufCode(hcode[im]);
  2079. int l = hufLength(hcode[im]);
  2080. if (c >> l) {
  2081. //
  2082. // Error: c is supposed to be an l-bit code,
  2083. // but c contains a value that is greater
  2084. // than the largest l-bit number.
  2085. //
  2086. // invalidTableEntry();
  2087. return false;
  2088. }
  2089. if (l > HUF_DECBITS) {
  2090. //
  2091. // Long code: add a secondary entry
  2092. //
  2093. HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
  2094. if (pl->len) {
  2095. //
  2096. // Error: a short code has already
  2097. // been stored in table entry *pl.
  2098. //
  2099. // invalidTableEntry();
  2100. return false;
  2101. }
  2102. pl->lit++;
  2103. if (pl->p) {
  2104. unsigned int *p = pl->p;
  2105. pl->p = new unsigned int[pl->lit];
  2106. for (unsigned int i = 0; i < pl->lit - 1u; ++i) pl->p[i] = p[i];
  2107. delete[] p;
  2108. } else {
  2109. pl->p = new unsigned int[1];
  2110. }
  2111. pl->p[pl->lit - 1] = im;
  2112. } else if (l) {
  2113. //
  2114. // Short code: init all primary entries
  2115. //
  2116. HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
  2117. for (long long i = 1ULL << (HUF_DECBITS - l); i > 0; i--, pl++) {
  2118. if (pl->len || pl->p) {
  2119. //
  2120. // Error: a short code or a long code has
  2121. // already been stored in table entry *pl.
  2122. //
  2123. // invalidTableEntry();
  2124. return false;
  2125. }
  2126. pl->len = l;
  2127. pl->lit = im;
  2128. }
  2129. }
  2130. }
  2131. return true;
  2132. }
  2133. //
  2134. // Free the long code entries of a decoding table built by hufBuildDecTable()
  2135. //
  2136. static void hufFreeDecTable(HufDec *hdecod) // io: Decoding table
  2137. {
  2138. for (int i = 0; i < HUF_DECSIZE; i++) {
  2139. if (hdecod[i].p) {
  2140. delete[] hdecod[i].p;
  2141. hdecod[i].p = 0;
  2142. }
  2143. }
  2144. }
  2145. //
  2146. // ENCODING
  2147. //
  2148. inline void outputCode(long long code, long long &c, int &lc, char *&out) {
  2149. outputBits(hufLength(code), hufCode(code), c, lc, out);
  2150. }
  2151. inline void sendCode(long long sCode, int runCount, long long runCode,
  2152. long long &c, int &lc, char *&out) {
  2153. //
  2154. // Output a run of runCount instances of the symbol sCount.
  2155. // Output the symbols explicitly, or if that is shorter, output
  2156. // the sCode symbol once followed by a runCode symbol and runCount
  2157. // expressed as an 8-bit number.
  2158. //
  2159. if (hufLength(sCode) + hufLength(runCode) + 8 < hufLength(sCode) * runCount) {
  2160. outputCode(sCode, c, lc, out);
  2161. outputCode(runCode, c, lc, out);
  2162. outputBits(8, runCount, c, lc, out);
  2163. } else {
  2164. while (runCount-- >= 0) outputCode(sCode, c, lc, out);
  2165. }
  2166. }
  2167. //
  2168. // Encode (compress) ni values based on the Huffman encoding table hcode:
  2169. //
  2170. static int hufEncode // return: output size (in bits)
  2171. (const long long *hcode, // i : encoding table
  2172. const unsigned short *in, // i : uncompressed input buffer
  2173. const int ni, // i : input buffer size (in bytes)
  2174. int rlc, // i : rl code
  2175. char *out) // o: compressed output buffer
  2176. {
  2177. char *outStart = out;
  2178. long long c = 0; // bits not yet written to out
  2179. int lc = 0; // number of valid bits in c (LSB)
  2180. int s = in[0];
  2181. int cs = 0;
  2182. //
  2183. // Loop on input values
  2184. //
  2185. for (int i = 1; i < ni; i++) {
  2186. //
  2187. // Count same values or send code
  2188. //
  2189. if (s == in[i] && cs < 255) {
  2190. cs++;
  2191. } else {
  2192. sendCode(hcode[s], cs, hcode[rlc], c, lc, out);
  2193. cs = 0;
  2194. }
  2195. s = in[i];
  2196. }
  2197. //
  2198. // Send remaining code
  2199. //
  2200. sendCode(hcode[s], cs, hcode[rlc], c, lc, out);
  2201. if (lc) *out = (c << (8 - lc)) & 0xff;
  2202. return (out - outStart) * 8 + lc;
  2203. }
  2204. //
  2205. // DECODING
  2206. //
  2207. //
  2208. // In order to force the compiler to inline them,
  2209. // getChar() and getCode() are implemented as macros
  2210. // instead of "inline" functions.
  2211. //
  2212. #define getChar(c, lc, in) \
  2213. { \
  2214. c = (c << 8) | *(unsigned char *)(in++); \
  2215. lc += 8; \
  2216. }
  2217. #if 0
  2218. #define getCode(po, rlc, c, lc, in, out, ob, oe) \
  2219. { \
  2220. if (po == rlc) { \
  2221. if (lc < 8) getChar(c, lc, in); \
  2222. \
  2223. lc -= 8; \
  2224. \
  2225. unsigned char cs = (c >> lc); \
  2226. \
  2227. if (out + cs > oe) return false; \
  2228. \
  2229. /* TinyEXR issue 78 */ \
  2230. unsigned short s = out[-1]; \
  2231. \
  2232. while (cs-- > 0) *out++ = s; \
  2233. } else if (out < oe) { \
  2234. *out++ = po; \
  2235. } else { \
  2236. return false; \
  2237. } \
  2238. }
  2239. #else
  2240. static bool getCode(int po, int rlc, long long &c, int &lc, const char *&in,
  2241. const char *in_end, unsigned short *&out,
  2242. const unsigned short *ob, const unsigned short *oe) {
  2243. (void)ob;
  2244. if (po == rlc) {
  2245. if (lc < 8) {
  2246. /* TinyEXR issue 78 */
  2247. /* TinyEXR issue 160. in + 1 -> in */
  2248. if (in >= in_end) {
  2249. return false;
  2250. }
  2251. getChar(c, lc, in);
  2252. }
  2253. lc -= 8;
  2254. unsigned char cs = (c >> lc);
  2255. if (out + cs > oe) return false;
  2256. // Bounds check for safety
  2257. // Issue 100.
  2258. if ((out - 1) < ob) return false;
  2259. unsigned short s = out[-1];
  2260. while (cs-- > 0) *out++ = s;
  2261. } else if (out < oe) {
  2262. *out++ = po;
  2263. } else {
  2264. return false;
  2265. }
  2266. return true;
  2267. }
  2268. #endif
  2269. //
  2270. // Decode (uncompress) ni bits based on encoding & decoding tables:
  2271. //
  2272. static bool hufDecode(const long long *hcode, // i : encoding table
  2273. const HufDec *hdecod, // i : decoding table
  2274. const char *in, // i : compressed input buffer
  2275. int ni, // i : input size (in bits)
  2276. int rlc, // i : run-length code
  2277. int no, // i : expected output size (in bytes)
  2278. unsigned short *out) // o: uncompressed output buffer
  2279. {
  2280. long long c = 0;
  2281. int lc = 0;
  2282. unsigned short *outb = out; // begin
  2283. unsigned short *oe = out + no; // end
  2284. const char *ie = in + (ni + 7) / 8; // input byte size
  2285. //
  2286. // Loop on input bytes
  2287. //
  2288. while (in < ie) {
  2289. getChar(c, lc, in);
  2290. //
  2291. // Access decoding table
  2292. //
  2293. while (lc >= HUF_DECBITS) {
  2294. const HufDec pl = hdecod[(c >> (lc - HUF_DECBITS)) & HUF_DECMASK];
  2295. if (pl.len) {
  2296. //
  2297. // Get short code
  2298. //
  2299. lc -= pl.len;
  2300. // std::cout << "lit = " << pl.lit << std::endl;
  2301. // std::cout << "rlc = " << rlc << std::endl;
  2302. // std::cout << "c = " << c << std::endl;
  2303. // std::cout << "lc = " << lc << std::endl;
  2304. // std::cout << "in = " << in << std::endl;
  2305. // std::cout << "out = " << out << std::endl;
  2306. // std::cout << "oe = " << oe << std::endl;
  2307. if (!getCode(pl.lit, rlc, c, lc, in, ie, out, outb, oe)) {
  2308. return false;
  2309. }
  2310. } else {
  2311. if (!pl.p) {
  2312. return false;
  2313. }
  2314. // invalidCode(); // wrong code
  2315. //
  2316. // Search long code
  2317. //
  2318. unsigned int j;
  2319. for (j = 0; j < pl.lit; j++) {
  2320. int l = hufLength(hcode[pl.p[j]]);
  2321. while (lc < l && in < ie) // get more bits
  2322. getChar(c, lc, in);
  2323. if (lc >= l) {
  2324. if (hufCode(hcode[pl.p[j]]) ==
  2325. ((c >> (lc - l)) & (((long long)(1) << l) - 1))) {
  2326. //
  2327. // Found : get long code
  2328. //
  2329. lc -= l;
  2330. if (!getCode(pl.p[j], rlc, c, lc, in, ie, out, outb, oe)) {
  2331. return false;
  2332. }
  2333. break;
  2334. }
  2335. }
  2336. }
  2337. if (j == pl.lit) {
  2338. return false;
  2339. // invalidCode(); // Not found
  2340. }
  2341. }
  2342. }
  2343. }
  2344. //
  2345. // Get remaining (short) codes
  2346. //
  2347. int i = (8 - ni) & 7;
  2348. c >>= i;
  2349. lc -= i;
  2350. while (lc > 0) {
  2351. const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
  2352. if (pl.len) {
  2353. lc -= pl.len;
  2354. if (!getCode(pl.lit, rlc, c, lc, in, ie, out, outb, oe)) {
  2355. return false;
  2356. }
  2357. } else {
  2358. return false;
  2359. // invalidCode(); // wrong (long) code
  2360. }
  2361. }
  2362. if (out - outb != no) {
  2363. return false;
  2364. }
  2365. // notEnoughData ();
  2366. return true;
  2367. }
  2368. static void countFrequencies(std::vector<long long> &freq,
  2369. const unsigned short data[/*n*/], int n) {
  2370. for (int i = 0; i < HUF_ENCSIZE; ++i) freq[i] = 0;
  2371. for (int i = 0; i < n; ++i) ++freq[data[i]];
  2372. }
  2373. static void writeUInt(char buf[4], unsigned int i) {
  2374. unsigned char *b = (unsigned char *)buf;
  2375. b[0] = i;
  2376. b[1] = i >> 8;
  2377. b[2] = i >> 16;
  2378. b[3] = i >> 24;
  2379. }
  2380. static unsigned int readUInt(const char buf[4]) {
  2381. const unsigned char *b = (const unsigned char *)buf;
  2382. return (b[0] & 0x000000ff) | ((b[1] << 8) & 0x0000ff00) |
  2383. ((b[2] << 16) & 0x00ff0000) | ((b[3] << 24) & 0xff000000);
  2384. }
  2385. //
  2386. // EXTERNAL INTERFACE
  2387. //
  2388. static int hufCompress(const unsigned short raw[], int nRaw,
  2389. char compressed[]) {
  2390. if (nRaw == 0) return 0;
  2391. std::vector<long long> freq(HUF_ENCSIZE);
  2392. countFrequencies(freq, raw, nRaw);
  2393. int im = 0;
  2394. int iM = 0;
  2395. hufBuildEncTable(freq.data(), &im, &iM);
  2396. char *tableStart = compressed + 20;
  2397. char *tableEnd = tableStart;
  2398. hufPackEncTable(freq.data(), im, iM, &tableEnd);
  2399. int tableLength = tableEnd - tableStart;
  2400. char *dataStart = tableEnd;
  2401. int nBits = hufEncode(freq.data(), raw, nRaw, iM, dataStart);
  2402. int data_length = (nBits + 7) / 8;
  2403. writeUInt(compressed, im);
  2404. writeUInt(compressed + 4, iM);
  2405. writeUInt(compressed + 8, tableLength);
  2406. writeUInt(compressed + 12, nBits);
  2407. writeUInt(compressed + 16, 0); // room for future extensions
  2408. return dataStart + data_length - compressed;
  2409. }
  2410. static bool hufUncompress(const char compressed[], int nCompressed,
  2411. std::vector<unsigned short> *raw) {
  2412. if (nCompressed == 0) {
  2413. if (raw->size() != 0) return false;
  2414. return false;
  2415. }
  2416. int im = readUInt(compressed);
  2417. int iM = readUInt(compressed + 4);
  2418. // int tableLength = readUInt (compressed + 8);
  2419. int nBits = readUInt(compressed + 12);
  2420. if (im < 0 || im >= HUF_ENCSIZE || iM < 0 || iM >= HUF_ENCSIZE) return false;
  2421. const char *ptr = compressed + 20;
  2422. //
  2423. // Fast decoder needs at least 2x64-bits of compressed data, and
  2424. // needs to be run-able on this platform. Otherwise, fall back
  2425. // to the original decoder
  2426. //
  2427. // if (FastHufDecoder::enabled() && nBits > 128)
  2428. //{
  2429. // FastHufDecoder fhd (ptr, nCompressed - (ptr - compressed), im, iM, iM);
  2430. // fhd.decode ((unsigned char*)ptr, nBits, raw, nRaw);
  2431. //}
  2432. // else
  2433. {
  2434. std::vector<long long> freq(HUF_ENCSIZE);
  2435. std::vector<HufDec> hdec(HUF_DECSIZE);
  2436. hufClearDecTable(&hdec.at(0));
  2437. hufUnpackEncTable(&ptr, nCompressed - (ptr - compressed), im, iM,
  2438. &freq.at(0));
  2439. {
  2440. if (nBits > 8 * (nCompressed - (ptr - compressed))) {
  2441. return false;
  2442. }
  2443. hufBuildDecTable(&freq.at(0), im, iM, &hdec.at(0));
  2444. hufDecode(&freq.at(0), &hdec.at(0), ptr, nBits, iM, raw->size(),
  2445. raw->data());
  2446. }
  2447. // catch (...)
  2448. //{
  2449. // hufFreeDecTable (hdec);
  2450. // throw;
  2451. //}
  2452. hufFreeDecTable(&hdec.at(0));
  2453. }
  2454. return true;
  2455. }
  2456. //
  2457. // Functions to compress the range of values in the pixel data
  2458. //
  2459. const int USHORT_RANGE = (1 << 16);
  2460. const int BITMAP_SIZE = (USHORT_RANGE >> 3);
  2461. static void bitmapFromData(const unsigned short data[/*nData*/], int nData,
  2462. unsigned char bitmap[BITMAP_SIZE],
  2463. unsigned short &minNonZero,
  2464. unsigned short &maxNonZero) {
  2465. for (int i = 0; i < BITMAP_SIZE; ++i) bitmap[i] = 0;
  2466. for (int i = 0; i < nData; ++i) bitmap[data[i] >> 3] |= (1 << (data[i] & 7));
  2467. bitmap[0] &= ~1; // zero is not explicitly stored in
  2468. // the bitmap; we assume that the
  2469. // data always contain zeroes
  2470. minNonZero = BITMAP_SIZE - 1;
  2471. maxNonZero = 0;
  2472. for (int i = 0; i < BITMAP_SIZE; ++i) {
  2473. if (bitmap[i]) {
  2474. if (minNonZero > i) minNonZero = i;
  2475. if (maxNonZero < i) maxNonZero = i;
  2476. }
  2477. }
  2478. }
  2479. static unsigned short forwardLutFromBitmap(
  2480. const unsigned char bitmap[BITMAP_SIZE], unsigned short lut[USHORT_RANGE]) {
  2481. int k = 0;
  2482. for (int i = 0; i < USHORT_RANGE; ++i) {
  2483. if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
  2484. lut[i] = k++;
  2485. else
  2486. lut[i] = 0;
  2487. }
  2488. return k - 1; // maximum value stored in lut[],
  2489. } // i.e. number of ones in bitmap minus 1
  2490. static unsigned short reverseLutFromBitmap(
  2491. const unsigned char bitmap[BITMAP_SIZE], unsigned short lut[USHORT_RANGE]) {
  2492. int k = 0;
  2493. for (int i = 0; i < USHORT_RANGE; ++i) {
  2494. if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7)))) lut[k++] = i;
  2495. }
  2496. int n = k - 1;
  2497. while (k < USHORT_RANGE) lut[k++] = 0;
  2498. return n; // maximum k where lut[k] is non-zero,
  2499. } // i.e. number of ones in bitmap minus 1
  2500. static void applyLut(const unsigned short lut[USHORT_RANGE],
  2501. unsigned short data[/*nData*/], int nData) {
  2502. for (int i = 0; i < nData; ++i) data[i] = lut[data[i]];
  2503. }
  2504. #ifdef __clang__
  2505. #pragma clang diagnostic pop
  2506. #endif // __clang__
  2507. #ifdef _MSC_VER
  2508. #pragma warning(pop)
  2509. #endif
  2510. static bool CompressPiz(unsigned char *outPtr, unsigned int *outSize,
  2511. const unsigned char *inPtr, size_t inSize,
  2512. const std::vector<ChannelInfo> &channelInfo,
  2513. int data_width, int num_lines) {
  2514. std::vector<unsigned char> bitmap(BITMAP_SIZE);
  2515. unsigned short minNonZero;
  2516. unsigned short maxNonZero;
  2517. #if !TINYEXR_LITTLE_ENDIAN
  2518. // @todo { PIZ compression on BigEndian architecture. }
  2519. return false;
  2520. #endif
  2521. // Assume `inSize` is multiple of 2 or 4.
  2522. std::vector<unsigned short> tmpBuffer(inSize / sizeof(unsigned short));
  2523. std::vector<PIZChannelData> channelData(channelInfo.size());
  2524. unsigned short *tmpBufferEnd = &tmpBuffer.at(0);
  2525. for (size_t c = 0; c < channelData.size(); c++) {
  2526. PIZChannelData &cd = channelData[c];
  2527. cd.start = tmpBufferEnd;
  2528. cd.end = cd.start;
  2529. cd.nx = data_width;
  2530. cd.ny = num_lines;
  2531. // cd.ys = c.channel().ySampling;
  2532. size_t pixelSize = sizeof(int); // UINT and FLOAT
  2533. if (channelInfo[c].requested_pixel_type == TINYEXR_PIXELTYPE_HALF) {
  2534. pixelSize = sizeof(short);
  2535. }
  2536. cd.size = static_cast<int>(pixelSize / sizeof(short));
  2537. tmpBufferEnd += cd.nx * cd.ny * cd.size;
  2538. }
  2539. const unsigned char *ptr = inPtr;
  2540. for (int y = 0; y < num_lines; ++y) {
  2541. for (size_t i = 0; i < channelData.size(); ++i) {
  2542. PIZChannelData &cd = channelData[i];
  2543. // if (modp (y, cd.ys) != 0)
  2544. // continue;
  2545. size_t n = static_cast<size_t>(cd.nx * cd.size);
  2546. memcpy(cd.end, ptr, n * sizeof(unsigned short));
  2547. ptr += n * sizeof(unsigned short);
  2548. cd.end += n;
  2549. }
  2550. }
  2551. bitmapFromData(&tmpBuffer.at(0), static_cast<int>(tmpBuffer.size()),
  2552. bitmap.data(), minNonZero, maxNonZero);
  2553. std::vector<unsigned short> lut(USHORT_RANGE);
  2554. unsigned short maxValue = forwardLutFromBitmap(bitmap.data(), lut.data());
  2555. applyLut(lut.data(), &tmpBuffer.at(0), static_cast<int>(tmpBuffer.size()));
  2556. //
  2557. // Store range compression info in _outBuffer
  2558. //
  2559. char *buf = reinterpret_cast<char *>(outPtr);
  2560. memcpy(buf, &minNonZero, sizeof(unsigned short));
  2561. buf += sizeof(unsigned short);
  2562. memcpy(buf, &maxNonZero, sizeof(unsigned short));
  2563. buf += sizeof(unsigned short);
  2564. if (minNonZero <= maxNonZero) {
  2565. memcpy(buf, reinterpret_cast<char *>(&bitmap[0] + minNonZero),
  2566. maxNonZero - minNonZero + 1);
  2567. buf += maxNonZero - minNonZero + 1;
  2568. }
  2569. //
  2570. // Apply wavelet encoding
  2571. //
  2572. for (size_t i = 0; i < channelData.size(); ++i) {
  2573. PIZChannelData &cd = channelData[i];
  2574. for (int j = 0; j < cd.size; ++j) {
  2575. wav2Encode(cd.start + j, cd.nx, cd.size, cd.ny, cd.nx * cd.size,
  2576. maxValue);
  2577. }
  2578. }
  2579. //
  2580. // Apply Huffman encoding; append the result to _outBuffer
  2581. //
  2582. // length header(4byte), then huff data. Initialize length header with zero,
  2583. // then later fill it by `length`.
  2584. char *lengthPtr = buf;
  2585. int zero = 0;
  2586. memcpy(buf, &zero, sizeof(int));
  2587. buf += sizeof(int);
  2588. int length =
  2589. hufCompress(&tmpBuffer.at(0), static_cast<int>(tmpBuffer.size()), buf);
  2590. memcpy(lengthPtr, &length, sizeof(int));
  2591. (*outSize) = static_cast<unsigned int>(
  2592. (reinterpret_cast<unsigned char *>(buf) - outPtr) +
  2593. static_cast<unsigned int>(length));
  2594. // Use uncompressed data when compressed data is larger than uncompressed.
  2595. // (Issue 40)
  2596. if ((*outSize) >= inSize) {
  2597. (*outSize) = static_cast<unsigned int>(inSize);
  2598. memcpy(outPtr, inPtr, inSize);
  2599. }
  2600. return true;
  2601. }
  2602. static bool DecompressPiz(unsigned char *outPtr, const unsigned char *inPtr,
  2603. size_t tmpBufSizeInBytes, size_t inLen, int num_channels,
  2604. const EXRChannelInfo *channels, int data_width,
  2605. int num_lines) {
  2606. if (inLen == tmpBufSizeInBytes) {
  2607. // Data is not compressed(Issue 40).
  2608. memcpy(outPtr, inPtr, inLen);
  2609. return true;
  2610. }
  2611. std::vector<unsigned char> bitmap(BITMAP_SIZE);
  2612. unsigned short minNonZero;
  2613. unsigned short maxNonZero;
  2614. #if !TINYEXR_LITTLE_ENDIAN
  2615. // @todo { PIZ compression on BigEndian architecture. }
  2616. return false;
  2617. #endif
  2618. memset(bitmap.data(), 0, BITMAP_SIZE);
  2619. if (inLen < 4) {
  2620. return false;
  2621. }
  2622. size_t readLen = 0;
  2623. const unsigned char *ptr = inPtr;
  2624. // minNonZero = *(reinterpret_cast<const unsigned short *>(ptr));
  2625. tinyexr::cpy2(&minNonZero, reinterpret_cast<const unsigned short *>(ptr));
  2626. // maxNonZero = *(reinterpret_cast<const unsigned short *>(ptr + 2));
  2627. tinyexr::cpy2(&maxNonZero, reinterpret_cast<const unsigned short *>(ptr + 2));
  2628. ptr += 4;
  2629. readLen += 4;
  2630. if (maxNonZero >= BITMAP_SIZE) {
  2631. return false;
  2632. }
  2633. //printf("maxNonZero = %d\n", maxNonZero);
  2634. //printf("minNonZero = %d\n", minNonZero);
  2635. //printf("len = %d\n", (maxNonZero - minNonZero + 1));
  2636. //printf("BITMAPSIZE - min = %d\n", (BITMAP_SIZE - minNonZero));
  2637. if (minNonZero <= maxNonZero) {
  2638. if (((maxNonZero - minNonZero + 1) + readLen) > inLen) {
  2639. // Input too short
  2640. return false;
  2641. }
  2642. memcpy(reinterpret_cast<char *>(&bitmap[0] + minNonZero), ptr,
  2643. maxNonZero - minNonZero + 1);
  2644. ptr += maxNonZero - minNonZero + 1;
  2645. readLen += maxNonZero - minNonZero + 1;
  2646. } else {
  2647. // Issue 194
  2648. if ((minNonZero == (BITMAP_SIZE - 1)) && (maxNonZero == 0)) {
  2649. // OK. all pixels are zero. And no need to read `bitmap` data.
  2650. } else {
  2651. // invalid minNonZero/maxNonZero combination.
  2652. return false;
  2653. }
  2654. }
  2655. std::vector<unsigned short> lut(USHORT_RANGE);
  2656. memset(lut.data(), 0, sizeof(unsigned short) * USHORT_RANGE);
  2657. unsigned short maxValue = reverseLutFromBitmap(bitmap.data(), lut.data());
  2658. //
  2659. // Huffman decoding
  2660. //
  2661. if ((readLen + 4) > inLen) {
  2662. return false;
  2663. }
  2664. int length=0;
  2665. // length = *(reinterpret_cast<const int *>(ptr));
  2666. tinyexr::cpy4(&length, reinterpret_cast<const int *>(ptr));
  2667. ptr += sizeof(int);
  2668. if (size_t((ptr - inPtr) + length) > inLen) {
  2669. return false;
  2670. }
  2671. std::vector<unsigned short> tmpBuffer(tmpBufSizeInBytes / sizeof(unsigned short));
  2672. hufUncompress(reinterpret_cast<const char *>(ptr), length, &tmpBuffer);
  2673. //
  2674. // Wavelet decoding
  2675. //
  2676. std::vector<PIZChannelData> channelData(static_cast<size_t>(num_channels));
  2677. unsigned short *tmpBufferEnd = &tmpBuffer.at(0);
  2678. for (size_t i = 0; i < static_cast<size_t>(num_channels); ++i) {
  2679. const EXRChannelInfo &chan = channels[i];
  2680. size_t pixelSize = sizeof(int); // UINT and FLOAT
  2681. if (chan.pixel_type == TINYEXR_PIXELTYPE_HALF) {
  2682. pixelSize = sizeof(short);
  2683. }
  2684. channelData[i].start = tmpBufferEnd;
  2685. channelData[i].end = channelData[i].start;
  2686. channelData[i].nx = data_width;
  2687. channelData[i].ny = num_lines;
  2688. // channelData[i].ys = 1;
  2689. channelData[i].size = static_cast<int>(pixelSize / sizeof(short));
  2690. tmpBufferEnd += channelData[i].nx * channelData[i].ny * channelData[i].size;
  2691. }
  2692. for (size_t i = 0; i < channelData.size(); ++i) {
  2693. PIZChannelData &cd = channelData[i];
  2694. for (int j = 0; j < cd.size; ++j) {
  2695. wav2Decode(cd.start + j, cd.nx, cd.size, cd.ny, cd.nx * cd.size,
  2696. maxValue);
  2697. }
  2698. }
  2699. //
  2700. // Expand the pixel data to their original range
  2701. //
  2702. applyLut(lut.data(), &tmpBuffer.at(0), static_cast<int>(tmpBufSizeInBytes / sizeof(unsigned short)));
  2703. for (int y = 0; y < num_lines; y++) {
  2704. for (size_t i = 0; i < channelData.size(); ++i) {
  2705. PIZChannelData &cd = channelData[i];
  2706. // if (modp (y, cd.ys) != 0)
  2707. // continue;
  2708. size_t n = static_cast<size_t>(cd.nx * cd.size);
  2709. memcpy(outPtr, cd.end, static_cast<size_t>(n * sizeof(unsigned short)));
  2710. outPtr += n * sizeof(unsigned short);
  2711. cd.end += n;
  2712. }
  2713. }
  2714. return true;
  2715. }
  2716. #endif // TINYEXR_USE_PIZ
  2717. #if TINYEXR_USE_ZFP
  2718. struct ZFPCompressionParam {
  2719. double rate;
  2720. unsigned int precision;
  2721. unsigned int __pad0;
  2722. double tolerance;
  2723. int type; // TINYEXR_ZFP_COMPRESSIONTYPE_*
  2724. unsigned int __pad1;
  2725. ZFPCompressionParam() {
  2726. type = TINYEXR_ZFP_COMPRESSIONTYPE_RATE;
  2727. rate = 2.0;
  2728. precision = 0;
  2729. tolerance = 0.0;
  2730. }
  2731. };
  2732. static bool FindZFPCompressionParam(ZFPCompressionParam *param,
  2733. const EXRAttribute *attributes,
  2734. int num_attributes, std::string *err) {
  2735. bool foundType = false;
  2736. for (int i = 0; i < num_attributes; i++) {
  2737. if ((strcmp(attributes[i].name, "zfpCompressionType") == 0)) {
  2738. if (attributes[i].size == 1) {
  2739. param->type = static_cast<int>(attributes[i].value[0]);
  2740. foundType = true;
  2741. break;
  2742. } else {
  2743. if (err) {
  2744. (*err) +=
  2745. "zfpCompressionType attribute must be uchar(1 byte) type.\n";
  2746. }
  2747. return false;
  2748. }
  2749. }
  2750. }
  2751. if (!foundType) {
  2752. if (err) {
  2753. (*err) += "`zfpCompressionType` attribute not found.\n";
  2754. }
  2755. return false;
  2756. }
  2757. if (param->type == TINYEXR_ZFP_COMPRESSIONTYPE_RATE) {
  2758. for (int i = 0; i < num_attributes; i++) {
  2759. if ((strcmp(attributes[i].name, "zfpCompressionRate") == 0) &&
  2760. (attributes[i].size == 8)) {
  2761. param->rate = *(reinterpret_cast<double *>(attributes[i].value));
  2762. return true;
  2763. }
  2764. }
  2765. if (err) {
  2766. (*err) += "`zfpCompressionRate` attribute not found.\n";
  2767. }
  2768. } else if (param->type == TINYEXR_ZFP_COMPRESSIONTYPE_PRECISION) {
  2769. for (int i = 0; i < num_attributes; i++) {
  2770. if ((strcmp(attributes[i].name, "zfpCompressionPrecision") == 0) &&
  2771. (attributes[i].size == 4)) {
  2772. param->rate = *(reinterpret_cast<int *>(attributes[i].value));
  2773. return true;
  2774. }
  2775. }
  2776. if (err) {
  2777. (*err) += "`zfpCompressionPrecision` attribute not found.\n";
  2778. }
  2779. } else if (param->type == TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY) {
  2780. for (int i = 0; i < num_attributes; i++) {
  2781. if ((strcmp(attributes[i].name, "zfpCompressionTolerance") == 0) &&
  2782. (attributes[i].size == 8)) {
  2783. param->tolerance = *(reinterpret_cast<double *>(attributes[i].value));
  2784. return true;
  2785. }
  2786. }
  2787. if (err) {
  2788. (*err) += "`zfpCompressionTolerance` attribute not found.\n";
  2789. }
  2790. } else {
  2791. if (err) {
  2792. (*err) += "Unknown value specified for `zfpCompressionType`.\n";
  2793. }
  2794. }
  2795. return false;
  2796. }
  2797. // Assume pixel format is FLOAT for all channels.
  2798. static bool DecompressZfp(float *dst, int dst_width, int dst_num_lines,
  2799. size_t num_channels, const unsigned char *src,
  2800. unsigned long src_size,
  2801. const ZFPCompressionParam &param) {
  2802. size_t uncompressed_size =
  2803. size_t(dst_width) * size_t(dst_num_lines) * num_channels;
  2804. if (uncompressed_size == src_size) {
  2805. // Data is not compressed(Issue 40).
  2806. memcpy(dst, src, src_size);
  2807. }
  2808. zfp_stream *zfp = NULL;
  2809. zfp_field *field = NULL;
  2810. TINYEXR_CHECK_AND_RETURN_C((dst_width % 4) == 0, false);
  2811. TINYEXR_CHECK_AND_RETURN_C((dst_num_lines % 4) == 0, false);
  2812. if ((size_t(dst_width) & 3U) || (size_t(dst_num_lines) & 3U)) {
  2813. return false;
  2814. }
  2815. field =
  2816. zfp_field_2d(reinterpret_cast<void *>(const_cast<unsigned char *>(src)),
  2817. zfp_type_float, static_cast<unsigned int>(dst_width),
  2818. static_cast<unsigned int>(dst_num_lines) *
  2819. static_cast<unsigned int>(num_channels));
  2820. zfp = zfp_stream_open(NULL);
  2821. if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_RATE) {
  2822. zfp_stream_set_rate(zfp, param.rate, zfp_type_float, /* dimension */ 2,
  2823. /* write random access */ 0);
  2824. } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_PRECISION) {
  2825. zfp_stream_set_precision(zfp, param.precision);
  2826. } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY) {
  2827. zfp_stream_set_accuracy(zfp, param.tolerance);
  2828. } else {
  2829. return false;
  2830. }
  2831. size_t buf_size = zfp_stream_maximum_size(zfp, field);
  2832. std::vector<unsigned char> buf(buf_size);
  2833. memcpy(&buf.at(0), src, src_size);
  2834. bitstream *stream = stream_open(&buf.at(0), buf_size);
  2835. zfp_stream_set_bit_stream(zfp, stream);
  2836. zfp_stream_rewind(zfp);
  2837. size_t image_size = size_t(dst_width) * size_t(dst_num_lines);
  2838. for (size_t c = 0; c < size_t(num_channels); c++) {
  2839. // decompress 4x4 pixel block.
  2840. for (size_t y = 0; y < size_t(dst_num_lines); y += 4) {
  2841. for (size_t x = 0; x < size_t(dst_width); x += 4) {
  2842. float fblock[16];
  2843. zfp_decode_block_float_2(zfp, fblock);
  2844. for (size_t j = 0; j < 4; j++) {
  2845. for (size_t i = 0; i < 4; i++) {
  2846. dst[c * image_size + ((y + j) * size_t(dst_width) + (x + i))] =
  2847. fblock[j * 4 + i];
  2848. }
  2849. }
  2850. }
  2851. }
  2852. }
  2853. zfp_field_free(field);
  2854. zfp_stream_close(zfp);
  2855. stream_close(stream);
  2856. return true;
  2857. }
  2858. // Assume pixel format is FLOAT for all channels.
  2859. static bool CompressZfp(std::vector<unsigned char> *outBuf,
  2860. unsigned int *outSize, const float *inPtr, int width,
  2861. int num_lines, int num_channels,
  2862. const ZFPCompressionParam &param) {
  2863. zfp_stream *zfp = NULL;
  2864. zfp_field *field = NULL;
  2865. TINYEXR_CHECK_AND_RETURN_C((width % 4) == 0, false);
  2866. TINYEXR_CHECK_AND_RETURN_C((num_lines % 4) == 0, false);
  2867. if ((size_t(width) & 3U) || (size_t(num_lines) & 3U)) {
  2868. return false;
  2869. }
  2870. // create input array.
  2871. field = zfp_field_2d(reinterpret_cast<void *>(const_cast<float *>(inPtr)),
  2872. zfp_type_float, static_cast<unsigned int>(width),
  2873. static_cast<unsigned int>(num_lines * num_channels));
  2874. zfp = zfp_stream_open(NULL);
  2875. if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_RATE) {
  2876. zfp_stream_set_rate(zfp, param.rate, zfp_type_float, 2, 0);
  2877. } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_PRECISION) {
  2878. zfp_stream_set_precision(zfp, param.precision);
  2879. } else if (param.type == TINYEXR_ZFP_COMPRESSIONTYPE_ACCURACY) {
  2880. zfp_stream_set_accuracy(zfp, param.tolerance);
  2881. } else {
  2882. return false;
  2883. }
  2884. size_t buf_size = zfp_stream_maximum_size(zfp, field);
  2885. outBuf->resize(buf_size);
  2886. bitstream *stream = stream_open(&outBuf->at(0), buf_size);
  2887. zfp_stream_set_bit_stream(zfp, stream);
  2888. zfp_field_free(field);
  2889. size_t image_size = size_t(width) * size_t(num_lines);
  2890. for (size_t c = 0; c < size_t(num_channels); c++) {
  2891. // compress 4x4 pixel block.
  2892. for (size_t y = 0; y < size_t(num_lines); y += 4) {
  2893. for (size_t x = 0; x < size_t(width); x += 4) {
  2894. float fblock[16];
  2895. for (size_t j = 0; j < 4; j++) {
  2896. for (size_t i = 0; i < 4; i++) {
  2897. fblock[j * 4 + i] =
  2898. inPtr[c * image_size + ((y + j) * size_t(width) + (x + i))];
  2899. }
  2900. }
  2901. zfp_encode_block_float_2(zfp, fblock);
  2902. }
  2903. }
  2904. }
  2905. zfp_stream_flush(zfp);
  2906. (*outSize) = static_cast<unsigned int>(zfp_stream_compressed_size(zfp));
  2907. zfp_stream_close(zfp);
  2908. return true;
  2909. }
  2910. #endif
  2911. //
  2912. // -----------------------------------------------------------------
  2913. //
  2914. // heuristics
  2915. #define TINYEXR_DIMENSION_THRESHOLD (1024 * 8192)
  2916. // TODO(syoyo): Refactor function arguments.
  2917. static bool DecodePixelData(/* out */ unsigned char **out_images,
  2918. const int *requested_pixel_types,
  2919. const unsigned char *data_ptr, size_t data_len,
  2920. int compression_type, int line_order, int width,
  2921. int height, int x_stride, int y, int line_no,
  2922. int num_lines, size_t pixel_data_size,
  2923. size_t num_attributes,
  2924. const EXRAttribute *attributes, size_t num_channels,
  2925. const EXRChannelInfo *channels,
  2926. const std::vector<size_t> &channel_offset_list) {
  2927. if (compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) { // PIZ
  2928. #if TINYEXR_USE_PIZ
  2929. if ((width == 0) || (num_lines == 0) || (pixel_data_size == 0)) {
  2930. // Invalid input #90
  2931. return false;
  2932. }
  2933. // Allocate original data size.
  2934. std::vector<unsigned char> outBuf(static_cast<size_t>(
  2935. static_cast<size_t>(width * num_lines) * pixel_data_size));
  2936. size_t tmpBufLen = outBuf.size();
  2937. bool ret = tinyexr::DecompressPiz(
  2938. reinterpret_cast<unsigned char *>(&outBuf.at(0)), data_ptr, tmpBufLen,
  2939. data_len, static_cast<int>(num_channels), channels, width, num_lines);
  2940. if (!ret) {
  2941. return false;
  2942. }
  2943. // For PIZ_COMPRESSION:
  2944. // pixel sample data for channel 0 for scanline 0
  2945. // pixel sample data for channel 1 for scanline 0
  2946. // pixel sample data for channel ... for scanline 0
  2947. // pixel sample data for channel n for scanline 0
  2948. // pixel sample data for channel 0 for scanline 1
  2949. // pixel sample data for channel 1 for scanline 1
  2950. // pixel sample data for channel ... for scanline 1
  2951. // pixel sample data for channel n for scanline 1
  2952. // ...
  2953. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  2954. if (channels[c].pixel_type == TINYEXR_PIXELTYPE_HALF) {
  2955. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  2956. const unsigned short *line_ptr = reinterpret_cast<unsigned short *>(
  2957. &outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
  2958. channel_offset_list[c] * static_cast<size_t>(width)));
  2959. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  2960. FP16 hf;
  2961. // hf.u = line_ptr[u];
  2962. // use `cpy` to avoid unaligned memory access when compiler's
  2963. // optimization is on.
  2964. tinyexr::cpy2(&(hf.u), line_ptr + u);
  2965. tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
  2966. if (requested_pixel_types[c] == TINYEXR_PIXELTYPE_HALF) {
  2967. unsigned short *image =
  2968. reinterpret_cast<unsigned short **>(out_images)[c];
  2969. if (line_order == 0) {
  2970. image += (static_cast<size_t>(line_no) + v) *
  2971. static_cast<size_t>(x_stride) +
  2972. u;
  2973. } else {
  2974. image += static_cast<size_t>(
  2975. (height - 1 - (line_no + static_cast<int>(v)))) *
  2976. static_cast<size_t>(x_stride) +
  2977. u;
  2978. }
  2979. *image = hf.u;
  2980. } else { // HALF -> FLOAT
  2981. FP32 f32 = half_to_float(hf);
  2982. float *image = reinterpret_cast<float **>(out_images)[c];
  2983. size_t offset = 0;
  2984. if (line_order == 0) {
  2985. offset = (static_cast<size_t>(line_no) + v) *
  2986. static_cast<size_t>(x_stride) +
  2987. u;
  2988. } else {
  2989. offset = static_cast<size_t>(
  2990. (height - 1 - (line_no + static_cast<int>(v)))) *
  2991. static_cast<size_t>(x_stride) +
  2992. u;
  2993. }
  2994. image += offset;
  2995. *image = f32.f;
  2996. }
  2997. }
  2998. }
  2999. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) {
  3000. TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT, false);
  3001. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3002. const unsigned int *line_ptr = reinterpret_cast<unsigned int *>(
  3003. &outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
  3004. channel_offset_list[c] * static_cast<size_t>(width)));
  3005. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  3006. unsigned int val;
  3007. // val = line_ptr[u];
  3008. tinyexr::cpy4(&val, line_ptr + u);
  3009. tinyexr::swap4(&val);
  3010. unsigned int *image =
  3011. reinterpret_cast<unsigned int **>(out_images)[c];
  3012. if (line_order == 0) {
  3013. image += (static_cast<size_t>(line_no) + v) *
  3014. static_cast<size_t>(x_stride) +
  3015. u;
  3016. } else {
  3017. image += static_cast<size_t>(
  3018. (height - 1 - (line_no + static_cast<int>(v)))) *
  3019. static_cast<size_t>(x_stride) +
  3020. u;
  3021. }
  3022. *image = val;
  3023. }
  3024. }
  3025. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  3026. TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT, false);
  3027. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3028. const float *line_ptr = reinterpret_cast<float *>(&outBuf.at(
  3029. v * pixel_data_size * static_cast<size_t>(width) +
  3030. channel_offset_list[c] * static_cast<size_t>(width)));
  3031. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  3032. float val;
  3033. // val = line_ptr[u];
  3034. tinyexr::cpy4(&val, line_ptr + u);
  3035. tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
  3036. float *image = reinterpret_cast<float **>(out_images)[c];
  3037. if (line_order == 0) {
  3038. image += (static_cast<size_t>(line_no) + v) *
  3039. static_cast<size_t>(x_stride) +
  3040. u;
  3041. } else {
  3042. image += static_cast<size_t>(
  3043. (height - 1 - (line_no + static_cast<int>(v)))) *
  3044. static_cast<size_t>(x_stride) +
  3045. u;
  3046. }
  3047. *image = val;
  3048. }
  3049. }
  3050. } else {
  3051. return false;
  3052. }
  3053. }
  3054. #else
  3055. return false;
  3056. #endif
  3057. } else if (compression_type == TINYEXR_COMPRESSIONTYPE_ZIPS ||
  3058. compression_type == TINYEXR_COMPRESSIONTYPE_ZIP) {
  3059. // Allocate original data size.
  3060. std::vector<unsigned char> outBuf(static_cast<size_t>(width) *
  3061. static_cast<size_t>(num_lines) *
  3062. pixel_data_size);
  3063. unsigned long dstLen = static_cast<unsigned long>(outBuf.size());
  3064. TINYEXR_CHECK_AND_RETURN_C(dstLen > 0, false);
  3065. if (!tinyexr::DecompressZip(
  3066. reinterpret_cast<unsigned char *>(&outBuf.at(0)), &dstLen, data_ptr,
  3067. static_cast<unsigned long>(data_len))) {
  3068. return false;
  3069. }
  3070. // For ZIP_COMPRESSION:
  3071. // pixel sample data for channel 0 for scanline 0
  3072. // pixel sample data for channel 1 for scanline 0
  3073. // pixel sample data for channel ... for scanline 0
  3074. // pixel sample data for channel n for scanline 0
  3075. // pixel sample data for channel 0 for scanline 1
  3076. // pixel sample data for channel 1 for scanline 1
  3077. // pixel sample data for channel ... for scanline 1
  3078. // pixel sample data for channel n for scanline 1
  3079. // ...
  3080. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  3081. if (channels[c].pixel_type == TINYEXR_PIXELTYPE_HALF) {
  3082. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3083. const unsigned short *line_ptr = reinterpret_cast<unsigned short *>(
  3084. &outBuf.at(v * static_cast<size_t>(pixel_data_size) *
  3085. static_cast<size_t>(width) +
  3086. channel_offset_list[c] * static_cast<size_t>(width)));
  3087. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  3088. tinyexr::FP16 hf;
  3089. // hf.u = line_ptr[u];
  3090. tinyexr::cpy2(&(hf.u), line_ptr + u);
  3091. tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
  3092. if (requested_pixel_types[c] == TINYEXR_PIXELTYPE_HALF) {
  3093. unsigned short *image =
  3094. reinterpret_cast<unsigned short **>(out_images)[c];
  3095. if (line_order == 0) {
  3096. image += (static_cast<size_t>(line_no) + v) *
  3097. static_cast<size_t>(x_stride) +
  3098. u;
  3099. } else {
  3100. image += (static_cast<size_t>(height) - 1U -
  3101. (static_cast<size_t>(line_no) + v)) *
  3102. static_cast<size_t>(x_stride) +
  3103. u;
  3104. }
  3105. *image = hf.u;
  3106. } else { // HALF -> FLOAT
  3107. tinyexr::FP32 f32 = half_to_float(hf);
  3108. float *image = reinterpret_cast<float **>(out_images)[c];
  3109. size_t offset = 0;
  3110. if (line_order == 0) {
  3111. offset = (static_cast<size_t>(line_no) + v) *
  3112. static_cast<size_t>(x_stride) +
  3113. u;
  3114. } else {
  3115. offset = (static_cast<size_t>(height) - 1U -
  3116. (static_cast<size_t>(line_no) + v)) *
  3117. static_cast<size_t>(x_stride) +
  3118. u;
  3119. }
  3120. image += offset;
  3121. *image = f32.f;
  3122. }
  3123. }
  3124. }
  3125. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) {
  3126. TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT, false);
  3127. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3128. const unsigned int *line_ptr = reinterpret_cast<unsigned int *>(
  3129. &outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
  3130. channel_offset_list[c] * static_cast<size_t>(width)));
  3131. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  3132. unsigned int val;
  3133. // val = line_ptr[u];
  3134. tinyexr::cpy4(&val, line_ptr + u);
  3135. tinyexr::swap4(&val);
  3136. unsigned int *image =
  3137. reinterpret_cast<unsigned int **>(out_images)[c];
  3138. if (line_order == 0) {
  3139. image += (static_cast<size_t>(line_no) + v) *
  3140. static_cast<size_t>(x_stride) +
  3141. u;
  3142. } else {
  3143. image += (static_cast<size_t>(height) - 1U -
  3144. (static_cast<size_t>(line_no) + v)) *
  3145. static_cast<size_t>(x_stride) +
  3146. u;
  3147. }
  3148. *image = val;
  3149. }
  3150. }
  3151. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  3152. TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT, false);
  3153. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3154. const float *line_ptr = reinterpret_cast<float *>(
  3155. &outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
  3156. channel_offset_list[c] * static_cast<size_t>(width)));
  3157. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  3158. float val;
  3159. // val = line_ptr[u];
  3160. tinyexr::cpy4(&val, line_ptr + u);
  3161. tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
  3162. float *image = reinterpret_cast<float **>(out_images)[c];
  3163. if (line_order == 0) {
  3164. image += (static_cast<size_t>(line_no) + v) *
  3165. static_cast<size_t>(x_stride) +
  3166. u;
  3167. } else {
  3168. image += (static_cast<size_t>(height) - 1U -
  3169. (static_cast<size_t>(line_no) + v)) *
  3170. static_cast<size_t>(x_stride) +
  3171. u;
  3172. }
  3173. *image = val;
  3174. }
  3175. }
  3176. } else {
  3177. return false;
  3178. }
  3179. }
  3180. } else if (compression_type == TINYEXR_COMPRESSIONTYPE_RLE) {
  3181. // Allocate original data size.
  3182. std::vector<unsigned char> outBuf(static_cast<size_t>(width) *
  3183. static_cast<size_t>(num_lines) *
  3184. pixel_data_size);
  3185. unsigned long dstLen = static_cast<unsigned long>(outBuf.size());
  3186. if (dstLen == 0) {
  3187. return false;
  3188. }
  3189. if (!tinyexr::DecompressRle(
  3190. reinterpret_cast<unsigned char *>(&outBuf.at(0)), dstLen, data_ptr,
  3191. static_cast<unsigned long>(data_len))) {
  3192. return false;
  3193. }
  3194. // For RLE_COMPRESSION:
  3195. // pixel sample data for channel 0 for scanline 0
  3196. // pixel sample data for channel 1 for scanline 0
  3197. // pixel sample data for channel ... for scanline 0
  3198. // pixel sample data for channel n for scanline 0
  3199. // pixel sample data for channel 0 for scanline 1
  3200. // pixel sample data for channel 1 for scanline 1
  3201. // pixel sample data for channel ... for scanline 1
  3202. // pixel sample data for channel n for scanline 1
  3203. // ...
  3204. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  3205. if (channels[c].pixel_type == TINYEXR_PIXELTYPE_HALF) {
  3206. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3207. const unsigned short *line_ptr = reinterpret_cast<unsigned short *>(
  3208. &outBuf.at(v * static_cast<size_t>(pixel_data_size) *
  3209. static_cast<size_t>(width) +
  3210. channel_offset_list[c] * static_cast<size_t>(width)));
  3211. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  3212. tinyexr::FP16 hf;
  3213. // hf.u = line_ptr[u];
  3214. tinyexr::cpy2(&(hf.u), line_ptr + u);
  3215. tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
  3216. if (requested_pixel_types[c] == TINYEXR_PIXELTYPE_HALF) {
  3217. unsigned short *image =
  3218. reinterpret_cast<unsigned short **>(out_images)[c];
  3219. if (line_order == 0) {
  3220. image += (static_cast<size_t>(line_no) + v) *
  3221. static_cast<size_t>(x_stride) +
  3222. u;
  3223. } else {
  3224. image += (static_cast<size_t>(height) - 1U -
  3225. (static_cast<size_t>(line_no) + v)) *
  3226. static_cast<size_t>(x_stride) +
  3227. u;
  3228. }
  3229. *image = hf.u;
  3230. } else { // HALF -> FLOAT
  3231. tinyexr::FP32 f32 = half_to_float(hf);
  3232. float *image = reinterpret_cast<float **>(out_images)[c];
  3233. if (line_order == 0) {
  3234. image += (static_cast<size_t>(line_no) + v) *
  3235. static_cast<size_t>(x_stride) +
  3236. u;
  3237. } else {
  3238. image += (static_cast<size_t>(height) - 1U -
  3239. (static_cast<size_t>(line_no) + v)) *
  3240. static_cast<size_t>(x_stride) +
  3241. u;
  3242. }
  3243. *image = f32.f;
  3244. }
  3245. }
  3246. }
  3247. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) {
  3248. TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_UINT, false);
  3249. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3250. const unsigned int *line_ptr = reinterpret_cast<unsigned int *>(
  3251. &outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
  3252. channel_offset_list[c] * static_cast<size_t>(width)));
  3253. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  3254. unsigned int val;
  3255. // val = line_ptr[u];
  3256. tinyexr::cpy4(&val, line_ptr + u);
  3257. tinyexr::swap4(&val);
  3258. unsigned int *image =
  3259. reinterpret_cast<unsigned int **>(out_images)[c];
  3260. if (line_order == 0) {
  3261. image += (static_cast<size_t>(line_no) + v) *
  3262. static_cast<size_t>(x_stride) +
  3263. u;
  3264. } else {
  3265. image += (static_cast<size_t>(height) - 1U -
  3266. (static_cast<size_t>(line_no) + v)) *
  3267. static_cast<size_t>(x_stride) +
  3268. u;
  3269. }
  3270. *image = val;
  3271. }
  3272. }
  3273. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  3274. TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT, false);
  3275. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3276. const float *line_ptr = reinterpret_cast<float *>(
  3277. &outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
  3278. channel_offset_list[c] * static_cast<size_t>(width)));
  3279. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  3280. float val;
  3281. // val = line_ptr[u];
  3282. tinyexr::cpy4(&val, line_ptr + u);
  3283. tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
  3284. float *image = reinterpret_cast<float **>(out_images)[c];
  3285. if (line_order == 0) {
  3286. image += (static_cast<size_t>(line_no) + v) *
  3287. static_cast<size_t>(x_stride) +
  3288. u;
  3289. } else {
  3290. image += (static_cast<size_t>(height) - 1U -
  3291. (static_cast<size_t>(line_no) + v)) *
  3292. static_cast<size_t>(x_stride) +
  3293. u;
  3294. }
  3295. *image = val;
  3296. }
  3297. }
  3298. } else {
  3299. return false;
  3300. }
  3301. }
  3302. } else if (compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) {
  3303. #if TINYEXR_USE_ZFP
  3304. tinyexr::ZFPCompressionParam zfp_compression_param;
  3305. std::string e;
  3306. if (!tinyexr::FindZFPCompressionParam(&zfp_compression_param, attributes,
  3307. int(num_attributes), &e)) {
  3308. // This code path should not be reachable.
  3309. return false;
  3310. }
  3311. // Allocate original data size.
  3312. std::vector<unsigned char> outBuf(static_cast<size_t>(width) *
  3313. static_cast<size_t>(num_lines) *
  3314. pixel_data_size);
  3315. unsigned long dstLen = outBuf.size();
  3316. TINYEXR_CHECK_AND_RETURN_C(dstLen > 0, false);
  3317. tinyexr::DecompressZfp(reinterpret_cast<float *>(&outBuf.at(0)), width,
  3318. num_lines, num_channels, data_ptr,
  3319. static_cast<unsigned long>(data_len),
  3320. zfp_compression_param);
  3321. // For ZFP_COMPRESSION:
  3322. // pixel sample data for channel 0 for scanline 0
  3323. // pixel sample data for channel 1 for scanline 0
  3324. // pixel sample data for channel ... for scanline 0
  3325. // pixel sample data for channel n for scanline 0
  3326. // pixel sample data for channel 0 for scanline 1
  3327. // pixel sample data for channel 1 for scanline 1
  3328. // pixel sample data for channel ... for scanline 1
  3329. // pixel sample data for channel n for scanline 1
  3330. // ...
  3331. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  3332. TINYEXR_CHECK_AND_RETURN_C(channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT, false);
  3333. if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  3334. TINYEXR_CHECK_AND_RETURN_C(requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT, false);
  3335. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3336. const float *line_ptr = reinterpret_cast<float *>(
  3337. &outBuf.at(v * pixel_data_size * static_cast<size_t>(width) +
  3338. channel_offset_list[c] * static_cast<size_t>(width)));
  3339. for (size_t u = 0; u < static_cast<size_t>(width); u++) {
  3340. float val;
  3341. tinyexr::cpy4(&val, line_ptr + u);
  3342. tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
  3343. float *image = reinterpret_cast<float **>(out_images)[c];
  3344. if (line_order == 0) {
  3345. image += (static_cast<size_t>(line_no) + v) *
  3346. static_cast<size_t>(x_stride) +
  3347. u;
  3348. } else {
  3349. image += (static_cast<size_t>(height) - 1U -
  3350. (static_cast<size_t>(line_no) + v)) *
  3351. static_cast<size_t>(x_stride) +
  3352. u;
  3353. }
  3354. *image = val;
  3355. }
  3356. }
  3357. } else {
  3358. return false;
  3359. }
  3360. }
  3361. #else
  3362. (void)attributes;
  3363. (void)num_attributes;
  3364. (void)num_channels;
  3365. return false;
  3366. #endif
  3367. } else if (compression_type == TINYEXR_COMPRESSIONTYPE_NONE) {
  3368. for (size_t c = 0; c < num_channels; c++) {
  3369. for (size_t v = 0; v < static_cast<size_t>(num_lines); v++) {
  3370. if (channels[c].pixel_type == TINYEXR_PIXELTYPE_HALF) {
  3371. const unsigned short *line_ptr =
  3372. reinterpret_cast<const unsigned short *>(
  3373. data_ptr + v * pixel_data_size * size_t(width) +
  3374. channel_offset_list[c] * static_cast<size_t>(width));
  3375. if (requested_pixel_types[c] == TINYEXR_PIXELTYPE_HALF) {
  3376. unsigned short *outLine =
  3377. reinterpret_cast<unsigned short *>(out_images[c]);
  3378. if (line_order == 0) {
  3379. outLine += (size_t(y) + v) * size_t(x_stride);
  3380. } else {
  3381. outLine +=
  3382. (size_t(height) - 1 - (size_t(y) + v)) * size_t(x_stride);
  3383. }
  3384. for (int u = 0; u < width; u++) {
  3385. tinyexr::FP16 hf;
  3386. // hf.u = line_ptr[u];
  3387. tinyexr::cpy2(&(hf.u), line_ptr + u);
  3388. tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
  3389. outLine[u] = hf.u;
  3390. }
  3391. } else if (requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT) {
  3392. float *outLine = reinterpret_cast<float *>(out_images[c]);
  3393. if (line_order == 0) {
  3394. outLine += (size_t(y) + v) * size_t(x_stride);
  3395. } else {
  3396. outLine +=
  3397. (size_t(height) - 1 - (size_t(y) + v)) * size_t(x_stride);
  3398. }
  3399. if (reinterpret_cast<const unsigned char *>(line_ptr + width) >
  3400. (data_ptr + data_len)) {
  3401. // Insufficient data size
  3402. return false;
  3403. }
  3404. for (int u = 0; u < width; u++) {
  3405. tinyexr::FP16 hf;
  3406. // address may not be aligned. use byte-wise copy for safety.#76
  3407. // hf.u = line_ptr[u];
  3408. tinyexr::cpy2(&(hf.u), line_ptr + u);
  3409. tinyexr::swap2(reinterpret_cast<unsigned short *>(&hf.u));
  3410. tinyexr::FP32 f32 = half_to_float(hf);
  3411. outLine[u] = f32.f;
  3412. }
  3413. } else {
  3414. return false;
  3415. }
  3416. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  3417. const float *line_ptr = reinterpret_cast<const float *>(
  3418. data_ptr + v * pixel_data_size * size_t(width) +
  3419. channel_offset_list[c] * static_cast<size_t>(width));
  3420. float *outLine = reinterpret_cast<float *>(out_images[c]);
  3421. if (line_order == 0) {
  3422. outLine += (size_t(y) + v) * size_t(x_stride);
  3423. } else {
  3424. outLine +=
  3425. (size_t(height) - 1 - (size_t(y) + v)) * size_t(x_stride);
  3426. }
  3427. if (reinterpret_cast<const unsigned char *>(line_ptr + width) >
  3428. (data_ptr + data_len)) {
  3429. // Insufficient data size
  3430. return false;
  3431. }
  3432. for (int u = 0; u < width; u++) {
  3433. float val;
  3434. tinyexr::cpy4(&val, line_ptr + u);
  3435. tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
  3436. outLine[u] = val;
  3437. }
  3438. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) {
  3439. const unsigned int *line_ptr = reinterpret_cast<const unsigned int *>(
  3440. data_ptr + v * pixel_data_size * size_t(width) +
  3441. channel_offset_list[c] * static_cast<size_t>(width));
  3442. unsigned int *outLine =
  3443. reinterpret_cast<unsigned int *>(out_images[c]);
  3444. if (line_order == 0) {
  3445. outLine += (size_t(y) + v) * size_t(x_stride);
  3446. } else {
  3447. outLine +=
  3448. (size_t(height) - 1 - (size_t(y) + v)) * size_t(x_stride);
  3449. }
  3450. if (reinterpret_cast<const unsigned char *>(line_ptr + width) >
  3451. (data_ptr + data_len)) {
  3452. // Corrupted data
  3453. return false;
  3454. }
  3455. for (int u = 0; u < width; u++) {
  3456. unsigned int val;
  3457. tinyexr::cpy4(&val, line_ptr + u);
  3458. tinyexr::swap4(reinterpret_cast<unsigned int *>(&val));
  3459. outLine[u] = val;
  3460. }
  3461. }
  3462. }
  3463. }
  3464. }
  3465. return true;
  3466. }
  3467. static bool DecodeTiledPixelData(
  3468. unsigned char **out_images, int *width, int *height,
  3469. const int *requested_pixel_types, const unsigned char *data_ptr,
  3470. size_t data_len, int compression_type, int line_order, int data_width,
  3471. int data_height, int tile_offset_x, int tile_offset_y, int tile_size_x,
  3472. int tile_size_y, size_t pixel_data_size, size_t num_attributes,
  3473. const EXRAttribute *attributes, size_t num_channels,
  3474. const EXRChannelInfo *channels,
  3475. const std::vector<size_t> &channel_offset_list) {
  3476. // Here, data_width and data_height are the dimensions of the current (sub)level.
  3477. if (tile_size_x * tile_offset_x > data_width ||
  3478. tile_size_y * tile_offset_y > data_height) {
  3479. return false;
  3480. }
  3481. // Compute actual image size in a tile.
  3482. if ((tile_offset_x + 1) * tile_size_x >= data_width) {
  3483. (*width) = data_width - (tile_offset_x * tile_size_x);
  3484. } else {
  3485. (*width) = tile_size_x;
  3486. }
  3487. if ((tile_offset_y + 1) * tile_size_y >= data_height) {
  3488. (*height) = data_height - (tile_offset_y * tile_size_y);
  3489. } else {
  3490. (*height) = tile_size_y;
  3491. }
  3492. // Image size = tile size.
  3493. return DecodePixelData(out_images, requested_pixel_types, data_ptr, data_len,
  3494. compression_type, line_order, (*width), tile_size_y,
  3495. /* stride */ tile_size_x, /* y */ 0, /* line_no */ 0,
  3496. (*height), pixel_data_size, num_attributes, attributes,
  3497. num_channels, channels, channel_offset_list);
  3498. }
  3499. static bool ComputeChannelLayout(std::vector<size_t> *channel_offset_list,
  3500. int *pixel_data_size, size_t *channel_offset,
  3501. int num_channels,
  3502. const EXRChannelInfo *channels) {
  3503. channel_offset_list->resize(static_cast<size_t>(num_channels));
  3504. (*pixel_data_size) = 0;
  3505. (*channel_offset) = 0;
  3506. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  3507. (*channel_offset_list)[c] = (*channel_offset);
  3508. if (channels[c].pixel_type == TINYEXR_PIXELTYPE_HALF) {
  3509. (*pixel_data_size) += sizeof(unsigned short);
  3510. (*channel_offset) += sizeof(unsigned short);
  3511. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  3512. (*pixel_data_size) += sizeof(float);
  3513. (*channel_offset) += sizeof(float);
  3514. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) {
  3515. (*pixel_data_size) += sizeof(unsigned int);
  3516. (*channel_offset) += sizeof(unsigned int);
  3517. } else {
  3518. // ???
  3519. return false;
  3520. }
  3521. }
  3522. return true;
  3523. }
  3524. // TODO: Simply return nullptr when failed to allocate?
  3525. static unsigned char **AllocateImage(int num_channels,
  3526. const EXRChannelInfo *channels,
  3527. const int *requested_pixel_types,
  3528. int data_width, int data_height, bool *success) {
  3529. unsigned char **images =
  3530. reinterpret_cast<unsigned char **>(static_cast<float **>(
  3531. malloc(sizeof(float *) * static_cast<size_t>(num_channels))));
  3532. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  3533. images[c] = NULL;
  3534. }
  3535. bool valid = true;
  3536. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  3537. size_t data_len =
  3538. static_cast<size_t>(data_width) * static_cast<size_t>(data_height);
  3539. if (channels[c].pixel_type == TINYEXR_PIXELTYPE_HALF) {
  3540. // pixel_data_size += sizeof(unsigned short);
  3541. // channel_offset += sizeof(unsigned short);
  3542. // Alloc internal image for half type.
  3543. if (requested_pixel_types[c] == TINYEXR_PIXELTYPE_HALF) {
  3544. images[c] =
  3545. reinterpret_cast<unsigned char *>(static_cast<unsigned short *>(
  3546. malloc(sizeof(unsigned short) * data_len)));
  3547. } else if (requested_pixel_types[c] == TINYEXR_PIXELTYPE_FLOAT) {
  3548. images[c] = reinterpret_cast<unsigned char *>(
  3549. static_cast<float *>(malloc(sizeof(float) * data_len)));
  3550. } else {
  3551. images[c] = NULL; // just in case.
  3552. valid = false;
  3553. break;
  3554. }
  3555. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  3556. // pixel_data_size += sizeof(float);
  3557. // channel_offset += sizeof(float);
  3558. images[c] = reinterpret_cast<unsigned char *>(
  3559. static_cast<float *>(malloc(sizeof(float) * data_len)));
  3560. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) {
  3561. // pixel_data_size += sizeof(unsigned int);
  3562. // channel_offset += sizeof(unsigned int);
  3563. images[c] = reinterpret_cast<unsigned char *>(
  3564. static_cast<unsigned int *>(malloc(sizeof(unsigned int) * data_len)));
  3565. } else {
  3566. images[c] = NULL; // just in case.
  3567. valid = false;
  3568. break;
  3569. }
  3570. }
  3571. if (!valid) {
  3572. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  3573. if (images[c]) {
  3574. free(images[c]);
  3575. images[c] = NULL;
  3576. }
  3577. }
  3578. if (success) {
  3579. (*success) = false;
  3580. }
  3581. } else {
  3582. if (success) {
  3583. (*success) = true;
  3584. }
  3585. }
  3586. return images;
  3587. }
  3588. #ifdef _WIN32
  3589. static inline std::wstring UTF8ToWchar(const std::string &str) {
  3590. int wstr_size =
  3591. MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), NULL, 0);
  3592. std::wstring wstr(wstr_size, 0);
  3593. MultiByteToWideChar(CP_UTF8, 0, str.data(), (int)str.size(), &wstr[0],
  3594. (int)wstr.size());
  3595. return wstr;
  3596. }
  3597. #endif
  3598. static int ParseEXRHeader(HeaderInfo *info, bool *empty_header,
  3599. const EXRVersion *version, std::string *err,
  3600. const unsigned char *buf, size_t size) {
  3601. const char *marker = reinterpret_cast<const char *>(&buf[0]);
  3602. if (empty_header) {
  3603. (*empty_header) = false;
  3604. }
  3605. if (version->multipart) {
  3606. if (size > 0 && marker[0] == '\0') {
  3607. // End of header list.
  3608. if (empty_header) {
  3609. (*empty_header) = true;
  3610. }
  3611. return TINYEXR_SUCCESS;
  3612. }
  3613. }
  3614. // According to the spec, the header of every OpenEXR file must contain at
  3615. // least the following attributes:
  3616. //
  3617. // channels chlist
  3618. // compression compression
  3619. // dataWindow box2i
  3620. // displayWindow box2i
  3621. // lineOrder lineOrder
  3622. // pixelAspectRatio float
  3623. // screenWindowCenter v2f
  3624. // screenWindowWidth float
  3625. bool has_channels = false;
  3626. bool has_compression = false;
  3627. bool has_data_window = false;
  3628. bool has_display_window = false;
  3629. bool has_line_order = false;
  3630. bool has_pixel_aspect_ratio = false;
  3631. bool has_screen_window_center = false;
  3632. bool has_screen_window_width = false;
  3633. bool has_name = false;
  3634. bool has_type = false;
  3635. info->name.clear();
  3636. info->type.clear();
  3637. info->data_window.min_x = 0;
  3638. info->data_window.min_y = 0;
  3639. info->data_window.max_x = 0;
  3640. info->data_window.max_y = 0;
  3641. info->line_order = 0; // @fixme
  3642. info->display_window.min_x = 0;
  3643. info->display_window.min_y = 0;
  3644. info->display_window.max_x = 0;
  3645. info->display_window.max_y = 0;
  3646. info->screen_window_center[0] = 0.0f;
  3647. info->screen_window_center[1] = 0.0f;
  3648. info->screen_window_width = -1.0f;
  3649. info->pixel_aspect_ratio = -1.0f;
  3650. info->tiled = 0;
  3651. info->tile_size_x = -1;
  3652. info->tile_size_y = -1;
  3653. info->tile_level_mode = -1;
  3654. info->tile_rounding_mode = -1;
  3655. info->attributes.clear();
  3656. // Read attributes
  3657. size_t orig_size = size;
  3658. for (size_t nattr = 0; nattr < TINYEXR_MAX_HEADER_ATTRIBUTES; nattr++) {
  3659. if (0 == size) {
  3660. if (err) {
  3661. (*err) += "Insufficient data size for attributes.\n";
  3662. }
  3663. return TINYEXR_ERROR_INVALID_DATA;
  3664. } else if (marker[0] == '\0') {
  3665. size--;
  3666. break;
  3667. }
  3668. std::string attr_name;
  3669. std::string attr_type;
  3670. std::vector<unsigned char> data;
  3671. size_t marker_size;
  3672. if (!tinyexr::ReadAttribute(&attr_name, &attr_type, &data, &marker_size,
  3673. marker, size)) {
  3674. if (err) {
  3675. (*err) += "Failed to read attribute.\n";
  3676. }
  3677. return TINYEXR_ERROR_INVALID_DATA;
  3678. }
  3679. marker += marker_size;
  3680. size -= marker_size;
  3681. // For a multipart file, the version field 9th bit is 0.
  3682. if ((version->tiled || version->multipart || version->non_image) && attr_name.compare("tiles") == 0) {
  3683. unsigned int x_size, y_size;
  3684. unsigned char tile_mode;
  3685. if (data.size() != 9) {
  3686. if (err) {
  3687. (*err) += "(ParseEXRHeader) Invalid attribute data size. Attribute data size must be 9.\n";
  3688. }
  3689. return TINYEXR_ERROR_INVALID_DATA;
  3690. }
  3691. memcpy(&x_size, &data.at(0), sizeof(int));
  3692. memcpy(&y_size, &data.at(4), sizeof(int));
  3693. tile_mode = data[8];
  3694. tinyexr::swap4(&x_size);
  3695. tinyexr::swap4(&y_size);
  3696. if (x_size > static_cast<unsigned int>(std::numeric_limits<int>::max()) ||
  3697. y_size > static_cast<unsigned int>(std::numeric_limits<int>::max())) {
  3698. if (err) {
  3699. (*err) = "Tile sizes were invalid.";
  3700. }
  3701. return TINYEXR_ERROR_UNSUPPORTED_FORMAT;
  3702. }
  3703. info->tile_size_x = static_cast<int>(x_size);
  3704. info->tile_size_y = static_cast<int>(y_size);
  3705. // mode = levelMode + roundingMode * 16
  3706. info->tile_level_mode = tile_mode & 0x3;
  3707. info->tile_rounding_mode = (tile_mode >> 4) & 0x1;
  3708. info->tiled = 1;
  3709. } else if (attr_name.compare("compression") == 0) {
  3710. bool ok = false;
  3711. if (data[0] < TINYEXR_COMPRESSIONTYPE_PIZ) {
  3712. ok = true;
  3713. }
  3714. if (data[0] == TINYEXR_COMPRESSIONTYPE_PIZ) {
  3715. #if TINYEXR_USE_PIZ
  3716. ok = true;
  3717. #else
  3718. if (err) {
  3719. (*err) = "PIZ compression is not supported.";
  3720. }
  3721. return TINYEXR_ERROR_UNSUPPORTED_FORMAT;
  3722. #endif
  3723. }
  3724. if (data[0] == TINYEXR_COMPRESSIONTYPE_ZFP) {
  3725. #if TINYEXR_USE_ZFP
  3726. ok = true;
  3727. #else
  3728. if (err) {
  3729. (*err) = "ZFP compression is not supported.";
  3730. }
  3731. return TINYEXR_ERROR_UNSUPPORTED_FORMAT;
  3732. #endif
  3733. }
  3734. if (!ok) {
  3735. if (err) {
  3736. (*err) = "Unknown compression type.";
  3737. }
  3738. return TINYEXR_ERROR_UNSUPPORTED_FORMAT;
  3739. }
  3740. info->compression_type = static_cast<int>(data[0]);
  3741. has_compression = true;
  3742. } else if (attr_name.compare("channels") == 0) {
  3743. // name: zero-terminated string, from 1 to 255 bytes long
  3744. // pixel type: int, possible values are: UINT = 0 HALF = 1 FLOAT = 2
  3745. // pLinear: unsigned char, possible values are 0 and 1
  3746. // reserved: three chars, should be zero
  3747. // xSampling: int
  3748. // ySampling: int
  3749. if (!ReadChannelInfo(info->channels, data)) {
  3750. if (err) {
  3751. (*err) += "Failed to parse channel info.\n";
  3752. }
  3753. return TINYEXR_ERROR_INVALID_DATA;
  3754. }
  3755. if (info->channels.size() < 1) {
  3756. if (err) {
  3757. (*err) += "# of channels is zero.\n";
  3758. }
  3759. return TINYEXR_ERROR_INVALID_DATA;
  3760. }
  3761. has_channels = true;
  3762. } else if (attr_name.compare("dataWindow") == 0) {
  3763. if (data.size() >= 16) {
  3764. memcpy(&info->data_window.min_x, &data.at(0), sizeof(int));
  3765. memcpy(&info->data_window.min_y, &data.at(4), sizeof(int));
  3766. memcpy(&info->data_window.max_x, &data.at(8), sizeof(int));
  3767. memcpy(&info->data_window.max_y, &data.at(12), sizeof(int));
  3768. tinyexr::swap4(&info->data_window.min_x);
  3769. tinyexr::swap4(&info->data_window.min_y);
  3770. tinyexr::swap4(&info->data_window.max_x);
  3771. tinyexr::swap4(&info->data_window.max_y);
  3772. has_data_window = true;
  3773. }
  3774. } else if (attr_name.compare("displayWindow") == 0) {
  3775. if (data.size() >= 16) {
  3776. memcpy(&info->display_window.min_x, &data.at(0), sizeof(int));
  3777. memcpy(&info->display_window.min_y, &data.at(4), sizeof(int));
  3778. memcpy(&info->display_window.max_x, &data.at(8), sizeof(int));
  3779. memcpy(&info->display_window.max_y, &data.at(12), sizeof(int));
  3780. tinyexr::swap4(&info->display_window.min_x);
  3781. tinyexr::swap4(&info->display_window.min_y);
  3782. tinyexr::swap4(&info->display_window.max_x);
  3783. tinyexr::swap4(&info->display_window.max_y);
  3784. has_display_window = true;
  3785. }
  3786. } else if (attr_name.compare("lineOrder") == 0) {
  3787. if (data.size() >= 1) {
  3788. info->line_order = static_cast<int>(data[0]);
  3789. has_line_order = true;
  3790. }
  3791. } else if (attr_name.compare("pixelAspectRatio") == 0) {
  3792. if (data.size() >= sizeof(float)) {
  3793. memcpy(&info->pixel_aspect_ratio, &data.at(0), sizeof(float));
  3794. tinyexr::swap4(&info->pixel_aspect_ratio);
  3795. has_pixel_aspect_ratio = true;
  3796. }
  3797. } else if (attr_name.compare("screenWindowCenter") == 0) {
  3798. if (data.size() >= 8) {
  3799. memcpy(&info->screen_window_center[0], &data.at(0), sizeof(float));
  3800. memcpy(&info->screen_window_center[1], &data.at(4), sizeof(float));
  3801. tinyexr::swap4(&info->screen_window_center[0]);
  3802. tinyexr::swap4(&info->screen_window_center[1]);
  3803. has_screen_window_center = true;
  3804. }
  3805. } else if (attr_name.compare("screenWindowWidth") == 0) {
  3806. if (data.size() >= sizeof(float)) {
  3807. memcpy(&info->screen_window_width, &data.at(0), sizeof(float));
  3808. tinyexr::swap4(&info->screen_window_width);
  3809. has_screen_window_width = true;
  3810. }
  3811. } else if (attr_name.compare("chunkCount") == 0) {
  3812. if (data.size() >= sizeof(int)) {
  3813. memcpy(&info->chunk_count, &data.at(0), sizeof(int));
  3814. tinyexr::swap4(&info->chunk_count);
  3815. }
  3816. } else if (attr_name.compare("name") == 0) {
  3817. if (!data.empty() && data[0]) {
  3818. data.push_back(0);
  3819. size_t len = strlen(reinterpret_cast<const char*>(&data[0]));
  3820. info->name.resize(len);
  3821. info->name.assign(reinterpret_cast<const char*>(&data[0]), len);
  3822. has_name = true;
  3823. }
  3824. } else if (attr_name.compare("type") == 0) {
  3825. if (!data.empty() && data[0]) {
  3826. data.push_back(0);
  3827. size_t len = strlen(reinterpret_cast<const char*>(&data[0]));
  3828. info->type.resize(len);
  3829. info->type.assign(reinterpret_cast<const char*>(&data[0]), len);
  3830. has_type = true;
  3831. }
  3832. } else {
  3833. // Custom attribute(up to TINYEXR_MAX_CUSTOM_ATTRIBUTES)
  3834. if (info->attributes.size() < TINYEXR_MAX_CUSTOM_ATTRIBUTES) {
  3835. EXRAttribute attrib;
  3836. #ifdef _MSC_VER
  3837. strncpy_s(attrib.name, attr_name.c_str(), 255);
  3838. strncpy_s(attrib.type, attr_type.c_str(), 255);
  3839. #else
  3840. strncpy(attrib.name, attr_name.c_str(), 255);
  3841. strncpy(attrib.type, attr_type.c_str(), 255);
  3842. #endif
  3843. attrib.name[255] = '\0';
  3844. attrib.type[255] = '\0';
  3845. //std::cout << "i = " << info->attributes.size() << ", dsize = " << data.size() << "\n";
  3846. attrib.size = static_cast<int>(data.size());
  3847. attrib.value = static_cast<unsigned char *>(malloc(data.size()));
  3848. memcpy(reinterpret_cast<char *>(attrib.value), &data.at(0),
  3849. data.size());
  3850. info->attributes.push_back(attrib);
  3851. }
  3852. }
  3853. }
  3854. // Check if required attributes exist
  3855. {
  3856. std::stringstream ss_err;
  3857. if (!has_compression) {
  3858. ss_err << "\"compression\" attribute not found in the header."
  3859. << std::endl;
  3860. }
  3861. if (!has_channels) {
  3862. ss_err << "\"channels\" attribute not found in the header." << std::endl;
  3863. }
  3864. if (!has_line_order) {
  3865. ss_err << "\"lineOrder\" attribute not found in the header." << std::endl;
  3866. }
  3867. if (!has_display_window) {
  3868. ss_err << "\"displayWindow\" attribute not found in the header."
  3869. << std::endl;
  3870. }
  3871. if (!has_data_window) {
  3872. ss_err << "\"dataWindow\" attribute not found in the header or invalid."
  3873. << std::endl;
  3874. }
  3875. if (!has_pixel_aspect_ratio) {
  3876. ss_err << "\"pixelAspectRatio\" attribute not found in the header."
  3877. << std::endl;
  3878. }
  3879. if (!has_screen_window_width) {
  3880. ss_err << "\"screenWindowWidth\" attribute not found in the header."
  3881. << std::endl;
  3882. }
  3883. if (!has_screen_window_center) {
  3884. ss_err << "\"screenWindowCenter\" attribute not found in the header."
  3885. << std::endl;
  3886. }
  3887. if (version->multipart || version->non_image) {
  3888. if (!has_name) {
  3889. ss_err << "\"name\" attribute not found in the header."
  3890. << std::endl;
  3891. }
  3892. if (!has_type) {
  3893. ss_err << "\"type\" attribute not found in the header."
  3894. << std::endl;
  3895. }
  3896. }
  3897. if (!(ss_err.str().empty())) {
  3898. if (err) {
  3899. (*err) += ss_err.str();
  3900. }
  3901. return TINYEXR_ERROR_INVALID_HEADER;
  3902. }
  3903. }
  3904. info->header_len = static_cast<unsigned int>(orig_size - size);
  3905. return TINYEXR_SUCCESS;
  3906. }
  3907. // C++ HeaderInfo to C EXRHeader conversion.
  3908. static bool ConvertHeader(EXRHeader *exr_header, const HeaderInfo &info, std::string *warn, std::string *err) {
  3909. exr_header->pixel_aspect_ratio = info.pixel_aspect_ratio;
  3910. exr_header->screen_window_center[0] = info.screen_window_center[0];
  3911. exr_header->screen_window_center[1] = info.screen_window_center[1];
  3912. exr_header->screen_window_width = info.screen_window_width;
  3913. exr_header->chunk_count = info.chunk_count;
  3914. exr_header->display_window.min_x = info.display_window.min_x;
  3915. exr_header->display_window.min_y = info.display_window.min_y;
  3916. exr_header->display_window.max_x = info.display_window.max_x;
  3917. exr_header->display_window.max_y = info.display_window.max_y;
  3918. exr_header->data_window.min_x = info.data_window.min_x;
  3919. exr_header->data_window.min_y = info.data_window.min_y;
  3920. exr_header->data_window.max_x = info.data_window.max_x;
  3921. exr_header->data_window.max_y = info.data_window.max_y;
  3922. exr_header->line_order = info.line_order;
  3923. exr_header->compression_type = info.compression_type;
  3924. exr_header->tiled = info.tiled;
  3925. exr_header->tile_size_x = info.tile_size_x;
  3926. exr_header->tile_size_y = info.tile_size_y;
  3927. exr_header->tile_level_mode = info.tile_level_mode;
  3928. exr_header->tile_rounding_mode = info.tile_rounding_mode;
  3929. EXRSetNameAttr(exr_header, info.name.c_str());
  3930. if (!info.type.empty()) {
  3931. bool valid = true;
  3932. if (info.type == "scanlineimage") {
  3933. if (exr_header->tiled) {
  3934. if (err) {
  3935. (*err) += "(ConvertHeader) tiled bit must be off for `scanlineimage` type.\n";
  3936. }
  3937. valid = false;
  3938. }
  3939. } else if (info.type == "tiledimage") {
  3940. if (!exr_header->tiled) {
  3941. if (err) {
  3942. (*err) += "(ConvertHeader) tiled bit must be on for `tiledimage` type.\n";
  3943. }
  3944. valid = false;
  3945. }
  3946. } else if (info.type == "deeptile") {
  3947. exr_header->non_image = 1;
  3948. if (!exr_header->tiled) {
  3949. if (err) {
  3950. (*err) += "(ConvertHeader) tiled bit must be on for `deeptile` type.\n";
  3951. }
  3952. valid = false;
  3953. }
  3954. } else if (info.type == "deepscanline") {
  3955. exr_header->non_image = 1;
  3956. if (exr_header->tiled) {
  3957. if (err) {
  3958. (*err) += "(ConvertHeader) tiled bit must be off for `deepscanline` type.\n";
  3959. }
  3960. //valid = false;
  3961. }
  3962. } else {
  3963. if (warn) {
  3964. std::stringstream ss;
  3965. ss << "(ConvertHeader) Unsupported or unknown info.type: " << info.type << "\n";
  3966. (*warn) += ss.str();
  3967. }
  3968. }
  3969. if (!valid) {
  3970. return false;
  3971. }
  3972. }
  3973. exr_header->num_channels = static_cast<int>(info.channels.size());
  3974. exr_header->channels = static_cast<EXRChannelInfo *>(malloc(
  3975. sizeof(EXRChannelInfo) * static_cast<size_t>(exr_header->num_channels)));
  3976. for (size_t c = 0; c < static_cast<size_t>(exr_header->num_channels); c++) {
  3977. #ifdef _MSC_VER
  3978. strncpy_s(exr_header->channels[c].name, info.channels[c].name.c_str(), 255);
  3979. #else
  3980. strncpy(exr_header->channels[c].name, info.channels[c].name.c_str(), 255);
  3981. #endif
  3982. // manually add '\0' for safety.
  3983. exr_header->channels[c].name[255] = '\0';
  3984. exr_header->channels[c].pixel_type = info.channels[c].pixel_type;
  3985. exr_header->channels[c].p_linear = info.channels[c].p_linear;
  3986. exr_header->channels[c].x_sampling = info.channels[c].x_sampling;
  3987. exr_header->channels[c].y_sampling = info.channels[c].y_sampling;
  3988. }
  3989. exr_header->pixel_types = static_cast<int *>(
  3990. malloc(sizeof(int) * static_cast<size_t>(exr_header->num_channels)));
  3991. for (size_t c = 0; c < static_cast<size_t>(exr_header->num_channels); c++) {
  3992. exr_header->pixel_types[c] = info.channels[c].pixel_type;
  3993. }
  3994. // Initially fill with values of `pixel_types`
  3995. exr_header->requested_pixel_types = static_cast<int *>(
  3996. malloc(sizeof(int) * static_cast<size_t>(exr_header->num_channels)));
  3997. for (size_t c = 0; c < static_cast<size_t>(exr_header->num_channels); c++) {
  3998. exr_header->requested_pixel_types[c] = info.channels[c].pixel_type;
  3999. }
  4000. exr_header->num_custom_attributes = static_cast<int>(info.attributes.size());
  4001. if (exr_header->num_custom_attributes > 0) {
  4002. // TODO(syoyo): Report warning when # of attributes exceeds
  4003. // `TINYEXR_MAX_CUSTOM_ATTRIBUTES`
  4004. if (exr_header->num_custom_attributes > TINYEXR_MAX_CUSTOM_ATTRIBUTES) {
  4005. exr_header->num_custom_attributes = TINYEXR_MAX_CUSTOM_ATTRIBUTES;
  4006. }
  4007. exr_header->custom_attributes = static_cast<EXRAttribute *>(malloc(
  4008. sizeof(EXRAttribute) * size_t(exr_header->num_custom_attributes)));
  4009. for (size_t i = 0; i < size_t(exr_header->num_custom_attributes); i++) {
  4010. memcpy(exr_header->custom_attributes[i].name, info.attributes[i].name,
  4011. 256);
  4012. memcpy(exr_header->custom_attributes[i].type, info.attributes[i].type,
  4013. 256);
  4014. exr_header->custom_attributes[i].size = info.attributes[i].size;
  4015. // Just copy pointer
  4016. exr_header->custom_attributes[i].value = info.attributes[i].value;
  4017. }
  4018. } else {
  4019. exr_header->custom_attributes = NULL;
  4020. }
  4021. exr_header->header_len = info.header_len;
  4022. return true;
  4023. }
  4024. struct OffsetData {
  4025. OffsetData() : num_x_levels(0), num_y_levels(0) {}
  4026. std::vector<std::vector<std::vector <tinyexr::tinyexr_uint64> > > offsets;
  4027. int num_x_levels;
  4028. int num_y_levels;
  4029. };
  4030. // -1 = error
  4031. static int LevelIndex(int lx, int ly, int tile_level_mode, int num_x_levels) {
  4032. switch (tile_level_mode) {
  4033. case TINYEXR_TILE_ONE_LEVEL:
  4034. return 0;
  4035. case TINYEXR_TILE_MIPMAP_LEVELS:
  4036. return lx;
  4037. case TINYEXR_TILE_RIPMAP_LEVELS:
  4038. return lx + ly * num_x_levels;
  4039. default:
  4040. return -1;
  4041. }
  4042. return 0;
  4043. }
  4044. static int LevelSize(int toplevel_size, int level, int tile_rounding_mode) {
  4045. if (level < 0) {
  4046. return -1;
  4047. }
  4048. int b = static_cast<int>(1u << static_cast<unsigned int>(level));
  4049. int level_size = toplevel_size / b;
  4050. if (tile_rounding_mode == TINYEXR_TILE_ROUND_UP && level_size * b < toplevel_size)
  4051. level_size += 1;
  4052. return std::max(level_size, 1);
  4053. }
  4054. static int DecodeTiledLevel(EXRImage* exr_image, const EXRHeader* exr_header,
  4055. const OffsetData& offset_data,
  4056. const std::vector<size_t>& channel_offset_list,
  4057. int pixel_data_size,
  4058. const unsigned char* head, const size_t size,
  4059. std::string* err) {
  4060. int num_channels = exr_header->num_channels;
  4061. int level_index = LevelIndex(exr_image->level_x, exr_image->level_y, exr_header->tile_level_mode, offset_data.num_x_levels);
  4062. int num_y_tiles = int(offset_data.offsets[size_t(level_index)].size());
  4063. if (num_y_tiles < 1) {
  4064. return TINYEXR_ERROR_INVALID_DATA;
  4065. }
  4066. int num_x_tiles = int(offset_data.offsets[size_t(level_index)][0].size());
  4067. if (num_x_tiles < 1) {
  4068. return TINYEXR_ERROR_INVALID_DATA;
  4069. }
  4070. int num_tiles = num_x_tiles * num_y_tiles;
  4071. int err_code = TINYEXR_SUCCESS;
  4072. enum {
  4073. EF_SUCCESS = 0,
  4074. EF_INVALID_DATA = 1,
  4075. EF_INSUFFICIENT_DATA = 2,
  4076. EF_FAILED_TO_DECODE = 4
  4077. };
  4078. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  4079. std::atomic<unsigned> error_flag(EF_SUCCESS);
  4080. #else
  4081. unsigned error_flag(EF_SUCCESS);
  4082. #endif
  4083. // Although the spec says : "...the data window is subdivided into an array of smaller rectangles...",
  4084. // the IlmImf library allows the dimensions of the tile to be larger (or equal) than the dimensions of the data window.
  4085. #if 0
  4086. if ((exr_header->tile_size_x > exr_image->width || exr_header->tile_size_y > exr_image->height) &&
  4087. exr_image->level_x == 0 && exr_image->level_y == 0) {
  4088. if (err) {
  4089. (*err) += "Failed to decode tile data.\n";
  4090. }
  4091. err_code = TINYEXR_ERROR_INVALID_DATA;
  4092. }
  4093. #endif
  4094. exr_image->tiles = static_cast<EXRTile*>(
  4095. calloc(sizeof(EXRTile), static_cast<size_t>(num_tiles)));
  4096. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  4097. std::vector<std::thread> workers;
  4098. std::atomic<int> tile_count(0);
  4099. int num_threads = std::max(1, int(std::thread::hardware_concurrency()));
  4100. if (num_threads > int(num_tiles)) {
  4101. num_threads = int(num_tiles);
  4102. }
  4103. for (int t = 0; t < num_threads; t++) {
  4104. workers.emplace_back(std::thread([&]()
  4105. {
  4106. int tile_idx = 0;
  4107. while ((tile_idx = tile_count++) < num_tiles) {
  4108. #else
  4109. #if TINYEXR_USE_OPENMP
  4110. #pragma omp parallel for
  4111. #endif
  4112. for (int tile_idx = 0; tile_idx < num_tiles; tile_idx++) {
  4113. #endif
  4114. // Allocate memory for each tile.
  4115. bool alloc_success = false;
  4116. exr_image->tiles[tile_idx].images = tinyexr::AllocateImage(
  4117. num_channels, exr_header->channels,
  4118. exr_header->requested_pixel_types, exr_header->tile_size_x,
  4119. exr_header->tile_size_y, &alloc_success);
  4120. if (!alloc_success) {
  4121. error_flag |= EF_INVALID_DATA;
  4122. continue;
  4123. }
  4124. int x_tile = tile_idx % num_x_tiles;
  4125. int y_tile = tile_idx / num_x_tiles;
  4126. // 16 byte: tile coordinates
  4127. // 4 byte : data size
  4128. // ~ : data(uncompressed or compressed)
  4129. tinyexr::tinyexr_uint64 offset = offset_data.offsets[size_t(level_index)][size_t(y_tile)][size_t(x_tile)];
  4130. if (offset + sizeof(int) * 5 > size) {
  4131. // Insufficient data size.
  4132. error_flag |= EF_INSUFFICIENT_DATA;
  4133. continue;
  4134. }
  4135. size_t data_size =
  4136. size_t(size - (offset + sizeof(int) * 5));
  4137. const unsigned char* data_ptr =
  4138. reinterpret_cast<const unsigned char*>(head + offset);
  4139. int tile_coordinates[4];
  4140. memcpy(tile_coordinates, data_ptr, sizeof(int) * 4);
  4141. tinyexr::swap4(&tile_coordinates[0]);
  4142. tinyexr::swap4(&tile_coordinates[1]);
  4143. tinyexr::swap4(&tile_coordinates[2]);
  4144. tinyexr::swap4(&tile_coordinates[3]);
  4145. if (tile_coordinates[2] != exr_image->level_x) {
  4146. // Invalid data.
  4147. error_flag |= EF_INVALID_DATA;
  4148. continue;
  4149. }
  4150. if (tile_coordinates[3] != exr_image->level_y) {
  4151. // Invalid data.
  4152. error_flag |= EF_INVALID_DATA;
  4153. continue;
  4154. }
  4155. int data_len;
  4156. memcpy(&data_len, data_ptr + 16,
  4157. sizeof(int)); // 16 = sizeof(tile_coordinates)
  4158. tinyexr::swap4(&data_len);
  4159. if (data_len < 2 || size_t(data_len) > data_size) {
  4160. // Insufficient data size.
  4161. error_flag |= EF_INSUFFICIENT_DATA;
  4162. continue;
  4163. }
  4164. // Move to data addr: 20 = 16 + 4;
  4165. data_ptr += 20;
  4166. bool ret = tinyexr::DecodeTiledPixelData(
  4167. exr_image->tiles[tile_idx].images,
  4168. &(exr_image->tiles[tile_idx].width),
  4169. &(exr_image->tiles[tile_idx].height),
  4170. exr_header->requested_pixel_types, data_ptr,
  4171. static_cast<size_t>(data_len), exr_header->compression_type,
  4172. exr_header->line_order,
  4173. exr_image->width, exr_image->height,
  4174. tile_coordinates[0], tile_coordinates[1], exr_header->tile_size_x,
  4175. exr_header->tile_size_y, static_cast<size_t>(pixel_data_size),
  4176. static_cast<size_t>(exr_header->num_custom_attributes),
  4177. exr_header->custom_attributes,
  4178. static_cast<size_t>(exr_header->num_channels),
  4179. exr_header->channels, channel_offset_list);
  4180. if (!ret) {
  4181. // Failed to decode tile data.
  4182. error_flag |= EF_FAILED_TO_DECODE;
  4183. }
  4184. exr_image->tiles[tile_idx].offset_x = tile_coordinates[0];
  4185. exr_image->tiles[tile_idx].offset_y = tile_coordinates[1];
  4186. exr_image->tiles[tile_idx].level_x = tile_coordinates[2];
  4187. exr_image->tiles[tile_idx].level_y = tile_coordinates[3];
  4188. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  4189. }
  4190. }));
  4191. } // num_thread loop
  4192. for (auto& t : workers) {
  4193. t.join();
  4194. }
  4195. #else
  4196. } // parallel for
  4197. #endif
  4198. // Even in the event of an error, the reserved memory may be freed.
  4199. exr_image->num_channels = num_channels;
  4200. exr_image->num_tiles = static_cast<int>(num_tiles);
  4201. if (error_flag) err_code = TINYEXR_ERROR_INVALID_DATA;
  4202. if (err) {
  4203. if (error_flag & EF_INSUFFICIENT_DATA) {
  4204. (*err) += "Insufficient data length.\n";
  4205. }
  4206. if (error_flag & EF_FAILED_TO_DECODE) {
  4207. (*err) += "Failed to decode tile data.\n";
  4208. }
  4209. }
  4210. return err_code;
  4211. }
  4212. static int DecodeChunk(EXRImage *exr_image, const EXRHeader *exr_header,
  4213. const OffsetData& offset_data,
  4214. const unsigned char *head, const size_t size,
  4215. std::string *err) {
  4216. int num_channels = exr_header->num_channels;
  4217. int num_scanline_blocks = 1;
  4218. if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZIP) {
  4219. num_scanline_blocks = 16;
  4220. } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) {
  4221. num_scanline_blocks = 32;
  4222. } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) {
  4223. num_scanline_blocks = 16;
  4224. #if TINYEXR_USE_ZFP
  4225. tinyexr::ZFPCompressionParam zfp_compression_param;
  4226. if (!FindZFPCompressionParam(&zfp_compression_param,
  4227. exr_header->custom_attributes,
  4228. int(exr_header->num_custom_attributes), err)) {
  4229. return TINYEXR_ERROR_INVALID_HEADER;
  4230. }
  4231. #endif
  4232. }
  4233. if (exr_header->data_window.max_x < exr_header->data_window.min_x ||
  4234. exr_header->data_window.max_y < exr_header->data_window.min_y) {
  4235. if (err) {
  4236. (*err) += "Invalid data window.\n";
  4237. }
  4238. return TINYEXR_ERROR_INVALID_DATA;
  4239. }
  4240. tinyexr_int64 data_width =
  4241. static_cast<tinyexr_int64>(exr_header->data_window.max_x) - static_cast<tinyexr_int64>(exr_header->data_window.min_x) + static_cast<tinyexr_int64>(1);
  4242. tinyexr_int64 data_height =
  4243. static_cast<tinyexr_int64>(exr_header->data_window.max_y) - static_cast<tinyexr_int64>(exr_header->data_window.min_y) + static_cast<tinyexr_int64>(1);
  4244. if (data_width <= 0) {
  4245. if (err) {
  4246. (*err) += "Invalid data window width.\n";
  4247. }
  4248. return TINYEXR_ERROR_INVALID_DATA;
  4249. }
  4250. if (data_height <= 0) {
  4251. if (err) {
  4252. (*err) += "Invalid data window height.\n";
  4253. }
  4254. return TINYEXR_ERROR_INVALID_DATA;
  4255. }
  4256. // Do not allow too large data_width and data_height. header invalid?
  4257. {
  4258. if ((data_width > TINYEXR_DIMENSION_THRESHOLD) || (data_height > TINYEXR_DIMENSION_THRESHOLD)) {
  4259. if (err) {
  4260. std::stringstream ss;
  4261. ss << "data_with or data_height too large. data_width: " << data_width
  4262. << ", "
  4263. << "data_height = " << data_height << std::endl;
  4264. (*err) += ss.str();
  4265. }
  4266. return TINYEXR_ERROR_INVALID_DATA;
  4267. }
  4268. if (exr_header->tiled) {
  4269. if ((exr_header->tile_size_x > TINYEXR_DIMENSION_THRESHOLD) || (exr_header->tile_size_y > TINYEXR_DIMENSION_THRESHOLD)) {
  4270. if (err) {
  4271. std::stringstream ss;
  4272. ss << "tile with or tile height too large. tile width: " << exr_header->tile_size_x
  4273. << ", "
  4274. << "tile height = " << exr_header->tile_size_y << std::endl;
  4275. (*err) += ss.str();
  4276. }
  4277. return TINYEXR_ERROR_INVALID_DATA;
  4278. }
  4279. }
  4280. }
  4281. const std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data.offsets[0][0];
  4282. size_t num_blocks = offsets.size();
  4283. std::vector<size_t> channel_offset_list;
  4284. int pixel_data_size = 0;
  4285. size_t channel_offset = 0;
  4286. if (!tinyexr::ComputeChannelLayout(&channel_offset_list, &pixel_data_size,
  4287. &channel_offset, num_channels,
  4288. exr_header->channels)) {
  4289. if (err) {
  4290. (*err) += "Failed to compute channel layout.\n";
  4291. }
  4292. return TINYEXR_ERROR_INVALID_DATA;
  4293. }
  4294. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  4295. std::atomic<bool> invalid_data(false);
  4296. #else
  4297. bool invalid_data(false);
  4298. #endif
  4299. if (exr_header->tiled) {
  4300. // value check
  4301. if (exr_header->tile_size_x < 0) {
  4302. if (err) {
  4303. std::stringstream ss;
  4304. ss << "Invalid tile size x : " << exr_header->tile_size_x << "\n";
  4305. (*err) += ss.str();
  4306. }
  4307. return TINYEXR_ERROR_INVALID_HEADER;
  4308. }
  4309. if (exr_header->tile_size_y < 0) {
  4310. if (err) {
  4311. std::stringstream ss;
  4312. ss << "Invalid tile size y : " << exr_header->tile_size_y << "\n";
  4313. (*err) += ss.str();
  4314. }
  4315. return TINYEXR_ERROR_INVALID_HEADER;
  4316. }
  4317. if (exr_header->tile_level_mode != TINYEXR_TILE_RIPMAP_LEVELS) {
  4318. EXRImage* level_image = NULL;
  4319. for (int level = 0; level < offset_data.num_x_levels; ++level) {
  4320. if (!level_image) {
  4321. level_image = exr_image;
  4322. } else {
  4323. level_image->next_level = new EXRImage;
  4324. InitEXRImage(level_image->next_level);
  4325. level_image = level_image->next_level;
  4326. }
  4327. level_image->width =
  4328. LevelSize(exr_header->data_window.max_x - exr_header->data_window.min_x + 1, level, exr_header->tile_rounding_mode);
  4329. if (level_image->width < 1) {
  4330. return TINYEXR_ERROR_INVALID_DATA;
  4331. }
  4332. level_image->height =
  4333. LevelSize(exr_header->data_window.max_y - exr_header->data_window.min_y + 1, level, exr_header->tile_rounding_mode);
  4334. if (level_image->height < 1) {
  4335. return TINYEXR_ERROR_INVALID_DATA;
  4336. }
  4337. level_image->level_x = level;
  4338. level_image->level_y = level;
  4339. int ret = DecodeTiledLevel(level_image, exr_header,
  4340. offset_data,
  4341. channel_offset_list,
  4342. pixel_data_size,
  4343. head, size,
  4344. err);
  4345. if (ret != TINYEXR_SUCCESS) return ret;
  4346. }
  4347. } else {
  4348. EXRImage* level_image = NULL;
  4349. for (int level_y = 0; level_y < offset_data.num_y_levels; ++level_y)
  4350. for (int level_x = 0; level_x < offset_data.num_x_levels; ++level_x) {
  4351. if (!level_image) {
  4352. level_image = exr_image;
  4353. } else {
  4354. level_image->next_level = new EXRImage;
  4355. InitEXRImage(level_image->next_level);
  4356. level_image = level_image->next_level;
  4357. }
  4358. level_image->width =
  4359. LevelSize(exr_header->data_window.max_x - exr_header->data_window.min_x + 1, level_x, exr_header->tile_rounding_mode);
  4360. if (level_image->width < 1) {
  4361. return TINYEXR_ERROR_INVALID_DATA;
  4362. }
  4363. level_image->height =
  4364. LevelSize(exr_header->data_window.max_y - exr_header->data_window.min_y + 1, level_y, exr_header->tile_rounding_mode);
  4365. if (level_image->height < 1) {
  4366. return TINYEXR_ERROR_INVALID_DATA;
  4367. }
  4368. level_image->level_x = level_x;
  4369. level_image->level_y = level_y;
  4370. int ret = DecodeTiledLevel(level_image, exr_header,
  4371. offset_data,
  4372. channel_offset_list,
  4373. pixel_data_size,
  4374. head, size,
  4375. err);
  4376. if (ret != TINYEXR_SUCCESS) return ret;
  4377. }
  4378. }
  4379. } else { // scanline format
  4380. // Don't allow too large image(256GB * pixel_data_size or more). Workaround
  4381. // for #104.
  4382. size_t total_data_len =
  4383. size_t(data_width) * size_t(data_height) * size_t(num_channels);
  4384. const bool total_data_len_overflown =
  4385. sizeof(void *) == 8 ? (total_data_len >= 0x4000000000) : false;
  4386. if ((total_data_len == 0) || total_data_len_overflown) {
  4387. if (err) {
  4388. std::stringstream ss;
  4389. ss << "Image data size is zero or too large: width = " << data_width
  4390. << ", height = " << data_height << ", channels = " << num_channels
  4391. << std::endl;
  4392. (*err) += ss.str();
  4393. }
  4394. return TINYEXR_ERROR_INVALID_DATA;
  4395. }
  4396. bool alloc_success = false;
  4397. exr_image->images = tinyexr::AllocateImage(
  4398. num_channels, exr_header->channels, exr_header->requested_pixel_types,
  4399. int(data_width), int(data_height), &alloc_success);
  4400. if (!alloc_success) {
  4401. if (err) {
  4402. std::stringstream ss;
  4403. ss << "Failed to allocate memory for Images. Maybe EXR header is corrupted or Image data size is too large: width = " << data_width
  4404. << ", height = " << data_height << ", channels = " << num_channels
  4405. << std::endl;
  4406. (*err) += ss.str();
  4407. }
  4408. return TINYEXR_ERROR_INVALID_DATA;
  4409. }
  4410. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  4411. std::vector<std::thread> workers;
  4412. std::atomic<int> y_count(0);
  4413. int num_threads = std::max(1, int(std::thread::hardware_concurrency()));
  4414. if (num_threads > int(num_blocks)) {
  4415. num_threads = int(num_blocks);
  4416. }
  4417. for (int t = 0; t < num_threads; t++) {
  4418. workers.emplace_back(std::thread([&]() {
  4419. int y = 0;
  4420. while ((y = y_count++) < int(num_blocks)) {
  4421. #else
  4422. #if TINYEXR_USE_OPENMP
  4423. #pragma omp parallel for
  4424. #endif
  4425. for (int y = 0; y < static_cast<int>(num_blocks); y++) {
  4426. #endif
  4427. size_t y_idx = static_cast<size_t>(y);
  4428. if (offsets[y_idx] + sizeof(int) * 2 > size) {
  4429. invalid_data = true;
  4430. } else {
  4431. // 4 byte: scan line
  4432. // 4 byte: data size
  4433. // ~ : pixel data(uncompressed or compressed)
  4434. size_t data_size =
  4435. size_t(size - (offsets[y_idx] + sizeof(int) * 2));
  4436. const unsigned char *data_ptr =
  4437. reinterpret_cast<const unsigned char *>(head + offsets[y_idx]);
  4438. int line_no;
  4439. memcpy(&line_no, data_ptr, sizeof(int));
  4440. int data_len;
  4441. memcpy(&data_len, data_ptr + 4, sizeof(int));
  4442. tinyexr::swap4(&line_no);
  4443. tinyexr::swap4(&data_len);
  4444. if (size_t(data_len) > data_size) {
  4445. invalid_data = true;
  4446. } else if ((line_no > (2 << 20)) || (line_no < -(2 << 20))) {
  4447. // Too large value. Assume this is invalid
  4448. // 2**20 = 1048576 = heuristic value.
  4449. invalid_data = true;
  4450. } else if (data_len == 0) {
  4451. // TODO(syoyo): May be ok to raise the threshold for example
  4452. // `data_len < 4`
  4453. invalid_data = true;
  4454. } else {
  4455. // line_no may be negative.
  4456. int end_line_no = (std::min)(line_no + num_scanline_blocks,
  4457. (exr_header->data_window.max_y + 1));
  4458. int num_lines = end_line_no - line_no;
  4459. if (num_lines <= 0) {
  4460. invalid_data = true;
  4461. } else {
  4462. // Move to data addr: 8 = 4 + 4;
  4463. data_ptr += 8;
  4464. // Adjust line_no with data_window.bmin.y
  4465. // overflow check
  4466. tinyexr_int64 lno =
  4467. static_cast<tinyexr_int64>(line_no) -
  4468. static_cast<tinyexr_int64>(exr_header->data_window.min_y);
  4469. if (lno > std::numeric_limits<int>::max()) {
  4470. line_no = -1; // invalid
  4471. } else if (lno < -std::numeric_limits<int>::max()) {
  4472. line_no = -1; // invalid
  4473. } else {
  4474. line_no -= exr_header->data_window.min_y;
  4475. }
  4476. if (line_no < 0) {
  4477. invalid_data = true;
  4478. } else {
  4479. if (!tinyexr::DecodePixelData(
  4480. exr_image->images, exr_header->requested_pixel_types,
  4481. data_ptr, static_cast<size_t>(data_len),
  4482. exr_header->compression_type, exr_header->line_order,
  4483. int(data_width), int(data_height), int(data_width), y, line_no,
  4484. num_lines, static_cast<size_t>(pixel_data_size),
  4485. static_cast<size_t>(
  4486. exr_header->num_custom_attributes),
  4487. exr_header->custom_attributes,
  4488. static_cast<size_t>(exr_header->num_channels),
  4489. exr_header->channels, channel_offset_list)) {
  4490. invalid_data = true;
  4491. }
  4492. }
  4493. }
  4494. }
  4495. }
  4496. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  4497. }
  4498. }));
  4499. }
  4500. for (auto &t : workers) {
  4501. t.join();
  4502. }
  4503. #else
  4504. } // omp parallel
  4505. #endif
  4506. }
  4507. if (invalid_data) {
  4508. if (err) {
  4509. (*err) += "Invalid/Corrupted data found when decoding pixels.\n";
  4510. }
  4511. // free alloced image.
  4512. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  4513. if (exr_image->images[c]) {
  4514. free(exr_image->images[c]);
  4515. exr_image->images[c] = NULL;
  4516. }
  4517. }
  4518. return TINYEXR_ERROR_INVALID_DATA;
  4519. }
  4520. // Overwrite `pixel_type` with `requested_pixel_type`.
  4521. {
  4522. for (int c = 0; c < exr_header->num_channels; c++) {
  4523. exr_header->pixel_types[c] = exr_header->requested_pixel_types[c];
  4524. }
  4525. }
  4526. {
  4527. exr_image->num_channels = num_channels;
  4528. exr_image->width = int(data_width);
  4529. exr_image->height = int(data_height);
  4530. }
  4531. return TINYEXR_SUCCESS;
  4532. }
  4533. static bool ReconstructLineOffsets(
  4534. std::vector<tinyexr::tinyexr_uint64> *offsets, size_t n,
  4535. const unsigned char *head, const unsigned char *marker, const size_t size) {
  4536. if (head >= marker) {
  4537. return false;
  4538. }
  4539. if (offsets->size() != n) {
  4540. return false;
  4541. }
  4542. for (size_t i = 0; i < n; i++) {
  4543. size_t offset = static_cast<size_t>(marker - head);
  4544. // Offset should not exceed whole EXR file/data size.
  4545. if ((offset + sizeof(tinyexr::tinyexr_uint64)) >= size) {
  4546. return false;
  4547. }
  4548. int y;
  4549. unsigned int data_len;
  4550. memcpy(&y, marker, sizeof(int));
  4551. memcpy(&data_len, marker + 4, sizeof(unsigned int));
  4552. if (data_len >= size) {
  4553. return false;
  4554. }
  4555. tinyexr::swap4(&y);
  4556. tinyexr::swap4(&data_len);
  4557. (*offsets)[i] = offset;
  4558. marker += data_len + 8; // 8 = 4 bytes(y) + 4 bytes(data_len)
  4559. }
  4560. return true;
  4561. }
  4562. static int FloorLog2(unsigned x) {
  4563. //
  4564. // For x > 0, floorLog2(y) returns floor(log(x)/log(2)).
  4565. //
  4566. int y = 0;
  4567. while (x > 1) {
  4568. y += 1;
  4569. x >>= 1u;
  4570. }
  4571. return y;
  4572. }
  4573. static int CeilLog2(unsigned x) {
  4574. //
  4575. // For x > 0, ceilLog2(y) returns ceil(log(x)/log(2)).
  4576. //
  4577. int y = 0;
  4578. int r = 0;
  4579. while (x > 1) {
  4580. if (x & 1)
  4581. r = 1;
  4582. y += 1;
  4583. x >>= 1u;
  4584. }
  4585. return y + r;
  4586. }
  4587. static int RoundLog2(int x, int tile_rounding_mode) {
  4588. return (tile_rounding_mode == TINYEXR_TILE_ROUND_DOWN) ? FloorLog2(static_cast<unsigned>(x)) : CeilLog2(static_cast<unsigned>(x));
  4589. }
  4590. static int CalculateNumXLevels(const EXRHeader* exr_header) {
  4591. int min_x = exr_header->data_window.min_x;
  4592. int max_x = exr_header->data_window.max_x;
  4593. int min_y = exr_header->data_window.min_y;
  4594. int max_y = exr_header->data_window.max_y;
  4595. int num = 0;
  4596. switch (exr_header->tile_level_mode) {
  4597. case TINYEXR_TILE_ONE_LEVEL:
  4598. num = 1;
  4599. break;
  4600. case TINYEXR_TILE_MIPMAP_LEVELS:
  4601. {
  4602. int w = max_x - min_x + 1;
  4603. int h = max_y - min_y + 1;
  4604. num = RoundLog2(std::max(w, h), exr_header->tile_rounding_mode) + 1;
  4605. }
  4606. break;
  4607. case TINYEXR_TILE_RIPMAP_LEVELS:
  4608. {
  4609. int w = max_x - min_x + 1;
  4610. num = RoundLog2(w, exr_header->tile_rounding_mode) + 1;
  4611. }
  4612. break;
  4613. default:
  4614. return -1;
  4615. }
  4616. return num;
  4617. }
  4618. static int CalculateNumYLevels(const EXRHeader* exr_header) {
  4619. int min_x = exr_header->data_window.min_x;
  4620. int max_x = exr_header->data_window.max_x;
  4621. int min_y = exr_header->data_window.min_y;
  4622. int max_y = exr_header->data_window.max_y;
  4623. int num = 0;
  4624. switch (exr_header->tile_level_mode) {
  4625. case TINYEXR_TILE_ONE_LEVEL:
  4626. num = 1;
  4627. break;
  4628. case TINYEXR_TILE_MIPMAP_LEVELS:
  4629. {
  4630. int w = max_x - min_x + 1;
  4631. int h = max_y - min_y + 1;
  4632. num = RoundLog2(std::max(w, h), exr_header->tile_rounding_mode) + 1;
  4633. }
  4634. break;
  4635. case TINYEXR_TILE_RIPMAP_LEVELS:
  4636. {
  4637. int h = max_y - min_y + 1;
  4638. num = RoundLog2(h, exr_header->tile_rounding_mode) + 1;
  4639. }
  4640. break;
  4641. default:
  4642. return -1;
  4643. }
  4644. return num;
  4645. }
  4646. static bool CalculateNumTiles(std::vector<int>& numTiles,
  4647. int toplevel_size,
  4648. int size,
  4649. int tile_rounding_mode) {
  4650. for (unsigned i = 0; i < numTiles.size(); i++) {
  4651. int l = LevelSize(toplevel_size, int(i), tile_rounding_mode);
  4652. if (l < 0) {
  4653. return false;
  4654. }
  4655. TINYEXR_CHECK_AND_RETURN_C(l <= std::numeric_limits<int>::max() - size + 1, false);
  4656. numTiles[i] = (l + size - 1) / size;
  4657. }
  4658. return true;
  4659. }
  4660. static bool PrecalculateTileInfo(std::vector<int>& num_x_tiles,
  4661. std::vector<int>& num_y_tiles,
  4662. const EXRHeader* exr_header) {
  4663. int min_x = exr_header->data_window.min_x;
  4664. int max_x = exr_header->data_window.max_x;
  4665. int min_y = exr_header->data_window.min_y;
  4666. int max_y = exr_header->data_window.max_y;
  4667. int num_x_levels = CalculateNumXLevels(exr_header);
  4668. if (num_x_levels < 0) {
  4669. return false;
  4670. }
  4671. int num_y_levels = CalculateNumYLevels(exr_header);
  4672. if (num_y_levels < 0) {
  4673. return false;
  4674. }
  4675. num_x_tiles.resize(size_t(num_x_levels));
  4676. num_y_tiles.resize(size_t(num_y_levels));
  4677. if (!CalculateNumTiles(num_x_tiles,
  4678. max_x - min_x + 1,
  4679. exr_header->tile_size_x,
  4680. exr_header->tile_rounding_mode)) {
  4681. return false;
  4682. }
  4683. if (!CalculateNumTiles(num_y_tiles,
  4684. max_y - min_y + 1,
  4685. exr_header->tile_size_y,
  4686. exr_header->tile_rounding_mode)) {
  4687. return false;
  4688. }
  4689. return true;
  4690. }
  4691. static void InitSingleResolutionOffsets(OffsetData& offset_data, size_t num_blocks) {
  4692. offset_data.offsets.resize(1);
  4693. offset_data.offsets[0].resize(1);
  4694. offset_data.offsets[0][0].resize(num_blocks);
  4695. offset_data.num_x_levels = 1;
  4696. offset_data.num_y_levels = 1;
  4697. }
  4698. // Return sum of tile blocks.
  4699. // 0 = error
  4700. static int InitTileOffsets(OffsetData& offset_data,
  4701. const EXRHeader* exr_header,
  4702. const std::vector<int>& num_x_tiles,
  4703. const std::vector<int>& num_y_tiles) {
  4704. int num_tile_blocks = 0;
  4705. offset_data.num_x_levels = static_cast<int>(num_x_tiles.size());
  4706. offset_data.num_y_levels = static_cast<int>(num_y_tiles.size());
  4707. switch (exr_header->tile_level_mode) {
  4708. case TINYEXR_TILE_ONE_LEVEL:
  4709. case TINYEXR_TILE_MIPMAP_LEVELS:
  4710. TINYEXR_CHECK_AND_RETURN_C(offset_data.num_x_levels == offset_data.num_y_levels, 0);
  4711. offset_data.offsets.resize(size_t(offset_data.num_x_levels));
  4712. for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) {
  4713. offset_data.offsets[l].resize(size_t(num_y_tiles[l]));
  4714. for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) {
  4715. offset_data.offsets[l][dy].resize(size_t(num_x_tiles[l]));
  4716. num_tile_blocks += num_x_tiles[l];
  4717. }
  4718. }
  4719. break;
  4720. case TINYEXR_TILE_RIPMAP_LEVELS:
  4721. offset_data.offsets.resize(static_cast<size_t>(offset_data.num_x_levels) * static_cast<size_t>(offset_data.num_y_levels));
  4722. for (int ly = 0; ly < offset_data.num_y_levels; ++ly) {
  4723. for (int lx = 0; lx < offset_data.num_x_levels; ++lx) {
  4724. int l = ly * offset_data.num_x_levels + lx;
  4725. offset_data.offsets[size_t(l)].resize(size_t(num_y_tiles[size_t(ly)]));
  4726. for (size_t dy = 0; dy < offset_data.offsets[size_t(l)].size(); ++dy) {
  4727. offset_data.offsets[size_t(l)][dy].resize(size_t(num_x_tiles[size_t(lx)]));
  4728. num_tile_blocks += num_x_tiles[size_t(lx)];
  4729. }
  4730. }
  4731. }
  4732. break;
  4733. default:
  4734. return 0;
  4735. }
  4736. return num_tile_blocks;
  4737. }
  4738. static bool IsAnyOffsetsAreInvalid(const OffsetData& offset_data) {
  4739. for (unsigned int l = 0; l < offset_data.offsets.size(); ++l)
  4740. for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy)
  4741. for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx)
  4742. if (reinterpret_cast<const tinyexr::tinyexr_int64&>(offset_data.offsets[l][dy][dx]) <= 0)
  4743. return true;
  4744. return false;
  4745. }
  4746. static bool isValidTile(const EXRHeader* exr_header,
  4747. const OffsetData& offset_data,
  4748. int dx, int dy, int lx, int ly) {
  4749. if (lx < 0 || ly < 0 || dx < 0 || dy < 0) return false;
  4750. int num_x_levels = offset_data.num_x_levels;
  4751. int num_y_levels = offset_data.num_y_levels;
  4752. switch (exr_header->tile_level_mode) {
  4753. case TINYEXR_TILE_ONE_LEVEL:
  4754. if (lx == 0 &&
  4755. ly == 0 &&
  4756. offset_data.offsets.size() > 0 &&
  4757. offset_data.offsets[0].size() > static_cast<size_t>(dy) &&
  4758. offset_data.offsets[0][size_t(dy)].size() > static_cast<size_t>(dx)) {
  4759. return true;
  4760. }
  4761. break;
  4762. case TINYEXR_TILE_MIPMAP_LEVELS:
  4763. if (lx < num_x_levels &&
  4764. ly < num_y_levels &&
  4765. offset_data.offsets.size() > static_cast<size_t>(lx) &&
  4766. offset_data.offsets[size_t(lx)].size() > static_cast<size_t>(dy) &&
  4767. offset_data.offsets[size_t(lx)][size_t(dy)].size() > static_cast<size_t>(dx)) {
  4768. return true;
  4769. }
  4770. break;
  4771. case TINYEXR_TILE_RIPMAP_LEVELS:
  4772. {
  4773. size_t idx = static_cast<size_t>(lx) + static_cast<size_t>(ly)* static_cast<size_t>(num_x_levels);
  4774. if (lx < num_x_levels &&
  4775. ly < num_y_levels &&
  4776. (offset_data.offsets.size() > idx) &&
  4777. offset_data.offsets[idx].size() > static_cast<size_t>(dy) &&
  4778. offset_data.offsets[idx][size_t(dy)].size() > static_cast<size_t>(dx)) {
  4779. return true;
  4780. }
  4781. }
  4782. break;
  4783. default:
  4784. return false;
  4785. }
  4786. return false;
  4787. }
  4788. static bool ReconstructTileOffsets(OffsetData& offset_data,
  4789. const EXRHeader* exr_header,
  4790. const unsigned char* head, const unsigned char* marker, const size_t /*size*/,
  4791. bool isMultiPartFile,
  4792. bool isDeep) {
  4793. int numXLevels = offset_data.num_x_levels;
  4794. for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) {
  4795. for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) {
  4796. for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) {
  4797. tinyexr::tinyexr_uint64 tileOffset = tinyexr::tinyexr_uint64(marker - head);
  4798. if (isMultiPartFile) {
  4799. //int partNumber;
  4800. marker += sizeof(int);
  4801. }
  4802. int tileX;
  4803. memcpy(&tileX, marker, sizeof(int));
  4804. tinyexr::swap4(&tileX);
  4805. marker += sizeof(int);
  4806. int tileY;
  4807. memcpy(&tileY, marker, sizeof(int));
  4808. tinyexr::swap4(&tileY);
  4809. marker += sizeof(int);
  4810. int levelX;
  4811. memcpy(&levelX, marker, sizeof(int));
  4812. tinyexr::swap4(&levelX);
  4813. marker += sizeof(int);
  4814. int levelY;
  4815. memcpy(&levelY, marker, sizeof(int));
  4816. tinyexr::swap4(&levelY);
  4817. marker += sizeof(int);
  4818. if (isDeep) {
  4819. tinyexr::tinyexr_int64 packed_offset_table_size;
  4820. memcpy(&packed_offset_table_size, marker, sizeof(tinyexr::tinyexr_int64));
  4821. tinyexr::swap8(reinterpret_cast<tinyexr::tinyexr_uint64*>(&packed_offset_table_size));
  4822. marker += sizeof(tinyexr::tinyexr_int64);
  4823. tinyexr::tinyexr_int64 packed_sample_size;
  4824. memcpy(&packed_sample_size, marker, sizeof(tinyexr::tinyexr_int64));
  4825. tinyexr::swap8(reinterpret_cast<tinyexr::tinyexr_uint64*>(&packed_sample_size));
  4826. marker += sizeof(tinyexr::tinyexr_int64);
  4827. // next Int64 is unpacked sample size - skip that too
  4828. marker += packed_offset_table_size + packed_sample_size + 8;
  4829. } else {
  4830. int dataSize;
  4831. memcpy(&dataSize, marker, sizeof(int));
  4832. tinyexr::swap4(&dataSize);
  4833. marker += sizeof(int);
  4834. marker += dataSize;
  4835. }
  4836. if (!isValidTile(exr_header, offset_data,
  4837. tileX, tileY, levelX, levelY)) {
  4838. return false;
  4839. }
  4840. int level_idx = LevelIndex(levelX, levelY, exr_header->tile_level_mode, numXLevels);
  4841. if (level_idx < 0) {
  4842. return false;
  4843. }
  4844. offset_data.offsets[size_t(level_idx)][size_t(tileY)][size_t(tileX)] = tileOffset;
  4845. }
  4846. }
  4847. }
  4848. return true;
  4849. }
  4850. // marker output is also
  4851. static int ReadOffsets(OffsetData& offset_data,
  4852. const unsigned char* head,
  4853. const unsigned char*& marker,
  4854. const size_t size,
  4855. const char** err) {
  4856. for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) {
  4857. for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) {
  4858. for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) {
  4859. tinyexr::tinyexr_uint64 offset;
  4860. if ((marker + sizeof(tinyexr_uint64)) >= (head + size)) {
  4861. tinyexr::SetErrorMessage("Insufficient data size in offset table.", err);
  4862. return TINYEXR_ERROR_INVALID_DATA;
  4863. }
  4864. memcpy(&offset, marker, sizeof(tinyexr::tinyexr_uint64));
  4865. tinyexr::swap8(&offset);
  4866. if (offset >= size) {
  4867. tinyexr::SetErrorMessage("Invalid offset value in DecodeEXRImage.", err);
  4868. return TINYEXR_ERROR_INVALID_DATA;
  4869. }
  4870. marker += sizeof(tinyexr::tinyexr_uint64); // = 8
  4871. offset_data.offsets[l][dy][dx] = offset;
  4872. }
  4873. }
  4874. }
  4875. return TINYEXR_SUCCESS;
  4876. }
  4877. static int DecodeEXRImage(EXRImage *exr_image, const EXRHeader *exr_header,
  4878. const unsigned char *head,
  4879. const unsigned char *marker, const size_t size,
  4880. const char **err) {
  4881. if (exr_image == NULL || exr_header == NULL || head == NULL ||
  4882. marker == NULL || (size <= tinyexr::kEXRVersionSize)) {
  4883. tinyexr::SetErrorMessage("Invalid argument for DecodeEXRImage().", err);
  4884. return TINYEXR_ERROR_INVALID_ARGUMENT;
  4885. }
  4886. int num_scanline_blocks = 1;
  4887. if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZIP) {
  4888. num_scanline_blocks = 16;
  4889. } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) {
  4890. num_scanline_blocks = 32;
  4891. } else if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) {
  4892. num_scanline_blocks = 16;
  4893. }
  4894. if (exr_header->data_window.max_x < exr_header->data_window.min_x ||
  4895. exr_header->data_window.max_x - exr_header->data_window.min_x ==
  4896. std::numeric_limits<int>::max()) {
  4897. // Issue 63
  4898. tinyexr::SetErrorMessage("Invalid data width value", err);
  4899. return TINYEXR_ERROR_INVALID_DATA;
  4900. }
  4901. tinyexr_int64 data_width =
  4902. static_cast<tinyexr_int64>(exr_header->data_window.max_x) - static_cast<tinyexr_int64>(exr_header->data_window.min_x) + static_cast<tinyexr_int64>(1);
  4903. if (data_width <= 0) {
  4904. tinyexr::SetErrorMessage("Invalid data window width value", err);
  4905. return TINYEXR_ERROR_INVALID_DATA;
  4906. }
  4907. if (exr_header->data_window.max_y < exr_header->data_window.min_y ||
  4908. exr_header->data_window.max_y - exr_header->data_window.min_y ==
  4909. std::numeric_limits<int>::max()) {
  4910. tinyexr::SetErrorMessage("Invalid data height value", err);
  4911. return TINYEXR_ERROR_INVALID_DATA;
  4912. }
  4913. tinyexr_int64 data_height =
  4914. static_cast<tinyexr_int64>(exr_header->data_window.max_y) - static_cast<tinyexr_int64>(exr_header->data_window.min_y) + static_cast<tinyexr_int64>(1);
  4915. if (data_height <= 0) {
  4916. tinyexr::SetErrorMessage("Invalid data window height value", err);
  4917. return TINYEXR_ERROR_INVALID_DATA;
  4918. }
  4919. // Do not allow too large data_width and data_height. header invalid?
  4920. {
  4921. if (data_width > TINYEXR_DIMENSION_THRESHOLD) {
  4922. tinyexr::SetErrorMessage("data width too large.", err);
  4923. return TINYEXR_ERROR_INVALID_DATA;
  4924. }
  4925. if (data_height > TINYEXR_DIMENSION_THRESHOLD) {
  4926. tinyexr::SetErrorMessage("data height too large.", err);
  4927. return TINYEXR_ERROR_INVALID_DATA;
  4928. }
  4929. }
  4930. if (exr_header->tiled) {
  4931. if (exr_header->tile_size_x > TINYEXR_DIMENSION_THRESHOLD) {
  4932. tinyexr::SetErrorMessage("tile width too large.", err);
  4933. return TINYEXR_ERROR_INVALID_DATA;
  4934. }
  4935. if (exr_header->tile_size_y > TINYEXR_DIMENSION_THRESHOLD) {
  4936. tinyexr::SetErrorMessage("tile height too large.", err);
  4937. return TINYEXR_ERROR_INVALID_DATA;
  4938. }
  4939. }
  4940. // Read offset tables.
  4941. OffsetData offset_data;
  4942. size_t num_blocks = 0;
  4943. // For a multi-resolution image, the size of the offset table will be calculated from the other attributes of the header.
  4944. // If chunk_count > 0 then chunk_count must be equal to the calculated tile count.
  4945. if (exr_header->tiled) {
  4946. {
  4947. std::vector<int> num_x_tiles, num_y_tiles;
  4948. if (!PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_header)) {
  4949. tinyexr::SetErrorMessage("Failed to precalculate tile info.", err);
  4950. return TINYEXR_ERROR_INVALID_DATA;
  4951. }
  4952. num_blocks = size_t(InitTileOffsets(offset_data, exr_header, num_x_tiles, num_y_tiles));
  4953. if (exr_header->chunk_count > 0) {
  4954. if (exr_header->chunk_count != static_cast<int>(num_blocks)) {
  4955. tinyexr::SetErrorMessage("Invalid offset table size.", err);
  4956. return TINYEXR_ERROR_INVALID_DATA;
  4957. }
  4958. }
  4959. }
  4960. int ret = ReadOffsets(offset_data, head, marker, size, err);
  4961. if (ret != TINYEXR_SUCCESS) return ret;
  4962. if (IsAnyOffsetsAreInvalid(offset_data)) {
  4963. if (!ReconstructTileOffsets(offset_data, exr_header,
  4964. head, marker, size,
  4965. exr_header->multipart, exr_header->non_image)) {
  4966. tinyexr::SetErrorMessage("Invalid Tile Offsets data.", err);
  4967. return TINYEXR_ERROR_INVALID_DATA;
  4968. }
  4969. }
  4970. } else if (exr_header->chunk_count > 0) {
  4971. // Use `chunkCount` attribute.
  4972. num_blocks = static_cast<size_t>(exr_header->chunk_count);
  4973. InitSingleResolutionOffsets(offset_data, num_blocks);
  4974. } else {
  4975. num_blocks = static_cast<size_t>(data_height) /
  4976. static_cast<size_t>(num_scanline_blocks);
  4977. if (num_blocks * static_cast<size_t>(num_scanline_blocks) <
  4978. static_cast<size_t>(data_height)) {
  4979. num_blocks++;
  4980. }
  4981. InitSingleResolutionOffsets(offset_data, num_blocks);
  4982. }
  4983. if (!exr_header->tiled) {
  4984. std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data.offsets[0][0];
  4985. for (size_t y = 0; y < num_blocks; y++) {
  4986. tinyexr::tinyexr_uint64 offset;
  4987. // Issue #81
  4988. if ((marker + sizeof(tinyexr_uint64)) >= (head + size)) {
  4989. tinyexr::SetErrorMessage("Insufficient data size in offset table.", err);
  4990. return TINYEXR_ERROR_INVALID_DATA;
  4991. }
  4992. memcpy(&offset, marker, sizeof(tinyexr::tinyexr_uint64));
  4993. tinyexr::swap8(&offset);
  4994. if (offset >= size) {
  4995. tinyexr::SetErrorMessage("Invalid offset value in DecodeEXRImage.", err);
  4996. return TINYEXR_ERROR_INVALID_DATA;
  4997. }
  4998. marker += sizeof(tinyexr::tinyexr_uint64); // = 8
  4999. offsets[y] = offset;
  5000. }
  5001. // If line offsets are invalid, we try to reconstruct it.
  5002. // See OpenEXR/IlmImf/ImfScanLineInputFile.cpp::readLineOffsets() for details.
  5003. for (size_t y = 0; y < num_blocks; y++) {
  5004. if (offsets[y] <= 0) {
  5005. // TODO(syoyo) Report as warning?
  5006. // if (err) {
  5007. // stringstream ss;
  5008. // ss << "Incomplete lineOffsets." << std::endl;
  5009. // (*err) += ss.str();
  5010. //}
  5011. bool ret =
  5012. ReconstructLineOffsets(&offsets, num_blocks, head, marker, size);
  5013. if (ret) {
  5014. // OK
  5015. break;
  5016. } else {
  5017. tinyexr::SetErrorMessage(
  5018. "Cannot reconstruct lineOffset table in DecodeEXRImage.", err);
  5019. return TINYEXR_ERROR_INVALID_DATA;
  5020. }
  5021. }
  5022. }
  5023. }
  5024. {
  5025. std::string e;
  5026. int ret = DecodeChunk(exr_image, exr_header, offset_data, head, size, &e);
  5027. if (ret != TINYEXR_SUCCESS) {
  5028. if (!e.empty()) {
  5029. tinyexr::SetErrorMessage(e, err);
  5030. }
  5031. #if 1
  5032. FreeEXRImage(exr_image);
  5033. #else
  5034. // release memory(if exists)
  5035. if ((exr_header->num_channels > 0) && exr_image && exr_image->images) {
  5036. for (size_t c = 0; c < size_t(exr_header->num_channels); c++) {
  5037. if (exr_image->images[c]) {
  5038. free(exr_image->images[c]);
  5039. exr_image->images[c] = NULL;
  5040. }
  5041. }
  5042. free(exr_image->images);
  5043. exr_image->images = NULL;
  5044. }
  5045. #endif
  5046. }
  5047. return ret;
  5048. }
  5049. }
  5050. static void GetLayers(const EXRHeader &exr_header,
  5051. std::vector<std::string> &layer_names) {
  5052. // Naive implementation
  5053. // Group channels by layers
  5054. // go over all channel names, split by periods
  5055. // collect unique names
  5056. layer_names.clear();
  5057. for (int c = 0; c < exr_header.num_channels; c++) {
  5058. std::string full_name(exr_header.channels[c].name);
  5059. const size_t pos = full_name.find_last_of('.');
  5060. if (pos != std::string::npos && pos != 0 && pos + 1 < full_name.size()) {
  5061. full_name.erase(pos);
  5062. if (std::find(layer_names.begin(), layer_names.end(), full_name) ==
  5063. layer_names.end())
  5064. layer_names.push_back(full_name);
  5065. }
  5066. }
  5067. }
  5068. struct LayerChannel {
  5069. explicit LayerChannel(size_t i, std::string n) : index(i), name(n) {}
  5070. size_t index;
  5071. std::string name;
  5072. };
  5073. static void ChannelsInLayer(const EXRHeader &exr_header,
  5074. const std::string &layer_name,
  5075. std::vector<LayerChannel> &channels) {
  5076. channels.clear();
  5077. //std::cout << "layer_name = " << layer_name << "\n";
  5078. for (int c = 0; c < exr_header.num_channels; c++) {
  5079. //std::cout << "chan[" << c << "] = " << exr_header.channels[c].name << "\n";
  5080. std::string ch_name(exr_header.channels[c].name);
  5081. if (layer_name.empty()) {
  5082. const size_t pos = ch_name.find_last_of('.');
  5083. if (pos != std::string::npos && pos < ch_name.size()) {
  5084. if (pos != 0) continue;
  5085. ch_name = ch_name.substr(pos + 1);
  5086. }
  5087. } else {
  5088. const size_t pos = ch_name.find(layer_name + '.');
  5089. if (pos == std::string::npos) continue;
  5090. if (pos == 0) {
  5091. ch_name = ch_name.substr(layer_name.size() + 1);
  5092. }
  5093. }
  5094. LayerChannel ch(size_t(c), ch_name);
  5095. channels.push_back(ch);
  5096. }
  5097. }
  5098. } // namespace tinyexr
  5099. int EXRLayers(const char *filename, const char **layer_names[], int *num_layers,
  5100. const char **err) {
  5101. EXRVersion exr_version;
  5102. EXRHeader exr_header;
  5103. InitEXRHeader(&exr_header);
  5104. {
  5105. int ret = ParseEXRVersionFromFile(&exr_version, filename);
  5106. if (ret != TINYEXR_SUCCESS) {
  5107. tinyexr::SetErrorMessage("Invalid EXR header.", err);
  5108. return ret;
  5109. }
  5110. if (exr_version.multipart || exr_version.non_image) {
  5111. tinyexr::SetErrorMessage(
  5112. "Loading multipart or DeepImage is not supported in LoadEXR() API",
  5113. err);
  5114. return TINYEXR_ERROR_INVALID_DATA; // @fixme.
  5115. }
  5116. }
  5117. int ret = ParseEXRHeaderFromFile(&exr_header, &exr_version, filename, err);
  5118. if (ret != TINYEXR_SUCCESS) {
  5119. FreeEXRHeader(&exr_header);
  5120. return ret;
  5121. }
  5122. std::vector<std::string> layer_vec;
  5123. tinyexr::GetLayers(exr_header, layer_vec);
  5124. (*num_layers) = int(layer_vec.size());
  5125. (*layer_names) = static_cast<const char **>(
  5126. malloc(sizeof(const char *) * static_cast<size_t>(layer_vec.size())));
  5127. for (size_t c = 0; c < static_cast<size_t>(layer_vec.size()); c++) {
  5128. #ifdef _MSC_VER
  5129. (*layer_names)[c] = _strdup(layer_vec[c].c_str());
  5130. #else
  5131. (*layer_names)[c] = strdup(layer_vec[c].c_str());
  5132. #endif
  5133. }
  5134. FreeEXRHeader(&exr_header);
  5135. return TINYEXR_SUCCESS;
  5136. }
  5137. int LoadEXR(float **out_rgba, int *width, int *height, const char *filename,
  5138. const char **err) {
  5139. return LoadEXRWithLayer(out_rgba, width, height, filename,
  5140. /* layername */ NULL, err);
  5141. }
  5142. int LoadEXRWithLayer(float **out_rgba, int *width, int *height,
  5143. const char *filename, const char *layername,
  5144. const char **err) {
  5145. if (out_rgba == NULL) {
  5146. tinyexr::SetErrorMessage("Invalid argument for LoadEXR()", err);
  5147. return TINYEXR_ERROR_INVALID_ARGUMENT;
  5148. }
  5149. EXRVersion exr_version;
  5150. EXRImage exr_image;
  5151. EXRHeader exr_header;
  5152. InitEXRHeader(&exr_header);
  5153. InitEXRImage(&exr_image);
  5154. {
  5155. int ret = ParseEXRVersionFromFile(&exr_version, filename);
  5156. if (ret != TINYEXR_SUCCESS) {
  5157. std::stringstream ss;
  5158. ss << "Failed to open EXR file or read version info from EXR file. code("
  5159. << ret << ")";
  5160. tinyexr::SetErrorMessage(ss.str(), err);
  5161. return ret;
  5162. }
  5163. if (exr_version.multipart || exr_version.non_image) {
  5164. tinyexr::SetErrorMessage(
  5165. "Loading multipart or DeepImage is not supported in LoadEXR() API",
  5166. err);
  5167. return TINYEXR_ERROR_INVALID_DATA; // @fixme.
  5168. }
  5169. }
  5170. {
  5171. int ret = ParseEXRHeaderFromFile(&exr_header, &exr_version, filename, err);
  5172. if (ret != TINYEXR_SUCCESS) {
  5173. FreeEXRHeader(&exr_header);
  5174. return ret;
  5175. }
  5176. }
  5177. // Read HALF channel as FLOAT.
  5178. for (int i = 0; i < exr_header.num_channels; i++) {
  5179. if (exr_header.pixel_types[i] == TINYEXR_PIXELTYPE_HALF) {
  5180. exr_header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
  5181. }
  5182. }
  5183. // TODO: Probably limit loading to layers (channels) selected by layer index
  5184. {
  5185. int ret = LoadEXRImageFromFile(&exr_image, &exr_header, filename, err);
  5186. if (ret != TINYEXR_SUCCESS) {
  5187. FreeEXRHeader(&exr_header);
  5188. return ret;
  5189. }
  5190. }
  5191. // RGBA
  5192. int idxR = -1;
  5193. int idxG = -1;
  5194. int idxB = -1;
  5195. int idxA = -1;
  5196. std::vector<std::string> layer_names;
  5197. tinyexr::GetLayers(exr_header, layer_names);
  5198. std::vector<tinyexr::LayerChannel> channels;
  5199. tinyexr::ChannelsInLayer(
  5200. exr_header, layername == NULL ? "" : std::string(layername), channels);
  5201. if (channels.size() < 1) {
  5202. if (layername == NULL) {
  5203. tinyexr::SetErrorMessage("Layer Not Found. Seems EXR contains channels with layer(e.g. `diffuse.R`). if you are using LoadEXR(), please try LoadEXRWithLayer(). LoadEXR() cannot load EXR having channels with layer.", err);
  5204. } else {
  5205. tinyexr::SetErrorMessage("Layer Not Found", err);
  5206. }
  5207. FreeEXRHeader(&exr_header);
  5208. FreeEXRImage(&exr_image);
  5209. return TINYEXR_ERROR_LAYER_NOT_FOUND;
  5210. }
  5211. size_t ch_count = channels.size() < 4 ? channels.size() : 4;
  5212. for (size_t c = 0; c < ch_count; c++) {
  5213. const tinyexr::LayerChannel &ch = channels[c];
  5214. if (ch.name == "R") {
  5215. idxR = int(ch.index);
  5216. } else if (ch.name == "G") {
  5217. idxG = int(ch.index);
  5218. } else if (ch.name == "B") {
  5219. idxB = int(ch.index);
  5220. } else if (ch.name == "A") {
  5221. idxA = int(ch.index);
  5222. }
  5223. }
  5224. if (channels.size() == 1) {
  5225. int chIdx = int(channels.front().index);
  5226. // Grayscale channel only.
  5227. (*out_rgba) = reinterpret_cast<float *>(
  5228. malloc(4 * sizeof(float) * static_cast<size_t>(exr_image.width) *
  5229. static_cast<size_t>(exr_image.height)));
  5230. if (exr_header.tiled) {
  5231. const size_t tile_size_x = static_cast<size_t>(exr_header.tile_size_x);
  5232. const size_t tile_size_y = static_cast<size_t>(exr_header.tile_size_y);
  5233. for (int it = 0; it < exr_image.num_tiles; it++) {
  5234. for (size_t j = 0; j < tile_size_y; j++) {
  5235. for (size_t i = 0; i < tile_size_x; i++) {
  5236. const size_t ii =
  5237. static_cast<size_t>(exr_image.tiles[it].offset_x) * tile_size_x +
  5238. i;
  5239. const size_t jj =
  5240. static_cast<size_t>(exr_image.tiles[it].offset_y) * tile_size_y +
  5241. j;
  5242. const size_t idx = ii + jj * static_cast<size_t>(exr_image.width);
  5243. // out of region check.
  5244. if (ii >= static_cast<size_t>(exr_image.width)) {
  5245. continue;
  5246. }
  5247. if (jj >= static_cast<size_t>(exr_image.height)) {
  5248. continue;
  5249. }
  5250. const size_t srcIdx = i + j * tile_size_x;
  5251. unsigned char **src = exr_image.tiles[it].images;
  5252. (*out_rgba)[4 * idx + 0] =
  5253. reinterpret_cast<float **>(src)[chIdx][srcIdx];
  5254. (*out_rgba)[4 * idx + 1] =
  5255. reinterpret_cast<float **>(src)[chIdx][srcIdx];
  5256. (*out_rgba)[4 * idx + 2] =
  5257. reinterpret_cast<float **>(src)[chIdx][srcIdx];
  5258. (*out_rgba)[4 * idx + 3] =
  5259. reinterpret_cast<float **>(src)[chIdx][srcIdx];
  5260. }
  5261. }
  5262. }
  5263. } else {
  5264. const size_t pixel_size = static_cast<size_t>(exr_image.width) *
  5265. static_cast<size_t>(exr_image.height);
  5266. for (size_t i = 0; i < pixel_size; i++) {
  5267. const float val =
  5268. reinterpret_cast<float **>(exr_image.images)[chIdx][i];
  5269. (*out_rgba)[4 * i + 0] = val;
  5270. (*out_rgba)[4 * i + 1] = val;
  5271. (*out_rgba)[4 * i + 2] = val;
  5272. (*out_rgba)[4 * i + 3] = val;
  5273. }
  5274. }
  5275. } else {
  5276. // Assume RGB(A)
  5277. if (idxR == -1) {
  5278. tinyexr::SetErrorMessage("R channel not found", err);
  5279. FreeEXRHeader(&exr_header);
  5280. FreeEXRImage(&exr_image);
  5281. return TINYEXR_ERROR_INVALID_DATA;
  5282. }
  5283. if (idxG == -1) {
  5284. tinyexr::SetErrorMessage("G channel not found", err);
  5285. FreeEXRHeader(&exr_header);
  5286. FreeEXRImage(&exr_image);
  5287. return TINYEXR_ERROR_INVALID_DATA;
  5288. }
  5289. if (idxB == -1) {
  5290. tinyexr::SetErrorMessage("B channel not found", err);
  5291. FreeEXRHeader(&exr_header);
  5292. FreeEXRImage(&exr_image);
  5293. return TINYEXR_ERROR_INVALID_DATA;
  5294. }
  5295. (*out_rgba) = reinterpret_cast<float *>(
  5296. malloc(4 * sizeof(float) * static_cast<size_t>(exr_image.width) *
  5297. static_cast<size_t>(exr_image.height)));
  5298. if (exr_header.tiled) {
  5299. const size_t tile_size_x = static_cast<size_t>(exr_header.tile_size_x);
  5300. const size_t tile_size_y = static_cast<size_t>(exr_header.tile_size_y);
  5301. for (int it = 0; it < exr_image.num_tiles; it++) {
  5302. for (size_t j = 0; j < tile_size_y; j++) {
  5303. for (size_t i = 0; i < tile_size_x; i++) {
  5304. const size_t ii =
  5305. static_cast<size_t>(exr_image.tiles[it].offset_x) *
  5306. tile_size_x +
  5307. i;
  5308. const size_t jj =
  5309. static_cast<size_t>(exr_image.tiles[it].offset_y) *
  5310. tile_size_y +
  5311. j;
  5312. const size_t idx = ii + jj * static_cast<size_t>(exr_image.width);
  5313. // out of region check.
  5314. if (ii >= static_cast<size_t>(exr_image.width)) {
  5315. continue;
  5316. }
  5317. if (jj >= static_cast<size_t>(exr_image.height)) {
  5318. continue;
  5319. }
  5320. const size_t srcIdx = i + j * tile_size_x;
  5321. unsigned char **src = exr_image.tiles[it].images;
  5322. (*out_rgba)[4 * idx + 0] =
  5323. reinterpret_cast<float **>(src)[idxR][srcIdx];
  5324. (*out_rgba)[4 * idx + 1] =
  5325. reinterpret_cast<float **>(src)[idxG][srcIdx];
  5326. (*out_rgba)[4 * idx + 2] =
  5327. reinterpret_cast<float **>(src)[idxB][srcIdx];
  5328. if (idxA != -1) {
  5329. (*out_rgba)[4 * idx + 3] =
  5330. reinterpret_cast<float **>(src)[idxA][srcIdx];
  5331. } else {
  5332. (*out_rgba)[4 * idx + 3] = 1.0;
  5333. }
  5334. }
  5335. }
  5336. }
  5337. } else {
  5338. const size_t pixel_size = static_cast<size_t>(exr_image.width) *
  5339. static_cast<size_t>(exr_image.height);
  5340. for (size_t i = 0; i < pixel_size; i++) {
  5341. (*out_rgba)[4 * i + 0] =
  5342. reinterpret_cast<float **>(exr_image.images)[idxR][i];
  5343. (*out_rgba)[4 * i + 1] =
  5344. reinterpret_cast<float **>(exr_image.images)[idxG][i];
  5345. (*out_rgba)[4 * i + 2] =
  5346. reinterpret_cast<float **>(exr_image.images)[idxB][i];
  5347. if (idxA != -1) {
  5348. (*out_rgba)[4 * i + 3] =
  5349. reinterpret_cast<float **>(exr_image.images)[idxA][i];
  5350. } else {
  5351. (*out_rgba)[4 * i + 3] = 1.0;
  5352. }
  5353. }
  5354. }
  5355. }
  5356. (*width) = exr_image.width;
  5357. (*height) = exr_image.height;
  5358. FreeEXRHeader(&exr_header);
  5359. FreeEXRImage(&exr_image);
  5360. return TINYEXR_SUCCESS;
  5361. }
  5362. int IsEXR(const char *filename) {
  5363. EXRVersion exr_version;
  5364. int ret = ParseEXRVersionFromFile(&exr_version, filename);
  5365. if (ret != TINYEXR_SUCCESS) {
  5366. return ret;
  5367. }
  5368. return TINYEXR_SUCCESS;
  5369. }
  5370. int IsEXRFromMemory(const unsigned char *memory, size_t size) {
  5371. EXRVersion exr_version;
  5372. int ret = ParseEXRVersionFromMemory(&exr_version, memory, size);
  5373. if (ret != TINYEXR_SUCCESS) {
  5374. return ret;
  5375. }
  5376. return TINYEXR_SUCCESS;
  5377. }
  5378. int ParseEXRHeaderFromMemory(EXRHeader *exr_header, const EXRVersion *version,
  5379. const unsigned char *memory, size_t size,
  5380. const char **err) {
  5381. if (memory == NULL || exr_header == NULL) {
  5382. tinyexr::SetErrorMessage(
  5383. "Invalid argument. `memory` or `exr_header` argument is null in "
  5384. "ParseEXRHeaderFromMemory()",
  5385. err);
  5386. // Invalid argument
  5387. return TINYEXR_ERROR_INVALID_ARGUMENT;
  5388. }
  5389. if (size < tinyexr::kEXRVersionSize) {
  5390. tinyexr::SetErrorMessage("Insufficient header/data size.\n", err);
  5391. return TINYEXR_ERROR_INVALID_DATA;
  5392. }
  5393. const unsigned char *marker = memory + tinyexr::kEXRVersionSize;
  5394. size_t marker_size = size - tinyexr::kEXRVersionSize;
  5395. tinyexr::HeaderInfo info;
  5396. info.clear();
  5397. int ret;
  5398. {
  5399. std::string err_str;
  5400. ret = ParseEXRHeader(&info, NULL, version, &err_str, marker, marker_size);
  5401. if (ret != TINYEXR_SUCCESS) {
  5402. if (err && !err_str.empty()) {
  5403. tinyexr::SetErrorMessage(err_str, err);
  5404. }
  5405. }
  5406. }
  5407. {
  5408. std::string warn;
  5409. std::string err_str;
  5410. if (!ConvertHeader(exr_header, info, &warn, &err_str)) {
  5411. // release mem
  5412. for (size_t i = 0; i < info.attributes.size(); i++) {
  5413. if (info.attributes[i].value) {
  5414. free(info.attributes[i].value);
  5415. }
  5416. }
  5417. if (err && !err_str.empty()) {
  5418. tinyexr::SetErrorMessage(err_str, err);
  5419. }
  5420. ret = TINYEXR_ERROR_INVALID_HEADER;
  5421. }
  5422. }
  5423. exr_header->multipart = version->multipart ? 1 : 0;
  5424. exr_header->non_image = version->non_image ? 1 : 0;
  5425. return ret;
  5426. }
  5427. int LoadEXRFromMemory(float **out_rgba, int *width, int *height,
  5428. const unsigned char *memory, size_t size,
  5429. const char **err) {
  5430. if (out_rgba == NULL || memory == NULL) {
  5431. tinyexr::SetErrorMessage("Invalid argument for LoadEXRFromMemory", err);
  5432. return TINYEXR_ERROR_INVALID_ARGUMENT;
  5433. }
  5434. EXRVersion exr_version;
  5435. EXRImage exr_image;
  5436. EXRHeader exr_header;
  5437. InitEXRHeader(&exr_header);
  5438. int ret = ParseEXRVersionFromMemory(&exr_version, memory, size);
  5439. if (ret != TINYEXR_SUCCESS) {
  5440. std::stringstream ss;
  5441. ss << "Failed to parse EXR version. code(" << ret << ")";
  5442. tinyexr::SetErrorMessage(ss.str(), err);
  5443. return ret;
  5444. }
  5445. ret = ParseEXRHeaderFromMemory(&exr_header, &exr_version, memory, size, err);
  5446. if (ret != TINYEXR_SUCCESS) {
  5447. return ret;
  5448. }
  5449. // Read HALF channel as FLOAT.
  5450. for (int i = 0; i < exr_header.num_channels; i++) {
  5451. if (exr_header.pixel_types[i] == TINYEXR_PIXELTYPE_HALF) {
  5452. exr_header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
  5453. }
  5454. }
  5455. InitEXRImage(&exr_image);
  5456. ret = LoadEXRImageFromMemory(&exr_image, &exr_header, memory, size, err);
  5457. if (ret != TINYEXR_SUCCESS) {
  5458. return ret;
  5459. }
  5460. // RGBA
  5461. int idxR = -1;
  5462. int idxG = -1;
  5463. int idxB = -1;
  5464. int idxA = -1;
  5465. for (int c = 0; c < exr_header.num_channels; c++) {
  5466. if (strcmp(exr_header.channels[c].name, "R") == 0) {
  5467. idxR = c;
  5468. } else if (strcmp(exr_header.channels[c].name, "G") == 0) {
  5469. idxG = c;
  5470. } else if (strcmp(exr_header.channels[c].name, "B") == 0) {
  5471. idxB = c;
  5472. } else if (strcmp(exr_header.channels[c].name, "A") == 0) {
  5473. idxA = c;
  5474. }
  5475. }
  5476. // TODO(syoyo): Refactor removing same code as used in LoadEXR().
  5477. if (exr_header.num_channels == 1) {
  5478. // Grayscale channel only.
  5479. (*out_rgba) = reinterpret_cast<float *>(
  5480. malloc(4 * sizeof(float) * static_cast<size_t>(exr_image.width) *
  5481. static_cast<size_t>(exr_image.height)));
  5482. if (exr_header.tiled) {
  5483. const size_t tile_size_x = static_cast<size_t>(exr_header.tile_size_x);
  5484. const size_t tile_size_y = static_cast<size_t>(exr_header.tile_size_y);
  5485. for (int it = 0; it < exr_image.num_tiles; it++) {
  5486. for (size_t j = 0; j < tile_size_y; j++) {
  5487. for (size_t i = 0; i < tile_size_x; i++) {
  5488. const size_t ii =
  5489. static_cast<size_t>(exr_image.tiles[it].offset_x) *
  5490. tile_size_x +
  5491. i;
  5492. const size_t jj =
  5493. static_cast<size_t>(exr_image.tiles[it].offset_y) *
  5494. tile_size_y +
  5495. j;
  5496. const size_t idx = ii + jj * static_cast<size_t>(exr_image.width);
  5497. // out of region check.
  5498. if (ii >= static_cast<size_t>(exr_image.width)) {
  5499. continue;
  5500. }
  5501. if (jj >= static_cast<size_t>(exr_image.height)) {
  5502. continue;
  5503. }
  5504. const size_t srcIdx = i + j * tile_size_x;
  5505. unsigned char **src = exr_image.tiles[it].images;
  5506. (*out_rgba)[4 * idx + 0] =
  5507. reinterpret_cast<float **>(src)[0][srcIdx];
  5508. (*out_rgba)[4 * idx + 1] =
  5509. reinterpret_cast<float **>(src)[0][srcIdx];
  5510. (*out_rgba)[4 * idx + 2] =
  5511. reinterpret_cast<float **>(src)[0][srcIdx];
  5512. (*out_rgba)[4 * idx + 3] =
  5513. reinterpret_cast<float **>(src)[0][srcIdx];
  5514. }
  5515. }
  5516. }
  5517. } else {
  5518. const size_t pixel_size = static_cast<size_t>(exr_image.width) *
  5519. static_cast<size_t>(exr_image.height);
  5520. for (size_t i = 0; i < pixel_size; i++) {
  5521. const float val = reinterpret_cast<float **>(exr_image.images)[0][i];
  5522. (*out_rgba)[4 * i + 0] = val;
  5523. (*out_rgba)[4 * i + 1] = val;
  5524. (*out_rgba)[4 * i + 2] = val;
  5525. (*out_rgba)[4 * i + 3] = val;
  5526. }
  5527. }
  5528. } else {
  5529. // TODO(syoyo): Support non RGBA image.
  5530. if (idxR == -1) {
  5531. tinyexr::SetErrorMessage("R channel not found", err);
  5532. // @todo { free exr_image }
  5533. return TINYEXR_ERROR_INVALID_DATA;
  5534. }
  5535. if (idxG == -1) {
  5536. tinyexr::SetErrorMessage("G channel not found", err);
  5537. // @todo { free exr_image }
  5538. return TINYEXR_ERROR_INVALID_DATA;
  5539. }
  5540. if (idxB == -1) {
  5541. tinyexr::SetErrorMessage("B channel not found", err);
  5542. // @todo { free exr_image }
  5543. return TINYEXR_ERROR_INVALID_DATA;
  5544. }
  5545. (*out_rgba) = reinterpret_cast<float *>(
  5546. malloc(4 * sizeof(float) * static_cast<size_t>(exr_image.width) *
  5547. static_cast<size_t>(exr_image.height)));
  5548. if (exr_header.tiled) {
  5549. const size_t tile_size_x = static_cast<size_t>(exr_header.tile_size_x);
  5550. const size_t tile_size_y = static_cast<size_t>(exr_header.tile_size_y);
  5551. for (int it = 0; it < exr_image.num_tiles; it++) {
  5552. for (size_t j = 0; j < tile_size_y; j++)
  5553. for (size_t i = 0; i < tile_size_x; i++) {
  5554. const size_t ii =
  5555. static_cast<size_t>(exr_image.tiles[it].offset_x) *
  5556. tile_size_x +
  5557. i;
  5558. const size_t jj =
  5559. static_cast<size_t>(exr_image.tiles[it].offset_y) *
  5560. tile_size_y +
  5561. j;
  5562. const size_t idx = ii + jj * static_cast<size_t>(exr_image.width);
  5563. // out of region check.
  5564. if (ii >= static_cast<size_t>(exr_image.width)) {
  5565. continue;
  5566. }
  5567. if (jj >= static_cast<size_t>(exr_image.height)) {
  5568. continue;
  5569. }
  5570. const size_t srcIdx = i + j * tile_size_x;
  5571. unsigned char **src = exr_image.tiles[it].images;
  5572. (*out_rgba)[4 * idx + 0] =
  5573. reinterpret_cast<float **>(src)[idxR][srcIdx];
  5574. (*out_rgba)[4 * idx + 1] =
  5575. reinterpret_cast<float **>(src)[idxG][srcIdx];
  5576. (*out_rgba)[4 * idx + 2] =
  5577. reinterpret_cast<float **>(src)[idxB][srcIdx];
  5578. if (idxA != -1) {
  5579. (*out_rgba)[4 * idx + 3] =
  5580. reinterpret_cast<float **>(src)[idxA][srcIdx];
  5581. } else {
  5582. (*out_rgba)[4 * idx + 3] = 1.0;
  5583. }
  5584. }
  5585. }
  5586. } else {
  5587. const size_t pixel_size = static_cast<size_t>(exr_image.width) *
  5588. static_cast<size_t>(exr_image.height);
  5589. for (size_t i = 0; i < pixel_size; i++) {
  5590. (*out_rgba)[4 * i + 0] =
  5591. reinterpret_cast<float **>(exr_image.images)[idxR][i];
  5592. (*out_rgba)[4 * i + 1] =
  5593. reinterpret_cast<float **>(exr_image.images)[idxG][i];
  5594. (*out_rgba)[4 * i + 2] =
  5595. reinterpret_cast<float **>(exr_image.images)[idxB][i];
  5596. if (idxA != -1) {
  5597. (*out_rgba)[4 * i + 3] =
  5598. reinterpret_cast<float **>(exr_image.images)[idxA][i];
  5599. } else {
  5600. (*out_rgba)[4 * i + 3] = 1.0;
  5601. }
  5602. }
  5603. }
  5604. }
  5605. (*width) = exr_image.width;
  5606. (*height) = exr_image.height;
  5607. FreeEXRHeader(&exr_header);
  5608. FreeEXRImage(&exr_image);
  5609. return TINYEXR_SUCCESS;
  5610. }
  5611. // Represents a read-only file mapped to an address space in memory.
  5612. // If no memory-mapping API is available, falls back to allocating a buffer
  5613. // with a copy of the file's data.
  5614. struct MemoryMappedFile {
  5615. unsigned char *data; // To the start of the file's data.
  5616. size_t size; // The size of the file in bytes.
  5617. #ifdef TINYEXR_USE_WIN32_MMAP
  5618. HANDLE windows_file;
  5619. HANDLE windows_file_mapping;
  5620. #elif defined(TINYEXR_USE_POSIX_MMAP)
  5621. int posix_descriptor;
  5622. #endif
  5623. // MemoryMappedFile's constructor tries to map memory to a file.
  5624. // If this succeeds, valid() will return true and all fields
  5625. // are usable; otherwise, valid() will return false.
  5626. MemoryMappedFile(const char *filename) {
  5627. data = NULL;
  5628. size = 0;
  5629. #ifdef TINYEXR_USE_WIN32_MMAP
  5630. windows_file_mapping = NULL;
  5631. windows_file =
  5632. CreateFileW(tinyexr::UTF8ToWchar(filename).c_str(), // lpFileName
  5633. GENERIC_READ, // dwDesiredAccess
  5634. FILE_SHARE_READ, // dwShareMode
  5635. NULL, // lpSecurityAttributes
  5636. OPEN_EXISTING, // dwCreationDisposition
  5637. FILE_ATTRIBUTE_READONLY, // dwFlagsAndAttributes
  5638. NULL); // hTemplateFile
  5639. if (windows_file == INVALID_HANDLE_VALUE) {
  5640. return;
  5641. }
  5642. windows_file_mapping = CreateFileMapping(windows_file, // hFile
  5643. NULL, // lpFileMappingAttributes
  5644. PAGE_READONLY, // flProtect
  5645. 0, // dwMaximumSizeHigh
  5646. 0, // dwMaximumSizeLow
  5647. NULL); // lpName
  5648. if (windows_file_mapping == NULL) {
  5649. return;
  5650. }
  5651. data = reinterpret_cast<unsigned char *>(
  5652. MapViewOfFile(windows_file_mapping, // hFileMappingObject
  5653. FILE_MAP_READ, // dwDesiredAccess
  5654. 0, // dwFileOffsetHigh
  5655. 0, // dwFileOffsetLow
  5656. 0)); // dwNumberOfBytesToMap
  5657. if (!data) {
  5658. return;
  5659. }
  5660. LARGE_INTEGER windows_file_size = {};
  5661. if (!GetFileSizeEx(windows_file, &windows_file_size) ||
  5662. static_cast<ULONGLONG>(windows_file_size.QuadPart) >
  5663. std::numeric_limits<size_t>::max()) {
  5664. UnmapViewOfFile(data);
  5665. data = NULL;
  5666. return;
  5667. }
  5668. size = static_cast<size_t>(windows_file_size.QuadPart);
  5669. #elif defined(TINYEXR_USE_POSIX_MMAP)
  5670. posix_descriptor = open(filename, O_RDONLY);
  5671. if (posix_descriptor == -1) {
  5672. return;
  5673. }
  5674. struct stat info;
  5675. if (fstat(posix_descriptor, &info) < 0) {
  5676. return;
  5677. }
  5678. // Make sure st_size is in the valid range for a size_t. The second case
  5679. // can only fail if a POSIX implementation defines off_t to be a larger
  5680. // type than size_t - for instance, compiling with _FILE_OFFSET_BITS=64
  5681. // on a 32-bit system. On current 64-bit systems, this check can never
  5682. // fail, so we turn off clang's Wtautological-type-limit-compare warning
  5683. // around this code.
  5684. #ifdef __clang__
  5685. #pragma clang diagnostic push
  5686. #pragma clang diagnostic ignored "-Wtautological-type-limit-compare"
  5687. #endif
  5688. if (info.st_size < 0 ||
  5689. info.st_size > std::numeric_limits<ssize_t>::max()) {
  5690. return;
  5691. }
  5692. #ifdef __clang__
  5693. #pragma clang diagnostic pop
  5694. #endif
  5695. size = static_cast<size_t>(info.st_size);
  5696. data = reinterpret_cast<unsigned char *>(
  5697. mmap(0, size, PROT_READ, MAP_SHARED, posix_descriptor, 0));
  5698. if (data == MAP_FAILED) {
  5699. data = nullptr;
  5700. return;
  5701. }
  5702. #else
  5703. FILE *fp = fopen(filename, "rb");
  5704. if (!fp) {
  5705. return;
  5706. }
  5707. // Calling fseek(fp, 0, SEEK_END) isn't strictly-conforming C code, but
  5708. // since neither the WIN32 nor POSIX APIs are available in this branch, this
  5709. // is a reasonable fallback option.
  5710. if (fseek(fp, 0, SEEK_END) != 0) {
  5711. fclose(fp);
  5712. return;
  5713. }
  5714. const long ftell_result = ftell(fp);
  5715. if (ftell_result < 0) {
  5716. // Error from ftell
  5717. fclose(fp);
  5718. return;
  5719. }
  5720. size = static_cast<size_t>(ftell_result);
  5721. if (fseek(fp, 0, SEEK_SET) != 0) {
  5722. fclose(fp);
  5723. size = 0;
  5724. return;
  5725. }
  5726. data = reinterpret_cast<unsigned char *>(malloc(size));
  5727. if (!data) {
  5728. size = 0;
  5729. fclose(fp);
  5730. return;
  5731. }
  5732. size_t read_bytes = fread(data, 1, size, fp);
  5733. if (read_bytes != size) {
  5734. // TODO: Try to read data until reading `size` bytes.
  5735. fclose(fp);
  5736. size = 0;
  5737. data = nullptr;
  5738. return;
  5739. }
  5740. fclose(fp);
  5741. #endif
  5742. }
  5743. // MemoryMappedFile's destructor closes all its handles.
  5744. ~MemoryMappedFile() {
  5745. #ifdef TINYEXR_USE_WIN32_MMAP
  5746. if (data) {
  5747. (void)UnmapViewOfFile(data);
  5748. data = NULL;
  5749. }
  5750. if (windows_file_mapping != NULL) {
  5751. (void)CloseHandle(windows_file_mapping);
  5752. }
  5753. if (windows_file != INVALID_HANDLE_VALUE) {
  5754. (void)CloseHandle(windows_file);
  5755. }
  5756. #elif defined(TINYEXR_USE_POSIX_MMAP)
  5757. if (data) {
  5758. (void)munmap(data, size);
  5759. data = NULL;
  5760. }
  5761. if (posix_descriptor != -1) {
  5762. (void)close(posix_descriptor);
  5763. }
  5764. #else
  5765. if (data) {
  5766. (void)free(data);
  5767. }
  5768. data = NULL;
  5769. #endif
  5770. }
  5771. // A MemoryMappedFile cannot be copied or moved.
  5772. // Only check for this when compiling with C++11 or higher, since deleted
  5773. // function definitions were added then.
  5774. #if TINYEXR_HAS_CXX11
  5775. #ifdef __clang__
  5776. #pragma clang diagnostic push
  5777. #pragma clang diagnostic ignored "-Wc++98-compat"
  5778. #endif
  5779. MemoryMappedFile(const MemoryMappedFile &) = delete;
  5780. MemoryMappedFile &operator=(const MemoryMappedFile &) = delete;
  5781. MemoryMappedFile(MemoryMappedFile &&other) noexcept = delete;
  5782. MemoryMappedFile &operator=(MemoryMappedFile &&other) noexcept = delete;
  5783. #ifdef __clang__
  5784. #pragma clang diagnostic pop
  5785. #endif
  5786. #endif
  5787. // Returns whether this was successfully opened.
  5788. bool valid() const { return data; }
  5789. };
  5790. int LoadEXRImageFromFile(EXRImage *exr_image, const EXRHeader *exr_header,
  5791. const char *filename, const char **err) {
  5792. if (exr_image == NULL) {
  5793. tinyexr::SetErrorMessage("Invalid argument for LoadEXRImageFromFile", err);
  5794. return TINYEXR_ERROR_INVALID_ARGUMENT;
  5795. }
  5796. MemoryMappedFile file(filename);
  5797. if (!file.valid()) {
  5798. tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err);
  5799. return TINYEXR_ERROR_CANT_OPEN_FILE;
  5800. }
  5801. if (file.size < 16) {
  5802. tinyexr::SetErrorMessage("File size too short : " + std::string(filename),
  5803. err);
  5804. return TINYEXR_ERROR_INVALID_FILE;
  5805. }
  5806. return LoadEXRImageFromMemory(exr_image, exr_header, file.data, file.size,
  5807. err);
  5808. }
  5809. int LoadEXRImageFromMemory(EXRImage *exr_image, const EXRHeader *exr_header,
  5810. const unsigned char *memory, const size_t size,
  5811. const char **err) {
  5812. if (exr_image == NULL || memory == NULL ||
  5813. (size < tinyexr::kEXRVersionSize)) {
  5814. tinyexr::SetErrorMessage("Invalid argument for LoadEXRImageFromMemory",
  5815. err);
  5816. return TINYEXR_ERROR_INVALID_ARGUMENT;
  5817. }
  5818. if (exr_header->header_len == 0) {
  5819. tinyexr::SetErrorMessage("EXRHeader variable is not initialized.", err);
  5820. return TINYEXR_ERROR_INVALID_ARGUMENT;
  5821. }
  5822. const unsigned char *head = memory;
  5823. const unsigned char *marker = reinterpret_cast<const unsigned char *>(
  5824. memory + exr_header->header_len +
  5825. 8); // +8 for magic number + version header.
  5826. return tinyexr::DecodeEXRImage(exr_image, exr_header, head, marker, size,
  5827. err);
  5828. }
  5829. namespace tinyexr
  5830. {
  5831. #ifdef __clang__
  5832. #pragma clang diagnostic push
  5833. #pragma clang diagnostic ignored "-Wsign-conversion"
  5834. #endif
  5835. // out_data must be allocated initially with the block-header size
  5836. // of the current image(-part) type
  5837. static bool EncodePixelData(/* out */ std::vector<unsigned char>& out_data,
  5838. const unsigned char* const* images,
  5839. int compression_type,
  5840. int /*line_order*/,
  5841. int width, // for tiled : tile.width
  5842. int /*height*/, // for tiled : header.tile_size_y
  5843. int x_stride, // for tiled : header.tile_size_x
  5844. int line_no, // for tiled : 0
  5845. int num_lines, // for tiled : tile.height
  5846. size_t pixel_data_size,
  5847. const std::vector<ChannelInfo>& channels,
  5848. const std::vector<size_t>& channel_offset_list,
  5849. std::string *err,
  5850. const void* compression_param = 0) // zfp compression param
  5851. {
  5852. size_t buf_size = static_cast<size_t>(width) *
  5853. static_cast<size_t>(num_lines) *
  5854. static_cast<size_t>(pixel_data_size);
  5855. //int last2bit = (buf_size & 3);
  5856. // buf_size must be multiple of four
  5857. //if(last2bit) buf_size += 4 - last2bit;
  5858. std::vector<unsigned char> buf(buf_size);
  5859. size_t start_y = static_cast<size_t>(line_no);
  5860. for (size_t c = 0; c < channels.size(); c++) {
  5861. if (channels[c].pixel_type == TINYEXR_PIXELTYPE_HALF) {
  5862. if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  5863. for (int y = 0; y < num_lines; y++) {
  5864. // Assume increasing Y
  5865. float *line_ptr = reinterpret_cast<float *>(&buf.at(
  5866. static_cast<size_t>(pixel_data_size * size_t(y) * size_t(width)) +
  5867. channel_offset_list[c] *
  5868. static_cast<size_t>(width)));
  5869. for (int x = 0; x < width; x++) {
  5870. tinyexr::FP16 h16;
  5871. h16.u = reinterpret_cast<const unsigned short * const *>(
  5872. images)[c][(y + start_y) * size_t(x_stride) + size_t(x)];
  5873. tinyexr::FP32 f32 = half_to_float(h16);
  5874. tinyexr::swap4(&f32.f);
  5875. // line_ptr[x] = f32.f;
  5876. tinyexr::cpy4(line_ptr + x, &(f32.f));
  5877. }
  5878. }
  5879. } else if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_HALF) {
  5880. for (int y = 0; y < num_lines; y++) {
  5881. // Assume increasing Y
  5882. unsigned short *line_ptr = reinterpret_cast<unsigned short *>(
  5883. &buf.at(static_cast<size_t>(pixel_data_size * y *
  5884. width) +
  5885. channel_offset_list[c] *
  5886. static_cast<size_t>(width)));
  5887. for (int x = 0; x < width; x++) {
  5888. unsigned short val = reinterpret_cast<const unsigned short * const *>(
  5889. images)[c][(y + start_y) * x_stride + x];
  5890. tinyexr::swap2(&val);
  5891. // line_ptr[x] = val;
  5892. tinyexr::cpy2(line_ptr + x, &val);
  5893. }
  5894. }
  5895. } else {
  5896. if (err) {
  5897. (*err) += "Invalid requested_pixel_type.\n";
  5898. }
  5899. return false;
  5900. }
  5901. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  5902. if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_HALF) {
  5903. for (int y = 0; y < num_lines; y++) {
  5904. // Assume increasing Y
  5905. unsigned short *line_ptr = reinterpret_cast<unsigned short *>(
  5906. &buf.at(static_cast<size_t>(pixel_data_size * y *
  5907. width) +
  5908. channel_offset_list[c] *
  5909. static_cast<size_t>(width)));
  5910. for (int x = 0; x < width; x++) {
  5911. tinyexr::FP32 f32;
  5912. f32.f = reinterpret_cast<const float * const *>(
  5913. images)[c][(y + start_y) * x_stride + x];
  5914. tinyexr::FP16 h16;
  5915. h16 = float_to_half_full(f32);
  5916. tinyexr::swap2(reinterpret_cast<unsigned short *>(&h16.u));
  5917. // line_ptr[x] = h16.u;
  5918. tinyexr::cpy2(line_ptr + x, &(h16.u));
  5919. }
  5920. }
  5921. } else if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_FLOAT) {
  5922. for (int y = 0; y < num_lines; y++) {
  5923. // Assume increasing Y
  5924. float *line_ptr = reinterpret_cast<float *>(&buf.at(
  5925. static_cast<size_t>(pixel_data_size * y * width) +
  5926. channel_offset_list[c] *
  5927. static_cast<size_t>(width)));
  5928. for (int x = 0; x < width; x++) {
  5929. float val = reinterpret_cast<const float * const *>(
  5930. images)[c][(y + start_y) * x_stride + x];
  5931. tinyexr::swap4(&val);
  5932. // line_ptr[x] = val;
  5933. tinyexr::cpy4(line_ptr + x, &val);
  5934. }
  5935. }
  5936. } else {
  5937. if (err) {
  5938. (*err) += "Invalid requested_pixel_type.\n";
  5939. }
  5940. return false;
  5941. }
  5942. } else if (channels[c].pixel_type == TINYEXR_PIXELTYPE_UINT) {
  5943. for (int y = 0; y < num_lines; y++) {
  5944. // Assume increasing Y
  5945. unsigned int *line_ptr = reinterpret_cast<unsigned int *>(&buf.at(
  5946. static_cast<size_t>(pixel_data_size * y * width) +
  5947. channel_offset_list[c] * static_cast<size_t>(width)));
  5948. for (int x = 0; x < width; x++) {
  5949. unsigned int val = reinterpret_cast<const unsigned int * const *>(
  5950. images)[c][(y + start_y) * x_stride + x];
  5951. tinyexr::swap4(&val);
  5952. // line_ptr[x] = val;
  5953. tinyexr::cpy4(line_ptr + x, &val);
  5954. }
  5955. }
  5956. }
  5957. }
  5958. if (compression_type == TINYEXR_COMPRESSIONTYPE_NONE) {
  5959. // 4 byte: scan line
  5960. // 4 byte: data size
  5961. // ~ : pixel data(uncompressed)
  5962. out_data.insert(out_data.end(), buf.begin(), buf.end());
  5963. } else if ((compression_type == TINYEXR_COMPRESSIONTYPE_ZIPS) ||
  5964. (compression_type == TINYEXR_COMPRESSIONTYPE_ZIP)) {
  5965. #if TINYEXR_USE_MINIZ
  5966. std::vector<unsigned char> block(mz_compressBound(
  5967. static_cast<unsigned long>(buf.size())));
  5968. #elif TINYEXR_USE_STB_ZLIB
  5969. // there is no compressBound() function, so we use a value that
  5970. // is grossly overestimated, but should always work
  5971. std::vector<unsigned char> block(256 + 2 * buf.size());
  5972. #else
  5973. std::vector<unsigned char> block(
  5974. compressBound(static_cast<uLong>(buf.size())));
  5975. #endif
  5976. tinyexr::tinyexr_uint64 outSize = block.size();
  5977. if (!tinyexr::CompressZip(&block.at(0), outSize,
  5978. reinterpret_cast<const unsigned char *>(&buf.at(0)),
  5979. static_cast<unsigned long>(buf.size()))) {
  5980. if (err) {
  5981. (*err) += "Zip compresssion failed.\n";
  5982. }
  5983. return false;
  5984. }
  5985. // 4 byte: scan line
  5986. // 4 byte: data size
  5987. // ~ : pixel data(compressed)
  5988. unsigned int data_len = static_cast<unsigned int>(outSize); // truncate
  5989. out_data.insert(out_data.end(), block.begin(), block.begin() + data_len);
  5990. } else if (compression_type == TINYEXR_COMPRESSIONTYPE_RLE) {
  5991. // (buf.size() * 3) / 2 would be enough.
  5992. std::vector<unsigned char> block((buf.size() * 3) / 2);
  5993. tinyexr::tinyexr_uint64 outSize = block.size();
  5994. if (!tinyexr::CompressRle(&block.at(0), outSize,
  5995. reinterpret_cast<const unsigned char *>(&buf.at(0)),
  5996. static_cast<unsigned long>(buf.size()))) {
  5997. if (err) {
  5998. (*err) += "RLE compresssion failed.\n";
  5999. }
  6000. return false;
  6001. }
  6002. // 4 byte: scan line
  6003. // 4 byte: data size
  6004. // ~ : pixel data(compressed)
  6005. unsigned int data_len = static_cast<unsigned int>(outSize); // truncate
  6006. out_data.insert(out_data.end(), block.begin(), block.begin() + data_len);
  6007. } else if (compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) {
  6008. #if TINYEXR_USE_PIZ
  6009. unsigned int bufLen =
  6010. 8192 + static_cast<unsigned int>(
  6011. 2 * static_cast<unsigned int>(
  6012. buf.size())); // @fixme { compute good bound. }
  6013. std::vector<unsigned char> block(bufLen);
  6014. unsigned int outSize = static_cast<unsigned int>(block.size());
  6015. if (!CompressPiz(&block.at(0), &outSize,
  6016. reinterpret_cast<const unsigned char *>(&buf.at(0)),
  6017. buf.size(), channels, width, num_lines)) {
  6018. if (err) {
  6019. (*err) += "PIZ compresssion failed.\n";
  6020. }
  6021. return false;
  6022. }
  6023. // 4 byte: scan line
  6024. // 4 byte: data size
  6025. // ~ : pixel data(compressed)
  6026. unsigned int data_len = outSize;
  6027. out_data.insert(out_data.end(), block.begin(), block.begin() + data_len);
  6028. #else
  6029. if (err) {
  6030. (*err) += "PIZ compression is disabled in this build.\n";
  6031. }
  6032. return false;
  6033. #endif
  6034. } else if (compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) {
  6035. #if TINYEXR_USE_ZFP
  6036. const ZFPCompressionParam* zfp_compression_param = reinterpret_cast<const ZFPCompressionParam*>(compression_param);
  6037. std::vector<unsigned char> block;
  6038. unsigned int outSize;
  6039. tinyexr::CompressZfp(
  6040. &block, &outSize, reinterpret_cast<const float *>(&buf.at(0)),
  6041. width, num_lines, static_cast<int>(channels.size()), *zfp_compression_param);
  6042. // 4 byte: scan line
  6043. // 4 byte: data size
  6044. // ~ : pixel data(compressed)
  6045. unsigned int data_len = outSize;
  6046. out_data.insert(out_data.end(), block.begin(), block.begin() + data_len);
  6047. #else
  6048. if (err) {
  6049. (*err) += "ZFP compression is disabled in this build.\n";
  6050. }
  6051. (void)compression_param;
  6052. return false;
  6053. #endif
  6054. } else {
  6055. return false;
  6056. }
  6057. return true;
  6058. }
  6059. static int EncodeTiledLevel(const EXRImage* level_image, const EXRHeader* exr_header,
  6060. const std::vector<tinyexr::ChannelInfo>& channels,
  6061. std::vector<std::vector<unsigned char> >& data_list,
  6062. size_t start_index, // for data_list
  6063. int num_x_tiles, int num_y_tiles,
  6064. const std::vector<size_t>& channel_offset_list,
  6065. int pixel_data_size,
  6066. const void* compression_param, // must be set if zfp compression is enabled
  6067. std::string* err) {
  6068. int num_tiles = num_x_tiles * num_y_tiles;
  6069. if (num_tiles != level_image->num_tiles) {
  6070. if (err) {
  6071. (*err) += "Invalid number of tiles in argument.\n";
  6072. }
  6073. return TINYEXR_ERROR_INVALID_ARGUMENT;
  6074. }
  6075. if ((exr_header->tile_size_x > level_image->width || exr_header->tile_size_y > level_image->height) &&
  6076. level_image->level_x == 0 && level_image->level_y == 0) {
  6077. if (err) {
  6078. (*err) += "Failed to encode tile data.\n";
  6079. }
  6080. return TINYEXR_ERROR_INVALID_DATA;
  6081. }
  6082. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  6083. std::atomic<bool> invalid_data(false);
  6084. #else
  6085. bool invalid_data(false);
  6086. #endif
  6087. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  6088. std::vector<std::thread> workers;
  6089. std::atomic<int> tile_count(0);
  6090. int num_threads = std::max(1, int(std::thread::hardware_concurrency()));
  6091. if (num_threads > int(num_tiles)) {
  6092. num_threads = int(num_tiles);
  6093. }
  6094. for (int t = 0; t < num_threads; t++) {
  6095. workers.emplace_back(std::thread([&]() {
  6096. int i = 0;
  6097. while ((i = tile_count++) < num_tiles) {
  6098. #else
  6099. // Use signed int since some OpenMP compiler doesn't allow unsigned type for
  6100. // `parallel for`
  6101. #if TINYEXR_USE_OPENMP
  6102. #pragma omp parallel for
  6103. #endif
  6104. for (int i = 0; i < num_tiles; i++) {
  6105. #endif
  6106. size_t tile_idx = static_cast<size_t>(i);
  6107. size_t data_idx = tile_idx + start_index;
  6108. int x_tile = i % num_x_tiles;
  6109. int y_tile = i / num_x_tiles;
  6110. EXRTile& tile = level_image->tiles[tile_idx];
  6111. const unsigned char* const* images =
  6112. static_cast<const unsigned char* const*>(tile.images);
  6113. data_list[data_idx].resize(5*sizeof(int));
  6114. size_t data_header_size = data_list[data_idx].size();
  6115. bool ret = EncodePixelData(data_list[data_idx],
  6116. images,
  6117. exr_header->compression_type,
  6118. 0, // increasing y
  6119. tile.width,
  6120. exr_header->tile_size_y,
  6121. exr_header->tile_size_x,
  6122. 0,
  6123. tile.height,
  6124. pixel_data_size,
  6125. channels,
  6126. channel_offset_list,
  6127. err, compression_param);
  6128. if (!ret) {
  6129. invalid_data = true;
  6130. continue;
  6131. }
  6132. if (data_list[data_idx].size() <= data_header_size) {
  6133. invalid_data = true;
  6134. continue;
  6135. }
  6136. int data_len = static_cast<int>(data_list[data_idx].size() - data_header_size);
  6137. //tileX, tileY, levelX, levelY // pixel_data_size(int)
  6138. memcpy(&data_list[data_idx][0], &x_tile, sizeof(int));
  6139. memcpy(&data_list[data_idx][4], &y_tile, sizeof(int));
  6140. memcpy(&data_list[data_idx][8], &level_image->level_x, sizeof(int));
  6141. memcpy(&data_list[data_idx][12], &level_image->level_y, sizeof(int));
  6142. memcpy(&data_list[data_idx][16], &data_len, sizeof(int));
  6143. swap4(reinterpret_cast<int*>(&data_list[data_idx][0]));
  6144. swap4(reinterpret_cast<int*>(&data_list[data_idx][4]));
  6145. swap4(reinterpret_cast<int*>(&data_list[data_idx][8]));
  6146. swap4(reinterpret_cast<int*>(&data_list[data_idx][12]));
  6147. swap4(reinterpret_cast<int*>(&data_list[data_idx][16]));
  6148. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  6149. }
  6150. }));
  6151. }
  6152. for (auto &t : workers) {
  6153. t.join();
  6154. }
  6155. #else
  6156. } // omp parallel
  6157. #endif
  6158. if (invalid_data) {
  6159. if (err) {
  6160. (*err) += "Failed to encode tile data.\n";
  6161. }
  6162. return TINYEXR_ERROR_INVALID_DATA;
  6163. }
  6164. return TINYEXR_SUCCESS;
  6165. }
  6166. static int NumScanlines(int compression_type) {
  6167. int num_scanlines = 1;
  6168. if (compression_type == TINYEXR_COMPRESSIONTYPE_ZIP) {
  6169. num_scanlines = 16;
  6170. } else if (compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) {
  6171. num_scanlines = 32;
  6172. } else if (compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) {
  6173. num_scanlines = 16;
  6174. }
  6175. return num_scanlines;
  6176. }
  6177. static int EncodeChunk(const EXRImage* exr_image, const EXRHeader* exr_header,
  6178. const std::vector<ChannelInfo>& channels,
  6179. int num_blocks,
  6180. tinyexr_uint64 chunk_offset, // starting offset of current chunk
  6181. bool is_multipart,
  6182. OffsetData& offset_data, // output block offsets, must be initialized
  6183. std::vector<std::vector<unsigned char> >& data_list, // output
  6184. tinyexr_uint64& total_size, // output: ending offset of current chunk
  6185. std::string* err) {
  6186. int num_scanlines = NumScanlines(exr_header->compression_type);
  6187. data_list.resize(num_blocks);
  6188. std::vector<size_t> channel_offset_list(
  6189. static_cast<size_t>(exr_header->num_channels));
  6190. int pixel_data_size = 0;
  6191. {
  6192. size_t channel_offset = 0;
  6193. for (size_t c = 0; c < static_cast<size_t>(exr_header->num_channels); c++) {
  6194. channel_offset_list[c] = channel_offset;
  6195. if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_HALF) {
  6196. pixel_data_size += sizeof(unsigned short);
  6197. channel_offset += sizeof(unsigned short);
  6198. } else if (channels[c].requested_pixel_type ==
  6199. TINYEXR_PIXELTYPE_FLOAT) {
  6200. pixel_data_size += sizeof(float);
  6201. channel_offset += sizeof(float);
  6202. } else if (channels[c].requested_pixel_type == TINYEXR_PIXELTYPE_UINT) {
  6203. pixel_data_size += sizeof(unsigned int);
  6204. channel_offset += sizeof(unsigned int);
  6205. } else {
  6206. if (err) {
  6207. (*err) += "Invalid requested_pixel_type.\n";
  6208. }
  6209. return TINYEXR_ERROR_INVALID_DATA;
  6210. }
  6211. }
  6212. }
  6213. const void* compression_param = 0;
  6214. #if TINYEXR_USE_ZFP
  6215. tinyexr::ZFPCompressionParam zfp_compression_param;
  6216. // Use ZFP compression parameter from custom attributes(if such a parameter
  6217. // exists)
  6218. {
  6219. std::string e;
  6220. bool ret = tinyexr::FindZFPCompressionParam(
  6221. &zfp_compression_param, exr_header->custom_attributes,
  6222. exr_header->num_custom_attributes, &e);
  6223. if (!ret) {
  6224. // Use predefined compression parameter.
  6225. zfp_compression_param.type = 0;
  6226. zfp_compression_param.rate = 2;
  6227. }
  6228. compression_param = &zfp_compression_param;
  6229. }
  6230. #endif
  6231. tinyexr_uint64 offset = chunk_offset;
  6232. tinyexr_uint64 doffset = is_multipart ? 4u : 0u;
  6233. if (exr_image->tiles) {
  6234. const EXRImage* level_image = exr_image;
  6235. size_t block_idx = 0;
  6236. //tinyexr::tinyexr_uint64 block_data_size = 0;
  6237. int num_levels = (exr_header->tile_level_mode != TINYEXR_TILE_RIPMAP_LEVELS) ?
  6238. offset_data.num_x_levels : (offset_data.num_x_levels * offset_data.num_y_levels);
  6239. for (int level_index = 0; level_index < num_levels; ++level_index) {
  6240. if (!level_image) {
  6241. if (err) {
  6242. (*err) += "Invalid number of tiled levels for EncodeChunk\n";
  6243. }
  6244. return TINYEXR_ERROR_INVALID_DATA;
  6245. }
  6246. int level_index_from_image = LevelIndex(level_image->level_x, level_image->level_y,
  6247. exr_header->tile_level_mode, offset_data.num_x_levels);
  6248. if (level_index_from_image < 0) {
  6249. if (err) {
  6250. (*err) += "Invalid tile level mode\n";
  6251. }
  6252. return TINYEXR_ERROR_INVALID_DATA;
  6253. }
  6254. if (level_index_from_image != level_index) {
  6255. if (err) {
  6256. (*err) += "Incorrect level ordering in tiled image\n";
  6257. }
  6258. return TINYEXR_ERROR_INVALID_DATA;
  6259. }
  6260. int num_y_tiles = int(offset_data.offsets[level_index].size());
  6261. if (num_y_tiles <= 0) {
  6262. if (err) {
  6263. (*err) += "Invalid Y tile size\n";
  6264. }
  6265. return TINYEXR_ERROR_INVALID_DATA;
  6266. }
  6267. int num_x_tiles = int(offset_data.offsets[level_index][0].size());
  6268. if (num_x_tiles <= 0) {
  6269. if (err) {
  6270. (*err) += "Invalid X tile size\n";
  6271. }
  6272. return TINYEXR_ERROR_INVALID_DATA;
  6273. }
  6274. std::string e;
  6275. int ret = EncodeTiledLevel(level_image,
  6276. exr_header,
  6277. channels,
  6278. data_list,
  6279. block_idx,
  6280. num_x_tiles,
  6281. num_y_tiles,
  6282. channel_offset_list,
  6283. pixel_data_size,
  6284. compression_param,
  6285. &e);
  6286. if (ret != TINYEXR_SUCCESS) {
  6287. if (!e.empty() && err) {
  6288. (*err) += e;
  6289. }
  6290. return ret;
  6291. }
  6292. for (size_t j = 0; j < static_cast<size_t>(num_y_tiles); ++j)
  6293. for (size_t i = 0; i < static_cast<size_t>(num_x_tiles); ++i) {
  6294. offset_data.offsets[level_index][j][i] = offset;
  6295. swap8(reinterpret_cast<tinyexr_uint64*>(&offset_data.offsets[level_index][j][i]));
  6296. offset += data_list[block_idx].size() + doffset;
  6297. //block_data_size += data_list[block_idx].size();
  6298. ++block_idx;
  6299. }
  6300. level_image = level_image->next_level;
  6301. }
  6302. TINYEXR_CHECK_AND_RETURN_C(static_cast<int>(block_idx) == num_blocks, TINYEXR_ERROR_INVALID_DATA);
  6303. total_size = offset;
  6304. } else { // scanlines
  6305. std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data.offsets[0][0];
  6306. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  6307. std::atomic<bool> invalid_data(false);
  6308. std::vector<std::thread> workers;
  6309. std::atomic<int> block_count(0);
  6310. int num_threads = std::min(std::max(1, int(std::thread::hardware_concurrency())), num_blocks);
  6311. for (int t = 0; t < num_threads; t++) {
  6312. workers.emplace_back(std::thread([&]() {
  6313. int i = 0;
  6314. while ((i = block_count++) < num_blocks) {
  6315. #else
  6316. bool invalid_data(false);
  6317. #if TINYEXR_USE_OPENMP
  6318. #pragma omp parallel for
  6319. #endif
  6320. for (int i = 0; i < num_blocks; i++) {
  6321. #endif
  6322. int start_y = num_scanlines * i;
  6323. int end_Y = (std::min)(num_scanlines * (i + 1), exr_image->height);
  6324. int num_lines = end_Y - start_y;
  6325. const unsigned char* const* images =
  6326. static_cast<const unsigned char* const*>(exr_image->images);
  6327. data_list[i].resize(2*sizeof(int));
  6328. size_t data_header_size = data_list[i].size();
  6329. bool ret = EncodePixelData(data_list[i],
  6330. images,
  6331. exr_header->compression_type,
  6332. 0, // increasing y
  6333. exr_image->width,
  6334. exr_image->height,
  6335. exr_image->width,
  6336. start_y,
  6337. num_lines,
  6338. pixel_data_size,
  6339. channels,
  6340. channel_offset_list,
  6341. err,
  6342. compression_param);
  6343. if (!ret) {
  6344. invalid_data = true;
  6345. continue; // "break" cannot be used with OpenMP
  6346. }
  6347. if (data_list[i].size() <= data_header_size) {
  6348. invalid_data = true;
  6349. continue; // "break" cannot be used with OpenMP
  6350. }
  6351. int data_len = static_cast<int>(data_list[i].size() - data_header_size);
  6352. memcpy(&data_list[i][0], &start_y, sizeof(int));
  6353. memcpy(&data_list[i][4], &data_len, sizeof(int));
  6354. swap4(reinterpret_cast<int*>(&data_list[i][0]));
  6355. swap4(reinterpret_cast<int*>(&data_list[i][4]));
  6356. #if TINYEXR_HAS_CXX11 && (TINYEXR_USE_THREAD > 0)
  6357. }
  6358. }));
  6359. }
  6360. for (auto &t : workers) {
  6361. t.join();
  6362. }
  6363. #else
  6364. } // omp parallel
  6365. #endif
  6366. if (invalid_data) {
  6367. if (err) {
  6368. (*err) += "Failed to encode scanline data.\n";
  6369. }
  6370. return TINYEXR_ERROR_INVALID_DATA;
  6371. }
  6372. for (size_t i = 0; i < static_cast<size_t>(num_blocks); i++) {
  6373. offsets[i] = offset;
  6374. tinyexr::swap8(reinterpret_cast<tinyexr::tinyexr_uint64 *>(&offsets[i]));
  6375. offset += data_list[i].size() + doffset;
  6376. }
  6377. total_size = static_cast<size_t>(offset);
  6378. }
  6379. return TINYEXR_SUCCESS;
  6380. }
  6381. // can save a single or multi-part image (no deep* formats)
  6382. static size_t SaveEXRNPartImageToMemory(const EXRImage* exr_images,
  6383. const EXRHeader** exr_headers,
  6384. unsigned int num_parts,
  6385. unsigned char** memory_out, const char** err) {
  6386. if (exr_images == NULL || exr_headers == NULL || num_parts == 0 ||
  6387. memory_out == NULL) {
  6388. SetErrorMessage("Invalid argument for SaveEXRNPartImageToMemory",
  6389. err);
  6390. return 0;
  6391. }
  6392. {
  6393. for (unsigned int i = 0; i < num_parts; ++i) {
  6394. if (exr_headers[i]->compression_type < 0) {
  6395. SetErrorMessage("Invalid argument for SaveEXRNPartImageToMemory",
  6396. err);
  6397. return 0;
  6398. }
  6399. #if !TINYEXR_USE_PIZ
  6400. if (exr_headers[i]->compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) {
  6401. SetErrorMessage("PIZ compression is not supported in this build",
  6402. err);
  6403. return 0;
  6404. }
  6405. #endif
  6406. if (exr_headers[i]->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) {
  6407. #if !TINYEXR_USE_ZFP
  6408. SetErrorMessage("ZFP compression is not supported in this build",
  6409. err);
  6410. return 0;
  6411. #else
  6412. // All channels must be fp32.
  6413. // No fp16 support in ZFP atm(as of 2023 June)
  6414. // https://github.com/LLNL/fpzip/issues/2
  6415. for (int c = 0; c < exr_headers[i]->num_channels; ++c) {
  6416. if (exr_headers[i]->requested_pixel_types[c] != TINYEXR_PIXELTYPE_FLOAT) {
  6417. SetErrorMessage("Pixel type must be FLOAT for ZFP compression",
  6418. err);
  6419. return 0;
  6420. }
  6421. }
  6422. #endif
  6423. }
  6424. }
  6425. }
  6426. std::vector<unsigned char> memory;
  6427. // Header
  6428. {
  6429. const char header[] = { 0x76, 0x2f, 0x31, 0x01 };
  6430. memory.insert(memory.end(), header, header + 4);
  6431. }
  6432. // Version
  6433. // using value from the first header
  6434. int long_name = exr_headers[0]->long_name;
  6435. {
  6436. char marker[] = { 2, 0, 0, 0 };
  6437. /* @todo
  6438. if (exr_header->non_image) {
  6439. marker[1] |= 0x8;
  6440. }
  6441. */
  6442. // tiled
  6443. if (num_parts == 1 && exr_images[0].tiles) {
  6444. marker[1] |= 0x2;
  6445. }
  6446. // long_name
  6447. if (long_name) {
  6448. marker[1] |= 0x4;
  6449. }
  6450. // multipart
  6451. if (num_parts > 1) {
  6452. marker[1] |= 0x10;
  6453. }
  6454. memory.insert(memory.end(), marker, marker + 4);
  6455. }
  6456. int total_chunk_count = 0;
  6457. std::vector<int> chunk_count(num_parts);
  6458. std::vector<OffsetData> offset_data(num_parts);
  6459. for (unsigned int i = 0; i < num_parts; ++i) {
  6460. if (!exr_images[i].tiles) {
  6461. int num_scanlines = NumScanlines(exr_headers[i]->compression_type);
  6462. chunk_count[i] =
  6463. (exr_images[i].height + num_scanlines - 1) / num_scanlines;
  6464. InitSingleResolutionOffsets(offset_data[i], chunk_count[i]);
  6465. total_chunk_count += chunk_count[i];
  6466. } else {
  6467. {
  6468. std::vector<int> num_x_tiles, num_y_tiles;
  6469. if (!PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i])) {
  6470. SetErrorMessage("Failed to precalculate Tile info",
  6471. err);
  6472. return TINYEXR_ERROR_INVALID_DATA;
  6473. }
  6474. int ntiles = InitTileOffsets(offset_data[i], exr_headers[i], num_x_tiles, num_y_tiles);
  6475. if (ntiles > 0) {
  6476. chunk_count[i] = ntiles;
  6477. } else {
  6478. SetErrorMessage("Failed to compute Tile offsets",
  6479. err);
  6480. return TINYEXR_ERROR_INVALID_DATA;
  6481. }
  6482. total_chunk_count += chunk_count[i];
  6483. }
  6484. }
  6485. }
  6486. // Write attributes to memory buffer.
  6487. std::vector< std::vector<tinyexr::ChannelInfo> > channels(num_parts);
  6488. {
  6489. std::set<std::string> partnames;
  6490. for (unsigned int i = 0; i < num_parts; ++i) {
  6491. //channels
  6492. {
  6493. std::vector<unsigned char> data;
  6494. for (int c = 0; c < exr_headers[i]->num_channels; c++) {
  6495. tinyexr::ChannelInfo info;
  6496. info.p_linear = 0;
  6497. info.pixel_type = exr_headers[i]->pixel_types[c];
  6498. info.requested_pixel_type = exr_headers[i]->requested_pixel_types[c];
  6499. info.x_sampling = 1;
  6500. info.y_sampling = 1;
  6501. info.name = std::string(exr_headers[i]->channels[c].name);
  6502. channels[i].push_back(info);
  6503. }
  6504. tinyexr::WriteChannelInfo(data, channels[i]);
  6505. tinyexr::WriteAttributeToMemory(&memory, "channels", "chlist", &data.at(0),
  6506. static_cast<int>(data.size()));
  6507. }
  6508. {
  6509. int comp = exr_headers[i]->compression_type;
  6510. swap4(&comp);
  6511. WriteAttributeToMemory(
  6512. &memory, "compression", "compression",
  6513. reinterpret_cast<const unsigned char*>(&comp), 1);
  6514. }
  6515. {
  6516. int data[4] = { 0, 0, exr_images[i].width - 1, exr_images[i].height - 1 };
  6517. swap4(&data[0]);
  6518. swap4(&data[1]);
  6519. swap4(&data[2]);
  6520. swap4(&data[3]);
  6521. WriteAttributeToMemory(
  6522. &memory, "dataWindow", "box2i",
  6523. reinterpret_cast<const unsigned char*>(data), sizeof(int) * 4);
  6524. int data0[4] = { 0, 0, exr_images[0].width - 1, exr_images[0].height - 1 };
  6525. swap4(&data0[0]);
  6526. swap4(&data0[1]);
  6527. swap4(&data0[2]);
  6528. swap4(&data0[3]);
  6529. // Note: must be the same across parts (currently, using value from the first header)
  6530. WriteAttributeToMemory(
  6531. &memory, "displayWindow", "box2i",
  6532. reinterpret_cast<const unsigned char*>(data0), sizeof(int) * 4);
  6533. }
  6534. {
  6535. unsigned char line_order = 0; // @fixme { read line_order from EXRHeader }
  6536. WriteAttributeToMemory(&memory, "lineOrder", "lineOrder",
  6537. &line_order, 1);
  6538. }
  6539. {
  6540. // Note: must be the same across parts
  6541. float aspectRatio = 1.0f;
  6542. swap4(&aspectRatio);
  6543. WriteAttributeToMemory(
  6544. &memory, "pixelAspectRatio", "float",
  6545. reinterpret_cast<const unsigned char*>(&aspectRatio), sizeof(float));
  6546. }
  6547. {
  6548. float center[2] = { 0.0f, 0.0f };
  6549. swap4(&center[0]);
  6550. swap4(&center[1]);
  6551. WriteAttributeToMemory(
  6552. &memory, "screenWindowCenter", "v2f",
  6553. reinterpret_cast<const unsigned char*>(center), 2 * sizeof(float));
  6554. }
  6555. {
  6556. float w = 1.0f;
  6557. swap4(&w);
  6558. WriteAttributeToMemory(&memory, "screenWindowWidth", "float",
  6559. reinterpret_cast<const unsigned char*>(&w),
  6560. sizeof(float));
  6561. }
  6562. if (exr_images[i].tiles) {
  6563. unsigned char tile_mode = static_cast<unsigned char>(exr_headers[i]->tile_level_mode & 0x3);
  6564. if (exr_headers[i]->tile_rounding_mode) tile_mode |= (1u << 4u);
  6565. //unsigned char data[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  6566. unsigned int datai[3] = { 0, 0, 0 };
  6567. unsigned char* data = reinterpret_cast<unsigned char*>(&datai[0]);
  6568. datai[0] = static_cast<unsigned int>(exr_headers[i]->tile_size_x);
  6569. datai[1] = static_cast<unsigned int>(exr_headers[i]->tile_size_y);
  6570. data[8] = tile_mode;
  6571. swap4(reinterpret_cast<unsigned int*>(&data[0]));
  6572. swap4(reinterpret_cast<unsigned int*>(&data[4]));
  6573. WriteAttributeToMemory(
  6574. &memory, "tiles", "tiledesc",
  6575. reinterpret_cast<const unsigned char*>(data), 9);
  6576. }
  6577. // must be present for multi-part files - according to spec.
  6578. if (num_parts > 1) {
  6579. // name
  6580. {
  6581. size_t len = 0;
  6582. if ((len = strlen(exr_headers[i]->name)) > 0) {
  6583. #if TINYEXR_HAS_CXX11
  6584. partnames.emplace(exr_headers[i]->name);
  6585. #else
  6586. partnames.insert(std::string(exr_headers[i]->name));
  6587. #endif
  6588. if (partnames.size() != i + 1) {
  6589. SetErrorMessage("'name' attributes must be unique for a multi-part file", err);
  6590. return 0;
  6591. }
  6592. WriteAttributeToMemory(
  6593. &memory, "name", "string",
  6594. reinterpret_cast<const unsigned char*>(exr_headers[i]->name),
  6595. static_cast<int>(len));
  6596. } else {
  6597. SetErrorMessage("Invalid 'name' attribute for a multi-part file", err);
  6598. return 0;
  6599. }
  6600. }
  6601. // type
  6602. {
  6603. const char* type = "scanlineimage";
  6604. if (exr_images[i].tiles) type = "tiledimage";
  6605. WriteAttributeToMemory(
  6606. &memory, "type", "string",
  6607. reinterpret_cast<const unsigned char*>(type),
  6608. static_cast<int>(strlen(type)));
  6609. }
  6610. // chunkCount
  6611. {
  6612. WriteAttributeToMemory(
  6613. &memory, "chunkCount", "int",
  6614. reinterpret_cast<const unsigned char*>(&chunk_count[i]),
  6615. 4);
  6616. }
  6617. }
  6618. // Custom attributes
  6619. if (exr_headers[i]->num_custom_attributes > 0) {
  6620. for (int j = 0; j < exr_headers[i]->num_custom_attributes; j++) {
  6621. tinyexr::WriteAttributeToMemory(
  6622. &memory, exr_headers[i]->custom_attributes[j].name,
  6623. exr_headers[i]->custom_attributes[j].type,
  6624. reinterpret_cast<const unsigned char*>(
  6625. exr_headers[i]->custom_attributes[j].value),
  6626. exr_headers[i]->custom_attributes[j].size);
  6627. }
  6628. }
  6629. { // end of header
  6630. memory.push_back(0);
  6631. }
  6632. }
  6633. }
  6634. if (num_parts > 1) {
  6635. // end of header list
  6636. memory.push_back(0);
  6637. }
  6638. tinyexr_uint64 chunk_offset = memory.size() + size_t(total_chunk_count) * sizeof(tinyexr_uint64);
  6639. tinyexr_uint64 total_size = 0;
  6640. std::vector< std::vector< std::vector<unsigned char> > > data_lists(num_parts);
  6641. for (unsigned int i = 0; i < num_parts; ++i) {
  6642. std::string e;
  6643. int ret = EncodeChunk(&exr_images[i], exr_headers[i],
  6644. channels[i],
  6645. chunk_count[i],
  6646. // starting offset of current chunk after part-number
  6647. chunk_offset,
  6648. num_parts > 1,
  6649. offset_data[i], // output: block offsets, must be initialized
  6650. data_lists[i], // output
  6651. total_size, // output
  6652. &e);
  6653. if (ret != TINYEXR_SUCCESS) {
  6654. if (!e.empty()) {
  6655. tinyexr::SetErrorMessage(e, err);
  6656. }
  6657. return 0;
  6658. }
  6659. chunk_offset = total_size;
  6660. }
  6661. // Allocating required memory
  6662. if (total_size == 0) { // something went wrong
  6663. tinyexr::SetErrorMessage("Output memory size is zero", err);
  6664. return TINYEXR_ERROR_INVALID_DATA;
  6665. }
  6666. (*memory_out) = static_cast<unsigned char*>(malloc(size_t(total_size)));
  6667. // Writing header
  6668. memcpy((*memory_out), &memory[0], memory.size());
  6669. unsigned char* memory_ptr = *memory_out + memory.size();
  6670. size_t sum = memory.size();
  6671. // Writing offset data for chunks
  6672. for (unsigned int i = 0; i < num_parts; ++i) {
  6673. if (exr_images[i].tiles) {
  6674. const EXRImage* level_image = &exr_images[i];
  6675. int num_levels = (exr_headers[i]->tile_level_mode != TINYEXR_TILE_RIPMAP_LEVELS) ?
  6676. offset_data[i].num_x_levels : (offset_data[i].num_x_levels * offset_data[i].num_y_levels);
  6677. for (int level_index = 0; level_index < num_levels; ++level_index) {
  6678. for (size_t j = 0; j < offset_data[i].offsets[level_index].size(); ++j) {
  6679. size_t num_bytes = sizeof(tinyexr_uint64) * offset_data[i].offsets[level_index][j].size();
  6680. sum += num_bytes;
  6681. if (sum > total_size) {
  6682. tinyexr::SetErrorMessage("Invalid offset bytes in Tiled Part image.", err);
  6683. return TINYEXR_ERROR_INVALID_DATA;
  6684. }
  6685. memcpy(memory_ptr,
  6686. reinterpret_cast<unsigned char*>(&offset_data[i].offsets[level_index][j][0]),
  6687. num_bytes);
  6688. memory_ptr += num_bytes;
  6689. }
  6690. level_image = level_image->next_level;
  6691. }
  6692. } else {
  6693. size_t num_bytes = sizeof(tinyexr::tinyexr_uint64) * static_cast<size_t>(chunk_count[i]);
  6694. sum += num_bytes;
  6695. if (sum > total_size) {
  6696. tinyexr::SetErrorMessage("Invalid offset bytes in Part image.", err);
  6697. return TINYEXR_ERROR_INVALID_DATA;
  6698. }
  6699. std::vector<tinyexr::tinyexr_uint64>& offsets = offset_data[i].offsets[0][0];
  6700. memcpy(memory_ptr, reinterpret_cast<unsigned char*>(&offsets[0]), num_bytes);
  6701. memory_ptr += num_bytes;
  6702. }
  6703. }
  6704. // Writing chunk data
  6705. for (unsigned int i = 0; i < num_parts; ++i) {
  6706. for (size_t j = 0; j < static_cast<size_t>(chunk_count[i]); ++j) {
  6707. if (num_parts > 1) {
  6708. sum += 4;
  6709. if (sum > total_size) {
  6710. tinyexr::SetErrorMessage("Buffer overrun in reading Part image chunk data.", err);
  6711. return TINYEXR_ERROR_INVALID_DATA;
  6712. }
  6713. unsigned int part_number = i;
  6714. swap4(&part_number);
  6715. memcpy(memory_ptr, &part_number, 4);
  6716. memory_ptr += 4;
  6717. }
  6718. sum += data_lists[i][j].size();
  6719. if (sum > total_size) {
  6720. tinyexr::SetErrorMessage("Buffer overrun in reading Part image chunk data.", err);
  6721. return TINYEXR_ERROR_INVALID_DATA;
  6722. }
  6723. memcpy(memory_ptr, &data_lists[i][j][0], data_lists[i][j].size());
  6724. memory_ptr += data_lists[i][j].size();
  6725. }
  6726. }
  6727. if (sum != total_size) {
  6728. tinyexr::SetErrorMessage("Corrupted Part image chunk data.", err);
  6729. return TINYEXR_ERROR_INVALID_DATA;
  6730. }
  6731. return size_t(total_size); // OK
  6732. }
  6733. #ifdef __clang__
  6734. #pragma clang diagnostic pop
  6735. #endif
  6736. } // tinyexr
  6737. size_t SaveEXRImageToMemory(const EXRImage* exr_image,
  6738. const EXRHeader* exr_header,
  6739. unsigned char** memory_out, const char** err) {
  6740. return tinyexr::SaveEXRNPartImageToMemory(exr_image, &exr_header, 1, memory_out, err);
  6741. }
  6742. int SaveEXRImageToFile(const EXRImage *exr_image, const EXRHeader *exr_header,
  6743. const char *filename, const char **err) {
  6744. if (exr_image == NULL || filename == NULL ||
  6745. exr_header->compression_type < 0) {
  6746. tinyexr::SetErrorMessage("Invalid argument for SaveEXRImageToFile", err);
  6747. return TINYEXR_ERROR_INVALID_ARGUMENT;
  6748. }
  6749. #if !TINYEXR_USE_PIZ
  6750. if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_PIZ) {
  6751. tinyexr::SetErrorMessage("PIZ compression is not supported in this build",
  6752. err);
  6753. return TINYEXR_ERROR_UNSUPPORTED_FEATURE;
  6754. }
  6755. #endif
  6756. #if !TINYEXR_USE_ZFP
  6757. if (exr_header->compression_type == TINYEXR_COMPRESSIONTYPE_ZFP) {
  6758. tinyexr::SetErrorMessage("ZFP compression is not supported in this build",
  6759. err);
  6760. return TINYEXR_ERROR_UNSUPPORTED_FEATURE;
  6761. }
  6762. #endif
  6763. FILE *fp = NULL;
  6764. #ifdef _WIN32
  6765. #if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API) // MSVC, MinGW GCC, or Clang
  6766. errno_t errcode =
  6767. _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"wb");
  6768. if (errcode != 0) {
  6769. tinyexr::SetErrorMessage("Cannot write a file: " + std::string(filename),
  6770. err);
  6771. return TINYEXR_ERROR_CANT_WRITE_FILE;
  6772. }
  6773. #else
  6774. // Unknown compiler or MinGW without MINGW_HAS_SECURE_API.
  6775. fp = fopen(filename, "wb");
  6776. #endif
  6777. #else
  6778. fp = fopen(filename, "wb");
  6779. #endif
  6780. if (!fp) {
  6781. tinyexr::SetErrorMessage("Cannot write a file: " + std::string(filename),
  6782. err);
  6783. return TINYEXR_ERROR_CANT_WRITE_FILE;
  6784. }
  6785. unsigned char *mem = NULL;
  6786. size_t mem_size = SaveEXRImageToMemory(exr_image, exr_header, &mem, err);
  6787. if (mem_size == 0) {
  6788. fclose(fp);
  6789. return TINYEXR_ERROR_SERIALIZATION_FAILED;
  6790. }
  6791. size_t written_size = 0;
  6792. if ((mem_size > 0) && mem) {
  6793. written_size = fwrite(mem, 1, mem_size, fp);
  6794. }
  6795. free(mem);
  6796. fclose(fp);
  6797. if (written_size != mem_size) {
  6798. tinyexr::SetErrorMessage("Cannot write a file", err);
  6799. return TINYEXR_ERROR_CANT_WRITE_FILE;
  6800. }
  6801. return TINYEXR_SUCCESS;
  6802. }
  6803. size_t SaveEXRMultipartImageToMemory(const EXRImage* exr_images,
  6804. const EXRHeader** exr_headers,
  6805. unsigned int num_parts,
  6806. unsigned char** memory_out, const char** err) {
  6807. if (exr_images == NULL || exr_headers == NULL || num_parts < 2 ||
  6808. memory_out == NULL) {
  6809. tinyexr::SetErrorMessage("Invalid argument for SaveEXRNPartImageToMemory",
  6810. err);
  6811. return 0;
  6812. }
  6813. return tinyexr::SaveEXRNPartImageToMemory(exr_images, exr_headers, num_parts, memory_out, err);
  6814. }
  6815. int SaveEXRMultipartImageToFile(const EXRImage* exr_images,
  6816. const EXRHeader** exr_headers,
  6817. unsigned int num_parts,
  6818. const char* filename,
  6819. const char** err) {
  6820. if (exr_images == NULL || exr_headers == NULL || num_parts < 2) {
  6821. tinyexr::SetErrorMessage("Invalid argument for SaveEXRMultipartImageToFile",
  6822. err);
  6823. return TINYEXR_ERROR_INVALID_ARGUMENT;
  6824. }
  6825. FILE *fp = NULL;
  6826. #ifdef _WIN32
  6827. #if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API) // MSVC, MinGW GCC, or Clang.
  6828. errno_t errcode =
  6829. _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"wb");
  6830. if (errcode != 0) {
  6831. tinyexr::SetErrorMessage("Cannot write a file: " + std::string(filename),
  6832. err);
  6833. return TINYEXR_ERROR_CANT_WRITE_FILE;
  6834. }
  6835. #else
  6836. // Unknown compiler or MinGW without MINGW_HAS_SECURE_API.
  6837. fp = fopen(filename, "wb");
  6838. #endif
  6839. #else
  6840. fp = fopen(filename, "wb");
  6841. #endif
  6842. if (!fp) {
  6843. tinyexr::SetErrorMessage("Cannot write a file: " + std::string(filename),
  6844. err);
  6845. return TINYEXR_ERROR_CANT_WRITE_FILE;
  6846. }
  6847. unsigned char *mem = NULL;
  6848. size_t mem_size = SaveEXRMultipartImageToMemory(exr_images, exr_headers, num_parts, &mem, err);
  6849. if (mem_size == 0) {
  6850. fclose(fp);
  6851. return TINYEXR_ERROR_SERIALIZATION_FAILED;
  6852. }
  6853. size_t written_size = 0;
  6854. if ((mem_size > 0) && mem) {
  6855. written_size = fwrite(mem, 1, mem_size, fp);
  6856. }
  6857. free(mem);
  6858. fclose(fp);
  6859. if (written_size != mem_size) {
  6860. tinyexr::SetErrorMessage("Cannot write a file", err);
  6861. return TINYEXR_ERROR_CANT_WRITE_FILE;
  6862. }
  6863. return TINYEXR_SUCCESS;
  6864. }
  6865. int LoadDeepEXR(DeepImage *deep_image, const char *filename, const char **err) {
  6866. if (deep_image == NULL) {
  6867. tinyexr::SetErrorMessage("Invalid argument for LoadDeepEXR", err);
  6868. return TINYEXR_ERROR_INVALID_ARGUMENT;
  6869. }
  6870. MemoryMappedFile file(filename);
  6871. if (!file.valid()) {
  6872. tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err);
  6873. return TINYEXR_ERROR_CANT_OPEN_FILE;
  6874. }
  6875. if (file.size == 0) {
  6876. tinyexr::SetErrorMessage("File size is zero : " + std::string(filename),
  6877. err);
  6878. return TINYEXR_ERROR_INVALID_FILE;
  6879. }
  6880. const char *head = reinterpret_cast<const char *>(file.data);
  6881. const char *marker = reinterpret_cast<const char *>(file.data);
  6882. // Header check.
  6883. {
  6884. const char header[] = {0x76, 0x2f, 0x31, 0x01};
  6885. if (memcmp(marker, header, 4) != 0) {
  6886. tinyexr::SetErrorMessage("Invalid magic number", err);
  6887. return TINYEXR_ERROR_INVALID_MAGIC_NUMBER;
  6888. }
  6889. marker += 4;
  6890. }
  6891. // Version, scanline.
  6892. {
  6893. // ver 2.0, scanline, deep bit on(0x800)
  6894. // must be [2, 0, 0, 0]
  6895. if (marker[0] != 2 || marker[1] != 8 || marker[2] != 0 || marker[3] != 0) {
  6896. tinyexr::SetErrorMessage("Unsupported version or scanline", err);
  6897. return TINYEXR_ERROR_UNSUPPORTED_FORMAT;
  6898. }
  6899. marker += 4;
  6900. }
  6901. int dx = -1;
  6902. int dy = -1;
  6903. int dw = -1;
  6904. int dh = -1;
  6905. int num_scanline_blocks = 1; // 16 for ZIP compression.
  6906. int compression_type = -1;
  6907. int num_channels = -1;
  6908. std::vector<tinyexr::ChannelInfo> channels;
  6909. // Read attributes
  6910. size_t size = file.size - tinyexr::kEXRVersionSize;
  6911. for (;;) {
  6912. if (0 == size) {
  6913. return TINYEXR_ERROR_INVALID_DATA;
  6914. } else if (marker[0] == '\0') {
  6915. marker++;
  6916. size--;
  6917. break;
  6918. }
  6919. std::string attr_name;
  6920. std::string attr_type;
  6921. std::vector<unsigned char> data;
  6922. size_t marker_size;
  6923. if (!tinyexr::ReadAttribute(&attr_name, &attr_type, &data, &marker_size,
  6924. marker, size)) {
  6925. std::stringstream ss;
  6926. ss << "Failed to parse attribute\n";
  6927. tinyexr::SetErrorMessage(ss.str(), err);
  6928. return TINYEXR_ERROR_INVALID_DATA;
  6929. }
  6930. marker += marker_size;
  6931. size -= marker_size;
  6932. if (attr_name.compare("compression") == 0) {
  6933. compression_type = data[0];
  6934. if (compression_type > TINYEXR_COMPRESSIONTYPE_PIZ) {
  6935. std::stringstream ss;
  6936. ss << "Unsupported compression type : " << compression_type;
  6937. tinyexr::SetErrorMessage(ss.str(), err);
  6938. return TINYEXR_ERROR_UNSUPPORTED_FORMAT;
  6939. }
  6940. if (compression_type == TINYEXR_COMPRESSIONTYPE_ZIP) {
  6941. num_scanline_blocks = 16;
  6942. }
  6943. } else if (attr_name.compare("channels") == 0) {
  6944. // name: zero-terminated string, from 1 to 255 bytes long
  6945. // pixel type: int, possible values are: UINT = 0 HALF = 1 FLOAT = 2
  6946. // pLinear: unsigned char, possible values are 0 and 1
  6947. // reserved: three chars, should be zero
  6948. // xSampling: int
  6949. // ySampling: int
  6950. if (!tinyexr::ReadChannelInfo(channels, data)) {
  6951. tinyexr::SetErrorMessage("Failed to parse channel info", err);
  6952. return TINYEXR_ERROR_INVALID_DATA;
  6953. }
  6954. num_channels = static_cast<int>(channels.size());
  6955. if (num_channels < 1) {
  6956. tinyexr::SetErrorMessage("Invalid channels format", err);
  6957. return TINYEXR_ERROR_INVALID_DATA;
  6958. }
  6959. } else if (attr_name.compare("dataWindow") == 0) {
  6960. memcpy(&dx, &data.at(0), sizeof(int));
  6961. memcpy(&dy, &data.at(4), sizeof(int));
  6962. memcpy(&dw, &data.at(8), sizeof(int));
  6963. memcpy(&dh, &data.at(12), sizeof(int));
  6964. tinyexr::swap4(&dx);
  6965. tinyexr::swap4(&dy);
  6966. tinyexr::swap4(&dw);
  6967. tinyexr::swap4(&dh);
  6968. } else if (attr_name.compare("displayWindow") == 0) {
  6969. int x;
  6970. int y;
  6971. int w;
  6972. int h;
  6973. memcpy(&x, &data.at(0), sizeof(int));
  6974. memcpy(&y, &data.at(4), sizeof(int));
  6975. memcpy(&w, &data.at(8), sizeof(int));
  6976. memcpy(&h, &data.at(12), sizeof(int));
  6977. tinyexr::swap4(&x);
  6978. tinyexr::swap4(&y);
  6979. tinyexr::swap4(&w);
  6980. tinyexr::swap4(&h);
  6981. }
  6982. }
  6983. TINYEXR_CHECK_AND_RETURN_C(dx >= 0, TINYEXR_ERROR_INVALID_DATA);
  6984. TINYEXR_CHECK_AND_RETURN_C(dy >= 0, TINYEXR_ERROR_INVALID_DATA);
  6985. TINYEXR_CHECK_AND_RETURN_C(dw >= 0, TINYEXR_ERROR_INVALID_DATA);
  6986. TINYEXR_CHECK_AND_RETURN_C(dh >= 0, TINYEXR_ERROR_INVALID_DATA);
  6987. TINYEXR_CHECK_AND_RETURN_C(num_channels >= 1, TINYEXR_ERROR_INVALID_DATA);
  6988. int data_width = dw - dx + 1;
  6989. int data_height = dh - dy + 1;
  6990. // Read offset tables.
  6991. int num_blocks = data_height / num_scanline_blocks;
  6992. if (num_blocks * num_scanline_blocks < data_height) {
  6993. num_blocks++;
  6994. }
  6995. std::vector<tinyexr::tinyexr_int64> offsets(static_cast<size_t>(num_blocks));
  6996. for (size_t y = 0; y < static_cast<size_t>(num_blocks); y++) {
  6997. tinyexr::tinyexr_int64 offset;
  6998. memcpy(&offset, marker, sizeof(tinyexr::tinyexr_int64));
  6999. tinyexr::swap8(reinterpret_cast<tinyexr::tinyexr_uint64 *>(&offset));
  7000. marker += sizeof(tinyexr::tinyexr_int64); // = 8
  7001. offsets[y] = offset;
  7002. }
  7003. #if TINYEXR_USE_PIZ
  7004. if ((compression_type == TINYEXR_COMPRESSIONTYPE_NONE) ||
  7005. (compression_type == TINYEXR_COMPRESSIONTYPE_RLE) ||
  7006. (compression_type == TINYEXR_COMPRESSIONTYPE_ZIPS) ||
  7007. (compression_type == TINYEXR_COMPRESSIONTYPE_ZIP) ||
  7008. (compression_type == TINYEXR_COMPRESSIONTYPE_PIZ)) {
  7009. #else
  7010. if ((compression_type == TINYEXR_COMPRESSIONTYPE_NONE) ||
  7011. (compression_type == TINYEXR_COMPRESSIONTYPE_RLE) ||
  7012. (compression_type == TINYEXR_COMPRESSIONTYPE_ZIPS) ||
  7013. (compression_type == TINYEXR_COMPRESSIONTYPE_ZIP)) {
  7014. #endif
  7015. // OK
  7016. } else {
  7017. tinyexr::SetErrorMessage("Unsupported compression format", err);
  7018. return TINYEXR_ERROR_UNSUPPORTED_FORMAT;
  7019. }
  7020. deep_image->image = static_cast<float ***>(
  7021. malloc(sizeof(float **) * static_cast<size_t>(num_channels)));
  7022. for (int c = 0; c < num_channels; c++) {
  7023. deep_image->image[c] = static_cast<float **>(
  7024. malloc(sizeof(float *) * static_cast<size_t>(data_height)));
  7025. for (int y = 0; y < data_height; y++) {
  7026. }
  7027. }
  7028. deep_image->offset_table = static_cast<int **>(
  7029. malloc(sizeof(int *) * static_cast<size_t>(data_height)));
  7030. for (int y = 0; y < data_height; y++) {
  7031. deep_image->offset_table[y] = static_cast<int *>(
  7032. malloc(sizeof(int) * static_cast<size_t>(data_width)));
  7033. }
  7034. for (size_t y = 0; y < static_cast<size_t>(num_blocks); y++) {
  7035. const unsigned char *data_ptr =
  7036. reinterpret_cast<const unsigned char *>(head + offsets[y]);
  7037. // int: y coordinate
  7038. // int64: packed size of pixel offset table
  7039. // int64: packed size of sample data
  7040. // int64: unpacked size of sample data
  7041. // compressed pixel offset table
  7042. // compressed sample data
  7043. int line_no;
  7044. tinyexr::tinyexr_int64 packedOffsetTableSize;
  7045. tinyexr::tinyexr_int64 packedSampleDataSize;
  7046. tinyexr::tinyexr_int64 unpackedSampleDataSize;
  7047. memcpy(&line_no, data_ptr, sizeof(int));
  7048. memcpy(&packedOffsetTableSize, data_ptr + 4,
  7049. sizeof(tinyexr::tinyexr_int64));
  7050. memcpy(&packedSampleDataSize, data_ptr + 12,
  7051. sizeof(tinyexr::tinyexr_int64));
  7052. memcpy(&unpackedSampleDataSize, data_ptr + 20,
  7053. sizeof(tinyexr::tinyexr_int64));
  7054. tinyexr::swap4(&line_no);
  7055. tinyexr::swap8(
  7056. reinterpret_cast<tinyexr::tinyexr_uint64 *>(&packedOffsetTableSize));
  7057. tinyexr::swap8(
  7058. reinterpret_cast<tinyexr::tinyexr_uint64 *>(&packedSampleDataSize));
  7059. tinyexr::swap8(
  7060. reinterpret_cast<tinyexr::tinyexr_uint64 *>(&unpackedSampleDataSize));
  7061. std::vector<int> pixelOffsetTable(static_cast<size_t>(data_width));
  7062. // decode pixel offset table.
  7063. {
  7064. unsigned long dstLen =
  7065. static_cast<unsigned long>(pixelOffsetTable.size() * sizeof(int));
  7066. if (!tinyexr::DecompressZip(
  7067. reinterpret_cast<unsigned char *>(&pixelOffsetTable.at(0)),
  7068. &dstLen, data_ptr + 28,
  7069. static_cast<unsigned long>(packedOffsetTableSize))) {
  7070. return false;
  7071. }
  7072. TINYEXR_CHECK_AND_RETURN_C(dstLen == pixelOffsetTable.size() * sizeof(int), TINYEXR_ERROR_INVALID_DATA);
  7073. for (size_t i = 0; i < static_cast<size_t>(data_width); i++) {
  7074. deep_image->offset_table[y][i] = pixelOffsetTable[i];
  7075. }
  7076. }
  7077. std::vector<unsigned char> sample_data(
  7078. static_cast<size_t>(unpackedSampleDataSize));
  7079. // decode sample data.
  7080. {
  7081. unsigned long dstLen = static_cast<unsigned long>(unpackedSampleDataSize);
  7082. if (dstLen) {
  7083. if (!tinyexr::DecompressZip(
  7084. reinterpret_cast<unsigned char *>(&sample_data.at(0)), &dstLen,
  7085. data_ptr + 28 + packedOffsetTableSize,
  7086. static_cast<unsigned long>(packedSampleDataSize))) {
  7087. return false;
  7088. }
  7089. TINYEXR_CHECK_AND_RETURN_C(dstLen == static_cast<unsigned long>(unpackedSampleDataSize), TINYEXR_ERROR_INVALID_DATA);
  7090. }
  7091. }
  7092. // decode sample
  7093. int sampleSize = -1;
  7094. std::vector<int> channel_offset_list(static_cast<size_t>(num_channels));
  7095. {
  7096. int channel_offset = 0;
  7097. for (size_t i = 0; i < static_cast<size_t>(num_channels); i++) {
  7098. channel_offset_list[i] = channel_offset;
  7099. if (channels[i].pixel_type == TINYEXR_PIXELTYPE_UINT) { // UINT
  7100. channel_offset += 4;
  7101. } else if (channels[i].pixel_type == TINYEXR_PIXELTYPE_HALF) { // half
  7102. channel_offset += 2;
  7103. } else if (channels[i].pixel_type ==
  7104. TINYEXR_PIXELTYPE_FLOAT) { // float
  7105. channel_offset += 4;
  7106. } else {
  7107. tinyexr::SetErrorMessage("Invalid pixel_type in chnnels.", err);
  7108. return TINYEXR_ERROR_INVALID_DATA;
  7109. }
  7110. }
  7111. sampleSize = channel_offset;
  7112. }
  7113. TINYEXR_CHECK_AND_RETURN_C(sampleSize >= 2, TINYEXR_ERROR_INVALID_DATA);
  7114. TINYEXR_CHECK_AND_RETURN_C(static_cast<size_t>(
  7115. pixelOffsetTable[static_cast<size_t>(data_width - 1)] *
  7116. sampleSize) == sample_data.size(), TINYEXR_ERROR_INVALID_DATA);
  7117. int samples_per_line = static_cast<int>(sample_data.size()) / sampleSize;
  7118. //
  7119. // Alloc memory
  7120. //
  7121. //
  7122. // pixel data is stored as image[channels][pixel_samples]
  7123. //
  7124. {
  7125. tinyexr::tinyexr_uint64 data_offset = 0;
  7126. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  7127. deep_image->image[c][y] = static_cast<float *>(
  7128. malloc(sizeof(float) * static_cast<size_t>(samples_per_line)));
  7129. if (channels[c].pixel_type == 0) { // UINT
  7130. for (size_t x = 0; x < static_cast<size_t>(samples_per_line); x++) {
  7131. unsigned int ui;
  7132. unsigned int *src_ptr = reinterpret_cast<unsigned int *>(
  7133. &sample_data.at(size_t(data_offset) + x * sizeof(int)));
  7134. tinyexr::cpy4(&ui, src_ptr);
  7135. deep_image->image[c][y][x] = static_cast<float>(ui); // @fixme
  7136. }
  7137. data_offset +=
  7138. sizeof(unsigned int) * static_cast<size_t>(samples_per_line);
  7139. } else if (channels[c].pixel_type == 1) { // half
  7140. for (size_t x = 0; x < static_cast<size_t>(samples_per_line); x++) {
  7141. tinyexr::FP16 f16;
  7142. const unsigned short *src_ptr = reinterpret_cast<unsigned short *>(
  7143. &sample_data.at(size_t(data_offset) + x * sizeof(short)));
  7144. tinyexr::cpy2(&(f16.u), src_ptr);
  7145. tinyexr::FP32 f32 = half_to_float(f16);
  7146. deep_image->image[c][y][x] = f32.f;
  7147. }
  7148. data_offset += sizeof(short) * static_cast<size_t>(samples_per_line);
  7149. } else { // float
  7150. for (size_t x = 0; x < static_cast<size_t>(samples_per_line); x++) {
  7151. float f;
  7152. const float *src_ptr = reinterpret_cast<float *>(
  7153. &sample_data.at(size_t(data_offset) + x * sizeof(float)));
  7154. tinyexr::cpy4(&f, src_ptr);
  7155. deep_image->image[c][y][x] = f;
  7156. }
  7157. data_offset += sizeof(float) * static_cast<size_t>(samples_per_line);
  7158. }
  7159. }
  7160. }
  7161. } // y
  7162. deep_image->width = data_width;
  7163. deep_image->height = data_height;
  7164. deep_image->channel_names = static_cast<const char **>(
  7165. malloc(sizeof(const char *) * static_cast<size_t>(num_channels)));
  7166. for (size_t c = 0; c < static_cast<size_t>(num_channels); c++) {
  7167. #ifdef _WIN32
  7168. deep_image->channel_names[c] = _strdup(channels[c].name.c_str());
  7169. #else
  7170. deep_image->channel_names[c] = strdup(channels[c].name.c_str());
  7171. #endif
  7172. }
  7173. deep_image->num_channels = num_channels;
  7174. return TINYEXR_SUCCESS;
  7175. }
  7176. void InitEXRImage(EXRImage *exr_image) {
  7177. if (exr_image == NULL) {
  7178. return;
  7179. }
  7180. exr_image->width = 0;
  7181. exr_image->height = 0;
  7182. exr_image->num_channels = 0;
  7183. exr_image->images = NULL;
  7184. exr_image->tiles = NULL;
  7185. exr_image->next_level = NULL;
  7186. exr_image->level_x = 0;
  7187. exr_image->level_y = 0;
  7188. exr_image->num_tiles = 0;
  7189. }
  7190. void FreeEXRErrorMessage(const char *msg) {
  7191. if (msg) {
  7192. free(reinterpret_cast<void *>(const_cast<char *>(msg)));
  7193. }
  7194. return;
  7195. }
  7196. void InitEXRHeader(EXRHeader *exr_header) {
  7197. if (exr_header == NULL) {
  7198. return;
  7199. }
  7200. memset(exr_header, 0, sizeof(EXRHeader));
  7201. }
  7202. int FreeEXRHeader(EXRHeader *exr_header) {
  7203. if (exr_header == NULL) {
  7204. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7205. }
  7206. if (exr_header->channels) {
  7207. free(exr_header->channels);
  7208. }
  7209. if (exr_header->pixel_types) {
  7210. free(exr_header->pixel_types);
  7211. }
  7212. if (exr_header->requested_pixel_types) {
  7213. free(exr_header->requested_pixel_types);
  7214. }
  7215. for (int i = 0; i < exr_header->num_custom_attributes; i++) {
  7216. if (exr_header->custom_attributes[i].value) {
  7217. free(exr_header->custom_attributes[i].value);
  7218. }
  7219. }
  7220. if (exr_header->custom_attributes) {
  7221. free(exr_header->custom_attributes);
  7222. }
  7223. EXRSetNameAttr(exr_header, NULL);
  7224. return TINYEXR_SUCCESS;
  7225. }
  7226. void EXRSetNameAttr(EXRHeader* exr_header, const char* name) {
  7227. if (exr_header == NULL) {
  7228. return;
  7229. }
  7230. memset(exr_header->name, 0, 256);
  7231. if (name != NULL) {
  7232. size_t len = std::min(strlen(name), size_t(255));
  7233. if (len) {
  7234. memcpy(exr_header->name, name, len);
  7235. }
  7236. }
  7237. }
  7238. int EXRNumLevels(const EXRImage* exr_image) {
  7239. if (exr_image == NULL) return 0;
  7240. if(exr_image->images) return 1; // scanlines
  7241. int levels = 1;
  7242. const EXRImage* level_image = exr_image;
  7243. while((level_image = level_image->next_level)) ++levels;
  7244. return levels;
  7245. }
  7246. int FreeEXRImage(EXRImage *exr_image) {
  7247. if (exr_image == NULL) {
  7248. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7249. }
  7250. if (exr_image->next_level) {
  7251. FreeEXRImage(exr_image->next_level);
  7252. delete exr_image->next_level;
  7253. }
  7254. for (int i = 0; i < exr_image->num_channels; i++) {
  7255. if (exr_image->images && exr_image->images[i]) {
  7256. free(exr_image->images[i]);
  7257. }
  7258. }
  7259. if (exr_image->images) {
  7260. free(exr_image->images);
  7261. }
  7262. if (exr_image->tiles) {
  7263. for (int tid = 0; tid < exr_image->num_tiles; tid++) {
  7264. for (int i = 0; i < exr_image->num_channels; i++) {
  7265. if (exr_image->tiles[tid].images && exr_image->tiles[tid].images[i]) {
  7266. free(exr_image->tiles[tid].images[i]);
  7267. }
  7268. }
  7269. if (exr_image->tiles[tid].images) {
  7270. free(exr_image->tiles[tid].images);
  7271. }
  7272. }
  7273. free(exr_image->tiles);
  7274. }
  7275. return TINYEXR_SUCCESS;
  7276. }
  7277. int ParseEXRHeaderFromFile(EXRHeader *exr_header, const EXRVersion *exr_version,
  7278. const char *filename, const char **err) {
  7279. if (exr_header == NULL || exr_version == NULL || filename == NULL) {
  7280. tinyexr::SetErrorMessage("Invalid argument for ParseEXRHeaderFromFile",
  7281. err);
  7282. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7283. }
  7284. MemoryMappedFile file(filename);
  7285. if (!file.valid()) {
  7286. tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err);
  7287. return TINYEXR_ERROR_CANT_OPEN_FILE;
  7288. }
  7289. return ParseEXRHeaderFromMemory(exr_header, exr_version, file.data, file.size,
  7290. err);
  7291. }
  7292. int ParseEXRMultipartHeaderFromMemory(EXRHeader ***exr_headers,
  7293. int *num_headers,
  7294. const EXRVersion *exr_version,
  7295. const unsigned char *memory, size_t size,
  7296. const char **err) {
  7297. if (memory == NULL || exr_headers == NULL || num_headers == NULL ||
  7298. exr_version == NULL) {
  7299. // Invalid argument
  7300. tinyexr::SetErrorMessage(
  7301. "Invalid argument for ParseEXRMultipartHeaderFromMemory", err);
  7302. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7303. }
  7304. if (size < tinyexr::kEXRVersionSize) {
  7305. tinyexr::SetErrorMessage("Data size too short", err);
  7306. return TINYEXR_ERROR_INVALID_DATA;
  7307. }
  7308. const unsigned char *marker = memory + tinyexr::kEXRVersionSize;
  7309. size_t marker_size = size - tinyexr::kEXRVersionSize;
  7310. std::vector<tinyexr::HeaderInfo> infos;
  7311. for (;;) {
  7312. tinyexr::HeaderInfo info;
  7313. info.clear();
  7314. std::string err_str;
  7315. bool empty_header = false;
  7316. int ret = ParseEXRHeader(&info, &empty_header, exr_version, &err_str,
  7317. marker, marker_size);
  7318. if (ret != TINYEXR_SUCCESS) {
  7319. // Free malloc-allocated memory here.
  7320. for (size_t i = 0; i < info.attributes.size(); i++) {
  7321. if (info.attributes[i].value) {
  7322. free(info.attributes[i].value);
  7323. }
  7324. }
  7325. tinyexr::SetErrorMessage(err_str, err);
  7326. return ret;
  7327. }
  7328. if (empty_header) {
  7329. marker += 1; // skip '\0'
  7330. break;
  7331. }
  7332. // `chunkCount` must exist in the header.
  7333. if (info.chunk_count == 0) {
  7334. // Free malloc-allocated memory here.
  7335. for (size_t i = 0; i < info.attributes.size(); i++) {
  7336. if (info.attributes[i].value) {
  7337. free(info.attributes[i].value);
  7338. }
  7339. }
  7340. tinyexr::SetErrorMessage(
  7341. "`chunkCount' attribute is not found in the header.", err);
  7342. return TINYEXR_ERROR_INVALID_DATA;
  7343. }
  7344. infos.push_back(info);
  7345. // move to next header.
  7346. marker += info.header_len;
  7347. size -= info.header_len;
  7348. }
  7349. // allocate memory for EXRHeader and create array of EXRHeader pointers.
  7350. (*exr_headers) =
  7351. static_cast<EXRHeader **>(malloc(sizeof(EXRHeader *) * infos.size()));
  7352. int retcode = TINYEXR_SUCCESS;
  7353. for (size_t i = 0; i < infos.size(); i++) {
  7354. EXRHeader *exr_header = static_cast<EXRHeader *>(malloc(sizeof(EXRHeader)));
  7355. memset(exr_header, 0, sizeof(EXRHeader));
  7356. std::string warn;
  7357. std::string _err;
  7358. if (!ConvertHeader(exr_header, infos[i], &warn, &_err)) {
  7359. // Free malloc-allocated memory here.
  7360. for (size_t k = 0; k < infos[i].attributes.size(); k++) {
  7361. if (infos[i].attributes[k].value) {
  7362. free(infos[i].attributes[k].value);
  7363. }
  7364. }
  7365. if (!_err.empty()) {
  7366. tinyexr::SetErrorMessage(
  7367. _err, err);
  7368. }
  7369. // continue to converting headers
  7370. retcode = TINYEXR_ERROR_INVALID_HEADER;
  7371. }
  7372. exr_header->multipart = exr_version->multipart ? 1 : 0;
  7373. (*exr_headers)[i] = exr_header;
  7374. }
  7375. (*num_headers) = static_cast<int>(infos.size());
  7376. return retcode;
  7377. }
  7378. int ParseEXRMultipartHeaderFromFile(EXRHeader ***exr_headers, int *num_headers,
  7379. const EXRVersion *exr_version,
  7380. const char *filename, const char **err) {
  7381. if (exr_headers == NULL || num_headers == NULL || exr_version == NULL ||
  7382. filename == NULL) {
  7383. tinyexr::SetErrorMessage(
  7384. "Invalid argument for ParseEXRMultipartHeaderFromFile()", err);
  7385. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7386. }
  7387. MemoryMappedFile file(filename);
  7388. if (!file.valid()) {
  7389. tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err);
  7390. return TINYEXR_ERROR_CANT_OPEN_FILE;
  7391. }
  7392. return ParseEXRMultipartHeaderFromMemory(
  7393. exr_headers, num_headers, exr_version, file.data, file.size, err);
  7394. }
  7395. int ParseEXRVersionFromMemory(EXRVersion *version, const unsigned char *memory,
  7396. size_t size) {
  7397. if (version == NULL || memory == NULL) {
  7398. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7399. }
  7400. if (size < tinyexr::kEXRVersionSize) {
  7401. return TINYEXR_ERROR_INVALID_DATA;
  7402. }
  7403. const unsigned char *marker = memory;
  7404. // Header check.
  7405. {
  7406. const char header[] = {0x76, 0x2f, 0x31, 0x01};
  7407. if (memcmp(marker, header, 4) != 0) {
  7408. return TINYEXR_ERROR_INVALID_MAGIC_NUMBER;
  7409. }
  7410. marker += 4;
  7411. }
  7412. version->tiled = false;
  7413. version->long_name = false;
  7414. version->non_image = false;
  7415. version->multipart = false;
  7416. // Parse version header.
  7417. {
  7418. // must be 2
  7419. if (marker[0] != 2) {
  7420. return TINYEXR_ERROR_INVALID_EXR_VERSION;
  7421. }
  7422. if (version == NULL) {
  7423. return TINYEXR_SUCCESS; // May OK
  7424. }
  7425. version->version = 2;
  7426. if (marker[1] & 0x2) { // 9th bit
  7427. version->tiled = true;
  7428. }
  7429. if (marker[1] & 0x4) { // 10th bit
  7430. version->long_name = true;
  7431. }
  7432. if (marker[1] & 0x8) { // 11th bit
  7433. version->non_image = true; // (deep image)
  7434. }
  7435. if (marker[1] & 0x10) { // 12th bit
  7436. version->multipart = true;
  7437. }
  7438. }
  7439. return TINYEXR_SUCCESS;
  7440. }
  7441. int ParseEXRVersionFromFile(EXRVersion *version, const char *filename) {
  7442. if (filename == NULL) {
  7443. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7444. }
  7445. FILE *fp = NULL;
  7446. #ifdef _WIN32
  7447. #if defined(_MSC_VER) || (defined(MINGW_HAS_SECURE_API) && MINGW_HAS_SECURE_API) // MSVC, MinGW GCC, or Clang.
  7448. errno_t err = _wfopen_s(&fp, tinyexr::UTF8ToWchar(filename).c_str(), L"rb");
  7449. if (err != 0) {
  7450. // TODO(syoyo): return wfopen_s erro code
  7451. return TINYEXR_ERROR_CANT_OPEN_FILE;
  7452. }
  7453. #else
  7454. // Unknown compiler or MinGW without MINGW_HAS_SECURE_API.
  7455. fp = fopen(filename, "rb");
  7456. #endif
  7457. #else
  7458. fp = fopen(filename, "rb");
  7459. #endif
  7460. if (!fp) {
  7461. return TINYEXR_ERROR_CANT_OPEN_FILE;
  7462. }
  7463. // Try to read kEXRVersionSize bytes; if the file is shorter than
  7464. // kEXRVersionSize, this will produce an error. This avoids a call to
  7465. // fseek(fp, 0, SEEK_END), which is not required to be supported by C
  7466. // implementations.
  7467. unsigned char buf[tinyexr::kEXRVersionSize];
  7468. size_t ret = fread(&buf[0], 1, tinyexr::kEXRVersionSize, fp);
  7469. fclose(fp);
  7470. if (ret != tinyexr::kEXRVersionSize) {
  7471. return TINYEXR_ERROR_INVALID_FILE;
  7472. }
  7473. return ParseEXRVersionFromMemory(version, buf, tinyexr::kEXRVersionSize);
  7474. }
  7475. int LoadEXRMultipartImageFromMemory(EXRImage *exr_images,
  7476. const EXRHeader **exr_headers,
  7477. unsigned int num_parts,
  7478. const unsigned char *memory,
  7479. const size_t size, const char **err) {
  7480. if (exr_images == NULL || exr_headers == NULL || num_parts == 0 ||
  7481. memory == NULL || (size <= tinyexr::kEXRVersionSize)) {
  7482. tinyexr::SetErrorMessage(
  7483. "Invalid argument for LoadEXRMultipartImageFromMemory()", err);
  7484. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7485. }
  7486. // compute total header size.
  7487. size_t total_header_size = 0;
  7488. for (unsigned int i = 0; i < num_parts; i++) {
  7489. if (exr_headers[i]->header_len == 0) {
  7490. tinyexr::SetErrorMessage("EXRHeader variable is not initialized.", err);
  7491. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7492. }
  7493. total_header_size += exr_headers[i]->header_len;
  7494. }
  7495. const char *marker = reinterpret_cast<const char *>(
  7496. memory + total_header_size + 4 +
  7497. 4); // +8 for magic number and version header.
  7498. marker += 1; // Skip empty header.
  7499. // NOTE 1:
  7500. // In multipart image, There is 'part number' before chunk data.
  7501. // 4 byte : part number
  7502. // 4+ : chunk
  7503. //
  7504. // NOTE 2:
  7505. // EXR spec says 'part number' is 'unsigned long' but actually this is
  7506. // 'unsigned int(4 bytes)' in OpenEXR implementation...
  7507. // http://www.openexr.com/openexrfilelayout.pdf
  7508. // Load chunk offset table.
  7509. std::vector<tinyexr::OffsetData> chunk_offset_table_list;
  7510. chunk_offset_table_list.reserve(num_parts);
  7511. for (size_t i = 0; i < static_cast<size_t>(num_parts); i++) {
  7512. chunk_offset_table_list.resize(chunk_offset_table_list.size() + 1);
  7513. tinyexr::OffsetData& offset_data = chunk_offset_table_list.back();
  7514. if (!exr_headers[i]->tiled || exr_headers[i]->tile_level_mode == TINYEXR_TILE_ONE_LEVEL) {
  7515. tinyexr::InitSingleResolutionOffsets(offset_data, size_t(exr_headers[i]->chunk_count));
  7516. std::vector<tinyexr::tinyexr_uint64>& offset_table = offset_data.offsets[0][0];
  7517. for (size_t c = 0; c < offset_table.size(); c++) {
  7518. tinyexr::tinyexr_uint64 offset;
  7519. memcpy(&offset, marker, 8);
  7520. tinyexr::swap8(&offset);
  7521. if (offset >= size) {
  7522. tinyexr::SetErrorMessage("Invalid offset size in EXR header chunks.",
  7523. err);
  7524. return TINYEXR_ERROR_INVALID_DATA;
  7525. }
  7526. offset_table[c] = offset + 4; // +4 to skip 'part number'
  7527. marker += 8;
  7528. }
  7529. } else {
  7530. {
  7531. std::vector<int> num_x_tiles, num_y_tiles;
  7532. if (!tinyexr::PrecalculateTileInfo(num_x_tiles, num_y_tiles, exr_headers[i])) {
  7533. tinyexr::SetErrorMessage("Invalid tile info.", err);
  7534. return TINYEXR_ERROR_INVALID_DATA;
  7535. }
  7536. int num_blocks = InitTileOffsets(offset_data, exr_headers[i], num_x_tiles, num_y_tiles);
  7537. if (num_blocks != exr_headers[i]->chunk_count) {
  7538. tinyexr::SetErrorMessage("Invalid offset table size.", err);
  7539. return TINYEXR_ERROR_INVALID_DATA;
  7540. }
  7541. }
  7542. for (unsigned int l = 0; l < offset_data.offsets.size(); ++l) {
  7543. for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy) {
  7544. for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) {
  7545. tinyexr::tinyexr_uint64 offset;
  7546. memcpy(&offset, marker, sizeof(tinyexr::tinyexr_uint64));
  7547. tinyexr::swap8(&offset);
  7548. if (offset >= size) {
  7549. tinyexr::SetErrorMessage("Invalid offset size in EXR header chunks.",
  7550. err);
  7551. return TINYEXR_ERROR_INVALID_DATA;
  7552. }
  7553. offset_data.offsets[l][dy][dx] = offset + 4; // +4 to skip 'part number'
  7554. marker += sizeof(tinyexr::tinyexr_uint64); // = 8
  7555. }
  7556. }
  7557. }
  7558. }
  7559. }
  7560. // Decode image.
  7561. for (size_t i = 0; i < static_cast<size_t>(num_parts); i++) {
  7562. tinyexr::OffsetData &offset_data = chunk_offset_table_list[i];
  7563. // First check 'part number' is identical to 'i'
  7564. for (unsigned int l = 0; l < offset_data.offsets.size(); ++l)
  7565. for (unsigned int dy = 0; dy < offset_data.offsets[l].size(); ++dy)
  7566. for (unsigned int dx = 0; dx < offset_data.offsets[l][dy].size(); ++dx) {
  7567. const unsigned char *part_number_addr =
  7568. memory + offset_data.offsets[l][dy][dx] - 4; // -4 to move to 'part number' field.
  7569. unsigned int part_no;
  7570. memcpy(&part_no, part_number_addr, sizeof(unsigned int)); // 4
  7571. tinyexr::swap4(&part_no);
  7572. if (part_no != i) {
  7573. tinyexr::SetErrorMessage("Invalid `part number' in EXR header chunks.",
  7574. err);
  7575. return TINYEXR_ERROR_INVALID_DATA;
  7576. }
  7577. }
  7578. std::string e;
  7579. int ret = tinyexr::DecodeChunk(&exr_images[i], exr_headers[i], offset_data,
  7580. memory, size, &e);
  7581. if (ret != TINYEXR_SUCCESS) {
  7582. if (!e.empty()) {
  7583. tinyexr::SetErrorMessage(e, err);
  7584. }
  7585. return ret;
  7586. }
  7587. }
  7588. return TINYEXR_SUCCESS;
  7589. }
  7590. int LoadEXRMultipartImageFromFile(EXRImage *exr_images,
  7591. const EXRHeader **exr_headers,
  7592. unsigned int num_parts, const char *filename,
  7593. const char **err) {
  7594. if (exr_images == NULL || exr_headers == NULL || num_parts == 0) {
  7595. tinyexr::SetErrorMessage(
  7596. "Invalid argument for LoadEXRMultipartImageFromFile", err);
  7597. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7598. }
  7599. MemoryMappedFile file(filename);
  7600. if (!file.valid()) {
  7601. tinyexr::SetErrorMessage("Cannot read file " + std::string(filename), err);
  7602. return TINYEXR_ERROR_CANT_OPEN_FILE;
  7603. }
  7604. return LoadEXRMultipartImageFromMemory(exr_images, exr_headers, num_parts,
  7605. file.data, file.size, err);
  7606. }
  7607. int SaveEXRToMemory(const float *data, int width, int height, int components,
  7608. const int save_as_fp16, const unsigned char **outbuf, const char **err) {
  7609. if ((components == 1) || components == 3 || components == 4) {
  7610. // OK
  7611. } else {
  7612. std::stringstream ss;
  7613. ss << "Unsupported component value : " << components << std::endl;
  7614. tinyexr::SetErrorMessage(ss.str(), err);
  7615. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7616. }
  7617. EXRHeader header;
  7618. InitEXRHeader(&header);
  7619. if ((width < 16) && (height < 16)) {
  7620. // No compression for small image.
  7621. header.compression_type = TINYEXR_COMPRESSIONTYPE_NONE;
  7622. } else {
  7623. header.compression_type = TINYEXR_COMPRESSIONTYPE_ZIP;
  7624. }
  7625. EXRImage image;
  7626. InitEXRImage(&image);
  7627. image.num_channels = components;
  7628. std::vector<float> images[4];
  7629. if (components == 1) {
  7630. images[0].resize(static_cast<size_t>(width * height));
  7631. memcpy(images[0].data(), data, sizeof(float) * size_t(width * height));
  7632. } else {
  7633. images[0].resize(static_cast<size_t>(width * height));
  7634. images[1].resize(static_cast<size_t>(width * height));
  7635. images[2].resize(static_cast<size_t>(width * height));
  7636. images[3].resize(static_cast<size_t>(width * height));
  7637. // Split RGB(A)RGB(A)RGB(A)... into R, G and B(and A) layers
  7638. for (size_t i = 0; i < static_cast<size_t>(width * height); i++) {
  7639. images[0][i] = data[static_cast<size_t>(components) * i + 0];
  7640. images[1][i] = data[static_cast<size_t>(components) * i + 1];
  7641. images[2][i] = data[static_cast<size_t>(components) * i + 2];
  7642. if (components == 4) {
  7643. images[3][i] = data[static_cast<size_t>(components) * i + 3];
  7644. }
  7645. }
  7646. }
  7647. float *image_ptr[4] = {0, 0, 0, 0};
  7648. if (components == 4) {
  7649. image_ptr[0] = &(images[3].at(0)); // A
  7650. image_ptr[1] = &(images[2].at(0)); // B
  7651. image_ptr[2] = &(images[1].at(0)); // G
  7652. image_ptr[3] = &(images[0].at(0)); // R
  7653. } else if (components == 3) {
  7654. image_ptr[0] = &(images[2].at(0)); // B
  7655. image_ptr[1] = &(images[1].at(0)); // G
  7656. image_ptr[2] = &(images[0].at(0)); // R
  7657. } else if (components == 1) {
  7658. image_ptr[0] = &(images[0].at(0)); // A
  7659. }
  7660. image.images = reinterpret_cast<unsigned char **>(image_ptr);
  7661. image.width = width;
  7662. image.height = height;
  7663. header.num_channels = components;
  7664. header.channels = static_cast<EXRChannelInfo *>(malloc(
  7665. sizeof(EXRChannelInfo) * static_cast<size_t>(header.num_channels)));
  7666. // Must be (A)BGR order, since most of EXR viewers expect this channel order.
  7667. if (components == 4) {
  7668. #ifdef _MSC_VER
  7669. strncpy_s(header.channels[0].name, "A", 255);
  7670. strncpy_s(header.channels[1].name, "B", 255);
  7671. strncpy_s(header.channels[2].name, "G", 255);
  7672. strncpy_s(header.channels[3].name, "R", 255);
  7673. #else
  7674. strncpy(header.channels[0].name, "A", 255);
  7675. strncpy(header.channels[1].name, "B", 255);
  7676. strncpy(header.channels[2].name, "G", 255);
  7677. strncpy(header.channels[3].name, "R", 255);
  7678. #endif
  7679. header.channels[0].name[strlen("A")] = '\0';
  7680. header.channels[1].name[strlen("B")] = '\0';
  7681. header.channels[2].name[strlen("G")] = '\0';
  7682. header.channels[3].name[strlen("R")] = '\0';
  7683. } else if (components == 3) {
  7684. #ifdef _MSC_VER
  7685. strncpy_s(header.channels[0].name, "B", 255);
  7686. strncpy_s(header.channels[1].name, "G", 255);
  7687. strncpy_s(header.channels[2].name, "R", 255);
  7688. #else
  7689. strncpy(header.channels[0].name, "B", 255);
  7690. strncpy(header.channels[1].name, "G", 255);
  7691. strncpy(header.channels[2].name, "R", 255);
  7692. #endif
  7693. header.channels[0].name[strlen("B")] = '\0';
  7694. header.channels[1].name[strlen("G")] = '\0';
  7695. header.channels[2].name[strlen("R")] = '\0';
  7696. } else {
  7697. #ifdef _MSC_VER
  7698. strncpy_s(header.channels[0].name, "A", 255);
  7699. #else
  7700. strncpy(header.channels[0].name, "A", 255);
  7701. #endif
  7702. header.channels[0].name[strlen("A")] = '\0';
  7703. }
  7704. header.pixel_types = static_cast<int *>(
  7705. malloc(sizeof(int) * static_cast<size_t>(header.num_channels)));
  7706. header.requested_pixel_types = static_cast<int *>(
  7707. malloc(sizeof(int) * static_cast<size_t>(header.num_channels)));
  7708. for (int i = 0; i < header.num_channels; i++) {
  7709. header.pixel_types[i] =
  7710. TINYEXR_PIXELTYPE_FLOAT; // pixel type of input image
  7711. if (save_as_fp16 > 0) {
  7712. header.requested_pixel_types[i] =
  7713. TINYEXR_PIXELTYPE_HALF; // save with half(fp16) pixel format
  7714. } else {
  7715. header.requested_pixel_types[i] =
  7716. TINYEXR_PIXELTYPE_FLOAT; // save with float(fp32) pixel format(i.e.
  7717. // no precision reduction)
  7718. }
  7719. }
  7720. unsigned char *mem_buf;
  7721. size_t mem_size = SaveEXRImageToMemory(&image, &header, &mem_buf, err);
  7722. if (mem_size == 0) {
  7723. return TINYEXR_ERROR_SERIALIZATION_FAILED;
  7724. }
  7725. free(header.channels);
  7726. free(header.pixel_types);
  7727. free(header.requested_pixel_types);
  7728. if (mem_size > size_t(std::numeric_limits<int>::max())) {
  7729. free(mem_buf);
  7730. return TINYEXR_ERROR_DATA_TOO_LARGE;
  7731. }
  7732. (*outbuf) = mem_buf;
  7733. return int(mem_size);
  7734. }
  7735. int SaveEXR(const float *data, int width, int height, int components,
  7736. const int save_as_fp16, const char *outfilename, const char **err) {
  7737. if ((components == 1) || components == 3 || components == 4) {
  7738. // OK
  7739. } else {
  7740. std::stringstream ss;
  7741. ss << "Unsupported component value : " << components << std::endl;
  7742. tinyexr::SetErrorMessage(ss.str(), err);
  7743. return TINYEXR_ERROR_INVALID_ARGUMENT;
  7744. }
  7745. EXRHeader header;
  7746. InitEXRHeader(&header);
  7747. if ((width < 16) && (height < 16)) {
  7748. // No compression for small image.
  7749. header.compression_type = TINYEXR_COMPRESSIONTYPE_NONE;
  7750. } else {
  7751. header.compression_type = TINYEXR_COMPRESSIONTYPE_ZIP;
  7752. }
  7753. EXRImage image;
  7754. InitEXRImage(&image);
  7755. image.num_channels = components;
  7756. std::vector<float> images[4];
  7757. const size_t pixel_count =
  7758. static_cast<size_t>(width) * static_cast<size_t>(height);
  7759. if (components == 1) {
  7760. images[0].resize(pixel_count);
  7761. memcpy(images[0].data(), data, sizeof(float) * pixel_count);
  7762. } else {
  7763. images[0].resize(pixel_count);
  7764. images[1].resize(pixel_count);
  7765. images[2].resize(pixel_count);
  7766. images[3].resize(pixel_count);
  7767. // Split RGB(A)RGB(A)RGB(A)... into R, G and B(and A) layers
  7768. for (size_t i = 0; i < pixel_count; i++) {
  7769. images[0][i] = data[static_cast<size_t>(components) * i + 0];
  7770. images[1][i] = data[static_cast<size_t>(components) * i + 1];
  7771. images[2][i] = data[static_cast<size_t>(components) * i + 2];
  7772. if (components == 4) {
  7773. images[3][i] = data[static_cast<size_t>(components) * i + 3];
  7774. }
  7775. }
  7776. }
  7777. float *image_ptr[4] = {0, 0, 0, 0};
  7778. if (components == 4) {
  7779. image_ptr[0] = &(images[3].at(0)); // A
  7780. image_ptr[1] = &(images[2].at(0)); // B
  7781. image_ptr[2] = &(images[1].at(0)); // G
  7782. image_ptr[3] = &(images[0].at(0)); // R
  7783. } else if (components == 3) {
  7784. image_ptr[0] = &(images[2].at(0)); // B
  7785. image_ptr[1] = &(images[1].at(0)); // G
  7786. image_ptr[2] = &(images[0].at(0)); // R
  7787. } else if (components == 1) {
  7788. image_ptr[0] = &(images[0].at(0)); // A
  7789. }
  7790. image.images = reinterpret_cast<unsigned char **>(image_ptr);
  7791. image.width = width;
  7792. image.height = height;
  7793. header.num_channels = components;
  7794. header.channels = static_cast<EXRChannelInfo *>(malloc(
  7795. sizeof(EXRChannelInfo) * static_cast<size_t>(header.num_channels)));
  7796. // Must be (A)BGR order, since most of EXR viewers expect this channel order.
  7797. if (components == 4) {
  7798. #ifdef _MSC_VER
  7799. strncpy_s(header.channels[0].name, "A", 255);
  7800. strncpy_s(header.channels[1].name, "B", 255);
  7801. strncpy_s(header.channels[2].name, "G", 255);
  7802. strncpy_s(header.channels[3].name, "R", 255);
  7803. #else
  7804. strncpy(header.channels[0].name, "A", 255);
  7805. strncpy(header.channels[1].name, "B", 255);
  7806. strncpy(header.channels[2].name, "G", 255);
  7807. strncpy(header.channels[3].name, "R", 255);
  7808. #endif
  7809. header.channels[0].name[strlen("A")] = '\0';
  7810. header.channels[1].name[strlen("B")] = '\0';
  7811. header.channels[2].name[strlen("G")] = '\0';
  7812. header.channels[3].name[strlen("R")] = '\0';
  7813. } else if (components == 3) {
  7814. #ifdef _MSC_VER
  7815. strncpy_s(header.channels[0].name, "B", 255);
  7816. strncpy_s(header.channels[1].name, "G", 255);
  7817. strncpy_s(header.channels[2].name, "R", 255);
  7818. #else
  7819. strncpy(header.channels[0].name, "B", 255);
  7820. strncpy(header.channels[1].name, "G", 255);
  7821. strncpy(header.channels[2].name, "R", 255);
  7822. #endif
  7823. header.channels[0].name[strlen("B")] = '\0';
  7824. header.channels[1].name[strlen("G")] = '\0';
  7825. header.channels[2].name[strlen("R")] = '\0';
  7826. } else {
  7827. #ifdef _MSC_VER
  7828. strncpy_s(header.channels[0].name, "A", 255);
  7829. #else
  7830. strncpy(header.channels[0].name, "A", 255);
  7831. #endif
  7832. header.channels[0].name[strlen("A")] = '\0';
  7833. }
  7834. header.pixel_types = static_cast<int *>(
  7835. malloc(sizeof(int) * static_cast<size_t>(header.num_channels)));
  7836. header.requested_pixel_types = static_cast<int *>(
  7837. malloc(sizeof(int) * static_cast<size_t>(header.num_channels)));
  7838. for (int i = 0; i < header.num_channels; i++) {
  7839. header.pixel_types[i] =
  7840. TINYEXR_PIXELTYPE_FLOAT; // pixel type of input image
  7841. if (save_as_fp16 > 0) {
  7842. header.requested_pixel_types[i] =
  7843. TINYEXR_PIXELTYPE_HALF; // save with half(fp16) pixel format
  7844. } else {
  7845. header.requested_pixel_types[i] =
  7846. TINYEXR_PIXELTYPE_FLOAT; // save with float(fp32) pixel format(i.e.
  7847. // no precision reduction)
  7848. }
  7849. }
  7850. int ret = SaveEXRImageToFile(&image, &header, outfilename, err);
  7851. if (ret != TINYEXR_SUCCESS) {
  7852. return ret;
  7853. }
  7854. free(header.channels);
  7855. free(header.pixel_types);
  7856. free(header.requested_pixel_types);
  7857. return ret;
  7858. }
  7859. #ifdef __clang__
  7860. // zero-as-null-pointer-constant
  7861. #pragma clang diagnostic pop
  7862. #endif
  7863. #endif // TINYEXR_IMPLEMENTATION_DEFINED
  7864. #endif // TINYEXR_IMPLEMENTATION