Пример #1
0
        public void Convert32To8()
        {
            if (this.BitsPerPixel != 32)
            {
                return;
            }

            if (this.ImageData == null)
            {
                return;
            }

            int length = this.ImageData.Length / 4;

            byte[] alphaData = new byte[length];
            bool hasAlpha = false;

            for (int i = 0; i < length; i++)
            {
                byte a = this.ImageData[i * 4 + 3];

                alphaData[i] = a;

                if (a != (byte)255)
                {
                    hasAlpha = true;
                }
            }

            this.AlphaData = hasAlpha ? alphaData : null;

            var imageColors = Enumerable.Range(0, length)
                .Select(t =>
                {
                    byte r = (byte)((this.ImageData[t * 4 + 2] * (0x1fU * 2) + 0xffU) / (0xffU * 2));
                    byte g = (byte)((this.ImageData[t * 4 + 1] * (0x3fU * 2) + 0xffU) / (0xffU * 2));
                    byte b = (byte)((this.ImageData[t * 4 + 0] * (0x1fU * 2) + 0xffU) / (0xffU * 2));

                    r = (byte)((r * (0xffU * 2) + 0x1fU) / (0x1fU * 2));
                    g = (byte)((g * (0xffU * 2) + 0x3fU) / (0x3fU * 2));
                    b = (byte)((b * (0xffU * 2) + 0x1fU) / (0x1fU * 2));

                    return Color.FromArgb((int)r, (int)g, (int)b);
                });

            var colors = imageColors
                .Distinct()
                .ToArray();

            if (colors.Length <= 256)
            {
                byte[] palette = new byte[colors.Length * 3];

                for (int i = 0; i < colors.Length; i++)
                {
                    palette[i * 3 + 0] = colors[i].R;
                    palette[i * 3 + 1] = colors[i].G;
                    palette[i * 3 + 2] = colors[i].B;
                }

                var imageData = imageColors
                    .Select(t =>
                    {
                        for (int i = 0; i < colors.Length; i++)
                        {
                            if (colors[i] == t)
                            {
                                return (byte)i;
                            }
                        }

                        return (byte)0;
                    })
                    .ToArray();

                this.SetPaletteColors(palette);
                this.ImageData = imageData;
            }
            else
            {
                byte[] data = new byte[length * 4];

                for (int i = 0; i < length; i++)
                {
                    byte r = (byte)((this.ImageData[i * 4 + 2] * (0x1fU * 2) + 0xffU) / (0xffU * 2));
                    byte g = (byte)((this.ImageData[i * 4 + 1] * (0x3fU * 2) + 0xffU) / (0xffU * 2));
                    byte b = (byte)((this.ImageData[i * 4 + 0] * (0x1fU * 2) + 0xffU) / (0xffU * 2));

                    r = (byte)((r * (0xffU * 2) + 0x1fU) / (0x1fU * 2));
                    g = (byte)((g * (0xffU * 2) + 0x3fU) / (0x3fU * 2));
                    b = (byte)((b * (0xffU * 2) + 0x1fU) / (0x1fU * 2));

                    data[i * 4 + 2] = r;
                    data[i * 4 + 1] = g;
                    data[i * 4 + 0] = b;
                }

                var image = new ColorQuant.WuColorQuantizer().Quantize(data);

                byte[] palette = new byte[768];

                for (int i = 0; i < image.Palette.Length / 4; i++)
                {
                    palette[i * 3 + 0] = image.Palette[i * 4 + 2];
                    palette[i * 3 + 1] = image.Palette[i * 4 + 1];
                    palette[i * 3 + 2] = image.Palette[i * 4 + 0];
                }

                this.SetPaletteColors(palette);
                this.ImageData = image.Bytes;
            }
        }
