Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.WriteLine("library version: {0}", liq_version());

            int width  = 3;
            int height = 1;

            var attr = liq_attr_create();

            if (attr == IntPtr.Zero)
            {
                throw new Exception("can't create attr");
            }

            byte[] bitmap =   // R, G, B, A,  R, G, B, A, ...
            {
                111, 222,  33, 255,
                255,   0, 255, 255,
                255,   0, 255, 255,
            };
            var img = liq_image_create_rgba(attr, bitmap, width, height, 0);

            if (img == IntPtr.Zero)
            {
                throw new Exception("can't create image");
            }

            var res = liq_quantize_image(attr, img);

            if (res == IntPtr.Zero)
            {
                throw new Exception("can't quantize image");
            }

            var buffer_size = width * height;
            var remapped    = new byte[buffer_size];

            var err = liq_write_remapped_image(res, img, remapped, (UIntPtr)buffer_size);

            if (err != liq_error.LIQ_OK)
            {
                throw new Exception("remapping error");
            }

            Console.WriteLine("first pixel is {0}th palette entry", remapped[0]);

            liq_palette pal = (liq_palette)Marshal.PtrToStructure(liq_get_palette(res), typeof(liq_palette));

            Console.WriteLine("palette entries: {0}; red of first entry: {1}", pal.count, pal.entries[0].r);

            liq_image_destroy(img);
            liq_result_destroy(res);
            liq_attr_destroy(attr);
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        public static UInt16[] EncodePalette(liq_palette Palette)
        {
            List <UInt16> EncodedPalette = new List <UInt16>();

            foreach (liq_color Entry in Palette.Entries)
            {
                EncodedPalette.Add(Pack255(Entry.R, Entry.G, Entry.B, Entry.A));
            }

            UInt16[] Array = EncodedPalette.ToArray();

            while (Array.Length % 4 != 0)
            {
                Array = (UInt16[])Array.Append((UInt16)0);
            }


            return(Array);
        }
Ejemplo n.º 5
0
 public PalettedImage(byte[] _Indices, liq_palette _Palette)
 {
     Indices = _Indices;
     Palette = _Palette;
 }