【问题标题】:How to reduce colors to a specified number in Delphi?如何在 Delphi 中将颜色减少到指定数量?
【发布时间】:2013-03-23 15:27:11
【问题描述】:

如何在 Delphi 中将颜色减少到指定数量 (

 Bmp.PixelFormat := pf8bit;

因为那样我无法控制颜色的数量。我不想抖动,因为我已经知道如何抖动 256 或更少颜色的图像。

我发现了这个Median Cut implementation,但它是 1990 年的纯 Pascal 并且:

  1. 在 Delphi 中无法编译
  2. 说它是共享软件,售价 25 德国马克
  3. 看起来(不知何故)不必要的复杂

我只想将TBitmap32(Graphics32 位图类,仅支持 32 位颜色)减少到 8 位-

我使用的 Delphi:7, 2005, XE3。

【问题讨论】:

  • 那么,你想用什么算法呢?
  • @DavidHeffernan 我猜 Median Cut 会很好。我读到它给出了可接受的结果,但找不到任何算法描述。我知道还有基于八叉树的算法——但也没有任何详细的描述。

标签: delphi colors bitmap reduction quantization


【解决方案1】:

使用 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);

【讨论】:

  • 谢谢,但我无法指定要获得多少种颜色,例如:16 或 32。
  • 很棒,甚至可以在每个 Delphi 中使用来自 tolderlund.eu/delphi 的 Malander 的 TGifImage。谢谢!
猜你喜欢
  • 2013-01-15
  • 1970-01-01
  • 2013-10-23
  • 2013-07-06
  • 1970-01-01
  • 2011-08-19
  • 2017-05-01
相关资源
最近更新 更多