GLS.ImageUtils.pas 137 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107
  1. //
  2. // The multimedia graphics platform 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 GLScene.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.