stb_image.c 196 KB

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