stb_image.c 199 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925
  1. // Modified by Lasse Oorni for Urho3D
  2. #include "stb_image.h"
  3. #define STB_IMAGE_IMPLEMENTATION
  4. #ifdef STB_IMAGE_IMPLEMENTATION
  5. #if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \
  6. || defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \
  7. || defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \
  8. || defined(STBI_ONLY_ZLIB)
  9. #ifndef STBI_ONLY_JPEG
  10. #define STBI_NO_JPEG
  11. #endif
  12. #ifndef STBI_ONLY_PNG
  13. #define STBI_NO_PNG
  14. #endif
  15. #ifndef STBI_ONLY_BMP
  16. #define STBI_NO_BMP
  17. #endif
  18. #ifndef STBI_ONLY_PSD
  19. #define STBI_NO_PSD
  20. #endif
  21. #ifndef STBI_ONLY_TGA
  22. #define STBI_NO_TGA
  23. #endif
  24. #ifndef STBI_ONLY_GIF
  25. #define STBI_NO_GIF
  26. #endif
  27. #ifndef STBI_ONLY_HDR
  28. #define STBI_NO_HDR
  29. #endif
  30. #ifndef STBI_ONLY_PIC
  31. #define STBI_NO_PIC
  32. #endif
  33. #ifndef STBI_ONLY_PNM
  34. #define STBI_NO_PNM
  35. #endif
  36. #endif
  37. #if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB)
  38. #define STBI_NO_ZLIB
  39. #endif
  40. #include <stdarg.h>
  41. #include <stddef.h> // ptrdiff_t on osx
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
  45. #include <math.h> // ldexp
  46. #endif
  47. #ifndef STBI_NO_STDIO
  48. #include <stdio.h>
  49. #endif
  50. #ifndef STBI_ASSERT
  51. #include <assert.h>
  52. #define STBI_ASSERT(x) assert(x)
  53. #endif
  54. #ifndef _MSC_VER
  55. #ifdef __cplusplus
  56. #define stbi_inline inline
  57. #else
  58. #define stbi_inline
  59. #endif
  60. #else
  61. #define stbi_inline __forceinline
  62. #endif
  63. #ifdef _MSC_VER
  64. typedef unsigned short stbi__uint16;
  65. typedef signed short stbi__int16;
  66. typedef unsigned int stbi__uint32;
  67. typedef signed int stbi__int32;
  68. #else
  69. #include <stdint.h>
  70. typedef uint16_t stbi__uint16;
  71. typedef int16_t stbi__int16;
  72. typedef uint32_t stbi__uint32;
  73. typedef int32_t stbi__int32;
  74. #endif
  75. // should produce compiler error if size is wrong
  76. typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
  77. #ifdef _MSC_VER
  78. #define STBI_NOTUSED(v) (void)(v)
  79. #else
  80. #define STBI_NOTUSED(v) (void)sizeof(v)
  81. #endif
  82. #ifdef _MSC_VER
  83. #define STBI_HAS_LROTL
  84. #endif
  85. #ifdef STBI_HAS_LROTL
  86. #define stbi_lrot(x,y) _lrotl(x,y)
  87. #else
  88. #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
  89. #endif
  90. #if defined(STBI_MALLOC) && defined(STBI_FREE) && defined(STBI_REALLOC)
  91. // ok
  92. #elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC)
  93. // ok
  94. #else
  95. #error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC."
  96. #endif
  97. #ifndef STBI_MALLOC
  98. #define STBI_MALLOC(sz) malloc(sz)
  99. #define STBI_REALLOC(p,sz) realloc(p,sz)
  100. #define STBI_FREE(p) free(p)
  101. #endif
  102. // x86/x64 detection
  103. #if defined(__x86_64__) || defined(_M_X64)
  104. #define STBI__X64_TARGET
  105. #elif defined(__i386) || defined(_M_IX86)
  106. #define STBI__X86_TARGET
  107. #endif
  108. // Urho3D: do not use SIMD instructions if URHO3D_SSE disabled
  109. #ifndef URHO3D_SSE
  110. #define STBI_NO_SIMD
  111. #endif
  112. #if defined(__GNUC__) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET)) && !defined(__SSE2__) && !defined(STBI_NO_SIMD)
  113. // NOTE: not clear do we actually need this for the 64-bit path?
  114. // gcc doesn't support sse2 intrinsics unless you compile with -msse2,
  115. // (but compiling with -msse2 allows the compiler to use SSE2 everywhere;
  116. // this is just broken and gcc are jerks for not fixing it properly
  117. // http://www.virtualdub.org/blog/pivot/entry.php?id=363 )
  118. #define STBI_NO_SIMD
  119. #endif
  120. #if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD)
  121. // Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET
  122. //
  123. // 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the
  124. // Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant.
  125. // As a result, enabling SSE2 on 32-bit MinGW is dangerous when not
  126. // simultaneously enabling "-mstackrealign".
  127. //
  128. // See https://github.com/nothings/stb/issues/81 for more information.
  129. //
  130. // So default to no SSE2 on 32-bit MinGW. If you've read this far and added
  131. // -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2.
  132. #define STBI_NO_SIMD
  133. #endif
  134. #if !defined(STBI_NO_SIMD) && defined(STBI__X86_TARGET)
  135. #define STBI_SSE2
  136. #include <emmintrin.h>
  137. #ifdef _MSC_VER
  138. #if _MSC_VER >= 1400 // not VC6
  139. #include <intrin.h> // __cpuid
  140. static int stbi__cpuid3(void)
  141. {
  142. int info[4];
  143. __cpuid(info,1);
  144. return info[3];
  145. }
  146. #else
  147. static int stbi__cpuid3(void)
  148. {
  149. int res;
  150. __asm {
  151. mov eax,1
  152. cpuid
  153. mov res,edx
  154. }
  155. return res;
  156. }
  157. #endif
  158. #define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
  159. static int stbi__sse2_available()
  160. {
  161. int info3 = stbi__cpuid3();
  162. return ((info3 >> 26) & 1) != 0;
  163. }
  164. #else // assume GCC-style if not VC++
  165. #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
  166. static int stbi__sse2_available()
  167. {
  168. #if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 408 // GCC 4.8 or later
  169. // GCC 4.8+ has a nice way to do this
  170. return __builtin_cpu_supports("sse2");
  171. #else
  172. // portable way to do this, preferably without using GCC inline ASM?
  173. // just bail for now.
  174. return 0;
  175. #endif
  176. }
  177. #endif
  178. #endif
  179. // ARM NEON
  180. #if defined(STBI_NO_SIMD) && defined(STBI_NEON)
  181. #undef STBI_NEON
  182. #endif
  183. #ifdef STBI_NEON
  184. #include <arm_neon.h>
  185. // assume GCC or Clang on ARM targets
  186. #define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
  187. #endif
  188. #ifndef STBI_SIMD_ALIGN
  189. #define STBI_SIMD_ALIGN(type, name) type name
  190. #endif
  191. ///////////////////////////////////////////////
  192. //
  193. // stbi__context struct and start_xxx functions
  194. // stbi__context structure is our basic context used by all images, so it
  195. // contains all the IO context, plus some basic image information
  196. typedef struct
  197. {
  198. stbi__uint32 img_x, img_y;
  199. int img_n, img_out_n;
  200. stbi_io_callbacks io;
  201. void *io_user_data;
  202. int read_from_callbacks;
  203. int buflen;
  204. stbi_uc buffer_start[128];
  205. stbi_uc *img_buffer, *img_buffer_end;
  206. stbi_uc *img_buffer_original;
  207. } stbi__context;
  208. static void stbi__refill_buffer(stbi__context *s);
  209. // initialize a memory-decode context
  210. static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len)
  211. {
  212. s->io.read = NULL;
  213. s->read_from_callbacks = 0;
  214. s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer;
  215. s->img_buffer_end = (stbi_uc *) buffer+len;
  216. }
  217. // initialize a callback-based context
  218. static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user)
  219. {
  220. s->io = *c;
  221. s->io_user_data = user;
  222. s->buflen = sizeof(s->buffer_start);
  223. s->read_from_callbacks = 1;
  224. s->img_buffer_original = s->buffer_start;
  225. stbi__refill_buffer(s);
  226. }
  227. #ifndef STBI_NO_STDIO
  228. static int stbi__stdio_read(void *user, char *data, int size)
  229. {
  230. return (int) fread(data,1,size,(FILE*) user);
  231. }
  232. static void stbi__stdio_skip(void *user, int n)
  233. {
  234. fseek((FILE*) user, n, SEEK_CUR);
  235. }
  236. static int stbi__stdio_eof(void *user)
  237. {
  238. return feof((FILE*) user);
  239. }
  240. static stbi_io_callbacks stbi__stdio_callbacks =
  241. {
  242. stbi__stdio_read,
  243. stbi__stdio_skip,
  244. stbi__stdio_eof,
  245. };
  246. static void stbi__start_file(stbi__context *s, FILE *f)
  247. {
  248. stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f);
  249. }
  250. //static void stop_file(stbi__context *s) { }
  251. #endif // !STBI_NO_STDIO
  252. static void stbi__rewind(stbi__context *s)
  253. {
  254. // conceptually rewind SHOULD rewind to the beginning of the stream,
  255. // but we just rewind to the beginning of the initial buffer, because
  256. // we only use it after doing 'test', which only ever looks at at most 92 bytes
  257. s->img_buffer = s->img_buffer_original;
  258. }
  259. #ifndef STBI_NO_JPEG
  260. static int stbi__jpeg_test(stbi__context *s);
  261. static stbi_uc *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  262. static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp);
  263. #endif
  264. #ifndef STBI_NO_PNG
  265. static int stbi__png_test(stbi__context *s);
  266. static stbi_uc *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  267. static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);
  268. #endif
  269. #ifndef STBI_NO_BMP
  270. static int stbi__bmp_test(stbi__context *s);
  271. static stbi_uc *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  272. static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp);
  273. #endif
  274. #ifndef STBI_NO_TGA
  275. static int stbi__tga_test(stbi__context *s);
  276. static stbi_uc *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  277. static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp);
  278. #endif
  279. #ifndef STBI_NO_PSD
  280. static int stbi__psd_test(stbi__context *s);
  281. static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  282. static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp);
  283. #endif
  284. #ifndef STBI_NO_HDR
  285. static int stbi__hdr_test(stbi__context *s);
  286. static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  287. static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp);
  288. #endif
  289. #ifndef STBI_NO_PIC
  290. static int stbi__pic_test(stbi__context *s);
  291. static stbi_uc *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  292. static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp);
  293. #endif
  294. #ifndef STBI_NO_GIF
  295. static int stbi__gif_test(stbi__context *s);
  296. static stbi_uc *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  297. static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);
  298. #endif
  299. #ifndef STBI_NO_PNM
  300. static int stbi__pnm_test(stbi__context *s);
  301. static stbi_uc *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp);
  302. static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);
  303. #endif
  304. // this is not threadsafe
  305. static const char *stbi__g_failure_reason;
  306. STBIDEF const char *stbi_failure_reason(void)
  307. {
  308. return stbi__g_failure_reason;
  309. }
  310. static int stbi__err(const char *str)
  311. {
  312. stbi__g_failure_reason = str;
  313. return 0;
  314. }
  315. static void *stbi__malloc(size_t size)
  316. {
  317. return STBI_MALLOC(size);
  318. }
  319. // stbi__err - error
  320. // stbi__errpf - error returning pointer to float
  321. // stbi__errpuc - error returning pointer to unsigned char
  322. #ifdef STBI_NO_FAILURE_STRINGS
  323. #define stbi__err(x,y) 0
  324. #elif defined(STBI_FAILURE_USERMSG)
  325. #define stbi__err(x,y) stbi__err(y)
  326. #else
  327. #define stbi__err(x,y) stbi__err(x)
  328. #endif
  329. #define stbi__errpf(x,y) ((float *) (stbi__err(x,y)?NULL:NULL))
  330. #define stbi__errpuc(x,y) ((unsigned char *) (stbi__err(x,y)?NULL:NULL))
  331. STBIDEF void stbi_image_free(void *retval_from_stbi_load)
  332. {
  333. STBI_FREE(retval_from_stbi_load);
  334. }
  335. #ifndef STBI_NO_LINEAR
  336. static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
  337. #endif
  338. #ifndef STBI_NO_HDR
  339. static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);
  340. #endif
  341. static int stbi__vertically_flip_on_load = 0;
  342. STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)
  343. {
  344. stbi__vertically_flip_on_load = flag_true_if_should_flip;
  345. }
  346. static unsigned char *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  347. {
  348. #ifndef STBI_NO_JPEG
  349. if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp);
  350. #endif
  351. #ifndef STBI_NO_PNG
  352. if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp);
  353. #endif
  354. #ifndef STBI_NO_BMP
  355. if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp);
  356. #endif
  357. #ifndef STBI_NO_GIF
  358. if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp);
  359. #endif
  360. #ifndef STBI_NO_PSD
  361. if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp);
  362. #endif
  363. #ifndef STBI_NO_PIC
  364. if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp);
  365. #endif
  366. #ifndef STBI_NO_PNM
  367. if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp);
  368. #endif
  369. #ifndef STBI_NO_HDR
  370. if (stbi__hdr_test(s)) {
  371. float *hdr = stbi__hdr_load(s, x,y,comp,req_comp);
  372. return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
  373. }
  374. #endif
  375. #ifndef STBI_NO_TGA
  376. // test tga last because it's a crappy test!
  377. if (stbi__tga_test(s))
  378. return stbi__tga_load(s,x,y,comp,req_comp);
  379. #endif
  380. return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt");
  381. }
  382. static unsigned char *stbi__load_flip(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  383. {
  384. unsigned char *result = stbi__load_main(s, x, y, comp, req_comp);
  385. if (stbi__vertically_flip_on_load && result != NULL) {
  386. int w = *x, h = *y;
  387. int depth = req_comp ? req_comp : *comp;
  388. int row,col,z;
  389. stbi_uc temp;
  390. // @OPTIMIZE: use a bigger temp buffer and memcpy multiple pixels at once
  391. for (row = 0; row < (h>>1); row++) {
  392. for (col = 0; col < w; col++) {
  393. for (z = 0; z < depth; z++) {
  394. temp = result[(row * w + col) * depth + z];
  395. result[(row * w + col) * depth + z] = result[((h - row - 1) * w + col) * depth + z];
  396. result[((h - row - 1) * w + col) * depth + z] = temp;
  397. }
  398. }
  399. }
  400. }
  401. return result;
  402. }
  403. static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)
  404. {
  405. if (stbi__vertically_flip_on_load && result != NULL) {
  406. int w = *x, h = *y;
  407. int depth = req_comp ? req_comp : *comp;
  408. int row,col,z;
  409. float temp;
  410. // @OPTIMIZE: use a bigger temp buffer and memcpy multiple pixels at once
  411. for (row = 0; row < (h>>1); row++) {
  412. for (col = 0; col < w; col++) {
  413. for (z = 0; z < depth; z++) {
  414. temp = result[(row * w + col) * depth + z];
  415. result[(row * w + col) * depth + z] = result[((h - row - 1) * w + col) * depth + z];
  416. result[((h - row - 1) * w + col) * depth + z] = temp;
  417. }
  418. }
  419. }
  420. }
  421. }
  422. #ifndef STBI_NO_STDIO
  423. static FILE *stbi__fopen(char const *filename, char const *mode)
  424. {
  425. FILE *f;
  426. #if defined(_MSC_VER) && _MSC_VER >= 1400
  427. if (0 != fopen_s(&f, filename, mode))
  428. f=0;
  429. #else
  430. f = fopen(filename, mode);
  431. #endif
  432. return f;
  433. }
  434. STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
  435. {
  436. FILE *f = stbi__fopen(filename, "rb");
  437. unsigned char *result;
  438. if (!f) return stbi__errpuc("can't fopen", "Unable to open file");
  439. result = stbi_load_from_file(f,x,y,comp,req_comp);
  440. fclose(f);
  441. return result;
  442. }
  443. STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  444. {
  445. unsigned char *result;
  446. stbi__context s;
  447. stbi__start_file(&s,f);
  448. result = stbi__load_flip(&s,x,y,comp,req_comp);
  449. if (result) {
  450. // need to 'unget' all the characters in the IO buffer
  451. fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
  452. }
  453. return result;
  454. }
  455. #endif //!STBI_NO_STDIO
  456. STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  457. {
  458. stbi__context s;
  459. stbi__start_mem(&s,buffer,len);
  460. return stbi__load_flip(&s,x,y,comp,req_comp);
  461. }
  462. STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
  463. {
  464. stbi__context s;
  465. stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
  466. return stbi__load_flip(&s,x,y,comp,req_comp);
  467. }
  468. #ifndef STBI_NO_LINEAR
  469. static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  470. {
  471. unsigned char *data;
  472. #ifndef STBI_NO_HDR
  473. if (stbi__hdr_test(s)) {
  474. float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp);
  475. if (hdr_data)
  476. stbi__float_postprocess(hdr_data,x,y,comp,req_comp);
  477. return hdr_data;
  478. }
  479. #endif
  480. data = stbi__load_flip(s, x, y, comp, req_comp);
  481. if (data)
  482. return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
  483. return stbi__errpf("unknown image type", "Image not of any known type, or corrupt");
  484. }
  485. STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  486. {
  487. stbi__context s;
  488. stbi__start_mem(&s,buffer,len);
  489. return stbi__loadf_main(&s,x,y,comp,req_comp);
  490. }
  491. STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
  492. {
  493. stbi__context s;
  494. stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
  495. return stbi__loadf_main(&s,x,y,comp,req_comp);
  496. }
  497. #ifndef STBI_NO_STDIO
  498. STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
  499. {
  500. float *result;
  501. FILE *f = stbi__fopen(filename, "rb");
  502. if (!f) return stbi__errpf("can't fopen", "Unable to open file");
  503. result = stbi_loadf_from_file(f,x,y,comp,req_comp);
  504. fclose(f);
  505. return result;
  506. }
  507. STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  508. {
  509. stbi__context s;
  510. stbi__start_file(&s,f);
  511. return stbi__loadf_main(&s,x,y,comp,req_comp);
  512. }
  513. #endif // !STBI_NO_STDIO
  514. #endif // !STBI_NO_LINEAR
  515. // these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is
  516. // defined, for API simplicity; if STBI_NO_LINEAR is defined, it always
  517. // reports false!
  518. STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
  519. {
  520. #ifndef STBI_NO_HDR
  521. stbi__context s;
  522. stbi__start_mem(&s,buffer,len);
  523. return stbi__hdr_test(&s);
  524. #else
  525. STBI_NOTUSED(buffer);
  526. STBI_NOTUSED(len);
  527. return 0;
  528. #endif
  529. }
  530. #ifndef STBI_NO_STDIO
  531. STBIDEF int stbi_is_hdr (char const *filename)
  532. {
  533. FILE *f = stbi__fopen(filename, "rb");
  534. int result=0;
  535. if (f) {
  536. result = stbi_is_hdr_from_file(f);
  537. fclose(f);
  538. }
  539. return result;
  540. }
  541. STBIDEF int stbi_is_hdr_from_file(FILE *f)
  542. {
  543. #ifndef STBI_NO_HDR
  544. stbi__context s;
  545. stbi__start_file(&s,f);
  546. return stbi__hdr_test(&s);
  547. #else
  548. return 0;
  549. #endif
  550. }
  551. #endif // !STBI_NO_STDIO
  552. STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
  553. {
  554. #ifndef STBI_NO_HDR
  555. stbi__context s;
  556. stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
  557. return stbi__hdr_test(&s);
  558. #else
  559. return 0;
  560. #endif
  561. }
  562. static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;
  563. static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;
  564. #ifndef STBI_NO_LINEAR
  565. STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; }
  566. STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; }
  567. #endif
  568. STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; }
  569. STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; }
  570. //////////////////////////////////////////////////////////////////////////////
  571. //
  572. // Common code used by all image loaders
  573. //
  574. enum
  575. {
  576. STBI__SCAN_load=0,
  577. STBI__SCAN_type,
  578. STBI__SCAN_header
  579. };
  580. static void stbi__refill_buffer(stbi__context *s)
  581. {
  582. int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);
  583. if (n == 0) {
  584. // at end of file, treat same as if from memory, but need to handle case
  585. // where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file
  586. s->read_from_callbacks = 0;
  587. s->img_buffer = s->buffer_start;
  588. s->img_buffer_end = s->buffer_start+1;
  589. *s->img_buffer = 0;
  590. } else {
  591. s->img_buffer = s->buffer_start;
  592. s->img_buffer_end = s->buffer_start + n;
  593. }
  594. }
  595. stbi_inline static stbi_uc stbi__get8(stbi__context *s)
  596. {
  597. if (s->img_buffer < s->img_buffer_end)
  598. return *s->img_buffer++;
  599. if (s->read_from_callbacks) {
  600. stbi__refill_buffer(s);
  601. return *s->img_buffer++;
  602. }
  603. return 0;
  604. }
  605. stbi_inline static int stbi__at_eof(stbi__context *s)
  606. {
  607. if (s->io.read) {
  608. if (!(s->io.eof)(s->io_user_data)) return 0;
  609. // if feof() is true, check if buffer = end
  610. // special case: we've only got the special 0 character at the end
  611. if (s->read_from_callbacks == 0) return 1;
  612. }
  613. return s->img_buffer >= s->img_buffer_end;
  614. }
  615. static void stbi__skip(stbi__context *s, int n)
  616. {
  617. if (n < 0) {
  618. s->img_buffer = s->img_buffer_end;
  619. return;
  620. }
  621. if (s->io.read) {
  622. int blen = (int) (s->img_buffer_end - s->img_buffer);
  623. if (blen < n) {
  624. s->img_buffer = s->img_buffer_end;
  625. (s->io.skip)(s->io_user_data, n - blen);
  626. return;
  627. }
  628. }
  629. s->img_buffer += n;
  630. }
  631. static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
  632. {
  633. if (s->io.read) {
  634. int blen = (int) (s->img_buffer_end - s->img_buffer);
  635. if (blen < n) {
  636. int res, count;
  637. memcpy(buffer, s->img_buffer, blen);
  638. count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen);
  639. res = (count == (n-blen));
  640. s->img_buffer = s->img_buffer_end;
  641. return res;
  642. }
  643. }
  644. if (s->img_buffer+n <= s->img_buffer_end) {
  645. memcpy(buffer, s->img_buffer, n);
  646. s->img_buffer += n;
  647. return 1;
  648. } else
  649. return 0;
  650. }
  651. static int stbi__get16be(stbi__context *s)
  652. {
  653. int z = stbi__get8(s);
  654. return (z << 8) + stbi__get8(s);
  655. }
  656. static stbi__uint32 stbi__get32be(stbi__context *s)
  657. {
  658. stbi__uint32 z = stbi__get16be(s);
  659. return (z << 16) + stbi__get16be(s);
  660. }
  661. static int stbi__get16le(stbi__context *s)
  662. {
  663. int z = stbi__get8(s);
  664. return z + (stbi__get8(s) << 8);
  665. }
  666. static stbi__uint32 stbi__get32le(stbi__context *s)
  667. {
  668. stbi__uint32 z = stbi__get16le(s);
  669. return z + (stbi__get16le(s) << 16);
  670. }
  671. #define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings
  672. //////////////////////////////////////////////////////////////////////////////
  673. //
  674. // generic converter from built-in img_n to req_comp
  675. // individual types do this automatically as much as possible (e.g. jpeg
  676. // does all cases internally since it needs to colorspace convert anyway,
  677. // and it never has alpha, so very few cases ). png can automatically
  678. // interleave an alpha=255 channel, but falls back to this for other cases
  679. //
  680. // assume data buffer is malloced, so malloc a new one and free that one
  681. // only failure mode is malloc failing
  682. static stbi_uc stbi__compute_y(int r, int g, int b)
  683. {
  684. return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8);
  685. }
  686. static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
  687. {
  688. int i,j;
  689. unsigned char *good;
  690. if (req_comp == img_n) return data;
  691. STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
  692. good = (unsigned char *) stbi__malloc(req_comp * x * y);
  693. if (good == NULL) {
  694. STBI_FREE(data);
  695. return stbi__errpuc("outofmem", "Out of memory");
  696. }
  697. for (j=0; j < (int) y; ++j) {
  698. unsigned char *src = data + j * x * img_n ;
  699. unsigned char *dest = good + j * x * req_comp;
  700. #define COMBO(a,b) ((a)*8+(b))
  701. #define CASE(a,b) case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
  702. // convert source image with img_n components to one with req_comp components;
  703. // avoid switch per pixel, so use switch per scanline and massive macros
  704. switch (COMBO(img_n, req_comp)) {
  705. CASE(1,2) dest[0]=src[0], dest[1]=255; break;
  706. CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break;
  707. CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break;
  708. CASE(2,1) dest[0]=src[0]; break;
  709. CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break;
  710. CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break;
  711. CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break;
  712. CASE(3,1) dest[0]=stbi__compute_y(src[0],src[1],src[2]); break;
  713. CASE(3,2) dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; break;
  714. CASE(4,1) dest[0]=stbi__compute_y(src[0],src[1],src[2]); break;
  715. CASE(4,2) dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break;
  716. CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break;
  717. default: STBI_ASSERT(0);
  718. }
  719. #undef CASE
  720. }
  721. STBI_FREE(data);
  722. return good;
  723. }
  724. #ifndef STBI_NO_LINEAR
  725. static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
  726. {
  727. int i,k,n;
  728. float *output = (float *) stbi__malloc(x * y * comp * sizeof(float));
  729. if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); }
  730. // compute number of non-alpha components
  731. if (comp & 1) n = comp; else n = comp-1;
  732. for (i=0; i < x*y; ++i) {
  733. for (k=0; k < n; ++k) {
  734. output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
  735. }
  736. if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
  737. }
  738. STBI_FREE(data);
  739. return output;
  740. }
  741. #endif
  742. #ifndef STBI_NO_HDR
  743. #define stbi__float2int(x) ((int) (x))
  744. static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp)
  745. {
  746. int i,k,n;
  747. stbi_uc *output = (stbi_uc *) stbi__malloc(x * y * comp);
  748. if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); }
  749. // compute number of non-alpha components
  750. if (comp & 1) n = comp; else n = comp-1;
  751. for (i=0; i < x*y; ++i) {
  752. for (k=0; k < n; ++k) {
  753. float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;
  754. if (z < 0) z = 0;
  755. if (z > 255) z = 255;
  756. output[i*comp + k] = (stbi_uc) stbi__float2int(z);
  757. }
  758. if (k < comp) {
  759. float z = data[i*comp+k] * 255 + 0.5f;
  760. if (z < 0) z = 0;
  761. if (z > 255) z = 255;
  762. output[i*comp + k] = (stbi_uc) stbi__float2int(z);
  763. }
  764. }
  765. STBI_FREE(data);
  766. return output;
  767. }
  768. #endif
  769. //////////////////////////////////////////////////////////////////////////////
  770. //
  771. // "baseline" JPEG/JFIF decoder
  772. //
  773. // simple implementation
  774. // - doesn't support delayed output of y-dimension
  775. // - simple interface (only one output format: 8-bit interleaved RGB)
  776. // - doesn't try to recover corrupt jpegs
  777. // - doesn't allow partial loading, loading multiple at once
  778. // - still fast on x86 (copying globals into locals doesn't help x86)
  779. // - allocates lots of intermediate memory (full size of all components)
  780. // - non-interleaved case requires this anyway
  781. // - allows good upsampling (see next)
  782. // high-quality
  783. // - upsampled channels are bilinearly interpolated, even across blocks
  784. // - quality integer IDCT derived from IJG's 'slow'
  785. // performance
  786. // - fast huffman; reasonable integer IDCT
  787. // - some SIMD kernels for common paths on targets with SSE2/NEON
  788. // - uses a lot of intermediate memory, could cache poorly
  789. #ifndef STBI_NO_JPEG
  790. // huffman decoding acceleration
  791. #define FAST_BITS 9 // larger handles more cases; smaller stomps less cache
  792. typedef struct
  793. {
  794. stbi_uc fast[1 << FAST_BITS];
  795. // weirdly, repacking this into AoS is a 10% speed loss, instead of a win
  796. stbi__uint16 code[256];
  797. stbi_uc values[256];
  798. stbi_uc size[257];
  799. unsigned int maxcode[18];
  800. int delta[17]; // old 'firstsymbol' - old 'firstcode'
  801. } stbi__huffman;
  802. typedef struct
  803. {
  804. stbi__context *s;
  805. stbi__huffman huff_dc[4];
  806. stbi__huffman huff_ac[4];
  807. stbi_uc dequant[4][64];
  808. stbi__int16 fast_ac[4][1 << FAST_BITS];
  809. // sizes for components, interleaved MCUs
  810. int img_h_max, img_v_max;
  811. int img_mcu_x, img_mcu_y;
  812. int img_mcu_w, img_mcu_h;
  813. // definition of jpeg image component
  814. struct
  815. {
  816. int id;
  817. int h,v;
  818. int tq;
  819. int hd,ha;
  820. int dc_pred;
  821. int x,y,w2,h2;
  822. stbi_uc *data;
  823. void *raw_data, *raw_coeff;
  824. stbi_uc *linebuf;
  825. short *coeff; // progressive only
  826. int coeff_w, coeff_h; // number of 8x8 coefficient blocks
  827. } img_comp[4];
  828. stbi__uint32 code_buffer; // jpeg entropy-coded buffer
  829. int code_bits; // number of valid bits
  830. unsigned char marker; // marker seen while filling entropy buffer
  831. int nomore; // flag if we saw a marker so must stop
  832. int progressive;
  833. int spec_start;
  834. int spec_end;
  835. int succ_high;
  836. int succ_low;
  837. int eob_run;
  838. int scan_n, order[4];
  839. int restart_interval, todo;
  840. // kernels
  841. void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]);
  842. void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step);
  843. stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs);
  844. } stbi__jpeg;
  845. static int stbi__build_huffman(stbi__huffman *h, int *count)
  846. {
  847. int i,j,k=0,code;
  848. // build size list for each symbol (from JPEG spec)
  849. for (i=0; i < 16; ++i)
  850. for (j=0; j < count[i]; ++j)
  851. h->size[k++] = (stbi_uc) (i+1);
  852. h->size[k] = 0;
  853. // compute actual symbols (from jpeg spec)
  854. code = 0;
  855. k = 0;
  856. for(j=1; j <= 16; ++j) {
  857. // compute delta to add to code to compute symbol id
  858. h->delta[j] = k - code;
  859. if (h->size[k] == j) {
  860. while (h->size[k] == j)
  861. h->code[k++] = (stbi__uint16) (code++);
  862. if (code-1 >= (1 << j)) return stbi__err("bad code lengths","Corrupt JPEG");
  863. }
  864. // compute largest code + 1 for this size, preshifted as needed later
  865. h->maxcode[j] = code << (16-j);
  866. code <<= 1;
  867. }
  868. h->maxcode[j] = 0xffffffff;
  869. // build non-spec acceleration table; 255 is flag for not-accelerated
  870. memset(h->fast, 255, 1 << FAST_BITS);
  871. for (i=0; i < k; ++i) {
  872. int s = h->size[i];
  873. if (s <= FAST_BITS) {
  874. int c = h->code[i] << (FAST_BITS-s);
  875. int m = 1 << (FAST_BITS-s);
  876. for (j=0; j < m; ++j) {
  877. h->fast[c+j] = (stbi_uc) i;
  878. }
  879. }
  880. }
  881. return 1;
  882. }
  883. // build a table that decodes both magnitude and value of small ACs in
  884. // one go.
  885. static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h)
  886. {
  887. int i;
  888. for (i=0; i < (1 << FAST_BITS); ++i) {
  889. stbi_uc fast = h->fast[i];
  890. fast_ac[i] = 0;
  891. if (fast < 255) {
  892. int rs = h->values[fast];
  893. int run = (rs >> 4) & 15;
  894. int magbits = rs & 15;
  895. int len = h->size[fast];
  896. if (magbits && len + magbits <= FAST_BITS) {
  897. // magnitude code followed by receive_extend code
  898. int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits);
  899. int m = 1 << (magbits - 1);
  900. if (k < m) k += (-1 << magbits) + 1;
  901. // if the result is small enough, we can fit it in fast_ac table
  902. if (k >= -128 && k <= 127)
  903. fast_ac[i] = (stbi__int16) ((k << 8) + (run << 4) + (len + magbits));
  904. }
  905. }
  906. }
  907. }
  908. static void stbi__grow_buffer_unsafe(stbi__jpeg *j)
  909. {
  910. do {
  911. int b = j->nomore ? 0 : stbi__get8(j->s);
  912. if (b == 0xff) {
  913. int c = stbi__get8(j->s);
  914. if (c != 0) {
  915. j->marker = (unsigned char) c;
  916. j->nomore = 1;
  917. return;
  918. }
  919. }
  920. j->code_buffer |= b << (24 - j->code_bits);
  921. j->code_bits += 8;
  922. } while (j->code_bits <= 24);
  923. }
  924. // (1 << n) - 1
  925. static stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
  926. // decode a jpeg huffman value from the bitstream
  927. stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
  928. {
  929. unsigned int temp;
  930. int c,k;
  931. if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
  932. // look at the top FAST_BITS and determine what symbol ID it is,
  933. // if the code is <= FAST_BITS
  934. c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
  935. k = h->fast[c];
  936. if (k < 255) {
  937. int s = h->size[k];
  938. if (s > j->code_bits)
  939. return -1;
  940. j->code_buffer <<= s;
  941. j->code_bits -= s;
  942. return h->values[k];
  943. }
  944. // naive test is to shift the code_buffer down so k bits are
  945. // valid, then test against maxcode. To speed this up, we've
  946. // preshifted maxcode left so that it has (16-k) 0s at the
  947. // end; in other words, regardless of the number of bits, it
  948. // wants to be compared against something shifted to have 16;
  949. // that way we don't need to shift inside the loop.
  950. temp = j->code_buffer >> 16;
  951. for (k=FAST_BITS+1 ; ; ++k)
  952. if (temp < h->maxcode[k])
  953. break;
  954. if (k == 17) {
  955. // error! code not found
  956. j->code_bits -= 16;
  957. return -1;
  958. }
  959. if (k > j->code_bits)
  960. return -1;
  961. // convert the huffman code to the symbol id
  962. c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
  963. STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);
  964. // convert the id to a symbol
  965. j->code_bits -= k;
  966. j->code_buffer <<= k;
  967. return h->values[c];
  968. }
  969. // bias[n] = (-1<<n) + 1
  970. static int const stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};
  971. // combined JPEG 'receive' and JPEG 'extend', since baseline
  972. // always extends everything it receives.
  973. stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n)
  974. {
  975. unsigned int k;
  976. int sgn;
  977. if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
  978. sgn = (stbi__int32)j->code_buffer >> 31; // sign bit is always in MSB
  979. k = stbi_lrot(j->code_buffer, n);
  980. STBI_ASSERT(n >= 0 && n < (int) (sizeof(stbi__bmask)/sizeof(*stbi__bmask)));
  981. j->code_buffer = k & ~stbi__bmask[n];
  982. k &= stbi__bmask[n];
  983. j->code_bits -= n;
  984. return k + (stbi__jbias[n] & ~sgn);
  985. }
  986. // get some unsigned bits
  987. stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n)
  988. {
  989. unsigned int k;
  990. if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
  991. k = stbi_lrot(j->code_buffer, n);
  992. j->code_buffer = k & ~stbi__bmask[n];
  993. k &= stbi__bmask[n];
  994. j->code_bits -= n;
  995. return k;
  996. }
  997. stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j)
  998. {
  999. unsigned int k;
  1000. if (j->code_bits < 1) stbi__grow_buffer_unsafe(j);
  1001. k = j->code_buffer;
  1002. j->code_buffer <<= 1;
  1003. --j->code_bits;
  1004. return k & 0x80000000;
  1005. }
  1006. // given a value that's at position X in the zigzag stream,
  1007. // where does it appear in the 8x8 matrix coded as row-major?
  1008. static stbi_uc stbi__jpeg_dezigzag[64+15] =
  1009. {
  1010. 0, 1, 8, 16, 9, 2, 3, 10,
  1011. 17, 24, 32, 25, 18, 11, 4, 5,
  1012. 12, 19, 26, 33, 40, 48, 41, 34,
  1013. 27, 20, 13, 6, 7, 14, 21, 28,
  1014. 35, 42, 49, 56, 57, 50, 43, 36,
  1015. 29, 22, 15, 23, 30, 37, 44, 51,
  1016. 58, 59, 52, 45, 38, 31, 39, 46,
  1017. 53, 60, 61, 54, 47, 55, 62, 63,
  1018. // let corrupt input sample past end
  1019. 63, 63, 63, 63, 63, 63, 63, 63,
  1020. 63, 63, 63, 63, 63, 63, 63
  1021. };
  1022. // decode one 64-entry block--
  1023. static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman *hdc, stbi__huffman *hac, stbi__int16 *fac, int b, stbi_uc *dequant)
  1024. {
  1025. int diff,dc,k;
  1026. int t;
  1027. if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
  1028. t = stbi__jpeg_huff_decode(j, hdc);
  1029. if (t < 0) return stbi__err("bad huffman code","Corrupt JPEG");
  1030. // 0 all the ac values now so we can do it 32-bits at a time
  1031. memset(data,0,64*sizeof(data[0]));
  1032. diff = t ? stbi__extend_receive(j, t) : 0;
  1033. dc = j->img_comp[b].dc_pred + diff;
  1034. j->img_comp[b].dc_pred = dc;
  1035. data[0] = (short) (dc * dequant[0]);
  1036. // decode AC components, see JPEG spec
  1037. k = 1;
  1038. do {
  1039. unsigned int zig;
  1040. int c,r,s;
  1041. if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
  1042. c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
  1043. r = fac[c];
  1044. if (r) { // fast-AC path
  1045. k += (r >> 4) & 15; // run
  1046. s = r & 15; // combined length
  1047. j->code_buffer <<= s;
  1048. j->code_bits -= s;
  1049. // decode into unzigzag'd location
  1050. zig = stbi__jpeg_dezigzag[k++];
  1051. data[zig] = (short) ((r >> 8) * dequant[zig]);
  1052. } else {
  1053. int rs = stbi__jpeg_huff_decode(j, hac);
  1054. if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
  1055. s = rs & 15;
  1056. r = rs >> 4;
  1057. if (s == 0) {
  1058. if (rs != 0xf0) break; // end block
  1059. k += 16;
  1060. } else {
  1061. k += r;
  1062. // decode into unzigzag'd location
  1063. zig = stbi__jpeg_dezigzag[k++];
  1064. data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]);
  1065. }
  1066. }
  1067. } while (k < 64);
  1068. return 1;
  1069. }
  1070. static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b)
  1071. {
  1072. int diff,dc;
  1073. int t;
  1074. if (j->spec_end != 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
  1075. if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
  1076. if (j->succ_high == 0) {
  1077. // first scan for DC coefficient, must be first
  1078. memset(data,0,64*sizeof(data[0])); // 0 all the ac values now
  1079. t = stbi__jpeg_huff_decode(j, hdc);
  1080. diff = t ? stbi__extend_receive(j, t) : 0;
  1081. dc = j->img_comp[b].dc_pred + diff;
  1082. j->img_comp[b].dc_pred = dc;
  1083. data[0] = (short) (dc << j->succ_low);
  1084. } else {
  1085. // refinement scan for DC coefficient
  1086. if (stbi__jpeg_get_bit(j))
  1087. data[0] += (short) (1 << j->succ_low);
  1088. }
  1089. return 1;
  1090. }
  1091. // @OPTIMIZE: store non-zigzagged during the decode passes,
  1092. // and only de-zigzag when dequantizing
  1093. static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac)
  1094. {
  1095. int k;
  1096. if (j->spec_start == 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
  1097. if (j->succ_high == 0) {
  1098. int shift = j->succ_low;
  1099. if (j->eob_run) {
  1100. --j->eob_run;
  1101. return 1;
  1102. }
  1103. k = j->spec_start;
  1104. do {
  1105. unsigned int zig;
  1106. int c,r,s;
  1107. if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
  1108. c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
  1109. r = fac[c];
  1110. if (r) { // fast-AC path
  1111. k += (r >> 4) & 15; // run
  1112. s = r & 15; // combined length
  1113. j->code_buffer <<= s;
  1114. j->code_bits -= s;
  1115. zig = stbi__jpeg_dezigzag[k++];
  1116. data[zig] = (short) ((r >> 8) << shift);
  1117. } else {
  1118. int rs = stbi__jpeg_huff_decode(j, hac);
  1119. if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
  1120. s = rs & 15;
  1121. r = rs >> 4;
  1122. if (s == 0) {
  1123. if (r < 15) {
  1124. j->eob_run = (1 << r);
  1125. if (r)
  1126. j->eob_run += stbi__jpeg_get_bits(j, r);
  1127. --j->eob_run;
  1128. break;
  1129. }
  1130. k += 16;
  1131. } else {
  1132. k += r;
  1133. zig = stbi__jpeg_dezigzag[k++];
  1134. data[zig] = (short) (stbi__extend_receive(j,s) << shift);
  1135. }
  1136. }
  1137. } while (k <= j->spec_end);
  1138. } else {
  1139. // refinement scan for these AC coefficients
  1140. short bit = (short) (1 << j->succ_low);
  1141. if (j->eob_run) {
  1142. --j->eob_run;
  1143. for (k = j->spec_start; k <= j->spec_end; ++k) {
  1144. short *p = &data[stbi__jpeg_dezigzag[k]];
  1145. if (*p != 0)
  1146. if (stbi__jpeg_get_bit(j))
  1147. if ((*p & bit)==0) {
  1148. if (*p > 0)
  1149. *p += bit;
  1150. else
  1151. *p -= bit;
  1152. }
  1153. }
  1154. } else {
  1155. k = j->spec_start;
  1156. do {
  1157. int r,s;
  1158. int rs = stbi__jpeg_huff_decode(j, hac); // @OPTIMIZE see if we can use the fast path here, advance-by-r is so slow, eh
  1159. if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
  1160. s = rs & 15;
  1161. r = rs >> 4;
  1162. if (s == 0) {
  1163. if (r < 15) {
  1164. j->eob_run = (1 << r) - 1;
  1165. if (r)
  1166. j->eob_run += stbi__jpeg_get_bits(j, r);
  1167. r = 64; // force end of block
  1168. } else {
  1169. // r=15 s=0 should write 16 0s, so we just do
  1170. // a run of 15 0s and then write s (which is 0),
  1171. // so we don't have to do anything special here
  1172. }
  1173. } else {
  1174. if (s != 1) return stbi__err("bad huffman code", "Corrupt JPEG");
  1175. // sign bit
  1176. if (stbi__jpeg_get_bit(j))
  1177. s = bit;
  1178. else
  1179. s = -bit;
  1180. }
  1181. // advance by r
  1182. while (k <= j->spec_end) {
  1183. short *p = &data[stbi__jpeg_dezigzag[k++]];
  1184. if (*p != 0) {
  1185. if (stbi__jpeg_get_bit(j))
  1186. if ((*p & bit)==0) {
  1187. if (*p > 0)
  1188. *p += bit;
  1189. else
  1190. *p -= bit;
  1191. }
  1192. } else {
  1193. if (r == 0) {
  1194. *p = (short) s;
  1195. break;
  1196. }
  1197. --r;
  1198. }
  1199. }
  1200. } while (k <= j->spec_end);
  1201. }
  1202. }
  1203. return 1;
  1204. }
  1205. // take a -128..127 value and stbi__clamp it and convert to 0..255
  1206. stbi_inline static stbi_uc stbi__clamp(int x)
  1207. {
  1208. // trick to use a single test to catch both cases
  1209. if ((unsigned int) x > 255) {
  1210. if (x < 0) return 0;
  1211. if (x > 255) return 255;
  1212. }
  1213. return (stbi_uc) x;
  1214. }
  1215. #define stbi__f2f(x) ((int) (((x) * 4096 + 0.5)))
  1216. #define stbi__fsh(x) ((x) << 12)
  1217. // derived from jidctint -- DCT_ISLOW
  1218. #define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
  1219. int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
  1220. p2 = s2; \
  1221. p3 = s6; \
  1222. p1 = (p2+p3) * stbi__f2f(0.5411961f); \
  1223. t2 = p1 + p3*stbi__f2f(-1.847759065f); \
  1224. t3 = p1 + p2*stbi__f2f( 0.765366865f); \
  1225. p2 = s0; \
  1226. p3 = s4; \
  1227. t0 = stbi__fsh(p2+p3); \
  1228. t1 = stbi__fsh(p2-p3); \
  1229. x0 = t0+t3; \
  1230. x3 = t0-t3; \
  1231. x1 = t1+t2; \
  1232. x2 = t1-t2; \
  1233. t0 = s7; \
  1234. t1 = s5; \
  1235. t2 = s3; \
  1236. t3 = s1; \
  1237. p3 = t0+t2; \
  1238. p4 = t1+t3; \
  1239. p1 = t0+t3; \
  1240. p2 = t1+t2; \
  1241. p5 = (p3+p4)*stbi__f2f( 1.175875602f); \
  1242. t0 = t0*stbi__f2f( 0.298631336f); \
  1243. t1 = t1*stbi__f2f( 2.053119869f); \
  1244. t2 = t2*stbi__f2f( 3.072711026f); \
  1245. t3 = t3*stbi__f2f( 1.501321110f); \
  1246. p1 = p5 + p1*stbi__f2f(-0.899976223f); \
  1247. p2 = p5 + p2*stbi__f2f(-2.562915447f); \
  1248. p3 = p3*stbi__f2f(-1.961570560f); \
  1249. p4 = p4*stbi__f2f(-0.390180644f); \
  1250. t3 += p1+p4; \
  1251. t2 += p2+p3; \
  1252. t1 += p2+p4; \
  1253. t0 += p1+p3;
  1254. static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64])
  1255. {
  1256. int i,val[64],*v=val;
  1257. stbi_uc *o;
  1258. short *d = data;
  1259. // columns
  1260. for (i=0; i < 8; ++i,++d, ++v) {
  1261. // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing
  1262. if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
  1263. && d[40]==0 && d[48]==0 && d[56]==0) {
  1264. // no shortcut 0 seconds
  1265. // (1|2|3|4|5|6|7)==0 0 seconds
  1266. // all separate -0.047 seconds
  1267. // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds
  1268. int dcterm = d[0] << 2;
  1269. v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
  1270. } else {
  1271. STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56])
  1272. // constants scaled things up by 1<<12; let's bring them back
  1273. // down, but keep 2 extra bits of precision
  1274. x0 += 512; x1 += 512; x2 += 512; x3 += 512;
  1275. v[ 0] = (x0+t3) >> 10;
  1276. v[56] = (x0-t3) >> 10;
  1277. v[ 8] = (x1+t2) >> 10;
  1278. v[48] = (x1-t2) >> 10;
  1279. v[16] = (x2+t1) >> 10;
  1280. v[40] = (x2-t1) >> 10;
  1281. v[24] = (x3+t0) >> 10;
  1282. v[32] = (x3-t0) >> 10;
  1283. }
  1284. }
  1285. for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
  1286. // no fast case since the first 1D IDCT spread components out
  1287. STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
  1288. // constants scaled things up by 1<<12, plus we had 1<<2 from first
  1289. // loop, plus horizontal and vertical each scale by sqrt(8) so together
  1290. // we've got an extra 1<<3, so 1<<17 total we need to remove.
  1291. // so we want to round that, which means adding 0.5 * 1<<17,
  1292. // aka 65536. Also, we'll end up with -128 to 127 that we want
  1293. // to encode as 0..255 by adding 128, so we'll add that before the shift
  1294. x0 += 65536 + (128<<17);
  1295. x1 += 65536 + (128<<17);
  1296. x2 += 65536 + (128<<17);
  1297. x3 += 65536 + (128<<17);
  1298. // tried computing the shifts into temps, or'ing the temps to see
  1299. // if any were out of range, but that was slower
  1300. o[0] = stbi__clamp((x0+t3) >> 17);
  1301. o[7] = stbi__clamp((x0-t3) >> 17);
  1302. o[1] = stbi__clamp((x1+t2) >> 17);
  1303. o[6] = stbi__clamp((x1-t2) >> 17);
  1304. o[2] = stbi__clamp((x2+t1) >> 17);
  1305. o[5] = stbi__clamp((x2-t1) >> 17);
  1306. o[3] = stbi__clamp((x3+t0) >> 17);
  1307. o[4] = stbi__clamp((x3-t0) >> 17);
  1308. }
  1309. }
  1310. #ifdef STBI_SSE2
  1311. // sse2 integer IDCT. not the fastest possible implementation but it
  1312. // produces bit-identical results to the generic C version so it's
  1313. // fully "transparent".
  1314. static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
  1315. {
  1316. // This is constructed to match our regular (generic) integer IDCT exactly.
  1317. __m128i row0, row1, row2, row3, row4, row5, row6, row7;
  1318. __m128i tmp;
  1319. // dot product constant: even elems=x, odd elems=y
  1320. #define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y))
  1321. // out(0) = c0[even]*x + c0[odd]*y (c0, x, y 16-bit, out 32-bit)
  1322. // out(1) = c1[even]*x + c1[odd]*y
  1323. #define dct_rot(out0,out1, x,y,c0,c1) \
  1324. __m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \
  1325. __m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \
  1326. __m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \
  1327. __m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \
  1328. __m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \
  1329. __m128i out1##_h = _mm_madd_epi16(c0##hi, c1)
  1330. // out = in << 12 (in 16-bit, out 32-bit)
  1331. #define dct_widen(out, in) \
  1332. __m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \
  1333. __m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4)
  1334. // wide add
  1335. #define dct_wadd(out, a, b) \
  1336. __m128i out##_l = _mm_add_epi32(a##_l, b##_l); \
  1337. __m128i out##_h = _mm_add_epi32(a##_h, b##_h)
  1338. // wide sub
  1339. #define dct_wsub(out, a, b) \
  1340. __m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \
  1341. __m128i out##_h = _mm_sub_epi32(a##_h, b##_h)
  1342. // butterfly a/b, add bias, then shift by "s" and pack
  1343. #define dct_bfly32o(out0, out1, a,b,bias,s) \
  1344. { \
  1345. __m128i abiased_l = _mm_add_epi32(a##_l, bias); \
  1346. __m128i abiased_h = _mm_add_epi32(a##_h, bias); \
  1347. dct_wadd(sum, abiased, b); \
  1348. dct_wsub(dif, abiased, b); \
  1349. out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \
  1350. out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \
  1351. }
  1352. // 8-bit interleave step (for transposes)
  1353. #define dct_interleave8(a, b) \
  1354. tmp = a; \
  1355. a = _mm_unpacklo_epi8(a, b); \
  1356. b = _mm_unpackhi_epi8(tmp, b)
  1357. // 16-bit interleave step (for transposes)
  1358. #define dct_interleave16(a, b) \
  1359. tmp = a; \
  1360. a = _mm_unpacklo_epi16(a, b); \
  1361. b = _mm_unpackhi_epi16(tmp, b)
  1362. #define dct_pass(bias,shift) \
  1363. { \
  1364. /* even part */ \
  1365. dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \
  1366. __m128i sum04 = _mm_add_epi16(row0, row4); \
  1367. __m128i dif04 = _mm_sub_epi16(row0, row4); \
  1368. dct_widen(t0e, sum04); \
  1369. dct_widen(t1e, dif04); \
  1370. dct_wadd(x0, t0e, t3e); \
  1371. dct_wsub(x3, t0e, t3e); \
  1372. dct_wadd(x1, t1e, t2e); \
  1373. dct_wsub(x2, t1e, t2e); \
  1374. /* odd part */ \
  1375. dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \
  1376. dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \
  1377. __m128i sum17 = _mm_add_epi16(row1, row7); \
  1378. __m128i sum35 = _mm_add_epi16(row3, row5); \
  1379. dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \
  1380. dct_wadd(x4, y0o, y4o); \
  1381. dct_wadd(x5, y1o, y5o); \
  1382. dct_wadd(x6, y2o, y5o); \
  1383. dct_wadd(x7, y3o, y4o); \
  1384. dct_bfly32o(row0,row7, x0,x7,bias,shift); \
  1385. dct_bfly32o(row1,row6, x1,x6,bias,shift); \
  1386. dct_bfly32o(row2,row5, x2,x5,bias,shift); \
  1387. dct_bfly32o(row3,row4, x3,x4,bias,shift); \
  1388. }
  1389. __m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f));
  1390. __m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f));
  1391. __m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f));
  1392. __m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f));
  1393. __m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f));
  1394. __m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f));
  1395. __m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f));
  1396. __m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f));
  1397. // rounding biases in column/row passes, see stbi__idct_block for explanation.
  1398. __m128i bias_0 = _mm_set1_epi32(512);
  1399. __m128i bias_1 = _mm_set1_epi32(65536 + (128<<17));
  1400. // load
  1401. row0 = _mm_load_si128((const __m128i *) (data + 0*8));
  1402. row1 = _mm_load_si128((const __m128i *) (data + 1*8));
  1403. row2 = _mm_load_si128((const __m128i *) (data + 2*8));
  1404. row3 = _mm_load_si128((const __m128i *) (data + 3*8));
  1405. row4 = _mm_load_si128((const __m128i *) (data + 4*8));
  1406. row5 = _mm_load_si128((const __m128i *) (data + 5*8));
  1407. row6 = _mm_load_si128((const __m128i *) (data + 6*8));
  1408. row7 = _mm_load_si128((const __m128i *) (data + 7*8));
  1409. // column pass
  1410. dct_pass(bias_0, 10);
  1411. {
  1412. // 16bit 8x8 transpose pass 1
  1413. dct_interleave16(row0, row4);
  1414. dct_interleave16(row1, row5);
  1415. dct_interleave16(row2, row6);
  1416. dct_interleave16(row3, row7);
  1417. // transpose pass 2
  1418. dct_interleave16(row0, row2);
  1419. dct_interleave16(row1, row3);
  1420. dct_interleave16(row4, row6);
  1421. dct_interleave16(row5, row7);
  1422. // transpose pass 3
  1423. dct_interleave16(row0, row1);
  1424. dct_interleave16(row2, row3);
  1425. dct_interleave16(row4, row5);
  1426. dct_interleave16(row6, row7);
  1427. }
  1428. // row pass
  1429. dct_pass(bias_1, 17);
  1430. {
  1431. // pack
  1432. __m128i p0 = _mm_packus_epi16(row0, row1); // a0a1a2a3...a7b0b1b2b3...b7
  1433. __m128i p1 = _mm_packus_epi16(row2, row3);
  1434. __m128i p2 = _mm_packus_epi16(row4, row5);
  1435. __m128i p3 = _mm_packus_epi16(row6, row7);
  1436. // 8bit 8x8 transpose pass 1
  1437. dct_interleave8(p0, p2); // a0e0a1e1...
  1438. dct_interleave8(p1, p3); // c0g0c1g1...
  1439. // transpose pass 2
  1440. dct_interleave8(p0, p1); // a0c0e0g0...
  1441. dct_interleave8(p2, p3); // b0d0f0h0...
  1442. // transpose pass 3
  1443. dct_interleave8(p0, p2); // a0b0c0d0...
  1444. dct_interleave8(p1, p3); // a4b4c4d4...
  1445. // store
  1446. _mm_storel_epi64((__m128i *) out, p0); out += out_stride;
  1447. _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride;
  1448. _mm_storel_epi64((__m128i *) out, p2); out += out_stride;
  1449. _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride;
  1450. _mm_storel_epi64((__m128i *) out, p1); out += out_stride;
  1451. _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride;
  1452. _mm_storel_epi64((__m128i *) out, p3); out += out_stride;
  1453. _mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e));
  1454. }
  1455. #undef dct_const
  1456. #undef dct_rot
  1457. #undef dct_widen
  1458. #undef dct_wadd
  1459. #undef dct_wsub
  1460. #undef dct_bfly32o
  1461. #undef dct_interleave8
  1462. #undef dct_interleave16
  1463. #undef dct_pass
  1464. }
  1465. #endif // STBI_SSE2
  1466. #ifdef STBI_NEON
  1467. // NEON integer IDCT. should produce bit-identical
  1468. // results to the generic C version.
  1469. static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
  1470. {
  1471. int16x8_t row0, row1, row2, row3, row4, row5, row6, row7;
  1472. int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f));
  1473. int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f));
  1474. int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f));
  1475. int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f));
  1476. int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f));
  1477. int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f));
  1478. int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f));
  1479. int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f));
  1480. int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f));
  1481. int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f));
  1482. int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f));
  1483. int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f));
  1484. #define dct_long_mul(out, inq, coeff) \
  1485. int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \
  1486. int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff)
  1487. #define dct_long_mac(out, acc, inq, coeff) \
  1488. int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \
  1489. int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff)
  1490. #define dct_widen(out, inq) \
  1491. int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \
  1492. int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12)
  1493. // wide add
  1494. #define dct_wadd(out, a, b) \
  1495. int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \
  1496. int32x4_t out##_h = vaddq_s32(a##_h, b##_h)
  1497. // wide sub
  1498. #define dct_wsub(out, a, b) \
  1499. int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \
  1500. int32x4_t out##_h = vsubq_s32(a##_h, b##_h)
  1501. // butterfly a/b, then shift using "shiftop" by "s" and pack
  1502. #define dct_bfly32o(out0,out1, a,b,shiftop,s) \
  1503. { \
  1504. dct_wadd(sum, a, b); \
  1505. dct_wsub(dif, a, b); \
  1506. out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \
  1507. out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \
  1508. }
  1509. #define dct_pass(shiftop, shift) \
  1510. { \
  1511. /* even part */ \
  1512. int16x8_t sum26 = vaddq_s16(row2, row6); \
  1513. dct_long_mul(p1e, sum26, rot0_0); \
  1514. dct_long_mac(t2e, p1e, row6, rot0_1); \
  1515. dct_long_mac(t3e, p1e, row2, rot0_2); \
  1516. int16x8_t sum04 = vaddq_s16(row0, row4); \
  1517. int16x8_t dif04 = vsubq_s16(row0, row4); \
  1518. dct_widen(t0e, sum04); \
  1519. dct_widen(t1e, dif04); \
  1520. dct_wadd(x0, t0e, t3e); \
  1521. dct_wsub(x3, t0e, t3e); \
  1522. dct_wadd(x1, t1e, t2e); \
  1523. dct_wsub(x2, t1e, t2e); \
  1524. /* odd part */ \
  1525. int16x8_t sum15 = vaddq_s16(row1, row5); \
  1526. int16x8_t sum17 = vaddq_s16(row1, row7); \
  1527. int16x8_t sum35 = vaddq_s16(row3, row5); \
  1528. int16x8_t sum37 = vaddq_s16(row3, row7); \
  1529. int16x8_t sumodd = vaddq_s16(sum17, sum35); \
  1530. dct_long_mul(p5o, sumodd, rot1_0); \
  1531. dct_long_mac(p1o, p5o, sum17, rot1_1); \
  1532. dct_long_mac(p2o, p5o, sum35, rot1_2); \
  1533. dct_long_mul(p3o, sum37, rot2_0); \
  1534. dct_long_mul(p4o, sum15, rot2_1); \
  1535. dct_wadd(sump13o, p1o, p3o); \
  1536. dct_wadd(sump24o, p2o, p4o); \
  1537. dct_wadd(sump23o, p2o, p3o); \
  1538. dct_wadd(sump14o, p1o, p4o); \
  1539. dct_long_mac(x4, sump13o, row7, rot3_0); \
  1540. dct_long_mac(x5, sump24o, row5, rot3_1); \
  1541. dct_long_mac(x6, sump23o, row3, rot3_2); \
  1542. dct_long_mac(x7, sump14o, row1, rot3_3); \
  1543. dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \
  1544. dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \
  1545. dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \
  1546. dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \
  1547. }
  1548. // load
  1549. row0 = vld1q_s16(data + 0*8);
  1550. row1 = vld1q_s16(data + 1*8);
  1551. row2 = vld1q_s16(data + 2*8);
  1552. row3 = vld1q_s16(data + 3*8);
  1553. row4 = vld1q_s16(data + 4*8);
  1554. row5 = vld1q_s16(data + 5*8);
  1555. row6 = vld1q_s16(data + 6*8);
  1556. row7 = vld1q_s16(data + 7*8);
  1557. // add DC bias
  1558. row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0));
  1559. // column pass
  1560. dct_pass(vrshrn_n_s32, 10);
  1561. // 16bit 8x8 transpose
  1562. {
  1563. // these three map to a single VTRN.16, VTRN.32, and VSWP, respectively.
  1564. // whether compilers actually get this is another story, sadly.
  1565. #define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; }
  1566. #define dct_trn32(x, y) { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); }
  1567. #define dct_trn64(x, y) { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); }
  1568. // pass 1
  1569. dct_trn16(row0, row1); // a0b0a2b2a4b4a6b6
  1570. dct_trn16(row2, row3);
  1571. dct_trn16(row4, row5);
  1572. dct_trn16(row6, row7);
  1573. // pass 2
  1574. dct_trn32(row0, row2); // a0b0c0d0a4b4c4d4
  1575. dct_trn32(row1, row3);
  1576. dct_trn32(row4, row6);
  1577. dct_trn32(row5, row7);
  1578. // pass 3
  1579. dct_trn64(row0, row4); // a0b0c0d0e0f0g0h0
  1580. dct_trn64(row1, row5);
  1581. dct_trn64(row2, row6);
  1582. dct_trn64(row3, row7);
  1583. #undef dct_trn16
  1584. #undef dct_trn32
  1585. #undef dct_trn64
  1586. }
  1587. // row pass
  1588. // vrshrn_n_s32 only supports shifts up to 16, we need
  1589. // 17. so do a non-rounding shift of 16 first then follow
  1590. // up with a rounding shift by 1.
  1591. dct_pass(vshrn_n_s32, 16);
  1592. {
  1593. // pack and round
  1594. uint8x8_t p0 = vqrshrun_n_s16(row0, 1);
  1595. uint8x8_t p1 = vqrshrun_n_s16(row1, 1);
  1596. uint8x8_t p2 = vqrshrun_n_s16(row2, 1);
  1597. uint8x8_t p3 = vqrshrun_n_s16(row3, 1);
  1598. uint8x8_t p4 = vqrshrun_n_s16(row4, 1);
  1599. uint8x8_t p5 = vqrshrun_n_s16(row5, 1);
  1600. uint8x8_t p6 = vqrshrun_n_s16(row6, 1);
  1601. uint8x8_t p7 = vqrshrun_n_s16(row7, 1);
  1602. // again, these can translate into one instruction, but often don't.
  1603. #define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; }
  1604. #define dct_trn8_16(x, y) { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); }
  1605. #define dct_trn8_32(x, y) { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); }
  1606. // sadly can't use interleaved stores here since we only write
  1607. // 8 bytes to each scan line!
  1608. // 8x8 8-bit transpose pass 1
  1609. dct_trn8_8(p0, p1);
  1610. dct_trn8_8(p2, p3);
  1611. dct_trn8_8(p4, p5);
  1612. dct_trn8_8(p6, p7);
  1613. // pass 2
  1614. dct_trn8_16(p0, p2);
  1615. dct_trn8_16(p1, p3);
  1616. dct_trn8_16(p4, p6);
  1617. dct_trn8_16(p5, p7);
  1618. // pass 3
  1619. dct_trn8_32(p0, p4);
  1620. dct_trn8_32(p1, p5);
  1621. dct_trn8_32(p2, p6);
  1622. dct_trn8_32(p3, p7);
  1623. // store
  1624. vst1_u8(out, p0); out += out_stride;
  1625. vst1_u8(out, p1); out += out_stride;
  1626. vst1_u8(out, p2); out += out_stride;
  1627. vst1_u8(out, p3); out += out_stride;
  1628. vst1_u8(out, p4); out += out_stride;
  1629. vst1_u8(out, p5); out += out_stride;
  1630. vst1_u8(out, p6); out += out_stride;
  1631. vst1_u8(out, p7);
  1632. #undef dct_trn8_8
  1633. #undef dct_trn8_16
  1634. #undef dct_trn8_32
  1635. }
  1636. #undef dct_long_mul
  1637. #undef dct_long_mac
  1638. #undef dct_widen
  1639. #undef dct_wadd
  1640. #undef dct_wsub
  1641. #undef dct_bfly32o
  1642. #undef dct_pass
  1643. }
  1644. #endif // STBI_NEON
  1645. #define STBI__MARKER_none 0xff
  1646. // if there's a pending marker from the entropy stream, return that
  1647. // otherwise, fetch from the stream and get a marker. if there's no
  1648. // marker, return 0xff, which is never a valid marker value
  1649. static stbi_uc stbi__get_marker(stbi__jpeg *j)
  1650. {
  1651. stbi_uc x;
  1652. if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; }
  1653. x = stbi__get8(j->s);
  1654. if (x != 0xff) return STBI__MARKER_none;
  1655. while (x == 0xff)
  1656. x = stbi__get8(j->s);
  1657. return x;
  1658. }
  1659. // in each scan, we'll have scan_n components, and the order
  1660. // of the components is specified by order[]
  1661. #define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
  1662. // after a restart interval, stbi__jpeg_reset the entropy decoder and
  1663. // the dc prediction
  1664. static void stbi__jpeg_reset(stbi__jpeg *j)
  1665. {
  1666. j->code_bits = 0;
  1667. j->code_buffer = 0;
  1668. j->nomore = 0;
  1669. j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;
  1670. j->marker = STBI__MARKER_none;
  1671. j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
  1672. j->eob_run = 0;
  1673. // no more than 1<<31 MCUs if no restart_interal? that's plenty safe,
  1674. // since we don't even allow 1<<30 pixels
  1675. }
  1676. static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
  1677. {
  1678. stbi__jpeg_reset(z);
  1679. if (!z->progressive) {
  1680. if (z->scan_n == 1) {
  1681. int i,j;
  1682. STBI_SIMD_ALIGN(short, data[64]);
  1683. int n = z->order[0];
  1684. // non-interleaved data, we just need to process one block at a time,
  1685. // in trivial scanline order
  1686. // number of blocks to do just depends on how many actual "pixels" this
  1687. // component has, independent of interleaved MCU blocking and such
  1688. int w = (z->img_comp[n].x+7) >> 3;
  1689. int h = (z->img_comp[n].y+7) >> 3;
  1690. for (j=0; j < h; ++j) {
  1691. for (i=0; i < w; ++i) {
  1692. int ha = z->img_comp[n].ha;
  1693. if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0;
  1694. z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data);
  1695. // every data block is an MCU, so countdown the restart interval
  1696. if (--z->todo <= 0) {
  1697. if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
  1698. // if it's NOT a restart, then just bail, so we get corrupt data
  1699. // rather than no data
  1700. if (!STBI__RESTART(z->marker)) return 1;
  1701. stbi__jpeg_reset(z);
  1702. }
  1703. }
  1704. }
  1705. return 1;
  1706. } else { // interleaved
  1707. int i,j,k,x,y;
  1708. STBI_SIMD_ALIGN(short, data[64]);
  1709. for (j=0; j < z->img_mcu_y; ++j) {
  1710. for (i=0; i < z->img_mcu_x; ++i) {
  1711. // scan an interleaved mcu... process scan_n components in order
  1712. for (k=0; k < z->scan_n; ++k) {
  1713. int n = z->order[k];
  1714. // scan out an mcu's worth of this component; that's just determined
  1715. // by the basic H and V specified for the component
  1716. for (y=0; y < z->img_comp[n].v; ++y) {
  1717. for (x=0; x < z->img_comp[n].h; ++x) {
  1718. int x2 = (i*z->img_comp[n].h + x)*8;
  1719. int y2 = (j*z->img_comp[n].v + y)*8;
  1720. int ha = z->img_comp[n].ha;
  1721. if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0;
  1722. z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data);
  1723. }
  1724. }
  1725. }
  1726. // after all interleaved components, that's an interleaved MCU,
  1727. // so now count down the restart interval
  1728. if (--z->todo <= 0) {
  1729. if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
  1730. if (!STBI__RESTART(z->marker)) return 1;
  1731. stbi__jpeg_reset(z);
  1732. }
  1733. }
  1734. }
  1735. return 1;
  1736. }
  1737. } else {
  1738. if (z->scan_n == 1) {
  1739. int i,j;
  1740. int n = z->order[0];
  1741. // non-interleaved data, we just need to process one block at a time,
  1742. // in trivial scanline order
  1743. // number of blocks to do just depends on how many actual "pixels" this
  1744. // component has, independent of interleaved MCU blocking and such
  1745. int w = (z->img_comp[n].x+7) >> 3;
  1746. int h = (z->img_comp[n].y+7) >> 3;
  1747. for (j=0; j < h; ++j) {
  1748. for (i=0; i < w; ++i) {
  1749. short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
  1750. if (z->spec_start == 0) {
  1751. if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
  1752. return 0;
  1753. } else {
  1754. int ha = z->img_comp[n].ha;
  1755. if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha]))
  1756. return 0;
  1757. }
  1758. // every data block is an MCU, so countdown the restart interval
  1759. if (--z->todo <= 0) {
  1760. if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
  1761. if (!STBI__RESTART(z->marker)) return 1;
  1762. stbi__jpeg_reset(z);
  1763. }
  1764. }
  1765. }
  1766. return 1;
  1767. } else { // interleaved
  1768. int i,j,k,x,y;
  1769. for (j=0; j < z->img_mcu_y; ++j) {
  1770. for (i=0; i < z->img_mcu_x; ++i) {
  1771. // scan an interleaved mcu... process scan_n components in order
  1772. for (k=0; k < z->scan_n; ++k) {
  1773. int n = z->order[k];
  1774. // scan out an mcu's worth of this component; that's just determined
  1775. // by the basic H and V specified for the component
  1776. for (y=0; y < z->img_comp[n].v; ++y) {
  1777. for (x=0; x < z->img_comp[n].h; ++x) {
  1778. int x2 = (i*z->img_comp[n].h + x);
  1779. int y2 = (j*z->img_comp[n].v + y);
  1780. short *data = z->img_comp[n].coeff + 64 * (x2 + y2 * z->img_comp[n].coeff_w);
  1781. if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
  1782. return 0;
  1783. }
  1784. }
  1785. }
  1786. // after all interleaved components, that's an interleaved MCU,
  1787. // so now count down the restart interval
  1788. if (--z->todo <= 0) {
  1789. if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
  1790. if (!STBI__RESTART(z->marker)) return 1;
  1791. stbi__jpeg_reset(z);
  1792. }
  1793. }
  1794. }
  1795. return 1;
  1796. }
  1797. }
  1798. }
  1799. static void stbi__jpeg_dequantize(short *data, stbi_uc *dequant)
  1800. {
  1801. int i;
  1802. for (i=0; i < 64; ++i)
  1803. data[i] *= dequant[i];
  1804. }
  1805. static void stbi__jpeg_finish(stbi__jpeg *z)
  1806. {
  1807. if (z->progressive) {
  1808. // dequantize and idct the data
  1809. int i,j,n;
  1810. for (n=0; n < z->s->img_n; ++n) {
  1811. int w = (z->img_comp[n].x+7) >> 3;
  1812. int h = (z->img_comp[n].y+7) >> 3;
  1813. for (j=0; j < h; ++j) {
  1814. for (i=0; i < w; ++i) {
  1815. short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
  1816. stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]);
  1817. z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data);
  1818. }
  1819. }
  1820. }
  1821. }
  1822. }
  1823. static int stbi__process_marker(stbi__jpeg *z, int m)
  1824. {
  1825. int L;
  1826. switch (m) {
  1827. case STBI__MARKER_none: // no marker found
  1828. return stbi__err("expected marker","Corrupt JPEG");
  1829. case 0xDD: // DRI - specify restart interval
  1830. if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG");
  1831. z->restart_interval = stbi__get16be(z->s);
  1832. return 1;
  1833. case 0xDB: // DQT - define quantization table
  1834. L = stbi__get16be(z->s)-2;
  1835. while (L > 0) {
  1836. int q = stbi__get8(z->s);
  1837. int p = q >> 4;
  1838. int t = q & 15,i;
  1839. if (p != 0) return stbi__err("bad DQT type","Corrupt JPEG");
  1840. if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG");
  1841. for (i=0; i < 64; ++i)
  1842. z->dequant[t][stbi__jpeg_dezigzag[i]] = stbi__get8(z->s);
  1843. L -= 65;
  1844. }
  1845. return L==0;
  1846. case 0xC4: // DHT - define huffman table
  1847. L = stbi__get16be(z->s)-2;
  1848. while (L > 0) {
  1849. stbi_uc *v;
  1850. int sizes[16],i,n=0;
  1851. int q = stbi__get8(z->s);
  1852. int tc = q >> 4;
  1853. int th = q & 15;
  1854. if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG");
  1855. for (i=0; i < 16; ++i) {
  1856. sizes[i] = stbi__get8(z->s);
  1857. n += sizes[i];
  1858. }
  1859. L -= 17;
  1860. if (tc == 0) {
  1861. if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0;
  1862. v = z->huff_dc[th].values;
  1863. } else {
  1864. if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0;
  1865. v = z->huff_ac[th].values;
  1866. }
  1867. for (i=0; i < n; ++i)
  1868. v[i] = stbi__get8(z->s);
  1869. if (tc != 0)
  1870. stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th);
  1871. L -= n;
  1872. }
  1873. return L==0;
  1874. }
  1875. // check for comment block or APP blocks
  1876. if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
  1877. stbi__skip(z->s, stbi__get16be(z->s)-2);
  1878. return 1;
  1879. }
  1880. return 0;
  1881. }
  1882. // after we see SOS
  1883. static int stbi__process_scan_header(stbi__jpeg *z)
  1884. {
  1885. int i;
  1886. int Ls = stbi__get16be(z->s);
  1887. z->scan_n = stbi__get8(z->s);
  1888. if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return stbi__err("bad SOS component count","Corrupt JPEG");
  1889. if (Ls != 6+2*z->scan_n) return stbi__err("bad SOS len","Corrupt JPEG");
  1890. for (i=0; i < z->scan_n; ++i) {
  1891. int id = stbi__get8(z->s), which;
  1892. int q = stbi__get8(z->s);
  1893. for (which = 0; which < z->s->img_n; ++which)
  1894. if (z->img_comp[which].id == id)
  1895. break;
  1896. if (which == z->s->img_n) return 0; // no match
  1897. z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG");
  1898. z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG");
  1899. z->order[i] = which;
  1900. }
  1901. {
  1902. int aa;
  1903. z->spec_start = stbi__get8(z->s);
  1904. z->spec_end = stbi__get8(z->s); // should be 63, but might be 0
  1905. aa = stbi__get8(z->s);
  1906. z->succ_high = (aa >> 4);
  1907. z->succ_low = (aa & 15);
  1908. if (z->progressive) {
  1909. if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13)
  1910. return stbi__err("bad SOS", "Corrupt JPEG");
  1911. } else {
  1912. if (z->spec_start != 0) return stbi__err("bad SOS","Corrupt JPEG");
  1913. if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS","Corrupt JPEG");
  1914. z->spec_end = 63;
  1915. }
  1916. }
  1917. return 1;
  1918. }
  1919. static int stbi__process_frame_header(stbi__jpeg *z, int scan)
  1920. {
  1921. stbi__context *s = z->s;
  1922. int Lf,p,i,q, h_max=1,v_max=1,c;
  1923. Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len","Corrupt JPEG"); // JPEG
  1924. p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
  1925. s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
  1926. s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires
  1927. c = stbi__get8(s);
  1928. if (c != 3 && c != 1) return stbi__err("bad component count","Corrupt JPEG"); // JFIF requires
  1929. s->img_n = c;
  1930. for (i=0; i < c; ++i) {
  1931. z->img_comp[i].data = NULL;
  1932. z->img_comp[i].linebuf = NULL;
  1933. }
  1934. if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG");
  1935. for (i=0; i < s->img_n; ++i) {
  1936. z->img_comp[i].id = stbi__get8(s);
  1937. if (z->img_comp[i].id != i+1) // JFIF requires
  1938. if (z->img_comp[i].id != i) // some version of jpegtran outputs non-JFIF-compliant files!
  1939. return stbi__err("bad component ID","Corrupt JPEG");
  1940. q = stbi__get8(s);
  1941. z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG");
  1942. z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG");
  1943. z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG");
  1944. }
  1945. if (scan != STBI__SCAN_load) return 1;
  1946. if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
  1947. for (i=0; i < s->img_n; ++i) {
  1948. if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
  1949. if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
  1950. }
  1951. // compute interleaved mcu info
  1952. z->img_h_max = h_max;
  1953. z->img_v_max = v_max;
  1954. z->img_mcu_w = h_max * 8;
  1955. z->img_mcu_h = v_max * 8;
  1956. z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
  1957. z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
  1958. for (i=0; i < s->img_n; ++i) {
  1959. // number of effective pixels (e.g. for non-interleaved MCU)
  1960. z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
  1961. z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
  1962. // to simplify generation, we'll allocate enough memory to decode
  1963. // the bogus oversized data from using interleaved MCUs and their
  1964. // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't
  1965. // discard the extra data until colorspace conversion
  1966. z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
  1967. z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
  1968. z->img_comp[i].raw_data = stbi__malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);
  1969. if (z->img_comp[i].raw_data == NULL) {
  1970. for(--i; i >= 0; --i) {
  1971. STBI_FREE(z->img_comp[i].raw_data);
  1972. z->img_comp[i].data = NULL;
  1973. }
  1974. return stbi__err("outofmem", "Out of memory");
  1975. }
  1976. // align blocks for idct using mmx/sse
  1977. z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);
  1978. z->img_comp[i].linebuf = NULL;
  1979. if (z->progressive) {
  1980. z->img_comp[i].coeff_w = (z->img_comp[i].w2 + 7) >> 3;
  1981. z->img_comp[i].coeff_h = (z->img_comp[i].h2 + 7) >> 3;
  1982. z->img_comp[i].raw_coeff = STBI_MALLOC(z->img_comp[i].coeff_w * z->img_comp[i].coeff_h * 64 * sizeof(short) + 15);
  1983. z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15);
  1984. } else {
  1985. z->img_comp[i].coeff = 0;
  1986. z->img_comp[i].raw_coeff = 0;
  1987. }
  1988. }
  1989. return 1;
  1990. }
  1991. // use comparisons since in some cases we handle more than one case (e.g. SOF)
  1992. #define stbi__DNL(x) ((x) == 0xdc)
  1993. #define stbi__SOI(x) ((x) == 0xd8)
  1994. #define stbi__EOI(x) ((x) == 0xd9)
  1995. #define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2)
  1996. #define stbi__SOS(x) ((x) == 0xda)
  1997. #define stbi__SOF_progressive(x) ((x) == 0xc2)
  1998. static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan)
  1999. {
  2000. int m;
  2001. z->marker = STBI__MARKER_none; // initialize cached marker to empty
  2002. m = stbi__get_marker(z);
  2003. if (!stbi__SOI(m)) return stbi__err("no SOI","Corrupt JPEG");
  2004. if (scan == STBI__SCAN_type) return 1;
  2005. m = stbi__get_marker(z);
  2006. while (!stbi__SOF(m)) {
  2007. if (!stbi__process_marker(z,m)) return 0;
  2008. m = stbi__get_marker(z);
  2009. while (m == STBI__MARKER_none) {
  2010. // some files have extra padding after their blocks, so ok, we'll scan
  2011. if (stbi__at_eof(z->s)) return stbi__err("no SOF", "Corrupt JPEG");
  2012. m = stbi__get_marker(z);
  2013. }
  2014. }
  2015. z->progressive = stbi__SOF_progressive(m);
  2016. if (!stbi__process_frame_header(z, scan)) return 0;
  2017. return 1;
  2018. }
  2019. // decode image to YCbCr format
  2020. static int stbi__decode_jpeg_image(stbi__jpeg *j)
  2021. {
  2022. int m;
  2023. for (m = 0; m < 4; m++) {
  2024. j->img_comp[m].raw_data = NULL;
  2025. j->img_comp[m].raw_coeff = NULL;
  2026. }
  2027. j->restart_interval = 0;
  2028. if (!stbi__decode_jpeg_header(j, STBI__SCAN_load)) return 0;
  2029. m = stbi__get_marker(j);
  2030. while (!stbi__EOI(m)) {
  2031. if (stbi__SOS(m)) {
  2032. if (!stbi__process_scan_header(j)) return 0;
  2033. if (!stbi__parse_entropy_coded_data(j)) return 0;
  2034. if (j->marker == STBI__MARKER_none ) {
  2035. // handle 0s at the end of image data from IP Kamera 9060
  2036. while (!stbi__at_eof(j->s)) {
  2037. int x = stbi__get8(j->s);
  2038. if (x == 255) {
  2039. j->marker = stbi__get8(j->s);
  2040. break;
  2041. } else if (x != 0) {
  2042. return stbi__err("junk before marker", "Corrupt JPEG");
  2043. }
  2044. }
  2045. // if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0
  2046. }
  2047. } else {
  2048. if (!stbi__process_marker(j, m)) return 0;
  2049. }
  2050. m = stbi__get_marker(j);
  2051. }
  2052. if (j->progressive)
  2053. stbi__jpeg_finish(j);
  2054. return 1;
  2055. }
  2056. // static jfif-centered resampling (across block boundaries)
  2057. typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1,
  2058. int w, int hs);
  2059. #define stbi__div4(x) ((stbi_uc) ((x) >> 2))
  2060. static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  2061. {
  2062. STBI_NOTUSED(out);
  2063. STBI_NOTUSED(in_far);
  2064. STBI_NOTUSED(w);
  2065. STBI_NOTUSED(hs);
  2066. return in_near;
  2067. }
  2068. static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  2069. {
  2070. // need to generate two samples vertically for every one in input
  2071. int i;
  2072. STBI_NOTUSED(hs);
  2073. for (i=0; i < w; ++i)
  2074. out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2);
  2075. return out;
  2076. }
  2077. static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  2078. {
  2079. // need to generate two samples horizontally for every one in input
  2080. int i;
  2081. stbi_uc *input = in_near;
  2082. if (w == 1) {
  2083. // if only one sample, can't do any interpolation
  2084. out[0] = out[1] = input[0];
  2085. return out;
  2086. }
  2087. out[0] = input[0];
  2088. out[1] = stbi__div4(input[0]*3 + input[1] + 2);
  2089. for (i=1; i < w-1; ++i) {
  2090. int n = 3*input[i]+2;
  2091. out[i*2+0] = stbi__div4(n+input[i-1]);
  2092. out[i*2+1] = stbi__div4(n+input[i+1]);
  2093. }
  2094. out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2);
  2095. out[i*2+1] = input[w-1];
  2096. STBI_NOTUSED(in_far);
  2097. STBI_NOTUSED(hs);
  2098. return out;
  2099. }
  2100. #define stbi__div16(x) ((stbi_uc) ((x) >> 4))
  2101. static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  2102. {
  2103. // need to generate 2x2 samples for every one in input
  2104. int i,t0,t1;
  2105. if (w == 1) {
  2106. out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
  2107. return out;
  2108. }
  2109. t1 = 3*in_near[0] + in_far[0];
  2110. out[0] = stbi__div4(t1+2);
  2111. for (i=1; i < w; ++i) {
  2112. t0 = t1;
  2113. t1 = 3*in_near[i]+in_far[i];
  2114. out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
  2115. out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
  2116. }
  2117. out[w*2-1] = stbi__div4(t1+2);
  2118. STBI_NOTUSED(hs);
  2119. return out;
  2120. }
  2121. #if defined(STBI_SSE2) || defined(STBI_NEON)
  2122. static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  2123. {
  2124. // need to generate 2x2 samples for every one in input
  2125. int i=0,t0,t1;
  2126. if (w == 1) {
  2127. out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
  2128. return out;
  2129. }
  2130. t1 = 3*in_near[0] + in_far[0];
  2131. // process groups of 8 pixels for as long as we can.
  2132. // note we can't handle the last pixel in a row in this loop
  2133. // because we need to handle the filter boundary conditions.
  2134. for (; i < ((w-1) & ~7); i += 8) {
  2135. #if defined(STBI_SSE2)
  2136. // load and perform the vertical filtering pass
  2137. // this uses 3*x + y = 4*x + (y - x)
  2138. __m128i zero = _mm_setzero_si128();
  2139. __m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i));
  2140. __m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i));
  2141. __m128i farw = _mm_unpacklo_epi8(farb, zero);
  2142. __m128i nearw = _mm_unpacklo_epi8(nearb, zero);
  2143. __m128i diff = _mm_sub_epi16(farw, nearw);
  2144. __m128i nears = _mm_slli_epi16(nearw, 2);
  2145. __m128i curr = _mm_add_epi16(nears, diff); // current row
  2146. // horizontal filter works the same based on shifted vers of current
  2147. // row. "prev" is current row shifted right by 1 pixel; we need to
  2148. // insert the previous pixel value (from t1).
  2149. // "next" is current row shifted left by 1 pixel, with first pixel
  2150. // of next block of 8 pixels added in.
  2151. __m128i prv0 = _mm_slli_si128(curr, 2);
  2152. __m128i nxt0 = _mm_srli_si128(curr, 2);
  2153. __m128i prev = _mm_insert_epi16(prv0, t1, 0);
  2154. __m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7);
  2155. // horizontal filter, polyphase implementation since it's convenient:
  2156. // even pixels = 3*cur + prev = cur*4 + (prev - cur)
  2157. // odd pixels = 3*cur + next = cur*4 + (next - cur)
  2158. // note the shared term.
  2159. __m128i bias = _mm_set1_epi16(8);
  2160. __m128i curs = _mm_slli_epi16(curr, 2);
  2161. __m128i prvd = _mm_sub_epi16(prev, curr);
  2162. __m128i nxtd = _mm_sub_epi16(next, curr);
  2163. __m128i curb = _mm_add_epi16(curs, bias);
  2164. __m128i even = _mm_add_epi16(prvd, curb);
  2165. __m128i odd = _mm_add_epi16(nxtd, curb);
  2166. // interleave even and odd pixels, then undo scaling.
  2167. __m128i int0 = _mm_unpacklo_epi16(even, odd);
  2168. __m128i int1 = _mm_unpackhi_epi16(even, odd);
  2169. __m128i de0 = _mm_srli_epi16(int0, 4);
  2170. __m128i de1 = _mm_srli_epi16(int1, 4);
  2171. // pack and write output
  2172. __m128i outv = _mm_packus_epi16(de0, de1);
  2173. _mm_storeu_si128((__m128i *) (out + i*2), outv);
  2174. #elif defined(STBI_NEON)
  2175. // load and perform the vertical filtering pass
  2176. // this uses 3*x + y = 4*x + (y - x)
  2177. uint8x8_t farb = vld1_u8(in_far + i);
  2178. uint8x8_t nearb = vld1_u8(in_near + i);
  2179. int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb));
  2180. int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2));
  2181. int16x8_t curr = vaddq_s16(nears, diff); // current row
  2182. // horizontal filter works the same based on shifted vers of current
  2183. // row. "prev" is current row shifted right by 1 pixel; we need to
  2184. // insert the previous pixel value (from t1).
  2185. // "next" is current row shifted left by 1 pixel, with first pixel
  2186. // of next block of 8 pixels added in.
  2187. int16x8_t prv0 = vextq_s16(curr, curr, 7);
  2188. int16x8_t nxt0 = vextq_s16(curr, curr, 1);
  2189. int16x8_t prev = vsetq_lane_s16(t1, prv0, 0);
  2190. int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7);
  2191. // horizontal filter, polyphase implementation since it's convenient:
  2192. // even pixels = 3*cur + prev = cur*4 + (prev - cur)
  2193. // odd pixels = 3*cur + next = cur*4 + (next - cur)
  2194. // note the shared term.
  2195. int16x8_t curs = vshlq_n_s16(curr, 2);
  2196. int16x8_t prvd = vsubq_s16(prev, curr);
  2197. int16x8_t nxtd = vsubq_s16(next, curr);
  2198. int16x8_t even = vaddq_s16(curs, prvd);
  2199. int16x8_t odd = vaddq_s16(curs, nxtd);
  2200. // undo scaling and round, then store with even/odd phases interleaved
  2201. uint8x8x2_t o;
  2202. o.val[0] = vqrshrun_n_s16(even, 4);
  2203. o.val[1] = vqrshrun_n_s16(odd, 4);
  2204. vst2_u8(out + i*2, o);
  2205. #endif
  2206. // "previous" value for next iter
  2207. t1 = 3*in_near[i+7] + in_far[i+7];
  2208. }
  2209. t0 = t1;
  2210. t1 = 3*in_near[i] + in_far[i];
  2211. out[i*2] = stbi__div16(3*t1 + t0 + 8);
  2212. for (++i; i < w; ++i) {
  2213. t0 = t1;
  2214. t1 = 3*in_near[i]+in_far[i];
  2215. out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
  2216. out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
  2217. }
  2218. out[w*2-1] = stbi__div4(t1+2);
  2219. STBI_NOTUSED(hs);
  2220. return out;
  2221. }
  2222. #endif
  2223. static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
  2224. {
  2225. // resample with nearest-neighbor
  2226. int i,j;
  2227. STBI_NOTUSED(in_far);
  2228. for (i=0; i < w; ++i)
  2229. for (j=0; j < hs; ++j)
  2230. out[i*hs+j] = in_near[i];
  2231. return out;
  2232. }
  2233. #ifdef STBI_JPEG_OLD
  2234. // this is the same YCbCr-to-RGB calculation that stb_image has used
  2235. // historically before the algorithm changes in 1.49
  2236. #define float2fixed(x) ((int) ((x) * 65536 + 0.5))
  2237. static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step)
  2238. {
  2239. int i;
  2240. for (i=0; i < count; ++i) {
  2241. int y_fixed = (y[i] << 16) + 32768; // rounding
  2242. int r,g,b;
  2243. int cr = pcr[i] - 128;
  2244. int cb = pcb[i] - 128;
  2245. r = y_fixed + cr*float2fixed(1.40200f);
  2246. g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f);
  2247. b = y_fixed + cb*float2fixed(1.77200f);
  2248. r >>= 16;
  2249. g >>= 16;
  2250. b >>= 16;
  2251. if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
  2252. if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
  2253. if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
  2254. out[0] = (stbi_uc)r;
  2255. out[1] = (stbi_uc)g;
  2256. out[2] = (stbi_uc)b;
  2257. out[3] = 255;
  2258. out += step;
  2259. }
  2260. }
  2261. #else
  2262. // this is a reduced-precision calculation of YCbCr-to-RGB introduced
  2263. // to make sure the code produces the same results in both SIMD and scalar
  2264. #define float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8)
  2265. static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step)
  2266. {
  2267. int i;
  2268. for (i=0; i < count; ++i) {
  2269. int y_fixed = (y[i] << 20) + (1<<19); // rounding
  2270. int r,g,b;
  2271. int cr = pcr[i] - 128;
  2272. int cb = pcb[i] - 128;
  2273. r = y_fixed + cr* float2fixed(1.40200f);
  2274. g = y_fixed + (cr*-float2fixed(0.71414f)) + ((cb*-float2fixed(0.34414f)) & 0xffff0000);
  2275. b = y_fixed + cb* float2fixed(1.77200f);
  2276. r >>= 20;
  2277. g >>= 20;
  2278. b >>= 20;
  2279. if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
  2280. if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
  2281. if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
  2282. out[0] = (stbi_uc)r;
  2283. out[1] = (stbi_uc)g;
  2284. out[2] = (stbi_uc)b;
  2285. out[3] = 255;
  2286. out += step;
  2287. }
  2288. }
  2289. #endif
  2290. #if defined(STBI_SSE2) || defined(STBI_NEON)
  2291. static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step)
  2292. {
  2293. int i = 0;
  2294. #ifdef STBI_SSE2
  2295. // step == 3 is pretty ugly on the final interleave, and i'm not convinced
  2296. // it's useful in practice (you wouldn't use it for textures, for example).
  2297. // so just accelerate step == 4 case.
  2298. if (step == 4) {
  2299. // this is a fairly straightforward implementation and not super-optimized.
  2300. __m128i signflip = _mm_set1_epi8(-0x80);
  2301. __m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f));
  2302. __m128i cr_const1 = _mm_set1_epi16( - (short) ( 0.71414f*4096.0f+0.5f));
  2303. __m128i cb_const0 = _mm_set1_epi16( - (short) ( 0.34414f*4096.0f+0.5f));
  2304. __m128i cb_const1 = _mm_set1_epi16( (short) ( 1.77200f*4096.0f+0.5f));
  2305. __m128i y_bias = _mm_set1_epi8((char) (unsigned char) 128);
  2306. __m128i xw = _mm_set1_epi16(255); // alpha channel
  2307. for (; i+7 < count; i += 8) {
  2308. // load
  2309. __m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i));
  2310. __m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i));
  2311. __m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i));
  2312. __m128i cr_biased = _mm_xor_si128(cr_bytes, signflip); // -128
  2313. __m128i cb_biased = _mm_xor_si128(cb_bytes, signflip); // -128
  2314. // unpack to short (and left-shift cr, cb by 8)
  2315. __m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes);
  2316. __m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased);
  2317. __m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased);
  2318. // color transform
  2319. __m128i yws = _mm_srli_epi16(yw, 4);
  2320. __m128i cr0 = _mm_mulhi_epi16(cr_const0, crw);
  2321. __m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw);
  2322. __m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1);
  2323. __m128i cr1 = _mm_mulhi_epi16(crw, cr_const1);
  2324. __m128i rws = _mm_add_epi16(cr0, yws);
  2325. __m128i gwt = _mm_add_epi16(cb0, yws);
  2326. __m128i bws = _mm_add_epi16(yws, cb1);
  2327. __m128i gws = _mm_add_epi16(gwt, cr1);
  2328. // descale
  2329. __m128i rw = _mm_srai_epi16(rws, 4);
  2330. __m128i bw = _mm_srai_epi16(bws, 4);
  2331. __m128i gw = _mm_srai_epi16(gws, 4);
  2332. // back to byte, set up for transpose
  2333. __m128i brb = _mm_packus_epi16(rw, bw);
  2334. __m128i gxb = _mm_packus_epi16(gw, xw);
  2335. // transpose to interleave channels
  2336. __m128i t0 = _mm_unpacklo_epi8(brb, gxb);
  2337. __m128i t1 = _mm_unpackhi_epi8(brb, gxb);
  2338. __m128i o0 = _mm_unpacklo_epi16(t0, t1);
  2339. __m128i o1 = _mm_unpackhi_epi16(t0, t1);
  2340. // store
  2341. _mm_storeu_si128((__m128i *) (out + 0), o0);
  2342. _mm_storeu_si128((__m128i *) (out + 16), o1);
  2343. out += 32;
  2344. }
  2345. }
  2346. #endif
  2347. #ifdef STBI_NEON
  2348. // in this version, step=3 support would be easy to add. but is there demand?
  2349. if (step == 4) {
  2350. // this is a fairly straightforward implementation and not super-optimized.
  2351. uint8x8_t signflip = vdup_n_u8(0x80);
  2352. int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f));
  2353. int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f));
  2354. int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f));
  2355. int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f));
  2356. for (; i+7 < count; i += 8) {
  2357. // load
  2358. uint8x8_t y_bytes = vld1_u8(y + i);
  2359. uint8x8_t cr_bytes = vld1_u8(pcr + i);
  2360. uint8x8_t cb_bytes = vld1_u8(pcb + i);
  2361. int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip));
  2362. int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip));
  2363. // expand to s16
  2364. int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4));
  2365. int16x8_t crw = vshll_n_s8(cr_biased, 7);
  2366. int16x8_t cbw = vshll_n_s8(cb_biased, 7);
  2367. // color transform
  2368. int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0);
  2369. int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0);
  2370. int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1);
  2371. int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1);
  2372. int16x8_t rws = vaddq_s16(yws, cr0);
  2373. int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1);
  2374. int16x8_t bws = vaddq_s16(yws, cb1);
  2375. // undo scaling, round, convert to byte
  2376. uint8x8x4_t o;
  2377. o.val[0] = vqrshrun_n_s16(rws, 4);
  2378. o.val[1] = vqrshrun_n_s16(gws, 4);
  2379. o.val[2] = vqrshrun_n_s16(bws, 4);
  2380. o.val[3] = vdup_n_u8(255);
  2381. // store, interleaving r/g/b/a
  2382. vst4_u8(out, o);
  2383. out += 8*4;
  2384. }
  2385. }
  2386. #endif
  2387. for (; i < count; ++i) {
  2388. int y_fixed = (y[i] << 20) + (1<<19); // rounding
  2389. int r,g,b;
  2390. int cr = pcr[i] - 128;
  2391. int cb = pcb[i] - 128;
  2392. r = y_fixed + cr* float2fixed(1.40200f);
  2393. g = y_fixed + cr*-float2fixed(0.71414f) + ((cb*-float2fixed(0.34414f)) & 0xffff0000);
  2394. b = y_fixed + cb* float2fixed(1.77200f);
  2395. r >>= 20;
  2396. g >>= 20;
  2397. b >>= 20;
  2398. if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
  2399. if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
  2400. if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
  2401. out[0] = (stbi_uc)r;
  2402. out[1] = (stbi_uc)g;
  2403. out[2] = (stbi_uc)b;
  2404. out[3] = 255;
  2405. out += step;
  2406. }
  2407. }
  2408. #endif
  2409. // set up the kernels
  2410. static void stbi__setup_jpeg(stbi__jpeg *j)
  2411. {
  2412. j->idct_block_kernel = stbi__idct_block;
  2413. j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row;
  2414. j->resample_row_hv_2_kernel = stbi__resample_row_hv_2;
  2415. #ifdef STBI_SSE2
  2416. if (stbi__sse2_available()) {
  2417. j->idct_block_kernel = stbi__idct_simd;
  2418. #ifndef STBI_JPEG_OLD
  2419. j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
  2420. #endif
  2421. j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
  2422. }
  2423. #endif
  2424. #ifdef STBI_NEON
  2425. j->idct_block_kernel = stbi__idct_simd;
  2426. #ifndef STBI_JPEG_OLD
  2427. j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
  2428. #endif
  2429. j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
  2430. #endif
  2431. }
  2432. // clean up the temporary component buffers
  2433. static void stbi__cleanup_jpeg(stbi__jpeg *j)
  2434. {
  2435. int i;
  2436. for (i=0; i < j->s->img_n; ++i) {
  2437. if (j->img_comp[i].raw_data) {
  2438. STBI_FREE(j->img_comp[i].raw_data);
  2439. j->img_comp[i].raw_data = NULL;
  2440. j->img_comp[i].data = NULL;
  2441. }
  2442. if (j->img_comp[i].raw_coeff) {
  2443. STBI_FREE(j->img_comp[i].raw_coeff);
  2444. j->img_comp[i].raw_coeff = 0;
  2445. j->img_comp[i].coeff = 0;
  2446. }
  2447. if (j->img_comp[i].linebuf) {
  2448. STBI_FREE(j->img_comp[i].linebuf);
  2449. j->img_comp[i].linebuf = NULL;
  2450. }
  2451. }
  2452. }
  2453. typedef struct
  2454. {
  2455. resample_row_func resample;
  2456. stbi_uc *line0,*line1;
  2457. int hs,vs; // expansion factor in each axis
  2458. int w_lores; // horizontal pixels pre-expansion
  2459. int ystep; // how far through vertical expansion we are
  2460. int ypos; // which pre-expansion row we're on
  2461. } stbi__resample;
  2462. static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
  2463. {
  2464. int n, decode_n;
  2465. z->s->img_n = 0; // make stbi__cleanup_jpeg safe
  2466. // validate req_comp
  2467. if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
  2468. // load a jpeg image from whichever source, but leave in YCbCr format
  2469. if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; }
  2470. // determine actual number of components to generate
  2471. n = req_comp ? req_comp : z->s->img_n;
  2472. if (z->s->img_n == 3 && n < 3)
  2473. decode_n = 1;
  2474. else
  2475. decode_n = z->s->img_n;
  2476. // resample and color-convert
  2477. {
  2478. int k;
  2479. unsigned int i,j;
  2480. stbi_uc *output;
  2481. stbi_uc *coutput[4];
  2482. stbi__resample res_comp[4];
  2483. for (k=0; k < decode_n; ++k) {
  2484. stbi__resample *r = &res_comp[k];
  2485. // allocate line buffer big enough for upsampling off the edges
  2486. // with upsample factor of 4
  2487. z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3);
  2488. if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
  2489. r->hs = z->img_h_max / z->img_comp[k].h;
  2490. r->vs = z->img_v_max / z->img_comp[k].v;
  2491. r->ystep = r->vs >> 1;
  2492. r->w_lores = (z->s->img_x + r->hs-1) / r->hs;
  2493. r->ypos = 0;
  2494. r->line0 = r->line1 = z->img_comp[k].data;
  2495. if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
  2496. else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2;
  2497. else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2;
  2498. else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel;
  2499. else r->resample = stbi__resample_row_generic;
  2500. }
  2501. // can't error after this so, this is safe
  2502. output = (stbi_uc *) stbi__malloc(n * z->s->img_x * z->s->img_y + 1);
  2503. if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
  2504. // now go ahead and resample
  2505. for (j=0; j < z->s->img_y; ++j) {
  2506. stbi_uc *out = output + n * z->s->img_x * j;
  2507. for (k=0; k < decode_n; ++k) {
  2508. stbi__resample *r = &res_comp[k];
  2509. int y_bot = r->ystep >= (r->vs >> 1);
  2510. coutput[k] = r->resample(z->img_comp[k].linebuf,
  2511. y_bot ? r->line1 : r->line0,
  2512. y_bot ? r->line0 : r->line1,
  2513. r->w_lores, r->hs);
  2514. if (++r->ystep >= r->vs) {
  2515. r->ystep = 0;
  2516. r->line0 = r->line1;
  2517. if (++r->ypos < z->img_comp[k].y)
  2518. r->line1 += z->img_comp[k].w2;
  2519. }
  2520. }
  2521. if (n >= 3) {
  2522. stbi_uc *y = coutput[0];
  2523. if (z->s->img_n == 3) {
  2524. z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
  2525. } else
  2526. for (i=0; i < z->s->img_x; ++i) {
  2527. out[0] = out[1] = out[2] = y[i];
  2528. out[3] = 255; // not used if n==3
  2529. out += n;
  2530. }
  2531. } else {
  2532. stbi_uc *y = coutput[0];
  2533. if (n == 1)
  2534. for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
  2535. else
  2536. for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255;
  2537. }
  2538. }
  2539. stbi__cleanup_jpeg(z);
  2540. *out_x = z->s->img_x;
  2541. *out_y = z->s->img_y;
  2542. if (comp) *comp = z->s->img_n; // report original components, not output
  2543. return output;
  2544. }
  2545. }
  2546. static unsigned char *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  2547. {
  2548. stbi__jpeg j;
  2549. j.s = s;
  2550. stbi__setup_jpeg(&j);
  2551. return load_jpeg_image(&j, x,y,comp,req_comp);
  2552. }
  2553. static int stbi__jpeg_test(stbi__context *s)
  2554. {
  2555. int r;
  2556. stbi__jpeg j;
  2557. j.s = s;
  2558. stbi__setup_jpeg(&j);
  2559. r = stbi__decode_jpeg_header(&j, STBI__SCAN_type);
  2560. stbi__rewind(s);
  2561. return r;
  2562. }
  2563. static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp)
  2564. {
  2565. if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) {
  2566. stbi__rewind( j->s );
  2567. return 0;
  2568. }
  2569. if (x) *x = j->s->img_x;
  2570. if (y) *y = j->s->img_y;
  2571. if (comp) *comp = j->s->img_n;
  2572. return 1;
  2573. }
  2574. static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp)
  2575. {
  2576. stbi__jpeg j;
  2577. j.s = s;
  2578. return stbi__jpeg_info_raw(&j, x, y, comp);
  2579. }
  2580. #endif
  2581. // public domain zlib decode v0.2 Sean Barrett 2006-11-18
  2582. // simple implementation
  2583. // - all input must be provided in an upfront buffer
  2584. // - all output is written to a single output buffer (can malloc/realloc)
  2585. // performance
  2586. // - fast huffman
  2587. #ifndef STBI_NO_ZLIB
  2588. // fast-way is faster to check than jpeg huffman, but slow way is slower
  2589. #define STBI__ZFAST_BITS 9 // accelerate all cases in default tables
  2590. #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
  2591. // zlib-style huffman encoding
  2592. // (jpegs packs from left, zlib from right, so can't share code)
  2593. typedef struct
  2594. {
  2595. stbi__uint16 fast[1 << STBI__ZFAST_BITS];
  2596. stbi__uint16 firstcode[16];
  2597. int maxcode[17];
  2598. stbi__uint16 firstsymbol[16];
  2599. stbi_uc size[288];
  2600. stbi__uint16 value[288];
  2601. } stbi__zhuffman;
  2602. stbi_inline static int stbi__bitreverse16(int n)
  2603. {
  2604. n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
  2605. n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
  2606. n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
  2607. n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
  2608. return n;
  2609. }
  2610. stbi_inline static int stbi__bit_reverse(int v, int bits)
  2611. {
  2612. STBI_ASSERT(bits <= 16);
  2613. // to bit reverse n bits, reverse 16 and shift
  2614. // e.g. 11 bits, bit reverse and shift away 5
  2615. return stbi__bitreverse16(v) >> (16-bits);
  2616. }
  2617. static int stbi__zbuild_huffman(stbi__zhuffman *z, stbi_uc *sizelist, int num)
  2618. {
  2619. int i,k=0;
  2620. int code, next_code[16], sizes[17];
  2621. // DEFLATE spec for generating codes
  2622. memset(sizes, 0, sizeof(sizes));
  2623. memset(z->fast, 0, sizeof(z->fast));
  2624. for (i=0; i < num; ++i)
  2625. ++sizes[sizelist[i]];
  2626. sizes[0] = 0;
  2627. for (i=1; i < 16; ++i)
  2628. if (sizes[i] > (1 << i))
  2629. return stbi__err("bad sizes", "Corrupt PNG");
  2630. code = 0;
  2631. for (i=1; i < 16; ++i) {
  2632. next_code[i] = code;
  2633. z->firstcode[i] = (stbi__uint16) code;
  2634. z->firstsymbol[i] = (stbi__uint16) k;
  2635. code = (code + sizes[i]);
  2636. if (sizes[i])
  2637. if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG");
  2638. z->maxcode[i] = code << (16-i); // preshift for inner loop
  2639. code <<= 1;
  2640. k += sizes[i];
  2641. }
  2642. z->maxcode[16] = 0x10000; // sentinel
  2643. for (i=0; i < num; ++i) {
  2644. int s = sizelist[i];
  2645. if (s) {
  2646. int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
  2647. stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i);
  2648. z->size [c] = (stbi_uc ) s;
  2649. z->value[c] = (stbi__uint16) i;
  2650. if (s <= STBI__ZFAST_BITS) {
  2651. int k = stbi__bit_reverse(next_code[s],s);
  2652. while (k < (1 << STBI__ZFAST_BITS)) {
  2653. z->fast[k] = fastv;
  2654. k += (1 << s);
  2655. }
  2656. }
  2657. ++next_code[s];
  2658. }
  2659. }
  2660. return 1;
  2661. }
  2662. // zlib-from-memory implementation for PNG reading
  2663. // because PNG allows splitting the zlib stream arbitrarily,
  2664. // and it's annoying structurally to have PNG call ZLIB call PNG,
  2665. // we require PNG read all the IDATs and combine them into a single
  2666. // memory buffer
  2667. typedef struct
  2668. {
  2669. stbi_uc *zbuffer, *zbuffer_end;
  2670. int num_bits;
  2671. stbi__uint32 code_buffer;
  2672. char *zout;
  2673. char *zout_start;
  2674. char *zout_end;
  2675. int z_expandable;
  2676. stbi__zhuffman z_length, z_distance;
  2677. } stbi__zbuf;
  2678. stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z)
  2679. {
  2680. if (z->zbuffer >= z->zbuffer_end) return 0;
  2681. return *z->zbuffer++;
  2682. }
  2683. static void stbi__fill_bits(stbi__zbuf *z)
  2684. {
  2685. do {
  2686. STBI_ASSERT(z->code_buffer < (1U << z->num_bits));
  2687. z->code_buffer |= stbi__zget8(z) << z->num_bits;
  2688. z->num_bits += 8;
  2689. } while (z->num_bits <= 24);
  2690. }
  2691. stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n)
  2692. {
  2693. unsigned int k;
  2694. if (z->num_bits < n) stbi__fill_bits(z);
  2695. k = z->code_buffer & ((1 << n) - 1);
  2696. z->code_buffer >>= n;
  2697. z->num_bits -= n;
  2698. return k;
  2699. }
  2700. static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)
  2701. {
  2702. int b,s,k;
  2703. // not resolved by fast table, so compute it the slow way
  2704. // use jpeg approach, which requires MSbits at top
  2705. k = stbi__bit_reverse(a->code_buffer, 16);
  2706. for (s=STBI__ZFAST_BITS+1; ; ++s)
  2707. if (k < z->maxcode[s])
  2708. break;
  2709. if (s == 16) return -1; // invalid code!
  2710. // code size is s, so:
  2711. b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
  2712. STBI_ASSERT(z->size[b] == s);
  2713. a->code_buffer >>= s;
  2714. a->num_bits -= s;
  2715. return z->value[b];
  2716. }
  2717. stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
  2718. {
  2719. int b,s;
  2720. if (a->num_bits < 16) stbi__fill_bits(a);
  2721. b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
  2722. if (b) {
  2723. s = b >> 9;
  2724. a->code_buffer >>= s;
  2725. a->num_bits -= s;
  2726. return b & 511;
  2727. }
  2728. return stbi__zhuffman_decode_slowpath(a, z);
  2729. }
  2730. static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes
  2731. {
  2732. char *q;
  2733. int cur, limit;
  2734. z->zout = zout;
  2735. if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
  2736. cur = (int) (z->zout - z->zout_start);
  2737. limit = (int) (z->zout_end - z->zout_start);
  2738. while (cur + n > limit)
  2739. limit *= 2;
  2740. q = (char *) STBI_REALLOC(z->zout_start, limit);
  2741. if (q == NULL) return stbi__err("outofmem", "Out of memory");
  2742. z->zout_start = q;
  2743. z->zout = q + cur;
  2744. z->zout_end = q + limit;
  2745. return 1;
  2746. }
  2747. static int stbi__zlength_base[31] = {
  2748. 3,4,5,6,7,8,9,10,11,13,
  2749. 15,17,19,23,27,31,35,43,51,59,
  2750. 67,83,99,115,131,163,195,227,258,0,0 };
  2751. static int stbi__zlength_extra[31]=
  2752. { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
  2753. static int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
  2754. 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
  2755. static int stbi__zdist_extra[32] =
  2756. { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
  2757. static int stbi__parse_huffman_block(stbi__zbuf *a)
  2758. {
  2759. char *zout = a->zout;
  2760. for(;;) {
  2761. int z = stbi__zhuffman_decode(a, &a->z_length);
  2762. if (z < 256) {
  2763. if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes
  2764. if (zout >= a->zout_end) {
  2765. if (!stbi__zexpand(a, zout, 1)) return 0;
  2766. zout = a->zout;
  2767. }
  2768. *zout++ = (char) z;
  2769. } else {
  2770. stbi_uc *p;
  2771. int len,dist;
  2772. if (z == 256) {
  2773. a->zout = zout;
  2774. return 1;
  2775. }
  2776. z -= 257;
  2777. len = stbi__zlength_base[z];
  2778. if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
  2779. z = stbi__zhuffman_decode(a, &a->z_distance);
  2780. if (z < 0) return stbi__err("bad huffman code","Corrupt PNG");
  2781. dist = stbi__zdist_base[z];
  2782. if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
  2783. if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");
  2784. if (zout + len > a->zout_end) {
  2785. if (!stbi__zexpand(a, zout, len)) return 0;
  2786. zout = a->zout;
  2787. }
  2788. p = (stbi_uc *) (zout - dist);
  2789. if (dist == 1) { // run of one byte; common in images.
  2790. stbi_uc v = *p;
  2791. if (len) { do *zout++ = v; while (--len); }
  2792. } else {
  2793. if (len) { do *zout++ = *p++; while (--len); }
  2794. }
  2795. }
  2796. }
  2797. }
  2798. static int stbi__compute_huffman_codes(stbi__zbuf *a)
  2799. {
  2800. static stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
  2801. stbi__zhuffman z_codelength;
  2802. stbi_uc lencodes[286+32+137];//padding for maximum single op
  2803. stbi_uc codelength_sizes[19];
  2804. int i,n;
  2805. int hlit = stbi__zreceive(a,5) + 257;
  2806. int hdist = stbi__zreceive(a,5) + 1;
  2807. int hclen = stbi__zreceive(a,4) + 4;
  2808. memset(codelength_sizes, 0, sizeof(codelength_sizes));
  2809. for (i=0; i < hclen; ++i) {
  2810. int s = stbi__zreceive(a,3);
  2811. codelength_sizes[length_dezigzag[i]] = (stbi_uc) s;
  2812. }
  2813. if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
  2814. n = 0;
  2815. while (n < hlit + hdist) {
  2816. int c = stbi__zhuffman_decode(a, &z_codelength);
  2817. if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG");
  2818. if (c < 16)
  2819. lencodes[n++] = (stbi_uc) c;
  2820. else if (c == 16) {
  2821. c = stbi__zreceive(a,2)+3;
  2822. memset(lencodes+n, lencodes[n-1], c);
  2823. n += c;
  2824. } else if (c == 17) {
  2825. c = stbi__zreceive(a,3)+3;
  2826. memset(lencodes+n, 0, c);
  2827. n += c;
  2828. } else {
  2829. STBI_ASSERT(c == 18);
  2830. c = stbi__zreceive(a,7)+11;
  2831. memset(lencodes+n, 0, c);
  2832. n += c;
  2833. }
  2834. }
  2835. if (n != hlit+hdist) return stbi__err("bad codelengths","Corrupt PNG");
  2836. if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
  2837. if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
  2838. return 1;
  2839. }
  2840. static int stbi__parse_uncomperssed_block(stbi__zbuf *a)
  2841. {
  2842. stbi_uc header[4];
  2843. int len,nlen,k;
  2844. if (a->num_bits & 7)
  2845. stbi__zreceive(a, a->num_bits & 7); // discard
  2846. // drain the bit-packed data into header
  2847. k = 0;
  2848. while (a->num_bits > 0) {
  2849. header[k++] = (stbi_uc) (a->code_buffer & 255); // suppress MSVC run-time check
  2850. a->code_buffer >>= 8;
  2851. a->num_bits -= 8;
  2852. }
  2853. STBI_ASSERT(a->num_bits == 0);
  2854. // now fill header the normal way
  2855. while (k < 4)
  2856. header[k++] = stbi__zget8(a);
  2857. len = header[1] * 256 + header[0];
  2858. nlen = header[3] * 256 + header[2];
  2859. if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG");
  2860. if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG");
  2861. if (a->zout + len > a->zout_end)
  2862. if (!stbi__zexpand(a, a->zout, len)) return 0;
  2863. memcpy(a->zout, a->zbuffer, len);
  2864. a->zbuffer += len;
  2865. a->zout += len;
  2866. return 1;
  2867. }
  2868. static int stbi__parse_zlib_header(stbi__zbuf *a)
  2869. {
  2870. int cmf = stbi__zget8(a);
  2871. int cm = cmf & 15;
  2872. /* int cinfo = cmf >> 4; */
  2873. int flg = stbi__zget8(a);
  2874. if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec
  2875. if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
  2876. if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png
  2877. // window = 1 << (8 + cinfo)... but who cares, we fully buffer output
  2878. return 1;
  2879. }
  2880. // @TODO: should statically initialize these for optimal thread safety
  2881. static stbi_uc stbi__zdefault_length[288], stbi__zdefault_distance[32];
  2882. static void stbi__init_zdefaults(void)
  2883. {
  2884. int i; // use <= to match clearly with spec
  2885. for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8;
  2886. for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9;
  2887. for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7;
  2888. for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8;
  2889. for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5;
  2890. }
  2891. static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
  2892. {
  2893. int final, type;
  2894. if (parse_header)
  2895. if (!stbi__parse_zlib_header(a)) return 0;
  2896. a->num_bits = 0;
  2897. a->code_buffer = 0;
  2898. do {
  2899. final = stbi__zreceive(a,1);
  2900. type = stbi__zreceive(a,2);
  2901. if (type == 0) {
  2902. if (!stbi__parse_uncomperssed_block(a)) return 0;
  2903. } else if (type == 3) {
  2904. return 0;
  2905. } else {
  2906. if (type == 1) {
  2907. // use fixed code lengths
  2908. if (!stbi__zdefault_distance[31]) stbi__init_zdefaults();
  2909. if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288)) return 0;
  2910. if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0;
  2911. } else {
  2912. if (!stbi__compute_huffman_codes(a)) return 0;
  2913. }
  2914. if (!stbi__parse_huffman_block(a)) return 0;
  2915. }
  2916. } while (!final);
  2917. return 1;
  2918. }
  2919. static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header)
  2920. {
  2921. a->zout_start = obuf;
  2922. a->zout = obuf;
  2923. a->zout_end = obuf + olen;
  2924. a->z_expandable = exp;
  2925. return stbi__parse_zlib(a, parse_header);
  2926. }
  2927. STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
  2928. {
  2929. stbi__zbuf a;
  2930. char *p = (char *) stbi__malloc(initial_size);
  2931. if (p == NULL) return NULL;
  2932. a.zbuffer = (stbi_uc *) buffer;
  2933. a.zbuffer_end = (stbi_uc *) buffer + len;
  2934. if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
  2935. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  2936. return a.zout_start;
  2937. } else {
  2938. STBI_FREE(a.zout_start);
  2939. return NULL;
  2940. }
  2941. }
  2942. STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
  2943. {
  2944. return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
  2945. }
  2946. STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
  2947. {
  2948. stbi__zbuf a;
  2949. char *p = (char *) stbi__malloc(initial_size);
  2950. if (p == NULL) return NULL;
  2951. a.zbuffer = (stbi_uc *) buffer;
  2952. a.zbuffer_end = (stbi_uc *) buffer + len;
  2953. if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
  2954. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  2955. return a.zout_start;
  2956. } else {
  2957. STBI_FREE(a.zout_start);
  2958. return NULL;
  2959. }
  2960. }
  2961. STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
  2962. {
  2963. stbi__zbuf a;
  2964. a.zbuffer = (stbi_uc *) ibuffer;
  2965. a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
  2966. if (stbi__do_zlib(&a, obuffer, olen, 0, 1))
  2967. return (int) (a.zout - a.zout_start);
  2968. else
  2969. return -1;
  2970. }
  2971. STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
  2972. {
  2973. stbi__zbuf a;
  2974. char *p = (char *) stbi__malloc(16384);
  2975. if (p == NULL) return NULL;
  2976. a.zbuffer = (stbi_uc *) buffer;
  2977. a.zbuffer_end = (stbi_uc *) buffer+len;
  2978. if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
  2979. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  2980. return a.zout_start;
  2981. } else {
  2982. STBI_FREE(a.zout_start);
  2983. return NULL;
  2984. }
  2985. }
  2986. STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
  2987. {
  2988. stbi__zbuf a;
  2989. a.zbuffer = (stbi_uc *) ibuffer;
  2990. a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
  2991. if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
  2992. return (int) (a.zout - a.zout_start);
  2993. else
  2994. return -1;
  2995. }
  2996. #endif
  2997. // public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18
  2998. // simple implementation
  2999. // - only 8-bit samples
  3000. // - no CRC checking
  3001. // - allocates lots of intermediate memory
  3002. // - avoids problem of streaming data between subsystems
  3003. // - avoids explicit window management
  3004. // performance
  3005. // - uses stb_zlib, a PD zlib implementation with fast huffman decoding
  3006. #ifndef STBI_NO_PNG
  3007. typedef struct
  3008. {
  3009. stbi__uint32 length;
  3010. stbi__uint32 type;
  3011. } stbi__pngchunk;
  3012. static stbi__pngchunk stbi__get_chunk_header(stbi__context *s)
  3013. {
  3014. stbi__pngchunk c;
  3015. c.length = stbi__get32be(s);
  3016. c.type = stbi__get32be(s);
  3017. return c;
  3018. }
  3019. static int stbi__check_png_header(stbi__context *s)
  3020. {
  3021. static stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };
  3022. int i;
  3023. for (i=0; i < 8; ++i)
  3024. if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG");
  3025. return 1;
  3026. }
  3027. typedef struct
  3028. {
  3029. stbi__context *s;
  3030. stbi_uc *idata, *expanded, *out;
  3031. } stbi__png;
  3032. enum {
  3033. STBI__F_none=0,
  3034. STBI__F_sub=1,
  3035. STBI__F_up=2,
  3036. STBI__F_avg=3,
  3037. STBI__F_paeth=4,
  3038. // synthetic filters used for first scanline to avoid needing a dummy row of 0s
  3039. STBI__F_avg_first,
  3040. STBI__F_paeth_first
  3041. };
  3042. static stbi_uc first_row_filter[5] =
  3043. {
  3044. STBI__F_none,
  3045. STBI__F_sub,
  3046. STBI__F_none,
  3047. STBI__F_avg_first,
  3048. STBI__F_paeth_first
  3049. };
  3050. static int stbi__paeth(int a, int b, int c)
  3051. {
  3052. int p = a + b - c;
  3053. int pa = abs(p-a);
  3054. int pb = abs(p-b);
  3055. int pc = abs(p-c);
  3056. if (pa <= pb && pa <= pc) return a;
  3057. if (pb <= pc) return b;
  3058. return c;
  3059. }
  3060. static stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };
  3061. // create the png data from post-deflated data
  3062. static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color)
  3063. {
  3064. stbi__context *s = a->s;
  3065. stbi__uint32 i,j,stride = x*out_n;
  3066. stbi__uint32 img_len, img_width_bytes;
  3067. int k;
  3068. int img_n = s->img_n; // copy it into a local for later
  3069. STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1);
  3070. a->out = (stbi_uc *) stbi__malloc(x * y * out_n); // extra bytes to write off the end into
  3071. if (!a->out) return stbi__err("outofmem", "Out of memory");
  3072. img_width_bytes = (((img_n * x * depth) + 7) >> 3);
  3073. img_len = (img_width_bytes + 1) * y;
  3074. if (s->img_x == x && s->img_y == y) {
  3075. if (raw_len != img_len) return stbi__err("not enough pixels","Corrupt PNG");
  3076. } else { // interlaced:
  3077. if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");
  3078. }
  3079. for (j=0; j < y; ++j) {
  3080. stbi_uc *cur = a->out + stride*j;
  3081. stbi_uc *prior = cur - stride;
  3082. int filter = *raw++;
  3083. int filter_bytes = img_n;
  3084. int width = x;
  3085. if (filter > 4)
  3086. return stbi__err("invalid filter","Corrupt PNG");
  3087. if (depth < 8) {
  3088. STBI_ASSERT(img_width_bytes <= x);
  3089. cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place
  3090. filter_bytes = 1;
  3091. width = img_width_bytes;
  3092. }
  3093. // if first row, use special filter that doesn't sample previous row
  3094. if (j == 0) filter = first_row_filter[filter];
  3095. // handle first byte explicitly
  3096. for (k=0; k < filter_bytes; ++k) {
  3097. switch (filter) {
  3098. case STBI__F_none : cur[k] = raw[k]; break;
  3099. case STBI__F_sub : cur[k] = raw[k]; break;
  3100. case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
  3101. case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break;
  3102. case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break;
  3103. case STBI__F_avg_first : cur[k] = raw[k]; break;
  3104. case STBI__F_paeth_first: cur[k] = raw[k]; break;
  3105. }
  3106. }
  3107. if (depth == 8) {
  3108. if (img_n != out_n)
  3109. cur[img_n] = 255; // first pixel
  3110. raw += img_n;
  3111. cur += out_n;
  3112. prior += out_n;
  3113. } else {
  3114. raw += 1;
  3115. cur += 1;
  3116. prior += 1;
  3117. }
  3118. // this is a little gross, so that we don't switch per-pixel or per-component
  3119. if (depth < 8 || img_n == out_n) {
  3120. int nk = (width - 1)*img_n;
  3121. #define CASE(f) \
  3122. case f: \
  3123. for (k=0; k < nk; ++k)
  3124. switch (filter) {
  3125. // "none" filter turns into a memcpy here; make that explicit.
  3126. case STBI__F_none: memcpy(cur, raw, nk); break;
  3127. CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); break;
  3128. CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
  3129. CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); break;
  3130. CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); break;
  3131. CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); break;
  3132. CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); break;
  3133. }
  3134. #undef CASE
  3135. raw += nk;
  3136. } else {
  3137. STBI_ASSERT(img_n+1 == out_n);
  3138. #define CASE(f) \
  3139. case f: \
  3140. for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \
  3141. for (k=0; k < img_n; ++k)
  3142. switch (filter) {
  3143. CASE(STBI__F_none) cur[k] = raw[k]; break;
  3144. CASE(STBI__F_sub) cur[k] = STBI__BYTECAST(raw[k] + cur[k-out_n]); break;
  3145. CASE(STBI__F_up) cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
  3146. CASE(STBI__F_avg) cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-out_n])>>1)); break;
  3147. CASE(STBI__F_paeth) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;
  3148. CASE(STBI__F_avg_first) cur[k] = STBI__BYTECAST(raw[k] + (cur[k-out_n] >> 1)); break;
  3149. CASE(STBI__F_paeth_first) cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-out_n],0,0)); break;
  3150. }
  3151. #undef CASE
  3152. }
  3153. }
  3154. // we make a separate pass to expand bits to pixels; for performance,
  3155. // this could run two scanlines behind the above code, so it won't
  3156. // intefere with filtering but will still be in the cache.
  3157. if (depth < 8) {
  3158. for (j=0; j < y; ++j) {
  3159. stbi_uc *cur = a->out + stride*j;
  3160. stbi_uc *in = a->out + stride*j + x*out_n - img_width_bytes;
  3161. // unpack 1/2/4-bit into a 8-bit buffer. allows us to keep the common 8-bit path optimal at minimal cost for 1/2/4-bit
  3162. // png guarante byte alignment, if width is not multiple of 8/4/2 we'll decode dummy trailing data that will be skipped in the later loop
  3163. stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range
  3164. // note that the final byte might overshoot and write more data than desired.
  3165. // we can allocate enough data that this never writes out of memory, but it
  3166. // could also overwrite the next scanline. can it overwrite non-empty data
  3167. // on the next scanline? yes, consider 1-pixel-wide scanlines with 1-bit-per-pixel.
  3168. // so we need to explicitly clamp the final ones
  3169. if (depth == 4) {
  3170. for (k=x*img_n; k >= 2; k-=2, ++in) {
  3171. *cur++ = scale * ((*in >> 4) );
  3172. *cur++ = scale * ((*in ) & 0x0f);
  3173. }
  3174. if (k > 0) *cur++ = scale * ((*in >> 4) );
  3175. } else if (depth == 2) {
  3176. for (k=x*img_n; k >= 4; k-=4, ++in) {
  3177. *cur++ = scale * ((*in >> 6) );
  3178. *cur++ = scale * ((*in >> 4) & 0x03);
  3179. *cur++ = scale * ((*in >> 2) & 0x03);
  3180. *cur++ = scale * ((*in ) & 0x03);
  3181. }
  3182. if (k > 0) *cur++ = scale * ((*in >> 6) );
  3183. if (k > 1) *cur++ = scale * ((*in >> 4) & 0x03);
  3184. if (k > 2) *cur++ = scale * ((*in >> 2) & 0x03);
  3185. } else if (depth == 1) {
  3186. for (k=x*img_n; k >= 8; k-=8, ++in) {
  3187. *cur++ = scale * ((*in >> 7) );
  3188. *cur++ = scale * ((*in >> 6) & 0x01);
  3189. *cur++ = scale * ((*in >> 5) & 0x01);
  3190. *cur++ = scale * ((*in >> 4) & 0x01);
  3191. *cur++ = scale * ((*in >> 3) & 0x01);
  3192. *cur++ = scale * ((*in >> 2) & 0x01);
  3193. *cur++ = scale * ((*in >> 1) & 0x01);
  3194. *cur++ = scale * ((*in ) & 0x01);
  3195. }
  3196. if (k > 0) *cur++ = scale * ((*in >> 7) );
  3197. if (k > 1) *cur++ = scale * ((*in >> 6) & 0x01);
  3198. if (k > 2) *cur++ = scale * ((*in >> 5) & 0x01);
  3199. if (k > 3) *cur++ = scale * ((*in >> 4) & 0x01);
  3200. if (k > 4) *cur++ = scale * ((*in >> 3) & 0x01);
  3201. if (k > 5) *cur++ = scale * ((*in >> 2) & 0x01);
  3202. if (k > 6) *cur++ = scale * ((*in >> 1) & 0x01);
  3203. }
  3204. if (img_n != out_n) {
  3205. // insert alpha = 255
  3206. stbi_uc *cur = a->out + stride*j;
  3207. int i;
  3208. if (img_n == 1) {
  3209. for (i=x-1; i >= 0; --i) {
  3210. cur[i*2+1] = 255;
  3211. cur[i*2+0] = cur[i];
  3212. }
  3213. } else {
  3214. STBI_ASSERT(img_n == 3);
  3215. for (i=x-1; i >= 0; --i) {
  3216. cur[i*4+3] = 255;
  3217. cur[i*4+2] = cur[i*3+2];
  3218. cur[i*4+1] = cur[i*3+1];
  3219. cur[i*4+0] = cur[i*3+0];
  3220. }
  3221. }
  3222. }
  3223. }
  3224. }
  3225. return 1;
  3226. }
  3227. static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced)
  3228. {
  3229. stbi_uc *final;
  3230. int p;
  3231. if (!interlaced)
  3232. return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color);
  3233. // de-interlacing
  3234. final = (stbi_uc *) stbi__malloc(a->s->img_x * a->s->img_y * out_n);
  3235. for (p=0; p < 7; ++p) {
  3236. int xorig[] = { 0,4,0,2,0,1,0 };
  3237. int yorig[] = { 0,0,4,0,2,0,1 };
  3238. int xspc[] = { 8,8,4,4,2,2,1 };
  3239. int yspc[] = { 8,8,8,4,4,2,2 };
  3240. int i,j,x,y;
  3241. // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
  3242. x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];
  3243. y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];
  3244. if (x && y) {
  3245. stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y;
  3246. if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) {
  3247. STBI_FREE(final);
  3248. return 0;
  3249. }
  3250. for (j=0; j < y; ++j) {
  3251. for (i=0; i < x; ++i) {
  3252. int out_y = j*yspc[p]+yorig[p];
  3253. int out_x = i*xspc[p]+xorig[p];
  3254. memcpy(final + out_y*a->s->img_x*out_n + out_x*out_n,
  3255. a->out + (j*x+i)*out_n, out_n);
  3256. }
  3257. }
  3258. STBI_FREE(a->out);
  3259. image_data += img_len;
  3260. image_data_len -= img_len;
  3261. }
  3262. }
  3263. a->out = final;
  3264. return 1;
  3265. }
  3266. static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n)
  3267. {
  3268. stbi__context *s = z->s;
  3269. stbi__uint32 i, pixel_count = s->img_x * s->img_y;
  3270. stbi_uc *p = z->out;
  3271. // compute color-based transparency, assuming we've
  3272. // already got 255 as the alpha value in the output
  3273. STBI_ASSERT(out_n == 2 || out_n == 4);
  3274. if (out_n == 2) {
  3275. for (i=0; i < pixel_count; ++i) {
  3276. p[1] = (p[0] == tc[0] ? 0 : 255);
  3277. p += 2;
  3278. }
  3279. } else {
  3280. for (i=0; i < pixel_count; ++i) {
  3281. if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
  3282. p[3] = 0;
  3283. p += 4;
  3284. }
  3285. }
  3286. return 1;
  3287. }
  3288. static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n)
  3289. {
  3290. stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
  3291. stbi_uc *p, *temp_out, *orig = a->out;
  3292. p = (stbi_uc *) stbi__malloc(pixel_count * pal_img_n);
  3293. if (p == NULL) return stbi__err("outofmem", "Out of memory");
  3294. // between here and free(out) below, exitting would leak
  3295. temp_out = p;
  3296. if (pal_img_n == 3) {
  3297. for (i=0; i < pixel_count; ++i) {
  3298. int n = orig[i]*4;
  3299. p[0] = palette[n ];
  3300. p[1] = palette[n+1];
  3301. p[2] = palette[n+2];
  3302. p += 3;
  3303. }
  3304. } else {
  3305. for (i=0; i < pixel_count; ++i) {
  3306. int n = orig[i]*4;
  3307. p[0] = palette[n ];
  3308. p[1] = palette[n+1];
  3309. p[2] = palette[n+2];
  3310. p[3] = palette[n+3];
  3311. p += 4;
  3312. }
  3313. }
  3314. STBI_FREE(a->out);
  3315. a->out = temp_out;
  3316. STBI_NOTUSED(len);
  3317. return 1;
  3318. }
  3319. static int stbi__unpremultiply_on_load = 0;
  3320. static int stbi__de_iphone_flag = 0;
  3321. STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
  3322. {
  3323. stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply;
  3324. }
  3325. STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
  3326. {
  3327. stbi__de_iphone_flag = flag_true_if_should_convert;
  3328. }
  3329. static void stbi__de_iphone(stbi__png *z)
  3330. {
  3331. stbi__context *s = z->s;
  3332. stbi__uint32 i, pixel_count = s->img_x * s->img_y;
  3333. stbi_uc *p = z->out;
  3334. if (s->img_out_n == 3) { // convert bgr to rgb
  3335. for (i=0; i < pixel_count; ++i) {
  3336. stbi_uc t = p[0];
  3337. p[0] = p[2];
  3338. p[2] = t;
  3339. p += 3;
  3340. }
  3341. } else {
  3342. STBI_ASSERT(s->img_out_n == 4);
  3343. if (stbi__unpremultiply_on_load) {
  3344. // convert bgr to rgb and unpremultiply
  3345. for (i=0; i < pixel_count; ++i) {
  3346. stbi_uc a = p[3];
  3347. stbi_uc t = p[0];
  3348. if (a) {
  3349. p[0] = p[2] * 255 / a;
  3350. p[1] = p[1] * 255 / a;
  3351. p[2] = t * 255 / a;
  3352. } else {
  3353. p[0] = p[2];
  3354. p[2] = t;
  3355. }
  3356. p += 4;
  3357. }
  3358. } else {
  3359. // convert bgr to rgb
  3360. for (i=0; i < pixel_count; ++i) {
  3361. stbi_uc t = p[0];
  3362. p[0] = p[2];
  3363. p[2] = t;
  3364. p += 4;
  3365. }
  3366. }
  3367. }
  3368. }
  3369. #define STBI__PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
  3370. static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
  3371. {
  3372. stbi_uc palette[1024], pal_img_n=0;
  3373. stbi_uc has_trans=0, tc[3];
  3374. stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
  3375. int first=1,k,interlace=0, color=0, depth=0, is_iphone=0;
  3376. stbi__context *s = z->s;
  3377. z->expanded = NULL;
  3378. z->idata = NULL;
  3379. z->out = NULL;
  3380. if (!stbi__check_png_header(s)) return 0;
  3381. if (scan == STBI__SCAN_type) return 1;
  3382. for (;;) {
  3383. stbi__pngchunk c = stbi__get_chunk_header(s);
  3384. switch (c.type) {
  3385. case STBI__PNG_TYPE('C','g','B','I'):
  3386. is_iphone = 1;
  3387. stbi__skip(s, c.length);
  3388. break;
  3389. case STBI__PNG_TYPE('I','H','D','R'): {
  3390. int comp,filter;
  3391. if (!first) return stbi__err("multiple IHDR","Corrupt PNG");
  3392. first = 0;
  3393. if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG");
  3394. s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
  3395. s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
  3396. depth = stbi__get8(s); if (depth != 1 && depth != 2 && depth != 4 && depth != 8) return stbi__err("1/2/4/8-bit only","PNG not supported: 1/2/4/8-bit only");
  3397. color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
  3398. if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");
  3399. comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");
  3400. filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
  3401. interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
  3402. if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG");
  3403. if (!pal_img_n) {
  3404. s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
  3405. if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
  3406. if (scan == STBI__SCAN_header) return 1;
  3407. } else {
  3408. // if paletted, then pal_n is our final components, and
  3409. // img_n is # components to decompress/filter.
  3410. s->img_n = 1;
  3411. if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG");
  3412. // if SCAN_header, have to scan to see if we have a tRNS
  3413. }
  3414. break;
  3415. }
  3416. case STBI__PNG_TYPE('P','L','T','E'): {
  3417. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  3418. if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG");
  3419. pal_len = c.length / 3;
  3420. if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG");
  3421. for (i=0; i < pal_len; ++i) {
  3422. palette[i*4+0] = stbi__get8(s);
  3423. palette[i*4+1] = stbi__get8(s);
  3424. palette[i*4+2] = stbi__get8(s);
  3425. palette[i*4+3] = 255;
  3426. }
  3427. break;
  3428. }
  3429. case STBI__PNG_TYPE('t','R','N','S'): {
  3430. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  3431. if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG");
  3432. if (pal_img_n) {
  3433. if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; }
  3434. if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG");
  3435. if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG");
  3436. pal_img_n = 4;
  3437. for (i=0; i < c.length; ++i)
  3438. palette[i*4+3] = stbi__get8(s);
  3439. } else {
  3440. if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG");
  3441. if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG");
  3442. has_trans = 1;
  3443. for (k=0; k < s->img_n; ++k)
  3444. tc[k] = (stbi_uc) (stbi__get16be(s) & 255) * stbi__depth_scale_table[depth]; // non 8-bit images will be larger
  3445. }
  3446. break;
  3447. }
  3448. case STBI__PNG_TYPE('I','D','A','T'): {
  3449. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  3450. if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG");
  3451. if (scan == STBI__SCAN_header) { s->img_n = pal_img_n; return 1; }
  3452. if ((int)(ioff + c.length) < (int)ioff) return 0;
  3453. if (ioff + c.length > idata_limit) {
  3454. stbi_uc *p;
  3455. if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
  3456. while (ioff + c.length > idata_limit)
  3457. idata_limit *= 2;
  3458. p = (stbi_uc *) STBI_REALLOC(z->idata, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
  3459. z->idata = p;
  3460. }
  3461. if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG");
  3462. ioff += c.length;
  3463. break;
  3464. }
  3465. case STBI__PNG_TYPE('I','E','N','D'): {
  3466. stbi__uint32 raw_len, bpl;
  3467. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  3468. if (scan != STBI__SCAN_load) return 1;
  3469. if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG");
  3470. // initial guess for decoded data size to avoid unnecessary reallocs
  3471. bpl = (s->img_x * depth + 7) / 8; // bytes per line, per component
  3472. raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;
  3473. z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone);
  3474. if (z->expanded == NULL) return 0; // zlib should set error
  3475. STBI_FREE(z->idata); z->idata = NULL;
  3476. if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
  3477. s->img_out_n = s->img_n+1;
  3478. else
  3479. s->img_out_n = s->img_n;
  3480. if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, depth, color, interlace)) return 0;
  3481. if (has_trans)
  3482. if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;
  3483. if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
  3484. stbi__de_iphone(z);
  3485. if (pal_img_n) {
  3486. // pal_img_n == 3 or 4
  3487. s->img_n = pal_img_n; // record the actual colors we had
  3488. s->img_out_n = pal_img_n;
  3489. if (req_comp >= 3) s->img_out_n = req_comp;
  3490. if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
  3491. return 0;
  3492. }
  3493. STBI_FREE(z->expanded); z->expanded = NULL;
  3494. return 1;
  3495. }
  3496. default:
  3497. // if critical, fail
  3498. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  3499. if ((c.type & (1 << 29)) == 0) {
  3500. #ifndef STBI_NO_FAILURE_STRINGS
  3501. // not threadsafe
  3502. static char invalid_chunk[] = "XXXX PNG chunk not known";
  3503. invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
  3504. invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
  3505. invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
  3506. invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
  3507. #endif
  3508. return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type");
  3509. }
  3510. stbi__skip(s, c.length);
  3511. break;
  3512. }
  3513. // end of PNG chunk, read and skip CRC
  3514. stbi__get32be(s);
  3515. }
  3516. }
  3517. static unsigned char *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp)
  3518. {
  3519. unsigned char *result=NULL;
  3520. if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
  3521. if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) {
  3522. result = p->out;
  3523. p->out = NULL;
  3524. if (req_comp && req_comp != p->s->img_out_n) {
  3525. result = stbi__convert_format(result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
  3526. p->s->img_out_n = req_comp;
  3527. if (result == NULL) return result;
  3528. }
  3529. *x = p->s->img_x;
  3530. *y = p->s->img_y;
  3531. if (n) *n = p->s->img_out_n;
  3532. }
  3533. STBI_FREE(p->out); p->out = NULL;
  3534. STBI_FREE(p->expanded); p->expanded = NULL;
  3535. STBI_FREE(p->idata); p->idata = NULL;
  3536. return result;
  3537. }
  3538. static unsigned char *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  3539. {
  3540. stbi__png p;
  3541. p.s = s;
  3542. return stbi__do_png(&p, x,y,comp,req_comp);
  3543. }
  3544. static int stbi__png_test(stbi__context *s)
  3545. {
  3546. int r;
  3547. r = stbi__check_png_header(s);
  3548. stbi__rewind(s);
  3549. return r;
  3550. }
  3551. static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp)
  3552. {
  3553. if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) {
  3554. stbi__rewind( p->s );
  3555. return 0;
  3556. }
  3557. if (x) *x = p->s->img_x;
  3558. if (y) *y = p->s->img_y;
  3559. if (comp) *comp = p->s->img_n;
  3560. return 1;
  3561. }
  3562. static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp)
  3563. {
  3564. stbi__png p;
  3565. p.s = s;
  3566. return stbi__png_info_raw(&p, x, y, comp);
  3567. }
  3568. #endif
  3569. // Microsoft/Windows BMP image
  3570. #ifndef STBI_NO_BMP
  3571. static int stbi__bmp_test_raw(stbi__context *s)
  3572. {
  3573. int r;
  3574. int sz;
  3575. if (stbi__get8(s) != 'B') return 0;
  3576. if (stbi__get8(s) != 'M') return 0;
  3577. stbi__get32le(s); // discard filesize
  3578. stbi__get16le(s); // discard reserved
  3579. stbi__get16le(s); // discard reserved
  3580. stbi__get32le(s); // discard data offset
  3581. sz = stbi__get32le(s);
  3582. r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124);
  3583. return r;
  3584. }
  3585. static int stbi__bmp_test(stbi__context *s)
  3586. {
  3587. int r = stbi__bmp_test_raw(s);
  3588. stbi__rewind(s);
  3589. return r;
  3590. }
  3591. // returns 0..31 for the highest set bit
  3592. static int stbi__high_bit(unsigned int z)
  3593. {
  3594. int n=0;
  3595. if (z == 0) return -1;
  3596. if (z >= 0x10000) n += 16, z >>= 16;
  3597. if (z >= 0x00100) n += 8, z >>= 8;
  3598. if (z >= 0x00010) n += 4, z >>= 4;
  3599. if (z >= 0x00004) n += 2, z >>= 2;
  3600. if (z >= 0x00002) n += 1, z >>= 1;
  3601. return n;
  3602. }
  3603. static int stbi__bitcount(unsigned int a)
  3604. {
  3605. a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2
  3606. a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4
  3607. a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
  3608. a = (a + (a >> 8)); // max 16 per 8 bits
  3609. a = (a + (a >> 16)); // max 32 per 8 bits
  3610. return a & 0xff;
  3611. }
  3612. static int stbi__shiftsigned(int v, int shift, int bits)
  3613. {
  3614. int result;
  3615. int z=0;
  3616. if (shift < 0) v <<= -shift;
  3617. else v >>= shift;
  3618. result = v;
  3619. z = bits;
  3620. while (z < 8) {
  3621. result += v >> z;
  3622. z += bits;
  3623. }
  3624. return result;
  3625. }
  3626. static stbi_uc *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  3627. {
  3628. stbi_uc *out;
  3629. unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
  3630. stbi_uc pal[256][4];
  3631. int psize=0,i,j,compress=0,width;
  3632. int bpp, flip_vertically, pad, target, offset, hsz;
  3633. if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP");
  3634. stbi__get32le(s); // discard filesize
  3635. stbi__get16le(s); // discard reserved
  3636. stbi__get16le(s); // discard reserved
  3637. offset = stbi__get32le(s);
  3638. hsz = stbi__get32le(s);
  3639. if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown");
  3640. if (hsz == 12) {
  3641. s->img_x = stbi__get16le(s);
  3642. s->img_y = stbi__get16le(s);
  3643. } else {
  3644. s->img_x = stbi__get32le(s);
  3645. s->img_y = stbi__get32le(s);
  3646. }
  3647. if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP");
  3648. bpp = stbi__get16le(s);
  3649. if (bpp == 1) return stbi__errpuc("monochrome", "BMP type not supported: 1-bit");
  3650. flip_vertically = ((int) s->img_y) > 0;
  3651. s->img_y = abs((int) s->img_y);
  3652. if (hsz == 12) {
  3653. if (bpp < 24)
  3654. psize = (offset - 14 - 24) / 3;
  3655. } else {
  3656. compress = stbi__get32le(s);
  3657. if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE");
  3658. stbi__get32le(s); // discard sizeof
  3659. stbi__get32le(s); // discard hres
  3660. stbi__get32le(s); // discard vres
  3661. stbi__get32le(s); // discard colorsused
  3662. stbi__get32le(s); // discard max important
  3663. if (hsz == 40 || hsz == 56) {
  3664. if (hsz == 56) {
  3665. stbi__get32le(s);
  3666. stbi__get32le(s);
  3667. stbi__get32le(s);
  3668. stbi__get32le(s);
  3669. }
  3670. if (bpp == 16 || bpp == 32) {
  3671. mr = mg = mb = 0;
  3672. if (compress == 0) {
  3673. if (bpp == 32) {
  3674. mr = 0xffu << 16;
  3675. mg = 0xffu << 8;
  3676. mb = 0xffu << 0;
  3677. ma = 0xffu << 24;
  3678. fake_a = 1; // @TODO: check for cases like alpha value is all 0 and switch it to 255
  3679. STBI_NOTUSED(fake_a);
  3680. } else {
  3681. mr = 31u << 10;
  3682. mg = 31u << 5;
  3683. mb = 31u << 0;
  3684. }
  3685. } else if (compress == 3) {
  3686. mr = stbi__get32le(s);
  3687. mg = stbi__get32le(s);
  3688. mb = stbi__get32le(s);
  3689. // not documented, but generated by photoshop and handled by mspaint
  3690. if (mr == mg && mg == mb) {
  3691. // ?!?!?
  3692. return stbi__errpuc("bad BMP", "bad BMP");
  3693. }
  3694. } else
  3695. return stbi__errpuc("bad BMP", "bad BMP");
  3696. }
  3697. } else {
  3698. STBI_ASSERT(hsz == 108 || hsz == 124);
  3699. mr = stbi__get32le(s);
  3700. mg = stbi__get32le(s);
  3701. mb = stbi__get32le(s);
  3702. ma = stbi__get32le(s);
  3703. stbi__get32le(s); // discard color space
  3704. for (i=0; i < 12; ++i)
  3705. stbi__get32le(s); // discard color space parameters
  3706. if (hsz == 124) {
  3707. stbi__get32le(s); // discard rendering intent
  3708. stbi__get32le(s); // discard offset of profile data
  3709. stbi__get32le(s); // discard size of profile data
  3710. stbi__get32le(s); // discard reserved
  3711. }
  3712. }
  3713. if (bpp < 16)
  3714. psize = (offset - 14 - hsz) >> 2;
  3715. }
  3716. s->img_n = ma ? 4 : 3;
  3717. if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
  3718. target = req_comp;
  3719. else
  3720. target = s->img_n; // if they want monochrome, we'll post-convert
  3721. out = (stbi_uc *) stbi__malloc(target * s->img_x * s->img_y);
  3722. if (!out) return stbi__errpuc("outofmem", "Out of memory");
  3723. if (bpp < 16) {
  3724. int z=0;
  3725. if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); }
  3726. for (i=0; i < psize; ++i) {
  3727. pal[i][2] = stbi__get8(s);
  3728. pal[i][1] = stbi__get8(s);
  3729. pal[i][0] = stbi__get8(s);
  3730. if (hsz != 12) stbi__get8(s);
  3731. pal[i][3] = 255;
  3732. }
  3733. stbi__skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
  3734. if (bpp == 4) width = (s->img_x + 1) >> 1;
  3735. else if (bpp == 8) width = s->img_x;
  3736. else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); }
  3737. pad = (-width)&3;
  3738. for (j=0; j < (int) s->img_y; ++j) {
  3739. for (i=0; i < (int) s->img_x; i += 2) {
  3740. int v=stbi__get8(s),v2=0;
  3741. if (bpp == 4) {
  3742. v2 = v & 15;
  3743. v >>= 4;
  3744. }
  3745. out[z++] = pal[v][0];
  3746. out[z++] = pal[v][1];
  3747. out[z++] = pal[v][2];
  3748. if (target == 4) out[z++] = 255;
  3749. if (i+1 == (int) s->img_x) break;
  3750. v = (bpp == 8) ? stbi__get8(s) : v2;
  3751. out[z++] = pal[v][0];
  3752. out[z++] = pal[v][1];
  3753. out[z++] = pal[v][2];
  3754. if (target == 4) out[z++] = 255;
  3755. }
  3756. stbi__skip(s, pad);
  3757. }
  3758. } else {
  3759. int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
  3760. int z = 0;
  3761. int easy=0;
  3762. stbi__skip(s, offset - 14 - hsz);
  3763. if (bpp == 24) width = 3 * s->img_x;
  3764. else if (bpp == 16) width = 2*s->img_x;
  3765. else /* bpp = 32 and pad = 0 */ width=0;
  3766. pad = (-width) & 3;
  3767. if (bpp == 24) {
  3768. easy = 1;
  3769. } else if (bpp == 32) {
  3770. if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
  3771. easy = 2;
  3772. }
  3773. if (!easy) {
  3774. if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }
  3775. // right shift amt to put high bit in position #7
  3776. rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr);
  3777. gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);
  3778. bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);
  3779. ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma);
  3780. }
  3781. for (j=0; j < (int) s->img_y; ++j) {
  3782. if (easy) {
  3783. for (i=0; i < (int) s->img_x; ++i) {
  3784. unsigned char a;
  3785. out[z+2] = stbi__get8(s);
  3786. out[z+1] = stbi__get8(s);
  3787. out[z+0] = stbi__get8(s);
  3788. z += 3;
  3789. a = (easy == 2 ? stbi__get8(s) : 255);
  3790. if (target == 4) out[z++] = a;
  3791. }
  3792. } else {
  3793. for (i=0; i < (int) s->img_x; ++i) {
  3794. stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s));
  3795. int a;
  3796. out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount));
  3797. out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount));
  3798. out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount));
  3799. a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
  3800. if (target == 4) out[z++] = STBI__BYTECAST(a);
  3801. }
  3802. }
  3803. stbi__skip(s, pad);
  3804. }
  3805. }
  3806. if (flip_vertically) {
  3807. stbi_uc t;
  3808. for (j=0; j < (int) s->img_y>>1; ++j) {
  3809. stbi_uc *p1 = out + j *s->img_x*target;
  3810. stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
  3811. for (i=0; i < (int) s->img_x*target; ++i) {
  3812. t = p1[i], p1[i] = p2[i], p2[i] = t;
  3813. }
  3814. }
  3815. }
  3816. if (req_comp && req_comp != target) {
  3817. out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
  3818. if (out == NULL) return out; // stbi__convert_format frees input on failure
  3819. }
  3820. *x = s->img_x;
  3821. *y = s->img_y;
  3822. if (comp) *comp = s->img_n;
  3823. return out;
  3824. }
  3825. #endif
  3826. // Targa Truevision - TGA
  3827. // by Jonathan Dummer
  3828. #ifndef STBI_NO_TGA
  3829. static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp)
  3830. {
  3831. int tga_w, tga_h, tga_comp;
  3832. int sz;
  3833. stbi__get8(s); // discard Offset
  3834. sz = stbi__get8(s); // color type
  3835. if( sz > 1 ) {
  3836. stbi__rewind(s);
  3837. return 0; // only RGB or indexed allowed
  3838. }
  3839. sz = stbi__get8(s); // image type
  3840. // only RGB or grey allowed, +/- RLE
  3841. if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11)) return 0;
  3842. stbi__skip(s,9);
  3843. tga_w = stbi__get16le(s);
  3844. if( tga_w < 1 ) {
  3845. stbi__rewind(s);
  3846. return 0; // test width
  3847. }
  3848. tga_h = stbi__get16le(s);
  3849. if( tga_h < 1 ) {
  3850. stbi__rewind(s);
  3851. return 0; // test height
  3852. }
  3853. sz = stbi__get8(s); // bits per pixel
  3854. // only RGB or RGBA or grey allowed
  3855. if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) {
  3856. stbi__rewind(s);
  3857. return 0;
  3858. }
  3859. tga_comp = sz;
  3860. if (x) *x = tga_w;
  3861. if (y) *y = tga_h;
  3862. if (comp) *comp = tga_comp / 8;
  3863. return 1; // seems to have passed everything
  3864. }
  3865. static int stbi__tga_test(stbi__context *s)
  3866. {
  3867. int res;
  3868. int sz;
  3869. stbi__get8(s); // discard Offset
  3870. sz = stbi__get8(s); // color type
  3871. if ( sz > 1 ) return 0; // only RGB or indexed allowed
  3872. sz = stbi__get8(s); // image type
  3873. if ( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) ) return 0; // only RGB or grey allowed, +/- RLE
  3874. stbi__get16be(s); // discard palette start
  3875. stbi__get16be(s); // discard palette length
  3876. stbi__get8(s); // discard bits per palette color entry
  3877. stbi__get16be(s); // discard x origin
  3878. stbi__get16be(s); // discard y origin
  3879. if ( stbi__get16be(s) < 1 ) return 0; // test width
  3880. if ( stbi__get16be(s) < 1 ) return 0; // test height
  3881. sz = stbi__get8(s); // bits per pixel
  3882. if ( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) )
  3883. res = 0;
  3884. else
  3885. res = 1;
  3886. stbi__rewind(s);
  3887. return res;
  3888. }
  3889. static stbi_uc *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  3890. {
  3891. // read in the TGA header stuff
  3892. int tga_offset = stbi__get8(s);
  3893. int tga_indexed = stbi__get8(s);
  3894. int tga_image_type = stbi__get8(s);
  3895. int tga_is_RLE = 0;
  3896. int tga_palette_start = stbi__get16le(s);
  3897. int tga_palette_len = stbi__get16le(s);
  3898. int tga_palette_bits = stbi__get8(s);
  3899. int tga_x_origin = stbi__get16le(s);
  3900. int tga_y_origin = stbi__get16le(s);
  3901. int tga_width = stbi__get16le(s);
  3902. int tga_height = stbi__get16le(s);
  3903. int tga_bits_per_pixel = stbi__get8(s);
  3904. int tga_comp = tga_bits_per_pixel / 8;
  3905. int tga_inverted = stbi__get8(s);
  3906. // image data
  3907. unsigned char *tga_data;
  3908. unsigned char *tga_palette = NULL;
  3909. int i, j;
  3910. unsigned char raw_data[4];
  3911. int RLE_count = 0;
  3912. int RLE_repeating = 0;
  3913. int read_next_pixel = 1;
  3914. // do a tiny bit of precessing
  3915. if ( tga_image_type >= 8 )
  3916. {
  3917. tga_image_type -= 8;
  3918. tga_is_RLE = 1;
  3919. }
  3920. /* int tga_alpha_bits = tga_inverted & 15; */
  3921. tga_inverted = 1 - ((tga_inverted >> 5) & 1);
  3922. // error check
  3923. if ( //(tga_indexed) ||
  3924. (tga_width < 1) || (tga_height < 1) ||
  3925. (tga_image_type < 1) || (tga_image_type > 3) ||
  3926. ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
  3927. (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
  3928. )
  3929. {
  3930. return NULL; // we don't report this as a bad TGA because we don't even know if it's TGA
  3931. }
  3932. // If I'm paletted, then I'll use the number of bits from the palette
  3933. if ( tga_indexed )
  3934. {
  3935. tga_comp = tga_palette_bits / 8;
  3936. }
  3937. // tga info
  3938. *x = tga_width;
  3939. *y = tga_height;
  3940. if (comp) *comp = tga_comp;
  3941. tga_data = (unsigned char*)stbi__malloc( (size_t)tga_width * tga_height * tga_comp );
  3942. if (!tga_data) return stbi__errpuc("outofmem", "Out of memory");
  3943. // skip to the data's starting position (offset usually = 0)
  3944. stbi__skip(s, tga_offset );
  3945. if ( !tga_indexed && !tga_is_RLE) {
  3946. for (i=0; i < tga_height; ++i) {
  3947. int y = tga_inverted ? tga_height -i - 1 : i;
  3948. stbi_uc *tga_row = tga_data + y*tga_width*tga_comp;
  3949. stbi__getn(s, tga_row, tga_width * tga_comp);
  3950. }
  3951. } else {
  3952. // do I need to load a palette?
  3953. if ( tga_indexed)
  3954. {
  3955. // any data to skip? (offset usually = 0)
  3956. stbi__skip(s, tga_palette_start );
  3957. // load the palette
  3958. tga_palette = (unsigned char*)stbi__malloc( tga_palette_len * tga_palette_bits / 8 );
  3959. if (!tga_palette) {
  3960. STBI_FREE(tga_data);
  3961. return stbi__errpuc("outofmem", "Out of memory");
  3962. }
  3963. if (!stbi__getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) {
  3964. STBI_FREE(tga_data);
  3965. STBI_FREE(tga_palette);
  3966. return stbi__errpuc("bad palette", "Corrupt TGA");
  3967. }
  3968. }
  3969. // load the data
  3970. for (i=0; i < tga_width * tga_height; ++i)
  3971. {
  3972. // if I'm in RLE mode, do I need to get a RLE stbi__pngchunk?
  3973. if ( tga_is_RLE )
  3974. {
  3975. if ( RLE_count == 0 )
  3976. {
  3977. // yep, get the next byte as a RLE command
  3978. int RLE_cmd = stbi__get8(s);
  3979. RLE_count = 1 + (RLE_cmd & 127);
  3980. RLE_repeating = RLE_cmd >> 7;
  3981. read_next_pixel = 1;
  3982. } else if ( !RLE_repeating )
  3983. {
  3984. read_next_pixel = 1;
  3985. }
  3986. } else
  3987. {
  3988. read_next_pixel = 1;
  3989. }
  3990. // OK, if I need to read a pixel, do it now
  3991. if ( read_next_pixel )
  3992. {
  3993. // load however much data we did have
  3994. if ( tga_indexed )
  3995. {
  3996. // read in 1 byte, then perform the lookup
  3997. int pal_idx = stbi__get8(s);
  3998. if ( pal_idx >= tga_palette_len )
  3999. {
  4000. // invalid index
  4001. pal_idx = 0;
  4002. }
  4003. pal_idx *= tga_bits_per_pixel / 8;
  4004. for (j = 0; j*8 < tga_bits_per_pixel; ++j)
  4005. {
  4006. raw_data[j] = tga_palette[pal_idx+j];
  4007. }
  4008. } else
  4009. {
  4010. // read in the data raw
  4011. for (j = 0; j*8 < tga_bits_per_pixel; ++j)
  4012. {
  4013. raw_data[j] = stbi__get8(s);
  4014. }
  4015. }
  4016. // clear the reading flag for the next pixel
  4017. read_next_pixel = 0;
  4018. } // end of reading a pixel
  4019. // copy data
  4020. for (j = 0; j < tga_comp; ++j)
  4021. tga_data[i*tga_comp+j] = raw_data[j];
  4022. // in case we're in RLE mode, keep counting down
  4023. --RLE_count;
  4024. }
  4025. // do I need to invert the image?
  4026. if ( tga_inverted )
  4027. {
  4028. for (j = 0; j*2 < tga_height; ++j)
  4029. {
  4030. int index1 = j * tga_width * tga_comp;
  4031. int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
  4032. for (i = tga_width * tga_comp; i > 0; --i)
  4033. {
  4034. unsigned char temp = tga_data[index1];
  4035. tga_data[index1] = tga_data[index2];
  4036. tga_data[index2] = temp;
  4037. ++index1;
  4038. ++index2;
  4039. }
  4040. }
  4041. }
  4042. // clear my palette, if I had one
  4043. if ( tga_palette != NULL )
  4044. {
  4045. STBI_FREE( tga_palette );
  4046. }
  4047. }
  4048. // swap RGB
  4049. if (tga_comp >= 3)
  4050. {
  4051. unsigned char* tga_pixel = tga_data;
  4052. for (i=0; i < tga_width * tga_height; ++i)
  4053. {
  4054. unsigned char temp = tga_pixel[0];
  4055. tga_pixel[0] = tga_pixel[2];
  4056. tga_pixel[2] = temp;
  4057. tga_pixel += tga_comp;
  4058. }
  4059. }
  4060. // convert to target component count
  4061. if (req_comp && req_comp != tga_comp)
  4062. tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
  4063. // the things I do to get rid of an error message, and yet keep
  4064. // Microsoft's C compilers happy... [8^(
  4065. tga_palette_start = tga_palette_len = tga_palette_bits =
  4066. tga_x_origin = tga_y_origin = 0;
  4067. // OK, done
  4068. return tga_data;
  4069. }
  4070. #endif
  4071. // *************************************************************************************************
  4072. // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
  4073. #ifndef STBI_NO_PSD
  4074. static int stbi__psd_test(stbi__context *s)
  4075. {
  4076. int r = (stbi__get32be(s) == 0x38425053);
  4077. stbi__rewind(s);
  4078. return r;
  4079. }
  4080. static stbi_uc *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  4081. {
  4082. int pixelCount;
  4083. int channelCount, compression;
  4084. int channel, i, count, len;
  4085. int w,h;
  4086. stbi_uc *out;
  4087. // Check identifier
  4088. if (stbi__get32be(s) != 0x38425053) // "8BPS"
  4089. return stbi__errpuc("not PSD", "Corrupt PSD image");
  4090. // Check file type version.
  4091. if (stbi__get16be(s) != 1)
  4092. return stbi__errpuc("wrong version", "Unsupported version of PSD image");
  4093. // Skip 6 reserved bytes.
  4094. stbi__skip(s, 6 );
  4095. // Read the number of channels (R, G, B, A, etc).
  4096. channelCount = stbi__get16be(s);
  4097. if (channelCount < 0 || channelCount > 16)
  4098. return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image");
  4099. // Read the rows and columns of the image.
  4100. h = stbi__get32be(s);
  4101. w = stbi__get32be(s);
  4102. // Make sure the depth is 8 bits.
  4103. if (stbi__get16be(s) != 8)
  4104. return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 bit");
  4105. // Make sure the color mode is RGB.
  4106. // Valid options are:
  4107. // 0: Bitmap
  4108. // 1: Grayscale
  4109. // 2: Indexed color
  4110. // 3: RGB color
  4111. // 4: CMYK color
  4112. // 7: Multichannel
  4113. // 8: Duotone
  4114. // 9: Lab color
  4115. if (stbi__get16be(s) != 3)
  4116. return stbi__errpuc("wrong color format", "PSD is not in RGB color format");
  4117. // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.)
  4118. stbi__skip(s,stbi__get32be(s) );
  4119. // Skip the image resources. (resolution, pen tool paths, etc)
  4120. stbi__skip(s, stbi__get32be(s) );
  4121. // Skip the reserved data.
  4122. stbi__skip(s, stbi__get32be(s) );
  4123. // Find out if the data is compressed.
  4124. // Known values:
  4125. // 0: no compression
  4126. // 1: RLE compressed
  4127. compression = stbi__get16be(s);
  4128. if (compression > 1)
  4129. return stbi__errpuc("bad compression", "PSD has an unknown compression format");
  4130. // Create the destination image.
  4131. out = (stbi_uc *) stbi__malloc(4 * w*h);
  4132. if (!out) return stbi__errpuc("outofmem", "Out of memory");
  4133. pixelCount = w*h;
  4134. // Initialize the data to zero.
  4135. //memset( out, 0, pixelCount * 4 );
  4136. // Finally, the image data.
  4137. if (compression) {
  4138. // RLE as used by .PSD and .TIFF
  4139. // Loop until you get the number of unpacked bytes you are expecting:
  4140. // Read the next source byte into n.
  4141. // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
  4142. // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
  4143. // Else if n is 128, noop.
  4144. // Endloop
  4145. // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
  4146. // which we're going to just skip.
  4147. stbi__skip(s, h * channelCount * 2 );
  4148. // Read the RLE data by channel.
  4149. for (channel = 0; channel < 4; channel++) {
  4150. stbi_uc *p;
  4151. p = out+channel;
  4152. if (channel >= channelCount) {
  4153. // Fill this channel with default data.
  4154. for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;
  4155. } else {
  4156. // Read the RLE data.
  4157. count = 0;
  4158. while (count < pixelCount) {
  4159. len = stbi__get8(s);
  4160. if (len == 128) {
  4161. // No-op.
  4162. } else if (len < 128) {
  4163. // Copy next len+1 bytes literally.
  4164. len++;
  4165. count += len;
  4166. while (len) {
  4167. *p = stbi__get8(s);
  4168. p += 4;
  4169. len--;
  4170. }
  4171. } else if (len > 128) {
  4172. stbi_uc val;
  4173. // Next -len+1 bytes in the dest are replicated from next source byte.
  4174. // (Interpret len as a negative 8-bit int.)
  4175. len ^= 0x0FF;
  4176. len += 2;
  4177. val = stbi__get8(s);
  4178. count += len;
  4179. while (len) {
  4180. *p = val;
  4181. p += 4;
  4182. len--;
  4183. }
  4184. }
  4185. }
  4186. }
  4187. }
  4188. } else {
  4189. // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
  4190. // where each channel consists of an 8-bit value for each pixel in the image.
  4191. // Read the data by channel.
  4192. for (channel = 0; channel < 4; channel++) {
  4193. stbi_uc *p;
  4194. p = out + channel;
  4195. if (channel > channelCount) {
  4196. // Fill this channel with default data.
  4197. for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;
  4198. } else {
  4199. // Read the data.
  4200. for (i = 0; i < pixelCount; i++)
  4201. *p = stbi__get8(s), p += 4;
  4202. }
  4203. }
  4204. }
  4205. if (req_comp && req_comp != 4) {
  4206. out = stbi__convert_format(out, 4, req_comp, w, h);
  4207. if (out == NULL) return out; // stbi__convert_format frees input on failure
  4208. }
  4209. if (comp) *comp = channelCount;
  4210. *y = h;
  4211. *x = w;
  4212. return out;
  4213. }
  4214. #endif
  4215. // *************************************************************************************************
  4216. // Softimage PIC loader
  4217. // by Tom Seddon
  4218. //
  4219. // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
  4220. // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
  4221. #ifndef STBI_NO_PIC
  4222. static int stbi__pic_is4(stbi__context *s,const char *str)
  4223. {
  4224. int i;
  4225. for (i=0; i<4; ++i)
  4226. if (stbi__get8(s) != (stbi_uc)str[i])
  4227. return 0;
  4228. return 1;
  4229. }
  4230. static int stbi__pic_test_core(stbi__context *s)
  4231. {
  4232. int i;
  4233. if (!stbi__pic_is4(s,"\x53\x80\xF6\x34"))
  4234. return 0;
  4235. for(i=0;i<84;++i)
  4236. stbi__get8(s);
  4237. if (!stbi__pic_is4(s,"PICT"))
  4238. return 0;
  4239. return 1;
  4240. }
  4241. typedef struct
  4242. {
  4243. stbi_uc size,type,channel;
  4244. } stbi__pic_packet;
  4245. static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest)
  4246. {
  4247. int mask=0x80, i;
  4248. for (i=0; i<4; ++i, mask>>=1) {
  4249. if (channel & mask) {
  4250. if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short");
  4251. dest[i]=stbi__get8(s);
  4252. }
  4253. }
  4254. return dest;
  4255. }
  4256. static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src)
  4257. {
  4258. int mask=0x80,i;
  4259. for (i=0;i<4; ++i, mask>>=1)
  4260. if (channel&mask)
  4261. dest[i]=src[i];
  4262. }
  4263. static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result)
  4264. {
  4265. int act_comp=0,num_packets=0,y,chained;
  4266. stbi__pic_packet packets[10];
  4267. // this will (should...) cater for even some bizarre stuff like having data
  4268. // for the same channel in multiple packets.
  4269. do {
  4270. stbi__pic_packet *packet;
  4271. if (num_packets==sizeof(packets)/sizeof(packets[0]))
  4272. return stbi__errpuc("bad format","too many packets");
  4273. packet = &packets[num_packets++];
  4274. chained = stbi__get8(s);
  4275. packet->size = stbi__get8(s);
  4276. packet->type = stbi__get8(s);
  4277. packet->channel = stbi__get8(s);
  4278. act_comp |= packet->channel;
  4279. if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)");
  4280. if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp");
  4281. } while (chained);
  4282. *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
  4283. for(y=0; y<height; ++y) {
  4284. int packet_idx;
  4285. for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
  4286. stbi__pic_packet *packet = &packets[packet_idx];
  4287. stbi_uc *dest = result+y*width*4;
  4288. switch (packet->type) {
  4289. default:
  4290. return stbi__errpuc("bad format","packet has bad compression type");
  4291. case 0: {//uncompressed
  4292. int x;
  4293. for(x=0;x<width;++x, dest+=4)
  4294. if (!stbi__readval(s,packet->channel,dest))
  4295. return 0;
  4296. break;
  4297. }
  4298. case 1://Pure RLE
  4299. {
  4300. int left=width, i;
  4301. while (left>0) {
  4302. stbi_uc count,value[4];
  4303. count=stbi__get8(s);
  4304. if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)");
  4305. if (count > left)
  4306. count = (stbi_uc) left;
  4307. if (!stbi__readval(s,packet->channel,value)) return 0;
  4308. for(i=0; i<count; ++i,dest+=4)
  4309. stbi__copyval(packet->channel,dest,value);
  4310. left -= count;
  4311. }
  4312. }
  4313. break;
  4314. case 2: {//Mixed RLE
  4315. int left=width;
  4316. while (left>0) {
  4317. int count = stbi__get8(s), i;
  4318. if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)");
  4319. if (count >= 128) { // Repeated
  4320. stbi_uc value[4];
  4321. int i;
  4322. if (count==128)
  4323. count = stbi__get16be(s);
  4324. else
  4325. count -= 127;
  4326. if (count > left)
  4327. return stbi__errpuc("bad file","scanline overrun");
  4328. if (!stbi__readval(s,packet->channel,value))
  4329. return 0;
  4330. for(i=0;i<count;++i, dest += 4)
  4331. stbi__copyval(packet->channel,dest,value);
  4332. } else { // Raw
  4333. ++count;
  4334. if (count>left) return stbi__errpuc("bad file","scanline overrun");
  4335. for(i=0;i<count;++i, dest+=4)
  4336. if (!stbi__readval(s,packet->channel,dest))
  4337. return 0;
  4338. }
  4339. left-=count;
  4340. }
  4341. break;
  4342. }
  4343. }
  4344. }
  4345. }
  4346. return result;
  4347. }
  4348. static stbi_uc *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp)
  4349. {
  4350. stbi_uc *result;
  4351. int i, x,y;
  4352. for (i=0; i<92; ++i)
  4353. stbi__get8(s);
  4354. x = stbi__get16be(s);
  4355. y = stbi__get16be(s);
  4356. if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)");
  4357. if ((1 << 28) / x < y) return stbi__errpuc("too large", "Image too large to decode");
  4358. stbi__get32be(s); //skip `ratio'
  4359. stbi__get16be(s); //skip `fields'
  4360. stbi__get16be(s); //skip `pad'
  4361. // intermediate buffer is RGBA
  4362. result = (stbi_uc *) stbi__malloc(x*y*4);
  4363. memset(result, 0xff, x*y*4);
  4364. if (!stbi__pic_load_core(s,x,y,comp, result)) {
  4365. STBI_FREE(result);
  4366. result=0;
  4367. }
  4368. *px = x;
  4369. *py = y;
  4370. if (req_comp == 0) req_comp = *comp;
  4371. result=stbi__convert_format(result,4,req_comp,x,y);
  4372. return result;
  4373. }
  4374. static int stbi__pic_test(stbi__context *s)
  4375. {
  4376. int r = stbi__pic_test_core(s);
  4377. stbi__rewind(s);
  4378. return r;
  4379. }
  4380. #endif
  4381. // *************************************************************************************************
  4382. // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
  4383. #ifndef STBI_NO_GIF
  4384. typedef struct
  4385. {
  4386. stbi__int16 prefix;
  4387. stbi_uc first;
  4388. stbi_uc suffix;
  4389. } stbi__gif_lzw;
  4390. typedef struct
  4391. {
  4392. int w,h;
  4393. stbi_uc *out; // output buffer (always 4 components)
  4394. int flags, bgindex, ratio, transparent, eflags;
  4395. stbi_uc pal[256][4];
  4396. stbi_uc lpal[256][4];
  4397. stbi__gif_lzw codes[4096];
  4398. stbi_uc *color_table;
  4399. int parse, step;
  4400. int lflags;
  4401. int start_x, start_y;
  4402. int max_x, max_y;
  4403. int cur_x, cur_y;
  4404. int line_size;
  4405. } stbi__gif;
  4406. static int stbi__gif_test_raw(stbi__context *s)
  4407. {
  4408. int sz;
  4409. if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0;
  4410. sz = stbi__get8(s);
  4411. if (sz != '9' && sz != '7') return 0;
  4412. if (stbi__get8(s) != 'a') return 0;
  4413. return 1;
  4414. }
  4415. static int stbi__gif_test(stbi__context *s)
  4416. {
  4417. int r = stbi__gif_test_raw(s);
  4418. stbi__rewind(s);
  4419. return r;
  4420. }
  4421. static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp)
  4422. {
  4423. int i;
  4424. for (i=0; i < num_entries; ++i) {
  4425. pal[i][2] = stbi__get8(s);
  4426. pal[i][1] = stbi__get8(s);
  4427. pal[i][0] = stbi__get8(s);
  4428. pal[i][3] = transp == i ? 0 : 255;
  4429. }
  4430. }
  4431. static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info)
  4432. {
  4433. stbi_uc version;
  4434. if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8')
  4435. return stbi__err("not GIF", "Corrupt GIF");
  4436. version = stbi__get8(s);
  4437. if (version != '7' && version != '9') return stbi__err("not GIF", "Corrupt GIF");
  4438. if (stbi__get8(s) != 'a') return stbi__err("not GIF", "Corrupt GIF");
  4439. stbi__g_failure_reason = "";
  4440. g->w = stbi__get16le(s);
  4441. g->h = stbi__get16le(s);
  4442. g->flags = stbi__get8(s);
  4443. g->bgindex = stbi__get8(s);
  4444. g->ratio = stbi__get8(s);
  4445. g->transparent = -1;
  4446. if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
  4447. if (is_info) return 1;
  4448. if (g->flags & 0x80)
  4449. stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
  4450. return 1;
  4451. }
  4452. static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
  4453. {
  4454. stbi__gif g;
  4455. if (!stbi__gif_header(s, &g, comp, 1)) {
  4456. stbi__rewind( s );
  4457. return 0;
  4458. }
  4459. if (x) *x = g.w;
  4460. if (y) *y = g.h;
  4461. return 1;
  4462. }
  4463. static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
  4464. {
  4465. stbi_uc *p, *c;
  4466. // recurse to decode the prefixes, since the linked-list is backwards,
  4467. // and working backwards through an interleaved image would be nasty
  4468. if (g->codes[code].prefix >= 0)
  4469. stbi__out_gif_code(g, g->codes[code].prefix);
  4470. if (g->cur_y >= g->max_y) return;
  4471. p = &g->out[g->cur_x + g->cur_y];
  4472. c = &g->color_table[g->codes[code].suffix * 4];
  4473. if (c[3] >= 128) {
  4474. p[0] = c[2];
  4475. p[1] = c[1];
  4476. p[2] = c[0];
  4477. p[3] = c[3];
  4478. }
  4479. g->cur_x += 4;
  4480. if (g->cur_x >= g->max_x) {
  4481. g->cur_x = g->start_x;
  4482. g->cur_y += g->step;
  4483. while (g->cur_y >= g->max_y && g->parse > 0) {
  4484. g->step = (1 << g->parse) * g->line_size;
  4485. g->cur_y = g->start_y + (g->step >> 1);
  4486. --g->parse;
  4487. }
  4488. }
  4489. }
  4490. static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
  4491. {
  4492. stbi_uc lzw_cs;
  4493. stbi__int32 len, code;
  4494. stbi__uint32 first;
  4495. stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
  4496. stbi__gif_lzw *p;
  4497. lzw_cs = stbi__get8(s);
  4498. if (lzw_cs > 12) return NULL;
  4499. clear = 1 << lzw_cs;
  4500. first = 1;
  4501. codesize = lzw_cs + 1;
  4502. codemask = (1 << codesize) - 1;
  4503. bits = 0;
  4504. valid_bits = 0;
  4505. for (code = 0; code < clear; code++) {
  4506. g->codes[code].prefix = -1;
  4507. g->codes[code].first = (stbi_uc) code;
  4508. g->codes[code].suffix = (stbi_uc) code;
  4509. }
  4510. // support no starting clear code
  4511. avail = clear+2;
  4512. oldcode = -1;
  4513. len = 0;
  4514. for(;;) {
  4515. if (valid_bits < codesize) {
  4516. if (len == 0) {
  4517. len = stbi__get8(s); // start new block
  4518. if (len == 0)
  4519. return g->out;
  4520. }
  4521. --len;
  4522. bits |= (stbi__int32) stbi__get8(s) << valid_bits;
  4523. valid_bits += 8;
  4524. } else {
  4525. stbi__int32 code = bits & codemask;
  4526. bits >>= codesize;
  4527. valid_bits -= codesize;
  4528. // @OPTIMIZE: is there some way we can accelerate the non-clear path?
  4529. if (code == clear) { // clear code
  4530. codesize = lzw_cs + 1;
  4531. codemask = (1 << codesize) - 1;
  4532. avail = clear + 2;
  4533. oldcode = -1;
  4534. first = 0;
  4535. } else if (code == clear + 1) { // end of stream code
  4536. stbi__skip(s, len);
  4537. while ((len = stbi__get8(s)) > 0)
  4538. stbi__skip(s,len);
  4539. return g->out;
  4540. } else if (code <= avail) {
  4541. if (first) return stbi__errpuc("no clear code", "Corrupt GIF");
  4542. if (oldcode >= 0) {
  4543. p = &g->codes[avail++];
  4544. if (avail > 4096) return stbi__errpuc("too many codes", "Corrupt GIF");
  4545. p->prefix = (stbi__int16) oldcode;
  4546. p->first = g->codes[oldcode].first;
  4547. p->suffix = (code == avail) ? p->first : g->codes[code].first;
  4548. } else if (code == avail)
  4549. return stbi__errpuc("illegal code in raster", "Corrupt GIF");
  4550. stbi__out_gif_code(g, (stbi__uint16) code);
  4551. if ((avail & codemask) == 0 && avail <= 0x0FFF) {
  4552. codesize++;
  4553. codemask = (1 << codesize) - 1;
  4554. }
  4555. oldcode = code;
  4556. } else {
  4557. return stbi__errpuc("illegal code in raster", "Corrupt GIF");
  4558. }
  4559. }
  4560. }
  4561. }
  4562. static void stbi__fill_gif_background(stbi__gif *g)
  4563. {
  4564. int i;
  4565. stbi_uc *c = g->pal[g->bgindex];
  4566. // @OPTIMIZE: write a dword at a time
  4567. for (i = 0; i < g->w * g->h * 4; i += 4) {
  4568. stbi_uc *p = &g->out[i];
  4569. p[0] = c[2];
  4570. p[1] = c[1];
  4571. p[2] = c[0];
  4572. p[3] = c[3];
  4573. }
  4574. }
  4575. // this function is designed to support animated gifs, although stb_image doesn't support it
  4576. static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp)
  4577. {
  4578. int i;
  4579. stbi_uc *old_out = 0;
  4580. if (g->out == 0) {
  4581. if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
  4582. g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
  4583. if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");
  4584. stbi__fill_gif_background(g);
  4585. } else {
  4586. // animated-gif-only path
  4587. if (((g->eflags & 0x1C) >> 2) == 3) {
  4588. old_out = g->out;
  4589. g->out = (stbi_uc *) stbi__malloc(4 * g->w * g->h);
  4590. if (g->out == 0) return stbi__errpuc("outofmem", "Out of memory");
  4591. memcpy(g->out, old_out, g->w*g->h*4);
  4592. }
  4593. }
  4594. for (;;) {
  4595. switch (stbi__get8(s)) {
  4596. case 0x2C: /* Image Descriptor */
  4597. {
  4598. stbi__int32 x, y, w, h;
  4599. stbi_uc *o;
  4600. x = stbi__get16le(s);
  4601. y = stbi__get16le(s);
  4602. w = stbi__get16le(s);
  4603. h = stbi__get16le(s);
  4604. if (((x + w) > (g->w)) || ((y + h) > (g->h)))
  4605. return stbi__errpuc("bad Image Descriptor", "Corrupt GIF");
  4606. g->line_size = g->w * 4;
  4607. g->start_x = x * 4;
  4608. g->start_y = y * g->line_size;
  4609. g->max_x = g->start_x + w * 4;
  4610. g->max_y = g->start_y + h * g->line_size;
  4611. g->cur_x = g->start_x;
  4612. g->cur_y = g->start_y;
  4613. g->lflags = stbi__get8(s);
  4614. if (g->lflags & 0x40) {
  4615. g->step = 8 * g->line_size; // first interlaced spacing
  4616. g->parse = 3;
  4617. } else {
  4618. g->step = g->line_size;
  4619. g->parse = 0;
  4620. }
  4621. if (g->lflags & 0x80) {
  4622. stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
  4623. g->color_table = (stbi_uc *) g->lpal;
  4624. } else if (g->flags & 0x80) {
  4625. for (i=0; i < 256; ++i) // @OPTIMIZE: stbi__jpeg_reset only the previous transparent
  4626. g->pal[i][3] = 255;
  4627. if (g->transparent >= 0 && (g->eflags & 0x01))
  4628. g->pal[g->transparent][3] = 0;
  4629. g->color_table = (stbi_uc *) g->pal;
  4630. } else
  4631. return stbi__errpuc("missing color table", "Corrupt GIF");
  4632. o = stbi__process_gif_raster(s, g);
  4633. if (o == NULL) return NULL;
  4634. if (req_comp && req_comp != 4)
  4635. o = stbi__convert_format(o, 4, req_comp, g->w, g->h);
  4636. return o;
  4637. }
  4638. case 0x21: // Comment Extension.
  4639. {
  4640. int len;
  4641. if (stbi__get8(s) == 0xF9) { // Graphic Control Extension.
  4642. len = stbi__get8(s);
  4643. if (len == 4) {
  4644. g->eflags = stbi__get8(s);
  4645. stbi__get16le(s); // delay
  4646. g->transparent = stbi__get8(s);
  4647. } else {
  4648. stbi__skip(s, len);
  4649. break;
  4650. }
  4651. }
  4652. while ((len = stbi__get8(s)) != 0)
  4653. stbi__skip(s, len);
  4654. break;
  4655. }
  4656. case 0x3B: // gif stream termination code
  4657. return (stbi_uc *) s; // using '1' causes warning on some compilers
  4658. default:
  4659. return stbi__errpuc("unknown code", "Corrupt GIF");
  4660. }
  4661. }
  4662. }
  4663. static stbi_uc *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  4664. {
  4665. stbi_uc *u = 0;
  4666. stbi__gif g;
  4667. memset(&g, 0, sizeof(g));
  4668. u = stbi__gif_load_next(s, &g, comp, req_comp);
  4669. if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
  4670. if (u) {
  4671. *x = g.w;
  4672. *y = g.h;
  4673. }
  4674. return u;
  4675. }
  4676. static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp)
  4677. {
  4678. return stbi__gif_info_raw(s,x,y,comp);
  4679. }
  4680. #endif
  4681. // *************************************************************************************************
  4682. // Radiance RGBE HDR loader
  4683. // originally by Nicolas Schulz
  4684. #ifndef STBI_NO_HDR
  4685. static int stbi__hdr_test_core(stbi__context *s)
  4686. {
  4687. const char *signature = "#?RADIANCE\n";
  4688. int i;
  4689. for (i=0; signature[i]; ++i)
  4690. if (stbi__get8(s) != signature[i])
  4691. return 0;
  4692. return 1;
  4693. }
  4694. static int stbi__hdr_test(stbi__context* s)
  4695. {
  4696. int r = stbi__hdr_test_core(s);
  4697. stbi__rewind(s);
  4698. return r;
  4699. }
  4700. #define STBI__HDR_BUFLEN 1024
  4701. static char *stbi__hdr_gettoken(stbi__context *z, char *buffer)
  4702. {
  4703. int len=0;
  4704. char c = '\0';
  4705. c = (char) stbi__get8(z);
  4706. while (!stbi__at_eof(z) && c != '\n') {
  4707. buffer[len++] = c;
  4708. if (len == STBI__HDR_BUFLEN-1) {
  4709. // flush to end of line
  4710. while (!stbi__at_eof(z) && stbi__get8(z) != '\n')
  4711. ;
  4712. break;
  4713. }
  4714. c = (char) stbi__get8(z);
  4715. }
  4716. buffer[len] = 0;
  4717. return buffer;
  4718. }
  4719. static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp)
  4720. {
  4721. if ( input[3] != 0 ) {
  4722. float f1;
  4723. // Exponent
  4724. f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
  4725. if (req_comp <= 2)
  4726. output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
  4727. else {
  4728. output[0] = input[0] * f1;
  4729. output[1] = input[1] * f1;
  4730. output[2] = input[2] * f1;
  4731. }
  4732. if (req_comp == 2) output[1] = 1;
  4733. if (req_comp == 4) output[3] = 1;
  4734. } else {
  4735. switch (req_comp) {
  4736. case 4: output[3] = 1; /* fallthrough */
  4737. case 3: output[0] = output[1] = output[2] = 0;
  4738. break;
  4739. case 2: output[1] = 1; /* fallthrough */
  4740. case 1: output[0] = 0;
  4741. break;
  4742. }
  4743. }
  4744. }
  4745. static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  4746. {
  4747. char buffer[STBI__HDR_BUFLEN];
  4748. char *token;
  4749. int valid = 0;
  4750. int width, height;
  4751. stbi_uc *scanline;
  4752. float *hdr_data;
  4753. int len;
  4754. unsigned char count, value;
  4755. int i, j, k, c1,c2, z;
  4756. // Check identifier
  4757. if (strcmp(stbi__hdr_gettoken(s,buffer), "#?RADIANCE") != 0)
  4758. return stbi__errpf("not HDR", "Corrupt HDR image");
  4759. // Parse header
  4760. for(;;) {
  4761. token = stbi__hdr_gettoken(s,buffer);
  4762. if (token[0] == 0) break;
  4763. if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
  4764. }
  4765. if (!valid) return stbi__errpf("unsupported format", "Unsupported HDR format");
  4766. // Parse width and height
  4767. // can't use sscanf() if we're not using stdio!
  4768. token = stbi__hdr_gettoken(s,buffer);
  4769. if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
  4770. token += 3;
  4771. height = (int) strtol(token, &token, 10);
  4772. while (*token == ' ') ++token;
  4773. if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
  4774. token += 3;
  4775. width = (int) strtol(token, NULL, 10);
  4776. *x = width;
  4777. *y = height;
  4778. if (comp) *comp = 3;
  4779. if (req_comp == 0) req_comp = 3;
  4780. // Read data
  4781. hdr_data = (float *) stbi__malloc(height * width * req_comp * sizeof(float));
  4782. // Load image data
  4783. // image data is stored as some number of sca
  4784. if ( width < 8 || width >= 32768) {
  4785. // Read flat data
  4786. for (j=0; j < height; ++j) {
  4787. for (i=0; i < width; ++i) {
  4788. stbi_uc rgbe[4];
  4789. main_decode_loop:
  4790. stbi__getn(s, rgbe, 4);
  4791. stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
  4792. }
  4793. }
  4794. } else {
  4795. // Read RLE-encoded data
  4796. scanline = NULL;
  4797. for (j = 0; j < height; ++j) {
  4798. c1 = stbi__get8(s);
  4799. c2 = stbi__get8(s);
  4800. len = stbi__get8(s);
  4801. if (c1 != 2 || c2 != 2 || (len & 0x80)) {
  4802. // not run-length encoded, so we have to actually use THIS data as a decoded
  4803. // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
  4804. stbi_uc rgbe[4];
  4805. rgbe[0] = (stbi_uc) c1;
  4806. rgbe[1] = (stbi_uc) c2;
  4807. rgbe[2] = (stbi_uc) len;
  4808. rgbe[3] = (stbi_uc) stbi__get8(s);
  4809. stbi__hdr_convert(hdr_data, rgbe, req_comp);
  4810. i = 1;
  4811. j = 0;
  4812. STBI_FREE(scanline);
  4813. goto main_decode_loop; // yes, this makes no sense
  4814. }
  4815. len <<= 8;
  4816. len |= stbi__get8(s);
  4817. if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }
  4818. if (scanline == NULL) scanline = (stbi_uc *) stbi__malloc(width * 4);
  4819. for (k = 0; k < 4; ++k) {
  4820. i = 0;
  4821. while (i < width) {
  4822. count = stbi__get8(s);
  4823. if (count > 128) {
  4824. // Run
  4825. value = stbi__get8(s);
  4826. count -= 128;
  4827. for (z = 0; z < count; ++z)
  4828. scanline[i++ * 4 + k] = value;
  4829. } else {
  4830. // Dump
  4831. for (z = 0; z < count; ++z)
  4832. scanline[i++ * 4 + k] = stbi__get8(s);
  4833. }
  4834. }
  4835. }
  4836. for (i=0; i < width; ++i)
  4837. stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
  4838. }
  4839. STBI_FREE(scanline);
  4840. }
  4841. return hdr_data;
  4842. }
  4843. static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp)
  4844. {
  4845. char buffer[STBI__HDR_BUFLEN];
  4846. char *token;
  4847. int valid = 0;
  4848. if (strcmp(stbi__hdr_gettoken(s,buffer), "#?RADIANCE") != 0) {
  4849. stbi__rewind( s );
  4850. return 0;
  4851. }
  4852. for(;;) {
  4853. token = stbi__hdr_gettoken(s,buffer);
  4854. if (token[0] == 0) break;
  4855. if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
  4856. }
  4857. if (!valid) {
  4858. stbi__rewind( s );
  4859. return 0;
  4860. }
  4861. token = stbi__hdr_gettoken(s,buffer);
  4862. if (strncmp(token, "-Y ", 3)) {
  4863. stbi__rewind( s );
  4864. return 0;
  4865. }
  4866. token += 3;
  4867. *y = (int) strtol(token, &token, 10);
  4868. while (*token == ' ') ++token;
  4869. if (strncmp(token, "+X ", 3)) {
  4870. stbi__rewind( s );
  4871. return 0;
  4872. }
  4873. token += 3;
  4874. *x = (int) strtol(token, NULL, 10);
  4875. *comp = 3;
  4876. return 1;
  4877. }
  4878. #endif // STBI_NO_HDR
  4879. #ifndef STBI_NO_BMP
  4880. static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
  4881. {
  4882. int hsz;
  4883. if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') {
  4884. stbi__rewind( s );
  4885. return 0;
  4886. }
  4887. stbi__skip(s,12);
  4888. hsz = stbi__get32le(s);
  4889. if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) {
  4890. stbi__rewind( s );
  4891. return 0;
  4892. }
  4893. if (hsz == 12) {
  4894. *x = stbi__get16le(s);
  4895. *y = stbi__get16le(s);
  4896. } else {
  4897. *x = stbi__get32le(s);
  4898. *y = stbi__get32le(s);
  4899. }
  4900. if (stbi__get16le(s) != 1) {
  4901. stbi__rewind( s );
  4902. return 0;
  4903. }
  4904. *comp = stbi__get16le(s) / 8;
  4905. return 1;
  4906. }
  4907. #endif
  4908. #ifndef STBI_NO_PSD
  4909. static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
  4910. {
  4911. int channelCount;
  4912. if (stbi__get32be(s) != 0x38425053) {
  4913. stbi__rewind( s );
  4914. return 0;
  4915. }
  4916. if (stbi__get16be(s) != 1) {
  4917. stbi__rewind( s );
  4918. return 0;
  4919. }
  4920. stbi__skip(s, 6);
  4921. channelCount = stbi__get16be(s);
  4922. if (channelCount < 0 || channelCount > 16) {
  4923. stbi__rewind( s );
  4924. return 0;
  4925. }
  4926. *y = stbi__get32be(s);
  4927. *x = stbi__get32be(s);
  4928. if (stbi__get16be(s) != 8) {
  4929. stbi__rewind( s );
  4930. return 0;
  4931. }
  4932. if (stbi__get16be(s) != 3) {
  4933. stbi__rewind( s );
  4934. return 0;
  4935. }
  4936. *comp = 4;
  4937. return 1;
  4938. }
  4939. #endif
  4940. #ifndef STBI_NO_PIC
  4941. static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
  4942. {
  4943. int act_comp=0,num_packets=0,chained;
  4944. stbi__pic_packet packets[10];
  4945. stbi__skip(s, 92);
  4946. *x = stbi__get16be(s);
  4947. *y = stbi__get16be(s);
  4948. if (stbi__at_eof(s)) return 0;
  4949. if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
  4950. stbi__rewind( s );
  4951. return 0;
  4952. }
  4953. stbi__skip(s, 8);
  4954. do {
  4955. stbi__pic_packet *packet;
  4956. if (num_packets==sizeof(packets)/sizeof(packets[0]))
  4957. return 0;
  4958. packet = &packets[num_packets++];
  4959. chained = stbi__get8(s);
  4960. packet->size = stbi__get8(s);
  4961. packet->type = stbi__get8(s);
  4962. packet->channel = stbi__get8(s);
  4963. act_comp |= packet->channel;
  4964. if (stbi__at_eof(s)) {
  4965. stbi__rewind( s );
  4966. return 0;
  4967. }
  4968. if (packet->size != 8) {
  4969. stbi__rewind( s );
  4970. return 0;
  4971. }
  4972. } while (chained);
  4973. *comp = (act_comp & 0x10 ? 4 : 3);
  4974. return 1;
  4975. }
  4976. #endif
  4977. // *************************************************************************************************
  4978. // Portable Gray Map and Portable Pixel Map loader
  4979. // by Ken Miller
  4980. //
  4981. // PGM: http://netpbm.sourceforge.net/doc/pgm.html
  4982. // PPM: http://netpbm.sourceforge.net/doc/ppm.html
  4983. //
  4984. // Known limitations:
  4985. // Does not support comments in the header section
  4986. // Does not support ASCII image data (formats P2 and P3)
  4987. // Does not support 16-bit-per-channel
  4988. #ifndef STBI_NO_PNM
  4989. static int stbi__pnm_test(stbi__context *s)
  4990. {
  4991. char p, t;
  4992. p = (char) stbi__get8(s);
  4993. t = (char) stbi__get8(s);
  4994. if (p != 'P' || (t != '5' && t != '6')) {
  4995. stbi__rewind( s );
  4996. return 0;
  4997. }
  4998. return 1;
  4999. }
  5000. static stbi_uc *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp)
  5001. {
  5002. stbi_uc *out;
  5003. if (!stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n))
  5004. return 0;
  5005. *x = s->img_x;
  5006. *y = s->img_y;
  5007. *comp = s->img_n;
  5008. out = (stbi_uc *) stbi__malloc(s->img_n * s->img_x * s->img_y);
  5009. if (!out) return stbi__errpuc("outofmem", "Out of memory");
  5010. stbi__getn(s, out, s->img_n * s->img_x * s->img_y);
  5011. if (req_comp && req_comp != s->img_n) {
  5012. out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y);
  5013. if (out == NULL) return out; // stbi__convert_format frees input on failure
  5014. }
  5015. return out;
  5016. }
  5017. static int stbi__pnm_isspace(char c)
  5018. {
  5019. return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
  5020. }
  5021. static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
  5022. {
  5023. while (!stbi__at_eof(s) && stbi__pnm_isspace(*c))
  5024. *c = (char) stbi__get8(s);
  5025. }
  5026. static int stbi__pnm_isdigit(char c)
  5027. {
  5028. return c >= '0' && c <= '9';
  5029. }
  5030. static int stbi__pnm_getinteger(stbi__context *s, char *c)
  5031. {
  5032. int value = 0;
  5033. while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) {
  5034. value = value*10 + (*c - '0');
  5035. *c = (char) stbi__get8(s);
  5036. }
  5037. return value;
  5038. }
  5039. static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp)
  5040. {
  5041. int maxv;
  5042. char c, p, t;
  5043. stbi__rewind( s );
  5044. // Get identifier
  5045. p = (char) stbi__get8(s);
  5046. t = (char) stbi__get8(s);
  5047. if (p != 'P' || (t != '5' && t != '6')) {
  5048. stbi__rewind( s );
  5049. return 0;
  5050. }
  5051. *comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm
  5052. c = (char) stbi__get8(s);
  5053. stbi__pnm_skip_whitespace(s, &c);
  5054. *x = stbi__pnm_getinteger(s, &c); // read width
  5055. stbi__pnm_skip_whitespace(s, &c);
  5056. *y = stbi__pnm_getinteger(s, &c); // read height
  5057. stbi__pnm_skip_whitespace(s, &c);
  5058. maxv = stbi__pnm_getinteger(s, &c); // read max value
  5059. if (maxv > 255)
  5060. return stbi__err("max value > 255", "PPM image not 8-bit");
  5061. else
  5062. return 1;
  5063. }
  5064. #endif
  5065. static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp)
  5066. {
  5067. #ifndef STBI_NO_JPEG
  5068. if (stbi__jpeg_info(s, x, y, comp)) return 1;
  5069. #endif
  5070. #ifndef STBI_NO_PNG
  5071. if (stbi__png_info(s, x, y, comp)) return 1;
  5072. #endif
  5073. #ifndef STBI_NO_GIF
  5074. if (stbi__gif_info(s, x, y, comp)) return 1;
  5075. #endif
  5076. #ifndef STBI_NO_BMP
  5077. if (stbi__bmp_info(s, x, y, comp)) return 1;
  5078. #endif
  5079. #ifndef STBI_NO_PSD
  5080. if (stbi__psd_info(s, x, y, comp)) return 1;
  5081. #endif
  5082. #ifndef STBI_NO_PIC
  5083. if (stbi__pic_info(s, x, y, comp)) return 1;
  5084. #endif
  5085. #ifndef STBI_NO_PNM
  5086. if (stbi__pnm_info(s, x, y, comp)) return 1;
  5087. #endif
  5088. #ifndef STBI_NO_HDR
  5089. if (stbi__hdr_info(s, x, y, comp)) return 1;
  5090. #endif
  5091. // test tga last because it's a crappy test!
  5092. #ifndef STBI_NO_TGA
  5093. if (stbi__tga_info(s, x, y, comp))
  5094. return 1;
  5095. #endif
  5096. return stbi__err("unknown image type", "Image not of any known type, or corrupt");
  5097. }
  5098. #ifndef STBI_NO_STDIO
  5099. STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp)
  5100. {
  5101. FILE *f = stbi__fopen(filename, "rb");
  5102. int result;
  5103. if (!f) return stbi__err("can't fopen", "Unable to open file");
  5104. result = stbi_info_from_file(f, x, y, comp);
  5105. fclose(f);
  5106. return result;
  5107. }
  5108. STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
  5109. {
  5110. int r;
  5111. stbi__context s;
  5112. long pos = ftell(f);
  5113. stbi__start_file(&s, f);
  5114. r = stbi__info_main(&s,x,y,comp);
  5115. fseek(f,pos,SEEK_SET);
  5116. return r;
  5117. }
  5118. #endif // !STBI_NO_STDIO
  5119. STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  5120. {
  5121. stbi__context s;
  5122. stbi__start_mem(&s,buffer,len);
  5123. return stbi__info_main(&s,x,y,comp);
  5124. }
  5125. STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
  5126. {
  5127. stbi__context s;
  5128. stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
  5129. return stbi__info_main(&s,x,y,comp);
  5130. }
  5131. #endif // STB_IMAGE_IMPLEMENTATION
  5132. /*
  5133. revision history:
  5134. 2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning
  5135. 2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit
  5136. 2.03 (2015-04-12) extra corruption checking (mmozeiko)
  5137. stbi_set_flip_vertically_on_load (nguillemot)
  5138. fix NEON support; fix mingw support
  5139. 2.02 (2015-01-19) fix incorrect assert, fix warning
  5140. 2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2
  5141. 2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG
  5142. 2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg)
  5143. progressive JPEG (stb)
  5144. PGM/PPM support (Ken Miller)
  5145. STBI_MALLOC,STBI_REALLOC,STBI_FREE
  5146. GIF bugfix -- seemingly never worked
  5147. STBI_NO_*, STBI_ONLY_*
  5148. 1.48 (2014-12-14) fix incorrectly-named assert()
  5149. 1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb)
  5150. optimize PNG (ryg)
  5151. fix bug in interlaced PNG with user-specified channel count (stb)
  5152. 1.46 (2014-08-26)
  5153. fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG
  5154. 1.45 (2014-08-16)
  5155. fix MSVC-ARM internal compiler error by wrapping malloc
  5156. 1.44 (2014-08-07)
  5157. various warning fixes from Ronny Chevalier
  5158. 1.43 (2014-07-15)
  5159. fix MSVC-only compiler problem in code changed in 1.42
  5160. 1.42 (2014-07-09)
  5161. don't define _CRT_SECURE_NO_WARNINGS (affects user code)
  5162. fixes to stbi__cleanup_jpeg path
  5163. added STBI_ASSERT to avoid requiring assert.h
  5164. 1.41 (2014-06-25)
  5165. fix search&replace from 1.36 that messed up comments/error messages
  5166. 1.40 (2014-06-22)
  5167. fix gcc struct-initialization warning
  5168. 1.39 (2014-06-15)
  5169. fix to TGA optimization when req_comp != number of components in TGA;
  5170. fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite)
  5171. add support for BMP version 5 (more ignored fields)
  5172. 1.38 (2014-06-06)
  5173. suppress MSVC warnings on integer casts truncating values
  5174. fix accidental rename of 'skip' field of I/O
  5175. 1.37 (2014-06-04)
  5176. remove duplicate typedef
  5177. 1.36 (2014-06-03)
  5178. convert to header file single-file library
  5179. if de-iphone isn't set, load iphone images color-swapped instead of returning NULL
  5180. 1.35 (2014-05-27)
  5181. various warnings
  5182. fix broken STBI_SIMD path
  5183. fix bug where stbi_load_from_file no longer left file pointer in correct place
  5184. fix broken non-easy path for 32-bit BMP (possibly never used)
  5185. TGA optimization by Arseny Kapoulkine
  5186. 1.34 (unknown)
  5187. use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case
  5188. 1.33 (2011-07-14)
  5189. make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements
  5190. 1.32 (2011-07-13)
  5191. support for "info" function for all supported filetypes (SpartanJ)
  5192. 1.31 (2011-06-20)
  5193. a few more leak fixes, bug in PNG handling (SpartanJ)
  5194. 1.30 (2011-06-11)
  5195. added ability to load files via callbacks to accomidate custom input streams (Ben Wenger)
  5196. removed deprecated format-specific test/load functions
  5197. removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway
  5198. error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha)
  5199. fix inefficiency in decoding 32-bit BMP (David Woo)
  5200. 1.29 (2010-08-16)
  5201. various warning fixes from Aurelien Pocheville
  5202. 1.28 (2010-08-01)
  5203. fix bug in GIF palette transparency (SpartanJ)
  5204. 1.27 (2010-08-01)
  5205. cast-to-stbi_uc to fix warnings
  5206. 1.26 (2010-07-24)
  5207. fix bug in file buffering for PNG reported by SpartanJ
  5208. 1.25 (2010-07-17)
  5209. refix trans_data warning (Won Chun)
  5210. 1.24 (2010-07-12)
  5211. perf improvements reading from files on platforms with lock-heavy fgetc()
  5212. minor perf improvements for jpeg
  5213. deprecated type-specific functions so we'll get feedback if they're needed
  5214. attempt to fix trans_data warning (Won Chun)
  5215. 1.23 fixed bug in iPhone support
  5216. 1.22 (2010-07-10)
  5217. removed image *writing* support
  5218. stbi_info support from Jetro Lauha
  5219. GIF support from Jean-Marc Lienher
  5220. iPhone PNG-extensions from James Brown
  5221. warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva)
  5222. 1.21 fix use of 'stbi_uc' in header (reported by jon blow)
  5223. 1.20 added support for Softimage PIC, by Tom Seddon
  5224. 1.19 bug in interlaced PNG corruption check (found by ryg)
  5225. 1.18 (2008-08-02)
  5226. fix a threading bug (local mutable static)
  5227. 1.17 support interlaced PNG
  5228. 1.16 major bugfix - stbi__convert_format converted one too many pixels
  5229. 1.15 initialize some fields for thread safety
  5230. 1.14 fix threadsafe conversion bug
  5231. header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
  5232. 1.13 threadsafe
  5233. 1.12 const qualifiers in the API
  5234. 1.11 Support installable IDCT, colorspace conversion routines
  5235. 1.10 Fixes for 64-bit (don't use "unsigned long")
  5236. optimized upsampling by Fabian "ryg" Giesen
  5237. 1.09 Fix format-conversion for PSD code (bad global variables!)
  5238. 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
  5239. 1.07 attempt to fix C++ warning/errors again
  5240. 1.06 attempt to fix C++ warning/errors again
  5241. 1.05 fix TGA loading to return correct *comp and use good luminance calc
  5242. 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
  5243. 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
  5244. 1.02 support for (subset of) HDR files, float interface for preferred access to them
  5245. 1.01 fix bug: possible bug in handling right-side up bmps... not sure
  5246. fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all
  5247. 1.00 interface to zlib that skips zlib header
  5248. 0.99 correct handling of alpha in palette
  5249. 0.98 TGA loader by lonesock; dynamically add loaders (untested)
  5250. 0.97 jpeg errors on too large a file; also catch another malloc failure
  5251. 0.96 fix detection of invalid v value - particleman@mollyrocket forum
  5252. 0.95 during header scan, seek to markers in case of padding
  5253. 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
  5254. 0.93 handle jpegtran output; verbose errors
  5255. 0.92 read 4,8,16,24,32-bit BMP files of several formats
  5256. 0.91 output 24-bit Windows 3.0 BMP files
  5257. 0.90 fix a few more warnings; bump version number to approach 1.0
  5258. 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
  5259. 0.60 fix compiling as c++
  5260. 0.59 fix warnings: merge Dave Moore's -Wall fixes
  5261. 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
  5262. 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available
  5263. 0.56 fix bug: zlib uncompressed mode len vs. nlen
  5264. 0.55 fix bug: restart_interval not initialized to 0
  5265. 0.54 allow NULL for 'int *comp'
  5266. 0.53 fix bug in png 3->4; speedup png decoding
  5267. 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
  5268. 0.51 obey req_comp requests, 1-component jpegs return as 1-component,
  5269. on 'test' only check type, not whether we support this variant
  5270. 0.50 (2006-11-19)
  5271. first released version
  5272. */