Using Delphi to quickly find the color of a full screen image 4. Using bitmapdata. Pas

Source: Internet
Author: User

Iv. Use of bitmapdata. Pas
(Note: The bitmapdata. Pas file in the above compressed package has a small bgu, mainly because the current background color is not taken into consideration when the mouse pointer is captured. It is always black. The updated bitmapdata. Pas file is downloaded in the archive on the third floor of this post .)
The above compressed package is a demonstration of bitmapdata. Pas.ProgramThe bitmapdata. Pas file can be obtained from the compressed package. In the bitmapdata. Pas file, I encapsulate bitmap data into a class tbdbitmapdata for ease of use. In addition, I have compiled a series of functions for the construction, conversion, and fuzzy comparison of BGR colors. Note that some constants are defined in the bitmapdata. Pas file. These constants are only used to increase the readability of the program. modifying these constants will not modify the data format supported by the program, but will only cause program running errors. The bitmapdata. Pas file is described as follows:

1. Function BGR (B, G, R: byte): tbdcolor;
A bgr color is generated based on the values of the blue (B), Green (G), and red (r) channels.

2. Function rgbtobgr (C: tcolor): tbdcolor;
Convert an RGB color format to BGR color format.

3. Function bgrtorgb (C: tbdcolor): tcolor;
Convert a BGR color format to an RGB color format.

4. Function bdcomparecolor (C1, C2: tbdcolor; const range: tbdcolorrange): Boolean;
Compare the colors C1 and C2 according to the color range, and return whether c1 and c2 are similar. C1 and c2 are BGR colors, and range is the variation range of colors.

tbdcolorrange is defined as follows:
tbdcolorrange = record
r: integer;
G: integer;
B: integer;
end;
r indicates the maximum difference between the red channel in C1 and C2, and G indicates the maximum difference between the green channel in C1 and C2; B indicates the maximum difference between the blue channel in C1 and C2.

The demo program compares two colors:
VaR
C1, C2: tbdcolor;
Range: tbdcolorrange;
Begin
Range. R: = 5;
Range. G: = 5;
Range. B: = 5;
C1: = bgr( 125,125,125 );
C2: = BGR (120,120,120 );
Bdcomparecolor (C1, C2, range); // success
C1: = bgr( 125,120,125 );
C2: = BGR (120,120,120 );
Bdcomparecolor (C1, C2, range); // success
C1: = bgr( 125,200,125 );
C2: = BGR (120,120,120 );
Bdcomparecolor (C1, C2, range); // failed
End;

5. constructor tbdbitmapdata. Create (const aname: string );
Create an instance of the tbdbitmapdata object. You can specify a name for the instance for future management.

6. Procedure tbdbitmapdata. Clear;
Clears the bitmap data loaded in the current tbdbitmapdata object.

7. Function tbdbitmapdata. loadfromstream (Stream: tstream; abackcolor: tbdcolor): Boolean;
Imports bitmap data from a stream and returns whether it is successful. If it fails, the error member is set to indicate the situation. The data in the data stream must be in 24-bit BMP format. Abackcolor can be used to set the background color of an image. This parameter can be omitted, indicating that the image does not use the background color.

8. Function tbdbitmapdata. savetostream (Stream: tstream): Boolean;
Export the currently loaded bitmap data to the data stream to check whether the operation is successful. If it fails, the error member is set to indicate the situation. The data is exported to the data stream in the 24-bit BMP file format.

9. Function tbdbitmapdata. loadfromfile (const filename: string; abackcolor: tbdcolor): Boolean;
Import bitmap data from filename and return whether the operation is successful. If it fails, the error member is set to indicate the situation. The file must be in 24-bit BMP format. Abackcolor can be used to set the background color of an image. This parameter can be omitted, indicating that the image does not use the background color.

10. Function tbdbitmapdata. savetofile (const filename: string): Boolean;
Export the currently loaded bitmap data to a file and return whether it is successful. If it fails, the error member is set to indicate the situation. Data is exported to a file in 24-bit BMP format.

