Пример #1
0
        public static async Task ConvertToWebp(string path, int qMin, int qMax, bool delSrc = false)
        {
            long        bytesSrc = new FileInfo(path).Length;
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.WebP;
            string outPath = Path.ChangeExtension(path, null) + ".webp";
            Random rand    = new Random();

            img.Quality = rand.Next(qMin, qMax + 1);

            if (img.Quality >= 100)
            {
                img.Settings.SetDefine(MagickFormat.WebP, "lossless", true);
            }

            string saveNote = $"WEBP Quality: {img.Quality.ToString().Replace("100", "Lossless")}";

            IOUtils.SaveImage(img, outPath);
            PostProcessing(path, outPath, bytesSrc, delSrc, saveNote);
        }
Пример #2
0
        static async Task ConvertToDdsNative(string path)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.Dds;
            DdsCompression compression = DdsCompression.None;

            if ((await Config.Get("ddsCompressionType")).Contains("BC1"))
            {
                compression = DdsCompression.Dxt1;
            }

            int mips = 0;

            if (await Config.GetBool("ddsEnableMips"))
            {
                mips = await Config.GetInt("mipCount");
            }
            var defines = new DdsWriteDefines {
                Compression = compression, Mipmaps = mips, FastMipmaps = true
            };

            img.Settings.SetDefines(defines);
            string outPath = Path.ChangeExtension(path, null) + ".dds";

            IOUtils.SaveImage(img, outPath);
        }
Пример #3
0
        public static async Task ConvertToPng(string path, int q = 50, bool delSrc = false)
        {
            long        bytesSrc = new FileInfo(path).Length;
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.Png;
            if (await Config.GetInt("pngColorDepth") == 1)
            {
                img.Format = MagickFormat.Png24;
            }
            if (await Config.GetInt("pngColorDepth") == 2)
            {
                img.Format = MagickFormat.Png32;
            }
            img.Quality = q;
            string outPath = Path.ChangeExtension(path, null) + ".png";

            if (path == outPath)
            {
                File.Delete(path);
            }

            IOUtils.SaveImage(img, outPath);
            PostProcessing(path, outPath, bytesSrc, delSrc);
        }
Пример #4
0
        static async Task <string> Write(MagickImage img, FT filter)    // Returns output path
        {
            img.Quality = await Program.GetFormatQuality(img);

            if (!appendFiltername)
            {
                if (!dontOverwrite)
                {
                    img.Write(img.FileName);
                    return(img.FileName);
                }
                else
                {
                    string pathNoExtension = Path.ChangeExtension(img.FileName, null);
                    string ext             = Path.GetExtension(img.FileName);
                    string outPath         = pathNoExtension + "-Scaled" + ext;
                    IOUtils.SaveImage(img, outPath);
                    return(outPath);
                }
            }
            else
            {
                string oldPath         = img.FileName;
                string pathNoExtension = Path.ChangeExtension(img.FileName, null);
                string ext             = Path.GetExtension(img.FileName);
                string outPath         = pathNoExtension + "-Scaled-" + filter.ToString().Replace("Catrom", "Bicubic") + ext;
                IOUtils.SaveImage(img, outPath);
                if (!dontOverwrite)
                {
                    File.Delete(oldPath);
                }
                return(outPath);
            }
        }
Пример #5
0
        public static async Task ConvertToBmp(string path, bool delSrc)
        {
            long        bytesSrc = new FileInfo(path).Length;
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.Bmp;
            string outPath = Path.ChangeExtension(path, null) + ".bmp";

            IOUtils.SaveImage(img, outPath);
            PostProcessing(path, outPath, bytesSrc, delSrc);
        }
Пример #6
0
        public static async Task ConvertToFlifMagick(string path, int q, bool delSrc)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format  = MagickFormat.Flif;
            img.Quality = q;
            img.Settings.SetDefine(MagickFormat.Flif, "lossless", true);
            string outPath = Path.ChangeExtension(path, null) + ".flif";

            IOUtils.SaveImage(img, outPath);
        }
Пример #7
0
        public static async Task ConvertToJpeg2000(string path, int q, bool delSrc = false)
        {
            long        bytesSrc = new FileInfo(path).Length;
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format = MagickFormat.Jp2;
            string outPath = Path.ChangeExtension(path, null) + ".jp2";

            img.Quality = q;
            IOUtils.SaveImage(img, outPath);
            PostProcessing(path, outPath, bytesSrc, delSrc);
        }
Пример #8
0
        public static async Task ConvertToJxl(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) + ".jxl";
            MagickImage img      = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            img.Format  = MagickFormat.Jxl;
            img.Quality = q;
            IOUtils.SaveImage(img, outPath);
            PostProcessing(path, outPath, bytesSrc, delSrc, $"JPEG XL Quality: {q}");
        }
Пример #9
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}");
        }