Phantom Documentation

[Help Home] [Phantom Home]
bitmap

The bitmap data type is used to store and compare bitmap images. Bitmaps can also be loaded from and saved to files. Phantom can capture a bitmap of a window and return it using the CaptureBitmap window member function.

The bitmap data type supports the following member functions:

blit
Clear
Find
GetColorAt
GetHeight
GetWidth
Invert
Load
ReplaceAll
ReplaceAllOther
Save
SetPixels

Bitmaps can also be compared using the equality ('==') operator. If every pixel matches between the two compared bitmaps, the equality returns true.

Example Code

# This script illustrates various bitmap functions. The
# results are saved for comparison.

# Include the PhantomTarget declarations file
use "PhantomTarget.dec";

# Start PhantomTarget
System("PhantomTarget.exe");

# Open the Button controls dialog
PhantomTarget._Controls.Pus_hButton.Select();

# Capture the bitmap of the entire controls window
bitmap All = Button.CaptureBitmap();
# Capture the bitmap of only the Graphic button
bitmap Graphic = Button.GBUTTON.CaptureBitmap();
# Capture the bitmap of the OK button
bitmap OK = Button.OK.CaptureBitmap();

# Close PhantomTarget
Button.Cancel.Click();
PhantomTarget.Close();

# Save the 3 bitmaps
All.Save("all.bmp");
Graphic.Save("graphic.bmp");
OK.Save("ok.bmp");

# Create temporary variables
bitmap B;
int X;
int Y;

# Load the Graphic button bitmap
B.Load("graphic.bmp");

# Get the width and height of B
# for use later.
int H = B.GetHeight();
int W = B.GetWidth();
disp("Height: " + string(H));
disp("Width: " + string(W));

# Compare B to Graphic... should be identical
if(B == Graphic){
  disp("B is the same as Graphic");
}

# Create a mask from B to copy only the 'target'
# pattern. First, the entire graphic will be inverted
# to turn all the white pixels to black, thus masking
# them out.
B.Invert();
B.Save("graphic_invert.bmp");

# Replace all non-black (0,0,0) colors in the mask with
# white (255,255,255). This will allow all the Graphic
# 'target' pixels to be copied.
B.ReplaceAllOther(0,0,0,255,255,255);

# Save for comparison
B.Save("graphic_mask.bmp");

# Now, the mask contained in B will be used to
# copy only the red target portion to tbe All
# bitmap, at pixel location 10, 120. Only
# the pixels with a corresponding mask pixel
# that is not black will be copied.
All.blit(10, 100, Graphic, B);
All.Save("all_blit.bmp");

# Now a green box will be placed in the lower
# left corner of the target Graphic. The box
# will fill the entire lower left quadrant
# of Graphic.
X[0] = 0;
X[1] = W/2;
Y[0] = 0;
Y[1] = H/2;
Graphic.SetPixels(X, Y, 0, 255, 0);
Graphic.Save("graphic_box.bmp");

# Now another blit will be performed with the
# new Graphic, using the same mask and placing
# the blit at 100,120. Notice that only the
# shape of the target was copied.
All.blit(100, 100, Graphic, B);
All.Save("all_blit_2.bmp");

# Now the OK button will be replaced by a red
# OK button. # First, find the location of the
# OK button in the All bitmap.
X = 0;
Y = 0;
if(!All.Find(X, Y, OK)){
  disp("OK button not found");
}

disp("OK X location: " + string(X));
disp("OK Y location: " + string(Y));

# Get the color of the gray area of OK
int Color = OK.GetColorAt(10,10);
disp("R,G,B color is: ");
disp(Color);

# Replace all gray in OK with red (255,0,0)
OK.ReplaceAll(Color[0], Color[1], Color[2], 255, 0, 0);
OK.Save("ok_red.bmp");

# Replace the gray OK bitmap with the red one
# using blit and the location found earlier.
# No mask is needed for this call.
All.blit(X, Y, OK);
All.Save("all_blit_3.bmp");

# Clear the bitmap just to illustrate the use
# of 'Clear'
All.Clear();
All.Save("all_clear.bmp");




See Also: CaptureBitmap, Variables


Copyright 2000-2011 Phantom Automated Solutions