Пример #1
0
 private static void ConvertCTexMipmap(
     NativeEnvironment e,
     ITexMipmap src,
     CTexMipmap *dst)
 {
     dst->bytes       = e.Pin(src.Bytes);
     dst->bytes_count = src.Bytes.Length;
     dst->width       = src.Width;
     dst->height      = src.Height;
     dst->decompressed_bytes_count = src.DecompressedBytesCount;
     dst->is_lz4_compressed        = src.IsLZ4Compressed;
     dst->format = src.Format;
 }
Пример #2
0
        private static void WriteMipmapV2And3(BinaryWriter writer, ITexMipmap mipmap)
        {
            writer.Write(mipmap.Width);
            writer.Write(mipmap.Height);
            writer.Write(mipmap.IsLZ4Compressed ? 1 : 0);
            writer.Write(mipmap.DecompressedBytesCount);

            using (var stream = mipmap.GetBytesStream())
            {
                writer.Write((int)stream.Length);
                writer.Flush();
                stream.CopyTo(writer.BaseStream);
            }
        }
Пример #3
0
        public ImageResult ConvertToImage(ITex tex, ITexMipmap sourceMipmap)
        {
            if (tex == null)
            {
                throw new ArgumentNullException(nameof(tex));
            }

            if (tex.IsGif)
            {
                return(ConvertToGif(tex));
            }

            var format = sourceMipmap.Format;

            if (format.IsCompressed())
            {
                throw new InvalidOperationException("Raw mipmap format must be uncompressed");
            }

            if (format.IsRawFormat())
            {
                var image = ImageFromRawFormat(format, sourceMipmap.Bytes, sourceMipmap.Width, sourceMipmap.Height);

                if (sourceMipmap.Width != tex.Header.ImageWidth ||
                    sourceMipmap.Height != tex.Header.ImageHeight)
                {
                    image.Mutate(x => x.Crop(tex.Header.ImageWidth, tex.Header.ImageHeight));
                }

                using (var memoryStream = new MemoryStream())
                {
                    image.SaveAsPng(memoryStream);

                    return(new ImageResult
                    {
                        Bytes = memoryStream.ToArray(),
                        Format = MipmapFormat.ImagePNG
                    });
                }
            }

            return(new ImageResult
            {
                Bytes = sourceMipmap.Bytes,
                Format = format
            });
        }
Пример #4
0
        private static void WriteMipmapV1(BinaryWriter writer, ITexMipmap mipmap)
        {
            if (mipmap.IsLZ4Compressed)
            {
                throw new InvalidOperationException(
                          $"Cannot write lz4 compressed mipmap when using tex container version: {TexImageContainerVersion.Version1}");
            }

            writer.Write(mipmap.Width);
            writer.Write(mipmap.Height);

            using (var stream = mipmap.GetBytesStream())
            {
                writer.Write((int)stream.Length);
                writer.Flush();
                stream.CopyTo(writer.BaseStream);
            }
        }
Пример #5
0
        private static void LZ4Compress(ITexMipmap mipmap)
        {
            var bytes       = mipmap.Bytes;
            var maximumSize = LZ4Codec.MaximumOutputSize(bytes.Length);
            var buffer      = new byte[maximumSize];

            var compressedSize = LZ4Codec.Encode(
                bytes, 0, bytes.Length,
                buffer, 0, buffer.Length);

            if (compressedSize < maximumSize)
            {
                Array.Resize(ref buffer, compressedSize);
            }

            mipmap.DecompressedBytesCount = bytes.Length;
            mipmap.Bytes           = buffer;
            mipmap.IsLZ4Compressed = true;
        }
Пример #6
0
        public void CompressMipmap(
            ITexMipmap mipmap,
            MipmapFormat targetCompressFormat,
            bool lz4Compress)
        {
            if (mipmap == null)
            {
                throw new ArgumentNullException(nameof(mipmap));
            }
            if (mipmap.IsLZ4Compressed)
            {
                throw new InvalidOperationException("Mipmap is already compressed using LZ4");
            }

            if (targetCompressFormat != mipmap.Format)
            {
                throw new NotSupportedException("Changing mipmap format is not yet supported");
            }

            if (lz4Compress)
            {
                LZ4Compress(mipmap);
            }
        }
Пример #7
0
        public void DecompressMipmap(ITexMipmap mipmap)
        {
            if (mipmap == null)
            {
                throw new ArgumentNullException(nameof(mipmap));
            }

            if (mipmap.IsLZ4Compressed)
            {
                mipmap.Bytes           = Lz4Decompress(mipmap.Bytes, mipmap.DecompressedBytesCount);
                mipmap.IsLZ4Compressed = false;
            }

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

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

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

            case MipmapFormat.CompressedDXT1:
                mipmap.Bytes  = DXT.DecompressImage(mipmap.Width, mipmap.Height, mipmap.Bytes, DXTFlags.DXT1);
                mipmap.Format = MipmapFormat.RGBA8888;
                break;
            }
        }