mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-01-18 14:41:21 +08:00
Merge pull request #115 from theClueless/imageOptimization
Imageoptimization
This commit is contained in:
commit
4cf3cff74d
@ -39,6 +39,19 @@ namespace Wox.Infrastructure.Image
|
|||||||
var contains = _data.ContainsKey(key);
|
var contains = _data.ContainsKey(key);
|
||||||
return contains;
|
return contains;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int CacheSize()
|
||||||
|
{
|
||||||
|
return _data.Count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// return the number of unique images in the cache (by reference not by checking images content)
|
||||||
|
/// </summary>
|
||||||
|
public int UniqueImagesInCache()
|
||||||
|
{
|
||||||
|
return _data.Values.Distinct().Count();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
49
Wox.Infrastructure/Image/ImageHashGenerator.cs
Normal file
49
Wox.Infrastructure/Image/ImageHashGenerator.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
|
||||||
|
namespace Wox.Infrastructure.Image
|
||||||
|
{
|
||||||
|
public interface IImageHashGenerator
|
||||||
|
{
|
||||||
|
string GetHashFromImage(ImageSource image);
|
||||||
|
}
|
||||||
|
public class ImageHashGenerator : IImageHashGenerator
|
||||||
|
{
|
||||||
|
public string GetHashFromImage(ImageSource imageSource)
|
||||||
|
{
|
||||||
|
if (!(imageSource is BitmapSource image))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var outStream = new MemoryStream())
|
||||||
|
{
|
||||||
|
// PngBitmapEncoder enc2 = new PngBitmapEncoder();
|
||||||
|
// enc2.Frames.Add(BitmapFrame.Create(tt));
|
||||||
|
|
||||||
|
var enc = new JpegBitmapEncoder();
|
||||||
|
var bitmapFrame = BitmapFrame.Create(image);
|
||||||
|
bitmapFrame.Freeze();
|
||||||
|
enc.Frames.Add(bitmapFrame);
|
||||||
|
enc.Save(outStream);
|
||||||
|
var byteArray = outStream.GetBuffer();
|
||||||
|
using (var sha1 = new SHA1CryptoServiceProvider())
|
||||||
|
{
|
||||||
|
var hash = Convert.ToBase64String(sha1.ComputeHash(byteArray));
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,8 @@ namespace Wox.Infrastructure.Image
|
|||||||
{
|
{
|
||||||
private static readonly ImageCache ImageCache = new ImageCache();
|
private static readonly ImageCache ImageCache = new ImageCache();
|
||||||
private static BinaryStorage<ConcurrentDictionary<string, int>> _storage;
|
private static BinaryStorage<ConcurrentDictionary<string, int>> _storage;
|
||||||
|
private static readonly ConcurrentDictionary<string, string> GuidToKey = new ConcurrentDictionary<string, string>();
|
||||||
|
private static IImageHashGenerator _hashGenerator;
|
||||||
|
|
||||||
|
|
||||||
private static readonly string[] ImageExtensions =
|
private static readonly string[] ImageExtensions =
|
||||||
@ -30,7 +32,8 @@ namespace Wox.Infrastructure.Image
|
|||||||
|
|
||||||
public static void Initialize()
|
public static void Initialize()
|
||||||
{
|
{
|
||||||
_storage = new BinaryStorage<ConcurrentDictionary<string, int>> ("Image");
|
_storage = new BinaryStorage<ConcurrentDictionary<string, int>>("Image");
|
||||||
|
_hashGenerator = new ImageHashGenerator();
|
||||||
ImageCache.Usage = _storage.TryLoad(new ConcurrentDictionary<string, int>());
|
ImageCache.Usage = _storage.TryLoad(new ConcurrentDictionary<string, int>());
|
||||||
|
|
||||||
foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon })
|
foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon })
|
||||||
@ -43,16 +46,12 @@ namespace Wox.Infrastructure.Image
|
|||||||
{
|
{
|
||||||
Stopwatch.Normal("|ImageLoader.Initialize|Preload images cost", () =>
|
Stopwatch.Normal("|ImageLoader.Initialize|Preload images cost", () =>
|
||||||
{
|
{
|
||||||
ImageCache.Usage.AsParallel().Where(i => !ImageCache.ContainsKey(i.Key)).ForAll(i =>
|
ImageCache.Usage.AsParallel().ForAll(x =>
|
||||||
{
|
{
|
||||||
var img = Load(i.Key);
|
Load(x.Key);
|
||||||
if (img != null)
|
|
||||||
{
|
|
||||||
ImageCache[i.Key] = img;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Log.Info($"|ImageLoader.Initialize|Number of preload images is <{ImageCache.Usage.Count}>");
|
Log.Info($"|ImageLoader.Initialize|Number of preload images is <{ImageCache.Usage.Count}>, Images Number: {ImageCache.CacheSize()}, Unique Items {ImageCache.UniqueImagesInCache()}");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,31 +60,56 @@ namespace Wox.Infrastructure.Image
|
|||||||
ImageCache.Cleanup();
|
ImageCache.Cleanup();
|
||||||
_storage.Save(ImageCache.Usage);
|
_storage.Save(ImageCache.Usage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ImageSource Load(string path, bool loadFullImage = false)
|
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)
|
||||||
{
|
{
|
||||||
ImageSource image;
|
ImageSource image;
|
||||||
|
ImageType type = ImageType.Error;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(path))
|
if (string.IsNullOrEmpty(path))
|
||||||
{
|
{
|
||||||
return ImageCache[Constant.ErrorIcon];
|
return new ImageResult(ImageCache[Constant.ErrorIcon], ImageType.Error);
|
||||||
}
|
}
|
||||||
if (ImageCache.ContainsKey(path))
|
if (ImageCache.ContainsKey(path))
|
||||||
{
|
{
|
||||||
return ImageCache[path];
|
return new ImageResult(ImageCache[path], ImageType.Cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
|
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
return new BitmapImage(new Uri(path));
|
var imageSource = new BitmapImage(new Uri(path));
|
||||||
|
imageSource.Freeze();
|
||||||
|
return new ImageResult(imageSource, ImageType.Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Path.IsPathRooted(path))
|
if (!Path.IsPathRooted(path))
|
||||||
{
|
{
|
||||||
path = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
|
path = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Directory.Exists(path))
|
if (Directory.Exists(path))
|
||||||
{
|
{
|
||||||
/* Directories can also have thumbnails instead of shell icons.
|
/* Directories can also have thumbnails instead of shell icons.
|
||||||
@ -94,14 +118,17 @@ namespace Wox.Infrastructure.Image
|
|||||||
* Wox responsibility.
|
* Wox responsibility.
|
||||||
* - Solution: just load the icon
|
* - Solution: just load the icon
|
||||||
*/
|
*/
|
||||||
|
type = ImageType.Folder;
|
||||||
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
|
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
|
||||||
Constant.ThumbnailSize, ThumbnailOptions.IconOnly);
|
Constant.ThumbnailSize, ThumbnailOptions.IconOnly);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (File.Exists(path))
|
else if (File.Exists(path))
|
||||||
{
|
{
|
||||||
var extension = Path.GetExtension(path).ToLower();
|
var extension = Path.GetExtension(path).ToLower();
|
||||||
if (ImageExtensions.Contains(extension))
|
if (ImageExtensions.Contains(extension))
|
||||||
{
|
{
|
||||||
|
type = ImageType.ImageFile;
|
||||||
if (loadFullImage)
|
if (loadFullImage)
|
||||||
{
|
{
|
||||||
image = LoadFullImage(path);
|
image = LoadFullImage(path);
|
||||||
@ -119,6 +146,7 @@ namespace Wox.Infrastructure.Image
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
type = ImageType.File;
|
||||||
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
|
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize,
|
||||||
Constant.ThumbnailSize, ThumbnailOptions.None);
|
Constant.ThumbnailSize, ThumbnailOptions.None);
|
||||||
}
|
}
|
||||||
@ -128,17 +156,50 @@ namespace Wox.Infrastructure.Image
|
|||||||
image = ImageCache[Constant.ErrorIcon];
|
image = ImageCache[Constant.ErrorIcon];
|
||||||
path = Constant.ErrorIcon;
|
path = Constant.ErrorIcon;
|
||||||
}
|
}
|
||||||
ImageCache[path] = image;
|
|
||||||
image.Freeze();
|
if (type != ImageType.Error)
|
||||||
|
{
|
||||||
|
image.Freeze();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (System.Exception e)
|
catch (System.Exception e)
|
||||||
{
|
{
|
||||||
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e);
|
Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e);
|
||||||
|
type = ImageType.Error;
|
||||||
image = ImageCache[Constant.ErrorIcon];
|
image = ImageCache[Constant.ErrorIcon];
|
||||||
ImageCache[path] = image;
|
ImageCache[path] = image;
|
||||||
}
|
}
|
||||||
return image;
|
return new ImageResult(image, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool EnableImageHash = true;
|
||||||
|
|
||||||
|
public static ImageSource Load(string path, bool loadFullImage = false)
|
||||||
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BitmapImage LoadFullImage(string path)
|
private static BitmapImage LoadFullImage(string path)
|
||||||
|
@ -110,7 +110,6 @@ namespace Wox.Infrastructure.Image
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
return Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
@ -71,6 +71,7 @@
|
|||||||
<Compile Include="Hotkey\InterceptKeys.cs" />
|
<Compile Include="Hotkey\InterceptKeys.cs" />
|
||||||
<Compile Include="Hotkey\KeyEvent.cs" />
|
<Compile Include="Hotkey\KeyEvent.cs" />
|
||||||
<Compile Include="Image\ImageCache.cs" />
|
<Compile Include="Image\ImageCache.cs" />
|
||||||
|
<Compile Include="Image\ImageHashGenerator.cs" />
|
||||||
<Compile Include="Image\ImageLoader.cs" />
|
<Compile Include="Image\ImageLoader.cs" />
|
||||||
<Compile Include="Image\ThumbnailReader.cs" />
|
<Compile Include="Image\ThumbnailReader.cs" />
|
||||||
<Compile Include="Logger\Log.cs" />
|
<Compile Include="Logger\Log.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user