GR32.pas 210 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686
  1. unit GR32;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Alex A. Denisov
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2009
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. * Michael Hansen <[email protected]>
  32. * Andre Beckedorf <[email protected]>
  33. * Mattias Andersson <[email protected]>
  34. * J. Tulach <tulach at position.cz>
  35. * Jouni Airaksinen <markvera at spacesynth.net>
  36. * Timothy Weber <teejaydub at users.sourceforge.net>
  37. *
  38. * ***** END LICENSE BLOCK ***** *)
  39. interface
  40. {$I GR32.inc}
  41. uses
  42. {$IFDEF FPC} LCLIntf, LCLType, Types, {$ELSE}
  43. {$IFDEF COMPILERXE2_UP}UITypes, Types, {$ENDIF} Windows, {$ENDIF}
  44. Vcl.Controls,
  45. Vcl.Graphics,
  46. System.Classes,
  47. System.SysUtils;
  48. { Version Control }
  49. const
  50. Graphics32Version = '3.0';
  51. { 32-bit Color }
  52. type
  53. PColor32 = ^TColor32;
  54. TColor32 = type Cardinal;
  55. PColor32Array = ^TColor32Array;
  56. TColor32Array = array [0..0] of TColor32;
  57. TArrayOfColor32 = array of TColor32;
  58. {$IFNDEF RGBA_FORMAT}
  59. TColor32Component = (ccBlue, ccGreen, ccRed, ccAlpha);
  60. {$ELSE}
  61. TColor32Component = (ccRed, ccGreen, ccBlue, ccAlpha);
  62. {$ENDIF}
  63. TColor32Components = set of TColor32Component;
  64. PColor32Entry = ^TColor32Entry;
  65. TColor32Entry = packed record
  66. case Integer of
  67. {$IFNDEF RGBA_FORMAT}
  68. 0: (B, G, R, A: Byte);
  69. {$ELSE}
  70. 0: (R, G, B, A: Byte);
  71. {$ENDIF}
  72. 1: (ARGB: TColor32);
  73. 2: (Planes: array[0..3] of Byte);
  74. 3: (Components: array[TColor32Component] of Byte);
  75. end;
  76. PColor32EntryArray = ^TColor32EntryArray;
  77. TColor32EntryArray = array [0..0] of TColor32Entry;
  78. TArrayOfColor32Entry = array of TColor32Entry;
  79. PPalette32 = ^TPalette32;
  80. TPalette32 = array [Byte] of TColor32;
  81. const
  82. // Some predefined color constants
  83. clBlack32 = TColor32({$IFNDEF RGBA_FORMAT} $FF000000 {$ELSE} $FF000000 {$ENDIF});
  84. clDimGray32 = TColor32({$IFNDEF RGBA_FORMAT} $FF3F3F3F {$ELSE} $FF3F3F3F {$ENDIF});
  85. clGray32 = TColor32({$IFNDEF RGBA_FORMAT} $FF7F7F7F {$ELSE} $FF7F7F7F {$ENDIF});
  86. clLightGray32 = TColor32({$IFNDEF RGBA_FORMAT} $FFBFBFBF {$ELSE} $FFBFBFBF {$ENDIF});
  87. clWhite32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFFFFF {$ELSE} $FFFFFFFF {$ENDIF});
  88. clMaroon32 = TColor32({$IFNDEF RGBA_FORMAT} $FF7F0000 {$ELSE} $FF00007F {$ENDIF});
  89. clGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF007F00 {$ELSE} $FF007F00 {$ENDIF});
  90. clOlive32 = TColor32({$IFNDEF RGBA_FORMAT} $FF7F7F00 {$ELSE} $FF007F7F {$ENDIF});
  91. clNavy32 = TColor32({$IFNDEF RGBA_FORMAT} $FF00007F {$ELSE} $FF7F0000 {$ENDIF});
  92. clPurple32 = TColor32({$IFNDEF RGBA_FORMAT} $FF7F007F {$ELSE} $FF7F007F {$ENDIF});
  93. clTeal32 = TColor32({$IFNDEF RGBA_FORMAT} $FF007F7F {$ELSE} $FF7F7F00 {$ENDIF});
  94. clRed32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFF0000 {$ELSE} $FF0000FF {$ENDIF});
  95. clLime32 = TColor32({$IFNDEF RGBA_FORMAT} $FF00FF00 {$ELSE} $FF00FF00 {$ENDIF});
  96. clYellow32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFFF00 {$ELSE} $FF00FFFF {$ENDIF});
  97. clBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF0000FF {$ELSE} $FFFF0000 {$ENDIF});
  98. clFuchsia32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFF00FF {$ELSE} $FFFF00FF {$ENDIF});
  99. clAqua32 = TColor32({$IFNDEF RGBA_FORMAT} $FF00FFFF {$ELSE} $FFFFFF00 {$ENDIF});
  100. clAliceBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF0F8FF {$ELSE} $FFFFF8F0 {$ENDIF});
  101. clAntiqueWhite32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFAEBD7 {$ELSE} $FFD7EBFA {$ENDIF});
  102. clAquamarine32 = TColor32({$IFNDEF RGBA_FORMAT} $FF7FFFD4 {$ELSE} $FFD4FF7F {$ENDIF});
  103. clAzure32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF0FFFF {$ELSE} $FFFFFFF0 {$ENDIF});
  104. clBeige32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF5F5DC {$ELSE} $FFDCF5F5 {$ENDIF});
  105. clBisque32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFE4C4 {$ELSE} $FFC4E4FF {$ENDIF});
  106. clBlancheDalmond32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFEBCD {$ELSE} $FFCDEBFF {$ENDIF});
  107. clBlueViolet32 = TColor32({$IFNDEF RGBA_FORMAT} $FF8A2BE2 {$ELSE} $FFE22B8A {$ENDIF});
  108. clBrown32 = TColor32({$IFNDEF RGBA_FORMAT} $FFA52A2A {$ELSE} $FF2A2AA5 {$ENDIF});
  109. clBurlyWood32 = TColor32({$IFNDEF RGBA_FORMAT} $FFDEB887 {$ELSE} $FF87B8DE {$ENDIF});
  110. clCadetblue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF5F9EA0 {$ELSE} $FFA09E5F {$ENDIF});
  111. clChartReuse32 = TColor32({$IFNDEF RGBA_FORMAT} $FF7FFF00 {$ELSE} $FF00FF7F {$ENDIF});
  112. clChocolate32 = TColor32({$IFNDEF RGBA_FORMAT} $FFD2691E {$ELSE} $FF1E69D2 {$ENDIF});
  113. clCoral32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFF7F50 {$ELSE} $FF507FFF {$ENDIF});
  114. clCornFlowerBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF6495ED {$ELSE} $FFED9564 {$ENDIF});
  115. clCornSilk32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFF8DC {$ELSE} $FFDCF8FF {$ENDIF});
  116. clCrimson32 = TColor32({$IFNDEF RGBA_FORMAT} $FFDC143C {$ELSE} $FF3C14DC {$ENDIF});
  117. clDarkBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF00008B {$ELSE} $FF8B0000 {$ENDIF});
  118. clDarkCyan32 = TColor32({$IFNDEF RGBA_FORMAT} $FF008B8B {$ELSE} $FF8B8B00 {$ENDIF});
  119. clDarkGoldenRod32 = TColor32({$IFNDEF RGBA_FORMAT} $FFB8860B {$ELSE} $FF0B86B8 {$ENDIF});
  120. clDarkGray32 = TColor32({$IFNDEF RGBA_FORMAT} $FFA9A9A9 {$ELSE} $FFA9A9A9 {$ENDIF});
  121. clDarkGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF006400 {$ELSE} $FF006400 {$ENDIF});
  122. clDarkGrey32 = TColor32({$IFNDEF RGBA_FORMAT} $FFA9A9A9 {$ELSE} $FFA9A9A9 {$ENDIF});
  123. clDarkKhaki32 = TColor32({$IFNDEF RGBA_FORMAT} $FFBDB76B {$ELSE} $FF6BB7BD {$ENDIF});
  124. clDarkMagenta32 = TColor32({$IFNDEF RGBA_FORMAT} $FF8B008B {$ELSE} $FF8B008B {$ENDIF});
  125. clDarkOliveGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF556B2F {$ELSE} $FF2F6B55 {$ENDIF});
  126. clDarkOrange32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFF8C00 {$ELSE} $FF008CFF {$ENDIF});
  127. clDarkOrchid32 = TColor32({$IFNDEF RGBA_FORMAT} $FF9932CC {$ELSE} $FFCC3299 {$ENDIF});
  128. clDarkRed32 = TColor32({$IFNDEF RGBA_FORMAT} $FF8B0000 {$ELSE} $FF00008B {$ENDIF});
  129. clDarkSalmon32 = TColor32({$IFNDEF RGBA_FORMAT} $FFE9967A {$ELSE} $FF7A96E9 {$ENDIF});
  130. clDarkSeaGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF8FBC8F {$ELSE} $FF8FBC8F {$ENDIF});
  131. clDarkSlateBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF483D8B {$ELSE} $FF8B3D48 {$ENDIF});
  132. clDarkSlateGray32 = TColor32({$IFNDEF RGBA_FORMAT} $FF2F4F4F {$ELSE} $FF4F4F2F {$ENDIF});
  133. clDarkSlateGrey32 = TColor32({$IFNDEF RGBA_FORMAT} $FF2F4F4F {$ELSE} $FF4F4F2F {$ENDIF});
  134. clDarkTurquoise32 = TColor32({$IFNDEF RGBA_FORMAT} $FF00CED1 {$ELSE} $FFD1CE00 {$ENDIF});
  135. clDarkViolet32 = TColor32({$IFNDEF RGBA_FORMAT} $FF9400D3 {$ELSE} $FFD30094 {$ENDIF});
  136. clDeepPink32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFF1493 {$ELSE} $FF9314FF {$ENDIF});
  137. clDeepSkyBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF00BFFF {$ELSE} $FFFFBF00 {$ENDIF});
  138. clDodgerBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF1E90FF {$ELSE} $FFFF901E {$ENDIF});
  139. clFireBrick32 = TColor32({$IFNDEF RGBA_FORMAT} $FFB22222 {$ELSE} $FF2222B2 {$ENDIF});
  140. clFloralWhite32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFFAF0 {$ELSE} $FFF0FAFF {$ENDIF});
  141. clGainsBoro32 = TColor32({$IFNDEF RGBA_FORMAT} $FFDCDCDC {$ELSE} $FFDCDCDC {$ENDIF});
  142. clGhostWhite32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF8F8FF {$ELSE} $FFFFF8F8 {$ENDIF});
  143. clGold32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFD700 {$ELSE} $FF00D7FF {$ENDIF});
  144. clGoldenRod32 = TColor32({$IFNDEF RGBA_FORMAT} $FFDAA520 {$ELSE} $FF20A5DA {$ENDIF});
  145. clGreenYellow32 = TColor32({$IFNDEF RGBA_FORMAT} $FFADFF2F {$ELSE} $FF2FFFAD {$ENDIF});
  146. clGrey32 = TColor32({$IFNDEF RGBA_FORMAT} $FF808080 {$ELSE} $FF808080 {$ENDIF});
  147. clHoneyDew32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF0FFF0 {$ELSE} $FFF0FFF0 {$ENDIF});
  148. clHotPink32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFF69B4 {$ELSE} $FFB469FF {$ENDIF});
  149. clIndianRed32 = TColor32({$IFNDEF RGBA_FORMAT} $FFCD5C5C {$ELSE} $FF5C5CCD {$ENDIF});
  150. clIndigo32 = TColor32({$IFNDEF RGBA_FORMAT} $FF4B0082 {$ELSE} $FF82004B {$ENDIF});
  151. clIvory32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFFFF0 {$ELSE} $FFF0FFFF {$ENDIF});
  152. clKhaki32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF0E68C {$ELSE} $FF8CE6F0 {$ENDIF});
  153. clLavender32 = TColor32({$IFNDEF RGBA_FORMAT} $FFE6E6FA {$ELSE} $FFFAE6E6 {$ENDIF});
  154. clLavenderBlush32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFF0F5 {$ELSE} $FFF5F0FF {$ENDIF});
  155. clLawnGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF7CFC00 {$ELSE} $FF00FC7C {$ENDIF});
  156. clLemonChiffon32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFFACD {$ELSE} $FFCDFAFF {$ENDIF});
  157. clLightBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FFADD8E6 {$ELSE} $FFE6D8AD {$ENDIF});
  158. clLightCoral32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF08080 {$ELSE} $FF8080F0 {$ENDIF});
  159. clLightCyan32 = TColor32({$IFNDEF RGBA_FORMAT} $FFE0FFFF {$ELSE} $FFFFFFE0 {$ENDIF});
  160. clLightGoldenRodYellow32= TColor32({$IFNDEF RGBA_FORMAT} $FFFAFAD2 {$ELSE} $FFD2FAFA {$ENDIF});
  161. clLightGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF90EE90 {$ELSE} $FF90EE90 {$ENDIF});
  162. clLightGrey32 = TColor32({$IFNDEF RGBA_FORMAT} $FFD3D3D3 {$ELSE} $FFD3D3D3 {$ENDIF});
  163. clLightPink32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFB6C1 {$ELSE} $FFC1B6FF {$ENDIF});
  164. clLightSalmon32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFA07A {$ELSE} $FF7AA0FF {$ENDIF});
  165. clLightSeagreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF20B2AA {$ELSE} $FFAAB220 {$ENDIF});
  166. clLightSkyblue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF87CEFA {$ELSE} $FFFACE87 {$ENDIF});
  167. clLightSlategray32 = TColor32({$IFNDEF RGBA_FORMAT} $FF778899 {$ELSE} $FF998877 {$ENDIF});
  168. clLightSlategrey32 = TColor32({$IFNDEF RGBA_FORMAT} $FF778899 {$ELSE} $FF998877 {$ENDIF});
  169. clLightSteelblue32 = TColor32({$IFNDEF RGBA_FORMAT} $FFB0C4DE {$ELSE} $FFDEC4B0 {$ENDIF});
  170. clLightYellow32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFFFE0 {$ELSE} $FFE0FFFF {$ENDIF});
  171. clLtGray32 = TColor32({$IFNDEF RGBA_FORMAT} $FFC0C0C0 {$ELSE} $FFC0C0C0 {$ENDIF});
  172. clMedGray32 = TColor32({$IFNDEF RGBA_FORMAT} $FFA0A0A4 {$ELSE} $FFA4A0A0 {$ENDIF});
  173. clDkGray32 = TColor32({$IFNDEF RGBA_FORMAT} $FF808080 {$ELSE} $FF808080 {$ENDIF});
  174. clMoneyGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FFC0DCC0 {$ELSE} $FFC0DCC0 {$ENDIF});
  175. clLegacySkyBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FFA6CAF0 {$ELSE} $FFF0CAA6 {$ENDIF});
  176. clCream32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFFBF0 {$ELSE} $FFF0FBFF {$ENDIF});
  177. clLimeGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF32CD32 {$ELSE} $FF32CD32 {$ENDIF});
  178. clLinen32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFAF0E6 {$ELSE} $FFE6F0FA {$ENDIF});
  179. clMediumAquamarine32 = TColor32({$IFNDEF RGBA_FORMAT} $FF66CDAA {$ELSE} $FFAACD66 {$ENDIF});
  180. clMediumBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF0000CD {$ELSE} $FFCD0000 {$ENDIF});
  181. clMediumOrchid32 = TColor32({$IFNDEF RGBA_FORMAT} $FFBA55D3 {$ELSE} $FFD355BA {$ENDIF});
  182. clMediumPurple32 = TColor32({$IFNDEF RGBA_FORMAT} $FF9370DB {$ELSE} $FFDB7093 {$ENDIF});
  183. clMediumSeaGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF3CB371 {$ELSE} $FF71B33C {$ENDIF});
  184. clMediumSlateBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF7B68EE {$ELSE} $FFEE687B {$ENDIF});
  185. clMediumSpringGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF00FA9A {$ELSE} $FF9AFA00 {$ENDIF});
  186. clMediumTurquoise32 = TColor32({$IFNDEF RGBA_FORMAT} $FF48D1CC {$ELSE} $FFCCD148 {$ENDIF});
  187. clMediumVioletRed32 = TColor32({$IFNDEF RGBA_FORMAT} $FFC71585 {$ELSE} $FF8515C7 {$ENDIF});
  188. clMidnightBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF191970 {$ELSE} $FF701919 {$ENDIF});
  189. clMintCream32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF5FFFA {$ELSE} $FFFAFFF5 {$ENDIF});
  190. clMistyRose32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFE4E1 {$ELSE} $FFE1E4FF {$ENDIF});
  191. clMoccasin32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFE4B5 {$ELSE} $FFB5E4FF {$ENDIF});
  192. clNavajoWhite32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFDEAD {$ELSE} $FFADDEFF {$ENDIF});
  193. clOldLace32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFDF5E6 {$ELSE} $FFE6F5FD {$ENDIF});
  194. clOliveDrab32 = TColor32({$IFNDEF RGBA_FORMAT} $FF6B8E23 {$ELSE} $FF238E6B {$ENDIF});
  195. clOrange32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFA500 {$ELSE} $FF00A5FF {$ENDIF});
  196. clOrangeRed32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFF4500 {$ELSE} $FF0045FF {$ENDIF});
  197. clOrchid32 = TColor32({$IFNDEF RGBA_FORMAT} $FFDA70D6 {$ELSE} $FFD670DA {$ENDIF});
  198. clPaleGoldenRod32 = TColor32({$IFNDEF RGBA_FORMAT} $FFEEE8AA {$ELSE} $FFAAE8EE {$ENDIF});
  199. clPaleGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF98FB98 {$ELSE} $FF98FB98 {$ENDIF});
  200. clPaleTurquoise32 = TColor32({$IFNDEF RGBA_FORMAT} $FFAFEEEE {$ELSE} $FFEEEEAF {$ENDIF});
  201. clPaleVioletred32 = TColor32({$IFNDEF RGBA_FORMAT} $FFDB7093 {$ELSE} $FF9370DB {$ENDIF});
  202. clPapayaWhip32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFEFD5 {$ELSE} $FFD5EFFF {$ENDIF});
  203. clPeachPuff32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFDAB9 {$ELSE} $FFB9DAFF {$ENDIF});
  204. clPeru32 = TColor32({$IFNDEF RGBA_FORMAT} $FFCD853F {$ELSE} $FF3F85CD {$ENDIF});
  205. clPlum32 = TColor32({$IFNDEF RGBA_FORMAT} $FFDDA0DD {$ELSE} $FFDDA0DD {$ENDIF});
  206. clPowderBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FFB0E0E6 {$ELSE} $FFE6E0B0 {$ENDIF});
  207. clRosyBrown32 = TColor32({$IFNDEF RGBA_FORMAT} $FFBC8F8F {$ELSE} $FF8F8FBC {$ENDIF});
  208. clRoyalBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF4169E1 {$ELSE} $FFE16941 {$ENDIF});
  209. clSaddleBrown32 = TColor32({$IFNDEF RGBA_FORMAT} $FF8B4513 {$ELSE} $FF13458B {$ENDIF});
  210. clSalmon32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFA8072 {$ELSE} $FF7280FA {$ENDIF});
  211. clSandyBrown32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF4A460 {$ELSE} $FF60A4F4 {$ENDIF});
  212. clSeaGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF2E8B57 {$ELSE} $FF578B2E {$ENDIF});
  213. clSeaShell32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFF5EE {$ELSE} $FFEEF5FF {$ENDIF});
  214. clSienna32 = TColor32({$IFNDEF RGBA_FORMAT} $FFA0522D {$ELSE} $FF2D52A0 {$ENDIF});
  215. clSilver32 = TColor32({$IFNDEF RGBA_FORMAT} $FFC0C0C0 {$ELSE} $FFC0C0C0 {$ENDIF});
  216. clSkyblue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF87CEEB {$ELSE} $FFEBCE87 {$ENDIF});
  217. clSlateBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF6A5ACD {$ELSE} $FFCD5A6A {$ENDIF});
  218. clSlateGray32 = TColor32({$IFNDEF RGBA_FORMAT} $FF708090 {$ELSE} $FF908070 {$ENDIF});
  219. clSlateGrey32 = TColor32({$IFNDEF RGBA_FORMAT} $FF708090 {$ELSE} $FF908070 {$ENDIF});
  220. clSnow32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFFFAFA {$ELSE} $FFFAFAFF {$ENDIF});
  221. clSpringgreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF00FF7F {$ELSE} $FF7FFF00 {$ENDIF});
  222. clSteelblue32 = TColor32({$IFNDEF RGBA_FORMAT} $FF4682B4 {$ELSE} $FFB48246 {$ENDIF});
  223. clTan32 = TColor32({$IFNDEF RGBA_FORMAT} $FFD2B48C {$ELSE} $FF8CB4D2 {$ENDIF});
  224. clThistle32 = TColor32({$IFNDEF RGBA_FORMAT} $FFD8BFD8 {$ELSE} $FFD8BFD8 {$ENDIF});
  225. clTomato32 = TColor32({$IFNDEF RGBA_FORMAT} $FFFF6347 {$ELSE} $FF4763FF {$ENDIF});
  226. clTurquoise32 = TColor32({$IFNDEF RGBA_FORMAT} $FF40E0D0 {$ELSE} $FFD0E040 {$ENDIF});
  227. clViolet32 = TColor32({$IFNDEF RGBA_FORMAT} $FFEE82EE {$ELSE} $FFEE82EE {$ENDIF});
  228. clWheat32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF5DEB3 {$ELSE} $FFB3DEF5 {$ENDIF});
  229. clWhitesmoke32 = TColor32({$IFNDEF RGBA_FORMAT} $FFF5F5F5 {$ELSE} $FFF5F5F5 {$ENDIF});
  230. clYellowgreen32 = TColor32({$IFNDEF RGBA_FORMAT} $FF9ACD32 {$ELSE} $FF32CD9A {$ENDIF});
  231. // Some semi-transparent color constants
  232. clTrWhite32 = TColor32({$IFNDEF RGBA_FORMAT} $7FFFFFFF {$ELSE} $7FFFFFFF {$ENDIF});
  233. clTrGray32 = TColor32({$IFNDEF RGBA_FORMAT} $7F7F7F7F {$ELSE} $7F7F7F7F {$ENDIF});
  234. clTrBlack32 = TColor32({$IFNDEF RGBA_FORMAT} $7F000000 {$ELSE} $7F000000 {$ENDIF});
  235. clTrRed32 = TColor32({$IFNDEF RGBA_FORMAT} $7FFF0000 {$ELSE} $7F0000FF {$ENDIF});
  236. clTrGreen32 = TColor32({$IFNDEF RGBA_FORMAT} $7F00FF00 {$ELSE} $7F00FF00 {$ENDIF});
  237. clTrBlue32 = TColor32({$IFNDEF RGBA_FORMAT} $7F0000FF {$ELSE} $7FFF0000 {$ENDIF});
  238. // Color construction and conversion functions
  239. function Color32(WinColor: TColor): TColor32; overload;
  240. function Color32(R, G, B: Byte; A: Byte = $FF): TColor32; overload;
  241. function Color32(Index: Byte; var Palette: TPalette32): TColor32; overload;
  242. function Gray32(Intensity: Byte; Alpha: Byte = $FF): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  243. function WinColor(Color32: TColor32): TColor;
  244. function ArrayOfColor32(const Colors: array of TColor32): TArrayOfColor32;
  245. // Unconditionally swap R & B channel, leave rest alone
  246. function SwapRedBlue(Color32: TColor32): TColor32;
  247. procedure SwapRedBlueMem(var Color32: TColor32);
  248. // Conditional swaps. Depends on RGBA_FORMAT define.
  249. function Color32ToBGRA(Color32: TColor32): DWORD; {$IFDEF USEINLINING} inline; {$ENDIF}
  250. procedure Color32ToBGRAMem(var Color32: TColor32); {$IFDEF USEINLINING} inline; {$ENDIF}
  251. function Color32ToRGBA(Color32: TColor32): DWORD; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  252. procedure Color32ToRGBAMem(var Color32: TColor32); {$IFDEF USEINLINING} inline; {$ENDIF}
  253. function BGRAToColor32(BGRA: DWORD): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  254. procedure BGRAToColor32Mem(var BGRA: DWORD); {$IFDEF USEINLINING} inline; {$ENDIF}
  255. function RGBAToColor32(RGBA: DWORD): TColor32; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  256. procedure RGBAToColor32mem(var RGBA: DWORD); {$IFDEF USEINLINING} inline; {$ENDIF}
  257. // Color component access
  258. procedure Color32ToRGB(Color32: TColor32; var R, G, B: Byte);
  259. procedure Color32ToRGBA(Color32: TColor32; var R, G, B, A: Byte); overload;
  260. function Color32Components(R, G, B, A: Boolean): TColor32Components;
  261. function RedComponent(Color32: TColor32): Integer; {$IFDEF USEINLINING} inline; {$ENDIF}
  262. function GreenComponent(Color32: TColor32): Integer; {$IFDEF USEINLINING} inline; {$ENDIF}
  263. function BlueComponent(Color32: TColor32): Integer; {$IFDEF USEINLINING} inline; {$ENDIF}
  264. function AlphaComponent(Color32: TColor32): Integer; {$IFDEF USEINLINING} inline; {$ENDIF}
  265. function Intensity(Color32: TColor32): Integer; {$IFDEF USEINLINING} inline; {$ENDIF}
  266. function InvertColor(Color32: TColor32): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  267. function SetAlpha(Color32: TColor32; NewAlpha: Integer): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  268. procedure ModifyAlpha(var Color32: TColor32; NewAlpha: Byte); {$IFDEF USEINLINING} inline; {$ENDIF}
  269. procedure ScaleAlpha(var Color32: TColor32; Scale: Single); {$IFDEF USEINLINING} inline; {$ENDIF}
  270. // Color space conversion
  271. function HSLtoRGB(H, S, L: Single; A: Integer = 255): TColor32; overload;
  272. function HSLtoRGB(H, S, L: Integer; A: Integer = 255): TColor32; overload;
  273. procedure RGBtoHSL(RGB: TColor32; out H, S, L : Single); overload;
  274. procedure RGBtoHSL(RGB: TColor32; out H, S, L: Byte); overload;
  275. function HSVtoRGB(H, S, V: Single; A: Integer = 255): TColor32;
  276. procedure RGBToHSV(Color: TColor32; out H, S, V: Single);
  277. {$IFNDEF PLATFORM_INDEPENDENT}
  278. // Palette conversion functions
  279. {$IFNDEF RGBA_FORMAT}
  280. function WinPalette(const P: TPalette32): HPALETTE;
  281. {$ENDIF RGBA_FORMAT}
  282. {$ENDIF}
  283. { A fixed-point type }
  284. type
  285. // This type has data bits arrangement compatible with Windows.TFixed
  286. PFixed = ^TFixed;
  287. TFixed = type Integer;
  288. {$NODEFINE TFixed}
  289. {$NODEFINE PFixedRec}
  290. PFixedRec = ^TFixedRec;
  291. {$NODEFINE TFixedRec}
  292. TFixedRec = packed record
  293. case Integer of
  294. 0: (Fixed: TFixed);
  295. 1: (Frac: Word; Int: SmallInt);
  296. end;
  297. PFixedArray = ^TFixedArray;
  298. TFixedArray = array [0..0] of TFixed;
  299. PArrayOfFixed = ^TArrayOfFixed;
  300. TArrayOfFixed = array of TFixed;
  301. PArrayOfArrayOfFixed = ^TArrayOfArrayOfFixed;
  302. TArrayOfArrayOfFixed = array of TArrayOfFixed;
  303. // TFloat determines the precision level for certain floating-point operations
  304. PFloat = ^TFloat;
  305. TFloat = Single;
  306. { Other dynamic arrays }
  307. type
  308. PByteArray = ^TByteArray;
  309. TByteArray = array [0..0] of Byte;
  310. PArrayOfByte = ^TArrayOfByte;
  311. TArrayOfByte = array of Byte;
  312. PWordArray = ^TWordArray;
  313. TWordArray = array [0..0] of Word;
  314. PArrayOfWord = ^TArrayOfWord;
  315. TArrayOfWord = array of Word;
  316. PIntegerArray = ^TIntegerArray;
  317. TIntegerArray = array [0..0] of Integer;
  318. PArrayOfInteger = ^TArrayOfInteger;
  319. TArrayOfInteger = array of Integer;
  320. PArrayOfArrayOfInteger = ^TArrayOfArrayOfInteger;
  321. TArrayOfArrayOfInteger = array of TArrayOfInteger;
  322. PCardinalArray = ^TCardinalArray;
  323. TCardinalArray = array [0..0] of Cardinal;
  324. PArrayOfCardinal = ^TArrayOfCardinal;
  325. TArrayOfCardinal = array of Cardinal;
  326. PArrayOfArrayOfCardinal = ^TArrayOfArrayOfCardinal;
  327. TArrayOfArrayOfCardinal = array of TArrayOfCardinal;
  328. PSingleArray = ^TSingleArray;
  329. TSingleArray = array [0..0] of Single;
  330. PArrayOfSingle = ^TArrayOfSingle;
  331. TArrayOfSingle = array of Single;
  332. PFloatArray = ^TFloatArray;
  333. TFloatArray = array [0..0] of TFloat;
  334. PArrayOfFloat = ^TArrayOfFloat;
  335. TArrayOfFloat = array of TFloat;
  336. const
  337. // Fixed point math constants
  338. FixedOne = $10000;
  339. FixedHalf = $7FFF;
  340. FixedPI = Round(PI * FixedOne);
  341. FixedToFloat = 1 / FixedOne;
  342. COne255th = 1 / $FF;
  343. function Fixed(S: Single): TFixed; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  344. function Fixed(I: Integer): TFixed; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  345. { Points }
  346. type
  347. {$IFNDEF FPC}
  348. {$IFNDEF BCB}
  349. PPoint = ^TPoint;
  350. TPoint = Windows.TPoint;
  351. {$ENDIF}
  352. {$ENDIF}
  353. PPointArray = ^TPointArray;
  354. TPointArray = array [0..0] of TPoint;
  355. PArrayOfPoint = ^TArrayOfPoint;
  356. TArrayOfPoint = array of TPoint;
  357. PArrayOfArrayOfPoint = ^TArrayOfArrayOfPoint;
  358. TArrayOfArrayOfPoint = array of TArrayOfPoint;
  359. PFloatPoint = ^TFloatPoint;
  360. TFloatPoint = record
  361. X, Y: TFloat;
  362. {$IFDEF SUPPORT_ENHANCED_RECORDS}
  363. public
  364. {$IFNDEF FPC}
  365. {$IFDEF COMPILERXE2_UP}
  366. constructor Create(P: TPointF); overload;
  367. {$ENDIF}
  368. constructor Create(P: TPoint); overload;
  369. constructor Create(X, Y: Integer); overload;
  370. constructor Create(X, Y: Single); overload;
  371. {$ENDIF}
  372. // operator overloads
  373. class operator Equal(const Lhs, Rhs: TFloatPoint): Boolean;
  374. class operator NotEqual(const Lhs, Rhs: TFloatPoint): Boolean;
  375. class operator Add(const Lhs, Rhs: TFloatPoint): TFloatPoint;
  376. class operator Subtract(const Lhs, Rhs: TFloatPoint): TFloatPoint;
  377. {$IFDEF COMPILERXE2_UP}
  378. class operator Explicit(A: TPointF): TFloatPoint;
  379. class operator Implicit(A: TPointF): TFloatPoint;
  380. {$ENDIF}
  381. class function Zero: TFloatPoint; inline; static;
  382. {$ENDIF}
  383. end;
  384. PFloatPointArray = ^TFloatPointArray;
  385. TFloatPointArray = array [0..0] of TFloatPoint;
  386. PArrayOfFloatPoint = ^TArrayOfFloatPoint;
  387. TArrayOfFloatPoint = array of TFloatPoint;
  388. PArrayOfArrayOfFloatPoint = ^TArrayOfArrayOfFloatPoint;
  389. TArrayOfArrayOfFloatPoint = array of TArrayOfFloatPoint;
  390. PFixedPoint = ^TFixedPoint;
  391. TFixedPoint = record
  392. X, Y: TFixed;
  393. {$IFDEF SUPPORT_ENHANCED_RECORDS}
  394. public
  395. {$IFNDEF FPC}
  396. {$IFDEF COMPILERXE2_UP}
  397. constructor Create(P: TPointF); overload;
  398. {$ENDIF}
  399. constructor Create(P: TFloatPoint); overload;
  400. constructor Create(X, Y: TFixed); overload;
  401. constructor Create(X, Y: Integer); overload;
  402. constructor Create(X, Y: TFloat); overload;
  403. {$ENDIF}
  404. // operator overloads
  405. class operator Equal(const Lhs, Rhs: TFixedPoint): Boolean;
  406. class operator NotEqual(const Lhs, Rhs: TFixedPoint): Boolean;
  407. class operator Add(const Lhs, Rhs: TFixedPoint): TFixedPoint;
  408. class operator Subtract(const Lhs, Rhs: TFixedPoint): TFixedPoint;
  409. class function Zero: TFixedPoint; inline; static;
  410. {$ENDIF}
  411. end;
  412. {$NODEFINE TFixedPoint}
  413. PFixedPointArray = ^TFixedPointArray;
  414. TFixedPointArray = array [0..0] of TFixedPoint;
  415. PArrayOfFixedPoint = ^TArrayOfFixedPoint;
  416. TArrayOfFixedPoint = array of TFixedPoint;
  417. PArrayOfArrayOfFixedPoint = ^TArrayOfArrayOfFixedPoint;
  418. TArrayOfArrayOfFixedPoint = array of TArrayOfFixedPoint;
  419. // construction and conversion of point types
  420. function Point(X, Y: Integer): TPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  421. function Point(const FP: TFloatPoint): TPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  422. function Point(const FXP: TFixedPoint): TPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  423. function FloatPoint(X, Y: Single): TFloatPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  424. function FloatPoint(const P: TPoint): TFloatPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  425. function FloatPoint(const FXP: TFixedPoint): TFloatPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  426. function FixedPoint(X, Y: Integer): TFixedPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  427. function FixedPoint(X, Y: Single): TFixedPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  428. function FixedPoint(const P: TPoint): TFixedPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  429. function FixedPoint(const FP: TFloatPoint): TFixedPoint; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  430. { Rectangles }
  431. type
  432. {$IFNDEF FPC}
  433. PRect = Windows.PRect;
  434. TRect = Windows.TRect;
  435. {$ENDIF}
  436. PFloatRect = ^TFloatRect;
  437. {$NODEFINE TFloatRect}
  438. {$IFDEF SupportsBoost}
  439. (*$HPPEMIT '#include <boost/strong_typedef.hpp>'*)
  440. {$ENDIF}
  441. (*$HPPEMIT 'namespace Gr32 {'*)
  442. {$IFDEF SupportsBoost}
  443. (*$HPPEMIT 'BOOST_STRONG_TYPEDEF(int, TFixed)'*)
  444. {$ELSE}
  445. (*$HPPEMIT 'typedef int TFixed;'*)
  446. {$ENDIF}
  447. (*$HPPEMIT 'struct TFixedPoint { float X, Y; }; typedef struct TFixedPoint TFixedPoint;'*)
  448. (*$HPPEMIT 'struct TFloatRect { float Left, Top, Right, Bottom; }; typedef struct TFloatRect TFloatRect;'*)
  449. (*$HPPEMIT 'struct TFixedRect { TFixed Left, Top, Right, Bottom; }; typedef struct TFixedRect TFixedRect;'*)
  450. (*$HPPEMIT '} // namespace Gr32 '*)
  451. TFloatRect = packed record
  452. case Integer of
  453. 0: (Left, Top, Right, Bottom: TFloat);
  454. 1: (TopLeft, BottomRight: TFloatPoint);
  455. end;
  456. {$NODEFINE PFixedRect}
  457. PFixedRect = ^TFixedRect;
  458. {$NODEFINE TFixedRect}
  459. TFixedRect = packed record
  460. case Integer of
  461. 0: (Left, Top, Right, Bottom: TFixed);
  462. 1: (TopLeft, BottomRight: TFixedPoint);
  463. end;
  464. TRectRounding = (rrClosest, rrOutside, rrInside);
  465. // Rectangle construction/conversion functions
  466. function MakeRect(const L, T, R, B: Integer): TRect; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  467. function MakeRect(const FR: TFloatRect; Rounding: TRectRounding = rrClosest): TRect; overload;
  468. function MakeRect(const FXR: TFixedRect; Rounding: TRectRounding = rrClosest): TRect; overload;
  469. function FixedRect(const L, T, R, B: TFixed): TFixedRect; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  470. function FixedRect(const TopLeft, BottomRight: TFixedPoint): TFixedRect; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  471. function FixedRect(const ARect: TRect): TFixedRect; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  472. function FixedRect(const FR: TFloatRect): TFixedRect; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  473. function FloatRect(const L, T, R, B: TFloat): TFloatRect; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  474. function FloatRect(const TopLeft, BottomRight: TFloatPoint): TFloatRect; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  475. function FloatRect(const ARect: TRect): TFloatRect; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  476. function FloatRect(const FXR: TFixedRect): TFloatRect; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  477. // Some basic operations over rectangles
  478. function IntersectRect(out Dst: TRect; const R1, R2: TRect): Boolean; overload;
  479. function IntersectRect(out Dst: TFloatRect; const FR1, FR2: TFloatRect): Boolean; overload;
  480. function UnionRect(out Rect: TRect; const R1, R2: TRect): Boolean; overload;
  481. function UnionRect(out Rect: TFloatRect; const R1, R2: TFloatRect): Boolean; overload;
  482. function EqualRect(const R1, R2: TRect): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  483. function EqualRect(const R1, R2: TFloatRect): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  484. procedure InflateRect(var R: TRect; Dx, Dy: Integer); overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  485. procedure InflateRect(var FR: TFloatRect; Dx, Dy: TFloat); overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  486. procedure OffsetRect(var R: TRect; Dx, Dy: Integer); overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  487. procedure OffsetRect(var FR: TFloatRect; Dx, Dy: TFloat); overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  488. function IsRectEmpty(const R: TRect): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  489. function IsRectEmpty(const FR: TFloatRect): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  490. function PtInRect(const R: TRect; const P: TPoint): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  491. function PtInRect(const R: TFloatRect; const P: TPoint): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  492. function PtInRect(const R: TRect; const P: TFloatPoint): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  493. function PtInRect(const R: TFloatRect; const P: TFloatPoint): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  494. function EqualRectSize(const R1, R2: TRect): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  495. function EqualRectSize(const R1, R2: TFloatRect): Boolean; overload; {$IFDEF USEINLINING} inline; {$ENDIF}
  496. type
  497. { TBitmap32 draw mode }
  498. TDrawMode = (dmOpaque, dmBlend, dmCustom, dmTransparent);
  499. TCombineMode = (cmBlend, cmMerge);
  500. TWrapMode = (wmClamp, wmRepeat, wmMirror);
  501. TWrapProc = function(Value, Max: Integer): Integer;
  502. TWrapProcEx = function(Value, Min, Max: Integer): Integer;
  503. {$IFDEF DEPRECATEDMODE}
  504. { Stretch filters }
  505. TStretchFilter = (sfNearest, sfDraft, sfLinear, sfCosine, sfSpline,
  506. sfLanczos, sfMitchell);
  507. {$ENDIF}
  508. type
  509. { TPlainInterfacedPersistent }
  510. { TPlainInterfacedPersistent provides simple interface support with
  511. optional reference-counting operation. }
  512. TPlainInterfacedPersistent = class(TPersistent, IInterface)
  513. private
  514. FRefCounted: Boolean;
  515. FRefCount: Integer;
  516. protected
  517. { IInterface }
  518. {$IFDEF FPC_HAS_CONSTREF}
  519. function QueryInterface(constref iid: TGuid; out obj): HResult; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
  520. function _AddRef: LongInt; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
  521. function _Release: LongInt; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
  522. {$ELSE}
  523. function QueryInterface(const iid: TGuid; out obj): HResult; stdcall;
  524. function _AddRef: LongInt; stdcall;
  525. function _Release: LongInt; stdcall;
  526. {$ENDIF}
  527. property RefCounted: Boolean read FRefCounted write FRefCounted;
  528. public
  529. procedure AfterConstruction; override;
  530. procedure BeforeDestruction; override;
  531. class function NewInstance: TObject; override;
  532. property RefCount: Integer read FRefCount;
  533. end;
  534. { TNotifiablePersistent }
  535. { TNotifiablePersistent provides a change notification mechanism }
  536. TNotifiablePersistent = class(TPlainInterfacedPersistent)
  537. private
  538. FUpdateCount: Integer;
  539. FOnChange: TNotifyEvent;
  540. protected
  541. property UpdateCount: Integer read FUpdateCount;
  542. public
  543. procedure BeforeDestruction; override;
  544. procedure Changed; virtual;
  545. procedure BeginUpdate; virtual;
  546. procedure EndUpdate; virtual;
  547. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  548. end;
  549. { TThreadPersistent }
  550. { TThreadPersistent is an ancestor for TBitmap32 object. In addition to
  551. TPersistent methods, it provides thread-safe locking and change notification }
  552. TThreadPersistent = class(TNotifiablePersistent)
  553. private
  554. FLockCount: Integer;
  555. protected
  556. {$IFDEF FPC}
  557. FLock: TCriticalSection;
  558. {$ELSE}
  559. FLock: TRTLCriticalSection;
  560. {$ENDIF}
  561. property LockCount: Integer read FLockCount;
  562. public
  563. constructor Create; virtual;
  564. destructor Destroy; override;
  565. procedure Lock;
  566. procedure Unlock;
  567. end;
  568. { TCustomMap }
  569. { An ancestor for bitmaps and similar 2D distributions wich have width and
  570. height properties }
  571. TCustomMap = class(TThreadPersistent)
  572. protected
  573. FHeight: Integer;
  574. FWidth: Integer;
  575. FOnResize: TNotifyEvent;
  576. procedure SetHeight(NewHeight: Integer); virtual;
  577. procedure SetWidth(NewWidth: Integer); virtual;
  578. procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); virtual;
  579. public
  580. constructor Create(Width, Height: Integer); reintroduce; overload;
  581. procedure Delete; virtual;
  582. function Empty: Boolean; virtual;
  583. procedure Resized; virtual;
  584. function SetSizeFrom(Source: TPersistent): Boolean;
  585. function SetSize(NewWidth, NewHeight: Integer): Boolean; virtual;
  586. property Height: Integer read FHeight write SetHeight;
  587. property Width: Integer read FWidth write SetWidth;
  588. property OnResize: TNotifyEvent read FOnResize write FOnResize;
  589. end;
  590. { TBitmap32 }
  591. { This is the core of Graphics32 unit. The TBitmap32 class is responsible
  592. for storage of a bitmap, as well as for drawing in it.
  593. The OnCombine event is fired only when DrawMode is set to dmCustom and two
  594. bitmaps are blended together. Unlike most normal events, it does not contain
  595. "Sender" parameter and is not called through some virtual method. This
  596. (a little bit non-standard) approach allows for faster operation. }
  597. const
  598. // common cases
  599. AREAINFO_RECT = $80000000;
  600. AREAINFO_LINE = $40000000; // 24 bits for line width in pixels...
  601. AREAINFO_ELLIPSE = $20000000;
  602. AREAINFO_ABSOLUTE = $10000000;
  603. AREAINFO_MASK = $FF000000;
  604. type
  605. TPixelCombineEvent = procedure(F: TColor32; var B: TColor32; M: Cardinal) of object;
  606. TAreaChangedEvent = procedure(Sender: TObject; const Area: TRect;
  607. const Info: Cardinal) of object;
  608. TCustomResampler = class;
  609. TCustomBackend = class;
  610. TCustomBackendClass = class of TCustomBackend;
  611. { TCustomBitmap32 }
  612. TCustomBitmap32 = class(TCustomMap)
  613. private
  614. FBackend: TCustomBackend;
  615. FBits: PColor32Array;
  616. FClipRect: TRect;
  617. FFixedClipRect: TFixedRect;
  618. F256ClipRect: TRect;
  619. FClipping: Boolean;
  620. FDrawMode: TDrawMode;
  621. FCombineMode: TCombineMode;
  622. FWrapMode: TWrapMode;
  623. FMasterAlpha: Cardinal;
  624. FOuterColor: TColor32;
  625. FPenColor: TColor32;
  626. FStippleCounter: Single;
  627. FStipplePattern: TArrayOfColor32;
  628. FStippleStep: Single;
  629. {$IFDEF DEPRECATEDMODE}
  630. FStretchFilter: TStretchFilter;
  631. {$ENDIF}
  632. FOnPixelCombine: TPixelCombineEvent;
  633. FOnAreaChanged: TAreaChangedEvent;
  634. FOldOnAreaChanged: TAreaChangedEvent;
  635. FMeasuringMode: Boolean;
  636. FResampler: TCustomResampler;
  637. procedure BackendChangedHandler(Sender: TObject); virtual;
  638. procedure BackendChangingHandler(Sender: TObject); virtual;
  639. {$IFDEF BITS_GETTER}
  640. function GetBits: PColor32Array; {$IFDEF USEINLINING} inline; {$ENDIF}
  641. {$ENDIF}
  642. function GetPixelPtr(X, Y: Integer): PColor32;
  643. function GetScanLine(Y: Integer): PColor32Array;
  644. procedure SetCombineMode(const Value: TCombineMode);
  645. procedure SetDrawMode(Value: TDrawMode);
  646. procedure SetWrapMode(Value: TWrapMode);
  647. procedure SetMasterAlpha(Value: Cardinal);
  648. {$IFDEF DEPRECATEDMODE}
  649. procedure SetStretchFilter(Value: TStretchFilter);
  650. {$ENDIF}
  651. procedure SetClipRect(const Value: TRect);
  652. procedure SetResampler(Resampler: TCustomResampler);
  653. function GetResamplerClassName: string;
  654. procedure SetResamplerClassName(const Value: string);
  655. function GetPenPos: TPoint;
  656. procedure SetPenPos(const Value: TPoint);
  657. function GetPenPosF: TFixedPoint;
  658. procedure SetPenPosF(const Value: TFixedPoint);
  659. public type
  660. TInfoHeaderVersion = (InfoHeaderVersion1, InfoHeaderVersion2, InfoHeaderVersion3, InfoHeaderVersion4, InfoHeaderVersion5);
  661. protected
  662. WrapProcHorz: TWrapProcEx;
  663. WrapProcVert: TWrapProcEx;
  664. BlendProc: Pointer;
  665. RasterX, RasterY: Integer;
  666. RasterXF, RasterYF: TFixed;
  667. procedure ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer); override;
  668. procedure CopyMapTo(Dst: TCustomBitmap32); virtual;
  669. procedure CopyPropertiesTo(Dst: TCustomBitmap32); virtual;
  670. procedure AssignTo(Dst: TPersistent); override;
  671. function LoadFromBMPStream(Stream: TStream; Size: Int64): boolean;
  672. function LoadFromDIBStream(Stream: TStream; Size: Int64): boolean;
  673. procedure SaveToDIBStream(Stream: TStream; SaveTopDown: Boolean = False); overload;
  674. procedure SaveToDIBStream(Stream: TStream; SaveTopDown: Boolean; InfoHeaderVersion: TInfoHeaderVersion); overload;
  675. function Equal(B: TCustomBitmap32): Boolean;
  676. procedure SET_T256(X, Y: Integer; C: TColor32);
  677. procedure SET_TS256(X, Y: Integer; C: TColor32);
  678. function GET_T256(X, Y: Integer): TColor32;
  679. function GET_TS256(X, Y: Integer): TColor32;
  680. procedure ReadData(Stream: TStream); virtual;
  681. procedure WriteData(Stream: TStream); virtual;
  682. procedure DefineProperties(Filer: TFiler); override;
  683. procedure InitializeBackend(Backend: TCustomBackendClass); virtual;
  684. procedure FinalizeBackend; virtual;
  685. procedure SetBackend(const Backend: TCustomBackend); virtual;
  686. {$IFDEF FPC_HAS_CONSTREF}
  687. function QueryInterface(constref iid: TGuid; out obj): HResult; {$IFDEF WINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
  688. {$ELSE}
  689. function QueryInterface(const iid: TGuid; out obj): HResult; stdcall;
  690. {$ENDIF}
  691. function GetPixel(X, Y: Integer): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  692. function GetPixelS(X, Y: Integer): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  693. function GetPixelW(X, Y: Integer): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  694. function GetPixelF(X, Y: Single): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  695. function GetPixelFS(X, Y: Single): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  696. function GetPixelFW(X, Y: Single): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  697. function GetPixelX(X, Y: TFixed): TColor32;
  698. function GetPixelXS(X, Y: TFixed): TColor32;
  699. function GetPixelXW(X, Y: TFixed): TColor32;
  700. function GetPixelFR(X, Y: Single): TColor32;
  701. function GetPixelXR(X, Y: TFixed): TColor32;
  702. function GetPixelB(X, Y: Integer): TColor32; {$IFDEF USEINLINING} inline; {$ENDIF}
  703. procedure SetPixel(X, Y: Integer; Value: TColor32); {$IFDEF USEINLINING} inline; {$ENDIF}
  704. procedure SetPixelS(X, Y: Integer; Value: TColor32);
  705. procedure SetPixelW(X, Y: Integer; Value: TColor32); {$IFDEF USEINLINING} inline; {$ENDIF}
  706. procedure SetPixelF(X, Y: Single; Value: TColor32); {$IFDEF USEINLINING} inline; {$ENDIF}
  707. procedure SetPixelFS(X, Y: Single; Value: TColor32);
  708. procedure SetPixelFW(X, Y: Single; Value: TColor32);
  709. procedure SetPixelX(X, Y: TFixed; Value: TColor32);
  710. procedure SetPixelXS(X, Y: TFixed; Value: TColor32);
  711. procedure SetPixelXW(X, Y: TFixed; Value: TColor32);
  712. public
  713. constructor Create(Backend: TCustomBackendClass); reintroduce; overload; virtual;
  714. constructor Create; reintroduce; overload; virtual;
  715. constructor Create(Width, Height: Integer); reintroduce; overload; virtual;
  716. destructor Destroy; override;
  717. class function GetPlatformBackendClass: TCustomBackendClass; virtual;
  718. procedure Assign(Source: TPersistent); override;
  719. function BoundsRect: TRect;
  720. function Empty: Boolean; override;
  721. procedure Clear; overload;
  722. procedure Clear(FillColor: TColor32); overload;
  723. procedure Delete; override;
  724. procedure BeginMeasuring(const Callback: TAreaChangedEvent);
  725. procedure EndMeasuring;
  726. function ReleaseBackend: TCustomBackend;
  727. procedure PropertyChanged; virtual;
  728. procedure Changed; overload; override;
  729. procedure Changed(const Area: TRect; const Info: Cardinal = AREAINFO_RECT); reintroduce; overload; virtual;
  730. procedure LoadFromStream(Stream: TStream); virtual;
  731. procedure SaveToStream(Stream: TStream; SaveTopDown: Boolean = False); overload; virtual;
  732. procedure SaveToStream(Stream: TStream; SaveTopDown: Boolean; InfoHeaderVersion: TInfoHeaderVersion); overload; virtual;
  733. procedure LoadFromFile(const FileName: string); virtual;
  734. procedure SaveToFile(const FileName: string; SaveTopDown: Boolean = False); overload; virtual;
  735. procedure SaveToFile(const FileName: string; SaveTopDown: Boolean; InfoHeaderVersion: TInfoHeaderVersion); overload; virtual;
  736. procedure LoadFromResourceID(Instance: THandle; ResID: Integer);
  737. procedure LoadFromResourceName(Instance: THandle; const ResName: string);
  738. procedure ResetAlpha; overload;
  739. procedure ResetAlpha(const AlphaValue: Byte); overload;
  740. procedure Draw(DstX, DstY: Integer; Src: TCustomBitmap32); overload;
  741. procedure Draw(DstX, DstY: Integer; const SrcRect: TRect; Src: TCustomBitmap32); overload;
  742. procedure Draw(const DstRect, SrcRect: TRect; Src: TCustomBitmap32); overload;
  743. procedure SetPixelT(X, Y: Integer; Value: TColor32); overload;
  744. procedure SetPixelT(var Ptr: PColor32; Value: TColor32); overload;
  745. procedure SetPixelTS(X, Y: Integer; Value: TColor32);
  746. procedure DrawTo(Dst: TCustomBitmap32); overload;
  747. procedure DrawTo(Dst: TCustomBitmap32; DstX, DstY: Integer); overload;
  748. procedure DrawTo(Dst: TCustomBitmap32; DstX, DstY: Integer; const SrcRect: TRect); overload;
  749. procedure DrawTo(Dst: TCustomBitmap32; const DstRect: TRect); overload;
  750. procedure DrawTo(Dst: TCustomBitmap32; const DstRect, SrcRect: TRect); overload;
  751. procedure SetStipple(NewStipple: TArrayOfColor32); overload;
  752. procedure SetStipple(NewStipple: array of TColor32); overload;
  753. procedure AdvanceStippleCounter(LengthPixels: Single);
  754. function GetStippleColor: TColor32;
  755. procedure HorzLine(X1, Y, X2: Integer; Value: TColor32);
  756. procedure HorzLineS(X1, Y, X2: Integer; Value: TColor32);
  757. procedure HorzLineT(X1, Y, X2: Integer; Value: TColor32);
  758. procedure HorzLineTS(X1, Y, X2: Integer; Value: TColor32);
  759. procedure HorzLineTSP(X1, Y, X2: Integer);
  760. procedure HorzLineX(X1, Y, X2: TFixed; Value: TColor32);
  761. procedure HorzLineXS(X1, Y, X2: TFixed; Value: TColor32);
  762. procedure VertLine(X, Y1, Y2: Integer; Value: TColor32);
  763. procedure VertLineS(X, Y1, Y2: Integer; Value: TColor32);
  764. procedure VertLineT(X, Y1, Y2: Integer; Value: TColor32);
  765. procedure VertLineTS(X, Y1, Y2: Integer; Value: TColor32);
  766. procedure VertLineTSP(X, Y1, Y2: Integer);
  767. procedure VertLineX(X, Y1, Y2: TFixed; Value: TColor32);
  768. procedure VertLineXS(X, Y1, Y2: TFixed; Value: TColor32);
  769. procedure Line(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean = False);
  770. procedure LineS(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean = False);
  771. procedure LineT(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean = False);
  772. procedure LineTS(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean = False);
  773. procedure LineA(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean = False);
  774. procedure LineAS(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean = False);
  775. procedure LineX(X1, Y1, X2, Y2: TFixed; Value: TColor32; L: Boolean = False); overload;
  776. procedure LineF(X1, Y1, X2, Y2: Single; Value: TColor32; L: Boolean = False); overload;
  777. procedure LineXS(X1, Y1, X2, Y2: TFixed; Value: TColor32; L: Boolean = False); overload;
  778. procedure LineFS(X1, Y1, X2, Y2: Single; Value: TColor32; L: Boolean = False); overload;
  779. procedure LineXP(X1, Y1, X2, Y2: TFixed; L: Boolean = False); overload;
  780. procedure LineFP(X1, Y1, X2, Y2: Single; L: Boolean = False); overload;
  781. procedure LineXSP(X1, Y1, X2, Y2: TFixed; L: Boolean = False); overload;
  782. procedure LineFSP(X1, Y1, X2, Y2: Single; L: Boolean = False); overload;
  783. property PenColor: TColor32 read FPenColor write FPenColor;
  784. procedure MoveTo(X, Y: Integer);
  785. procedure LineToS(X, Y: Integer);
  786. procedure LineToTS(X, Y: Integer);
  787. procedure LineToAS(X, Y: Integer);
  788. procedure MoveToX(X, Y: TFixed);
  789. procedure MoveToF(X, Y: Single);
  790. procedure LineToXS(X, Y: TFixed);
  791. procedure LineToFS(X, Y: Single);
  792. procedure LineToXSP(X, Y: TFixed);
  793. procedure LineToFSP(X, Y: Single);
  794. property PenPos: TPoint read GetPenPos write SetPenPos;
  795. property PenPosF: TFixedPoint read GetPenPosF write SetPenPosF;
  796. procedure FillRect(X1, Y1, X2, Y2: Integer; Value: TColor32);
  797. procedure FillRectS(X1, Y1, X2, Y2: Integer; Value: TColor32); overload;
  798. procedure FillRectT(X1, Y1, X2, Y2: Integer; Value: TColor32);
  799. procedure FillRectTS(X1, Y1, X2, Y2: Integer; Value: TColor32); overload;
  800. procedure FillRectS(const ARect: TRect; Value: TColor32); overload;
  801. procedure FillRectTS(const ARect: TRect; Value: TColor32); overload;
  802. procedure FrameRectS(X1, Y1, X2, Y2: Integer; Value: TColor32); overload;
  803. procedure FrameRectTS(X1, Y1, X2, Y2: Integer; Value: TColor32); overload;
  804. procedure FrameRectTSP(X1, Y1, X2, Y2: Integer);
  805. procedure FrameRectS(const ARect: TRect; Value: TColor32); overload;
  806. procedure FrameRectTS(const ARect: TRect; Value: TColor32); overload;
  807. procedure RaiseRectTS(X1, Y1, X2, Y2: Integer; Contrast: Integer); overload;
  808. procedure RaiseRectTS(const ARect: TRect; Contrast: Integer); overload;
  809. procedure Roll(Dx, Dy: Integer; FillBack: Boolean; FillColor: TColor32);
  810. procedure FlipHorz(Dst: TCustomBitmap32 = nil);
  811. procedure FlipVert(Dst: TCustomBitmap32 = nil);
  812. procedure Rotate90(Dst: TCustomBitmap32 = nil);
  813. procedure Rotate180(Dst: TCustomBitmap32 = nil);
  814. procedure Rotate270(Dst: TCustomBitmap32 = nil);
  815. procedure ResetClipRect;
  816. property Pixel[X, Y: Integer]: TColor32 read GetPixel write SetPixel; default;
  817. property PixelS[X, Y: Integer]: TColor32 read GetPixelS write SetPixelS;
  818. property PixelW[X, Y: Integer]: TColor32 read GetPixelW write SetPixelW;
  819. property PixelX[X, Y: TFixed]: TColor32 read GetPixelX write SetPixelX;
  820. property PixelXS[X, Y: TFixed]: TColor32 read GetPixelXS write SetPixelXS;
  821. property PixelXW[X, Y: TFixed]: TColor32 read GetPixelXW write SetPixelXW;
  822. property PixelF[X, Y: Single]: TColor32 read GetPixelF write SetPixelF;
  823. property PixelFS[X, Y: Single]: TColor32 read GetPixelFS write SetPixelFS;
  824. property PixelFW[X, Y: Single]: TColor32 read GetPixelFW write SetPixelFW;
  825. property PixelFR[X, Y: Single]: TColor32 read GetPixelFR;
  826. property PixelXR[X, Y: TFixed]: TColor32 read GetPixelXR;
  827. property Backend: TCustomBackend read FBackend write SetBackend;
  828. {$IFDEF BITS_GETTER}
  829. property Bits: PColor32Array read GetBits;
  830. {$ELSE}
  831. property Bits: PColor32Array read FBits;
  832. {$ENDIF}
  833. property ClipRect: TRect read FClipRect write SetClipRect;
  834. property Clipping: Boolean read FClipping;
  835. property PixelPtr[X, Y: Integer]: PColor32 read GetPixelPtr;
  836. property ScanLine[Y: Integer]: PColor32Array read GetScanLine;
  837. property StippleCounter: Single read FStippleCounter write FStippleCounter;
  838. property StippleStep: Single read FStippleStep write FStippleStep;
  839. property MeasuringMode: Boolean read FMeasuringMode;
  840. published
  841. property DrawMode: TDrawMode read FDrawMode write SetDrawMode default dmOpaque;
  842. property CombineMode: TCombineMode read FCombineMode write SetCombineMode default cmBlend;
  843. property WrapMode: TWrapMode read FWrapMode write SetWrapMode default wmClamp;
  844. property MasterAlpha: Cardinal read FMasterAlpha write SetMasterAlpha default $FF;
  845. property OuterColor: TColor32 read FOuterColor write FOuterColor default 0;
  846. {$IFDEF DEPRECATEDMODE}
  847. property StretchFilter: TStretchFilter read FStretchFilter write SetStretchFilter default sfNearest;
  848. {$ENDIF}
  849. property ResamplerClassName: string read GetResamplerClassName write SetResamplerClassName;
  850. property Resampler: TCustomResampler read FResampler write SetResampler;
  851. property OnChange;
  852. property OnPixelCombine: TPixelCombineEvent read FOnPixelCombine write FOnPixelCombine;
  853. property OnAreaChanged: TAreaChangedEvent read FOnAreaChanged write FOnAreaChanged;
  854. property OnResize;
  855. end;
  856. TCustomBitmap32Class = class of TCustomBitmap32;
  857. TBitmap32 = class(TCustomBitmap32)
  858. private
  859. FOnHandleChanged: TNotifyEvent;
  860. procedure BackendChangedHandler(Sender: TObject); override;
  861. procedure BackendChangingHandler(Sender: TObject); override;
  862. procedure FontChanged(Sender: TObject);
  863. procedure CanvasChanged(Sender: TObject);
  864. function GetCanvas: TCanvas; {$IFDEF USEINLINING} inline; {$ENDIF}
  865. function GetBitmapInfo: TBitmapInfo; {$IFDEF USEINLINING} inline; {$ENDIF}
  866. function GetHandle: HBITMAP; {$IFDEF USEINLINING} inline; {$ENDIF}
  867. function GetHDC: HDC; {$IFDEF USEINLINING} inline; {$ENDIF}
  868. function GetFont: TFont;
  869. procedure SetFont(Value: TFont);
  870. protected
  871. procedure FinalizeBackend; override;
  872. procedure SetBackend(const Backend: TCustomBackend); override;
  873. procedure HandleChanged; virtual;
  874. procedure CopyPropertiesTo(Dst: TCustomBitmap32); override;
  875. public
  876. class function GetPlatformBackendClass: TCustomBackendClass; override;
  877. {$IFDEF BCB}
  878. procedure Draw(const DstRect, SrcRect: TRect; hSrc: Cardinal); overload;
  879. {$ELSE}
  880. procedure Draw(const DstRect, SrcRect: TRect; hSrc: HDC); overload;
  881. {$ENDIF}
  882. {$IFDEF BCB}
  883. procedure DrawTo(hDst: Cardinal; DstX, DstY: Integer); overload;
  884. procedure DrawTo(hDst: Cardinal; const DstRect, SrcRect: TRect); overload;
  885. procedure TileTo(hDst: Cardinal; const DstRect, SrcRect: TRect; MaxTileSize: integer = 1024); overload;
  886. {$ELSE}
  887. procedure DrawTo(hDst: HDC; DstX: Integer = 0; DstY: Integer = 0); overload;
  888. procedure DrawTo(hDst: HDC; const DstRect, SrcRect: TRect); overload;
  889. procedure TileTo(hDst: HDC; const DstRect, SrcRect: TRect; MaxTileSize: integer = 1024); overload;
  890. {$ENDIF}
  891. {$IFDEF COMPILER2009_UP}
  892. procedure DrawTo(Dst: TControlCanvas; DstX: Integer = 0; DstY: Integer = 0); overload;
  893. procedure DrawTo(Dst: TControlCanvas; const DstRect, SrcRect: TRect); overload;
  894. procedure TileTo(Dst: TControlCanvas; const DstRect, SrcRect: TRect; MaxTileSize: integer = 1024); overload;
  895. {$ENDIF}
  896. procedure UpdateFont;
  897. procedure Textout(X, Y: Integer; const Text: string); overload;
  898. procedure Textout(X, Y: Integer; const ClipRect: TRect; const Text: string); overload;
  899. procedure Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string); overload;
  900. function TextExtent(const Text: string): TSize;
  901. function TextHeight(const Text: string): Integer;
  902. function TextWidth(const Text: string): Integer;
  903. procedure RenderText(X, Y: Integer; const Text: string; AALevel: Integer; Color: TColor32);
  904. property Canvas: TCanvas read GetCanvas;
  905. function CanvasAllocated: Boolean;
  906. procedure DeleteCanvas;
  907. property Font: TFont read GetFont write SetFont;
  908. property BitmapHandle: HBITMAP read GetHandle;
  909. property BitmapInfo: TBitmapInfo read GetBitmapInfo;
  910. property Handle: HDC read GetHDC;
  911. published
  912. property OnHandleChanged: TNotifyEvent read FOnHandleChanged write FOnHandleChanged;
  913. end;
  914. { TCustomBackend }
  915. { This class functions as backend for the TBitmap32 class.
  916. It manages and provides the backing buffer as well as OS or
  917. graphics subsystem specific features.}
  918. TCustomBackend = class(TThreadPersistent)
  919. protected
  920. FBits: PColor32Array;
  921. FOwner: TCustomBitmap32;
  922. FOnChanging: TNotifyEvent;
  923. procedure Changing; virtual;
  924. {$IFDEF BITS_GETTER}
  925. function GetBits: PColor32Array; virtual;
  926. {$ENDIF}
  927. procedure InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean); virtual;
  928. procedure FinalizeSurface; virtual;
  929. public
  930. constructor Create; overload; override;
  931. constructor Create(Owner: TCustomBitmap32); reintroduce; overload; virtual;
  932. destructor Destroy; override;
  933. procedure Assign(Source: TPersistent); override;
  934. procedure Clear; virtual;
  935. function Empty: Boolean; virtual;
  936. procedure ChangeSize(out Width, Height: Integer; NewWidth, NewHeight: Integer; ClearBuffer: Boolean = True); virtual;
  937. {$IFDEF BITS_GETTER}
  938. property Bits: PColor32Array read GetBits;
  939. {$ELSE}
  940. property Bits: PColor32Array read FBits;
  941. {$ENDIF}
  942. property OnChanging: TNotifyEvent read FOnChanging write FOnChanging;
  943. end;
  944. { TCustomSampler }
  945. TCustomSampler = class(TNotifiablePersistent)
  946. public
  947. function GetSampleInt(X, Y: Integer): TColor32; virtual;
  948. function GetSampleFixed(X, Y: TFixed): TColor32; virtual;
  949. function GetSampleFloat(X, Y: TFloat): TColor32; virtual;
  950. procedure PrepareSampling; virtual;
  951. procedure FinalizeSampling; virtual;
  952. function HasBounds: Boolean; virtual;
  953. function GetSampleBounds: TFloatRect; virtual;
  954. end;
  955. TPixelAccessMode = (pamUnsafe, pamSafe, pamWrap, pamTransparentEdge);
  956. { TCustomResampler }
  957. { Base class for TCustomBitmap32 specific resamplers. }
  958. TCustomResampler = class(TCustomSampler)
  959. private
  960. FBitmap: TCustomBitmap32;
  961. FClipRect: TRect;
  962. FPixelAccessMode: TPixelAccessMode;
  963. procedure SetPixelAccessMode(const Value: TPixelAccessMode);
  964. protected
  965. function GetWidth: TFloat; virtual;
  966. procedure Resample(
  967. Dst: TCustomBitmap32; DstRect: TRect; DstClip: TRect;
  968. Src: TCustomBitmap32; SrcRect: TRect;
  969. CombineOp: TDrawMode; CombineCallBack: TPixelCombineEvent); virtual; abstract;
  970. procedure AssignTo(Dst: TPersistent); override;
  971. property ClipRect: TRect read FClipRect;
  972. public
  973. constructor Create; overload; virtual;
  974. constructor Create(ABitmap: TCustomBitmap32); overload; virtual;
  975. procedure Changed; override;
  976. procedure PrepareSampling; override;
  977. function HasBounds: Boolean; override;
  978. function GetSampleBounds: TFloatRect; override;
  979. property Bitmap: TCustomBitmap32 read FBitmap write FBitmap;
  980. property Width: TFloat read GetWidth;
  981. published
  982. property PixelAccessMode: TPixelAccessMode read FPixelAccessMode write SetPixelAccessMode default pamSafe;
  983. end;
  984. TCustomResamplerClass = class of TCustomResampler;
  985. var
  986. StockBitmap: TBitmap;
  987. var
  988. // Default file format version of BMPs written by SaveToStream/SaveToFile.
  989. DefaultBitmapHeaderVersion: TCustomBitmap32.TInfoHeaderVersion = InfoHeaderVersion4;
  990. resourcestring
  991. RCStrUnmatchedReferenceCounting = 'Unmatched reference counting.';
  992. RCStrCannotSetSize = 'Can''t set size from ''%s''';
  993. RCStrInpropriateBackend = 'Inappropriate Backend';
  994. implementation
  995. uses
  996. Math,
  997. Clipbrd,
  998. GR32_Blend,
  999. GR32_LowLevel,
  1000. GR32_Math,
  1001. GR32_Resamplers,
  1002. GR32_Containers,
  1003. GR32_Gamma,
  1004. GR32_Clipboard,
  1005. GR32_Backends,
  1006. GR32_Backends_Generic,
  1007. {$IFDEF FPC}
  1008. {$IFDEF LCLWin32}
  1009. GR32_Backends_LCL_Win,
  1010. {$ENDIF}
  1011. {$IF defined(LCLGtk) or defined(LCLGtk2)}
  1012. GR32_Backends_LCL_Gtk,
  1013. {$IFEND}
  1014. {$IFDEF LCLCarbon}
  1015. GR32_Backends_LCL_Carbon,
  1016. {$ENDIF}
  1017. {$IFDEF LCLCustomDrawn}
  1018. GR32_Backends_LCL_CustomDrawn,
  1019. {$ENDIF}
  1020. {$ELSE}
  1021. GR32_Backends_VCL,
  1022. {$ENDIF}
  1023. GR32_VectorUtils;
  1024. type
  1025. { We can not use the Win32 defined records and constants here since we are cross-platform. }
  1026. // Bitmap file and info headers.
  1027. // See : https://docs.microsoft.com/en-us/windows/win32/gdi/bitmap-header-types
  1028. // BMP file header
  1029. TBitmapFileHeader = packed record
  1030. bfType: Word;
  1031. bfSize: DWORD;
  1032. bfReserved1: Word;
  1033. bfReserved2: Word;
  1034. bfOffBits: DWORD;
  1035. end;
  1036. // OS/2 v1 header: BITMAPCOREHEADER
  1037. // Note that this is the only header that isn't compatible with the V1 header
  1038. TBitmapCoreHeader = packed record
  1039. bcSize: DWORD;
  1040. bcWidth: Word;
  1041. bcHeight: Word;
  1042. bcPlanes: Word;
  1043. bcBitCount: Word;
  1044. end;
  1045. // V1 header: BITMAPINFOHEADER
  1046. // All versions after BITMAPINFOHEADER are supersets of BITMAPINFOHEADER.
  1047. // Each new version adds to the one before it.
  1048. TBitmapInfoHeader = packed record
  1049. biSize: DWORD;
  1050. biWidth: Longint;
  1051. biHeight: Longint;
  1052. biPlanes: Word;
  1053. biBitCount: Word;
  1054. biCompression: DWORD;
  1055. biSizeImage: DWORD;
  1056. biXPelsPerMeter: Longint;
  1057. biYPelsPerMeter: Longint;
  1058. biClrUsed: DWORD;
  1059. biClrImportant: DWORD;
  1060. end;
  1061. // OS/2 v2 header - all fields are optional: variable header size!
  1062. TBitmapCoreHeader2 = packed record
  1063. InfoHeader: TBitmapInfoHeader;
  1064. bc2ResUnit: WORD;
  1065. bc2Reserved: WORD;
  1066. bc2Orientation: WORD;
  1067. bc2Halftoning: WORD;
  1068. bc2HalftoneSize1: DWORD;
  1069. bc2HalftoneSize2: DWORD;
  1070. bc2ColorSpace: DWORD;
  1071. bc2AppData: DWORD;
  1072. end;
  1073. // Undocumented V2 header
  1074. TBitmapV2Header = packed record
  1075. InfoHeader: TBitmapInfoHeader;
  1076. bV2RedMask: DWORD;
  1077. bV2GreenMask: DWORD;
  1078. bV2BlueMask: DWORD;
  1079. end;
  1080. // Undocumented V3 header
  1081. TBitmapV3Header = packed record
  1082. InfoHeader: TBitmapInfoHeader;
  1083. bV3RedMask: DWORD;
  1084. bV3GreenMask: DWORD;
  1085. bV3BlueMask: DWORD;
  1086. bV3AlphaMask: DWORD;
  1087. end;
  1088. // V4 header: BITMAPV4HEADER
  1089. TBitmapV4Header = packed record
  1090. bV4Size: DWORD;
  1091. bV4Width: Longint;
  1092. bV4Height: Longint;
  1093. bV4Planes: Word;
  1094. bV4BitCount: Word;
  1095. bV4V4Compression: DWORD;
  1096. bV4SizeImage: DWORD;
  1097. bV4XPelsPerMeter: Longint;
  1098. bV4YPelsPerMeter: Longint;
  1099. bV4ClrUsed: DWORD;
  1100. bV4ClrImportant: DWORD;
  1101. bV4RedMask: DWORD;
  1102. bV4GreenMask: DWORD;
  1103. bV4BlueMask: DWORD;
  1104. bV4AlphaMask: DWORD;
  1105. bV4CSType: DWORD;
  1106. bV4Endpoints: array[0..35] of byte; // TCIEXYZTriple
  1107. bV4GammaRed: DWORD;
  1108. bV4GammaGreen: DWORD;
  1109. bV4GammaBlue: DWORD;
  1110. end;
  1111. // V5 header: BITMAPV5HEADER
  1112. TBitmapV5Header = packed record
  1113. bV5Size: DWORD;
  1114. bV5Width: Longint;
  1115. bV5Height: Longint;
  1116. bV5Planes: Word;
  1117. bV5BitCount: Word;
  1118. bV5Compression: DWORD;
  1119. bV5SizeImage: DWORD;
  1120. bV5XPelsPerMeter: Longint;
  1121. bV5YPelsPerMeter: Longint;
  1122. bV5ClrUsed: DWORD;
  1123. bV5ClrImportant: DWORD;
  1124. bV5RedMask: DWORD;
  1125. bV5GreenMask: DWORD;
  1126. bV5BlueMask: DWORD;
  1127. bV5AlphaMask: DWORD;
  1128. bV5CSType: DWORD;
  1129. bV5Endpoints: array[0..35] of byte; // TCIEXYZTriple
  1130. bV5GammaRed: DWORD;
  1131. bV5GammaGreen: DWORD;
  1132. bV5GammaBlue: DWORD;
  1133. bV5Intent: DWORD;
  1134. bV5ProfileData: DWORD;
  1135. bV5ProfileSize: DWORD;
  1136. bV5Reserved: DWORD;
  1137. end;
  1138. // Just a TBitmapInfoHeader with easy access to the color masks
  1139. TBitmapInfoEx = packed record
  1140. bmiHeader: TBitmapInfoHeader;
  1141. bmiColors: array[0..3] of DWORD;
  1142. end;
  1143. TBitmapHeader = packed record
  1144. FileHeader: TBitmapFileHeader;
  1145. case integer of
  1146. 0: (CoreHeader: TBitmapCoreHeader); // 12 - a.k.a. OS/2 v1
  1147. 1: (InfoHeader: TBitmapInfoHeader); // 40
  1148. 2: (V2Header: TBitmapV2Header); // 52
  1149. 3: (V3Header: TBitmapV3Header); // 56
  1150. 4: (V4Header: TBitmapV4Header); // 108
  1151. 5: (V5Header: TBitmapV5Header); // 124
  1152. 6: (CoreHeader2: TBitmapCoreHeader2); // 16..64
  1153. -1: (Header: TBitmapInfoEx);
  1154. end;
  1155. const
  1156. BI_RGB = 0;
  1157. BI_BITFIELDS = 3;
  1158. type
  1159. TGraphicAccess = class(TGraphic);
  1160. const
  1161. ZERO_RECT: TRect = (Left: 0; Top: 0; Right: 0; Bottom: 0);
  1162. { Color construction and conversion functions }
  1163. function Color32(WinColor: TColor): TColor32; overload;
  1164. begin
  1165. if (WinColor < 0) then
  1166. WinColor := GetSysColor(WinColor and $000000FF);
  1167. {$IF Defined(PUREPASCAL) or Defined(TARGET_X64)}
  1168. {$IFNDEF RGBA_FORMAT}
  1169. Result := $FF000000 or ((TColor32(WinColor) and $FF0000) shr 16) or (TColor32(WinColor) and $FF00) or ((TColor32(WinColor) and $FF) shl 16);
  1170. {$ELSE RGBA_FORMAT}
  1171. Result := $FF000000 or (TColor32(WinColor) and $FFFFFF);
  1172. {$ENDIF RGBA_FORMAT}
  1173. {$ELSE}
  1174. asm
  1175. MOV EAX,WinColor
  1176. BSWAP EAX
  1177. MOV AL,$FF
  1178. ROR EAX,8
  1179. MOV Result,EAX
  1180. end;
  1181. {$IFEND}
  1182. end;
  1183. function Color32(R, G, B: Byte; A: Byte = $FF): TColor32; overload;
  1184. {$IF Defined(PUREPASCAL) or Defined(TARGET_X64)}
  1185. begin
  1186. {$IFNDEF RGBA_FORMAT}
  1187. Result := (A shl 24) or (R shl 16) or (G shl 8) or B;
  1188. {$ELSE RGBA_FORMAT}
  1189. Result := (A shl 24) or (B shl 16) or (G shl 8) or R;
  1190. {$ENDIF RGBA_FORMAT}
  1191. {$ELSE}
  1192. asm
  1193. MOV AH, A
  1194. SHL EAX, 16
  1195. MOV AH, DL
  1196. MOV AL, CL
  1197. {$IFEND}
  1198. end;
  1199. function Color32(Index: Byte; var Palette: TPalette32): TColor32; overload;
  1200. begin
  1201. Result := Palette[Index];
  1202. end;
  1203. function Gray32(Intensity: Byte; Alpha: Byte = $FF): TColor32;
  1204. begin
  1205. Result := TColor32(Alpha) shl 24 + TColor32(Intensity) shl 16 +
  1206. TColor32(Intensity) shl 8 + TColor32(Intensity);
  1207. end;
  1208. function WinColor(Color32: TColor32): TColor;
  1209. {$IFDEF RGBA_FORMAT}
  1210. begin
  1211. Result := Color32 and $00FFFFFF;
  1212. end;
  1213. {$ELSE RGBA_FORMAT}
  1214. {$IF Defined(PUREPASCAL)}
  1215. begin
  1216. Result := ((Color32 and $00FF0000) shr 16) or
  1217. (Color32 and $0000FF00) or
  1218. ((Color32 and $000000FF) shl 16);
  1219. {$ELSE}
  1220. {$IFDEF FPC}assembler; nostackframe;{$ENDIF}
  1221. asm
  1222. {$IFDEF TARGET_x64}
  1223. MOV EAX, ECX
  1224. {$ENDIF}
  1225. // the alpha channel byte is set to zero!
  1226. ROL EAX, 8 // ABGR -> BGRA
  1227. XOR AL, AL // BGRA -> BGR0
  1228. BSWAP EAX // BGR0 -> 0RGB
  1229. {$IFEND}
  1230. end;
  1231. {$ENDIF RGBA_FORMAT}
  1232. function ArrayOfColor32(const Colors: array of TColor32): TArrayOfColor32;
  1233. var
  1234. L: Integer;
  1235. begin
  1236. // build a dynamic color array from specified colors
  1237. L := Length(Colors);
  1238. SetLength(Result, L);
  1239. MoveLongword(Colors[0], Result[0], L);
  1240. end;
  1241. procedure SwapRedBlueMem(var Color32: TColor32);
  1242. {$IF Defined(PUREPASCAL)}
  1243. var
  1244. Temp: Byte;
  1245. begin
  1246. Temp := TColor32Entry(Color32).R;
  1247. TColor32Entry(Color32).R := TColor32Entry(Color32).B;
  1248. TColor32Entry(Color32).B := Temp;
  1249. {$ELSE}
  1250. asm
  1251. MOV EDX, DWORD PTR [Color32]
  1252. ROL EDX, 8 // ARGB -> RGBA
  1253. BSWAP EDX // RGBA -> ABGR
  1254. MOV DWORD PTR [Color32], EDX
  1255. {$IFEND}
  1256. end;
  1257. function SwapRedBlue(Color32: TColor32): TColor32;
  1258. {$IF Defined(PUREPASCAL)}
  1259. begin
  1260. Result := Color32;
  1261. SwapRedBlueMem(Result);
  1262. {$ELSE}
  1263. asm
  1264. {$IFDEF TARGET_x64}
  1265. MOV EAX, ECX
  1266. {$ENDIF}
  1267. ROL EAX, 8 // ARGB -> RGBA
  1268. BSWAP EAX // RGBA -> ABGR
  1269. {$IFEND}
  1270. end;
  1271. function Color32ToBGRA(Color32: TColor32): DWORD;
  1272. begin
  1273. {$IFNDEF RGBA_FORMAT}
  1274. Result := Color32;
  1275. {$ELSE RGBA_FORMAT}
  1276. Result := SwapRedBlue(Color32);
  1277. {$ENDIF RGBA_FORMAT}
  1278. end;
  1279. procedure Color32ToBGRAMem(var Color32: TColor32);
  1280. begin
  1281. {$IFDEF RGBA_FORMAT}
  1282. SwapRedBlueMem(Color32);
  1283. {$ENDIF RGBA_FORMAT}
  1284. end;
  1285. function Color32ToRGBA(Color32: TColor32): DWORD;
  1286. begin
  1287. {$IFDEF RGBA_FORMAT}
  1288. Result := Color32;
  1289. {$ELSE RGBA_FORMAT}
  1290. Result := SwapRedBlue(Color32);
  1291. {$ENDIF RGBA_FORMAT}
  1292. end;
  1293. procedure Color32ToRGBAMem(var Color32: TColor32); {$IFDEF USEINLINING} inline; {$ENDIF}
  1294. begin
  1295. {$IFNDEF RGBA_FORMAT}
  1296. SwapRedBlueMem(Color32);
  1297. {$ENDIF RGBA_FORMAT}
  1298. end;
  1299. function BGRAToColor32(BGRA: DWORD): TColor32;
  1300. begin
  1301. {$IFNDEF RGBA_FORMAT}
  1302. Result := BGRA;
  1303. {$ELSE RGBA_FORMAT}
  1304. Result := SwapRedBlue(BGRA);
  1305. {$ENDIF RGBA_FORMAT}
  1306. end;
  1307. procedure BGRAToColor32Mem(var BGRA: DWORD); {$IFDEF USEINLINING} inline; {$ENDIF}
  1308. begin
  1309. {$IFDEF RGBA_FORMAT}
  1310. SwapRedBlueMem(TColor32(BGRA));
  1311. {$ENDIF RGBA_FORMAT}
  1312. end;
  1313. function RGBAToColor32(RGBA: DWORD): TColor32;
  1314. begin
  1315. {$IFDEF RGBA_FORMAT}
  1316. Result := RGBA;
  1317. {$ELSE RGBA_FORMAT}
  1318. Result := SwapRedBlue(RGBA);
  1319. {$ENDIF RGBA_FORMAT}
  1320. end;
  1321. procedure RGBAToColor32mem(var RGBA: DWORD); {$IFDEF USEINLINING} inline; {$ENDIF}
  1322. begin
  1323. {$IFNDEF RGBA_FORMAT}
  1324. SwapRedBlueMem(TColor32(RGBA));
  1325. {$ENDIF RGBA_FORMAT}
  1326. end;
  1327. procedure Color32ToRGB(Color32: TColor32; var R, G, B: Byte);
  1328. begin
  1329. {$IFNDEF RGBA_FORMAT}
  1330. R := (Color32 and $00FF0000) shr 16;
  1331. G := (Color32 and $0000FF00) shr 8;
  1332. B := (Color32 and $000000FF);
  1333. {$ELSE RGBA_FORMAT}
  1334. R := (Color32 and $000000FF);
  1335. G := (Color32 and $0000FF00) shr 8;
  1336. B := (Color32 and $00FF0000) shr 16;
  1337. {$ENDIF RGBA_FORMAT}
  1338. end;
  1339. procedure Color32ToRGBA(Color32: TColor32; var R, G, B, A: Byte);
  1340. begin
  1341. {$IFNDEF RGBA_FORMAT}
  1342. A := Color32 shr 24;
  1343. R := (Color32 and $00FF0000) shr 16;
  1344. G := (Color32 and $0000FF00) shr 8;
  1345. B := (Color32 and $000000FF);
  1346. {$ELSE RGBA_FORMAT}
  1347. A := Color32 shr 24;
  1348. R := (Color32 and $000000FF);
  1349. G := (Color32 and $0000FF00) shr 8;
  1350. B := (Color32 and $00FF0000) shr 16;
  1351. {$ENDIF RGBA_FORMAT}
  1352. end;
  1353. function Color32Components(R, G, B, A: Boolean): TColor32Components;
  1354. const
  1355. ccR : array[Boolean] of TColor32Components = ([], [ccRed]);
  1356. ccG : array[Boolean] of TColor32Components = ([], [ccGreen]);
  1357. ccB : array[Boolean] of TColor32Components = ([], [ccBlue]);
  1358. ccA : array[Boolean] of TColor32Components = ([], [ccAlpha]);
  1359. begin
  1360. Result := ccR[R] + ccG[G] + ccB[B] + ccA[A];
  1361. end;
  1362. function RedComponent(Color32: TColor32): Integer;
  1363. begin
  1364. {$IFNDEF RGBA_FORMAT}
  1365. Result := (Color32 and $00FF0000) shr 16;
  1366. {$ELSE RGBA_FORMAT}
  1367. Result := Color32 and $000000FF;
  1368. {$ENDIF RGBA_FORMAT}
  1369. end;
  1370. function GreenComponent(Color32: TColor32): Integer;
  1371. begin
  1372. Result := (Color32 and $0000FF00) shr 8;
  1373. end;
  1374. function BlueComponent(Color32: TColor32): Integer;
  1375. begin
  1376. {$IFNDEF RGBA_FORMAT}
  1377. Result := Color32 and $000000FF;
  1378. {$ELSE RGBA_FORMAT}
  1379. Result := (Color32 and $00FF0000) shr 16;
  1380. {$ENDIF RGBA_FORMAT}
  1381. end;
  1382. function AlphaComponent(Color32: TColor32): Integer;
  1383. begin
  1384. Result := Color32 shr 24;
  1385. end;
  1386. function Intensity(Color32: TColor32): Integer;
  1387. begin
  1388. // (R * 61 + G * 174 + B * 21) / 256
  1389. {$IFNDEF RGBA_FORMAT}
  1390. Result := (
  1391. (Color32 and $00FF0000) shr 16 * 61 +
  1392. (Color32 and $0000FF00) shr 8 * 174 +
  1393. (Color32 and $000000FF) * 21
  1394. ) shr 8;
  1395. {$ELSE RGBA_FORMAT}
  1396. Result := (
  1397. (Color32 and $000000FF) * 61 +
  1398. (Color32 and $0000FF00) shr 8 * 174 +
  1399. (Color32 and $00FF0000) shr 16 * 21
  1400. ) shr 8;
  1401. {$ENDIF RGBA_FORMAT}
  1402. end;
  1403. function InvertColor(Color32: TColor32): TColor32;
  1404. begin
  1405. TColor32Entry(Result).R := $FF - TColor32Entry(Color32).R;
  1406. TColor32Entry(Result).G := $FF - TColor32Entry(Color32).G;
  1407. TColor32Entry(Result).B := $FF - TColor32Entry(Color32).B;
  1408. TColor32Entry(Result).A := TColor32Entry(Color32).A;
  1409. end;
  1410. function SetAlpha(Color32: TColor32; NewAlpha: Integer): TColor32;
  1411. begin
  1412. if NewAlpha < 0 then
  1413. NewAlpha := 0
  1414. else
  1415. if NewAlpha > $FF then
  1416. NewAlpha := $FF;
  1417. Result := (Color32 and $00FFFFFF) or (TColor32(NewAlpha) shl 24);
  1418. end;
  1419. procedure ModifyAlpha(var Color32: TColor32; NewAlpha: Byte);
  1420. begin
  1421. TColor32Entry(Color32).A := NewAlpha;
  1422. end;
  1423. procedure ScaleAlpha(var Color32: TColor32; Scale: Single);
  1424. begin
  1425. TColor32Entry(Color32).A := Round(Scale * TColor32Entry(Color32).A);
  1426. end;
  1427. { Color space conversions }
  1428. function HSLtoRGB(H, S, L: Single; A: Integer): TColor32;
  1429. const
  1430. OneOverThree = 1 / 3;
  1431. var
  1432. M1, M2: Single;
  1433. function HueToColor(Hue: Single): Byte;
  1434. var
  1435. V: Double;
  1436. begin
  1437. Hue := Hue - Floor(Hue);
  1438. if 6 * Hue < 1 then
  1439. V := M1 + (M2 - M1) * Hue * 6
  1440. else
  1441. if 2 * Hue < 1 then
  1442. V := M2
  1443. else
  1444. if 3 * Hue < 2 then
  1445. V := M1 + (M2 - M1) * (2 * OneOverThree - Hue) * 6
  1446. else
  1447. V := M1;
  1448. Result := Round($FF * V);
  1449. end;
  1450. begin
  1451. if S = 0 then
  1452. begin
  1453. Result := Gray32(Round($FF * L), A);
  1454. Exit;
  1455. end;
  1456. if L <= 0.5 then
  1457. M2 := L * (1 + S)
  1458. else
  1459. M2 := L + S - L * S;
  1460. M1 := 2 * L - M2;
  1461. Result := Color32(
  1462. HueToColor(H + OneOverThree),
  1463. HueToColor(H),
  1464. HueToColor(H - OneOverThree),
  1465. A);
  1466. end;
  1467. procedure RGBtoHSL(RGB: TColor32; out H, S, L : Single);
  1468. const
  1469. // reciprocal mul. opt.
  1470. R6 = 1 / 6;
  1471. var
  1472. R, G, B, D, Cmax, Cmin: Single;
  1473. begin
  1474. R := RedComponent(RGB) * COne255th;
  1475. G := GreenComponent(RGB) * COne255th;
  1476. B := BlueComponent(RGB) * COne255th;
  1477. Cmax := Max(R, Max(G, B));
  1478. Cmin := Min(R, Min(G, B));
  1479. L := (Cmax + Cmin) * 0.5;
  1480. if Cmax = Cmin then
  1481. begin
  1482. H := 0;
  1483. S := 0
  1484. end else
  1485. begin
  1486. D := Cmax - Cmin;
  1487. if L < 0.5 then
  1488. S := D / (Cmax + Cmin)
  1489. else
  1490. S := D / (2 - Cmax - Cmin);
  1491. if R = Cmax then
  1492. H := (G - B) / D
  1493. else
  1494. if G = Cmax then
  1495. H := 2 + (B - R) / D
  1496. else
  1497. H := 4 + (R - G) / D;
  1498. H := H * R6;
  1499. if H < 0 then
  1500. H := H + 1
  1501. end;
  1502. end;
  1503. function HSLtoRGB(H, S, L, A: Integer): TColor32;
  1504. var
  1505. V, M, M1, M2, VSF: Integer;
  1506. begin
  1507. if L <= $7F then
  1508. V := L * (256 + S) shr 8
  1509. else
  1510. V := L + S - Integer(Div255(L * S));
  1511. if V <= 0 then
  1512. Result := Gray32(L, A)
  1513. else
  1514. begin
  1515. M := L * 2 - V;
  1516. H := H * 6;
  1517. VSF := (V - M) * (H and $FF) shr 8;
  1518. M1 := M + VSF;
  1519. M2 := V - VSF;
  1520. case H shr 8 of
  1521. 0: Result := Color32(V, M1, M, A);
  1522. 1: Result := Color32(M2, V, M, A);
  1523. 2: Result := Color32(M, V, M1, A);
  1524. 3: Result := Color32(M, M2, V, A);
  1525. 4: Result := Color32(M1, M, V, A);
  1526. 5: Result := Color32(V, M, M2, A);
  1527. else
  1528. Result := 0;
  1529. end;
  1530. end;
  1531. end;
  1532. procedure RGBtoHSL(RGB: TColor32; out H, S, L: Byte);
  1533. var
  1534. R, G, B, D, Cmax, Cmin, HL: Integer;
  1535. begin
  1536. R := RedComponent(RGB);
  1537. G := GreenComponent(RGB);
  1538. B := BlueComponent(RGB);
  1539. Cmax := Max(R, G, B);
  1540. Cmin := Min(R, G, B);
  1541. L := (Cmax + Cmin) shr 1;
  1542. if Cmax = Cmin then
  1543. begin
  1544. H := 0;
  1545. S := 0
  1546. end else
  1547. begin
  1548. D := (Cmax - Cmin) * $FF;
  1549. if L <= $7F then
  1550. S := D div (Cmax + Cmin)
  1551. else
  1552. S := D div ($FF * 2 - Cmax - Cmin);
  1553. D := D * 6;
  1554. if R = Cmax then
  1555. HL := (G - B) * $FF * $FF div D
  1556. else
  1557. if G = Cmax then
  1558. HL := $FF * 2 div 6 + (B - R) * $FF * $FF div D
  1559. else
  1560. HL := $FF * 4 div 6 + (R - G) * $FF * $FF div D;
  1561. if HL < 0 then
  1562. HL := HL + $FF * 2;
  1563. H := HL;
  1564. end;
  1565. end;
  1566. function HSVtoRGB(H, S, V: Single; A: Integer): TColor32;
  1567. var
  1568. Fraction: Single;
  1569. Sel, Q, P: Integer;
  1570. begin
  1571. V := 255 * V;
  1572. if S = 0 then
  1573. begin
  1574. Result := Gray32(Trunc(V), A);
  1575. Exit;
  1576. end;
  1577. H := (H - Floor(H)) * 6; // 0 <= H < 6
  1578. Fraction := H - Floor(H);
  1579. Sel := Trunc(H);
  1580. if (Sel mod 2) = 0 then
  1581. Fraction := 1 - Fraction;
  1582. P := Round(V * (1 - S));
  1583. Q := Round(V * (1 - S * Fraction));
  1584. case Sel of
  1585. 0: Result := Color32(Trunc(V), Q, P, A);
  1586. 1: Result := Color32(Q, Trunc(V), P, A);
  1587. 2: Result := Color32(P, Trunc(V), Q, A);
  1588. 3: Result := Color32(P, Q, Trunc(V), A);
  1589. 4: Result := Color32(Q, P, Trunc(V), A);
  1590. 5: Result := Color32(Trunc(V), P, Q, A);
  1591. else
  1592. Result := Gray32(0, A);
  1593. end;
  1594. end;
  1595. procedure RGBToHSV(Color: TColor32; out H, S, V: Single);
  1596. var
  1597. Delta, RGBMin, RGBMax: integer;
  1598. R, G, B: Integer;
  1599. const
  1600. COneSixth = 1 / 6;
  1601. begin
  1602. R := RedComponent(Color);
  1603. G := GreenComponent(Color);
  1604. B := BlueComponent(Color);
  1605. RGBMin := Min(R, Min(G, B));
  1606. RGBMax := Max(R, Max(G, B));
  1607. V := RGBMax * COne255th;
  1608. Delta := RGBMax - RGBMin;
  1609. if RGBMax = 0 then
  1610. S := 0
  1611. else
  1612. S := Delta / RGBMax;
  1613. if S = 0.0 then
  1614. H := 0
  1615. else
  1616. begin
  1617. if R = RGBMax then
  1618. H := COneSixth * (G - B) / Delta
  1619. else
  1620. if G = RGBMax then
  1621. H := COneSixth * (2 + (B - R) / Delta)
  1622. else
  1623. if B = RGBMax then
  1624. H := COneSixth * (4 + (R - G) / Delta);
  1625. if H < 0.0 then
  1626. H := H + 1;
  1627. end;
  1628. end;
  1629. { Palette conversion }
  1630. {$IFNDEF RGBA_FORMAT}
  1631. function WinPalette(const P: TPalette32): HPALETTE;
  1632. var
  1633. L: TMaxLogPalette;
  1634. L0: LOGPALETTE absolute L;
  1635. I: Cardinal;
  1636. Cl: TColor32;
  1637. begin
  1638. L.palVersion := $300;
  1639. L.palNumEntries := 256;
  1640. for I := 0 to $FF do
  1641. begin
  1642. Cl := P[I];
  1643. with L.palPalEntry[I] do
  1644. begin
  1645. peFlags := 0;
  1646. peRed := RedComponent(Cl);
  1647. peGreen := GreenComponent(Cl);
  1648. peBlue := BlueComponent(Cl);
  1649. end;
  1650. end;
  1651. Result := CreatePalette(l0);
  1652. end;
  1653. {$ENDIF RGBA_FORMAT}
  1654. { Fixed-point conversion routines }
  1655. function Fixed(S: Single): TFixed;
  1656. begin
  1657. Result := Round(S * FixedOne);
  1658. end;
  1659. function Fixed(I: Integer): TFixed;
  1660. begin
  1661. Result := I shl 16;
  1662. end;
  1663. { Points }
  1664. function Point(X, Y: Integer): TPoint;
  1665. begin
  1666. Result.X := X;
  1667. Result.Y := Y;
  1668. end;
  1669. function Point(const FP: TFloatPoint): TPoint;
  1670. begin
  1671. Result.X := Round(FP.X);
  1672. Result.Y := Round(FP.Y);
  1673. end;
  1674. function Point(const FXP: TFixedPoint): TPoint;
  1675. begin
  1676. Result.X := FixedRound(FXP.X);
  1677. Result.Y := FixedRound(FXP.Y);
  1678. end;
  1679. function FloatPoint(X, Y: Single): TFloatPoint;
  1680. begin
  1681. Result.X := X;
  1682. Result.Y := Y;
  1683. end;
  1684. function FloatPoint(const P: TPoint): TFloatPoint;
  1685. begin
  1686. Result.X := P.X;
  1687. Result.Y := P.Y;
  1688. end;
  1689. function FloatPoint(const FXP: TFixedPoint): TFloatPoint;
  1690. begin
  1691. with FXP do
  1692. begin
  1693. Result.X := X * FixedToFloat;
  1694. Result.Y := Y * FixedToFloat;
  1695. end;
  1696. end;
  1697. {$IFDEF SUPPORT_ENHANCED_RECORDS}
  1698. {$IFNDEF FPC}
  1699. constructor TFloatPoint.Create(P: TPoint);
  1700. begin
  1701. Self.X := P.X;
  1702. Self.Y := P.Y;
  1703. end;
  1704. {$IFDEF COMPILERXE2_UP}
  1705. constructor TFloatPoint.Create(P: TPointF);
  1706. begin
  1707. Self.X := P.X;
  1708. Self.Y := P.Y;
  1709. end;
  1710. {$ENDIF}
  1711. constructor TFloatPoint.Create(X, Y: Integer);
  1712. begin
  1713. Self.X := X;
  1714. Self.Y := Y;
  1715. end;
  1716. constructor TFloatPoint.Create(X, Y: TFloat);
  1717. begin
  1718. Self.X := X;
  1719. Self.Y := Y;
  1720. end;
  1721. {$ENDIF}
  1722. // operator overloads
  1723. class operator TFloatPoint.Equal(const Lhs, Rhs: TFloatPoint): Boolean;
  1724. begin
  1725. Result := (Lhs.X = Rhs.X) and (Lhs.Y = Rhs.Y);
  1726. end;
  1727. class operator TFloatPoint.NotEqual(const Lhs, Rhs: TFloatPoint): Boolean;
  1728. begin
  1729. Result := (Lhs.X <> Rhs.X) or (Lhs.Y <> Rhs.Y);
  1730. end;
  1731. class operator TFloatPoint.Add(const Lhs, Rhs: TFloatPoint): TFloatPoint;
  1732. begin
  1733. Result.X := Lhs.X + Rhs.X;
  1734. Result.Y := Lhs.Y + Rhs.Y;
  1735. end;
  1736. class operator TFloatPoint.Subtract(const Lhs, Rhs: TFloatPoint): TFloatPoint;
  1737. begin
  1738. Result.X := Lhs.X - Rhs.X;
  1739. Result.Y := Lhs.Y - Rhs.Y;
  1740. end;
  1741. {$IFDEF COMPILERXE2_UP}
  1742. class operator TFloatPoint.Explicit(A: TPointF): TFloatPoint;
  1743. begin
  1744. Result.X := A.X;
  1745. Result.Y := A.Y;
  1746. end;
  1747. class operator TFloatPoint.Implicit(A: TPointF): TFloatPoint;
  1748. begin
  1749. Result.X := A.X;
  1750. Result.Y := A.Y;
  1751. end;
  1752. {$ENDIF}
  1753. class function TFloatPoint.Zero: TFloatPoint;
  1754. begin
  1755. Result.X := 0;
  1756. Result.Y := 0;
  1757. end;
  1758. {$IFNDEF FPC}
  1759. {$IFDEF COMPILERXE2_UP}
  1760. constructor TFixedPoint.Create(P: TPointF);
  1761. begin
  1762. Self.X := Fixed(P.X);
  1763. Self.Y := Fixed(P.Y);
  1764. end;
  1765. {$ENDIF}
  1766. constructor TFixedPoint.Create(P: TFloatPoint);
  1767. begin
  1768. Self.X := Fixed(P.X);
  1769. Self.Y := Fixed(P.Y);
  1770. end;
  1771. constructor TFixedPoint.Create(X, Y: TFixed);
  1772. begin
  1773. Self.X := X;
  1774. Self.Y := Y;
  1775. end;
  1776. constructor TFixedPoint.Create(X, Y: Integer);
  1777. begin
  1778. Self.X := Fixed(X);
  1779. Self.Y := Fixed(Y);
  1780. end;
  1781. constructor TFixedPoint.Create(X, Y: TFloat);
  1782. begin
  1783. Self.X := Fixed(X);
  1784. Self.Y := Fixed(Y);
  1785. end;
  1786. {$ENDIF}
  1787. // operator overloads
  1788. class operator TFixedPoint.Equal(const Lhs, Rhs: TFixedPoint): Boolean;
  1789. begin
  1790. Result := (Lhs.X = Rhs.X) and (Lhs.Y = Rhs.Y);
  1791. end;
  1792. class operator TFixedPoint.NotEqual(const Lhs, Rhs: TFixedPoint): Boolean;
  1793. begin
  1794. Result := (Lhs.X <> Rhs.X) or (Lhs.Y <> Rhs.Y);
  1795. end;
  1796. class operator TFixedPoint.Add(const Lhs, Rhs: TFixedPoint): TFixedPoint;
  1797. begin
  1798. Result.X := Lhs.X + Rhs.X;
  1799. Result.Y := Lhs.Y + Rhs.Y;
  1800. end;
  1801. class operator TFixedPoint.Subtract(const Lhs, Rhs: TFixedPoint): TFixedPoint;
  1802. begin
  1803. Result.X := Lhs.X - Rhs.X;
  1804. Result.Y := Lhs.Y - Rhs.Y;
  1805. end;
  1806. class function TFixedPoint.Zero: TFixedPoint;
  1807. begin
  1808. Result.X := 0;
  1809. Result.Y := 0;
  1810. end;
  1811. {$ENDIF}
  1812. function FixedPoint(X, Y: Integer): TFixedPoint; overload;
  1813. begin
  1814. Result.X := X shl 16;
  1815. Result.Y := Y shl 16;
  1816. end;
  1817. function FixedPoint(X, Y: Single): TFixedPoint; overload;
  1818. begin
  1819. Result.X := Round(X * FixedOne);
  1820. Result.Y := Round(Y * FixedOne);
  1821. end;
  1822. function FixedPoint(const P: TPoint): TFixedPoint; overload;
  1823. begin
  1824. Result.X := P.X shl 16;
  1825. Result.Y := P.Y shl 16;
  1826. end;
  1827. function FixedPoint(const FP: TFloatPoint): TFixedPoint; overload;
  1828. begin
  1829. Result.X := Round(FP.X * FixedOne);
  1830. Result.Y := Round(FP.Y * FixedOne);
  1831. end;
  1832. { Rectangles }
  1833. function MakeRect(const L, T, R, B: Integer): TRect;
  1834. begin
  1835. with Result do
  1836. begin
  1837. Left := L;
  1838. Top := T;
  1839. Right := R;
  1840. Bottom := B;
  1841. end;
  1842. end;
  1843. function MakeRect(const FR: TFloatRect; Rounding: TRectRounding): TRect;
  1844. begin
  1845. with FR do
  1846. case Rounding of
  1847. rrClosest:
  1848. begin
  1849. Result.Left := Round(Left);
  1850. Result.Top := Round(Top);
  1851. Result.Right := Round(Right);
  1852. Result.Bottom := Round(Bottom);
  1853. end;
  1854. rrInside:
  1855. begin
  1856. Result.Left := Ceil(Left);
  1857. Result.Top := Ceil(Top);
  1858. Result.Right := Floor(Right);
  1859. Result.Bottom := Floor(Bottom);
  1860. if Result.Right < Result.Left then Result.Right := Result.Left;
  1861. if Result.Bottom < Result.Top then Result.Bottom := Result.Top;
  1862. end;
  1863. rrOutside:
  1864. begin
  1865. Result.Left := Floor(Left);
  1866. Result.Top := Floor(Top);
  1867. Result.Right := Ceil(Right);
  1868. Result.Bottom := Ceil(Bottom);
  1869. end;
  1870. end;
  1871. end;
  1872. function MakeRect(const FXR: TFixedRect; Rounding: TRectRounding): TRect;
  1873. begin
  1874. with FXR do
  1875. case Rounding of
  1876. rrClosest:
  1877. begin
  1878. Result.Left := FixedRound(Left);
  1879. Result.Top := FixedRound(Top);
  1880. Result.Right := FixedRound(Right);
  1881. Result.Bottom := FixedRound(Bottom);
  1882. end;
  1883. rrInside:
  1884. begin
  1885. Result.Left := FixedCeil(Left);
  1886. Result.Top := FixedCeil(Top);
  1887. Result.Right := FixedFloor(Right);
  1888. Result.Bottom := FixedFloor(Bottom);
  1889. if Result.Right < Result.Left then Result.Right := Result.Left;
  1890. if Result.Bottom < Result.Top then Result.Bottom := Result.Top;
  1891. end;
  1892. rrOutside:
  1893. begin
  1894. Result.Left := FixedFloor(Left);
  1895. Result.Top := FixedFloor(Top);
  1896. Result.Right := FixedCeil(Right);
  1897. Result.Bottom := FixedCeil(Bottom);
  1898. end;
  1899. end;
  1900. end;
  1901. function FixedRect(const L, T, R, B: TFixed): TFixedRect;
  1902. begin
  1903. with Result do
  1904. begin
  1905. Left := L;
  1906. Top := T;
  1907. Right := R;
  1908. Bottom := B;
  1909. end;
  1910. end;
  1911. function FixedRect(const TopLeft, BottomRight: TFixedPoint): TFixedRect;
  1912. begin
  1913. Result.TopLeft := TopLeft;
  1914. Result.BottomRight := BottomRight;
  1915. end;
  1916. function FixedRect(const ARect: TRect): TFixedRect;
  1917. begin
  1918. with Result do
  1919. begin
  1920. Left := ARect.Left shl 16;
  1921. Top := ARect.Top shl 16;
  1922. Right := ARect.Right shl 16;
  1923. Bottom := ARect.Bottom shl 16;
  1924. end;
  1925. end;
  1926. function FixedRect(const FR: TFloatRect): TFixedRect;
  1927. begin
  1928. with Result do
  1929. begin
  1930. Left := Round(FR.Left * 65536);
  1931. Top := Round(FR.Top * 65536);
  1932. Right := Round(FR.Right * 65536);
  1933. Bottom := Round(FR.Bottom * 65536);
  1934. end;
  1935. end;
  1936. function FloatRect(const L, T, R, B: TFloat): TFloatRect;
  1937. begin
  1938. with Result do
  1939. begin
  1940. Left := L;
  1941. Top := T;
  1942. Right := R;
  1943. Bottom := B;
  1944. end;
  1945. end;
  1946. function FloatRect(const TopLeft, BottomRight: TFloatPoint): TFloatRect;
  1947. begin
  1948. Result.TopLeft := TopLeft;
  1949. Result.BottomRight := BottomRight;
  1950. end;
  1951. function FloatRect(const ARect: TRect): TFloatRect;
  1952. begin
  1953. with Result do
  1954. begin
  1955. Left := ARect.Left;
  1956. Top := ARect.Top;
  1957. Right := ARect.Right;
  1958. Bottom := ARect.Bottom;
  1959. end;
  1960. end;
  1961. function FloatRect(const FXR: TFixedRect): TFloatRect;
  1962. begin
  1963. with Result do
  1964. begin
  1965. Left := FXR.Left * FixedToFloat;
  1966. Top := FXR.Top * FixedToFloat;
  1967. Right := FXR.Right * FixedToFloat;
  1968. Bottom := FXR.Bottom * FixedToFloat;
  1969. end;
  1970. end;
  1971. function IntersectRect(out Dst: TRect; const R1, R2: TRect): Boolean;
  1972. begin
  1973. if R1.Left >= R2.Left then Dst.Left := R1.Left else Dst.Left := R2.Left;
  1974. if R1.Right <= R2.Right then Dst.Right := R1.Right else Dst.Right := R2.Right;
  1975. if R1.Top >= R2.Top then Dst.Top := R1.Top else Dst.Top := R2.Top;
  1976. if R1.Bottom <= R2.Bottom then Dst.Bottom := R1.Bottom else Dst.Bottom := R2.Bottom;
  1977. Result := (Dst.Right >= Dst.Left) and (Dst.Bottom >= Dst.Top);
  1978. if not Result then Dst := ZERO_RECT;
  1979. end;
  1980. function IntersectRect(out Dst: TFloatRect; const FR1, FR2: TFloatRect): Boolean;
  1981. begin
  1982. Dst.Left := Math.Max(FR1.Left, FR2.Left);
  1983. Dst.Right := Math.Min(FR1.Right, FR2.Right);
  1984. Dst.Top := Math.Max(FR1.Top, FR2.Top);
  1985. Dst.Bottom := Math.Min(FR1.Bottom, FR2.Bottom);
  1986. Result := (Dst.Right >= Dst.Left) and (Dst.Bottom >= Dst.Top);
  1987. if not Result then FillLongword(Dst, 4, 0);
  1988. end;
  1989. function IsRectEmpty(const R: TRect): Boolean;
  1990. begin
  1991. Result := (R.Right <= R.Left) or (R.Bottom <= R.Top);
  1992. end;
  1993. function IsRectEmpty(const FR: TFloatRect): Boolean;
  1994. begin
  1995. Result := (FR.Right <= FR.Left) or (FR.Bottom <= FR.Top);
  1996. end;
  1997. function UnionRect(out Rect: TRect; const R1, R2: TRect): Boolean;
  1998. begin
  1999. Rect := R1;
  2000. if not IsRectEmpty(R2) then
  2001. begin
  2002. if R2.Left < R1.Left then Rect.Left := R2.Left;
  2003. if R2.Top < R1.Top then Rect.Top := R2.Top;
  2004. if R2.Right > R1.Right then Rect.Right := R2.Right;
  2005. if R2.Bottom > R1.Bottom then Rect.Bottom := R2.Bottom;
  2006. end;
  2007. Result := not IsRectEmpty(Rect);
  2008. if not Result then Rect := ZERO_RECT;
  2009. end;
  2010. function UnionRect(out Rect: TFloatRect; const R1, R2: TFloatRect): Boolean;
  2011. begin
  2012. Rect := R1;
  2013. if not IsRectEmpty(R2) then
  2014. begin
  2015. if R2.Left < R1.Left then Rect.Left := R2.Left;
  2016. if R2.Top < R1.Top then Rect.Top := R2.Top;
  2017. if R2.Right > R1.Right then Rect.Right := R2.Right;
  2018. if R2.Bottom > R1.Bottom then Rect.Bottom := R2.Bottom;
  2019. end;
  2020. Result := not IsRectEmpty(Rect);
  2021. if not Result then FillLongword(Rect, 4, 0);
  2022. end;
  2023. function EqualRect(const R1, R2: TRect): Boolean;
  2024. begin
  2025. Result := CompareMem(@R1, @R2, SizeOf(TRect));
  2026. end;
  2027. function EqualRect(const R1, R2: TFloatRect): Boolean;
  2028. begin
  2029. Result := CompareMem(@R1, @R2, SizeOf(TFloatRect));
  2030. end;
  2031. function EqualRectSize(const R1, R2: TRect): Boolean;
  2032. begin
  2033. Result := ((R1.Right - R1.Left) = (R2.Right - R2.Left)) and
  2034. ((R1.Bottom - R1.Top) = (R2.Bottom - R2.Top));
  2035. end;
  2036. function EqualRectSize(const R1, R2: TFloatRect): Boolean;
  2037. var
  2038. _R1: TFixedRect;
  2039. _R2: TFixedRect;
  2040. begin
  2041. _R1 := FixedRect(R1);
  2042. _R2 := FixedRect(R2);
  2043. Result := ((_R1.Right - _R1.Left) = (_R2.Right - _R2.Left)) and
  2044. ((_R1.Bottom - _R1.Top) = (_R2.Bottom - _R2.Top));
  2045. end;
  2046. procedure InflateRect(var R: TRect; Dx, Dy: Integer);
  2047. begin
  2048. Dec(R.Left, Dx); Dec(R.Top, Dy);
  2049. Inc(R.Right, Dx); Inc(R.Bottom, Dy);
  2050. end;
  2051. procedure InflateRect(var FR: TFloatRect; Dx, Dy: TFloat);
  2052. begin
  2053. with FR do
  2054. begin
  2055. Left := Left - Dx; Top := Top - Dy;
  2056. Right := Right + Dx; Bottom := Bottom + Dy;
  2057. end;
  2058. end;
  2059. procedure OffsetRect(var R: TRect; Dx, Dy: Integer);
  2060. begin
  2061. Inc(R.Left, Dx); Inc(R.Top, Dy);
  2062. Inc(R.Right, Dx); Inc(R.Bottom, Dy);
  2063. end;
  2064. procedure OffsetRect(var FR: TFloatRect; Dx, Dy: TFloat);
  2065. begin
  2066. with FR do
  2067. begin
  2068. Left := Left + Dx; Top := Top + Dy;
  2069. Right := Right + Dx; Bottom := Bottom + Dy;
  2070. end;
  2071. end;
  2072. function PtInRect(const R: TRect; const P: TPoint): Boolean;
  2073. begin
  2074. Result := (P.X >= R.Left) and (P.X < R.Right) and
  2075. (P.Y >= R.Top) and (P.Y < R.Bottom);
  2076. end;
  2077. function PtInRect(const R: TFloatRect; const P: TPoint): Boolean;
  2078. begin
  2079. Result := (P.X >= R.Left) and (P.X < R.Right) and
  2080. (P.Y >= R.Top) and (P.Y < R.Bottom);
  2081. end;
  2082. function PtInRect(const R: TRect; const P: TFloatPoint): Boolean;
  2083. begin
  2084. Result := (P.X >= R.Left) and (P.X < R.Right) and
  2085. (P.Y >= R.Top) and (P.Y < R.Bottom);
  2086. end;
  2087. function PtInRect(const R: TFloatRect; const P: TFloatPoint): Boolean;
  2088. begin
  2089. Result := (P.X >= R.Left) and (P.X < R.Right) and
  2090. (P.Y >= R.Top) and (P.Y < R.Bottom);
  2091. end;
  2092. { TSimpleInterfacedPersistent }
  2093. function TPlainInterfacedPersistent._AddRef: Integer;
  2094. begin
  2095. if FRefCounted then
  2096. Result := InterlockedIncrement(FRefCount)
  2097. else
  2098. Result := -1;
  2099. end;
  2100. function TPlainInterfacedPersistent._Release: Integer;
  2101. begin
  2102. if FRefCounted then
  2103. begin
  2104. Result := InterlockedDecrement(FRefCount);
  2105. if Result = 0 then
  2106. Destroy;
  2107. end
  2108. else
  2109. Result := -1;
  2110. end;
  2111. function TPlainInterfacedPersistent.QueryInterface(
  2112. {$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF}IID: TGUID; out Obj): HResult;
  2113. const
  2114. E_NOINTERFACE = HResult($80004002);
  2115. begin
  2116. if GetInterface(IID, Obj) then
  2117. Result := 0
  2118. else
  2119. Result := E_NOINTERFACE;
  2120. end;
  2121. procedure TPlainInterfacedPersistent.AfterConstruction;
  2122. begin
  2123. inherited;
  2124. // Release the constructor's implicit refcount
  2125. InterlockedDecrement(FRefCount);
  2126. end;
  2127. procedure TPlainInterfacedPersistent.BeforeDestruction;
  2128. begin
  2129. if RefCounted and (RefCount <> 0) then
  2130. raise Exception.Create(RCStrUnmatchedReferenceCounting);
  2131. inherited;
  2132. end;
  2133. class function TPlainInterfacedPersistent.NewInstance: TObject;
  2134. begin
  2135. Result := inherited NewInstance;
  2136. // Set an implicit refcount so that refcounting
  2137. // during construction won't destroy the object.
  2138. TPlainInterfacedPersistent(Result).FRefCount := 1;
  2139. end;
  2140. { TNotifiablePersistent }
  2141. procedure TNotifiablePersistent.Beforedestruction;
  2142. begin
  2143. inherited;
  2144. inc(FUpdateCount);
  2145. end;
  2146. procedure TNotifiablePersistent.BeginUpdate;
  2147. begin
  2148. Inc(FUpdateCount);
  2149. end;
  2150. procedure TNotifiablePersistent.Changed;
  2151. begin
  2152. if (FUpdateCount = 0) and Assigned(FOnChange) then FOnChange(Self);
  2153. end;
  2154. procedure TNotifiablePersistent.EndUpdate;
  2155. begin
  2156. Assert(FUpdateCount > 0, 'Unpaired TThreadPersistent.EndUpdate');
  2157. Dec(FUpdateCount);
  2158. end;
  2159. { TThreadPersistent }
  2160. constructor TThreadPersistent.Create;
  2161. begin
  2162. InitializeCriticalSection(FLock);
  2163. end;
  2164. destructor TThreadPersistent.Destroy;
  2165. begin
  2166. DeleteCriticalSection(FLock);
  2167. inherited;
  2168. end;
  2169. procedure TThreadPersistent.Lock;
  2170. begin
  2171. InterlockedIncrement(FLockCount);
  2172. EnterCriticalSection(FLock);
  2173. end;
  2174. procedure TThreadPersistent.Unlock;
  2175. begin
  2176. LeaveCriticalSection(FLock);
  2177. InterlockedDecrement(FLockCount);
  2178. end;
  2179. { TCustomMap }
  2180. constructor TCustomMap.Create(Width, Height: Integer);
  2181. begin
  2182. Create;
  2183. SetSize(Width, Height);
  2184. end;
  2185. procedure TCustomMap.ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer);
  2186. begin
  2187. Width := NewWidth;
  2188. Height := NewHeight;
  2189. end;
  2190. procedure TCustomMap.Delete;
  2191. begin
  2192. SetSize(0, 0);
  2193. end;
  2194. function TCustomMap.Empty: Boolean;
  2195. begin
  2196. Result := (Width = 0) or (Height = 0);
  2197. end;
  2198. procedure TCustomMap.Resized;
  2199. begin
  2200. if Assigned(FOnResize) then FOnResize(Self);
  2201. end;
  2202. procedure TCustomMap.SetHeight(NewHeight: Integer);
  2203. begin
  2204. SetSize(Width, NewHeight);
  2205. end;
  2206. function TCustomMap.SetSize(NewWidth, NewHeight: Integer): Boolean;
  2207. begin
  2208. if NewWidth < 0 then NewWidth := 0;
  2209. if NewHeight < 0 then NewHeight := 0;
  2210. Result := (NewWidth <> FWidth) or (NewHeight <> FHeight);
  2211. if Result then
  2212. begin
  2213. ChangeSize(FWidth, FHeight, NewWidth, NewHeight);
  2214. Changed;
  2215. Resized;
  2216. end;
  2217. end;
  2218. function TCustomMap.SetSizeFrom(Source: TPersistent): Boolean;
  2219. begin
  2220. if Source is TCustomMap then
  2221. Result := SetSize(TCustomMap(Source).Width, TCustomMap(Source).Height)
  2222. else if Source is TGraphic then
  2223. Result := SetSize(TGraphic(Source).Width, TGraphic(Source).Height)
  2224. else if Source is TControl then
  2225. Result := SetSize(TControl(Source).Width, TControl(Source).Height)
  2226. else if Source = nil then
  2227. Result := SetSize(0, 0)
  2228. else
  2229. raise Exception.CreateFmt(RCStrCannotSetSize, [Source.ClassName]);
  2230. end;
  2231. procedure TCustomMap.SetWidth(NewWidth: Integer);
  2232. begin
  2233. SetSize(NewWidth, Height);
  2234. end;
  2235. { TCustomBitmap32 }
  2236. constructor TCustomBitmap32.Create(Backend: TCustomBackendClass);
  2237. begin
  2238. inherited Create;
  2239. InitializeBackend(Backend);
  2240. FOuterColor := $00000000; // by default as full transparency black
  2241. FMasterAlpha := $FF;
  2242. FPenColor := clWhite32;
  2243. FStippleStep := 1;
  2244. FCombineMode := cmBlend;
  2245. BlendProc := @BLEND_MEM[FCombineMode]^;
  2246. WrapProcHorz := GetWrapProcEx(WrapMode);
  2247. WrapProcVert := GetWrapProcEx(WrapMode);
  2248. FResampler := TNearestResampler.Create(Self);
  2249. end;
  2250. constructor TCustomBitmap32.Create;
  2251. begin
  2252. Create(GetPlatformBackendClass);
  2253. end;
  2254. destructor TCustomBitmap32.Destroy;
  2255. begin
  2256. Lock;
  2257. try
  2258. SetSize(0, 0);
  2259. FResampler.Free;
  2260. FinalizeBackend;
  2261. finally
  2262. Unlock;
  2263. end;
  2264. inherited;
  2265. end;
  2266. procedure TCustomBitmap32.InitializeBackend(Backend: TCustomBackendClass);
  2267. begin
  2268. Backend.Create(Self);
  2269. end;
  2270. procedure TCustomBitmap32.FinalizeBackend;
  2271. begin
  2272. // Drop ownership of backend now:
  2273. // It's a zombie now.
  2274. FBackend.FOwner := nil;
  2275. FBackend.OnChange := nil;
  2276. FBackend.OnChanging := nil;
  2277. (*
  2278. Release our reference to the backend
  2279. Note: The backend won't necessarily be freed immediately.
  2280. This is required to circumvent a problem with the magic procedure cleanup
  2281. of interfaces that have ref-counting forcefully disabled:
  2282. Quality Central report #9157 and #9500:
  2283. http://qc.codegear.com/wc/qcmain.aspx?d=9157
  2284. http://qc.codegear.com/wc/qcmain.aspx?d=9500
  2285. if any backend interface is used within the same procedure in which
  2286. the owner bitmap is also freed, the magic procedure cleanup will
  2287. clear that particular interface long after the bitmap and its backend
  2288. are gone. This will result in all sorts of madness - mostly heap corruption
  2289. and AVs.
  2290. Here is an example:
  2291. procedure Test;
  2292. var
  2293. MyBitmap: TBitmap32;
  2294. begin
  2295. MyBitmap := TBitmap32.Create;
  2296. MyBitmap.SetSize(100, 100);
  2297. (MyBitmap.Backend as ICanvasSupport).Canvas;
  2298. MyBitmap.Free;
  2299. end; // _IntfClear will try to clear (MyBitmap.Backend as ICanvasSupport)
  2300. // which points to the interface at the previous location of MyBitmap.Backend in memory.
  2301. // MyBitmap.Backend is gone and the _Release call is invalid, so raise hell .
  2302. Here is an example for a correct workaround:
  2303. procedure Test;
  2304. var
  2305. MyBitmap: TBitmap32;
  2306. CanvasIntf: ICanvasSupport;
  2307. begin
  2308. MyBitmap := TBitmap32.Create;
  2309. MyBitmap.SetSize(100, 100);
  2310. CanvasIntf := MyBitmap.Backend as ICanvasSupport;
  2311. CanvasIntf.Canvas;
  2312. CanvasIntf := nil; // this will call _IntfClear and IInterface._Release
  2313. MyBitmap.Free;
  2314. end; // _IntfClear will try to clear CanvasIntf,
  2315. // it's nil, no _Release is called, everything is fine.
  2316. Since the above code is pretty fiddly, we introduce ref-counting for the
  2317. backend. That way the backend will be released once all references are dropped.
  2318. So, release our reference to the backend now:
  2319. *)
  2320. FBackend._Release;
  2321. FBackend := nil;
  2322. end;
  2323. procedure TCustomBitmap32.SetBackend(const Backend: TCustomBackend);
  2324. begin
  2325. if Assigned(Backend) and (Backend <> FBackend) then
  2326. begin
  2327. BeginUpdate;
  2328. Backend.FOwner := Self;
  2329. if Assigned(FBackend) then
  2330. begin
  2331. Backend.Assign(FBackend);
  2332. FinalizeBackend;
  2333. end;
  2334. FBackend := Backend;
  2335. FBackend.OnChange := BackendChangedHandler;
  2336. FBackend.OnChanging := BackendChangingHandler;
  2337. EndUpdate;
  2338. FBackend.Changed;
  2339. Changed;
  2340. end;
  2341. end;
  2342. function TCustomBitmap32.ReleaseBackend: TCustomBackend;
  2343. begin
  2344. FBackend._AddRef; // Increase ref-count for external use
  2345. Result := FBackend;
  2346. end;
  2347. function TCustomBitmap32.QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} IID: TGUID; out Obj): HResult;
  2348. begin
  2349. Result := FBackend.QueryInterface(IID, Obj);
  2350. if Result <> S_OK then
  2351. Result := inherited QueryInterface(IID, Obj);
  2352. end;
  2353. procedure TCustomBitmap32.ChangeSize(var Width, Height: Integer; NewWidth, NewHeight: Integer);
  2354. begin
  2355. FBackend.ChangeSize(Width, Height, NewWidth, NewHeight);
  2356. end;
  2357. procedure TCustomBitmap32.BackendChangingHandler(Sender: TObject);
  2358. begin
  2359. // descendants can override this method.
  2360. end;
  2361. procedure TCustomBitmap32.BackendChangedHandler(Sender: TObject);
  2362. begin
  2363. FBits := FBackend.Bits;
  2364. ResetClipRect;
  2365. end;
  2366. function TCustomBitmap32.Empty: Boolean;
  2367. begin
  2368. Result := FBackend.Empty or inherited Empty;
  2369. end;
  2370. procedure TCustomBitmap32.Clear;
  2371. begin
  2372. Clear(clBlack32);
  2373. end;
  2374. procedure TCustomBitmap32.Clear(FillColor: TColor32);
  2375. begin
  2376. if Empty then
  2377. Exit;
  2378. if not MeasuringMode then
  2379. begin
  2380. if Clipping then
  2381. FillRect(FClipRect.Left, FClipRect.Top, FClipRect.Right, FClipRect.Bottom, FillColor)
  2382. else
  2383. FillLongword(Bits[0], Width * Height, FillColor);
  2384. end;
  2385. Changed;
  2386. end;
  2387. procedure TCustomBitmap32.Delete;
  2388. begin
  2389. SetSize(0, 0);
  2390. end;
  2391. procedure TCustomBitmap32.AssignTo(Dst: TPersistent);
  2392. procedure AssignToBitmap(Bmp: TBitmap; SrcBitmap: TCustomBitmap32);
  2393. var
  2394. SavedBackend: TCustomBackend;
  2395. FontSupport: IFontSupport;
  2396. begin
  2397. RequireBackendSupport(SrcBitmap, [IDeviceContextSupport], romOr, False, SavedBackend);
  2398. try
  2399. {$IFDEF COMPILER2009_UP}
  2400. Bmp.SetSize(0, 0);
  2401. {$ELSE}
  2402. Bmp.Width := 0;
  2403. Bmp.Height := 0;
  2404. {$ENDIF}
  2405. Bmp.PixelFormat := pf32Bit;
  2406. {$IFDEF COMPILER2009_UP}
  2407. Bmp.SetSize(SrcBitmap.Width, SrcBitmap.Height);
  2408. {$ELSE}
  2409. Bmp.Width := SrcBitmap.Width;
  2410. Bmp.Height := SrcBitmap.Height;
  2411. {$ENDIF}
  2412. if Supports(SrcBitmap.Backend, IFontSupport, FontSupport) then // this is optional
  2413. begin
  2414. Bmp.Canvas.Font.Assign(FontSupport.Font);
  2415. FontSupport := nil;
  2416. end;
  2417. if SrcBitmap.Empty then Exit;
  2418. Bmp.Canvas.Lock;
  2419. try
  2420. (SrcBitmap.Backend as IDeviceContextSupport).DrawTo(Bmp.Canvas.Handle,
  2421. BoundsRect, BoundsRect)
  2422. finally
  2423. Bmp.Canvas.UnLock;
  2424. end;
  2425. finally
  2426. RestoreBackend(SrcBitmap, SavedBackend);
  2427. end;
  2428. end;
  2429. begin
  2430. if (Dst is TPicture) then
  2431. AssignToBitmap(TPicture(Dst).Bitmap, Self)
  2432. else
  2433. if (Dst is TBitmap) then
  2434. AssignToBitmap(TBitmap(Dst), Self)
  2435. else
  2436. if (Dst is TClipboard) then
  2437. begin
  2438. if (not CopyBitmap32ToClipboard(Self)) then
  2439. inherited;
  2440. end else
  2441. inherited;
  2442. end;
  2443. procedure TCustomBitmap32.Assign(Source: TPersistent);
  2444. procedure AssignFromGraphicPlain(TargetBitmap: TCustomBitmap32;
  2445. SrcGraphic: TGraphic; FillColor: TColor32; ResetAlphaAfterDrawing: Boolean);
  2446. var
  2447. SavedBackend: TCustomBackend;
  2448. Canvas: TCanvas;
  2449. DeviceContextSupport: IDeviceContextSupport;
  2450. CanvasSupport: ICanvasSupport;
  2451. InteroperabilitySupport: IInteroperabilitySupport;
  2452. begin
  2453. if not Assigned(SrcGraphic) then
  2454. Exit;
  2455. RequireBackendSupport(TargetBitmap, [IDeviceContextSupport, ICanvasSupport,
  2456. IInteroperabilitySupport], romOr, True, SavedBackend);
  2457. try
  2458. TargetBitmap.SetSize(SrcGraphic.Width, SrcGraphic.Height);
  2459. if TargetBitmap.Empty then Exit;
  2460. TargetBitmap.Clear(FillColor);
  2461. if Supports(TargetBitmap.Backend, IInteroperabilitySupport, InteroperabilitySupport) then
  2462. begin
  2463. InteroperabilitySupport.CopyFrom(SrcGraphic);
  2464. InteroperabilitySupport := nil;
  2465. end else
  2466. if Supports(TargetBitmap.Backend, ICanvasSupport, CanvasSupport) then
  2467. begin
  2468. TGraphicAccess(SrcGraphic).Draw(CanvasSupport.Canvas,
  2469. MakeRect(0, 0, TargetBitmap.Width, TargetBitmap.Height));
  2470. CanvasSupport := nil;
  2471. end else
  2472. if Supports(TargetBitmap.Backend, IDeviceContextSupport, DeviceContextSupport) then
  2473. begin
  2474. Canvas := TCanvas.Create;
  2475. try
  2476. Canvas.Lock;
  2477. try
  2478. Canvas.Handle := DeviceContextSupport.Handle;
  2479. TGraphicAccess(SrcGraphic).Draw(Canvas,
  2480. MakeRect(0, 0, TargetBitmap.Width, TargetBitmap.Height));
  2481. finally
  2482. Canvas.Unlock;
  2483. end;
  2484. finally
  2485. Canvas.Free;
  2486. end;
  2487. DeviceContextSupport := nil;
  2488. end else
  2489. raise Exception.Create(RCStrInpropriateBackend);
  2490. if ResetAlphaAfterDrawing then
  2491. ResetAlpha;
  2492. finally
  2493. RestoreBackend(TargetBitmap, SavedBackend);
  2494. end;
  2495. end;
  2496. procedure AssignFromGraphicMasked(TargetBitmap: TCustomBitmap32; SrcGraphic: TGraphic);
  2497. var
  2498. TempBitmap: TCustomBitmap32;
  2499. I: integer;
  2500. DstP, SrcP: PColor32;
  2501. DstColor: TColor32;
  2502. begin
  2503. AssignFromGraphicPlain(TargetBitmap, SrcGraphic, clWhite32, False); // mask on white
  2504. if TargetBitmap.Empty then
  2505. begin
  2506. TargetBitmap.Clear;
  2507. Exit;
  2508. end;
  2509. if TargetBitmap.Backend <> nil then
  2510. // Use the same backend type as the target. See Issue #127
  2511. TempBitmap := TCustomBitmap32.Create(TCustomBackendClass(TargetBitmap.Backend.ClassType))
  2512. else
  2513. TempBitmap := TCustomBitmap32.Create;
  2514. try
  2515. AssignFromGraphicPlain(TempBitmap, SrcGraphic, clRed32, False); // mask on red
  2516. DstP := @TargetBitmap.Bits[0];
  2517. SrcP := @TempBitmap.Bits[0];
  2518. for I := 0 to TargetBitmap.Width * TargetBitmap.Height - 1 do
  2519. begin
  2520. DstColor := DstP^ and $00FFFFFF;
  2521. // this checks for transparency by comparing the pixel-color of the
  2522. // temporary bitmap (red masked) with the pixel of our
  2523. // bitmap (white masked). if they match, make that pixel opaque
  2524. if DstColor = (SrcP^ and $00FFFFFF) then
  2525. DstP^ := DstColor or $FF000000
  2526. else
  2527. // if the colors do not match (that is the case if there is a
  2528. // match "is clRed32 = clWhite32 ?"), just make that pixel
  2529. // transparent:
  2530. DstP^ := DstColor;
  2531. Inc(SrcP); Inc(DstP);
  2532. end;
  2533. finally
  2534. TempBitmap.Free;
  2535. end;
  2536. end;
  2537. procedure AssignFromBitmap(TargetBitmap: TCustomBitmap32; SrcBmp: TBitmap);
  2538. var
  2539. TransparentColor: TColor32;
  2540. DstP: PColor32;
  2541. I: integer;
  2542. DstColor: TColor32;
  2543. FontSupport: IFontSupport;
  2544. begin
  2545. AssignFromGraphicPlain(TargetBitmap, SrcBmp, 0, SrcBmp.PixelFormat <> pf32bit);
  2546. if TargetBitmap.Empty then Exit;
  2547. if SrcBmp.Transparent then
  2548. begin
  2549. TransparentColor := Color32(SrcBmp.TransparentColor) and $00FFFFFF;
  2550. DstP := @TargetBitmap.Bits[0];
  2551. for I := 0 to TargetBitmap.Width * TargetBitmap.Height - 1 do
  2552. begin
  2553. DstColor := DstP^ and $00FFFFFF;
  2554. if DstColor = TransparentColor then
  2555. DstP^ := DstColor;
  2556. Inc(DstP);
  2557. end;
  2558. end;
  2559. if Supports(TargetBitmap.Backend, IFontSupport, FontSupport) then // this is optional
  2560. FontSupport.Font.Assign(SrcBmp.Canvas.Font);
  2561. end;
  2562. procedure AssignFromIcon(TargetBitmap: TCustomBitmap32; SrcIcon: TIcon);
  2563. var
  2564. I: Integer;
  2565. P: PColor32Entry;
  2566. ReassignFromMasked: Boolean;
  2567. begin
  2568. AssignFromGraphicPlain(TargetBitmap, SrcIcon, 0, False);
  2569. if TargetBitmap.Empty then Exit;
  2570. // Check if the icon was painted with a merged alpha channel.
  2571. // That happens transparently for new-style 32-bit icons.
  2572. // For all other bit depths GDI will reset our alpha channel to opaque.
  2573. ReassignFromMasked := True;
  2574. P := PColor32Entry(@TargetBitmap.Bits[0]);
  2575. for I := 0 to TargetBitmap.Height * TargetBitmap.Width - 1 do
  2576. begin
  2577. if P.A > 0 then
  2578. begin
  2579. ReassignFromMasked := False;
  2580. Break;
  2581. end;
  2582. Inc(P);
  2583. end;
  2584. // No alpha values found? Use masked approach...
  2585. if ReassignFromMasked then
  2586. AssignFromGraphicMasked(TargetBitmap, SrcIcon);
  2587. end;
  2588. procedure AssignFromGraphic(TargetBitmap: TCustomBitmap32; SrcGraphic: TGraphic);
  2589. begin
  2590. if SrcGraphic is TBitmap then
  2591. AssignFromBitmap(TargetBitmap, TBitmap(SrcGraphic))
  2592. else if SrcGraphic is TIcon then
  2593. AssignFromIcon(TargetBitmap, TIcon(SrcGraphic))
  2594. {$IFNDEF PLATFORM_INDEPENDENT}
  2595. else if SrcGraphic is TMetaFile then
  2596. AssignFromGraphicMasked(TargetBitmap, SrcGraphic)
  2597. {$IFDEF COMPILER2010_UP}
  2598. else if SrcGraphic is TWICImage then
  2599. AssignFromGraphicPlain(TargetBitmap, SrcGraphic, 0, False)
  2600. {$ENDIF}
  2601. {$ENDIF}
  2602. else
  2603. AssignFromGraphicPlain(TargetBitmap, SrcGraphic, clWhite32, True);
  2604. end;
  2605. var
  2606. Picture: TPicture;
  2607. begin
  2608. BeginUpdate;
  2609. try
  2610. if (not Assigned(Source)) then
  2611. SetSize(0, 0)
  2612. else
  2613. if (Source is TCustomBitmap32) then
  2614. begin
  2615. TCustomBitmap32(Source).CopyMapTo(Self);
  2616. TCustomBitmap32(Source).CopyPropertiesTo(Self);
  2617. end else
  2618. if (Source is TGraphic) then
  2619. AssignFromGraphic(Self, TGraphic(Source))
  2620. else
  2621. if (Source is TPicture) then
  2622. AssignFromGraphic(Self, TPicture(Source).Graphic)
  2623. else
  2624. if (Source is TClipboard) then
  2625. begin
  2626. if (not PasteBitmap32FromClipboard(Self)) then
  2627. begin
  2628. // Fallback to TPicture
  2629. Picture := TPicture.Create;
  2630. try
  2631. Picture.Assign(TClipboard(Source));
  2632. AssignFromGraphic(Self, Picture.Graphic);
  2633. finally
  2634. Picture.Free;
  2635. end;
  2636. end;
  2637. end else
  2638. inherited; // default handler
  2639. finally;
  2640. EndUpdate;
  2641. Changed;
  2642. end;
  2643. end;
  2644. procedure TCustomBitmap32.CopyMapTo(Dst: TCustomBitmap32);
  2645. begin
  2646. Dst.SetSize(Width, Height);
  2647. if not Empty then
  2648. MoveLongword(Bits[0], Dst.Bits[0], Width * Height);
  2649. end;
  2650. procedure TCustomBitmap32.CopyPropertiesTo(Dst: TCustomBitmap32);
  2651. begin
  2652. with Dst do
  2653. begin
  2654. DrawMode := Self.DrawMode;
  2655. CombineMode := Self.CombineMode;
  2656. WrapMode := Self.WrapMode;
  2657. MasterAlpha := Self.MasterAlpha;
  2658. OuterColor := Self.OuterColor;
  2659. {$IFDEF DEPRECATEDMODE}
  2660. StretchFilter := Self.StretchFilter;
  2661. {$ENDIF}
  2662. ResamplerClassName := Self.ResamplerClassName;
  2663. if Assigned(Resampler) and Assigned(Self.Resampler) then
  2664. Resampler.Assign(Self.Resampler);
  2665. end;
  2666. end;
  2667. constructor TCustomBitmap32.Create(Width, Height: Integer);
  2668. begin
  2669. Create;
  2670. SetSize(Width, Height);
  2671. end;
  2672. {$IFDEF BITS_GETTER}
  2673. function TCustomBitmap32.GetBits: PColor32Array;
  2674. begin
  2675. Result := FBackend.Bits;
  2676. end;
  2677. {$ENDIF}
  2678. procedure TCustomBitmap32.SetPenPos(const Value: TPoint);
  2679. begin
  2680. MoveTo(Value.X, Value.Y);
  2681. end;
  2682. procedure TCustomBitmap32.SetPenPosF(const Value: TFixedPoint);
  2683. begin
  2684. MoveTo(Value.X, Value.Y);
  2685. end;
  2686. procedure TCustomBitmap32.SetPixel(X, Y: Integer; Value: TColor32);
  2687. begin
  2688. Bits[X + Y * Width] := Value;
  2689. end;
  2690. procedure TCustomBitmap32.SetPixelS(X, Y: Integer; Value: TColor32);
  2691. begin
  2692. if {$IFDEF CHANGED_IN_PIXELS}not FMeasuringMode and{$ENDIF}
  2693. (X >= FClipRect.Left) and (X < FClipRect.Right) and
  2694. (Y >= FClipRect.Top) and (Y < FClipRect.Bottom) then
  2695. Bits[X + Y * Width] := Value;
  2696. {$IFDEF CHANGED_IN_PIXELS}
  2697. Changed(MakeRect(X, Y, X + 1, Y + 1));
  2698. {$ENDIF}
  2699. end;
  2700. function TCustomBitmap32.GetScanLine(Y: Integer): PColor32Array;
  2701. begin
  2702. Result := @Bits[Y * FWidth];
  2703. end;
  2704. function TCustomBitmap32.GetPenPos: TPoint;
  2705. begin
  2706. Result.X := RasterX;
  2707. Result.Y := RasterY;
  2708. end;
  2709. function TCustomBitmap32.GetPenPosF: TFixedPoint;
  2710. begin
  2711. Result.X := RasterXF;
  2712. Result.Y := RasterYF;
  2713. end;
  2714. function TCustomBitmap32.GetPixel(X, Y: Integer): TColor32;
  2715. begin
  2716. Result := Bits[X + Y * Width];
  2717. end;
  2718. function TCustomBitmap32.GetPixelS(X, Y: Integer): TColor32;
  2719. begin
  2720. if (X >= FClipRect.Left) and (X < FClipRect.Right) and
  2721. (Y >= FClipRect.Top) and (Y < FClipRect.Bottom) then
  2722. Result := Bits[X + Y * Width]
  2723. else
  2724. Result := OuterColor;
  2725. end;
  2726. function TCustomBitmap32.GetPixelPtr(X, Y: Integer): PColor32;
  2727. begin
  2728. Result := @Bits[X + Y * Width];
  2729. end;
  2730. procedure TCustomBitmap32.Draw(DstX, DstY: Integer; Src: TCustomBitmap32);
  2731. begin
  2732. if Assigned(Src) then Src.DrawTo(Self, DstX, DstY);
  2733. end;
  2734. procedure TCustomBitmap32.Draw(DstX, DstY: Integer; const SrcRect: TRect;
  2735. Src: TCustomBitmap32);
  2736. begin
  2737. if Assigned(Src) then Src.DrawTo(Self, DstX, DstY, SrcRect);
  2738. end;
  2739. procedure TCustomBitmap32.Draw(const DstRect, SrcRect: TRect; Src: TCustomBitmap32);
  2740. begin
  2741. if Assigned(Src) then Src.DrawTo(Self, DstRect, SrcRect);
  2742. end;
  2743. procedure TCustomBitmap32.DrawTo(Dst: TCustomBitmap32);
  2744. begin
  2745. BlockTransfer(Dst, 0, 0, Dst.ClipRect, Self, BoundsRect, DrawMode,
  2746. FOnPixelCombine);
  2747. end;
  2748. procedure TCustomBitmap32.DrawTo(Dst: TCustomBitmap32; DstX, DstY: Integer);
  2749. begin
  2750. BlockTransfer(Dst, DstX, DstY, Dst.ClipRect, Self, BoundsRect, DrawMode,
  2751. FOnPixelCombine);
  2752. end;
  2753. procedure TCustomBitmap32.DrawTo(Dst: TCustomBitmap32; DstX, DstY: Integer;
  2754. const SrcRect: TRect);
  2755. begin
  2756. BlockTransfer(Dst, DstX, DstY, Dst.ClipRect, Self, SrcRect,
  2757. DrawMode, FOnPixelCombine);
  2758. end;
  2759. procedure TCustomBitmap32.DrawTo(Dst: TCustomBitmap32; const DstRect: TRect);
  2760. begin
  2761. StretchTransfer(Dst, DstRect, Dst.ClipRect, Self, BoundsRect, Resampler,
  2762. DrawMode, FOnPixelCombine);
  2763. end;
  2764. procedure TCustomBitmap32.DrawTo(Dst: TCustomBitmap32; const DstRect,
  2765. SrcRect: TRect);
  2766. begin
  2767. StretchTransfer(Dst, DstRect, Dst.ClipRect, Self, SrcRect, Resampler,
  2768. DrawMode, FOnPixelCombine);
  2769. end;
  2770. procedure TCustomBitmap32.ResetAlpha;
  2771. begin
  2772. ResetAlpha($FF);
  2773. end;
  2774. procedure TCustomBitmap32.ResetAlpha(const AlphaValue: Byte);
  2775. var
  2776. I: Integer;
  2777. P: PByteArray;
  2778. begin
  2779. if not FMeasuringMode then
  2780. begin
  2781. {$IFDEF FPC}
  2782. P := Pointer(Bits);
  2783. for I := 0 to Width * Height - 1 do
  2784. begin
  2785. P^[3] := AlphaValue;
  2786. Inc(P, 4);
  2787. end
  2788. {$ELSE}
  2789. P := Pointer(Bits);
  2790. Inc(P, 3); //shift the pointer to 'alpha' component of the first pixel
  2791. I := Width * Height;
  2792. if I > 16 then
  2793. begin
  2794. I := I * 4 - 64;
  2795. Inc(P, I);
  2796. //16x enrolled loop
  2797. I := - I;
  2798. repeat
  2799. P^[I] := AlphaValue;
  2800. P^[I + 4] := AlphaValue;
  2801. P^[I + 8] := AlphaValue;
  2802. P^[I + 12] := AlphaValue;
  2803. P^[I + 16] := AlphaValue;
  2804. P^[I + 20] := AlphaValue;
  2805. P^[I + 24] := AlphaValue;
  2806. P^[I + 28] := AlphaValue;
  2807. P^[I + 32] := AlphaValue;
  2808. P^[I + 36] := AlphaValue;
  2809. P^[I + 40] := AlphaValue;
  2810. P^[I + 44] := AlphaValue;
  2811. P^[I + 48] := AlphaValue;
  2812. P^[I + 52] := AlphaValue;
  2813. P^[I + 56] := AlphaValue;
  2814. P^[I + 60] := AlphaValue;
  2815. Inc(I, 64)
  2816. until I > 0;
  2817. //eventually remaining bits
  2818. Dec(I, 64);
  2819. while I < 0 do
  2820. begin
  2821. P^[I + 64] := AlphaValue;
  2822. Inc(I, 4);
  2823. end;
  2824. end
  2825. else
  2826. begin
  2827. Dec(I);
  2828. I := I * 4;
  2829. while I >= 0 do
  2830. begin
  2831. P^[I] := AlphaValue;
  2832. Dec(I, 4);
  2833. end;
  2834. end;
  2835. {$ENDIF}
  2836. end;
  2837. Changed;
  2838. end;
  2839. function TCustomBitmap32.GetPixelB(X, Y: Integer): TColor32;
  2840. begin
  2841. // WARNING: this function should never be used on empty bitmaps !!!
  2842. if X < 0 then X := 0
  2843. else if X >= Width then X := Width - 1;
  2844. if Y < 0 then Y := 0
  2845. else if Y >= Height then Y := Height - 1;
  2846. Result := Bits[X + Y * Width];
  2847. end;
  2848. procedure TCustomBitmap32.SetPixelT(X, Y: Integer; Value: TColor32);
  2849. begin
  2850. TBlendMem(BlendProc)(Value, Bits[X + Y * Width]);
  2851. EMMS;
  2852. end;
  2853. procedure TCustomBitmap32.SetPixelT(var Ptr: PColor32; Value: TColor32);
  2854. begin
  2855. TBlendMem(BlendProc)(Value, Ptr^);
  2856. Inc(Ptr);
  2857. EMMS;
  2858. end;
  2859. procedure TCustomBitmap32.SetPixelTS(X, Y: Integer; Value: TColor32);
  2860. begin
  2861. if {$IFDEF CHANGED_IN_PIXELS}not FMeasuringMode and{$ENDIF}
  2862. (X >= FClipRect.Left) and (X < FClipRect.Right) and
  2863. (Y >= FClipRect.Top) and (Y < FClipRect.Bottom) then
  2864. begin
  2865. TBlendMem(BlendProc)(Value, Bits[X + Y * Width]);
  2866. EMMS;
  2867. end;
  2868. {$IFDEF CHANGED_IN_PIXELS}
  2869. Changed(MakeRect(X, Y, X + 1, Y + 1));
  2870. {$ENDIF}
  2871. end;
  2872. procedure TCustomBitmap32.SET_T256(X, Y: Integer; C: TColor32);
  2873. var
  2874. flrx, flry, celx, cely: Longword;
  2875. P: PColor32;
  2876. A: TColor32;
  2877. begin
  2878. { Warning: EMMS should be called after using this method }
  2879. flrx := X and $FF;
  2880. flry := Y and $FF;
  2881. {$IF Defined(PUREPASCAL) or Defined(TARGET_X64)}
  2882. X := X div 256;
  2883. Y := Y div 256;
  2884. {$ELSE}
  2885. asm
  2886. SAR X, 8
  2887. SAR Y, 8
  2888. end;
  2889. {$IFEND}
  2890. P := @Bits[X + Y * FWidth];
  2891. if FCombineMode = cmBlend then
  2892. begin
  2893. A := C shr 24; // opacity
  2894. celx := A * GAMMA_ENCODING_TABLE[flrx xor $FF];
  2895. cely := GAMMA_ENCODING_TABLE[flry xor $FF];
  2896. flrx := A * GAMMA_ENCODING_TABLE[flrx];
  2897. flry := GAMMA_ENCODING_TABLE[flry];
  2898. CombineMem(C, P^, celx * cely shr 16); Inc(P);
  2899. CombineMem(C, P^, flrx * cely shr 16); Inc(P, FWidth);
  2900. CombineMem(C, P^, flrx * flry shr 16); Dec(P);
  2901. CombineMem(C, P^, celx * flry shr 16);
  2902. end
  2903. else
  2904. begin
  2905. celx := GAMMA_ENCODING_TABLE[flrx xor $FF];
  2906. cely := GAMMA_ENCODING_TABLE[flry xor $FF];
  2907. flrx := GAMMA_ENCODING_TABLE[flrx];
  2908. flry := GAMMA_ENCODING_TABLE[flry];
  2909. CombineMem(MergeReg(C, P^), P^, celx * cely shr 8); Inc(P);
  2910. CombineMem(MergeReg(C, P^), P^, flrx * cely shr 8); Inc(P, FWidth);
  2911. CombineMem(MergeReg(C, P^), P^, flrx * flry shr 8); Dec(P);
  2912. CombineMem(MergeReg(C, P^), P^, celx * flry shr 8);
  2913. end;
  2914. end;
  2915. procedure TCustomBitmap32.SET_TS256(X, Y: Integer; C: TColor32);
  2916. var
  2917. flrx, flry, celx, cely: Longword;
  2918. P: PColor32;
  2919. A: TColor32;
  2920. begin
  2921. { Warning: EMMS should be called after using this method }
  2922. // we're checking against Left - 1 and Top - 1 due to antialiased values...
  2923. if (X < F256ClipRect.Left - 256) or (X >= F256ClipRect.Right) or
  2924. (Y < F256ClipRect.Top - 256) or (Y >= F256ClipRect.Bottom) then Exit;
  2925. flrx := X and $FF;
  2926. flry := Y and $FF;
  2927. {$IF Defined(PUREPASCAL) or Defined(TARGET_X64)}
  2928. X := X div 256;
  2929. Y := Y div 256;
  2930. {$ELSE}
  2931. asm
  2932. SAR X, 8
  2933. SAR Y, 8
  2934. end;
  2935. {$IFEND}
  2936. P := @Bits[X + Y * FWidth];
  2937. if FCombineMode = cmBlend then
  2938. begin
  2939. A := C shr 24; // opacity
  2940. celx := A * GAMMA_ENCODING_TABLE[flrx xor $FF];
  2941. cely := GAMMA_ENCODING_TABLE[flry xor $FF];
  2942. flrx := A * GAMMA_ENCODING_TABLE[flrx];
  2943. flry := GAMMA_ENCODING_TABLE[flry];
  2944. if (X >= FClipRect.Left) and (Y >= FClipRect.Top) and
  2945. (X < FClipRect.Right - 1) and (Y < FClipRect.Bottom - 1) then
  2946. begin
  2947. CombineMem(C, P^, celx * cely shr 16); Inc(P);
  2948. CombineMem(C, P^, flrx * cely shr 16); Inc(P, FWidth);
  2949. CombineMem(C, P^, flrx * flry shr 16); Dec(P);
  2950. CombineMem(C, P^, celx * flry shr 16);
  2951. end
  2952. else // "pixel" lies on the edge of the bitmap
  2953. with FClipRect do
  2954. begin
  2955. if (X >= Left) and (Y >= Top) then CombineMem(C, P^, celx * cely shr 16); Inc(P);
  2956. if (X < Right - 1) and (Y >= Top) then CombineMem(C, P^, flrx * cely shr 16); Inc(P, FWidth);
  2957. if (X < Right - 1) and (Y < Bottom - 1) then CombineMem(C, P^, flrx * flry shr 16); Dec(P);
  2958. if (X >= Left) and (Y < Bottom - 1) then CombineMem(C, P^, celx * flry shr 16);
  2959. end;
  2960. end
  2961. else
  2962. begin
  2963. celx := GAMMA_ENCODING_TABLE[flrx xor $FF];
  2964. cely := GAMMA_ENCODING_TABLE[flry xor $FF];
  2965. flrx := GAMMA_ENCODING_TABLE[flrx];
  2966. flry := GAMMA_ENCODING_TABLE[flry];
  2967. if (X >= FClipRect.Left) and (Y >= FClipRect.Top) and
  2968. (X < FClipRect.Right - 1) and (Y < FClipRect.Bottom - 1) then
  2969. begin
  2970. CombineMem(MergeReg(C, P^), P^, celx * cely shr 8); Inc(P);
  2971. CombineMem(MergeReg(C, P^), P^, flrx * cely shr 8); Inc(P, FWidth);
  2972. CombineMem(MergeReg(C, P^), P^, flrx * flry shr 8); Dec(P);
  2973. CombineMem(MergeReg(C, P^), P^, celx * flry shr 8);
  2974. end
  2975. else // "pixel" lies on the edge of the bitmap
  2976. with FClipRect do
  2977. begin
  2978. if (X >= Left) and (Y >= Top) then CombineMem(MergeReg(C, P^), P^, celx * cely shr 8); Inc(P);
  2979. if (X < Right - 1) and (Y >= Top) then CombineMem(MergeReg(C, P^), P^, flrx * cely shr 8); Inc(P, FWidth);
  2980. if (X < Right - 1) and (Y < Bottom - 1) then CombineMem(MergeReg(C, P^), P^, flrx * flry shr 8); Dec(P);
  2981. if (X >= Left) and (Y < Bottom - 1) then CombineMem(MergeReg(C, P^), P^, celx * flry shr 8);
  2982. end;
  2983. end;
  2984. end;
  2985. procedure TCustomBitmap32.SetPixelF(X, Y: Single; Value: TColor32);
  2986. begin
  2987. SET_T256(Round(X * 256), Round(Y * 256), Value);
  2988. {$IFNDEF OMIT_MMX}
  2989. EMMS;
  2990. {$ENDIF}
  2991. end;
  2992. procedure TCustomBitmap32.SetPixelX(X, Y: TFixed; Value: TColor32);
  2993. begin
  2994. X := (X + $7F) shr 8;
  2995. Y := (Y + $7F) shr 8;
  2996. SET_T256(X, Y, Value);
  2997. {$IFNDEF OMIT_MMX}
  2998. EMMS;
  2999. {$ENDIF}
  3000. end;
  3001. procedure TCustomBitmap32.SetPixelFS(X, Y: Single; Value: TColor32);
  3002. begin
  3003. {$IFDEF CHANGED_IN_PIXELS}
  3004. if not FMeasuringMode then
  3005. begin
  3006. {$ENDIF}
  3007. SET_TS256(Round(X * 256), Round(Y * 256), Value);
  3008. EMMS;
  3009. {$IFDEF CHANGED_IN_PIXELS}
  3010. end;
  3011. Changed(MakeRect(FloatRect(X, Y, X + 1, Y + 1)));
  3012. {$ENDIF}
  3013. end;
  3014. procedure TCustomBitmap32.SetPixelFW(X, Y: Single; Value: TColor32);
  3015. begin
  3016. {$IFDEF CHANGED_IN_PIXELS}
  3017. if not FMeasuringMode then
  3018. begin
  3019. {$ENDIF}
  3020. SetPixelXW(Round(X * FixedOne), Round(Y * FixedOne), Value);
  3021. EMMS;
  3022. {$IFDEF CHANGED_IN_PIXELS}
  3023. end;
  3024. Changed(MakeRect(FloatRect(X, Y, X + 1, Y + 1)));
  3025. {$ENDIF}
  3026. end;
  3027. procedure TCustomBitmap32.SetPixelXS(X, Y: TFixed; Value: TColor32);
  3028. begin
  3029. {$IFDEF CHANGED_IN_PIXELS}
  3030. if not FMeasuringMode then
  3031. begin
  3032. {$ENDIF}
  3033. {$IF Defined(PUREPASCAL) or Defined(TARGET_X64)}
  3034. X := (X + $7F) div 256;
  3035. Y := (Y + $7F) div 256;
  3036. {$ELSE}
  3037. asm
  3038. ADD X, $7F
  3039. ADD Y, $7F
  3040. SAR X, 8
  3041. SAR Y, 8
  3042. end;
  3043. {$IFEND}
  3044. SET_TS256(X, Y, Value);
  3045. EMMS;
  3046. {$IFDEF CHANGED_IN_PIXELS}
  3047. end;
  3048. Changed(MakeRect(X, Y, X + 1, Y + 1));
  3049. {$ENDIF}
  3050. end;
  3051. function TCustomBitmap32.GET_T256(X, Y: Integer): TColor32;
  3052. // When using this, remember that it interpolates towards next x and y!
  3053. var
  3054. Pos: Integer;
  3055. begin
  3056. Pos := (X shr 8) + (Y shr 8) * FWidth;
  3057. Result := Interpolator(GAMMA_ENCODING_TABLE[X and $FF xor $FF],
  3058. GAMMA_ENCODING_TABLE[Y and $FF xor $FF],
  3059. @Bits[Pos], @Bits[Pos + FWidth]);
  3060. end;
  3061. function TCustomBitmap32.GET_TS256(X, Y: Integer): TColor32;
  3062. var
  3063. Width256, Height256: Integer;
  3064. begin
  3065. if (X >= F256ClipRect.Left) and (Y >= F256ClipRect.Top) then
  3066. begin
  3067. Width256 := (FClipRect.Right - 1) shl 8;
  3068. Height256 := (FClipRect.Bottom - 1) shl 8;
  3069. if (X < Width256) and (Y < Height256) then
  3070. Result := GET_T256(X,Y)
  3071. else if (X = Width256) and (Y <= Height256) then
  3072. // We're exactly on the right border: no need to interpolate.
  3073. Result := Pixel[FClipRect.Right - 1, Y shr 8]
  3074. else if (X <= Width256) and (Y = Height256) then
  3075. // We're exactly on the bottom border: no need to interpolate.
  3076. Result := Pixel[X shr 8, FClipRect.Bottom - 1]
  3077. else
  3078. Result := FOuterColor;
  3079. end
  3080. else
  3081. Result := FOuterColor;
  3082. end;
  3083. function TCustomBitmap32.GetPixelF(X, Y: Single): TColor32;
  3084. begin
  3085. Result := GET_T256(Round(X * 256), Round(Y * 256));
  3086. {$IFNDEF OMIT_MMX}
  3087. EMMS;
  3088. {$ENDIF}
  3089. end;
  3090. function TCustomBitmap32.GetPixelFS(X, Y: Single): TColor32;
  3091. begin
  3092. Result := GET_TS256(Round(X * 256), Round(Y * 256));
  3093. {$IFNDEF OMIT_MMX}
  3094. EMMS;
  3095. {$ENDIF}
  3096. end;
  3097. function TCustomBitmap32.GetPixelFW(X, Y: Single): TColor32;
  3098. begin
  3099. Result := GetPixelXW(Round(X * FixedOne), Round(Y * FixedOne));
  3100. {$IFNDEF OMIT_MMX}
  3101. EMMS;
  3102. {$ENDIF}
  3103. end;
  3104. function TCustomBitmap32.GetPixelX(X, Y: TFixed): TColor32;
  3105. begin
  3106. X := (X + $7F) shr 8;
  3107. Y := (Y + $7F) shr 8;
  3108. Result := GET_T256(X, Y);
  3109. {$IFNDEF OMIT_MMX}
  3110. EMMS;
  3111. {$ENDIF}
  3112. end;
  3113. function TCustomBitmap32.GetPixelXS(X, Y: TFixed): TColor32;
  3114. {$IFDEF PUREPASCAL}
  3115. begin
  3116. X := (X + $7F) div 256;
  3117. Y := (Y + $7F) div 256;
  3118. Result := GET_TS256(X, Y);
  3119. EMMS;
  3120. {$ELSE}
  3121. {$IFDEF FPC}assembler;{$ENDIF}
  3122. asm
  3123. {$IFDEF TARGET_x64}
  3124. PUSH RBP
  3125. SUB RSP,$30
  3126. MOV RBP,RSP
  3127. {$ENDIF}
  3128. ADD X, $7F
  3129. ADD Y, $7F
  3130. SAR X, 8
  3131. SAR Y, 8
  3132. CALL TCustomBitmap32.GET_TS256
  3133. {$IFNDEF OMIT_MMX}
  3134. CMP MMX_ACTIVE.Integer, $00
  3135. JZ @Exit
  3136. DB $0F, $77 /// EMMS
  3137. @Exit:
  3138. {$ENDIF}
  3139. {$IFDEF TARGET_x64}
  3140. LEA RSP,[RBP+$30]
  3141. POP RBP
  3142. {$ENDIF}
  3143. {$ENDIF}
  3144. end;
  3145. function TCustomBitmap32.GetPixelFR(X, Y: Single): TColor32;
  3146. begin
  3147. Result := FResampler.GetSampleFloat(X, Y);
  3148. end;
  3149. function TCustomBitmap32.GetPixelXR(X, Y: TFixed): TColor32;
  3150. begin
  3151. Result := FResampler.GetSampleFixed(X, Y);
  3152. end;
  3153. function TCustomBitmap32.GetPixelW(X, Y: Integer): TColor32;
  3154. begin
  3155. with FClipRect do
  3156. Result := Bits[FWidth * WrapProcVert(Y, Top, Bottom - 1) + WrapProcHorz(X, Left, Right - 1)];
  3157. end;
  3158. procedure TCustomBitmap32.SetPixelW(X, Y: Integer; Value: TColor32);
  3159. begin
  3160. with FClipRect do
  3161. Bits[FWidth * WrapProcVert(Y, Top, Bottom - 1) + WrapProcHorz(X, Left, Right - 1)] := Value;
  3162. end;
  3163. function TCustomBitmap32.GetPixelXW(X, Y: TFixed): TColor32;
  3164. var
  3165. X1, X2, Y1, Y2 :Integer;
  3166. W: Integer;
  3167. begin
  3168. X2 := TFixedRec(X).Int;
  3169. Y2 := TFixedRec(Y).Int;
  3170. with FClipRect do
  3171. begin
  3172. W := Right - 1;
  3173. X1 := WrapProcHorz(X2, Left, W);
  3174. X2 := WrapProcHorz(X2 + 1, Left, W);
  3175. W := Bottom - 1;
  3176. Y1 := WrapProcVert(Y2, Top, W) * Width;
  3177. Y2 := WrapProcVert(Y2 + 1, Top, W) * Width;
  3178. end;
  3179. W := WordRec(TFixedRec(X).Frac).Hi;
  3180. Result := CombineReg(CombineReg(Bits[X2 + Y2], Bits[X1 + Y2], W),
  3181. CombineReg(Bits[X2 + Y1], Bits[X1 + Y1], W),
  3182. WordRec(TFixedRec(Y).Frac).Hi);
  3183. EMMS;
  3184. end;
  3185. class function TCustomBitmap32.GetPlatformBackendClass: TCustomBackendClass;
  3186. begin
  3187. Result := TMemoryBackend;
  3188. end;
  3189. procedure TCustomBitmap32.SetPixelXW(X, Y: TFixed; Value: TColor32);
  3190. begin
  3191. {$IF Defined(PUREPASCAL) or Defined(TARGET_X64)}
  3192. X := (X + $7F) div 256;
  3193. Y := (Y + $7F) div 256;
  3194. {$ELSE}
  3195. asm
  3196. ADD X, $7F
  3197. ADD Y, $7F
  3198. SAR X, 8
  3199. SAR Y, 8
  3200. end;
  3201. {$IFEND}
  3202. with F256ClipRect do
  3203. SET_T256(WrapProcHorz(X, Left, Right - 128), WrapProcVert(Y, Top, Bottom - 128), Value);
  3204. EMMS;
  3205. end;
  3206. procedure TCustomBitmap32.SetStipple(NewStipple: TArrayOfColor32);
  3207. begin
  3208. FStippleCounter := 0;
  3209. FStipplePattern := Copy(NewStipple, 0, Length(NewStipple));
  3210. end;
  3211. procedure TCustomBitmap32.SetStipple(NewStipple: array of TColor32);
  3212. var
  3213. L: Integer;
  3214. begin
  3215. FStippleCounter := 0;
  3216. L := High(NewStipple) + 1;
  3217. SetLength(FStipplePattern, L);
  3218. MoveLongword(NewStipple[0], FStipplePattern[0], L);
  3219. end;
  3220. procedure TCustomBitmap32.AdvanceStippleCounter(LengthPixels: Single);
  3221. var
  3222. L: Integer;
  3223. Delta: Single;
  3224. begin
  3225. L := Length(FStipplePattern);
  3226. Delta := LengthPixels * FStippleStep;
  3227. if (L = 0) or (Delta = 0) then
  3228. Exit;
  3229. FStippleCounter := FStippleCounter + Delta;
  3230. FStippleCounter := FStippleCounter - Floor(FStippleCounter / L) * L;
  3231. end;
  3232. function TCustomBitmap32.GetStippleColor: TColor32;
  3233. var
  3234. L: Integer;
  3235. NextIndex, PrevIndex: Integer;
  3236. PrevWeight: Integer;
  3237. begin
  3238. L := Length(FStipplePattern);
  3239. if L = 0 then
  3240. begin
  3241. // no pattern defined, just return something and exit
  3242. Result := clBlack32;
  3243. Exit;
  3244. end;
  3245. FStippleCounter := Wrap(FStippleCounter, L);
  3246. {$IFDEF FPC}
  3247. PrevIndex := Trunc(FStippleCounter);
  3248. {$ELSE}
  3249. PrevIndex := Round(FStippleCounter - 0.5);
  3250. {$ENDIF}
  3251. PrevWeight := $FF - Round($FF * (FStippleCounter - PrevIndex));
  3252. if PrevIndex < 0 then FStippleCounter := L - 1;
  3253. NextIndex := PrevIndex + 1;
  3254. if NextIndex >= L then NextIndex := 0;
  3255. if PrevWeight = $FF then Result := FStipplePattern[PrevIndex]
  3256. else
  3257. begin
  3258. Result := CombineReg(
  3259. FStipplePattern[PrevIndex],
  3260. FStipplePattern[NextIndex],
  3261. PrevWeight);
  3262. EMMS;
  3263. end;
  3264. FStippleCounter := FStippleCounter + FStippleStep;
  3265. end;
  3266. procedure TCustomBitmap32.HorzLine(X1, Y, X2: Integer; Value: TColor32);
  3267. begin
  3268. if not FMeasuringMode then
  3269. FillLongword(Bits[X1 + Y * Width], X2 - X1 + 1, Value);
  3270. Changed(MakeRect(X1, Y, X2+1, Y+1));
  3271. end;
  3272. procedure TCustomBitmap32.HorzLineS(X1, Y, X2: Integer; Value: TColor32);
  3273. begin
  3274. if (Y >= FClipRect.Top) and (Y < FClipRect.Bottom) and TestClip(X1, X2, FClipRect.Left, FClipRect.Right) then
  3275. HorzLine(X1, Y, X2, Value);
  3276. end;
  3277. procedure TCustomBitmap32.HorzLineT(X1, Y, X2: Integer; Value: TColor32);
  3278. var
  3279. i: Integer;
  3280. P: PColor32;
  3281. BlendMem: TBlendMem;
  3282. begin
  3283. if X2 < X1 then
  3284. Exit;
  3285. if not FMeasuringMode then
  3286. begin
  3287. P := PixelPtr[X1, Y];
  3288. BlendMem := TBlendMem(BlendProc);
  3289. for i := X1 to X2 do
  3290. begin
  3291. BlendMem(Value, P^);
  3292. Inc(P);
  3293. end;
  3294. EMMS;
  3295. end;
  3296. Changed(MakeRect(X1, Y, X2+1, Y+1));
  3297. end;
  3298. procedure TCustomBitmap32.HorzLineTS(X1, Y, X2: Integer; Value: TColor32);
  3299. begin
  3300. if (Y >= FClipRect.Top) and (Y < FClipRect.Bottom) and TestClip(X1, X2, FClipRect.Left, FClipRect.Right) then
  3301. HorzLineT(X1, Y, X2, Value);
  3302. end;
  3303. procedure TCustomBitmap32.HorzLineTSP(X1, Y, X2: Integer);
  3304. var
  3305. I, N: Integer;
  3306. begin
  3307. if Empty then
  3308. Exit;
  3309. if (Y >= FClipRect.Top) and (Y < FClipRect.Bottom) then
  3310. begin
  3311. if ((X1 < FClipRect.Left) and (X2 < FClipRect.Left)) or
  3312. ((X1 >= FClipRect.Right) and (X2 >= FClipRect.Right)) then
  3313. begin
  3314. if not FMeasuringMode then
  3315. AdvanceStippleCounter(Abs(X2 - X1) + 1);
  3316. Exit;
  3317. end;
  3318. if X1 < FClipRect.Left then
  3319. begin
  3320. if not FMeasuringMode then
  3321. AdvanceStippleCounter(FClipRect.Left - X1);
  3322. X1 := FClipRect.Left;
  3323. end else
  3324. if X1 >= FClipRect.Right then
  3325. begin
  3326. if not FMeasuringMode then
  3327. AdvanceStippleCounter(X1 - (FClipRect.Right - 1));
  3328. X1 := FClipRect.Right - 1;
  3329. end;
  3330. N := 0;
  3331. if X2 < FClipRect.Left then
  3332. begin
  3333. N := FClipRect.Left - X2;
  3334. X2 := FClipRect.Left;
  3335. end else
  3336. if X2 >= FClipRect.Right then
  3337. begin
  3338. N := X2 - (FClipRect.Right - 1);
  3339. X2 := FClipRect.Right - 1;
  3340. end;
  3341. if not FMeasuringMode then
  3342. begin
  3343. if X2 >= X1 then
  3344. begin
  3345. for I := X1 to X2 do
  3346. SetPixelT(I, Y, GetStippleColor);
  3347. end else
  3348. begin
  3349. for I := X1 downto X2 do
  3350. SetPixelT(I, Y, GetStippleColor);
  3351. end;
  3352. end;
  3353. Changed(MakeRect(X1, Y, X2+1, Y+1));
  3354. if (not FMeasuringMode) and (N > 0) then
  3355. AdvanceStippleCounter(N);
  3356. end else
  3357. if not FMeasuringMode then
  3358. AdvanceStippleCounter(Abs(X2 - X1) + 1);
  3359. end;
  3360. procedure TCustomBitmap32.HorzLineX(X1, Y, X2: TFixed; Value: TColor32);
  3361. //Author: Michael Hansen
  3362. var
  3363. I: Integer;
  3364. X1F, X2F, YF, Count: Integer;
  3365. Wx1, Wx2, Wy, Wt: TColor32;
  3366. PDst: PColor32;
  3367. begin
  3368. if X1 > X2 then
  3369. Swap(X1, X2);
  3370. if not FMeasuringMode then
  3371. begin
  3372. X1F := X1 shr 16;
  3373. X2F := X2 shr 16;
  3374. YF := Y shr 16;
  3375. PDst := PixelPtr[X1F, YF];
  3376. Wy := Y and $ffff xor $ffff;
  3377. Wx1 := X1 and $ffff xor $ffff;
  3378. Wx2 := X2 and $ffff;
  3379. Count := X2F - X1F - 1;
  3380. if Wy > 0 then
  3381. begin
  3382. CombineMem(Value, PDst^, GAMMA_ENCODING_TABLE[(Wy * Wx1) shr 24]);
  3383. Inc(PDst);
  3384. Wt := GAMMA_ENCODING_TABLE[Wy shr 8];
  3385. for I := 0 to Count - 1 do
  3386. begin
  3387. CombineMem(Value, PDst^, Wt);
  3388. Inc(PDst);
  3389. end;
  3390. CombineMem(Value, PDst^, GAMMA_ENCODING_TABLE[(Wy * Wx2) shr 24]);
  3391. end;
  3392. PDst := PixelPtr[X1F, YF + 1];
  3393. Wy := Wy xor $ffff;
  3394. if Wy > 0 then
  3395. begin
  3396. CombineMem(Value, PDst^, GAMMA_ENCODING_TABLE[(Wy * Wx1) shr 24]);
  3397. Inc(PDst);
  3398. Wt := GAMMA_ENCODING_TABLE[Wy shr 8];
  3399. for I := 0 to Count - 1 do
  3400. begin
  3401. CombineMem(Value, PDst^, Wt);
  3402. Inc(PDst);
  3403. end;
  3404. CombineMem(Value, PDst^, GAMMA_ENCODING_TABLE[(Wy * Wx2) shr 24]);
  3405. end;
  3406. EMMS;
  3407. end;
  3408. Changed(MakeRect(FixedRect(X1, Y, X2+1, Y+1), rrOutside), AREAINFO_LINE + 2);
  3409. end;
  3410. procedure TCustomBitmap32.HorzLineXS(X1, Y, X2: TFixed; Value: TColor32);
  3411. //author: Michael Hansen
  3412. begin
  3413. if X1 > X2 then
  3414. Swap(X1, X2);
  3415. X1 := Constrain(X1, FFixedClipRect.Left, FFixedClipRect.Right);
  3416. X2 := Constrain(X2, FFixedClipRect.Left, FFixedClipRect.Right);
  3417. if (Abs(X2 - X1) > FIXEDONE) and InRange(Y, FFixedClipRect.Top, FFixedClipRect.Bottom - FIXEDONE) then
  3418. HorzLineX(X1, Y, X2, Value)
  3419. else
  3420. LineXS(X1, Y, X2, Y, Value);
  3421. end;
  3422. procedure TCustomBitmap32.VertLine(X, Y1, Y2: Integer; Value: TColor32);
  3423. var
  3424. I, NH, NL: Integer;
  3425. P: PColor32;
  3426. begin
  3427. if Y2 < Y1 then
  3428. Exit;
  3429. if not FMeasuringMode then
  3430. begin
  3431. P := PixelPtr[X, Y1];
  3432. I := Y2 - Y1 + 1;
  3433. NH := I shr 2;
  3434. NL := I and $03;
  3435. for I := 0 to NH - 1 do
  3436. begin
  3437. P^ := Value; Inc(P, Width);
  3438. P^ := Value; Inc(P, Width);
  3439. P^ := Value; Inc(P, Width);
  3440. P^ := Value; Inc(P, Width);
  3441. end;
  3442. for I := 0 to NL - 1 do
  3443. begin
  3444. P^ := Value; Inc(P, Width);
  3445. end;
  3446. end;
  3447. Changed(MakeRect(X, Y1, X+1, Y2+1));
  3448. end;
  3449. procedure TCustomBitmap32.VertLineS(X, Y1, Y2: Integer; Value: TColor32);
  3450. begin
  3451. if (X >= FClipRect.Left) and (X < FClipRect.Right) and TestClip(Y1, Y2, FClipRect.Top, FClipRect.Bottom) then
  3452. VertLine(X, Y1, Y2, Value);
  3453. end;
  3454. procedure TCustomBitmap32.VertLineT(X, Y1, Y2: Integer; Value: TColor32);
  3455. var
  3456. i: Integer;
  3457. P: PColor32;
  3458. BlendMem: TBlendMem;
  3459. begin
  3460. if not FMeasuringMode then
  3461. begin
  3462. P := PixelPtr[X, Y1];
  3463. BlendMem := TBlendMem(BlendProc);
  3464. for i := Y1 to Y2 do
  3465. begin
  3466. BlendMem(Value, P^);
  3467. Inc(P, Width);
  3468. end;
  3469. EMMS;
  3470. end;
  3471. Changed(MakeRect(X, Y1, X+1, Y2+1));
  3472. end;
  3473. procedure TCustomBitmap32.VertLineTS(X, Y1, Y2: Integer; Value: TColor32);
  3474. begin
  3475. if (X >= FClipRect.Left) and (X < FClipRect.Right) and TestClip(Y1, Y2, FClipRect.Top, FClipRect.Bottom) then
  3476. VertLineT(X, Y1, Y2, Value);
  3477. end;
  3478. procedure TCustomBitmap32.VertLineTSP(X, Y1, Y2: Integer);
  3479. var
  3480. I, N: Integer;
  3481. begin
  3482. if Empty then
  3483. Exit;
  3484. if (X >= FClipRect.Left) and (X < FClipRect.Right) then
  3485. begin
  3486. if ((Y1 < FClipRect.Top) and (Y2 < FClipRect.Top)) or
  3487. ((Y1 >= FClipRect.Bottom) and (Y2 >= FClipRect.Bottom)) then
  3488. begin
  3489. if not FMeasuringMode then
  3490. AdvanceStippleCounter(Abs(Y2 - Y1) + 1);
  3491. Exit;
  3492. end;
  3493. if Y1 < FClipRect.Top then
  3494. begin
  3495. if not FMeasuringMode then
  3496. AdvanceStippleCounter(FClipRect.Top - Y1);
  3497. Y1 := FClipRect.Top;
  3498. end else
  3499. if Y1 >= FClipRect.Bottom then
  3500. begin
  3501. if not FMeasuringMode then
  3502. AdvanceStippleCounter(Y1 - (FClipRect.Bottom - 1));
  3503. Y1 := FClipRect.Bottom - 1;
  3504. end;
  3505. N := 0;
  3506. if Y2 < FClipRect.Top then
  3507. begin
  3508. N := FClipRect.Top - Y2;
  3509. Y2 := FClipRect.Top;
  3510. end else
  3511. if Y2 >= FClipRect.Bottom then
  3512. begin
  3513. N := Y2 - (FClipRect.Bottom - 1);
  3514. Y2 := FClipRect.Bottom - 1;
  3515. end;
  3516. if not FMeasuringMode then
  3517. begin
  3518. if Y2 >= Y1 then
  3519. begin
  3520. for I := Y1 to Y2 do
  3521. SetPixelT(X, I, GetStippleColor)
  3522. end else
  3523. begin
  3524. for I := Y1 downto Y2 do
  3525. SetPixelT(X, I, GetStippleColor);
  3526. end;
  3527. end;
  3528. Changed(MakeRect(X, Y1, X+1, Y2+1));
  3529. if (not FMeasuringMode) and (N > 0) then
  3530. AdvanceStippleCounter(N);
  3531. end else
  3532. if not FMeasuringMode then
  3533. AdvanceStippleCounter(Abs(Y2 - Y1) + 1);
  3534. end;
  3535. procedure TCustomBitmap32.VertLineX(X, Y1, Y2: TFixed; Value: TColor32);
  3536. //Author: Michael Hansen
  3537. var
  3538. I: Integer;
  3539. Y1F, Y2F, XF, Count: Integer;
  3540. Wy1, Wy2, Wx, Wt: TColor32;
  3541. PDst: PColor32;
  3542. begin
  3543. if Y1 > Y2 then
  3544. Swap(Y1, Y2);
  3545. if not FMeasuringMode then
  3546. begin
  3547. Y1F := Y1 shr 16;
  3548. Y2F := Y2 shr 16;
  3549. XF := X shr 16;
  3550. PDst := PixelPtr[XF, Y1F];
  3551. Wx := X and $ffff xor $ffff;
  3552. Wy1 := Y1 and $ffff xor $ffff;
  3553. Wy2 := Y2 and $ffff;
  3554. Count := Y2F - Y1F - 1;
  3555. if Wx > 0 then
  3556. begin
  3557. CombineMem(Value, PDst^, GAMMA_ENCODING_TABLE[(Wx * Wy1) shr 24]);
  3558. Inc(PDst, FWidth);
  3559. Wt := GAMMA_ENCODING_TABLE[Wx shr 8];
  3560. for I := 0 to Count - 1 do
  3561. begin
  3562. CombineMem(Value, PDst^, Wt);
  3563. Inc(PDst, FWidth);
  3564. end;
  3565. CombineMem(Value, PDst^, GAMMA_ENCODING_TABLE[(Wx * Wy2) shr 24]);
  3566. end;
  3567. PDst := PixelPtr[XF + 1, Y1F];
  3568. Wx := Wx xor $ffff;
  3569. if Wx > 0 then
  3570. begin
  3571. CombineMem(Value, PDst^, GAMMA_ENCODING_TABLE[(Wx * Wy1) shr 24]);
  3572. Inc(PDst, FWidth);
  3573. Wt := GAMMA_ENCODING_TABLE[Wx shr 8];
  3574. for I := 0 to Count - 1 do
  3575. begin
  3576. CombineMem(Value, PDst^, Wt);
  3577. Inc(PDst, FWidth);
  3578. end;
  3579. CombineMem(Value, PDst^, GAMMA_ENCODING_TABLE[(Wx * Wy2) shr 24]);
  3580. end;
  3581. EMMS;
  3582. end;
  3583. Changed(MakeRect(FixedRect(X, Y1, X+1, Y2+1), rrOutside), AREAINFO_LINE + 2);
  3584. end;
  3585. procedure TCustomBitmap32.VertLineXS(X, Y1, Y2: TFixed; Value: TColor32);
  3586. //author: Michael Hansen
  3587. begin
  3588. if Y1 > Y2 then
  3589. Swap(Y1, Y2);
  3590. Y1 := Constrain(Y1, FFixedClipRect.Top, FFixedClipRect.Bottom - FIXEDONE);
  3591. Y2 := Constrain(Y2, FFixedClipRect.Top, FFixedClipRect.Bottom - FIXEDONE);
  3592. if (Abs(Y2 - Y1) > FIXEDONE) and InRange(X, FFixedClipRect.Left, FFixedClipRect.Right - FIXEDONE) then
  3593. VertLineX(X, Y1, Y2, Value)
  3594. else
  3595. LineXS(X, Y1, X, Y2, Value);
  3596. end;
  3597. procedure TCustomBitmap32.Line(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean);
  3598. var
  3599. Dy, Dx, Sy, Sx, I, Delta: Integer;
  3600. P: PColor32;
  3601. begin
  3602. Dx := X2 - X1;
  3603. Dy := Y2 - Y1;
  3604. if Dx > 0 then
  3605. Sx := 1
  3606. else
  3607. if Dx < 0 then
  3608. begin
  3609. Dx := -Dx;
  3610. Sx := -1;
  3611. end else // Dx = 0
  3612. begin
  3613. if Dy > 0 then
  3614. VertLine(X1, Y1, Y2 - 1, Value)
  3615. else
  3616. if Dy < 0 then
  3617. VertLine(X1, Y2 + 1, Y1, Value);
  3618. if L then
  3619. begin
  3620. if not FMeasuringMode then
  3621. Pixel[X2, Y2] := Value;
  3622. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  3623. end;
  3624. Exit;
  3625. end;
  3626. if Dy > 0 then
  3627. Sy := 1
  3628. else
  3629. if Dy < 0 then
  3630. begin
  3631. Dy := -Dy;
  3632. Sy := -1;
  3633. end else // Dy = 0
  3634. begin
  3635. if X2 > X1 then
  3636. HorzLine(X1, Y1, X2 - 1, Value)
  3637. else
  3638. HorzLine(X2 + 1, Y1, X1, Value);
  3639. if L then
  3640. begin
  3641. if not FMeasuringMode then
  3642. Pixel[X2, Y2] := Value;
  3643. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  3644. end;
  3645. Exit;
  3646. end;
  3647. if not FMeasuringMode then
  3648. begin
  3649. P := PixelPtr[X1, Y1];
  3650. Sy := Sy * Width;
  3651. if Dx > Dy then
  3652. begin
  3653. Delta := Dx shr 1;
  3654. for I := 0 to Dx - 1 do
  3655. begin
  3656. P^ := Value;
  3657. Inc(P, Sx);
  3658. Inc(Delta, Dy);
  3659. if Delta >= Dx then
  3660. begin
  3661. Inc(P, Sy);
  3662. Dec(Delta, Dx);
  3663. end;
  3664. end;
  3665. end else // Dx < Dy
  3666. begin
  3667. Delta := Dy shr 1;
  3668. for I := 0 to Dy - 1 do
  3669. begin
  3670. P^ := Value;
  3671. Inc(P, Sy);
  3672. Inc(Delta, Dx);
  3673. if Delta >= Dy then
  3674. begin
  3675. Inc(P, Sx);
  3676. Dec(Delta, Dy);
  3677. end;
  3678. end;
  3679. end;
  3680. if L then
  3681. P^ := Value;
  3682. end;
  3683. Changed(MakeRect(X1, Y1, X2, Y2), AREAINFO_LINE + 1);
  3684. end;
  3685. procedure TCustomBitmap32.LineS(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean);
  3686. var
  3687. Dx2, Dy2,Cx1, Cx2, Cy1, Cy2, PI, Sx, Sy, Dx, Dy, xd, yd, rem, term, e: Integer;
  3688. OC: Int64;
  3689. Swapped, CheckAux: Boolean;
  3690. P: PColor32;
  3691. ChangedRect: TRect;
  3692. begin
  3693. ChangedRect := MakeRect(X1, Y1, X2, Y2);
  3694. if not FMeasuringMode then
  3695. begin
  3696. Dx := X2 - X1; Dy := Y2 - Y1;
  3697. // check for trivial cases...
  3698. if Dx = 0 then // vertical line?
  3699. begin
  3700. if Dy > 0 then
  3701. VertLineS(X1, Y1, Y2 - 1, Value)
  3702. else
  3703. if Dy < 0 then
  3704. VertLineS(X1, Y2 + 1, Y1, Value);
  3705. if L then
  3706. begin
  3707. PixelS[X2, Y2] := Value;
  3708. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  3709. end;
  3710. Exit;
  3711. end else
  3712. if Dy = 0 then // horizontal line?
  3713. begin
  3714. if Dx > 0 then
  3715. HorzLineS(X1, Y1, X2 - 1, Value)
  3716. else
  3717. if Dx < 0 then
  3718. HorzLineS(X2 + 1, Y1, X1, Value);
  3719. if L then
  3720. begin
  3721. PixelS[X2, Y2] := Value;
  3722. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  3723. end;
  3724. Exit;
  3725. end;
  3726. Cx1 := FClipRect.Left; Cx2 := FClipRect.Right - 1;
  3727. Cy1 := FClipRect.Top; Cy2 := FClipRect.Bottom - 1;
  3728. if Dx > 0 then
  3729. begin
  3730. if (X1 > Cx2) or (X2 < Cx1) then
  3731. Exit; // segment not visible
  3732. Sx := 1;
  3733. end else
  3734. begin
  3735. if (X2 > Cx2) or (X1 < Cx1)
  3736. then Exit; // segment not visible
  3737. Sx := -1;
  3738. X1 := -X1; X2 := -X2; Dx := -Dx;
  3739. Cx1 := -Cx1; Cx2 := -Cx2;
  3740. Swap(Cx1, Cx2);
  3741. end;
  3742. if Dy > 0 then
  3743. begin
  3744. if (Y1 > Cy2) or (Y2 < Cy1) then
  3745. Exit; // segment not visible
  3746. Sy := 1;
  3747. end else
  3748. begin
  3749. if (Y2 > Cy2) or (Y1 < Cy1) then
  3750. Exit; // segment not visible
  3751. Sy := -1;
  3752. Y1 := -Y1; Y2 := -Y2; Dy := -Dy;
  3753. Cy1 := -Cy1; Cy2 := -Cy2;
  3754. Swap(Cy1, Cy2);
  3755. end;
  3756. if Dx < Dy then
  3757. begin
  3758. Swapped := True;
  3759. Swap(X1, Y1); Swap(X2, Y2); Swap(Dx, Dy);
  3760. Swap(Cx1, Cy1); Swap(Cx2, Cy2); Swap(Sx, Sy);
  3761. end else
  3762. Swapped := False;
  3763. // Bresenham's set up:
  3764. Dx2 := Dx shl 1; Dy2 := Dy shl 1;
  3765. xd := X1; yd := Y1; e := Dy2 - Dx; term := X2;
  3766. CheckAux := True;
  3767. // Clipping rect horizontal entry
  3768. if Y1 < Cy1 then
  3769. begin
  3770. OC := Int64(Dx2) * (Cy1 - Y1) - Dx;
  3771. Inc(xd, OC div Dy2);
  3772. rem := OC mod Dy2;
  3773. if xd > Cx2 then
  3774. Exit;
  3775. if xd >= Cx1 then
  3776. begin
  3777. yd := Cy1;
  3778. Dec(e, rem + Dx);
  3779. if rem > 0 then
  3780. begin
  3781. Inc(xd);
  3782. Inc(e, Dy2);
  3783. end;
  3784. CheckAux := False; // to avoid ugly goto we set this to omit the next check
  3785. end;
  3786. end;
  3787. // clipping rect vertical entry
  3788. if CheckAux and (X1 < Cx1) then
  3789. begin
  3790. OC := Int64(Dy2) * (Cx1 - X1);
  3791. Inc(yd, OC div Dx2);
  3792. rem := OC mod Dx2;
  3793. if (yd > Cy2) or (yd = Cy2) and (rem >= Dx) then
  3794. Exit;
  3795. xd := Cx1;
  3796. Inc(e, rem);
  3797. if (rem >= Dx) then
  3798. begin
  3799. Inc(yd);
  3800. Dec(e, Dx2);
  3801. end;
  3802. end;
  3803. // set auxiliary var to indicate that term is not clipped, since
  3804. // term still has the unclipped value assigned at setup.
  3805. CheckAux := False;
  3806. // is the segment exiting the clipping rect?
  3807. if Y2 > Cy2 then
  3808. begin
  3809. OC := Int64(Dx2) * (Cy2 - Y1) + Dx;
  3810. term := X1 + OC div Dy2;
  3811. rem := OC mod Dy2;
  3812. if rem = 0 then
  3813. Dec(term);
  3814. CheckAux := True; // set auxiliary var to indicate that term is clipped
  3815. end;
  3816. if term > Cx2 then
  3817. begin
  3818. term := Cx2;
  3819. CheckAux := True; // set auxiliary var to indicate that term is clipped
  3820. end;
  3821. Inc(term);
  3822. if Sy = -1 then
  3823. yd := -yd;
  3824. if Sx = -1 then
  3825. begin
  3826. xd := -xd;
  3827. term := -term;
  3828. end;
  3829. Dec(Dx2, Dy2);
  3830. if Swapped then
  3831. begin
  3832. PI := Sx * Width;
  3833. P := @Bits[yd + xd * Width];
  3834. end else
  3835. begin
  3836. PI := Sx;
  3837. Sy := Sy * Width;
  3838. P := @Bits[xd + yd * Width];
  3839. end;
  3840. // do we need to skip the last pixel of the line and is term not clipped?
  3841. if not(L or CheckAux) then
  3842. begin
  3843. if xd < term then
  3844. Dec(term)
  3845. else
  3846. Inc(term);
  3847. end;
  3848. while xd <> term do
  3849. begin
  3850. Inc(xd, Sx);
  3851. P^ := Value;
  3852. Inc(P, PI);
  3853. if e >= 0 then
  3854. begin
  3855. Inc(P, Sy);
  3856. Dec(e, Dx2);
  3857. end else
  3858. Inc(e, Dy2);
  3859. end;
  3860. end;
  3861. Changed(ChangedRect, AREAINFO_LINE + 1);
  3862. end;
  3863. procedure TCustomBitmap32.LineT(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean);
  3864. var
  3865. Dy, Dx, Sy, Sx, I, Delta: Integer;
  3866. P: PColor32;
  3867. BlendMem: TBlendMem;
  3868. ChangedRect: TRect;
  3869. begin
  3870. ChangedRect := MakeRect(X1, Y1, X2, Y2);
  3871. if not FMeasuringMode then
  3872. begin
  3873. Dx := X2 - X1;
  3874. Dy := Y2 - Y1;
  3875. if Dx > 0 then
  3876. Sx := 1
  3877. else
  3878. if Dx < 0 then
  3879. begin
  3880. Dx := -Dx;
  3881. Sx := -1;
  3882. end else // Dx = 0
  3883. begin
  3884. if Dy > 0 then
  3885. VertLineT(X1, Y1, Y2 - 1, Value)
  3886. else
  3887. if Dy < 0 then
  3888. VertLineT(X1, Y2 + 1, Y1, Value);
  3889. if L then
  3890. begin
  3891. SetPixelT(X2, Y2, Value);
  3892. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  3893. end;
  3894. Exit;
  3895. end;
  3896. if Dy > 0 then
  3897. Sy := 1
  3898. else
  3899. if Dy < 0 then
  3900. begin
  3901. Dy := -Dy;
  3902. Sy := -1;
  3903. end else // Dy = 0
  3904. begin
  3905. if X2 > X1 then
  3906. HorzLineT(X1, Y1, X2 - 1, Value)
  3907. else
  3908. HorzLineT(X2 + 1, Y1, X1, Value);
  3909. if L then
  3910. begin
  3911. SetPixelT(X2, Y2, Value);
  3912. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  3913. end;
  3914. Exit;
  3915. end;
  3916. P := PixelPtr[X1, Y1];
  3917. Sy := Sy * Width;
  3918. BlendMem := TBlendMem(BlendProc);
  3919. if Dx > Dy then
  3920. begin
  3921. Delta := Dx shr 1;
  3922. for I := 0 to Dx - 1 do
  3923. begin
  3924. BlendMem(Value, P^);
  3925. Inc(P, Sx);
  3926. Inc(Delta, Dy);
  3927. if Delta >= Dx then
  3928. begin
  3929. Inc(P, Sy);
  3930. Dec(Delta, Dx);
  3931. end;
  3932. end;
  3933. end else // Dx < Dy
  3934. begin
  3935. Delta := Dy shr 1;
  3936. for I := 0 to Dy - 1 do
  3937. begin
  3938. BlendMem(Value, P^);
  3939. Inc(P, Sy);
  3940. Inc(Delta, Dx);
  3941. if Delta >= Dy then
  3942. begin
  3943. Inc(P, Sx);
  3944. Dec(Delta, Dy);
  3945. end;
  3946. end;
  3947. end;
  3948. if L then
  3949. BlendMem(Value, P^);
  3950. EMMS;
  3951. end;
  3952. Changed(ChangedRect, AREAINFO_LINE + 1);
  3953. end;
  3954. procedure TCustomBitmap32.LineTS(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean);
  3955. var
  3956. Cx1, Cx2, Cy1, Cy2, PI, Sx, Sy, Dx, Dy, xd, yd, Dx2, Dy2, rem, term, e: Integer;
  3957. OC: Int64;
  3958. Swapped, CheckAux: Boolean;
  3959. P: PColor32;
  3960. BlendMem: TBlendMem;
  3961. ChangedRect: TRect;
  3962. begin
  3963. ChangedRect := MakeRect(X1, Y1, X2, Y2);
  3964. if not FMeasuringMode then
  3965. begin
  3966. Dx := X2 - X1; Dy := Y2 - Y1;
  3967. // check for trivial cases...
  3968. if Dx = 0 then // vertical line?
  3969. begin
  3970. if Dy > 0 then
  3971. VertLineTS(X1, Y1, Y2 - 1, Value)
  3972. else
  3973. if Dy < 0 then
  3974. VertLineTS(X1, Y2 + 1, Y1, Value);
  3975. if L then
  3976. begin
  3977. SetPixelTS(X2, Y2, Value);
  3978. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  3979. end;
  3980. Exit;
  3981. end else
  3982. if Dy = 0 then // horizontal line?
  3983. begin
  3984. if Dx > 0 then
  3985. HorzLineTS(X1, Y1, X2 - 1, Value)
  3986. else
  3987. if Dx < 0 then
  3988. HorzLineTS(X2 + 1, Y1, X1, Value);
  3989. if L then
  3990. begin
  3991. SetPixelTS(X2, Y2, Value);
  3992. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  3993. end;
  3994. Exit;
  3995. end;
  3996. Cx1 := FClipRect.Left; Cx2 := FClipRect.Right - 1;
  3997. Cy1 := FClipRect.Top; Cy2 := FClipRect.Bottom - 1;
  3998. if Dx > 0 then
  3999. begin
  4000. if (X1 > Cx2) or (X2 < Cx1) then
  4001. Exit; // segment not visible
  4002. Sx := 1;
  4003. end else
  4004. begin
  4005. if (X2 > Cx2) or (X1 < Cx1) then
  4006. Exit; // segment not visible
  4007. Sx := -1;
  4008. X1 := -X1; X2 := -X2; Dx := -Dx;
  4009. Cx1 := -Cx1; Cx2 := -Cx2;
  4010. Swap(Cx1, Cx2);
  4011. end;
  4012. if Dy > 0 then
  4013. begin
  4014. if (Y1 > Cy2) or (Y2 < Cy1) then
  4015. Exit; // segment not visible
  4016. Sy := 1;
  4017. end else
  4018. begin
  4019. if (Y2 > Cy2) or (Y1 < Cy1) then
  4020. Exit; // segment not visible
  4021. Sy := -1;
  4022. Y1 := -Y1; Y2 := -Y2; Dy := -Dy;
  4023. Cy1 := -Cy1; Cy2 := -Cy2;
  4024. Swap(Cy1, Cy2);
  4025. end;
  4026. if Dx < Dy then
  4027. begin
  4028. Swapped := True;
  4029. Swap(X1, Y1); Swap(X2, Y2); Swap(Dx, Dy);
  4030. Swap(Cx1, Cy1); Swap(Cx2, Cy2); Swap(Sx, Sy);
  4031. end else
  4032. Swapped := False;
  4033. // Bresenham's set up:
  4034. Dx2 := Dx shl 1; Dy2 := Dy shl 1;
  4035. xd := X1; yd := Y1; e := Dy2 - Dx; term := X2;
  4036. CheckAux := True;
  4037. // clipping rect horizontal entry
  4038. if Y1 < Cy1 then
  4039. begin
  4040. OC := Int64(Dx2) * (Cy1 - Y1) - Dx;
  4041. Inc(xd, OC div Dy2);
  4042. rem := OC mod Dy2;
  4043. if xd > Cx2 then
  4044. Exit;
  4045. if xd >= Cx1 then
  4046. begin
  4047. yd := Cy1;
  4048. Dec(e, rem + Dx);
  4049. if rem > 0 then
  4050. begin
  4051. Inc(xd);
  4052. Inc(e, Dy2);
  4053. end;
  4054. CheckAux := False; // to avoid ugly goto we set this to omit the next check
  4055. end;
  4056. end;
  4057. // clipping rect vertical entry
  4058. if CheckAux and (X1 < Cx1) then
  4059. begin
  4060. OC := Int64(Dy2) * (Cx1 - X1);
  4061. Inc(yd, OC div Dx2);
  4062. rem := OC mod Dx2;
  4063. if (yd > Cy2) or (yd = Cy2) and (rem >= Dx) then
  4064. Exit;
  4065. xd := Cx1;
  4066. Inc(e, rem);
  4067. if (rem >= Dx) then
  4068. begin
  4069. Inc(yd);
  4070. Dec(e, Dx2);
  4071. end;
  4072. end;
  4073. // set auxiliary var to indicate that term is not clipped, since
  4074. // term still has the unclipped value assigned at setup.
  4075. CheckAux := False;
  4076. // is the segment exiting the clipping rect?
  4077. if Y2 > Cy2 then
  4078. begin
  4079. OC := Int64(Dx2) * (Cy2 - Y1) + Dx;
  4080. term := X1 + OC div Dy2;
  4081. rem := OC mod Dy2;
  4082. if rem = 0 then
  4083. Dec(term);
  4084. CheckAux := True; // set auxiliary var to indicate that term is clipped
  4085. end;
  4086. if term > Cx2 then
  4087. begin
  4088. term := Cx2;
  4089. CheckAux := True; // set auxiliary var to indicate that term is clipped
  4090. end;
  4091. Inc(term);
  4092. if Sy = -1 then
  4093. yd := -yd;
  4094. if Sx = -1 then
  4095. begin
  4096. xd := -xd;
  4097. term := -term;
  4098. end;
  4099. Dec(Dx2, Dy2);
  4100. if Swapped then
  4101. begin
  4102. PI := Sx * Width;
  4103. P := @Bits[yd + xd * Width];
  4104. end else
  4105. begin
  4106. PI := Sx;
  4107. Sy := Sy * Width;
  4108. P := @Bits[xd + yd * Width];
  4109. end;
  4110. // do we need to skip the last pixel of the line and is term not clipped?
  4111. if not(L or CheckAux) then
  4112. begin
  4113. if xd < term then
  4114. Dec(term)
  4115. else
  4116. Inc(term);
  4117. end;
  4118. BlendMem := BLEND_MEM[FCombineMode]^;
  4119. while xd <> term do
  4120. begin
  4121. Inc(xd, Sx);
  4122. BlendMem(Value, P^);
  4123. Inc(P, PI);
  4124. if e >= 0 then
  4125. begin
  4126. Inc(P, Sy);
  4127. Dec(e, Dx2);
  4128. end
  4129. else
  4130. Inc(e, Dy2);
  4131. end;
  4132. EMMS;
  4133. end;
  4134. Changed(ChangedRect, AREAINFO_LINE + 1);
  4135. end;
  4136. procedure TCustomBitmap32.LineX(X1, Y1, X2, Y2: TFixed; Value: TColor32; L: Boolean);
  4137. var
  4138. n, i: Integer;
  4139. nx, ny, hyp, hypl: Integer;
  4140. A: TColor32;
  4141. h: Single;
  4142. ChangedRect: TRect;
  4143. begin
  4144. ChangedRect := MakeRect(FixedRect(X1, Y1, X2, Y2), rrOutside);
  4145. if not FMeasuringMode then
  4146. begin
  4147. nx := X2 - X1; ny := Y2 - Y1;
  4148. Inc(X1, 127); Inc(Y1, 127); Inc(X2, 127); Inc(Y2, 127);
  4149. hyp := Hypot(nx, ny);
  4150. if hyp = 0 then
  4151. Exit;
  4152. hypl := hyp + (Integer(L) * FixedOne);
  4153. if (hypl < 256) then
  4154. Exit;
  4155. n := hypl shr 16;
  4156. if n > 0 then
  4157. begin
  4158. h := 65536 / hyp;
  4159. nx := Round(nx * h); ny := Round(ny * h);
  4160. for i := 0 to n - 1 do
  4161. begin
  4162. SET_T256(X1 shr 8, Y1 shr 8, Value);
  4163. Inc(X1, nx);
  4164. Inc(Y1, ny);
  4165. end;
  4166. end;
  4167. A := Value shr 24;
  4168. hyp := hypl - n shl 16;
  4169. A := A * Cardinal(hyp) shl 8 and $FF000000;
  4170. SET_T256((X1 + X2 - nx) shr 9, (Y1 + Y2 - ny) shr 9, Value and $00FFFFFF + A);
  4171. EMMS;
  4172. end;
  4173. Changed(ChangedRect, AREAINFO_LINE + 2); // +1 for AA
  4174. end;
  4175. procedure TCustomBitmap32.LineF(X1, Y1, X2, Y2: Single; Value: TColor32; L: Boolean);
  4176. begin
  4177. LineX(Fixed(X1), Fixed(Y1), Fixed(X2), Fixed(Y2), Value, L);
  4178. end;
  4179. procedure TCustomBitmap32.LineXS(X1, Y1, X2, Y2: TFixed; Value: TColor32; L: Boolean);
  4180. var
  4181. n, i: Integer;
  4182. ex, ey, nx, ny, hyp, hypl: Integer;
  4183. A: TColor32;
  4184. h: Single;
  4185. ChangedRect: TRect;
  4186. begin
  4187. ChangedRect := MakeRect(FixedRect(X1, Y1, X2, Y2), rrOutside);
  4188. ex := X2; ey := Y2;
  4189. // Check for visibility and clip the coordinates
  4190. if not ClipLine(Integer(X1), Integer(Y1), Integer(X2), Integer(Y2),
  4191. FFixedClipRect.Left - $10000,
  4192. FFixedClipRect.Top - $10000,
  4193. FFixedClipRect.Right, FFixedClipRect.Bottom) then
  4194. Exit;
  4195. if not FMeasuringMode then
  4196. begin
  4197. { TODO : Handle L on clipping here... }
  4198. if (ex <> X2) or (ey <> Y2) then
  4199. L := True;
  4200. // Check if it lies entirely in the bitmap area. Even after clipping
  4201. // some pixels may lie outside the bitmap due to antialiasing
  4202. if (X1 > FFixedClipRect.Left) and (X1 < FFixedClipRect.Right - $20000) and
  4203. (Y1 > FFixedClipRect.Top) and (Y1 < FFixedClipRect.Bottom - $20000) and
  4204. (X2 > FFixedClipRect.Left) and (X2 < FFixedClipRect.Right - $20000) and
  4205. (Y2 > FFixedClipRect.Top) and (Y2 < FFixedClipRect.Bottom - $20000) then
  4206. begin
  4207. LineX(X1, Y1, X2, Y2, Value, L);
  4208. Exit;
  4209. end;
  4210. // if we are still here, it means that the line touches one or several bitmap
  4211. // boundaries. Use the safe version of antialiased pixel routine
  4212. nx := X2 - X1; ny := Y2 - Y1;
  4213. Inc(X1, 127); Inc(Y1, 127); Inc(X2, 127); Inc(Y2, 127);
  4214. hyp := Hypot(nx, ny);
  4215. if hyp = 0 then
  4216. Exit;
  4217. hypl := hyp + (Integer(L) * FixedOne);
  4218. if hypl < 256 then
  4219. Exit;
  4220. n := hypl shr 16;
  4221. if n > 0 then
  4222. begin
  4223. h := 65536 / hyp;
  4224. nx := Round(nx * h); ny := Round(ny * h);
  4225. for i := 0 to n - 1 do
  4226. begin
  4227. SET_TS256(SAR_8(X1), SAR_8(Y1), Value);
  4228. X1 := X1 + nx;
  4229. Y1 := Y1 + ny;
  4230. end;
  4231. end;
  4232. A := Value shr 24;
  4233. hyp := hypl - n shl 16;
  4234. A := A * Cardinal(hyp) shl 8 and $FF000000;
  4235. SET_TS256(SAR_9(X1 + X2 - nx), SAR_9(Y1 + Y2 - ny), Value and $00FFFFFF + A);
  4236. EMMS;
  4237. end;
  4238. Changed(ChangedRect, AREAINFO_LINE + 2); // +1 for AA
  4239. end;
  4240. procedure TCustomBitmap32.LineFS(X1, Y1, X2, Y2: Single; Value: TColor32; L: Boolean);
  4241. begin
  4242. LineXS(Fixed(X1), Fixed(Y1), Fixed(X2), Fixed(Y2), Value, L);
  4243. end;
  4244. procedure TCustomBitmap32.LineXP(X1, Y1, X2, Y2: TFixed; L: Boolean);
  4245. var
  4246. n, i: Integer;
  4247. nx, ny, hyp, hypl: Integer;
  4248. A, C: TColor32;
  4249. ChangedRect: TRect;
  4250. begin
  4251. ChangedRect := MakeRect(FixedRect(X1, Y1, X2, Y2), rrOutside);
  4252. if not FMeasuringMode then
  4253. begin
  4254. nx := X2 - X1; ny := Y2 - Y1;
  4255. Inc(X1, 127); Inc(Y1, 127); Inc(X2, 127); Inc(Y2, 127);
  4256. hyp := Hypot(nx, ny);
  4257. if hyp = 0 then
  4258. Exit;
  4259. hypl := hyp + (Integer(L) * FixedOne);
  4260. if hypl < 256 then
  4261. Exit;
  4262. n := hypl shr 16;
  4263. if n > 0 then
  4264. begin
  4265. nx := Round(nx / hyp * 65536);
  4266. ny := Round(ny / hyp * 65536);
  4267. for i := 0 to n - 1 do
  4268. begin
  4269. C := GetStippleColor;
  4270. SET_T256(X1 shr 8, Y1 shr 8, C);
  4271. EMMS;
  4272. X1 := X1 + nx;
  4273. Y1 := Y1 + ny;
  4274. end;
  4275. end;
  4276. C := GetStippleColor;
  4277. A := C shr 24;
  4278. hyp := hypl - n shl 16;
  4279. A := A * Longword(hyp) shl 8 and $FF000000;
  4280. SET_T256((X1 + X2 - nx) shr 9, (Y1 + Y2 - ny) shr 9, C and $00FFFFFF + A);
  4281. EMMS;
  4282. end;
  4283. Changed(ChangedRect, AREAINFO_LINE + 2); // +1 for AA
  4284. end;
  4285. procedure TCustomBitmap32.LineFP(X1, Y1, X2, Y2: Single; L: Boolean);
  4286. begin
  4287. LineXP(Fixed(X1), Fixed(Y1), Fixed(X2), Fixed(Y2), L);
  4288. end;
  4289. procedure TCustomBitmap32.LineXSP(X1, Y1, X2, Y2: TFixed; L: Boolean);
  4290. const
  4291. StippleInc: array [Boolean] of Integer = (0, 1);
  4292. var
  4293. n, i: Integer;
  4294. sx, sy, ex, ey, nx, ny, hyp, hypl: Integer;
  4295. A, C: TColor32;
  4296. ChangedRect: TRect;
  4297. begin
  4298. ChangedRect := MakeRect(FixedRect(X1, Y1, X2, Y2), rrOutside);
  4299. if not FMeasuringMode then
  4300. begin
  4301. sx := X1; sy := Y1; ex := X2; ey := Y2;
  4302. // Check for visibility and clip the coordinates
  4303. if not ClipLine(Integer(X1), Integer(Y1), Integer(X2), Integer(Y2),
  4304. FFixedClipRect.Left - $10000, FFixedClipRect.Top - $10000,
  4305. FFixedClipRect.Right, FFixedClipRect.Bottom) then
  4306. begin
  4307. AdvanceStippleCounter(GR32_Math.Hypot(Integer((X2 - X1) shr 16), Integer((Y2 - Y1) shr 16) - StippleInc[L]));
  4308. Exit;
  4309. end;
  4310. if (ex <> X2) or (ey <> Y2) then
  4311. L := True;
  4312. // Check if it lies entirely in the bitmap area. Even after clipping
  4313. // some pixels may lie outside the bitmap due to antialiasing
  4314. if (X1 > FFixedClipRect.Left) and (X1 < FFixedClipRect.Right - $20000) and
  4315. (Y1 > FFixedClipRect.Top) and (Y1 < FFixedClipRect.Bottom - $20000) and
  4316. (X2 > FFixedClipRect.Left) and (X2 < FFixedClipRect.Right - $20000) and
  4317. (Y2 > FFixedClipRect.Top) and (Y2 < FFixedClipRect.Bottom - $20000) then
  4318. begin
  4319. LineXP(X1, Y1, X2, Y2, L);
  4320. Exit;
  4321. end;
  4322. if (sx <> X1) or (sy <> Y1) then
  4323. AdvanceStippleCounter(GR32_Math.Hypot(Integer((X1 - sx) shr 16), Integer((Y1 - sy) shr 16)));
  4324. // if we are still here, it means that the line touches one or several bitmap
  4325. // boundaries. Use the safe version of antialiased pixel routine
  4326. nx := X2 - X1; ny := Y2 - Y1;
  4327. Inc(X1, 127); Inc(Y1, 127); Inc(X2, 127); Inc(Y2, 127);
  4328. hyp := GR32_Math.Hypot(nx, ny);
  4329. if hyp = 0 then
  4330. Exit;
  4331. hypl := hyp + (Integer(L) * FixedOne);
  4332. if hypl < 256 then
  4333. Exit;
  4334. n := hypl shr 16;
  4335. if n > 0 then
  4336. begin
  4337. nx := Round(nx / hyp * 65536); ny := Round(ny / hyp * 65536);
  4338. for i := 0 to n - 1 do
  4339. begin
  4340. C := GetStippleColor;
  4341. SET_TS256(SAR_8(X1), SAR_8(Y1), C);
  4342. EMMS;
  4343. X1 := X1 + nx;
  4344. Y1 := Y1 + ny;
  4345. end;
  4346. end;
  4347. C := GetStippleColor;
  4348. A := C shr 24;
  4349. hyp := hypl - n shl 16;
  4350. A := A * Longword(hyp) shl 8 and $FF000000;
  4351. SET_TS256(SAR_9(X1 + X2 - nx), SAR_9(Y1 + Y2 - ny), C and $00FFFFFF + A);
  4352. EMMS;
  4353. if (ex <> X2) or (ey <> Y2) then
  4354. AdvanceStippleCounter(GR32_Math.Hypot(Integer((X2 - ex) shr 16), Integer((Y2 - ey) shr 16) - StippleInc[L]));
  4355. end;
  4356. Changed(ChangedRect, AREAINFO_LINE + 2); // +1 for AA
  4357. end;
  4358. procedure TCustomBitmap32.LineFSP(X1, Y1, X2, Y2: Single; L: Boolean);
  4359. begin
  4360. LineXSP(Fixed(X1), Fixed(Y1), Fixed(X2), Fixed(Y2), L);
  4361. end;
  4362. procedure TCustomBitmap32.LineA(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean);
  4363. var
  4364. Dx, Dy, Sx, Sy, D: Integer;
  4365. EC, EA: Word;
  4366. CI: Byte;
  4367. P: PColor32;
  4368. BlendMemEx: TBlendMemEx;
  4369. begin
  4370. if (X1 = X2) or (Y1 = Y2) then
  4371. begin
  4372. LineT(X1, Y1, X2, Y2, Value, L);
  4373. Exit;
  4374. end;
  4375. if not FMeasuringMode then
  4376. begin
  4377. Dx := X2 - X1;
  4378. Dy := Y2 - Y1;
  4379. if Dx > 0 then
  4380. Sx := 1
  4381. else
  4382. begin
  4383. Sx := -1;
  4384. Dx := -Dx;
  4385. end;
  4386. if Dy > 0 then
  4387. Sy := 1
  4388. else
  4389. begin
  4390. Sy := -1;
  4391. Dy := -Dy;
  4392. end;
  4393. EC := 0;
  4394. BLEND_MEM[FCombineMode]^(Value, Bits[X1 + Y1 * Width]);
  4395. BlendMemEx := BLEND_MEM_EX[FCombineMode]^;
  4396. if Dy > Dx then
  4397. begin
  4398. EA := Dx shl 16 div Dy;
  4399. if not L then
  4400. Dec(Dy);
  4401. while Dy > 0 do
  4402. begin
  4403. Dec(Dy);
  4404. D := EC;
  4405. Inc(EC, EA);
  4406. if EC <= D then
  4407. Inc(X1, Sx);
  4408. Inc(Y1, Sy);
  4409. CI := EC shr 8;
  4410. P := @Bits[X1 + Y1 * Width];
  4411. BlendMemEx(Value, P^, GAMMA_ENCODING_TABLE[CI xor $FF]);
  4412. Inc(P, Sx);
  4413. BlendMemEx(Value, P^, GAMMA_ENCODING_TABLE[CI]);
  4414. end;
  4415. end else // DY <= DX
  4416. begin
  4417. EA := Dy shl 16 div Dx;
  4418. if not L then
  4419. Dec(Dx);
  4420. while Dx > 0 do
  4421. begin
  4422. Dec(Dx);
  4423. D := EC;
  4424. Inc(EC, EA);
  4425. if EC <= D then
  4426. Inc(Y1, Sy);
  4427. Inc(X1, Sx);
  4428. CI := EC shr 8;
  4429. P := @Bits[X1 + Y1 * Width];
  4430. BlendMemEx(Value, P^, GAMMA_ENCODING_TABLE[CI xor $FF]);
  4431. if Sy = 1 then
  4432. Inc(P, Width) else Dec(P, Width);
  4433. BlendMemEx(Value, P^, GAMMA_ENCODING_TABLE[CI]);
  4434. end;
  4435. end;
  4436. EMMS;
  4437. end;
  4438. Changed(MakeRect(X1, Y1, X2, Y2), AREAINFO_LINE + 2); // +1 for AA
  4439. end;
  4440. procedure TCustomBitmap32.LineAS(X1, Y1, X2, Y2: Integer; Value: TColor32; L: Boolean);
  4441. var
  4442. Cx1, Cx2, Cy1, Cy2, PI, Sx, Sy, Dx, Dy, xd, yd, rem, term, tmp: Integer;
  4443. CheckVert, CornerAA, TermClipped: Boolean;
  4444. D1, D2: PInteger;
  4445. EC, EA, ED, D: Word;
  4446. CI: Byte;
  4447. P: PColor32;
  4448. BlendMemEx: TBlendMemEx;
  4449. ChangedRect: TRect;
  4450. begin
  4451. if (FClipRect.Right = FClipRect.Left) or (FClipRect.Bottom = FClipRect.Top) then
  4452. Exit;
  4453. ChangedRect := MakeRect(X1, Y1, X2, Y2);
  4454. if not FMeasuringMode then
  4455. begin
  4456. Dx := X2 - X1; Dy := Y2 - Y1;
  4457. // check for trivial cases...
  4458. if Abs(Dx) = Abs(Dy) then // diagonal line?
  4459. begin
  4460. LineTS(X1, Y1, X2, Y2, Value, L);
  4461. Exit;
  4462. end else
  4463. if Dx = 0 then // vertical line?
  4464. begin
  4465. if Dy > 0 then
  4466. VertLineTS(X1, Y1, Y2 - 1, Value)
  4467. else
  4468. if Dy < 0 then
  4469. VertLineTS(X1, Y2 + 1, Y1, Value);
  4470. if L then
  4471. begin
  4472. SetPixelTS(X2, Y2, Value);
  4473. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  4474. end;
  4475. Exit;
  4476. end else
  4477. if Dy = 0 then // horizontal line?
  4478. begin
  4479. if Dx > 0 then
  4480. HorzLineTS(X1, Y1, X2 - 1, Value)
  4481. else
  4482. if Dx < 0 then
  4483. HorzLineTS(X2 + 1, Y1, X1, Value);
  4484. if L then
  4485. begin
  4486. SetPixelTS(X2, Y2, Value);
  4487. Changed(MakeRect(X2, Y2, X2+1, Y2+1));
  4488. end;
  4489. Exit;
  4490. end;
  4491. Cx1 := FClipRect.Left; Cx2 := FClipRect.Right - 1;
  4492. Cy1 := FClipRect.Top; Cy2 := FClipRect.Bottom - 1;
  4493. if Dx > 0 then
  4494. begin
  4495. if (X1 > Cx2) or (X2 < Cx1) then
  4496. Exit; // segment not visible
  4497. Sx := 1;
  4498. end else
  4499. begin
  4500. if (X2 > Cx2) or (X1 < Cx1) then
  4501. Exit; // segment not visible
  4502. Sx := -1;
  4503. X1 := -X1; X2 := -X2; Dx := -Dx;
  4504. Cx1 := -Cx1; Cx2 := -Cx2;
  4505. Swap(Cx1, Cx2);
  4506. end;
  4507. if Dy > 0 then
  4508. begin
  4509. if (Y1 > Cy2) or (Y2 < Cy1) then
  4510. Exit; // segment not visible
  4511. Sy := 1;
  4512. end else
  4513. begin
  4514. if (Y2 > Cy2) or (Y1 < Cy1) then
  4515. Exit; // segment not visible
  4516. Sy := -1;
  4517. Y1 := -Y1; Y2 := -Y2; Dy := -Dy;
  4518. Cy1 := -Cy1; Cy2 := -Cy2;
  4519. Swap(Cy1, Cy2);
  4520. end;
  4521. if Dx < Dy then
  4522. begin
  4523. Swap(X1, Y1); Swap(X2, Y2); Swap(Dx, Dy);
  4524. Swap(Cx1, Cy1); Swap(Cx2, Cy2); Swap(Sx, Sy);
  4525. D1 := @yd; D2 := @xd;
  4526. PI := Sy;
  4527. end else
  4528. begin
  4529. D1 := @xd; D2 := @yd;
  4530. PI := Sy * Width;
  4531. end;
  4532. rem := 0;
  4533. EA := Dy shl 16 div Dx;
  4534. EC := 0;
  4535. xd := X1; yd := Y1;
  4536. CheckVert := True;
  4537. CornerAA := False;
  4538. BlendMemEx := BLEND_MEM_EX[FCombineMode]^;
  4539. // clipping rect horizontal entry
  4540. if Y1 < Cy1 then
  4541. begin
  4542. tmp := (Cy1 - Y1) * 65536;
  4543. rem := tmp - 65536; // rem := (Cy1 - Y1 - 1) * 65536;
  4544. if tmp mod EA > 0 then
  4545. tmp := tmp div EA + 1
  4546. else
  4547. tmp := tmp div EA;
  4548. xd := Math.Min(xd + tmp, X2 + 1);
  4549. EC := tmp * EA;
  4550. if rem mod EA > 0 then
  4551. rem := rem div EA + 1
  4552. else
  4553. rem := rem div EA;
  4554. tmp := tmp - rem;
  4555. // check whether the line is partly visible
  4556. if xd > Cx2 then
  4557. begin
  4558. // do we need to draw an antialiased part on the corner of the clip rect?
  4559. if xd <= Cx2 + tmp then
  4560. CornerAA := True
  4561. else
  4562. Exit;
  4563. end;
  4564. if (xd {+ 1} >= Cx1) or CornerAA then
  4565. begin
  4566. yd := Cy1;
  4567. rem := xd; // save old xd
  4568. ED := EC - EA;
  4569. term := SwapConstrain(xd - tmp, Cx1, Cx2);
  4570. if CornerAA then
  4571. begin
  4572. Dec(ED, (xd - Cx2 - 1) * EA);
  4573. xd := Cx2 + 1;
  4574. end;
  4575. // do we need to negate the vars?
  4576. if Sy = -1 then
  4577. yd := -yd;
  4578. if Sx = -1 then
  4579. begin
  4580. xd := -xd;
  4581. term := -term;
  4582. end;
  4583. // draw special case horizontal line entry (draw only last half of entering segment)
  4584. while xd <> term do
  4585. begin
  4586. Inc(xd, -Sx);
  4587. BlendMemEx(Value, Bits[D1^ + D2^ * Width], GAMMA_ENCODING_TABLE[ED shr 8]);
  4588. Dec(ED, EA);
  4589. end;
  4590. EMMS;
  4591. if CornerAA then
  4592. begin
  4593. // we only needed to draw the visible antialiased part of the line,
  4594. // everything else is outside of our cliprect, so exit now since
  4595. // there is nothing more to paint...
  4596. Changed(MakeRect(X1, Y1, X2, Y2), AREAINFO_LINE + 2);
  4597. Exit;
  4598. end;
  4599. if Sy = -1 then
  4600. yd := -yd; // negate back
  4601. xd := rem; // restore old xd
  4602. CheckVert := False; // to avoid ugly goto we set this to omit the next check
  4603. end;
  4604. end;
  4605. // clipping rect vertical entry
  4606. if CheckVert and (X1 < Cx1) then
  4607. begin
  4608. tmp := (Cx1 - X1) * EA;
  4609. Inc(yd, tmp div 65536);
  4610. EC := tmp;
  4611. xd := Cx1;
  4612. if (yd > Cy2) then
  4613. Exit // Nothing modified so far - no need to call Changed
  4614. else
  4615. if (yd = Cy2) then
  4616. CornerAA := True;
  4617. end;
  4618. term := X2;
  4619. TermClipped := False;
  4620. CheckVert := False;
  4621. // horizontal exit?
  4622. if Y2 > Cy2 then
  4623. begin
  4624. tmp := (Cy2 - Y1) * 65536;
  4625. term := X1 + tmp div EA;
  4626. if not(tmp mod EA > 0) then
  4627. Dec(Term);
  4628. if term < Cx2 then
  4629. begin
  4630. rem := tmp + 65536; // was: rem := (Cy2 - Y1 + 1) * 65536;
  4631. if rem mod EA > 0 then
  4632. rem := X1 + rem div EA + 1
  4633. else
  4634. rem := X1 + rem div EA;
  4635. if rem > Cx2 then
  4636. rem := Cx2;
  4637. CheckVert := True;
  4638. end;
  4639. TermClipped := True;
  4640. end;
  4641. if term > Cx2 then
  4642. begin
  4643. term := Cx2;
  4644. TermClipped := True;
  4645. end;
  4646. Inc(term);
  4647. if Sy = -1 then
  4648. yd := -yd;
  4649. if Sx = -1 then
  4650. begin
  4651. xd := -xd;
  4652. term := -term;
  4653. rem := -rem;
  4654. end;
  4655. // draw line
  4656. if not CornerAA then
  4657. begin
  4658. // do we need to skip the last pixel of the line and is term not clipped?
  4659. if not(L or TermClipped) and not CheckVert then
  4660. begin
  4661. if xd < term then
  4662. Dec(term)
  4663. else
  4664. if xd > term then
  4665. Inc(term);
  4666. end;
  4667. while xd <> term do
  4668. begin
  4669. CI := EC shr 8;
  4670. P := @Bits[D1^ + D2^ * Width];
  4671. BlendMemEx(Value, P^, GAMMA_ENCODING_TABLE[CI xor $FF]);
  4672. Inc(P, PI);
  4673. BlendMemEx(Value, P^, GAMMA_ENCODING_TABLE[CI]);
  4674. // check for overflow and jump to next line...
  4675. D := EC;
  4676. Inc(EC, EA);
  4677. if EC <= D then
  4678. Inc(yd, Sy);
  4679. Inc(xd, Sx);
  4680. end;
  4681. EMMS;
  4682. end;
  4683. // draw special case horizontal line exit (draw only first half of exiting segment)
  4684. if CheckVert then
  4685. begin
  4686. while xd <> rem do
  4687. begin
  4688. BlendMemEx(Value, Bits[D1^ + D2^ * Width], GAMMA_ENCODING_TABLE[EC shr 8 xor $FF]);
  4689. Inc(EC, EA);
  4690. Inc(xd, Sx);
  4691. end;
  4692. EMMS;
  4693. end;
  4694. end;
  4695. Changed(ChangedRect, AREAINFO_LINE + 2); // +1 for AA
  4696. end;
  4697. procedure TCustomBitmap32.MoveTo(X, Y: Integer);
  4698. begin
  4699. RasterX := X;
  4700. RasterY := Y;
  4701. end;
  4702. procedure TCustomBitmap32.LineToS(X, Y: Integer);
  4703. begin
  4704. LineS(RasterX, RasterY, X, Y, PenColor);
  4705. RasterX := X;
  4706. RasterY := Y;
  4707. end;
  4708. procedure TCustomBitmap32.LineToTS(X, Y: Integer);
  4709. begin
  4710. LineTS(RasterX, RasterY, X, Y, PenColor);
  4711. RasterX := X;
  4712. RasterY := Y;
  4713. end;
  4714. procedure TCustomBitmap32.LineToAS(X, Y: Integer);
  4715. begin
  4716. LineAS(RasterX, RasterY, X, Y, PenColor);
  4717. RasterX := X;
  4718. RasterY := Y;
  4719. end;
  4720. procedure TCustomBitmap32.MoveToX(X, Y: TFixed);
  4721. begin
  4722. RasterXF := X;
  4723. RasterYF := Y;
  4724. end;
  4725. procedure TCustomBitmap32.MoveToF(X, Y: Single);
  4726. begin
  4727. RasterXF := Fixed(X);
  4728. RasterYF := Fixed(Y);
  4729. end;
  4730. procedure TCustomBitmap32.LineToXS(X, Y: TFixed);
  4731. begin
  4732. LineXS(RasterXF, RasterYF, X, Y, PenColor);
  4733. RasterXF := X;
  4734. RasterYF := Y;
  4735. end;
  4736. procedure TCustomBitmap32.LineToFS(X, Y: Single);
  4737. begin
  4738. LineToXS(Fixed(X), Fixed(Y));
  4739. end;
  4740. procedure TCustomBitmap32.LineToXSP(X, Y: TFixed);
  4741. begin
  4742. LineXSP(RasterXF, RasterYF, X, Y);
  4743. RasterXF := X;
  4744. RasterYF := Y;
  4745. end;
  4746. procedure TCustomBitmap32.LineToFSP(X, Y: Single);
  4747. begin
  4748. LineToXSP(Fixed(X), Fixed(Y));
  4749. end;
  4750. procedure TCustomBitmap32.FillRect(X1, Y1, X2, Y2: Integer; Value: TColor32);
  4751. var
  4752. j: Integer;
  4753. P: PColor32Array;
  4754. begin
  4755. if not FMeasuringMode then
  4756. begin
  4757. if (FBits <> nil) then
  4758. for j := Y1 to Y2 - 1 do
  4759. begin
  4760. P := Pointer(@Bits[j * FWidth]);
  4761. FillLongword(P[X1], X2 - X1, Value);
  4762. end;
  4763. end;
  4764. Changed(MakeRect(X1, Y1, X2+1, Y2+1));
  4765. end;
  4766. procedure TCustomBitmap32.FillRectS(X1, Y1, X2, Y2: Integer; Value: TColor32);
  4767. begin
  4768. if (X2 > X1) and (Y2 > Y1) and
  4769. (X1 < FClipRect.Right) and (Y1 < FClipRect.Bottom) and
  4770. (X2 > FClipRect.Left) and (Y2 > FClipRect.Top) then
  4771. begin
  4772. if X1 < FClipRect.Left then
  4773. X1 := FClipRect.Left;
  4774. if Y1 < FClipRect.Top then
  4775. Y1 := FClipRect.Top;
  4776. if X2 > FClipRect.Right then
  4777. X2 := FClipRect.Right;
  4778. if Y2 > FClipRect.Bottom then
  4779. Y2 := FClipRect.Bottom;
  4780. FillRect(X1, Y1, X2, Y2, Value); // Calls Changed()
  4781. end;
  4782. end;
  4783. procedure TCustomBitmap32.FillRectT(X1, Y1, X2, Y2: Integer; Value: TColor32);
  4784. var
  4785. i, j: Integer;
  4786. P: PColor32;
  4787. A: Integer;
  4788. ChangedRect: TRect;
  4789. begin
  4790. A := Value shr 24;
  4791. if A = $FF then
  4792. FillRect(X1, Y1, X2, Y2, Value) // calls Changed...
  4793. else
  4794. if A <> 0 then
  4795. begin
  4796. ChangedRect := MakeRect(X1, Y1, X2 + 1, Y2 + 1);
  4797. if not FMeasuringMode then
  4798. begin
  4799. Dec(Y2);
  4800. Dec(X2);
  4801. for j := Y1 to Y2 do
  4802. begin
  4803. P := GetPixelPtr(X1, j);
  4804. if CombineMode = cmBlend then
  4805. begin
  4806. for i := X1 to X2 do
  4807. begin
  4808. CombineMem(Value, P^, A);
  4809. Inc(P);
  4810. end;
  4811. end else
  4812. begin
  4813. for i := X1 to X2 do
  4814. begin
  4815. MergeMem(Value, P^);
  4816. Inc(P);
  4817. end;
  4818. end;
  4819. end;
  4820. EMMS;
  4821. end;
  4822. Changed(ChangedRect);
  4823. end;
  4824. end;
  4825. procedure TCustomBitmap32.FillRectTS(X1, Y1, X2, Y2: Integer; Value: TColor32);
  4826. begin
  4827. if (X2 > X1) and (Y2 > Y1) and
  4828. (X1 < FClipRect.Right) and (Y1 < FClipRect.Bottom) and
  4829. (X2 > FClipRect.Left) and (Y2 > FClipRect.Top) then
  4830. begin
  4831. if X1 < FClipRect.Left then
  4832. X1 := FClipRect.Left;
  4833. if Y1 < FClipRect.Top then
  4834. Y1 := FClipRect.Top;
  4835. if X2 > FClipRect.Right then
  4836. X2 := FClipRect.Right;
  4837. if Y2 > FClipRect.Bottom then
  4838. Y2 := FClipRect.Bottom;
  4839. FillRectT(X1, Y1, X2, Y2, Value); // Calls Changed()
  4840. end;
  4841. end;
  4842. procedure TCustomBitmap32.FillRectS(const ARect: TRect; Value: TColor32);
  4843. begin
  4844. FillRectS(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom, Value);
  4845. end;
  4846. procedure TCustomBitmap32.FillRectTS(const ARect: TRect; Value: TColor32);
  4847. begin
  4848. FillRectTS(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom, Value);
  4849. end;
  4850. procedure TCustomBitmap32.FrameRectS(X1, Y1, X2, Y2: Integer; Value: TColor32);
  4851. begin
  4852. // measuring is handled in inner drawing operations...
  4853. if (X2 > X1) and (Y2 > Y1) and
  4854. (X1 < FClipRect.Right) and (Y1 < FClipRect.Bottom) and
  4855. (X2 > FClipRect.Left) and (Y2 > FClipRect.Top) then
  4856. begin
  4857. Dec(Y2);
  4858. Dec(X2);
  4859. HorzLineS(X1, Y1, X2, Value);
  4860. if Y2 > Y1 then
  4861. HorzLineS(X1, Y2, X2, Value);
  4862. if Y2 > Y1 + 1 then
  4863. begin
  4864. VertLineS(X1, Y1 + 1, Y2 - 1, Value);
  4865. if X2 > X1 then
  4866. VertLineS(X2, Y1 + 1, Y2 - 1, Value);
  4867. end;
  4868. end;
  4869. end;
  4870. procedure TCustomBitmap32.FrameRectTS(X1, Y1, X2, Y2: Integer; Value: TColor32);
  4871. begin
  4872. // measuring is handled in inner drawing operations...
  4873. if (X2 > X1) and (Y2 > Y1) and
  4874. (X1 < FClipRect.Right) and (Y1 < FClipRect.Bottom) and
  4875. (X2 > FClipRect.Left) and (Y2 > FClipRect.Top) then
  4876. begin
  4877. Dec(Y2);
  4878. Dec(X2);
  4879. HorzLineTS(X1, Y1, X2, Value);
  4880. if Y2 > Y1 then
  4881. HorzLineTS(X1, Y2, X2, Value);
  4882. if Y2 > Y1 + 1 then
  4883. begin
  4884. VertLineTS(X1, Y1 + 1, Y2 - 1, Value);
  4885. if X2 > X1 then
  4886. VertLineTS(X2, Y1 + 1, Y2 - 1, Value);
  4887. end;
  4888. end;
  4889. end;
  4890. procedure TCustomBitmap32.FrameRectTSP(X1, Y1, X2, Y2: Integer);
  4891. begin
  4892. // measuring is handled in inner drawing operations...
  4893. if (X2 > X1) and (Y2 > Y1) and
  4894. (X1 < Width) and (Y1 < Height) and // don't check against ClipRect here
  4895. (X2 > 0) and (Y2 > 0) then // due to StippleCounter
  4896. begin
  4897. Dec(X2);
  4898. Dec(Y2);
  4899. if X1 = X2 then
  4900. begin
  4901. if Y1 = Y2 then
  4902. begin
  4903. SetPixelT(X1, Y1, GetStippleColor);
  4904. Changed(MakeRect(X1, Y1, X1 + 1, Y1 + 1));
  4905. end else
  4906. VertLineTSP(X1, Y1, Y2)
  4907. end else
  4908. begin
  4909. if Y1 = Y2 then
  4910. HorzLineTSP(X1, Y1, X2)
  4911. else
  4912. begin
  4913. HorzLineTSP(X1, Y1, X2 - 1);
  4914. VertLineTSP(X2, Y1, Y2 - 1);
  4915. HorzLineTSP(X2, Y2, X1 + 1);
  4916. VertLineTSP(X1, Y2, Y1 + 1);
  4917. end;
  4918. end;
  4919. end;
  4920. end;
  4921. procedure TCustomBitmap32.FrameRectS(const ARect: TRect; Value: TColor32);
  4922. begin
  4923. FrameRectS(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom, Value);
  4924. end;
  4925. procedure TCustomBitmap32.FrameRectTS(const ARect: TRect; Value: TColor32);
  4926. begin
  4927. FrameRectTS(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom, Value);
  4928. end;
  4929. procedure TCustomBitmap32.RaiseRectTS(X1, Y1, X2, Y2: Integer; Contrast: Integer);
  4930. var
  4931. C1, C2: TColor32;
  4932. begin
  4933. // measuring is handled in inner drawing operations...
  4934. if (X2 > X1) and (Y2 > Y1) and
  4935. (X1 < FClipRect.Right) and (Y1 < FClipRect.Bottom) and
  4936. (X2 > FClipRect.Left) and (Y2 > FClipRect.Top) then
  4937. begin
  4938. if (Contrast > 0) then
  4939. begin
  4940. C1 := SetAlpha(clWhite32, Clamp(Contrast * 512 div 100));
  4941. C2 := SetAlpha(clBlack32, Clamp(Contrast * $FF div 100));
  4942. end else
  4943. if Contrast < 0 then
  4944. begin
  4945. Contrast := -Contrast;
  4946. C1 := SetAlpha(clBlack32, Clamp(Contrast * $FF div 100));
  4947. C2 := SetAlpha(clWhite32, Clamp(Contrast * 512 div 100));
  4948. end else
  4949. Exit;
  4950. Dec(X2);
  4951. Dec(Y2);
  4952. HorzLineTS(X1, Y1, X2, C1);
  4953. HorzLineTS(X1, Y2, X2, C2);
  4954. Inc(Y1);
  4955. Dec(Y2);
  4956. VertLineTS(X1, Y1, Y2, C1);
  4957. VertLineTS(X2, Y1, Y2, C2);
  4958. end;
  4959. end;
  4960. procedure TCustomBitmap32.RaiseRectTS(const ARect: TRect; Contrast: Integer);
  4961. begin
  4962. RaiseRectTS(ARect.Left, ARect.Top, ARect.Right, ARect.Bottom, Contrast);
  4963. end;
  4964. function TCustomBitmap32.LoadFromBMPStream(Stream: TStream; Size: Int64): boolean;
  4965. var
  4966. BitmapFileHeader: TBitmapFileHeader;
  4967. begin
  4968. Result := False;
  4969. if (Stream.Read(BitmapFileHeader, SizeOf(TBitmapFileHeader)) < SizeOf(TBitmapFileHeader)) then
  4970. exit;
  4971. // Verify file signature
  4972. if (BitmapFileHeader.bfType <> $4D42) then // BM file signature
  4973. exit;
  4974. if (BitmapFileHeader.bfSize <> Size) then
  4975. BitmapFileHeader.bfSize := Size;
  4976. Result := LoadFromDIBStream(Stream, BitmapFileHeader.bfSize - SizeOf(TBitmapFileHeader));
  4977. end;
  4978. function TCustomBitmap32.LoadFromDIBStream(Stream: TStream; Size: Int64): boolean;
  4979. function GetShift(Mask, TargetMask: DWORD): integer;
  4980. begin
  4981. Result := 0;
  4982. if (Mask = 0) then
  4983. exit;
  4984. // Shift the mask all the way to the right, so the mask starts at bit 0
  4985. while (Mask and 1 = 0) do
  4986. begin
  4987. Mask := Mask shr 1;
  4988. Dec(Result);
  4989. end;
  4990. // Shift the target mask all the way to the right, so the mask starts at bit 0
  4991. while (TargetMask and 1 = 0) do
  4992. begin
  4993. TargetMask := TargetMask shr 1;
  4994. Inc(Result);
  4995. end;
  4996. // The result is the number of bits the mask must be shifted in order to align
  4997. // with the target mask.
  4998. // Positive value means shift left, negative means shift right.
  4999. end;
  5000. procedure EnsureAlpha;
  5001. var
  5002. Alpha: PByte;
  5003. i: integer;
  5004. begin
  5005. // Since the alpha channel of this format isn't defined we can either assume
  5006. // that all pixels have Alpha=255 or we can assume that the alpha is
  5007. // specified in the source pixel data.
  5008. // Instead of just chosing one of these and hoping for the best we make a
  5009. // choice based on the actual alpha values: If the bitmap contains alpha
  5010. // values then we leave it as is. If it doesn't contain alpha values then
  5011. // we reset the alpha of the whole bitmap to 255.
  5012. Alpha := @(PColor32Entry(Bits).A);
  5013. for i := 0 to Width * Height - 1 do
  5014. begin
  5015. if (Alpha^ <> 0) then
  5016. exit;
  5017. Inc(Alpha, SizeOf(DWORD));
  5018. end;
  5019. ResetAlpha;
  5020. end;
  5021. type
  5022. TChannelMask = record
  5023. Shift: integer;
  5024. Mask: DWORD;
  5025. Enabled: boolean;
  5026. end;
  5027. var
  5028. BitmapHeader: TBitmapHeader;
  5029. ChunkSize: integer;
  5030. InfoHeaderVersion: TInfoHeaderVersion;
  5031. i, j: integer;
  5032. Row: integer;
  5033. DeltaRow: integer;
  5034. Pixels: PDWord;
  5035. ChannelMasks: array[TColor32Component] of TChannelMask;
  5036. Channel: TColor32Component;
  5037. Value, NewValue: DWORD;
  5038. Padding: integer;
  5039. ScanlineRow: PColor32Array;
  5040. const
  5041. {$IFNDEF RGBA_FORMAT}
  5042. Masks: array[TColor32Component] of DWORD = ($000000FF, $0000FF00, $00FF0000, $FF000000); // BGRA
  5043. ChannelToIndex: array[TColor32Component] of integer = (2, 1, 0, 3); // Maps TColor32Component to RGBA index
  5044. {$ELSE}
  5045. Masks: array[TColor32Component] of DWORD = ($00FF0000, $0000FF00, $000000FF, $FF000000); // RGBA
  5046. ChannelToIndex: array[TColor32Component] of integer = (0, 1, 2, 3); // Maps TColor32Component to RGBA index
  5047. {$ENDIF}
  5048. begin
  5049. Result := False;
  5050. FillChar(BitmapHeader, SizeOf(TBitmapHeader), 0);
  5051. // Read the info header size field
  5052. Dec(Size, SizeOf(BitmapHeader.InfoHeader.biSize));
  5053. if (Size < 0) then
  5054. exit;
  5055. if (Stream.Read(BitmapHeader.InfoHeader.biSize, SizeOf(BitmapHeader.InfoHeader.biSize)) <> SizeOf(BitmapHeader.InfoHeader.biSize)) then
  5056. exit;
  5057. // Validate info header size and determine header version
  5058. case BitmapHeader.InfoHeader.biSize of
  5059. SizeOf(TBitmapCoreHeader): exit;
  5060. // Size of TBitmapCoreHeader2 vary from 16 to 64 but we assume it's 64
  5061. SizeOf(TBitmapCoreHeader2):exit;
  5062. SizeOf(TBitmapInfoHeader): InfoHeaderVersion := InfoHeaderVersion1;
  5063. SizeOf(TBitmapV2Header): InfoHeaderVersion := InfoHeaderVersion2;
  5064. SizeOf(TBitmapV3Header): InfoHeaderVersion := InfoHeaderVersion3;
  5065. SizeOf(TBitmapV4Header): InfoHeaderVersion := InfoHeaderVersion4;
  5066. SizeOf(TBitmapV5Header): InfoHeaderVersion := InfoHeaderVersion5;
  5067. else
  5068. if (BitmapHeader.InfoHeader.biSize < SizeOf(TBitmapInfoHeader)) then
  5069. exit;
  5070. // Guard against bogus header size. Limit is arbitrary.
  5071. if (BitmapHeader.InfoHeader.biSize >= 4096) then
  5072. exit;
  5073. // Assume header is v1 compatible
  5074. InfoHeaderVersion := InfoHeaderVersion1;
  5075. end;
  5076. // Get the rest of the bitmap info header
  5077. ChunkSize := BitmapHeader.InfoHeader.biSize - SizeOf(BitmapHeader.InfoHeader.biSize);
  5078. Dec(Size, ChunkSize);
  5079. if (Size < 0) then
  5080. exit;
  5081. if (Stream.Read(BitmapHeader.InfoHeader.biWidth, ChunkSize) <> ChunkSize) then
  5082. exit;
  5083. // We only support BI_RGB and BI_BITFIELDS compression
  5084. if (BitmapHeader.InfoHeader.biCompression <> BI_RGB) and (BitmapHeader.InfoHeader.biCompression <> BI_BITFIELDS) then
  5085. exit;
  5086. // We only support 24-bit and 32-bit bitmaps
  5087. if not (BitmapHeader.InfoHeader.biBitCount in [24, 32]) then
  5088. exit;
  5089. // Planes must be 1
  5090. if (BitmapHeader.InfoHeader.biPlanes <> 1) then
  5091. BitmapHeader.InfoHeader.biPlanes := 1;
  5092. // Width must be positive
  5093. if (BitmapHeader.InfoHeader.biWidth < 0) then
  5094. BitmapHeader.InfoHeader.biWidth := -BitmapHeader.InfoHeader.biWidth;
  5095. // Validate compression and fetch RGBA masks
  5096. if (BitmapHeader.InfoHeader.biCompression = BI_BITFIELDS) then
  5097. begin
  5098. // Reject invalid 24-bit bitfields
  5099. if BitmapHeader.InfoHeader.biBitCount = 24 then
  5100. exit;
  5101. // For versions > v1 the RGB color mask is part of the header so it has already
  5102. // been read as part of the header. For version = v1 it is stored just after
  5103. // the header, before the color table.
  5104. // Note: It seems that the above is not always true in practice; The three mask values
  5105. // can be present even when they are already part of the header. For example a copy/paste
  5106. // of a V5 DIB can cause this to happen. Apparently this is a bug in Windows. Since
  5107. // Windows seems able to work around it we'll try to do that too; If there's exactly 12
  5108. // bytes too many when we're about to read the pixel data, then we skip 12 bytes ahead.
  5109. if (InfoHeaderVersion = InfoHeaderVersion1) then
  5110. begin
  5111. // Read the RGB mask into the v2 header RGB mask fields
  5112. ChunkSize := 3 * SizeOf(DWORD);
  5113. Dec(Size, ChunkSize);
  5114. if (Size < 0) then
  5115. exit;
  5116. if (Stream.Read(BitmapHeader.V2Header.bV2RedMask, ChunkSize) <> ChunkSize) then
  5117. exit;
  5118. end;
  5119. // Check if RGBA mask is present and values are valid
  5120. // Validate RGB mask; All components must have a value and the values must not overlap.
  5121. if (BitmapHeader.V2Header.bV2RedMask = 0) or (BitmapHeader.V2Header.bV2GreenMask = 0) or (BitmapHeader.V2Header.bV2BlueMask = 0) or
  5122. ((BitmapHeader.V2Header.bV2RedMask and BitmapHeader.V2Header.bV2GreenMask <> 0) or
  5123. (BitmapHeader.V2Header.bV2RedMask and BitmapHeader.V2Header.bV2BlueMask <> 0) or
  5124. (BitmapHeader.V2Header.bV2GreenMask and BitmapHeader.V2Header.bV2BlueMask <> 0)) then
  5125. begin
  5126. // Reset to 888
  5127. BitmapHeader.V2Header.bV2RedMask := Masks[ccRed];
  5128. BitmapHeader.V2Header.bV2GreenMask := Masks[ccGreen];
  5129. BitmapHeader.V2Header.bV2BlueMask := Masks[ccBlue];
  5130. end;
  5131. // Validate A mask. Value is optional but must not overlap RGB mask.
  5132. if (InfoHeaderVersion >= InfoHeaderVersion3) then
  5133. begin
  5134. if (BitmapHeader.V3Header.bV3AlphaMask and (BitmapHeader.V3Header.bV3RedMask or BitmapHeader.V3Header.bV3GreenMask or BitmapHeader.V3Header.bV3BlueMask) <> 0) then
  5135. // Reset alpha
  5136. BitmapHeader.V3Header.bV3AlphaMask := $FFFFFFFF and
  5137. not(BitmapHeader.V3Header.bV3RedMask or BitmapHeader.V3Header.bV3GreenMask or BitmapHeader.V3Header.bV3BlueMask);
  5138. end;
  5139. // If the color mask corresponds to the BI_RGB layout then we change
  5140. // the compression type to BI_RGB so we can read the pixels with
  5141. // the simpler BI_RGB method.
  5142. if (InfoHeaderVersion <= InfoHeaderVersion2) then
  5143. begin
  5144. if (BitmapHeader.V2Header.bV2RedMask = Masks[ccRed]) and
  5145. (BitmapHeader.V2Header.bV2GreenMask = Masks[ccGreen]) and
  5146. (BitmapHeader.V2Header.bV2BlueMask = Masks[ccBlue]) then
  5147. BitmapHeader.InfoHeader.biCompression := BI_RGB;
  5148. end else
  5149. begin
  5150. if (BitmapHeader.V3Header.bV3RedMask = Masks[ccRed]) and
  5151. (BitmapHeader.V3Header.bV3GreenMask = Masks[ccGreen]) and
  5152. (BitmapHeader.V3Header.bV3BlueMask = Masks[ccBlue]) and
  5153. (BitmapHeader.V3Header.bV3AlphaMask = Masks[ccAlpha]) then
  5154. BitmapHeader.InfoHeader.biCompression := BI_RGB;
  5155. end;
  5156. end;
  5157. // Skip color table so we're ready to read the pixel data
  5158. // Note: We ignore the pixel offset stored in the header since this
  5159. // value is often incorrect.
  5160. if (BitmapHeader.InfoHeader.biClrUsed > 0) then
  5161. begin
  5162. Dec(Size, BitmapHeader.InfoHeader.biClrUsed * SizeOf(DWORD));
  5163. if (Size < 0) then
  5164. exit;
  5165. Stream.Seek(BitmapHeader.InfoHeader.biClrUsed * SizeOf(DWORD), soCurrent);
  5166. end;
  5167. // Pad input rows to 32 bits
  5168. Padding := (SizeOf(DWORD) - (((BitmapHeader.InfoHeader.biBitCount shr 3) * BitmapHeader.InfoHeader.biWidth) and (SizeOf(DWORD)-1))) and (SizeOf(DWORD)-1);
  5169. // Make sure there's enough data left for the pixels
  5170. Dec(Size, ((BitmapHeader.InfoHeader.biBitCount shr 3) * BitmapHeader.InfoHeader.biWidth + Padding) * Abs(BitmapHeader.InfoHeader.biHeight));
  5171. if (Size < 0) then
  5172. exit;
  5173. // Work around BMPs with an extra color mask after the header:
  5174. // If there's exactly 12 bytes too many then we assume that these are an extra
  5175. // color mask and skip it.
  5176. if (BitmapHeader.InfoHeader.biCompression = BI_BITFIELDS) and (InfoHeaderVersion >= InfoHeaderVersion1) then
  5177. begin
  5178. ChunkSize := 3 * SizeOf(DWORD);
  5179. if (Size = ChunkSize) then
  5180. Stream.Seek(ChunkSize, soFromCurrent);
  5181. end;
  5182. // Set bitmap size and allocate bit pixel data so we can read into it
  5183. SetSize(BitmapHeader.InfoHeader.biWidth, Abs(BitmapHeader.InfoHeader.biHeight));
  5184. // Check whether the bitmap is saved top-down or bottom-up:
  5185. // - Negavive height: top-down
  5186. // - Positive height: bottom-up
  5187. if (BitmapHeader.InfoHeader.biHeight > 0) then
  5188. begin
  5189. // Bitmap is stored bottom-up
  5190. Row := Height-1;
  5191. DeltaRow := -1;
  5192. end else
  5193. begin
  5194. // Bitmap is stored top-down
  5195. Row := 0;
  5196. DeltaRow := 1;
  5197. end;
  5198. if (BitmapHeader.InfoHeader.biCompression = BI_RGB) then
  5199. begin
  5200. case BitmapHeader.InfoHeader.biBitCount of
  5201. 24:
  5202. // Read one RGB pixel at a time
  5203. for i := 0 to Height - 1 do
  5204. begin
  5205. ScanlineRow := Scanline[Row];
  5206. for j := 0 to Width - 1 do
  5207. begin
  5208. // Read RGB data and reset alpha
  5209. Stream.ReadBuffer(ScanlineRow[j], 3);
  5210. TColor32Entry(ScanlineRow[j]).A := $FF;
  5211. end;
  5212. if (Padding > 0) then
  5213. Stream.Seek(Padding, soCurrent);
  5214. Inc(Row, DeltaRow);
  5215. end;
  5216. 32:
  5217. begin
  5218. Assert(Padding = 0);
  5219. if (BitmapHeader.InfoHeader.biHeight > 0) then
  5220. begin
  5221. // Bitmap is stored bottom-up: Read one row at a time
  5222. ChunkSize := Width * SizeOf(DWORD);
  5223. for i := Height - 1 downto 0 do
  5224. Stream.ReadBuffer(Scanline[i]^, ChunkSize);
  5225. end else
  5226. // Bitmap is stored top-down: Read all rows in one go
  5227. Stream.ReadBuffer(Bits^, Width * Height * SizeOf(DWORD))
  5228. end;
  5229. end;
  5230. if (InfoHeaderVersion < InfoHeaderVersion3) then
  5231. EnsureAlpha;
  5232. {$IFDEF RGBA_FORMAT}
  5233. // BMP stores pixels in ABGR order but we need them in ARGB order.
  5234. // Swap R and B channels
  5235. for i := 0 to FHeight * FWidth - 1 do
  5236. SwapRedBlueMem(FBits[i]);
  5237. {$ENDIF}
  5238. end else
  5239. begin
  5240. Assert(Padding = 0);
  5241. // Determine how much we need to shift the masked color values in order
  5242. // to get them into the desired position.
  5243. for Channel := Low(TColor32Component) to High(TColor32Component) do
  5244. begin
  5245. ChannelMasks[Channel].Shift := GetShift(BitmapHeader.Header.bmiColors[ChannelToIndex[Channel]], Masks[Channel]);
  5246. ChannelMasks[Channel].Mask := Masks[Channel];
  5247. ChannelMasks[Channel].Enabled := (Channel <> ccAlpha) or (InfoHeaderVersion >= InfoHeaderVersion3);
  5248. end;
  5249. // Read one row at a time into our bitmap and then decode it in place.
  5250. ChunkSize := Width * SizeOf(DWORD);
  5251. for i := 0 to Height-1 do
  5252. begin
  5253. Pixels := PDWord(Scanline[Row]);
  5254. Stream.ReadBuffer(Pixels^, ChunkSize);
  5255. for j := 0 to Width-1 do
  5256. begin
  5257. Value := Pixels^;
  5258. NewValue := 0;
  5259. for Channel := Low(TColor32Component) to High(TColor32Component) do
  5260. begin
  5261. if (ChannelMasks[Channel].Enabled) then
  5262. begin
  5263. if (ChannelMasks[Channel].Shift > 0) then
  5264. NewValue := NewValue or ((Value shl ChannelMasks[Channel].Shift) and ChannelMasks[Channel].Mask)
  5265. else
  5266. if (ChannelMasks[Channel].Shift < 0) then
  5267. NewValue := NewValue or ((Value shr -ChannelMasks[Channel].Shift) and ChannelMasks[Channel].Mask)
  5268. else
  5269. NewValue := NewValue or (Value and ChannelMasks[Channel].Mask);
  5270. end else
  5271. NewValue := NewValue or ChannelMasks[Channel].Mask; // Set alpha=255
  5272. end;
  5273. Pixels^ := NewValue;
  5274. Inc(Pixels);
  5275. end;
  5276. Inc(Row, DeltaRow);
  5277. end;
  5278. {$IFDEF RGBA_FORMAT}
  5279. // For BI_BITFIELDS we do not need to swap the R and B channels at the end because
  5280. // we have already done it by adjusting the Masks and ChannelToIndex arrays.
  5281. {$ENDIF}
  5282. end;
  5283. Result := True;
  5284. end;
  5285. procedure TCustomBitmap32.LoadFromStream(Stream: TStream);
  5286. var
  5287. SavePos: Int64;
  5288. {$ifdef LOADFROMSTREAM}
  5289. P: TPicture;
  5290. {$else LOADFROMSTREAM}
  5291. B: TBitmap;
  5292. {$endif LOADFROMSTREAM}
  5293. begin
  5294. SavePos := Stream.Position;
  5295. if (not LoadFromBMPStream(Stream, Stream.Size)) then
  5296. begin
  5297. Stream.Position := SavePos;
  5298. {$ifdef LOADFROMSTREAM}
  5299. // TPicture.LoadFromStream requires TGraphic.CanLoadFromStream.
  5300. // Introduced in Delphi 10.2 and present in FPC as well
  5301. // See issue #145
  5302. P := TPicture.Create;
  5303. try
  5304. P.LoadFromStream(Stream);
  5305. Assign(P);
  5306. finally
  5307. P.Free;
  5308. end;
  5309. {$else LOADFROMSTREAM}
  5310. // Fallback to TBitmap for Delphi 10.1 and older
  5311. B := TBitmap.Create;
  5312. try
  5313. B.LoadFromStream(Stream);
  5314. Assign(B);
  5315. finally
  5316. B.Free;
  5317. end;
  5318. {$endif LOADFROMSTREAM}
  5319. end;
  5320. Changed;
  5321. end;
  5322. procedure TCustomBitmap32.SaveToStream(Stream: TStream; SaveTopDown: Boolean);
  5323. begin
  5324. SaveToStream(Stream, SaveTopDown, DefaultBitmapHeaderVersion);
  5325. end;
  5326. procedure TCustomBitmap32.SaveToStream(Stream: TStream; SaveTopDown: Boolean; InfoHeaderVersion: TInfoHeaderVersion);
  5327. var
  5328. FileHeader: TBitmapFileHeader;
  5329. BitmapSize: Integer;
  5330. HeaderSize: integer;
  5331. begin
  5332. BitmapSize := Width * Height * SizeOf(DWORD);
  5333. case InfoHeaderVersion of
  5334. InfoHeaderVersion1: HeaderSize := SizeOf(TBitmapInfoHeader);// 40
  5335. InfoHeaderVersion2: HeaderSize := SizeOf(TBitmapV2Header); // 52
  5336. InfoHeaderVersion3: HeaderSize := SizeOf(TBitmapV3Header); // 56
  5337. InfoHeaderVersion4: HeaderSize := SizeOf(TBitmapV4Header); // 108
  5338. InfoHeaderVersion5: HeaderSize := SizeOf(TBitmapV5Header); // 124
  5339. else
  5340. raise Exception.Create('Invalid header version');
  5341. end;
  5342. FileHeader.bfType := $4D42; // Magic bytes for Windows Bitmap
  5343. FileHeader.bfSize := BitmapSize + SizeOf(TBitmapFileHeader) + HeaderSize; // Size of file
  5344. FileHeader.bfReserved1 := 0;
  5345. FileHeader.bfReserved2 := 0;
  5346. // The offset, in bytes, from the beginning of the BITMAPFILEHEADER structure to the bitmap bits.
  5347. FileHeader.bfOffBits := SizeOf(TBitmapFileHeader) + HeaderSize;
  5348. // Note that the above offsets and sizes doesn't include a potential BI_BITFIELDS
  5349. // color mask. This doesn't appear to be a problem since nobody (wisely) uses the
  5350. // values in these fields anyway.
  5351. Stream.WriteBuffer(FileHeader, SizeOf(FileHeader));
  5352. SaveToDIBStream(Stream, SaveTopDown, InfoHeaderVersion);
  5353. end;
  5354. procedure TCustomBitmap32.SaveToDIBStream(Stream: TStream; SaveTopDown: Boolean);
  5355. begin
  5356. SaveToDIBStream(Stream, SaveTopDown, DefaultBitmapHeaderVersion);
  5357. end;
  5358. procedure TCustomBitmap32.SaveToDIBStream(Stream: TStream; SaveTopDown: Boolean; InfoHeaderVersion: TInfoHeaderVersion);
  5359. type
  5360. TDIBHeader = packed record
  5361. case TInfoHeaderVersion of
  5362. InfoHeaderVersion1: (InfoHeader: TBitmapInfoHeader); // 40
  5363. InfoHeaderVersion2: (V2Header: TBitmapV2Header); // 52
  5364. InfoHeaderVersion3: (V3Header: TBitmapV3Header); // 56
  5365. InfoHeaderVersion4: (V4Header: TBitmapV4Header); // 108
  5366. InfoHeaderVersion5: (V5Header: TBitmapV5Header); // 124
  5367. end;
  5368. {$IFDEF FPC}
  5369. const
  5370. LCS_GM_IMAGES = 4;
  5371. {$ENDIF}
  5372. var
  5373. HeaderSize: Cardinal;
  5374. Header: TDIBHeader;
  5375. i: Integer;
  5376. W: Integer;
  5377. begin
  5378. // Determine info header size based on header version.
  5379. // Note: Formats lower than InfoHeaderVersion3 doesn't formally
  5380. // support alpha but that doesn't mean that we can't store the alpha
  5381. // anyway.
  5382. case InfoHeaderVersion of
  5383. InfoHeaderVersion1: HeaderSize := SizeOf(TBitmapInfoHeader);// 40
  5384. InfoHeaderVersion2: HeaderSize := SizeOf(TBitmapV2Header); // 52
  5385. InfoHeaderVersion3: HeaderSize := SizeOf(TBitmapV3Header); // 56
  5386. InfoHeaderVersion4: HeaderSize := SizeOf(TBitmapV4Header); // 108
  5387. InfoHeaderVersion5: HeaderSize := SizeOf(TBitmapV5Header); // 124
  5388. else
  5389. raise Exception.Create('Invalid header version');
  5390. end;
  5391. Header := Default(TDIBHeader);
  5392. Header.InfoHeader.biSize := HeaderSize;
  5393. Header.InfoHeader.biWidth := Width;
  5394. if SaveTopDown then
  5395. Header.InfoHeader.biHeight := -Height
  5396. else
  5397. Header.InfoHeader.biHeight := Height;
  5398. Header.InfoHeader.biPlanes := 1;
  5399. Header.InfoHeader.biBitCount := 32; // This implementation only support writing 32-bit format
  5400. Header.InfoHeader.biCompression := BI_RGB; // BI_RGB or BI_BITFIELDS
  5401. Header.InfoHeader.biSizeImage := Width * Height * SizeOf(DWORD);
  5402. Header.InfoHeader.biXPelsPerMeter := 0;
  5403. Header.InfoHeader.biYPelsPerMeter := 0;
  5404. Header.InfoHeader.biClrUsed := 0; // No palette
  5405. Header.InfoHeader.biClrImportant := 0;
  5406. if (InfoHeaderVersion >= InfoHeaderVersion3) then
  5407. begin
  5408. // First header version to formally support Alpha. But this requires that we use
  5409. // BI_BITFIELDS compression.
  5410. Header.InfoHeader.biCompression := BI_BITFIELDS; // Switch from BI_RGB
  5411. Header.V3Header.bV3AlphaMask := $FF000000;
  5412. end;
  5413. if (InfoHeaderVersion < InfoHeaderVersion2) and (Header.InfoHeader.biCompression = BI_BITFIELDS) then
  5414. // The Version 1 header doesn't include the mask so expand
  5415. // the header size so the mask will be written.
  5416. Inc(HeaderSize, 12);
  5417. if (Header.InfoHeader.biCompression = BI_BITFIELDS) then
  5418. begin
  5419. // We only support the bit masks that correspond directly to the ABGR format.
  5420. // Also note that WIC, among others, ignores the alpha if we use a different mask.
  5421. Header.V2Header.bV2RedMask := $00FF0000;
  5422. Header.V2Header.bV2GreenMask := $0000FF00;
  5423. Header.V2Header.bV2BlueMask := $000000FF;
  5424. end;
  5425. if (InfoHeaderVersion >= InfoHeaderVersion4) then
  5426. Header.V4Header.bV4CSType := $73524742;// LCS_sRGB
  5427. if (InfoHeaderVersion >= InfoHeaderVersion5) then
  5428. Header.V5Header.bV5Intent := LCS_GM_IMAGES;
  5429. Stream.WriteBuffer(Header, HeaderSize);
  5430. // Pixel array
  5431. {$IFNDEF RGBA_FORMAT}
  5432. if SaveTopDown then
  5433. begin
  5434. // NOTE: We can save the whole buffer in one run because
  5435. // we do not support scanline strides (yet).
  5436. Stream.WriteBuffer(Bits^, Header.InfoHeader.biSizeImage);
  5437. end
  5438. else
  5439. begin
  5440. W := Width * SizeOf(DWORD);
  5441. for i := Height - 1 downto 0 do
  5442. Stream.WriteBuffer(ScanLine[i]^, W);
  5443. end;
  5444. {$ELSE RGBA_FORMAT}
  5445. // Bits are in RGBA format but we need to write BGRA
  5446. if SaveTopDown then
  5447. begin
  5448. for i := 0 to Width*Height-1 do
  5449. Stream.Write(SwapRedBlue(Bits[i]), SizeOf(TColor32));
  5450. end
  5451. else
  5452. begin
  5453. for i := Height - 1 downto 0 do
  5454. for W := 0 to Width-1 do
  5455. Stream.Write(SwapRedBlue(ScanLine[i][W]), SizeOf(TColor32));
  5456. end;
  5457. {$ENDIF RGBA_FORMAT}
  5458. end;
  5459. procedure TCustomBitmap32.LoadFromFile(const FileName: string);
  5460. var
  5461. FileStream: TFileStream;
  5462. {$ifndef LOADFROMSTREAM}
  5463. P: TPicture;
  5464. {$endif LOADFROMSTREAM}
  5465. begin
  5466. {$if (not defined(FPC)) and (CompilerVersion >= 31.0)} // TBufferedFileStream was introduced in Delphi 10.1
  5467. FileStream := TBufferedFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  5468. {$else}
  5469. FileStream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  5470. {$ifend}
  5471. try
  5472. {$ifdef LOADFROMSTREAM}
  5473. LoadFromStream(FileStream);
  5474. {$else LOADFROMSTREAM}
  5475. if (LoadFromBMPStream(FileStream, FileStream.Size)) then
  5476. begin
  5477. Changed;
  5478. exit;
  5479. end;
  5480. {$endif LOADFROMSTREAM}
  5481. finally
  5482. FileStream.Free;
  5483. end;
  5484. {$ifndef LOADFROMSTREAM}
  5485. // Fallback to determing file format based on file type for Delphi 10.1. and older
  5486. // See issue #145
  5487. P := TPicture.Create;
  5488. try
  5489. P.LoadFromFile(FileName);
  5490. Assign(P);
  5491. finally
  5492. P.Free;
  5493. end;
  5494. {$endif LOADFROMSTREAM}
  5495. end;
  5496. procedure TCustomBitmap32.SaveToFile(const FileName: string; SaveTopDown: Boolean);
  5497. begin
  5498. SaveToFile(FileName, SaveTopDown, DefaultBitmapHeaderVersion);
  5499. end;
  5500. procedure TCustomBitmap32.SaveToFile(const FileName: string; SaveTopDown: Boolean; InfoHeaderVersion: TInfoHeaderVersion);
  5501. var
  5502. FileStream: TFileStream;
  5503. begin
  5504. FileStream := TFileStream.Create(Filename, fmCreate);
  5505. try
  5506. SaveToStream(FileStream, SaveTopDown, InfoHeaderVersion);
  5507. finally
  5508. FileStream.Free;
  5509. end;
  5510. end;
  5511. procedure TCustomBitmap32.LoadFromResourceID(Instance: THandle; ResID: Integer);
  5512. var
  5513. Stream: TStream;
  5514. B: TBitmap;
  5515. begin
  5516. Stream := TResourceStream.CreateFromID(Instance, ResID, RT_BITMAP);
  5517. try
  5518. if (LoadFromDIBStream(Stream, Stream.Size)) then
  5519. begin
  5520. Changed;
  5521. exit;
  5522. end;
  5523. finally
  5524. Stream.Free;
  5525. end;
  5526. // Fall back to TBitmap
  5527. B := TBitmap.Create;
  5528. try
  5529. B.LoadFromResourceID(Instance, ResID);
  5530. Assign(B);
  5531. finally
  5532. B.Free;
  5533. end;
  5534. Changed;
  5535. end;
  5536. procedure TCustomBitmap32.LoadFromResourceName(Instance: THandle; const ResName: string);
  5537. var
  5538. Stream: TStream;
  5539. B: TBitmap;
  5540. begin
  5541. Stream := TResourceStream.Create(Instance, ResName, RT_BITMAP);
  5542. try
  5543. if (LoadFromDIBStream(Stream, Stream.Size)) then
  5544. begin
  5545. Changed;
  5546. exit;
  5547. end;
  5548. finally
  5549. Stream.Free;
  5550. end;
  5551. // Fall back to TBitmap
  5552. B := TBitmap.Create;
  5553. try
  5554. B.LoadFromResourceName(Instance, ResName);
  5555. Assign(B);
  5556. finally
  5557. B.Free;
  5558. end;
  5559. Changed;
  5560. end;
  5561. function TCustomBitmap32.Equal(B: TCustomBitmap32): Boolean;
  5562. var
  5563. S1, S2: TMemoryStream;
  5564. begin
  5565. Result := (B <> nil) and (ClassType = B.ClassType);
  5566. if Empty or B.Empty then
  5567. begin
  5568. Result := Empty and B.Empty;
  5569. Exit;
  5570. end;
  5571. if Result then
  5572. begin
  5573. S1 := TMemoryStream.Create;
  5574. try
  5575. SaveToStream(S1);
  5576. S2 := TMemoryStream.Create;
  5577. try
  5578. B.SaveToStream(S2);
  5579. Result := (S1.Size = S2.Size) and CompareMem(S1.Memory, S2.Memory, S1.Size);
  5580. finally
  5581. S2.Free;
  5582. end;
  5583. finally
  5584. S1.Free;
  5585. end;
  5586. end;
  5587. end;
  5588. procedure TCustomBitmap32.DefineProperties(Filer: TFiler);
  5589. function DoWrite: Boolean;
  5590. begin
  5591. if Filer.Ancestor <> nil then
  5592. Result := not (Filer.Ancestor is TCustomBitmap32) or
  5593. not Equal(TCustomBitmap32(Filer.Ancestor))
  5594. else
  5595. Result := not Empty;
  5596. end;
  5597. begin
  5598. Filer.DefineBinaryProperty('Data', ReadData, WriteData, DoWrite);
  5599. end;
  5600. procedure TCustomBitmap32.ReadData(Stream: TStream);
  5601. var
  5602. Width, Height: Integer;
  5603. begin
  5604. try
  5605. Stream.ReadBuffer(Width, 4);
  5606. Stream.ReadBuffer(Height, 4);
  5607. SetSize(Width, Height);
  5608. Stream.ReadBuffer(Bits[0], FWidth * FHeight * 4);
  5609. finally
  5610. Changed;
  5611. end;
  5612. end;
  5613. procedure TCustomBitmap32.WriteData(Stream: TStream);
  5614. begin
  5615. Stream.WriteBuffer(FWidth, 4);
  5616. Stream.WriteBuffer(FHeight, 4);
  5617. Stream.WriteBuffer(Bits[0], FWidth * FHeight * 4);
  5618. end;
  5619. procedure TCustomBitmap32.SetCombineMode(const Value: TCombineMode);
  5620. begin
  5621. if FCombineMode <> Value then
  5622. begin
  5623. FCombineMode := Value;
  5624. BlendProc := @BLEND_MEM[FCombineMode]^;
  5625. Changed;
  5626. end;
  5627. end;
  5628. procedure TCustomBitmap32.SetDrawMode(Value: TDrawMode);
  5629. begin
  5630. if FDrawMode <> Value then
  5631. begin
  5632. FDrawMode := Value;
  5633. Changed;
  5634. end;
  5635. end;
  5636. procedure TCustomBitmap32.SetWrapMode(Value: TWrapMode);
  5637. begin
  5638. if FWrapMode <> Value then
  5639. begin
  5640. FWrapMode := Value;
  5641. WrapProcHorz := GetWrapProcEx(WrapMode, FClipRect.Left, FClipRect.Right - 1);
  5642. WrapProcVert := GetWrapProcEx(WrapMode, FClipRect.Top, FClipRect.Bottom - 1);
  5643. Changed;
  5644. end;
  5645. end;
  5646. procedure TCustomBitmap32.SetMasterAlpha(Value: Cardinal);
  5647. begin
  5648. if FMasterAlpha <> Value then
  5649. begin
  5650. FMasterAlpha := Value;
  5651. Changed;
  5652. end;
  5653. end;
  5654. {$IFDEF DEPRECATEDMODE}
  5655. procedure TCustomBitmap32.SetStretchFilter(Value: TStretchFilter);
  5656. begin
  5657. if FStretchFilter <> Value then
  5658. begin
  5659. FStretchFilter := Value;
  5660. case FStretchFilter of
  5661. sfNearest: TNearestResampler.Create(Self);
  5662. sfDraft: TDraftResampler.Create(Self);
  5663. sfLinear: TLinearResampler.Create(Self);
  5664. else
  5665. TKernelResampler.Create(Self);
  5666. with FResampler as TKernelResampler do
  5667. case FStretchFilter of
  5668. sfCosine: Kernel := TCosineKernel.Create;
  5669. sfSpline: Kernel := TSplineKernel.Create;
  5670. sfLanczos: Kernel := TLanczosKernel.Create;
  5671. sfMitchell: Kernel := TMitchellKernel.Create;
  5672. end;
  5673. end;
  5674. Changed;
  5675. end;
  5676. end;
  5677. {$ENDIF}
  5678. procedure TCustomBitmap32.Roll(Dx, Dy: Integer; FillBack: Boolean; FillColor: TColor32);
  5679. var
  5680. Shift, L: Integer;
  5681. R: TRect;
  5682. begin
  5683. if Empty or ((Dx = 0) and (Dy = 0)) then
  5684. Exit;
  5685. if not FMeasuringMode then
  5686. begin
  5687. if (Abs(Dx) >= Width) or (Abs(Dy) >= Height) then
  5688. begin
  5689. if FillBack then
  5690. Clear(FillColor);
  5691. Exit;
  5692. end;
  5693. Shift := Dx + Dy * Width;
  5694. L := (Width * Height - Abs(Shift));
  5695. if Shift > 0 then
  5696. Move(Bits[0], Bits[Shift], L shl 2)
  5697. else
  5698. MoveLongword(Bits[-Shift], Bits[0], L);
  5699. if FillBack then
  5700. begin
  5701. R := MakeRect(0, 0, Width, Height);
  5702. OffsetRect(R, Dx, Dy);
  5703. IntersectRect(R, R, MakeRect(0, 0, Width, Height));
  5704. if R.Top > 0 then
  5705. FillRect(0, 0, Width, R.Top, FillColor)
  5706. else
  5707. if R.Top = 0 then
  5708. FillRect(0, R.Bottom, Width, Height, FillColor);
  5709. if R.Left > 0 then
  5710. FillRect(0, R.Top, R.Left, R.Bottom, FillColor)
  5711. else
  5712. if R.Left = 0 then
  5713. FillRect(R.Right, R.Top, Width, R.Bottom, FillColor);
  5714. end;
  5715. end;
  5716. Changed;
  5717. end;
  5718. procedure TCustomBitmap32.FlipHorz(Dst: TCustomBitmap32);
  5719. var
  5720. i, j: Integer;
  5721. P1, P2: PColor32;
  5722. tmp: TColor32;
  5723. W, W2: Integer;
  5724. begin
  5725. W := Width;
  5726. if (Dst = nil) or (Dst = Self) then
  5727. begin
  5728. if not FMeasuringMode then
  5729. begin
  5730. { In-place flipping }
  5731. P1 := PColor32(Bits);
  5732. P2 := P1;
  5733. Inc(P2, Width - 1);
  5734. W2 := Width shr 1;
  5735. for J := 0 to Height - 1 do
  5736. begin
  5737. for I := 0 to W2 - 1 do
  5738. begin
  5739. tmp := P1^;
  5740. P1^ := P2^;
  5741. P2^ := tmp;
  5742. Inc(P1);
  5743. Dec(P2);
  5744. end;
  5745. Inc(P1, W - W2);
  5746. Inc(P2, W + W2);
  5747. end;
  5748. end;
  5749. Changed;
  5750. end
  5751. else
  5752. begin
  5753. { Flip to Dst }
  5754. if not FMeasuringMode then
  5755. begin
  5756. Dst.BeginUpdate;
  5757. Dst.SetSize(W, Height);
  5758. P1 := PColor32(Bits);
  5759. P2 := PColor32(Dst.Bits);
  5760. Inc(P2, W - 1);
  5761. for J := 0 to Height - 1 do
  5762. begin
  5763. for I := 0 to W - 1 do
  5764. begin
  5765. P2^ := P1^;
  5766. Inc(P1);
  5767. Dec(P2);
  5768. end;
  5769. Inc(P2, W shl 1);
  5770. end;
  5771. Dst.EndUpdate;
  5772. end;
  5773. Dst.Changed;
  5774. end;
  5775. end;
  5776. procedure TCustomBitmap32.FlipVert(Dst: TCustomBitmap32);
  5777. var
  5778. J, J2: Integer;
  5779. Buffer: PColor32Array;
  5780. P1, P2: PColor32;
  5781. begin
  5782. // TODO : MeasuringMode
  5783. if (Dst = nil) or (Dst = Self) then
  5784. begin
  5785. { in-place }
  5786. if not FMeasuringMode then
  5787. begin
  5788. J2 := Height - 1;
  5789. GetMem(Buffer, Width shl 2);
  5790. for J := 0 to Height div 2 - 1 do
  5791. begin
  5792. P1 := PColor32(ScanLine[J]);
  5793. P2 := PColor32(ScanLine[J2]);
  5794. MoveLongword(P1^, Buffer^, Width);
  5795. MoveLongword(P2^, P1^, Width);
  5796. MoveLongword(Buffer^, P2^, Width);
  5797. Dec(J2);
  5798. end;
  5799. FreeMem(Buffer);
  5800. end;
  5801. Changed;
  5802. end
  5803. else
  5804. begin
  5805. if not FMeasuringMode then
  5806. begin
  5807. Dst.SetSize(Width, Height);
  5808. J2 := Height - 1;
  5809. for J := 0 to Height - 1 do
  5810. begin
  5811. MoveLongword(ScanLine[J]^, Dst.ScanLine[J2]^, Width);
  5812. Dec(J2);
  5813. end;
  5814. end;
  5815. Dst.Changed;
  5816. end;
  5817. end;
  5818. procedure TCustomBitmap32.Rotate90(Dst: TCustomBitmap32);
  5819. var
  5820. Tmp: TCustomBitmap32;
  5821. X, Y, I, J: Integer;
  5822. begin
  5823. if not FMeasuringMode then
  5824. begin
  5825. if Dst = nil then
  5826. begin
  5827. Tmp := TCustomBitmap32.Create; // TODO : Use TMemoryBackend
  5828. Dst := Tmp;
  5829. end
  5830. else
  5831. begin
  5832. Tmp := nil;
  5833. Dst.BeginUpdate;
  5834. end;
  5835. Dst.SetSize(Height, Width);
  5836. I := 0;
  5837. for Y := 0 to Height - 1 do
  5838. begin
  5839. J := Height - 1 - Y;
  5840. for X := 0 to Width - 1 do
  5841. begin
  5842. Dst.Bits[J] := Bits[I];
  5843. Inc(I);
  5844. Inc(J, Height);
  5845. end;
  5846. end;
  5847. if Tmp <> nil then
  5848. begin
  5849. Tmp.CopyMapTo(Self);
  5850. Tmp.Free;
  5851. end
  5852. else
  5853. begin
  5854. Dst.EndUpdate;
  5855. Dst.Changed;
  5856. end;
  5857. end else
  5858. if Dst = nil then
  5859. Changed
  5860. else
  5861. Dst.Changed;
  5862. end;
  5863. procedure TCustomBitmap32.Rotate180(Dst: TCustomBitmap32);
  5864. var
  5865. I, I2: Integer;
  5866. Tmp: TColor32;
  5867. begin
  5868. if Dst <> nil then
  5869. begin
  5870. if not FMeasuringMode then
  5871. begin
  5872. Dst.SetSize(Width, Height);
  5873. I2 := Width * Height - 1;
  5874. for I := 0 to Width * Height - 1 do
  5875. begin
  5876. Dst.Bits[I2] := Bits[I];
  5877. Dec(I2);
  5878. end;
  5879. end;
  5880. Dst.Changed;
  5881. end
  5882. else
  5883. begin
  5884. if not FMeasuringMode then
  5885. begin
  5886. I2 := Width * Height - 1;
  5887. for I := 0 to Width * Height div 2 - 1 do
  5888. begin
  5889. Tmp := Bits[I2];
  5890. Bits[I2] := Bits[I];
  5891. Bits[I] := Tmp;
  5892. Dec(I2);
  5893. end;
  5894. end;
  5895. Changed;
  5896. end;
  5897. end;
  5898. procedure TCustomBitmap32.Rotate270(Dst: TCustomBitmap32);
  5899. var
  5900. Tmp: TCustomBitmap32;
  5901. X, Y, I, J: Integer;
  5902. begin
  5903. if not FMeasuringMode then
  5904. begin
  5905. if Dst = nil then
  5906. begin
  5907. Tmp := TCustomBitmap32.Create; { TODO : Revise creating of temporary bitmaps here... }
  5908. // TODO : Use TMemoryBackend
  5909. Dst := Tmp;
  5910. end
  5911. else
  5912. begin
  5913. Tmp := nil;
  5914. Dst.BeginUpdate;
  5915. end;
  5916. Dst.SetSize(Height, Width);
  5917. I := 0;
  5918. for Y := 0 to Height - 1 do
  5919. begin
  5920. J := (Width - 1) * Height + Y;
  5921. for X := 0 to Width - 1 do
  5922. begin
  5923. Dst.Bits[J] := Bits[I];
  5924. Inc(I);
  5925. Dec(J, Height);
  5926. end;
  5927. end;
  5928. if Tmp <> nil then
  5929. begin
  5930. Tmp.CopyMapTo(Self);
  5931. Tmp.Free;
  5932. end
  5933. else
  5934. begin
  5935. Dst.EndUpdate;
  5936. Dst.Changed;
  5937. end;
  5938. end else
  5939. if Dst = nil then
  5940. Changed
  5941. else
  5942. Dst.Changed;
  5943. end;
  5944. function TCustomBitmap32.BoundsRect: TRect;
  5945. begin
  5946. Result.Left := 0;
  5947. Result.Top := 0;
  5948. Result.Right := Width;
  5949. Result.Bottom := Height;
  5950. end;
  5951. procedure TCustomBitmap32.SetClipRect(const Value: TRect);
  5952. begin
  5953. IntersectRect(FClipRect, Value, BoundsRect);
  5954. FFixedClipRect := FixedRect(FClipRect);
  5955. with FClipRect do
  5956. F256ClipRect := Rect(Left shl 8, Top shl 8, Right shl 8, Bottom shl 8);
  5957. FClipping := not EqualRect(FClipRect, BoundsRect);
  5958. WrapProcHorz := GetWrapProcEx(WrapMode, FClipRect.Left, FClipRect.Right - 1);
  5959. WrapProcVert := GetWrapProcEx(WrapMode, FClipRect.Top, FClipRect.Bottom - 1);
  5960. end;
  5961. procedure TCustomBitmap32.ResetClipRect;
  5962. begin
  5963. ClipRect := BoundsRect;
  5964. end;
  5965. procedure TCustomBitmap32.BeginMeasuring(const Callback: TAreaChangedEvent);
  5966. begin
  5967. FMeasuringMode := True;
  5968. FOldOnAreaChanged := FOnAreaChanged;
  5969. FOnAreaChanged := Callback;
  5970. end;
  5971. procedure TCustomBitmap32.EndMeasuring;
  5972. begin
  5973. FMeasuringMode := False;
  5974. FOnAreaChanged := FOldOnAreaChanged;
  5975. end;
  5976. procedure TCustomBitmap32.PropertyChanged;
  5977. begin
  5978. // don't force invalidation of whole bitmap area as this is unnecessary
  5979. inherited Changed;
  5980. end;
  5981. procedure TCustomBitmap32.Changed;
  5982. begin
  5983. if ((FUpdateCount = 0) or FMeasuringMode) and Assigned(FOnAreaChanged) then
  5984. FOnAreaChanged(Self, BoundsRect, AREAINFO_RECT);
  5985. if not FMeasuringMode then
  5986. inherited;
  5987. end;
  5988. procedure TCustomBitmap32.Changed(const Area: TRect; const Info: Cardinal);
  5989. begin
  5990. if ((FUpdateCount = 0) or FMeasuringMode) and Assigned(FOnAreaChanged) then
  5991. FOnAreaChanged(Self, Area, Info);
  5992. if not FMeasuringMode then
  5993. inherited Changed;
  5994. end;
  5995. procedure TCustomBitmap32.SetResampler(Resampler: TCustomResampler);
  5996. begin
  5997. if Assigned(Resampler) and (FResampler <> Resampler) then
  5998. begin
  5999. if Assigned(FResampler) then
  6000. FResampler.Free;
  6001. FResampler := Resampler;
  6002. Changed;
  6003. end;
  6004. end;
  6005. function TCustomBitmap32.GetResamplerClassName: string;
  6006. begin
  6007. Result := FResampler.ClassName;
  6008. end;
  6009. procedure TCustomBitmap32.SetResamplerClassName(const Value: string);
  6010. var
  6011. ResamplerClass: TCustomResamplerClass;
  6012. begin
  6013. if (Value <> '') and (FResampler.ClassName <> Value) and Assigned(ResamplerList) then
  6014. begin
  6015. ResamplerClass := TCustomResamplerClass(ResamplerList.Find(Value));
  6016. if Assigned(ResamplerClass) then
  6017. ResamplerClass.Create(Self);
  6018. end;
  6019. end;
  6020. { TBitmap32 }
  6021. procedure TBitmap32.FinalizeBackend;
  6022. var
  6023. FontSupport: IFontSupport;
  6024. CanvasSupport: ICanvasSupport;
  6025. begin
  6026. if Supports(Backend, IFontSupport, FontSupport) then
  6027. begin
  6028. FontSupport.OnFontChange := nil;
  6029. FontSupport := nil;
  6030. end;
  6031. if Supports(Backend, ICanvasSupport, CanvasSupport) then
  6032. begin
  6033. CanvasSupport.OnCanvasChange := nil;
  6034. CanvasSupport := nil;
  6035. end;
  6036. inherited;
  6037. end;
  6038. procedure TBitmap32.BackendChangingHandler(Sender: TObject);
  6039. begin
  6040. inherited;
  6041. FontChanged(Self);
  6042. DeleteCanvas;
  6043. end;
  6044. procedure TBitmap32.BackendChangedHandler(Sender: TObject);
  6045. begin
  6046. inherited;
  6047. HandleChanged;
  6048. end;
  6049. procedure TBitmap32.FontChanged(Sender: TObject);
  6050. begin
  6051. // TODO: still required?
  6052. end;
  6053. procedure TBitmap32.CanvasChanged(Sender: TObject);
  6054. begin
  6055. Changed;
  6056. end;
  6057. procedure TBitmap32.CopyPropertiesTo(Dst: TCustomBitmap32);
  6058. var
  6059. FontSupport, DstFontSupport: IFontSupport;
  6060. begin
  6061. inherited;
  6062. if (Dst is TBitmap32) and
  6063. Supports(Dst.Backend, IFontSupport, DstFontSupport) and Supports(Self.Backend, IFontSupport, FontSupport) then
  6064. DstFontSupport.Font.Assign(FontSupport.Font);
  6065. end;
  6066. function TBitmap32.GetCanvas: TCanvas;
  6067. begin
  6068. Result := (FBackend as ICanvasSupport).Canvas;
  6069. end;
  6070. function TBitmap32.GetBitmapInfo: TBitmapInfo;
  6071. begin
  6072. Result := (FBackend as IBitmapContextSupport).BitmapInfo;
  6073. end;
  6074. function TBitmap32.GetHandle: HBITMAP;
  6075. begin
  6076. Result := (FBackend as IBitmapContextSupport).BitmapHandle;
  6077. end;
  6078. function TBitmap32.GetHDC: HDC;
  6079. begin
  6080. Result := (FBackend as IDeviceContextSupport).Handle;
  6081. end;
  6082. class function TBitmap32.GetPlatformBackendClass: TCustomBackendClass;
  6083. begin
  6084. {$IFDEF FPC}
  6085. Result := TLCLBackend;
  6086. {$ELSE}
  6087. Result := TGDIBackend;
  6088. {$ENDIF}
  6089. end;
  6090. function TBitmap32.GetFont: TFont;
  6091. begin
  6092. Result := (FBackend as IFontSupport).Font;
  6093. end;
  6094. procedure TBitmap32.SetBackend(const Backend: TCustomBackend);
  6095. var
  6096. FontSupport: IFontSupport;
  6097. CanvasSupport: ICanvasSupport;
  6098. begin
  6099. if Assigned(Backend) and (Backend <> FBackend) then
  6100. begin
  6101. if Supports(Backend, IFontSupport, FontSupport) then
  6102. FontSupport.OnFontChange := FontChanged;
  6103. if Supports(Backend, ICanvasSupport, CanvasSupport) then
  6104. CanvasSupport.OnCanvasChange := CanvasChanged;
  6105. inherited;
  6106. end;
  6107. end;
  6108. procedure TBitmap32.SetFont(Value: TFont);
  6109. begin
  6110. (FBackend as IFontSupport).Font := Value;
  6111. end;
  6112. procedure TBitmap32.HandleChanged;
  6113. begin
  6114. if Assigned(FOnHandleChanged) then
  6115. FOnHandleChanged(Self);
  6116. end;
  6117. {$IFDEF BCB}
  6118. procedure TBitmap32.Draw(const DstRect, SrcRect: TRect; hSrc: Cardinal);
  6119. {$ELSE}
  6120. procedure TBitmap32.Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
  6121. {$ENDIF}
  6122. begin
  6123. (FBackend as IDeviceContextSupport).Draw(DstRect, SrcRect, hSrc);
  6124. end;
  6125. procedure TBitmap32.DrawTo(hDst: {$IFDEF BCB}Cardinal{$ELSE}HDC{$ENDIF}; DstX, DstY: Integer);
  6126. begin
  6127. if not Empty then
  6128. (FBackend as IDeviceContextSupport).DrawTo(hDst, DstX, DstY);
  6129. end;
  6130. procedure TBitmap32.DrawTo(hDst: {$IFDEF BCB}Cardinal{$ELSE}HDC{$ENDIF}; const DstRect, SrcRect: TRect);
  6131. begin
  6132. if not Empty then
  6133. (FBackend as IDeviceContextSupport).DrawTo(hDst, DstRect, SrcRect);
  6134. end;
  6135. procedure TBitmap32.TileTo(hDst: {$IFDEF BCB}Cardinal{$ELSE}HDC{$ENDIF}; const DstRect, SrcRect: TRect; MaxTileSize: integer);
  6136. var
  6137. DstW, DstH: Integer;
  6138. TilesX, TilesY: Integer;
  6139. Buffer: TCustomBitmap32;
  6140. I, J: Integer;
  6141. ClipRect, R: TRect;
  6142. X, Y: Integer;
  6143. DeviceContextSupport: IDeviceContextSupport;
  6144. begin
  6145. DstW := DstRect.Right - DstRect.Left;
  6146. DstH := DstRect.Bottom - DstRect.Top;
  6147. TilesX := (DstW + MaxTileSize - 1) div MaxTileSize;
  6148. TilesY := (DstH + MaxTileSize - 1) div MaxTileSize;
  6149. Buffer := TBitmap32.Create;
  6150. try
  6151. DeviceContextSupport := Buffer.Backend as IDeviceContextSupport;
  6152. try
  6153. for J := 0 to TilesY - 1 do
  6154. begin
  6155. for I := 0 to TilesX - 1 do
  6156. begin
  6157. ClipRect.Left := I * MaxTileSize;
  6158. ClipRect.Top := J * MaxTileSize;
  6159. ClipRect.Right := (I + 1) * MaxTileSize;
  6160. ClipRect.Bottom := (J + 1) * MaxTileSize;
  6161. if ClipRect.Right > DstW then
  6162. ClipRect.Right := DstW;
  6163. if ClipRect.Bottom > DstH then
  6164. ClipRect.Bottom := DstH;
  6165. X := ClipRect.Left;
  6166. Y := ClipRect.Top;
  6167. OffsetRect(ClipRect, -X, -Y);
  6168. R := DstRect;
  6169. OffsetRect(R, -X - DstRect.Left, -Y - DstRect.Top);
  6170. Buffer.SetSize(ClipRect.Right, ClipRect.Bottom);
  6171. StretchTransfer(Buffer, R, ClipRect, Self, SrcRect, Resampler, DrawMode, FOnPixelCombine);
  6172. DeviceContextSupport.DrawTo(hDst,
  6173. MakeRect(X + DstRect.Left, Y + DstRect.Top, X + DstRect.Left+ClipRect.Right, Y + DstRect.Top+ClipRect.Bottom),
  6174. Buffer.BoundsRect
  6175. );
  6176. end;
  6177. end;
  6178. finally
  6179. DeviceContextSupport := nil;
  6180. end;
  6181. finally
  6182. Buffer.Free;
  6183. end;
  6184. end;
  6185. {$IFDEF COMPILER2009_UP}
  6186. procedure TBitmap32.DrawTo(Dst: TControlCanvas; DstX, DstY: Integer);
  6187. begin
  6188. DrawTo(Dst.Handle, DstX, DstY);
  6189. end;
  6190. procedure TBitmap32.DrawTo(Dst: TControlCanvas; const DstRect, SrcRect: TRect);
  6191. begin
  6192. DrawTo(Dst.Handle, DstRect, SrcRect);
  6193. end;
  6194. procedure TBitmap32.TileTo(Dst: TControlCanvas; const DstRect, SrcRect: TRect; MaxTileSize: integer);
  6195. begin
  6196. TileTo(Dst.Handle, DstRect, SrcRect, MaxTileSize);
  6197. end;
  6198. {$ENDIF}
  6199. procedure TBitmap32.UpdateFont;
  6200. begin
  6201. (FBackend as IFontSupport).UpdateFont;
  6202. end;
  6203. // Text and Fonts //
  6204. function TBitmap32.TextExtent(const Text: string): TSize;
  6205. begin
  6206. Result := (FBackend as ITextSupport).TextExtent(Text);
  6207. end;
  6208. // -------------------------------------------------------------------
  6209. procedure TBitmap32.Textout(X, Y: Integer; const Text: string);
  6210. begin
  6211. (FBackend as ITextSupport).Textout(X, Y, Text);
  6212. end;
  6213. // -------------------------------------------------------------------
  6214. procedure TBitmap32.Textout(X, Y: Integer; const ClipRect: TRect; const Text: string);
  6215. begin
  6216. (FBackend as ITextSupport).Textout(X, Y, ClipRect, Text);
  6217. end;
  6218. // -------------------------------------------------------------------
  6219. procedure TBitmap32.Textout(var DstRect: TRect; const Flags: Cardinal; const Text: string);
  6220. begin
  6221. (FBackend as ITextSupport).Textout(DstRect, Flags, Text);
  6222. end;
  6223. // -------------------------------------------------------------------
  6224. function TBitmap32.TextHeight(const Text: string): Integer;
  6225. begin
  6226. Result := (FBackend as ITextSupport).TextExtent(Text).cY;
  6227. end;
  6228. // -------------------------------------------------------------------
  6229. function TBitmap32.TextWidth(const Text: string): Integer;
  6230. begin
  6231. Result := (FBackend as ITextSupport).TextExtent(Text).cX;
  6232. end;
  6233. // -------------------------------------------------------------------
  6234. {$IFNDEF FPC}
  6235. procedure SetFontAntialiasing(const Font: TFont; Quality: Cardinal);
  6236. var
  6237. LogFont: TLogFont;
  6238. begin
  6239. LogFont := Default(TLogFont);
  6240. with LogFont do
  6241. begin
  6242. lfHeight := Font.Height;
  6243. lfWidth := 0; { have font mapper choose }
  6244. {$IFDEF COMPILER2005_UP}
  6245. lfEscapement := Font.Orientation;
  6246. lfOrientation := Font.Orientation;
  6247. {$ELSE}
  6248. lfEscapement := 0;
  6249. lfOrientation := 0;
  6250. {$ENDIF}
  6251. if fsBold in Font.Style then
  6252. lfWeight := FW_BOLD
  6253. else
  6254. lfWeight := FW_NORMAL;
  6255. lfItalic := Byte(fsItalic in Font.Style);
  6256. lfUnderline := Byte(fsUnderline in Font.Style);
  6257. lfStrikeOut := Byte(fsStrikeOut in Font.Style);
  6258. lfCharSet := Byte(Font.Charset);
  6259. if AnsiCompareText(Font.Name, 'Default') = 0 then // do not localize
  6260. StrLCopy(lfFaceName, @DefFontData.Name[1], LF_FACESIZE-1)
  6261. else
  6262. StrLCopy(lfFaceName, @Font.Name[1], LF_FACESIZE-1);
  6263. lfQuality := Quality;
  6264. { Only True Type fonts support the angles }
  6265. if lfOrientation <> 0 then
  6266. lfOutPrecision := OUT_TT_ONLY_PRECIS
  6267. else
  6268. lfOutPrecision := OUT_DEFAULT_PRECIS;
  6269. lfClipPrecision := CLIP_DEFAULT_PRECIS;
  6270. case Font.Pitch of
  6271. fpVariable: lfPitchAndFamily := VARIABLE_PITCH;
  6272. fpFixed: lfPitchAndFamily := FIXED_PITCH;
  6273. else
  6274. lfPitchAndFamily := DEFAULT_PITCH;
  6275. end;
  6276. end;
  6277. Font.Handle := CreateFontIndirect(LogFont);
  6278. end;
  6279. {$ENDIF}
  6280. procedure TextBlueToAlpha(const B: TCustomBitmap32; Color: TColor32);
  6281. (*
  6282. asm
  6283. PUSH EDI
  6284. MOV ECX, [B+$44].Integer
  6285. IMUL ECX, [B+$40].Integer
  6286. MOV EDI, [B+$54].Integer
  6287. @PixelLoop:
  6288. MOV EAX, [EDI]
  6289. SHL EAX, 24
  6290. ADD EAX, Color
  6291. MOV [EDI], EAX
  6292. ADD EDI, 4
  6293. LOOP @PixelLoop
  6294. POP EDI
  6295. end;
  6296. *)
  6297. var
  6298. I: Integer;
  6299. P: PColor32;
  6300. begin
  6301. // convert blue channel to alpha and fill the color
  6302. Color := Color and $00FFFFFF;
  6303. P := @B.Bits[0];
  6304. for I := 0 to B.Width * B.Height - 1 do
  6305. begin
  6306. if P^ <> 0 then
  6307. P^ := ((P^ and $FF) shl 24) or Color
  6308. else
  6309. P^ := 0;
  6310. Inc(P);
  6311. end;
  6312. end;
  6313. procedure TextScaleDown(const B, B2: TCustomBitmap32; const N: Integer;
  6314. const Color: TColor32); // use only the blue channel
  6315. var
  6316. I, J, X, Y, P, Q, Sz, S: Integer;
  6317. Src: PColor32;
  6318. Dst: PColor32;
  6319. begin
  6320. Sz := 1 shl N - 1;
  6321. Dst := PColor32(B.ScanLine[0]);
  6322. for J := 0 to B.Height - 1 do
  6323. begin
  6324. Y := J shl N;
  6325. for I := 0 to B.Width - 1 do
  6326. begin
  6327. X := I shl N;
  6328. S := 0;
  6329. for Q := Y to Y + Sz do
  6330. begin
  6331. Src := B2.PixelPtr[X, Q];
  6332. for P := X to X + Sz do
  6333. begin
  6334. S := S + Integer(Src^ and $000000FF);
  6335. Inc(Src);
  6336. end;
  6337. end;
  6338. S := S shr N shr N;
  6339. Dst^ := TColor32(S shl 24) + Color;
  6340. Inc(Dst);
  6341. end;
  6342. end;
  6343. end;
  6344. procedure TBitmap32.RenderText(X, Y: Integer; const Text: string; AALevel: Integer; Color: TColor32);
  6345. var
  6346. B, B2: TBitmap32;
  6347. Sz: TSize;
  6348. {$IFNDEF PLATFORM_INDEPENDENT}
  6349. SzSpace: TSize;
  6350. {$ENDIF}
  6351. Alpha: TColor32;
  6352. StockCanvas: TCanvas;
  6353. begin
  6354. if Empty then
  6355. Exit;
  6356. Alpha := Color shr 24;
  6357. Color := Color and $00FFFFFF;
  6358. AALevel := Constrain(AALevel, -1, 4);
  6359. {$IFDEF FPC}
  6360. if AALevel > -1 then
  6361. Font.Quality := fqNonAntialiased
  6362. else
  6363. Font.Quality := fqAntialiased;
  6364. {$ELSE}
  6365. if AALevel > -1 then
  6366. SetFontAntialiasing(Font, NONANTIALIASED_QUALITY)
  6367. else
  6368. SetFontAntialiasing(Font, ANTIALIASED_QUALITY);
  6369. {$ENDIF}
  6370. { TODO : Optimize Clipping here }
  6371. B := TBitmap32.Create;
  6372. try
  6373. if AALevel <= 0 then
  6374. begin
  6375. Sz := Self.TextExtent(Text) + Self.TextExtent(' ');
  6376. B.SetSize(Sz.cX, Sz.cY);
  6377. B.Font := Font;
  6378. B.Clear(0);
  6379. B.Font.Color := clWhite;
  6380. B.Textout(0, 0, Text);
  6381. TextBlueToAlpha(B, Color);
  6382. end
  6383. else
  6384. begin
  6385. StockCanvas := StockBitmap.Canvas;
  6386. StockCanvas.Lock;
  6387. try
  6388. StockCanvas.Font := Font;
  6389. StockCanvas.Font.Size := Font.Size shl AALevel;
  6390. {$IFDEF PLATFORM_INDEPENDENT}
  6391. Sz := StockCanvas.TextExtent(Text) + StockCanvas.TextExtent(' ');
  6392. {$ELSE}
  6393. Windows.GetTextExtentPoint32(StockCanvas.Handle, PChar(Text), Length(Text), Sz);
  6394. Windows.GetTextExtentPoint32(StockCanvas.Handle, PChar(string(' ')), 1, SzSpace);
  6395. Sz := Sz + SzSpace;
  6396. {$ENDIF}
  6397. Sz.Cx := (Sz.cx shr AALevel + 1) shl AALevel;
  6398. Sz.Cy := (Sz.cy shr AALevel + 1) shl AALevel;
  6399. B2 := TBitmap32.Create;
  6400. try
  6401. B2.SetSize(Sz.Cx, Sz.Cy);
  6402. B2.Clear(0);
  6403. B2.Font := StockCanvas.Font;
  6404. B2.Font.Color := clWhite;
  6405. B2.Textout(0, 0, Text);
  6406. B.SetSize(Sz.cx shr AALevel, Sz.cy shr AALevel);
  6407. TextScaleDown(B, B2, AALevel, Color);
  6408. finally
  6409. B2.Free;
  6410. end;
  6411. finally
  6412. StockCanvas.Unlock;
  6413. end;
  6414. end;
  6415. B.DrawMode := dmBlend;
  6416. B.MasterAlpha := Alpha;
  6417. B.CombineMode := CombineMode;
  6418. B.DrawTo(Self, X, Y);
  6419. finally
  6420. B.Free;
  6421. end;
  6422. {$IFDEF FPC}
  6423. Font.Quality := fqDefault;
  6424. {$ELSE}
  6425. SetFontAntialiasing(Font, DEFAULT_QUALITY);
  6426. {$ENDIF}
  6427. end;
  6428. // -------------------------------------------------------------------
  6429. function TBitmap32.CanvasAllocated: Boolean;
  6430. var
  6431. CanvasSupport: ICanvasSupport;
  6432. begin
  6433. Result := Supports(Backend, ICanvasSupport, CanvasSupport) and CanvasSupport.CanvasAllocated;
  6434. end;
  6435. procedure TBitmap32.DeleteCanvas;
  6436. var
  6437. CanvasSupport: ICanvasSupport;
  6438. begin
  6439. if Supports(Backend, ICanvasSupport, CanvasSupport) then
  6440. CanvasSupport.DeleteCanvas;
  6441. end;
  6442. { TCustomBackend }
  6443. constructor TCustomBackend.Create;
  6444. begin
  6445. RefCounted := True;
  6446. _AddRef;
  6447. inherited;
  6448. end;
  6449. constructor TCustomBackend.Create(Owner: TCustomBitmap32);
  6450. begin
  6451. FOwner := Owner;
  6452. Create;
  6453. if Assigned(Owner) then
  6454. Owner.Backend := Self;
  6455. end;
  6456. destructor TCustomBackend.Destroy;
  6457. begin
  6458. Clear;
  6459. inherited;
  6460. end;
  6461. procedure TCustomBackend.Clear;
  6462. var
  6463. Width, Height: Integer;
  6464. begin
  6465. if Assigned(FOwner) then
  6466. ChangeSize(FOwner.FWidth, FOwner.FHeight, 0, 0, False)
  6467. else
  6468. ChangeSize(Width, Height, 0, 0, False);
  6469. end;
  6470. procedure TCustomBackend.Changing;
  6471. begin
  6472. if Assigned(FOnChanging) then
  6473. FOnChanging(Self);
  6474. end;
  6475. {$IFDEF BITS_GETTER}
  6476. function TCustomBackend.GetBits: PColor32Array;
  6477. begin
  6478. Result := FBits;
  6479. end;
  6480. {$ENDIF}
  6481. procedure TCustomBackend.ChangeSize(out Width, Height: Integer; NewWidth, NewHeight: Integer; ClearBuffer: Boolean);
  6482. begin
  6483. try
  6484. Changing;
  6485. FinalizeSurface;
  6486. Width := 0;
  6487. Height := 0;
  6488. if (NewWidth > 0) and (NewHeight > 0) then
  6489. InitializeSurface(NewWidth, NewHeight, ClearBuffer);
  6490. Width := NewWidth;
  6491. Height := NewHeight;
  6492. finally
  6493. Changed;
  6494. end;
  6495. end;
  6496. procedure TCustomBackend.Assign(Source: TPersistent);
  6497. var
  6498. SrcBackend: TCustomBackend;
  6499. begin
  6500. if Source is TCustomBackend then
  6501. begin
  6502. if Assigned(FOwner) then
  6503. begin
  6504. SrcBackend := TCustomBackend(Source);
  6505. ChangeSize(
  6506. FOwner.FWidth, FOwner.FHeight,
  6507. SrcBackend.FOwner.Width, SrcBackend.FOwner.Height,
  6508. False
  6509. );
  6510. if not SrcBackend.Empty then
  6511. MoveLongword(
  6512. SrcBackend.Bits[0], Bits[0],
  6513. SrcBackend.FOwner.Width * SrcBackend.FOwner.Height
  6514. );
  6515. end;
  6516. end
  6517. else
  6518. inherited;
  6519. end;
  6520. function TCustomBackend.Empty: Boolean;
  6521. begin
  6522. Result := False;
  6523. end;
  6524. procedure TCustomBackend.FinalizeSurface;
  6525. begin
  6526. // descendants override this method
  6527. end;
  6528. procedure TCustomBackend.InitializeSurface(NewWidth, NewHeight: Integer; ClearBuffer: Boolean);
  6529. begin
  6530. // descendants override this method
  6531. end;
  6532. { TCustomSampler }
  6533. function TCustomSampler.GetSampleInt(X, Y: Integer): TColor32;
  6534. begin
  6535. Result := GetSampleFixed(X * FixedOne, Y * FixedOne);
  6536. end;
  6537. function TCustomSampler.GetSampleFixed(X, Y: TFixed): TColor32;
  6538. begin
  6539. Result := GetSampleFloat(X * FixedToFloat, Y * FixedToFloat);
  6540. end;
  6541. function TCustomSampler.GetSampleFloat(X, Y: TFloat): TColor32;
  6542. begin
  6543. Result := GetSampleFixed(Fixed(X), Fixed(Y));
  6544. end;
  6545. procedure TCustomSampler.PrepareSampling;
  6546. begin
  6547. // descendants override this method
  6548. end;
  6549. procedure TCustomSampler.FinalizeSampling;
  6550. begin
  6551. // descendants override this method
  6552. end;
  6553. function TCustomSampler.HasBounds: Boolean;
  6554. begin
  6555. Result := False;
  6556. end;
  6557. function TCustomSampler.GetSampleBounds: TFloatRect;
  6558. const
  6559. InfRect: TFloatRect = (Left: -Infinity; Top: -Infinity; Right: Infinity; Bottom: Infinity);
  6560. begin
  6561. Result := InfRect;
  6562. end;
  6563. { TCustomResampler }
  6564. procedure TCustomResampler.AssignTo(Dst: TPersistent);
  6565. begin
  6566. if Dst is TCustomResampler then
  6567. SmartAssign(Self, Dst)
  6568. else
  6569. inherited;
  6570. end;
  6571. procedure TCustomResampler.Changed;
  6572. begin
  6573. if Assigned(FBitmap) then FBitmap.Changed;
  6574. end;
  6575. constructor TCustomResampler.Create;
  6576. begin
  6577. inherited;
  6578. FPixelAccessMode := pamSafe;
  6579. end;
  6580. constructor TCustomResampler.Create(ABitmap: TCustomBitmap32);
  6581. begin
  6582. Create;
  6583. FBitmap := ABitmap;
  6584. if Assigned(ABitmap) then ABitmap.Resampler := Self;
  6585. end;
  6586. function TCustomResampler.GetSampleBounds: TFloatRect;
  6587. begin
  6588. Result := FloatRect(FBitmap.ClipRect);
  6589. if PixelAccessMode = pamTransparentEdge then
  6590. InflateRect(Result, 1, 1);
  6591. end;
  6592. function TCustomResampler.GetWidth: TFloat;
  6593. begin
  6594. Result := 0;
  6595. end;
  6596. function TCustomResampler.HasBounds: Boolean;
  6597. begin
  6598. Result := FPixelAccessMode <> pamWrap;
  6599. end;
  6600. procedure TCustomResampler.PrepareSampling;
  6601. begin
  6602. FClipRect := FBitmap.ClipRect;
  6603. end;
  6604. procedure TCustomResampler.SetPixelAccessMode(
  6605. const Value: TPixelAccessMode);
  6606. begin
  6607. if FPixelAccessMode <> Value then
  6608. begin
  6609. FPixelAccessMode := Value;
  6610. Changed;
  6611. end;
  6612. end;
  6613. initialization
  6614. SetGamma;
  6615. StockBitmap := TBitmap.Create;
  6616. StockBitmap.Width := 8;
  6617. StockBitmap.Height := 8;
  6618. finalization
  6619. StockBitmap.Free;
  6620. end.