public static async Task PostProcessDDS(string path) { Logger.Log("[ImgProc] PostProcessDDS: Loading MagickImage from " + path); MagickImage img = ImgUtils.GetMagickImage(path); string ext = "dds"; img = ResizeImagePostMagick(img); img.Format = MagickFormat.Png00; img.Write(path); string outPath = Path.ChangeExtension(img.FileName, ext); await NvCompress.PngToDds(path, outPath); if (Upscale.currentMode == Upscale.UpscaleMode.Batch) { PostProcessingQueue.lastOutfile = outPath; } if (Upscale.currentMode == Upscale.UpscaleMode.Single || Upscale.currentMode == Upscale.UpscaleMode.Composition) { PreviewUI.lastOutfile = outPath; } if (outPath.ToLower() != path.ToLower()) { if (Logger.doLogIo) { Logger.Log("[ImgProc] Deleting source file: " + path); } File.Delete(path); } }
public static async Task PostProcessImage(string path, Format format, bool dontResize) { Logger.Log($"[ImgProc] Post-Processing {Path.GetFileName(path)} to {format}, resize: {!dontResize}"); if (!dontResize) { ResizeImagePost(path); } MagickImage img = ImgUtils.GetMagickImage(path); string newExt = "png"; bool magick = true; if (format == Format.Source) { newExt = Path.GetExtension(path).Replace(".", ""); } if (format == Format.Png50) { img.Format = MagickFormat.Png; img.Quality = 50; } if (format == Format.PngFast) { img.Format = MagickFormat.Png; img.Quality = 20; } if (format == Format.Jpeg) { newExt = "jpg"; int q = Config.GetInt("jpegQ"); if (Config.GetBool("useMozJpeg")) { MozJpeg.Encode(path, GetOutPath(path, newExt, ExtMode.UseNew, ""), q); magick = false; } else { img.Format = MagickFormat.Jpeg; img.Quality = q; } } if (format == Format.Weppy) { img.Format = MagickFormat.WebP; img.Quality = Config.GetInt("webpQ"); if (img.Quality >= 100) { img.Settings.SetDefine(MagickFormat.WebP, "lossless", true); } newExt = "webp"; } if (format == Format.BMP) { img.Format = MagickFormat.Bmp; newExt = "bmp"; } if (format == Format.TGA) { img.Format = MagickFormat.Tga; newExt = "tga"; } if (format == Format.DDS) { magick = false; newExt = "tga"; await NvCompress.PngToDds(path, GetOutPath(path, newExt, ExtMode.UseNew, "")); } if (format == Format.GIF) { img.Format = MagickFormat.Gif; newExt = "gif"; } await Task.Delay(1); string outPath = GetOutPath(path, newExt, ExtMode.UseNew, ""); if (Upscale.currentMode == Upscale.UpscaleMode.Batch) { PostProcessingQueue.lastOutfile = outPath; } if (Upscale.currentMode == Upscale.UpscaleMode.Single || Upscale.currentMode == Upscale.UpscaleMode.Composition) { PreviewUI.lastOutfile = outPath; } if (magick) { img.Write(outPath); Logger.Log("[ImgProc] Written image to " + outPath); } if (outPath.ToLower() != path.ToLower()) { if (Logger.doLogIo) { Logger.Log("[ImgProc] Deleting source file: " + path); } File.Delete(path); } }
public static async Task ConvertImage(string path, Format format, bool fillAlpha, ExtMode extMode, bool deleteSource = true, string overrideOutPath = "", bool allowTgaFlip = false) { MagickImage img = ImgUtils.GetMagickImage(path, allowTgaFlip); string newExt = "png"; bool magick = true; Logger.Log($"[ImgProc] Converting {path} to {format}, DelSrc: {deleteSource}, Fill: {fillAlpha}, Ext: {extMode}"); if (format == Format.PngRaw) { img.Format = MagickFormat.Png32; img.Quality = 0; } if (format == Format.Png50) { img.Format = MagickFormat.Png32; img.Quality = 50; } if (format == Format.PngFast) { img.Format = MagickFormat.Png32; img.Quality = 20; } if (format == Format.Jpeg) { newExt = "jpg"; int q = Config.GetInt("jpegQ"); if (Config.GetBool("useMozJpeg")) { MozJpeg.Encode(path, GetOutPath(path, newExt, extMode, overrideOutPath), q); magick = false; } else { img.Format = MagickFormat.Jpeg; img.Quality = q; } } if (format == Format.Weppy) { img.Format = MagickFormat.WebP; img.Quality = Config.GetInt("webpQ"); if (img.Quality >= 100) { img.Settings.SetDefine(MagickFormat.WebP, "lossless", true); } newExt = "webp"; } if (format == Format.BMP) { img.Format = MagickFormat.Bmp; newExt = "bmp"; } if (format == Format.TGA) { img.Format = MagickFormat.Tga; newExt = "tga"; } if (format == Format.DDS) { magick = false; newExt = "tga"; await NvCompress.PngToDds(path, GetOutPath(path, newExt, ExtMode.UseNew, "")); } if (format == Format.GIF) { img.Format = MagickFormat.Gif; } if (magick) { img = CheckColorDepth(path, img); if (fillAlpha) { img = ImgUtils.FillAlphaWithBgColor(img); } } string outPath = GetOutPath(path, newExt, extMode, overrideOutPath); if (File.Exists(outPath)) { if (Logger.doLogIo) { Logger.Log("[ImgProc] File exists at - making sure it doesn't have readonly flag"); } IOUtils.RemoveReadonlyFlag(outPath); } bool inPathIsOutPath = outPath.ToLower() == path.ToLower(); if (inPathIsOutPath) // Force overwrite by deleting source file before writing new file - THIS IS IMPORTANT { File.Delete(path); } if (magick) { img.Write(outPath); Logger.Log("[ImgProc] Written image to " + outPath); } if (deleteSource && !inPathIsOutPath) { if (Logger.doLogIo) { Logger.Log("[ImgProc] Deleting source file: " + path); } File.Delete(path); } img.Dispose(); IOUtils.RemoveReadonlyFlag(outPath); await Task.Delay(1); }