public unsafe static Bitmap QuantizeImage(Bitmap Image, int Colors) { byte[] BitmapArray = MakeBitmapArray(Image); var attr = Liq.liq_attr_create(); Liq.liq_set_max_colors(attr, Colors); Liq.liq_set_min_posterization(attr, 2); var LiqImage = Liq.liq_image_create_rgba(attr, BitmapArray, Image.Width, Image.Height, 0); var LiqQuantized = Liq.liq_quantize_image(attr, LiqImage); byte[] Texture = new byte[Image.Width * Image.Height]; Liq.liq_write_remapped_image(LiqQuantized, LiqImage, Texture, (UIntPtr)(Image.Width * Image.Height)); liq_palette LiqPalette = (liq_palette)Marshal.PtrToStructure(Liq.liq_get_palette(LiqQuantized), typeof(liq_palette)); Bitmap BMP; fixed(byte *p = Texture) { IntPtr ptr = (IntPtr)p; BMP = new Bitmap(Image.Width, Image.Height, Image.Width, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, ptr); } System.Drawing.Imaging.ColorPalette pal = BMP.Palette; for (int i = 0; i < BMP.Palette.Entries.Count(); i++) { pal.Entries[i] = Color.FromArgb(LiqPalette.Entries[i].A, LiqPalette.Entries[i].R, LiqPalette.Entries[i].G, LiqPalette.Entries[i].B); } BMP.Palette = pal; return(BMP); }
public static PalettedImage CreatePalettedImage(Bitmap Image, int Format) { byte[] BitmapArray = MakeBitmapArray(Image); var attr = Liq.liq_attr_create(); Liq.liq_set_max_colors(attr, GetColorNum(Format)); Liq.liq_set_min_posterization(attr, 2); var LiqImage = Liq.liq_image_create_rgba(attr, BitmapArray, Image.Width, Image.Height, 0); var LiqQuantized = Liq.liq_quantize_image(attr, LiqImage); byte[] Texture = new byte[Image.Width * Image.Height]; Liq.liq_write_remapped_image(LiqQuantized, LiqImage, Texture, (UIntPtr)(Image.Width * Image.Height)); liq_palette LiqPalette = (liq_palette)Marshal.PtrToStructure(Liq.liq_get_palette(LiqQuantized), typeof(liq_palette)); return(new PalettedImage(Texture, LiqPalette)); }