コード例 #1
0
        static string GetOutPath(string path, string newExt, ExtMode extMode, string overrideOutPath)
        {
            if (!string.IsNullOrWhiteSpace(overrideOutPath))
            {
                return(overrideOutPath);
            }

            if (extMode == ExtMode.UseNew)
            {
                return(Path.ChangeExtension(path, newExt));
            }

            if (extMode == ExtMode.KeepOld)
            {
                return(path);
            }

            if (extMode == ExtMode.AppendNew)
            {
                return(path + "." + newExt);
            }

            return(null);
        }
コード例 #2
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);
        }