中国开发网: 论坛: 程序员情感CBD: 贴子 271131
没脾气2x: 给你贴个我用delphi写的锐化函数,应该还能再优化
procedure NoxImage_Sharpen( aSrcBitmap: TBitmap; aTarBitmap: TBitmap );
const
templatePhotoshopSharpenMore
: array [0..2, 0..2] of Double = ( ( -0.25, -0.25, -0.25), (-0.25, 3, -0.25), (-0.25, -0.25, -0.25) );
templatePhotoshopSharpen
: array [0..2, 0..2] of Double = ( ( 0, -0.25, 0), (-0.25, 2, -0.25), (0, -0.25, 0) );
templateMySharpen
: array [0..2, 0..2] of Double = ( ( -0.05, -0.2, 0-0.05), (-0.2, 2, -0.2), (-0.05, -0.2, -0.05) );
templateSharpenKeyPoint: TPoint = (X: 1; Y: 1);

var
template: array [0..2, 0..2] of Double;
type
TRGBRecord = record
R: Byte;
G: Byte;
B: Byte;
Reversed: Byte;
end;
var
aSrcBitmapBuf: array of TRGBRecord; { y*width + x }
aTarBitmapBuf: array of TRGBRecord;
aBitmapWidth: Integer;
aBitmapHeight: Integer;
var
// 循环中的 TarBitmap 点
ix, iy: Integer;

// 内循环模板中的坐标
sxi, syi: Integer;
sx, sy: Integer;

// 色彩值临时
colorR: Double;
colorG: Double;
colorB: Double;

// 原色值和新色值
srcRGB, tarRGB: TRGBRecord;
srcColor: TColor;
tarColor: TColor;

// TarBitmap 一个点在 SrcBitmap 中所占的空间比例
mx, my: Double;

aPointer: PByteArray;

o1, o2, o3: Double;

begin
Move( templatePhotoshopSharpen, template, SizeOf(template) );
aBitmapWidth := aSrcBitmap.Width;
aBitmapHeight := aSrcBitmap.Height;

SetLength( aSrcBitmapBuf, aBitmapWidth * aBitmapHeight );
SetLength( aTarBitmapBuf, aBitmapWidth * aBitmapHeight );

for iy:=0 to aBitmapHeight - 1 do
begin
aPointer := aSrcBitmap.ScanLine[iy];

for ix:=0 to aBitmapWidth - 1 do
begin
aSrcBitmapBuf[iy*aBitmapWidth + ix].B := aPointer[ix*3 + 0];
aSrcBitmapBuf[iy*aBitmapWidth + ix].G := aPointer[ix*3 + 1];
aSrcBitmapBuf[iy*aBitmapWidth + ix].R := aPointer[ix*3 + 2];
end;
end;

aTarBitmap.Width := aBitmapWidth;
aTarBitmap.Height := aBitmapHeight;

// 遍历 TarBitmap 横向所有点
for ix := 0 to aBitmapWidth - 1 do
begin

// 遍历 TarBitmap 纵向所有点
for iy := 0 to aBitmapHeight - 1 do
begin

srcRGB := aSrcBitmapBuf[iy*aBitmapWidth + ix];

if (ix < templateSharpenKeyPoint.X)
or (iy < templateSharpenKeyPoint.Y)
or (ix > aBitmapWidth - 3 + templateSharpenKeyPoint.X )
or (iy > aBitmapHeight - 3 + templateSharpenKeyPoint.Y )
then
begin
tarRGB := srcRGB;
end else
begin

colorR := 0;
colorG := 0;
colorB := 0;

for sxi := 0 to 2 do
begin
sx := ix - templateSharpenKeyPoint.X + sxi;

for syi := 0 to 2 do
begin
sy := iy - templateSharpenKeyPoint.Y + syi;

srcRGB := aSrcBitmapBuf[sy*aBitmapWidth + sx];

colorR := colorR + srcRGB.R * template[sxi, syi];
colorG := colorG + srcRGB.G * template[sxi, syi];
colorB := colorB + srcRGB.B * template[sxi, syi];

end;
end;

if colorR < 0 then colorR := 0;
if colorR > 255 then colorR := 255;
if colorG < 0 then colorG := 0;
if colorG > 255 then colorG := 255;
if colorB < 0 then colorB := 0;
if colorB > 255 then colorB := 255;

tarRGB.R := Round(colorR);
tarRGB.G := Round(colorG);
tarRGB.B := Round(colorB);
end;

aTarBitmapBuf[iy*aBitmapWidth + ix] := tarRGB;

end;
end;

aTarBitmap.PixelFormat := pf24bit;
for iy:=0 to aBitmapHeight - 1 do
begin
aPointer := aTarBitmap.ScanLine[iy];

for ix:=0 to aBitmapWidth - 1 do
begin
aPointer[ix*3 + 0] := aTarBitmapBuf[iy*aBitmapWidth + ix].B;
aPointer[ix*3 + 1] := aTarBitmapBuf[iy*aBitmapWidth + ix].G;
aPointer[ix*3 + 2] := aTarBitmapBuf[iy*aBitmapWidth + ix].R;
end;
end;

end;
Notemper2x 3.1 ( ̄ε( ̄#)
没脾气2x 之 个人综合篇: http://notemper2x.cndev.org/
我的 panoramio 相册: http://panoramio.com/user/zhaixudong
我的 flickr相册: http://www.flickr.com/photos/notemper2x/



QQ号20250出售,售价400,000元整(5位、皇冠80级、VIP7)

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录