GLS.ImageUtils.pas 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103
  1. //
  2. // The graphics engine GLScene https://github.com/glscene
  3. //
  4. unit GLS.ImageUtils;
  5. (* Main purpose is as a fallback in cases where there is no other way to process images *)
  6. // TODO: Complite InfToXXX
  7. // TODO: BPTC decompression
  8. // TODO: S3TC compression
  9. // TODO: LATC compression
  10. // TODO: RGTC compression
  11. // TODO: BPTC compression
  12. // TODO: Build3DMipmap
  13. interface
  14. {$I GLS.Scene.inc}
  15. uses
  16. Winapi.OpenGL,
  17. Winapi.OpenGLext,
  18. System.SysUtils,
  19. System.Classes,
  20. System.Math,
  21. GLS.OpenGLTokens,
  22. GLS.Strings,
  23. GLS.TextureFormat,
  24. GLS.VectorGeometry,
  25. GLS.Utils;
  26. var
  27. vImageScaleFilterWidth: Integer = 5; // Relative sample radius for filtering
  28. type
  29. TIntermediateFormat = record
  30. R, G, B, A: Single;
  31. end;
  32. TPointerArray = array of Pointer;
  33. PRGBA32F = ^TIntermediateFormat;
  34. TIntermediateFormatArray = array [0 .. MaxInt div (2 * SizeOf(TIntermediateFormat))] of TIntermediateFormat;
  35. PIntermediateFormatArray = ^TIntermediateFormatArray;
  36. TU48BitBlock = array [0 .. 3, 0 .. 3] of Byte;
  37. T48BitBlock = array [0 .. 3, 0 .. 3] of SmallInt;
  38. EGLImageUtils = class(Exception);
  39. TImageFilterFunction = function(Value: Single): Single;
  40. TImageAlphaProc = procedure(var AColor: TIntermediateFormat);
  41. function ImageBoxFilter(Value: Single): Single;
  42. function ImageTriangleFilter(Value: Single): Single;
  43. function ImageHermiteFilter(Value: Single): Single;
  44. function ImageBellFilter(Value: Single): Single;
  45. function ImageSplineFilter(Value: Single): Single;
  46. function ImageLanczos3Filter(Value: Single): Single;
  47. function ImageMitchellFilter(Value: Single): Single;
  48. procedure ImageAlphaFromIntensity(var AColor: TIntermediateFormat);
  49. procedure ImageAlphaSuperBlackTransparent(var AColor: TIntermediateFormat);
  50. procedure ImageAlphaLuminance(var AColor: TIntermediateFormat);
  51. procedure ImageAlphaLuminanceSqrt(var AColor: TIntermediateFormat);
  52. procedure ImageAlphaOpaque(var AColor: TIntermediateFormat);
  53. procedure ImageAlphaTopLeftPointColorTransparent(var AColor: TIntermediateFormat);
  54. procedure ImageAlphaInverseLuminance(var AColor: TIntermediateFormat);
  55. procedure ImageAlphaInverseLuminanceSqrt(var AColor: TIntermediateFormat);
  56. procedure ImageAlphaBottomRightPointColorTransparent(var AColor: TIntermediateFormat);
  57. procedure ConvertImage(const ASrc: Pointer; const ADst: Pointer; ASrcColorFormat, ADstColorFormat: Cardinal; ASrcDataType, ADstDataType: Cardinal; AWidth, AHeight: Integer);
  58. procedure RescaleImage(const ASrc: Pointer; const ADst: Pointer; AColorFormat: Cardinal; ADataType: Cardinal; AFilter: TImageFilterFunction; ASrcWidth, ASrcHeight, ADstWidth, ADstHeight: Integer);
  59. procedure Build2DMipmap(const ASrc: Pointer; const ADst: TPointerArray; AColorFormat: Cardinal; ADataType: Cardinal; AFilter: TImageFilterFunction; ASrcWidth, ASrcHeight: Integer);
  60. procedure AlphaGammaBrightCorrection(const ASrc: Pointer; AColorFormat: Cardinal; ADataType: Cardinal; ASrcWidth, ASrcHeight: Integer; anAlphaProc: TImageAlphaProc; ABrightness: Single; AGamma: Single);
  61. //------------------------------------------------------------------------------
  62. implementation
  63. //------------------------------------------------------------------------------
  64. const
  65. cSuperBlack: TIntermediateFormat = (R: 0.0; G: 0.0; B: 0.0; A: 0.0);
  66. type
  67. TConvertToImfProc = procedure(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  68. TConvertFromInfProc = procedure(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  69. procedure Swap(var A, B: Integer); inline;
  70. var
  71. C: Integer;
  72. begin
  73. C := A;
  74. A := B;
  75. B := C;
  76. end;
  77. // ------------------------------ OpenGL format image to RGBA Float
  78. procedure UnsupportedToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  79. begin
  80. raise EGLImageUtils.Create('Unimplemented type of conversion');
  81. end;
  82. procedure UbyteToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  83. var
  84. pSource: PByte;
  85. n: Integer;
  86. c0: Single;
  87. function GetChannel: Single;
  88. begin
  89. Result := pSource^;
  90. Inc(pSource);
  91. end;
  92. begin
  93. pSource := PByte(ASource);
  94. case AColorFormat of
  95. //{$I ImgUtilCaseGL2Imf.inc}
  96. GL_RGB, GL_RGB_INTEGER:
  97. for n := 0 to AWidth*AHeight-1 do
  98. begin
  99. ADest[n].R := GetChannel;
  100. ADest[n].G := GetChannel;
  101. ADest[n].B := GetChannel;
  102. ADest[n].A := 255.0;
  103. end;
  104. GL_BGR, GL_BGR_INTEGER:
  105. for n := 0 to AWidth*AHeight-1 do
  106. begin
  107. ADest[n].B := GetChannel;
  108. ADest[n].G := GetChannel;
  109. ADest[n].R := GetChannel;
  110. ADest[n].A := 255.0;
  111. end;
  112. GL_RGBA, GL_RGBA_INTEGER:
  113. for n := 0 to AWidth*AHeight-1 do
  114. begin
  115. ADest[n].R := GetChannel;
  116. ADest[n].G := GetChannel;
  117. ADest[n].B := GetChannel;
  118. ADest[n].A := GetChannel;
  119. end;
  120. GL_BGRA, GL_BGRA_INTEGER:
  121. for n := 0 to AWidth*AHeight-1 do
  122. begin
  123. ADest[n].B := GetChannel;
  124. ADest[n].G := GetChannel;
  125. ADest[n].R := GetChannel;
  126. ADest[n].A := GetChannel;
  127. end;
  128. GL_ALPHA, GL_ALPHA_INTEGER:
  129. for n := 0 to AWidth*AHeight-1 do
  130. begin
  131. ADest[n].R := 0;
  132. ADest[n].G := 0;
  133. ADest[n].B := 0;
  134. ADest[n].A := GetChannel;
  135. end;
  136. GL_LUMINANCE, GL_LUMINANCE_INTEGER_EXT:
  137. for n := 0 to AWidth*AHeight-1 do
  138. begin
  139. c0 := GetChannel;
  140. ADest[n].R := c0;
  141. ADest[n].G := c0;
  142. ADest[n].B := c0;
  143. ADest[n].A := 255.0;
  144. end;
  145. GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA_INTEGER_EXT:
  146. for n := 0 to AWidth*AHeight-1 do
  147. begin
  148. c0 := GetChannel;
  149. ADest[n].R := c0;
  150. ADest[n].G := c0;
  151. ADest[n].B := c0;
  152. ADest[n].A := GetChannel;
  153. end;
  154. GL_INTENSITY:
  155. for n := 0 to AWidth*AHeight-1 do
  156. begin
  157. c0 := GetChannel;
  158. ADest[n].R := c0;
  159. ADest[n].G := c0;
  160. ADest[n].B := c0;
  161. ADest[n].A := c0;
  162. end;
  163. GL_RED, GL_RED_INTEGER:
  164. for n := 0 to AWidth*AHeight-1 do
  165. begin
  166. ADest[n].R := GetChannel;
  167. ADest[n].G := 0;
  168. ADest[n].B := 0;
  169. ADest[n].A := 255;
  170. end;
  171. GL_GREEN, GL_GREEN_INTEGER:
  172. for n := 0 to AWidth*AHeight-1 do
  173. begin
  174. ADest[n].R := 0;
  175. ADest[n].G := GetChannel;
  176. ADest[n].B := 0;
  177. ADest[n].A := 255;
  178. end;
  179. GL_BLUE, GL_BLUE_INTEGER:
  180. for n := 0 to AWidth*AHeight-1 do
  181. begin
  182. ADest[n].R := 0;
  183. ADest[n].G := 0;
  184. ADest[n].B := GetChannel;
  185. ADest[n].A := 255;
  186. end;
  187. GL_RG, GL_RG_INTEGER:
  188. for n := 0 to AWidth*AHeight-1 do
  189. begin
  190. ADest[n].R := GetChannel;
  191. ADest[n].G := GetChannel;
  192. ADest[n].B := 0;
  193. ADest[n].A := 255;
  194. end;
  195. else
  196. raise EGLImageUtils.Create(strInvalidType);
  197. end;
  198. end;
  199. procedure Ubyte332ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  200. var
  201. pSource: PByte;
  202. c0, c1, c2, c3: Byte;
  203. n: Integer;
  204. procedure GetChannel;
  205. begin
  206. c0 := pSource^;
  207. c1 := $E0 and c0;
  208. c2 := $E0 and (c0 shl 3);
  209. c3 := $C0 and (c0 shl 6);
  210. Inc(pSource);
  211. end;
  212. begin
  213. pSource := PByte(ASource);
  214. case AColorFormat of
  215. GL_RGB:
  216. for n := 0 to AWidth * AHeight - 1 do
  217. begin
  218. GetChannel;
  219. ADest[n].R := c1;
  220. ADest[n].G := c2;
  221. ADest[n].B := c3;
  222. end;
  223. GL_BGR:
  224. for n := 0 to AWidth * AHeight - 1 do
  225. begin
  226. GetChannel;
  227. ADest[n].B := c1;
  228. ADest[n].G := c2;
  229. ADest[n].R := c3;
  230. end;
  231. else
  232. raise EGLImageUtils.Create(strInvalidType);
  233. end;
  234. end;
  235. procedure Ubyte233RToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  236. var
  237. pSource: PByte;
  238. c0, c1, c2, c3: Byte;
  239. n: Integer;
  240. procedure GetChannel;
  241. begin
  242. c0 := pSource^;
  243. c3 := $E0 and c0;
  244. c2 := $E0 and (c0 shl 3);
  245. c1 := $C0 and (c0 shl 6);
  246. Inc(pSource);
  247. end;
  248. begin
  249. pSource := PByte(ASource);
  250. case AColorFormat of
  251. GL_RGB:
  252. for n := 0 to AWidth * AHeight - 1 do
  253. begin
  254. GetChannel;
  255. ADest[n].R := c1;
  256. ADest[n].G := c2;
  257. ADest[n].B := c3;
  258. end;
  259. GL_BGR:
  260. for n := 0 to AWidth * AHeight - 1 do
  261. begin
  262. GetChannel;
  263. ADest[n].B := c1;
  264. ADest[n].G := c2;
  265. ADest[n].R := c3;
  266. end;
  267. else
  268. raise EGLImageUtils.Create(strInvalidType);
  269. end;
  270. end;
  271. procedure ByteToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  272. var
  273. pSource: PShortInt;
  274. n: Integer;
  275. c0: Single;
  276. function GetChannel: Single;
  277. begin
  278. Result := pSource^;
  279. Inc(pSource);
  280. end;
  281. begin
  282. pSource := PShortInt(ASource);
  283. case AColorFormat of
  284. //{$I ImgUtilCaseGL2Imf.inc}
  285. GL_RGB, GL_RGB_INTEGER:
  286. for n := 0 to AWidth*AHeight-1 do
  287. begin
  288. ADest[n].R := GetChannel;
  289. ADest[n].G := GetChannel;
  290. ADest[n].B := GetChannel;
  291. ADest[n].A := 255.0;
  292. end;
  293. GL_BGR, GL_BGR_INTEGER:
  294. for n := 0 to AWidth*AHeight-1 do
  295. begin
  296. ADest[n].B := GetChannel;
  297. ADest[n].G := GetChannel;
  298. ADest[n].R := GetChannel;
  299. ADest[n].A := 255.0;
  300. end;
  301. GL_RGBA, GL_RGBA_INTEGER:
  302. for n := 0 to AWidth*AHeight-1 do
  303. begin
  304. ADest[n].R := GetChannel;
  305. ADest[n].G := GetChannel;
  306. ADest[n].B := GetChannel;
  307. ADest[n].A := GetChannel;
  308. end;
  309. GL_BGRA, GL_BGRA_INTEGER:
  310. for n := 0 to AWidth*AHeight-1 do
  311. begin
  312. ADest[n].B := GetChannel;
  313. ADest[n].G := GetChannel;
  314. ADest[n].R := GetChannel;
  315. ADest[n].A := GetChannel;
  316. end;
  317. GL_ALPHA, GL_ALPHA_INTEGER:
  318. for n := 0 to AWidth*AHeight-1 do
  319. begin
  320. ADest[n].R := 0;
  321. ADest[n].G := 0;
  322. ADest[n].B := 0;
  323. ADest[n].A := GetChannel;
  324. end;
  325. GL_LUMINANCE, GL_LUMINANCE_INTEGER_EXT:
  326. for n := 0 to AWidth*AHeight-1 do
  327. begin
  328. c0 := GetChannel;
  329. ADest[n].R := c0;
  330. ADest[n].G := c0;
  331. ADest[n].B := c0;
  332. ADest[n].A := 255.0;
  333. end;
  334. GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA_INTEGER_EXT:
  335. for n := 0 to AWidth*AHeight-1 do
  336. begin
  337. c0 := GetChannel;
  338. ADest[n].R := c0;
  339. ADest[n].G := c0;
  340. ADest[n].B := c0;
  341. ADest[n].A := GetChannel;
  342. end;
  343. GL_INTENSITY:
  344. for n := 0 to AWidth*AHeight-1 do
  345. begin
  346. c0 := GetChannel;
  347. ADest[n].R := c0;
  348. ADest[n].G := c0;
  349. ADest[n].B := c0;
  350. ADest[n].A := c0;
  351. end;
  352. GL_RED, GL_RED_INTEGER:
  353. for n := 0 to AWidth*AHeight-1 do
  354. begin
  355. ADest[n].R := GetChannel;
  356. ADest[n].G := 0;
  357. ADest[n].B := 0;
  358. ADest[n].A := 255;
  359. end;
  360. GL_GREEN, GL_GREEN_INTEGER:
  361. for n := 0 to AWidth*AHeight-1 do
  362. begin
  363. ADest[n].R := 0;
  364. ADest[n].G := GetChannel;
  365. ADest[n].B := 0;
  366. ADest[n].A := 255;
  367. end;
  368. GL_BLUE, GL_BLUE_INTEGER:
  369. for n := 0 to AWidth*AHeight-1 do
  370. begin
  371. ADest[n].R := 0;
  372. ADest[n].G := 0;
  373. ADest[n].B := GetChannel;
  374. ADest[n].A := 255;
  375. end;
  376. GL_RG, GL_RG_INTEGER:
  377. for n := 0 to AWidth*AHeight-1 do
  378. begin
  379. ADest[n].R := GetChannel;
  380. ADest[n].G := GetChannel;
  381. ADest[n].B := 0;
  382. ADest[n].A := 255;
  383. end;
  384. else
  385. raise EGLImageUtils.Create(strInvalidType);
  386. end;
  387. end;
  388. procedure UShortToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  389. var
  390. pSource: PWord;
  391. n: Integer;
  392. c0: Single;
  393. function GetChannel: Single;
  394. begin
  395. Result := pSource^ / $100;
  396. Inc(pSource);
  397. end;
  398. begin
  399. pSource := PWord(ASource);
  400. case AColorFormat of
  401. GL_RGB, GL_RGB_INTEGER:
  402. for n := 0 to AWidth*AHeight-1 do
  403. begin
  404. ADest[n].R := GetChannel;
  405. ADest[n].G := GetChannel;
  406. ADest[n].B := GetChannel;
  407. ADest[n].A := 255.0;
  408. end;
  409. GL_BGR, GL_BGR_INTEGER:
  410. for n := 0 to AWidth*AHeight-1 do
  411. begin
  412. ADest[n].B := GetChannel;
  413. ADest[n].G := GetChannel;
  414. ADest[n].R := GetChannel;
  415. ADest[n].A := 255.0;
  416. end;
  417. GL_RGBA, GL_RGBA_INTEGER:
  418. for n := 0 to AWidth*AHeight-1 do
  419. begin
  420. ADest[n].R := GetChannel;
  421. ADest[n].G := GetChannel;
  422. ADest[n].B := GetChannel;
  423. ADest[n].A := GetChannel;
  424. end;
  425. GL_BGRA, GL_BGRA_INTEGER:
  426. for n := 0 to AWidth*AHeight-1 do
  427. begin
  428. ADest[n].B := GetChannel;
  429. ADest[n].G := GetChannel;
  430. ADest[n].R := GetChannel;
  431. ADest[n].A := GetChannel;
  432. end;
  433. GL_ALPHA, GL_ALPHA_INTEGER:
  434. for n := 0 to AWidth*AHeight-1 do
  435. begin
  436. ADest[n].R := 0;
  437. ADest[n].G := 0;
  438. ADest[n].B := 0;
  439. ADest[n].A := GetChannel;
  440. end;
  441. GL_LUMINANCE, GL_LUMINANCE_INTEGER_EXT:
  442. for n := 0 to AWidth*AHeight-1 do
  443. begin
  444. c0 := GetChannel;
  445. ADest[n].R := c0;
  446. ADest[n].G := c0;
  447. ADest[n].B := c0;
  448. ADest[n].A := 255.0;
  449. end;
  450. GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA_INTEGER_EXT:
  451. for n := 0 to AWidth*AHeight-1 do
  452. begin
  453. c0 := GetChannel;
  454. ADest[n].R := c0;
  455. ADest[n].G := c0;
  456. ADest[n].B := c0;
  457. ADest[n].A := GetChannel;
  458. end;
  459. GL_INTENSITY:
  460. for n := 0 to AWidth*AHeight-1 do
  461. begin
  462. c0 := GetChannel;
  463. ADest[n].R := c0;
  464. ADest[n].G := c0;
  465. ADest[n].B := c0;
  466. ADest[n].A := c0;
  467. end;
  468. GL_RED, GL_RED_INTEGER:
  469. for n := 0 to AWidth*AHeight-1 do
  470. begin
  471. ADest[n].R := GetChannel;
  472. ADest[n].G := 0;
  473. ADest[n].B := 0;
  474. ADest[n].A := 255;
  475. end;
  476. GL_GREEN, GL_GREEN_INTEGER:
  477. for n := 0 to AWidth*AHeight-1 do
  478. begin
  479. ADest[n].R := 0;
  480. ADest[n].G := GetChannel;
  481. ADest[n].B := 0;
  482. ADest[n].A := 255;
  483. end;
  484. GL_BLUE, GL_BLUE_INTEGER:
  485. for n := 0 to AWidth*AHeight-1 do
  486. begin
  487. ADest[n].R := 0;
  488. ADest[n].G := 0;
  489. ADest[n].B := GetChannel;
  490. ADest[n].A := 255;
  491. end;
  492. GL_RG, GL_RG_INTEGER:
  493. for n := 0 to AWidth*AHeight-1 do
  494. begin
  495. ADest[n].R := GetChannel;
  496. ADest[n].G := GetChannel;
  497. ADest[n].B := 0;
  498. ADest[n].A := 255;
  499. end;
  500. else
  501. raise EGLImageUtils.Create(strInvalidType);
  502. end;
  503. end;
  504. procedure ShortToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  505. var
  506. pSource: PSmallInt;
  507. n: Integer;
  508. c0: Single;
  509. function GetChannel: Single;
  510. begin
  511. Result := pSource^ / $100;
  512. Inc(pSource);
  513. end;
  514. begin
  515. pSource := PSmallInt(ASource);
  516. case AColorFormat of
  517. GL_RGB, GL_RGB_INTEGER:
  518. for n := 0 to AWidth*AHeight-1 do
  519. begin
  520. ADest[n].R := GetChannel;
  521. ADest[n].G := GetChannel;
  522. ADest[n].B := GetChannel;
  523. ADest[n].A := 255.0;
  524. end;
  525. GL_BGR, GL_BGR_INTEGER:
  526. for n := 0 to AWidth*AHeight-1 do
  527. begin
  528. ADest[n].B := GetChannel;
  529. ADest[n].G := GetChannel;
  530. ADest[n].R := GetChannel;
  531. ADest[n].A := 255.0;
  532. end;
  533. GL_RGBA, GL_RGBA_INTEGER:
  534. for n := 0 to AWidth*AHeight-1 do
  535. begin
  536. ADest[n].R := GetChannel;
  537. ADest[n].G := GetChannel;
  538. ADest[n].B := GetChannel;
  539. ADest[n].A := GetChannel;
  540. end;
  541. GL_BGRA, GL_BGRA_INTEGER:
  542. for n := 0 to AWidth*AHeight-1 do
  543. begin
  544. ADest[n].B := GetChannel;
  545. ADest[n].G := GetChannel;
  546. ADest[n].R := GetChannel;
  547. ADest[n].A := GetChannel;
  548. end;
  549. GL_ALPHA, GL_ALPHA_INTEGER:
  550. for n := 0 to AWidth*AHeight-1 do
  551. begin
  552. ADest[n].R := 0;
  553. ADest[n].G := 0;
  554. ADest[n].B := 0;
  555. ADest[n].A := GetChannel;
  556. end;
  557. GL_LUMINANCE, GL_LUMINANCE_INTEGER_EXT:
  558. for n := 0 to AWidth*AHeight-1 do
  559. begin
  560. c0 := GetChannel;
  561. ADest[n].R := c0;
  562. ADest[n].G := c0;
  563. ADest[n].B := c0;
  564. ADest[n].A := 255.0;
  565. end;
  566. GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA_INTEGER_EXT:
  567. for n := 0 to AWidth*AHeight-1 do
  568. begin
  569. c0 := GetChannel;
  570. ADest[n].R := c0;
  571. ADest[n].G := c0;
  572. ADest[n].B := c0;
  573. ADest[n].A := GetChannel;
  574. end;
  575. GL_INTENSITY:
  576. for n := 0 to AWidth*AHeight-1 do
  577. begin
  578. c0 := GetChannel;
  579. ADest[n].R := c0;
  580. ADest[n].G := c0;
  581. ADest[n].B := c0;
  582. ADest[n].A := c0;
  583. end;
  584. GL_RED, GL_RED_INTEGER:
  585. for n := 0 to AWidth*AHeight-1 do
  586. begin
  587. ADest[n].R := GetChannel;
  588. ADest[n].G := 0;
  589. ADest[n].B := 0;
  590. ADest[n].A := 255;
  591. end;
  592. GL_GREEN, GL_GREEN_INTEGER:
  593. for n := 0 to AWidth*AHeight-1 do
  594. begin
  595. ADest[n].R := 0;
  596. ADest[n].G := GetChannel;
  597. ADest[n].B := 0;
  598. ADest[n].A := 255;
  599. end;
  600. GL_BLUE, GL_BLUE_INTEGER:
  601. for n := 0 to AWidth*AHeight-1 do
  602. begin
  603. ADest[n].R := 0;
  604. ADest[n].G := 0;
  605. ADest[n].B := GetChannel;
  606. ADest[n].A := 255;
  607. end;
  608. GL_RG, GL_RG_INTEGER:
  609. for n := 0 to AWidth*AHeight-1 do
  610. begin
  611. ADest[n].R := GetChannel;
  612. ADest[n].G := GetChannel;
  613. ADest[n].B := 0;
  614. ADest[n].A := 255;
  615. end;
  616. else
  617. raise EGLImageUtils.Create(strInvalidType);
  618. end;
  619. end;
  620. procedure UIntToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  621. var
  622. pSource: PLongWord;
  623. n: Integer;
  624. c0: Single;
  625. function GetChannel: Single;
  626. begin
  627. Result := pSource^ / $1000000;
  628. Inc(pSource);
  629. end;
  630. begin
  631. pSource := PLongWord(ASource);
  632. case AColorFormat of
  633. GL_RGB, GL_RGB_INTEGER:
  634. for n := 0 to AWidth*AHeight-1 do
  635. begin
  636. ADest[n].R := GetChannel;
  637. ADest[n].G := GetChannel;
  638. ADest[n].B := GetChannel;
  639. ADest[n].A := 255.0;
  640. end;
  641. GL_BGR, GL_BGR_INTEGER:
  642. for n := 0 to AWidth*AHeight-1 do
  643. begin
  644. ADest[n].B := GetChannel;
  645. ADest[n].G := GetChannel;
  646. ADest[n].R := GetChannel;
  647. ADest[n].A := 255.0;
  648. end;
  649. GL_RGBA, GL_RGBA_INTEGER:
  650. for n := 0 to AWidth*AHeight-1 do
  651. begin
  652. ADest[n].R := GetChannel;
  653. ADest[n].G := GetChannel;
  654. ADest[n].B := GetChannel;
  655. ADest[n].A := GetChannel;
  656. end;
  657. GL_BGRA, GL_BGRA_INTEGER:
  658. for n := 0 to AWidth*AHeight-1 do
  659. begin
  660. ADest[n].B := GetChannel;
  661. ADest[n].G := GetChannel;
  662. ADest[n].R := GetChannel;
  663. ADest[n].A := GetChannel;
  664. end;
  665. GL_ALPHA, GL_ALPHA_INTEGER:
  666. for n := 0 to AWidth*AHeight-1 do
  667. begin
  668. ADest[n].R := 0;
  669. ADest[n].G := 0;
  670. ADest[n].B := 0;
  671. ADest[n].A := GetChannel;
  672. end;
  673. GL_LUMINANCE, GL_LUMINANCE_INTEGER_EXT:
  674. for n := 0 to AWidth*AHeight-1 do
  675. begin
  676. c0 := GetChannel;
  677. ADest[n].R := c0;
  678. ADest[n].G := c0;
  679. ADest[n].B := c0;
  680. ADest[n].A := 255.0;
  681. end;
  682. GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA_INTEGER_EXT:
  683. for n := 0 to AWidth*AHeight-1 do
  684. begin
  685. c0 := GetChannel;
  686. ADest[n].R := c0;
  687. ADest[n].G := c0;
  688. ADest[n].B := c0;
  689. ADest[n].A := GetChannel;
  690. end;
  691. GL_INTENSITY:
  692. for n := 0 to AWidth*AHeight-1 do
  693. begin
  694. c0 := GetChannel;
  695. ADest[n].R := c0;
  696. ADest[n].G := c0;
  697. ADest[n].B := c0;
  698. ADest[n].A := c0;
  699. end;
  700. GL_RED, GL_RED_INTEGER:
  701. for n := 0 to AWidth*AHeight-1 do
  702. begin
  703. ADest[n].R := GetChannel;
  704. ADest[n].G := 0;
  705. ADest[n].B := 0;
  706. ADest[n].A := 255;
  707. end;
  708. GL_GREEN, GL_GREEN_INTEGER:
  709. for n := 0 to AWidth*AHeight-1 do
  710. begin
  711. ADest[n].R := 0;
  712. ADest[n].G := GetChannel;
  713. ADest[n].B := 0;
  714. ADest[n].A := 255;
  715. end;
  716. GL_BLUE, GL_BLUE_INTEGER:
  717. for n := 0 to AWidth*AHeight-1 do
  718. begin
  719. ADest[n].R := 0;
  720. ADest[n].G := 0;
  721. ADest[n].B := GetChannel;
  722. ADest[n].A := 255;
  723. end;
  724. GL_RG, GL_RG_INTEGER:
  725. for n := 0 to AWidth*AHeight-1 do
  726. begin
  727. ADest[n].R := GetChannel;
  728. ADest[n].G := GetChannel;
  729. ADest[n].B := 0;
  730. ADest[n].A := 255;
  731. end;
  732. else
  733. raise EGLImageUtils.Create(strInvalidType);
  734. end;
  735. end;
  736. procedure IntToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  737. var
  738. pSource: PLongInt;
  739. n: Integer;
  740. c0: Single;
  741. function GetChannel: Single;
  742. begin
  743. Result := pSource^ / $1000000;
  744. Inc(pSource);
  745. end;
  746. begin
  747. pSource := PLongInt(ASource);
  748. case AColorFormat of
  749. GL_RGB, GL_RGB_INTEGER:
  750. for n := 0 to AWidth*AHeight-1 do
  751. begin
  752. ADest[n].R := GetChannel;
  753. ADest[n].G := GetChannel;
  754. ADest[n].B := GetChannel;
  755. ADest[n].A := 255.0;
  756. end;
  757. GL_BGR, GL_BGR_INTEGER:
  758. for n := 0 to AWidth*AHeight-1 do
  759. begin
  760. ADest[n].B := GetChannel;
  761. ADest[n].G := GetChannel;
  762. ADest[n].R := GetChannel;
  763. ADest[n].A := 255.0;
  764. end;
  765. GL_RGBA, GL_RGBA_INTEGER:
  766. for n := 0 to AWidth*AHeight-1 do
  767. begin
  768. ADest[n].R := GetChannel;
  769. ADest[n].G := GetChannel;
  770. ADest[n].B := GetChannel;
  771. ADest[n].A := GetChannel;
  772. end;
  773. GL_BGRA, GL_BGRA_INTEGER:
  774. for n := 0 to AWidth*AHeight-1 do
  775. begin
  776. ADest[n].B := GetChannel;
  777. ADest[n].G := GetChannel;
  778. ADest[n].R := GetChannel;
  779. ADest[n].A := GetChannel;
  780. end;
  781. GL_ALPHA, GL_ALPHA_INTEGER:
  782. for n := 0 to AWidth*AHeight-1 do
  783. begin
  784. ADest[n].R := 0;
  785. ADest[n].G := 0;
  786. ADest[n].B := 0;
  787. ADest[n].A := GetChannel;
  788. end;
  789. GL_LUMINANCE, GL_LUMINANCE_INTEGER_EXT:
  790. for n := 0 to AWidth*AHeight-1 do
  791. begin
  792. c0 := GetChannel;
  793. ADest[n].R := c0;
  794. ADest[n].G := c0;
  795. ADest[n].B := c0;
  796. ADest[n].A := 255.0;
  797. end;
  798. GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA_INTEGER_EXT:
  799. for n := 0 to AWidth*AHeight-1 do
  800. begin
  801. c0 := GetChannel;
  802. ADest[n].R := c0;
  803. ADest[n].G := c0;
  804. ADest[n].B := c0;
  805. ADest[n].A := GetChannel;
  806. end;
  807. GL_INTENSITY:
  808. for n := 0 to AWidth*AHeight-1 do
  809. begin
  810. c0 := GetChannel;
  811. ADest[n].R := c0;
  812. ADest[n].G := c0;
  813. ADest[n].B := c0;
  814. ADest[n].A := c0;
  815. end;
  816. GL_RED, GL_RED_INTEGER:
  817. for n := 0 to AWidth*AHeight-1 do
  818. begin
  819. ADest[n].R := GetChannel;
  820. ADest[n].G := 0;
  821. ADest[n].B := 0;
  822. ADest[n].A := 255;
  823. end;
  824. GL_GREEN, GL_GREEN_INTEGER:
  825. for n := 0 to AWidth*AHeight-1 do
  826. begin
  827. ADest[n].R := 0;
  828. ADest[n].G := GetChannel;
  829. ADest[n].B := 0;
  830. ADest[n].A := 255;
  831. end;
  832. GL_BLUE, GL_BLUE_INTEGER:
  833. for n := 0 to AWidth*AHeight-1 do
  834. begin
  835. ADest[n].R := 0;
  836. ADest[n].G := 0;
  837. ADest[n].B := GetChannel;
  838. ADest[n].A := 255;
  839. end;
  840. GL_RG, GL_RG_INTEGER:
  841. for n := 0 to AWidth*AHeight-1 do
  842. begin
  843. ADest[n].R := GetChannel;
  844. ADest[n].G := GetChannel;
  845. ADest[n].B := 0;
  846. ADest[n].A := 255;
  847. end;
  848. else
  849. raise EGLImageUtils.Create(strInvalidType);
  850. end;
  851. end;
  852. procedure FloatToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  853. var
  854. pSource: PSingle;
  855. n: Integer;
  856. c0: Single;
  857. function GetChannel: Single;
  858. begin
  859. Result := pSource^ * 255.0;
  860. Inc(pSource);
  861. end;
  862. begin
  863. pSource := PSingle(ASource);
  864. case AColorFormat of
  865. GL_RGB, GL_RGB_INTEGER:
  866. for n := 0 to AWidth*AHeight-1 do
  867. begin
  868. ADest[n].R := GetChannel;
  869. ADest[n].G := GetChannel;
  870. ADest[n].B := GetChannel;
  871. ADest[n].A := 255.0;
  872. end;
  873. GL_BGR, GL_BGR_INTEGER:
  874. for n := 0 to AWidth*AHeight-1 do
  875. begin
  876. ADest[n].B := GetChannel;
  877. ADest[n].G := GetChannel;
  878. ADest[n].R := GetChannel;
  879. ADest[n].A := 255.0;
  880. end;
  881. GL_RGBA, GL_RGBA_INTEGER:
  882. for n := 0 to AWidth*AHeight-1 do
  883. begin
  884. ADest[n].R := GetChannel;
  885. ADest[n].G := GetChannel;
  886. ADest[n].B := GetChannel;
  887. ADest[n].A := GetChannel;
  888. end;
  889. GL_BGRA, GL_BGRA_INTEGER:
  890. for n := 0 to AWidth*AHeight-1 do
  891. begin
  892. ADest[n].B := GetChannel;
  893. ADest[n].G := GetChannel;
  894. ADest[n].R := GetChannel;
  895. ADest[n].A := GetChannel;
  896. end;
  897. GL_ALPHA, GL_ALPHA_INTEGER:
  898. for n := 0 to AWidth*AHeight-1 do
  899. begin
  900. ADest[n].R := 0;
  901. ADest[n].G := 0;
  902. ADest[n].B := 0;
  903. ADest[n].A := GetChannel;
  904. end;
  905. GL_LUMINANCE, GL_LUMINANCE_INTEGER_EXT:
  906. for n := 0 to AWidth*AHeight-1 do
  907. begin
  908. c0 := GetChannel;
  909. ADest[n].R := c0;
  910. ADest[n].G := c0;
  911. ADest[n].B := c0;
  912. ADest[n].A := 255.0;
  913. end;
  914. GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA_INTEGER_EXT:
  915. for n := 0 to AWidth*AHeight-1 do
  916. begin
  917. c0 := GetChannel;
  918. ADest[n].R := c0;
  919. ADest[n].G := c0;
  920. ADest[n].B := c0;
  921. ADest[n].A := GetChannel;
  922. end;
  923. GL_INTENSITY:
  924. for n := 0 to AWidth*AHeight-1 do
  925. begin
  926. c0 := GetChannel;
  927. ADest[n].R := c0;
  928. ADest[n].G := c0;
  929. ADest[n].B := c0;
  930. ADest[n].A := c0;
  931. end;
  932. GL_RED, GL_RED_INTEGER:
  933. for n := 0 to AWidth*AHeight-1 do
  934. begin
  935. ADest[n].R := GetChannel;
  936. ADest[n].G := 0;
  937. ADest[n].B := 0;
  938. ADest[n].A := 255;
  939. end;
  940. GL_GREEN, GL_GREEN_INTEGER:
  941. for n := 0 to AWidth*AHeight-1 do
  942. begin
  943. ADest[n].R := 0;
  944. ADest[n].G := GetChannel;
  945. ADest[n].B := 0;
  946. ADest[n].A := 255;
  947. end;
  948. GL_BLUE, GL_BLUE_INTEGER:
  949. for n := 0 to AWidth*AHeight-1 do
  950. begin
  951. ADest[n].R := 0;
  952. ADest[n].G := 0;
  953. ADest[n].B := GetChannel;
  954. ADest[n].A := 255;
  955. end;
  956. GL_RG, GL_RG_INTEGER:
  957. for n := 0 to AWidth*AHeight-1 do
  958. begin
  959. ADest[n].R := GetChannel;
  960. ADest[n].G := GetChannel;
  961. ADest[n].B := 0;
  962. ADest[n].A := 255;
  963. end;
  964. else
  965. raise EGLImageUtils.Create(strInvalidType);
  966. end;
  967. end;
  968. procedure HalfFloatToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  969. var
  970. pSource: PHalfFloat;
  971. n: Integer;
  972. c0: Single;
  973. function GetChannel: Single;
  974. begin
  975. Result := HalfToFloat(pSource^) * 255.0;
  976. Inc(pSource);
  977. end;
  978. begin
  979. pSource := PHalfFloat(ASource);
  980. case AColorFormat of
  981. GL_RGB, GL_RGB_INTEGER:
  982. for n := 0 to AWidth*AHeight-1 do
  983. begin
  984. ADest[n].R := GetChannel;
  985. ADest[n].G := GetChannel;
  986. ADest[n].B := GetChannel;
  987. ADest[n].A := 255.0;
  988. end;
  989. GL_BGR, GL_BGR_INTEGER:
  990. for n := 0 to AWidth*AHeight-1 do
  991. begin
  992. ADest[n].B := GetChannel;
  993. ADest[n].G := GetChannel;
  994. ADest[n].R := GetChannel;
  995. ADest[n].A := 255.0;
  996. end;
  997. GL_RGBA, GL_RGBA_INTEGER:
  998. for n := 0 to AWidth*AHeight-1 do
  999. begin
  1000. ADest[n].R := GetChannel;
  1001. ADest[n].G := GetChannel;
  1002. ADest[n].B := GetChannel;
  1003. ADest[n].A := GetChannel;
  1004. end;
  1005. GL_BGRA, GL_BGRA_INTEGER:
  1006. for n := 0 to AWidth*AHeight-1 do
  1007. begin
  1008. ADest[n].B := GetChannel;
  1009. ADest[n].G := GetChannel;
  1010. ADest[n].R := GetChannel;
  1011. ADest[n].A := GetChannel;
  1012. end;
  1013. GL_ALPHA, GL_ALPHA_INTEGER:
  1014. for n := 0 to AWidth*AHeight-1 do
  1015. begin
  1016. ADest[n].R := 0;
  1017. ADest[n].G := 0;
  1018. ADest[n].B := 0;
  1019. ADest[n].A := GetChannel;
  1020. end;
  1021. GL_LUMINANCE, GL_LUMINANCE_INTEGER_EXT:
  1022. for n := 0 to AWidth*AHeight-1 do
  1023. begin
  1024. c0 := GetChannel;
  1025. ADest[n].R := c0;
  1026. ADest[n].G := c0;
  1027. ADest[n].B := c0;
  1028. ADest[n].A := 255.0;
  1029. end;
  1030. GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA_INTEGER_EXT:
  1031. for n := 0 to AWidth*AHeight-1 do
  1032. begin
  1033. c0 := GetChannel;
  1034. ADest[n].R := c0;
  1035. ADest[n].G := c0;
  1036. ADest[n].B := c0;
  1037. ADest[n].A := GetChannel;
  1038. end;
  1039. GL_INTENSITY:
  1040. for n := 0 to AWidth*AHeight-1 do
  1041. begin
  1042. c0 := GetChannel;
  1043. ADest[n].R := c0;
  1044. ADest[n].G := c0;
  1045. ADest[n].B := c0;
  1046. ADest[n].A := c0;
  1047. end;
  1048. GL_RED, GL_RED_INTEGER:
  1049. for n := 0 to AWidth*AHeight-1 do
  1050. begin
  1051. ADest[n].R := GetChannel;
  1052. ADest[n].G := 0;
  1053. ADest[n].B := 0;
  1054. ADest[n].A := 255;
  1055. end;
  1056. GL_GREEN, GL_GREEN_INTEGER:
  1057. for n := 0 to AWidth*AHeight-1 do
  1058. begin
  1059. ADest[n].R := 0;
  1060. ADest[n].G := GetChannel;
  1061. ADest[n].B := 0;
  1062. ADest[n].A := 255;
  1063. end;
  1064. GL_BLUE, GL_BLUE_INTEGER:
  1065. for n := 0 to AWidth*AHeight-1 do
  1066. begin
  1067. ADest[n].R := 0;
  1068. ADest[n].G := 0;
  1069. ADest[n].B := GetChannel;
  1070. ADest[n].A := 255;
  1071. end;
  1072. GL_RG, GL_RG_INTEGER:
  1073. for n := 0 to AWidth*AHeight-1 do
  1074. begin
  1075. ADest[n].R := GetChannel;
  1076. ADest[n].G := GetChannel;
  1077. ADest[n].B := 0;
  1078. ADest[n].A := 255;
  1079. end;
  1080. else
  1081. raise EGLImageUtils.Create(strInvalidType);
  1082. end;
  1083. end;
  1084. procedure UInt8888ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1085. var
  1086. pSource: PByte;
  1087. n: Integer;
  1088. c0, c1, c2, c3: Byte;
  1089. procedure GetChannel;
  1090. begin
  1091. c0 := pSource^;
  1092. Inc(pSource);
  1093. c1 := pSource^;
  1094. Inc(pSource);
  1095. c2 := pSource^;
  1096. Inc(pSource);
  1097. c3 := pSource^;
  1098. Inc(pSource);
  1099. end;
  1100. begin
  1101. pSource := PByte(ASource);
  1102. case AColorFormat of
  1103. GL_RGBA, GL_RGBA_INTEGER:
  1104. for n := 0 to AWidth * AHeight - 1 do
  1105. begin
  1106. GetChannel;
  1107. ADest[n].R := c0;
  1108. ADest[n].G := c1;
  1109. ADest[n].B := c2;
  1110. ADest[n].A := c3;
  1111. end;
  1112. GL_BGRA, GL_BGRA_INTEGER:
  1113. for n := 0 to AWidth * AHeight - 1 do
  1114. begin
  1115. GetChannel;
  1116. ADest[n].B := c0;
  1117. ADest[n].G := c1;
  1118. ADest[n].R := c2;
  1119. ADest[n].A := c3;
  1120. end;
  1121. else
  1122. raise EGLImageUtils.Create(strInvalidType);
  1123. end;
  1124. end;
  1125. procedure UInt8888RevToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1126. var
  1127. pSource: PByte;
  1128. n: Integer;
  1129. c0, c1, c2, c3: Byte;
  1130. procedure GetChannel;
  1131. begin
  1132. c3 := pSource^;
  1133. Inc(pSource);
  1134. c2 := pSource^;
  1135. Inc(pSource);
  1136. c1 := pSource^;
  1137. Inc(pSource);
  1138. c0 := pSource^;
  1139. Inc(pSource);
  1140. end;
  1141. begin
  1142. pSource := PByte(ASource);
  1143. case AColorFormat of
  1144. GL_RGBA, GL_RGBA_INTEGER:
  1145. for n := 0 to AWidth * AHeight - 1 do
  1146. begin
  1147. GetChannel;
  1148. ADest[n].R := c0;
  1149. ADest[n].G := c1;
  1150. ADest[n].B := c2;
  1151. ADest[n].A := c3;
  1152. end;
  1153. GL_BGRA, GL_BGRA_INTEGER:
  1154. for n := 0 to AWidth * AHeight - 1 do
  1155. begin
  1156. GetChannel;
  1157. ADest[n].B := c0;
  1158. ADest[n].G := c1;
  1159. ADest[n].R := c2;
  1160. ADest[n].A := c3;
  1161. end;
  1162. else
  1163. raise EGLImageUtils.Create(strInvalidType);
  1164. end;
  1165. end;
  1166. procedure UShort4444ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1167. var
  1168. pSource: PByte;
  1169. n: Integer;
  1170. c0, c1, c2, c3, c4: Byte;
  1171. procedure GetChannel;
  1172. begin
  1173. c0 := pSource^;
  1174. c3 := $F0 and (c0 shl 4);
  1175. c4 := $F0 and c0;
  1176. Inc(pSource);
  1177. c0 := pSource^;
  1178. c1 := $F0 and (c0 shl 4);
  1179. c2 := $F0 and c0;
  1180. Inc(pSource);
  1181. end;
  1182. begin
  1183. pSource := PByte(ASource);
  1184. case AColorFormat of
  1185. GL_RGBA, GL_RGBA_INTEGER:
  1186. for n := 0 to AWidth * AHeight - 1 do
  1187. begin
  1188. GetChannel;
  1189. ADest[n].R := c1;
  1190. ADest[n].G := c2;
  1191. ADest[n].B := c3;
  1192. ADest[n].A := c4;
  1193. end;
  1194. GL_BGRA, GL_BGRA_INTEGER:
  1195. for n := 0 to AWidth * AHeight - 1 do
  1196. begin
  1197. GetChannel;
  1198. ADest[n].R := c1;
  1199. ADest[n].G := c2;
  1200. ADest[n].B := c3;
  1201. ADest[n].A := c4;
  1202. end;
  1203. else
  1204. raise EGLImageUtils.Create(strInvalidType);
  1205. end;
  1206. end;
  1207. procedure UShort4444RevToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1208. var
  1209. pSource: PByte;
  1210. n: Integer;
  1211. c0, c1, c2, c3, c4: Byte;
  1212. procedure GetChannel;
  1213. begin
  1214. c0 := pSource^;
  1215. c1 := $F0 and (c0 shl 4);
  1216. c2 := $F0 and c0;
  1217. Inc(pSource);
  1218. c0 := pSource^;
  1219. c3 := $F0 and (c0 shl 4);
  1220. c4 := $F0 and c0;
  1221. Inc(pSource);
  1222. end;
  1223. begin
  1224. pSource := PByte(ASource);
  1225. case AColorFormat of
  1226. GL_RGBA, GL_RGBA_INTEGER:
  1227. for n := 0 to AWidth * AHeight - 1 do
  1228. begin
  1229. GetChannel;
  1230. ADest[n].R := c1;
  1231. ADest[n].G := c2;
  1232. ADest[n].B := c3;
  1233. ADest[n].A := c4;
  1234. end;
  1235. GL_BGRA, GL_BGRA_INTEGER:
  1236. for n := 0 to AWidth * AHeight - 1 do
  1237. begin
  1238. GetChannel;
  1239. ADest[n].B := c1;
  1240. ADest[n].G := c2;
  1241. ADest[n].R := c3;
  1242. ADest[n].A := c4;
  1243. end;
  1244. else
  1245. raise EGLImageUtils.Create(strInvalidType);
  1246. end;
  1247. end;
  1248. procedure UShort565ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1249. var
  1250. pSource: PWord;
  1251. n: Integer;
  1252. c0: Word;
  1253. c1, c2, c3: Byte;
  1254. procedure GetChannel;
  1255. begin
  1256. c0 := pSource^;
  1257. c3 := (c0 and $001F) shl 3;
  1258. c2 := (c0 and $07E0) shr 3;
  1259. c1 := (c0 and $F800) shr 8;
  1260. Inc(pSource);
  1261. end;
  1262. begin
  1263. pSource := PWord(ASource);
  1264. case AColorFormat of
  1265. GL_RGB, GL_RGB_INTEGER:
  1266. for n := 0 to AWidth * AHeight - 1 do
  1267. begin
  1268. GetChannel;
  1269. ADest[n].R := c1;
  1270. ADest[n].G := c2;
  1271. ADest[n].B := c3;
  1272. end;
  1273. GL_BGR, GL_BGR_INTEGER:
  1274. for n := 0 to AWidth * AHeight - 1 do
  1275. begin
  1276. GetChannel;
  1277. ADest[n].B := c1;
  1278. ADest[n].G := c2;
  1279. ADest[n].R := c3;
  1280. end;
  1281. else
  1282. raise EGLImageUtils.Create(strInvalidType);
  1283. end;
  1284. end;
  1285. procedure UShort565RevToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1286. var
  1287. pSource: PWord;
  1288. n: Integer;
  1289. c0: Word;
  1290. c1, c2, c3: Byte;
  1291. procedure GetChannel;
  1292. begin
  1293. c0 := pSource^;
  1294. c1 := (c0 and $001F) shl 3;
  1295. c2 := (c0 and $07E0) shr 3;
  1296. c3 := (c0 and $F800) shr 8;
  1297. Inc(pSource);
  1298. end;
  1299. begin
  1300. pSource := PWord(ASource);
  1301. case AColorFormat of
  1302. GL_RGB, GL_RGB_INTEGER:
  1303. for n := 0 to AWidth * AHeight - 1 do
  1304. begin
  1305. GetChannel;
  1306. ADest[n].R := c1;
  1307. ADest[n].G := c2;
  1308. ADest[n].B := c3;
  1309. end;
  1310. GL_BGR, GL_BGR_INTEGER:
  1311. for n := 0 to AWidth * AHeight - 1 do
  1312. begin
  1313. GetChannel;
  1314. ADest[n].B := c1;
  1315. ADest[n].G := c2;
  1316. ADest[n].R := c3;
  1317. end;
  1318. else
  1319. raise EGLImageUtils.Create(strInvalidType);
  1320. end;
  1321. end;
  1322. procedure UShort5551ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1323. var
  1324. pSource: PWord;
  1325. n: Integer;
  1326. c0: Word;
  1327. c1, c2, c3, c4: Byte;
  1328. procedure GetChannel;
  1329. begin
  1330. c0 := pSource^;
  1331. c4 := (c0 and $001F) shl 3;
  1332. c3 := (c0 and $03E0) shr 2;
  1333. c2 := (c0 and $7C00) shr 7;
  1334. c1 := (c0 and $8000) shr 8;
  1335. Inc(pSource);
  1336. end;
  1337. begin
  1338. pSource := PWord(ASource);
  1339. case AColorFormat of
  1340. GL_RGBA, GL_RGBA_INTEGER:
  1341. for n := 0 to AWidth * AHeight - 1 do
  1342. begin
  1343. GetChannel;
  1344. ADest[n].R := c1;
  1345. ADest[n].G := c2;
  1346. ADest[n].B := c3;
  1347. ADest[n].A := c4;
  1348. end;
  1349. GL_BGRA, GL_BGRA_INTEGER:
  1350. for n := 0 to AWidth * AHeight - 1 do
  1351. begin
  1352. GetChannel;
  1353. ADest[n].B := c1;
  1354. ADest[n].G := c2;
  1355. ADest[n].R := c3;
  1356. ADest[n].A := c4;
  1357. end;
  1358. else
  1359. raise EGLImageUtils.Create(strInvalidType);
  1360. end;
  1361. end;
  1362. procedure UShort5551RevToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1363. var
  1364. pSource: PWord;
  1365. n: Integer;
  1366. c0: Word;
  1367. c1, c2, c3, c4: Byte;
  1368. procedure GetChannel;
  1369. begin
  1370. c0 := pSource^;
  1371. c1 := (c0 and $001F) shl 3;
  1372. c2 := (c0 and $03E0) shr 2;
  1373. c3 := (c0 and $7C00) shr 7;
  1374. c4 := (c0 and $8000) shr 8;
  1375. Inc(pSource);
  1376. end;
  1377. begin
  1378. pSource := PWord(ASource);
  1379. case AColorFormat of
  1380. GL_RGBA, GL_RGBA_INTEGER:
  1381. for n := 0 to AWidth * AHeight - 1 do
  1382. begin
  1383. GetChannel;
  1384. ADest[n].R := c1;
  1385. ADest[n].G := c2;
  1386. ADest[n].B := c3;
  1387. ADest[n].A := c4;
  1388. end;
  1389. GL_BGRA, GL_BGRA_INTEGER:
  1390. for n := 0 to AWidth * AHeight - 1 do
  1391. begin
  1392. GetChannel;
  1393. ADest[n].B := c1;
  1394. ADest[n].G := c2;
  1395. ADest[n].R := c3;
  1396. ADest[n].A := c4;
  1397. end;
  1398. else
  1399. raise EGLImageUtils.Create(strInvalidType);
  1400. end;
  1401. end;
  1402. procedure UInt_10_10_10_2_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1403. var
  1404. pSource: PLongWord;
  1405. n: Integer;
  1406. c0: LongWord;
  1407. c1, c2, c3, c4: Word;
  1408. procedure GetChannel;
  1409. begin
  1410. c0 := pSource^;
  1411. c1 := (c0 and $000003FF) shl 6;
  1412. c2 := (c0 and $000FFC00) shr 4;
  1413. c3 := (c0 and $3FF00000) shr 14;
  1414. c4 := (c0 and $C0000000) shr 16;
  1415. Inc(pSource);
  1416. end;
  1417. begin
  1418. pSource := PLongWord(ASource);
  1419. case AColorFormat of
  1420. GL_RGBA, GL_RGBA_INTEGER:
  1421. for n := 0 to AWidth * AHeight - 1 do
  1422. begin
  1423. GetChannel;
  1424. ADest[n].R := c1 / $100;
  1425. ADest[n].G := c2 / $100;
  1426. ADest[n].B := c3 / $100;
  1427. ADest[n].A := c4;
  1428. end;
  1429. GL_BGRA, GL_BGRA_INTEGER:
  1430. for n := 0 to AWidth * AHeight - 1 do
  1431. begin
  1432. GetChannel;
  1433. ADest[n].B := c1 / $100;
  1434. ADest[n].G := c2 / $100;
  1435. ADest[n].R := c3 / $100;
  1436. ADest[n].A := c4;
  1437. end;
  1438. else
  1439. raise EGLImageUtils.Create(strInvalidType);
  1440. end;
  1441. end;
  1442. procedure UInt_10_10_10_2_Rev_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1443. var
  1444. pSource: PLongWord;
  1445. n: Integer;
  1446. c0: LongWord;
  1447. c1, c2, c3, c4: Word;
  1448. procedure GetChannel;
  1449. begin
  1450. c0 := pSource^;
  1451. c1 := (c0 and $000003FF) shl 6;
  1452. c2 := (c0 and $000FFC00) shr 4;
  1453. c3 := (c0 and $3FF00000) shr 14;
  1454. c4 := (c0 and $C0000000) shr 16;
  1455. Inc(pSource);
  1456. end;
  1457. begin
  1458. pSource := PLongWord(ASource);
  1459. case AColorFormat of
  1460. GL_RGBA, GL_RGBA_INTEGER:
  1461. for n := 0 to AWidth * AHeight - 1 do
  1462. begin
  1463. GetChannel;
  1464. ADest[n].R := c1 / $100;
  1465. ADest[n].G := c2 / $100;
  1466. ADest[n].B := c3 / $100;
  1467. ADest[n].A := c4;
  1468. end;
  1469. GL_BGRA, GL_BGRA_INTEGER:
  1470. for n := 0 to AWidth * AHeight - 1 do
  1471. begin
  1472. GetChannel;
  1473. ADest[n].B := c1 / $100;
  1474. ADest[n].G := c2 / $100;
  1475. ADest[n].R := c3 / $100;
  1476. ADest[n].A := c4;
  1477. end;
  1478. else
  1479. raise EGLImageUtils.Create(strInvalidType);
  1480. end;
  1481. end;
  1482. // ------------------------------ Decompression
  1483. procedure DecodeColor565(col: Word; out R, G, B: Byte);
  1484. begin
  1485. R := col and $1F;
  1486. G := (col shr 5) and $3F;
  1487. B := (col shr 11) and $1F;
  1488. end;
  1489. procedure DXT1_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1490. var
  1491. x, y, i, j, k, select, offset: Integer;
  1492. col0, col1: Word;
  1493. colors: TU48BitBlock;
  1494. bitmask: Cardinal;
  1495. temp: PGLubyte;
  1496. r0, g0, b0, r1, g1, b1: Byte;
  1497. begin
  1498. temp := PGLubyte(ASource);
  1499. for y := 0 to (AHeight div 4) - 1 do
  1500. begin
  1501. for x := 0 to (AWidth div 4) - 1 do
  1502. begin
  1503. col0 := PWord(temp)^;
  1504. Inc(temp, 2);
  1505. col1 := PWord(temp)^;
  1506. Inc(temp, 2);
  1507. bitmask := PCardinal(temp)^;
  1508. Inc(temp, 4);
  1509. DecodeColor565(col0, r0, g0, b0);
  1510. DecodeColor565(col1, r1, g1, b1);
  1511. colors[0][0] := r0 shl 3;
  1512. colors[0][1] := g0 shl 2;
  1513. colors[0][2] := b0 shl 3;
  1514. colors[0][3] := $FF;
  1515. colors[1][0] := r1 shl 3;
  1516. colors[1][1] := g1 shl 2;
  1517. colors[1][2] := b1 shl 3;
  1518. colors[1][3] := $FF;
  1519. if col0 > col1 then
  1520. begin
  1521. colors[2][0] := (2 * colors[0][0] + colors[1][0] + 1) div 3;
  1522. colors[2][1] := (2 * colors[0][1] + colors[1][1] + 1) div 3;
  1523. colors[2][2] := (2 * colors[0][2] + colors[1][2] + 1) div 3;
  1524. colors[2][3] := $FF;
  1525. colors[3][0] := (colors[0][0] + 2 * colors[1][0] + 1) div 3;
  1526. colors[3][1] := (colors[0][1] + 2 * colors[1][1] + 1) div 3;
  1527. colors[3][2] := (colors[0][2] + 2 * colors[1][2] + 1) div 3;
  1528. colors[3][3] := $FF;
  1529. end
  1530. else
  1531. begin
  1532. colors[2][0] := (colors[0][0] + colors[1][0]) div 2;
  1533. colors[2][1] := (colors[0][1] + colors[1][1]) div 2;
  1534. colors[2][2] := (colors[0][2] + colors[1][2]) div 2;
  1535. colors[2][3] := $FF;
  1536. colors[3][0] := (colors[0][0] + 2 * colors[1][0] + 1) div 3;
  1537. colors[3][1] := (colors[0][1] + 2 * colors[1][1] + 1) div 3;
  1538. colors[3][2] := (colors[0][2] + 2 * colors[1][2] + 1) div 3;
  1539. colors[3][3] := 0;
  1540. end;
  1541. k := 0;
  1542. for j := 0 to 3 do
  1543. begin
  1544. for i := 0 to 3 do
  1545. begin
  1546. select := (bitmask and (3 shl (k * 2))) shr (k * 2);
  1547. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  1548. begin
  1549. offset := ((4 * y + j) * AWidth + (4 * x + i));
  1550. ADest[offset].B := colors[select][0];
  1551. ADest[offset].G := colors[select][1];
  1552. ADest[offset].R := colors[select][2];
  1553. ADest[offset].A := colors[select][3];
  1554. end;
  1555. Inc(k);
  1556. end;
  1557. end;
  1558. end;
  1559. end;
  1560. end;
  1561. procedure DXT3_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1562. var
  1563. x, y, i, j, k, select: Integer;
  1564. col0, col1, wrd: Word;
  1565. colors: TU48BitBlock;
  1566. bitmask, offset: Cardinal;
  1567. temp: PGLubyte;
  1568. r0, g0, b0, r1, g1, b1: Byte;
  1569. alpha: array [0 .. 3] of Word;
  1570. begin
  1571. temp := PGLubyte(ASource);
  1572. for y := 0 to (AHeight div 4) - 1 do
  1573. begin
  1574. for x := 0 to (AWidth div 4) - 1 do
  1575. begin
  1576. alpha[0] := PWord(temp)^;
  1577. Inc(temp, 2);
  1578. alpha[1] := PWord(temp)^;
  1579. Inc(temp, 2);
  1580. alpha[2] := PWord(temp)^;
  1581. Inc(temp, 2);
  1582. alpha[3] := PWord(temp)^;
  1583. Inc(temp, 2);
  1584. col0 := PWord(temp)^;
  1585. Inc(temp, 2);
  1586. col1 := PWord(temp)^;
  1587. Inc(temp, 2);
  1588. bitmask := PCardinal(temp)^;
  1589. Inc(temp, 4);
  1590. DecodeColor565(col0, r0, g0, b0);
  1591. DecodeColor565(col1, r1, g1, b1);
  1592. colors[0][0] := r0 shl 3;
  1593. colors[0][1] := g0 shl 2;
  1594. colors[0][2] := b0 shl 3;
  1595. colors[0][3] := $FF;
  1596. colors[1][0] := r1 shl 3;
  1597. colors[1][1] := g1 shl 2;
  1598. colors[1][2] := b1 shl 3;
  1599. colors[1][3] := $FF;
  1600. colors[2][0] := (2 * colors[0][0] + colors[1][0] + 1) div 3;
  1601. colors[2][1] := (2 * colors[0][1] + colors[1][1] + 1) div 3;
  1602. colors[2][2] := (2 * colors[0][2] + colors[1][2] + 1) div 3;
  1603. colors[2][3] := $FF;
  1604. colors[3][0] := (colors[0][0] + 2 * colors[1][0] + 1) div 3;
  1605. colors[3][1] := (colors[0][1] + 2 * colors[1][1] + 1) div 3;
  1606. colors[3][2] := (colors[0][2] + 2 * colors[1][2] + 1) div 3;
  1607. colors[3][3] := $FF;
  1608. k := 0;
  1609. for j := 0 to 3 do
  1610. begin
  1611. for i := 0 to 3 do
  1612. begin
  1613. select := (bitmask and (3 shl (k * 2))) shr (k * 2);
  1614. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  1615. begin
  1616. offset := ((4 * y + j) * AWidth + (4 * x + i));
  1617. ADest[offset].B := colors[select][0];
  1618. ADest[offset].G := colors[select][1];
  1619. ADest[offset].R := colors[select][2];
  1620. ADest[offset].A := colors[select][3];
  1621. end;
  1622. Inc(k);
  1623. end;
  1624. end;
  1625. for j := 0 to 3 do
  1626. begin
  1627. wrd := alpha[j];
  1628. for i := 0 to 3 do
  1629. begin
  1630. if (((4 * x + i) < AWidth) and ((4 * y + j) < AHeight)) then
  1631. begin
  1632. offset := ((4 * y + j) * AWidth + (4 * x + i));
  1633. r0 := wrd and $0F;
  1634. ADest[offset].A := r0 or (r0 shl 4);
  1635. end;
  1636. wrd := wrd shr 4;
  1637. end;
  1638. end;
  1639. end;
  1640. end;
  1641. end;
  1642. procedure DXT5_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1643. var
  1644. x, y, i, j, k, select, offset: Integer;
  1645. col0, col1: Word;
  1646. colors: TU48BitBlock;
  1647. bits, bitmask: Cardinal;
  1648. temp, alphamask: PGLubyte;
  1649. r0, g0, b0, r1, g1, b1: Byte;
  1650. alphas: array [0 .. 7] of Byte;
  1651. begin
  1652. temp := PGLubyte(ASource);
  1653. for y := 0 to (AHeight div 4) - 1 do
  1654. begin
  1655. for x := 0 to (AWidth div 4) - 1 do
  1656. begin
  1657. alphas[0] := temp^;
  1658. Inc(temp);
  1659. alphas[1] := temp^;
  1660. Inc(temp);
  1661. alphamask := temp;
  1662. Inc(temp, 6);
  1663. col0 := PWord(temp)^;
  1664. Inc(temp, 2);
  1665. col1 := PWord(temp)^;
  1666. Inc(temp, 2);
  1667. bitmask := PCardinal(temp)^;
  1668. Inc(temp, 4);
  1669. DecodeColor565(col0, r0, g0, b0);
  1670. DecodeColor565(col1, r1, g1, b1);
  1671. colors[0][0] := r0 shl 3;
  1672. colors[0][1] := g0 shl 2;
  1673. colors[0][2] := b0 shl 3;
  1674. colors[0][3] := $FF;
  1675. colors[1][0] := r1 shl 3;
  1676. colors[1][1] := g1 shl 2;
  1677. colors[1][2] := b1 shl 3;
  1678. colors[1][3] := $FF;
  1679. colors[2][0] := (2 * colors[0][0] + colors[1][0] + 1) div 3;
  1680. colors[2][1] := (2 * colors[0][1] + colors[1][1] + 1) div 3;
  1681. colors[2][2] := (2 * colors[0][2] + colors[1][2] + 1) div 3;
  1682. colors[2][3] := $FF;
  1683. colors[3][0] := (colors[0][0] + 2 * colors[1][0] + 1) div 3;
  1684. colors[3][1] := (colors[0][1] + 2 * colors[1][1] + 1) div 3;
  1685. colors[3][2] := (colors[0][2] + 2 * colors[1][2] + 1) div 3;
  1686. colors[3][3] := $FF;
  1687. k := 0;
  1688. for j := 0 to 3 do
  1689. begin
  1690. for i := 0 to 3 do
  1691. begin
  1692. select := (bitmask and (3 shl (k * 2))) shr (k * 2);
  1693. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  1694. begin
  1695. offset := ((4 * y + j) * AWidth + (4 * x + i));
  1696. ADest[offset].B := colors[select][0];
  1697. ADest[offset].G := colors[select][1];
  1698. ADest[offset].R := colors[select][2];
  1699. end;
  1700. Inc(k);
  1701. end;
  1702. end;
  1703. if (alphas[0] > alphas[1]) then
  1704. begin
  1705. alphas[2] := (6 * alphas[0] + 1 * alphas[1] + 3) div 7;
  1706. alphas[3] := (5 * alphas[0] + 2 * alphas[1] + 3) div 7;
  1707. alphas[4] := (4 * alphas[0] + 3 * alphas[1] + 3) div 7;
  1708. alphas[5] := (3 * alphas[0] + 4 * alphas[1] + 3) div 7;
  1709. alphas[6] := (2 * alphas[0] + 5 * alphas[1] + 3) div 7;
  1710. alphas[7] := (1 * alphas[0] + 6 * alphas[1] + 3) div 7;
  1711. end
  1712. else
  1713. begin
  1714. alphas[2] := (4 * alphas[0] + 1 * alphas[1] + 2) div 5;
  1715. alphas[3] := (3 * alphas[0] + 2 * alphas[1] + 2) div 5;
  1716. alphas[4] := (2 * alphas[0] + 3 * alphas[1] + 2) div 5;
  1717. alphas[5] := (1 * alphas[0] + 4 * alphas[1] + 2) div 5;
  1718. alphas[6] := 0;
  1719. alphas[7] := $FF;
  1720. end;
  1721. bits := PCardinal(alphamask)^;
  1722. for j := 0 to 1 do
  1723. begin
  1724. for i := 0 to 3 do
  1725. begin
  1726. if (((4 * x + i) < AWidth) and ((4 * y + j) < AHeight)) then
  1727. begin
  1728. offset := ((4 * y + j) * AWidth + (4 * x + i));
  1729. ADest[offset].A := alphas[bits and 7];
  1730. end;
  1731. bits := bits shr 3;
  1732. end;
  1733. end;
  1734. Inc(alphamask, 3);
  1735. bits := PCardinal(alphamask)^;
  1736. for j := 2 to 3 do
  1737. begin
  1738. for i := 0 to 3 do
  1739. begin
  1740. if (((4 * x + i) < AWidth) and ((4 * y + j) < AHeight)) then
  1741. begin
  1742. offset := ((4 * y + j) * AWidth + (4 * x + i));
  1743. ADest[offset].A := alphas[bits and 7];
  1744. end;
  1745. bits := bits shr 3;
  1746. end;
  1747. end;
  1748. end;
  1749. end;
  1750. end;
  1751. procedure Decode48BitBlock(ACode: Int64; out ABlock: TU48BitBlock); overload;
  1752. var
  1753. x, y: Byte;
  1754. begin
  1755. for y := 0 to 3 do
  1756. for x := 0 to 3 do
  1757. begin
  1758. ABlock[x][y] := Byte(ACode and $03);
  1759. ACode := ACode shr 2;
  1760. end;
  1761. end;
  1762. procedure Decode48BitBlock(ACode: Int64; out ABlock: T48BitBlock); overload;
  1763. var
  1764. x, y: Byte;
  1765. begin
  1766. for y := 0 to 3 do
  1767. for x := 0 to 3 do
  1768. begin
  1769. ABlock[x][y] := SmallInt(ACode and $03);
  1770. ACode := ACode shr 2;
  1771. end;
  1772. end;
  1773. procedure LATC1_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1774. var
  1775. x, y, i, j, offset: Integer;
  1776. LUM0, LUM1: Byte;
  1777. lum: Single;
  1778. colors: TU48BitBlock;
  1779. bitmask: Int64;
  1780. temp: PGLubyte;
  1781. begin
  1782. temp := PGLubyte(ASource);
  1783. for y := 0 to (AHeight div 4) - 1 do
  1784. begin
  1785. for x := 0 to (AWidth div 4) - 1 do
  1786. begin
  1787. LUM0 := temp^;
  1788. Inc(temp);
  1789. LUM1 := temp^;
  1790. Inc(temp);
  1791. bitmask := PInt64(temp)^;
  1792. Inc(temp, 6);
  1793. Decode48BitBlock(bitmask, colors);
  1794. for j := 0 to 3 do
  1795. begin
  1796. for i := 0 to 3 do
  1797. begin
  1798. if LUM0 > LUM1 then
  1799. case colors[j, i] of
  1800. 0:
  1801. colors[j, i] := LUM0;
  1802. 1:
  1803. colors[j, i] := LUM1;
  1804. 2:
  1805. colors[j, i] := (6 * LUM0 + LUM1) div 7;
  1806. 3:
  1807. colors[j, i] := (5 * LUM0 + 2 * LUM1) div 7;
  1808. 4:
  1809. colors[j, i] := (4 * LUM0 + 3 * LUM1) div 7;
  1810. 5:
  1811. colors[j, i] := (3 * LUM0 + 4 * LUM1) div 7;
  1812. 6:
  1813. colors[j, i] := (2 * LUM0 + 5 * LUM1) div 7;
  1814. 7:
  1815. colors[j, i] := (LUM0 + 6 * LUM1) div 7;
  1816. end
  1817. else
  1818. case colors[j, i] of
  1819. 0:
  1820. colors[j, i] := LUM0;
  1821. 1:
  1822. colors[j, i] := LUM1;
  1823. 2:
  1824. colors[j, i] := (4 * LUM0 + LUM1) div 5;
  1825. 3:
  1826. colors[j, i] := (3 * LUM0 + 2 * LUM1) div 5;
  1827. 4:
  1828. colors[j, i] := (2 * LUM0 + 3 * LUM1) div 5;
  1829. 5:
  1830. colors[j, i] := (LUM0 + 4 * LUM1) div 5;
  1831. 6:
  1832. colors[j, i] := 0;
  1833. 7:
  1834. colors[j, i] := 255;
  1835. end;
  1836. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  1837. begin
  1838. offset := ((4 * y + j) * AWidth + (4 * x + i));
  1839. lum := colors[j, i];
  1840. ADest[offset].R := lum;
  1841. ADest[offset].G := lum;
  1842. ADest[offset].B := lum;
  1843. ADest[offset].A := 255.0;
  1844. end;
  1845. end;
  1846. end;
  1847. end;
  1848. end;
  1849. end;
  1850. procedure SLATC1_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1851. var
  1852. x, y, i, j, offset: Integer;
  1853. LUM0, LUM1: SmallInt;
  1854. lum: Single;
  1855. colors: T48BitBlock;
  1856. bitmask: Int64;
  1857. temp: PGLubyte;
  1858. begin
  1859. temp := PGLubyte(ASource);
  1860. for y := 0 to (AHeight div 4) - 1 do
  1861. begin
  1862. for x := 0 to (AWidth div 4) - 1 do
  1863. begin
  1864. LUM0 := PSmallInt(temp)^;
  1865. Inc(temp);
  1866. LUM1 := PSmallInt(temp)^;
  1867. Inc(temp);
  1868. bitmask := PInt64(temp)^;
  1869. Inc(temp, 6);
  1870. Decode48BitBlock(bitmask, colors);
  1871. for j := 0 to 3 do
  1872. begin
  1873. for i := 0 to 3 do
  1874. begin
  1875. if LUM0 > LUM1 then
  1876. case colors[j, i] of
  1877. 0:
  1878. colors[j, i] := LUM0;
  1879. 1:
  1880. colors[j, i] := LUM1;
  1881. 2:
  1882. colors[j, i] := (6 * LUM0 + LUM1) div 7;
  1883. 3:
  1884. colors[j, i] := (5 * LUM0 + 2 * LUM1) div 7;
  1885. 4:
  1886. colors[j, i] := (4 * LUM0 + 3 * LUM1) div 7;
  1887. 5:
  1888. colors[j, i] := (3 * LUM0 + 4 * LUM1) div 7;
  1889. 6:
  1890. colors[j, i] := (2 * LUM0 + 5 * LUM1) div 7;
  1891. 7:
  1892. colors[j, i] := (LUM0 + 6 * LUM1) div 7;
  1893. end
  1894. else
  1895. case colors[j, i] of
  1896. 0:
  1897. colors[j, i] := LUM0;
  1898. 1:
  1899. colors[j, i] := LUM1;
  1900. 2:
  1901. colors[j, i] := (4 * LUM0 + LUM1) div 5;
  1902. 3:
  1903. colors[j, i] := (3 * LUM0 + 2 * LUM1) div 5;
  1904. 4:
  1905. colors[j, i] := (2 * LUM0 + 3 * LUM1) div 5;
  1906. 5:
  1907. colors[j, i] := (LUM0 + 4 * LUM1) div 5;
  1908. 6:
  1909. colors[j, i] := -127;
  1910. 7:
  1911. colors[j, i] := 127;
  1912. end;
  1913. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  1914. begin
  1915. offset := ((4 * y + j) * AWidth + (4 * x + i));
  1916. lum := 2 * colors[j, i];
  1917. ADest[offset].R := lum;
  1918. ADest[offset].G := lum;
  1919. ADest[offset].B := lum;
  1920. ADest[offset].A := 127.0;
  1921. end;
  1922. end;
  1923. end;
  1924. end;
  1925. end;
  1926. end;
  1927. procedure LATC2_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  1928. var
  1929. x, y, i, j, offset: Integer;
  1930. LUM0, LUM1: Byte;
  1931. lum: Single;
  1932. colors: TU48BitBlock;
  1933. bitmask: Int64;
  1934. temp: PGLubyte;
  1935. begin
  1936. temp := PGLubyte(ASource);
  1937. for y := 0 to (AHeight div 4) - 1 do
  1938. begin
  1939. for x := 0 to (AWidth div 4) - 1 do
  1940. begin
  1941. LUM0 := temp^;
  1942. Inc(temp);
  1943. LUM1 := temp^;
  1944. Inc(temp);
  1945. bitmask := PInt64(temp)^;
  1946. Inc(temp, 6);
  1947. Decode48BitBlock(bitmask, colors);
  1948. for j := 0 to 3 do
  1949. begin
  1950. for i := 0 to 3 do
  1951. begin
  1952. if LUM0 > LUM1 then
  1953. case colors[j, i] of
  1954. 0:
  1955. colors[j, i] := LUM0;
  1956. 1:
  1957. colors[j, i] := LUM1;
  1958. 2:
  1959. colors[j, i] := (6 * LUM0 + LUM1) div 7;
  1960. 3:
  1961. colors[j, i] := (5 * LUM0 + 2 * LUM1) div 7;
  1962. 4:
  1963. colors[j, i] := (4 * LUM0 + 3 * LUM1) div 7;
  1964. 5:
  1965. colors[j, i] := (3 * LUM0 + 4 * LUM1) div 7;
  1966. 6:
  1967. colors[j, i] := (2 * LUM0 + 5 * LUM1) div 7;
  1968. 7:
  1969. colors[j, i] := (LUM0 + 6 * LUM1) div 7;
  1970. end
  1971. else
  1972. case colors[j, i] of
  1973. 0:
  1974. colors[j, i] := LUM0;
  1975. 1:
  1976. colors[j, i] := LUM1;
  1977. 2:
  1978. colors[j, i] := (4 * LUM0 + LUM1) div 5;
  1979. 3:
  1980. colors[j, i] := (3 * LUM0 + 2 * LUM1) div 5;
  1981. 4:
  1982. colors[j, i] := (2 * LUM0 + 3 * LUM1) div 5;
  1983. 5:
  1984. colors[j, i] := (LUM0 + 4 * LUM1) div 5;
  1985. 6:
  1986. colors[j, i] := 0;
  1987. 7:
  1988. colors[j, i] := 255;
  1989. end;
  1990. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  1991. begin
  1992. offset := ((4 * y + j) * AWidth + (4 * x + i));
  1993. lum := colors[j][i];
  1994. ADest[offset].R := lum;
  1995. ADest[offset].G := lum;
  1996. ADest[offset].B := lum;
  1997. end;
  1998. end; // for i
  1999. end; // for j
  2000. LUM0 := temp^;
  2001. Inc(temp);
  2002. LUM1 := temp^;
  2003. Inc(temp);
  2004. bitmask := PInt64(temp)^;
  2005. Inc(temp, 6);
  2006. Decode48BitBlock(bitmask, colors);
  2007. for j := 0 to 3 do
  2008. begin
  2009. for i := 0 to 3 do
  2010. begin
  2011. if LUM0 > LUM1 then
  2012. case colors[j, i] of
  2013. 0:
  2014. colors[j, i] := LUM0;
  2015. 1:
  2016. colors[j, i] := LUM1;
  2017. 2:
  2018. colors[j, i] := (6 * LUM0 + LUM1) div 7;
  2019. 3:
  2020. colors[j, i] := (5 * LUM0 + 2 * LUM1) div 7;
  2021. 4:
  2022. colors[j, i] := (4 * LUM0 + 3 * LUM1) div 7;
  2023. 5:
  2024. colors[j, i] := (3 * LUM0 + 4 * LUM1) div 7;
  2025. 6:
  2026. colors[j, i] := (2 * LUM0 + 5 * LUM1) div 7;
  2027. 7:
  2028. colors[j, i] := (LUM0 + 6 * LUM1) div 7;
  2029. end
  2030. else
  2031. case colors[j, i] of
  2032. 0:
  2033. colors[j, i] := LUM0;
  2034. 1:
  2035. colors[j, i] := LUM1;
  2036. 2:
  2037. colors[j, i] := (4 * LUM0 + LUM1) div 5;
  2038. 3:
  2039. colors[j, i] := (3 * LUM0 + 2 * LUM1) div 5;
  2040. 4:
  2041. colors[j, i] := (2 * LUM0 + 3 * LUM1) div 5;
  2042. 5:
  2043. colors[j, i] := (LUM0 + 4 * LUM1) div 5;
  2044. 6:
  2045. colors[j, i] := 0;
  2046. 7:
  2047. colors[j, i] := 255;
  2048. end;
  2049. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  2050. ADest[((4 * y + j) * AWidth + (4 * x + i))].A := colors[j][i];
  2051. end;
  2052. end;
  2053. end;
  2054. end;
  2055. end;
  2056. procedure SLATC2_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  2057. var
  2058. x, y, i, j, offset: Integer;
  2059. LUM0, LUM1: SmallInt;
  2060. lum: Single;
  2061. colors: T48BitBlock;
  2062. bitmask: Int64;
  2063. temp: PGLubyte;
  2064. begin
  2065. temp := PGLubyte(ASource);
  2066. for y := 0 to (AHeight div 4) - 1 do
  2067. begin
  2068. for x := 0 to (AWidth div 4) - 1 do
  2069. begin
  2070. LUM0 := PSmallInt(temp)^;
  2071. Inc(temp);
  2072. LUM1 := PSmallInt(temp)^;
  2073. Inc(temp);
  2074. bitmask := PInt64(temp)^;
  2075. Inc(temp, 6);
  2076. Decode48BitBlock(bitmask, colors);
  2077. for j := 0 to 3 do
  2078. begin
  2079. for i := 0 to 3 do
  2080. begin
  2081. if LUM0 > LUM1 then
  2082. case colors[j, i] of
  2083. 0:
  2084. colors[j, i] := LUM0;
  2085. 1:
  2086. colors[j, i] := LUM1;
  2087. 2:
  2088. colors[j, i] := (6 * LUM0 + LUM1) div 7;
  2089. 3:
  2090. colors[j, i] := (5 * LUM0 + 2 * LUM1) div 7;
  2091. 4:
  2092. colors[j, i] := (4 * LUM0 + 3 * LUM1) div 7;
  2093. 5:
  2094. colors[j, i] := (3 * LUM0 + 4 * LUM1) div 7;
  2095. 6:
  2096. colors[j, i] := (2 * LUM0 + 5 * LUM1) div 7;
  2097. 7:
  2098. colors[j, i] := (LUM0 + 6 * LUM1) div 7;
  2099. end
  2100. else
  2101. case colors[j, i] of
  2102. 0:
  2103. colors[j, i] := LUM0;
  2104. 1:
  2105. colors[j, i] := LUM1;
  2106. 2:
  2107. colors[j, i] := (4 * LUM0 + LUM1) div 5;
  2108. 3:
  2109. colors[j, i] := (3 * LUM0 + 2 * LUM1) div 5;
  2110. 4:
  2111. colors[j, i] := (2 * LUM0 + 3 * LUM1) div 5;
  2112. 5:
  2113. colors[j, i] := (LUM0 + 4 * LUM1) div 5;
  2114. 6:
  2115. colors[j, i] := -127;
  2116. 7:
  2117. colors[j, i] := 127;
  2118. end;
  2119. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  2120. begin
  2121. offset := ((4 * y + j) * AWidth + (4 * x + i));
  2122. lum := 2 * colors[j][i];
  2123. ADest[offset].R := lum;
  2124. ADest[offset].G := lum;
  2125. ADest[offset].B := lum;
  2126. end;
  2127. end;
  2128. end;
  2129. LUM0 := PSmallInt(temp)^;
  2130. Inc(temp);
  2131. LUM1 := PSmallInt(temp)^;
  2132. Inc(temp);
  2133. bitmask := PInt64(temp)^;
  2134. Inc(temp, 6);
  2135. Decode48BitBlock(bitmask, colors);
  2136. for j := 0 to 3 do
  2137. begin
  2138. for i := 0 to 3 do
  2139. begin
  2140. if LUM0 > LUM1 then
  2141. case colors[j, i] of
  2142. 0:
  2143. colors[j, i] := LUM0;
  2144. 1:
  2145. colors[j, i] := LUM1;
  2146. 2:
  2147. colors[j, i] := (6 * LUM0 + LUM1) div 7;
  2148. 3:
  2149. colors[j, i] := (5 * LUM0 + 2 * LUM1) div 7;
  2150. 4:
  2151. colors[j, i] := (4 * LUM0 + 3 * LUM1) div 7;
  2152. 5:
  2153. colors[j, i] := (3 * LUM0 + 4 * LUM1) div 7;
  2154. 6:
  2155. colors[j, i] := (2 * LUM0 + 5 * LUM1) div 7;
  2156. 7:
  2157. colors[j, i] := (LUM0 + 6 * LUM1) div 7;
  2158. end
  2159. else
  2160. case colors[j, i] of
  2161. 0:
  2162. colors[j, i] := LUM0;
  2163. 1:
  2164. colors[j, i] := LUM1;
  2165. 2:
  2166. colors[j, i] := (4 * LUM0 + LUM1) div 5;
  2167. 3:
  2168. colors[j, i] := (3 * LUM0 + 2 * LUM1) div 5;
  2169. 4:
  2170. colors[j, i] := (2 * LUM0 + 3 * LUM1) div 5;
  2171. 5:
  2172. colors[j, i] := (LUM0 + 4 * LUM1) div 5;
  2173. 6:
  2174. colors[j, i] := -127;
  2175. 7:
  2176. colors[j, i] := 127;
  2177. end;
  2178. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  2179. begin
  2180. ADest[((4 * y + j) * AWidth + (4 * x + i))].A := 2 * colors[j][i];
  2181. end;
  2182. end;
  2183. end;
  2184. end;
  2185. end;
  2186. end;
  2187. procedure RGTC1_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  2188. var
  2189. x, y, i, j, offset: Integer;
  2190. RED0, RED1: Byte;
  2191. lum: Single;
  2192. colors: TU48BitBlock;
  2193. bitmask: Int64;
  2194. temp: PGLubyte;
  2195. begin
  2196. temp := PGLubyte(ASource);
  2197. for y := 0 to (AHeight div 4) - 1 do
  2198. begin
  2199. for x := 0 to (AWidth div 4) - 1 do
  2200. begin
  2201. RED0 := temp^;
  2202. Inc(temp);
  2203. RED1 := temp^;
  2204. Inc(temp);
  2205. bitmask := PInt64(temp)^;
  2206. Inc(temp, 6);
  2207. Decode48BitBlock(bitmask, colors);
  2208. for j := 0 to 3 do
  2209. begin
  2210. for i := 0 to 3 do
  2211. begin
  2212. if RED0 > RED1 then
  2213. case colors[j, i] of
  2214. 0:
  2215. colors[j, i] := RED0;
  2216. 1:
  2217. colors[j, i] := RED1;
  2218. 2:
  2219. colors[j, i] := (6 * RED0 + RED1) div 7;
  2220. 3:
  2221. colors[j, i] := (5 * RED0 + 2 * RED1) div 7;
  2222. 4:
  2223. colors[j, i] := (4 * RED0 + 3 * RED1) div 7;
  2224. 5:
  2225. colors[j, i] := (3 * RED0 + 4 * RED1) div 7;
  2226. 6:
  2227. colors[j, i] := (2 * RED0 + 5 * RED1) div 7;
  2228. 7:
  2229. colors[j, i] := (RED0 + 6 * RED1) div 7;
  2230. end
  2231. else
  2232. case colors[j, i] of
  2233. 0:
  2234. colors[j, i] := RED0;
  2235. 1:
  2236. colors[j, i] := RED1;
  2237. 2:
  2238. colors[j, i] := (4 * RED0 + RED1) div 5;
  2239. 3:
  2240. colors[j, i] := (3 * RED0 + 2 * RED1) div 5;
  2241. 4:
  2242. colors[j, i] := (2 * RED0 + 3 * RED1) div 5;
  2243. 5:
  2244. colors[j, i] := (RED0 + 4 * RED1) div 5;
  2245. 6:
  2246. colors[j, i] := 0;
  2247. 7:
  2248. colors[j, i] := 255;
  2249. end;
  2250. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  2251. begin
  2252. offset := ((4 * y + j) * AWidth + (4 * x + i)) * 4;
  2253. lum := colors[j][i];
  2254. ADest[offset].R := lum;
  2255. ADest[offset].G := 0.0;
  2256. ADest[offset].B := 0.0;
  2257. ADest[offset].A := 255.0;
  2258. end;
  2259. end;
  2260. end;
  2261. end;
  2262. end;
  2263. end;
  2264. procedure SRGTC1_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  2265. var
  2266. x, y, i, j, offset: Integer;
  2267. RED0, RED1: SmallInt;
  2268. lum: Single;
  2269. colors: T48BitBlock;
  2270. bitmask: Int64;
  2271. temp: PGLubyte;
  2272. begin
  2273. temp := PGLubyte(ASource);
  2274. for y := 0 to (AHeight div 4) - 1 do
  2275. begin
  2276. for x := 0 to (AWidth div 4) - 1 do
  2277. begin
  2278. RED0 := PSmallInt(temp)^;
  2279. Inc(temp);
  2280. RED1 := PSmallInt(temp)^;
  2281. Inc(temp);
  2282. bitmask := PInt64(temp)^;
  2283. Inc(temp, 6);
  2284. Decode48BitBlock(bitmask, colors);
  2285. for j := 0 to 3 do
  2286. begin
  2287. for i := 0 to 3 do
  2288. begin
  2289. if RED0 > RED1 then
  2290. case colors[j, i] of
  2291. 0:
  2292. colors[j, i] := RED0;
  2293. 1:
  2294. colors[j, i] := RED1;
  2295. 2:
  2296. colors[j, i] := (6 * RED0 + RED1) div 7;
  2297. 3:
  2298. colors[j, i] := (5 * RED0 + 2 * RED1) div 7;
  2299. 4:
  2300. colors[j, i] := (4 * RED0 + 3 * RED1) div 7;
  2301. 5:
  2302. colors[j, i] := (3 * RED0 + 4 * RED1) div 7;
  2303. 6:
  2304. colors[j, i] := (2 * RED0 + 5 * RED1) div 7;
  2305. 7:
  2306. colors[j, i] := (RED0 + 6 * RED1) div 7;
  2307. end
  2308. else
  2309. case colors[j, i] of
  2310. 0:
  2311. colors[j, i] := RED0;
  2312. 1:
  2313. colors[j, i] := RED1;
  2314. 2:
  2315. colors[j, i] := (4 * RED0 + RED1) div 5;
  2316. 3:
  2317. colors[j, i] := (3 * RED0 + 2 * RED1) div 5;
  2318. 4:
  2319. colors[j, i] := (2 * RED0 + 3 * RED1) div 5;
  2320. 5:
  2321. colors[j, i] := (RED0 + 4 * RED1) div 5;
  2322. 6:
  2323. colors[j, i] := -127;
  2324. 7:
  2325. colors[j, i] := 127;
  2326. end;
  2327. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  2328. begin
  2329. offset := ((4 * y + j) * AWidth + (4 * x + i));
  2330. lum := 2 * colors[j][i];
  2331. ADest[offset].R := lum;
  2332. ADest[offset].G := 0.0;
  2333. ADest[offset].B := 0.0;
  2334. ADest[offset].A := 127.0;
  2335. end;
  2336. end;
  2337. end;
  2338. end;
  2339. end;
  2340. end;
  2341. procedure RGTC2_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  2342. var
  2343. x, y, i, j, offset: Integer;
  2344. RED0, RED1: Byte;
  2345. colors: TU48BitBlock;
  2346. bitmask: Int64;
  2347. temp: PGLubyte;
  2348. begin
  2349. temp := PGLubyte(ASource);
  2350. for y := 0 to (AHeight div 4) - 1 do
  2351. begin
  2352. for x := 0 to (AWidth div 4) - 1 do
  2353. begin
  2354. RED0 := temp^;
  2355. Inc(temp);
  2356. RED1 := temp^;
  2357. Inc(temp);
  2358. bitmask := PInt64(temp)^;
  2359. Inc(temp, 6);
  2360. Decode48BitBlock(bitmask, colors);
  2361. for j := 0 to 3 do
  2362. begin
  2363. for i := 0 to 3 do
  2364. begin
  2365. if RED0 > RED1 then
  2366. case colors[j, i] of
  2367. 0:
  2368. colors[j, i] := RED0;
  2369. 1:
  2370. colors[j, i] := RED1;
  2371. 2:
  2372. colors[j, i] := (6 * RED0 + RED1) div 7;
  2373. 3:
  2374. colors[j, i] := (5 * RED0 + 2 * RED1) div 7;
  2375. 4:
  2376. colors[j, i] := (4 * RED0 + 3 * RED1) div 7;
  2377. 5:
  2378. colors[j, i] := (3 * RED0 + 4 * RED1) div 7;
  2379. 6:
  2380. colors[j, i] := (2 * RED0 + 5 * RED1) div 7;
  2381. 7:
  2382. colors[j, i] := (RED0 + 6 * RED1) div 7;
  2383. end
  2384. else
  2385. case colors[j, i] of
  2386. 0:
  2387. colors[j, i] := RED0;
  2388. 1:
  2389. colors[j, i] := RED1;
  2390. 2:
  2391. colors[j, i] := (4 * RED0 + RED1) div 5;
  2392. 3:
  2393. colors[j, i] := (3 * RED0 + 2 * RED1) div 5;
  2394. 4:
  2395. colors[j, i] := (2 * RED0 + 3 * RED1) div 5;
  2396. 5:
  2397. colors[j, i] := (RED0 + 4 * RED1) div 5;
  2398. 6:
  2399. colors[j, i] := 0;
  2400. 7:
  2401. colors[j, i] := 255;
  2402. end;
  2403. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  2404. begin
  2405. offset := ((4 * y + j) * AWidth + (4 * x + i));
  2406. ADest[offset].R := colors[j][i];
  2407. ADest[offset].B := 0.0;
  2408. end;
  2409. end;
  2410. end;
  2411. RED0 := temp^;
  2412. Inc(temp);
  2413. RED1 := temp^;
  2414. Inc(temp);
  2415. bitmask := PInt64(temp)^;
  2416. Inc(temp, 6);
  2417. Decode48BitBlock(bitmask, colors);
  2418. for j := 0 to 3 do
  2419. begin
  2420. for i := 0 to 3 do
  2421. begin
  2422. if RED0 > RED1 then
  2423. case colors[j, i] of
  2424. 0:
  2425. colors[j, i] := RED0;
  2426. 1:
  2427. colors[j, i] := RED1;
  2428. 2:
  2429. colors[j, i] := (6 * RED0 + RED1) div 7;
  2430. 3:
  2431. colors[j, i] := (5 * RED0 + 2 * RED1) div 7;
  2432. 4:
  2433. colors[j, i] := (4 * RED0 + 3 * RED1) div 7;
  2434. 5:
  2435. colors[j, i] := (3 * RED0 + 4 * RED1) div 7;
  2436. 6:
  2437. colors[j, i] := (2 * RED0 + 5 * RED1) div 7;
  2438. 7:
  2439. colors[j, i] := (RED0 + 6 * RED1) div 7;
  2440. end
  2441. else
  2442. case colors[j, i] of
  2443. 0:
  2444. colors[j, i] := RED0;
  2445. 1:
  2446. colors[j, i] := RED1;
  2447. 2:
  2448. colors[j, i] := (4 * RED0 + RED1) div 5;
  2449. 3:
  2450. colors[j, i] := (3 * RED0 + 2 * RED1) div 5;
  2451. 4:
  2452. colors[j, i] := (2 * RED0 + 3 * RED1) div 5;
  2453. 5:
  2454. colors[j, i] := (RED0 + 4 * RED1) div 5;
  2455. 6:
  2456. colors[j, i] := 0;
  2457. 7:
  2458. colors[j, i] := 255;
  2459. end;
  2460. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  2461. begin
  2462. offset := ((4 * y + j) * AWidth + (4 * x + i));
  2463. ADest[offset].G := colors[j][i];
  2464. ADest[offset].A := 255.0;
  2465. end;
  2466. end;
  2467. end;
  2468. end;
  2469. end;
  2470. end;
  2471. procedure SRGTC2_ToImf(ASource: Pointer; ADest: PIntermediateFormatArray; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  2472. var
  2473. x, y, i, j, offset: Integer;
  2474. RED0, RED1: SmallInt;
  2475. lum: Single;
  2476. colors: T48BitBlock;
  2477. bitmask: Int64;
  2478. temp: PGLubyte;
  2479. begin
  2480. temp := PGLubyte(ASource);
  2481. for y := 0 to (AHeight div 4) - 1 do
  2482. begin
  2483. for x := 0 to (AWidth div 4) - 1 do
  2484. begin
  2485. RED0 := PSmallInt(temp)^;
  2486. Inc(temp);
  2487. RED1 := PSmallInt(temp)^;
  2488. Inc(temp);
  2489. bitmask := PInt64(temp)^;
  2490. Inc(temp, 6);
  2491. Decode48BitBlock(bitmask, colors);
  2492. for j := 0 to 3 do
  2493. begin
  2494. for i := 0 to 3 do
  2495. begin
  2496. if RED0 > RED1 then
  2497. case colors[j, i] of
  2498. 0:
  2499. colors[j, i] := RED0;
  2500. 1:
  2501. colors[j, i] := RED1;
  2502. 2:
  2503. colors[j, i] := (6 * RED0 + RED1) div 7;
  2504. 3:
  2505. colors[j, i] := (5 * RED0 + 2 * RED1) div 7;
  2506. 4:
  2507. colors[j, i] := (4 * RED0 + 3 * RED1) div 7;
  2508. 5:
  2509. colors[j, i] := (3 * RED0 + 4 * RED1) div 7;
  2510. 6:
  2511. colors[j, i] := (2 * RED0 + 5 * RED1) div 7;
  2512. 7:
  2513. colors[j, i] := (RED0 + 6 * RED1) div 7;
  2514. end
  2515. else
  2516. case colors[j, i] of
  2517. 0:
  2518. colors[j, i] := RED0;
  2519. 1:
  2520. colors[j, i] := RED1;
  2521. 2:
  2522. colors[j, i] := (4 * RED0 + RED1) div 5;
  2523. 3:
  2524. colors[j, i] := (3 * RED0 + 2 * RED1) div 5;
  2525. 4:
  2526. colors[j, i] := (2 * RED0 + 3 * RED1) div 5;
  2527. 5:
  2528. colors[j, i] := (RED0 + 4 * RED1) div 5;
  2529. 6:
  2530. colors[j, i] := -127;
  2531. 7:
  2532. colors[j, i] := 127;
  2533. end;
  2534. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  2535. begin
  2536. offset := ((4 * y + j) * AWidth + (4 * x + i));
  2537. lum := 2 * colors[j][i];
  2538. ADest[offset].R := lum;
  2539. ADest[offset].B := 0.0;
  2540. end;
  2541. end;
  2542. end;
  2543. RED0 := PSmallInt(temp)^;
  2544. Inc(temp);
  2545. RED1 := PSmallInt(temp)^;
  2546. Inc(temp);
  2547. bitmask := PInt64(temp)^;
  2548. Inc(temp, 6);
  2549. Decode48BitBlock(bitmask, colors);
  2550. for j := 0 to 3 do
  2551. begin
  2552. for i := 0 to 3 do
  2553. begin
  2554. if RED0 > RED1 then
  2555. case colors[j, i] of
  2556. 0:
  2557. colors[j, i] := RED0;
  2558. 1:
  2559. colors[j, i] := RED1;
  2560. 2:
  2561. colors[j, i] := (6 * RED0 + RED1) div 7;
  2562. 3:
  2563. colors[j, i] := (5 * RED0 + 2 * RED1) div 7;
  2564. 4:
  2565. colors[j, i] := (4 * RED0 + 3 * RED1) div 7;
  2566. 5:
  2567. colors[j, i] := (3 * RED0 + 4 * RED1) div 7;
  2568. 6:
  2569. colors[j, i] := (2 * RED0 + 5 * RED1) div 7;
  2570. 7:
  2571. colors[j, i] := (RED0 + 6 * RED1) div 7;
  2572. end
  2573. else
  2574. case colors[j, i] of
  2575. 0:
  2576. colors[j, i] := RED0;
  2577. 1:
  2578. colors[j, i] := RED1;
  2579. 2:
  2580. colors[j, i] := (4 * RED0 + RED1) div 5;
  2581. 3:
  2582. colors[j, i] := (3 * RED0 + 2 * RED1) div 5;
  2583. 4:
  2584. colors[j, i] := (2 * RED0 + 3 * RED1) div 5;
  2585. 5:
  2586. colors[j, i] := (RED0 + 4 * RED1) div 5;
  2587. 6:
  2588. colors[j, i] := -127;
  2589. 7:
  2590. colors[j, i] := 127;
  2591. end;
  2592. if ((4 * x + i) < AWidth) and ((4 * y + j) < AHeight) then
  2593. begin
  2594. offset := ((4 * y + j) * AWidth + (4 * x + i));
  2595. lum := 2 * colors[j][i];
  2596. ADest[offset].G := lum;
  2597. ADest[offset].A := 127.0;
  2598. end;
  2599. end;
  2600. end;
  2601. end;
  2602. end;
  2603. end;
  2604. // ------------------------------ RGBA Float to OpenGL format image
  2605. procedure UnsupportedFromImf(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  2606. begin
  2607. raise EGLImageUtils.Create('Unimplemented type of conversion');
  2608. end;
  2609. procedure ImfToUbyte(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  2610. var
  2611. pDest: PByte;
  2612. n: Integer;
  2613. procedure SetChannel(AValue: Single);
  2614. begin
  2615. pDest^ := Trunc(ClampValue(AValue, 0.0, 255.0));
  2616. Inc(pDest);
  2617. end;
  2618. procedure SetChannelI(AValue: Single);
  2619. begin
  2620. pDest^ := Trunc(AValue);
  2621. Inc(pDest);
  2622. end;
  2623. begin
  2624. pDest := PByte(ADest);
  2625. case AColorFormat of
  2626. GL_RGB:
  2627. for n := 0 to AWidth*AHeight-1 do
  2628. begin
  2629. SetChannel(ASource[n].R);
  2630. SetChannel(ASource[n].G);
  2631. SetChannel(ASource[n].B);
  2632. end;
  2633. GL_RGB_INTEGER:
  2634. for n := 0 to AWidth*AHeight-1 do
  2635. begin
  2636. SetChannelI(ASource[n].R);
  2637. SetChannelI(ASource[n].G);
  2638. SetChannelI(ASource[n].B);
  2639. end;
  2640. GL_BGR:
  2641. for n := 0 to AWidth*AHeight-1 do
  2642. begin
  2643. SetChannel(ASource[n].B);
  2644. SetChannel(ASource[n].G);
  2645. SetChannel(ASource[n].R);
  2646. end;
  2647. GL_BGR_INTEGER:
  2648. for n := 0 to AWidth*AHeight-1 do
  2649. begin
  2650. SetChannelI(ASource[n].B);
  2651. SetChannelI(ASource[n].G);
  2652. SetChannelI(ASource[n].R);
  2653. end;
  2654. GL_RGBA:
  2655. for n := 0 to AWidth*AHeight-1 do
  2656. begin
  2657. SetChannel(ASource[n].R);
  2658. SetChannel(ASource[n].G);
  2659. SetChannel(ASource[n].B);
  2660. SetChannel(ASource[n].A);
  2661. end;
  2662. GL_RGBA_INTEGER:
  2663. for n := 0 to AWidth*AHeight-1 do
  2664. begin
  2665. SetChannelI(ASource[n].R);
  2666. SetChannelI(ASource[n].G);
  2667. SetChannelI(ASource[n].B);
  2668. SetChannelI(ASource[n].A);
  2669. end;
  2670. GL_BGRA:
  2671. for n := 0 to AWidth*AHeight-1 do
  2672. begin
  2673. SetChannel(ASource[n].B);
  2674. SetChannel(ASource[n].G);
  2675. SetChannel(ASource[n].R);
  2676. SetChannel(ASource[n].A);
  2677. end;
  2678. GL_BGRA_INTEGER:
  2679. for n := 0 to AWidth*AHeight-1 do
  2680. begin
  2681. SetChannelI(ASource[n].B);
  2682. SetChannelI(ASource[n].G);
  2683. SetChannelI(ASource[n].R);
  2684. SetChannelI(ASource[n].A);
  2685. end;
  2686. GL_ALPHA:
  2687. for n := 0 to AWidth*AHeight-1 do
  2688. begin
  2689. SetChannel(ASource[n].A);
  2690. end;
  2691. GL_ALPHA_INTEGER:
  2692. for n := 0 to AWidth*AHeight-1 do
  2693. begin
  2694. SetChannelI(ASource[n].A);
  2695. end;
  2696. GL_LUMINANCE:
  2697. for n := 0 to AWidth*AHeight-1 do
  2698. begin
  2699. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2700. end;
  2701. GL_LUMINANCE_INTEGER_EXT:
  2702. for n := 0 to AWidth*AHeight-1 do
  2703. begin
  2704. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2705. end;
  2706. GL_LUMINANCE_ALPHA:
  2707. for n := 0 to AWidth*AHeight-1 do
  2708. begin
  2709. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2710. SetChannel(ASource[n].A);
  2711. end;
  2712. GL_LUMINANCE_ALPHA_INTEGER_EXT:
  2713. for n := 0 to AWidth*AHeight-1 do
  2714. begin
  2715. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2716. SetChannelI(ASource[n].A);
  2717. end;
  2718. GL_INTENSITY:
  2719. for n := 0 to AWidth*AHeight-1 do
  2720. begin
  2721. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2722. end;
  2723. GL_RED:
  2724. for n := 0 to AWidth*AHeight-1 do
  2725. begin
  2726. SetChannel(ASource[n].R);
  2727. end;
  2728. GL_RED_INTEGER:
  2729. for n := 0 to AWidth*AHeight-1 do
  2730. begin
  2731. SetChannelI(ASource[n].R);
  2732. end;
  2733. GL_GREEN:
  2734. for n := 0 to AWidth*AHeight-1 do
  2735. begin
  2736. SetChannel(ASource[n].G);
  2737. end;
  2738. GL_GREEN_INTEGER:
  2739. for n := 0 to AWidth*AHeight-1 do
  2740. begin
  2741. SetChannelI(ASource[n].G);
  2742. end;
  2743. GL_BLUE:
  2744. for n := 0 to AWidth*AHeight-1 do
  2745. begin
  2746. SetChannel(ASource[n].B);
  2747. end;
  2748. GL_BLUE_INTEGER:
  2749. for n := 0 to AWidth*AHeight-1 do
  2750. begin
  2751. SetChannelI(ASource[n].B);
  2752. end;
  2753. GL_RG:
  2754. for n := 0 to AWidth*AHeight-1 do
  2755. begin
  2756. SetChannel(ASource[n].R);
  2757. SetChannel(ASource[n].G);
  2758. end;
  2759. GL_RG_INTEGER:
  2760. for n := 0 to AWidth*AHeight-1 do
  2761. begin
  2762. SetChannelI(ASource[n].R);
  2763. SetChannelI(ASource[n].G);
  2764. end;
  2765. else
  2766. raise EGLImageUtils.Create(strInvalidType);
  2767. end;
  2768. end;
  2769. procedure ImfToByte(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  2770. var
  2771. pDest: PShortInt;
  2772. n: Integer;
  2773. procedure SetChannel(AValue: Single);
  2774. begin
  2775. pDest^ := Trunc(ClampValue(AValue, -127.0, 127.0));
  2776. Inc(pDest);
  2777. end;
  2778. procedure SetChannelI(AValue: Single);
  2779. begin
  2780. pDest^ := Trunc(AValue);
  2781. Inc(pDest);
  2782. end;
  2783. begin
  2784. pDest := PShortInt(ADest);
  2785. case AColorFormat of
  2786. GL_RGB:
  2787. for n := 0 to AWidth*AHeight-1 do
  2788. begin
  2789. SetChannel(ASource[n].R);
  2790. SetChannel(ASource[n].G);
  2791. SetChannel(ASource[n].B);
  2792. end;
  2793. GL_RGB_INTEGER:
  2794. for n := 0 to AWidth*AHeight-1 do
  2795. begin
  2796. SetChannelI(ASource[n].R);
  2797. SetChannelI(ASource[n].G);
  2798. SetChannelI(ASource[n].B);
  2799. end;
  2800. GL_BGR:
  2801. for n := 0 to AWidth*AHeight-1 do
  2802. begin
  2803. SetChannel(ASource[n].B);
  2804. SetChannel(ASource[n].G);
  2805. SetChannel(ASource[n].R);
  2806. end;
  2807. GL_BGR_INTEGER:
  2808. for n := 0 to AWidth*AHeight-1 do
  2809. begin
  2810. SetChannelI(ASource[n].B);
  2811. SetChannelI(ASource[n].G);
  2812. SetChannelI(ASource[n].R);
  2813. end;
  2814. GL_RGBA:
  2815. for n := 0 to AWidth*AHeight-1 do
  2816. begin
  2817. SetChannel(ASource[n].R);
  2818. SetChannel(ASource[n].G);
  2819. SetChannel(ASource[n].B);
  2820. SetChannel(ASource[n].A);
  2821. end;
  2822. GL_RGBA_INTEGER:
  2823. for n := 0 to AWidth*AHeight-1 do
  2824. begin
  2825. SetChannelI(ASource[n].R);
  2826. SetChannelI(ASource[n].G);
  2827. SetChannelI(ASource[n].B);
  2828. SetChannelI(ASource[n].A);
  2829. end;
  2830. GL_BGRA:
  2831. for n := 0 to AWidth*AHeight-1 do
  2832. begin
  2833. SetChannel(ASource[n].B);
  2834. SetChannel(ASource[n].G);
  2835. SetChannel(ASource[n].R);
  2836. SetChannel(ASource[n].A);
  2837. end;
  2838. GL_BGRA_INTEGER:
  2839. for n := 0 to AWidth*AHeight-1 do
  2840. begin
  2841. SetChannelI(ASource[n].B);
  2842. SetChannelI(ASource[n].G);
  2843. SetChannelI(ASource[n].R);
  2844. SetChannelI(ASource[n].A);
  2845. end;
  2846. GL_ALPHA:
  2847. for n := 0 to AWidth*AHeight-1 do
  2848. begin
  2849. SetChannel(ASource[n].A);
  2850. end;
  2851. GL_ALPHA_INTEGER:
  2852. for n := 0 to AWidth*AHeight-1 do
  2853. begin
  2854. SetChannelI(ASource[n].A);
  2855. end;
  2856. GL_LUMINANCE:
  2857. for n := 0 to AWidth*AHeight-1 do
  2858. begin
  2859. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2860. end;
  2861. GL_LUMINANCE_INTEGER_EXT:
  2862. for n := 0 to AWidth*AHeight-1 do
  2863. begin
  2864. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2865. end;
  2866. GL_LUMINANCE_ALPHA:
  2867. for n := 0 to AWidth*AHeight-1 do
  2868. begin
  2869. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2870. SetChannel(ASource[n].A);
  2871. end;
  2872. GL_LUMINANCE_ALPHA_INTEGER_EXT:
  2873. for n := 0 to AWidth*AHeight-1 do
  2874. begin
  2875. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2876. SetChannelI(ASource[n].A);
  2877. end;
  2878. GL_INTENSITY:
  2879. for n := 0 to AWidth*AHeight-1 do
  2880. begin
  2881. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  2882. end;
  2883. GL_RED:
  2884. for n := 0 to AWidth*AHeight-1 do
  2885. begin
  2886. SetChannel(ASource[n].R);
  2887. end;
  2888. GL_RED_INTEGER:
  2889. for n := 0 to AWidth*AHeight-1 do
  2890. begin
  2891. SetChannelI(ASource[n].R);
  2892. end;
  2893. GL_GREEN:
  2894. for n := 0 to AWidth*AHeight-1 do
  2895. begin
  2896. SetChannel(ASource[n].G);
  2897. end;
  2898. GL_GREEN_INTEGER:
  2899. for n := 0 to AWidth*AHeight-1 do
  2900. begin
  2901. SetChannelI(ASource[n].G);
  2902. end;
  2903. GL_BLUE:
  2904. for n := 0 to AWidth*AHeight-1 do
  2905. begin
  2906. SetChannel(ASource[n].B);
  2907. end;
  2908. GL_BLUE_INTEGER:
  2909. for n := 0 to AWidth*AHeight-1 do
  2910. begin
  2911. SetChannelI(ASource[n].B);
  2912. end;
  2913. GL_RG:
  2914. for n := 0 to AWidth*AHeight-1 do
  2915. begin
  2916. SetChannel(ASource[n].R);
  2917. SetChannel(ASource[n].G);
  2918. end;
  2919. GL_RG_INTEGER:
  2920. for n := 0 to AWidth*AHeight-1 do
  2921. begin
  2922. SetChannelI(ASource[n].R);
  2923. SetChannelI(ASource[n].G);
  2924. end;
  2925. else
  2926. raise EGLImageUtils.Create(strInvalidType);
  2927. end;
  2928. end;
  2929. procedure ImfToUShort(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  2930. var
  2931. pDest: PWord;
  2932. n: Integer;
  2933. procedure SetChannel(AValue: Single);
  2934. begin
  2935. pDest^ := Trunc(ClampValue(AValue, 0.0, 65535.0));
  2936. Inc(pDest);
  2937. end;
  2938. procedure SetChannelI(AValue: Single);
  2939. begin
  2940. pDest^ := Trunc(AValue);
  2941. Inc(pDest);
  2942. end;
  2943. begin
  2944. pDest := PWord(ADest);
  2945. case AColorFormat of
  2946. GL_RGB:
  2947. for n := 0 to AWidth*AHeight-1 do
  2948. begin
  2949. SetChannel(ASource[n].R);
  2950. SetChannel(ASource[n].G);
  2951. SetChannel(ASource[n].B);
  2952. end;
  2953. GL_RGB_INTEGER:
  2954. for n := 0 to AWidth*AHeight-1 do
  2955. begin
  2956. SetChannelI(ASource[n].R);
  2957. SetChannelI(ASource[n].G);
  2958. SetChannelI(ASource[n].B);
  2959. end;
  2960. GL_BGR:
  2961. for n := 0 to AWidth*AHeight-1 do
  2962. begin
  2963. SetChannel(ASource[n].B);
  2964. SetChannel(ASource[n].G);
  2965. SetChannel(ASource[n].R);
  2966. end;
  2967. GL_BGR_INTEGER:
  2968. for n := 0 to AWidth*AHeight-1 do
  2969. begin
  2970. SetChannelI(ASource[n].B);
  2971. SetChannelI(ASource[n].G);
  2972. SetChannelI(ASource[n].R);
  2973. end;
  2974. GL_RGBA:
  2975. for n := 0 to AWidth*AHeight-1 do
  2976. begin
  2977. SetChannel(ASource[n].R);
  2978. SetChannel(ASource[n].G);
  2979. SetChannel(ASource[n].B);
  2980. SetChannel(ASource[n].A);
  2981. end;
  2982. GL_RGBA_INTEGER:
  2983. for n := 0 to AWidth*AHeight-1 do
  2984. begin
  2985. SetChannelI(ASource[n].R);
  2986. SetChannelI(ASource[n].G);
  2987. SetChannelI(ASource[n].B);
  2988. SetChannelI(ASource[n].A);
  2989. end;
  2990. GL_BGRA:
  2991. for n := 0 to AWidth*AHeight-1 do
  2992. begin
  2993. SetChannel(ASource[n].B);
  2994. SetChannel(ASource[n].G);
  2995. SetChannel(ASource[n].R);
  2996. SetChannel(ASource[n].A);
  2997. end;
  2998. GL_BGRA_INTEGER:
  2999. for n := 0 to AWidth*AHeight-1 do
  3000. begin
  3001. SetChannelI(ASource[n].B);
  3002. SetChannelI(ASource[n].G);
  3003. SetChannelI(ASource[n].R);
  3004. SetChannelI(ASource[n].A);
  3005. end;
  3006. GL_ALPHA:
  3007. for n := 0 to AWidth*AHeight-1 do
  3008. begin
  3009. SetChannel(ASource[n].A);
  3010. end;
  3011. GL_ALPHA_INTEGER:
  3012. for n := 0 to AWidth*AHeight-1 do
  3013. begin
  3014. SetChannelI(ASource[n].A);
  3015. end;
  3016. GL_LUMINANCE:
  3017. for n := 0 to AWidth*AHeight-1 do
  3018. begin
  3019. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3020. end;
  3021. GL_LUMINANCE_INTEGER_EXT:
  3022. for n := 0 to AWidth*AHeight-1 do
  3023. begin
  3024. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3025. end;
  3026. GL_LUMINANCE_ALPHA:
  3027. for n := 0 to AWidth*AHeight-1 do
  3028. begin
  3029. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3030. SetChannel(ASource[n].A);
  3031. end;
  3032. GL_LUMINANCE_ALPHA_INTEGER_EXT:
  3033. for n := 0 to AWidth*AHeight-1 do
  3034. begin
  3035. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3036. SetChannelI(ASource[n].A);
  3037. end;
  3038. GL_INTENSITY:
  3039. for n := 0 to AWidth*AHeight-1 do
  3040. begin
  3041. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3042. end;
  3043. GL_RED:
  3044. for n := 0 to AWidth*AHeight-1 do
  3045. begin
  3046. SetChannel(ASource[n].R);
  3047. end;
  3048. GL_RED_INTEGER:
  3049. for n := 0 to AWidth*AHeight-1 do
  3050. begin
  3051. SetChannelI(ASource[n].R);
  3052. end;
  3053. GL_GREEN:
  3054. for n := 0 to AWidth*AHeight-1 do
  3055. begin
  3056. SetChannel(ASource[n].G);
  3057. end;
  3058. GL_GREEN_INTEGER:
  3059. for n := 0 to AWidth*AHeight-1 do
  3060. begin
  3061. SetChannelI(ASource[n].G);
  3062. end;
  3063. GL_BLUE:
  3064. for n := 0 to AWidth*AHeight-1 do
  3065. begin
  3066. SetChannel(ASource[n].B);
  3067. end;
  3068. GL_BLUE_INTEGER:
  3069. for n := 0 to AWidth*AHeight-1 do
  3070. begin
  3071. SetChannelI(ASource[n].B);
  3072. end;
  3073. GL_RG:
  3074. for n := 0 to AWidth*AHeight-1 do
  3075. begin
  3076. SetChannel(ASource[n].R);
  3077. SetChannel(ASource[n].G);
  3078. end;
  3079. GL_RG_INTEGER:
  3080. for n := 0 to AWidth*AHeight-1 do
  3081. begin
  3082. SetChannelI(ASource[n].R);
  3083. SetChannelI(ASource[n].G);
  3084. end;
  3085. else
  3086. raise EGLImageUtils.Create(strInvalidType);
  3087. end;
  3088. end;
  3089. procedure ImfToShort(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  3090. var
  3091. pDest: PSmallInt;
  3092. n: Integer;
  3093. procedure SetChannel(AValue: Single);
  3094. begin
  3095. pDest^ := Trunc(ClampValue(AValue, -32767.0, 32767.0));
  3096. Inc(pDest);
  3097. end;
  3098. procedure SetChannelI(AValue: Single);
  3099. begin
  3100. pDest^ := Trunc(AValue);
  3101. Inc(pDest);
  3102. end;
  3103. begin
  3104. pDest := PSmallInt(ADest);
  3105. case AColorFormat of
  3106. GL_RGB:
  3107. for n := 0 to AWidth*AHeight-1 do
  3108. begin
  3109. SetChannel(ASource[n].R);
  3110. SetChannel(ASource[n].G);
  3111. SetChannel(ASource[n].B);
  3112. end;
  3113. GL_RGB_INTEGER:
  3114. for n := 0 to AWidth*AHeight-1 do
  3115. begin
  3116. SetChannelI(ASource[n].R);
  3117. SetChannelI(ASource[n].G);
  3118. SetChannelI(ASource[n].B);
  3119. end;
  3120. GL_BGR:
  3121. for n := 0 to AWidth*AHeight-1 do
  3122. begin
  3123. SetChannel(ASource[n].B);
  3124. SetChannel(ASource[n].G);
  3125. SetChannel(ASource[n].R);
  3126. end;
  3127. GL_BGR_INTEGER:
  3128. for n := 0 to AWidth*AHeight-1 do
  3129. begin
  3130. SetChannelI(ASource[n].B);
  3131. SetChannelI(ASource[n].G);
  3132. SetChannelI(ASource[n].R);
  3133. end;
  3134. GL_RGBA:
  3135. for n := 0 to AWidth*AHeight-1 do
  3136. begin
  3137. SetChannel(ASource[n].R);
  3138. SetChannel(ASource[n].G);
  3139. SetChannel(ASource[n].B);
  3140. SetChannel(ASource[n].A);
  3141. end;
  3142. GL_RGBA_INTEGER:
  3143. for n := 0 to AWidth*AHeight-1 do
  3144. begin
  3145. SetChannelI(ASource[n].R);
  3146. SetChannelI(ASource[n].G);
  3147. SetChannelI(ASource[n].B);
  3148. SetChannelI(ASource[n].A);
  3149. end;
  3150. GL_BGRA:
  3151. for n := 0 to AWidth*AHeight-1 do
  3152. begin
  3153. SetChannel(ASource[n].B);
  3154. SetChannel(ASource[n].G);
  3155. SetChannel(ASource[n].R);
  3156. SetChannel(ASource[n].A);
  3157. end;
  3158. GL_BGRA_INTEGER:
  3159. for n := 0 to AWidth*AHeight-1 do
  3160. begin
  3161. SetChannelI(ASource[n].B);
  3162. SetChannelI(ASource[n].G);
  3163. SetChannelI(ASource[n].R);
  3164. SetChannelI(ASource[n].A);
  3165. end;
  3166. GL_ALPHA:
  3167. for n := 0 to AWidth*AHeight-1 do
  3168. begin
  3169. SetChannel(ASource[n].A);
  3170. end;
  3171. GL_ALPHA_INTEGER:
  3172. for n := 0 to AWidth*AHeight-1 do
  3173. begin
  3174. SetChannelI(ASource[n].A);
  3175. end;
  3176. GL_LUMINANCE:
  3177. for n := 0 to AWidth*AHeight-1 do
  3178. begin
  3179. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3180. end;
  3181. GL_LUMINANCE_INTEGER_EXT:
  3182. for n := 0 to AWidth*AHeight-1 do
  3183. begin
  3184. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3185. end;
  3186. GL_LUMINANCE_ALPHA:
  3187. for n := 0 to AWidth*AHeight-1 do
  3188. begin
  3189. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3190. SetChannel(ASource[n].A);
  3191. end;
  3192. GL_LUMINANCE_ALPHA_INTEGER_EXT:
  3193. for n := 0 to AWidth*AHeight-1 do
  3194. begin
  3195. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3196. SetChannelI(ASource[n].A);
  3197. end;
  3198. GL_INTENSITY:
  3199. for n := 0 to AWidth*AHeight-1 do
  3200. begin
  3201. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3202. end;
  3203. GL_RED:
  3204. for n := 0 to AWidth*AHeight-1 do
  3205. begin
  3206. SetChannel(ASource[n].R);
  3207. end;
  3208. GL_RED_INTEGER:
  3209. for n := 0 to AWidth*AHeight-1 do
  3210. begin
  3211. SetChannelI(ASource[n].R);
  3212. end;
  3213. GL_GREEN:
  3214. for n := 0 to AWidth*AHeight-1 do
  3215. begin
  3216. SetChannel(ASource[n].G);
  3217. end;
  3218. GL_GREEN_INTEGER:
  3219. for n := 0 to AWidth*AHeight-1 do
  3220. begin
  3221. SetChannelI(ASource[n].G);
  3222. end;
  3223. GL_BLUE:
  3224. for n := 0 to AWidth*AHeight-1 do
  3225. begin
  3226. SetChannel(ASource[n].B);
  3227. end;
  3228. GL_BLUE_INTEGER:
  3229. for n := 0 to AWidth*AHeight-1 do
  3230. begin
  3231. SetChannelI(ASource[n].B);
  3232. end;
  3233. GL_RG:
  3234. for n := 0 to AWidth*AHeight-1 do
  3235. begin
  3236. SetChannel(ASource[n].R);
  3237. SetChannel(ASource[n].G);
  3238. end;
  3239. GL_RG_INTEGER:
  3240. for n := 0 to AWidth*AHeight-1 do
  3241. begin
  3242. SetChannelI(ASource[n].R);
  3243. SetChannelI(ASource[n].G);
  3244. end;
  3245. else
  3246. raise EGLImageUtils.Create(strInvalidType);
  3247. end;
  3248. end;
  3249. procedure ImfToUInt(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  3250. var
  3251. pDest: PLongWord;
  3252. n: Integer;
  3253. procedure SetChannel(AValue: Single);
  3254. begin
  3255. pDest^ := Trunc(ClampValue(AValue, 0.0, $FFFFFFFF));
  3256. Inc(pDest);
  3257. end;
  3258. procedure SetChannelI(AValue: Single);
  3259. begin
  3260. pDest^ := Trunc(AValue);
  3261. Inc(pDest);
  3262. end;
  3263. begin
  3264. pDest := PLongWord(ADest);
  3265. case AColorFormat of
  3266. GL_RGB:
  3267. for n := 0 to AWidth*AHeight-1 do
  3268. begin
  3269. SetChannel(ASource[n].R);
  3270. SetChannel(ASource[n].G);
  3271. SetChannel(ASource[n].B);
  3272. end;
  3273. GL_RGB_INTEGER:
  3274. for n := 0 to AWidth*AHeight-1 do
  3275. begin
  3276. SetChannelI(ASource[n].R);
  3277. SetChannelI(ASource[n].G);
  3278. SetChannelI(ASource[n].B);
  3279. end;
  3280. GL_BGR:
  3281. for n := 0 to AWidth*AHeight-1 do
  3282. begin
  3283. SetChannel(ASource[n].B);
  3284. SetChannel(ASource[n].G);
  3285. SetChannel(ASource[n].R);
  3286. end;
  3287. GL_BGR_INTEGER:
  3288. for n := 0 to AWidth*AHeight-1 do
  3289. begin
  3290. SetChannelI(ASource[n].B);
  3291. SetChannelI(ASource[n].G);
  3292. SetChannelI(ASource[n].R);
  3293. end;
  3294. GL_RGBA:
  3295. for n := 0 to AWidth*AHeight-1 do
  3296. begin
  3297. SetChannel(ASource[n].R);
  3298. SetChannel(ASource[n].G);
  3299. SetChannel(ASource[n].B);
  3300. SetChannel(ASource[n].A);
  3301. end;
  3302. GL_RGBA_INTEGER:
  3303. for n := 0 to AWidth*AHeight-1 do
  3304. begin
  3305. SetChannelI(ASource[n].R);
  3306. SetChannelI(ASource[n].G);
  3307. SetChannelI(ASource[n].B);
  3308. SetChannelI(ASource[n].A);
  3309. end;
  3310. GL_BGRA:
  3311. for n := 0 to AWidth*AHeight-1 do
  3312. begin
  3313. SetChannel(ASource[n].B);
  3314. SetChannel(ASource[n].G);
  3315. SetChannel(ASource[n].R);
  3316. SetChannel(ASource[n].A);
  3317. end;
  3318. GL_BGRA_INTEGER:
  3319. for n := 0 to AWidth*AHeight-1 do
  3320. begin
  3321. SetChannelI(ASource[n].B);
  3322. SetChannelI(ASource[n].G);
  3323. SetChannelI(ASource[n].R);
  3324. SetChannelI(ASource[n].A);
  3325. end;
  3326. GL_ALPHA:
  3327. for n := 0 to AWidth*AHeight-1 do
  3328. begin
  3329. SetChannel(ASource[n].A);
  3330. end;
  3331. GL_ALPHA_INTEGER:
  3332. for n := 0 to AWidth*AHeight-1 do
  3333. begin
  3334. SetChannelI(ASource[n].A);
  3335. end;
  3336. GL_LUMINANCE:
  3337. for n := 0 to AWidth*AHeight-1 do
  3338. begin
  3339. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3340. end;
  3341. GL_LUMINANCE_INTEGER_EXT:
  3342. for n := 0 to AWidth*AHeight-1 do
  3343. begin
  3344. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3345. end;
  3346. GL_LUMINANCE_ALPHA:
  3347. for n := 0 to AWidth*AHeight-1 do
  3348. begin
  3349. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3350. SetChannel(ASource[n].A);
  3351. end;
  3352. GL_LUMINANCE_ALPHA_INTEGER_EXT:
  3353. for n := 0 to AWidth*AHeight-1 do
  3354. begin
  3355. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3356. SetChannelI(ASource[n].A);
  3357. end;
  3358. GL_INTENSITY:
  3359. for n := 0 to AWidth*AHeight-1 do
  3360. begin
  3361. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3362. end;
  3363. GL_RED:
  3364. for n := 0 to AWidth*AHeight-1 do
  3365. begin
  3366. SetChannel(ASource[n].R);
  3367. end;
  3368. GL_RED_INTEGER:
  3369. for n := 0 to AWidth*AHeight-1 do
  3370. begin
  3371. SetChannelI(ASource[n].R);
  3372. end;
  3373. GL_GREEN:
  3374. for n := 0 to AWidth*AHeight-1 do
  3375. begin
  3376. SetChannel(ASource[n].G);
  3377. end;
  3378. GL_GREEN_INTEGER:
  3379. for n := 0 to AWidth*AHeight-1 do
  3380. begin
  3381. SetChannelI(ASource[n].G);
  3382. end;
  3383. GL_BLUE:
  3384. for n := 0 to AWidth*AHeight-1 do
  3385. begin
  3386. SetChannel(ASource[n].B);
  3387. end;
  3388. GL_BLUE_INTEGER:
  3389. for n := 0 to AWidth*AHeight-1 do
  3390. begin
  3391. SetChannelI(ASource[n].B);
  3392. end;
  3393. GL_RG:
  3394. for n := 0 to AWidth*AHeight-1 do
  3395. begin
  3396. SetChannel(ASource[n].R);
  3397. SetChannel(ASource[n].G);
  3398. end;
  3399. GL_RG_INTEGER:
  3400. for n := 0 to AWidth*AHeight-1 do
  3401. begin
  3402. SetChannelI(ASource[n].R);
  3403. SetChannelI(ASource[n].G);
  3404. end;
  3405. else
  3406. raise EGLImageUtils.Create(strInvalidType);
  3407. end;
  3408. end;
  3409. procedure ImfToInt(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  3410. var
  3411. pDest: PLongInt;
  3412. n: Integer;
  3413. procedure SetChannel(AValue: Single);
  3414. begin
  3415. pDest^ := Trunc(ClampValue(AValue, -$7FFFFFFF, $7FFFFFFF));
  3416. Inc(pDest);
  3417. end;
  3418. procedure SetChannelI(AValue: Single);
  3419. begin
  3420. pDest^ := Trunc(AValue);
  3421. Inc(pDest);
  3422. end;
  3423. begin
  3424. pDest := PLongInt(ADest);
  3425. case AColorFormat of
  3426. GL_RGB:
  3427. for n := 0 to AWidth*AHeight-1 do
  3428. begin
  3429. SetChannel(ASource[n].R);
  3430. SetChannel(ASource[n].G);
  3431. SetChannel(ASource[n].B);
  3432. end;
  3433. GL_RGB_INTEGER:
  3434. for n := 0 to AWidth*AHeight-1 do
  3435. begin
  3436. SetChannelI(ASource[n].R);
  3437. SetChannelI(ASource[n].G);
  3438. SetChannelI(ASource[n].B);
  3439. end;
  3440. GL_BGR:
  3441. for n := 0 to AWidth*AHeight-1 do
  3442. begin
  3443. SetChannel(ASource[n].B);
  3444. SetChannel(ASource[n].G);
  3445. SetChannel(ASource[n].R);
  3446. end;
  3447. GL_BGR_INTEGER:
  3448. for n := 0 to AWidth*AHeight-1 do
  3449. begin
  3450. SetChannelI(ASource[n].B);
  3451. SetChannelI(ASource[n].G);
  3452. SetChannelI(ASource[n].R);
  3453. end;
  3454. GL_RGBA:
  3455. for n := 0 to AWidth*AHeight-1 do
  3456. begin
  3457. SetChannel(ASource[n].R);
  3458. SetChannel(ASource[n].G);
  3459. SetChannel(ASource[n].B);
  3460. SetChannel(ASource[n].A);
  3461. end;
  3462. GL_RGBA_INTEGER:
  3463. for n := 0 to AWidth*AHeight-1 do
  3464. begin
  3465. SetChannelI(ASource[n].R);
  3466. SetChannelI(ASource[n].G);
  3467. SetChannelI(ASource[n].B);
  3468. SetChannelI(ASource[n].A);
  3469. end;
  3470. GL_BGRA:
  3471. for n := 0 to AWidth*AHeight-1 do
  3472. begin
  3473. SetChannel(ASource[n].B);
  3474. SetChannel(ASource[n].G);
  3475. SetChannel(ASource[n].R);
  3476. SetChannel(ASource[n].A);
  3477. end;
  3478. GL_BGRA_INTEGER:
  3479. for n := 0 to AWidth*AHeight-1 do
  3480. begin
  3481. SetChannelI(ASource[n].B);
  3482. SetChannelI(ASource[n].G);
  3483. SetChannelI(ASource[n].R);
  3484. SetChannelI(ASource[n].A);
  3485. end;
  3486. GL_ALPHA:
  3487. for n := 0 to AWidth*AHeight-1 do
  3488. begin
  3489. SetChannel(ASource[n].A);
  3490. end;
  3491. GL_ALPHA_INTEGER:
  3492. for n := 0 to AWidth*AHeight-1 do
  3493. begin
  3494. SetChannelI(ASource[n].A);
  3495. end;
  3496. GL_LUMINANCE:
  3497. for n := 0 to AWidth*AHeight-1 do
  3498. begin
  3499. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3500. end;
  3501. GL_LUMINANCE_INTEGER_EXT:
  3502. for n := 0 to AWidth*AHeight-1 do
  3503. begin
  3504. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3505. end;
  3506. GL_LUMINANCE_ALPHA:
  3507. for n := 0 to AWidth*AHeight-1 do
  3508. begin
  3509. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3510. SetChannel(ASource[n].A);
  3511. end;
  3512. GL_LUMINANCE_ALPHA_INTEGER_EXT:
  3513. for n := 0 to AWidth*AHeight-1 do
  3514. begin
  3515. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3516. SetChannelI(ASource[n].A);
  3517. end;
  3518. GL_INTENSITY:
  3519. for n := 0 to AWidth*AHeight-1 do
  3520. begin
  3521. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3522. end;
  3523. GL_RED:
  3524. for n := 0 to AWidth*AHeight-1 do
  3525. begin
  3526. SetChannel(ASource[n].R);
  3527. end;
  3528. GL_RED_INTEGER:
  3529. for n := 0 to AWidth*AHeight-1 do
  3530. begin
  3531. SetChannelI(ASource[n].R);
  3532. end;
  3533. GL_GREEN:
  3534. for n := 0 to AWidth*AHeight-1 do
  3535. begin
  3536. SetChannel(ASource[n].G);
  3537. end;
  3538. GL_GREEN_INTEGER:
  3539. for n := 0 to AWidth*AHeight-1 do
  3540. begin
  3541. SetChannelI(ASource[n].G);
  3542. end;
  3543. GL_BLUE:
  3544. for n := 0 to AWidth*AHeight-1 do
  3545. begin
  3546. SetChannel(ASource[n].B);
  3547. end;
  3548. GL_BLUE_INTEGER:
  3549. for n := 0 to AWidth*AHeight-1 do
  3550. begin
  3551. SetChannelI(ASource[n].B);
  3552. end;
  3553. GL_RG:
  3554. for n := 0 to AWidth*AHeight-1 do
  3555. begin
  3556. SetChannel(ASource[n].R);
  3557. SetChannel(ASource[n].G);
  3558. end;
  3559. GL_RG_INTEGER:
  3560. for n := 0 to AWidth*AHeight-1 do
  3561. begin
  3562. SetChannelI(ASource[n].R);
  3563. SetChannelI(ASource[n].G);
  3564. end;
  3565. else
  3566. raise EGLImageUtils.Create(strInvalidType);
  3567. end;
  3568. end;
  3569. procedure ImfToFloat(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  3570. const
  3571. cInv255 = 1.0 / 255.0;
  3572. var
  3573. pDest: PSingle;
  3574. n: Integer;
  3575. procedure SetChannel(AValue: Single);
  3576. begin
  3577. pDest^ := AValue * cInv255;
  3578. Inc(pDest);
  3579. end;
  3580. procedure SetChannelI(AValue: Single);
  3581. begin
  3582. pDest^ := AValue * cInv255;
  3583. Inc(pDest);
  3584. end;
  3585. begin
  3586. pDest := PSingle(ADest);
  3587. case AColorFormat of
  3588. GL_RGB:
  3589. for n := 0 to AWidth*AHeight-1 do
  3590. begin
  3591. SetChannel(ASource[n].R);
  3592. SetChannel(ASource[n].G);
  3593. SetChannel(ASource[n].B);
  3594. end;
  3595. GL_RGB_INTEGER:
  3596. for n := 0 to AWidth*AHeight-1 do
  3597. begin
  3598. SetChannelI(ASource[n].R);
  3599. SetChannelI(ASource[n].G);
  3600. SetChannelI(ASource[n].B);
  3601. end;
  3602. GL_BGR:
  3603. for n := 0 to AWidth*AHeight-1 do
  3604. begin
  3605. SetChannel(ASource[n].B);
  3606. SetChannel(ASource[n].G);
  3607. SetChannel(ASource[n].R);
  3608. end;
  3609. GL_BGR_INTEGER:
  3610. for n := 0 to AWidth*AHeight-1 do
  3611. begin
  3612. SetChannelI(ASource[n].B);
  3613. SetChannelI(ASource[n].G);
  3614. SetChannelI(ASource[n].R);
  3615. end;
  3616. GL_RGBA:
  3617. for n := 0 to AWidth*AHeight-1 do
  3618. begin
  3619. SetChannel(ASource[n].R);
  3620. SetChannel(ASource[n].G);
  3621. SetChannel(ASource[n].B);
  3622. SetChannel(ASource[n].A);
  3623. end;
  3624. GL_RGBA_INTEGER:
  3625. for n := 0 to AWidth*AHeight-1 do
  3626. begin
  3627. SetChannelI(ASource[n].R);
  3628. SetChannelI(ASource[n].G);
  3629. SetChannelI(ASource[n].B);
  3630. SetChannelI(ASource[n].A);
  3631. end;
  3632. GL_BGRA:
  3633. for n := 0 to AWidth*AHeight-1 do
  3634. begin
  3635. SetChannel(ASource[n].B);
  3636. SetChannel(ASource[n].G);
  3637. SetChannel(ASource[n].R);
  3638. SetChannel(ASource[n].A);
  3639. end;
  3640. GL_BGRA_INTEGER:
  3641. for n := 0 to AWidth*AHeight-1 do
  3642. begin
  3643. SetChannelI(ASource[n].B);
  3644. SetChannelI(ASource[n].G);
  3645. SetChannelI(ASource[n].R);
  3646. SetChannelI(ASource[n].A);
  3647. end;
  3648. GL_ALPHA:
  3649. for n := 0 to AWidth*AHeight-1 do
  3650. begin
  3651. SetChannel(ASource[n].A);
  3652. end;
  3653. GL_ALPHA_INTEGER:
  3654. for n := 0 to AWidth*AHeight-1 do
  3655. begin
  3656. SetChannelI(ASource[n].A);
  3657. end;
  3658. GL_LUMINANCE:
  3659. for n := 0 to AWidth*AHeight-1 do
  3660. begin
  3661. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3662. end;
  3663. GL_LUMINANCE_INTEGER_EXT:
  3664. for n := 0 to AWidth*AHeight-1 do
  3665. begin
  3666. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3667. end;
  3668. GL_LUMINANCE_ALPHA:
  3669. for n := 0 to AWidth*AHeight-1 do
  3670. begin
  3671. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3672. SetChannel(ASource[n].A);
  3673. end;
  3674. GL_LUMINANCE_ALPHA_INTEGER_EXT:
  3675. for n := 0 to AWidth*AHeight-1 do
  3676. begin
  3677. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3678. SetChannelI(ASource[n].A);
  3679. end;
  3680. GL_INTENSITY:
  3681. for n := 0 to AWidth*AHeight-1 do
  3682. begin
  3683. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3684. end;
  3685. GL_RED:
  3686. for n := 0 to AWidth*AHeight-1 do
  3687. begin
  3688. SetChannel(ASource[n].R);
  3689. end;
  3690. GL_RED_INTEGER:
  3691. for n := 0 to AWidth*AHeight-1 do
  3692. begin
  3693. SetChannelI(ASource[n].R);
  3694. end;
  3695. GL_GREEN:
  3696. for n := 0 to AWidth*AHeight-1 do
  3697. begin
  3698. SetChannel(ASource[n].G);
  3699. end;
  3700. GL_GREEN_INTEGER:
  3701. for n := 0 to AWidth*AHeight-1 do
  3702. begin
  3703. SetChannelI(ASource[n].G);
  3704. end;
  3705. GL_BLUE:
  3706. for n := 0 to AWidth*AHeight-1 do
  3707. begin
  3708. SetChannel(ASource[n].B);
  3709. end;
  3710. GL_BLUE_INTEGER:
  3711. for n := 0 to AWidth*AHeight-1 do
  3712. begin
  3713. SetChannelI(ASource[n].B);
  3714. end;
  3715. GL_RG:
  3716. for n := 0 to AWidth*AHeight-1 do
  3717. begin
  3718. SetChannel(ASource[n].R);
  3719. SetChannel(ASource[n].G);
  3720. end;
  3721. GL_RG_INTEGER:
  3722. for n := 0 to AWidth*AHeight-1 do
  3723. begin
  3724. SetChannelI(ASource[n].R);
  3725. SetChannelI(ASource[n].G);
  3726. end;
  3727. else
  3728. raise EGLImageUtils.Create(strInvalidType);
  3729. end;
  3730. end;
  3731. procedure ImfToHalf(ASource: PIntermediateFormatArray; ADest: Pointer; AColorFormat: Cardinal; AWidth, AHeight: Integer);
  3732. const
  3733. cInv255 = 1.0 / 255.0;
  3734. var
  3735. pDest: PHalfFloat;
  3736. n: Integer;
  3737. procedure SetChannel(AValue: Single);
  3738. begin
  3739. pDest^ := FloatToHalf(AValue * cInv255);
  3740. Inc(pDest);
  3741. end;
  3742. procedure SetChannelI(AValue: Single);
  3743. begin
  3744. pDest^ := FloatToHalf(AValue * cInv255);
  3745. Inc(pDest);
  3746. end;
  3747. begin
  3748. pDest := PHalfFloat(ADest);
  3749. case AColorFormat of
  3750. GL_RGB:
  3751. for n := 0 to AWidth*AHeight-1 do
  3752. begin
  3753. SetChannel(ASource[n].R);
  3754. SetChannel(ASource[n].G);
  3755. SetChannel(ASource[n].B);
  3756. end;
  3757. GL_RGB_INTEGER:
  3758. for n := 0 to AWidth*AHeight-1 do
  3759. begin
  3760. SetChannelI(ASource[n].R);
  3761. SetChannelI(ASource[n].G);
  3762. SetChannelI(ASource[n].B);
  3763. end;
  3764. GL_BGR:
  3765. for n := 0 to AWidth*AHeight-1 do
  3766. begin
  3767. SetChannel(ASource[n].B);
  3768. SetChannel(ASource[n].G);
  3769. SetChannel(ASource[n].R);
  3770. end;
  3771. GL_BGR_INTEGER:
  3772. for n := 0 to AWidth*AHeight-1 do
  3773. begin
  3774. SetChannelI(ASource[n].B);
  3775. SetChannelI(ASource[n].G);
  3776. SetChannelI(ASource[n].R);
  3777. end;
  3778. GL_RGBA:
  3779. for n := 0 to AWidth*AHeight-1 do
  3780. begin
  3781. SetChannel(ASource[n].R);
  3782. SetChannel(ASource[n].G);
  3783. SetChannel(ASource[n].B);
  3784. SetChannel(ASource[n].A);
  3785. end;
  3786. GL_RGBA_INTEGER:
  3787. for n := 0 to AWidth*AHeight-1 do
  3788. begin
  3789. SetChannelI(ASource[n].R);
  3790. SetChannelI(ASource[n].G);
  3791. SetChannelI(ASource[n].B);
  3792. SetChannelI(ASource[n].A);
  3793. end;
  3794. GL_BGRA:
  3795. for n := 0 to AWidth*AHeight-1 do
  3796. begin
  3797. SetChannel(ASource[n].B);
  3798. SetChannel(ASource[n].G);
  3799. SetChannel(ASource[n].R);
  3800. SetChannel(ASource[n].A);
  3801. end;
  3802. GL_BGRA_INTEGER:
  3803. for n := 0 to AWidth*AHeight-1 do
  3804. begin
  3805. SetChannelI(ASource[n].B);
  3806. SetChannelI(ASource[n].G);
  3807. SetChannelI(ASource[n].R);
  3808. SetChannelI(ASource[n].A);
  3809. end;
  3810. GL_ALPHA:
  3811. for n := 0 to AWidth*AHeight-1 do
  3812. begin
  3813. SetChannel(ASource[n].A);
  3814. end;
  3815. GL_ALPHA_INTEGER:
  3816. for n := 0 to AWidth*AHeight-1 do
  3817. begin
  3818. SetChannelI(ASource[n].A);
  3819. end;
  3820. GL_LUMINANCE:
  3821. for n := 0 to AWidth*AHeight-1 do
  3822. begin
  3823. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3824. end;
  3825. GL_LUMINANCE_INTEGER_EXT:
  3826. for n := 0 to AWidth*AHeight-1 do
  3827. begin
  3828. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3829. end;
  3830. GL_LUMINANCE_ALPHA:
  3831. for n := 0 to AWidth*AHeight-1 do
  3832. begin
  3833. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3834. SetChannel(ASource[n].A);
  3835. end;
  3836. GL_LUMINANCE_ALPHA_INTEGER_EXT:
  3837. for n := 0 to AWidth*AHeight-1 do
  3838. begin
  3839. SetChannelI(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3840. SetChannelI(ASource[n].A);
  3841. end;
  3842. GL_INTENSITY:
  3843. for n := 0 to AWidth*AHeight-1 do
  3844. begin
  3845. SetChannel(ASource[n].R + ASource[n].G + ASource[n].B / 3.0);
  3846. end;
  3847. GL_RED:
  3848. for n := 0 to AWidth*AHeight-1 do
  3849. begin
  3850. SetChannel(ASource[n].R);
  3851. end;
  3852. GL_RED_INTEGER:
  3853. for n := 0 to AWidth*AHeight-1 do
  3854. begin
  3855. SetChannelI(ASource[n].R);
  3856. end;
  3857. GL_GREEN:
  3858. for n := 0 to AWidth*AHeight-1 do
  3859. begin
  3860. SetChannel(ASource[n].G);
  3861. end;
  3862. GL_GREEN_INTEGER:
  3863. for n := 0 to AWidth*AHeight-1 do
  3864. begin
  3865. SetChannelI(ASource[n].G);
  3866. end;
  3867. GL_BLUE:
  3868. for n := 0 to AWidth*AHeight-1 do
  3869. begin
  3870. SetChannel(ASource[n].B);
  3871. end;
  3872. GL_BLUE_INTEGER:
  3873. for n := 0 to AWidth*AHeight-1 do
  3874. begin
  3875. SetChannelI(ASource[n].B);
  3876. end;
  3877. GL_RG:
  3878. for n := 0 to AWidth*AHeight-1 do
  3879. begin
  3880. SetChannel(ASource[n].R);
  3881. SetChannel(ASource[n].G);
  3882. end;
  3883. GL_RG_INTEGER:
  3884. for n := 0 to AWidth*AHeight-1 do
  3885. begin
  3886. SetChannelI(ASource[n].R);
  3887. SetChannelI(ASource[n].G);
  3888. end;
  3889. else
  3890. raise EGLImageUtils.Create(strInvalidType);
  3891. end;
  3892. end;
  3893. // ------------------------------ Compression
  3894. { function FloatTo565(const AColor: TIntermediateFormat): Integer;
  3895. var
  3896. r, g, b: Integer;
  3897. begin
  3898. // get the components in the correct range
  3899. r := Round( 31.0*AColor.R, 31 );
  3900. g := Round( 63.0*AColor.G, 63 );
  3901. b := Round( 31.0*AColor.B, 31 );
  3902. // pack into a single value
  3903. Result := ( r shl 11 ) or ( g shl 5 ) or b;
  3904. end;
  3905. procedure WriteColourBlock(a, b: Integer; const indices: PByteArray; out block: TU48BitBlock);
  3906. var
  3907. I, J: Byte;
  3908. begin
  3909. // write the endpoints
  3910. block[0][0] := a and $ff;
  3911. block[0][1] := a shr 8;
  3912. block[0][2] := b and $ff;
  3913. block[0][3] := b shr 8;
  3914. // write the indices
  3915. for i := 0 to 3 do
  3916. begin
  3917. J := 4*i;
  3918. block[1][i] = indices[J+0] or ( indices[J+1] shl 2 ) or ( indices[J+2] shl 4 ) or ( indices[J+3] shl 6 );
  3919. end;
  3920. end;
  3921. procedure WriteColourBlock3(start, end_: TIntermediateFormat; const indices: PByteArray; out block: TU48BitBlock);
  3922. var
  3923. i, a, b: Integer;
  3924. remapped: array[0..15] of Byte;
  3925. begin
  3926. // get the packed values
  3927. a := FloatTo565( start );
  3928. b := FloatTo565( end_ );
  3929. // remap the indices
  3930. if a <= b then
  3931. begin
  3932. // use the indices directly
  3933. for i := 0 to 15 do
  3934. remapped[i] := indices[i];
  3935. end
  3936. else
  3937. begin
  3938. // swap a and b
  3939. Swap( a, b );
  3940. for i := 0 to 15 do
  3941. begin
  3942. if indices[i] = 0 then
  3943. remapped[i] := 1
  3944. else if indices[i] = 1 then
  3945. remapped[i] := 0
  3946. else
  3947. remapped[i] := indices[i];
  3948. end;
  3949. end;
  3950. // write the block
  3951. WriteColourBlock( a, b, remapped, block );
  3952. end;
  3953. procedure WriteColourBlock4(start, end_: TIntermediateFormat; const indices: PByteArray; out block: TU48BitBlock);
  3954. var
  3955. i, a, b: Integer;
  3956. remapped: array[0..15] of Byte;
  3957. begin
  3958. // get the packed values
  3959. a := FloatTo565( start );
  3960. b := FloatTo565( end_ );
  3961. // remap the indices
  3962. if a < b then
  3963. begin
  3964. // swap a and b
  3965. Swap( a, b );
  3966. for i := 0 to 15 do
  3967. remapped[i] := ( indices[i] xor $01 ) and $03;
  3968. end
  3969. else if a = b then
  3970. begin
  3971. // use index 0
  3972. for i := 0 to 15 do
  3973. remapped[i] := 0;
  3974. end
  3975. else
  3976. begin
  3977. // use the indices directly
  3978. for i := 0 to 15 do
  3979. remapped[i] := indices[i];
  3980. end;
  3981. // write the block
  3982. WriteColourBlock( a, b, remapped, block );
  3983. end; }
  3984. // ------------------------------ Image filters
  3985. function ImageBoxFilter(Value: Single): Single;
  3986. begin
  3987. if (Value > -0.5) and (Value <= 0.5) then
  3988. Result := 1.0
  3989. else
  3990. Result := 0.0;
  3991. end;
  3992. function ImageTriangleFilter(Value: Single): Single;
  3993. begin
  3994. if Value < 0.0 then
  3995. Value := -Value;
  3996. if Value < 1.0 then
  3997. Result := 1.0 - Value
  3998. else
  3999. Result := 0.0;
  4000. end;
  4001. function ImageHermiteFilter(Value: Single): Single;
  4002. begin
  4003. if Value < 0.0 then
  4004. Value := -Value;
  4005. if Value < 1 then
  4006. Result := (2 * Value - 3) * Sqr(Value) + 1
  4007. else
  4008. Result := 0;
  4009. end;
  4010. function ImageBellFilter(Value: Single): Single;
  4011. begin
  4012. if Value < 0.0 then
  4013. Value := -Value;
  4014. if Value < 0.5 then
  4015. Result := 0.75 - Sqr(Value)
  4016. else if Value < 1.5 then
  4017. begin
  4018. Value := Value - 1.5;
  4019. Result := 0.5 * Sqr(Value);
  4020. end
  4021. else
  4022. Result := 0.0;
  4023. end;
  4024. function ImageSplineFilter(Value: Single): Single;
  4025. var
  4026. temp: Single;
  4027. begin
  4028. if Value < 0.0 then
  4029. Value := -Value;
  4030. if Value < 1.0 then
  4031. begin
  4032. temp := Sqr(Value);
  4033. Result := 0.5 * temp * Value - temp + 2.0 / 3.0;
  4034. end
  4035. else if Value < 2.0 then
  4036. begin
  4037. Value := 2.0 - Value;
  4038. Result := Sqr(Value) * Value / 6.0;
  4039. end
  4040. else
  4041. Result := 0.0;
  4042. end;
  4043. function ImageLanczos3Filter(Value: Single): Single;
  4044. const
  4045. Radius = 3.0;
  4046. begin
  4047. Result := 1;
  4048. if Value = 0 then
  4049. Exit;
  4050. if Value < 0.0 then
  4051. Value := -Value;
  4052. if Value < Radius then
  4053. begin
  4054. Value := Value * pi;
  4055. Result := Radius * Sin(Value) * Sin(Value / Radius) / (Value * Value);
  4056. end
  4057. else
  4058. Result := 0.0;
  4059. end;
  4060. function ImageMitchellFilter(Value: Single): Single;
  4061. const
  4062. B = 1.0 / 3.0;
  4063. C = 1.0 / 3.0;
  4064. var
  4065. temp: Single;
  4066. begin
  4067. if Value < 0.0 then
  4068. Value := -Value;
  4069. temp := Sqr(Value);
  4070. if Value < 1.0 then
  4071. begin
  4072. Value := (((12.0 - 9.0 * B - 6.0 * C) * (Value * temp)) + ((-18.0 + 12.0 * B + 6.0 * C) * temp) + (6.0 - 2.0 * B));
  4073. Result := Value / 6.0;
  4074. end
  4075. else if Value < 2.0 then
  4076. begin
  4077. Value := (((-B - 6.0 * C) * (Value * temp)) + ((6.0 * B + 30.0 * C) * temp) + ((-12.0 * B - 48.0 * C) * Value) + (8.0 * B + 24.0 * C));
  4078. Result := Value / 6.0;
  4079. end
  4080. else
  4081. Result := 0.0;
  4082. end;
  4083. const cInvThree = 1.0/3.0;
  4084. procedure ImageAlphaFromIntensity(var AColor: TIntermediateFormat);
  4085. begin
  4086. AColor.A := (AColor.R + AColor.B + AColor.G) * cInvThree;
  4087. end;
  4088. procedure ImageAlphaSuperBlackTransparent(var AColor: TIntermediateFormat);
  4089. begin
  4090. if (AColor.R = 0.0) and (AColor.B = 0.0) and (AColor.G = 0.0) then
  4091. AColor.A := 0.0
  4092. else
  4093. AColor.A := 255.0;
  4094. end;
  4095. procedure ImageAlphaLuminance(var AColor: TIntermediateFormat);
  4096. begin
  4097. AColor.A := (AColor.R + AColor.B + AColor.G) * cInvThree;
  4098. AColor.R := AColor.A;
  4099. AColor.G := AColor.A;
  4100. AColor.B := AColor.A;
  4101. end;
  4102. procedure ImageAlphaLuminanceSqrt(var AColor: TIntermediateFormat);
  4103. begin
  4104. AColor.A := Sqrt((AColor.R + AColor.B + AColor.G) * cInvThree);
  4105. end;
  4106. procedure ImageAlphaOpaque(var AColor: TIntermediateFormat);
  4107. begin
  4108. AColor.A := 255.0;
  4109. end;
  4110. var
  4111. vTopLeftColor: TIntermediateFormat;
  4112. procedure ImageAlphaTopLeftPointColorTransparent(var AColor: TIntermediateFormat);
  4113. begin
  4114. if CompareMem(@AColor, @vTopLeftColor, 3*SizeOf(Single)) then
  4115. AColor.A := 0.0;
  4116. end;
  4117. procedure ImageAlphaInverseLuminance(var AColor: TIntermediateFormat);
  4118. begin
  4119. AColor.A := 255.0 - (AColor.R + AColor.B + AColor.G) * cInvThree;
  4120. AColor.R := AColor.A;
  4121. AColor.G := AColor.A;
  4122. AColor.B := AColor.A;
  4123. end;
  4124. procedure ImageAlphaInverseLuminanceSqrt(var AColor: TIntermediateFormat);
  4125. begin
  4126. AColor.A := 255.0 - Sqrt((AColor.R + AColor.B + AColor.G) * cInvThree);
  4127. end;
  4128. var
  4129. vBottomRightColor: TIntermediateFormat;
  4130. procedure ImageAlphaBottomRightPointColorTransparent(var AColor: TIntermediateFormat);
  4131. begin
  4132. if CompareMem(@AColor, @vBottomRightColor, 3*SizeOf(Single)) then
  4133. AColor.A := 0.0;
  4134. end;
  4135. type
  4136. // Contributor for a pixel
  4137. TContributor = record
  4138. pixel: Integer; // Source pixel
  4139. weight: Single; // Pixel weight
  4140. end;
  4141. TContributorList = array [0 .. MaxInt div (2 * SizeOf(TContributor))] of TContributor;
  4142. PContributorList = ^TContributorList;
  4143. // List of source pixels contributing to a destination pixel
  4144. TCList = record
  4145. n: Integer;
  4146. p: PContributorList;
  4147. end;
  4148. TCListList = array [0 .. MaxInt div (2 * SizeOf(TCList))] of TCList;
  4149. PCListList = ^TCListList;
  4150. // ------------------------------ Data type conversion table
  4151. type
  4152. TConvertTableRec = record
  4153. type_: Cardinal;
  4154. proc1: TConvertToImfProc;
  4155. proc2: TConvertFromInfProc;
  4156. end;
  4157. const
  4158. cConvertTable: array [0 .. 36] of TConvertTableRec = (
  4159. (type_: GL_UNSIGNED_BYTE; proc1: UbyteToImf; proc2: ImfToUbyte),
  4160. (type_: GL_UNSIGNED_BYTE_3_3_2; proc1: Ubyte332ToImf; proc2: UnsupportedFromImf),
  4161. (type_: GL_UNSIGNED_BYTE_2_3_3_REV; proc1: Ubyte233RToImf; proc2: UnsupportedFromImf),
  4162. (type_: GL_BYTE; proc1: ByteToImf; proc2: ImfToByte),
  4163. (type_: GL_UNSIGNED_SHORT; proc1: UShortToImf; proc2: ImfToUShort),
  4164. (type_: GL_SHORT; proc1: ShortToImf; proc2: ImfToShort),
  4165. (type_: GL_UNSIGNED_INT; proc1: UIntToImf; proc2: ImfToUInt),
  4166. (type_: GL_INT; proc1: IntToImf; proc2: ImfToInt),
  4167. (type_: GL_FLOAT; proc1: FloatToImf; proc2: ImfToFloat),
  4168. (type_: GL_HALF_FLOAT; proc1: HalfFloatToImf; proc2: ImfToHalf),
  4169. (type_: GL_UNSIGNED_INT_8_8_8_8; proc1: UInt8888ToImf; proc2: UnsupportedFromImf),
  4170. (type_: GL_UNSIGNED_INT_8_8_8_8_REV; proc1: UInt8888RevToImf; proc2: UnsupportedFromImf),
  4171. (type_: GL_UNSIGNED_SHORT_4_4_4_4; proc1: UShort4444ToImf; proc2: UnsupportedFromImf),
  4172. (type_: GL_UNSIGNED_SHORT_4_4_4_4_REV; proc1: UShort4444RevToImf; proc2: UnsupportedFromImf),
  4173. (type_: GL_UNSIGNED_SHORT_5_6_5; proc1: UShort565ToImf; proc2: UnsupportedFromImf),
  4174. (type_: GL_UNSIGNED_SHORT_5_6_5_REV; proc1: UShort565RevToImf; proc2: UnsupportedFromImf),
  4175. (type_: GL_UNSIGNED_SHORT_5_5_5_1; proc1: UShort5551ToImf; proc2: UnsupportedFromImf),
  4176. (type_: GL_UNSIGNED_SHORT_1_5_5_5_REV; proc1: UShort5551RevToImf; proc2: UnsupportedFromImf),
  4177. (type_: GL_UNSIGNED_INT_10_10_10_2; proc1: UInt_10_10_10_2_ToImf; proc2: UnsupportedFromImf),
  4178. (type_: GL_UNSIGNED_INT_2_10_10_10_REV; proc1: UInt_10_10_10_2_Rev_ToImf; proc2: UnsupportedFromImf),
  4179. (type_: GL_COMPRESSED_RGB_S3TC_DXT1_EXT; proc1: DXT1_ToImf; proc2: UnsupportedFromImf),
  4180. (type_: GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; proc1: DXT1_ToImf; proc2: UnsupportedFromImf),
  4181. (type_: GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; proc1: DXT3_ToImf; proc2: UnsupportedFromImf),
  4182. (type_: GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; proc1: DXT5_ToImf; proc2: UnsupportedFromImf),
  4183. (type_: GL_COMPRESSED_SRGB_S3TC_DXT1_EXT; proc1: UnsupportedToImf; proc2: UnsupportedFromImf),
  4184. (type_: GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT; proc1: UnsupportedToImf; proc2: UnsupportedFromImf),
  4185. (type_: GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT; proc1: UnsupportedToImf; proc2: UnsupportedFromImf),
  4186. (type_: GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT; proc1: UnsupportedToImf; proc2: UnsupportedFromImf),
  4187. (type_: GL_COMPRESSED_LUMINANCE_LATC1_EXT; proc1: LATC1_ToImf; proc2: UnsupportedFromImf),
  4188. (type_: GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT; proc1: SLATC1_ToImf; proc2: UnsupportedFromImf),
  4189. (type_: GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT; proc1: LATC2_ToImf; proc2: UnsupportedFromImf),
  4190. (type_: GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT; proc1: SLATC2_ToImf; proc2: UnsupportedFromImf),
  4191. (type_: GL_COMPRESSED_LUMINANCE_ALPHA_3DC_ATI; proc1: UnsupportedToImf; proc2: UnsupportedFromImf),
  4192. (type_: GL_COMPRESSED_RED_RGTC1; proc1: RGTC1_ToImf; proc2: UnsupportedFromImf),
  4193. (type_: GL_COMPRESSED_SIGNED_RED_RGTC1; proc1: SRGTC1_ToImf; proc2: UnsupportedFromImf),
  4194. (type_: GL_COMPRESSED_RG_RGTC2; proc1: RGTC2_ToImf; proc2: UnsupportedFromImf),
  4195. (type_: GL_COMPRESSED_SIGNED_RG_RGTC2; proc1: SRGTC2_ToImf; proc2: UnsupportedFromImf));
  4196. procedure ConvertImage(const ASrc: Pointer; const ADst: Pointer; ASrcColorFormat, ADstColorFormat: Cardinal; ASrcDataType, ADstDataType: Cardinal; AWidth, AHeight: Integer);
  4197. var
  4198. ConvertToIntermediateFormat: TConvertToImfProc;
  4199. ConvertFromIntermediateFormat: TConvertFromInfProc;
  4200. i, size: Integer;
  4201. tempBuf: PIntermediateFormatArray;
  4202. begin
  4203. if AWidth < 1 then
  4204. Exit;
  4205. AHeight := MaxInteger(1, AHeight);
  4206. // Allocate memory
  4207. size := AWidth * AHeight * SizeOf(TIntermediateFormat);
  4208. GetMem(tempBuf, size);
  4209. FillChar(tempBuf^, size, $00);
  4210. // Find function to convert external format to intermediate format
  4211. ConvertToIntermediateFormat := UnsupportedToImf;
  4212. for i := 0 to high(cConvertTable) do
  4213. begin
  4214. if ASrcDataType = cConvertTable[i].type_ then
  4215. begin
  4216. ConvertToIntermediateFormat := cConvertTable[i].proc1;
  4217. break;
  4218. end;
  4219. end;
  4220. try
  4221. ConvertToIntermediateFormat(ASrc, tempBuf, ASrcColorFormat, AWidth, AHeight);
  4222. except
  4223. FreeMem(tempBuf);
  4224. raise;
  4225. end;
  4226. // Find function to convert intermediate format to external format
  4227. ConvertFromIntermediateFormat := UnsupportedFromImf;
  4228. for i := 0 to high(cConvertTable) do
  4229. begin
  4230. if ADstDataType = cConvertTable[i].type_ then
  4231. begin
  4232. ConvertFromIntermediateFormat := cConvertTable[i].proc2;
  4233. break;
  4234. end;
  4235. end;
  4236. try
  4237. ConvertFromIntermediateFormat(tempBuf, ADst, ADstColorFormat, AWidth, AHeight);
  4238. except
  4239. FreeMem(tempBuf);
  4240. raise;
  4241. end;
  4242. FreeMem(tempBuf);
  4243. end;
  4244. procedure RescaleImage(
  4245. const ASrc: Pointer;
  4246. const ADst: Pointer;
  4247. AColorFormat: Cardinal;
  4248. ADataType: Cardinal;
  4249. AFilter: TImageFilterFunction;
  4250. ASrcWidth, ASrcHeight, ADstWidth, ADstHeight: Integer);
  4251. var
  4252. ConvertToIntermediateFormat: TConvertToImfProc;
  4253. ConvertFromIntermediateFormat: TConvertFromInfProc;
  4254. i, j, k, n, size: Integer;
  4255. tempBuf1, tempBuf2, SourceLine, DestLine: PIntermediateFormatArray;
  4256. contrib: PCListList;
  4257. xscale, yscale: Single; // Zoom scale factors
  4258. width, fscale, weight: Single; // Filter calculation variables
  4259. center: Single; // Filter calculation variables
  4260. left, right: Integer; // Filter calculation variables
  4261. color1, color2: TIntermediateFormat;
  4262. begin
  4263. if (ASrcWidth < 1) or (ADstWidth < 1) then
  4264. Exit;
  4265. ASrcHeight := MaxInteger(1, ASrcHeight);
  4266. ADstHeight := MaxInteger(1, ADstHeight);
  4267. // Allocate memory
  4268. size := ASrcWidth * ASrcHeight * SizeOf(TIntermediateFormat);
  4269. GetMem(tempBuf1, size);
  4270. FillChar(tempBuf1^, size, $00);
  4271. // Find function to convert external format to intermediate format
  4272. ConvertToIntermediateFormat := UnsupportedToImf;
  4273. for i := 0 to high(cConvertTable) do
  4274. begin
  4275. if ADataType = cConvertTable[i].type_ then
  4276. begin
  4277. ConvertToIntermediateFormat := cConvertTable[i].proc1;
  4278. ConvertFromIntermediateFormat := cConvertTable[i].proc2;
  4279. break;
  4280. end;
  4281. end;
  4282. try
  4283. ConvertToIntermediateFormat(ASrc, tempBuf1, AColorFormat, ASrcWidth, ASrcHeight);
  4284. except
  4285. FreeMem(tempBuf1);
  4286. raise;
  4287. end;
  4288. // Rescale
  4289. if ASrcWidth = 1 then
  4290. xscale := ADstWidth / ASrcWidth
  4291. else
  4292. xscale := (ADstWidth - 1) / (ASrcWidth - 1);
  4293. if ASrcHeight = 1 then
  4294. yscale := ADstHeight / ASrcHeight
  4295. else
  4296. yscale := (ADstHeight - 1) / (ASrcHeight - 1);
  4297. // Pre-calculate filter contributions for a row
  4298. GetMem(contrib, ADstWidth * SizeOf(TCList));
  4299. // Horizontal sub-sampling
  4300. // Scales from bigger to smaller width
  4301. if xscale < 1.0 then
  4302. begin
  4303. width := vImageScaleFilterWidth / xscale;
  4304. fscale := 1.0 / xscale;
  4305. for i := 0 to ADstWidth - 1 do
  4306. begin
  4307. contrib^[i].n := 0;
  4308. GetMem(contrib^[i].p, Trunc(width * 2.0 + 1) * SizeOf(TContributor));
  4309. center := i / xscale;
  4310. left := floor(center - width);
  4311. right := ceil(center + width);
  4312. for j := left to right do
  4313. begin
  4314. weight := AFilter((center - j) / fscale) / fscale;
  4315. if weight = 0.0 then
  4316. continue;
  4317. if (j < 0) then
  4318. n := -j
  4319. else if (j >= ASrcWidth) then
  4320. n := ASrcWidth - j + ASrcWidth - 1
  4321. else
  4322. n := j;
  4323. k := contrib^[i].n;
  4324. contrib^[i].n := contrib^[i].n + 1;
  4325. contrib^[i].p^[k].pixel := n;
  4326. contrib^[i].p^[k].weight := weight;
  4327. end;
  4328. end;
  4329. end
  4330. else
  4331. // Horizontal super-sampling
  4332. // Scales from smaller to bigger width
  4333. begin
  4334. for i := 0 to ADstWidth - 1 do
  4335. begin
  4336. contrib^[i].n := 0;
  4337. GetMem(contrib^[i].p, Trunc(vImageScaleFilterWidth * 2.0 + 1) * SizeOf(TContributor));
  4338. center := i / xscale;
  4339. left := floor(center - vImageScaleFilterWidth);
  4340. right := ceil(center + vImageScaleFilterWidth);
  4341. for j := left to right do
  4342. begin
  4343. weight := AFilter(center - j);
  4344. if weight = 0.0 then
  4345. continue;
  4346. if (j < 0) then
  4347. n := -j
  4348. else if (j >= ASrcWidth) then
  4349. n := ASrcWidth - j + ASrcWidth - 1
  4350. else
  4351. n := j;
  4352. k := contrib^[i].n;
  4353. contrib^[i].n := contrib^[i].n + 1;
  4354. contrib^[i].p^[k].pixel := n;
  4355. contrib^[i].p^[k].weight := weight;
  4356. end;
  4357. end;
  4358. end;
  4359. size := ADstWidth * ASrcHeight * SizeOf(TIntermediateFormat);
  4360. GetMem(tempBuf2, size);
  4361. // Apply filter to sample horizontally from Src to Work
  4362. for k := 0 to ASrcHeight - 1 do
  4363. begin
  4364. SourceLine := @tempBuf1[k * ASrcWidth];
  4365. DestLine := @tempBuf2[k * ADstWidth];
  4366. for i := 0 to ADstWidth - 1 do
  4367. begin
  4368. color1 := cSuperBlack;
  4369. for j := 0 to contrib^[i].n - 1 do
  4370. begin
  4371. weight := contrib^[i].p^[j].weight;
  4372. if weight = 0.0 then
  4373. continue;
  4374. color2 := SourceLine[contrib^[i].p^[j].pixel];
  4375. color1.R := color1.R + color2.R * weight;
  4376. color1.G := color1.G + color2.G * weight;
  4377. color1.B := color1.B + color2.B * weight;
  4378. color1.A := color1.A + color2.A * weight;
  4379. end;
  4380. // Set new pixel value
  4381. DestLine[i] := color1;
  4382. end;
  4383. end;
  4384. // Free the memory allocated for horizontal filter weights
  4385. for i := 0 to ADstWidth - 1 do
  4386. FreeMem(contrib^[i].p);
  4387. FreeMem(contrib);
  4388. // Pre-calculate filter contributions for a column
  4389. GetMem(contrib, ADstHeight * SizeOf(TCList));
  4390. // Vertical sub-sampling
  4391. // Scales from bigger to smaller height
  4392. if yscale < 1.0 then
  4393. begin
  4394. width := vImageScaleFilterWidth / yscale;
  4395. fscale := 1.0 / yscale;
  4396. for i := 0 to ADstHeight - 1 do
  4397. begin
  4398. contrib^[i].n := 0;
  4399. GetMem(contrib^[i].p, Trunc(width * 2.0 + 1) * SizeOf(TContributor));
  4400. center := i / yscale;
  4401. left := floor(center - width);
  4402. right := ceil(center + width);
  4403. for j := left to right do
  4404. begin
  4405. weight := AFilter((center - j) / fscale) / fscale;
  4406. if weight = 0.0 then
  4407. continue;
  4408. if (j < 0) then
  4409. n := -j
  4410. else if (j >= ASrcHeight) then
  4411. n := MaxInteger(ASrcHeight - j + ASrcHeight - 1, 0)
  4412. else
  4413. n := j;
  4414. k := contrib^[i].n;
  4415. contrib^[i].n := contrib^[i].n + 1;
  4416. contrib^[i].p^[k].pixel := n;
  4417. contrib^[i].p^[k].weight := weight;
  4418. end;
  4419. end
  4420. end
  4421. else
  4422. // Vertical super-sampling
  4423. // Scales from smaller to bigger height
  4424. begin
  4425. for i := 0 to ADstHeight - 1 do
  4426. begin
  4427. contrib^[i].n := 0;
  4428. GetMem(contrib^[i].p, Trunc(vImageScaleFilterWidth * 2.0 + 1) * SizeOf(TContributor));
  4429. center := i / yscale;
  4430. left := floor(center - vImageScaleFilterWidth);
  4431. right := ceil(center + vImageScaleFilterWidth);
  4432. for j := left to right do
  4433. begin
  4434. weight := AFilter(center - j);
  4435. if weight = 0.0 then
  4436. continue;
  4437. if j < 0 then
  4438. n := -j
  4439. else if (j >= ASrcHeight) then
  4440. n := MaxInteger(ASrcHeight - j + ASrcHeight - 1, 0)
  4441. else
  4442. n := j;
  4443. k := contrib^[i].n;
  4444. contrib^[i].n := contrib^[i].n + 1;
  4445. contrib^[i].p^[k].pixel := n;
  4446. contrib^[i].p^[k].weight := weight;
  4447. end;
  4448. end;
  4449. end;
  4450. size := ADstWidth * ADstHeight * SizeOf(TIntermediateFormat);
  4451. ReallocMem(tempBuf1, size);
  4452. // Apply filter to sample vertically from Work to Dst
  4453. for k := 0 to ADstWidth - 1 do
  4454. begin
  4455. for i := 0 to ADstHeight - 1 do
  4456. begin
  4457. color1 := cSuperBlack;
  4458. for j := 0 to contrib^[i].n - 1 do
  4459. begin
  4460. weight := contrib^[i].p^[j].weight;
  4461. if weight = 0.0 then
  4462. continue;
  4463. color2 := tempBuf2[k + contrib^[i].p^[j].pixel * ADstWidth];
  4464. color1.R := color1.R + color2.R * weight;
  4465. color1.G := color1.G + color2.G * weight;
  4466. color1.B := color1.B + color2.B * weight;
  4467. color1.A := color1.A + color2.A * weight;
  4468. end;
  4469. tempBuf1[k + i * ADstWidth] := color1;
  4470. end;
  4471. end;
  4472. // Free the memory allocated for vertical filter weights
  4473. for i := 0 to ADstHeight - 1 do
  4474. FreeMem(contrib^[i].p);
  4475. FreeMem(contrib);
  4476. FreeMem(tempBuf2);
  4477. // Back to native image format
  4478. try
  4479. ConvertFromIntermediateFormat(tempBuf1, ADst, AColorFormat, ADstWidth, ADstHeight);
  4480. except
  4481. FreeMem(tempBuf1);
  4482. raise;
  4483. end;
  4484. FreeMem(tempBuf1);
  4485. end;
  4486. procedure Div2(var Value: Integer); inline;
  4487. begin
  4488. Value := Value div 2;
  4489. if Value = 0 then
  4490. Value := 1;
  4491. end;
  4492. procedure Build2DMipmap(
  4493. const ASrc: Pointer;
  4494. const ADst: TPointerArray;
  4495. AColorFormat: Cardinal;
  4496. ADataType: Cardinal;
  4497. AFilter: TImageFilterFunction;
  4498. ASrcWidth, ASrcHeight: Integer);
  4499. var
  4500. ConvertToIntermediateFormat: TConvertToImfProc;
  4501. ConvertFromIntermediateFormat: TConvertFromInfProc;
  4502. ADstWidth, ADstHeight: Integer;
  4503. i, j, k, n, size, level: Integer;
  4504. tempBuf1, tempBuf2, storePtr, SourceLine, DestLine: PIntermediateFormatArray;
  4505. contrib: PCListList;
  4506. xscale, yscale: Single;
  4507. width, fscale, weight: Single;
  4508. center: Single;
  4509. left, right: Integer;
  4510. color1, color2: TIntermediateFormat;
  4511. tempW, tempH: Integer;
  4512. begin
  4513. if ASrcWidth < 1 then
  4514. Exit;
  4515. ASrcHeight := MaxInteger(1, ASrcHeight);
  4516. // Allocate memory
  4517. tempW := ASrcWidth;
  4518. tempH := ASrcHeight;
  4519. size := 0;
  4520. for level := 0 to High(ADst) + 1 do
  4521. begin
  4522. Inc(size, tempW * tempH * SizeOf(TIntermediateFormat));
  4523. Div2(tempW);
  4524. Div2(tempH);
  4525. end;
  4526. GetMem(tempBuf1, size);
  4527. storePtr := tempBuf1;
  4528. FillChar(tempBuf1^, size, $00);
  4529. GetMem(tempBuf2, ASrcWidth * ASrcHeight * SizeOf(TIntermediateFormat));
  4530. // Find function to convert external format to intermediate format
  4531. ConvertToIntermediateFormat := UnsupportedToImf;
  4532. ConvertFromIntermediateFormat := UnsupportedFromImf;
  4533. for i := 0 to high(cConvertTable) do
  4534. begin
  4535. if ADataType = cConvertTable[i].type_ then
  4536. begin
  4537. ConvertToIntermediateFormat := cConvertTable[i].proc1;
  4538. ConvertFromIntermediateFormat := cConvertTable[i].proc2;
  4539. break;
  4540. end;
  4541. end;
  4542. try
  4543. ConvertToIntermediateFormat(ASrc, tempBuf1, AColorFormat, ASrcWidth, ASrcHeight);
  4544. except
  4545. FreeMem(tempBuf1);
  4546. raise;
  4547. end;
  4548. contrib := nil;
  4549. tempW := ASrcWidth;
  4550. tempH := ADstHeight;
  4551. try
  4552. // Downsampling
  4553. for level := 0 to High(ADst) do
  4554. begin
  4555. ADstWidth := ASrcWidth;
  4556. ADstHeight := ASrcHeight;
  4557. Div2(ADstWidth);
  4558. Div2(ADstHeight);
  4559. xscale := MaxFloat((ADstWidth - 1) / (ASrcWidth - 1), 0.25);
  4560. yscale := MaxFloat((ADstHeight - 1) / (ASrcHeight - 1), 0.25);
  4561. // Pre-calculate filter contributions for a row
  4562. ReallocMem(contrib, ADstWidth * SizeOf(TCList));
  4563. // Horizontal sub-sampling
  4564. // Scales from bigger to smaller width
  4565. width := vImageScaleFilterWidth / xscale;
  4566. fscale := 1.0 / xscale;
  4567. for i := 0 to ADstWidth - 1 do
  4568. begin
  4569. contrib^[i].n := 0;
  4570. GetMem(contrib^[i].p, Trunc(width * 2.0 + 1.0) * SizeOf(TContributor));
  4571. center := i / xscale;
  4572. left := floor(center - width);
  4573. right := ceil(center + width);
  4574. for j := left to right do
  4575. begin
  4576. weight := AFilter((center - j) / fscale) / fscale;
  4577. if weight = 0.0 then
  4578. continue;
  4579. if (j < 0) then
  4580. n := -j
  4581. else if (j >= ASrcWidth) then
  4582. n := MaxInteger(ASrcWidth - j + ASrcWidth - 1, 0)
  4583. else
  4584. n := j;
  4585. k := contrib^[i].n;
  4586. contrib^[i].n := contrib^[i].n + 1;
  4587. contrib^[i].p^[k].pixel := n;
  4588. contrib^[i].p^[k].weight := weight;
  4589. end;
  4590. end;
  4591. // Apply filter to sample horizontally from Src to Work
  4592. for k := 0 to ASrcHeight - 1 do
  4593. begin
  4594. SourceLine := @tempBuf1[k * ASrcWidth];
  4595. DestLine := @tempBuf2[k * ADstWidth];
  4596. for i := 0 to ADstWidth - 1 do
  4597. begin
  4598. color1 := cSuperBlack;
  4599. for j := 0 to contrib^[i].n - 1 do
  4600. begin
  4601. weight := contrib^[i].p^[j].weight;
  4602. if weight = 0.0 then
  4603. continue;
  4604. color2 := SourceLine[contrib^[i].p^[j].pixel];
  4605. color1.R := color1.R + color2.R * weight;
  4606. color1.G := color1.G + color2.G * weight;
  4607. color1.B := color1.B + color2.B * weight;
  4608. color1.A := color1.A + color2.A * weight;
  4609. end;
  4610. // Set new pixel value
  4611. DestLine[i] := color1;
  4612. end;
  4613. end;
  4614. // Free the memory allocated for horizontal filter weights
  4615. for i := 0 to ADstWidth - 1 do
  4616. FreeMem(contrib^[i].p);
  4617. // Pre-calculate filter contributions for a column
  4618. ReallocMem(contrib, ADstHeight * SizeOf(TCList));
  4619. // Vertical sub-sampling
  4620. // Scales from bigger to smaller height
  4621. width := vImageScaleFilterWidth / yscale;
  4622. fscale := 1.0 / yscale;
  4623. for i := 0 to ADstHeight - 1 do
  4624. begin
  4625. contrib^[i].n := 0;
  4626. GetMem(contrib^[i].p, Trunc(width * 2.0 + 1) * SizeOf(TContributor));
  4627. center := i / yscale;
  4628. left := floor(center - width);
  4629. right := ceil(center + width);
  4630. for j := left to right do
  4631. begin
  4632. weight := AFilter((center - j) / fscale) / fscale;
  4633. if weight = 0.0 then
  4634. continue;
  4635. if (j < 0) then
  4636. n := -j
  4637. else if (j >= ASrcHeight) then
  4638. n := MaxInteger(ASrcHeight - j + ASrcHeight - 1, 0)
  4639. else
  4640. n := j;
  4641. k := contrib^[i].n;
  4642. contrib^[i].n := contrib^[i].n + 1;
  4643. contrib^[i].p^[k].pixel := n;
  4644. contrib^[i].p^[k].weight := weight;
  4645. end;
  4646. end;
  4647. size := ASrcWidth * ASrcHeight * SizeOf(TIntermediateFormat);
  4648. Inc(PByte(tempBuf1), size);
  4649. // Apply filter to sample vertically from Work to Dst
  4650. for k := 0 to ADstWidth - 1 do
  4651. begin
  4652. for i := 0 to ADstHeight - 1 do
  4653. begin
  4654. color1 := cSuperBlack;
  4655. for j := 0 to contrib^[i].n - 1 do
  4656. begin
  4657. weight := contrib^[i].p^[j].weight;
  4658. if weight = 0.0 then
  4659. continue;
  4660. n := k + contrib^[i].p^[j].pixel * ADstWidth;
  4661. color2 := tempBuf2[n];
  4662. color1.R := color1.R + color2.R * weight;
  4663. color1.G := color1.G + color2.G * weight;
  4664. color1.B := color1.B + color2.B * weight;
  4665. color1.A := color1.A + color2.A * weight;
  4666. end;
  4667. tempBuf1[k + i * ADstWidth] := color1;
  4668. end;
  4669. end;
  4670. // Free the memory allocated for vertical filter weights
  4671. for i := 0 to ADstHeight - 1 do
  4672. FreeMem(contrib^[i].p);
  4673. ASrcWidth := ADstWidth;
  4674. ASrcHeight := ADstHeight;
  4675. // Back to native image format
  4676. ConvertFromIntermediateFormat(
  4677. tempBuf1, ADst[level], AColorFormat, ASrcWidth, ASrcHeight);
  4678. end;
  4679. finally
  4680. if Assigned(contrib) then
  4681. FreeMem(contrib);
  4682. FreeMem(tempBuf2);
  4683. FreeMem(storePtr);
  4684. end;
  4685. end;
  4686. procedure AlphaGammaBrightCorrection(
  4687. const ASrc: Pointer;
  4688. AColorFormat: Cardinal;
  4689. ADataType: Cardinal;
  4690. ASrcWidth, ASrcHeight: Integer;
  4691. anAlphaProc: TImageAlphaProc;
  4692. ABrightness: Single;
  4693. AGamma: Single);
  4694. var
  4695. ConvertToIntermediateFormat: TConvertToImfProc;
  4696. ConvertFromIntermediateFormat: TConvertFromInfProc;
  4697. tempBuf1: PIntermediateFormatArray;
  4698. Size, I: Integer;
  4699. begin
  4700. if ASrcWidth < 1 then
  4701. Exit;
  4702. ASrcHeight := MaxInteger(1, ASrcHeight);
  4703. Size := ASrcWidth * ASrcHeight;
  4704. GetMem(tempBuf1, Size * SizeOf(TIntermediateFormat));
  4705. // Find function to convert external format to intermediate format
  4706. ConvertToIntermediateFormat := UnsupportedToImf;
  4707. ConvertFromIntermediateFormat := UnsupportedFromImf;
  4708. for i := 0 to high(cConvertTable) do
  4709. begin
  4710. if ADataType = cConvertTable[i].type_ then
  4711. begin
  4712. ConvertToIntermediateFormat := cConvertTable[i].proc1;
  4713. ConvertFromIntermediateFormat := cConvertTable[i].proc2;
  4714. break;
  4715. end;
  4716. end;
  4717. try
  4718. ConvertToIntermediateFormat(
  4719. ASrc, tempBuf1, AColorFormat, ASrcWidth, ASrcHeight);
  4720. vTopLeftColor := tempBuf1[0];
  4721. vBottomRightColor := tempBuf1[Size-1];
  4722. if Assigned(anAlphaProc) then
  4723. for I := Size - 1 downto 0 do
  4724. anAlphaProc(tempBuf1[I]);
  4725. if ABrightness <> 1.0 then
  4726. for I := Size - 1 downto 0 do
  4727. with tempBuf1[I] do
  4728. begin
  4729. R := R * ABrightness;
  4730. G := G * ABrightness;
  4731. B := B * ABrightness;
  4732. end;
  4733. if AGamma <> 1.0 then
  4734. for I := Size - 1 downto 0 do
  4735. with tempBuf1[I] do
  4736. begin
  4737. R := Power(R, AGamma);
  4738. G := Power(G, AGamma);
  4739. B := Power(B, AGamma);
  4740. end;
  4741. // Back to native image format
  4742. ConvertFromIntermediateFormat(
  4743. tempBuf1, ASrc, AColorFormat, ASrcWidth, ASrcHeight);
  4744. except
  4745. FreeMem(tempBuf1);
  4746. raise;
  4747. end;
  4748. FreeMem(tempBuf1);
  4749. end;
  4750. end.