Exemplo n.º 1
0
        public static ImageData Load(Stream s)
        {
            ImageData    img;
            BinaryReader br    = new BinaryReader(s);
            var          magic = br.ReadUInt32();

            s.Position = 0;
            if (magic == 0xE0FFD8FF)
            {
                System.Drawing.Bitmap image = new System.Drawing.Bitmap(s);
                img = FromBitmap(image);
            }
            else if (magic == 0x20534444)
            {
                IImage image = Dds.Create(s);
                img = FromPfimImg(image);
            }
            //tga image
            else
            {
                IImage image = Targa.Create(s);
                img = FromPfimImg(image);
            }
            return(img);
        }
Exemplo n.º 2
0
 public int Pfim()
 {
     using (var image = Dds.Create(data, _pfimConfig))
     {
         return(image.BytesPerPixel);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Parses any DXT texture from raw bytes including header using Pfim.
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="mipmap">Does the texture contain mipmap data</param>
        /// <returns></returns>
        private static Texture2D LoadTexturePfim(byte[] bytes, bool mipmap)
        {
            IImage imageData = Dds.Create(bytes, new PfimConfig());

            imageData.Decompress();

            ImageFormat frmt = imageData.Format;

            TextureFormat texFrmt;

            switch (imageData.Format)
            {
            case ImageFormat.Rgb24:
                texFrmt = TextureFormat.RGB24;
                break;

            case ImageFormat.Rgba32:
                texFrmt = TextureFormat.RGBA32;
                break;

            default:
                throw new Exception($"Unknown raw image format {frmt}");
            }

            return(LoadTextureFromBytes(imageData.Data, imageData.Width, imageData.Height, texFrmt, mipmap));
        }
Exemplo n.º 4
0
        public DDSImage(Stream stream)
        {
            if (stream == null)
            {
                throw new Exception($"DDSImage ctor: {nameof(stream)} is null");
            }

            _ddsImageFile = Dds.Create(stream, new PfimConfig(decompress: true));
        }
Exemplo n.º 5
0
        public void TestDdsCompression(string path)
        {
            var data   = File.ReadAllBytes(Path.Combine("data", path));
            var image  = Dds.Create(data, new PfimConfig());
            var image2 = Dds.Create(data, new PfimConfig(decompress: false));

            Assert.False(image.Compressed);
            Assert.True(image2.Compressed);
            Assert.NotEqual(image.Data, image2.Data);
            Assert.Equal(image.Format, image2.Format);
            image2.Decompress();
            Assert.Equal(image.Data, image2.Data);
        }
Exemplo n.º 6
0
        public void TestMipMapProperties(string allPath, ulong hash)
        {
            var path   = Path.Combine("data", Path.Combine(allPath.Split('\\')));
            var data   = File.ReadAllBytes(path);
            var image  = Pfim.FromFile(path);
            var image2 = Dds.Create(data, new PfimConfig());

            Assert.Equal(image.MipMaps, image2.MipMaps);

            var mipMapLengths = image.MipMaps.Sum(x => x.DataLen);
            var hash1         = Hash64(image.Data, image.DataLen + mipMapLengths);

            Assert.Equal(hash1, Hash64(image2.Data, image2.DataLen + mipMapLengths));
            Assert.Equal(hash, hash1);
        }
Exemplo n.º 7
0
        public DDSImage(System.IO.Stream ddsImage)
        {
            if (ddsImage == null)
            {
                return;
            }

            if (!ddsImage.CanRead)
            {
                return;
            }

            _image = Dds.Create(ddsImage, new PfimConfig());
            Parse();
        }
Exemplo n.º 8
0
        public DDSImage(byte[] ddsImage)
        {
            if (ddsImage == null)
            {
                return;
            }

            if (ddsImage.Length == 0)
            {
                return;
            }

            _image = Dds.Create(ddsImage, new PfimConfig());
            Parse();
        }
Exemplo n.º 9
0
        private bool TryLoadDds(Stream s, out Texture2D result)
        {
            try
            {
                s.Seek(0, SeekOrigin.Begin);
                var img = Dds.Create(s, true);

                Texture2D tex = new Texture2D(Editor.graphics, img.Width, img.Height, false, SurfaceFormat.Color);
                if (img.Format == ImageFormat.Rgb24)
                {
                    // convert from 24bit to 32...
                    var convertedData = new byte[img.Width * img.Height * 4];
                    using (var br = new BinaryReader(new MemoryStream(img.Data)))
                        using (var bw = new BinaryWriter(new MemoryStream(convertedData)))
                        {
                            for (int i = 0; i < convertedData.Length; i += 4)
                            {
                                byte r = br.ReadByte();
                                byte g = br.ReadByte();
                                byte b = br.ReadByte();

                                bw.Write(b);
                                bw.Write(g);
                                bw.Write(r);
                                bw.Write((byte)255);
                            }
                        }

                    tex.SetData <byte>(convertedData);
                }
                else if (img.Format == ImageFormat.Rgba32)
                {
                    tex.SetData <byte>(img.Data);
                }
                else
                {
                    throw new InvalidOperationException("unsupported format: " + img.Format);
                }

                result = tex;
            }
            catch (Exception e) {
                Log.WriteLine("TryLoadDds : " + e.Message);
                result = null;
            }
            return(result != null);
        }
Exemplo n.º 10
0
            public DdsFrame(Stream stream)
            {
                using (var dds = Dds.Create(stream, new PfimConfig()))
                {
                    Size = new Size(dds.Width, dds.Height);
                    Data = dds.Data;
                    switch (dds.Format)
                    {
                    // SpriteFrameType refers to the channel byte order, which is reversed from the little-endian bit order
                    case ImageFormat.Rgba32: Type = SpriteFrameType.Bgra32; break;

                    case ImageFormat.Rgb24: Type = SpriteFrameType.Bgr24; break;

                    default: throw new InvalidDataException($"Unhandled ImageFormat {dds.Format}");
                    }
                }
            }
Exemplo n.º 11
0
        public void TestDdsMipMap1()
        {
            var image        = Pfim.FromFile(Path.Combine("data", "wose_BC1_UNORM.DDS"));
            var expectedMips = new[]
            {
                new MipMapOffset(36, 36, 144, 20736, 5184),
                new MipMapOffset(18, 18, 80, 25920, 1600),
                new MipMapOffset(9, 9, 48, 27520, 576),
                new MipMapOffset(4, 4, 16, 28096, 64),
                new MipMapOffset(2, 2, 16, 28160, 64),
                new MipMapOffset(1, 1, 16, 28224, 64)
            };

            Assert.Equal(expectedMips, image.MipMaps);
            Assert.Equal(28224 + 64, image.Data.Length);

            image = Dds.Create(File.ReadAllBytes(Path.Combine("data", "wose_BC1_UNORM.DDS")), new PfimConfig());
            Assert.Equal(expectedMips, image.MipMaps);
            Assert.Equal(28224 + 64, image.Data.Length);
        }
Exemplo n.º 12
0
        public void TestDdsMipMap1()
        {
            var image        = Pfim.FromFile(Path.Combine("data", "wose_BC1_UNORM.DDS"));
            var expectedMips = new[]
            {
                new MipMapOffset(36, 36, 108, 15552, 3888),
                new MipMapOffset(18, 18, 60, 19440, 1200),
                new MipMapOffset(9, 9, 36, 20640, 432),
                new MipMapOffset(4, 4, 12, 21072, 48),
                new MipMapOffset(2, 2, 12, 21120, 48),
                new MipMapOffset(1, 1, 12, 21168, 48)
            };

            Assert.Equal(expectedMips, image.MipMaps);
            Assert.Equal(21168 + 48, image.Data.Length);

            image = Dds.Create(File.ReadAllBytes(Path.Combine("data", "wose_BC1_UNORM.DDS")), new PfimConfig());
            Assert.Equal(expectedMips, image.MipMaps);
            Assert.Equal(21168 + 48, image.Data.Length);
        }
Exemplo n.º 13
0
        public unsafe static byte[] MakeMipsImage(byte[] data)
        {
            var dds   = Dds.Create(new MemoryStream(data));
            var tlen  = dds.Width * dds.Height * 2 * 4;
            var mips  = SiliconStudio.Xenko.Graphics.Image.CountMips(dds.Width, dds.Height);
            var adata = Marshal.AllocHGlobal(tlen);

            var isi = ISImageFromDDS(dds);

            var off = 0;

            for (var i = 0; i < mips; ++i)
            {
                off += MakeMipLevel(isi, (uint *)adata, i, off, dds.BytesPerPixel == 4);
            }

            using (var ms = new MemoryStream()) {
                SiliconStudio.Xenko.Graphics.Image.New2D(dds.Width, dds.Height, mips, PixelFormat.R8G8B8A8_UNorm, 1, adata).Save(ms, ImageFileType.Xenko);
                ms.Flush();
                return(ms.GetBuffer());
            }
        }
Exemplo n.º 14
0
                static async Task <MemoryStream> getDDS(Stream stream)
                {
                    MemoryStream ms = null;

                    using var pfimImage = Dds.Create(stream, new PfimConfig());
                    if (pfimImage.Compressed)
                    {
                        pfimImage.Decompress();
                    }
                    if (pfimImage.Format == ImageFormat.Rgba32)
                    {
                        ms = new MemoryStream();
                        using var image = Image.LoadPixelData <Bgra32>(tightData(pfimImage), pfimImage.Width, pfimImage.Height);
                        await image.SaveAsPngAsync(ms);
                    }
                    else if (pfimImage.Format == ImageFormat.Rgb24)
                    {
                        ms = new MemoryStream();
                        using var image = Image.LoadPixelData <Bgr24>(tightData(pfimImage), pfimImage.Width, pfimImage.Height);
                        await image.SaveAsPngAsync(ms);
                    }
                    return(ms);
                }
Exemplo n.º 15
0
        /// <summary>
        /// Gets the DDS.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>MemoryStream.</returns>
        /// <exception cref="System.AggregateException"></exception>
        private async Task <MemoryStream> GetDDS(Stream stream)
        {
            if (stream.CanSeek)
            {
                stream.Seek(0, SeekOrigin.Begin);
            }
            var          exceptions = new List <Exception>();
            MemoryStream ms         = null;

            // Default provider (SixLabors.Textures)
            try
            {
                var image = await ddsDecoder.DecodeStreamToImageAsync(stream);

                ms = new MemoryStream();
                await image.SaveAsPngAsync(ms);
            }
            catch (Exception ex)
            {
                if (ms != null)
                {
                    ms.Close();
                    await ms.DisposeAsync();
                }
                ms = null;
                exceptions.Add(ex);
            }
            // fallback #1 (BCnEncoder.NET)
            if (ms == null)
            {
                if (stream.CanSeek)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                }
                try
                {
                    var file  = DdsFile.Load(stream);
                    var image = await ddsDecoder.DecodeToImageAsync(file);

                    ms = new MemoryStream();
                    await image.SaveAsPngAsync(ms);
                }
                catch (Exception ex)
                {
                    if (ms != null)
                    {
                        ms.Close();
                        await ms.DisposeAsync();
                    }
                    ms = null;
                    exceptions.Add(ex);
                }
            }
            // fallback #2 (pfim)
            if (ms == null)
            {
                if (stream.CanSeek)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                }
                try
                {
                    using var pfimImage = Dds.Create(stream, new PfimConfig());
                    if (pfimImage.Compressed)
                    {
                        pfimImage.Decompress();
                    }
                    if (pfimImage.Format == ImageFormat.Rgba32)
                    {
                        ms = new MemoryStream();
                        using var image = Image.LoadPixelData <Bgra32>(ddsDecoder.TightData(pfimImage), pfimImage.Width, pfimImage.Height);
                        await image.SaveAsPngAsync(ms);
                    }
                    else if (pfimImage.Format == ImageFormat.Rgb24)
                    {
                        ms = new MemoryStream();
                        using var image = Image.LoadPixelData <Bgr24>(ddsDecoder.TightData(pfimImage), pfimImage.Width, pfimImage.Height);
                        await image.SaveAsPngAsync(ms);
                    }
                }
                catch (Exception ex)
                {
                    if (ms != null)
                    {
                        ms.Close();
                        await ms.DisposeAsync();
                    }
                    ms = null;
                    exceptions.Add(ex);
                }
            }
            // Fallback can result in memory stream being empty so throw aggregate exception only if all attempts failed
            if (ms == null && exceptions.Count == 3)
            {
                throw new AggregateException(exceptions);
            }
            return(ms);
        }
