Exemplo n.º 1
0
        public static bool ConvertFromDdsAndSave(Stream ms, string outfilename, ESaveFileTypes filetype, bool vflip = false, bool hflip = false)
        {
            byte[] rentedBuffer = null;
            try
            {
                var offset = 0;

                var len = checked ((int)ms.Length);
                rentedBuffer = ArrayPool <byte> .Shared.Rent(len);

                int readBytes;
                while (offset < len &&
                       (readBytes = ms.Read(rentedBuffer, offset, len - offset)) > 0)
                {
                    offset += readBytes;
                }

                var outDir = new FileInfo(outfilename).Directory.FullName;
                Directory.CreateDirectory(outDir);
                var fileName  = Path.GetFileNameWithoutExtension(outfilename);
                var extension = filetype.ToString().ToLower();
                var newpath   = Path.Combine(outDir, $"{fileName}.{extension}");

                //TexconvNative.ConvertAndSaveDdsImage(rentedBuffer, newpath, filetype, vflip, hflip);
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    var buffer = Array.Empty <byte>();
                    using (var blob = new ManagedBlob())
                    {
                        var l = TexconvNative.ConvertFromDds(rentedBuffer, blob.GetBlob(), filetype, vflip, hflip);
                        buffer = blob.GetBytes();
                    }
                    File.WriteAllBytes(newpath, buffer);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            finally
            {
                if (rentedBuffer is object)
                {
                    ArrayPool <byte> .Shared.Return(rentedBuffer);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts a dds image to another texture format
        /// </summary>
        public static byte[] ConvertFromDds(Stream stream, EUncookExtension textureType, bool vflip = false, bool hflip = false)
        {
            if (textureType == EUncookExtension.dds)
            {
                throw new NotSupportedException("texture to convert from dds must not be dds iteslf");
            }

            byte[] rentedBuffer = null;
            try
            {
                var offset = 0;

                var len = checked ((int)stream.Length);
                rentedBuffer = ArrayPool <byte> .Shared.Rent(len);

                int readBytes;
                while (offset < len &&
                       (readBytes = stream.Read(rentedBuffer, offset, len - offset)) > 0)
                {
                    offset += readBytes;
                }

                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    var buffer = Array.Empty <byte>();
                    using (var blob = new ManagedBlob())
                    {
                        var l = TexconvNative.ConvertFromDds(rentedBuffer, blob.GetBlob(), ToSaveFormat(textureType), vflip, hflip);
                        buffer = blob.GetBytes();
                    }
                    return(buffer);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            finally
            {
                if (rentedBuffer is object)
                {
                    ArrayPool <byte> .Shared.Return(rentedBuffer);
                }
            }
        }