From bd5f01d4c0a44b3a04c7d19021d3c057ebbdbdc8 Mon Sep 17 00:00:00 2001 From: Wizzup? Date: Mon, 12 Oct 2009 14:37:31 +0000 Subject: [PATCH] GPL v3. (Not aGPL after all.) git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@124 3f818213-9676-44b0-a9b4-5e4c4e03d09d --- COPYING | 141 ++- Projects/SAMufasaGUI/project1.lpi | 114 +- Units/MMLCore/bitmaps.pas | 1071 ++++++++-------- Units/MMLCore/client.pas | 19 +- Units/MMLCore/colour_conv.pas | 23 + Units/MMLCore/dtm.pas | 23 + Units/MMLCore/dtmutil.pas | 23 + Units/MMLCore/files.pas | 23 + Units/MMLCore/finder.pas | 1969 +++++++++++++++-------------- Units/MMLCore/input.pas | 23 + Units/MMLCore/mmath.pas | 23 + Units/MMLCore/mufasatypes.pas | 23 + Units/MMLCore/window.pas | 23 + 13 files changed, 1882 insertions(+), 1616 deletions(-) diff --git a/COPYING b/COPYING index dba13ed..94a9ed0 100644 --- a/COPYING +++ b/COPYING @@ -1,5 +1,5 @@ - GNU AFFERO GENERAL PUBLIC LICENSE - Version 3, 19 November 2007 + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies @@ -7,15 +7,17 @@ Preamble - The GNU Affero General Public License is a free, copyleft license for -software and other kinds of works, specifically designed to ensure -cooperation with the community in the case of network server software. + The GNU General Public License is a free, copyleft license for +software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, -our General Public Licenses are intended to guarantee your freedom to +the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free -software for all its users. +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you @@ -24,34 +26,44 @@ them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. - Developers that use our General Public Licenses protect your rights -with two steps: (1) assert copyright on the software, and (2) offer -you this License which gives you legal permission to copy, distribute -and/or modify the software. + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. - A secondary benefit of defending all users' freedom is that -improvements made in alternate versions of the program, if they -receive widespread use, become available for other developers to -incorporate. Many developers of free software are heartened and -encouraged by the resulting cooperation. However, in the case of -software used on network servers, this result may fail to come about. -The GNU General Public License permits making a modified version and -letting the public access it on a server without ever releasing its -source code to the public. + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. - The GNU Affero General Public License is designed specifically to -ensure that, in such cases, the modified source code becomes available -to the community. It requires the operator of a network server to -provide the source code of the modified version running there to the -users of that server. Therefore, public use of a modified version, on -a publicly accessible server, gives the public access to the source -code of the modified version. + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. - An older license, called the Affero General Public License and -published by Affero, was designed to accomplish similar goals. This is -a different license, not a version of the Affero GPL, but Affero has -released a new version of the Affero GPL which permits relicensing under -this license. + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. @@ -60,7 +72,7 @@ modification follow. 0. Definitions. - "This License" refers to version 3 of the GNU Affero General Public License. + "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. @@ -537,45 +549,35 @@ to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. - 13. Remote Network Interaction; Use with the GNU General Public License. - - Notwithstanding any other provision of this License, if you modify the -Program, your modified version must prominently offer all users -interacting with it remotely through a computer network (if your version -supports such interaction) an opportunity to receive the Corresponding -Source of your version by providing access to the Corresponding Source -from a network server at no charge, through some standard or customary -means of facilitating copying of software. This Corresponding Source -shall include the Corresponding Source for any work covered by version 3 -of the GNU General Public License that is incorporated pursuant to the -following paragraph. + 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed -under version 3 of the GNU General Public License into a single +under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, -but the work with which it is combined will remain governed by version -3 of the GNU General Public License. +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of -the GNU Affero General Public License from time to time. Such new versions -will be similar in spirit to the present version, but may differ in detail to +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU Affero General +Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the -GNU Affero General Public License, you may choose any version ever published +GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future -versions of the GNU Affero General Public License can be used, that proxy's +versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. @@ -633,29 +635,40 @@ the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by + it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. + GNU General Public License for more details. - You should have received a copy of the GNU Affero General Public License + You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. - If your software can interact with users remotely through a computer -network, you should also make sure that it provides a way for users to -get its source. For example, if your program is a web application, its -interface could display a "Source" link that leads users to an archive -of the code. There are many ways you could offer source, and different -solutions will be better for different programs; see section 13 for the -specific requirements. + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU AGPL, see +For more information on this, and how to apply and follow the GNU GPL, see . + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/Projects/SAMufasaGUI/project1.lpi b/Projects/SAMufasaGUI/project1.lpi index 1e609bd..b7cee4c 100644 --- a/Projects/SAMufasaGUI/project1.lpi +++ b/Projects/SAMufasaGUI/project1.lpi @@ -7,7 +7,7 @@ <UseXPManifest Value="True"/> - <ActiveEditorIndexAtStart Value="10"/> + <ActiveEditorIndexAtStart Value="4"/> </General> <VersionInfo> <ProjectVersion Value=""/> @@ -33,7 +33,7 @@ <PackageName Value="LCL"/> </Item2> </RequiredPackages> - <Units Count="133"> + <Units Count="135"> <Unit0> <Filename Value="project1.lpr"/> <IsPartOfProject Value="True"/> @@ -302,7 +302,7 @@ <Filename Value="../../Units/MMLCore/client.pas"/> <IsPartOfProject Value="True"/> <UnitName Value="Client"/> - <CursorPos X="69" Y="8"/> + <CursorPos X="47" Y="3"/> <TopLine Value="1"/> <EditorIndex Value="4"/> <UsageCount Value="156"/> @@ -313,7 +313,7 @@ <IsPartOfProject Value="True"/> <UnitName Value="MufasaTypes"/> <CursorPos X="17" Y="40"/> - <TopLine Value="1"/> + <TopLine Value="9"/> <EditorIndex Value="3"/> <UsageCount Value="156"/> <Loaded Value="True"/> @@ -375,8 +375,8 @@ <Filename Value="../../Units/MMLCore/finder.pas"/> <IsPartOfProject Value="True"/> <UnitName Value="finder"/> - <CursorPos X="22" Y="479"/> - <TopLine Value="461"/> + <CursorPos X="7" Y="8"/> + <TopLine Value="1"/> <EditorIndex Value="1"/> <UsageCount Value="149"/> <Loaded Value="True"/> @@ -518,7 +518,7 @@ <Unit66> <Filename Value="../../Units/MMLAddon/PSInc/pscompile.inc"/> <CursorPos X="42" Y="79"/> - <TopLine Value="31"/> + <TopLine Value="48"/> <EditorIndex Value="11"/> <UsageCount Value="46"/> <Loaded Value="True"/> @@ -539,7 +539,7 @@ <Filename Value="../../Units/MMLCore/bitmaps.pas"/> <IsPartOfProject Value="True"/> <UnitName Value="bitmaps"/> - <CursorPos X="86" Y="459"/> + <CursorPos X="1" Y="1"/> <TopLine Value="1"/> <EditorIndex Value="5"/> <UsageCount Value="135"/> @@ -561,8 +561,8 @@ <Unit72> <Filename Value="../../Units/MMLAddon/PSInc/Wrappers/bitmap.inc"/> <IsPartOfProject Value="True"/> - <CursorPos X="27" Y="111"/> - <TopLine Value="76"/> + <CursorPos X="26" Y="93"/> + <TopLine Value="1"/> <EditorIndex Value="10"/> <UsageCount Value="134"/> <Loaded Value="True"/> @@ -981,127 +981,141 @@ <TopLine Value="60"/> <UsageCount Value="8"/> </Unit132> + <Unit133> + <Filename Value="../../../../Documents/fpc/rtl/unix/classes.pp"/> + <UnitName Value="Classes"/> + <CursorPos X="1" Y="1"/> + <TopLine Value="1"/> + <UsageCount Value="10"/> + </Unit133> + <Unit134> + <Filename Value="../../../../Documents/fpc/rtl/unix/sysutils.pp"/> + <UnitName Value="sysutils"/> + <CursorPos X="1" Y="1"/> + <TopLine Value="1"/> + <UsageCount Value="10"/> + </Unit134> </Units> <JumpHistory Count="30" HistoryIndex="29"> <Position1> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="72" Column="35" TopLine="26"/> + <Caret Line="11" Column="130" TopLine="1"/> </Position1> <Position2> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="515" Column="19" TopLine="464"/> + <Caret Line="10" Column="112" TopLine="1"/> </Position2> <Position3> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="11" Column="130" TopLine="1"/> + <Caret Line="8" Column="83" TopLine="1"/> </Position3> <Position4> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="10" Column="112" TopLine="1"/> + <Caret Line="36" Column="43" TopLine="2"/> </Position4> <Position5> - <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="8" Column="83" TopLine="1"/> + <Filename Value="../../Units/MMLCore/bitmaps.pas"/> + <Caret Line="57" Column="52" TopLine="16"/> </Position5> <Position6> - <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="36" Column="43" TopLine="2"/> + <Filename Value="../../Units/MMLCore/bitmaps.pas"/> + <Caret Line="78" Column="129" TopLine="68"/> </Position6> <Position7> <Filename Value="../../Units/MMLCore/bitmaps.pas"/> - <Caret Line="57" Column="52" TopLine="16"/> + <Caret Line="270" Column="13" TopLine="242"/> </Position7> <Position8> <Filename Value="../../Units/MMLCore/bitmaps.pas"/> - <Caret Line="78" Column="129" TopLine="68"/> + <Caret Line="4" Column="116" TopLine="1"/> </Position8> <Position9> - <Filename Value="../../Units/MMLCore/bitmaps.pas"/> - <Caret Line="270" Column="13" TopLine="242"/> + <Filename Value="../../Units/MMLCore/finder.pas"/> + <Caret Line="36" Column="43" TopLine="2"/> </Position9> <Position10> - <Filename Value="../../Units/MMLCore/bitmaps.pas"/> - <Caret Line="4" Column="116" TopLine="1"/> + <Filename Value="../../Units/MMLCore/finder.pas"/> + <Caret Line="38" Column="90" TopLine="10"/> </Position10> <Position11> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="36" Column="43" TopLine="2"/> + <Caret Line="579" Column="25" TopLine="542"/> </Position11> <Position12> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="38" Column="90" TopLine="10"/> + <Caret Line="7" Column="132" TopLine="1"/> </Position12> <Position13> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="579" Column="25" TopLine="542"/> + <Caret Line="124" Column="19" TopLine="96"/> </Position13> <Position14> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="7" Column="132" TopLine="1"/> + <Caret Line="207" Column="19" TopLine="179"/> </Position14> <Position15> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="124" Column="19" TopLine="96"/> + <Caret Line="582" Column="53" TopLine="543"/> </Position15> <Position16> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="207" Column="19" TopLine="179"/> + <Caret Line="38" Column="53" TopLine="1"/> </Position16> <Position17> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="582" Column="53" TopLine="543"/> + <Caret Line="570" Column="127" TopLine="527"/> </Position17> <Position18> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="38" Column="53" TopLine="1"/> + <Caret Line="546" Column="51" TopLine="543"/> </Position18> <Position19> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="570" Column="127" TopLine="527"/> + <Caret Line="540" Column="32" TopLine="525"/> </Position19> <Position20> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="546" Column="51" TopLine="543"/> + <Caret Line="38" Column="68" TopLine="38"/> </Position20> <Position21> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="540" Column="32" TopLine="525"/> + <Caret Line="540" Column="46" TopLine="525"/> </Position21> <Position22> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="38" Column="68" TopLine="38"/> + <Caret Line="38" Column="46" TopLine="38"/> </Position22> <Position23> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="540" Column="46" TopLine="525"/> + <Caret Line="10" Column="137" TopLine="1"/> </Position23> <Position24> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="38" Column="46" TopLine="38"/> + <Caret Line="124" Column="19" TopLine="96"/> </Position24> <Position25> - <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="10" Column="137" TopLine="1"/> - </Position25> - <Position26> - <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="124" Column="19" TopLine="96"/> - </Position26> - <Position27> <Filename Value="../../Units/MMLAddon/mmlpsthread.pas"/> <Caret Line="135" Column="23" TopLine="120"/> + </Position25> + <Position26> + <Filename Value="../../Units/MMLAddon/PSInc/Wrappers/bitmap.inc"/> + <Caret Line="108" Column="19" TopLine="61"/> + </Position26> + <Position27> + <Filename Value="../../Units/MMLCore/finder.pas"/> + <Caret Line="36" Column="1" TopLine="1"/> </Position27> <Position28> <Filename Value="../../Units/MMLAddon/PSInc/Wrappers/bitmap.inc"/> - <Caret Line="108" Column="19" TopLine="61"/> + <Caret Line="114" Column="27" TopLine="77"/> </Position28> <Position29> <Filename Value="../../Units/MMLCore/finder.pas"/> - <Caret Line="36" Column="1" TopLine="1"/> + <Caret Line="8" Column="7" TopLine="1"/> </Position29> <Position30> - <Filename Value="../../Units/MMLAddon/PSInc/Wrappers/bitmap.inc"/> - <Caret Line="114" Column="27" TopLine="77"/> + <Filename Value="../../Units/MMLCore/client.pas"/> + <Caret Line="23" Column="8" TopLine="1"/> </Position30> </JumpHistory> </ProjectOptions> diff --git a/Units/MMLCore/bitmaps.pas b/Units/MMLCore/bitmaps.pas index d73b857..d908386 100644 --- a/Units/MMLCore/bitmaps.pas +++ b/Units/MMLCore/bitmaps.pas @@ -1,524 +1,547 @@ -unit bitmaps; - -{$mode objfpc}{$H+} - -interface -uses - Classes, SysUtils, FPImgCanv,FPImage,IntfGraphics,graphtype,MufasaTypes,window,graphics; - -type - - { TMufasaBitmap } - - TMufasaBitmap = class(TObject) - private - w,h : integer; - TransparentColor : TRGB32; - TransparentSet : boolean; - public - FData : PRGB32; - Index : integer; - BmpName : string; //Optional? - procedure SetSize(AWidth,AHeight : integer); - property Width : Integer read w; - property Height : Integer read h; - procedure ValidatePoint(x,y : integer); - function SaveToFile(const FileName : string) :boolean; - procedure LoadFromFile(const FileName : string); - procedure FastSetPixel(x,y : integer; Color : TColor); - procedure FastSetPixels(TPA : TPointArray; Colors : TIntegerArray); - function FastGetPixel(x,y : integer) : TColor; - function FastGetPixels(TPA : TPointArray) : TIntegerArray; - Procedure SetTransparentColor(Col : TColor); - Function GetTransparentColor : TColor; - procedure FastDrawClear(Color : TColor); - procedure FastDrawTransparent(x, y: Integer; TargetBitmap: TMufasaBitmap); - procedure FastReplaceColor(OldColor, NewColor: TColor); - procedure CopyClientToBitmap(MWindow : TMWindow; xs, ys, xe, ye: Integer); - constructor Create; - destructor Destroy;override; - end; - - TMufasaBmpArray = Array of TMufasaBitmap; - { TMBitmaps } - TMBitmaps = class(TObject) - protected - Client : TObject; - FreeSpots : Array of integer; - BmpArray : TMufasaBmpArray; - BmpsCurr,BmpsHigh,FreeSpotsHigh,FreeSpotsLen : integer; - public - function GetBMP(Index : integer) : TMufasaBitmap; - property Bmp[Index : integer]: TMufasaBitmap read GetBMP; - function CreateBMP(w, h: integer): Integer; - function CreateMirroredBitmap(bitmap: Integer; MirrorStyle : TBmpMirrorStyle): Integer; - function CreateBMPFromFile(const Path : string) : integer; - function CreateBMPFromString(width,height : integer; Data : string) : integer;overload; - function CreateBMPFromString(BmpName : string; width,height : integer; Data : string) : integer;overload; - procedure FreeBMP( Number : integer); - constructor Create(Owner : TObject); - destructor Destroy;override; - end; - - -implementation - -uses - Windowutil,paszlib,DCPbase64; - -function Min(a,b:integer) : integer; -begin - if a < b then - result := a - else - result := b; -end; - -{ TMBitmaps } - - -function TMBitmaps.GetBMP(Index: integer): TMufasaBitmap; -begin - Result := nil; - if (Index >= 0) and (Index <= BmpsCurr) then - if BmpArray[Index] <> nil then - Result := BmpArray[Index]; - if Result = nil then - raise Exception.CreateFmt('The bitmap[%d] does not exist',[Index]); -end; - -function TMBitmaps.CreateBMP(w,h : integer): Integer; -begin - if BmpsCurr < BmpsHigh then - begin; - inc(BmpsCurr); - Result := BmpsCurr; - end else if (FreeSpotsHigh > -1) then - begin; - Result := FreeSpots[FreeSpotsHigh]; - dec(FreeSpotsHigh); - end else - begin; - SetLength(BmpArray, BmpsHigh + 6); - BmpsHigh := BmpsHigh + 5; - inc(BmpsCurr); - Result := BmpsCurr; - end; - BmpArray[Result] := TMufasaBitmap.Create; - BmpArray[Result].SetSize(w,h); - BmpArray[Result].Index:= Result; -end; - -function TMBitmaps.CreateMirroredBitmap(bitmap: Integer; - MirrorStyle: TBmpMirrorStyle): Integer; -var - w,h : integer; - y,x : integer; - Source,Dest : PRGB32; -begin - Source := Bmp[Bitmap].FData; - w := BmpArray[Bitmap].Width; - h := BmpArray[Bitmap].Height; - if MirrorStyle = MirrorLine then - Result := CreateBMP(h,w) - else - Result := CreateBMP(w,h); - Dest := BmpArray[Result].FData; - case MirrorStyle of - MirrorWidth : for y := (h-1) downto 0 do - for x := (w-1) downto 0 do - Dest[y*w+x] := Source[y*w+w-1-x]; - MirrorHeight : for y := (h-1) downto 0 do - Move(Source[y*w],Dest[(h-1 - y) * w],w*SizeOf(TRGB32)); - MirrorLine : for y := (h-1) downto 0 do - for x := (w-1) downto 0 do - Dest[x*h+y] := Source[y*w+x]; - - end; -//Can be optmized, this is just proof of concept -end; - -function TMBitmaps.CreateBMPFromFile(const Path: string): integer; -begin - Result := CreateBMP(0,0); - BmpArray[result].LoadFromFile(Path); -end; - -function HexToInt(HexNum: string): LongInt;inline; -begin - Result:=StrToInt('$' + HexNum); -end; - -function TMBitmaps.CreateBMPFromString(width, height: integer; Data: string): integer; -var - I,II,x,y: LongWord; - DestLen : LongWord; - Dest,Source : string; - DestPoint, Point : PByte; - LazIntf : TLazIntfImage; - -begin - Result := CreateBMP(width,height); - if (Data <> '') and (Length(Data) <> 6) then - begin; - Point := Pointer(BmpArray[Result].FData); - if Data[1] = 'b' then - begin; - Source := Base64DecodeStr(Copy(Data,2,Length(Data) - 1)); - Destlen := Width * Height * 3; - Setlength(Dest,DestLen); - if uncompress(PChar(Dest),Destlen,pchar(Source), Length(Source)) = Z_OK then - begin; - DestPoint := @Dest[1]; - i := 0; - ii := 2; - Dec(DestLen); - if DestLen > 2 then - begin; - while (ii < DestLen) do - Begin; - Point[i]:= DestPoint[ii+2]; - Point[i+1]:= DestPoint[ii+1]; - Point[i+2]:= DestPoint[ii]; - ii := ii + 3; - i := i + 4; - end; - Point[i] := DestPoint[1]; - Point[i+1] := DestPoint[0]; - Point[i+2] := DestPoint[ii]; - end else if (Width = 1) and (Height =1 ) then - begin; - Point[0] := DestPoint[1]; - Point[1] := DestPoint[0]; - Point[2] := DestPoint[2]; - end; - end; - end else if Data[1] = 'z' then - begin; - Destlen := Width * Height * 3 *2; - Setlength(Dest,DestLen); - ii := (Length(Data) - 1) div 2; - SetLength(Source,ii); - for i := 1 to ii do - Source[i] := Chr(HexToInt(Data[i * 2] + Data[i * 2+1])); - if uncompress(PChar(Dest),Destlen,pchar(Source), ii) = Z_OK then - begin; - ii := 1; - i := 0; - while (II < DestLen) do - begin; - Point[i+2]:= HexToInt(Dest[ii] + Dest[ii + 1]); - Point[i+1]:= HexToInt(Dest[ii+2] + Dest[ii + 3]); - Point[i]:= HexToInt(Dest[ii+4] + Dest[ii + 5]); - ii := ii + 6; - i := i + 4; - end; - end; - end else if LongWord(Length(Data)) = (Width * Height * 3 * 2) then - begin; - ii := 1; - i := 0; - Destlen := Width * Height * 3 * 2; - while (II < DestLen) do - begin; - Point[i+2]:= HexToInt(Data[ii] + Data[ii + 1]); - Point[i+1]:= HexToInt(Data[ii+2] + Data[ii + 3]); - Point[i]:= HexToInt(Data[ii+4] + Data[ii + 5]); - ii := ii + 6; - i := i + 4; - end; - end; - end else - begin; - if Length(data) = 6 then - BmpArray[Result].FastDrawClear(HexToInt(Data)); -// else -// FastDrawClear(Result,clBlack); - end; -end; - -function TMBitmaps.CreateBMPFromString(BmpName: string; width, height: integer; - Data: string): integer; -begin - Result := Self.CreateBMPFromString(width,height,data); - Bmp[Result].BmpName:= BmpName; - -end; - -procedure TMBitmaps.FreeBMP(Number: integer); -var - ToDestroy : TMufasaBitmap; -begin - ToDestroy := GetBMP(Number); - if Number = BmpsCurr then - Dec(BmpsCurr) - else - begin; - inc(FreeSpotsHigh); - if FreeSpotsHigh = FreeSpotsLen then - begin; - inc(FreeSpotsLen); - SetLength(FreeSpots, FreeSpotsLen); - end; - FreeSpots[FreeSpotsHigh] := Number; - end; - //Just for testing purposes - if ToDestroy.BmpName = '' then - Writeln(Format('BMP[%d] has been freed.',[number])) - else - Writeln(Format('BMP[%s] has been freed.',[ToDestroy.BmpName])); - FreeAndNil(ToDestroy); -end; - -function TMufasaBitmap.SaveToFile(const FileName: string): boolean; -var - rawImage : TRawImage; - Bmp : TLazIntfImage; -begin - ArrDataToRawImage(FData,Point(w,h),RawImage); -// Bmp := Graphics.TBitmap.Create; - Bmp := TLazIntfImage.Create(RawImage,false); - Bmp.SaveToFile(FileName); - Bmp.Free; -end; - -procedure TMufasaBitmap.LoadFromFile(const FileName: string); -var - LazIntf : TLazIntfImage; - RawImageDesc : TRawImageDescription; -begin - if FileExists(FileName) then - begin; - LazIntf := TLazIntfImage.Create(0,0); - RawImageDesc.Init_BPP32_B8G8R8_BIO_TTB(LazIntf.Width,LazIntf.Height); - LazIntf.DataDescription := RawImageDesc; - LazIntf.LoadFromFile(FileName); - if Assigned(FData) then - Freemem(FData); - Self.W := LazIntf.Width; - Self.H := LazIntf.Height; - FData := GetMem(Self.W*Self.H*SizeOf(TRGB32)); - Move(LazIntf.PixelData[0],FData[0],w*h*sizeOf(TRGB32)); - LazIntf.Free; - end; -end; - -function RGBToBGR(Color : TColor) : TRGB32; inline; -begin; - Result.R := Color and $ff; - Result.G := Color shr 8 and $ff; - Result.B := Color shr 16 and $ff; -end; - -function BGRToRGB(BGR : TRGB32) : TColor;inline; -begin; - Result := BGR.R or BGR.g shl 8 or BGR.b shl 16; -end; - -procedure TMufasaBitmap.FastSetPixel(x, y: integer; Color: TColor); -begin - ValidatePoint(x,y); - FData[y*w+x] := RGBToBGR(Color); -end; - -procedure TMufasaBitmap.FastSetPixels(TPA: TPointArray; Colors: TIntegerArray); -var - i,len : integer; -begin - len := High(TPA); - if Len <> High(colors) then - Raise Exception.CreateFMT('TPA/Colors Length differ',[]); - for i := 0 to len do - begin; - ValidatePoint(TPA[i].x,TPA[i].y); - FData[TPA[i].y * w + TPA[i].x] := RGBToBGR(Colors[i]); - end; -end; - -function TMufasaBitmap.FastGetPixel(x, y: integer): TColor; -begin - ValidatePoint(x,y); - Result := BGRToRGB(FData[y*w+x]); -end; - -function TMufasaBitmap.FastGetPixels(TPA: TPointArray): TIntegerArray; -var - i,len : integer; -begin - len := high(TPA); - SetLength(result,len+1); - for i := 0 to len do - begin; - ValidatePoint(TPA[i].x,TPA[i].y); - Result[i] := BGRToRGB(FData[TPA[i].y*w + TPA[i].x]); - end; -end; - -procedure TMufasaBitmap.SetTransparentColor(Col: TColor); -begin - TransparentColor:= RGBToBGR(Col); - TransparentSet:= True; -end; - -function TMufasaBitmap.GetTransparentColor: TColor; -begin - if TransparentSet then - Result := BGRToRGB(TransparentColor) - else - raise Exception.CreateFmt('Transparent color for Bitmap[%d] isn''t set',[index]); -end; - -procedure TMufasaBitmap.FastDrawClear(Color: TColor); -var - i : integer; - Rec : TRGB32; -begin - Rec := RGBToBGR(Color); - if h > 0 then - begin; - for i := (w-1) downto 0 do - FData[i] := Rec; - for i := (h-1) downto 1 do - Move(FData[0],FData[i*w],w*SizeOf(TRGB32)); - end; -end; - -procedure TMufasaBitmap.FastDrawTransparent(x, y: Integer; - TargetBitmap: TMufasaBitmap); -var - MinW,MinH,TargetW,TargetH : Integer; - loopx,loopy : integer; -begin - ValidatePoint(x,y); - TargetW := TargetBitmap.Width; - TargetH := TargetBitmap.height; - MinW := Min(w-1,TargetW-x-1); - MinH := Min(h-1,TargetH-y-1); - if TransparentSet then - begin; - for loopy := 0 to MinH do - for loopx := 0 to MinW do - if LongWord(FData[loopy * w + loopx]) <> LongWord(TransparentColor) then - TargetBitmap.FData[(loopy + y) * TargetW + loopx + x] := FData[Loopy * w + loopx]; - - end - else - for loopy := 0 to MinH do - Move(FData[loopy*w],TargetBitmap.FData[(loopy+y) * TargetW + x],(MinW+1) * SizeOf(TRGB32)); - -end; - -procedure TMufasaBitmap.FastReplaceColor(OldColor, NewColor: TColor); -var - OldCol,NewCol : TRGB32; - i : integer; -begin - OldCol := RGBToBGR(OldColor); - NewCol := RGBToBGR(NewColor); - for i := w*h-1 downto 0 do - if LongWord(FData[i]) = LongWord(OldCol) then - FData[i] := NewCol; -end; - -procedure TMufasaBitmap.CopyClientToBitmap(MWindow : TMWindow; xs, ys, xe, ye: Integer); -var - wi,hi,y : integer; - PtrRet : TRetData; - Rows : integer; -begin - Self.ValidatePoint(xs,ys); - Self.ValidatePoint(xe,ye); - wi := xe-xs + 1; - hi := ye-ys + 1; - PtrRet := MWindow.ReturnData(xs,ys,wi,hi); - for y := 0 to (hi-1) do - Move(PtrRet.Ptr[y * (wi + PtrRet.IncPtrWith)], FData[y * self.w],wi * SizeOf(TRGB32)); - MWindow.FreeReturnData; -end; - -constructor TMBitmaps.Create(Owner: TObject); -begin - inherited Create; - SetLength(BmpArray,50); - SetLength(FreeSpots, 50); - FreeSpotsLen := 50; - BmpsHigh := 49; - BmpsCurr := -1; - FreeSpotsHigh := -1; - Self.Client := Owner; -end; - -destructor TMBitmaps.Destroy; -var - I : integer; -begin - for i := 0 to BmpsCurr do - if BmpArray[i] <> nil then - begin; - if BmpArray[i].BmpName = '' then - Writeln(Format('BMP[%d] has not been freed in the script, freeing it now.',[i])) - else - Writeln(Format('BMP[%s] has not been freed in the script, freeing it now.',[BmpArray[i].BmpName])); - FreeAndNil(BmpArray[i]); - end; - SetLength(BmpArray,0); - SetLength(FreeSpots,0); - inherited Destroy; -end; - - -{ TMufasaBitmap } -procedure TMufasaBitmap.SetSize(Awidth, Aheight: integer); -var - NewData : PRGB32; - i,minw,minh : integer; -begin - if (AWidth <> w) or (AHeight <> h) then - begin; - if AWidth*AHeight <> 0 then - begin; - NewData := GetMem(AWidth * AHeight * SizeOf(TRGB32)); - FillDWord(NewData[0],AWidth*AHeight,0); - end - else - NewData := nil; - if Assigned(FData) and Assigned(NewData) and (w*H <> 0) then - begin; - minw := Min(AWidth,w); - minh := Min(AHeight,h); - for i := 0 to minh - 1 do - Move(FData[i*w],Newdata[i*AWidth],minw * SizeOf(TRGB32)); - end; - if Assigned(FData) then - FreeMem(FData); - FData := NewData; - w := AWidth; - h := AHeight; - end; -end; - -procedure TMufasaBitmap.ValidatePoint(x, y: integer); -begin - if (x <0) or (x >= w) or (y < 0) or (y >= h) then - raise Exception.CreateFmt('You are accesing an invalid point, (%d,%d) at bitmap[%d]',[x,y,index]); -end; - -constructor TMufasaBitmap.Create; -begin - inherited Create; - BmpName:= ''; - FData:= nil; - TransparentSet:= False; - w := 0; - h := 0; -end; - -destructor TMufasaBitmap.Destroy; -begin - if Assigned(FData) then - Freemem(FData); - inherited Destroy; -end; - -end. - +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + Bitmaps class for the Mufasa Macro Library +} + +unit bitmaps; + +{$mode objfpc}{$H+} + +interface +uses + Classes, SysUtils, FPImgCanv,FPImage,IntfGraphics,graphtype,MufasaTypes,window,graphics; + +type + + { TMufasaBitmap } + + TMufasaBitmap = class(TObject) + private + w,h : integer; + TransparentColor : TRGB32; + TransparentSet : boolean; + public + FData : PRGB32; + Index : integer; + BmpName : string; //Optional? + procedure SetSize(AWidth,AHeight : integer); + property Width : Integer read w; + property Height : Integer read h; + procedure ValidatePoint(x,y : integer); + function SaveToFile(const FileName : string) :boolean; + procedure LoadFromFile(const FileName : string); + procedure FastSetPixel(x,y : integer; Color : TColor); + procedure FastSetPixels(TPA : TPointArray; Colors : TIntegerArray); + function FastGetPixel(x,y : integer) : TColor; + function FastGetPixels(TPA : TPointArray) : TIntegerArray; + Procedure SetTransparentColor(Col : TColor); + Function GetTransparentColor : TColor; + procedure FastDrawClear(Color : TColor); + procedure FastDrawTransparent(x, y: Integer; TargetBitmap: TMufasaBitmap); + procedure FastReplaceColor(OldColor, NewColor: TColor); + procedure CopyClientToBitmap(MWindow : TMWindow; xs, ys, xe, ye: Integer); + constructor Create; + destructor Destroy;override; + end; + + TMufasaBmpArray = Array of TMufasaBitmap; + { TMBitmaps } + TMBitmaps = class(TObject) + protected + Client : TObject; + FreeSpots : Array of integer; + BmpArray : TMufasaBmpArray; + BmpsCurr,BmpsHigh,FreeSpotsHigh,FreeSpotsLen : integer; + public + function GetBMP(Index : integer) : TMufasaBitmap; + property Bmp[Index : integer]: TMufasaBitmap read GetBMP; + function CreateBMP(w, h: integer): Integer; + function CreateMirroredBitmap(bitmap: Integer; MirrorStyle : TBmpMirrorStyle): Integer; + function CreateBMPFromFile(const Path : string) : integer; + function CreateBMPFromString(width,height : integer; Data : string) : integer;overload; + function CreateBMPFromString(BmpName : string; width,height : integer; Data : string) : integer;overload; + procedure FreeBMP( Number : integer); + constructor Create(Owner : TObject); + destructor Destroy;override; + end; + + +implementation + +uses + Windowutil,paszlib,DCPbase64; + +function Min(a,b:integer) : integer; +begin + if a < b then + result := a + else + result := b; +end; + +{ TMBitmaps } + + +function TMBitmaps.GetBMP(Index: integer): TMufasaBitmap; +begin + Result := nil; + if (Index >= 0) and (Index <= BmpsCurr) then + if BmpArray[Index] <> nil then + Result := BmpArray[Index]; + if Result = nil then + raise Exception.CreateFmt('The bitmap[%d] does not exist',[Index]); +end; + +function TMBitmaps.CreateBMP(w,h : integer): Integer; +begin + if BmpsCurr < BmpsHigh then + begin; + inc(BmpsCurr); + Result := BmpsCurr; + end else if (FreeSpotsHigh > -1) then + begin; + Result := FreeSpots[FreeSpotsHigh]; + dec(FreeSpotsHigh); + end else + begin; + SetLength(BmpArray, BmpsHigh + 6); + BmpsHigh := BmpsHigh + 5; + inc(BmpsCurr); + Result := BmpsCurr; + end; + BmpArray[Result] := TMufasaBitmap.Create; + BmpArray[Result].SetSize(w,h); + BmpArray[Result].Index:= Result; +end; + +function TMBitmaps.CreateMirroredBitmap(bitmap: Integer; + MirrorStyle: TBmpMirrorStyle): Integer; +var + w,h : integer; + y,x : integer; + Source,Dest : PRGB32; +begin + Source := Bmp[Bitmap].FData; + w := BmpArray[Bitmap].Width; + h := BmpArray[Bitmap].Height; + if MirrorStyle = MirrorLine then + Result := CreateBMP(h,w) + else + Result := CreateBMP(w,h); + Dest := BmpArray[Result].FData; + case MirrorStyle of + MirrorWidth : for y := (h-1) downto 0 do + for x := (w-1) downto 0 do + Dest[y*w+x] := Source[y*w+w-1-x]; + MirrorHeight : for y := (h-1) downto 0 do + Move(Source[y*w],Dest[(h-1 - y) * w],w*SizeOf(TRGB32)); + MirrorLine : for y := (h-1) downto 0 do + for x := (w-1) downto 0 do + Dest[x*h+y] := Source[y*w+x]; + + end; +//Can be optmized, this is just proof of concept +end; + +function TMBitmaps.CreateBMPFromFile(const Path: string): integer; +begin + Result := CreateBMP(0,0); + BmpArray[result].LoadFromFile(Path); +end; + +function HexToInt(HexNum: string): LongInt;inline; +begin + Result:=StrToInt('$' + HexNum); +end; + +function TMBitmaps.CreateBMPFromString(width, height: integer; Data: string): integer; +var + I,II,x,y: LongWord; + DestLen : LongWord; + Dest,Source : string; + DestPoint, Point : PByte; + LazIntf : TLazIntfImage; + +begin + Result := CreateBMP(width,height); + if (Data <> '') and (Length(Data) <> 6) then + begin; + Point := Pointer(BmpArray[Result].FData); + if Data[1] = 'b' then + begin; + Source := Base64DecodeStr(Copy(Data,2,Length(Data) - 1)); + Destlen := Width * Height * 3; + Setlength(Dest,DestLen); + if uncompress(PChar(Dest),Destlen,pchar(Source), Length(Source)) = Z_OK then + begin; + DestPoint := @Dest[1]; + i := 0; + ii := 2; + Dec(DestLen); + if DestLen > 2 then + begin; + while (ii < DestLen) do + Begin; + Point[i]:= DestPoint[ii+2]; + Point[i+1]:= DestPoint[ii+1]; + Point[i+2]:= DestPoint[ii]; + ii := ii + 3; + i := i + 4; + end; + Point[i] := DestPoint[1]; + Point[i+1] := DestPoint[0]; + Point[i+2] := DestPoint[ii]; + end else if (Width = 1) and (Height =1 ) then + begin; + Point[0] := DestPoint[1]; + Point[1] := DestPoint[0]; + Point[2] := DestPoint[2]; + end; + end; + end else if Data[1] = 'z' then + begin; + Destlen := Width * Height * 3 *2; + Setlength(Dest,DestLen); + ii := (Length(Data) - 1) div 2; + SetLength(Source,ii); + for i := 1 to ii do + Source[i] := Chr(HexToInt(Data[i * 2] + Data[i * 2+1])); + if uncompress(PChar(Dest),Destlen,pchar(Source), ii) = Z_OK then + begin; + ii := 1; + i := 0; + while (II < DestLen) do + begin; + Point[i+2]:= HexToInt(Dest[ii] + Dest[ii + 1]); + Point[i+1]:= HexToInt(Dest[ii+2] + Dest[ii + 3]); + Point[i]:= HexToInt(Dest[ii+4] + Dest[ii + 5]); + ii := ii + 6; + i := i + 4; + end; + end; + end else if LongWord(Length(Data)) = (Width * Height * 3 * 2) then + begin; + ii := 1; + i := 0; + Destlen := Width * Height * 3 * 2; + while (II < DestLen) do + begin; + Point[i+2]:= HexToInt(Data[ii] + Data[ii + 1]); + Point[i+1]:= HexToInt(Data[ii+2] + Data[ii + 3]); + Point[i]:= HexToInt(Data[ii+4] + Data[ii + 5]); + ii := ii + 6; + i := i + 4; + end; + end; + end else + begin; + if Length(data) = 6 then + BmpArray[Result].FastDrawClear(HexToInt(Data)); +// else +// FastDrawClear(Result,clBlack); + end; +end; + +function TMBitmaps.CreateBMPFromString(BmpName: string; width, height: integer; + Data: string): integer; +begin + Result := Self.CreateBMPFromString(width,height,data); + Bmp[Result].BmpName:= BmpName; + +end; + +procedure TMBitmaps.FreeBMP(Number: integer); +var + ToDestroy : TMufasaBitmap; +begin + ToDestroy := GetBMP(Number); + if Number = BmpsCurr then + Dec(BmpsCurr) + else + begin; + inc(FreeSpotsHigh); + if FreeSpotsHigh = FreeSpotsLen then + begin; + inc(FreeSpotsLen); + SetLength(FreeSpots, FreeSpotsLen); + end; + FreeSpots[FreeSpotsHigh] := Number; + end; + //Just for testing purposes + if ToDestroy.BmpName = '' then + Writeln(Format('BMP[%d] has been freed.',[number])) + else + Writeln(Format('BMP[%s] has been freed.',[ToDestroy.BmpName])); + FreeAndNil(ToDestroy); +end; + +function TMufasaBitmap.SaveToFile(const FileName: string): boolean; +var + rawImage : TRawImage; + Bmp : TLazIntfImage; +begin + ArrDataToRawImage(FData,Point(w,h),RawImage); +// Bmp := Graphics.TBitmap.Create; + Bmp := TLazIntfImage.Create(RawImage,false); + Bmp.SaveToFile(FileName); + Bmp.Free; +end; + +procedure TMufasaBitmap.LoadFromFile(const FileName: string); +var + LazIntf : TLazIntfImage; + RawImageDesc : TRawImageDescription; +begin + if FileExists(FileName) then + begin; + LazIntf := TLazIntfImage.Create(0,0); + RawImageDesc.Init_BPP32_B8G8R8_BIO_TTB(LazIntf.Width,LazIntf.Height); + LazIntf.DataDescription := RawImageDesc; + LazIntf.LoadFromFile(FileName); + if Assigned(FData) then + Freemem(FData); + Self.W := LazIntf.Width; + Self.H := LazIntf.Height; + FData := GetMem(Self.W*Self.H*SizeOf(TRGB32)); + Move(LazIntf.PixelData[0],FData[0],w*h*sizeOf(TRGB32)); + LazIntf.Free; + end; +end; + +function RGBToBGR(Color : TColor) : TRGB32; inline; +begin; + Result.R := Color and $ff; + Result.G := Color shr 8 and $ff; + Result.B := Color shr 16 and $ff; +end; + +function BGRToRGB(BGR : TRGB32) : TColor;inline; +begin; + Result := BGR.R or BGR.g shl 8 or BGR.b shl 16; +end; + +procedure TMufasaBitmap.FastSetPixel(x, y: integer; Color: TColor); +begin + ValidatePoint(x,y); + FData[y*w+x] := RGBToBGR(Color); +end; + +procedure TMufasaBitmap.FastSetPixels(TPA: TPointArray; Colors: TIntegerArray); +var + i,len : integer; +begin + len := High(TPA); + if Len <> High(colors) then + Raise Exception.CreateFMT('TPA/Colors Length differ',[]); + for i := 0 to len do + begin; + ValidatePoint(TPA[i].x,TPA[i].y); + FData[TPA[i].y * w + TPA[i].x] := RGBToBGR(Colors[i]); + end; +end; + +function TMufasaBitmap.FastGetPixel(x, y: integer): TColor; +begin + ValidatePoint(x,y); + Result := BGRToRGB(FData[y*w+x]); +end; + +function TMufasaBitmap.FastGetPixels(TPA: TPointArray): TIntegerArray; +var + i,len : integer; +begin + len := high(TPA); + SetLength(result,len+1); + for i := 0 to len do + begin; + ValidatePoint(TPA[i].x,TPA[i].y); + Result[i] := BGRToRGB(FData[TPA[i].y*w + TPA[i].x]); + end; +end; + +procedure TMufasaBitmap.SetTransparentColor(Col: TColor); +begin + TransparentColor:= RGBToBGR(Col); + TransparentSet:= True; +end; + +function TMufasaBitmap.GetTransparentColor: TColor; +begin + if TransparentSet then + Result := BGRToRGB(TransparentColor) + else + raise Exception.CreateFmt('Transparent color for Bitmap[%d] isn''t set',[index]); +end; + +procedure TMufasaBitmap.FastDrawClear(Color: TColor); +var + i : integer; + Rec : TRGB32; +begin + Rec := RGBToBGR(Color); + if h > 0 then + begin; + for i := (w-1) downto 0 do + FData[i] := Rec; + for i := (h-1) downto 1 do + Move(FData[0],FData[i*w],w*SizeOf(TRGB32)); + end; +end; + +procedure TMufasaBitmap.FastDrawTransparent(x, y: Integer; + TargetBitmap: TMufasaBitmap); +var + MinW,MinH,TargetW,TargetH : Integer; + loopx,loopy : integer; +begin + ValidatePoint(x,y); + TargetW := TargetBitmap.Width; + TargetH := TargetBitmap.height; + MinW := Min(w-1,TargetW-x-1); + MinH := Min(h-1,TargetH-y-1); + if TransparentSet then + begin; + for loopy := 0 to MinH do + for loopx := 0 to MinW do + if LongWord(FData[loopy * w + loopx]) <> LongWord(TransparentColor) then + TargetBitmap.FData[(loopy + y) * TargetW + loopx + x] := FData[Loopy * w + loopx]; + + end + else + for loopy := 0 to MinH do + Move(FData[loopy*w],TargetBitmap.FData[(loopy+y) * TargetW + x],(MinW+1) * SizeOf(TRGB32)); + +end; + +procedure TMufasaBitmap.FastReplaceColor(OldColor, NewColor: TColor); +var + OldCol,NewCol : TRGB32; + i : integer; +begin + OldCol := RGBToBGR(OldColor); + NewCol := RGBToBGR(NewColor); + for i := w*h-1 downto 0 do + if LongWord(FData[i]) = LongWord(OldCol) then + FData[i] := NewCol; +end; + +procedure TMufasaBitmap.CopyClientToBitmap(MWindow : TMWindow; xs, ys, xe, ye: Integer); +var + wi,hi,y : integer; + PtrRet : TRetData; + Rows : integer; +begin + Self.ValidatePoint(xs,ys); + Self.ValidatePoint(xe,ye); + wi := xe-xs + 1; + hi := ye-ys + 1; + PtrRet := MWindow.ReturnData(xs,ys,wi,hi); + for y := 0 to (hi-1) do + Move(PtrRet.Ptr[y * (wi + PtrRet.IncPtrWith)], FData[y * self.w],wi * SizeOf(TRGB32)); + MWindow.FreeReturnData; +end; + +constructor TMBitmaps.Create(Owner: TObject); +begin + inherited Create; + SetLength(BmpArray,50); + SetLength(FreeSpots, 50); + FreeSpotsLen := 50; + BmpsHigh := 49; + BmpsCurr := -1; + FreeSpotsHigh := -1; + Self.Client := Owner; +end; + +destructor TMBitmaps.Destroy; +var + I : integer; +begin + for i := 0 to BmpsCurr do + if BmpArray[i] <> nil then + begin; + if BmpArray[i].BmpName = '' then + Writeln(Format('BMP[%d] has not been freed in the script, freeing it now.',[i])) + else + Writeln(Format('BMP[%s] has not been freed in the script, freeing it now.',[BmpArray[i].BmpName])); + FreeAndNil(BmpArray[i]); + end; + SetLength(BmpArray,0); + SetLength(FreeSpots,0); + inherited Destroy; +end; + + +{ TMufasaBitmap } +procedure TMufasaBitmap.SetSize(Awidth, Aheight: integer); +var + NewData : PRGB32; + i,minw,minh : integer; +begin + if (AWidth <> w) or (AHeight <> h) then + begin; + if AWidth*AHeight <> 0 then + begin; + NewData := GetMem(AWidth * AHeight * SizeOf(TRGB32)); + FillDWord(NewData[0],AWidth*AHeight,0); + end + else + NewData := nil; + if Assigned(FData) and Assigned(NewData) and (w*H <> 0) then + begin; + minw := Min(AWidth,w); + minh := Min(AHeight,h); + for i := 0 to minh - 1 do + Move(FData[i*w],Newdata[i*AWidth],minw * SizeOf(TRGB32)); + end; + if Assigned(FData) then + FreeMem(FData); + FData := NewData; + w := AWidth; + h := AHeight; + end; +end; + +procedure TMufasaBitmap.ValidatePoint(x, y: integer); +begin + if (x <0) or (x >= w) or (y < 0) or (y >= h) then + raise Exception.CreateFmt('You are accesing an invalid point, (%d,%d) at bitmap[%d]',[x,y,index]); +end; + +constructor TMufasaBitmap.Create; +begin + inherited Create; + BmpName:= ''; + FData:= nil; + TransparentSet:= False; + w := 0; + h := 0; +end; + +destructor TMufasaBitmap.Destroy; +begin + if Assigned(FData) then + Freemem(FData); + inherited Destroy; +end; + +end. + diff --git a/Units/MMLCore/client.pas b/Units/MMLCore/client.pas index 0438ee2..b949497 100644 --- a/Units/MMLCore/client.pas +++ b/Units/MMLCore/client.pas @@ -1,15 +1,24 @@ { This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer - Copyright (c) 2009 by Raymond van Venentie and Merlijn Wajer + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. See the file COPYING, included in this distribution, for details about the copyright. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - + Client class for the Mufasa Macro Library } diff --git a/Units/MMLCore/colour_conv.pas b/Units/MMLCore/colour_conv.pas index 14ee48b..ce35ca7 100644 --- a/Units/MMLCore/colour_conv.pas +++ b/Units/MMLCore/colour_conv.pas @@ -1,3 +1,26 @@ +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + Colour Conversion Utilities for the Mufasa Macro Library +} + unit colour_conv; {$mode objfpc}{$H+} diff --git a/Units/MMLCore/dtm.pas b/Units/MMLCore/dtm.pas index dc5a7c1..0352c53 100644 --- a/Units/MMLCore/dtm.pas +++ b/Units/MMLCore/dtm.pas @@ -1,3 +1,26 @@ +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + DTM class for the Mufasa Macro Library +} + unit dtm; {$mode objfpc}{$H+} diff --git a/Units/MMLCore/dtmutil.pas b/Units/MMLCore/dtmutil.pas index 0e9133a..5a138a0 100644 --- a/Units/MMLCore/dtmutil.pas +++ b/Units/MMLCore/dtmutil.pas @@ -1,3 +1,26 @@ +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + DTM Utilities for the Mufasa Macro Library +} + unit dtmutil; {$mode objfpc}{$H+} diff --git a/Units/MMLCore/files.pas b/Units/MMLCore/files.pas index d96fe2b..a72fbb1 100644 --- a/Units/MMLCore/files.pas +++ b/Units/MMLCore/files.pas @@ -1,3 +1,26 @@ +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + Files Class for the Mufasa Macro Library +} + unit files; {$mode objfpc}{$H+} diff --git a/Units/MMLCore/finder.pas b/Units/MMLCore/finder.pas index 45bc942..624fd68 100644 --- a/Units/MMLCore/finder.pas +++ b/Units/MMLCore/finder.pas @@ -1,973 +1,996 @@ -unit finder; - -{$mode objfpc}{$H+} - -interface - -uses - Classes, SysUtils,bitmaps, MufasaTypes; // Types - -{ TMFinder Class } - -{ - Should be 100% independant, as all platform dependant code is in the - Window and Input classes. - - Let's try not to use any OS-specific defines here? ;) -} - -type - TMFinder = class(TObject) - constructor Create(aClient: TObject); - destructor Destroy; override; - private - Procedure UpdateCachedValues(NewWidth,NewHeight : integer); - procedure DefaultOperations(var xs,ys,xe,ye : integer); - //Loads the Spiral into ClientTPA (Will not cause problems) - procedure LoadSpiralPath(startX, startY, x1, y1, x2, y2: Integer); - public - function CountColorTolerance(Color, xs, ys, xe, ye, Tolerance: Integer): Integer; - procedure SetToleranceSpeed(nCTS: Integer); - function SimilarColors(Color1,Color2,Tolerance : Integer) : boolean; - // Possibly turn x, y into a TPoint var. - function FindColor(var x, y: Integer; Color, xs, ys, xe, ye: Integer): Boolean; - function FindColorSpiral(var x, y: Integer; color, xs, ys, xe, ye: Integer): Boolean; - function FindColorTolerance(var x, y: Integer; Color, xs, ys, xe, ye, tol: Integer): Boolean; - function FindColorsTolerance(var Points: TPointArray; Color, xs, ys, xe, ye, Tol: Integer): Boolean; - function FindColorsSpiralTolerance(x, y: Integer; var Points: TPointArray; color, xs, ys, xe, ye: Integer; Tolerance: Integer) : boolean; - function FindColors(var TPA: TPointArray; Color, xs, ys, xe, ye: Integer): Boolean; - //Bitmap functions - function FindBitmap(bitmap: TMufasaBitmap; var x, y: Integer): Boolean; - function FindBitmapIn(bitmap: TMufasaBitmap; var x, y: Integer; xs, ys, xe, ye: Integer): Boolean; - function FindBitmapToleranceIn(bitmap: TMufasaBitmap; var x, y: Integer; xs, ys, xe, ye: Integer; tolerance: Integer): Boolean; - function FindBitmapSpiral(bitmap: TMufasaBitmap; var x, y: Integer; xs, ys, xe, ye: Integer): Boolean; - function FindBitmapSpiralTolerance(bitmap: TMufasaBitmap; var x, y: Integer; xs, ys, xe, ye,tolerance : integer): Boolean; - function FindBitmapsSpiralTolerance(bitmap: TMufasaBitmap; x, y: Integer; var Points : TPointArray; xs, ys, xe, ye,tolerance: Integer): Boolean; - protected - Client: TObject; - CachedWidth, CachedHeight : integer; - ClientTPA : TPointArray; - hueMod, satMod: Extended; - CTS: Integer; - end; - -implementation -uses - Client, // For the Client Casts. - colour_conv // For RGBToColor, etc. - ; -type - TPRGB32Array = array of PRGB32; - -procedure TMFinder.LoadSpiralPath(startX, startY, x1, y1, x2, y2: Integer); -var - i,y,x,c,Ring : integer; - CurrBox : TBox; -begin; - i := 0; - Ring := 1; - c := 0; - CurrBox.x1 := Startx-1; - CurrBox.y1 := Starty-1; - CurrBox.x2 := Startx+1; - CurrBox.y2 := Starty+1; - if (startx >= x1) and (startx <= x2) and (starty >= y1) and (starty <= y2) then - begin; - ClientTPA[c] := Point(Startx, StartY); - inc(c); - end; - Repeat - if (CurrBox.x2 >= x1) and (CurrBox.x1 <= x2) and (Currbox.y1 >= y1) and (Currbox.y1 <= y2) then - for i := CurrBox.x1 + 1 to CurrBox.x2 do - if (I >= x1) and ( I <= x2) then - begin; - ClientTPA[c] := Point(i,CurrBox.y1); - inc(c); - end; - if (CurrBox.x2 >= x1) and (CurrBox.x2 <= x2) and (Currbox.y2 >= y1) and (Currbox.y1 <= y2) then - for i := CurrBox.y1 + 1 to CurrBox.y2 do - if (I >= y1) and ( I <= y2) then - begin; - ClientTPA[c] := Point(Currbox.x2, I); - inc(c); - end; - if (CurrBox.x2 >= x1) and (CurrBox.x1 <= x2) and (Currbox.y2 >= y1) and (Currbox.y2 <= y2) then - for i := CurrBox.x2 - 1 downto CurrBox.x1 do - if (I >= x1) and ( I <= x2) then - begin; - ClientTPA[c] := Point(i,CurrBox.y2); - inc(c); - end; - if (CurrBox.x1 >= x1) and (CurrBox.x1 <= x2) and (Currbox.y2 >= y1) and (Currbox.y1 <= y2) then - for i := CurrBox.y2 - 1 downto CurrBox.y1 do - if (I >= y1) and ( I <= y2) then - begin; - ClientTPA[c] := Point(Currbox.x1, I); - inc(c); - end; - inc(ring); - CurrBox.x1 := Startx-ring; - CurrBox.y1 := Starty-Ring; - CurrBox.x2 := Startx+Ring; - CurrBox.y2 := Starty+Ring; - until (Currbox.x1 < x1) and (Currbox.x2 > x2) and (currbox.y1 < y1) and (currbox.y2 > y2); -end; - -function CalculateRowPtrs(ReturnData : TRetData; RowCount : integer) : TPRGB32Array;overload; -var - I : integer; -begin; - setlength(result,RowCount); - for i := 0 to RowCount - 1do - begin; - result[i] := ReturnData.Ptr; - inc(ReturnData.Ptr,ReturnData.IncPtrWith); - end; -end; - -function CalculateRowPtrs(Bitmap : TMufasaBitmap) : TPRGB32Array;overload; -var - I : integer; -begin; - setlength(result,Bitmap.Height); - for i := 0 to Bitmap.Height - 1 do - result[i] := Bitmap.FData + Bitmap.Width; -end; - -constructor TMFinder.Create(aClient: TObject); - -begin - inherited Create; - - Self.Client := aClient; - Self.CTS := 1; - Self.hueMod := 0.2; - Self.satMod := 0.2; - -end; - -destructor TMFinder.Destroy; -begin - - inherited; -end; - -procedure TMFinder.SetToleranceSpeed(nCTS: Integer); -begin - if (nCTS < 0) or (nCTS > 2) then - raise Exception.CreateFmt('The given CTS ([%d]) is invalid.',[nCTS]); - Self.CTS := nCTS; -end; - -function TMFinder.SimilarColors(Color1, Color2,Tolerance: Integer) : boolean; -var - R1,G1,B1,R2,G2,B2 : Byte; - H1,S1,L1,H2,S2,L2 : extended; -begin - Result := False; - ColorToRGB(Color1,R1,G1,B1); - ColorToRGB(Color2,R2,G2,B2); - if Color1 = Color2 then - Result := true - else - case CTS of - 0: Result := ((Abs(R1-R2) <= Tolerance) and (Abs(G1-G2) <= Tolerance) and (Abs(B1-B2) <= Tolerance)); - 1: Result := (Sqrt(sqr(R1-R2) + sqr(G1-G2) + sqr(B1-B2)) <= Tolerance); - 2: begin - RGBToHSL(R1,g1,b1,H1,S1,L1); - RGBToHSL(R2,g2,b2,H2,S2,L2); - Result := ((abs(H1 - H2) <= (hueMod * Tolerance)) and (abs(S2-S1) <= (satMod * Tolerance)) and (abs(L1-L2) <= Tolerance)); - end; - end; -end; - - -function ColorSame(var CTS,Tolerance : Integer; var R1,G1,B1,R2,G2,B2 : byte; var H1,S1,L1,huemod,satmod : extended) : boolean; inline; -var - H2,S2,L2 : extended; -begin - Result := False; - case CTS of - 0: Result := ((Abs(R1-R2) <= Tolerance) and (Abs(G1-G2) <= Tolerance) and (Abs(B1-B2) <= Tolerance)); - 1: Result := (Sqrt(sqr(R1-R2) + sqr(G1-G2) + sqr(B1-B2)) <= Tolerance); - 2: begin - RGBToHSL(R1,g1,b1,H1,S1,L1); - RGBToHSL(R2,g2,b2,H2,S2,L2); - Result := ((abs(H1 - H2) <= (hueMod * Tolerance)) and (abs(S2-S1) <= (satMod * Tolerance)) and (abs(L1-L2) <= Tolerance)); - end; - end; -end; - -procedure TMFinder.UpdateCachedValues(NewWidth, NewHeight: integer); -begin - CachedWidth := NewWidth; - CachedHeight := NewHeight; - SetLength(ClientTPA,NewWidth * NewHeight); -end; - -procedure TMFinder.DefaultOperations(var xs, ys, xe, ye: integer); -var - w,h : integer; -begin -{ if xs > xe then - Swap(xs,xe); - if ys > ye then - Swap(ys,ye);} - if xs < 0 then - // xs := 0; - raise Exception.createFMT('Any Find Function, you did not pass a ' + - 'correct xs: %d.', [xs]); - if ys < 0 then -// ys := 0; - raise Exception.createFMT('Any Find Function, you did not pass a ' + - 'correct ys: %d.', [ys]); - - TClient(Self.Client).MWindow.GetDimensions(w,h); - if (w <> CachedWidth) or (h <> CachedHeight) then - UpdateCachedValues(w,h); - if xe >= w then -// xe := w-1; - raise Exception.createFMT('Any Find Function, you did not pass a ' + - 'correct xe: %d.', [xe]); - if ye >= h then -// ye := h-1; - raise Exception.createFMT('Any Find Function, you did not pass a ' + - 'correct ye: %d.', [ye]); -end; - -function TMFinder.CountColorTolerance(Color, xs, ys, xe, ye, Tolerance: Integer): Integer; -var - PtrData: TRetData; - Ptr: PRGB32; - PtrInc: Integer; - clR, clG, clB : byte; - dX, dY, xx, yy: Integer; - h,s,l,hmod,smod : extended; - Ccts : integer; -begin - Result := 0; - DefaultOperations(xs, ys, xe, ye); - dX := xe - xs; - dY := ye - ys; - ColorToRGB(Color, clR, clG, clB); - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - Ptr := PtrData.Ptr; - PtrInc := PtrData.IncPtrWith; - CCts := Self.CTS; - result := 0; - if cts = 2 then - begin; - RGBToHSL(clR,clG,clB,h,s,l); - hmod := Self.hueMod; - smod := Self.satMod; - end; - for yy := ys to ye do - begin; - for xx := xs to xe do - begin; - if ColorSame(CCts,Tolerance,clR,clG,clB,Ptr^.r,Ptr^.g,Ptr^.b,H,S,L,hmod,smod) then - inc(result); - Inc(Ptr); - end; - Inc(Ptr, PtrInc) - end; - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindColor(var x, y: Integer; Color, xs, ys, xe, ye: Integer): Boolean; -var - PtrData: TRetData; - Ptr: PRGB32; - PtrInc: Integer; - dX, dY, clR, clG, clB, xx, yy: Integer; - -begin - Result := false; - // checks for valid xs,ys,xe,ye? (may involve GetDimensions) - DefaultOperations(xs,ys,xe,ye); - - // calculate delta x and y - dX := xe - xs; - dY := ye - ys; - - //next, convert the color to r,g,b - ColorToRGB(Color, clR, clG, clB); - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - - // Do we want to "cache" these vars? - // We will, for now. Easier to type. - Ptr := PtrData.Ptr; - PtrInc := PtrData.IncPtrWith; - - for yy := ys to ye do - begin; - for xx := xs to xe do - begin; - // Colour comparison here. Possibly with tolerance? ;) - if (Ptr^.R = clR) and (Ptr^.G = clG) and (Ptr^.B = clB) then - begin - Result := True; - x := xx; - y := yy; - - TClient(Client).MWindow.FreeReturnData; - Exit; - end; - Inc(Ptr); - end; - Inc(Ptr, PtrInc) - end; - - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindColorSpiral(var x, y: Integer; color, xs, ys, xe, - ye: Integer): Boolean; -var - PtrData: TRetData; - RowData : TPRGB32Array; - dX, dY, clR, clG, clB, i,HiSpiral: Integer; - -begin - Result := false; - // checks for valid xs,ys,xe,ye? (may involve GetDimensions) - DefaultOperations(xs,ys,xe,ye); - - // calculate delta x and y - dX := xe - xs; - dY := ye - ys; - - //next, convert the color to r,g,b - ColorToRGB(Color, clR, clG, clB); - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - //Load rowdata - RowData:= CalculateRowPtrs(ptrdata,dy+1); - //Load the spiral path - LoadSpiralPath(x-xs,y-ys,0,0,dx,dy); - - - HiSpiral := (dy+1) * (dx + 1) -1; - for i := 0 to HiSpiral do - if (RowData[ClientTPA[i].y][ClientTPA[i].x].R = clR) and (RowData[ClientTPA[i].y][ClientTPA[i].x].G = clG) - and (RowData[ClientTPA[i].y][ClientTPA[i].x].B = clB) then - begin - Result := True; - x := ClientTPA[i].x + xs; - y := ClientTPA[i].y + ys; - TClient(Client).MWindow.FreeReturnData; - Exit; - end; - - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindColorTolerance(var x, y: Integer; Color, xs, ys, xe, ye, tol: Integer): Boolean; -var - PtrData: TRetData; - Ptr: PRGB32; - PtrInc: Integer; - dX, dY, clR, clG, clB, xx, yy: Integer; - H1, S1, L1, H2, S2, L2: Extended; - - label Hit; - label Miss; - -begin - Result := false; - // checks for valid xs,ys,xe,ye? (may involve GetDimensions) - DefaultOperations(xs,ys,xe,ye); - - // calculate delta x and y - dX := xe - xs; - dY := ye - ys; - //next, convert the color to r,g,b - ColorToRGB(Color, clR, clG, clB); - ColorToHSL(Color, H1, S1, L1); - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - - // Do we want to "cache" these vars? - // We will, for now. Easier to type. - Ptr := PtrData.Ptr; - PtrInc := PtrData.IncPtrWith; - - case CTS of - 0: - for yy := ys to ye do - begin - for xx := xs to xe do - begin - if ((abs(clB-Ptr^.B) <= Tol) and (abs(clG-Ptr^.G) <= Tol) and (Abs(clR-Ptr^.R) <= Tol)) then - goto Hit; - inc(Ptr); - end; - Inc(Ptr, PtrInc); - end; - - 1: - begin - Tol := Sqr(Tol); - - for yy := ys to ye do - begin - for xx := xs to xe do - begin - if (sqr(clB - Ptr^.B) + sqr(clG - Ptr^.G) + sqr(clR-Ptr^.R)) <= Tol then - goto Hit; - inc(ptr); - end; - Inc(Ptr, PtrInc); - end; - - end; - 2: - // Can be optimized a lot... RGBToHSL isn't really inline, - // and hueMod * tol is also calculated every time. - begin - for yy := ys to ye do - for xx := xs to xe do - begin - RGBToHSL(Ptr^.R,Ptr^.G,Ptr^.B,H2,S2,L2); - if ((abs(H1 - H2) <= (hueMod * tol)) and (abs(S1 - S2) <= (satMod * tol)) and (abs(L1 - L2) <= Tol)) then - goto Hit; - inc(Ptr); - end; - Inc(Ptr, PtrInc); - end; - end; - Result := False; - TClient(Client).MWindow.FreeReturnData; - Exit; - - Hit: - Result := True; - x := xx; - y := yy; - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindColorsTolerance(var Points: TPointArray; Color, xs, ys, - xe, ye, Tol: Integer): Boolean; -var - PtrData: TRetData; - Ptr: PRGB32; - PtrInc,C: Integer; - dX, dY, clR, clG, clB, xx, yy: Integer; - H1, S1, L1, H2, S2, L2: Extended; -begin - Result := false; - DefaultOperations(xs,ys,xe,ye); - - dX := xe - xs; - dY := ye - ys; - //next, convert the color to r,g,b - ColorToRGB(Color, clR, clG, clB); - ColorToHSL(Color, H1, S1, L1); - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - - // Do we want to "cache" these vars? - // We will, for now. Easier to type. - Ptr := PtrData.Ptr; - PtrInc := PtrData.IncPtrWith; - c := 0; - case CTS of - 0: - for yy := ys to ye do - begin - for xx := xs to xe do - begin - if ((abs(clB-Ptr^.B) <= Tol) and (abs(clG-Ptr^.G) <= Tol) and (Abs(clR-Ptr^.R) <= Tol)) then - begin; - ClientTPA[c].x := xx; - ClientTPA[c].y := yy; - inc(c); - end; - inc(Ptr); - end; - Inc(Ptr, PtrInc); - end; - - 1: - for yy := ys to ye do - begin - for xx := xs to xe do - begin - if (Sqrt(sqr(clR-Ptr^.R) + sqr(clG - Ptr^.G) + sqr(clB - Ptr^.B)) <= Tol) then - begin; - ClientTPA[c].x := xx; - ClientTPA[c].y := yy; - inc(c); - end; - inc(ptr); - end; - Inc(Ptr, PtrInc); - end; - 2: - begin - for yy := ys to ye do - for xx := xs to xe do - begin - RGBToHSL(Ptr^.R,Ptr^.G,Ptr^.B,H2,S2,L2); - if ((abs(H1 - H2) <= (hueMod * tol)) and (abs(S1 - S2) <= (satMod * tol)) and (abs(L1 - L2) <= Tol)) then - begin; - ClientTPA[c].x := xx; - ClientTPA[c].y := yy; - inc(c); - end; - inc(Ptr); - end; - Inc(Ptr, PtrInc); - end; - end; - SetLength(Points, C); - Move(ClientTPA[0], Points[0], C * SizeOf(TPoint)); - Result := C > 0; - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindColorsSpiralTolerance(x, y: Integer; - var Points: TPointArray; color, xs, ys, xe, ye: Integer; Tolerance: Integer - ): boolean; -var - PtrData: TRetData; - c : integer; - RowData : TPRGB32Array; - dX, dY, clR, clG, clB, i,SpiralHi: Integer; - H1, S1, L1, H2, S2, L2: Extended; -begin - Result := false; - DefaultOperations(xs,ys,xe,ye); - - dX := xe - xs; - dY := ye - ys; - //next, convert the color to r,g,b - ColorToRGB(Color, clR, clG, clB); - ColorToHSL(Color, H1, S1, L1); - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - - c := 0; - - //Load rowdata - RowData:= CalculateRowPtrs(ptrdata,dy+1); - //Load the spiral path - LoadSpiralPath(x-xs,y-ys,0,0,dx,dy); - SpiralHi := (dx + 1) * (dy + 1) - 1; - case CTS of - 0: - for i := 0 to SpiralHi do - if ((abs(clB-RowData[ClientTPA[i].y][ClientTPA[i].x].B) <= Tolerance) and - (abs(clG-RowData[ClientTPA[i].y][ClientTPA[i].x].G) <= Tolerance) and - (Abs(clR-RowData[ClientTPA[i].y][ClientTPA[i].x].R) <= Tolerance)) then - begin; - ClientTPA[c].x := ClientTPA[i].x + xs; - ClientTPA[c].y := ClientTPA[i].y + ys; - inc(c); - end; - - - 1: - for i := 0 to SpiralHi do - if (Sqrt(sqr(clR - RowData[ClientTPA[i].y][ClientTPA[i].x].R) + - sqr(clG - RowData[ClientTPA[i].y][ClientTPA[i].x].G) + - sqr(clB - RowData[ClientTPA[i].y][ClientTPA[i].x].B)) <= Tolerance) then - begin; - ClientTPA[c].x := ClientTPA[i].x + xs; - ClientTPA[c].y := ClientTPA[i].y + ys; - inc(c); - end; - - 2: - for i := 0 to SpiralHi do - begin; - RGBToHSL(RowData[ClientTPA[i].y][ClientTPA[i].x].R, - RowData[ClientTPA[i].y][ClientTPA[i].x].G, - RowData[ClientTPA[i].y][ClientTPA[i].x].B,H2,S2,L2); - if ((abs(H1 - H2) <= (hueMod * Tolerance)) and (abs(S1 - S2) <= (satMod * Tolerance)) and (abs(L1 - L2) <= Tolerance)) then - begin; - ClientTPA[c].x := ClientTPA[i].x + xs; - ClientTPA[c].y := ClientTPA[i].y + ys; - inc(c); - end; - end; - end; - SetLength(Points, C); - Move(ClientTPA[0], Points[0], C * SizeOf(TPoint)); - Result := C > 0; - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindColors(var TPA: TPointArray; Color, xs, ys, xe, ye: Integer): Boolean; -var - PtrData: TRetData; - Ptr: PRGB32; - PtrInc: Integer; - dX, dY, clR, clG, clB, xx, yy, i: Integer; - -begin - Result := false; - DefaultOperations(xs,ys,xe,ye); - - dX := xe - xs; - dY := ye - ys; - - I := 0; - - ColorToRGB(Color, clR, clG, clB); - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - - Ptr := PtrData.Ptr; - PtrInc := PtrData.IncPtrWith; - - for yy := ys to ye do - begin; - for xx := xs to xe do - begin; - if (Ptr^.R = clR) and (Ptr^.G = clG) and (Ptr^.B = clB) then - begin - Self.ClientTPA[I].x := xx; - Self.ClientTPA[i].y := yy; - Inc(I); - end; - Inc(Ptr); - end; - Inc(Ptr, PtrInc); - end; - - SetLength(TPA, I); - - Move(ClientTPA[0], TPA[0], i * SizeOf(TPoint)); - - Result := I > 0; - - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindBitmap(bitmap: TMufasaBitmap; var x, y: Integer): Boolean; -var - w,h : integer; -begin - TClient(Client).MWindow.GetDimensions(w,h); - result := Self.FindBitmapIn(bitmap,x,y,0,0,w-1,h-1); -end; - -function TMFinder.FindBitmapIn(bitmap: TMufasaBitmap; var x, y: Integer; xs, - ys, xe, ye: Integer): Boolean; -var - MainRowdata : TPRGB32Array; - BmpRowData : TPRGB32Array; - PtrData : TRetData; - BmpW,BmpH : integer; - xBmp,yBmp : integer; - tmpY : integer; - dX, dY, xx, yy: Integer; -label NotFoundBmp; - //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. - -begin - Result := false; - // checks for valid xs,ys,xe,ye? (may involve GetDimensions) - DefaultOperations(xs,ys,xe,ye); - - // calculate delta x and y - dX := xe - xs; - dY := ye - ys; - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - //Caculate the row ptrs - MainRowdata:= CalculateRowPtrs(PtrData,dy+1); - BmpRowData:= CalculateRowPtrs(bitmap); - //Get the 'fixed' bmp size - BmpW := bitmap.Width - 1; - BmpH := bitmap.Height - 1; - //Heck our bitmap cannot be outside the search area - dX := dX - bmpW; - dY := dY - bmpH; - for yy := 0 to dY do - for xx := 0 to dX do - begin; - for yBmp:= 0 to BmpH do - begin; - tmpY := yBmp + yy; - for xBmp := 0 to BmpW do - if (BmpRowData[yBmp][xBmp].R <> MainRowdata[tmpY][xBmp + xx].R) or - (BmpRowData[yBmp][xBmp].G <> MainRowdata[tmpY][xBmp + xx].G) or - (BmpRowData[yBmp][xBmp].B <> MainRowdata[tmpY][xBmp + xx].B) then - goto NotFoundBmp; - - end; - //We did find the Bmp, otherwise we would be at the part below - TClient(Client).MWindow.FreeReturnData; - x := xx + xs; - y := yy + ys; - result := true; - exit; - NotFoundBmp: - end; - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindBitmapToleranceIn(bitmap: TMufasaBitmap; var x, y: Integer; xs, - ys, xe, ye: Integer; tolerance: Integer): Boolean; -var - MainRowdata : TPRGB32Array; - BmpRowData : TPRGB32Array; - PtrData : TRetData; - BmpW,BmpH : integer; - xBmp,yBmp : integer; - tmpY : integer; - dX, dY, xx, yy: Integer; - CCTS : integer; - H,S,L,HMod,SMod : extended; -label NotFoundBmp; - //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. - -begin - Result := false; - // checks for valid xs,ys,xe,ye? (may involve GetDimensions) - DefaultOperations(xs,ys,xe,ye); - - // calculate delta x and y - dX := xe - xs; - dY := ye - ys; - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - //Caculate the row ptrs - MainRowdata:= CalculateRowPtrs(PtrData,dy+1); - BmpRowData:= CalculateRowPtrs(bitmap); - //Get the 'fixed' bmp size - BmpW := bitmap.Width - 1; - BmpH := bitmap.Height - 1; - //Heck our bitmap cannot be outside the search area - dX := dX - bmpW; - dY := dY - bmpH; - //We wont want HSL comparison with BMPs, right? Not for now atleast. - CCTS := Self.CTS; - if CCTS > 1 then - CCTS := 1; - for yy := 0 to dY do - for xx := 0 to dX do - begin; - for yBmp:= 0 to BmpH do - begin; - tmpY := yBmp + yy; - for xBmp := 0 to BmpW do - if not ColorSame(CCTS,tolerance, - BmpRowData[yBmp][xBmp].R,BmpRowData[yBmp][xBmp].G,BmpRowData[yBmp][xBmp].B, - MainRowdata[tmpY][xBmp + xx].R,MainRowdata[tmpY][xBmp + xx].G,MainRowdata[tmpY][xBmp + xx].B, - H,S,L,HMod,SMod) then - goto NotFoundBmp; - - end; - //We did find the Bmp, otherwise we would be at the part below - TClient(Client).MWindow.FreeReturnData; - x := xx + xs; - y := yy + ys; - result := true; - exit; - NotFoundBmp: - end; - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindBitmapSpiral(bitmap: TMufasaBitmap; var x, y: Integer; - xs, ys, xe, ye: Integer): Boolean; -var - MainRowdata : TPRGB32Array; - BmpRowData : TPRGB32Array; - PtrData : TRetData; - BmpW,BmpH : integer; - xBmp,yBmp : integer; - tmpY : integer; - dX, dY, i,HiSpiral: Integer; -label NotFoundBmp; - //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. - -begin - Result := false; - // checks for valid xs,ys,xe,ye? (may involve GetDimensions) - DefaultOperations(xs,ys,xe,ye); - - // calculate delta x and y - dX := xe - xs; - dY := ye - ys; - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - //Caculate the row ptrs - MainRowdata:= CalculateRowPtrs(PtrData,dy+1); - BmpRowData:= CalculateRowPtrs(bitmap); - //Get the 'fixed' bmp size - BmpW := bitmap.Width - 1; - BmpH := bitmap.Height - 1; - //Heck, our bitmap cannot be outside the search area - dX := dX - bmpW; - dY := dY - bmpH; - //Load the spiral into memory - LoadSpiralPath(x-xs,y-ys,0,0,dX,dY); - HiSpiral := (dx+1) * (dy+1) - 1; - for i := 0 to HiSpiral do - begin; - for yBmp:= 0 to BmpH do - begin; - tmpY := yBmp + ClientTPA[i].y; - for xBmp := 0 to BmpW do - if (BmpRowData[yBmp][xBmp].R <> MainRowdata[tmpY][xBmp + ClientTPA[i].x].R) or - (BmpRowData[yBmp][xBmp].G <> MainRowdata[tmpY][xBmp + ClientTPA[i].x].G) or - (BmpRowData[yBmp][xBmp].B <> MainRowdata[tmpY][xBmp + ClientTPA[i].x].B) then - goto NotFoundBmp; - - end; - //We did find the Bmp, otherwise we would be at the part below - TClient(Client).MWindow.FreeReturnData; - x := ClientTPA[i].x + xs; - y := ClientTPA[i].y + ys; - result := true; - exit; - NotFoundBmp: - end; - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindBitmapSpiralTolerance(bitmap: TMufasaBitmap; var x, - y: Integer; xs, ys, xe, ye, tolerance: integer): Boolean; -var - MainRowdata : TPRGB32Array; - BmpRowData : TPRGB32Array; - PtrData : TRetData; - BmpW,BmpH : integer; - xBmp,yBmp : integer; - tmpY : integer; - dX, dY, i,HiSpiral: Integer; - CCTS : integer; - H,S,L,HMod,SMod : extended; -label NotFoundBmp; - //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. - -begin - Result := false; - // checks for valid xs,ys,xe,ye? (may involve GetDimensions) - DefaultOperations(xs,ys,xe,ye); - - // calculate delta x and y - dX := xe - xs; - dY := ye - ys; - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - //Caculate the row ptrs - MainRowdata:= CalculateRowPtrs(PtrData,dy+1); - BmpRowData:= CalculateRowPtrs(bitmap); - //Get the 'fixed' bmp size - BmpW := bitmap.Width - 1; - BmpH := bitmap.Height - 1; - //Heck, our bitmap cannot be outside the search area - dX := dX - bmpW; - dY := dY - bmpH; - //Load the spiral into memory - LoadSpiralPath(x-xs,y-ys,0,0,dX,dY); - HiSpiral := (dx+1) * (dy+1) - 1; - //NO HSL. - CCTS := Self.CTS; - if CCTS > 1 then - CCTS := 1; - for i := 0 to HiSpiral do - begin; - for yBmp:= 0 to BmpH do - begin; - tmpY := yBmp + ClientTPA[i].y; - for xBmp := 0 to BmpW do - if not ColorSame(CCTS,tolerance, - BmpRowData[yBmp][xBmp].R,BmpRowData[yBmp][xBmp].G,BmpRowData[yBmp][xBmp].B, - MainRowdata[tmpY][xBmp + ClientTPA[i].x].R,MainRowdata[tmpY][xBmp + ClientTPA[i].x].G, - MainRowdata[tmpY][xBmp + ClientTPA[i].x].B, - H,S,L,HMod,SMod) then - goto NotFoundBmp; - - end; - //We did find the Bmp, otherwise we would be at the part below - x := ClientTPA[i].x + xs; - y := ClientTPA[i].y + ys; - result := true; - exit; - NotFoundBmp: - end; - TClient(Client).MWindow.FreeReturnData; -end; - -function TMFinder.FindBitmapsSpiralTolerance(bitmap: TMufasaBitmap; x, - y: Integer; var Points: TPointArray; xs, ys, xe, ye,tolerance: Integer): Boolean; -var - MainRowdata : TPRGB32Array; - BmpRowData : TPRGB32Array; - PtrData : TRetData; - BmpW,BmpH : integer; - xBmp,yBmp : integer; - tmpY : integer; - dX, dY, i,HiSpiral: Integer; - FoundC : integer; - CCTS : integer; - H,S,L,HMod,SMod : extended; -label NotFoundBmp; - //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. - -begin - Result := false; - // checks for valid xs,ys,xe,ye? (may involve GetDimensions) - DefaultOperations(xs,ys,xe,ye); - - // calculate delta x and y - dX := xe - xs; - dY := ye - ys; - - PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); - //Caculate the row ptrs - MainRowdata:= CalculateRowPtrs(PtrData,dy+1); - BmpRowData:= CalculateRowPtrs(bitmap); - //Get the 'fixed' bmp size - BmpW := bitmap.Width - 1; - BmpH := bitmap.Height - 1; - //Heck, our bitmap cannot be outside the search area - dX := dX - bmpW; - dY := dY - bmpH; - //Load the spiral into memory - LoadSpiralPath(x-xs,y-ys,0,0,dX,dY); - HiSpiral := (dx+1) * (dy+1) - 1; - //NO HSL. - CCTS := Self.CTS; - if CCTS > 1 then - CCTS := 1; - FoundC := 0; - for i := 0 to HiSpiral do - begin; - for yBmp:= 0 to BmpH do - begin; - tmpY := yBmp + ClientTPA[i].y; - for xBmp := 0 to BmpW do - if not ColorSame(CCTS,tolerance, - BmpRowData[yBmp][xBmp].R,BmpRowData[yBmp][xBmp].G,BmpRowData[yBmp][xBmp].B, - MainRowdata[tmpY][xBmp + ClientTPA[i].x].R,MainRowdata[tmpY][xBmp + ClientTPA[i].x].G, - MainRowdata[tmpY][xBmp + ClientTPA[i].x].B, - H,S,L,HMod,SMod) then - goto NotFoundBmp; - - end; - //We did find the Bmp, otherwise we would be at the part below - ClientTPA[FoundC].x := ClientTPA[i].x + xs; - ClientTPA[FoundC].y := ClientTPA[i].y + ys; - inc(FoundC); - NotFoundBmp: - end; - if FoundC > 0 then - begin; - result := true; - SetLength(Points,FoundC); - Move(ClientTPA[0], Points[0], FoundC * SizeOf(TPoint)); - end; - TClient(Client).MWindow.FreeReturnData; -end; - -end. - +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + Finder class for the Mufasa Macro Library +} + +unit finder; + +{$mode objfpc}{$H+} + +interface + +uses + Classes, SysUtils,bitmaps, MufasaTypes; // Types + +{ TMFinder Class } + +{ + Should be 100% independant, as all platform dependant code is in the + Window and Input classes. + + Let's try not to use any OS-specific defines here? ;) +} + +type + TMFinder = class(TObject) + constructor Create(aClient: TObject); + destructor Destroy; override; + private + Procedure UpdateCachedValues(NewWidth,NewHeight : integer); + procedure DefaultOperations(var xs,ys,xe,ye : integer); + //Loads the Spiral into ClientTPA (Will not cause problems) + procedure LoadSpiralPath(startX, startY, x1, y1, x2, y2: Integer); + public + function CountColorTolerance(Color, xs, ys, xe, ye, Tolerance: Integer): Integer; + procedure SetToleranceSpeed(nCTS: Integer); + function SimilarColors(Color1,Color2,Tolerance : Integer) : boolean; + // Possibly turn x, y into a TPoint var. + function FindColor(var x, y: Integer; Color, xs, ys, xe, ye: Integer): Boolean; + function FindColorSpiral(var x, y: Integer; color, xs, ys, xe, ye: Integer): Boolean; + function FindColorTolerance(var x, y: Integer; Color, xs, ys, xe, ye, tol: Integer): Boolean; + function FindColorsTolerance(var Points: TPointArray; Color, xs, ys, xe, ye, Tol: Integer): Boolean; + function FindColorsSpiralTolerance(x, y: Integer; var Points: TPointArray; color, xs, ys, xe, ye: Integer; Tolerance: Integer) : boolean; + function FindColors(var TPA: TPointArray; Color, xs, ys, xe, ye: Integer): Boolean; + //Bitmap functions + function FindBitmap(bitmap: TMufasaBitmap; var x, y: Integer): Boolean; + function FindBitmapIn(bitmap: TMufasaBitmap; var x, y: Integer; xs, ys, xe, ye: Integer): Boolean; + function FindBitmapToleranceIn(bitmap: TMufasaBitmap; var x, y: Integer; xs, ys, xe, ye: Integer; tolerance: Integer): Boolean; + function FindBitmapSpiral(bitmap: TMufasaBitmap; var x, y: Integer; xs, ys, xe, ye: Integer): Boolean; + function FindBitmapSpiralTolerance(bitmap: TMufasaBitmap; var x, y: Integer; xs, ys, xe, ye,tolerance : integer): Boolean; + function FindBitmapsSpiralTolerance(bitmap: TMufasaBitmap; x, y: Integer; var Points : TPointArray; xs, ys, xe, ye,tolerance: Integer): Boolean; + protected + Client: TObject; + CachedWidth, CachedHeight : integer; + ClientTPA : TPointArray; + hueMod, satMod: Extended; + CTS: Integer; + end; + +implementation +uses + Client, // For the Client Casts. + colour_conv // For RGBToColor, etc. + ; +type + TPRGB32Array = array of PRGB32; + +procedure TMFinder.LoadSpiralPath(startX, startY, x1, y1, x2, y2: Integer); +var + i,y,x,c,Ring : integer; + CurrBox : TBox; +begin; + i := 0; + Ring := 1; + c := 0; + CurrBox.x1 := Startx-1; + CurrBox.y1 := Starty-1; + CurrBox.x2 := Startx+1; + CurrBox.y2 := Starty+1; + if (startx >= x1) and (startx <= x2) and (starty >= y1) and (starty <= y2) then + begin; + ClientTPA[c] := Point(Startx, StartY); + inc(c); + end; + Repeat + if (CurrBox.x2 >= x1) and (CurrBox.x1 <= x2) and (Currbox.y1 >= y1) and (Currbox.y1 <= y2) then + for i := CurrBox.x1 + 1 to CurrBox.x2 do + if (I >= x1) and ( I <= x2) then + begin; + ClientTPA[c] := Point(i,CurrBox.y1); + inc(c); + end; + if (CurrBox.x2 >= x1) and (CurrBox.x2 <= x2) and (Currbox.y2 >= y1) and (Currbox.y1 <= y2) then + for i := CurrBox.y1 + 1 to CurrBox.y2 do + if (I >= y1) and ( I <= y2) then + begin; + ClientTPA[c] := Point(Currbox.x2, I); + inc(c); + end; + if (CurrBox.x2 >= x1) and (CurrBox.x1 <= x2) and (Currbox.y2 >= y1) and (Currbox.y2 <= y2) then + for i := CurrBox.x2 - 1 downto CurrBox.x1 do + if (I >= x1) and ( I <= x2) then + begin; + ClientTPA[c] := Point(i,CurrBox.y2); + inc(c); + end; + if (CurrBox.x1 >= x1) and (CurrBox.x1 <= x2) and (Currbox.y2 >= y1) and (Currbox.y1 <= y2) then + for i := CurrBox.y2 - 1 downto CurrBox.y1 do + if (I >= y1) and ( I <= y2) then + begin; + ClientTPA[c] := Point(Currbox.x1, I); + inc(c); + end; + inc(ring); + CurrBox.x1 := Startx-ring; + CurrBox.y1 := Starty-Ring; + CurrBox.x2 := Startx+Ring; + CurrBox.y2 := Starty+Ring; + until (Currbox.x1 < x1) and (Currbox.x2 > x2) and (currbox.y1 < y1) and (currbox.y2 > y2); +end; + +function CalculateRowPtrs(ReturnData : TRetData; RowCount : integer) : TPRGB32Array;overload; +var + I : integer; +begin; + setlength(result,RowCount); + for i := 0 to RowCount - 1do + begin; + result[i] := ReturnData.Ptr; + inc(ReturnData.Ptr,ReturnData.IncPtrWith); + end; +end; + +function CalculateRowPtrs(Bitmap : TMufasaBitmap) : TPRGB32Array;overload; +var + I : integer; +begin; + setlength(result,Bitmap.Height); + for i := 0 to Bitmap.Height - 1 do + result[i] := Bitmap.FData + Bitmap.Width; +end; + +constructor TMFinder.Create(aClient: TObject); + +begin + inherited Create; + + Self.Client := aClient; + Self.CTS := 1; + Self.hueMod := 0.2; + Self.satMod := 0.2; + +end; + +destructor TMFinder.Destroy; +begin + + inherited; +end; + +procedure TMFinder.SetToleranceSpeed(nCTS: Integer); +begin + if (nCTS < 0) or (nCTS > 2) then + raise Exception.CreateFmt('The given CTS ([%d]) is invalid.',[nCTS]); + Self.CTS := nCTS; +end; + +function TMFinder.SimilarColors(Color1, Color2,Tolerance: Integer) : boolean; +var + R1,G1,B1,R2,G2,B2 : Byte; + H1,S1,L1,H2,S2,L2 : extended; +begin + Result := False; + ColorToRGB(Color1,R1,G1,B1); + ColorToRGB(Color2,R2,G2,B2); + if Color1 = Color2 then + Result := true + else + case CTS of + 0: Result := ((Abs(R1-R2) <= Tolerance) and (Abs(G1-G2) <= Tolerance) and (Abs(B1-B2) <= Tolerance)); + 1: Result := (Sqrt(sqr(R1-R2) + sqr(G1-G2) + sqr(B1-B2)) <= Tolerance); + 2: begin + RGBToHSL(R1,g1,b1,H1,S1,L1); + RGBToHSL(R2,g2,b2,H2,S2,L2); + Result := ((abs(H1 - H2) <= (hueMod * Tolerance)) and (abs(S2-S1) <= (satMod * Tolerance)) and (abs(L1-L2) <= Tolerance)); + end; + end; +end; + + +function ColorSame(var CTS,Tolerance : Integer; var R1,G1,B1,R2,G2,B2 : byte; var H1,S1,L1,huemod,satmod : extended) : boolean; inline; +var + H2,S2,L2 : extended; +begin + Result := False; + case CTS of + 0: Result := ((Abs(R1-R2) <= Tolerance) and (Abs(G1-G2) <= Tolerance) and (Abs(B1-B2) <= Tolerance)); + 1: Result := (Sqrt(sqr(R1-R2) + sqr(G1-G2) + sqr(B1-B2)) <= Tolerance); + 2: begin + RGBToHSL(R1,g1,b1,H1,S1,L1); + RGBToHSL(R2,g2,b2,H2,S2,L2); + Result := ((abs(H1 - H2) <= (hueMod * Tolerance)) and (abs(S2-S1) <= (satMod * Tolerance)) and (abs(L1-L2) <= Tolerance)); + end; + end; +end; + +procedure TMFinder.UpdateCachedValues(NewWidth, NewHeight: integer); +begin + CachedWidth := NewWidth; + CachedHeight := NewHeight; + SetLength(ClientTPA,NewWidth * NewHeight); +end; + +procedure TMFinder.DefaultOperations(var xs, ys, xe, ye: integer); +var + w,h : integer; +begin +{ if xs > xe then + Swap(xs,xe); + if ys > ye then + Swap(ys,ye);} + if xs < 0 then + // xs := 0; + raise Exception.createFMT('Any Find Function, you did not pass a ' + + 'correct xs: %d.', [xs]); + if ys < 0 then +// ys := 0; + raise Exception.createFMT('Any Find Function, you did not pass a ' + + 'correct ys: %d.', [ys]); + + TClient(Self.Client).MWindow.GetDimensions(w,h); + if (w <> CachedWidth) or (h <> CachedHeight) then + UpdateCachedValues(w,h); + if xe >= w then +// xe := w-1; + raise Exception.createFMT('Any Find Function, you did not pass a ' + + 'correct xe: %d.', [xe]); + if ye >= h then +// ye := h-1; + raise Exception.createFMT('Any Find Function, you did not pass a ' + + 'correct ye: %d.', [ye]); +end; + +function TMFinder.CountColorTolerance(Color, xs, ys, xe, ye, Tolerance: Integer): Integer; +var + PtrData: TRetData; + Ptr: PRGB32; + PtrInc: Integer; + clR, clG, clB : byte; + dX, dY, xx, yy: Integer; + h,s,l,hmod,smod : extended; + Ccts : integer; +begin + Result := 0; + DefaultOperations(xs, ys, xe, ye); + dX := xe - xs; + dY := ye - ys; + ColorToRGB(Color, clR, clG, clB); + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + Ptr := PtrData.Ptr; + PtrInc := PtrData.IncPtrWith; + CCts := Self.CTS; + result := 0; + if cts = 2 then + begin; + RGBToHSL(clR,clG,clB,h,s,l); + hmod := Self.hueMod; + smod := Self.satMod; + end; + for yy := ys to ye do + begin; + for xx := xs to xe do + begin; + if ColorSame(CCts,Tolerance,clR,clG,clB,Ptr^.r,Ptr^.g,Ptr^.b,H,S,L,hmod,smod) then + inc(result); + Inc(Ptr); + end; + Inc(Ptr, PtrInc) + end; + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindColor(var x, y: Integer; Color, xs, ys, xe, ye: Integer): Boolean; +var + PtrData: TRetData; + Ptr: PRGB32; + PtrInc: Integer; + dX, dY, clR, clG, clB, xx, yy: Integer; + +begin + Result := false; + // checks for valid xs,ys,xe,ye? (may involve GetDimensions) + DefaultOperations(xs,ys,xe,ye); + + // calculate delta x and y + dX := xe - xs; + dY := ye - ys; + + //next, convert the color to r,g,b + ColorToRGB(Color, clR, clG, clB); + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + + // Do we want to "cache" these vars? + // We will, for now. Easier to type. + Ptr := PtrData.Ptr; + PtrInc := PtrData.IncPtrWith; + + for yy := ys to ye do + begin; + for xx := xs to xe do + begin; + // Colour comparison here. Possibly with tolerance? ;) + if (Ptr^.R = clR) and (Ptr^.G = clG) and (Ptr^.B = clB) then + begin + Result := True; + x := xx; + y := yy; + + TClient(Client).MWindow.FreeReturnData; + Exit; + end; + Inc(Ptr); + end; + Inc(Ptr, PtrInc) + end; + + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindColorSpiral(var x, y: Integer; color, xs, ys, xe, + ye: Integer): Boolean; +var + PtrData: TRetData; + RowData : TPRGB32Array; + dX, dY, clR, clG, clB, i,HiSpiral: Integer; + +begin + Result := false; + // checks for valid xs,ys,xe,ye? (may involve GetDimensions) + DefaultOperations(xs,ys,xe,ye); + + // calculate delta x and y + dX := xe - xs; + dY := ye - ys; + + //next, convert the color to r,g,b + ColorToRGB(Color, clR, clG, clB); + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + //Load rowdata + RowData:= CalculateRowPtrs(ptrdata,dy+1); + //Load the spiral path + LoadSpiralPath(x-xs,y-ys,0,0,dx,dy); + + + HiSpiral := (dy+1) * (dx + 1) -1; + for i := 0 to HiSpiral do + if (RowData[ClientTPA[i].y][ClientTPA[i].x].R = clR) and (RowData[ClientTPA[i].y][ClientTPA[i].x].G = clG) + and (RowData[ClientTPA[i].y][ClientTPA[i].x].B = clB) then + begin + Result := True; + x := ClientTPA[i].x + xs; + y := ClientTPA[i].y + ys; + TClient(Client).MWindow.FreeReturnData; + Exit; + end; + + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindColorTolerance(var x, y: Integer; Color, xs, ys, xe, ye, tol: Integer): Boolean; +var + PtrData: TRetData; + Ptr: PRGB32; + PtrInc: Integer; + dX, dY, clR, clG, clB, xx, yy: Integer; + H1, S1, L1, H2, S2, L2: Extended; + + label Hit; + label Miss; + +begin + Result := false; + // checks for valid xs,ys,xe,ye? (may involve GetDimensions) + DefaultOperations(xs,ys,xe,ye); + + // calculate delta x and y + dX := xe - xs; + dY := ye - ys; + //next, convert the color to r,g,b + ColorToRGB(Color, clR, clG, clB); + ColorToHSL(Color, H1, S1, L1); + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + + // Do we want to "cache" these vars? + // We will, for now. Easier to type. + Ptr := PtrData.Ptr; + PtrInc := PtrData.IncPtrWith; + + case CTS of + 0: + for yy := ys to ye do + begin + for xx := xs to xe do + begin + if ((abs(clB-Ptr^.B) <= Tol) and (abs(clG-Ptr^.G) <= Tol) and (Abs(clR-Ptr^.R) <= Tol)) then + goto Hit; + inc(Ptr); + end; + Inc(Ptr, PtrInc); + end; + + 1: + begin + Tol := Sqr(Tol); + + for yy := ys to ye do + begin + for xx := xs to xe do + begin + if (sqr(clB - Ptr^.B) + sqr(clG - Ptr^.G) + sqr(clR-Ptr^.R)) <= Tol then + goto Hit; + inc(ptr); + end; + Inc(Ptr, PtrInc); + end; + + end; + 2: + // Can be optimized a lot... RGBToHSL isn't really inline, + // and hueMod * tol is also calculated every time. + begin + for yy := ys to ye do + for xx := xs to xe do + begin + RGBToHSL(Ptr^.R,Ptr^.G,Ptr^.B,H2,S2,L2); + if ((abs(H1 - H2) <= (hueMod * tol)) and (abs(S1 - S2) <= (satMod * tol)) and (abs(L1 - L2) <= Tol)) then + goto Hit; + inc(Ptr); + end; + Inc(Ptr, PtrInc); + end; + end; + Result := False; + TClient(Client).MWindow.FreeReturnData; + Exit; + + Hit: + Result := True; + x := xx; + y := yy; + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindColorsTolerance(var Points: TPointArray; Color, xs, ys, + xe, ye, Tol: Integer): Boolean; +var + PtrData: TRetData; + Ptr: PRGB32; + PtrInc,C: Integer; + dX, dY, clR, clG, clB, xx, yy: Integer; + H1, S1, L1, H2, S2, L2: Extended; +begin + Result := false; + DefaultOperations(xs,ys,xe,ye); + + dX := xe - xs; + dY := ye - ys; + //next, convert the color to r,g,b + ColorToRGB(Color, clR, clG, clB); + ColorToHSL(Color, H1, S1, L1); + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + + // Do we want to "cache" these vars? + // We will, for now. Easier to type. + Ptr := PtrData.Ptr; + PtrInc := PtrData.IncPtrWith; + c := 0; + case CTS of + 0: + for yy := ys to ye do + begin + for xx := xs to xe do + begin + if ((abs(clB-Ptr^.B) <= Tol) and (abs(clG-Ptr^.G) <= Tol) and (Abs(clR-Ptr^.R) <= Tol)) then + begin; + ClientTPA[c].x := xx; + ClientTPA[c].y := yy; + inc(c); + end; + inc(Ptr); + end; + Inc(Ptr, PtrInc); + end; + + 1: + for yy := ys to ye do + begin + for xx := xs to xe do + begin + if (Sqrt(sqr(clR-Ptr^.R) + sqr(clG - Ptr^.G) + sqr(clB - Ptr^.B)) <= Tol) then + begin; + ClientTPA[c].x := xx; + ClientTPA[c].y := yy; + inc(c); + end; + inc(ptr); + end; + Inc(Ptr, PtrInc); + end; + 2: + begin + for yy := ys to ye do + for xx := xs to xe do + begin + RGBToHSL(Ptr^.R,Ptr^.G,Ptr^.B,H2,S2,L2); + if ((abs(H1 - H2) <= (hueMod * tol)) and (abs(S1 - S2) <= (satMod * tol)) and (abs(L1 - L2) <= Tol)) then + begin; + ClientTPA[c].x := xx; + ClientTPA[c].y := yy; + inc(c); + end; + inc(Ptr); + end; + Inc(Ptr, PtrInc); + end; + end; + SetLength(Points, C); + Move(ClientTPA[0], Points[0], C * SizeOf(TPoint)); + Result := C > 0; + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindColorsSpiralTolerance(x, y: Integer; + var Points: TPointArray; color, xs, ys, xe, ye: Integer; Tolerance: Integer + ): boolean; +var + PtrData: TRetData; + c : integer; + RowData : TPRGB32Array; + dX, dY, clR, clG, clB, i,SpiralHi: Integer; + H1, S1, L1, H2, S2, L2: Extended; +begin + Result := false; + DefaultOperations(xs,ys,xe,ye); + + dX := xe - xs; + dY := ye - ys; + //next, convert the color to r,g,b + ColorToRGB(Color, clR, clG, clB); + ColorToHSL(Color, H1, S1, L1); + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + + c := 0; + + //Load rowdata + RowData:= CalculateRowPtrs(ptrdata,dy+1); + //Load the spiral path + LoadSpiralPath(x-xs,y-ys,0,0,dx,dy); + SpiralHi := (dx + 1) * (dy + 1) - 1; + case CTS of + 0: + for i := 0 to SpiralHi do + if ((abs(clB-RowData[ClientTPA[i].y][ClientTPA[i].x].B) <= Tolerance) and + (abs(clG-RowData[ClientTPA[i].y][ClientTPA[i].x].G) <= Tolerance) and + (Abs(clR-RowData[ClientTPA[i].y][ClientTPA[i].x].R) <= Tolerance)) then + begin; + ClientTPA[c].x := ClientTPA[i].x + xs; + ClientTPA[c].y := ClientTPA[i].y + ys; + inc(c); + end; + + + 1: + for i := 0 to SpiralHi do + if (Sqrt(sqr(clR - RowData[ClientTPA[i].y][ClientTPA[i].x].R) + + sqr(clG - RowData[ClientTPA[i].y][ClientTPA[i].x].G) + + sqr(clB - RowData[ClientTPA[i].y][ClientTPA[i].x].B)) <= Tolerance) then + begin; + ClientTPA[c].x := ClientTPA[i].x + xs; + ClientTPA[c].y := ClientTPA[i].y + ys; + inc(c); + end; + + 2: + for i := 0 to SpiralHi do + begin; + RGBToHSL(RowData[ClientTPA[i].y][ClientTPA[i].x].R, + RowData[ClientTPA[i].y][ClientTPA[i].x].G, + RowData[ClientTPA[i].y][ClientTPA[i].x].B,H2,S2,L2); + if ((abs(H1 - H2) <= (hueMod * Tolerance)) and (abs(S1 - S2) <= (satMod * Tolerance)) and (abs(L1 - L2) <= Tolerance)) then + begin; + ClientTPA[c].x := ClientTPA[i].x + xs; + ClientTPA[c].y := ClientTPA[i].y + ys; + inc(c); + end; + end; + end; + SetLength(Points, C); + Move(ClientTPA[0], Points[0], C * SizeOf(TPoint)); + Result := C > 0; + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindColors(var TPA: TPointArray; Color, xs, ys, xe, ye: Integer): Boolean; +var + PtrData: TRetData; + Ptr: PRGB32; + PtrInc: Integer; + dX, dY, clR, clG, clB, xx, yy, i: Integer; + +begin + Result := false; + DefaultOperations(xs,ys,xe,ye); + + dX := xe - xs; + dY := ye - ys; + + I := 0; + + ColorToRGB(Color, clR, clG, clB); + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + + Ptr := PtrData.Ptr; + PtrInc := PtrData.IncPtrWith; + + for yy := ys to ye do + begin; + for xx := xs to xe do + begin; + if (Ptr^.R = clR) and (Ptr^.G = clG) and (Ptr^.B = clB) then + begin + Self.ClientTPA[I].x := xx; + Self.ClientTPA[i].y := yy; + Inc(I); + end; + Inc(Ptr); + end; + Inc(Ptr, PtrInc); + end; + + SetLength(TPA, I); + + Move(ClientTPA[0], TPA[0], i * SizeOf(TPoint)); + + Result := I > 0; + + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindBitmap(bitmap: TMufasaBitmap; var x, y: Integer): Boolean; +var + w,h : integer; +begin + TClient(Client).MWindow.GetDimensions(w,h); + result := Self.FindBitmapIn(bitmap,x,y,0,0,w-1,h-1); +end; + +function TMFinder.FindBitmapIn(bitmap: TMufasaBitmap; var x, y: Integer; xs, + ys, xe, ye: Integer): Boolean; +var + MainRowdata : TPRGB32Array; + BmpRowData : TPRGB32Array; + PtrData : TRetData; + BmpW,BmpH : integer; + xBmp,yBmp : integer; + tmpY : integer; + dX, dY, xx, yy: Integer; +label NotFoundBmp; + //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. + +begin + Result := false; + // checks for valid xs,ys,xe,ye? (may involve GetDimensions) + DefaultOperations(xs,ys,xe,ye); + + // calculate delta x and y + dX := xe - xs; + dY := ye - ys; + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + //Caculate the row ptrs + MainRowdata:= CalculateRowPtrs(PtrData,dy+1); + BmpRowData:= CalculateRowPtrs(bitmap); + //Get the 'fixed' bmp size + BmpW := bitmap.Width - 1; + BmpH := bitmap.Height - 1; + //Heck our bitmap cannot be outside the search area + dX := dX - bmpW; + dY := dY - bmpH; + for yy := 0 to dY do + for xx := 0 to dX do + begin; + for yBmp:= 0 to BmpH do + begin; + tmpY := yBmp + yy; + for xBmp := 0 to BmpW do + if (BmpRowData[yBmp][xBmp].R <> MainRowdata[tmpY][xBmp + xx].R) or + (BmpRowData[yBmp][xBmp].G <> MainRowdata[tmpY][xBmp + xx].G) or + (BmpRowData[yBmp][xBmp].B <> MainRowdata[tmpY][xBmp + xx].B) then + goto NotFoundBmp; + + end; + //We did find the Bmp, otherwise we would be at the part below + TClient(Client).MWindow.FreeReturnData; + x := xx + xs; + y := yy + ys; + result := true; + exit; + NotFoundBmp: + end; + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindBitmapToleranceIn(bitmap: TMufasaBitmap; var x, y: Integer; xs, + ys, xe, ye: Integer; tolerance: Integer): Boolean; +var + MainRowdata : TPRGB32Array; + BmpRowData : TPRGB32Array; + PtrData : TRetData; + BmpW,BmpH : integer; + xBmp,yBmp : integer; + tmpY : integer; + dX, dY, xx, yy: Integer; + CCTS : integer; + H,S,L,HMod,SMod : extended; +label NotFoundBmp; + //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. + +begin + Result := false; + // checks for valid xs,ys,xe,ye? (may involve GetDimensions) + DefaultOperations(xs,ys,xe,ye); + + // calculate delta x and y + dX := xe - xs; + dY := ye - ys; + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + //Caculate the row ptrs + MainRowdata:= CalculateRowPtrs(PtrData,dy+1); + BmpRowData:= CalculateRowPtrs(bitmap); + //Get the 'fixed' bmp size + BmpW := bitmap.Width - 1; + BmpH := bitmap.Height - 1; + //Heck our bitmap cannot be outside the search area + dX := dX - bmpW; + dY := dY - bmpH; + //We wont want HSL comparison with BMPs, right? Not for now atleast. + CCTS := Self.CTS; + if CCTS > 1 then + CCTS := 1; + for yy := 0 to dY do + for xx := 0 to dX do + begin; + for yBmp:= 0 to BmpH do + begin; + tmpY := yBmp + yy; + for xBmp := 0 to BmpW do + if not ColorSame(CCTS,tolerance, + BmpRowData[yBmp][xBmp].R,BmpRowData[yBmp][xBmp].G,BmpRowData[yBmp][xBmp].B, + MainRowdata[tmpY][xBmp + xx].R,MainRowdata[tmpY][xBmp + xx].G,MainRowdata[tmpY][xBmp + xx].B, + H,S,L,HMod,SMod) then + goto NotFoundBmp; + + end; + //We did find the Bmp, otherwise we would be at the part below + TClient(Client).MWindow.FreeReturnData; + x := xx + xs; + y := yy + ys; + result := true; + exit; + NotFoundBmp: + end; + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindBitmapSpiral(bitmap: TMufasaBitmap; var x, y: Integer; + xs, ys, xe, ye: Integer): Boolean; +var + MainRowdata : TPRGB32Array; + BmpRowData : TPRGB32Array; + PtrData : TRetData; + BmpW,BmpH : integer; + xBmp,yBmp : integer; + tmpY : integer; + dX, dY, i,HiSpiral: Integer; +label NotFoundBmp; + //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. + +begin + Result := false; + // checks for valid xs,ys,xe,ye? (may involve GetDimensions) + DefaultOperations(xs,ys,xe,ye); + + // calculate delta x and y + dX := xe - xs; + dY := ye - ys; + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + //Caculate the row ptrs + MainRowdata:= CalculateRowPtrs(PtrData,dy+1); + BmpRowData:= CalculateRowPtrs(bitmap); + //Get the 'fixed' bmp size + BmpW := bitmap.Width - 1; + BmpH := bitmap.Height - 1; + //Heck, our bitmap cannot be outside the search area + dX := dX - bmpW; + dY := dY - bmpH; + //Load the spiral into memory + LoadSpiralPath(x-xs,y-ys,0,0,dX,dY); + HiSpiral := (dx+1) * (dy+1) - 1; + for i := 0 to HiSpiral do + begin; + for yBmp:= 0 to BmpH do + begin; + tmpY := yBmp + ClientTPA[i].y; + for xBmp := 0 to BmpW do + if (BmpRowData[yBmp][xBmp].R <> MainRowdata[tmpY][xBmp + ClientTPA[i].x].R) or + (BmpRowData[yBmp][xBmp].G <> MainRowdata[tmpY][xBmp + ClientTPA[i].x].G) or + (BmpRowData[yBmp][xBmp].B <> MainRowdata[tmpY][xBmp + ClientTPA[i].x].B) then + goto NotFoundBmp; + + end; + //We did find the Bmp, otherwise we would be at the part below + TClient(Client).MWindow.FreeReturnData; + x := ClientTPA[i].x + xs; + y := ClientTPA[i].y + ys; + result := true; + exit; + NotFoundBmp: + end; + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindBitmapSpiralTolerance(bitmap: TMufasaBitmap; var x, + y: Integer; xs, ys, xe, ye, tolerance: integer): Boolean; +var + MainRowdata : TPRGB32Array; + BmpRowData : TPRGB32Array; + PtrData : TRetData; + BmpW,BmpH : integer; + xBmp,yBmp : integer; + tmpY : integer; + dX, dY, i,HiSpiral: Integer; + CCTS : integer; + H,S,L,HMod,SMod : extended; +label NotFoundBmp; + //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. + +begin + Result := false; + // checks for valid xs,ys,xe,ye? (may involve GetDimensions) + DefaultOperations(xs,ys,xe,ye); + + // calculate delta x and y + dX := xe - xs; + dY := ye - ys; + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + //Caculate the row ptrs + MainRowdata:= CalculateRowPtrs(PtrData,dy+1); + BmpRowData:= CalculateRowPtrs(bitmap); + //Get the 'fixed' bmp size + BmpW := bitmap.Width - 1; + BmpH := bitmap.Height - 1; + //Heck, our bitmap cannot be outside the search area + dX := dX - bmpW; + dY := dY - bmpH; + //Load the spiral into memory + LoadSpiralPath(x-xs,y-ys,0,0,dX,dY); + HiSpiral := (dx+1) * (dy+1) - 1; + //NO HSL. + CCTS := Self.CTS; + if CCTS > 1 then + CCTS := 1; + for i := 0 to HiSpiral do + begin; + for yBmp:= 0 to BmpH do + begin; + tmpY := yBmp + ClientTPA[i].y; + for xBmp := 0 to BmpW do + if not ColorSame(CCTS,tolerance, + BmpRowData[yBmp][xBmp].R,BmpRowData[yBmp][xBmp].G,BmpRowData[yBmp][xBmp].B, + MainRowdata[tmpY][xBmp + ClientTPA[i].x].R,MainRowdata[tmpY][xBmp + ClientTPA[i].x].G, + MainRowdata[tmpY][xBmp + ClientTPA[i].x].B, + H,S,L,HMod,SMod) then + goto NotFoundBmp; + + end; + //We did find the Bmp, otherwise we would be at the part below + x := ClientTPA[i].x + xs; + y := ClientTPA[i].y + ys; + result := true; + exit; + NotFoundBmp: + end; + TClient(Client).MWindow.FreeReturnData; +end; + +function TMFinder.FindBitmapsSpiralTolerance(bitmap: TMufasaBitmap; x, + y: Integer; var Points: TPointArray; xs, ys, xe, ye,tolerance: Integer): Boolean; +var + MainRowdata : TPRGB32Array; + BmpRowData : TPRGB32Array; + PtrData : TRetData; + BmpW,BmpH : integer; + xBmp,yBmp : integer; + tmpY : integer; + dX, dY, i,HiSpiral: Integer; + FoundC : integer; + CCTS : integer; + H,S,L,HMod,SMod : extended; +label NotFoundBmp; + //Don't know if the compiler has any speed-troubles with goto jumping in nested for loops. + +begin + Result := false; + // checks for valid xs,ys,xe,ye? (may involve GetDimensions) + DefaultOperations(xs,ys,xe,ye); + + // calculate delta x and y + dX := xe - xs; + dY := ye - ys; + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + //Caculate the row ptrs + MainRowdata:= CalculateRowPtrs(PtrData,dy+1); + BmpRowData:= CalculateRowPtrs(bitmap); + //Get the 'fixed' bmp size + BmpW := bitmap.Width - 1; + BmpH := bitmap.Height - 1; + //Heck, our bitmap cannot be outside the search area + dX := dX - bmpW; + dY := dY - bmpH; + //Load the spiral into memory + LoadSpiralPath(x-xs,y-ys,0,0,dX,dY); + HiSpiral := (dx+1) * (dy+1) - 1; + //NO HSL. + CCTS := Self.CTS; + if CCTS > 1 then + CCTS := 1; + FoundC := 0; + for i := 0 to HiSpiral do + begin; + for yBmp:= 0 to BmpH do + begin; + tmpY := yBmp + ClientTPA[i].y; + for xBmp := 0 to BmpW do + if not ColorSame(CCTS,tolerance, + BmpRowData[yBmp][xBmp].R,BmpRowData[yBmp][xBmp].G,BmpRowData[yBmp][xBmp].B, + MainRowdata[tmpY][xBmp + ClientTPA[i].x].R,MainRowdata[tmpY][xBmp + ClientTPA[i].x].G, + MainRowdata[tmpY][xBmp + ClientTPA[i].x].B, + H,S,L,HMod,SMod) then + goto NotFoundBmp; + + end; + //We did find the Bmp, otherwise we would be at the part below + ClientTPA[FoundC].x := ClientTPA[i].x + xs; + ClientTPA[FoundC].y := ClientTPA[i].y + ys; + inc(FoundC); + NotFoundBmp: + end; + if FoundC > 0 then + begin; + result := true; + SetLength(Points,FoundC); + Move(ClientTPA[0], Points[0], FoundC * SizeOf(TPoint)); + end; + TClient(Client).MWindow.FreeReturnData; +end; + +end. + diff --git a/Units/MMLCore/input.pas b/Units/MMLCore/input.pas index 3ab34a3..7f158cd 100644 --- a/Units/MMLCore/input.pas +++ b/Units/MMLCore/input.pas @@ -1,3 +1,26 @@ +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + Input Class for the Mufasa Macro Library +} + unit Input; {$mode objfpc}{$H+} diff --git a/Units/MMLCore/mmath.pas b/Units/MMLCore/mmath.pas index d0dd32a..68f3d7d 100644 --- a/Units/MMLCore/mmath.pas +++ b/Units/MMLCore/mmath.pas @@ -1,3 +1,26 @@ +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + Mufasa Math Unit for the Mufasa Macro Library +} + unit mmath; // mufasa math diff --git a/Units/MMLCore/mufasatypes.pas b/Units/MMLCore/mufasatypes.pas index 7f7a671..5b12689 100644 --- a/Units/MMLCore/mufasatypes.pas +++ b/Units/MMLCore/mufasatypes.pas @@ -1,3 +1,26 @@ +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + Type declarations for the Mufasa Macro Library +} + unit MufasaTypes; {$mode objfpc}{$H+} diff --git a/Units/MMLCore/window.pas b/Units/MMLCore/window.pas index 5976280..88d8472 100644 --- a/Units/MMLCore/window.pas +++ b/Units/MMLCore/window.pas @@ -1,3 +1,26 @@ +{ + This file is part of the Mufasa Macro Library (MML) + Copyright (c) 2009 by Raymond van Venentië and Merlijn Wajer + + MML is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + MML is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MML. If not, see <http://www.gnu.org/licenses/>. + + See the file COPYING, included in this distribution, + for details about the copyright. + + Window class for the Mufasa Macro Library +} + unit Window; {$mode objfpc}{$H+}