stb_image.c 143 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545
  1. #include "stb_image.h"
  2. #ifndef STBI_HEADER_FILE_ONLY
  3. #ifndef STBI_NO_HDR
  4. #include <math.h> // ldexp
  5. #include <string.h> // strcmp
  6. #endif
  7. #ifndef STBI_NO_STDIO
  8. #include <stdio.h>
  9. #endif
  10. #include <stdlib.h>
  11. #include <memory.h>
  12. #include <assert.h>
  13. #include <stdarg.h>
  14. #ifndef _MSC_VER
  15. #ifdef __cplusplus
  16. #define __forceinline inline
  17. #else
  18. #define __forceinline
  19. #endif
  20. #endif
  21. // implementation:
  22. typedef unsigned char uint8;
  23. typedef unsigned short uint16;
  24. typedef signed short int16;
  25. typedef unsigned int uint32;
  26. typedef signed int int32;
  27. #ifndef ANDROID
  28. typedef unsigned int uint;
  29. #endif
  30. // should produce compiler error if size is wrong
  31. typedef unsigned char validate_uint32[sizeof(uint32)==4 ? 1 : -1];
  32. #if defined(STBI_NO_STDIO) && !defined(STBI_NO_WRITE)
  33. #define STBI_NO_WRITE
  34. #endif
  35. #define STBI_NOTUSED(v) v=v
  36. #ifdef _MSC_VER
  37. #define STBI_HAS_LRTOL
  38. #endif
  39. #ifdef STBI_HAS_LRTOL
  40. #define stbi_lrot(x,y) _lrotl(x,y)
  41. #else
  42. #define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
  43. #endif
  44. //////////////////////////////////////////////////////////////////////////////
  45. //
  46. // Generic API that works on all image types
  47. //
  48. // deprecated functions
  49. // is it a jpeg?
  50. extern int stbi_jpeg_test_memory (stbi_uc const *buffer, int len);
  51. extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  52. extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  53. #ifndef STBI_NO_STDIO
  54. extern stbi_uc *stbi_jpeg_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  55. extern int stbi_jpeg_test_file (FILE *f);
  56. extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  57. extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp);
  58. extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
  59. #endif
  60. // is it a png?
  61. extern int stbi_png_test_memory (stbi_uc const *buffer, int len);
  62. extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  63. extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  64. #ifndef STBI_NO_STDIO
  65. extern stbi_uc *stbi_png_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  66. extern int stbi_png_info (char const *filename, int *x, int *y, int *comp);
  67. extern int stbi_png_test_file (FILE *f);
  68. extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  69. extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp);
  70. #endif
  71. // is it a bmp?
  72. extern int stbi_bmp_test_memory (stbi_uc const *buffer, int len);
  73. extern stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  74. extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  75. #ifndef STBI_NO_STDIO
  76. extern int stbi_bmp_test_file (FILE *f);
  77. extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  78. #endif
  79. // is it a tga?
  80. extern int stbi_tga_test_memory (stbi_uc const *buffer, int len);
  81. extern stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  82. extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  83. #ifndef STBI_NO_STDIO
  84. extern int stbi_tga_test_file (FILE *f);
  85. extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  86. #endif
  87. // is it a psd?
  88. extern int stbi_psd_test_memory (stbi_uc const *buffer, int len);
  89. extern stbi_uc *stbi_psd_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  90. extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  91. #ifndef STBI_NO_STDIO
  92. extern int stbi_psd_test_file (FILE *f);
  93. extern stbi_uc *stbi_psd_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  94. #endif
  95. // is it an hdr?
  96. extern int stbi_hdr_test_memory (stbi_uc const *buffer, int len);
  97. extern float * stbi_hdr_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  98. extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  99. #ifndef STBI_NO_STDIO
  100. extern int stbi_hdr_test_file (FILE *f);
  101. extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  102. #endif
  103. // is it a pic?
  104. extern int stbi_pic_test_memory (stbi_uc const *buffer, int len);
  105. extern stbi_uc *stbi_pic_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  106. extern stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  107. #ifndef STBI_NO_STDIO
  108. extern int stbi_pic_test_file (FILE *f);
  109. extern stbi_uc *stbi_pic_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  110. #endif
  111. // is it a gif?
  112. extern int stbi_gif_test_memory (stbi_uc const *buffer, int len);
  113. extern stbi_uc *stbi_gif_load (char const *filename, int *x, int *y, int *comp, int req_comp);
  114. extern stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
  115. extern int stbi_gif_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  116. #ifndef STBI_NO_STDIO
  117. extern int stbi_gif_test_file (FILE *f);
  118. extern stbi_uc *stbi_gif_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
  119. extern int stbi_gif_info (char const *filename, int *x, int *y, int *comp);
  120. extern int stbi_gif_info_from_file (FILE *f, int *x, int *y, int *comp);
  121. #endif
  122. // this is not threadsafe
  123. static const char *failure_reason;
  124. const char *stbi_failure_reason(void)
  125. {
  126. return failure_reason;
  127. }
  128. static int e(const char *str)
  129. {
  130. failure_reason = str;
  131. return 0;
  132. }
  133. #ifdef STBI_NO_FAILURE_STRINGS
  134. #define e(x,y) 0
  135. #elif defined(STBI_FAILURE_USERMSG)
  136. #define e(x,y) e(y)
  137. #else
  138. #define e(x,y) e(x)
  139. #endif
  140. #define epf(x,y) ((float *) (e(x,y)?NULL:NULL))
  141. #define epuc(x,y) ((unsigned char *) (e(x,y)?NULL:NULL))
  142. void stbi_image_free(void *retval_from_stbi_load)
  143. {
  144. free(retval_from_stbi_load);
  145. }
  146. #define MAX_LOADERS 32
  147. stbi_loader *loaders[MAX_LOADERS];
  148. static int max_loaders = 0;
  149. int stbi_register_loader(stbi_loader *loader)
  150. {
  151. int i;
  152. for (i=0; i < MAX_LOADERS; ++i) {
  153. // already present?
  154. if (loaders[i] == loader)
  155. return 1;
  156. // end of the list?
  157. if (loaders[i] == NULL) {
  158. loaders[i] = loader;
  159. max_loaders = i+1;
  160. return 1;
  161. }
  162. }
  163. // no room for it
  164. return 0;
  165. }
  166. #ifndef STBI_NO_HDR
  167. static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
  168. static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp);
  169. #endif
  170. #ifndef STBI_NO_STDIO
  171. unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
  172. {
  173. FILE *f = fopen(filename, "rb");
  174. unsigned char *result;
  175. if (!f) return epuc("can't fopen", "Unable to open file");
  176. result = stbi_load_from_file(f,x,y,comp,req_comp);
  177. fclose(f);
  178. return result;
  179. }
  180. unsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  181. {
  182. int i;
  183. if (stbi_jpeg_test_file(f)) return stbi_jpeg_load_from_file(f,x,y,comp,req_comp);
  184. if (stbi_png_test_file(f)) return stbi_png_load_from_file(f,x,y,comp,req_comp);
  185. if (stbi_bmp_test_file(f)) return stbi_bmp_load_from_file(f,x,y,comp,req_comp);
  186. if (stbi_gif_test_file(f)) return stbi_gif_load_from_file(f,x,y,comp,req_comp);
  187. if (stbi_psd_test_file(f)) return stbi_psd_load_from_file(f,x,y,comp,req_comp);
  188. if (stbi_pic_test_file(f)) return stbi_pic_load_from_file(f,x,y,comp,req_comp);
  189. #ifndef STBI_NO_HDR
  190. if (stbi_hdr_test_file(f)) {
  191. float *hdr = stbi_hdr_load_from_file(f, x,y,comp,req_comp);
  192. return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
  193. }
  194. #endif
  195. for (i=0; i < max_loaders; ++i)
  196. if (loaders[i]->test_file(f))
  197. return loaders[i]->load_from_file(f,x,y,comp,req_comp);
  198. // test tga last because it's a crappy test!
  199. if (stbi_tga_test_file(f))
  200. return stbi_tga_load_from_file(f,x,y,comp,req_comp);
  201. return epuc("unknown image type", "Image not of any known type, or corrupt");
  202. }
  203. #endif
  204. unsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  205. {
  206. int i;
  207. if (stbi_jpeg_test_memory(buffer,len)) return stbi_jpeg_load_from_memory(buffer,len,x,y,comp,req_comp);
  208. if (stbi_png_test_memory(buffer,len)) return stbi_png_load_from_memory(buffer,len,x,y,comp,req_comp);
  209. if (stbi_bmp_test_memory(buffer,len)) return stbi_bmp_load_from_memory(buffer,len,x,y,comp,req_comp);
  210. if (stbi_gif_test_memory(buffer,len)) return stbi_gif_load_from_memory(buffer,len,x,y,comp,req_comp);
  211. if (stbi_psd_test_memory(buffer,len)) return stbi_psd_load_from_memory(buffer,len,x,y,comp,req_comp);
  212. if (stbi_pic_test_memory(buffer,len)) return stbi_pic_load_from_memory(buffer,len,x,y,comp,req_comp);
  213. #ifndef STBI_NO_HDR
  214. if (stbi_hdr_test_memory(buffer, len)) {
  215. float *hdr = stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);
  216. return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
  217. }
  218. #endif
  219. for (i=0; i < max_loaders; ++i)
  220. if (loaders[i]->test_memory(buffer,len))
  221. return loaders[i]->load_from_memory(buffer,len,x,y,comp,req_comp);
  222. // test tga last because it's a crappy test!
  223. if (stbi_tga_test_memory(buffer,len))
  224. return stbi_tga_load_from_memory(buffer,len,x,y,comp,req_comp);
  225. return epuc("unknown image type", "Image not of any known type, or corrupt");
  226. }
  227. #ifndef STBI_NO_HDR
  228. #ifndef STBI_NO_STDIO
  229. float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
  230. {
  231. FILE *f = fopen(filename, "rb");
  232. float *result;
  233. if (!f) return epf("can't fopen", "Unable to open file");
  234. result = stbi_loadf_from_file(f,x,y,comp,req_comp);
  235. fclose(f);
  236. return result;
  237. }
  238. float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  239. {
  240. unsigned char *data;
  241. #ifndef STBI_NO_HDR
  242. if (stbi_hdr_test_file(f))
  243. return stbi_hdr_load_from_file(f,x,y,comp,req_comp);
  244. #endif
  245. data = stbi_load_from_file(f, x, y, comp, req_comp);
  246. if (data)
  247. return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
  248. return epf("unknown image type", "Image not of any known type, or corrupt");
  249. }
  250. #endif
  251. float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  252. {
  253. stbi_uc *data;
  254. #ifndef STBI_NO_HDR
  255. if (stbi_hdr_test_memory(buffer, len))
  256. return stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);
  257. #endif
  258. data = stbi_load_from_memory(buffer, len, x, y, comp, req_comp);
  259. if (data)
  260. return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
  261. return epf("unknown image type", "Image not of any known type, or corrupt");
  262. }
  263. #endif
  264. // these is-hdr-or-not is defined independent of whether STBI_NO_HDR is
  265. // defined, for API simplicity; if STBI_NO_HDR is defined, it always
  266. // reports false!
  267. int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
  268. {
  269. #ifndef STBI_NO_HDR
  270. return stbi_hdr_test_memory(buffer, len);
  271. #else
  272. STBI_NOTUSED(buffer);
  273. STBI_NOTUSED(len);
  274. return 0;
  275. #endif
  276. }
  277. #ifndef STBI_NO_STDIO
  278. extern int stbi_is_hdr (char const *filename)
  279. {
  280. FILE *f = fopen(filename, "rb");
  281. int result=0;
  282. if (f) {
  283. result = stbi_is_hdr_from_file(f);
  284. fclose(f);
  285. }
  286. return result;
  287. }
  288. extern int stbi_is_hdr_from_file(FILE *f)
  289. {
  290. #ifndef STBI_NO_HDR
  291. return stbi_hdr_test_file(f);
  292. #else
  293. return 0;
  294. #endif
  295. }
  296. #endif
  297. #ifndef STBI_NO_HDR
  298. static float h2l_gamma_i=1.0f/2.2f, h2l_scale_i=1.0f;
  299. static float l2h_gamma=2.2f, l2h_scale=1.0f;
  300. void stbi_hdr_to_ldr_gamma(float gamma) { h2l_gamma_i = 1/gamma; }
  301. void stbi_hdr_to_ldr_scale(float scale) { h2l_scale_i = 1/scale; }
  302. void stbi_ldr_to_hdr_gamma(float gamma) { l2h_gamma = gamma; }
  303. void stbi_ldr_to_hdr_scale(float scale) { l2h_scale = scale; }
  304. #endif
  305. //////////////////////////////////////////////////////////////////////////////
  306. //
  307. // Common code used by all image loaders
  308. //
  309. enum
  310. {
  311. SCAN_load=0,
  312. SCAN_type,
  313. SCAN_header
  314. };
  315. typedef struct
  316. {
  317. uint32 img_x, img_y;
  318. int img_n, img_out_n;
  319. #ifndef STBI_NO_STDIO
  320. FILE *img_file;
  321. int buflen;
  322. uint8 buffer_start[128];
  323. int from_file;
  324. #endif
  325. uint8 *img_buffer, *img_buffer_end;
  326. } stbi;
  327. #ifndef STBI_NO_STDIO
  328. static void start_file(stbi *s, FILE *f)
  329. {
  330. s->img_file = f;
  331. s->buflen = sizeof(s->buffer_start);
  332. s->img_buffer_end = s->buffer_start + s->buflen;
  333. s->img_buffer = s->img_buffer_end;
  334. s->from_file = 1;
  335. }
  336. #endif
  337. static void start_mem(stbi *s, uint8 const *buffer, int len)
  338. {
  339. #ifndef STBI_NO_STDIO
  340. s->img_file = NULL;
  341. s->from_file = 0;
  342. #endif
  343. s->img_buffer = (uint8 *) buffer;
  344. s->img_buffer_end = (uint8 *) buffer+len;
  345. }
  346. #ifndef STBI_NO_STDIO
  347. static void refill_buffer(stbi *s)
  348. {
  349. int n = fread(s->buffer_start, 1, s->buflen, s->img_file);
  350. if (n == 0) {
  351. s->from_file = 0;
  352. s->img_buffer = s->img_buffer_end-1;
  353. *s->img_buffer = 0;
  354. } else {
  355. s->img_buffer = s->buffer_start;
  356. s->img_buffer_end = s->buffer_start + n;
  357. }
  358. }
  359. #endif
  360. __forceinline static int get8(stbi *s)
  361. {
  362. if (s->img_buffer < s->img_buffer_end)
  363. return *s->img_buffer++;
  364. #ifndef STBI_NO_STDIO
  365. if (s->from_file) {
  366. refill_buffer(s);
  367. return *s->img_buffer++;
  368. }
  369. #endif
  370. return 0;
  371. }
  372. __forceinline static int at_eof(stbi *s)
  373. {
  374. #ifndef STBI_NO_STDIO
  375. if (s->img_file) {
  376. if (!feof(s->img_file)) return 0;
  377. // if feof() is true, check if buffer = end
  378. // special case: we've only got the special 0 character at the end
  379. if (s->from_file == 0) return 1;
  380. }
  381. #endif
  382. return s->img_buffer >= s->img_buffer_end;
  383. }
  384. __forceinline static uint8 get8u(stbi *s)
  385. {
  386. return (uint8) get8(s);
  387. }
  388. static void skip(stbi *s, int n)
  389. {
  390. #ifndef STBI_NO_STDIO
  391. if (s->img_file) {
  392. int blen = s->img_buffer_end - s->img_buffer;
  393. if (blen < n) {
  394. s->img_buffer = s->img_buffer_end;
  395. fseek(s->img_file, n - blen, SEEK_CUR);
  396. return;
  397. }
  398. }
  399. #endif
  400. s->img_buffer += n;
  401. }
  402. static int getn(stbi *s, stbi_uc *buffer, int n)
  403. {
  404. #ifndef STBI_NO_STDIO
  405. if (s->img_file) {
  406. int blen = s->img_buffer_end - s->img_buffer;
  407. if (blen < n) {
  408. int res;
  409. memcpy(buffer, s->img_buffer, blen);
  410. res = ((int) fread(buffer + blen, 1, n - blen, s->img_file) == (n-blen));
  411. s->img_buffer = s->img_buffer_end;
  412. return res;
  413. }
  414. }
  415. #endif
  416. if (s->img_buffer+n <= s->img_buffer_end) {
  417. memcpy(buffer, s->img_buffer, n);
  418. s->img_buffer += n;
  419. return 1;
  420. } else
  421. return 0;
  422. }
  423. static int get16(stbi *s)
  424. {
  425. int z = get8(s);
  426. return (z << 8) + get8(s);
  427. }
  428. static uint32 get32(stbi *s)
  429. {
  430. uint32 z = get16(s);
  431. return (z << 16) + get16(s);
  432. }
  433. static int get16le(stbi *s)
  434. {
  435. int z = get8(s);
  436. return z + (get8(s) << 8);
  437. }
  438. static uint32 get32le(stbi *s)
  439. {
  440. uint32 z = get16le(s);
  441. return z + (get16le(s) << 16);
  442. }
  443. //////////////////////////////////////////////////////////////////////////////
  444. //
  445. // generic converter from built-in img_n to req_comp
  446. // individual types do this automatically as much as possible (e.g. jpeg
  447. // does all cases internally since it needs to colorspace convert anyway,
  448. // and it never has alpha, so very few cases ). png can automatically
  449. // interleave an alpha=255 channel, but falls back to this for other cases
  450. //
  451. // assume data buffer is malloced, so malloc a new one and free that one
  452. // only failure mode is malloc failing
  453. static uint8 compute_y(int r, int g, int b)
  454. {
  455. return (uint8) (((r*77) + (g*150) + (29*b)) >> 8);
  456. }
  457. static unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, uint x, uint y)
  458. {
  459. int i,j;
  460. unsigned char *good;
  461. if (req_comp == img_n) return data;
  462. assert(req_comp >= 1 && req_comp <= 4);
  463. good = (unsigned char *) malloc(req_comp * x * y);
  464. if (good == NULL) {
  465. free(data);
  466. return epuc("outofmem", "Out of memory");
  467. }
  468. for (j=0; j < (int) y; ++j) {
  469. unsigned char *src = data + j * x * img_n ;
  470. unsigned char *dest = good + j * x * req_comp;
  471. #define COMBO(a,b) ((a)*8+(b))
  472. #define CASE(a,b) case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
  473. // convert source image with img_n components to one with req_comp components;
  474. // avoid switch per pixel, so use switch per scanline and massive macros
  475. switch (COMBO(img_n, req_comp)) {
  476. CASE(1,2) dest[0]=src[0], dest[1]=255; break;
  477. CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break;
  478. CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break;
  479. CASE(2,1) dest[0]=src[0]; break;
  480. CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break;
  481. CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break;
  482. CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break;
  483. CASE(3,1) dest[0]=compute_y(src[0],src[1],src[2]); break;
  484. CASE(3,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = 255; break;
  485. CASE(4,1) dest[0]=compute_y(src[0],src[1],src[2]); break;
  486. CASE(4,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break;
  487. CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break;
  488. default: assert(0);
  489. }
  490. #undef CASE
  491. }
  492. free(data);
  493. return good;
  494. }
  495. #ifndef STBI_NO_HDR
  496. static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
  497. {
  498. int i,k,n;
  499. float *output = (float *) malloc(x * y * comp * sizeof(float));
  500. if (output == NULL) { free(data); return epf("outofmem", "Out of memory"); }
  501. // compute number of non-alpha components
  502. if (comp & 1) n = comp; else n = comp-1;
  503. for (i=0; i < x*y; ++i) {
  504. for (k=0; k < n; ++k) {
  505. output[i*comp + k] = (float) pow(data[i*comp+k]/255.0f, l2h_gamma) * l2h_scale;
  506. }
  507. if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
  508. }
  509. free(data);
  510. return output;
  511. }
  512. #define float2int(x) ((int) (x))
  513. static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp)
  514. {
  515. int i,k,n;
  516. stbi_uc *output = (stbi_uc *) malloc(x * y * comp);
  517. if (output == NULL) { free(data); return epuc("outofmem", "Out of memory"); }
  518. // compute number of non-alpha components
  519. if (comp & 1) n = comp; else n = comp-1;
  520. for (i=0; i < x*y; ++i) {
  521. for (k=0; k < n; ++k) {
  522. float z = (float) pow(data[i*comp+k]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f;
  523. if (z < 0) z = 0;
  524. if (z > 255) z = 255;
  525. output[i*comp + k] = (uint8) float2int(z);
  526. }
  527. if (k < comp) {
  528. float z = data[i*comp+k] * 255 + 0.5f;
  529. if (z < 0) z = 0;
  530. if (z > 255) z = 255;
  531. output[i*comp + k] = (uint8) float2int(z);
  532. }
  533. }
  534. free(data);
  535. return output;
  536. }
  537. #endif
  538. //////////////////////////////////////////////////////////////////////////////
  539. //
  540. // "baseline" JPEG/JFIF decoder (not actually fully baseline implementation)
  541. //
  542. // simple implementation
  543. // - channel subsampling of at most 2 in each dimension
  544. // - doesn't support delayed output of y-dimension
  545. // - simple interface (only one output format: 8-bit interleaved RGB)
  546. // - doesn't try to recover corrupt jpegs
  547. // - doesn't allow partial loading, loading multiple at once
  548. // - still fast on x86 (copying globals into locals doesn't help x86)
  549. // - allocates lots of intermediate memory (full size of all components)
  550. // - non-interleaved case requires this anyway
  551. // - allows good upsampling (see next)
  552. // high-quality
  553. // - upsampled channels are bilinearly interpolated, even across blocks
  554. // - quality integer IDCT derived from IJG's 'slow'
  555. // performance
  556. // - fast huffman; reasonable integer IDCT
  557. // - uses a lot of intermediate memory, could cache poorly
  558. // - load http://nothings.org/remote/anemones.jpg 3 times on 2.8Ghz P4
  559. // stb_jpeg: 1.34 seconds (MSVC6, default release build)
  560. // stb_jpeg: 1.06 seconds (MSVC6, processor = Pentium Pro)
  561. // IJL11.dll: 1.08 seconds (compiled by intel)
  562. // IJG 1998: 0.98 seconds (MSVC6, makefile provided by IJG)
  563. // IJG 1998: 0.95 seconds (MSVC6, makefile + proc=PPro)
  564. // huffman decoding acceleration
  565. #define FAST_BITS 9 // larger handles more cases; smaller stomps less cache
  566. typedef struct
  567. {
  568. uint8 fast[1 << FAST_BITS];
  569. // weirdly, repacking this into AoS is a 10% speed loss, instead of a win
  570. uint16 code[256];
  571. uint8 values[256];
  572. uint8 size[257];
  573. unsigned int maxcode[18];
  574. int delta[17]; // old 'firstsymbol' - old 'firstcode'
  575. } huffman;
  576. typedef struct
  577. {
  578. #ifdef STBI_SIMD
  579. unsigned short dequant2[4][64];
  580. #endif
  581. stbi s;
  582. huffman huff_dc[4];
  583. huffman huff_ac[4];
  584. uint8 dequant[4][64];
  585. // sizes for components, interleaved MCUs
  586. int img_h_max, img_v_max;
  587. int img_mcu_x, img_mcu_y;
  588. int img_mcu_w, img_mcu_h;
  589. // definition of jpeg image component
  590. struct
  591. {
  592. int id;
  593. int h,v;
  594. int tq;
  595. int hd,ha;
  596. int dc_pred;
  597. int x,y,w2,h2;
  598. uint8 *data;
  599. void *raw_data;
  600. uint8 *linebuf;
  601. } img_comp[4];
  602. uint32 code_buffer; // jpeg entropy-coded buffer
  603. int code_bits; // number of valid bits
  604. unsigned char marker; // marker seen while filling entropy buffer
  605. int nomore; // flag if we saw a marker so must stop
  606. int scan_n, order[4];
  607. int restart_interval, todo;
  608. } jpeg;
  609. static int build_huffman(huffman *h, int *count)
  610. {
  611. int i,j,k=0,code;
  612. // build size list for each symbol (from JPEG spec)
  613. for (i=0; i < 16; ++i)
  614. for (j=0; j < count[i]; ++j)
  615. h->size[k++] = (uint8) (i+1);
  616. h->size[k] = 0;
  617. // compute actual symbols (from jpeg spec)
  618. code = 0;
  619. k = 0;
  620. for(j=1; j <= 16; ++j) {
  621. // compute delta to add to code to compute symbol id
  622. h->delta[j] = k - code;
  623. if (h->size[k] == j) {
  624. while (h->size[k] == j)
  625. h->code[k++] = (uint16) (code++);
  626. if (code-1 >= (1 << j)) return e("bad code lengths","Corrupt JPEG");
  627. }
  628. // compute largest code + 1 for this size, preshifted as needed later
  629. h->maxcode[j] = code << (16-j);
  630. code <<= 1;
  631. }
  632. h->maxcode[j] = 0xffffffff;
  633. // build non-spec acceleration table; 255 is flag for not-accelerated
  634. memset(h->fast, 255, 1 << FAST_BITS);
  635. for (i=0; i < k; ++i) {
  636. int s = h->size[i];
  637. if (s <= FAST_BITS) {
  638. int c = h->code[i] << (FAST_BITS-s);
  639. int m = 1 << (FAST_BITS-s);
  640. for (j=0; j < m; ++j) {
  641. h->fast[c+j] = (uint8) i;
  642. }
  643. }
  644. }
  645. return 1;
  646. }
  647. static void grow_buffer_unsafe(jpeg *j)
  648. {
  649. do {
  650. int b = j->nomore ? 0 : get8(&j->s);
  651. if (b == 0xff) {
  652. int c = get8(&j->s);
  653. if (c != 0) {
  654. j->marker = (unsigned char) c;
  655. j->nomore = 1;
  656. return;
  657. }
  658. }
  659. j->code_buffer |= b << (24 - j->code_bits);
  660. j->code_bits += 8;
  661. } while (j->code_bits <= 24);
  662. }
  663. // (1 << n) - 1
  664. static uint32 bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
  665. // decode a jpeg huffman value from the bitstream
  666. __forceinline static int decode(jpeg *j, huffman *h)
  667. {
  668. unsigned int temp;
  669. int c,k;
  670. if (j->code_bits < 16) grow_buffer_unsafe(j);
  671. // look at the top FAST_BITS and determine what symbol ID it is,
  672. // if the code is <= FAST_BITS
  673. c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
  674. k = h->fast[c];
  675. if (k < 255) {
  676. int s = h->size[k];
  677. if (s > j->code_bits)
  678. return -1;
  679. j->code_buffer <<= s;
  680. j->code_bits -= s;
  681. return h->values[k];
  682. }
  683. // naive test is to shift the code_buffer down so k bits are
  684. // valid, then test against maxcode. To speed this up, we've
  685. // preshifted maxcode left so that it has (16-k) 0s at the
  686. // end; in other words, regardless of the number of bits, it
  687. // wants to be compared against something shifted to have 16;
  688. // that way we don't need to shift inside the loop.
  689. temp = j->code_buffer >> 16;
  690. for (k=FAST_BITS+1 ; ; ++k)
  691. if (temp < h->maxcode[k])
  692. break;
  693. if (k == 17) {
  694. // error! code not found
  695. j->code_bits -= 16;
  696. return -1;
  697. }
  698. if (k > j->code_bits)
  699. return -1;
  700. // convert the huffman code to the symbol id
  701. c = ((j->code_buffer >> (32 - k)) & bmask[k]) + h->delta[k];
  702. assert((((j->code_buffer) >> (32 - h->size[c])) & bmask[h->size[c]]) == h->code[c]);
  703. // convert the id to a symbol
  704. j->code_bits -= k;
  705. j->code_buffer <<= k;
  706. return h->values[c];
  707. }
  708. // combined JPEG 'receive' and JPEG 'extend', since baseline
  709. // always extends everything it receives.
  710. __forceinline static int extend_receive(jpeg *j, int n)
  711. {
  712. unsigned int m = 1 << (n-1);
  713. unsigned int k;
  714. if (j->code_bits < n) grow_buffer_unsafe(j);
  715. #if 1
  716. k = stbi_lrot(j->code_buffer, n);
  717. j->code_buffer = k & ~bmask[n];
  718. k &= bmask[n];
  719. j->code_bits -= n;
  720. #else
  721. k = (j->code_buffer >> (32 - n)) & bmask[n];
  722. j->code_bits -= n;
  723. j->code_buffer <<= n;
  724. #endif
  725. // the following test is probably a random branch that won't
  726. // predict well. I tried to table accelerate it but failed.
  727. // maybe it's compiling as a conditional move?
  728. if (k < m)
  729. return (-1 << n) + k + 1;
  730. else
  731. return k;
  732. }
  733. // given a value that's at position X in the zigzag stream,
  734. // where does it appear in the 8x8 matrix coded as row-major?
  735. static uint8 dezigzag[64+15] =
  736. {
  737. 0, 1, 8, 16, 9, 2, 3, 10,
  738. 17, 24, 32, 25, 18, 11, 4, 5,
  739. 12, 19, 26, 33, 40, 48, 41, 34,
  740. 27, 20, 13, 6, 7, 14, 21, 28,
  741. 35, 42, 49, 56, 57, 50, 43, 36,
  742. 29, 22, 15, 23, 30, 37, 44, 51,
  743. 58, 59, 52, 45, 38, 31, 39, 46,
  744. 53, 60, 61, 54, 47, 55, 62, 63,
  745. // let corrupt input sample past end
  746. 63, 63, 63, 63, 63, 63, 63, 63,
  747. 63, 63, 63, 63, 63, 63, 63
  748. };
  749. // decode one 64-entry block--
  750. static int decode_block(jpeg *j, short data[64], huffman *hdc, huffman *hac, int b)
  751. {
  752. int diff,dc,k;
  753. int t = decode(j, hdc);
  754. if (t < 0) return e("bad huffman code","Corrupt JPEG");
  755. // 0 all the ac values now so we can do it 32-bits at a time
  756. memset(data,0,64*sizeof(data[0]));
  757. diff = t ? extend_receive(j, t) : 0;
  758. dc = j->img_comp[b].dc_pred + diff;
  759. j->img_comp[b].dc_pred = dc;
  760. data[0] = (short) dc;
  761. // decode AC components, see JPEG spec
  762. k = 1;
  763. do {
  764. int r,s;
  765. int rs = decode(j, hac);
  766. if (rs < 0) return e("bad huffman code","Corrupt JPEG");
  767. s = rs & 15;
  768. r = rs >> 4;
  769. if (s == 0) {
  770. if (rs != 0xf0) break; // end block
  771. k += 16;
  772. } else {
  773. k += r;
  774. // decode into unzigzag'd location
  775. data[dezigzag[k++]] = (short) extend_receive(j,s);
  776. }
  777. } while (k < 64);
  778. return 1;
  779. }
  780. // take a -128..127 value and clamp it and convert to 0..255
  781. __forceinline static uint8 clamp(int x)
  782. {
  783. // trick to use a single test to catch both cases
  784. if ((unsigned int) x > 255) {
  785. if (x < 0) return 0;
  786. if (x > 255) return 255;
  787. }
  788. return (uint8) x;
  789. }
  790. #define f2f(x) (int) (((x) * 4096 + 0.5))
  791. #define fsh(x) ((x) << 12)
  792. // derived from jidctint -- DCT_ISLOW
  793. #define IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
  794. int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
  795. p2 = s2; \
  796. p3 = s6; \
  797. p1 = (p2+p3) * f2f(0.5411961f); \
  798. t2 = p1 + p3*f2f(-1.847759065f); \
  799. t3 = p1 + p2*f2f( 0.765366865f); \
  800. p2 = s0; \
  801. p3 = s4; \
  802. t0 = fsh(p2+p3); \
  803. t1 = fsh(p2-p3); \
  804. x0 = t0+t3; \
  805. x3 = t0-t3; \
  806. x1 = t1+t2; \
  807. x2 = t1-t2; \
  808. t0 = s7; \
  809. t1 = s5; \
  810. t2 = s3; \
  811. t3 = s1; \
  812. p3 = t0+t2; \
  813. p4 = t1+t3; \
  814. p1 = t0+t3; \
  815. p2 = t1+t2; \
  816. p5 = (p3+p4)*f2f( 1.175875602f); \
  817. t0 = t0*f2f( 0.298631336f); \
  818. t1 = t1*f2f( 2.053119869f); \
  819. t2 = t2*f2f( 3.072711026f); \
  820. t3 = t3*f2f( 1.501321110f); \
  821. p1 = p5 + p1*f2f(-0.899976223f); \
  822. p2 = p5 + p2*f2f(-2.562915447f); \
  823. p3 = p3*f2f(-1.961570560f); \
  824. p4 = p4*f2f(-0.390180644f); \
  825. t3 += p1+p4; \
  826. t2 += p2+p3; \
  827. t1 += p2+p4; \
  828. t0 += p1+p3;
  829. #ifdef STBI_SIMD
  830. typedef unsigned short stbi_dequantize_t;
  831. #else
  832. typedef uint8 stbi_dequantize_t;
  833. #endif
  834. // .344 seconds on 3*anemones.jpg
  835. static void idct_block(uint8 *out, int out_stride, short data[64], stbi_dequantize_t *dequantize)
  836. {
  837. int i,val[64],*v=val;
  838. stbi_dequantize_t *dq = dequantize;
  839. uint8 *o;
  840. short *d = data;
  841. // columns
  842. for (i=0; i < 8; ++i,++d,++dq, ++v) {
  843. // if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing
  844. if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
  845. && d[40]==0 && d[48]==0 && d[56]==0) {
  846. // no shortcut 0 seconds
  847. // (1|2|3|4|5|6|7)==0 0 seconds
  848. // all separate -0.047 seconds
  849. // 1 && 2|3 && 4|5 && 6|7: -0.047 seconds
  850. int dcterm = d[0] * dq[0] << 2;
  851. v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
  852. } else {
  853. IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24],
  854. d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56])
  855. // constants scaled things up by 1<<12; let's bring them back
  856. // down, but keep 2 extra bits of precision
  857. x0 += 512; x1 += 512; x2 += 512; x3 += 512;
  858. v[ 0] = (x0+t3) >> 10;
  859. v[56] = (x0-t3) >> 10;
  860. v[ 8] = (x1+t2) >> 10;
  861. v[48] = (x1-t2) >> 10;
  862. v[16] = (x2+t1) >> 10;
  863. v[40] = (x2-t1) >> 10;
  864. v[24] = (x3+t0) >> 10;
  865. v[32] = (x3-t0) >> 10;
  866. }
  867. }
  868. for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
  869. // no fast case since the first 1D IDCT spread components out
  870. IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
  871. // constants scaled things up by 1<<12, plus we had 1<<2 from first
  872. // loop, plus horizontal and vertical each scale by sqrt(8) so together
  873. // we've got an extra 1<<3, so 1<<17 total we need to remove.
  874. // so we want to round that, which means adding 0.5 * 1<<17,
  875. // aka 65536. Also, we'll end up with -128 to 127 that we want
  876. // to encode as 0..255 by adding 128, so we'll add that before the shift
  877. x0 += 65536 + (128<<17);
  878. x1 += 65536 + (128<<17);
  879. x2 += 65536 + (128<<17);
  880. x3 += 65536 + (128<<17);
  881. // tried computing the shifts into temps, or'ing the temps to see
  882. // if any were out of range, but that was slower
  883. o[0] = clamp((x0+t3) >> 17);
  884. o[7] = clamp((x0-t3) >> 17);
  885. o[1] = clamp((x1+t2) >> 17);
  886. o[6] = clamp((x1-t2) >> 17);
  887. o[2] = clamp((x2+t1) >> 17);
  888. o[5] = clamp((x2-t1) >> 17);
  889. o[3] = clamp((x3+t0) >> 17);
  890. o[4] = clamp((x3-t0) >> 17);
  891. }
  892. }
  893. #ifdef STBI_SIMD
  894. static stbi_idct_8x8 stbi_idct_installed = idct_block;
  895. extern void stbi_install_idct(stbi_idct_8x8 func)
  896. {
  897. stbi_idct_installed = func;
  898. }
  899. #endif
  900. #define MARKER_none 0xff
  901. // if there's a pending marker from the entropy stream, return that
  902. // otherwise, fetch from the stream and get a marker. if there's no
  903. // marker, return 0xff, which is never a valid marker value
  904. static uint8 get_marker(jpeg *j)
  905. {
  906. uint8 x;
  907. if (j->marker != MARKER_none) { x = j->marker; j->marker = MARKER_none; return x; }
  908. x = get8u(&j->s);
  909. if (x != 0xff) return MARKER_none;
  910. while (x == 0xff)
  911. x = get8u(&j->s);
  912. return x;
  913. }
  914. // in each scan, we'll have scan_n components, and the order
  915. // of the components is specified by order[]
  916. #define RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
  917. // after a restart interval, reset the entropy decoder and
  918. // the dc prediction
  919. static void reset(jpeg *j)
  920. {
  921. j->code_bits = 0;
  922. j->code_buffer = 0;
  923. j->nomore = 0;
  924. j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;
  925. j->marker = MARKER_none;
  926. j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
  927. // no more than 1<<31 MCUs if no restart_interal? that's plenty safe,
  928. // since we don't even allow 1<<30 pixels
  929. }
  930. static int parse_entropy_coded_data(jpeg *z)
  931. {
  932. reset(z);
  933. if (z->scan_n == 1) {
  934. int i,j;
  935. #ifdef STBI_SIMD
  936. __declspec(align(16))
  937. #endif
  938. short data[64];
  939. int n = z->order[0];
  940. // non-interleaved data, we just need to process one block at a time,
  941. // in trivial scanline order
  942. // number of blocks to do just depends on how many actual "pixels" this
  943. // component has, independent of interleaved MCU blocking and such
  944. int w = (z->img_comp[n].x+7) >> 3;
  945. int h = (z->img_comp[n].y+7) >> 3;
  946. for (j=0; j < h; ++j) {
  947. for (i=0; i < w; ++i) {
  948. if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
  949. #ifdef STBI_SIMD
  950. stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
  951. #else
  952. idct_block(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
  953. #endif
  954. // every data block is an MCU, so countdown the restart interval
  955. if (--z->todo <= 0) {
  956. if (z->code_bits < 24) grow_buffer_unsafe(z);
  957. // if it's NOT a restart, then just bail, so we get corrupt data
  958. // rather than no data
  959. if (!RESTART(z->marker)) return 1;
  960. reset(z);
  961. }
  962. }
  963. }
  964. } else { // interleaved!
  965. int i,j,k,x,y;
  966. short data[64];
  967. for (j=0; j < z->img_mcu_y; ++j) {
  968. for (i=0; i < z->img_mcu_x; ++i) {
  969. // scan an interleaved mcu... process scan_n components in order
  970. for (k=0; k < z->scan_n; ++k) {
  971. int n = z->order[k];
  972. // scan out an mcu's worth of this component; that's just determined
  973. // by the basic H and V specified for the component
  974. for (y=0; y < z->img_comp[n].v; ++y) {
  975. for (x=0; x < z->img_comp[n].h; ++x) {
  976. int x2 = (i*z->img_comp[n].h + x)*8;
  977. int y2 = (j*z->img_comp[n].v + y)*8;
  978. if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
  979. #ifdef STBI_SIMD
  980. stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
  981. #else
  982. idct_block(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
  983. #endif
  984. }
  985. }
  986. }
  987. // after all interleaved components, that's an interleaved MCU,
  988. // so now count down the restart interval
  989. if (--z->todo <= 0) {
  990. if (z->code_bits < 24) grow_buffer_unsafe(z);
  991. // if it's NOT a restart, then just bail, so we get corrupt data
  992. // rather than no data
  993. if (!RESTART(z->marker)) return 1;
  994. reset(z);
  995. }
  996. }
  997. }
  998. }
  999. return 1;
  1000. }
  1001. static int process_marker(jpeg *z, int m)
  1002. {
  1003. int L;
  1004. switch (m) {
  1005. case MARKER_none: // no marker found
  1006. return e("expected marker","Corrupt JPEG");
  1007. case 0xC2: // SOF - progressive
  1008. return e("progressive jpeg","JPEG format not supported (progressive)");
  1009. case 0xDD: // DRI - specify restart interval
  1010. if (get16(&z->s) != 4) return e("bad DRI len","Corrupt JPEG");
  1011. z->restart_interval = get16(&z->s);
  1012. return 1;
  1013. case 0xDB: // DQT - define quantization table
  1014. L = get16(&z->s)-2;
  1015. while (L > 0) {
  1016. int q = get8(&z->s);
  1017. int p = q >> 4;
  1018. int t = q & 15,i;
  1019. if (p != 0) return e("bad DQT type","Corrupt JPEG");
  1020. if (t > 3) return e("bad DQT table","Corrupt JPEG");
  1021. for (i=0; i < 64; ++i)
  1022. z->dequant[t][dezigzag[i]] = get8u(&z->s);
  1023. #ifdef STBI_SIMD
  1024. for (i=0; i < 64; ++i)
  1025. z->dequant2[t][i] = z->dequant[t][i];
  1026. #endif
  1027. L -= 65;
  1028. }
  1029. return L==0;
  1030. case 0xC4: // DHT - define huffman table
  1031. L = get16(&z->s)-2;
  1032. while (L > 0) {
  1033. uint8 *v;
  1034. int sizes[16],i,m=0;
  1035. int q = get8(&z->s);
  1036. int tc = q >> 4;
  1037. int th = q & 15;
  1038. if (tc > 1 || th > 3) return e("bad DHT header","Corrupt JPEG");
  1039. for (i=0; i < 16; ++i) {
  1040. sizes[i] = get8(&z->s);
  1041. m += sizes[i];
  1042. }
  1043. L -= 17;
  1044. if (tc == 0) {
  1045. if (!build_huffman(z->huff_dc+th, sizes)) return 0;
  1046. v = z->huff_dc[th].values;
  1047. } else {
  1048. if (!build_huffman(z->huff_ac+th, sizes)) return 0;
  1049. v = z->huff_ac[th].values;
  1050. }
  1051. for (i=0; i < m; ++i)
  1052. v[i] = get8u(&z->s);
  1053. L -= m;
  1054. }
  1055. return L==0;
  1056. }
  1057. // check for comment block or APP blocks
  1058. if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
  1059. skip(&z->s, get16(&z->s)-2);
  1060. return 1;
  1061. }
  1062. return 0;
  1063. }
  1064. // after we see SOS
  1065. static int process_scan_header(jpeg *z)
  1066. {
  1067. int i;
  1068. int Ls = get16(&z->s);
  1069. z->scan_n = get8(&z->s);
  1070. if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s.img_n) return e("bad SOS component count","Corrupt JPEG");
  1071. if (Ls != 6+2*z->scan_n) return e("bad SOS len","Corrupt JPEG");
  1072. for (i=0; i < z->scan_n; ++i) {
  1073. int id = get8(&z->s), which;
  1074. int q = get8(&z->s);
  1075. for (which = 0; which < z->s.img_n; ++which)
  1076. if (z->img_comp[which].id == id)
  1077. break;
  1078. if (which == z->s.img_n) return 0;
  1079. z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return e("bad DC huff","Corrupt JPEG");
  1080. z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return e("bad AC huff","Corrupt JPEG");
  1081. z->order[i] = which;
  1082. }
  1083. if (get8(&z->s) != 0) return e("bad SOS","Corrupt JPEG");
  1084. get8(&z->s); // should be 63, but might be 0
  1085. if (get8(&z->s) != 0) return e("bad SOS","Corrupt JPEG");
  1086. return 1;
  1087. }
  1088. static int process_frame_header(jpeg *z, int scan)
  1089. {
  1090. stbi *s = &z->s;
  1091. int Lf,p,i,q, h_max=1,v_max=1,c;
  1092. Lf = get16(s); if (Lf < 11) return e("bad SOF len","Corrupt JPEG"); // JPEG
  1093. p = get8(s); if (p != 8) return e("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
  1094. s->img_y = get16(s); if (s->img_y == 0) return e("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
  1095. s->img_x = get16(s); if (s->img_x == 0) return e("0 width","Corrupt JPEG"); // JPEG requires
  1096. c = get8(s);
  1097. if (c != 3 && c != 1) return e("bad component count","Corrupt JPEG"); // JFIF requires
  1098. s->img_n = c;
  1099. for (i=0; i < c; ++i) {
  1100. z->img_comp[i].data = NULL;
  1101. z->img_comp[i].linebuf = NULL;
  1102. }
  1103. if (Lf != 8+3*s->img_n) return e("bad SOF len","Corrupt JPEG");
  1104. for (i=0; i < s->img_n; ++i) {
  1105. z->img_comp[i].id = get8(s);
  1106. if (z->img_comp[i].id != i+1) // JFIF requires
  1107. if (z->img_comp[i].id != i) // some version of jpegtran outputs non-JFIF-compliant files!
  1108. return e("bad component ID","Corrupt JPEG");
  1109. q = get8(s);
  1110. z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return e("bad H","Corrupt JPEG");
  1111. z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return e("bad V","Corrupt JPEG");
  1112. z->img_comp[i].tq = get8(s); if (z->img_comp[i].tq > 3) return e("bad TQ","Corrupt JPEG");
  1113. }
  1114. if (scan != SCAN_load) return 1;
  1115. if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode");
  1116. for (i=0; i < s->img_n; ++i) {
  1117. if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
  1118. if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
  1119. }
  1120. // compute interleaved mcu info
  1121. z->img_h_max = h_max;
  1122. z->img_v_max = v_max;
  1123. z->img_mcu_w = h_max * 8;
  1124. z->img_mcu_h = v_max * 8;
  1125. z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
  1126. z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
  1127. for (i=0; i < s->img_n; ++i) {
  1128. // number of effective pixels (e.g. for non-interleaved MCU)
  1129. z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
  1130. z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
  1131. // to simplify generation, we'll allocate enough memory to decode
  1132. // the bogus oversized data from using interleaved MCUs and their
  1133. // big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't
  1134. // discard the extra data until colorspace conversion
  1135. z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
  1136. z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
  1137. z->img_comp[i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);
  1138. if (z->img_comp[i].raw_data == NULL) {
  1139. for(--i; i >= 0; --i) {
  1140. free(z->img_comp[i].raw_data);
  1141. z->img_comp[i].data = NULL;
  1142. }
  1143. return e("outofmem", "Out of memory");
  1144. }
  1145. // align blocks for installable-idct using mmx/sse
  1146. z->img_comp[i].data = (uint8*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);
  1147. z->img_comp[i].linebuf = NULL;
  1148. }
  1149. return 1;
  1150. }
  1151. // use comparisons since in some cases we handle more than one case (e.g. SOF)
  1152. #define DNL(x) ((x) == 0xdc)
  1153. #define SOI(x) ((x) == 0xd8)
  1154. #define EOI(x) ((x) == 0xd9)
  1155. #define SOF(x) ((x) == 0xc0 || (x) == 0xc1)
  1156. #define SOS(x) ((x) == 0xda)
  1157. static int decode_jpeg_header(jpeg *z, int scan)
  1158. {
  1159. int m;
  1160. z->marker = MARKER_none; // initialize cached marker to empty
  1161. m = get_marker(z);
  1162. if (!SOI(m)) return e("no SOI","Corrupt JPEG");
  1163. if (scan == SCAN_type) return 1;
  1164. m = get_marker(z);
  1165. while (!SOF(m)) {
  1166. if (!process_marker(z,m)) return 0;
  1167. m = get_marker(z);
  1168. while (m == MARKER_none) {
  1169. // some files have extra padding after their blocks, so ok, we'll scan
  1170. if (at_eof(&z->s)) return e("no SOF", "Corrupt JPEG");
  1171. m = get_marker(z);
  1172. }
  1173. }
  1174. if (!process_frame_header(z, scan)) return 0;
  1175. return 1;
  1176. }
  1177. static int decode_jpeg_image(jpeg *j)
  1178. {
  1179. int m;
  1180. j->restart_interval = 0;
  1181. if (!decode_jpeg_header(j, SCAN_load)) return 0;
  1182. m = get_marker(j);
  1183. while (!EOI(m)) {
  1184. if (SOS(m)) {
  1185. if (!process_scan_header(j)) return 0;
  1186. if (!parse_entropy_coded_data(j)) return 0;
  1187. if (j->marker == MARKER_none ) {
  1188. // handle 0s at the end of image data from IP Kamera 9060
  1189. while (!at_eof(&j->s)) {
  1190. int x = get8(&j->s);
  1191. if (x == 255) {
  1192. j->marker = get8u(&j->s);
  1193. break;
  1194. } else if (x != 0) {
  1195. return 0;
  1196. }
  1197. }
  1198. // if we reach eof without hitting a marker, get_marker() below will fail and we'll eventually return 0
  1199. }
  1200. } else {
  1201. if (!process_marker(j, m)) return 0;
  1202. }
  1203. m = get_marker(j);
  1204. }
  1205. return 1;
  1206. }
  1207. // static jfif-centered resampling (across block boundaries)
  1208. typedef uint8 *(*resample_row_func)(uint8 *out, uint8 *in0, uint8 *in1,
  1209. int w, int hs);
  1210. #define div4(x) ((uint8) ((x) >> 2))
  1211. static uint8 *resample_row_1(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1212. {
  1213. STBI_NOTUSED(out);
  1214. STBI_NOTUSED(in_far);
  1215. STBI_NOTUSED(w);
  1216. STBI_NOTUSED(hs);
  1217. return in_near;
  1218. }
  1219. static uint8* resample_row_v_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1220. {
  1221. // need to generate two samples vertically for every one in input
  1222. int i;
  1223. STBI_NOTUSED(hs);
  1224. for (i=0; i < w; ++i)
  1225. out[i] = div4(3*in_near[i] + in_far[i] + 2);
  1226. return out;
  1227. }
  1228. static uint8* resample_row_h_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1229. {
  1230. // need to generate two samples horizontally for every one in input
  1231. int i;
  1232. uint8 *input = in_near;
  1233. if (w == 1) {
  1234. // if only one sample, can't do any interpolation
  1235. out[0] = out[1] = input[0];
  1236. return out;
  1237. }
  1238. out[0] = input[0];
  1239. out[1] = div4(input[0]*3 + input[1] + 2);
  1240. for (i=1; i < w-1; ++i) {
  1241. int n = 3*input[i]+2;
  1242. out[i*2+0] = div4(n+input[i-1]);
  1243. out[i*2+1] = div4(n+input[i+1]);
  1244. }
  1245. out[i*2+0] = div4(input[w-2]*3 + input[w-1] + 2);
  1246. out[i*2+1] = input[w-1];
  1247. STBI_NOTUSED(in_far);
  1248. STBI_NOTUSED(hs);
  1249. return out;
  1250. }
  1251. #define div16(x) ((uint8) ((x) >> 4))
  1252. static uint8 *resample_row_hv_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1253. {
  1254. // need to generate 2x2 samples for every one in input
  1255. int i,t0,t1;
  1256. if (w == 1) {
  1257. out[0] = out[1] = div4(3*in_near[0] + in_far[0] + 2);
  1258. return out;
  1259. }
  1260. t1 = 3*in_near[0] + in_far[0];
  1261. out[0] = div4(t1+2);
  1262. for (i=1; i < w; ++i) {
  1263. t0 = t1;
  1264. t1 = 3*in_near[i]+in_far[i];
  1265. out[i*2-1] = div16(3*t0 + t1 + 8);
  1266. out[i*2 ] = div16(3*t1 + t0 + 8);
  1267. }
  1268. out[w*2-1] = div4(t1+2);
  1269. STBI_NOTUSED(hs);
  1270. return out;
  1271. }
  1272. static uint8 *resample_row_generic(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
  1273. {
  1274. // resample with nearest-neighbor
  1275. int i,j;
  1276. in_far = in_far;
  1277. for (i=0; i < w; ++i)
  1278. for (j=0; j < hs; ++j)
  1279. out[i*hs+j] = in_near[i];
  1280. return out;
  1281. }
  1282. #define float2fixed(x) ((int) ((x) * 65536 + 0.5))
  1283. // 0.38 seconds on 3*anemones.jpg (0.25 with processor = Pro)
  1284. // VC6 without processor=Pro is generating multiple LEAs per multiply!
  1285. static void YCbCr_to_RGB_row(uint8 *out, const uint8 *y, const uint8 *pcb, const uint8 *pcr, int count, int step)
  1286. {
  1287. int i;
  1288. for (i=0; i < count; ++i) {
  1289. int y_fixed = (y[i] << 16) + 32768; // rounding
  1290. int r,g,b;
  1291. int cr = pcr[i] - 128;
  1292. int cb = pcb[i] - 128;
  1293. r = y_fixed + cr*float2fixed(1.40200f);
  1294. g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f);
  1295. b = y_fixed + cb*float2fixed(1.77200f);
  1296. r >>= 16;
  1297. g >>= 16;
  1298. b >>= 16;
  1299. if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
  1300. if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
  1301. if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
  1302. out[0] = (uint8)r;
  1303. out[1] = (uint8)g;
  1304. out[2] = (uint8)b;
  1305. out[3] = 255;
  1306. out += step;
  1307. }
  1308. }
  1309. #ifdef STBI_SIMD
  1310. static stbi_YCbCr_to_RGB_run stbi_YCbCr_installed = YCbCr_to_RGB_row;
  1311. void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func)
  1312. {
  1313. stbi_YCbCr_installed = func;
  1314. }
  1315. #endif
  1316. // clean up the temporary component buffers
  1317. static void cleanup_jpeg(jpeg *j)
  1318. {
  1319. int i;
  1320. for (i=0; i < j->s.img_n; ++i) {
  1321. if (j->img_comp[i].data) {
  1322. free(j->img_comp[i].raw_data);
  1323. j->img_comp[i].data = NULL;
  1324. }
  1325. if (j->img_comp[i].linebuf) {
  1326. free(j->img_comp[i].linebuf);
  1327. j->img_comp[i].linebuf = NULL;
  1328. }
  1329. }
  1330. }
  1331. typedef struct
  1332. {
  1333. resample_row_func resample;
  1334. uint8 *line0,*line1;
  1335. int hs,vs; // expansion factor in each axis
  1336. int w_lores; // horizontal pixels pre-expansion
  1337. int ystep; // how far through vertical expansion we are
  1338. int ypos; // which pre-expansion row we're on
  1339. } stbi_resample;
  1340. static uint8 *load_jpeg_image(jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
  1341. {
  1342. int n, decode_n;
  1343. // validate req_comp
  1344. if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
  1345. z->s.img_n = 0;
  1346. // load a jpeg image from whichever source
  1347. if (!decode_jpeg_image(z)) { cleanup_jpeg(z); return NULL; }
  1348. // determine actual number of components to generate
  1349. n = req_comp ? req_comp : z->s.img_n;
  1350. if (z->s.img_n == 3 && n < 3)
  1351. decode_n = 1;
  1352. else
  1353. decode_n = z->s.img_n;
  1354. // resample and color-convert
  1355. {
  1356. int k;
  1357. uint i,j;
  1358. uint8 *output;
  1359. uint8 *coutput[4];
  1360. stbi_resample res_comp[4];
  1361. for (k=0; k < decode_n; ++k) {
  1362. stbi_resample *r = &res_comp[k];
  1363. // allocate line buffer big enough for upsampling off the edges
  1364. // with upsample factor of 4
  1365. z->img_comp[k].linebuf = (uint8 *) malloc(z->s.img_x + 3);
  1366. if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
  1367. r->hs = z->img_h_max / z->img_comp[k].h;
  1368. r->vs = z->img_v_max / z->img_comp[k].v;
  1369. r->ystep = r->vs >> 1;
  1370. r->w_lores = (z->s.img_x + r->hs-1) / r->hs;
  1371. r->ypos = 0;
  1372. r->line0 = r->line1 = z->img_comp[k].data;
  1373. if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
  1374. else if (r->hs == 1 && r->vs == 2) r->resample = resample_row_v_2;
  1375. else if (r->hs == 2 && r->vs == 1) r->resample = resample_row_h_2;
  1376. else if (r->hs == 2 && r->vs == 2) r->resample = resample_row_hv_2;
  1377. else r->resample = resample_row_generic;
  1378. }
  1379. // can't error after this so, this is safe
  1380. output = (uint8 *) malloc(n * z->s.img_x * z->s.img_y + 1);
  1381. if (!output) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
  1382. // now go ahead and resample
  1383. for (j=0; j < z->s.img_y; ++j) {
  1384. uint8 *out = output + n * z->s.img_x * j;
  1385. for (k=0; k < decode_n; ++k) {
  1386. stbi_resample *r = &res_comp[k];
  1387. int y_bot = r->ystep >= (r->vs >> 1);
  1388. coutput[k] = r->resample(z->img_comp[k].linebuf,
  1389. y_bot ? r->line1 : r->line0,
  1390. y_bot ? r->line0 : r->line1,
  1391. r->w_lores, r->hs);
  1392. if (++r->ystep >= r->vs) {
  1393. r->ystep = 0;
  1394. r->line0 = r->line1;
  1395. if (++r->ypos < z->img_comp[k].y)
  1396. r->line1 += z->img_comp[k].w2;
  1397. }
  1398. }
  1399. if (n >= 3) {
  1400. uint8 *y = coutput[0];
  1401. if (z->s.img_n == 3) {
  1402. #ifdef STBI_SIMD
  1403. stbi_YCbCr_installed(out, y, coutput[1], coutput[2], z->s.img_x, n);
  1404. #else
  1405. YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s.img_x, n);
  1406. #endif
  1407. } else
  1408. for (i=0; i < z->s.img_x; ++i) {
  1409. out[0] = out[1] = out[2] = y[i];
  1410. out[3] = 255; // not used if n==3
  1411. out += n;
  1412. }
  1413. } else {
  1414. uint8 *y = coutput[0];
  1415. if (n == 1)
  1416. for (i=0; i < z->s.img_x; ++i) out[i] = y[i];
  1417. else
  1418. for (i=0; i < z->s.img_x; ++i) *out++ = y[i], *out++ = 255;
  1419. }
  1420. }
  1421. cleanup_jpeg(z);
  1422. *out_x = z->s.img_x;
  1423. *out_y = z->s.img_y;
  1424. if (comp) *comp = z->s.img_n; // report original components, not output
  1425. return output;
  1426. }
  1427. }
  1428. #ifndef STBI_NO_STDIO
  1429. unsigned char *stbi_jpeg_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  1430. {
  1431. jpeg j;
  1432. start_file(&j.s, f);
  1433. return load_jpeg_image(&j, x,y,comp,req_comp);
  1434. }
  1435. unsigned char *stbi_jpeg_load(char const *filename, int *x, int *y, int *comp, int req_comp)
  1436. {
  1437. unsigned char *data;
  1438. FILE *f = fopen(filename, "rb");
  1439. if (!f) return NULL;
  1440. data = stbi_jpeg_load_from_file(f,x,y,comp,req_comp);
  1441. fclose(f);
  1442. return data;
  1443. }
  1444. #endif
  1445. unsigned char *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  1446. {
  1447. #ifdef STBI_SMALL_STACK
  1448. unsigned char *result;
  1449. jpeg *j = (jpeg *) malloc(sizeof(*j));
  1450. start_mem(&j->s, buffer, len);
  1451. result = load_jpeg_image(j,x,y,comp,req_comp);
  1452. free(j);
  1453. return result;
  1454. #else
  1455. jpeg j;
  1456. start_mem(&j.s, buffer,len);
  1457. return load_jpeg_image(&j, x,y,comp,req_comp);
  1458. #endif
  1459. }
  1460. static int stbi_jpeg_info_raw(jpeg *j, int *x, int *y, int *comp)
  1461. {
  1462. if (!decode_jpeg_header(j, SCAN_header))
  1463. return 0;
  1464. if (x) *x = j->s.img_x;
  1465. if (y) *y = j->s.img_y;
  1466. if (comp) *comp = j->s.img_n;
  1467. return 1;
  1468. }
  1469. #ifndef STBI_NO_STDIO
  1470. int stbi_jpeg_test_file(FILE *f)
  1471. {
  1472. int n,r;
  1473. jpeg j;
  1474. n = ftell(f);
  1475. start_file(&j.s, f);
  1476. r = decode_jpeg_header(&j, SCAN_type);
  1477. fseek(f,n,SEEK_SET);
  1478. return r;
  1479. }
  1480. int stbi_jpeg_info_from_file(FILE *f, int *x, int *y, int *comp)
  1481. {
  1482. jpeg j;
  1483. long n = ftell(f);
  1484. int res;
  1485. start_file(&j.s, f);
  1486. res = stbi_jpeg_info_raw(&j, x, y, comp);
  1487. fseek(f, n, SEEK_SET);
  1488. return res;
  1489. }
  1490. int stbi_jpeg_info(char const *filename, int *x, int *y, int *comp)
  1491. {
  1492. FILE *f = fopen(filename, "rb");
  1493. int result;
  1494. if (!f) return e("can't fopen", "Unable to open file");
  1495. result = stbi_jpeg_info_from_file(f, x, y, comp);
  1496. fclose(f);
  1497. return result;
  1498. }
  1499. #endif
  1500. int stbi_jpeg_test_memory(stbi_uc const *buffer, int len)
  1501. {
  1502. jpeg j;
  1503. start_mem(&j.s, buffer,len);
  1504. return decode_jpeg_header(&j, SCAN_type);
  1505. }
  1506. int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  1507. {
  1508. jpeg j;
  1509. start_mem(&j.s, buffer, len);
  1510. return stbi_jpeg_info_raw(&j, x, y, comp);
  1511. }
  1512. #ifndef STBI_NO_STDIO
  1513. extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp);
  1514. extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
  1515. #endif
  1516. extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
  1517. // public domain zlib decode v0.2 Sean Barrett 2006-11-18
  1518. // simple implementation
  1519. // - all input must be provided in an upfront buffer
  1520. // - all output is written to a single output buffer (can malloc/realloc)
  1521. // performance
  1522. // - fast huffman
  1523. // fast-way is faster to check than jpeg huffman, but slow way is slower
  1524. #define ZFAST_BITS 9 // accelerate all cases in default tables
  1525. #define ZFAST_MASK ((1 << ZFAST_BITS) - 1)
  1526. // zlib-style huffman encoding
  1527. // (jpegs packs from left, zlib from right, so can't share code)
  1528. typedef struct
  1529. {
  1530. uint16 fast[1 << ZFAST_BITS];
  1531. uint16 firstcode[16];
  1532. int maxcode[17];
  1533. uint16 firstsymbol[16];
  1534. uint8 size[288];
  1535. uint16 value[288];
  1536. } zhuffman;
  1537. __forceinline static int bitreverse16(int n)
  1538. {
  1539. n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
  1540. n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
  1541. n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
  1542. n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
  1543. return n;
  1544. }
  1545. __forceinline static int bit_reverse(int v, int bits)
  1546. {
  1547. assert(bits <= 16);
  1548. // to bit reverse n bits, reverse 16 and shift
  1549. // e.g. 11 bits, bit reverse and shift away 5
  1550. return bitreverse16(v) >> (16-bits);
  1551. }
  1552. static int zbuild_huffman(zhuffman *z, uint8 *sizelist, int num)
  1553. {
  1554. int i,k=0;
  1555. int code, next_code[16], sizes[17];
  1556. // DEFLATE spec for generating codes
  1557. memset(sizes, 0, sizeof(sizes));
  1558. memset(z->fast, 255, sizeof(z->fast));
  1559. for (i=0; i < num; ++i)
  1560. ++sizes[sizelist[i]];
  1561. sizes[0] = 0;
  1562. for (i=1; i < 16; ++i)
  1563. assert(sizes[i] <= (1 << i));
  1564. code = 0;
  1565. for (i=1; i < 16; ++i) {
  1566. next_code[i] = code;
  1567. z->firstcode[i] = (uint16) code;
  1568. z->firstsymbol[i] = (uint16) k;
  1569. code = (code + sizes[i]);
  1570. if (sizes[i])
  1571. if (code-1 >= (1 << i)) return e("bad codelengths","Corrupt JPEG");
  1572. z->maxcode[i] = code << (16-i); // preshift for inner loop
  1573. code <<= 1;
  1574. k += sizes[i];
  1575. }
  1576. z->maxcode[16] = 0x10000; // sentinel
  1577. for (i=0; i < num; ++i) {
  1578. int s = sizelist[i];
  1579. if (s) {
  1580. int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
  1581. z->size[c] = (uint8)s;
  1582. z->value[c] = (uint16)i;
  1583. if (s <= ZFAST_BITS) {
  1584. int k = bit_reverse(next_code[s],s);
  1585. while (k < (1 << ZFAST_BITS)) {
  1586. z->fast[k] = (uint16) c;
  1587. k += (1 << s);
  1588. }
  1589. }
  1590. ++next_code[s];
  1591. }
  1592. }
  1593. return 1;
  1594. }
  1595. // zlib-from-memory implementation for PNG reading
  1596. // because PNG allows splitting the zlib stream arbitrarily,
  1597. // and it's annoying structurally to have PNG call ZLIB call PNG,
  1598. // we require PNG read all the IDATs and combine them into a single
  1599. // memory buffer
  1600. typedef struct
  1601. {
  1602. uint8 *zbuffer, *zbuffer_end;
  1603. int num_bits;
  1604. uint32 code_buffer;
  1605. char *zout;
  1606. char *zout_start;
  1607. char *zout_end;
  1608. int z_expandable;
  1609. zhuffman z_length, z_distance;
  1610. } zbuf;
  1611. __forceinline static int zget8(zbuf *z)
  1612. {
  1613. if (z->zbuffer >= z->zbuffer_end) return 0;
  1614. return *z->zbuffer++;
  1615. }
  1616. static void fill_bits(zbuf *z)
  1617. {
  1618. do {
  1619. assert(z->code_buffer < (1U << z->num_bits));
  1620. z->code_buffer |= zget8(z) << z->num_bits;
  1621. z->num_bits += 8;
  1622. } while (z->num_bits <= 24);
  1623. }
  1624. __forceinline static unsigned int zreceive(zbuf *z, int n)
  1625. {
  1626. unsigned int k;
  1627. if (z->num_bits < n) fill_bits(z);
  1628. k = z->code_buffer & ((1 << n) - 1);
  1629. z->code_buffer >>= n;
  1630. z->num_bits -= n;
  1631. return k;
  1632. }
  1633. __forceinline static int zhuffman_decode(zbuf *a, zhuffman *z)
  1634. {
  1635. int b,s,k;
  1636. if (a->num_bits < 16) fill_bits(a);
  1637. b = z->fast[a->code_buffer & ZFAST_MASK];
  1638. if (b < 0xffff) {
  1639. s = z->size[b];
  1640. a->code_buffer >>= s;
  1641. a->num_bits -= s;
  1642. return z->value[b];
  1643. }
  1644. // not resolved by fast table, so compute it the slow way
  1645. // use jpeg approach, which requires MSbits at top
  1646. k = bit_reverse(a->code_buffer, 16);
  1647. for (s=ZFAST_BITS+1; ; ++s)
  1648. if (k < z->maxcode[s])
  1649. break;
  1650. if (s == 16) return -1; // invalid code!
  1651. // code size is s, so:
  1652. b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
  1653. assert(z->size[b] == s);
  1654. a->code_buffer >>= s;
  1655. a->num_bits -= s;
  1656. return z->value[b];
  1657. }
  1658. static int expand(zbuf *z, int n) // need to make room for n bytes
  1659. {
  1660. char *q;
  1661. int cur, limit;
  1662. if (!z->z_expandable) return e("output buffer limit","Corrupt PNG");
  1663. cur = (int) (z->zout - z->zout_start);
  1664. limit = (int) (z->zout_end - z->zout_start);
  1665. while (cur + n > limit)
  1666. limit *= 2;
  1667. q = (char *) realloc(z->zout_start, limit);
  1668. if (q == NULL) return e("outofmem", "Out of memory");
  1669. z->zout_start = q;
  1670. z->zout = q + cur;
  1671. z->zout_end = q + limit;
  1672. return 1;
  1673. }
  1674. static int length_base[31] = {
  1675. 3,4,5,6,7,8,9,10,11,13,
  1676. 15,17,19,23,27,31,35,43,51,59,
  1677. 67,83,99,115,131,163,195,227,258,0,0 };
  1678. static int length_extra[31]=
  1679. { 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 };
  1680. static int dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
  1681. 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
  1682. static int dist_extra[32] =
  1683. { 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};
  1684. static int parse_huffman_block(zbuf *a)
  1685. {
  1686. for(;;) {
  1687. int z = zhuffman_decode(a, &a->z_length);
  1688. if (z < 256) {
  1689. if (z < 0) return e("bad huffman code","Corrupt PNG"); // error in huffman codes
  1690. if (a->zout >= a->zout_end) if (!expand(a, 1)) return 0;
  1691. *a->zout++ = (char) z;
  1692. } else {
  1693. uint8 *p;
  1694. int len,dist;
  1695. if (z == 256) return 1;
  1696. z -= 257;
  1697. len = length_base[z];
  1698. if (length_extra[z]) len += zreceive(a, length_extra[z]);
  1699. z = zhuffman_decode(a, &a->z_distance);
  1700. if (z < 0) return e("bad huffman code","Corrupt PNG");
  1701. dist = dist_base[z];
  1702. if (dist_extra[z]) dist += zreceive(a, dist_extra[z]);
  1703. if (a->zout - a->zout_start < dist) return e("bad dist","Corrupt PNG");
  1704. if (a->zout + len > a->zout_end) if (!expand(a, len)) return 0;
  1705. p = (uint8 *) (a->zout - dist);
  1706. while (len--)
  1707. *a->zout++ = *p++;
  1708. }
  1709. }
  1710. }
  1711. static int compute_huffman_codes(zbuf *a)
  1712. {
  1713. static uint8 length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
  1714. zhuffman z_codelength;
  1715. uint8 lencodes[286+32+137];//padding for maximum single op
  1716. uint8 codelength_sizes[19];
  1717. int i,n;
  1718. int hlit = zreceive(a,5) + 257;
  1719. int hdist = zreceive(a,5) + 1;
  1720. int hclen = zreceive(a,4) + 4;
  1721. memset(codelength_sizes, 0, sizeof(codelength_sizes));
  1722. for (i=0; i < hclen; ++i) {
  1723. int s = zreceive(a,3);
  1724. codelength_sizes[length_dezigzag[i]] = (uint8) s;
  1725. }
  1726. if (!zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
  1727. n = 0;
  1728. while (n < hlit + hdist) {
  1729. int c = zhuffman_decode(a, &z_codelength);
  1730. assert(c >= 0 && c < 19);
  1731. if (c < 16)
  1732. lencodes[n++] = (uint8) c;
  1733. else if (c == 16) {
  1734. c = zreceive(a,2)+3;
  1735. memset(lencodes+n, lencodes[n-1], c);
  1736. n += c;
  1737. } else if (c == 17) {
  1738. c = zreceive(a,3)+3;
  1739. memset(lencodes+n, 0, c);
  1740. n += c;
  1741. } else {
  1742. assert(c == 18);
  1743. c = zreceive(a,7)+11;
  1744. memset(lencodes+n, 0, c);
  1745. n += c;
  1746. }
  1747. }
  1748. if (n != hlit+hdist) return e("bad codelengths","Corrupt PNG");
  1749. if (!zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
  1750. if (!zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
  1751. return 1;
  1752. }
  1753. static int parse_uncompressed_block(zbuf *a)
  1754. {
  1755. uint8 header[4];
  1756. int len,nlen,k;
  1757. if (a->num_bits & 7)
  1758. zreceive(a, a->num_bits & 7); // discard
  1759. // drain the bit-packed data into header
  1760. k = 0;
  1761. while (a->num_bits > 0) {
  1762. header[k++] = (uint8) (a->code_buffer & 255); // wtf this warns?
  1763. a->code_buffer >>= 8;
  1764. a->num_bits -= 8;
  1765. }
  1766. assert(a->num_bits == 0);
  1767. // now fill header the normal way
  1768. while (k < 4)
  1769. header[k++] = (uint8) zget8(a);
  1770. len = header[1] * 256 + header[0];
  1771. nlen = header[3] * 256 + header[2];
  1772. if (nlen != (len ^ 0xffff)) return e("zlib corrupt","Corrupt PNG");
  1773. if (a->zbuffer + len > a->zbuffer_end) return e("read past buffer","Corrupt PNG");
  1774. if (a->zout + len > a->zout_end)
  1775. if (!expand(a, len)) return 0;
  1776. memcpy(a->zout, a->zbuffer, len);
  1777. a->zbuffer += len;
  1778. a->zout += len;
  1779. return 1;
  1780. }
  1781. static int parse_zlib_header(zbuf *a)
  1782. {
  1783. int cmf = zget8(a);
  1784. int cm = cmf & 15;
  1785. /* int cinfo = cmf >> 4; */
  1786. int flg = zget8(a);
  1787. if ((cmf*256+flg) % 31 != 0) return e("bad zlib header","Corrupt PNG"); // zlib spec
  1788. if (flg & 32) return e("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
  1789. if (cm != 8) return e("bad compression","Corrupt PNG"); // DEFLATE required for png
  1790. // window = 1 << (8 + cinfo)... but who cares, we fully buffer output
  1791. return 1;
  1792. }
  1793. // @TODO: should statically initialize these for optimal thread safety
  1794. static uint8 default_length[288], default_distance[32];
  1795. static void init_defaults(void)
  1796. {
  1797. int i; // use <= to match clearly with spec
  1798. for (i=0; i <= 143; ++i) default_length[i] = 8;
  1799. for ( ; i <= 255; ++i) default_length[i] = 9;
  1800. for ( ; i <= 279; ++i) default_length[i] = 7;
  1801. for ( ; i <= 287; ++i) default_length[i] = 8;
  1802. for (i=0; i <= 31; ++i) default_distance[i] = 5;
  1803. }
  1804. int stbi_png_partial; // a quick hack to only allow decoding some of a PNG... I should implement real streaming support instead
  1805. static int parse_zlib(zbuf *a, int parse_header)
  1806. {
  1807. int final, type;
  1808. if (parse_header)
  1809. if (!parse_zlib_header(a)) return 0;
  1810. a->num_bits = 0;
  1811. a->code_buffer = 0;
  1812. do {
  1813. final = zreceive(a,1);
  1814. type = zreceive(a,2);
  1815. if (type == 0) {
  1816. if (!parse_uncompressed_block(a)) return 0;
  1817. } else if (type == 3) {
  1818. return 0;
  1819. } else {
  1820. if (type == 1) {
  1821. // use fixed code lengths
  1822. if (!default_distance[31]) init_defaults();
  1823. if (!zbuild_huffman(&a->z_length , default_length , 288)) return 0;
  1824. if (!zbuild_huffman(&a->z_distance, default_distance, 32)) return 0;
  1825. } else {
  1826. if (!compute_huffman_codes(a)) return 0;
  1827. }
  1828. if (!parse_huffman_block(a)) return 0;
  1829. }
  1830. if (stbi_png_partial && a->zout - a->zout_start > 65536)
  1831. break;
  1832. } while (!final);
  1833. return 1;
  1834. }
  1835. static int do_zlib(zbuf *a, char *obuf, int olen, int exp, int parse_header)
  1836. {
  1837. a->zout_start = obuf;
  1838. a->zout = obuf;
  1839. a->zout_end = obuf + olen;
  1840. a->z_expandable = exp;
  1841. return parse_zlib(a, parse_header);
  1842. }
  1843. char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
  1844. {
  1845. zbuf a;
  1846. char *p = (char *) malloc(initial_size);
  1847. if (p == NULL) return NULL;
  1848. a.zbuffer = (uint8 *) buffer;
  1849. a.zbuffer_end = (uint8 *) buffer + len;
  1850. if (do_zlib(&a, p, initial_size, 1, 1)) {
  1851. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  1852. return a.zout_start;
  1853. } else {
  1854. free(a.zout_start);
  1855. return NULL;
  1856. }
  1857. }
  1858. char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
  1859. {
  1860. return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
  1861. }
  1862. char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
  1863. {
  1864. zbuf a;
  1865. char *p = (char *) malloc(initial_size);
  1866. if (p == NULL) return NULL;
  1867. a.zbuffer = (uint8 *) buffer;
  1868. a.zbuffer_end = (uint8 *) buffer + len;
  1869. if (do_zlib(&a, p, initial_size, 1, parse_header)) {
  1870. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  1871. return a.zout_start;
  1872. } else {
  1873. free(a.zout_start);
  1874. return NULL;
  1875. }
  1876. }
  1877. int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
  1878. {
  1879. zbuf a;
  1880. a.zbuffer = (uint8 *) ibuffer;
  1881. a.zbuffer_end = (uint8 *) ibuffer + ilen;
  1882. if (do_zlib(&a, obuffer, olen, 0, 1))
  1883. return (int) (a.zout - a.zout_start);
  1884. else
  1885. return -1;
  1886. }
  1887. char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
  1888. {
  1889. zbuf a;
  1890. char *p = (char *) malloc(16384);
  1891. if (p == NULL) return NULL;
  1892. a.zbuffer = (uint8 *) buffer;
  1893. a.zbuffer_end = (uint8 *) buffer+len;
  1894. if (do_zlib(&a, p, 16384, 1, 0)) {
  1895. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  1896. return a.zout_start;
  1897. } else {
  1898. free(a.zout_start);
  1899. return NULL;
  1900. }
  1901. }
  1902. int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
  1903. {
  1904. zbuf a;
  1905. a.zbuffer = (uint8 *) ibuffer;
  1906. a.zbuffer_end = (uint8 *) ibuffer + ilen;
  1907. if (do_zlib(&a, obuffer, olen, 0, 0))
  1908. return (int) (a.zout - a.zout_start);
  1909. else
  1910. return -1;
  1911. }
  1912. // public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18
  1913. // simple implementation
  1914. // - only 8-bit samples
  1915. // - no CRC checking
  1916. // - allocates lots of intermediate memory
  1917. // - avoids problem of streaming data between subsystems
  1918. // - avoids explicit window management
  1919. // performance
  1920. // - uses stb_zlib, a PD zlib implementation with fast huffman decoding
  1921. typedef struct
  1922. {
  1923. uint32 length;
  1924. uint32 type;
  1925. } chunk;
  1926. #define PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
  1927. static chunk get_chunk_header(stbi *s)
  1928. {
  1929. chunk c;
  1930. c.length = get32(s);
  1931. c.type = get32(s);
  1932. return c;
  1933. }
  1934. static int check_png_header(stbi *s)
  1935. {
  1936. static uint8 png_sig[8] = { 137,80,78,71,13,10,26,10 };
  1937. int i;
  1938. for (i=0; i < 8; ++i)
  1939. if (get8(s) != png_sig[i]) return e("bad png sig","Not a PNG");
  1940. return 1;
  1941. }
  1942. typedef struct
  1943. {
  1944. stbi s;
  1945. uint8 *idata, *expanded, *out;
  1946. } png;
  1947. enum {
  1948. F_none=0, F_sub=1, F_up=2, F_avg=3, F_paeth=4,
  1949. F_avg_first, F_paeth_first
  1950. };
  1951. static uint8 first_row_filter[5] =
  1952. {
  1953. F_none, F_sub, F_none, F_avg_first, F_paeth_first
  1954. };
  1955. static int paeth(int a, int b, int c)
  1956. {
  1957. int p = a + b - c;
  1958. int pa = abs(p-a);
  1959. int pb = abs(p-b);
  1960. int pc = abs(p-c);
  1961. if (pa <= pb && pa <= pc) return a;
  1962. if (pb <= pc) return b;
  1963. return c;
  1964. }
  1965. // create the png data from post-deflated data
  1966. static int create_png_image_raw(png *a, uint8 *raw, uint32 raw_len, int out_n, uint32 x, uint32 y)
  1967. {
  1968. stbi *s = &a->s;
  1969. uint32 i,j,stride = x*out_n;
  1970. int k;
  1971. int img_n = s->img_n; // copy it into a local for later
  1972. assert(out_n == s->img_n || out_n == s->img_n+1);
  1973. if (stbi_png_partial) y = 1;
  1974. a->out = (uint8 *) malloc(x * y * out_n);
  1975. if (!a->out) return e("outofmem", "Out of memory");
  1976. if (!stbi_png_partial) {
  1977. if (s->img_x == x && s->img_y == y) {
  1978. if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
  1979. } else { // interlaced:
  1980. if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
  1981. }
  1982. }
  1983. for (j=0; j < y; ++j) {
  1984. uint8 *cur = a->out + stride*j;
  1985. uint8 *prior = cur - stride;
  1986. int filter = *raw++;
  1987. if (filter > 4) return e("invalid filter","Corrupt PNG");
  1988. // if first row, use special filter that doesn't sample previous row
  1989. if (j == 0) filter = first_row_filter[filter];
  1990. // handle first pixel explicitly
  1991. for (k=0; k < img_n; ++k) {
  1992. switch (filter) {
  1993. case F_none : cur[k] = raw[k]; break;
  1994. case F_sub : cur[k] = raw[k]; break;
  1995. case F_up : cur[k] = raw[k] + prior[k]; break;
  1996. case F_avg : cur[k] = raw[k] + (prior[k]>>1); break;
  1997. case F_paeth : cur[k] = (uint8) (raw[k] + paeth(0,prior[k],0)); break;
  1998. case F_avg_first : cur[k] = raw[k]; break;
  1999. case F_paeth_first: cur[k] = raw[k]; break;
  2000. }
  2001. }
  2002. if (img_n != out_n) cur[img_n] = 255;
  2003. raw += img_n;
  2004. cur += out_n;
  2005. prior += out_n;
  2006. // this is a little gross, so that we don't switch per-pixel or per-component
  2007. if (img_n == out_n) {
  2008. #define CASE(f) \
  2009. case f: \
  2010. for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \
  2011. for (k=0; k < img_n; ++k)
  2012. switch (filter) {
  2013. CASE(F_none) cur[k] = raw[k]; break;
  2014. CASE(F_sub) cur[k] = raw[k] + cur[k-img_n]; break;
  2015. CASE(F_up) cur[k] = raw[k] + prior[k]; break;
  2016. CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-img_n])>>1); break;
  2017. CASE(F_paeth) cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;
  2018. CASE(F_avg_first) cur[k] = raw[k] + (cur[k-img_n] >> 1); break;
  2019. CASE(F_paeth_first) cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],0,0)); break;
  2020. }
  2021. #undef CASE
  2022. } else {
  2023. assert(img_n+1 == out_n);
  2024. #define CASE(f) \
  2025. case f: \
  2026. for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \
  2027. for (k=0; k < img_n; ++k)
  2028. switch (filter) {
  2029. CASE(F_none) cur[k] = raw[k]; break;
  2030. CASE(F_sub) cur[k] = raw[k] + cur[k-out_n]; break;
  2031. CASE(F_up) cur[k] = raw[k] + prior[k]; break;
  2032. CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-out_n])>>1); break;
  2033. CASE(F_paeth) cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;
  2034. CASE(F_avg_first) cur[k] = raw[k] + (cur[k-out_n] >> 1); break;
  2035. CASE(F_paeth_first) cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],0,0)); break;
  2036. }
  2037. #undef CASE
  2038. }
  2039. }
  2040. return 1;
  2041. }
  2042. static int create_png_image(png *a, uint8 *raw, uint32 raw_len, int out_n, int interlaced)
  2043. {
  2044. uint8 *final;
  2045. int p;
  2046. int save;
  2047. if (!interlaced)
  2048. return create_png_image_raw(a, raw, raw_len, out_n, a->s.img_x, a->s.img_y);
  2049. save = stbi_png_partial;
  2050. stbi_png_partial = 0;
  2051. // de-interlacing
  2052. final = (uint8 *) malloc(a->s.img_x * a->s.img_y * out_n);
  2053. for (p=0; p < 7; ++p) {
  2054. int xorig[] = { 0,4,0,2,0,1,0 };
  2055. int yorig[] = { 0,0,4,0,2,0,1 };
  2056. int xspc[] = { 8,8,4,4,2,2,1 };
  2057. int yspc[] = { 8,8,8,4,4,2,2 };
  2058. int i,j,x,y;
  2059. // pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
  2060. x = (a->s.img_x - xorig[p] + xspc[p]-1) / xspc[p];
  2061. y = (a->s.img_y - yorig[p] + yspc[p]-1) / yspc[p];
  2062. if (x && y) {
  2063. if (!create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
  2064. free(final);
  2065. return 0;
  2066. }
  2067. for (j=0; j < y; ++j)
  2068. for (i=0; i < x; ++i)
  2069. memcpy(final + (j*yspc[p]+yorig[p])*a->s.img_x*out_n + (i*xspc[p]+xorig[p])*out_n,
  2070. a->out + (j*x+i)*out_n, out_n);
  2071. free(a->out);
  2072. raw += (x*out_n+1)*y;
  2073. raw_len -= (x*out_n+1)*y;
  2074. }
  2075. }
  2076. a->out = final;
  2077. stbi_png_partial = save;
  2078. return 1;
  2079. }
  2080. static int compute_transparency(png *z, uint8 tc[3], int out_n)
  2081. {
  2082. stbi *s = &z->s;
  2083. uint32 i, pixel_count = s->img_x * s->img_y;
  2084. uint8 *p = z->out;
  2085. // compute color-based transparency, assuming we've
  2086. // already got 255 as the alpha value in the output
  2087. assert(out_n == 2 || out_n == 4);
  2088. if (out_n == 2) {
  2089. for (i=0; i < pixel_count; ++i) {
  2090. p[1] = (p[0] == tc[0] ? 0 : 255);
  2091. p += 2;
  2092. }
  2093. } else {
  2094. for (i=0; i < pixel_count; ++i) {
  2095. if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
  2096. p[3] = 0;
  2097. p += 4;
  2098. }
  2099. }
  2100. return 1;
  2101. }
  2102. static int expand_palette(png *a, uint8 *palette, int len, int pal_img_n)
  2103. {
  2104. uint32 i, pixel_count = a->s.img_x * a->s.img_y;
  2105. uint8 *p, *temp_out, *orig = a->out;
  2106. p = (uint8 *) malloc(pixel_count * pal_img_n);
  2107. if (p == NULL) return e("outofmem", "Out of memory");
  2108. // between here and free(out) below, exitting would leak
  2109. temp_out = p;
  2110. if (pal_img_n == 3) {
  2111. for (i=0; i < pixel_count; ++i) {
  2112. int n = orig[i]*4;
  2113. p[0] = palette[n ];
  2114. p[1] = palette[n+1];
  2115. p[2] = palette[n+2];
  2116. p += 3;
  2117. }
  2118. } else {
  2119. for (i=0; i < pixel_count; ++i) {
  2120. int n = orig[i]*4;
  2121. p[0] = palette[n ];
  2122. p[1] = palette[n+1];
  2123. p[2] = palette[n+2];
  2124. p[3] = palette[n+3];
  2125. p += 4;
  2126. }
  2127. }
  2128. free(a->out);
  2129. a->out = temp_out;
  2130. STBI_NOTUSED(len);
  2131. return 1;
  2132. }
  2133. static int stbi_unpremultiply_on_load = 0;
  2134. static int stbi_de_iphone_flag = 0;
  2135. void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
  2136. {
  2137. stbi_unpremultiply_on_load = flag_true_if_should_unpremultiply;
  2138. }
  2139. void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
  2140. {
  2141. stbi_de_iphone_flag = flag_true_if_should_convert;
  2142. }
  2143. static void stbi_de_iphone(png *z)
  2144. {
  2145. stbi *s = &z->s;
  2146. uint32 i, pixel_count = s->img_x * s->img_y;
  2147. uint8 *p = z->out;
  2148. if (s->img_out_n == 3) { // convert bgr to rgb
  2149. for (i=0; i < pixel_count; ++i) {
  2150. uint8 t = p[0];
  2151. p[0] = p[2];
  2152. p[2] = t;
  2153. p += 3;
  2154. }
  2155. } else {
  2156. assert(s->img_out_n == 4);
  2157. if (stbi_unpremultiply_on_load) {
  2158. // convert bgr to rgb and unpremultiply
  2159. for (i=0; i < pixel_count; ++i) {
  2160. uint8 a = p[3];
  2161. uint8 t = p[0];
  2162. if (a) {
  2163. p[0] = p[2] * 255 / a;
  2164. p[1] = p[1] * 255 / a;
  2165. p[2] = t * 255 / a;
  2166. } else {
  2167. p[0] = p[2];
  2168. p[2] = t;
  2169. }
  2170. p += 4;
  2171. }
  2172. } else {
  2173. // convert bgr to rgb
  2174. for (i=0; i < pixel_count; ++i) {
  2175. uint8 t = p[0];
  2176. p[0] = p[2];
  2177. p[2] = t;
  2178. p += 4;
  2179. }
  2180. }
  2181. }
  2182. }
  2183. static int parse_png_file(png *z, int scan, int req_comp)
  2184. {
  2185. uint8 palette[1024], pal_img_n=0;
  2186. uint8 has_trans=0, tc[3];
  2187. uint32 ioff=0, idata_limit=0, i, pal_len=0;
  2188. int first=1,k,interlace=0, iphone=0;
  2189. stbi *s = &z->s;
  2190. if (!check_png_header(s)) return 0;
  2191. if (scan == SCAN_type) return 1;
  2192. for (;;) {
  2193. chunk c = get_chunk_header(s);
  2194. switch (c.type) {
  2195. case PNG_TYPE('C','g','B','I'):
  2196. iphone = stbi_de_iphone_flag;
  2197. skip(s, c.length);
  2198. break;
  2199. case PNG_TYPE('I','H','D','R'): {
  2200. int depth,color,comp,filter;
  2201. if (!first) return e("multiple IHDR","Corrupt PNG");
  2202. first = 0;
  2203. if (c.length != 13) return e("bad IHDR len","Corrupt PNG");
  2204. s->img_x = get32(s); if (s->img_x > (1 << 24)) return e("too large","Very large image (corrupt?)");
  2205. s->img_y = get32(s); if (s->img_y > (1 << 24)) return e("too large","Very large image (corrupt?)");
  2206. depth = get8(s); if (depth != 8) return e("8bit only","PNG not supported: 8-bit only");
  2207. color = get8(s); if (color > 6) return e("bad ctype","Corrupt PNG");
  2208. if (color == 3) pal_img_n = 3; else if (color & 1) return e("bad ctype","Corrupt PNG");
  2209. comp = get8(s); if (comp) return e("bad comp method","Corrupt PNG");
  2210. filter= get8(s); if (filter) return e("bad filter method","Corrupt PNG");
  2211. interlace = get8(s); if (interlace>1) return e("bad interlace method","Corrupt PNG");
  2212. if (!s->img_x || !s->img_y) return e("0-pixel image","Corrupt PNG");
  2213. if (!pal_img_n) {
  2214. s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
  2215. if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode");
  2216. if (scan == SCAN_header) return 1;
  2217. } else {
  2218. // if paletted, then pal_n is our final components, and
  2219. // img_n is # components to decompress/filter.
  2220. s->img_n = 1;
  2221. if ((1 << 30) / s->img_x / 4 < s->img_y) return e("too large","Corrupt PNG");
  2222. // if SCAN_header, have to scan to see if we have a tRNS
  2223. }
  2224. break;
  2225. }
  2226. case PNG_TYPE('P','L','T','E'): {
  2227. if (first) return e("first not IHDR", "Corrupt PNG");
  2228. if (c.length > 256*3) return e("invalid PLTE","Corrupt PNG");
  2229. pal_len = c.length / 3;
  2230. if (pal_len * 3 != c.length) return e("invalid PLTE","Corrupt PNG");
  2231. for (i=0; i < pal_len; ++i) {
  2232. palette[i*4+0] = get8u(s);
  2233. palette[i*4+1] = get8u(s);
  2234. palette[i*4+2] = get8u(s);
  2235. palette[i*4+3] = 255;
  2236. }
  2237. break;
  2238. }
  2239. case PNG_TYPE('t','R','N','S'): {
  2240. if (first) return e("first not IHDR", "Corrupt PNG");
  2241. if (z->idata) return e("tRNS after IDAT","Corrupt PNG");
  2242. if (pal_img_n) {
  2243. if (scan == SCAN_header) { s->img_n = 4; return 1; }
  2244. if (pal_len == 0) return e("tRNS before PLTE","Corrupt PNG");
  2245. if (c.length > pal_len) return e("bad tRNS len","Corrupt PNG");
  2246. pal_img_n = 4;
  2247. for (i=0; i < c.length; ++i)
  2248. palette[i*4+3] = get8u(s);
  2249. } else {
  2250. if (!(s->img_n & 1)) return e("tRNS with alpha","Corrupt PNG");
  2251. if (c.length != (uint32) s->img_n*2) return e("bad tRNS len","Corrupt PNG");
  2252. has_trans = 1;
  2253. for (k=0; k < s->img_n; ++k)
  2254. tc[k] = (uint8) get16(s); // non 8-bit images will be larger
  2255. }
  2256. break;
  2257. }
  2258. case PNG_TYPE('I','D','A','T'): {
  2259. if (first) return e("first not IHDR", "Corrupt PNG");
  2260. if (pal_img_n && !pal_len) return e("no PLTE","Corrupt PNG");
  2261. if (scan == SCAN_header) { s->img_n = pal_img_n; return 1; }
  2262. if (ioff + c.length > idata_limit) {
  2263. uint8 *p;
  2264. if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
  2265. while (ioff + c.length > idata_limit)
  2266. idata_limit *= 2;
  2267. p = (uint8 *) realloc(z->idata, idata_limit); if (p == NULL) return e("outofmem", "Out of memory");
  2268. z->idata = p;
  2269. }
  2270. if (!getn(s, z->idata+ioff,c.length)) return e("outofdata","Corrupt PNG");
  2271. ioff += c.length;
  2272. break;
  2273. }
  2274. case PNG_TYPE('I','E','N','D'): {
  2275. uint32 raw_len;
  2276. if (first) return e("first not IHDR", "Corrupt PNG");
  2277. if (scan != SCAN_load) return 1;
  2278. if (z->idata == NULL) return e("no IDAT","Corrupt PNG");
  2279. z->expanded = (uint8 *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !iphone);
  2280. if (z->expanded == NULL) return 0; // zlib should set error
  2281. free(z->idata); z->idata = NULL;
  2282. if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
  2283. s->img_out_n = s->img_n+1;
  2284. else
  2285. s->img_out_n = s->img_n;
  2286. if (!create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace)) return 0;
  2287. if (has_trans)
  2288. if (!compute_transparency(z, tc, s->img_out_n)) return 0;
  2289. if (iphone && s->img_out_n > 2)
  2290. stbi_de_iphone(z);
  2291. if (pal_img_n) {
  2292. // pal_img_n == 3 or 4
  2293. s->img_n = pal_img_n; // record the actual colors we had
  2294. s->img_out_n = pal_img_n;
  2295. if (req_comp >= 3) s->img_out_n = req_comp;
  2296. if (!expand_palette(z, palette, pal_len, s->img_out_n))
  2297. return 0;
  2298. }
  2299. free(z->expanded); z->expanded = NULL;
  2300. return 1;
  2301. }
  2302. default:
  2303. // if critical, fail
  2304. if (first) return e("first not IHDR", "Corrupt PNG");
  2305. if ((c.type & (1 << 29)) == 0) {
  2306. #ifndef STBI_NO_FAILURE_STRINGS
  2307. // not threadsafe
  2308. static char invalid_chunk[] = "XXXX chunk not known";
  2309. invalid_chunk[0] = (uint8) (c.type >> 24);
  2310. invalid_chunk[1] = (uint8) (c.type >> 16);
  2311. invalid_chunk[2] = (uint8) (c.type >> 8);
  2312. invalid_chunk[3] = (uint8) (c.type >> 0);
  2313. #endif
  2314. return e(invalid_chunk, "PNG not supported: unknown chunk type");
  2315. }
  2316. skip(s, c.length);
  2317. break;
  2318. }
  2319. // end of chunk, read and skip CRC
  2320. get32(s);
  2321. }
  2322. }
  2323. static unsigned char *do_png(png *p, int *x, int *y, int *n, int req_comp)
  2324. {
  2325. unsigned char *result=NULL;
  2326. p->expanded = NULL;
  2327. p->idata = NULL;
  2328. p->out = NULL;
  2329. if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
  2330. if (parse_png_file(p, SCAN_load, req_comp)) {
  2331. result = p->out;
  2332. p->out = NULL;
  2333. if (req_comp && req_comp != p->s.img_out_n) {
  2334. result = convert_format(result, p->s.img_out_n, req_comp, p->s.img_x, p->s.img_y);
  2335. p->s.img_out_n = req_comp;
  2336. if (result == NULL) return result;
  2337. }
  2338. *x = p->s.img_x;
  2339. *y = p->s.img_y;
  2340. if (n) *n = p->s.img_n;
  2341. }
  2342. free(p->out); p->out = NULL;
  2343. free(p->expanded); p->expanded = NULL;
  2344. free(p->idata); p->idata = NULL;
  2345. return result;
  2346. }
  2347. #ifndef STBI_NO_STDIO
  2348. unsigned char *stbi_png_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  2349. {
  2350. png p;
  2351. start_file(&p.s, f);
  2352. return do_png(&p, x,y,comp,req_comp);
  2353. }
  2354. unsigned char *stbi_png_load(char const *filename, int *x, int *y, int *comp, int req_comp)
  2355. {
  2356. unsigned char *data;
  2357. FILE *f = fopen(filename, "rb");
  2358. if (!f) return NULL;
  2359. data = stbi_png_load_from_file(f,x,y,comp,req_comp);
  2360. fclose(f);
  2361. return data;
  2362. }
  2363. #endif
  2364. unsigned char *stbi_png_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  2365. {
  2366. png p;
  2367. start_mem(&p.s, buffer,len);
  2368. return do_png(&p, x,y,comp,req_comp);
  2369. }
  2370. #ifndef STBI_NO_STDIO
  2371. int stbi_png_test_file(FILE *f)
  2372. {
  2373. png p;
  2374. int n,r;
  2375. n = ftell(f);
  2376. start_file(&p.s, f);
  2377. r = parse_png_file(&p, SCAN_type,STBI_default);
  2378. fseek(f,n,SEEK_SET);
  2379. return r;
  2380. }
  2381. #endif
  2382. int stbi_png_test_memory(stbi_uc const *buffer, int len)
  2383. {
  2384. png p;
  2385. start_mem(&p.s, buffer, len);
  2386. return parse_png_file(&p, SCAN_type,STBI_default);
  2387. }
  2388. static int stbi_png_info_raw(png *p, int *x, int *y, int *comp)
  2389. {
  2390. if (!parse_png_file(p, SCAN_header, 0))
  2391. return 0;
  2392. if (x) *x = p->s.img_x;
  2393. if (y) *y = p->s.img_y;
  2394. if (comp) *comp = p->s.img_n;
  2395. return 1;
  2396. }
  2397. #ifndef STBI_NO_STDIO
  2398. int stbi_png_info (char const *filename, int *x, int *y, int *comp)
  2399. {
  2400. int res;
  2401. FILE *f = fopen(filename, "rb");
  2402. if (!f) return 0;
  2403. res = stbi_png_info_from_file(f, x, y, comp);
  2404. fclose(f);
  2405. return res;
  2406. }
  2407. int stbi_png_info_from_file(FILE *f, int *x, int *y, int *comp)
  2408. {
  2409. png p;
  2410. int res;
  2411. long n = ftell(f);
  2412. start_file(&p.s, f);
  2413. res = stbi_png_info_raw(&p, x, y, comp);
  2414. fseek(f, n, SEEK_SET);
  2415. return res;
  2416. }
  2417. #endif // !STBI_NO_STDIO
  2418. int stbi_png_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  2419. {
  2420. png p;
  2421. start_mem(&p.s, buffer, len);
  2422. return stbi_png_info_raw(&p, x, y, comp);
  2423. }
  2424. // Microsoft/Windows BMP image
  2425. static int bmp_test(stbi *s)
  2426. {
  2427. int sz;
  2428. if (get8(s) != 'B') return 0;
  2429. if (get8(s) != 'M') return 0;
  2430. get32le(s); // discard filesize
  2431. get16le(s); // discard reserved
  2432. get16le(s); // discard reserved
  2433. get32le(s); // discard data offset
  2434. sz = get32le(s);
  2435. if (sz == 12 || sz == 40 || sz == 56 || sz == 108) return 1;
  2436. return 0;
  2437. }
  2438. #ifndef STBI_NO_STDIO
  2439. int stbi_bmp_test_file (FILE *f)
  2440. {
  2441. stbi s;
  2442. int r,n = ftell(f);
  2443. start_file(&s,f);
  2444. r = bmp_test(&s);
  2445. fseek(f,n,SEEK_SET);
  2446. return r;
  2447. }
  2448. #endif
  2449. int stbi_bmp_test_memory (stbi_uc const *buffer, int len)
  2450. {
  2451. stbi s;
  2452. start_mem(&s, buffer, len);
  2453. return bmp_test(&s);
  2454. }
  2455. // returns 0..31 for the highest set bit
  2456. static int high_bit(unsigned int z)
  2457. {
  2458. int n=0;
  2459. if (z == 0) return -1;
  2460. if (z >= 0x10000) n += 16, z >>= 16;
  2461. if (z >= 0x00100) n += 8, z >>= 8;
  2462. if (z >= 0x00010) n += 4, z >>= 4;
  2463. if (z >= 0x00004) n += 2, z >>= 2;
  2464. if (z >= 0x00002) n += 1, z >>= 1;
  2465. return n;
  2466. }
  2467. static int bitcount(unsigned int a)
  2468. {
  2469. a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2
  2470. a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4
  2471. a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
  2472. a = (a + (a >> 8)); // max 16 per 8 bits
  2473. a = (a + (a >> 16)); // max 32 per 8 bits
  2474. return a & 0xff;
  2475. }
  2476. static int shiftsigned(int v, int shift, int bits)
  2477. {
  2478. int result;
  2479. int z=0;
  2480. if (shift < 0) v <<= -shift;
  2481. else v >>= shift;
  2482. result = v;
  2483. z = bits;
  2484. while (z < 8) {
  2485. result += v >> z;
  2486. z += bits;
  2487. }
  2488. return result;
  2489. }
  2490. static stbi_uc *bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp)
  2491. {
  2492. uint8 *out;
  2493. unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
  2494. stbi_uc pal[256][4];
  2495. int psize=0,i,j,compress=0,width;
  2496. int bpp, flip_vertically, pad, target, offset, hsz;
  2497. if (get8(s) != 'B' || get8(s) != 'M') return epuc("not BMP", "Corrupt BMP");
  2498. get32le(s); // discard filesize
  2499. get16le(s); // discard reserved
  2500. get16le(s); // discard reserved
  2501. offset = get32le(s);
  2502. hsz = get32le(s);
  2503. if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) return epuc("unknown BMP", "BMP type not supported: unknown");
  2504. if (hsz == 12) {
  2505. s->img_x = get16le(s);
  2506. s->img_y = get16le(s);
  2507. } else {
  2508. s->img_x = get32le(s);
  2509. s->img_y = get32le(s);
  2510. }
  2511. if (get16le(s) != 1) return epuc("bad BMP", "bad BMP");
  2512. bpp = get16le(s);
  2513. if (bpp == 1) return epuc("monochrome", "BMP type not supported: 1-bit");
  2514. flip_vertically = ((int) s->img_y) > 0;
  2515. s->img_y = abs((int) s->img_y);
  2516. if (hsz == 12) {
  2517. if (bpp < 24)
  2518. psize = (offset - 14 - 24) / 3;
  2519. } else {
  2520. compress = get32le(s);
  2521. if (compress == 1 || compress == 2) return epuc("BMP RLE", "BMP type not supported: RLE");
  2522. get32le(s); // discard sizeof
  2523. get32le(s); // discard hres
  2524. get32le(s); // discard vres
  2525. get32le(s); // discard colorsused
  2526. get32le(s); // discard max important
  2527. if (hsz == 40 || hsz == 56) {
  2528. if (hsz == 56) {
  2529. get32le(s);
  2530. get32le(s);
  2531. get32le(s);
  2532. get32le(s);
  2533. }
  2534. if (bpp == 16 || bpp == 32) {
  2535. mr = mg = mb = 0;
  2536. if (compress == 0) {
  2537. if (bpp == 32) {
  2538. mr = 0xffu << 16;
  2539. mg = 0xffu << 8;
  2540. mb = 0xffu << 0;
  2541. ma = 0xffu << 24;
  2542. fake_a = 1; // @TODO: check for cases like alpha value is all 0 and switch it to 255
  2543. } else {
  2544. mr = 31u << 10;
  2545. mg = 31u << 5;
  2546. mb = 31u << 0;
  2547. }
  2548. } else if (compress == 3) {
  2549. mr = get32le(s);
  2550. mg = get32le(s);
  2551. mb = get32le(s);
  2552. // not documented, but generated by photoshop and handled by mspaint
  2553. if (mr == mg && mg == mb) {
  2554. // ?!?!?
  2555. return epuc("bad BMP", "bad BMP");
  2556. }
  2557. } else
  2558. return epuc("bad BMP", "bad BMP");
  2559. }
  2560. } else {
  2561. assert(hsz == 108);
  2562. mr = get32le(s);
  2563. mg = get32le(s);
  2564. mb = get32le(s);
  2565. ma = get32le(s);
  2566. get32le(s); // discard color space
  2567. for (i=0; i < 12; ++i)
  2568. get32le(s); // discard color space parameters
  2569. }
  2570. if (bpp < 16)
  2571. psize = (offset - 14 - hsz) >> 2;
  2572. }
  2573. s->img_n = ma ? 4 : 3;
  2574. if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
  2575. target = req_comp;
  2576. else
  2577. target = s->img_n; // if they want monochrome, we'll post-convert
  2578. out = (stbi_uc *) malloc(target * s->img_x * s->img_y);
  2579. if (!out) return epuc("outofmem", "Out of memory");
  2580. if (bpp < 16) {
  2581. int z=0;
  2582. if (psize == 0 || psize > 256) { free(out); return epuc("invalid", "Corrupt BMP"); }
  2583. for (i=0; i < psize; ++i) {
  2584. pal[i][2] = get8u(s);
  2585. pal[i][1] = get8u(s);
  2586. pal[i][0] = get8u(s);
  2587. if (hsz != 12) get8(s);
  2588. pal[i][3] = 255;
  2589. }
  2590. skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
  2591. if (bpp == 4) width = (s->img_x + 1) >> 1;
  2592. else if (bpp == 8) width = s->img_x;
  2593. else { free(out); return epuc("bad bpp", "Corrupt BMP"); }
  2594. pad = (-width)&3;
  2595. for (j=0; j < (int) s->img_y; ++j) {
  2596. for (i=0; i < (int) s->img_x; i += 2) {
  2597. int v=get8(s),v2=0;
  2598. if (bpp == 4) {
  2599. v2 = v & 15;
  2600. v >>= 4;
  2601. }
  2602. out[z++] = pal[v][0];
  2603. out[z++] = pal[v][1];
  2604. out[z++] = pal[v][2];
  2605. if (target == 4) out[z++] = 255;
  2606. if (i+1 == (int) s->img_x) break;
  2607. v = (bpp == 8) ? get8(s) : v2;
  2608. out[z++] = pal[v][0];
  2609. out[z++] = pal[v][1];
  2610. out[z++] = pal[v][2];
  2611. if (target == 4) out[z++] = 255;
  2612. }
  2613. skip(s, pad);
  2614. }
  2615. } else {
  2616. int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
  2617. int z = 0;
  2618. int easy=0;
  2619. skip(s, offset - 14 - hsz);
  2620. if (bpp == 24) width = 3 * s->img_x;
  2621. else if (bpp == 16) width = 2*s->img_x;
  2622. else /* bpp = 32 and pad = 0 */ width=0;
  2623. pad = (-width) & 3;
  2624. if (bpp == 24) {
  2625. easy = 1;
  2626. } else if (bpp == 32) {
  2627. if (mb == 0xff && mg == 0xff00 && mr == 0xff000000 && ma == 0xff000000)
  2628. easy = 2;
  2629. }
  2630. if (!easy) {
  2631. if (!mr || !mg || !mb) return epuc("bad masks", "Corrupt BMP");
  2632. // right shift amt to put high bit in position #7
  2633. rshift = high_bit(mr)-7; rcount = bitcount(mr);
  2634. gshift = high_bit(mg)-7; gcount = bitcount(mr);
  2635. bshift = high_bit(mb)-7; bcount = bitcount(mr);
  2636. ashift = high_bit(ma)-7; acount = bitcount(mr);
  2637. }
  2638. for (j=0; j < (int) s->img_y; ++j) {
  2639. if (easy) {
  2640. for (i=0; i < (int) s->img_x; ++i) {
  2641. int a;
  2642. out[z+2] = get8u(s);
  2643. out[z+1] = get8u(s);
  2644. out[z+0] = get8u(s);
  2645. z += 3;
  2646. a = (easy == 2 ? get8(s) : 255);
  2647. if (target == 4) out[z++] = (uint8) a;
  2648. }
  2649. } else {
  2650. for (i=0; i < (int) s->img_x; ++i) {
  2651. uint32 v = (bpp == 16 ? get16le(s) : get32le(s));
  2652. int a;
  2653. out[z++] = (uint8) shiftsigned(v & mr, rshift, rcount);
  2654. out[z++] = (uint8) shiftsigned(v & mg, gshift, gcount);
  2655. out[z++] = (uint8) shiftsigned(v & mb, bshift, bcount);
  2656. a = (ma ? shiftsigned(v & ma, ashift, acount) : 255);
  2657. if (target == 4) out[z++] = (uint8) a;
  2658. }
  2659. }
  2660. skip(s, pad);
  2661. }
  2662. }
  2663. if (flip_vertically) {
  2664. stbi_uc t;
  2665. for (j=0; j < (int) s->img_y>>1; ++j) {
  2666. stbi_uc *p1 = out + j *s->img_x*target;
  2667. stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
  2668. for (i=0; i < (int) s->img_x*target; ++i) {
  2669. t = p1[i], p1[i] = p2[i], p2[i] = t;
  2670. }
  2671. }
  2672. }
  2673. if (req_comp && req_comp != target) {
  2674. out = convert_format(out, target, req_comp, s->img_x, s->img_y);
  2675. if (out == NULL) return out; // convert_format frees input on failure
  2676. }
  2677. *x = s->img_x;
  2678. *y = s->img_y;
  2679. if (comp) *comp = target;
  2680. return out;
  2681. }
  2682. #ifndef STBI_NO_STDIO
  2683. stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp)
  2684. {
  2685. stbi_uc *data;
  2686. FILE *f = fopen(filename, "rb");
  2687. if (!f) return NULL;
  2688. data = stbi_bmp_load_from_file(f, x,y,comp,req_comp);
  2689. fclose(f);
  2690. return data;
  2691. }
  2692. stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
  2693. {
  2694. stbi s;
  2695. start_file(&s, f);
  2696. return bmp_load(&s, x,y,comp,req_comp);
  2697. }
  2698. #endif
  2699. stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  2700. {
  2701. stbi s;
  2702. start_mem(&s, buffer, len);
  2703. return bmp_load(&s, x,y,comp,req_comp);
  2704. }
  2705. // Targa Truevision - TGA
  2706. // by Jonathan Dummer
  2707. static int tga_info(stbi *s, int *x, int *y, int *comp)
  2708. {
  2709. int tga_w, tga_h, tga_comp;
  2710. int sz;
  2711. get8u(s); // discard Offset
  2712. sz = get8u(s); // color type
  2713. if( sz > 1 ) return 0; // only RGB or indexed allowed
  2714. sz = get8u(s); // image type
  2715. // only RGB or grey allowed, +/- RLE
  2716. if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11)) return 0;
  2717. get16le(s); // discard palette start
  2718. get16le(s); // discard palette length
  2719. get8(s); // discard bits per palette color entry
  2720. get16le(s); // discard x origin
  2721. get16le(s); // discard y origin
  2722. tga_w = get16le(s);
  2723. if( tga_w < 1 ) return 0; // test width
  2724. tga_h = get16le(s);
  2725. if( tga_h < 1 ) return 0; // test height
  2726. sz = get8(s); // bits per pixel
  2727. // only RGB or RGBA or grey allowed
  2728. if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) return 0;
  2729. tga_comp = sz;
  2730. if (x) *x = tga_w;
  2731. if (y) *y = tga_h;
  2732. if (comp) *comp = tga_comp / 8;
  2733. return 1; // seems to have passed everything
  2734. }
  2735. #ifndef STBI_NO_STDIO
  2736. int stbi_tga_info_from_file(FILE *f, int *x, int *y, int *comp)
  2737. {
  2738. stbi s;
  2739. int r;
  2740. long n = ftell(f);
  2741. start_file(&s, f);
  2742. r = tga_info(&s, x, y, comp);
  2743. fseek(f, n, SEEK_SET);
  2744. return r;
  2745. }
  2746. #endif
  2747. int stbi_tga_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  2748. {
  2749. stbi s;
  2750. start_mem(&s, buffer, len);
  2751. return tga_info(&s, x, y, comp);
  2752. }
  2753. static int tga_test(stbi *s)
  2754. {
  2755. int sz;
  2756. get8u(s); // discard Offset
  2757. sz = get8u(s); // color type
  2758. if ( sz > 1 ) return 0; // only RGB or indexed allowed
  2759. sz = get8u(s); // image type
  2760. if ( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) ) return 0; // only RGB or grey allowed, +/- RLE
  2761. get16(s); // discard palette start
  2762. get16(s); // discard palette length
  2763. get8(s); // discard bits per palette color entry
  2764. get16(s); // discard x origin
  2765. get16(s); // discard y origin
  2766. if ( get16(s) < 1 ) return 0; // test width
  2767. if ( get16(s) < 1 ) return 0; // test height
  2768. sz = get8(s); // bits per pixel
  2769. if ( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) ) return 0; // only RGB or RGBA or grey allowed
  2770. return 1; // seems to have passed everything
  2771. }
  2772. #ifndef STBI_NO_STDIO
  2773. int stbi_tga_test_file (FILE *f)
  2774. {
  2775. stbi s;
  2776. int r,n = ftell(f);
  2777. start_file(&s, f);
  2778. r = tga_test(&s);
  2779. fseek(f,n,SEEK_SET);
  2780. return r;
  2781. }
  2782. #endif
  2783. int stbi_tga_test_memory (stbi_uc const *buffer, int len)
  2784. {
  2785. stbi s;
  2786. start_mem(&s, buffer, len);
  2787. return tga_test(&s);
  2788. }
  2789. static stbi_uc *tga_load(stbi *s, int *x, int *y, int *comp, int req_comp)
  2790. {
  2791. // read in the TGA header stuff
  2792. int tga_offset = get8u(s);
  2793. int tga_indexed = get8u(s);
  2794. int tga_image_type = get8u(s);
  2795. int tga_is_RLE = 0;
  2796. int tga_palette_start = get16le(s);
  2797. int tga_palette_len = get16le(s);
  2798. int tga_palette_bits = get8u(s);
  2799. int tga_x_origin = get16le(s);
  2800. int tga_y_origin = get16le(s);
  2801. int tga_width = get16le(s);
  2802. int tga_height = get16le(s);
  2803. int tga_bits_per_pixel = get8u(s);
  2804. int tga_inverted = get8u(s);
  2805. // image data
  2806. unsigned char *tga_data;
  2807. unsigned char *tga_palette = NULL;
  2808. int i, j;
  2809. unsigned char raw_data[4];
  2810. unsigned char trans_data[4];
  2811. int RLE_count = 0;
  2812. int RLE_repeating = 0;
  2813. int read_next_pixel = 1;
  2814. // do a tiny bit of precessing
  2815. if ( tga_image_type >= 8 )
  2816. {
  2817. tga_image_type -= 8;
  2818. tga_is_RLE = 1;
  2819. }
  2820. /* int tga_alpha_bits = tga_inverted & 15; */
  2821. tga_inverted = 1 - ((tga_inverted >> 5) & 1);
  2822. // error check
  2823. if ( //(tga_indexed) ||
  2824. (tga_width < 1) || (tga_height < 1) ||
  2825. (tga_image_type < 1) || (tga_image_type > 3) ||
  2826. ((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
  2827. (tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
  2828. )
  2829. {
  2830. return NULL;
  2831. }
  2832. // If I'm paletted, then I'll use the number of bits from the palette
  2833. if ( tga_indexed )
  2834. {
  2835. tga_bits_per_pixel = tga_palette_bits;
  2836. }
  2837. // tga info
  2838. *x = tga_width;
  2839. *y = tga_height;
  2840. if ( (req_comp < 1) || (req_comp > 4) )
  2841. {
  2842. // just use whatever the file was
  2843. req_comp = tga_bits_per_pixel / 8;
  2844. *comp = req_comp;
  2845. } else
  2846. {
  2847. // force a new number of components
  2848. *comp = tga_bits_per_pixel/8;
  2849. }
  2850. tga_data = (unsigned char*)malloc( tga_width * tga_height * req_comp );
  2851. // skip to the data's starting position (offset usually = 0)
  2852. skip(s, tga_offset );
  2853. // do I need to load a palette?
  2854. if ( tga_indexed )
  2855. {
  2856. // any data to skip? (offset usually = 0)
  2857. skip(s, tga_palette_start );
  2858. // load the palette
  2859. tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );
  2860. if (!getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 ))
  2861. return NULL;
  2862. }
  2863. // load the data
  2864. trans_data[0] = trans_data[1] = trans_data[2] = trans_data[3] = 0;
  2865. for (i=0; i < tga_width * tga_height; ++i)
  2866. {
  2867. // if I'm in RLE mode, do I need to get a RLE chunk?
  2868. if ( tga_is_RLE )
  2869. {
  2870. if ( RLE_count == 0 )
  2871. {
  2872. // yep, get the next byte as a RLE command
  2873. int RLE_cmd = get8u(s);
  2874. RLE_count = 1 + (RLE_cmd & 127);
  2875. RLE_repeating = RLE_cmd >> 7;
  2876. read_next_pixel = 1;
  2877. } else if ( !RLE_repeating )
  2878. {
  2879. read_next_pixel = 1;
  2880. }
  2881. } else
  2882. {
  2883. read_next_pixel = 1;
  2884. }
  2885. // OK, if I need to read a pixel, do it now
  2886. if ( read_next_pixel )
  2887. {
  2888. // load however much data we did have
  2889. if ( tga_indexed )
  2890. {
  2891. // read in 1 byte, then perform the lookup
  2892. int pal_idx = get8u(s);
  2893. if ( pal_idx >= tga_palette_len )
  2894. {
  2895. // invalid index
  2896. pal_idx = 0;
  2897. }
  2898. pal_idx *= tga_bits_per_pixel / 8;
  2899. for (j = 0; j*8 < tga_bits_per_pixel; ++j)
  2900. {
  2901. raw_data[j] = tga_palette[pal_idx+j];
  2902. }
  2903. } else
  2904. {
  2905. // read in the data raw
  2906. for (j = 0; j*8 < tga_bits_per_pixel; ++j)
  2907. {
  2908. raw_data[j] = get8u(s);
  2909. }
  2910. }
  2911. // convert raw to the intermediate format
  2912. switch (tga_bits_per_pixel)
  2913. {
  2914. case 8:
  2915. // Luminous => RGBA
  2916. trans_data[0] = raw_data[0];
  2917. trans_data[1] = raw_data[0];
  2918. trans_data[2] = raw_data[0];
  2919. trans_data[3] = 255;
  2920. break;
  2921. case 16:
  2922. // Luminous,Alpha => RGBA
  2923. trans_data[0] = raw_data[0];
  2924. trans_data[1] = raw_data[0];
  2925. trans_data[2] = raw_data[0];
  2926. trans_data[3] = raw_data[1];
  2927. break;
  2928. case 24:
  2929. // BGR => RGBA
  2930. trans_data[0] = raw_data[2];
  2931. trans_data[1] = raw_data[1];
  2932. trans_data[2] = raw_data[0];
  2933. trans_data[3] = 255;
  2934. break;
  2935. case 32:
  2936. // BGRA => RGBA
  2937. trans_data[0] = raw_data[2];
  2938. trans_data[1] = raw_data[1];
  2939. trans_data[2] = raw_data[0];
  2940. trans_data[3] = raw_data[3];
  2941. break;
  2942. }
  2943. // clear the reading flag for the next pixel
  2944. read_next_pixel = 0;
  2945. } // end of reading a pixel
  2946. // convert to final format
  2947. switch (req_comp)
  2948. {
  2949. case 1:
  2950. // RGBA => Luminance
  2951. tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);
  2952. break;
  2953. case 2:
  2954. // RGBA => Luminance,Alpha
  2955. tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);
  2956. tga_data[i*req_comp+1] = trans_data[3];
  2957. break;
  2958. case 3:
  2959. // RGBA => RGB
  2960. tga_data[i*req_comp+0] = trans_data[0];
  2961. tga_data[i*req_comp+1] = trans_data[1];
  2962. tga_data[i*req_comp+2] = trans_data[2];
  2963. break;
  2964. case 4:
  2965. // RGBA => RGBA
  2966. tga_data[i*req_comp+0] = trans_data[0];
  2967. tga_data[i*req_comp+1] = trans_data[1];
  2968. tga_data[i*req_comp+2] = trans_data[2];
  2969. tga_data[i*req_comp+3] = trans_data[3];
  2970. break;
  2971. }
  2972. // in case we're in RLE mode, keep counting down
  2973. --RLE_count;
  2974. }
  2975. // do I need to invert the image?
  2976. if ( tga_inverted )
  2977. {
  2978. for (j = 0; j*2 < tga_height; ++j)
  2979. {
  2980. int index1 = j * tga_width * req_comp;
  2981. int index2 = (tga_height - 1 - j) * tga_width * req_comp;
  2982. for (i = tga_width * req_comp; i > 0; --i)
  2983. {
  2984. unsigned char temp = tga_data[index1];
  2985. tga_data[index1] = tga_data[index2];
  2986. tga_data[index2] = temp;
  2987. ++index1;
  2988. ++index2;
  2989. }
  2990. }
  2991. }
  2992. // clear my palette, if I had one
  2993. if ( tga_palette != NULL )
  2994. {
  2995. free( tga_palette );
  2996. }
  2997. // the things I do to get rid of an error message, and yet keep
  2998. // Microsoft's C compilers happy... [8^(
  2999. tga_palette_start = tga_palette_len = tga_palette_bits =
  3000. tga_x_origin = tga_y_origin = 0;
  3001. // OK, done
  3002. return tga_data;
  3003. }
  3004. #ifndef STBI_NO_STDIO
  3005. stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp)
  3006. {
  3007. stbi_uc *data;
  3008. FILE *f = fopen(filename, "rb");
  3009. if (!f) return NULL;
  3010. data = stbi_tga_load_from_file(f, x,y,comp,req_comp);
  3011. fclose(f);
  3012. return data;
  3013. }
  3014. stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
  3015. {
  3016. stbi s;
  3017. start_file(&s, f);
  3018. return tga_load(&s, x,y,comp,req_comp);
  3019. }
  3020. #endif
  3021. stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  3022. {
  3023. stbi s;
  3024. start_mem(&s, buffer, len);
  3025. return tga_load(&s, x,y,comp,req_comp);
  3026. }
  3027. // *************************************************************************************************
  3028. // Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
  3029. static int psd_test(stbi *s)
  3030. {
  3031. if (get32(s) != 0x38425053) return 0; // "8BPS"
  3032. else return 1;
  3033. }
  3034. #ifndef STBI_NO_STDIO
  3035. int stbi_psd_test_file(FILE *f)
  3036. {
  3037. stbi s;
  3038. int r,n = ftell(f);
  3039. start_file(&s, f);
  3040. r = psd_test(&s);
  3041. fseek(f,n,SEEK_SET);
  3042. return r;
  3043. }
  3044. #endif
  3045. int stbi_psd_test_memory(stbi_uc const *buffer, int len)
  3046. {
  3047. stbi s;
  3048. start_mem(&s, buffer, len);
  3049. return psd_test(&s);
  3050. }
  3051. static stbi_uc *psd_load(stbi *s, int *x, int *y, int *comp, int req_comp)
  3052. {
  3053. int pixelCount;
  3054. int channelCount, compression;
  3055. int channel, i, count, len;
  3056. int w,h;
  3057. uint8 *out;
  3058. // Check identifier
  3059. if (get32(s) != 0x38425053) // "8BPS"
  3060. return epuc("not PSD", "Corrupt PSD image");
  3061. // Check file type version.
  3062. if (get16(s) != 1)
  3063. return epuc("wrong version", "Unsupported version of PSD image");
  3064. // Skip 6 reserved bytes.
  3065. skip(s, 6 );
  3066. // Read the number of channels (R, G, B, A, etc).
  3067. channelCount = get16(s);
  3068. if (channelCount < 0 || channelCount > 16)
  3069. return epuc("wrong channel count", "Unsupported number of channels in PSD image");
  3070. // Read the rows and columns of the image.
  3071. h = get32(s);
  3072. w = get32(s);
  3073. // Make sure the depth is 8 bits.
  3074. if (get16(s) != 8)
  3075. return epuc("unsupported bit depth", "PSD bit depth is not 8 bit");
  3076. // Make sure the color mode is RGB.
  3077. // Valid options are:
  3078. // 0: Bitmap
  3079. // 1: Grayscale
  3080. // 2: Indexed color
  3081. // 3: RGB color
  3082. // 4: CMYK color
  3083. // 7: Multichannel
  3084. // 8: Duotone
  3085. // 9: Lab color
  3086. if (get16(s) != 3)
  3087. return epuc("wrong color format", "PSD is not in RGB color format");
  3088. // Skip the Mode Data. (It's the palette for indexed color; other info for other modes.)
  3089. skip(s,get32(s) );
  3090. // Skip the image resources. (resolution, pen tool paths, etc)
  3091. skip(s, get32(s) );
  3092. // Skip the reserved data.
  3093. skip(s, get32(s) );
  3094. // Find out if the data is compressed.
  3095. // Known values:
  3096. // 0: no compression
  3097. // 1: RLE compressed
  3098. compression = get16(s);
  3099. if (compression > 1)
  3100. return epuc("bad compression", "PSD has an unknown compression format");
  3101. // Create the destination image.
  3102. out = (stbi_uc *) malloc(4 * w*h);
  3103. if (!out) return epuc("outofmem", "Out of memory");
  3104. pixelCount = w*h;
  3105. // Initialize the data to zero.
  3106. //memset( out, 0, pixelCount * 4 );
  3107. // Finally, the image data.
  3108. if (compression) {
  3109. // RLE as used by .PSD and .TIFF
  3110. // Loop until you get the number of unpacked bytes you are expecting:
  3111. // Read the next source byte into n.
  3112. // If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
  3113. // Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
  3114. // Else if n is 128, noop.
  3115. // Endloop
  3116. // The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
  3117. // which we're going to just skip.
  3118. skip(s, h * channelCount * 2 );
  3119. // Read the RLE data by channel.
  3120. for (channel = 0; channel < 4; channel++) {
  3121. uint8 *p;
  3122. p = out+channel;
  3123. if (channel >= channelCount) {
  3124. // Fill this channel with default data.
  3125. for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;
  3126. } else {
  3127. // Read the RLE data.
  3128. count = 0;
  3129. while (count < pixelCount) {
  3130. len = get8(s);
  3131. if (len == 128) {
  3132. // No-op.
  3133. } else if (len < 128) {
  3134. // Copy next len+1 bytes literally.
  3135. len++;
  3136. count += len;
  3137. while (len) {
  3138. *p = get8u(s);
  3139. p += 4;
  3140. len--;
  3141. }
  3142. } else if (len > 128) {
  3143. uint8 val;
  3144. // Next -len+1 bytes in the dest are replicated from next source byte.
  3145. // (Interpret len as a negative 8-bit int.)
  3146. len ^= 0x0FF;
  3147. len += 2;
  3148. val = get8u(s);
  3149. count += len;
  3150. while (len) {
  3151. *p = val;
  3152. p += 4;
  3153. len--;
  3154. }
  3155. }
  3156. }
  3157. }
  3158. }
  3159. } else {
  3160. // We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
  3161. // where each channel consists of an 8-bit value for each pixel in the image.
  3162. // Read the data by channel.
  3163. for (channel = 0; channel < 4; channel++) {
  3164. uint8 *p;
  3165. p = out + channel;
  3166. if (channel > channelCount) {
  3167. // Fill this channel with default data.
  3168. for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;
  3169. } else {
  3170. // Read the data.
  3171. for (i = 0; i < pixelCount; i++)
  3172. *p = get8u(s), p += 4;
  3173. }
  3174. }
  3175. }
  3176. if (req_comp && req_comp != 4) {
  3177. out = convert_format(out, 4, req_comp, w, h);
  3178. if (out == NULL) return out; // convert_format frees input on failure
  3179. }
  3180. if (comp) *comp = channelCount;
  3181. *y = h;
  3182. *x = w;
  3183. return out;
  3184. }
  3185. #ifndef STBI_NO_STDIO
  3186. stbi_uc *stbi_psd_load(char const *filename, int *x, int *y, int *comp, int req_comp)
  3187. {
  3188. stbi_uc *data;
  3189. FILE *f = fopen(filename, "rb");
  3190. if (!f) return NULL;
  3191. data = stbi_psd_load_from_file(f, x,y,comp,req_comp);
  3192. fclose(f);
  3193. return data;
  3194. }
  3195. stbi_uc *stbi_psd_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  3196. {
  3197. stbi s;
  3198. start_file(&s, f);
  3199. return psd_load(&s, x,y,comp,req_comp);
  3200. }
  3201. #endif
  3202. stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  3203. {
  3204. stbi s;
  3205. start_mem(&s, buffer, len);
  3206. return psd_load(&s, x,y,comp,req_comp);
  3207. }
  3208. // *************************************************************************************************
  3209. // Softimage PIC loader
  3210. // by Tom Seddon
  3211. //
  3212. // See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
  3213. // See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
  3214. static int pic_is4(stbi *s,const char *str)
  3215. {
  3216. int i;
  3217. for (i=0; i<4; ++i)
  3218. if (get8(s) != (stbi_uc)str[i])
  3219. return 0;
  3220. return 1;
  3221. }
  3222. static int pic_test(stbi *s)
  3223. {
  3224. int i;
  3225. if (!pic_is4(s,"\x53\x80\xF6\x34"))
  3226. return 0;
  3227. for(i=0;i<84;++i)
  3228. get8(s);
  3229. if (!pic_is4(s,"PICT"))
  3230. return 0;
  3231. return 1;
  3232. }
  3233. typedef struct
  3234. {
  3235. stbi_uc size,type,channel;
  3236. } pic_packet_t;
  3237. static stbi_uc *pic_readval(stbi *s, int channel, stbi_uc *dest)
  3238. {
  3239. int mask=0x80, i;
  3240. for (i=0; i<4; ++i, mask>>=1) {
  3241. if (channel & mask) {
  3242. if (at_eof(s)) return epuc("bad file","PIC file too short");
  3243. dest[i]=get8u(s);
  3244. }
  3245. }
  3246. return dest;
  3247. }
  3248. static void pic_copyval(int channel,stbi_uc *dest,const stbi_uc *src)
  3249. {
  3250. int mask=0x80,i;
  3251. for (i=0;i<4; ++i, mask>>=1)
  3252. if (channel&mask)
  3253. dest[i]=src[i];
  3254. }
  3255. static stbi_uc *pic_load2(stbi *s,int width,int height,int *comp, stbi_uc *result)
  3256. {
  3257. int act_comp=0,num_packets=0,y,chained;
  3258. pic_packet_t packets[10];
  3259. // this will (should...) cater for even some bizarre stuff like having data
  3260. // for the same channel in multiple packets.
  3261. do {
  3262. pic_packet_t *packet;
  3263. if (num_packets==sizeof(packets)/sizeof(packets[0]))
  3264. return epuc("bad format","too many packets");
  3265. packet = &packets[num_packets++];
  3266. chained = get8(s);
  3267. packet->size = get8u(s);
  3268. packet->type = get8u(s);
  3269. packet->channel = get8u(s);
  3270. act_comp |= packet->channel;
  3271. if (at_eof(s)) return epuc("bad file","file too short (reading packets)");
  3272. if (packet->size != 8) return epuc("bad format","packet isn't 8bpp");
  3273. } while (chained);
  3274. *comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
  3275. for(y=0; y<height; ++y) {
  3276. int packet_idx;
  3277. for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
  3278. pic_packet_t *packet = &packets[packet_idx];
  3279. stbi_uc *dest = result+y*width*4;
  3280. switch (packet->type) {
  3281. default:
  3282. return epuc("bad format","packet has bad compression type");
  3283. case 0: {//uncompressed
  3284. int x;
  3285. for(x=0;x<width;++x, dest+=4)
  3286. if (!pic_readval(s,packet->channel,dest))
  3287. return 0;
  3288. break;
  3289. }
  3290. case 1://Pure RLE
  3291. {
  3292. int left=width, i;
  3293. while (left>0) {
  3294. stbi_uc count,value[4];
  3295. count=get8u(s);
  3296. if (at_eof(s)) return epuc("bad file","file too short (pure read count)");
  3297. if (count > left)
  3298. count = (uint8) left;
  3299. if (!pic_readval(s,packet->channel,value)) return 0;
  3300. for(i=0; i<count; ++i,dest+=4)
  3301. pic_copyval(packet->channel,dest,value);
  3302. left -= count;
  3303. }
  3304. }
  3305. break;
  3306. case 2: {//Mixed RLE
  3307. int left=width;
  3308. while (left>0) {
  3309. int count = get8(s), i;
  3310. if (at_eof(s)) return epuc("bad file","file too short (mixed read count)");
  3311. if (count >= 128) { // Repeated
  3312. stbi_uc value[4];
  3313. int i;
  3314. if (count==128)
  3315. count = get16(s);
  3316. else
  3317. count -= 127;
  3318. if (count > left)
  3319. return epuc("bad file","scanline overrun");
  3320. if (!pic_readval(s,packet->channel,value))
  3321. return 0;
  3322. for(i=0;i<count;++i, dest += 4)
  3323. pic_copyval(packet->channel,dest,value);
  3324. } else { // Raw
  3325. ++count;
  3326. if (count>left) return epuc("bad file","scanline overrun");
  3327. for(i=0;i<count;++i, dest+=4)
  3328. if (!pic_readval(s,packet->channel,dest))
  3329. return 0;
  3330. }
  3331. left-=count;
  3332. }
  3333. break;
  3334. }
  3335. }
  3336. }
  3337. }
  3338. return result;
  3339. }
  3340. static stbi_uc *pic_load(stbi *s,int *px,int *py,int *comp,int req_comp)
  3341. {
  3342. stbi_uc *result;
  3343. int i, x,y;
  3344. for (i=0; i<92; ++i)
  3345. get8(s);
  3346. x = get16(s);
  3347. y = get16(s);
  3348. if (at_eof(s)) return epuc("bad file","file too short (pic header)");
  3349. if ((1 << 28) / x < y) return epuc("too large", "Image too large to decode");
  3350. get32(s); //skip `ratio'
  3351. get16(s); //skip `fields'
  3352. get16(s); //skip `pad'
  3353. // intermediate buffer is RGBA
  3354. result = (stbi_uc *) malloc(x*y*4);
  3355. memset(result, 0xff, x*y*4);
  3356. if (!pic_load2(s,x,y,comp, result)) {
  3357. free(result);
  3358. result=0;
  3359. }
  3360. *px = x;
  3361. *py = y;
  3362. if (req_comp == 0) req_comp = *comp;
  3363. result=convert_format(result,4,req_comp,x,y);
  3364. return result;
  3365. }
  3366. int stbi_pic_test_memory(stbi_uc const *buffer, int len)
  3367. {
  3368. stbi s;
  3369. start_mem(&s,buffer,len);
  3370. return pic_test(&s);
  3371. }
  3372. stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  3373. {
  3374. stbi s;
  3375. start_mem(&s,buffer,len);
  3376. return pic_load(&s,x,y,comp,req_comp);
  3377. }
  3378. #ifndef STBI_NO_STDIO
  3379. int stbi_pic_test_file(FILE *f)
  3380. {
  3381. int result;
  3382. long l = ftell(f);
  3383. stbi s;
  3384. start_file(&s,f);
  3385. result = pic_test(&s);
  3386. fseek(f,l,SEEK_SET);
  3387. return result;
  3388. }
  3389. stbi_uc *stbi_pic_load(char const *filename,int *x, int *y, int *comp, int req_comp)
  3390. {
  3391. stbi_uc *result;
  3392. FILE *f=fopen(filename,"rb");
  3393. if (!f) return 0;
  3394. result = stbi_pic_load_from_file(f,x,y,comp,req_comp);
  3395. fclose(f);
  3396. return result;
  3397. }
  3398. stbi_uc *stbi_pic_load_from_file(FILE *f,int *x, int *y, int *comp, int req_comp)
  3399. {
  3400. stbi s;
  3401. start_file(&s,f);
  3402. return pic_load(&s,x,y,comp,req_comp);
  3403. }
  3404. #endif
  3405. // *************************************************************************************************
  3406. // GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
  3407. typedef struct stbi_gif_lzw_struct {
  3408. int16 prefix;
  3409. uint8 first;
  3410. uint8 suffix;
  3411. } stbi_gif_lzw;
  3412. typedef struct stbi_gif_struct
  3413. {
  3414. int w,h;
  3415. stbi_uc *out; // output buffer (always 4 components)
  3416. int flags, bgindex, ratio, transparent, eflags;
  3417. uint8 pal[256][4];
  3418. uint8 lpal[256][4];
  3419. stbi_gif_lzw codes[4096];
  3420. uint8 *color_table;
  3421. int parse, step;
  3422. int lflags;
  3423. int start_x, start_y;
  3424. int max_x, max_y;
  3425. int cur_x, cur_y;
  3426. int line_size;
  3427. } stbi_gif;
  3428. static int gif_test(stbi *s)
  3429. {
  3430. int sz;
  3431. if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8') return 0;
  3432. sz = get8(s);
  3433. if (sz != '9' && sz != '7') return 0;
  3434. if (get8(s) != 'a') return 0;
  3435. return 1;
  3436. }
  3437. #ifndef STBI_NO_STDIO
  3438. int stbi_gif_test_file (FILE *f)
  3439. {
  3440. stbi s;
  3441. int r,n = ftell(f);
  3442. start_file(&s,f);
  3443. r = gif_test(&s);
  3444. fseek(f,n,SEEK_SET);
  3445. return r;
  3446. }
  3447. #endif
  3448. int stbi_gif_test_memory (stbi_uc const *buffer, int len)
  3449. {
  3450. stbi s;
  3451. start_mem(&s, buffer, len);
  3452. return gif_test(&s);
  3453. }
  3454. static void stbi_gif_parse_colortable(stbi *s, uint8 pal[256][4], int num_entries, int transp)
  3455. {
  3456. int i;
  3457. for (i=0; i < num_entries; ++i) {
  3458. pal[i][2] = get8u(s);
  3459. pal[i][1] = get8u(s);
  3460. pal[i][0] = get8u(s);
  3461. pal[i][3] = transp ? 0 : 255;
  3462. }
  3463. }
  3464. static int stbi_gif_header(stbi *s, stbi_gif *g, int *comp, int is_info)
  3465. {
  3466. uint8 version;
  3467. if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8')
  3468. return e("not GIF", "Corrupt GIF");
  3469. version = get8u(s);
  3470. if (version != '7' && version != '9') return e("not GIF", "Corrupt GIF");
  3471. if (get8(s) != 'a') return e("not GIF", "Corrupt GIF");
  3472. failure_reason = "";
  3473. g->w = get16le(s);
  3474. g->h = get16le(s);
  3475. g->flags = get8(s);
  3476. g->bgindex = get8(s);
  3477. g->ratio = get8(s);
  3478. g->transparent = -1;
  3479. if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
  3480. if (is_info) return 1;
  3481. if (g->flags & 0x80)
  3482. stbi_gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
  3483. return 1;
  3484. }
  3485. static int stbi_gif_info_raw(stbi *s, int *x, int *y, int *comp)
  3486. {
  3487. stbi_gif g;
  3488. if (!stbi_gif_header(s, &g, comp, 1)) return 0;
  3489. if (x) *x = g.w;
  3490. if (y) *y = g.h;
  3491. return 1;
  3492. }
  3493. static void stbi_out_gif_code(stbi_gif *g, uint16 code)
  3494. {
  3495. uint8 *p, *c;
  3496. // recurse to decode the prefixes, since the linked-list is backwards,
  3497. // and working backwards through an interleaved image would be nasty
  3498. if (g->codes[code].prefix >= 0)
  3499. stbi_out_gif_code(g, g->codes[code].prefix);
  3500. if (g->cur_y >= g->max_y) return;
  3501. p = &g->out[g->cur_x + g->cur_y];
  3502. c = &g->color_table[g->codes[code].suffix * 4];
  3503. if (c[3] >= 128) {
  3504. p[0] = c[2];
  3505. p[1] = c[1];
  3506. p[2] = c[0];
  3507. p[3] = c[3];
  3508. }
  3509. g->cur_x += 4;
  3510. if (g->cur_x >= g->max_x) {
  3511. g->cur_x = g->start_x;
  3512. g->cur_y += g->step;
  3513. while (g->cur_y >= g->max_y && g->parse > 0) {
  3514. g->step = (1 << g->parse) * g->line_size;
  3515. g->cur_y = g->start_y + (g->step >> 1);
  3516. --g->parse;
  3517. }
  3518. }
  3519. }
  3520. static uint8 *stbi_process_gif_raster(stbi *s, stbi_gif *g)
  3521. {
  3522. uint8 lzw_cs;
  3523. int32 len, code;
  3524. uint32 first;
  3525. int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
  3526. stbi_gif_lzw *p;
  3527. lzw_cs = get8u(s);
  3528. clear = 1 << lzw_cs;
  3529. first = 1;
  3530. codesize = lzw_cs + 1;
  3531. codemask = (1 << codesize) - 1;
  3532. bits = 0;
  3533. valid_bits = 0;
  3534. for (code = 0; code < clear; code++) {
  3535. g->codes[code].prefix = -1;
  3536. g->codes[code].first = (uint8) code;
  3537. g->codes[code].suffix = (uint8) code;
  3538. }
  3539. // support no starting clear code
  3540. avail = clear+2;
  3541. oldcode = -1;
  3542. len = 0;
  3543. for(;;) {
  3544. if (valid_bits < codesize) {
  3545. if (len == 0) {
  3546. len = get8(s); // start new block
  3547. if (len == 0)
  3548. return g->out;
  3549. }
  3550. --len;
  3551. bits |= (int32) get8(s) << valid_bits;
  3552. valid_bits += 8;
  3553. } else {
  3554. int32 code = bits & codemask;
  3555. bits >>= codesize;
  3556. valid_bits -= codesize;
  3557. // @OPTIMIZE: is there some way we can accelerate the non-clear path?
  3558. if (code == clear) { // clear code
  3559. codesize = lzw_cs + 1;
  3560. codemask = (1 << codesize) - 1;
  3561. avail = clear + 2;
  3562. oldcode = -1;
  3563. first = 0;
  3564. } else if (code == clear + 1) { // end of stream code
  3565. skip(s, len);
  3566. while ((len = get8(s)) > 0)
  3567. skip(s,len);
  3568. return g->out;
  3569. } else if (code <= avail) {
  3570. if (first) return epuc("no clear code", "Corrupt GIF");
  3571. if (oldcode >= 0) {
  3572. p = &g->codes[avail++];
  3573. if (avail > 4096) return epuc("too many codes", "Corrupt GIF");
  3574. p->prefix = (int16) oldcode;
  3575. p->first = g->codes[oldcode].first;
  3576. p->suffix = (code == avail) ? p->first : g->codes[code].first;
  3577. } else if (code == avail)
  3578. return epuc("illegal code in raster", "Corrupt GIF");
  3579. stbi_out_gif_code(g, (uint16) code);
  3580. if ((avail & codemask) == 0 && avail <= 0x0FFF) {
  3581. codesize++;
  3582. codemask = (1 << codesize) - 1;
  3583. }
  3584. oldcode = code;
  3585. } else {
  3586. return epuc("illegal code in raster", "Corrupt GIF");
  3587. }
  3588. }
  3589. }
  3590. }
  3591. static void stbi_fill_gif_background(stbi_gif *g)
  3592. {
  3593. int i;
  3594. uint8 *c = g->pal[g->bgindex];
  3595. // @OPTIMIZE: write a dword at a time
  3596. for (i = 0; i < g->w * g->h * 4; i += 4) {
  3597. uint8 *p = &g->out[i];
  3598. p[0] = c[2];
  3599. p[1] = c[1];
  3600. p[2] = c[0];
  3601. p[3] = c[3];
  3602. }
  3603. }
  3604. // this function is designed to support animated gifs, although stb_image doesn't support it
  3605. static uint8 *stbi_gif_load_next(stbi *s, stbi_gif *g, int *comp, int req_comp)
  3606. {
  3607. int i;
  3608. uint8 *old_out = 0;
  3609. if (g->out == 0) {
  3610. if (!stbi_gif_header(s, g, comp,0)) return 0; // failure_reason set by stbi_gif_header
  3611. g->out = (uint8 *) malloc(4 * g->w * g->h);
  3612. if (g->out == 0) return epuc("outofmem", "Out of memory");
  3613. stbi_fill_gif_background(g);
  3614. } else {
  3615. // animated-gif-only path
  3616. if (((g->eflags & 0x1C) >> 2) == 3) {
  3617. old_out = g->out;
  3618. g->out = (uint8 *) malloc(4 * g->w * g->h);
  3619. if (g->out == 0) return epuc("outofmem", "Out of memory");
  3620. memcpy(g->out, old_out, g->w*g->h*4);
  3621. }
  3622. }
  3623. for (;;) {
  3624. switch (get8(s)) {
  3625. case 0x2C: /* Image Descriptor */
  3626. {
  3627. int32 x, y, w, h;
  3628. uint8 *o;
  3629. x = get16le(s);
  3630. y = get16le(s);
  3631. w = get16le(s);
  3632. h = get16le(s);
  3633. if (((x + w) > (g->w)) || ((y + h) > (g->h)))
  3634. return epuc("bad Image Descriptor", "Corrupt GIF");
  3635. g->line_size = g->w * 4;
  3636. g->start_x = x * 4;
  3637. g->start_y = y * g->line_size;
  3638. g->max_x = g->start_x + w * 4;
  3639. g->max_y = g->start_y + h * g->line_size;
  3640. g->cur_x = g->start_x;
  3641. g->cur_y = g->start_y;
  3642. g->lflags = get8(s);
  3643. if (g->lflags & 0x40) {
  3644. g->step = 8 * g->line_size; // first interlaced spacing
  3645. g->parse = 3;
  3646. } else {
  3647. g->step = g->line_size;
  3648. g->parse = 0;
  3649. }
  3650. if (g->lflags & 0x80) {
  3651. stbi_gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
  3652. g->color_table = (uint8 *) g->lpal;
  3653. } else if (g->flags & 0x80) {
  3654. for (i=0; i < 256; ++i) // @OPTIMIZE: reset only the previous transparent
  3655. g->pal[i][3] = 255;
  3656. if (g->transparent >= 0 && (g->eflags & 0x01))
  3657. g->pal[g->transparent][3] = 0;
  3658. g->color_table = (uint8 *) g->pal;
  3659. } else
  3660. return epuc("missing color table", "Corrupt GIF");
  3661. o = stbi_process_gif_raster(s, g);
  3662. if (o == NULL) return NULL;
  3663. if (req_comp && req_comp != 4)
  3664. o = convert_format(o, 4, req_comp, g->w, g->h);
  3665. return o;
  3666. }
  3667. case 0x21: // Comment Extension.
  3668. {
  3669. int len;
  3670. if (get8(s) == 0xF9) { // Graphic Control Extension.
  3671. len = get8(s);
  3672. if (len == 4) {
  3673. g->eflags = get8(s);
  3674. get16le(s); // delay
  3675. g->transparent = get8(s);
  3676. } else {
  3677. skip(s, len);
  3678. break;
  3679. }
  3680. }
  3681. while ((len = get8(s)) != 0)
  3682. skip(s, len);
  3683. break;
  3684. }
  3685. case 0x3B: // gif stream termination code
  3686. return (uint8 *) 1;
  3687. default:
  3688. return epuc("unknown code", "Corrupt GIF");
  3689. }
  3690. }
  3691. }
  3692. #ifndef STBI_NO_STDIO
  3693. stbi_uc *stbi_gif_load (char const *filename, int *x, int *y, int *comp, int req_comp)
  3694. {
  3695. uint8 *data;
  3696. FILE *f = fopen(filename, "rb");
  3697. if (!f) return NULL;
  3698. data = stbi_gif_load_from_file(f, x,y,comp,req_comp);
  3699. fclose(f);
  3700. return data;
  3701. }
  3702. stbi_uc *stbi_gif_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
  3703. {
  3704. uint8 *u = 0;
  3705. stbi s;
  3706. stbi_gif g={0};
  3707. start_file(&s, f);
  3708. u = stbi_gif_load_next(&s, &g, comp, req_comp);
  3709. if (u == (void *) 1) u = 0; // end of animated gif marker
  3710. if (u) {
  3711. *x = g.w;
  3712. *y = g.h;
  3713. }
  3714. return u;
  3715. }
  3716. #endif
  3717. stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  3718. {
  3719. uint8 *u = 0;
  3720. stbi s;
  3721. stbi_gif g={0};
  3722. start_mem(&s, buffer, len);
  3723. u = stbi_gif_load_next(&s, &g, comp, req_comp);
  3724. if (u == (void *) 1) u = 0; // end of animated gif marker
  3725. if (u) {
  3726. *x = g.w;
  3727. *y = g.h;
  3728. }
  3729. return u;
  3730. }
  3731. #ifndef STBI_NO_STDIO
  3732. int stbi_gif_info (char const *filename, int *x, int *y, int *comp)
  3733. {
  3734. int res;
  3735. FILE *f = fopen(filename, "rb");
  3736. if (!f) return 0;
  3737. res = stbi_gif_info_from_file(f, x, y, comp);
  3738. fclose(f);
  3739. return res;
  3740. }
  3741. int stbi_gif_info_from_file(FILE *f, int *x, int *y, int *comp)
  3742. {
  3743. stbi s;
  3744. int res;
  3745. long n = ftell(f);
  3746. start_file(&s, f);
  3747. res = stbi_gif_info_raw(&s, x, y, comp);
  3748. fseek(f, n, SEEK_SET);
  3749. return res;
  3750. }
  3751. #endif // !STBI_NO_STDIO
  3752. int stbi_gif_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  3753. {
  3754. stbi s;
  3755. start_mem(&s, buffer, len);
  3756. return stbi_gif_info_raw(&s, x, y, comp);
  3757. }
  3758. // *************************************************************************************************
  3759. // Radiance RGBE HDR loader
  3760. // originally by Nicolas Schulz
  3761. #ifndef STBI_NO_HDR
  3762. static int hdr_test(stbi *s)
  3763. {
  3764. const char *signature = "#?RADIANCE\n";
  3765. int i;
  3766. for (i=0; signature[i]; ++i)
  3767. if (get8(s) != signature[i])
  3768. return 0;
  3769. return 1;
  3770. }
  3771. int stbi_hdr_test_memory(stbi_uc const *buffer, int len)
  3772. {
  3773. stbi s;
  3774. start_mem(&s, buffer, len);
  3775. return hdr_test(&s);
  3776. }
  3777. #ifndef STBI_NO_STDIO
  3778. int stbi_hdr_test_file(FILE *f)
  3779. {
  3780. stbi s;
  3781. int r,n = ftell(f);
  3782. start_file(&s, f);
  3783. r = hdr_test(&s);
  3784. fseek(f,n,SEEK_SET);
  3785. return r;
  3786. }
  3787. #endif
  3788. #define HDR_BUFLEN 1024
  3789. static char *hdr_gettoken(stbi *z, char *buffer)
  3790. {
  3791. int len=0;
  3792. char c = '\0';
  3793. c = (char) get8(z);
  3794. while (!at_eof(z) && c != '\n') {
  3795. buffer[len++] = c;
  3796. if (len == HDR_BUFLEN-1) {
  3797. // flush to end of line
  3798. while (!at_eof(z) && get8(z) != '\n')
  3799. ;
  3800. break;
  3801. }
  3802. c = (char) get8(z);
  3803. }
  3804. buffer[len] = 0;
  3805. return buffer;
  3806. }
  3807. static void hdr_convert(float *output, stbi_uc *input, int req_comp)
  3808. {
  3809. if ( input[3] != 0 ) {
  3810. float f1;
  3811. // Exponent
  3812. f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
  3813. if (req_comp <= 2)
  3814. output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
  3815. else {
  3816. output[0] = input[0] * f1;
  3817. output[1] = input[1] * f1;
  3818. output[2] = input[2] * f1;
  3819. }
  3820. if (req_comp == 2) output[1] = 1;
  3821. if (req_comp == 4) output[3] = 1;
  3822. } else {
  3823. switch (req_comp) {
  3824. case 4: output[3] = 1; /* fallthrough */
  3825. case 3: output[0] = output[1] = output[2] = 0;
  3826. break;
  3827. case 2: output[1] = 1; /* fallthrough */
  3828. case 1: output[0] = 0;
  3829. break;
  3830. }
  3831. }
  3832. }
  3833. static float *hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp)
  3834. {
  3835. char buffer[HDR_BUFLEN];
  3836. char *token;
  3837. int valid = 0;
  3838. int width, height;
  3839. stbi_uc *scanline;
  3840. float *hdr_data;
  3841. int len;
  3842. unsigned char count, value;
  3843. int i, j, k, c1,c2, z;
  3844. // Check identifier
  3845. if (strcmp(hdr_gettoken(s,buffer), "#?RADIANCE") != 0)
  3846. return epf("not HDR", "Corrupt HDR image");
  3847. // Parse header
  3848. for(;;) {
  3849. token = hdr_gettoken(s,buffer);
  3850. if (token[0] == 0) break;
  3851. if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
  3852. }
  3853. if (!valid) return epf("unsupported format", "Unsupported HDR format");
  3854. // Parse width and height
  3855. // can't use sscanf() if we're not using stdio!
  3856. token = hdr_gettoken(s,buffer);
  3857. if (strncmp(token, "-Y ", 3)) return epf("unsupported data layout", "Unsupported HDR format");
  3858. token += 3;
  3859. height = strtol(token, &token, 10);
  3860. while (*token == ' ') ++token;
  3861. if (strncmp(token, "+X ", 3)) return epf("unsupported data layout", "Unsupported HDR format");
  3862. token += 3;
  3863. width = strtol(token, NULL, 10);
  3864. *x = width;
  3865. *y = height;
  3866. *comp = 3;
  3867. if (req_comp == 0) req_comp = 3;
  3868. // Read data
  3869. hdr_data = (float *) malloc(height * width * req_comp * sizeof(float));
  3870. // Load image data
  3871. // image data is stored as some number of sca
  3872. if ( width < 8 || width >= 32768) {
  3873. // Read flat data
  3874. for (j=0; j < height; ++j) {
  3875. for (i=0; i < width; ++i) {
  3876. stbi_uc rgbe[4];
  3877. main_decode_loop:
  3878. getn(s, rgbe, 4);
  3879. hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
  3880. }
  3881. }
  3882. } else {
  3883. // Read RLE-encoded data
  3884. scanline = NULL;
  3885. for (j = 0; j < height; ++j) {
  3886. c1 = get8(s);
  3887. c2 = get8(s);
  3888. len = get8(s);
  3889. if (c1 != 2 || c2 != 2 || (len & 0x80)) {
  3890. // not run-length encoded, so we have to actually use THIS data as a decoded
  3891. // pixel (note this can't be a valid pixel--one of RGB must be >= 128)
  3892. uint8 rgbe[4];
  3893. rgbe[0] = (uint8) c1;
  3894. rgbe[1] = (uint8) c2;
  3895. rgbe[2] = (uint8) len;
  3896. rgbe[3] = (uint8) get8u(s);
  3897. hdr_convert(hdr_data, rgbe, req_comp);
  3898. i = 1;
  3899. j = 0;
  3900. free(scanline);
  3901. goto main_decode_loop; // yes, this makes no sense
  3902. }
  3903. len <<= 8;
  3904. len |= get8(s);
  3905. if (len != width) { free(hdr_data); free(scanline); return epf("invalid decoded scanline length", "corrupt HDR"); }
  3906. if (scanline == NULL) scanline = (stbi_uc *) malloc(width * 4);
  3907. for (k = 0; k < 4; ++k) {
  3908. i = 0;
  3909. while (i < width) {
  3910. count = get8u(s);
  3911. if (count > 128) {
  3912. // Run
  3913. value = get8u(s);
  3914. count -= 128;
  3915. for (z = 0; z < count; ++z)
  3916. scanline[i++ * 4 + k] = value;
  3917. } else {
  3918. // Dump
  3919. for (z = 0; z < count; ++z)
  3920. scanline[i++ * 4 + k] = get8u(s);
  3921. }
  3922. }
  3923. }
  3924. for (i=0; i < width; ++i)
  3925. hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
  3926. }
  3927. free(scanline);
  3928. }
  3929. return hdr_data;
  3930. }
  3931. #ifndef STBI_NO_STDIO
  3932. float *stbi_hdr_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
  3933. {
  3934. stbi s;
  3935. start_file(&s,f);
  3936. return hdr_load(&s,x,y,comp,req_comp);
  3937. }
  3938. #endif
  3939. float *stbi_hdr_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
  3940. {
  3941. stbi s;
  3942. start_mem(&s,buffer, len);
  3943. return hdr_load(&s,x,y,comp,req_comp);
  3944. }
  3945. #endif // STBI_NO_HDR
  3946. #ifndef STBI_NO_STDIO
  3947. int stbi_info(char const *filename, int *x, int *y, int *comp)
  3948. {
  3949. FILE *f = fopen(filename, "rb");
  3950. int result;
  3951. if (!f) return e("can't fopen", "Unable to open file");
  3952. result = stbi_info_from_file(f, x, y, comp);
  3953. fclose(f);
  3954. return result;
  3955. }
  3956. int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
  3957. {
  3958. if (stbi_jpeg_info_from_file(f, x, y, comp))
  3959. return 1;
  3960. if (stbi_png_info_from_file(f, x, y, comp))
  3961. return 1;
  3962. if (stbi_gif_info_from_file(f, x, y, comp))
  3963. return 1;
  3964. // @TODO: stbi_bmp_info_from_file
  3965. // @TODO: stbi_psd_info_from_file
  3966. #ifndef STBI_NO_HDR
  3967. // @TODO: stbi_hdr_info_from_file
  3968. #endif
  3969. // test tga last because it's a crappy test!
  3970. if (stbi_tga_info_from_file(f, x, y, comp))
  3971. return 1;
  3972. return e("unknown image type", "Image not of any known type, or corrupt");
  3973. }
  3974. #endif // !STBI_NO_STDIO
  3975. int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
  3976. {
  3977. if (stbi_jpeg_info_from_memory(buffer, len, x, y, comp))
  3978. return 1;
  3979. if (stbi_png_info_from_memory(buffer, len, x, y, comp))
  3980. return 1;
  3981. if (stbi_gif_info_from_memory(buffer, len, x, y, comp))
  3982. return 1;
  3983. // @TODO: stbi_bmp_info_from_memory
  3984. // @TODO: stbi_psd_info_from_memory
  3985. #ifndef STBI_NO_HDR
  3986. // @TODO: stbi_hdr_info_from_memory
  3987. #endif
  3988. // test tga last because it's a crappy test!
  3989. if (stbi_tga_info_from_memory(buffer, len, x, y, comp))
  3990. return 1;
  3991. return e("unknown image type", "Image not of any known type, or corrupt");
  3992. }
  3993. #endif // STBI_HEADER_FILE_ONLY
  3994. /*
  3995. revision history:
  3996. 1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
  3997. 1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
  3998. 1.27 (2010-08-01)
  3999. cast-to-uint8 to fix warnings
  4000. 1.26 (2010-07-24)
  4001. fix bug in file buffering for PNG reported by SpartanJ
  4002. 1.25 (2010-07-17)
  4003. refix trans_data warning (Won Chun)
  4004. 1.24 (2010-07-12)
  4005. perf improvements reading from files on platforms with lock-heavy fgetc()
  4006. minor perf improvements for jpeg
  4007. deprecated type-specific functions so we'll get feedback if they're needed
  4008. attempt to fix trans_data warning (Won Chun)
  4009. 1.23 fixed bug in iPhone support
  4010. 1.22 (2010-07-10)
  4011. removed image *writing* support
  4012. removed image *writing* support
  4013. stbi_info support from Jetro Lauha
  4014. GIF support from Jean-Marc Lienher
  4015. iPhone PNG-extensions from James Brown
  4016. warning-fixes from Nicolas Schulz and Janez Zemva (i.e. Janez (U+017D)emva)
  4017. 1.21 fix use of 'uint8' in header (reported by jon blow)
  4018. 1.20 added support for Softimage PIC, by Tom Seddon
  4019. 1.19 bug in interlaced PNG corruption check (found by ryg)
  4020. 1.18 2008-08-02
  4021. fix a threading bug (local mutable static)
  4022. 1.17 support interlaced PNG
  4023. 1.16 major bugfix - convert_format converted one too many pixels
  4024. 1.15 initialize some fields for thread safety
  4025. 1.14 fix threadsafe conversion bug
  4026. header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
  4027. 1.13 threadsafe
  4028. 1.12 const qualifiers in the API
  4029. 1.11 Support installable IDCT, colorspace conversion routines
  4030. 1.10 Fixes for 64-bit (don't use "unsigned long")
  4031. optimized upsampling by Fabian "ryg" Giesen
  4032. 1.09 Fix format-conversion for PSD code (bad global variables!)
  4033. 1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
  4034. 1.07 attempt to fix C++ warning/errors again
  4035. 1.06 attempt to fix C++ warning/errors again
  4036. 1.05 fix TGA loading to return correct *comp and use good luminance calc
  4037. 1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
  4038. 1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
  4039. 1.02 support for (subset of) HDR files, float interface for preferred access to them
  4040. 1.01 fix bug: possible bug in handling right-side up bmps... not sure
  4041. fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
  4042. 1.00 interface to zlib that skips zlib header
  4043. 0.99 correct handling of alpha in palette
  4044. 0.98 TGA loader by lonesock; dynamically add loaders (untested)
  4045. 0.97 jpeg errors on too large a file; also catch another malloc failure
  4046. 0.96 fix detection of invalid v value - particleman@mollyrocket forum
  4047. 0.95 during header scan, seek to markers in case of padding
  4048. 0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
  4049. 0.93 handle jpegtran output; verbose errors
  4050. 0.92 read 4,8,16,24,32-bit BMP files of several formats
  4051. 0.91 output 24-bit Windows 3.0 BMP files
  4052. 0.90 fix a few more warnings; bump version number to approach 1.0
  4053. 0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
  4054. 0.60 fix compiling as c++
  4055. 0.59 fix warnings: merge Dave Moore's -Wall fixes
  4056. 0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
  4057. 0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less
  4058. than 16 available
  4059. 0.56 fix bug: zlib uncompressed mode len vs. nlen
  4060. 0.55 fix bug: restart_interval not initialized to 0
  4061. 0.54 allow NULL for 'int *comp'
  4062. 0.53 fix bug in png 3->4; speedup png decoding
  4063. 0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
  4064. 0.51 obey req_comp requests, 1-component jpegs return as 1-component,
  4065. on 'test' only check type, not whether we support this variant
  4066. */