2014-07-14 19:03:52 +08:00
|
|
|
|
using System;
|
2016-04-22 06:37:40 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
using System.IO;
|
2016-04-26 08:20:10 +08:00
|
|
|
|
using System.Linq;
|
2016-05-04 04:18:26 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
2016-05-03 05:37:01 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
using Wox.Infrastructure.Storage;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
|
2016-04-26 08:20:10 +08:00
|
|
|
|
namespace Wox.Infrastructure.Image
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
public static class ImageLoader
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
2017-01-13 23:40:32 +08:00
|
|
|
|
private static readonly ImageCache ImageCache = new ImageCache();
|
2017-02-13 00:57:24 +08:00
|
|
|
|
private static BinaryStorage<ConcurrentDictionary<string, int>> _storage;
|
2020-01-04 03:16:17 +08:00
|
|
|
|
private static readonly ConcurrentDictionary<string, string> GuidToKey = new ConcurrentDictionary<string, string>();
|
|
|
|
|
private static IImageHashGenerator _hashGenerator;
|
2016-08-20 08:02:47 +08:00
|
|
|
|
|
2014-07-14 19:03:52 +08:00
|
|
|
|
|
2018-03-31 15:19:55 +08:00
|
|
|
|
private static readonly string[] ImageExtensions =
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
|
|
|
|
".png",
|
|
|
|
|
".jpg",
|
|
|
|
|
".jpeg",
|
|
|
|
|
".gif",
|
|
|
|
|
".bmp",
|
|
|
|
|
".tiff",
|
|
|
|
|
".ico"
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-28 10:09:57 +08:00
|
|
|
|
|
2017-02-13 00:57:24 +08:00
|
|
|
|
public static void Initialize()
|
2016-03-28 10:09:57 +08:00
|
|
|
|
{
|
2020-01-04 03:16:17 +08:00
|
|
|
|
_storage = new BinaryStorage<ConcurrentDictionary<string, int>>("Image");
|
|
|
|
|
_hashGenerator = new ImageHashGenerator();
|
2017-02-13 00:57:24 +08:00
|
|
|
|
ImageCache.Usage = _storage.TryLoad(new ConcurrentDictionary<string, int>());
|
|
|
|
|
|
|
|
|
|
foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon })
|
|
|
|
|
{
|
|
|
|
|
ImageSource img = new BitmapImage(new Uri(icon));
|
|
|
|
|
img.Freeze();
|
|
|
|
|
ImageCache[icon] = img;
|
|
|
|
|
}
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
Stopwatch.Normal("|ImageLoader.Initialize|Preload images cost", () =>
|
|
|
|
|
{
|
2020-01-04 03:16:17 +08:00
|
|
|
|
ImageCache.Usage.AsParallel().Where(i => !ImageCache.ContainsKey(i.Key)).ForAll(x =>
|
2017-02-13 00:57:24 +08:00
|
|
|
|
{
|
2020-01-04 03:16:17 +08:00
|
|
|
|
Load(x.Key);
|
2017-02-13 00:57:24 +08:00
|
|
|
|
});
|
|
|
|
|
});
|
2020-01-04 03:16:17 +08:00
|
|
|
|
Log.Info($"|ImageLoader.Initialize|Number of preload images is <{ImageCache.Usage.Count}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
|
2017-02-13 00:57:24 +08:00
|
|
|
|
});
|
2016-04-21 08:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
public static void Save()
|
2016-04-21 08:53:21 +08:00
|
|
|
|
{
|
2017-01-13 23:40:32 +08:00
|
|
|
|
ImageCache.Cleanup();
|
2017-02-13 00:57:24 +08:00
|
|
|
|
_storage.Save(ImageCache.Usage);
|
2016-03-28 10:09:57 +08:00
|
|
|
|
}
|
2020-01-04 03:16:17 +08:00
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
Error,
|
|
|
|
|
Cache
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ImageResult LoadInternal(string path, bool loadFullImage = false)
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
2018-03-31 15:19:55 +08:00
|
|
|
|
ImageSource image;
|
2020-01-04 03:16:17 +08:00
|
|
|
|
ImageType type = ImageType.Error;
|
2015-01-15 20:47:48 +08:00
|
|
|
|
try
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
2018-03-31 15:19:55 +08:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
2015-01-15 20:47:48 +08:00
|
|
|
|
{
|
2020-01-04 03:16:17 +08:00
|
|
|
|
return new ImageResult(ImageCache[Constant.ErrorIcon], ImageType.Error);
|
2016-05-04 06:21:03 +08:00
|
|
|
|
}
|
2018-03-31 15:19:55 +08:00
|
|
|
|
if (ImageCache.ContainsKey(path))
|
2016-05-04 06:21:03 +08:00
|
|
|
|
{
|
2020-01-04 03:16:17 +08:00
|
|
|
|
return new ImageResult(ImageCache[path], ImageType.Cache);
|
2015-01-15 20:47:48 +08:00
|
|
|
|
}
|
2020-01-04 03:16:17 +08:00
|
|
|
|
|
2016-05-04 04:18:26 +08:00
|
|
|
|
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
|
2015-02-04 23:16:41 +08:00
|
|
|
|
{
|
2020-01-04 03:16:17 +08:00
|
|
|
|
return new ImageResult(new BitmapImage(new Uri(path)), ImageType.Data);
|
2015-02-04 23:16:41 +08:00
|
|
|
|
}
|
2018-03-31 15:19:55 +08:00
|
|
|
|
|
|
|
|
|
if (!Path.IsPathRooted(path))
|
2015-02-04 23:16:41 +08:00
|
|
|
|
{
|
2018-03-31 15:19:55 +08:00
|
|
|
|
path = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
|
|
|
|
|
}
|
2020-01-04 03:16:17 +08:00
|
|
|
|
|
2018-03-31 15:19:55 +08:00
|
|
|
|
if (Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
/* Directories can also have thumbnails instead of shell icons.
|
|
|
|
|
* Generating thumbnails for a bunch of folders while scrolling through
|
|
|
|
|
* results from Everything makes a big impact on performance and
|
|
|
|
|
* Wox responsibility.
|
|
|
|
|
* - Solution: just load the icon
|
|
|
|
|
*/
|
2020-01-04 03:16:17 +08:00
|
|
|
|
type = ImageType.Folder;
|
2018-03-31 15:19:55 +08:00
|
|
|
|
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
|
|
|
|
|
Constant.ThumbnailSize, ThumbnailOptions.IconOnly);
|
2020-01-04 03:16:17 +08:00
|
|
|
|
|
2018-03-31 15:19:55 +08:00
|
|
|
|
}
|
|
|
|
|
else if (File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
var extension = Path.GetExtension(path).ToLower();
|
|
|
|
|
if (ImageExtensions.Contains(extension))
|
2015-11-02 08:04:05 +08:00
|
|
|
|
{
|
2020-01-04 03:16:17 +08:00
|
|
|
|
type = ImageType.ImageFile;
|
2018-03-31 15:19:55 +08:00
|
|
|
|
if (loadFullImage)
|
2016-05-04 06:21:03 +08:00
|
|
|
|
{
|
2018-03-31 15:19:55 +08:00
|
|
|
|
image = LoadFullImage(path);
|
2016-05-04 06:21:03 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-03-31 15:19:55 +08:00
|
|
|
|
/* Although the documentation for GetImage on MSDN indicates that
|
|
|
|
|
* if a thumbnail is available it will return one, this has proved to not
|
|
|
|
|
* be the case in many situations while testing.
|
|
|
|
|
* - Solution: explicitly pass the ThumbnailOnly flag
|
|
|
|
|
*/
|
|
|
|
|
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
|
|
|
|
|
Constant.ThumbnailSize, ThumbnailOptions.ThumbnailOnly);
|
2016-05-04 06:21:03 +08:00
|
|
|
|
}
|
2015-11-02 08:04:05 +08:00
|
|
|
|
}
|
2016-05-04 04:18:26 +08:00
|
|
|
|
else
|
2015-11-02 08:04:05 +08:00
|
|
|
|
{
|
2020-01-04 03:16:17 +08:00
|
|
|
|
type = ImageType.File;
|
2018-03-31 15:19:55 +08:00
|
|
|
|
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
|
|
|
|
|
Constant.ThumbnailSize, ThumbnailOptions.None);
|
2015-11-02 08:04:05 +08:00
|
|
|
|
}
|
2016-05-04 04:18:26 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-03-31 15:19:55 +08:00
|
|
|
|
image = ImageCache[Constant.ErrorIcon];
|
|
|
|
|
path = Constant.ErrorIcon;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
}
|
2020-01-04 03:16:17 +08:00
|
|
|
|
|
|
|
|
|
if (type != ImageType.Error)
|
|
|
|
|
{
|
|
|
|
|
image.Freeze();
|
|
|
|
|
}
|
2016-05-04 04:18:26 +08:00
|
|
|
|
}
|
2018-03-31 15:19:55 +08:00
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e);
|
2020-01-04 03:16:17 +08:00
|
|
|
|
type = ImageType.Error;
|
2018-03-31 15:19:55 +08:00
|
|
|
|
image = ImageCache[Constant.ErrorIcon];
|
|
|
|
|
ImageCache[path] = image;
|
|
|
|
|
}
|
2020-01-04 03:16:17 +08:00
|
|
|
|
return new ImageResult(image, type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool EnableImageHash = true;
|
|
|
|
|
|
|
|
|
|
public static ImageSource Load(string path, bool loadFullImage = false)
|
|
|
|
|
{
|
|
|
|
|
// return LoadInternal(path, loadFullImage).ImageSource;
|
|
|
|
|
var imageResult = LoadInternal(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)
|
|
|
|
|
{
|
|
|
|
|
if (GuidToKey.TryGetValue(hash, out string key))
|
|
|
|
|
{ // image already exists
|
|
|
|
|
img = ImageCache[key];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{ // new guid
|
|
|
|
|
GuidToKey[hash] = path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update cache
|
|
|
|
|
ImageCache[path] = img;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return img;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-31 15:19:55 +08:00
|
|
|
|
private static BitmapImage LoadFullImage(string path)
|
2016-08-20 20:10:33 +08:00
|
|
|
|
{
|
2018-03-31 15:19:55 +08:00
|
|
|
|
BitmapImage image = new BitmapImage();
|
|
|
|
|
image.BeginInit();
|
|
|
|
|
image.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
|
|
image.UriSource = new Uri(path);
|
|
|
|
|
image.EndInit();
|
|
|
|
|
return image;
|
2016-08-20 20:10:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-14 19:03:52 +08:00
|
|
|
|
}
|