Пример #1
0
        public static byte[] Save(Bitmap bmp, ImageSettings settings)
        {
            settings.Width  = bmp.Width;
            settings.Height = bmp.Height;
            var points = GetPointSequence(settings);

            var ms          = new MemoryStream();
            var etc1encoder = new ETC1.Encoder();

            Enum.TryParse <DXT.Formats>(settings.Format.ToString(), false, out var dxtFormat);
            var dxtencoder = new DXT.Encoder(dxtFormat);

            using (var bw = new BinaryWriterX(ms))
            {
                foreach (var point in points)
                {
                    int x = Clamp(point.X, 0, bmp.Width);
                    int y = Clamp(point.Y, 0, bmp.Height);

                    var color = bmp.GetPixel(x, y);
                    if (settings.PixelShader != null)
                    {
                        color = settings.PixelShader(color);
                    }

                    switch (settings.Format)
                    {
                    case Format.L8:
                        bw.Write(color.G);
                        break;

                    case Format.A8:
                        bw.Write(color.A);
                        break;

                    case Format.LA44:
                        bw.WriteNibble(color.A / 16);
                        bw.WriteNibble(color.G / 16);
                        break;

                    case Format.LA88:
                        bw.Write(color.A);
                        bw.Write(color.G);
                        break;

                    case Format.HL88:
                        bw.Write(color.G);
                        bw.Write(color.R);
                        break;

                    case Format.RGB565:
                        bw.Write((short)((color.R / 8 << 11) | (color.G / 4 << 5) | (color.B / 8)));
                        break;

                    case Format.RGB888:
                        bw.Write(color.B);
                        bw.Write(color.G);
                        bw.Write(color.R);
                        break;

                    case Format.RGBA5551:
                        bw.Write((short)((color.R / 8 << 11) | (color.G / 8 << 6) | (color.B / 8 << 1) | color.A / 128));
                        break;

                    case Format.RGBA4444:
                        bw.WriteNibble(color.A / 16);
                        bw.WriteNibble(color.B / 16);
                        bw.WriteNibble(color.G / 16);
                        bw.WriteNibble(color.R / 16);
                        break;

                    case Format.RGBA8888:
                        bw.Write(color.A);
                        bw.Write(color.B);
                        bw.Write(color.G);
                        bw.Write(color.R);
                        break;

                    case Format.ETC1:
                    case Format.ETC1A4:
                        etc1encoder.Set(color, data =>
                        {
                            if (settings.Format == Format.ETC1A4)
                            {
                                bw.Write(data.Alpha);
                            }
                            bw.WriteStruct(data.Block);
                        });
                        break;

                    case Format.DXT1:
                    case Format.DXT5:
                        dxtencoder.Set(color, data =>
                        {
                            if (settings.Format == Format.DXT5)
                            {
                                bw.Write(data.alpha);
                            }
                            bw.Write(data.block);
                        });
                        break;

                    case Format.L4:
                        bw.WriteNibble(color.G / 16);
                        break;

                    case Format.A4:
                        bw.WriteNibble(color.A / 16);
                        break;

                    default:
                        throw new NotSupportedException();
                    }
                }
            }

            return(ms.ToArray());
        }
Пример #2
0
        public static byte[] Save(Bitmap bmp, ImageSettings settings)
        {
            settings.Width  = bmp.Width;
            settings.Height = bmp.Height;
            var points = GetPointSequence(settings);

            var ms          = new MemoryStream();
            var etc1encoder = new ETC1.Encoder();

            using (var bw = new BinaryWriterX(ms))
            {
                foreach (var point in points)
                {
                    int x = Clamp(point.X, 0, bmp.Width);
                    int y = Clamp(point.Y, 0, bmp.Height);

                    var color = bmp.GetPixel(x, y);
                    //if (color.A == 0) color = default(Color); // daigasso seems to need this

                    switch (settings.Format)
                    {
                    case Format.L8:
                        bw.Write(color.G);
                        break;

                    case Format.A8:
                        bw.Write(color.A);
                        break;

                    case Format.LA44:
                        bw.WriteNibble(color.A / 16);
                        bw.WriteNibble(color.G / 16);
                        break;

                    case Format.LA88:
                        bw.Write(color.A);
                        bw.Write(color.G);
                        break;

                    case Format.HL88:
                        bw.Write(color.G);
                        bw.Write(color.R);
                        break;

                    case Format.RGB565:
                        bw.Write((short)((color.R / 8 << 11) | (color.G / 4 << 5) | (color.B / 8)));
                        break;

                    case Format.RGB888:
                        bw.Write(color.B);
                        bw.Write(color.G);
                        bw.Write(color.R);
                        break;

                    case Format.RGBA5551:
                        bw.Write((short)((color.R / 8 << 11) | (color.G / 8 << 6) | (color.B / 8 << 1) | color.A / 128));
                        break;

                    case Format.RGBA4444:
                        bw.WriteNibble(color.A / 16);
                        bw.WriteNibble(color.B / 16);
                        bw.WriteNibble(color.G / 16);
                        bw.WriteNibble(color.R / 16);
                        break;

                    case Format.RGBA8888:
                        bw.Write(color.A);
                        bw.Write(color.B);
                        bw.Write(color.G);
                        bw.Write(color.R);
                        break;

                    case Format.ETC1:
                    case Format.ETC1A4:
                        etc1encoder.Set(color, data =>
                        {
                            if (settings.Format == Format.ETC1A4)
                            {
                                bw.Write(data.Alpha);
                            }
                            bw.WriteStruct(data.Block);
                        });
                        break;

                    case Format.L4:
                        bw.WriteNibble(color.G / 16);
                        break;

                    case Format.A4:
                        bw.WriteNibble(color.A / 16);
                        break;

                    default:
                        throw new NotSupportedException();
                    }
                }
            }

            return(ms.ToArray());
        }