Пример #2
0
        public void ConvertToFormat24()
        {
            if (this.Format == DatImageFormat.Format24)
            {
                return;
            }

            var data = this.GetImageData();

            if (data == null)
            {
                return;
            }

            int length = data.Length / 4;

            byte[] palette;
            byte[] colors;

            var dataColors = Enumerable.Range(0, length)
                .Select(t => Color.FromArgb(data[t * 4 + 2], data[t * 4 + 1], data[t * 4 + 0]))
                .Distinct()
                .ToArray();

            if (dataColors.Length <= 256)
            {
                palette = new byte[dataColors.Length * 3];

                for (int i = 0; i < dataColors.Length; i++)
                {
                    palette[i * 3 + 0] = dataColors[i].R;
                    palette[i * 3 + 1] = dataColors[i].G;
                    palette[i * 3 + 2] = dataColors[i].B;
                }

                colors = Enumerable.Range(0, length)
                    .Select(t => Color.FromArgb(data[t * 4 + 2], data[t * 4 + 1], data[t * 4 + 0]))
                    .Select(t =>
                    {
                        for (int i = 0; i < dataColors.Length; i++)
                        {
                            if (dataColors[i] == t)
                            {
                                return (byte)i;
                            }
                        }

                        return (byte)0;
                    })
                    .ToArray();
            }
            else
            {
                var image = new ColorQuant.WuColorQuantizer().Quantize(data);
                int paletteColorsCount = image.Palette.Length / 4;

                palette = new byte[paletteColorsCount * 3];

                for (int i = 0; i < paletteColorsCount; i++)
                {
                    palette[i * 3 + 0] = image.Palette[i * 4 + 2];
                    palette[i * 3 + 1] = image.Palette[i * 4 + 1];
                    palette[i * 3 + 2] = image.Palette[i * 4 + 0];
                }

                colors = image.Bytes;
            }

            byte[] imageData = new byte[palette.Length + length * 2];

            palette.CopyTo(imageData, 0);

            for (int i = 0; i < length; i++)
            {
                imageData[palette.Length + i * 2 + 0] = colors[i];
                imageData[palette.Length + i * 2 + 1] = data[i * 4 + 3];
            }

            this.Format = DatImageFormat.Format24;
            this.ColorsCount = (short)(palette.Length / 3);
            this.rawData = imageData;
        }
Пример #3
0
        private void InitData(int w, int h, byte[] data)
        {
            int length = w * h;

            uint[] palette = new uint[256];
            byte[] colors;

            for (int i = 0; i < length; i++)
            {
                if (data[i * 4 + 3] >= 0x80)
                {
                    data[i * 4 + 3] = 0xff;
                }
                else
                {
                    data[i * 4 + 0] = 0;
                    data[i * 4 + 1] = 0;
                    data[i * 4 + 2] = 0;
                    data[i * 4 + 3] = 0;
                }
            }

            var dataColors = Enumerable.Range(0, length)
                             .Select(t => (uint)((data[t * 4 + 0] << 16) | (data[t * 4 + 1] << 8) | data[t * 4 + 2]))
                             .Distinct()
                             .ToArray();

            if (dataColors.Length <= 256)
            {
                for (int i = 0; i < dataColors.Length; i++)
                {
                    palette[i] = dataColors[i];
                }

                for (int i = dataColors.Length; i < 256; i++)
                {
                    palette[i] = 0;
                }

                colors = Enumerable.Range(0, length)
                         .Select(t => (uint)((data[t * 4 + 0] << 16) | (data[t * 4 + 1] << 8) | data[t * 4 + 2]))
                         .Select(t =>
                {
                    for (int i = 0; i < dataColors.Length; i++)
                    {
                        if (dataColors[i] == t)
                        {
                            return((byte)i);
                        }
                    }

                    return((byte)0);
                })
                         .ToArray();
            }
            else
            {
                var image = new ColorQuant.WuColorQuantizer().Quantize(data);
                int paletteColorsCount = image.Palette.Length / 4;

                for (int i = 0; i < paletteColorsCount; i++)
                {
                    palette[i] = (uint)((image.Palette[i * 4 + 0] << 16) | (image.Palette[i * 4 + 1] << 8) | image.Palette[i * 4 + 2]);
                }

                for (int i = paletteColorsCount; i < 256; i++)
                {
                    palette[i] = 0;
                }

                colors = image.Bytes;
            }

            this.SetPalette(palette);
            this.SetRawData(w, h, colors);

            this.Compress(data);
        }
