mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-11 17:59:17 +00:00
Limitations: Only supports x64 systems
This commit is contained in:
@@ -75,74 +75,75 @@ internal class ImageMagickProvider : AnimationProvider
|
||||
}
|
||||
|
||||
public override Task<BitmapSource> GetRenderedFrame(int index)
|
||||
{
|
||||
return new Task<BitmapSource>(GetRenderedFrame);
|
||||
}
|
||||
|
||||
protected virtual BitmapSource GetRenderedFrame()
|
||||
{
|
||||
var fullSize = Meta.GetSize();
|
||||
|
||||
return new Task<BitmapSource>(() =>
|
||||
var settings = new MagickReadSettings
|
||||
{
|
||||
var settings = new MagickReadSettings
|
||||
BackgroundColor = MagickColors.None,
|
||||
Defines = new DngReadDefines
|
||||
{
|
||||
BackgroundColor = MagickColors.None,
|
||||
Defines = new DngReadDefines
|
||||
{
|
||||
OutputColor = DngOutputColor.SRGB,
|
||||
UseCameraWhiteBalance = true,
|
||||
DisableAutoBrightness = false
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using (MagickImageCollection 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)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
OutputColor = DngOutputColor.SRGB,
|
||||
UseCameraWhiteBalance = true,
|
||||
DisableAutoBrightness = false
|
||||
}
|
||||
catch (Exception e)
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using (MagickImageCollection layers = new MagickImageCollection(Path.LocalPath, settings))
|
||||
{
|
||||
ProcessHelper.WriteLog(e.ToString());
|
||||
return null!;
|
||||
IMagickImage<byte> mi;
|
||||
// Only flatten multi-layer gimp xcf files.
|
||||
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;
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ProcessHelper.WriteLog(e.ToString());
|
||||
return null!;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
private static TransformedBitmap RotateAndScaleThumbnail(BitmapImage image, Orientation orientation,
|
||||
protected static TransformedBitmap RotateAndScaleThumbnail(BitmapImage image, Orientation orientation,
|
||||
Size fullSize)
|
||||
{
|
||||
var swap = false;
|
||||
|
@@ -0,0 +1,221 @@
|
||||
// Copyright © 2024 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using ImageGlass.Base.Photoing.Codecs;
|
||||
using ImageGlass.WebP;
|
||||
using ImageMagick;
|
||||
using ImageMagick.Formats;
|
||||
using QuickLook.Common.Helpers;
|
||||
using QuickLook.Common.Plugin;
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace QuickLook.Plugin.ImageViewer.AnimatedImage.Providers;
|
||||
|
||||
internal class WebPProvider : ImageMagickProvider
|
||||
{
|
||||
private bool _isPlaying;
|
||||
|
||||
public WebPProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
|
||||
{
|
||||
}
|
||||
|
||||
public override Task<BitmapSource> GetRenderedFrame(int index)
|
||||
{
|
||||
return new Task<BitmapSource>(() =>
|
||||
{
|
||||
var settings = new MagickReadSettings
|
||||
{
|
||||
BackgroundColor = MagickColors.None,
|
||||
Defines = new DngReadDefines
|
||||
{
|
||||
OutputColor = DngOutputColor.SRGB,
|
||||
UseCameraWhiteBalance = true,
|
||||
DisableAutoBrightness = false
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
// Unfortunately we only support Animated WebP on x64 platforms
|
||||
if (Environment.Is64BitProcess)
|
||||
{
|
||||
var layers = MagickImageInfo.ReadCollection(Path.LocalPath);
|
||||
int count = layers.Count();
|
||||
|
||||
// Animated WebP image
|
||||
if (count > 1)
|
||||
{
|
||||
return AnimatedWebP(Path.LocalPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
return base.GetRenderedFrame();
|
||||
}
|
||||
}
|
||||
|
||||
return base.GetRenderedFrame();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
ProcessHelper.WriteLog(e.ToString());
|
||||
return null!;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
_isPlaying = false;
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
private BitmapSource AnimatedWebP(string fileName)
|
||||
{
|
||||
using var webP = new WebPWrapper();
|
||||
|
||||
var aniWebP = webP.AnimLoad(fileName);
|
||||
var frames = aniWebP.Select(frame =>
|
||||
{
|
||||
var duration = frame.Duration > 0 ? frame.Duration : 100;
|
||||
var bitmap = frame.Bitmap;
|
||||
|
||||
return new AnimatedImgFrame(frame.Bitmap, (uint)duration);
|
||||
});
|
||||
|
||||
var animatedImg = new AnimatedImg(frames, frames.Count());
|
||||
|
||||
var writeableBitmap = Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
var frame = animatedImg.Frames.ElementAt(0);
|
||||
var bitmap = (Bitmap)frame.Bitmap;
|
||||
return bitmap.ToWriteableBitmap();
|
||||
});
|
||||
|
||||
_isPlaying = true;
|
||||
_ = Task.Factory.StartNew(() =>
|
||||
{
|
||||
while (_isPlaying)
|
||||
{
|
||||
foreach (var frame in animatedImg.Frames)
|
||||
{
|
||||
if (!_isPlaying) break;
|
||||
|
||||
writeableBitmap.Dispatcher.Invoke(() =>
|
||||
{
|
||||
var bitmap = (Bitmap)frame.Bitmap;
|
||||
bitmap.CopyToWriteableBitmap(writeableBitmap);
|
||||
});
|
||||
|
||||
Thread.Sleep((int)frame.Duration.TotalMilliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
animatedImg?.Dispose();
|
||||
animatedImg = null;
|
||||
}, TaskCreationOptions.LongRunning);
|
||||
|
||||
return writeableBitmap;
|
||||
}
|
||||
}
|
||||
|
||||
file static class Extension
|
||||
{
|
||||
public static WriteableBitmap ToWriteableBitmap(this Bitmap bitmap)
|
||||
{
|
||||
if (bitmap == null) throw new ArgumentNullException(nameof(bitmap));
|
||||
|
||||
var pixelFormat = bitmap.PixelFormat;
|
||||
var width = bitmap.Width;
|
||||
var height = bitmap.Height;
|
||||
|
||||
var wpfPixelFormat = pixelFormat switch
|
||||
{
|
||||
System.Drawing.Imaging.PixelFormat.Format32bppArgb => PixelFormats.Bgra32,
|
||||
System.Drawing.Imaging.PixelFormat.Format24bppRgb => PixelFormats.Bgr24,
|
||||
_ => throw new NotSupportedException($"Unsupported PixelFormat: {pixelFormat}")
|
||||
};
|
||||
|
||||
var writeableBitmap = new WriteableBitmap(width, height, 96, 96, wpfPixelFormat, null);
|
||||
|
||||
var bitmapData = bitmap.LockBits(
|
||||
new Rectangle(0, 0, width, height),
|
||||
ImageLockMode.ReadOnly,
|
||||
pixelFormat);
|
||||
|
||||
try
|
||||
{
|
||||
writeableBitmap.Lock();
|
||||
unsafe
|
||||
{
|
||||
Buffer.MemoryCopy(
|
||||
source: bitmapData.Scan0.ToPointer(),
|
||||
destination: writeableBitmap.BackBuffer.ToPointer(),
|
||||
destinationSizeInBytes: writeableBitmap.BackBufferStride * height,
|
||||
sourceBytesToCopy: bitmapData.Stride * height);
|
||||
}
|
||||
|
||||
writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
|
||||
}
|
||||
finally
|
||||
{
|
||||
bitmap.UnlockBits(bitmapData);
|
||||
writeableBitmap.Unlock();
|
||||
}
|
||||
|
||||
return writeableBitmap;
|
||||
}
|
||||
|
||||
public static void CopyToWriteableBitmap(this Bitmap bitmap, WriteableBitmap writeableBitmap)
|
||||
{
|
||||
var pixelFormat = bitmap.PixelFormat;
|
||||
var width = bitmap.Width;
|
||||
var height = bitmap.Height;
|
||||
|
||||
var bitmapData = bitmap.LockBits(
|
||||
new Rectangle(0, 0, width, height),
|
||||
ImageLockMode.ReadOnly,
|
||||
pixelFormat);
|
||||
|
||||
try
|
||||
{
|
||||
writeableBitmap.Lock();
|
||||
unsafe
|
||||
{
|
||||
Buffer.MemoryCopy(
|
||||
source: bitmapData.Scan0.ToPointer(),
|
||||
destination: writeableBitmap.BackBuffer.ToPointer(),
|
||||
destinationSizeInBytes: writeableBitmap.BackBufferStride * height,
|
||||
sourceBytesToCopy: bitmapData.Stride * height);
|
||||
}
|
||||
|
||||
writeableBitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
|
||||
}
|
||||
finally
|
||||
{
|
||||
bitmap.UnlockBits(bitmapData);
|
||||
writeableBitmap.Unlock();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
// Copyright © 2018 Paddy Xu
|
||||
// Copyright © 2024 QL-Win Contributors
|
||||
//
|
||||
// This file is part of QuickLook program.
|
||||
//
|
||||
@@ -76,6 +76,9 @@ public class Plugin : IViewer
|
||||
AnimatedImage.AnimatedImage.Providers.Add(
|
||||
new KeyValuePair<string[], Type>([".icns"],
|
||||
typeof(IcnsProvider)));
|
||||
AnimatedImage.AnimatedImage.Providers.Add(
|
||||
new KeyValuePair<string[], Type>([".webp"],
|
||||
typeof(WebPProvider)));
|
||||
AnimatedImage.AnimatedImage.Providers.Add(
|
||||
new KeyValuePair<string[], Type>(["*"],
|
||||
typeof(ImageMagickProvider)));
|
||||
|
@@ -55,6 +55,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="QuickLook.ImageGlass.WebP" Version="1.4.0" />
|
||||
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="14.2.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
Reference in New Issue
Block a user