// 种子填充,从VC资料中翻译过来
procedure Zztc(bmp: TBitmap; w, h, x, y: integer);
// bmp: 为要处理的位图
// w, h: 位图的宽度、高度
// x, y: 初始种子点
type
TSeed = record
Left: integer;
Top : integer;
end;
ASeed = array of TSeed; // 用来模拟堆栈
const
tempCol = 10; // 临时颜色变量,填充后
var
xCurrent, yCurrent, // 当前要处理的点坐标
StackPoint: integer; // 指向堆栈顶的指针
Seeds: ASeed;
colCurrent: Byte; // 当前点颜色
pCurrent: PByteArray;
begin
// 这里是关键,bmp 只有黑白两色,
// bmp 是24位图,但为了后面处理方便,用以下方法改为 8 位色图
if bmp.PixelFormat <> pf8bit then
bmp.PixelFormat := pf8bit;
// 就出了以上问题
SetLength(Seeds, w * h);
Seeds[0].Left := x; // 初始化
Seeds[0].Top := y;
StackPoint := 1;
while StackPoint <> 0 do
begin
xCurrent := Seeds[StackPoint - 1].Left;
yCurrent := Seeds[StackPoint - 1].Top;
dec(StackPoint);
pCurrent := bmp.ScanLine[yCurrent];
// 当前点标记为临时颜色
pCurrent[xCurrent] := tempCol;
// 判断左面的点
if xCurrent > 0 then
begin
pCurrent := bmp.ScanLine[yCurrent];
colCurrent := pCurrent[xCurrent - 1];
if (colCurrent <> 0) and (colCurrent <> tempCol) then
begin
inc(StackPoint);
Seeds[StackPoint - 1].Left := xCurrent - 1;
Seeds[StackPoint - 1].Top := yCurrent;
end;
end;
// 判断下面的点
if yCurrent < h - 1 then
begin
pCurrent := bmp.ScanLine[yCurrent + 1];
colCurrent := pCurrent[xCurrent];
if (colCurrent <> 0) and (colCurrent <> tempCol) then
begin
inc(StackPoint);
Seeds[StackPoint - 1].Left := xCurrent;
Seeds[StackPoint - 1].Top := yCurrent + 1;
end;
end;
// 判断右面的点
if xCurrent < w - 1 then
begin
pCurrent := bmp.ScanLine[yCurrent];
colCurrent := pCurrent[xCurrent + 1];
if (colCurrent <> 0) and (colCurrent <> tempCol) then
begin
inc(StackPoint);
Seeds[StackPoint - 1].Left := xCurrent + 1;
Seeds[StackPoint - 1].Top := yCurrent;
end;
end;
// 判断上面的点
if yCurrent > 0 then
begin
pCurrent := bmp.ScanLine[yCurrent - 1];
colCurrent := pCurrent[xCurrent];
if (colCurrent <> 0) and (colCurrent <> tempCol) then
begin
inc(StackPoint);
Seeds[StackPoint - 1].Left := xCurrent;
Seeds[StackPoint - 1].Top := yCurrent - 1;
end;
end;
end;
Seeds := nil;
// 将以上填充了临时颜色的点以外的所有点填充成黑色,为了填满黑色区域内的空洞
for yCurrent := 0 to h - 1 do
begin
pCurrent := bmp.ScanLine[yCurrent];
for xCurrent := 0 to w - 1 do
begin
if pCurrent[xCurrent] = tempCol then
pCurrent[xCurrent] := 19
else
pCurrent[xCurrent] := 0;
end;
end;
end;
// 调入位图
procedure TForm1.btnLoadClick(Sender: TObject);
begin
Image1.Picture.Bitmap.LoadFromFile(BmpFile);
end;
// 种子填充
procedure TForm1.btnZzTcClick(Sender: TObject);
begin
with Image1.Picture do
Zztc(Bitmap, Bitmap.Width, Bitmap.Height, 40, 30);
Image1.Refresh;
end;
重复操作如下: 调入图像(btnLoadClick)--种子填充(btnZzTcClick)
结果第一次成功,第二次以后就不行了!
非要重新启动程序!!!!!!!!!!
我看了很多关于减少颜色数的贴子,就是搞不明白:像这种本来就只有黑白两色的24位色图,如果将其变为8位图
if bmp.PixelFormat <> pf8bit then
bmp.PixelFormat := pf8bit;
所以请大家帮忙,