// Returns a MagickImage but converts the file first if it's not compatible with IM. public static MagickImage ReadImage(string path, bool showInfo = true) { try { if (Path.GetExtension(path).ToLower() == ".dds") { try { return(new MagickImage(path)); // Try reading DDS with IM, fall back to DdsFileTypePlusHack if it fails } catch { //Logger.Log("Failed to read DDS using Mackig.NET - Trying DdsFileTypePlusHack"); try { MagickImage img = null; Surface surface = DdsFile.Load(path); img = ConvertToMagickImage(surface); img.HasAlpha = DdsFile.HasTransparency(surface); return(img); } catch (Exception e) { Program.Print("Error reading DDS: " + Path.GetFileName(path) + "!"); return(null); } } } if (Path.GetExtension(path).ToLower() == "flif") // IM does not support FLIF, so we convert it first { return(FlifInterface.DecodeToMagickImage(path, false)); } else { MagickImage img = new MagickImage(path); if (showInfo) { Program.Print($"-> Loaded image {Path.GetFileName(path).Truncate(60)} ({img})"); } return(img); } } catch { Program.Print("Error reading " + Path.GetFileName(path) + "!"); } return(null); }
public static async Task ConvertToFlif(string path, int q, bool delSrc) { long bytesSrc = new FileInfo(path).Length; string outPath = Path.ChangeExtension(path, null) + ".flif"; if (await Config.GetInt("flifEnc") == 1) { await FlifInterface.EncodeImage(path, q, delSrc); } else { await ConvertToFlifMagick(path, q, delSrc); } PostProcessing(path, outPath, bytesSrc, delSrc); }
public static async void ConvertDirToFlif(bool useFlifExe, int q, bool delSrc) { int counter = 1; FileInfo[] files = IOUtils.GetFiles(); Program.PreProcessing(); foreach (FileInfo file in files) { Program.ShowProgress("Converting Image ", counter, files.Length); counter++; if (useFlifExe) { FlifInterface.EncodeImage(file.FullName, q, delSrc); } else { ConvertToFlif(file.FullName, q, delSrc); } await Program.PutTaskDelay(); } Program.PostProcessing(); }
public static void ConvertFileList(string[] files, ComboBox qualityCombox, ComboBox qualityMaxCombox, IF selectedFormat, CheckBox delSrcCbox) { int qMin = qualityCombox.GetInt(); int qMax = qMin; if (!string.IsNullOrWhiteSpace(qualityMaxCombox.Text.Trim())) { qMax = qualityMaxCombox.GetInt(); } foreach (string file in files) { Program.Print("\nDropped File: " + Path.GetFileName(file).Truncate(90)); if (!IOUtils.IsPathDirectory(file)) { if (selectedFormat == IF.JPG) { ConvertUtils.ConvertToJpeg(file, qMin, qMax, delSrcCbox.Checked); } if (selectedFormat == IF.PNG) { ConvertUtils.ConvertToPng(file, qMin, delSrcCbox.Checked); } if (selectedFormat == IF.DDS) { if (Config.GetInt("ddsEnc") == 0) { ConvertUtils.ConvertToDds(file, delSrcCbox.Checked); } if (Config.GetInt("ddsEnc") == 1) { DdsInterface.NvCompress(file, Path.ChangeExtension(file, "dds"), delSrcCbox.Checked); } if (Config.GetInt("ddsEnc") == 2) { DdsInterface.Crunch(file, qMin, qMax, delSrcCbox.Checked); } } if (selectedFormat == IF.TGA) { ConvertUtils.ConvertToTga(file, delSrcCbox.Checked); } if (selectedFormat == IF.WEBP) { ConvertUtils.ConvertToWebp(file, qMin, qMax, delSrcCbox.Checked); } if (selectedFormat == IF.J2K) { ConvertUtils.ConvertToJpeg2000(file, qMin, delSrcCbox.Checked); } if (selectedFormat == IF.FLIF) { if (Config.GetInt("flifEnc") == 1) { FlifInterface.EncodeImage(file, qMin, delSrcCbox.Checked); } else { ConvertUtils.ConvertToFlif(file, qMin, delSrcCbox.Checked); } } if (selectedFormat == IF.BMP) { ConvertUtils.ConvertToBmp(file, delSrcCbox.Checked); } if (selectedFormat == IF.AVIF) { ConvertUtils.ConvertToAvif(file, qMin, delSrcCbox.Checked); } if (selectedFormat == IF.HEIF) { HeifInterface.EncodeImage(file, qMin, delSrcCbox.Checked); } } } }
public static async Task ConvertFileList(string[] files, ComboBox qualityCombox, ComboBox qualityMaxCombox, IF selectedFormat, CheckBox delSrcCbox) { int qMin = qualityCombox.GetInt(); int qMax = qMin; if (!string.IsNullOrWhiteSpace(qualityMaxCombox.Text.Trim())) { qMax = qualityMaxCombox.GetInt(); } foreach (string file in files) { Logger.Log("\nDropped File: " + Path.GetFileName(file).Truncate(90)); if (!IOUtils.IsPathDirectory(file)) { if (selectedFormat == IF.JPG) { await ConvertUtils.ConvertToJpeg(file, qMin, qMax, delSrcCbox.Checked); } if (selectedFormat == IF.PNG) { await ConvertUtils.ConvertToPng(file, qMin, delSrcCbox.Checked); } if (selectedFormat == IF.DDS) { await ConvertUtils.ConvertToDds(file, qMin, qMax, delSrcCbox.Checked); } if (selectedFormat == IF.TGA) { await ConvertUtils.ConvertToTga(file, delSrcCbox.Checked); } if (selectedFormat == IF.WEBP) { await ConvertUtils.ConvertToWebp(file, qMin, qMax, delSrcCbox.Checked); } if (selectedFormat == IF.J2K) { await ConvertUtils.ConvertToJpeg2000(file, qMin, delSrcCbox.Checked); } if (selectedFormat == IF.FLIF) { if (await Config.GetInt("flifEnc") == 1) { await FlifInterface.EncodeImage(file, qMin, delSrcCbox.Checked); } else { await ConvertUtils.ConvertToFlif(file, qMin, delSrcCbox.Checked); } } if (selectedFormat == IF.BMP) { await ConvertUtils.ConvertToBmp(file, delSrcCbox.Checked); } if (selectedFormat == IF.AVIF) { await ConvertUtils.ConvertToAvif(file, qMin, delSrcCbox.Checked); } if (selectedFormat == IF.HEIF) { await HeifInterface.EncodeImage(file, qMin, delSrcCbox.Checked); } if (selectedFormat == IF.JXL) { await ConvertUtils.ConvertToJxl(file, qMin, qMax, delSrcCbox.Checked); } } } }