Exemplo n.º 1
0
        public void NewResult()
        {
            var result = new ColorQuantizerResult(1, 1);

            Assert.Single(result.Bytes);
            Assert.Equal(4, result.Palette.Length);
        }
Exemplo n.º 2
0
 public ImageStats(string name, ColorQuantizerResult source)
 {
     Name   = name;
     Colors = new List <ColorStats>();
     foreach (var val in source.Stats)
     {
         var factor = (val.Value * 100.0f) / source.Size;
         var color  = new ColorStats(val.Key, factor);
         Colors.Add(color);
     }
     Colors = Colors
              .OrderByDescending(x => x.Factor)
              .ToList();
 }
Exemplo n.º 3
0
        public UVRT(Bitmap bitmap, PixelFormat pixelFormat, ImageType imageType, int mipLevels, byte[] palette = null)
        {
            if (!IsPow2(bitmap.Width) || !IsPow2(bitmap.Height))
            {
                throw new Exception("Width and height must be powers of two.");
            }

            if (imageType.IsIndexed())
            {
                byte indexBits = imageType.GetIndexBits();

                byte[]  indices;
                Color[] colorPalette;
                if (palette != null)
                {
                    indices      = new byte[bitmap.Width * bitmap.Height];
                    colorPalette = BitmapUtil.PaletteToColors(palette, pixelFormat);
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        for (int x = 0; x < bitmap.Width; x++)
                        {
                            int index = BitmapUtil.GetClosestColorIndex(bitmap.GetPixel(x, y), colorPalette, pixelFormat != PixelFormat.RGB565);
                            if (index == -1)
                            {
                                throw new Exception("Could not find a palette entry for pixel (" + x + ", " + y + ").");
                            }

                            indices[y * bitmap.Width + x] = (byte)(index & 0xFF);
                        }
                    }
                }
                else
                {
                    BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                                      ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    byte[] argbData = new byte[data.Stride * bitmap.Height];
                    Marshal.Copy(data.Scan0, argbData, 0, argbData.Length);
                    bitmap.UnlockBits(data);

                    WuAlphaColorQuantizer quantizer = new WuAlphaColorQuantizer();
                    ColorQuantizerResult  result    = quantizer.Quantize(argbData, 1 << indexBits);

                    indices      = result.Bytes;
                    colorPalette = new Color[1 << indexBits];
                    for (int i = 0; i < result.Palette.Length / 4; i++)
                    {
                        colorPalette[i] = Color.FromArgb(result.Palette[i * 4 + 3], result.Palette[i * 4 + 2], result.Palette[i * 4 + 1], result.Palette[i * 4 + 0]);
                    }
                }

                byte[] converted = new byte[bitmap.Width * bitmap.Height * indexBits / 8];
                for (int y = 0; y < bitmap.Height; y++)
                {
                    for (int x = 0; x < bitmap.Width; x++)
                    {
                        int dataIndex = y * bitmap.Width + x;
                        int index     = indices[dataIndex];
                        if (indexBits == 4)
                        {
                            int shift = (dataIndex & 1) * 4;
                            converted[dataIndex / 2] = (byte)((converted[dataIndex / 2] & ~(0xF << shift)) | ((index & 0xF) << shift));
                        }
                        else
                        {
                            converted[dataIndex] = (byte)(index & 0xFF);
                        }
                    }
                }

                this.Data    = BitmapUtil.Swizzle(converted, (bitmap.Width * imageType.GetIndexBits()) / 8, bitmap.Height, (bitmap.Width * imageType.GetIndexBits()) / 8, bitmap.Height);
                this.Palette = BitmapUtil.ColorsToPalette(colorPalette, pixelFormat);
            }
            else
            {
                if (pixelFormat == PixelFormat.DXT1A || pixelFormat == PixelFormat.DXT1A_EXT)
                {
                    this.Data = BitmapUtil.CompressDxt1(bitmap.Width, bitmap.Height, pixelFormat == PixelFormat.DXT1A_EXT, bitmap);
                }
                else
                {
                    int pixelSize = pixelFormat.GetSize();

                    byte[] converted = new byte[bitmap.Width * bitmap.Height * pixelSize];
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        for (int x = 0; x < bitmap.Width; x++)
                        {
                            int pixel = pixelFormat.FromColor(bitmap.GetPixel(x, y));
                            for (int j = 0; j < pixelSize; j++)
                            {
                                converted[(y * bitmap.Width + x) * pixelSize + j] = (byte)((pixel >> (j * 8)) & 0xFF);
                            }
                        }
                    }

                    this.Data = BitmapUtil.Swizzle(converted, bitmap.Width * pixelSize, bitmap.Height, bitmap.Width * pixelSize, bitmap.Height);
                }

                this.Palette = new byte[0];
            }

            this.Format    = pixelFormat;
            this.Type      = imageType;
            this.Width     = (ushort)bitmap.Width;
            this.Height    = (ushort)bitmap.Height;
            this.MipLevels = mipLevels;
        }