Example #1
0
        public static MagickImage GetMagickImage(string path, bool allowTgaFlip = false)
        {
            if (Logger.doLogIo)
            {
                Logger.Log("[ImgUtils] Reading MagickImage from " + path);
            }
            MagickImage image;

            if (Path.GetExtension(path).ToLower() == ".dds")
            {
                try
                {
                    image = new MagickImage(path);      // Try reading DDS with IM, fall back to DdsFileTypePlusHack if it fails
                }
                catch (Exception magickEx)
                {
                    Logger.Log($"[ImgUtils] Failed to read DDS using Magick.NET ({magickEx.Message}) - Trying DdsFileTypePlusHack");
                    try
                    {
                        Surface surface = DdsFile.Load(path);
                        image          = ConvertToMagickImage(surface);
                        image.HasAlpha = DdsFile.HasTransparency(surface);
                    }
                    catch (Exception ddsEx)
                    {
                        Logger.ErrorMessage("[ImgUtils] This DDS format appears to be incompatible.", ddsEx);
                        return(null);
                    }
                }
            }
            else
            {
                image = new MagickImage(path);
            }
            if (allowTgaFlip && Path.GetExtension(path).ToLower() == ".tga" && Config.GetBool("flipTga"))
            {
                image.Flip();
                if (Logger.doLogIo)
                {
                    Logger.Log("[Flipped TGA]", true, true);
                }
            }
            if (Logger.doLogIo)
            {
                Logger.Log("[OK]", true, true);
            }
            return(image);
        }
Example #2
0
 // 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);
 }
Example #3
0
        public static MagickImage LoadImage(FileInfo file)
        {
            MagickImage image;

            if (file.Extension.ToLower() == ".dds" && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                Surface surface = DdsFile.Load(file.FullName);
                image          = ConvertToMagickImage(surface);
                image.HasAlpha = DdsFile.HasTransparency(surface);
            }
            else
            {
                image = new MagickImage(file.FullName);
            }
            return(image);
        }