Flow.Launcher/Flow.Launcher.Infrastructure/Image/ImageLoader.cs

413 lines
15 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
2014-07-14 11:03:52 +00:00
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2014-07-14 11:03:52 +00:00
using System.Windows.Media;
using System.Windows.Media.Imaging;
2025-04-13 09:11:36 +00:00
using Flow.Launcher.Infrastructure.Logger;
2020-04-21 09:12:17 +00:00
using Flow.Launcher.Infrastructure.Storage;
2025-04-09 08:15:37 +00:00
using SharpVectors.Converters;
using SharpVectors.Renderers.Wpf;
2014-07-14 11:03:52 +00:00
2020-08-05 09:57:23 +00:00
namespace Flow.Launcher.Infrastructure.Image
2014-07-14 11:03:52 +00:00
{
public static class ImageLoader
2014-07-14 11:03:52 +00:00
{
2025-04-08 13:38:33 +00:00
private static readonly string ClassName = nameof(ImageLoader);
private static readonly ImageCache ImageCache = new();
private static Lock storageLock { get; } = new();
2024-05-29 22:57:26 +00:00
private static BinaryStorage<List<(string, bool)>> _storage;
private static readonly ConcurrentDictionary<string, string> GuidToKey = new();
2025-09-18 10:29:45 +00:00
private static ImageHashGenerator _hashGenerator;
private static readonly bool EnableImageHash = true;
2025-08-13 06:01:04 +00:00
public static ImageSource Image => ImageCache[Constant.ImageIcon, false];
public static ImageSource MissingImage => ImageCache[Constant.MissingImgIcon, false];
public static ImageSource LoadingImage => ImageCache[Constant.LoadingImgIcon, false];
2022-11-26 17:17:13 +00:00
public const int SmallIconSize = 64;
public const int FullIconSize = 256;
2025-04-09 08:15:37 +00:00
public const int FullImageSize = 320;
2025-09-18 10:29:45 +00:00
private static readonly string[] ImageExtensions = [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico"];
2025-04-09 08:15:37 +00:00
private static readonly string SvgExtension = ".svg";
2014-07-14 11:03:52 +00:00
public static async Task InitializeAsync()
{
var usage = await Task.Run(() =>
{
_storage = new BinaryStorage<List<(string, bool)>>("Image");
_hashGenerator = new ImageHashGenerator();
var usage = LoadStorageToConcurrentDictionary();
_storage.ClearData();
ImageCache.Initialize(usage);
2023-04-24 14:58:32 +00:00
2025-08-13 06:01:04 +00:00
foreach (var icon in new[] { Constant.DefaultIcon, Constant.ImageIcon, Constant.MissingImgIcon, Constant.LoadingImgIcon })
{
ImageSource img = new BitmapImage(new Uri(icon));
img.Freeze();
ImageCache[icon, false] = img;
}
return usage;
});
_ = Task.Run(async () =>
{
2025-04-13 09:11:36 +00:00
await Stopwatch.InfoAsync(ClassName, "Preload images cost", async () =>
{
2024-05-29 22:57:26 +00:00
foreach (var (path, isFullImage) in usage)
{
await LoadAsync(path, isFullImage);
}
});
2025-04-13 09:11:36 +00:00
Log.Info(ClassName, $"Number of preload images is <{ImageCache.CacheSize()}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
});
}
public static void Save()
{
lock (storageLock)
{
try
{
_storage.Save([.. ImageCache.EnumerateEntries().Select(x => x.Key)]);
}
catch (System.Exception e)
{
Log.Exception(ClassName, "Failed to save image cache to file", e);
}
}
}
private static List<(string, bool)> LoadStorageToConcurrentDictionary()
{
lock (storageLock)
{
return _storage.TryLoad([]);
}
}
private class ImageResult
{
public ImageResult(ImageSource imageSource, ImageType imageType)
{
ImageSource = imageSource;
ImageType = imageType;
}
public ImageType ImageType { get; }
public ImageSource ImageSource { get; }
}
private enum ImageType
{
File,
Folder,
Data,
ImageFile,
2022-09-02 11:36:57 +00:00
FullImageFile,
Error,
Cache
}
private static async ValueTask<ImageResult> LoadInternalAsync(string path, bool loadFullImage = false)
2014-07-14 11:03:52 +00:00
{
2020-05-02 11:12:34 +00:00
ImageResult imageResult;
2015-01-15 12:47:48 +00:00
try
2014-07-14 11:03:52 +00:00
{
if (string.IsNullOrEmpty(path))
2015-01-15 12:47:48 +00:00
{
return new ImageResult(MissingImage, ImageType.Error);
2016-05-03 22:21:03 +00:00
}
// extra scope for use of same variable name
2022-09-02 11:38:49 +00:00
{
if (ImageCache.TryGetValue(path, loadFullImage, out var imageSource))
{
return new ImageResult(imageSource, ImageType.Cache);
}
2022-09-02 11:38:49 +00:00
}
if (Uri.TryCreate(path, UriKind.RelativeOrAbsolute, out var uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
{
2022-10-30 19:24:38 +00:00
var image = await LoadRemoteImageAsync(loadFullImage, uriResult);
ImageCache[path, loadFullImage] = image;
2022-09-21 16:02:39 +00:00
return new ImageResult(image, ImageType.ImageFile);
}
2024-12-02 07:20:21 +00:00
if (path.StartsWith("data:image", StringComparison.OrdinalIgnoreCase))
2015-02-04 15:16:41 +00:00
{
2020-01-03 20:33:00 +00:00
var imageSource = new BitmapImage(new Uri(path));
imageSource.Freeze();
return new ImageResult(imageSource, ImageType.Data);
2015-02-04 15:16:41 +00:00
}
2022-11-23 07:03:15 +00:00
imageResult = await Task.Run(() => GetThumbnailResult(ref path, loadFullImage));
2020-05-02 11:12:34 +00:00
}
catch (System.Exception e)
{
try
{
2020-05-03 04:32:52 +00:00
// Get thumbnail may fail for certain images on the first try, retry again has proven to work
2020-05-02 11:12:34 +00:00
imageResult = GetThumbnailResult(ref path, loadFullImage);
}
catch (System.Exception e2)
{
2025-04-15 04:51:38 +00:00
Log.Exception(ClassName, $"Failed to get thumbnail for {path} on first try", e);
Log.Exception(ClassName, $"Failed to get thumbnail for {path} on second try", e2);
2025-08-13 06:01:04 +00:00
ImageSource image = MissingImage;
ImageCache[path, false] = image;
2020-05-02 11:12:34 +00:00
imageResult = new ImageResult(image, ImageType.Error);
}
2020-05-02 11:12:34 +00:00
}
return imageResult;
}
2022-10-30 19:24:38 +00:00
private static async Task<BitmapImage> LoadRemoteImageAsync(bool loadFullImage, Uri uriResult)
{
// Download image from url
2025-04-05 08:38:06 +00:00
await using var resp = await Http.Http.GetStreamAsync(uriResult);
2022-10-30 19:24:38 +00:00
await using var buffer = new MemoryStream();
await resp.CopyToAsync(buffer);
buffer.Seek(0, SeekOrigin.Begin);
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
if (!loadFullImage)
{
image.DecodePixelHeight = SmallIconSize;
image.DecodePixelWidth = SmallIconSize;
}
2022-10-30 19:24:38 +00:00
image.StreamSource = buffer;
image.EndInit();
image.StreamSource = null;
image.Freeze();
return image;
}
2020-05-02 11:12:34 +00:00
private static ImageResult GetThumbnailResult(ref string path, bool loadFullImage = false)
{
ImageSource image;
ImageType type = ImageType.Error;
if (Directory.Exists(path))
{
/* Directories can also have thumbnails instead of shell icons.
* Generating thumbnails for a bunch of folder results while scrolling
* could have a big impact on performance and Flow.Launcher responsibility.
2020-05-02 11:12:34 +00:00
* - Solution: just load the icon
*/
type = ImageType.Folder;
image = GetThumbnail(path, ThumbnailOptions.IconOnly);
}
else if (File.Exists(path))
{
var extension = Path.GetExtension(path).ToLower();
if (ImageExtensions.Contains(extension))
{
2020-05-02 11:12:34 +00:00
type = ImageType.ImageFile;
if (loadFullImage)
2015-11-02 00:04:05 +00:00
{
try
{
image = LoadFullImage(path);
type = ImageType.FullImageFile;
}
2025-04-09 08:28:53 +00:00
catch (NotSupportedException ex)
{
image = Image;
type = ImageType.Error;
2025-04-13 09:11:36 +00:00
Log.Exception(ClassName, $"Failed to load image file from path {path}: {ex.Message}", ex);
}
2015-11-02 00:04:05 +00:00
}
else
2015-11-02 00:04:05 +00:00
{
/* Although the documentation for GetImage on MSDN indicates that
2020-05-02 11:12:34 +00:00
* if a thumbnail is available it will return one, this has proved to not
* be the case in many situations while testing.
2020-05-02 11:12:34 +00:00
* - Solution: explicitly pass the ThumbnailOnly flag
*/
image = GetThumbnail(path, ThumbnailOptions.ThumbnailOnly);
2015-11-02 00:04:05 +00:00
}
}
2025-04-09 08:15:37 +00:00
else if (extension == SvgExtension)
{
try
{
2025-04-09 08:24:09 +00:00
image = LoadSvgImage(path, loadFullImage);
2025-04-09 08:15:37 +00:00
type = ImageType.FullImageFile;
}
2025-04-09 08:28:53 +00:00
catch (System.Exception ex)
2025-04-09 08:15:37 +00:00
{
image = Image;
type = ImageType.Error;
2025-04-13 09:11:36 +00:00
Log.Exception(ClassName, $"Failed to load SVG image from path {path}: {ex.Message}", ex);
2025-04-09 08:15:37 +00:00
}
}
else
{
2020-05-02 11:12:34 +00:00
type = ImageType.File;
image = GetThumbnail(path, ThumbnailOptions.None, loadFullImage ? FullIconSize : SmallIconSize);
}
}
2020-05-02 11:12:34 +00:00
else
{
2025-08-13 06:01:04 +00:00
image = MissingImage;
2020-09-06 20:55:12 +00:00
path = Constant.MissingImgIcon;
2020-05-02 11:12:34 +00:00
}
if (type != ImageType.Error)
{
image.Freeze();
}
return new ImageResult(image, type);
}
private static BitmapSource GetThumbnail(string path, ThumbnailOptions option = ThumbnailOptions.ThumbnailOnly,
int size = SmallIconSize)
2020-05-02 11:12:34 +00:00
{
return WindowsThumbnailProvider.GetThumbnail(
path,
size,
size,
2020-05-02 11:12:34 +00:00
option);
}
2022-10-31 03:07:04 +00:00
public static bool CacheContainImage(string path, bool loadFullImage = false)
{
return ImageCache.ContainsKey(path, loadFullImage);
}
2022-12-29 03:43:40 +00:00
public static bool TryGetValue(string path, bool loadFullImage, out ImageSource image)
{
return ImageCache.TryGetValue(path, loadFullImage, out image);
}
2025-04-02 10:22:36 +00:00
public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullImage = false, bool cacheImage = true)
{
var imageResult = await LoadInternalAsync(path, loadFullImage);
var img = imageResult.ImageSource;
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
{
// we need to get image hash
string hash = EnableImageHash ? _hashGenerator.GetHashFromImage(img) : null;
if (hash != null)
2020-01-03 20:33:00 +00:00
{
if (GuidToKey.TryGetValue(hash, out string key))
{
// image already exists
img = ImageCache[key, loadFullImage] ?? img;
}
2025-04-02 10:22:36 +00:00
else if (cacheImage)
{
2025-04-02 10:22:36 +00:00
// save guid key
GuidToKey[hash] = path;
}
}
2025-04-02 10:22:36 +00:00
if (cacheImage)
{
// update cache
ImageCache[path, loadFullImage] = img;
}
}
2020-01-03 20:33:00 +00:00
return img;
2014-07-14 11:03:52 +00:00
}
2025-09-18 10:29:45 +00:00
private static BitmapImage LoadFullImage(string path)
{
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(path);
2022-10-19 05:39:27 +00:00
image.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
image.EndInit();
2025-04-09 08:15:37 +00:00
if (image.PixelWidth > FullImageSize)
{
BitmapImage resizedWidth = new BitmapImage();
resizedWidth.BeginInit();
resizedWidth.CacheOption = BitmapCacheOption.OnLoad;
resizedWidth.UriSource = new Uri(path);
resizedWidth.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
2025-04-09 08:15:37 +00:00
resizedWidth.DecodePixelWidth = FullImageSize;
resizedWidth.EndInit();
2025-04-09 08:15:37 +00:00
if (resizedWidth.PixelHeight > FullImageSize)
{
BitmapImage resizedHeight = new BitmapImage();
resizedHeight.BeginInit();
resizedHeight.CacheOption = BitmapCacheOption.OnLoad;
resizedHeight.UriSource = new Uri(path);
resizedHeight.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
2025-04-09 08:15:37 +00:00
resizedHeight.DecodePixelHeight = FullImageSize;
resizedHeight.EndInit();
return resizedHeight;
}
return resizedWidth;
}
return image;
}
2025-04-09 08:15:37 +00:00
2025-09-18 10:29:45 +00:00
private static RenderTargetBitmap LoadSvgImage(string path, bool loadFullImage = false)
2025-04-09 08:15:37 +00:00
{
// Set up drawing settings
var desiredHeight = loadFullImage ? FullImageSize : SmallIconSize;
var drawingSettings = new WpfDrawingSettings
{
IncludeRuntime = true,
// Set IgnoreRootViewbox to false to respect the SVG's viewBox
IgnoreRootViewbox = false
};
// Load and render the SVG
var converter = new FileSvgReader(drawingSettings);
2025-04-09 09:13:44 +00:00
var drawing = converter.Read(new Uri(path));
2025-04-09 08:15:37 +00:00
// Calculate scale to achieve desired height
var drawingBounds = drawing.Bounds;
if (drawingBounds.Height <= 0)
{
throw new InvalidOperationException($"Invalid SVG dimensions: Height must be greater than zero in {path}");
}
2025-04-09 08:15:37 +00:00
var scale = desiredHeight / drawingBounds.Height;
var scaledWidth = drawingBounds.Width * scale;
var scaledHeight = drawingBounds.Height * scale;
// Convert the Drawing to a Bitmap
var drawingVisual = new DrawingVisual();
2025-04-09 09:13:44 +00:00
using (DrawingContext drawingContext = drawingVisual.RenderOpen())
{
drawingContext.PushTransform(new ScaleTransform(scale, scale));
drawingContext.DrawDrawing(drawing);
}
2025-04-09 08:15:37 +00:00
// Create a RenderTargetBitmap to hold the rendered image
var bitmap = new RenderTargetBitmap(
(int)Math.Ceiling(scaledWidth),
(int)Math.Ceiling(scaledHeight),
96, // DpiX
96, // DpiY
PixelFormats.Pbgra32);
bitmap.Render(drawingVisual);
return bitmap;
}
}
2014-07-14 11:03:52 +00:00
}