Code Cleanup

This commit is contained in:
ema
2025-07-01 03:18:48 +08:00
parent 2750cb75dd
commit fa61b1e68f

View File

@@ -48,23 +48,21 @@ internal class ImageMagickProvider : AnimationProvider
{ {
try try
{ {
using (var buffer = new MemoryStream(Meta.GetThumbnail())) using var buffer = new MemoryStream(Meta.GetThumbnail());
{ if (buffer.Length == 0)
if (buffer.Length == 0) return null;
return null;
var img = new BitmapImage(); var img = new BitmapImage();
img.BeginInit(); img.BeginInit();
img.StreamSource = buffer; img.StreamSource = buffer;
img.CacheOption = BitmapCacheOption.OnLoad; img.CacheOption = BitmapCacheOption.OnLoad;
img.EndInit(); img.EndInit();
var transformed = RotateAndScaleThumbnail(img, orientation, fullSize); var transformed = RotateAndScaleThumbnail(img, orientation, fullSize);
Helper.DpiHack(transformed); Helper.DpiHack(transformed);
transformed.Freeze(); transformed.Freeze();
return transformed; return transformed;
}
} }
catch (Exception e) catch (Exception e)
{ {
@@ -95,42 +93,40 @@ internal class ImageMagickProvider : AnimationProvider
try try
{ {
using (MagickImageCollection layers = new MagickImageCollection(Path.LocalPath, settings)) using var layers = new MagickImageCollection(Path.LocalPath, settings);
IMagickImage<byte> mi;
// Only flatten multi-layer gimp xcf files.
if (Path.LocalPath.ToLower().EndsWith(".xcf") && layers.Count > 1)
{ {
IMagickImage<byte> mi; // Flatten crops layers to canvas
// Only flatten multi-layer gimp xcf files. mi = layers.Flatten(MagickColor.FromRgba(0, 0, 0, 0));
if (Path.LocalPath.ToLower().EndsWith(".xcf") && layers.Count > 1)
{
// Flatten crops layers to canvas
mi = layers.Flatten(MagickColor.FromRgba(0, 0, 0, 0));
}
else
{
mi = layers[0];
}
if (SettingHelper.Get("UseColorProfile", false, "QuickLook.Plugin.ImageViewer"))
{
if (mi.ColorSpace == ColorSpace.RGB || mi.ColorSpace == ColorSpace.sRGB || mi.ColorSpace == ColorSpace.scRGB)
{
mi.SetProfile(ColorProfile.SRGB);
if (ContextObject.ColorProfileName != null)
mi.SetProfile(new ColorProfile(ContextObject.ColorProfileName)); // map to monitor color
}
}
mi.AutoOrient();
if (mi.Width != (int)fullSize.Width || mi.Height != (int)fullSize.Height)
mi.Resize((uint)fullSize.Width, (uint)fullSize.Height);
mi.Density = new Density(DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal,
DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical);
var img = mi.ToBitmapSourceWithDensity();
img.Freeze();
return img;
} }
else
{
mi = layers[0];
}
if (SettingHelper.Get("UseColorProfile", false, "QuickLook.Plugin.ImageViewer"))
{
if (mi.ColorSpace == ColorSpace.RGB || mi.ColorSpace == ColorSpace.sRGB || mi.ColorSpace == ColorSpace.scRGB)
{
mi.SetProfile(ColorProfile.SRGB);
if (ContextObject.ColorProfileName != null)
mi.SetProfile(new ColorProfile(ContextObject.ColorProfileName)); // map to monitor color
}
}
mi.AutoOrient();
if (mi.Width != (int)fullSize.Width || mi.Height != (int)fullSize.Height)
mi.Resize((uint)fullSize.Width, (uint)fullSize.Height);
mi.Density = new Density(DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal,
DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical);
var img = mi.ToBitmapSourceWithDensity();
img.Freeze();
return img;
} }
catch (Exception e) catch (Exception e)
{ {
@@ -143,8 +139,7 @@ internal class ImageMagickProvider : AnimationProvider
{ {
} }
protected static TransformedBitmap RotateAndScaleThumbnail(BitmapImage image, Orientation orientation, protected static TransformedBitmap RotateAndScaleThumbnail(BitmapImage image, Orientation orientation, Size fullSize)
Size fullSize)
{ {
var swap = false; var swap = false;