Example #1
0
        /// <summary>
        /// Decides on an extension based on a format string. Returns extension with a dot, or null if invalid format.
        /// </summary>
        /// <param name="format">Format string to decide on.</param>
        /// <returns>Returns extension with a dot, or null if invalid format.</returns>
        public static string GetExtensionFromFormat(string format)
        {
            string ext = null;

            // KFreon: Check if format is valid
            if (!isValidFormat(format))
            {
                ext = null;
            }

            // KFreon: Get a format
            if (ResILImage.IsTextureDDS(format))
            {
                ext = ".DDS";
            }
            else
            {
                if (!format.Contains('.'))
                {
                    ext = '.' + format;
                }
                else
                {
                    ext = format;
                }
            }
            return(ext);
        }
Example #2
0
        public override bool ConvertAndSave(ImageType type, Stream stream, ResILImageBase.MipMapMode MipsMode = MipMapMode.BuildAll, CompressedDataFormat surface = CompressedDataFormat.None, int quality = 80, bool SetJPGQuality = true)
        {
            // KFreon: If converting to something other than V8U8...
            if (surface != SurfaceFormat)
            {
                byte[]       RawImageData = GetImageDataAs3Channel(); // KFreon: Get image data as raw rgb pixels
                int          stride       = (Width * 32 + 7) / 8;
                BitmapSource test         = BitmapSource.Create(Width, Height, 96, 96, PixelFormats.Bgr32, BitmapPalettes.Halftone125, RawImageData, stride);

                MemoryTributary   stream2 = new MemoryTributary();
                JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(test));
                encoder.Save(stream2);

                using (ResILImage img = new ResILImage(stream2))
                    return(img.ConvertAndSave(type, stream, MipsMode, surface, quality, SetJPGQuality));
            }
            else
            {
                // KFreon: Deal with mips first
                int  expectedMips = EstimateNumMips(Width, Height);
                bool success      = true;
                switch (MipsMode)
                {
                case MipMapMode.BuildAll:
                    if (expectedMips != Mips)
                    {
                        success = BuildMipMaps();
                    }
                    break;

                case MipMapMode.Rebuild:
                    // KFreon: Remove existing mips before building them again
                    if (!RemoveMipMaps())
                    {
                        success = false;
                    }
                    else
                    {
                        success = BuildMipMaps();
                    }
                    break;

                case MipMapMode.ForceRemove:
                case MipMapMode.RemoveAllButOne:
                    success = RemoveMipMaps();
                    break;
                }

                if (!success)
                {
                    Debug.WriteLine("Failed to fix mipmaps.");
                    return(false);
                }

                // KFreon: Build formatting and write out to file
                return(WriteV8U8ToStream(MipMaps, stream, Height, Width, Mips, false));
            }
        }
        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Title  = "Select destination";
            sfd.Filter = String.IsNullOrEmpty(vm.SelectedFormat) ? "" : ResILImage.GetFilterString(ResILImage.GetExtensionFromFormat(vm.SelectedFormat));
            if (sfd.ShowDialog() == true)
            {
                vm.SavePath = sfd.FileName;
            }
        }