public void DecompressMipmap(TexMipmap mipmap)
        {
            if (mipmap.IsLZ4Compressed)
            {
                mipmap.Bytes           = Lz4Decompress(mipmap.Bytes, mipmap.PixelCount);
                mipmap.IsLZ4Compressed = false;
            }

            if (mipmap.Format.IsImage())
            {
                return;
            }

            switch (mipmap.Format)
            {
            case MipmapFormat.CompressedDXT5:
                mipmap.Bytes  = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXT.DXTFlags.DXT5);
                mipmap.Format = MipmapFormat.RGBA8888;
                break;

            case MipmapFormat.CompressedDXT3:
                mipmap.Bytes  = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXT.DXTFlags.DXT3);
                mipmap.Format = MipmapFormat.RGBA8888;
                break;

            case MipmapFormat.CompressedDXT1:
                mipmap.Bytes  = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXT.DXTFlags.DXT1);
                mipmap.Format = MipmapFormat.RGBA8888;
                break;
            }
        }
Esempio n. 2
0
        public TexMipmap ReadFromStream(Stream stream, Tex tex)
        {
            using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
            {
                var       version = tex.MipmapsContainer.MipmapContainerVersion;
                TexMipmap mipmap;

                switch (version)
                {
                case TexMipmapContainerVersion.Version1:
                    mipmap = new TexMipmap()
                    {
                        Width      = reader.ReadInt32(),
                        Height     = reader.ReadInt32(),
                        BytesCount = reader.ReadInt32(),
                    };
                    break;

                case TexMipmapContainerVersion.Version2:
                case TexMipmapContainerVersion.Version3:
                    mipmap = new TexMipmap()
                    {
                        Width           = reader.ReadInt32(),
                        Height          = reader.ReadInt32(),
                        IsLZ4Compressed = reader.ReadInt32() == 1,
                        PixelCount      = reader.ReadInt32(),
                        BytesCount      = reader.ReadInt32()
                    };
                    break;

                default:
                    throw new NotImplementedException($"Tex mipmap container version: {version} is not supported!");
                }

                mipmap.Format = TexMipmapFormatGetter.GetFormatForTex(tex);

                ReadBytes(reader, mipmap);

                return(mipmap);
            }
        }
Esempio n. 3
0
        protected void ReadBytes(BinaryReader reader, TexMipmap mipmap)
        {
            if (!ReadMipmapBytes)
            {
                reader.BaseStream.Seek(mipmap.BytesCount, SeekOrigin.Current);
                return;
            }

            mipmap.Bytes = new byte[mipmap.BytesCount];

            var bytesRead = reader.Read(mipmap.Bytes, 0, mipmap.BytesCount);

            if (bytesRead != mipmap.BytesCount)
            {
                throw new Exception("Failed to read bytes from stream while reading mipmap");
            }

            if (DecompressMipmapBytes)
            {
                _texMipmapDecompressor.DecompressMipmap(mipmap);
            }
        }
Esempio n. 4
0
        public ITex LoadFileToTex(FileInfo filePath)
        {
            switch (filePath.Extension)
            {
            case ".tex-full-json":
            {
                var settings = new JsonSerializerSettings {
                    TypeNameHandling = TypeNameHandling.Auto
                };
                ITex tex = JsonConvert.DeserializeObject <Tex>(File.ReadAllText(filePath.FullName), settings);
                return(tex);
            }

            default:
            {
                Image  image = Image.FromFile(filePath.FullName);
                byte[] imageBytes;

                using (var ms = new MemoryStream())
                {
                    image.Save(ms, image.RawFormat);
                    imageBytes = ms.ToArray();
                }

                // var fullJsonInfo = Newtonsoft.Json.JsonConvert.SerializeObject(image);
                // File.WriteAllText($"{filePath}.test-json", fullJsonInfo);

                // var bytes = image.SavePixelData();

                var tex = new Tex {
                };

                tex.Magic1 = "TEXV0005";
                tex.Magic2 = "TEXI0001";

                tex.Header = new TexHeader
                {
                    Format        = (TexFormat)0,
                    Flags         = (TexFlags)2,
                    TextureWidth  = image.Width,
                    TextureHeight = image.Height,
                    ImageWidth    = image.Width,
                    ImageHeight   = image.Height,
                    UnkInt0       = 4278190080
                };
                tex.ImagesContainer       = new TexImageContainer {
                };
                tex.ImagesContainer.Magic = "TEXB0003";

                switch (tex.ImagesContainer.Magic)
                {
                case "TEXB0001":
                case "TEXB0002":
                    break;

                case "TEXB0003":
                    tex.ImagesContainer.ImageFormat = FreeImageFormat.FIF_PNG;
                    break;

                default:
                    throw new UnknownMagicException(nameof(TexImageContainerReader), tex.ImagesContainer.Magic);
                }

                tex.ImagesContainer.ImageContainerVersion = (TexImageContainerVersion)Convert.ToInt32(tex.ImagesContainer.Magic.Substring(4));

                // var mipmapCount = 1;

                // if (mipmapCount > Constants.MaximumMipmapCount)
                //     throw new UnsafeTexException(
                //         $"Mipmap count exceeds limit: {mipmapCount}/{Constants.MaximumMipmapCount}");

                // var readFunction = PickMipmapReader(container.ImageContainerVersion);
                var format   = TexMipmapFormatGetter.GetFormatForTex(tex.ImagesContainer.ImageFormat, tex.Header.Format);
                var teximage = new TexImage();

                // var mipmap = readFunction(reader);
                // mipmap.Format = format;

                // if (DecompressMipmapBytes)
                //     _texMipmapDecompressor.DecompressMipmap(mipmap);

                var mipmap = new TexMipmap {
                };

                mipmap.Format = MipmapFormat.ImagePNG;

                mipmap.Width  = image.Width;
                mipmap.Height = image.Height;

                mipmap.Bytes = imageBytes;

                teximage.Mipmaps.Add(mipmap);

                tex.ImagesContainer.Images.Add(teximage);

                return(tex);
            }
            }
        }