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 bool Autofix(List<TPFTexInfo> texes)
        {
            bool retval = false;

            OverallProg.ChangeProgressBar(0, texes.Count);
            
            foreach (TPFTexInfo tex in texes)
            {
                Overall.UpdateText("Fixing: " + tex.TexName);
                DebugOutput.PrintLn("Fixing: " + tex.TexName + Environment.NewLine + "     FORMAT -> Current: " + tex.Format + "  Expected: " + tex.ExpectedFormat + Environment.NewLine + "     MIPS -> Current: " + tex.NumMips + "  Expected: " + tex.ExpectedMips);
                byte[] arr = tex.Extract(null, true);


                using (ResILImage img = new ResILImage(arr))
                {
                    string path = tex.Autofixedpath(TemporaryPath);
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                    ResIL.Unmanaged.CompressedDataFormat surface = ParseSurfaceFormat(tex.ExpectedFormat);
                    bool success = false;

                    FixMips(tex, img);

                    if (surface == ResIL.Unmanaged.CompressedDataFormat.None)
                        MessageBox.Show("Tell KFreon about this: format = " + tex.ExpectedFormat);
                    else
                        success = img.ConvertAndSave(ResIL.Unmanaged.ImageType.Dds, path, surface: surface);

                    if (!success)
                    {
                        DebugOutput.PrintLn("Autofix failed on image: " + tex.TexName + ". Reason: " + ResILImage.GetResILError());
                        tex.AutofixSuccess = false;
                    }

                    if (!retval && success)
                        retval = true;
                    else if (retval && !success)
                        retval = false;

                    tex.FilePath = Path.GetDirectoryName(tex.Autofixedpath(TemporaryPath));
                    tex.EnumerateDetails();
                }
                
                
                RedrawTreeView();
            }
            Overall.UpdateText("Autofix complete." + (!retval ? "Some errors occured." : ""));
            OverallProg.ChangeProgressBar(1, 1);
            return retval;
        }
        private void PreviewObject(TPFTexInfo tex)
        {
            // KFreon: Clear old image
            ClearPreview();

            // KFreon: Get data
            byte[] data = tex.Extract(null, true);
            if (data == null)
                return;

            // KFreon: Load new one
            if (tex.isDef)
            {
                DisappearTextBox(false);
                try
                {
                    string message = Encoding.UTF8.GetString(data);
                    this.Invoke(new Action(() => texmodPreviewBox.Text = message));
                }
                catch (Exception e)
                {
                    DebugOutput.PrintLn("Unable to get text from data: " + e.Message);
                }
            }
            else
            {
                //KFreonLib.Textures.Methods.GetImage(tex.Format, data);
                Bitmap img = null;
                using (ResILImage kfimg = new ResILImage(data))
                    img = new Bitmap(new MemoryStream(kfimg.ToArray(ResIL.Unmanaged.ImageType.Jpg)));
                if (img == null)
                    return;

                try
                {
                    this.Invoke(new Action(() => PreviewBox.Image = KFreonLib.Textures.Creation.GenerateThumbImage(img, 512)));
                }
                catch { }

                DisappearTextBox(true);
                img.Dispose();
            }
        }
        private bool FixMips(TPFTexInfo info, ResILImage img)
        {
            // KFreon: Build or remove mips depending on requirements. Note case where expected == existing not present as that's what MipsCorrect is.
            if (info.ExpectedMips > info.NumMips)
            {
                if (!img.BuildMipmaps(info.NumMips == 1))
                {
                    DebugOutput.PrintLn(String.Format("Failed to build mipmaps for {0}: {1}", info.TexName, ResILImage.GetResILError()));
                    return false;
                }
            }
            else
            {
                if (!img.RemoveMipmaps(info.NumMips == 1))
                {
                    DebugOutput.PrintLn(String.Format("Failed to remove mipmaps for {0}: {1}", info.TexName, ResILImage.GetResILError()));
                    return false;
                }
            }

            //img.Mips = info.ExpectedMips;
            info.NumMips = info.ExpectedMips;

            return true;
        }