11. Function tbdbitmapdata. loadfrombitmap (Bitmap: tbitmap): Boolean;
Import data from a tbitmap object and return whether the result is successful. If it fails, the error member is set to indicate the situation. The background color of the imported image is determined by bitmap. Transparent and bitmap. transparentcolor.

12. Function tbdbitmapdata. savetobitmap (Bitmap: tbitmap): Boolean;
Export the current bitmap data to a tbitmap object and return whether it is successful. If it fails, the error member is set to indicate the situation. During export, bitmap. Transparent and bitmap. transparentcolor are set based on the current background color. The loadfrombitmap and savetobitmap functions can be used to convert the tbdbitmapdata object and the tbitmap object.

13. Function tbdbitmapdata. copyformscreen (left, top, awidth, aheight: integer): Boolean;
From the specified range on the screen, and import data, return whether it is successful. If it fails, the error member is set to indicate the situation. Left is the left margin, which can be omitted. The default value is 0. Top is the top margin, which can be omitted. The default value is 0. awidth is the width, which can be omitted, the default value is the width from left to the right of the screen. The height of aheight can be omitted. The default value is the height from top to the bottom of the screen.

14. Function tbdbitmapdata. copyformcursor: Boolean;
Take the image of the current pointer and import the data. If it fails, the error member is set to indicate the situation. If the mouse pointer is an animation pointer, the first frame is captured by default. The current background color is used to fill the background when capturing the image. If no background color is specified, the background color is white (RGB (255,255,255.

15. Function tbdbitmapdata. Compare (BMP: tbdbitmapdata; left, top: integer): Boolean;
16. Function tbdbitmapdata. Compare (BMP: tbdbitmapdata; const range: tbdcolorrange; left, top: integer): Boolean;
These two functions are used to compare the bitmap specified by BMP at the specified position of the current bitmap. No matter whether the comparison is consistent or not, the error member is not modified. The first function is used for exact comparison, and the second function is used for Fuzzy comparison. The bitmap area specified by BMP must be smaller than or equal to the current bitmap. The bitmap specified by BMP cannot exceed the current bitmap; otherwise, the comparison fails. BMP indicates the specified bitmap data. Left indicates the left margin for comparison, which can be omitted. The default value is 0. Top indicates the top margin for comparison, which can be omitted. The default value is 0; range is the color change range.

17. Function tbdbitmapdata. findimage (BMP: tbdbitmapdata; var left, top: integer): Boolean;
18. Function tbdbitmapdata. findimage (BMP: tbdbitmapdata; const range: tbdcolorrange; var left, top: integer): Boolean;
Reload the two functions to find the subgraph consistent with BMP from the current bitmap and return whether to find it. The error member is not modified whether or not it is found. The first function is used for exact comparison, and the second function is used for Fuzzy comparison. When searching, the left and top settings are ignored. From the upper left corner of the current bitmap, you can search from left to right and from top to bottom. Return true, set left and top to locate the subgraph, and return false if not. Set left and top to-1. BMP indicates the specified subgraph data, left indicates the left margin of the subgraph to be found, top indicates the top margin of the subgraph to be found, and range indicates the color change range.

The demo program searches for subgraphs on the screen:

var
bit1, bit2: tbdbitmapdata;
left, top: integer;
begin
bit1: = tbdbitmapdata. create;
bit2: = tbdbitmapdata. create;
bit1.copyformscreen;
bit2.loadfromfile ('filename ');
If bit1.findimage (bit2, left, top) then
begin
{the subgraphs have been found and processed accordingly ...}
end;
bit1.free;
bit2.free;
end;

19. Function tbdbitmapdata. findcenterimage (BMP: tbdbitmapdata; var left, top: integer): Boolean;
20. Function tbdbitmapdata. findcenterimage (BMP: tbdbitmapdata; const range: tbdcolorrange; var left, top: integer): Boolean;
reload two functions to find the subgraph consistent with BMP from the current bitmap, whether to find the returned result. The error member is not modified whether or not it is found. The first function is used for exact comparison, and the second function is used for Fuzzy comparison. The left and top settings are used as the base points for searching. Return true, set left and top to locate the subgraph, and return false if not. Set left and top to-1. BMP indicates the specified subgraph data, left indicates the left margin of the subgraph to be found, top indicates the top margin of the subgraph to be found, and range indicates the color change range.

21. Function tbdbitmapdata. enumimage (BMP: tbdbitmapdata; enumimageproc: tbdenumimageproc; lparam: integer): Boolean;
22. Function tbdbitmapdata. enumimage (BMP: tbdbitmapdata; const range: tbdcolorrange; enumimageproc: tbdenumimageproc; lparam: integer): Boolean;
Reload the two functions, from the current bitmap to find all the subgraph consistent with BMP, that is, the enumerated bitmap, returns whether to find. The error member is not modified whether or not it is found. The first function is used for exact comparison, and the second function is used for Fuzzy comparison. Search from the upper left corner of the current bitmap from left to right and from top to bottom. Every time a subgraph is found, the callback function enumimageproc is called. If enumimageproc returns false, the system stops searching and ends the function. BMP is the subgraph data; enumimageproc is the callback function; lparam is the parameter sent when the callback function is called, which can be omitted. The default value is 0; range is the color change range. The Declaration format of tbdenumimageproc is as follows:

Tbdenumimageproc = function (left, top: integer; BMP: tbdbitmapdata; lparam: integer): Boolean;

Left indicates the left margin of the subgraph to be found, top indicates the top margin of the subgraph to be found, BMP indicates the subgraph data to be searched when enumimage is called, and lparam indicates the Setting Parameter given when enumimage is called. The return value of this function indicates whether to continue enumeration.

23. Function tbdbitmapdata. findcolor (color: tbdcolor; var left, top: integer): Boolean;
24. Function tbdbitmapdata. findcolor (color: tbdcolor; const range: tbdcolorrange; var left, top: integer): Boolean;
Reload the two functions to find the specified color from the current bitmap, ignore the settings of the background color backcolor of the current bitmap, and return whether to find it. The error member is not modified whether or not it is found. The first function is used for exact comparison, and the second function is used for Fuzzy comparison. When searching, the left and top settings are ignored. From the upper left corner of the current bitmap, you can search from left to right and from top to bottom. Return true, set left and top to locate the color, and return false if not. Set left and top to-1. Color is BGR color, left is the left margin of the color, top is the top margin of the color, and range is the color change range.

25. Function tbdbitmapdata. findcentercolor (color: tbdcolor; var left, top: integer): Boolean;

26. Function tbdbitmapdata. findcentercolor (color: tbdcolor; const range: tbdcolorrange; var left, top: integer): Boolean;

reload the two functions to find the specified color from the current bitmap, ignore the backcolor settings of the current bitmap background color, and return whether to find the backcolor. The error member is not modified whether or not it is found. The first function is used for exact comparison, and the second function is used for Fuzzy comparison. The left and top settings are used as the base points for searching. Return true, set left and top to locate the color, and return false if not. Set left and top to-1. Color is BGR color, left is the left margin of the color, top is the top margin of the color, and range is the color change range.

the demo program uses a dot as the center to blur the color around the screen:
var
bit: tbdbitmapdata;
range: tbdcolorrange;
left, top: integer;
begin
bit: = tbdbitmapdata. create;
bit. copyformscreen;
range. r: = 5;
range. g: = 5;
range. b: = 5;
left: = 600;
top := 380;
If bit. findcentercolor (BGR (0,250,250), range, left, top) Then
begin
{color found, corresponding processing ...}
end;
bit. free;
end;

27. Function tbdbitmapdata. enumcolor (color: tbdcolor; enumcolorproc: tbdenumcolorproc; lparam: integer): Boolean;
28. Function tbdbitmapdata. enumcolor (color: tbdcolor; const range: tbdcolorrange; enumcolorproc: tbdenumcolorproc; lparam: integer): Boolean;
Reload the two functions to find all the specified colors from the current image, that is, enumeration colors, ignore the settings of the background color backcolor of the current bitmap, and return whether to find them. The error member is not modified whether or not it is found. The first function is used for exact comparison, and the second function is used for Fuzzy comparison. Search from the upper left corner of the current bitmap from left to right and from top to bottom. Every time a color is found, call the callback function enumcolorproc. If enumcolorproc returns false, stop searching and end the function. Color is in BGR format; enumcolorproc is the callback function; lparam is the parameter sent when the callback function is called, which can be omitted. The default value is 0; range is the color change range. The Declaration format of tbdenumcolorproc is as follows:

Tbdenumcolorproc = function (left, top: integer; color: tbdcolor; lparam: integer): Boolean;

Left indicates the left margin of the color to be found, top indicates the top margin of the color to be found, and color indicates the color to be found. When fuzzy search is used, the color is actually found; lparam is the Setting Parameter provided when enumcolor is called. The return value of this function indicates whether to continue enumeration.

29. tbdbitmapdata. Error
Description of the most recent operation error. In terms of performance, only import, export, and other operations can modify this member. The search, enumeration, and other operations will not modify this Member whether or not they are successful.

30. tbdbitmapdata. Name
The name of the current bitmap, which can be read and written. This facilitates the management of bitmap data.

31. tbdbitmapdata. Width
Current bitmap width, in pixels, read-only.

32. tbdbitmapdata. Height
The current bitmap height, in pixels, is read-only.

33. tbdbitmapdata. backcolor
The background color of the current bitmap, in BGR format. It can be read and written. When the color is bd_colorless, it indicates that the bitmap does not use the background color.

34. tbdbitmapdata. linewidth
The width of the bitmap data in each row after alignment, in bytes, read-only.

35. tbdbitmapdata. sparewidth
The Extra width of the bitmap data in each row after alignment. The unit is byte and read-only.

36. tbdbitmapdata. Size
The length of bitmap data, in bytes. It is read-only.

37. tbdbitmapdata. Bits
Bitmap data buffer pointer, read-only. This pointer is read-only, but it points to data that can be read and written. This attribute can be viewed as a one-dimensional byte array, which can be used to access and modify data in the buffer zone.

38. tbdbitmapdata. pixels [left, top: integer]
The pixel color of the bitmap. It is in BGR format and can be read and written. This attribute can be used to regard bitmap as a two-dimensional Pixel matrix, allowing users to access and modify the pixel color in the matrix.

DemonstrationCode, Bitmap data access:
VaR
Bit: tbdbitmapdata;
Begin
Bit: = tbdbitmapdata. Create;
Bit. copyformscreen;
 
Bit. Bits [50]; // access in byte format
 
Bit. pixels [10, 10]; // access in BGR color format
 
Bit [10, 10]; // equivalent to bit. pixels [10, 10];
 
Bit. Free;
End;

Use the tbdbitmapdata object to find the differences between the two images. From the upper right corner, use the double-layer loop to traverse all the pixel points on the two images and compare them with each other. The incomplete code is as follows:

Procedure tform1.button5click (Sender: tobject );
VaR
BMP 1, BMP 2: tbdbitmapdata;
Left, top: integer;
Isexit: Boolean;
Begin
BMP 1: = tbdbitmapdata. Create;
BMP 2: = tbdbitmapdata. Create;

BMP 1.loadfromfile ('file name 1 ');
BMP 2.loadfromfile ('file name 2 ');

// Assume that the two images are the same size.

Isexit: = false;
For top: = 0 to BMP 1.height-1 do
Begin
For left: = 0 to BMP 1.width-1 do
Begin
If BMP 1 [left, top] <> BMP 2 [left, top] Then
Begin
// The two images are different at the (left, top) position.

// Processing...

If {if you do not want to continue searching for other then}
begin
isexit: = true; // used to exit the loop
break;
end;
end;
If isexit then break;
end;

BMP 1.free;
BMP 2.free;
End;

The above code is incomplete and can be modified as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.