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
Sabtu, 14 November 2009
Image Merge
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtDlgs, StdCtrls, Math, JPeg;
type
TRGB = record
R : byte;
G : byte;
B : byte;
end;
TRGBArray = array[0..32767] of TRGB;
pRGBArray = ^TRGBArray;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ScrollBar1: TScrollBar;
Label1: TLabel;
Label2: TLabel;
OpenPictureDialog1: TOpenPictureDialog;
Label3: TLabel;
Button3: TButton;
SavePictureDialog1: TSavePictureDialog;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure MergeBitmaps(ARatio : Single);
procedure FormPaint(Sender: TObject);
procedure ScrollBar1Change(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
Bitmap1, Bitmap2, BitmapOut : TBitmap;
Scanlines1, Scanlines2, ScanlinesOut : array of pRGBArray;
Loaded1, Loaded2 : boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Bitmap1 := TBitmap.Create;
Bitmap2 := TBitmap.Create;
BitmapOut := TBitmap.Create;
BitmapOut.PixelFormat := pf24bit;
Loaded1 := False;
Loaded2 := False;
Canvas.Brush.Color := clBtnFace;
end;
procedure TForm1.Button1Click(Sender: TObject);
Var
Row : integer;
JPeg : TJPegImage;
filename, ext : string;
begin
if not OpenPictureDialog1.Execute then Exit;
Label1.Caption := OpenPictureDialog1.FileName;
filename := LowerCase(OpenPictureDialog1.FileName);
ext := ExtractFileExt(filename);
if (ext = '.jpg') or (ext = '.jpeg') then begin
JPeg := TJPegImage.Create;
JPeg.LoadFromFile(filename);
Bitmap1.Assign(JPeg);
JPeg.Free;
end else begin
Bitmap1.LoadFromFile(filename);
end;
Bitmap1.PixelFormat := pf24bit;
Loaded1 := True;
SetLength(Scanlines1, Bitmap1.Height);
for Row := 0 to Bitmap1.Height - 1 do
Scanlines1[Row] := Bitmap1.Scanline[Row];
if Loaded2 then begin
MergeBitmaps(ScrollBar1.Position/100);
Canvas.FillRect(ClientRect);
Label1.Repaint;
Label2.Repaint;
Label3.Repaint;
Canvas.Draw(20, 120, BitmapOut);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
Var
Row : integer;
JPeg : TJPegImage;
filename, ext : string;
begin
if not OpenPictureDialog1.Execute then Exit;
Label2.Caption := OpenPictureDialog1.FileName;
filename := LowerCase(OpenPictureDialog1.FileName);
ext := ExtractFileExt(filename);
if (ext = '.jpg') or (ext = '.jpeg') then begin
JPeg := TJPegImage.Create;
JPeg.LoadFromFile(filename);
Bitmap2.Assign(JPeg);
JPeg.Free;
end else begin
Bitmap2.LoadFromFile(filename);
end;
Bitmap2.PixelFormat := pf24bit;
Loaded2 := True;
SetLength(Scanlines2, Bitmap2.Height);
for Row := 0 to Bitmap2.Height - 1 do
Scanlines2[Row] := Bitmap2.Scanline[Row];
if Loaded1 then begin
MergeBitmaps(ScrollBar1.Position/100);
Canvas.FillRect(ClientRect);
Label1.Repaint;
Label2.Repaint;
Label3.Repaint;
Canvas.Draw(20, 120, BitmapOut);
end;
end;
procedure TForm1.MergeBitmaps(ARatio : Single);
Var
x, y, W, H : integer;
Ratio, RatioMin : integer;
begin
Ratio := Round(ARatio * 256);
RatioMin := 256 - Ratio;
W := Min(Bitmap1.Width, Bitmap2.Width);
H := Min(Bitmap1.Height, Bitmap2.Height);
BitmapOut.Width := W;
BitmapOut.Height := H;
SetLength(ScanlinesOut, H);
for y := 0 to H-1 do ScanlinesOut[y] := BitmapOut.ScanLine[y];
for y := 0 to H-1 do begin
for x := 0 to W-1 do begin
ScanlinesOut[y][x].R := (Ratio*Scanlines1[y][x].R +
RatioMin*Scanlines2[y][x].R) shr 8;
ScanlinesOut[y][x].G := (Ratio*Scanlines1[y][x].G +
RatioMin*Scanlines2[y][x].G) shr 8;
ScanlinesOut[y][x].B := (Ratio*Scanlines1[y][x].B +
RatioMin*Scanlines2[y][x].B) shr 8;
end;
end;
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
if Loaded1 and Loaded2 then Canvas.Draw(20, 120, BitmapOut);
end;
procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
Label3.Caption := IntToStr(ScrollBar1.Position) + '%';
if not (Loaded1 and Loaded2) then Exit;
MergeBitmaps(ScrollBar1.Position/100);
Canvas.Draw(20, 120, BitmapOut);
end;
procedure TForm1.Button3Click(Sender: TObject);
Var
JPeg : TJPegImage;
filename, ext : string;
index : byte;
begin
if not (Loaded1 and Loaded2) then Exit;
if not SavePictureDialog1.Execute then Exit;
filename := LowerCase(SavePictureDialog1.FileName);
ext := ExtractFileExt(filename);
if length(ext) > 0 then SetLength(filename, Length(filename)-Length(ext));
index := SavePictureDialog1.FilterIndex;
if index = 2 then begin
JPeg := TJPegImage.Create;
JPeg.CompressionQuality := 90;
JPeg.Assign(BitmapOut);
JPeg.Compress;
JPeg.SaveToFile(filename + '.jpg');
JPeg.Free;
end else BitmapOut.SaveToFile(filename + '.bmp');
end;
end.
0 komentar:
Posting Komentar