Beispiel #1
0
        /// <summary>
        /// Quantizes the bitmap with the given code book.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        /// <param name="codeBook">The code book.</param>
        /// <returns></returns>
        public static byte[] QuantizeImage(Bitmap bitmap, VQCodeBook codeBook)
        {
            VQBlock[] blocks = CreateBlocks(bitmap, codeBook.BlockWidth, codeBook.BlockHeight);
            byte[]    result = new byte[blocks.Length];
            double    dump   = 0.0;

            for (int i = 0; i < blocks.Length; i++)
            {
                VQBlock block = blocks[i];
                result[i] = (byte)Nearest(block, codeBook.Entries, codeBook.Entries.Length, ref dump);
            }
            return(result);
        }
Beispiel #2
0
        private byte[] BitmapToRawVQResized(Bitmap source, int size, int minSize, VQCodeBook codeBook)
        {
            if (size > minSize)
            {
                minSize = size;
            }

            // Resize the image
            Bitmap img = new Bitmap(minSize, minSize, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img))
            {
                using (ImageAttributes attr = new ImageAttributes())
                {
                    attr.SetWrapMode(WrapMode.TileFlipXY);
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.DrawImage(source, new Rectangle(0, 0, size, size), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, attr);
                }
            }
            return(VectorQuantizer.QuantizeImage(img, codeBook));
        }
Beispiel #3
0
        private byte[] BitmapToRawVQ(Bitmap source, int codeBookSize, out byte[][] palette)
        {
            Bitmap img = source;

            byte[] destination = new byte[img.Width * img.Height];

            // If this is not a 32-bit ARGB bitmap, convert it to one
            if (img.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            {
                Bitmap newImage = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newImage))
                {
                    g.DrawImage(img, 0, 0, img.Width, img.Height);
                }
                img = newImage;
            }

            //Create code book
            m_codeBook = VectorQuantizer.CreateCodebook(img, codeBookSize / 4);

            //Write palette
            palette = new byte[codeBookSize][];
            for (int i = 0; i < m_codeBook.Entries.Length; i++)
            {
                byte[] pixels = m_codeBook.Entries[i].ToArray();
                for (int j = 0; j < 4; j++)
                {
                    int paletteIndex = i * 4 + j;
                    palette[paletteIndex]    = new byte[4];
                    palette[paletteIndex][0] = pixels[j * 4 + 3];
                    palette[paletteIndex][1] = pixels[j * 4 + 2];
                    palette[paletteIndex][2] = pixels[j * 4 + 1];
                    palette[paletteIndex][3] = pixels[j * 4];
                }
            }

            //Quantize image
            return(VectorQuantizer.QuantizeImage(img, m_codeBook));
        }
Beispiel #4
0
        /// <summary>
        /// Quantizes the bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        /// <param name="blockWidth">Width of the block.</param>
        /// <param name="blockHeight">Height of the block.</param>
        /// <param name="codebookSize">Size of the codebook as block count.</param>
        /// <param name="palette">The palette.</param>
        /// <returns></returns>
        public static byte[] QuantizeImage(Bitmap bitmap, int codeBookSize, int blockWidth = 2, int blockHeight = 2)
        {
            VQCodeBook codeBook = CreateCodebook(bitmap, codeBookSize, blockWidth, blockHeight);

            return(QuantizeImage(bitmap, codeBook));
        }