GR32.pas 219 KB

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