Пример #4
0
        private void InitData(int w, int h, byte[] data)
        {
            int length = w * h;

            uint[] palette = new uint[256];
            byte[] colors;

            for (int i = 0; i < length; i++)
            {
                if (data[i * 4 + 3] >= 0x80)
                {
                    data[i * 4 + 3] = 0xff;
                }
                else
                {
                    data[i * 4 + 0] = 0;
                    data[i * 4 + 1] = 0;
                    data[i * 4 + 2] = 0;
                    data[i * 4 + 3] = 0;
                }
            }

            var dataColors = Enumerable.Range(0, length)
                .Select(t => (uint)((data[t * 4 + 0] << 16) | (data[t * 4 + 1] << 8) | data[t * 4 + 2]))
                .Distinct()
                .ToArray();

            if (dataColors.Length <= 256)
            {
                for (int i = 0; i < dataColors.Length; i++)
                {
                    palette[i] = dataColors[i];
                }

                for (int i = dataColors.Length; i < 256; i++)
                {
                    palette[i] = 0;
                }

                colors = Enumerable.Range(0, length)
                    .Select(t => (uint)((data[t * 4 + 0] << 16) | (data[t * 4 + 1] << 8) | data[t * 4 + 2]))
                    .Select(t =>
                    {
                        for (int i = 0; i < dataColors.Length; i++)
                        {
                            if (dataColors[i] == t)
                            {
                                return (byte)i;
                            }
                        }

                        return (byte)0;
                    })
                    .ToArray();
            }
            else
            {
                var image = new ColorQuant.WuColorQuantizer().Quantize(data);
                int paletteColorsCount = image.Palette.Length / 4;

                for (int i = 0; i < paletteColorsCount; i++)
                {
                    palette[i] = (uint)((image.Palette[i * 4 + 0] << 16) | (image.Palette[i * 4 + 1] << 8) | image.Palette[i * 4 + 2]);
                }

                for (int i = paletteColorsCount; i < 256; i++)
                {
                    palette[i] = 0;
                }

                colors = image.Bytes;
            }

            this.SetPalette(palette);
            this.SetRawData(w, h, colors);

            this.Compress(data);
        }
Пример #5
0
        public void ConvertToFormat24()
        {
            if (this.Format == DatImageFormat.Format24)
            {
                return;
            }

            var data = this.GetImageData();

            if (data == null)
            {
                return;
            }

            int length = data.Length / 4;

            byte[] palette;
            byte[] colors;

            var dataColors = Enumerable.Range(0, length)
                             .Select(t => Color.FromArgb(data[t * 4 + 2], data[t * 4 + 1], data[t * 4 + 0]))
                             .Distinct()
                             .ToArray();

            if (dataColors.Length <= 256)
            {
                palette = new byte[dataColors.Length * 3];

                for (int i = 0; i < dataColors.Length; i++)
                {
                    palette[i * 3 + 0] = dataColors[i].R;
                    palette[i * 3 + 1] = dataColors[i].G;
                    palette[i * 3 + 2] = dataColors[i].B;
                }

                colors = Enumerable.Range(0, length)
                         .Select(t => Color.FromArgb(data[t * 4 + 2], data[t * 4 + 1], data[t * 4 + 0]))
                         .Select(t =>
                {
                    for (int i = 0; i < dataColors.Length; i++)
                    {
                        if (dataColors[i] == t)
                        {
                            return((byte)i);
                        }
                    }

                    return((byte)0);
                })
                         .ToArray();
            }
            else
            {
                var image = new ColorQuant.WuColorQuantizer().Quantize(data);
                int paletteColorsCount = image.Palette.Length / 4;

                palette = new byte[paletteColorsCount * 3];

                for (int i = 0; i < paletteColorsCount; i++)
                {
                    palette[i * 3 + 0] = image.Palette[i * 4 + 2];
                    palette[i * 3 + 1] = image.Palette[i * 4 + 1];
                    palette[i * 3 + 2] = image.Palette[i * 4 + 0];
                }

                colors = image.Bytes;
            }

            byte[] imageData = new byte[palette.Length + length * 2];

            palette.CopyTo(imageData, 0);

            for (int i = 0; i < length; i++)
            {
                imageData[palette.Length + i * 2 + 0] = colors[i];
                imageData[palette.Length + i * 2 + 1] = data[i * 4 + 3];
            }

            this.Format      = DatImageFormat.Format24;
            this.ColorsCount = (short)(palette.Length / 3);
            this.rawData     = imageData;
        }
