Delphi基本圖像處理方法匯總
本文實(shí)例匯總了Delphi基本圖像處理方法。分享給大家供大家參考。具體分析如下:
//浮雕
procedure Emboss(SrcBmp,DestBmp:TBitmap;AzimuthChange:integer);overload;
var
i, j, Gray, Azimuthvalue, R, G, B: integer;
SrcRGB, SrcRGB1, SrcRGB2, DestRGB: pRGBTriple;
begin
for i := 0 to SrcBmp.Height - 1 do
begin
SrcRGB := SrcBmp.ScanLine[i];
DestRGB := DestBmp.ScanLine[i];
if (AzimuthChange >= -180) and (AzimuthChange < -135) then
begin
if i > 0 then
SrcRGB1 := SrcBmp.ScanLine[i-1]
else
SrcRGB1 := SrcRGB;
Inc(SrcRGB1);
SrcRGB2 := SrcRGB;
Inc(SrcRGB2);
end
else if (AzimuthChange >= -135) and (AzimuthChange < -90) then
begin
if i > 0 then
SrcRGB1 := SrcBmp.ScanLine[i-1]
else
SrcRGB1 := SrcRGB;
SrcRGB2 := SrcRGB1;
Inc(SrcRGB2);
end
else if (AzimuthChange >= -90) and (AzimuthChange < -45) then
begin
if i > 0 then
SrcRGB1 := SrcBmp.ScanLine[i-1]
else
SrcRGB1 := SrcRGB;
SrcRGB2 := SrcRGB1;
end
else if (AzimuthChange >= -45) and (AzimuthChange < 0) then
begin
SrcRGB1 := SrcRGB;
if i > 0 then
SrcRGB2 := SrcBmp.ScanLine[i-1]
else
SrcRGB2 := SrcRGB;
end
else if (AzimuthChange >= 0) and (AzimuthChange < 45) then
begin
SrcRGB2 := SrcRGB;
if (i < SrcBmp.Height - 1) then
SrcRGB1 := SrcBmp.ScanLine[i+1]
else
SrcRGB1 := SrcRGB;
end
else if (AzimuthChange >= 45) and (AzimuthChange < 90) then
begin
if (i < SrcBmp.Height - 1) then
SrcRGB1 := SrcBmp.ScanLine[i+1]
else
SrcRGB1 := SrcRGB;
SrcRGB2 := SrcRGB1;
end
else if (AzimuthChange >= 90) and (AzimuthChange < 135) then
begin
if (i < SrcBmp.Height - 1) then
SrcRGB1 := SrcBmp.ScanLine[i+1]
else
SrcRGB1 := SrcRGB;
SrcRGB2 := SrcRGB1;
Inc(SrcRGB1);
end
else if (AzimuthChange >= 135) and (AzimuthChange <= 180) then
begin
if (i < SrcBmp.Height - 1) then
SrcRGB2 := SrcBmp.ScanLine[i+1]
else
SrcRGB2 := SrcRGB;
Inc(SrcRGB2);
SrcRGB1 := SrcRGB;
Inc(SrcRGB1);
end;
for j := 0 to SrcBmp.Width - 1 do
begin
if (AzimuthChange >= -180) and (AzimuthChange < -135) then
begin
Azimuthvalue := AzimuthChange + 180;
R:=SrcRGB.rgbtRed-((SrcRGB1.rgbtRed)*Azimuthvalue div 45)-((SrcRGB2.rgbtRed)*(45-Azimuthvalue) div 45)+78;
G:=SrcRGB.rgbtGreen-((SrcRGB1.rgbtGreen)*Azimuthvalue div 45)-((SrcRGB2.rgbtGreen)*(45-Azimuthvalue) div 45)+78;
B:=SrcRGB.rgbtBlue-((SrcRGB1.rgbtBlue)*Azimuthvalue div 45)-((SrcRGB2.rgbtBlue)*(45-Azimuthvalue) div 45)+78;
end
else if (AzimuthChange >= -135) and (AzimuthChange < -90) then
begin
Azimuthvalue := AzimuthChange + 135;
R:=SrcRGB.rgbtRed-((SrcRGB1.rgbtRed)*Azimuthvalue div 45)-((SrcRGB2.rgbtRed)*(45-Azimuthvalue) div 45)+78;
G:=SrcRGB.rgbtGreen-((SrcRGB1.rgbtGreen)*Azimuthvalue div 45)-((SrcRGB2.rgbtGreen)*(45-Azimuthvalue) div 45)+78;
B:=SrcRGB.rgbtBlue-((SrcRGB1.rgbtBlue)*Azimuthvalue div 45)-((SrcRGB2.rgbtBlue)*(45-Azimuthvalue) div 45)+78;
end
else if (AzimuthChange >= -90) and (AzimuthChange < -45) then
begin
if j=1 then Inc(SrcRGB1,-1);
Azimuthvalue := AzimuthChange + 90;
R:=SrcRGB.rgbtRed-((SrcRGB1.rgbtRed)*Azimuthvalue div 45)-((SrcRGB2.rgbtRed)*(45-Azimuthvalue) div 45)+78;
G:=SrcRGB.rgbtGreen-((SrcRGB1.rgbtGreen)*Azimuthvalue div 45)-((SrcRGB2.rgbtGreen)*(45-Azimuthvalue) div 45)+78;
B:=SrcRGB.rgbtBlue-((SrcRGB1.rgbtBlue)*Azimuthvalue div 45)-((SrcRGB2.rgbtBlue)*(45-Azimuthvalue) div 45)+78;
end
else if (AzimuthChange >= -45) and (AzimuthChange < 0) then
begin
if j=1 then
begin
Inc(SrcRGB1,-1);
Inc(SrcRGB2,-1);
end;
Azimuthvalue := AzimuthChange + 45;
R:=SrcRGB.rgbtRed-((SrcRGB1.rgbtRed)*Azimuthvalue div 45)-((SrcRGB2.rgbtRed)*(45-Azimuthvalue) div 45)+78;
G:=SrcRGB.rgbtGreen-((SrcRGB1.rgbtGreen)*Azimuthvalue div 45)-((SrcRGB2.rgbtGreen)*(45-Azimuthvalue) div 45)+78;
B:=SrcRGB.rgbtBlue-((SrcRGB1.rgbtBlue)*Azimuthvalue div 45)-((SrcRGB2.rgbtBlue)*(45-Azimuthvalue) div 45)+78;
end
else if (AzimuthChange >= 0) and (AzimuthChange < 45) then
begin
if j=1 then
begin
Inc(SrcRGB1,-1);
Inc(SrcRGB2,-1);
end;
Azimuthvalue := AzimuthChange;
R:=SrcRGB.rgbtRed-((SrcRGB1.rgbtRed)*Azimuthvalue div 45)-((SrcRGB2.rgbtRed)*(45-Azimuthvalue) div 45)+78;
G:=SrcRGB.rgbtGreen-((SrcRGB1.rgbtGreen)*Azimuthvalue div 45)-((SrcRGB2.rgbtGreen)*(45-Azimuthvalue) div 45)+78;
B:=SrcRGB.rgbtBlue-((SrcRGB1.rgbtBlue)*Azimuthvalue div 45)-((SrcRGB2.rgbtBlue)*(45-Azimuthvalue) div 45)+78;
end
else if (AzimuthChange >= 45) and (AzimuthChange < 90) then
begin
if j=1 then Inc(SrcRGB2,-1);
Azimuthvalue := AzimuthChange - 45;
R:=SrcRGB.rgbtRed-((SrcRGB1.rgbtRed)*Azimuthvalue div 45)-((SrcRGB2.rgbtRed)*(45-Azimuthvalue) div 45)+78;
G:=SrcRGB.rgbtGreen-((SrcRGB1.rgbtGreen)*Azimuthvalue div 45)-((SrcRGB2.rgbtGreen)*(45-Azimuthvalue) div 45)+78;
B:=SrcRGB.rgbtBlue-((SrcRGB1.rgbtBlue)*Azimuthvalue div 45)-((SrcRGB2.rgbtBlue)*(45-Azimuthvalue) div 45)+78;
end
else if (AzimuthChange >= 90) and (AzimuthChange < 135) then
begin
Azimuthvalue := AzimuthChange - 90;
R:=SrcRGB.rgbtRed-((SrcRGB1.rgbtRed)*Azimuthvalue div 45)-((SrcRGB2.rgbtRed)*(45-Azimuthvalue) div 45)+78;
G:=SrcRGB.rgbtGreen-((SrcRGB1.rgbtGreen)*Azimuthvalue div 45)-((SrcRGB2.rgbtGreen)*(45-Azimuthvalue) div 45)+78;
B:=SrcRGB.rgbtBlue-((SrcRGB1.rgbtBlue)*Azimuthvalue div 45)-((SrcRGB2.rgbtBlue)*(45-Azimuthvalue) div 45)+78;
end
else if (AzimuthChange >= 135) and (AzimuthChange <= 180) then
begin
Azimuthvalue := AzimuthChange - 135;
R:=SrcRGB.rgbtRed-((SrcRGB1.rgbtRed)*Azimuthvalue div 45)-((SrcRGB2.rgbtRed)*(45-Azimuthvalue) div 45)+78;
G:=SrcRGB.rgbtGreen-((SrcRGB1.rgbtGreen)*Azimuthvalue div 45)-((SrcRGB2.rgbtGreen)*(45-Azimuthvalue) div 45)+78;
B:=SrcRGB.rgbtBlue-((SrcRGB1.rgbtBlue)*Azimuthvalue div 45)-((SrcRGB2.rgbtBlue)*(45-Azimuthvalue) div 45)+78;
end;
R:=Min(R,255);
R:=Max(R,0);
G:=Min(G,255);
G:=Max(G,0);
B:=Min(B,255);
B:=Max(B,0);
Gray := (R shr 2) + (R shr 4) + (G shr 1) + (G shr 4) + (B shr 3);
DestRGB.rgbtRed:=Gray;
DestRGB.rgbtGreen:=Gray;
DestRGB.rgbtBlue:=Gray;
if (j=-180) and (AzimuthChange<-135)) or ((AzimuthChange>=90) and (AzimuthChange<=180))) then
begin
Inc(SrcRGB1);
end;
if (j=135) and (AzimuthChange<180)) or ((AzimuthChange>=-180) and (AzimuthChange<=-90))) then
begin
Inc(SrcRGB2);
end;
Inc(SrcRGB);
Inc(DestRGB);
end;
end;
end;
procedure Emboss(Bmp:TBitmap;AzimuthChange:integer;ElevationChange:integer;WeightChange:integer);overload;
var
DestBmp:TBitmap;
begin
DestBmp:=TBitmap.Create;
DestBmp.Assign(Bmp);
Emboss(Bmp,DestBmp,AzimuthChange,ElevationChange,WeightChange);
Bmp.Assign(DestBmp);
end;
//反色
procedure Negative(Bmp:TBitmap);
var
i, j: Integer;
PRGB: pRGBTriple;
begin
Bmp.PixelFormat:=pf24Bit;
for i := 0 to Bmp.Height - 1 do
begin
PRGB := Bmp.ScanLine[i];
for j := 0 to Bmp.Width - 1 do
begin
PRGB^.rgbtRed :=not PRGB^.rgbtRed ;
PRGB^.rgbtGreen :=not PRGB^.rgbtGreen;
PRGB^.rgbtBlue :=not PRGB^.rgbtBlue;
Inc(PRGB);
end;
end;
end;
//曝光
procedure Exposure(Bmp:TBitmap);
var
i, j: integer;
PRGB: pRGBTriple;
begin
Bmp.PixelFormat:=pf24Bit;
for i := 0 to Bmp.Height - 1 do
begin
PRGB := Bmp.ScanLine[i];
for j := 0 to Bmp.Width - 1 do
begin
if PRGB^.rgbtRed<128 then
PRGB^.rgbtRed :=not PRGB^.rgbtRed ;
if PRGB^.rgbtGreen<128 then
PRGB^.rgbtGreen :=not PRGB^.rgbtGreen;
if PRGB^.rgbtBlue<128 then
PRGB^.rgbtBlue :=not PRGB^.rgbtBlue;
Inc(PRGB);
end;
end;
end;
//模糊
procedure Blur(SrcBmp:TBitmap);
var
i, j:Integer;
SrcRGB:pRGBTriple;
SrcNextRGB:pRGBTriple;
SrcPreRGB:pRGBTriple;
Value:Integer;
procedure IncRGB;
begin
Inc(SrcPreRGB);
Inc(SrcRGB);
Inc(SrcNextRGB);
end;
procedure DecRGB;
begin
Inc(SrcPreRGB,-1);
Inc(SrcRGB,-1);
Inc(SrcNextRGB,-1);
end;
begin
SrcBmp.PixelFormat:=pf24Bit;
for i := 0 to SrcBmp.Height - 1 do
begin
if i > 0 then
SrcPreRGB:=SrcBmp.ScanLine[i-1]
else
SrcPreRGB := SrcBmp.ScanLine[i];
SrcRGB := SrcBmp.ScanLine[i];
if i < SrcBmp.Height - 1 then
SrcNextRGB:=SrcBmp.ScanLine[i+1]
else
SrcNextRGB:=SrcBmp.ScanLine[i];
for j := 0 to SrcBmp.Width - 1 do
begin
if j > 0 then DecRGB;
Value:=SrcPreRGB.rgbtRed+SrcRGB.rgbtRed+SrcNextRGB.rgbtRed;
if j > 0 then IncRGB;
Value:=Value+SrcPreRGB.rgbtRed+SrcRGB.rgbtRed+SrcNextRGB.rgbtRed;
if j < SrcBmp.Width - 1 then IncRGB;
Value:=(Value+SrcPreRGB.rgbtRed+SrcRGB.rgbtRed+SrcNextRGB.rgbtRed) div 9;
DecRGB;
SrcRGB.rgbtRed:=value;
if j > 0 then DecRGB;
Value:=SrcPreRGB.rgbtGreen+SrcRGB.rgbtGreen+SrcNextRGB.rgbtGreen;
if j > 0 then IncRGB;
Value:=Value+SrcPreRGB.rgbtGreen+SrcRGB.rgbtGreen+SrcNextRGB.rgbtGreen;
if j < SrcBmp.Width - 1 then IncRGB;
Value:=(Value+SrcPreRGB.rgbtGreen+SrcRGB.rgbtGreen+SrcNextRGB.rgbtGreen) div 9;
DecRGB;
SrcRGB.rgbtGreen:=value;
if j > 0 then DecRGB;
Value:=SrcPreRGB.rgbtBlue+SrcRGB.rgbtBlue+SrcNextRGB.rgbtBlue;
if j > 0 then IncRGB;
Value:=Value+SrcPreRGB.rgbtBlue+SrcRGB.rgbtBlue+SrcNextRGB.rgbtBlue;
if j < SrcBmp.Width - 1 then IncRGB;
Value:=(Value+SrcPreRGB.rgbtBlue+SrcRGB.rgbtBlue+SrcNextRGB.rgbtBlue) div 9;
DecRGB;
SrcRGB.rgbtBlue:=value;
IncRGB;
end;
end;
end;
//銳化
procedure Sharpen(SrcBmp:TBitmap);
var
i, j: integer;
SrcRGB: pRGBTriple;
SrcPreRGB: pRGBTriple;
Value: integer;
begin
SrcBmp.PixelFormat:=pf24Bit;
for i := 0 to SrcBmp.Height - 1 do
begin
SrcRGB := SrcBmp.ScanLine[i];
if i > 0 then
SrcPreRGB:=SrcBmp.ScanLine[i-1]
else
SrcPreRGB:=SrcBmp.ScanLine[i];
for j := 0 to SrcBmp.Width - 1 do
begin
if j = 1 then Dec(SrcPreRGB);
Value:=SrcRGB.rgbtRed+(SrcRGB.rgbtRed-SrcPreRGB.rgbtRed) div 2;
Value:=Max(0,Value);
Value:=Min(255,Value);
SrcRGB.rgbtRed:=value;
Value:=SrcRGB.rgbtGreen+(SrcRGB.rgbtGreen-SrcPreRGB.rgbtGreen) div 2;
Value:=Max(0,Value);
Value:=Min(255,Value);
SrcRGB.rgbtGreen:=value;
Value:=SrcRGB.rgbtBlue+(SrcRGB.rgbtBlue-SrcPreRGB.rgbtBlue) div 2;
Value:=Max(0,Value);
Value:=Min(255,Value);
SrcRGB.rgbtBlue:=value;
Inc(SrcRGB);
Inc(SrcPreRGB);
end;
end;
end;
[圖像的旋轉(zhuǎn)和翻轉(zhuǎn)]
以下代碼用ScanLine配合指針移動(dòng)實(shí)現(xiàn),用于24位色!
//旋轉(zhuǎn)90度
procedure Rotate90(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Width := Bitmap.Height;
Bmp.Height := Bitmap.Width;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine[i];
Inc(rowOut,Height - j);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;
//旋轉(zhuǎn)180度
procedure Rotate180(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Width := Bitmap.Width;
Bmp.Height := Bitmap.Height;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine[Height - j];
Inc(rowOut,Width - i);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;
//旋轉(zhuǎn)270度
procedure Rotate270(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Width := Bitmap.Height;
Bmp.Height := Bitmap.Width;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine[Width - i];
Inc(rowOut,j);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;
//任意角度
function RotateBitmap(Bitmap:TBitmap;Angle:Integer;BackColor:TColor):TBitmap;
var
i,j,iOriginal,jOriginal,CosPoint,SinPoint : integer;
RowOriginal,RowRotated : pRGBTriple;
SinTheta,CosTheta : Extended;
AngleAdd : integer;
begin
Result:=TBitmap.Create;
Result.PixelFormat := pf24bit;
Result.Canvas.Brush.Color:=BackColor;
Angle:=Angle Mod 360;
if Angle<0 then Angle:=360-Abs(Angle);
if Angle=0 then
Result.Assign(Bitmap)
else if Angle=90 then
begin
Result.Assign(Bitmap);
Rotate90(Result);//如果是旋轉(zhuǎn)90度,直接調(diào)用上面的代碼
end
else if (Angle>90) and (Angle<180) then
begin
AngleAdd:=90;
Angle:=Angle-AngleAdd;
end
else if Angle=180 then
begin
Result.Assign(Bitmap);
Rotate180(Result);//如果是旋轉(zhuǎn)180度,直接調(diào)用上面的過程
end
else if (Angle>180) and (Angle<270) then
begin
AngleAdd:=180;
Angle:=Angle-AngleAdd;
end
else if Angle=270 then
begin
Result.Assign(Bitmap);
Rotate270(Result);//如果是旋轉(zhuǎn)270度,直接調(diào)用上面的過程
end
else if (Angle>270) and (Angle<360) then
begin
AngleAdd:=270;
Angle:=Angle-AngleAdd;
end
else
AngleAdd:=0;
if (Angle>0) and (Angle<90) then
begin
SinCos((Angle + AngleAdd) * Pi / 180, SinTheta, CosTheta);
if (SinTheta * CosTheta) < 0 then
begin
Result.Width := Round(Abs(Bitmap.Width * CosTheta - Bitmap.Height * SinTheta));
Result.Height := Round(Abs(Bitmap.Width * SinTheta - Bitmap.Height * CosTheta));
end
else
begin
Result.Width := Round(Abs(Bitmap.Width * CosTheta + Bitmap.Height * SinTheta));
Result.Height := Round(Abs(Bitmap.Width * SinTheta + Bitmap.Height * CosTheta));
end;
CosTheta:=Abs(CosTheta);
SinTheta:=Abs(SinTheta);
if (AngleAdd=0) or (AngleAdd=180) then
begin
CosPoint:=Round(Bitmap.Height*CosTheta);
SinPoint:=Round(Bitmap.Height*SinTheta);
end
else
begin
SinPoint:=Round(Bitmap.Width*CosTheta);
CosPoint:=Round(Bitmap.Width*SinTheta);
end;
for j := 0 to Result.Height-1 do
begin
RowRotated := Result.Scanline[j];
for i := 0 to Result.Width-1 do
begin
Case AngleAdd of
0:
begin
jOriginal := Round((j+1)*CosTheta-(i+1-SinPoint)*SinTheta)-1;
iOriginal := Round((i+1)*CosTheta-(CosPoint-j-1)*SinTheta)-1;
end;
90:
begin
iOriginal := Round((j+1)*SinTheta-(i+1-SinPoint)*CosTheta)-1;
jOriginal := Bitmap.Height-Round((i+1)*SinTheta-(CosPoint-j-1)*CosTheta);
end;
180:
begin
jOriginal := Bitmap.Height-Round((j+1)*CosTheta-(i+1-SinPoint)*SinTheta);
iOriginal := Bitmap.Width-Round((i+1)*CosTheta-(CosPoint-j-1)*SinTheta);
end;
270:
begin
iOriginal := Bitmap.Width-Round((j+1)*SinTheta-(i+1-SinPoint)*CosTheta);
jOriginal := Round((i+1)*SinTheta-(CosPoint-j-1)*CosTheta)-1;
end;
end;
if (iOriginal >= 0) and (iOriginal <= Bitmap.Width-1)and
(jOriginal >= 0) and (jOriginal <= Bitmap.Height-1)
then
begin
RowOriginal := Bitmap.Scanline[jOriginal];
Inc(RowOriginal,iOriginal);
RowRotated^ := RowOriginal^;
Inc(RowRotated);
end
else
begin
Inc(RowRotated);
end;
end;
end;
end;
end;
//水平翻轉(zhuǎn)
procedure FlipHorz(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Width := Bitmap.Width;
Bmp.Height := Bitmap.Height;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine[j];
Inc(rowOut,Width - i);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;
//垂直翻轉(zhuǎn)
procedure FlipVert(const Bitmap:TBitmap);
var
i,j:Integer;
rowIn,rowOut:pRGBTriple;
Bmp:TBitmap;
Width,Height:Integer;
begin
Bmp:=TBitmap.Create;
Bmp.Width := Bitmap.Height;
Bmp.Height := Bitmap.Width;
Bmp.PixelFormat := pf24bit;
Width:=Bitmap.Width-1;
Height:=Bitmap.Height-1;
for j := 0 to Height do
begin
rowIn := Bitmap.ScanLine[j];
for i := 0 to Width do
begin
rowOut := Bmp.ScanLine[Height - j];
Inc(rowOut,i);
rowOut^ := rowIn^;
Inc(rowIn);
end;
end;
Bitmap.Assign(Bmp);
end;
[亮度、對比度、飽和度的調(diào)整]
以下代碼用ScanLine配合指針移動(dòng)實(shí)現(xiàn)!
function Min(a, b: integer): integer;
begin
if a < b then
result := a
else
result := b;
end;
function Max(a, b: integer): integer;
begin
if a > b then
result := a
else
result := b;
end;
//亮度調(diào)整
procedure BrightnessChange(const SrcBmp,DestBmp:TBitmap;ValueChange:integer);
var
i, j: integer;
SrcRGB, DestRGB: pRGBTriple;
begin
for i := 0 to SrcBmp.Height - 1 do
begin
SrcRGB := SrcBmp.ScanLine[i];
DestRGB := DestBmp.ScanLine[i];
for j := 0 to SrcBmp.Width - 1 do
begin
if ValueChange > 0 then
begin
DestRGB.rgbtRed := Min(255, SrcRGB.rgbtRed + ValueChange);
DestRGB.rgbtGreen := Min(255, SrcRGB.rgbtGreen + ValueChange);
DestRGB.rgbtBlue := Min(255, SrcRGB.rgbtBlue + ValueChange);
end else begin
DestRGB.rgbtRed := Max(0, SrcRGB.rgbtRed + ValueChange);
DestRGB.rgbtGreen := Max(0, SrcRGB.rgbtGreen + ValueChange);
DestRGB.rgbtBlue := Max(0, SrcRGB.rgbtBlue + ValueChange);
end;
Inc(SrcRGB);
Inc(DestRGB);
end;
end;
end;
//對比度調(diào)整
procedure ContrastChange(const SrcBmp,DestBmp:TBitmap;ValueChange:integer);
var
i, j: integer;
SrcRGB, DestRGB: pRGBTriple;
begin
for i := 0 to SrcBmp.Height - 1 do
begin
SrcRGB := SrcBmp.ScanLine[i];
DestRGB := DestBmp.ScanLine[i];
for j := 0 to SrcBmp.Width - 1 do
begin
if ValueChange>=0 then
begin
if SrcRGB.rgbtRed >= 128 then
DestRGB.rgbtRed := Min(255, SrcRGB.rgbtRed + ValueChange)
else
DestRGB.rgbtRed := Max(0, SrcRGB.rgbtRed - ValueChange);
if SrcRGB.rgbtGreen >= 128 then
DestRGB.rgbtGreen := Min(255, SrcRGB.rgbtGreen + ValueChange)
else
DestRGB.rgbtGreen := Max(0, SrcRGB.rgbtGreen - ValueChange);
if SrcRGB.rgbtBlue >= 128 then
DestRGB.rgbtBlue := Min(255, SrcRGB.rgbtBlue + ValueChange)
else
DestRGB.rgbtBlue := Max(0, SrcRGB.rgbtBlue - ValueChange);
end
else
begin
if SrcRGB.rgbtRed >= 128 then
DestRGB.rgbtRed := Max(128, SrcRGB.rgbtRed + ValueChange)
else
DestRGB.rgbtRed := Min(128, SrcRGB.rgbtRed - ValueChange);
if SrcRGB.rgbtGreen >= 128 then
DestRGB.rgbtGreen := Max(128, SrcRGB.rgbtGreen + ValueChange)
else
DestRGB.rgbtGreen := Min(128, SrcRGB.rgbtGreen - ValueChange);
if SrcRGB.rgbtBlue >= 128 then
DestRGB.rgbtBlue := Max(128, SrcRGB.rgbtBlue + ValueChange)
else
DestRGB.rgbtBlue := Min(128, SrcRGB.rgbtBlue - ValueChange);
end;
Inc(SrcRGB);
Inc(DestRGB);
end;
end;
end;
//飽和度調(diào)整
procedure SaturationChange(const SrcBmp,DestBmp:TBitmap;ValueChange:integer);
var
Grays: array[0..767] of Integer;
Alpha: array[0..255] of Word;
Gray, x, y: Integer;
SrcRGB,DestRGB: pRGBTriple;
i: Byte;
begin
ValueChange:=ValueChange+255;
for i := 0 to 255 do
Alpha[i] := (i * ValueChange) Shr 8;
x := 0;
for i := 0 to 255 do
begin
Gray := i - Alpha[i];
Grays[x] := Gray;
Inc(x);
Grays[x] := Gray;
Inc(x);
Grays[x] := Gray;
Inc(x);
end;
for y := 0 to SrcBmp.Height - 1 do
begin
SrcRGB := SrcBmp.ScanLine[Y];
DestRGB := DestBmp.ScanLine[Y];
for x := 0 to SrcBmp.Width - 1 do
begin
Gray := Grays[SrcRGB.rgbtRed + SrcRGB.rgbtGreen + SrcRGB.rgbtBlue];
if Gray + Alpha[SrcRGB.rgbtRed]>0 then
DestRGB.rgbtRed := Min(255,Gray + Alpha[SrcRGB.rgbtRed])
else
DestRGB.rgbtRed := 0;
if Gray + Alpha[SrcRGB.rgbtGreen]>0 then
DestRGB.rgbtGreen := Min(255,Gray + Alpha[SrcRGB.rgbtGreen])
else
DestRGB.rgbtGreen := 0;
if Gray + Alpha[SrcRGB.rgbtBlue]>0 then
DestRGB.rgbtBlue := Min(255,Gray + Alpha[SrcRGB.rgbtBlue])
else
DestRGB.rgbtBlue := 0;
Inc(SrcRGB);
Inc(DestRGB);
end;
end;
end;
//RGB調(diào)整
procedure RGBChange(SrcBmp,DestBmp:TBitmap;RedChange,GreenChange,BlueChange:integer);
var
SrcRGB, DestRGB: pRGBTriple;
i,j:integer;
begin
for i := 0 to SrcBmp.Height- 1 do
begin
SrcRGB := SrcBmp.ScanLine[i];
DestRGB :=DestBmp.ScanLine[i];
for j := 0 to SrcBmp.Width - 1 do
begin
if RedChange> 0 then
DestRGB.rgbtRed := Min(255, SrcRGB.rgbtRed + RedChange)
else
DestRGB.rgbtRed := Max(0, SrcRGB.rgbtRed + RedChange);
if GreenChange> 0 then
DestRGB.rgbtGreen := Min(255, SrcRGB.rgbtGreen + GreenChange)
else
DestRGB.rgbtGreen := Max(0, SrcRGB.rgbtGreen + GreenChange);
if BlueChange> 0 then
DestRGB.rgbtBlue := Min(255, SrcRGB.rgbtBlue + BlueChange)
else
DestRGB.rgbtBlue := Max(0, SrcRGB.rgbtBlue + BlueChange);
Inc(SrcRGB);
Inc(DestRGB);
end;
end;
end;
[顏色調(diào)整]
//RGB<=>BGR
procedure RGB2BGR(const Bitmap:TBitmap);
var
X: Integer;
Y: Integer;
PRGB: pRGBTriple;
Color: Byte;
begin
for Y := 0 to (Bitmap.Height - 1) do
begin
for X := 0 to (Bitmap.Width - 1) do
begin
Color := PRGB^.rgbtRed;
PRGB^.rgbtRed := PRGB^.rgbtBlue;
PRGB^.rgbtBlue := Color;
Inc(PRGB);
end;
end
end;
end;
//灰度化(加權(quán))
procedure Grayscale(const Bitmap:TBitmap);
var
X: Integer;
Y: Integer;
PRGB: pRGBTriple;
Gray: Byte;
begin
for Y := 0 to (Bitmap.Height - 1) do
begin
PRGB := Bitmap.ScanLine[Y];
for X := 0 to (Bitmap.Width - 1) do
begin
Gray := (77 * Red + 151 * Green + 28 * Blue) shr 8;
PRGB^.rgbtRed:=Gray;
PRGB^.rgbtGreen:=Gray;
PRGB^.rgbtBlue:=Gray;
Inc(PRGB);
end;
end;
end;
理論篇:
關(guān)鍵詞:
繪圖區(qū)-即窗口顯示圖像的區(qū)域,亦可為全屏幕(在全屏幕下繪圖的效果比一般窗口下好)
中心點(diǎn)-即要繪圖區(qū)顯示的中心點(diǎn)在原始圖像的坐標(biāo)(聲明:這個(gè)概念特別重要)
先說說圖像的放大,要放大一張圖片,我們一般的做法是直接放大圖像,但本文介紹的方法僅放大我們能夠看到的部分,放大分兩種情況,一種是放大后比繪圖區(qū)還要小,這種情況沒什么好說,當(dāng)然是顯示全部的圖像;第二種是放大后的圖像比繪圖區(qū)大,這才是我們今天要討論的重點(diǎn)話題,這種情況下我們先要確定圖像放大后的大小,然后根據(jù)“中心點(diǎn)”計(jì)算在原始圖像的位置和大小,最后把截取的圖像放大到繪圖區(qū)。
再說說圖像的漫游,當(dāng)顯示的圖像超過繪圖區(qū)時(shí),我們需要對圖像進(jìn)行漫游,以便看到全部的圖像。原理是:當(dāng)鼠標(biāo)在繪圖區(qū)進(jìn)行單擊時(shí),這時(shí)開始漫游,先記錄鼠標(biāo)的單擊位置,然后檢測鼠標(biāo)的移動(dòng),根據(jù)鼠標(biāo)和上次的位移計(jì)算出“中心點(diǎn)”(需要將屏幕坐標(biāo)轉(zhuǎn)換為原始圖像坐標(biāo)),根據(jù)在上面放大的原理到原始圖像中取出要顯示的部分,放大顯示到繪圖區(qū)。
算法實(shí)現(xiàn)篇:
1.圖像放大
變量定義:
PZoom:放大率(整數(shù):100時(shí)為100%,根據(jù)需要可以將 100 該為 10000 或者更大些,但不推薦使用浮點(diǎn)數(shù))
a,b:中心點(diǎn)
w,h:要截取原始圖像的寬和高
x,y:要截取的位置(左上角)
sw,sh:原始圖像的寬和高
p1,p2:放大比例
aw,ah:放大后圖像的大小
pw,ph:繪圖區(qū)大小
vx,vy:在繪圖區(qū)顯示的位置(左上角)
vw,vh:在繪圖區(qū)顯示的大小
ptx,pty:臨時(shí)變量
已知的變量:PZoom,(a,b),(sw,sh),(p1,p2),(aw,ah),(pw,ph)
要計(jì)算的變量:(x,y),(w,h),(vx,vy),(vw,vh)
開始計(jì)算:
aw=Round(PZoom*sw/100); ah=Round(PZoom*sh/100); p1=aw/pw p2=ah/ph // 注:Round 用于取整,如其他語言的Int(),Fix()等 if p1>1 then w=Round(sw/p1) else w=sw if p2>1 then h=Round(sh/p2) else h=sh // 注:shr 為右移運(yùn)算符,可以使用“>>1”、“div 2”、“/2”或“Round(w/2)”代替 x=a-w shr 1 y=b-h shr 1 // 注:div 為整除運(yùn)算符 ptx=(w*PZoom) div 100 pty=(h*PZoom) div 100 // 以下計(jì)算在繪圖區(qū)顯示的圖像大小和位置
變量
Pencent:double; // 縮放比 wx:double; // 寬縮放比 hx:double; // 高縮放比 // 獲得縮放比 wx:=pw/ptx hx:=ph/pty if wx>hx then Pencent:=hx else Pencent:=wx; // 獲得圖片最后的大小 vw:=Round(Pencent*ptx); vh:=Round(Pencent*pty); // 計(jì)算出圖片的位置 vx:=(pw-vw) div 2; vy:=(ph-vh) div 2; // ------------------------------------
好了,兩個(gè)重要的任務(wù)完成(x,y),(w,h),(vx,vy),(vw,vh)已經(jīng)全部計(jì)算得出,下面的工作就是顯示了,我們選擇 Windows API 進(jìn)行操作
變量
sDC 為原始圖片的設(shè)備句柄(DC) tDC 為臨時(shí)設(shè)備句柄 dDC 最終設(shè)備句柄 BitBlt(tDC,0,0,w,h,sDC,0,0,SRCCOPY); SetStretchBltMode(dDC,STRETCH_DELETESCANS); StretchBlt(dDC,0,0,vw,vh,tDC,0,0,w,h,SRCCOPY);
最后繪制到顯示的區(qū)域即可:
例如:
BitBlt(GetDC(0),vx,vy,vx+vw,xy+vh,dDC,0,0,SRCCOPY);
2.圖像漫游
先定義三個(gè)全局變量:
FBeginDragPoint :TPoint; // 記錄鼠標(biāo)開始拖動(dòng)的位置 FBeginDragSBPoint :TPoint; // 記錄“中心點(diǎn)”位置 FBeginDrag :boolean; // 是否已經(jīng)開始“拖動(dòng)” a,b :integer; // “中心點(diǎn)”位置
在鼠標(biāo)左鍵點(diǎn)擊時(shí),記錄鼠標(biāo)的位置和“中心點(diǎn)”的位置,同時(shí)設(shè)置 FBeginDrag 為真
當(dāng)鼠標(biāo)右鍵彈起時(shí),設(shè)置 FBeginDrag 為假
鼠標(biāo)移動(dòng)時(shí),判斷 FBeginDrag ,如果為假不進(jìn)行處理,如果為真進(jìn)行下面處理:
假設(shè) X,Y 為鼠標(biāo)當(dāng)前的位置
a=FBeginDragPoint.X-((X-FBeginDragPoint.X)*100) div PZoom b=FBeginDragPoint.Y-((Y-FBeginDragPoint.Y)*100) div PZoom
最后使用上面介紹的圖像放大顯示出圖像
技巧篇:
1.如果圖像較大,使用 delphi 的 位圖對象會(huì)出現(xiàn)內(nèi)存溢出錯(cuò)誤,這時(shí)可以進(jìn)行如下設(shè)置:
bitImage:=TBitmap.Create; bitImage.PixelFormat:=pf24bit; bitImage.ReleaseHandle;
2.如果要讓圖像自動(dòng)適應(yīng)窗口的大小,參考以下代碼:
var p1,p2 :double; begin p1:=pw/sw; p2:=ph/sw; if p1>p2 then PZoom:=Round(p2*100) else PZoom:=Round(p1*100); if PZoom=0 then PZoom:=100; end;
Delphi灰度圖像像素顏色亮度處理
在圖像處理中,速度是很重要的。因此,我們得重新處理一下TBitmap,得到TVczhBitmap。這只是因?yàn)镚etPixels和SetPixels的速度太慢,換一個(gè)方法而已。
unit untBitmapProc; interface uses Graphics, SysUtils; type TVczhBitmap=class(TBitmap) private Data:PByteArray; Line:Integer; procedure SetFormat; function GetBytePointer(X,Y:Integer):PByte; procedure SetBytes(X,Y:Integer;Value:Byte); function GetBytes(X,Y:Integer):Byte; protected published constructor Create; public property Bytes[X,Y:Integer]:Byte read GetBytes write SetBytes; procedure LoadFromFile(FileName:String); procedure ToGray; end; implementation procedure TVczhBitmap.SetFormat; begin HandleType:=bmDIB; PixelFormat:=pf24bit; end; function TVczhBitmap.GetBytePointer(X,Y:Integer):PByte; begin if Line<>Y then begin Line:=Y; Data:=ScanLine[Y]; end; Longint(result):=Longint(Data)+X; end; procedure TVczhBitmap.SetBytes(X,Y:Integer;Value:Byte); begin GetBytePointer(X,Y)^:=Value; end; function TVczhBitmap.GetBytes(X,Y:Integer):Byte; begin result:=GetBytePointer(X,Y)^; end; constructor TVczhBitmap.Create; begin inherited Create; SetFormat; Line:=-1; end; procedure TVczhBitmap.LoadFromFile(FileName:String); begin inherited LoadFromFile(FileName); SetFormat; Line:=-1; end; procedure TVczhBitmap.ToGray; var X,Y,R:Integer; B:Byte; begin for Y:=0 to Height-1 do for X:=0 to Width-1 do begin R:=0; for B:=0 to 2 do R:=R+GetBytes(X*3+B,Y); for B:=0 to 2 do SetBytes(X*3+B,Y,R div 3); end; end; end.
此后,我們需要建立幾個(gè)窗體。第一個(gè)用來顯示圖片,第二個(gè)用來處理圖片,其他的窗體都繼承自第二個(gè)窗體,包含實(shí)際的處理方法。
先看第二個(gè)窗口:
unit untProc;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, untBitmapProc, StdCtrls, ComCtrls;
type
TfrmProcessor = class(TForm)
pbBar: TPaintBox;
gpProc: TGroupBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure pbBarPaint(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
BarData:array[0..255]of Byte;
Bar:TVczhBitmap;
procedure DrawBar;
end;
var
frmProcessor: TfrmProcessor;
implementation
{$R *.dfm}
uses untViewer;
procedure TfrmProcessor.DrawBar;
var I:Integer;
begin
Bar.Canvas.FillRect(Bar.Canvas.ClipRect);
Bar.Canvas.MoveTo(0,255-BarData[0]);
for I:=1 to 255 do
Bar.Canvas.LineTo(I,255-BarData[I]);
end;
procedure TfrmProcessor.FormCreate(Sender: TObject);
begin
Bar:=TVczhBitmap.Create;
Bar.Width:=256;
Bar.Height:=256;
Bar.Canvas.Brush.Color:=clWhite;
Bar.Canvas.Brush.Style:=bsSolid;
end;
procedure TfrmProcessor.FormDestroy(Sender: TObject);
begin
Bar.Free;
end;
procedure TfrmProcessor.FormShow(Sender: TObject);
var I:Integer;
begin
for I:=0 to 255 do
BarData[I]:=I;
DrawBar;
end;
procedure TfrmProcessor.pbBarPaint(Sender: TObject);
begin
pbBar.Canvas.Draw(0,0,Bar);
end;
procedure TfrmProcessor.Button1Click(Sender: TObject);
var X,Y:Integer;
begin
for Y:=0 to Buffer.Height-1 do
for X:=0 to Buffer.Width*3-1 do
Played.Bytes[X,Y]:=BarData[Buffer.Bytes[X,Y]];
frmViewer.FormPaint(frmViewer);
end;
end.
之后,做一個(gè)窗口繼承自它,則調(diào)整BarData[]后,按Apply即可看到結(jié)果。
現(xiàn)在開始將圖像處理。具體效果見示例程序。
一、顏色反轉(zhuǎn)。
灰度圖像的顏色都是從0~255,所以,為了使顏色反轉(zhuǎn),我們可以用255減去該顏色值以得到反轉(zhuǎn)后的顏色。
var I:Integer; begin inherited; for I:=0 to 255 do BarData[I]:=255-I;//用255減去該顏色值 DrawBar; pbBarPaint(pbBar); end;
二、縮小顏色范圍以增強(qiáng)或減弱亮度
顏色本來是從0~255的。如果調(diào)節(jié)它的范圍,例如從0~16,則會(huì)是圖像明顯變暗。我們可以把起始值設(shè)為a,把終止值設(shè)為b,則新的顏色值New=a+(b-1)*Old/255。這樣做的話可以改變亮度,并且不會(huì)破壞原先顏色的順序。代碼如下
var I:Integer; begin for I:=0 to 255 do BarData[I]:=(255-sbMin.Position)+Round((sbMin.Position-sbMax.Position)/255*I); DrawBar; pbBarPaint(pbBar); Button1Click(Button1); end;
這里的sbMin.Position和sbMaxPosition都是反轉(zhuǎn)過的。所以使用時(shí)要用255去減
三、增加某個(gè)范圍內(nèi)的顏色范圍
如果圖像本身的顏色范圍很小的畫,你可以通過這種方法來加大圖像的對比度,有利于對圖像的分析。具體做法:
選取一個(gè)值a做為起始值,選取一個(gè)值b做為終止值,然后按以下公式變形:
| 0 (X<=a)
f(X)= | 255/(b-a)*(X-a)
| 255(X>=b)
var I:Integer; begin for I:=0 to 255 do begin if I<=sbMin.Position then BarData[I]:=0 else if I>=sbMax.Position then BarData[I]:=255 else BarData[I]:=Round(255/(sbMax.Position-sbMin.Position)*(I-sbMin.Position)); end; DrawBar; pbBarPaint(pbBar); Button1Click(Button1); end;
四、變?yōu)楹诎讏D片
在使用第三個(gè)功能的時(shí)候,你會(huì)發(fā)現(xiàn)當(dāng)b<=a時(shí),圖像上的顏色除了黑色就是白色。這樣操作的好處是不能直接顯示出來的。這只要到了比較高級的圖像處理如邊緣檢測等,才有作用。本例可以拿第三種方法的公式再變形,因此不作詳細(xì)闡述。
五、指數(shù)級亮度調(diào)整
我們假設(shè)這個(gè)圖的定義域是[0,1],值域也是[0,1]。那么,定義函數(shù)f(x)=x^c,則f(x)的圖像有一段如上圖。我們再用鼠標(biāo)操作時(shí),可以在上面取一點(diǎn)P(a,b),然后使f(x)通過點(diǎn)P,則c=ln(b)/ln(a)。有了c之后,我們就可以對顏色進(jìn)行操作了:
New=(Old/255)^c*255=exp(ln(old/255)*c)*255 var ea,eb,ec:Extended; I:Integer; begin ea:=A/255; eb:=B/255; ec:=Ln(eb)/Ln(ea); for I:=1 to 255 do BarData[I]:=Round(Exp(Ln((I/255))*ec)*255); DrawBar; pbBarPaint(pbBar); Button1Click(Button1); end;
這樣做可以調(diào)節(jié)圖像的亮度。
Delphi圖形顯示特效的技巧
概述
----目前在許多學(xué)習(xí)軟件、游戲光盤中,經(jīng)常會(huì)看到各種
圖形顯示技巧,憑著圖形的移動(dòng)、交錯(cuò)、雨滴狀、百頁窗、積木堆疊等顯現(xiàn)方式,使畫面變得更為生動(dòng)活潑,更 能吸引觀眾。本文將探討如何在delphi中實(shí)現(xiàn)各種圖形顯示技巧。
基本原理
----在delphi中,實(shí)現(xiàn)一副圖象的顯示是非常簡單的,只要在form中定義一個(gè)timage組件,設(shè)置其picture屬性,然后選 擇任何有效的.ico、.bmp、.emf或.wmf文件,進(jìn)行l(wèi)oad,所選文 件就顯示在timage組件中了。但這只是直接將圖形顯示在窗體中,毫無技巧可言。為了使圖形顯示具有別具一格的效果,可以按下列步驟實(shí)現(xiàn):
----定義一個(gè)timage組件,把要顯示的圖形先裝入到timage組件中,也就是說,把圖形內(nèi)容從磁盤載入內(nèi)存中, 做為圖形緩存。
----創(chuàng)建一新的位圖對象,其尺寸跟timage組件中的圖形一樣。
----利用畫布(canvas)的copyrect功能(將一個(gè)畫布的矩形區(qū)域拷貝到另一個(gè)畫布的矩形區(qū)域),使用技巧,動(dòng)態(tài)形
成位圖文件內(nèi)容,然后在窗體中顯示位圖。
----實(shí)現(xiàn)方法
下面介紹各種圖形顯示技巧:
1.推拉效果
將要顯示的圖形由上、下、左、右方向拉進(jìn)屏幕內(nèi)顯示,同時(shí)將屏幕上原來的舊圖蓋掉,此種效果可分為四種,上拉、下拉、左拉、右拉,但原理都差不多,以上拉 效果為例。
原理:首先將放在暫存圖形的第一條水平線,搬移至要顯示的位圖的最后一條,接著再將暫存圖形的前兩條水平線,依序搬移至要顯示位圖的最后兩條水平線,然后搬移前三條、前四條叄?直到全部圖形數(shù)據(jù)搬完為止。在搬移的過程中即可看到顯示的位圖由下而上浮起,而達(dá)到上拉的效果。
程序算法:
procedure tform1.button1click(sender: tobject); var newbmp: tbitmap; i,bmpheight,bmpwidth:integer; begin newbmp:= tbitmap.create; newbmp.width:=image1.width; newbmp.height:=image1.height; bmpheight:=image1.height; bmpwidth:=image1.width; for i:=0 to bmpheight do begin newbmp.canvas.copyrect(rect (0,bmpheight-i,bmpwidth,bmpheight), image1.canvas, rect(0,0,bmpwidth,i)); form1.canvas.draw(120,100,newbmp); end; newbmp.free; end;
2.垂直交錯(cuò)效果
原理:將要顯示的圖形拆成兩部分,奇數(shù)條掃描線由上往下搬移,偶數(shù)條掃描線的部分則由下往上搬移,而且兩者同時(shí)進(jìn)行。從屏幕上便可看到分別由上下兩端出現(xiàn)的較淡圖形向屏幕中央移動(dòng),直到完全清楚為止。
程序算法:
procedure tform1.button4click(sender: tobject); var newbmp:tbitmap; i,j,bmpheight,bmpwidth:integer; begin newbmp:= tbitmap.create; newbmp.width:=image1.width; newbmp.height:=image1.height; bmpheight:=image1.height; bmpwidth:=image1.width; i:=0; while i< =bmpheight do begin j:=i; while j >0 do begin newbmp.canvas.copyrect(rect(0,j-1,bmpwidth,j), image1.canvas, rect(0,bmpheight-i+j-1,bmpwidth,bmpheight-i+j)); newbmp.canvas.copyrect(rect (0,bmpheight-j,bmpwidth,bmpheight-j+1), image1.canvas, rect(0,i-j,bmpwidth,i-j+1)); j:=j-2; end; form1.canvas.draw(120,100,newbmp); i:=i+2; end; newbmp.free; end;
3.水平交錯(cuò)效果
原理:同垂直交錯(cuò)效果原理一樣,只是將分成兩組后的圖形分別由左右兩端移進(jìn)屏幕。
程序算法:
procedure tform1.button5click(sender: tobject); var newbmp:tbitmap; i,j,bmpheight,bmpwidth:integer; begin newbmp:= tbitmap.create; newbmp.width:=image1.width; newbmp.height:=image1.height; bmpheight:=image1.height; bmpwidth:=image1.width; i:=0; while i< =bmpwidth do begin j:=i; while j >0 do begin newbmp.canvas.copyrect(rect(j-1,0,j,bmpheight), image1.canvas, rect(bmpwidth-i+j-1,0,bmpwidth-i+j,bmpheight)); newbmp.canvas.copyrect(rect (bmpwidth-j,0,bmpwidth-j+1,bmpheight), image1.canvas, rect(i-j,0,i-j+1,bmpheight)); j:=j-2; end; form1.canvas.draw(120,100,newbmp); i:=i+2; end; newbmp.free; end;
4.雨滴效果
原理:將暫存圖形的最后一條掃描線,依序搬移到可視位圖的第一條到最后一條掃描線,讓此條掃描線在屏幕上留下它的軌跡。接著再把暫存圖形的倒數(shù)第二條掃描線,依序搬移到可視位圖的第一條到倒數(shù)第二條掃描線。其余的掃描線依此類推。
程序算法:
procedure tform1.button3click(sender: tobject); var newbmp:tbitmap; i,j,bmpheight,bmpwidth:integer; begin newbmp:= tbitmap.create; newbmp.width:=image1.width; newbmp.height:=image1.height; bmpheight:=image1.height; bmpwidth:=image1.width; for i:=bmpheight downto 1 do for j:=1 to i do begin newbmp.canvas.copyrect(rect(0,j-1,bmpwidth,j), image1.canvas, rect(0,i-1,bmpwidth,i)); form1.canvas.draw(120,100,newbmp); end; newbmp.free; end;
5.百葉窗效果
原理:將放在暫存圖形的數(shù)據(jù)分成若干組,然后依次從第一組到最后一組搬移,第一次每組各搬移第一條掃描線到可視位圖的相應(yīng)位置,第二次搬移第二條掃描線,接著搬移第三條、第四條掃描線.
程序算法:
procedure tform1.button6click(sender: tobject); var newbmp:tbitmap; i,j,bmpheight,bmpwidth:integer; xgroup,xcount:integer; begin newbmp:= tbitmap.create; newbmp.width:=image1.width; newbmp.height:=image1.height; bmpheight:=image1.height; bmpwidth:=image1.width; xgroup:=16; xcount:=bmpheight div xgroup; for i:=0 to xcount do for j:=0 to xgroup do begin newbmp.canvas.copyrect(rect (0,xcount*j+i-1,bmpwidth,xcount*j+i), image1.canvas, rect(0,xcount*j+i-1,bmpwidth,xcount*j+i)); form1.canvas.draw(120,100,newbmp); end; newbmp.free; end;
6.積木效果
原理:是雨滴效果的一種變化,不同之處在于,積木效果每次搬移的是一塊圖形,而不只是一根掃描線。
程序算法:
procedure tform1.button7click(sender: tobject); var newbmp:tbitmap; i,j,bmpheight,bmpwidth:integer; begin newbmp:= tbitmap.create; newbmp.width:=image1.width; newbmp.height:=image1.height; bmpheight:=image1.height; bmpwidth:=image1.width; i:=bmpheight; while i>0 do begin for j:=10 to i do begin newbmp.canvas.copyrect(rect(0,j-10,bmpwidth,j), image1.canvas, rect(0,i-10,bmpwidth,i)); form1.canvas.draw(120,100,newbmp); end; i:=i-10; end; newbmp.free; end;
結(jié)束語
上述圖形顯示效果均已上機(jī)通過。使用效果很好。
用Delphi實(shí)現(xiàn)圖像放大鏡
向窗體上添加兩個(gè)TImage組件,其中一個(gè)TImage組件的Name屬性設(shè)置為Image1,它充當(dāng)原圖片顯示的載體。另一個(gè)TImage組件的Name屬性設(shè)置為Image2,它可以顯示放大后的圖像。
本例的核心是StretchBlt函數(shù),利用StretchBlt函數(shù)實(shí)現(xiàn)局部圖像放大,響應(yīng)代碼如下:
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
StretchBlt(Image2.Canvas.Handle,0,0,Image2.Width,Image2.Height,
Image1.Canvas.Handle, X-20,Y-20,40,40,SRCCOPY);
Image2.Refresh;
Screen.Cursors[1]:=LoadCursorFromFile('MAGNIFY.CUR');
Self.Cursor:=1;
end;
程序首先會(huì)調(diào)用StretchBlt函數(shù),以鼠標(biāo)當(dāng)前位置作為中心點(diǎn),以邊長為40選中Image1組件上的局部圖像,并放大此局部圖像到Image2組件上。然后通過調(diào)用Image2組件的Refresh方法以刷新Image2組件的顯示。最后設(shè)置鼠標(biāo)指針為新的形狀。
程序代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Image2: TImage;
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Image1MouseMove(Sender:TObject;Shift:TShiftState;X,Y: Integer);
begin
StretchBlt(Image2.Canvas.Handle,0,0,Image2.Width,Image2.Height,Image1.Canvas.Handle, X-20,Y-20,40,40,SRCCOPY);
Image2.Refresh;
Screen.Cursors[1]:=LoadCursorFromFile('MAGNIFY.CUR');
Self.Cursor:=1;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,Y: Integer);
begin
Screen.Cursors[1]:=crDefault;
Self.Cursor:=1;
end;
end.
保存文件,然后按F9鍵運(yùn)行程序,程序運(yùn)行。
放大圖像是一個(gè)優(yōu)秀的看圖軟件必備的功能,本實(shí)例提供了一種非常簡便易行的方法,不但代碼數(shù)量少,而且執(zhí)行效率高。
希望本文所述對大家的Delphi程序設(shè)計(jì)有所幫助。
上一篇:Delphi實(shí)現(xiàn)獲取進(jìn)程列表及相關(guān)信息的實(shí)例
欄 目:Delphi
下一篇:Delphi 在窗體上創(chuàng)建自己光標(biāo)的實(shí)例
本文標(biāo)題:Delphi基本圖像處理方法匯總
本文地址:http://www.jygsgssxh.com/a1/Delphi/8600.html
您可能感興趣的文章
- 01-10在Delphi實(shí)現(xiàn)在數(shù)據(jù)庫中存取圖像的圖文演示無錯(cuò)
- 01-10delphi建立、讀取、存貯INI文件的方法《三》
- 01-10Delphi Command模式
- 01-10delphi 正弦曲線圖
- 01-10delphi建立、讀取、存貯INI文件的方法《二》
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函數(shù)
- 01-10delphi中一個(gè)值得大家來考慮的DLL問題
- 01-10初探Delphi中的插件編程


閱讀排行
本欄相關(guān)
- 01-10在Delphi實(shí)現(xiàn)在數(shù)據(jù)庫中存取圖像的圖
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10delphi 正弦曲線圖
- 01-10Delphi Command模式
- 01-10delphi建立、讀取、存貯INI文件的方法
- 01-10插件管理框架 for Delphi(二)
- 01-10插件管理框架 for Delphi(一)
- 01-10Delphi中判斷文件是否為文本文件的函
- 01-10delphi中一個(gè)值得大家來考慮的DLL問題
- 01-10初探Delphi中的插件編程
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法


