一种快速实施、具有多种选择的廉价方法是使用 TGIFImage
uses
gifimg;
Procedure ReduceTo8Bit(var bmp:TBitmap; ColorReduction: TColorReduction; DitherMode: TDitherMode);
var
GI:TGifImage;
begin
GI:=TGifImage.Create;
try
GI.DitherMode := DitherMode;
GI.ColorReduction := ColorReduction;
GI.Assign(bmp);
bmp.Assign(GI.Bitmap);
finally
GI.Free;
end;
end;
测试
procedure TForm3.Button2Click(Sender: TObject);
var
bmp:TBitmap;
begin
bmp:=TBitmap.Create;
try
bmp.LoadFromFile('C:\bilder\bummi.bmp');
ReduceTo8Bit(bmp,rmQuantizeWindows,dmSierra);
bmp.SaveToFile('C:\bilder\bummi_8bit.bmp');
finally
bmp.Free;
end;
end;
如果必须设置每个像素的位数,则更简单的方法是使用来自 gifimg 的 ReduceColors 和 rmQuantize
// BytesPerPixel integer with range of Range 3 - 8
DestBMP := ReduceColors(SourceBMP,rmQuantize,dmNearest,BytesPerPixel,0);