public static void ConvertToJpeg(string path, int qMin, int qMax, bool delSource = false) { Random rand = new Random(); int q = rand.Next(qMin, qMax + 1); string outPath = Path.ChangeExtension(path, null) + ".jpg"; PreProcessing(path, " [JPEG Quality: " + q + "]"); if (Config.GetInt("jpegEnc") == 0) { MagickImage img = IOUtils.ReadImage(path); if (img == null) { return; } img.Format = MagickFormat.Jpeg; img.Quality = q; img = SetJpegChromaSubsampling(img); img.Write(outPath); PostProcessing(img, path, outPath, delSource); } else { switch (Config.GetInt("jpegChromaSubsampling")) { case 0: MozJpeg.Encode(path, outPath, q, MozJpeg.Subsampling.Chroma420); break; case 1: MozJpeg.Encode(path, outPath, q, MozJpeg.Subsampling.Chroma422); break; case 2: MozJpeg.Encode(path, outPath, q, MozJpeg.Subsampling.Chroma444); break; } PostProcessing(null, path, outPath, delSource); } }
public void Write(Image i, Stream s) { using (var bitmap = new Bitmap(i)) using (var encoder = new MozJpeg()) { var data = encoder.Encode(bitmap, Quality, false); s.Write(data, 0, data.Length); } }
public static async Task ConvertToJpeg(string path, int qMin, int qMax, bool delSrc = false) { long bytesSrc = new FileInfo(path).Length; Random rand = new Random(); int q = rand.Next(qMin, qMax + 1); string outPath = Path.ChangeExtension(path, null) + ".jpg"; if (await Config.GetInt("jpegEnc") == 0) // Native Magick JPEG Encoder { MagickImage img = IOUtils.ReadImage(path); if (img == null) { return; } img.Format = MagickFormat.Jpeg; img.Quality = q; img = await SetJpegChromaSubsampling(img); IOUtils.SaveImage(img, outPath); } else // MozJPEG Encoder { bool convert = !IsPng(path); if (convert) { path = await ConvertToTempPng(path); } switch (await Config.GetInt("jpegChromaSubsampling")) { case 0: MozJpeg.Encode(path, outPath, q, MozJpeg.Subsampling.Chroma420); break; case 1: MozJpeg.Encode(path, outPath, q, MozJpeg.Subsampling.Chroma422); break; case 2: MozJpeg.Encode(path, outPath, q, MozJpeg.Subsampling.Chroma444); break; } if (convert) { IOUtils.TryDeleteIfExists(path); } } PostProcessing(path, outPath, bytesSrc, delSrc, $"JPEG Quality: {q}"); }
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); }
//Load JPEG examples private void buttonSave_Click(object sender, System.EventArgs e) { string fileName; byte[] rawJpeg; try { //get the picturebox image Bitmap bmp = (Bitmap)pictureBox.Image; //Test simple save function with quality 75 fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SimpleSave.jpg"); using (MozJpeg mozJpeg = new MozJpeg()) mozJpeg.Save(bmp, fileName, 75); MessageBox.Show("Made " + fileName); //Test encode in memory with quality 75 in Baseline format fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Baseline.jpg"); using (MozJpeg mozJpeg = new MozJpeg()) rawJpeg = mozJpeg.Encode(bmp, 75, true, TJFlags.BASELINE); File.WriteAllBytes(fileName, rawJpeg); MessageBox.Show("Made " + fileName); //Test encode lossly mode in memory with quality 75, with JFIF in grayscale fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Grayscale.jpg"); using (MozJpeg mozJpeg = new MozJpeg()) rawJpeg = mozJpeg.Encode(bmp, 75, true, TJFlags.NONE, TJSubsamplingOptions.TJSAMP_GRAY); File.WriteAllBytes(fileName, rawJpeg); MessageBox.Show("Made " + fileName); //Test encode lossly mode in memory with quality 75, with JFIF and optimize scan fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Optimize1.jpg"); using (MozJpeg mozJpeg = new MozJpeg()) rawJpeg = mozJpeg.Encode(bmp, 75, true, TJFlags.DC_SCAN_OPT2); File.WriteAllBytes(fileName, rawJpeg); MessageBox.Show("Made " + fileName); //Test encode lossly mode in memory with quality 75, without JFIF and optimize scan fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Optimize2.jpg"); using (MozJpeg mozJpeg = new MozJpeg()) rawJpeg = mozJpeg.Encode(bmp, 75, true, TJFlags.ACCURATEDCT | TJFlags.DC_SCAN_OPT2); File.WriteAllBytes(fileName, rawJpeg); MessageBox.Show("Made " + fileName); //Test encode lossly mode in memory with quality 75, without JFIF and optimize scan fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Optimize3.jpg"); using (MozJpeg mozJpeg = new MozJpeg()) rawJpeg = mozJpeg.Encode(bmp, 75, false, TJFlags.ACCURATEDCT | TJFlags.DC_SCAN_OPT2); File.WriteAllBytes(fileName, rawJpeg); MessageBox.Show("Made " + fileName); //Test encode lossly mode in memory with quality 75, without JFIF and optimize scan fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Arithmetic.jpg"); using (MozJpeg mozJpeg = new MozJpeg()) rawJpeg = mozJpeg.Encode(bmp, 75, false, TJFlags.ACCURATEDCT | TJFlags.DC_SCAN_OPT2 | TJFlags.ARITHMETIC); File.WriteAllBytes(fileName, rawJpeg); MessageBox.Show("Made " + fileName); //Test encode lossly mode in memory with quality 75, without JFIF and optimize scan fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Tune_MS_SSIM.jpg"); using (MozJpeg mozJpeg = new MozJpeg()) rawJpeg = mozJpeg.Encode(bmp, 75, false, TJFlags.ACCURATEDCT | TJFlags.DC_SCAN_OPT2 | TJFlags.TUNE_MS_SSIM); File.WriteAllBytes(fileName, rawJpeg); MessageBox.Show("Made " + fileName); MessageBox.Show("End of Test"); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\nIn MozJpegExample.buttonSave_Click", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }