unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, jpeg, ExtCtrls, Menus, ComCtrls;
const
MaxPixelCount = 32768;
type
pRGBArray = ^TRGBArray;
TRGBArray = ARRAY[0..MaxPixelCount-1] OF TRGBTriple;
TForm1 = class(TForm)
Image1: TImage;
BitBtn1: TBitBtn;
MainMenu1: TMainMenu;
ggg1: TMenuItem;
StatusBar1: TStatusBar;
xcvb1: TMenuItem;
xcvbxc1: TMenuItem;
xcvbxc2: TMenuItem;
Button1: TButton;
Label1: TLabel;
Button2: TButton;
OpenDialog1: TOpenDialog;
Button3: TButton;
Button4: TButton;
Button5: TButton;
procedure ggg1Click(Sender: TObject);
procedure xcvb1Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
ProgressBar1:TProgressBar;
IsImageChanged:boolean;
filename:string;
function Min(a, b: integer): integer;
function Max(a, b: integer): integer;
procedure Blur(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure Blur5(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure NotColor(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
procedure Exposure(SrcBmp:Tbitmap;AProgressBar:TProgressBar);
function LoadImage(const InputImageFileName: string):boolean;
procedure JpgToBmp(const JpgFileName:string;AJpg:TJpegImage;Abmp:Tbitmap);
function IsJpgFile(const FileName:string):boolean;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Exposure(SrcBmp: Tbitmap; AProgressBar: TProgressBar);
var
i, j: integer;
SrcRow: pRGBArray;
ScanlineBytes:integer;
begin
ScanlineBytes:=SrcBmp.Width*3;
SrcRow := SrcBmp.ScanLine[0];
AProgressBar.Max:=SrcBmp.Height - 1;
for i := 0 to SrcBmp.Height - 1 do
begin
for j := 0 to SrcBmp.Width - 1 do
begin
SrcRow[j].rgbtRed :=(SrcRow[j].rgbtGreen+SrcRow[j].rgbtRed)div 2;
SrcRow[j].rgbtGreen :=(SrcRow[j].rgbtBlue+SrcRow[j].rgbtGreen)div 2;
SrcRow[j].rgbtBlue :=(SrcRow[j].rgbtRed+SrcRow[j].rgbtBlue)div 2;
end;
dec(integer(SrcRow), ScanlineBytes);
AProgressBar.Position:=i;
end;
end;
procedure TForm1.ggg1Click(Sender: TObject);
begin
if not (Sender is TMenuItem) then exit;
if Image1.Picture.Bitmap.Height=0 then exit;
ProgressBar1.Visible:=true;
case TMenuItem(Sender).tag of
0:NotColor(Image1.Picture.Bitmap,ProgressBar1);
1:Exposure(Image1.Picture.Bitmap,ProgressBar1);
2:showmessage('此功能对于jpg格式的文件尚存在bug!');
//2:Emboss(Image1.Picture.Bitmap,ProgressBar1);
// 3:Engrave(Image1.Picture.Bitmap,ProgressBar1);
//4:begin
// if (LowerCase(ExtractFileExt(FileName))='.jpg')
// or (LowerCase(ExtractFileExt(FileName))='.jpeg') then
// begin
// showmessage('此功能对于jpg格式的文件尚存在bug!');
// exit;
// end;
// Blur(Image1.Picture.Bitmap,ProgressBar1);
// end;
// 5:Sharpen(Image1.Picture.Bitmap,ProgressBar1);
// 6
urple(Image1.Picture.Bitmap,ProgressBar1);
// 7:Spooky(Image1.Picture.Bitmap,ProgressBar1);
end; //end case
// ProgressBar1.Visible:=false;
//form1.IsImageChanged:=true;
//self.Image1.Repaint;
// self.Repaint;
end;
procedure TForm1.NotColor(SrcBmp: Tbitmap; AProgressBar: TProgressBar);
var
i, j: integer;
SrcRow: pRGBArray;
ScanlineBytes:integer;
begin
ScanlineBytes:=SrcBmp.Width*3;
SrcRow := SrcBmp.ScanLine[0];
AProgressBar.Max:=SrcBmp.Height - 1;
for i := 0 to SrcBmp.Height - 1 do
begin
for j := 0 to SrcBmp.Width - 1 do
begin
SrcRow[j].rgbtRed :=not SrcRow[j].rgbtRed ;
SrcRow[j].rgbtGreen :=not SrcRow[j].rgbtGreen;
SrcRow[j].rgbtBlue :=not SrcRow[j].rgbtBlue;
end;
dec(integer(SrcRow), ScanlineBytes);
AProgressBar.Position:=i;
end;
end;
procedure TForm1.xcvb1Click(Sender: TObject);
begin
NotColor(Image1.Picture.Bitmap,ProgressBar1);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ProgressBar1.Visible:=true;
NotColor(Image1.Picture.Bitmap,ProgressBar1);
ProgressBar1.Visible:=false;
form1.IsImageChanged:=true;
self.Image1.Repaint;
self.Repaint;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ProgressBar1:=TProgressBar.Create(self);
ProgressBar1.Parent:=StatusBar1;
ProgressBar1.Visible:=false;
ProgressBar1.Width:=400;
ProgressBar1.Height:=StatusBar1.Height-4;
ProgressBar1.Left:=StatusBar1.Width-ProgressBar1.Width;
ProgressBar1.Top:=2;
end;
function TForm1.IsJpgFile(const FileName: string): boolean;
begin
result:=(LowerCase( ExtractFileExt(FileName))='.jpg') or (LowerCase( ExtractFileExt(FileName))='.jpeg');
end;
procedure TForm1.JpgToBmp(const JpgFileName: string; AJpg: TJpegImage;
Abmp: Tbitmap);
begin
try
AJpg.LoadFromFile(JpgFileName);
Abmp.Assign(AJpg);
finally
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(ParamStr(1));
label1.Caption := ParamStr(1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
OpenDialog1.Execute;
if OpenDialog1.FileName <> '' then
begin
filename:= OpenDialog1.FileName;
//image1.Picture.LoadFromFile(OpenDialog1.FileName);
LoadImage(FileName);
end;
end;
function TForm1.LoadImage(const InputImageFileName: string):boolean;
var
Bmp:Tbitmap;
jpg:Tjpegimage;
h:integer;
w:integer;
begin
jpg:=nil;
bmp:=Tbitmap.Create;
bmp.PixelFormat:=pf24bit;
//FOldBmp:=Tbitmap.Create;
//FOldBmp.PixelFormat:=pf24bit;
if IsJpgFile(InputImageFileName) then
begin
jpg:=Tjpegimage.Create;
JpgToBmp(InputImageFileName,Jpg,bmp);
end
else
Bmp.LoadFromFile(InputImageFileName);
//FOldBmp.Assign(bmp);
image1.Visible:=false;
image1.AutoSize:=false;
image1.Stretch:=true;
Image1.Picture.Bitmap.PixelFormat:=pf24bit;
Image1.Picture.Bitmap.Assign(bmp);
h:=500;
w:=600;
if (Bmp.Width<=h) and (Bmp.Height<=h) then
begin
Image1.Height:=Bmp.Height;
Image1.Width:=Bmp.Width;
end
else if Bmp.Height>=Bmp.Width then
begin
image1.Height:=h;
image1.Width:=image1.Height*Bmp.Width div Bmp.Height;
image1.Stretch:=true;
end
else if Bmp.Width>Bmp.Height then
begin
image1.Width:=h;
image1.Height:=image1.Width*Bmp.Height div Bmp.Width;
end;
//panel1.Width:=image1.Width;
//panel1.Height:=image1.Height;
image1.Left:=0;
image1.Top:=0;
//panel1.Top:=(h-panel1.Height)div 2;
// panel1.Left:=(w-panel1.Width)div 2;
image1.Visible:=true;
if assigned(jpg) then
begin
jpg.Free;
jpg:=nil;
end;
bmp.Free;
end;
function TForm1.Max(a, b: integer): integer;
begin
if a > b then result := a
else result := b;
end;
function TForm1.Min(a, b: integer): integer;
begin
if a < b then result := a
else result := b;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ProgressBar1.Visible:=true;
Exposure(Image1.Picture.Bitmap,ProgressBar1);
ProgressBar1.Visible:=false;
form1.IsImageChanged:=true;
self.Image1.Repaint;
self.Repaint;
end;
procedure TForm1.Blur(SrcBmp: Tbitmap; AProgressBar: TProgressBar);
var
i, j: integer;
SrcRow: pRGBArray;
SrcNextRow
RGBArray;
SrcPreRow
RGBArray;
Value:integer;
begin
AProgressBar.Max:=SrcBmp.Height - 2;
for i := 1 to SrcBmp.Height - 2 do
begin
SrcRow := SrcBmp.ScanLine
;
SrcPreRow:=SrcBmp.ScanLine[i-1];
SrcNextRow:=SrcBmp.ScanLine[i+1];
//DestRow := DestBmp.ScanLine;
// for each pixel in row
for j := 1 to SrcBmp.Width - 1 do
begin
// add brightness value to pixel's RGB values
// RGB values must be less than 256
Value:=(SrcPreRow[j-1].rgbtRed+SrcRow[j-1].rgbtRed+SrcNextRow[j-1].rgbtRed+
SrcPreRow[j].rgbtRed+SrcRow[j].rgbtRed+SrcNextRow[j].rgbtRed+
SrcPreRow[j+1].rgbtRed+SrcRow[j+1].rgbtRed+SrcNextRow[j+1].rgbtRed)div 9;
SrcRow[j].rgbtRed:=value;
Value:=(SrcPreRow[j-1].rgbtGreen+SrcRow[j-1].rgbtGreen+SrcNextRow[j-1].rgbtGreen+
SrcPreRow[j].rgbtGreen+SrcRow[j].rgbtGreen+SrcNextRow[j].rgbtGreen+
SrcPreRow[j+1].rgbtGreen+SrcRow[j+1].rgbtGreen+SrcNextRow[j+1].rgbtGreen)div 9;
SrcRow[j].rgbtGreen:=value;
Value:=(SrcPreRow[j-1].rgbtBlue+SrcRow[j-1].rgbtBlue+SrcNextRow[j-1].rgbtBlue+
SrcPreRow[j].rgbtBlue+SrcRow[j].rgbtBlue+SrcNextRow[j].rgbtBlue+
SrcPreRow[j+1].rgbtBlue+SrcRow[j+1].rgbtBlue+SrcNextRow[j+1].rgbtBlue)div 9;
SrcRow[j].rgbtBlue:=value;
end;
AProgressBar.Position:=i;
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ProgressBar1.Visible:=true;
Blur(Image1.Picture.Bitmap,ProgressBar1);
ProgressBar1.Visible:=false;
form1.IsImageChanged:=true;
self.Image1.Repaint;
self.Repaint;
end;
procedure TForm1.Blur5(SrcBmp: Tbitmap; AProgressBar: TProgressBar);
var
//i, j: integer;
// SrcRow: pRGBArray;
i, j: integer;
SrcRow: pRGBArray;
SrcPreRowRGBArray;
Value:integer;
begin
AProgressBar.Max:=SrcBmp.Height - 1;
for i := 1 to SrcBmp.Height - 1 do
begin
SrcRow := SrcBmp.ScanLine;
SrcPreRow:=SrcBmp.ScanLine[i-1];
//DestRow := DestBmp.ScanLine;
// for each pixel in row
for j := 0 to SrcBmp.Width - 1 do
begin
// add brightness value to pixel's RGB values
// RGB values must be less than 256
Value:=SrcRow[j].rgbtRed+(SrcRow[j].rgbtRed-SrcPreRow[j-1].rgbtRed)div 2;
Value:=Max(0,Value);
Value:=Min(255,Value);
SrcRow[j].rgbtRed:=value;
Value:=SrcRow[j].rgbtGreen+(SrcRow[j].rgbtGreen-SrcPreRow[j-1].rgbtGreen)div 2;
Value:=Max(0,Value);
Value:=Min(255,Value);
SrcRow[j].rgbtGreen:=value;
Value:=SrcRow[j].rgbtBlue+(SrcRow[j].rgbtBlue-SrcPreRow[j-1].rgbtBlue)div 2;
Value:=Max(0,Value);
Value:=Min(255,Value);
SrcRow[j].rgbtBlue:=value;
end;
AProgressBar.Position:=i;
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
ProgressBar1.Visible:=true;
Blur5(Image1.Picture.Bitmap,ProgressBar1);
ProgressBar1.Visible:=false;
form1.IsImageChanged:=true;
self.Image1.Repaint;
self.Repaint;
end;
end.