基本图象处理代码(1)

2008-04-09 04:27:40来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

[图像的旋转和翻转]

以下代码只能用于24位色的位图,用于处理其它色深的位图请自行修改。

//旋转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;
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;

//旋转180度
procedure Rotate180(const Bitmap:TBitmap);
var
i, j: Integer;
row1, row2, row3, rowRGB: PRGBTriple;;
Width, Height: Integer;
begin
Width := Bitmap.Width;
Height := Bitmap.Height;
GetMem(rowRGB, Width * 3);
Bitmap.PixelFormat := pf32bit;
for j := 0 to (Height div 2) - 1 do
begin
row1 := Bitmap.ScanLine[j];
row3 := row1;
Inc(row3, Width - 1);
for i := 0 to (Width div 2) - 1 do
begin
rowRGB^ := row1^;
row1^ := row3^;
row3^ := rowRGB^;
Inc(row1);
Dec(row3);
end;
row2 := Bitmap.ScanLine[Height - j - 1];
row3 := row2;
Inc(row3, Width - 1);
for i := 0 to (Width div 2) - 1 do
begin
rowRGB^ := row2^;
row2^ := row3^;
row3^ := rowRGB^;
Inc(row2);
Dec(row3);
end;
Dec(row1, (Width div 2));
Dec(row2, (Width div 2));
CopyMemory(rowRGB, row1, Width * 3);
CopyMemory(row1, row2, Width * 3);
CopyMemory(row2, rowRGB, Width * 3);
end;
if Height mod 2 = 1 then
begin
row1 := Bitmap.ScanLine[(Width div 2) 1];
row2 := row1;
Inc(row2, Width - 1);
for i := 0 to (Width div 2) - 1 do
begin
rowRGB^ := row1^;
row1^ := row2^;
row2^ := rowRGB^;
Inc(row1);
Dec(row2);
end;
end;
FreeMem(rowRGB);
end;

//旋转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;
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;

前面为判断部分,如果是90,180,270这类特殊角度,将调用上面的代码,这样效率更高!

//任意角度
function RotateBitmap(const 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);//如果是旋转90度,直接调用上面的代码
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);//如果是旋转180度,直接调用上面的过程
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);//如果是旋转270度,直接调用上面的过程
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);

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:由图像的灰度化看基本图像处理(3)

下一篇:Borland重操旧业 推出新品再次叫板微软