Exemplo n.º 16
0
        public override void Read(BinaryReader br)
        {
            long offset = br.BaseStream.Position;

            br.BaseStream.Seek(4, SeekOrigin.Current); //"GBIX"
            GBIXSize    = br.ReadUInt32();
            GBIXContent = br.ReadBytes((int)GBIXSize);
            br.BaseStream.Seek(4, SeekOrigin.Current); //"PVRT"

            Size   = br.ReadUInt32();
            Type   = (PVRType)br.ReadByte();
            Format = (PVRFormat)br.ReadByte();
            br.BaseStream.Seek(2, SeekOrigin.Current);
            Width  = br.ReadUInt16();
            Height = br.ReadUInt16();

            if (Format == PVRFormat.VQ)
            {
                var palette = new Color4[1024];
                for (int i = 0; i < palette.Length; i++)
                {
                    palette[i] = ReadColor(br);
                }
                var bytes = new byte[Width * Height / 4];
                for (int i = 0; i < Width * Height / 4; i++)
                {
                    bytes[i] = br.ReadByte();
                }
                DecodeVQ(bytes, palette);
            }
            else if (Type == PVRType.RGB565 || Type == PVRType.ARGB1555 || Type == PVRType.ARGB4444)
            {
                Pixels = new Color4[Width * Height];
                for (int i = 0; i < Width * Height; i++)
                {
                    Pixels[i] = ReadColor(br);
                }
                Unswizzle();
            }
            else if (Type == PVRType.DDS_RGB24 || Type == PVRType.DDS_RGBA32)
            {
                Dds image = Dds.Create(br.BaseStream, new PfimConfig());

                byte[] pixels = image.Data;
                Pixels = new Color4[image.Width * image.Height];
                for (int i = 0; i < image.Width * image.Height; i++)
                {
                    int index = i * image.BytesPerPixel;
                    if (image.BytesPerPixel > 3)
                    {
                        if (pixels[index + 3] < 0.8)
                        {
                            HasTransparency = true;
                        }
                        Pixels[i] = new Color4(pixels[index + 2], pixels[index + 1], pixels[index], pixels[index + 3]);
                    }
                    else
                    {
                        HasTransparency = false;
                        Pixels[i]       = new Color4(pixels[index + 2], pixels[index + 1], pixels[index], 255);
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Exemplo n.º 17
0
                static async Task <MemoryStream> getDDS(Stream stream)
                {
                    if (stream.CanSeek)
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                    var          exceptions = new List <Exception>();
                    MemoryStream ms         = null;

                    try
                    {
                        ms = new MemoryStream();
                        var file    = DdsFile.Load(stream);
                        var decoder = new BcDecoder();
                        var image   = decoder.Decode(file);
                        await image.SaveAsPngAsync(ms);
                    }
                    catch (Exception ex)
                    {
                        if (ms != null)
                        {
                            ms.Close();
                            await ms.DisposeAsync();
                        }
                        ms = null;
                        exceptions.Add(ex);
                    }
                    // fallback
                    if (ms == null)
                    {
                        if (stream.CanSeek)
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                        }
                        try
                        {
                            using var pfimImage = Dds.Create(stream, new PfimConfig());
                            if (pfimImage.Compressed)
                            {
                                pfimImage.Decompress();
                            }
                            if (pfimImage.Format == ImageFormat.Rgba32)
                            {
                                ms = new MemoryStream();
                                using var image = Image.LoadPixelData <Bgra32>(tightData(pfimImage), pfimImage.Width, pfimImage.Height);
                                await image.SaveAsPngAsync(ms);
                            }
                            else if (pfimImage.Format == ImageFormat.Rgb24)
                            {
                                ms = new MemoryStream();
                                using var image = Image.LoadPixelData <Bgr24>(tightData(pfimImage), pfimImage.Width, pfimImage.Height);
                                await image.SaveAsPngAsync(ms);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ms != null)
                            {
                                ms.Close();
                                await ms.DisposeAsync();
                            }
                            ms = null;
                            exceptions.Add(ex);
                        }
                    }
                    // Fallback can result in memory stream being empty so throw aggregate exception only if both attempts failed
                    if (ms == null && exceptions.Count == 2)
                    {
                        throw new AggregateException(exceptions);
                    }
                    return(ms);
                }