Esempio n. 1
0
        //Load JPEG example
        private void buttonLoad_Click(object sender, System.EventArgs e)
        {
            try
            {
                using (OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog())
                {
                    openFileDialog.Filter   = "Image files (*.jpg, *.jpeg, *.png)|*.jpg;*.jpeg;*.png";
                    openFileDialog.FileName = "";
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        this.buttonSave.Enabled = true;
                        this.buttonSave.Enabled = true;
                        string pathFileName = openFileDialog.FileName;

                        if (Path.GetExtension(pathFileName) == ".jpeg" || Path.GetExtension(pathFileName) == ".jpg")
                        {
                            using (MozJpeg mozJpeg = new MozJpeg())
                                this.pictureBox.Image = mozJpeg.Load(pathFileName);
                        }
                        else
                        {
                            this.pictureBox.Image = Image.FromFile(pathFileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\nIn MozJpegExample.buttonLoad_Click", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 2
0
        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);
         }
 }
Esempio n. 4
0
        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}");
        }
Esempio n. 5
0
 public void RecompressSpan(
     [CombinatorialValues(
          TJSubsamplingOption.Gray,
          TJSubsamplingOption.Chrominance411,
          TJSubsamplingOption.Chrominance420,
          TJSubsamplingOption.Chrominance440,
          TJSubsamplingOption.Chrominance422,
          TJSubsamplingOption.Chrominance444)]
     TJSubsamplingOption subsampling,
     [CombinatorialValues(1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)]
     int quality)
 {
     foreach (var data in TestUtils.GetTestImagesData("*.jpg"))
     {
         var compressed = MozJpeg.Recompress(data.Item2.AsSpan(), quality, subsampling);
         var fileName   = Path.GetFileName(data.Item1);
         Trace.WriteLine($"{fileName}; old#: {data.Item2.Length}; new#: {compressed.Length}; Ration: {compressed.Length / data.Item2.Length}; Subsampling: {subsampling}; Quality: {quality}");
     }
 }
Esempio n. 6
0
        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);
            }
        }
Esempio n. 7
0
        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);
        }
Esempio n. 8
0
        //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);
            }
        }
Esempio n. 9
0
        //Information of JPEG example
        private void buttonInfo_Click(object sender, EventArgs e)
        {
            int   width;
            int   height;
            float horizontalResolution;
            float verticalResolution;
            TJSubsamplingOptions subsampl;
            TJColorSpaces        colorspace;
            string subsamplLong;
            string colorspaceLong;

            try
            {
                using (OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog())
                {
                    openFileDialog.Filter   = "Jpeg files (*.jpg, *.jpeg)|*.jpg;*.jpeg";
                    openFileDialog.FileName = "";
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        this.buttonSave.Enabled = true;
                        this.buttonSave.Enabled = true;
                        string pathFileName = openFileDialog.FileName;
                        byte[] rawJpeg      = File.ReadAllBytes(pathFileName);

                        using (MozJpeg mozJpeg = new MozJpeg())
                            mozJpeg.GetInfo(rawJpeg, out width, out height, out horizontalResolution, out verticalResolution, out subsampl, out colorspace);

                        switch (subsampl)
                        {
                        case TJSubsamplingOptions.TJSAMP_444:
                            subsamplLong = "4:4:4 (no chrominance subsampling)";
                            break;

                        case TJSubsamplingOptions.TJSAMP_422:
                            subsamplLong = "4:2:2";
                            break;

                        case TJSubsamplingOptions.TJSAMP_420:
                            subsamplLong = "4:2:0";
                            break;

                        case TJSubsamplingOptions.TJSAMP_GRAY:
                            subsamplLong = "Grayscale. The JPEG not contain chrominance components.";
                            break;

                        case TJSubsamplingOptions.TJSAMP_440:
                            subsamplLong = "4:4:0";
                            break;

                        case TJSubsamplingOptions.TJSAMP_411:
                            subsamplLong = "4:1:1";
                            break;

                        default:
                            subsamplLong = "Unknown.";
                            break;
                        }

                        switch (colorspace)
                        {
                        case TJColorSpaces.TJCS_RGB:
                            colorspaceLong = "RGB.";
                            break;

                        case TJColorSpaces.TJCS_YCbCr:
                            colorspaceLong = "YCbCr.";
                            break;

                        case TJColorSpaces.TJCS_GRAY:
                            colorspaceLong = "Grayscale.";
                            break;

                        case TJColorSpaces.TJCS_CMYK:
                            colorspaceLong = "YCCK.";
                            break;

                        case TJColorSpaces.TJCS_YCCK:
                            colorspaceLong = "YCCK (AKA 'YCbCrK').";
                            break;

                        default:
                            colorspaceLong = "Unknown.";
                            break;
                        }

                        MessageBox.Show("Width: " + width + "\n" +
                                        "Height: " + height + "\n" +
                                        "Chroma subsample: " + subsamplLong + "\n" +
                                        "Color space: " + colorspaceLong + "\n" +
                                        "Horizontal resolution: " + horizontalResolution + "\n" +
                                        "Vertical resolution: " + verticalResolution + "\n");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\r\nIn MozJpegExample.buttonInfo_Click", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }