5,388
社区成员
发帖
与我相关
我的任务
分享
procedure Sharp(Dest, Source: TBitmap; Threshold: Integer; Matrix: array of Integer); stdcall;
var
Gray: array of Byte;
p: PRGBQuad;
pg, py, px: pByte;
m: PInteger;
x, y, i, j: Integer;
right, bottom: Integer;
mSize, Radius, n: Integer;
begin
n := Length(Matrix);
if Source.Empty or (n < 9) then Exit;
mSize := Trunc(Sqrt(n));
Radius := mSize shr 1;
Source.PixelFormat := pf24bit;
Dest.PixelFormat := pf24bit;
if Dest.Empty or (Dest.Width <> Source.Width) or (Dest.Height <> Source.Height) then
begin
Dest.Width := Source.Width;
Dest.Height := Source.Height;
end;
SetLength(Gray, Dest.Width * Dest.Height);
pg := @Gray[0];
for y := 0 to Source.Height - 1 do
begin
p := Source.ScanLine[y];
for x := 1 to Source.Width do
begin
pg^ := (p.rgbRed * 306 + p.rgbGreen * 601 + p.rgbBlue * 117 + 512) shr 10;
Inc(pg);
Inc(Integer(p), 3);
end;
end;
pg := @Gray[0];
bottom := Dest.Height - Radius - 1;
right := Dest.Width - Radius - 1;
for y := Radius to bottom do
begin
p := Dest.ScanLine[y];
Inc(Integer(p), Radius * 3);
for x := Radius to right do
begin
n := 0;
py := pg;
m := @Matrix[0];
for i := 1 to mSize do
begin
px := py;
for j := 1 to mSize do
begin
Inc(n, px^ * m^);
Inc(px);
Inc(m);
end;
Inc(py, Dest.Width);
end;
if n > Threshold then p.rgbRed := 0 else p.rgbRed := 255;
p.rgbGreen := p.rgbRed;
p.rgbBlue := p.rgbRed;
Inc(Integer(p), 3);
Inc(pg);
end;
Inc(pg, mSize - 1);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
marrs: array[0..24] of Integer = (
0, 0, -1, 0, 0,
0, -1, -2, -1, 0,
-1, -2, 16, -2, -1,
0, -1, -2, -1, 0,
0, 0, -1, 0, 0
);
var
bmp, newBmp: TBitmap;
begin
bmp := TBitmap.Create;
bmp.LoadFromFile('d:\source.bmp');
newBmp := TBitmap.Create;
Sharp(newBmp, bmp, 127, marrs);
Canvas.Draw(0, 0, newBmp);
newBmp.Free;
bmp.Free;
end;