Recent Posts

Selamat datang di Coding Delphi Land Weblog kumpulan source code pemogram delphi

(Bukan maksud untuk menggurui tetapi marilah kita berbagi ilmu tuk perkembangan kemajuan teknologi kita

Minggu, 15 November 2009

Alpha Blending

unit BlendU;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, jpeg, ExtCtrls, Spin,math;

type
TForm1 = class(TForm)
Panel1: TPanel;
ImgMain: TImage;
ImgBackground: TImage;
ImgMaskMap: TImage;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
SpinEdit1: TSpinEdit;
SpinEdit2: TSpinEdit;
Label4: TLabel;
Label5: TLabel;
procedure TileBackground;
procedure DrawBlend;
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure FormPaint(Sender: TObject);
procedure SpinEdit2Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
BlendedBitmap: tbitmap;

implementation

{$R *.DFM}

procedure tform1.TileBackground;
var
x,y: integer;
// x,y -coordinates in height's and width's of the background
h,w: integer; // the width and height of the background image being tiled
begin
h:=imgBackground.height;
w:=imgBackground.Width;
for x:=0 to 5 do
for y:=0 to 5 do
BlendedBitmap.canvas.draw(x*w,y*h,imgBackground.picture.bitmap);
// draw the background in different locations
end;

procedure tform1.DrawBlend;
var
maskpba: pbytearray; // pointer to information in the mask bitmap
Blendpba: pbytearray; // pointer to information in blendedbitmap
Mainpba: pbytearray; // pointer to information in the main image being averaged with the background
mx,my: integer; // used to draw the blend in an area that won't try accessing information outside the images
x,y: integer; // coordinates of a pixel
x2: integer; // used to hold x shl 2
x3: integer; // used to hold x2 + left for the memory location in the
left,top: integer; // the left and top positions of the blended graphic

begin
TileBackGround; // clear blendedbitmap and tile the background image
Left:=SpinEdit1.Value;
top:=SpinEdit2.Value;
// get the input coordinates
mx:=Min(Min(BlendedBitmap.width-left,imgMaskMap.width),imgMain.width);
// get the smallest width of an image
my:=Min(Min(BlendedBitmap.Height-top,imgMaskMap.Height),imgMain.Height);
// get the smallest Height of an image
dec(mx); // subtract 1
dec(my); // subtract 1
left:=left shl 2; // now, instead of a # pixels from pixel 0, it is the number of bytes into the pointer
// it is better to do this here than in the loop
for y:=my downto 0 do // loop through the rows of pixels
begin
maskpba:=imgMaskMap.Picture.Bitmap.ScanLine[y];
Blendpba:=BlendedBitmap.ScanLine[y+top];
Mainpba:=imgMain.Picture.Bitmap.ScanLine[y];
// get the pointers to rows of pixels data
for x:=mx downto 0 do // loop through the various pixels in a row
begin
x2:=x shl 2;
x3:=x2+left;
// calculate the number of bytes into the pointer to the memory location of the pixel

// average the 2 colours together by a ratio determined by the Mask
// each part of the colour(red, green, and blue) is averaged separately
Blendpba[x3]:=(Mainpba[x2]*Maskpba[x2]+Blendpba[x3]*($FF-Maskpba[x2])) shr 8;
// average blue bytes
inc(x2);
inc(x3);
Blendpba[x3]:=(Mainpba[x2]*Maskpba[x2]+Blendpba[x3]*($FF-Maskpba[x2])) shr 8;
// average green bytes
inc(x2);
inc(x3);
Blendpba[x2+left]:=(Mainpba[x2]*Maskpba[x2]+Blendpba[x3]*($FF-Maskpba[x2])) shr 8;
// average red bytes
end;
end;
canvas.draw(panel1.width,0,blendedBitmap);
// draw the bitmap on the form
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
BlendedBitmap:=tbitmap.create;
imgMain.picture.bitmap.pixelformat:=pf32bit;
imgMaskMap.picture.bitmap.pixelformat:=pf32bit;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
BlendedBitmap.Height:=clientheight;
if panel1.width
BlendedBitmap.Width:=clientwidth-panel1.Width;
BlendedBitmap.pixelformat:=pf32bit;
DrawBlend;
Canvas.Draw(panel1.width,0,BlendedBitmap);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
Canvas.Draw(panel1.width,0,BlendedBitmap);
end;

procedure TForm1.SpinEdit2Change(Sender: TObject);
begin // also linked to spinedit1
drawblend;
end;

end.

0 komentar:

Posting Komentar