summaryrefslogtreecommitdiff
path: root/examples/img/masktest/masktest.pas
blob: d9db3e6a06c5201d96949ae90cb1b19491f88905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
{
    fpGUI  -  Free Pascal GUI Library

    Example: Display BMP file with monochrome mask

    Copyright (C) 2000 - 2007 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    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.
}

program MaskTest;

{$ifdef fpc}
  {$mode objfpc}{$H+}
{$endif}

uses
  Classes, GFXBase, fpGFX, fpImg, BMPReader;

type
  TMainWindow = class(TFWindow)
  private
    Image2, Image4, Image8, Image24, Mask: TFBitmap;
    Image2Canvas, Image4Canvas, Image8Canvas,
      Image24Canvas, MaskCanvas: TFCanvas;
  protected
    procedure   Paint(Sender: TObject; const ARect: TRect);
  public
    constructor Create; overload;
    destructor  Destroy; override;
  end;

constructor TMainWindow.Create;
begin
  inherited Create(nil, [woWindow]);

  // Load and prepare the images
  Image2 := CreateImageFromFile(GFScreen, TBMPReader, 'image2.bmp');
  Image2Canvas := TFCanvas(GFScreen.CreateBitmapCanvas(Image2.Width, Image2.Height));
  Image2Canvas.DrawImage(Image2, Point(0, 0));

  Image4 := CreateImageFromFile(GFScreen, TBMPReader, 'image4.bmp');
  Image4Canvas := TFCanvas(GFScreen.CreateBitmapCanvas(Image4.Width, Image4.Height));
  Image4Canvas.DrawImage(Image4, Point(0, 0));

  Image8 := CreateImageFromFile(GFScreen, TBMPReader, 'image8.bmp');
  Image8Canvas := TFCanvas(GFScreen.CreateBitmapCanvas(Image8.Width, Image8.Height));
  Image8Canvas.DrawImage(Image8, Point(0, 0));

  Image24 := CreateImageFromFile(GFScreen, TBMPReader, 'image24.bmp');
  Image24Canvas := TFCanvas(GFScreen.CreateBitmapCanvas(Image24.Width, Image24.Height));
  Image24Canvas.DrawImage(Image24, Point(0, 0));

  // Load and prepare the image mask
  Mask := CreateImageFromFile(GFScreen, TBMPReader, 'mask.bmp');
  MaskCanvas := TFCanvas(GFScreen.CreateMonoBitmapCanvas(Mask.Width, Mask.Height));
//  MaskCanvas := Display.DefaultScreen.CreateMonoBitmap(Mask.Width, Mask.Height);
  MaskCanvas.DrawImage(Mask, Point(0, 0));

  Title := 'fpImg Blitting Mask Test';
  OnPaint := @Paint;
  SetClientSize(Size(Image2.Width * 2 + 64, Image2.Height * 2 + 64));
end;

destructor TMainWindow.Destroy;
begin
  MaskCanvas.Free;
  Mask.Free;
  Image24Canvas.Free;
  Image24.Free;
  Image8Canvas.Free;
  Image8.Free;
  Image4Canvas.Free;
  Image4.Free;
  Image2Canvas.Free;
  Image2.Free;
  inherited Destroy;
end;

procedure TMainWindow.Paint(Sender: TObject; const ARect: TRect);
var
  Color: TGfxColor;
  r: TRect;
  i, x1, y1, x2, y2: Integer;
begin
  Color.Red := 0;
  Color.Green := 0;
  Color.Alpha := 0;
  r.Left := ARect.Left;
  r.Right := ARect.Right;
  for i := ARect.Top to ARect.Bottom - 1 do
  begin
    Color.Blue := $ffff - (i * $ffff) div Height;
    Color.Red := Color.Blue shr 1;
    Canvas.SetColor(Color);
    r.Top := i;
    r.Bottom := i + 1;
    Canvas.FillRect(r);
  end;

  x1 := ClientWidth div 4 - Image2.Width div 2;
  y1 := ClientHeight div 4 - Image2.Height div 2;
  x2 := x1 + ClientWidth div 2;
  y2 := y1 + ClientHeight div 2;

  Canvas.SetColor(colWhite);
  Canvas.MaskedCopy(Image2Canvas, MaskCanvas, Point(x1, y1));
  Canvas.TextOut(Point(x1, y1 + Image2.Height), 'monochrome');
  Canvas.MaskedCopy(Image4Canvas, MaskCanvas, Point(x2, y1));
  Canvas.TextOut(Point(x2, y1 + Image2.Height), '4bpp palettized');
  Canvas.MaskedCopy(Image8Canvas, MaskCanvas, Point(x1, y2));
  Canvas.TextOut(Point(x1, y2 + Image2.Height), '8bpp palettized');
  Canvas.MaskedCopy(Image24Canvas, MaskCanvas, Point(x2, y2));
  Canvas.TextOut(Point(x2, y2 + Image2.Height), '24bpp true color');
end;

var
  MainWindow: TMainWindow;
begin
  GFApplication.Initialize;
  MainWindow := TMainWindow.Create;
  MainWindow.Show;
  GFApplication.Run;
end.