Пример #6
0
        public void Convert32To8()
        {
            if (this.BitsPerPixel != 32)
            {
                return;
            }

            if (this.ImageData == null)
            {
                return;
            }

            int length = this.ImageData.Length / 4;

            byte[] alphaData = new byte[length];
            bool   hasAlpha  = false;

            for (int i = 0; i < length; i++)
            {
                byte a = this.ImageData[i * 4 + 3];

                alphaData[i] = a;

                if (a != (byte)255)
                {
                    hasAlpha = true;
                }
            }

            this.AlphaData = hasAlpha ? alphaData : null;

            var imageColors = Enumerable.Range(0, length)
                              .Select(t =>
            {
                byte r = (byte)((this.ImageData[t * 4 + 2] * (0x1fU * 2) + 0xffU) / (0xffU * 2));
                byte g = (byte)((this.ImageData[t * 4 + 1] * (0x3fU * 2) + 0xffU) / (0xffU * 2));
                byte b = (byte)((this.ImageData[t * 4 + 0] * (0x1fU * 2) + 0xffU) / (0xffU * 2));

                r = (byte)((r * (0xffU * 2) + 0x1fU) / (0x1fU * 2));
                g = (byte)((g * (0xffU * 2) + 0x3fU) / (0x3fU * 2));
                b = (byte)((b * (0xffU * 2) + 0x1fU) / (0x1fU * 2));

                return(Color.FromArgb((int)r, (int)g, (int)b));
            });

            var colors = imageColors
                         .Distinct()
                         .ToArray();

            if (colors.Length <= 256)
            {
                byte[] palette = new byte[colors.Length * 3];

                for (int i = 0; i < colors.Length; i++)
                {
                    palette[i * 3 + 0] = colors[i].R;
                    palette[i * 3 + 1] = colors[i].G;
                    palette[i * 3 + 2] = colors[i].B;
                }

                var imageData = imageColors
                                .Select(t =>
                {
                    for (int i = 0; i < colors.Length; i++)
                    {
                        if (colors[i] == t)
                        {
                            return((byte)i);
                        }
                    }

                    return((byte)0);
                })
                                .ToArray();

                this.SetPaletteColors(palette);
                this.ImageData = imageData;
            }
            else
            {
                byte[] data = new byte[length * 4];

                for (int i = 0; i < length; i++)
                {
                    byte r = (byte)((this.ImageData[i * 4 + 2] * (0x1fU * 2) + 0xffU) / (0xffU * 2));
                    byte g = (byte)((this.ImageData[i * 4 + 1] * (0x3fU * 2) + 0xffU) / (0xffU * 2));
                    byte b = (byte)((this.ImageData[i * 4 + 0] * (0x1fU * 2) + 0xffU) / (0xffU * 2));

                    r = (byte)((r * (0xffU * 2) + 0x1fU) / (0x1fU * 2));
                    g = (byte)((g * (0xffU * 2) + 0x3fU) / (0x3fU * 2));
                    b = (byte)((b * (0xffU * 2) + 0x1fU) / (0x1fU * 2));

                    data[i * 4 + 2] = r;
                    data[i * 4 + 1] = g;
                    data[i * 4 + 0] = b;
                }

                var image = new ColorQuant.WuColorQuantizer().Quantize(data);

                byte[] palette = new byte[768];

                for (int i = 0; i < image.Palette.Length / 4; i++)
                {
                    palette[i * 3 + 0] = image.Palette[i * 4 + 2];
                    palette[i * 3 + 1] = image.Palette[i * 4 + 1];
                    palette[i * 3 + 2] = image.Palette[i * 4 + 0];
                }

                this.SetPaletteColors(palette);
                this.ImageData = image.Bytes;
            }
        }