Skip to content

Commit

Permalink
Support Animated WebP #1024 #1324
Browse files Browse the repository at this point in the history
Limitations: Only supports x64 systems
  • Loading branch information
emako committed Dec 30, 2024
1 parent d96c6e2 commit 9483862
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,74 +75,75 @@ public override Task<BitmapSource> GetThumbnail(Size renderSize)
}

public override Task<BitmapSource> GetRenderedFrame(int index)
{
return new Task<BitmapSource>(GetRenderedFrame);
}

protected virtual BitmapSource GetRenderedFrame()
{
var fullSize = Meta.GetSize();
var settings = new MagickReadSettings
{
BackgroundColor = MagickColors.None,
Defines = new DngReadDefines
{
OutputColor = DngOutputColor.SRGB,
UseCameraWhiteBalance = true,
DisableAutoBrightness = false
}
};

return new Task<BitmapSource>(() =>
try
{
var settings = new MagickReadSettings
using (MagickImageCollection layers = new MagickImageCollection(Path.LocalPath, settings))
{
BackgroundColor = MagickColors.None,
Defines = new DngReadDefines
IMagickImage<byte> mi;
// Only flatten multi-layer gimp xcf files.
if (Path.LocalPath.ToLower().EndsWith(".xcf") && layers.Count > 1)
{
OutputColor = DngOutputColor.SRGB,
UseCameraWhiteBalance = true,
DisableAutoBrightness = false
// Flatten crops layers to canvas
mi = layers.Flatten(MagickColor.FromRgba(0, 0, 0, 0));
}
};

try
{
using (MagickImageCollection layers = new MagickImageCollection(Path.LocalPath, settings))
else
{
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"))
mi = layers[0];
}
if (SettingHelper.Get("UseColorProfile", false, "QuickLook.Plugin.ImageViewer"))
{
if (mi.ColorSpace == ColorSpace.RGB || mi.ColorSpace == ColorSpace.sRGB || mi.ColorSpace == ColorSpace.scRGB)
{
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.SetProfile(ColorProfile.SRGB);
if (ContextObject.ColorProfileName != null)
mi.SetProfile(new ColorProfile(ContextObject.ColorProfileName)); // map to monitor color
}
}

mi.AutoOrient();
mi.AutoOrient();

if (mi.Width != (int)fullSize.Width || mi.Height != (int)fullSize.Height)
mi.Resize((uint)fullSize.Width, (uint)fullSize.Height);
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);
mi.Density = new Density(DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal,
DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical);

var img = mi.ToBitmapSourceWithDensity();
var img = mi.ToBitmapSourceWithDensity();

img.Freeze();
return img;
}
img.Freeze();
return img;
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
return null!;
}
});
}
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// 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 wpfPixelFormat = pixelFormat switch
{
System.Drawing.Imaging.PixelFormat.Format32bppArgb => PixelFormats.Bgra32,
System.Drawing.Imaging.PixelFormat.Format24bppRgb => PixelFormats.Bgr24,
_ => throw new NotSupportedException($"Unsupported PixelFormat: {pixelFormat}")
};

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();
}
}
}
5 changes: 4 additions & 1 deletion QuickLook.Plugin/QuickLook.Plugin.ImageViewer/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2018 Paddy Xu
// Copyright © 2024 QL-Win Contributors
//
// This file is part of QuickLook program.
//
Expand Down Expand Up @@ -76,6 +76,9 @@ public void Init()
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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 9483862

Please sign in to comment.