pS1 := GrayBmp.ScanLine[0]; //源灰度图
pS2 := PByteArray(Integer(pS1) - BytesLineGray);
pS3 := PByteArray(Integer(pS2) - BytesLineGray);
pS4 := PByteArray(Integer(pS3) - BytesLineGray);
pDes := FMaskPathBmp.ScanLine[0];
// pDes := PByteArray(Integer(FMaskBmp.scanLine[0]) + (FMaskBmp.Height - 1) * BytesLineMask);
//目标灰度为源矩形块的平均值
case Quality of
aqHigh:
begin //高精度4X4采样
for i := 0 to FMaskPathBmp.Height - 1 do
begin
for j := 0 to FmaskPathBmp.Width - 1 do
begin
x := j * 4;
pDes^[j] :=
(pS1^[x] + pS1^[x + 1] + pS1^[x + 2] + pS1^[x + 3] +
pS2^[x] + pS2^[x + 1] + pS2^[x + 2] + pS2^[x + 3] +
pS3^[x] + pS3^[x + 1] + pS3^[x + 2] + pS3^[x + 3] +
pS4^[x] + pS4^[x + 1] + pS4^[x + 2] + pS4^[x + 3]) shr 4;
end;
pS1 := PByteArray(Integer(pS4) - BytesLineGray);
pS2 := PByteArray(Integer(pS1) - BytesLineGray);
pS3 := PByteArray(Integer(pS2) - BytesLineGray);
pS4 := PByteArray(Integer(pS3) - BytesLineGray);
pDes := PByteArray(Integer(pDes) - BytesLineMask);
end;
end;
aqNormal:
begin //普通精度3X3采样
for i := 0 to FMaskPathBmp.Height - 1 do
begin
for j := 0 to FMaskPathBmp.Width - 1 do
begin
x := j * 3;
pDes^[j] :=
(pS1^[x] + pS1^[x + 1] + pS1^[x + 2] shr 1 +
pS2^[x] + pS2^[x + 1] + pS2^[x + 2] +
pS3^[x] shr 1 + pS3^[x + 1] + pS3^[x + 2]) shr 3;
end;
pS1 := PByteArray(Integer(pS3) - BytesLineGray);
pS2 := PByteArray(Integer(pS1) - BytesLineGray);
pS3 := PByteArray(Integer(pS2) - BytesLineGray);
pDes := PByteArray(Integer(pDes) - BytesLineMask);
end;
end;
aqLow:
begin //低精度2X2采样
for i := 0 to FMaskPathBmp.Height - 1 do
begin
for j := 0 to FMaskPathBmp.Width - 1 do
begin
x := j * 2;
pDes^[j] :=
(pS1^[x] + pS1^[x + 1] +
pS2^[x] + pS2^[x + 1]) shr 2;
end;
pS1 := PByteArray(Integer(pS2) - BytesLineGray);
pS2 := PByteArray(Integer(pS1) - BytesLineGray);
pDes := PByteArray(Integer(pDes) - BytesLineMask);
end;
end;
aqNone:
begin //无平滑效果
for i := 0 to FMaskPathBmp.Height - 1 do
begin
CopyMemory(pDes, pS1, FMaskPathBmp.Width);
pS1 := PByteArray(Integer(pS1) - BytesLineGray);
pDes := PByteArray(Integer(pDes) - BytesLineMask);
end;
end;
end;
比起这段代码快多了。虽然下面这段代码更小,由于要在内循环内运行许多次加法 除法运算,
速度要慢很多
for y := 0 to GrayBmp.Height Div Scale -1 do
begin
DestScan := FMaskPathBmp.ScanLine[y];
for x := 0 to GrayBmp.Width Div Scale -1 do
begin
tot := 0;
for j := 0 to Scale - 1 do
begin
BigScan := pByteArray(GrayBmp.ScanLine[Scale * y + j] );
for i := 0 to Scale -1 do
begin
tot := tot + BigScan[Scale * x+i];
end;
end;
DestScan[x] := tot div (Scale * Scale);
end;
end;
我找到一个MMX的,不过用你哪个也像是性能差不多
////////////////////////////////////////////////////////////////////////////////
// File: PegtopResampleUtils.pas
// Version: 1.00
// Date: 16 Mar 2005 created 1.00
// Author: Jens Gruschel (GRU)
// Copyright: (c) 2005 Jens Gruschel
// Website: http://www.pegtop.net/delphi
////////////////////////////////////////////////////////////////////////////////
// Description:
// Procedures to resample bitmaps using MMX.
////////////////////////////////////////////////////////////////////////////////
// License:
// Please read the license.txt file that comes with this software
// and visit http://www.pegtop.net/delphi
////////////////////////////////////////////////////////////////////////////////
// Documentation / Installation:
// Please read the readme.txt file that comes with this software
// and visit http://www.pegtop.net/delphi
////////////////////////////////////////////////////////////////////////////////