Exemplo n.º 1
0
        /// <summary>
        /// Function to compress an image using block compression.
        /// </summary>
        /// <param name="imageFile">The file to compress.</param>
        /// <param name="format">The block compression format to use.</param>
        /// <param name="mipCount">The number of mip levels.</param>
        /// <returns>The virtual file with the compressed data.</returns>
        public IGorgonVirtualFile Compress(IGorgonVirtualFile imageFile, BufferFormat format, int mipCount)
        {
            string             directory      = Path.GetDirectoryName(imageFile.PhysicalFile.FullPath);
            Process            texConvProcess = null;
            IGorgonVirtualFile encodedFile    = null;

            var info = new ProcessStartInfo
            {
                Arguments        = $"-f {format.ToString().ToUpper()} -y -m {mipCount} -fl 12.0 -ft DDS -o \"{directory}\" -nologo \"{imageFile.PhysicalFile.FullPath}\"",
                ErrorDialog      = true,
                FileName         = _texConv.FullName,
                WorkingDirectory = directory,
                UseShellExecute  = false,
#if DEBUG
                CreateNoWindow = false,
#else
                CreateNoWindow = true,
#endif
                RedirectStandardError  = true,
                RedirectStandardOutput = true
            };

            try
            {
                texConvProcess = Process.Start(info);

                if (texConvProcess == null)
                {
                    return(null);
                }

                texConvProcess.WaitForExit();

                string errorData = texConvProcess.StandardError.ReadToEnd();

                if (errorData.StartsWith("Invalid value", StringComparison.OrdinalIgnoreCase))
                {
                    errorData = errorData.Substring(0, errorData.IndexOf("\n", StringComparison.OrdinalIgnoreCase));
                    throw new GorgonException(GorgonResult.CannotWrite, Resources.GORIMG_ERR_CANNOT_COMPRESS, new IOException(errorData));
                }

                errorData = texConvProcess.StandardOutput.ReadToEnd();

                if (errorData.Contains("FAILED"))
                {
                    throw new GorgonException(GorgonResult.CannotWrite, Resources.GORIMG_ERR_CANNOT_COMPRESS, new IOException(errorData));
                }

                _writer.FileSystem.Refresh();

                encodedFile = _writer.FileSystem.GetFile(imageFile.FullPath);
                return(encodedFile);
            }
            finally
            {
                texConvProcess?.Dispose();
            }
        }