# BGRABitmap tutorial 5

Deutsch (de) | English (en) | Français (fr) | Español (es) | Edit

Home | Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 | Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 | Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 | Tutorial 16 | Edit

This tutorial shows you how to use layers.

### Create a new project

Create a new project and add a reference to BGRABitmap, the same way as in the first tutorial.

A mask is a grayscale image. When a mask is applied on an image, the parts of the image that overlap with black parts of the mask are erased and become transparent, whereas parts that overlap with white parts of the mask are kept. In other words, the mask is like an alpha channel the defines opacity. If the mask value is zero, then it becomes transparent, and if a mask value is 255, it becomes opaque.

In this example, the image, the start image is in the upper-left corner, the mask in the upper-right one, and the result when applying the mask in the lower-left corner.

Code in OnPaint :

var temp,tex,mask: TBGRABitmap;
begin
temp:= TBGRABitmap.Create(640,480,ColorToBGRA(ColorToRGB(clBtnFace)));

tex := TBGRABitmap.Create('texture.png');
BGRAReplace(tex,tex.Resample(128,80));

//show image in the upper-left corner
temp.PutImage(10,10,tex,dmDrawWithTransparency);

//create a mask with ellipse and rectangle

//show mask in the upper-right corner

//apply the mask to the image

//show the result image in the lower-left corner
temp.PutImage(10,100,tex,dmDrawWithTransparency);

tex.Free;

//show everything on the screen
temp.Draw(Canvas,0,0,True);
temp.Free;
end;

### Erasing parts of an image

Some functions allow to erase an ellipse, a rectangle etc. It means that the part of the image becomes transparent. It is thus possible to draw a hole in an image. If the alpha parameter is 255, the hole is completely transparent. If not, the hole is half-transparent.

Here an ellipse is erased on the left with an alpha parameter of 255, and another ellipse is erased on the right with an alpha parameter of 128.

Code in OnPaint :

var image,tex: TBGRABitmap;
begin
image := TBGRABitmap.Create(640,480,ColorToBGRA(ColorToRGB(clBtnFace)));

tex := TBGRABitmap.Create('texture.png');
BGRAReplace(tex,tex.Resample(128,80));

//show image
image.PutImage(10,10,tex,dmDrawWithTransparency);

//erase parts
tex.EraseEllipseAntialias(40,40,30,30,255);
tex.EraseEllipseAntialias(80,40,30,30,128);

//show result
image.PutImage(10,100,tex,dmDrawWithTransparency);

tex.Free;

//show everything on the screen
image.Draw(Canvas,0,0,True);
image.Free;
end;

With the object inspector, add an OnPaint handler and write :

procedure TForm1.FormPaint(Sender: TObject);
var image: TBGRABitmap;
size: single;

procedure DrawMoon;
var layer: TBGRABitmap;
begin
layer := TBGRABitmap.Create(image.Width,image.Height);
layer.FillEllipseAntialias(layer.Width/2,layer.Height/2,size*0.4,size*0.4,BGRA(224,224,224,128));
layer.EraseEllipseAntialias(layer.Width/2+size*0.15,layer.Height/2,size*0.3,size*0.3,255);
image.PutImage(0,0,layer,dmDrawWithTransparency);
layer.Free;
end;

begin
image := TBGRABitmap.Create(ClientWidth,ClientHeight);

//Compute available space in both directions
if image.Height < image.Width then
size := image.Height
else
size := image.Width;

BGRA(128,192,255),BGRA(0,0,255),
gtLinear,PointF(0,0),PointF(0,image.Height),
dmSet);

DrawMoon;

image.Draw(Canvas,0,0,True);
image.free;
end;

The procedure creates an image and fills it with a blue gradient. This is the background layer.

The procedure DrawMoon creates a layer, draws a moon in it. First a white disk is drawn, then a smaller disk is subtracted. Finally, this layer is merged with the background.

### Run the program

You should see a blue sky with a moon. When you resize the form, the image is resized accordingly.

### Add another layer with a sun

In the OnPaint event, add the following subprocedure :

  procedure DrawSun;
begin
layer := TBGRABitmap.Create(image.Width,image.Height);
BGRA(255,255,0),BGRA(255,0,0),
dmSet);
image.PutImage(0,0,layer,dmDrawWithTransparency);
layer.Free;
end;

This procedures creates a radial gradient of red and orange and applies a circular mask to it. This results in a colored disk. Finally, the layer is merged with the background.

Add a call to this procedure to draw it after the moon.

### Run the program

You should see a blue sky with a moon and a sun. When you resize the form, the image is resized accordingly.

Add the following subprocedure in the OnPaint event :

  procedure ApplyLight;
var layer: TBGRABitmap;
begin
layer := TBGRABitmap.Create(image.Width,image.Height);
BGRA(255,255,255),BGRA(64,64,64),
dmSet);
image.BlendImage(0,0,layer,boMultiply);
layer.Free;
end;

This procedure draws a layer with a white radial gradient. It is then applied to multiply the image.

### Resulting code

procedure TForm1.FormPaint(Sender: TObject);
var image: TBGRABitmap;
size: single;

procedure DrawMoon;
var layer: TBGRABitmap;
begin
layer := TBGRABitmap.Create(image.Width,image.Height);
layer.FillEllipseAntialias(layer.Width/2,layer.Height/2,size*0.4,size*0.4,BGRA(224,224,224,128));
layer.EraseEllipseAntialias(layer.Width/2+size*0.15,layer.Height/2,size*0.3,size*0.3,255);
image.PutImage(0,0,layer,dmDrawWithTransparency);
layer.Free;
end;

procedure DrawSun;
begin
layer := TBGRABitmap.Create(image.Width,image.Height);
BGRA(255,255,0),BGRA(255,0,0),
dmSet);
image.PutImage(0,0,layer,dmDrawWithTransparency);
layer.Free;
end;

procedure ApplyLight;
var layer: TBGRABitmap;
begin
layer := TBGRABitmap.Create(image.Width,image.Height);
BGRA(255,255,255),BGRA(64,64,64),
dmSet);
image.BlendImage(0,0,layer,boMultiply);
layer.Free;
end;

begin
image := TBGRABitmap.Create(ClientWidth,ClientHeight);

if image.Height < image.Width then
size := image.Height
else
size := image.Width;

BGRA(128,192,255),BGRA(0,0,255),
gtLinear,PointF(0,0),PointF(0,image.Height),
dmSet);

DrawMoon;
DrawSun;
ApplyLight;

image.Draw(Canvas,0,0,True);
image.free;
end;

### Run the program

You should see a blue sky with a moon and a sun, with a light effect. When you resize the form, the image is resized accordingly.