stb_image.c 196 KB

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