Ejemplo n.º 1
0
        /// <summary>
        /// Convert the specified image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="libraryData">The library data.</param>
        /// <param name="request">The decompression request</param>
        /// <exception cref="TextureToolsException">Decompression failed</exception>
        private void Convert(TexImage image, DxtTextureLibraryData libraryData, ConvertingRequest request)
        {
            // TODO: temp if request format is SRGB we force it to non-srgb to perform the conversion. Will not work if texture input is SRGB
            var outputFormat = request.Format.IsSRgb() ? request.Format.ToNonSRgb() : request.Format;

            Log.Verbose($"Converting texture from {(PixelFormat)libraryData.Metadata.format} to {outputFormat}");

            var scratchImage = new ScratchImage();
            var hr           = Utilities.Convert(libraryData.DxtImages, libraryData.DxtImages.Length, ref libraryData.Metadata, (DXGI_FORMAT)outputFormat, TEX_FILTER_FLAGS.TEX_FILTER_BOX, 0.0f, scratchImage);

            if (hr != HRESULT.S_OK)
            {
                Log.Error("Converting failed: " + hr);
                throw new TextureToolsException("Converting failed: " + hr);
            }

            // Freeing Memory
            if (image.DisposingLibrary != null)
            {
                image.DisposingLibrary.Dispose(image);
            }

            libraryData.Image      = scratchImage;
            libraryData.DxtImages  = libraryData.Image.GetImages();
            libraryData.Metadata   = libraryData.Image.metadata;
            image.DisposingLibrary = this;

            // adapt the image format based on desired output format
            ChangeDxtImageType(libraryData, (DXGI_FORMAT)request.Format);

            UpdateImage(image, libraryData);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Compresses the specified image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="libraryData">The library data.</param>
        /// <param name="request">The request.</param>
        /// <exception cref="TextureToolsException">Compression failed</exception>
        private void Compress(TexImage image, DxtTextureLibraryData libraryData, CompressingRequest request)
        {
            Log.Verbose("Converting/Compressing with " + request.Format + " ...");

            if (libraryData.DxtImages == null || libraryData.DxtImages.Length == 0)
            {
                return;
            }

            ScratchImage scratchImage = new ScratchImage();

            HRESULT hr;

            if (request.Format.IsCompressed())
            {
                var topImage = libraryData.DxtImages[0];
                if (topImage.Width % 4 != 0 || topImage.Height % 4 != 0)
                {
                    throw new TextureToolsException(string.Format("The provided texture cannot be compressed into format '{0}' " +
                                                                  "because its top resolution ({1}-{2}) is not a multiple of 4.", request.Format, topImage.Width, topImage.Height));
                }

                hr = Utilities.Compress(libraryData.DxtImages, libraryData.DxtImages.Length, ref libraryData.Metadata,
                                        RetrieveNativeFormat(request.Format), TEX_COMPRESS_FLAGS.TEX_COMPRESS_DEFAULT, 0.5f, scratchImage);
            }
            else
            {
                hr = Utilities.Convert(libraryData.DxtImages, libraryData.DxtImages.Length, ref libraryData.Metadata,
                                       RetrieveNativeFormat(request.Format), TEX_FILTER_FLAGS.TEX_FILTER_DEFAULT, 0.5f, scratchImage);
            }


            if (hr != HRESULT.S_OK)
            {
                Log.Error("Compression failed: " + hr);
                throw new TextureToolsException("Compression failed: " + hr);
            }

            if (image.DisposingLibrary != null)
            {
                image.DisposingLibrary.Dispose(image);
            }

            // Updating attributes
            libraryData.Image      = scratchImage;
            libraryData.DxtImages  = libraryData.Image.GetImages();
            libraryData.Metadata   = libraryData.Image.metadata;
            image.DisposingLibrary = this;

            UpdateImage(image, libraryData);
        }