From b9fc0d26287e46017515e4ac3e569ca2c60f622f Mon Sep 17 00:00:00 2001 From: Jesús Higueras Date: Mon, 23 Mar 2020 20:05:49 +0100 Subject: Add BlurHash support to backend --- MediaBrowser.Controller/Drawing/IImageEncoder.cs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'MediaBrowser.Controller/Drawing/IImageEncoder.cs') diff --git a/MediaBrowser.Controller/Drawing/IImageEncoder.cs b/MediaBrowser.Controller/Drawing/IImageEncoder.cs index 88e67b648..1d3f0d3b4 100644 --- a/MediaBrowser.Controller/Drawing/IImageEncoder.cs +++ b/MediaBrowser.Controller/Drawing/IImageEncoder.cs @@ -43,6 +43,13 @@ namespace MediaBrowser.Controller.Drawing /// The image dimensions. ImageDimensions GetImageSize(string path); + /// + /// Get the blurhash of an image. + /// + /// The filepath of the image. + /// The blurhash. + string GetImageHash(string path); + /// /// Encode an image. /// -- cgit v1.2.3 From 29443e36817e4866cc58e8397c1233b05624284a Mon Sep 17 00:00:00 2001 From: Vasily Date: Tue, 26 May 2020 00:50:29 +0300 Subject: Apply suggestions from code review Co-authored-by: dkanada --- .vscode/tasks.json | 1 - Emby.Server.Implementations/Library/LibraryManager.cs | 1 - MediaBrowser.Controller/Drawing/IImageEncoder.cs | 2 +- MediaBrowser.Model/Dto/BaseItemDto.cs | 2 +- 4 files changed, 2 insertions(+), 4 deletions(-) (limited to 'MediaBrowser.Controller/Drawing/IImageEncoder.cs') diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 7475617c9..2289fd991 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -21,6 +21,5 @@ ], "problemMatcher": "$msCompile" } - ] } diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index 579fb7edd..e63776bff 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -71,7 +71,6 @@ namespace Emby.Server.Implementations.Library private readonly ConcurrentDictionary _libraryItemsCache; private readonly IImageProcessor _imageProcessor; - private NamingOptions _namingOptions; private string[] _videoFileExtensions; diff --git a/MediaBrowser.Controller/Drawing/IImageEncoder.cs b/MediaBrowser.Controller/Drawing/IImageEncoder.cs index 1d3f0d3b4..4baec6204 100644 --- a/MediaBrowser.Controller/Drawing/IImageEncoder.cs +++ b/MediaBrowser.Controller/Drawing/IImageEncoder.cs @@ -44,7 +44,7 @@ namespace MediaBrowser.Controller.Drawing ImageDimensions GetImageSize(string path); /// - /// Get the blurhash of an image. + /// Gets the blurhash of an image. /// /// The filepath of the image. /// The blurhash. diff --git a/MediaBrowser.Model/Dto/BaseItemDto.cs b/MediaBrowser.Model/Dto/BaseItemDto.cs index df84dcf12..6213206ff 100644 --- a/MediaBrowser.Model/Dto/BaseItemDto.cs +++ b/MediaBrowser.Model/Dto/BaseItemDto.cs @@ -511,7 +511,7 @@ namespace MediaBrowser.Model.Dto public string SeriesThumbImageTag { get; set; } /// - /// Gets or sets the blurhash for the image tags. + /// Gets or sets the blurhashes for the image tags. /// /// The blurhashes. public Dictionary ImageBlurHashes { get; set; } -- cgit v1.2.3 From 26eef1bbf823e6f9fc22b11d95a17b1370b21842 Mon Sep 17 00:00:00 2001 From: Vasily Date: Mon, 1 Jun 2020 18:12:49 +0300 Subject: Move logic of computing Blurhash components to ImageProcessor Also rename last few instances of GetImageHash to GetImageBlurHash for clarity --- Emby.Drawing/ImageProcessor.cs | 19 ++++++++++++++++++- Emby.Drawing/NullImageEncoder.cs | 2 +- Emby.Server.Implementations/Library/LibraryManager.cs | 1 + Jellyfin.Drawing.Skia/SkiaEncoder.cs | 18 +----------------- MediaBrowser.Controller/Drawing/IImageEncoder.cs | 4 +++- 5 files changed, 24 insertions(+), 20 deletions(-) (limited to 'MediaBrowser.Controller/Drawing/IImageEncoder.cs') diff --git a/Emby.Drawing/ImageProcessor.cs b/Emby.Drawing/ImageProcessor.cs index 35da6f635..89bb3068b 100644 --- a/Emby.Drawing/ImageProcessor.cs +++ b/Emby.Drawing/ImageProcessor.cs @@ -315,7 +315,24 @@ namespace Emby.Drawing /// public string GetImageBlurHash(string path) - => _imageEncoder.GetImageHash(path); + { + var size = GetImageDimensions(path); + if (size.Width <= 0 || size.Height <= 0) + { + return string.Empty; + } + + // We want tiles to be as close to square as possible, and to *mostly* keep under 16 tiles for performance. + // One tile is (width / xComp) x (height / yComp) pixels, which means that ideally yComp = xComp * height / width. + // See more at https://github.com/woltapp/blurhash/#how-do-i-pick-the-number-of-x-and-y-components + float xCompF = MathF.Sqrt(16.0f * size.Width / size.Height); + float yCompF = xCompF * size.Height / size.Width; + + int xComp = Math.Min((int)xCompF + 1, 9); + int yComp = Math.Min((int)yCompF + 1, 9); + + return _imageEncoder.GetImageBlurHash(xComp, yComp, path); + } /// public string GetImageCacheTag(BaseItem item, ItemImageInfo image) diff --git a/Emby.Drawing/NullImageEncoder.cs b/Emby.Drawing/NullImageEncoder.cs index 54de7212a..bbb5c1716 100644 --- a/Emby.Drawing/NullImageEncoder.cs +++ b/Emby.Drawing/NullImageEncoder.cs @@ -44,7 +44,7 @@ namespace Emby.Drawing } /// - public string GetImageHash(string path) + public string GetImageBlurHash(int xComp, int yComp, string path) { throw new NotImplementedException(); } diff --git a/Emby.Server.Implementations/Library/LibraryManager.cs b/Emby.Server.Implementations/Library/LibraryManager.cs index bb3e3dd11..15362182c 100644 --- a/Emby.Server.Implementations/Library/LibraryManager.cs +++ b/Emby.Server.Implementations/Library/LibraryManager.cs @@ -1882,6 +1882,7 @@ namespace Emby.Server.Implementations.Library ImageDimensions size = _imageProcessor.GetImageDimensions(item, image); image.Width = size.Width; image.Height = size.Height; + try { image.BlurHash = _imageProcessor.GetImageBlurHash(image.Path); diff --git a/Jellyfin.Drawing.Skia/SkiaEncoder.cs b/Jellyfin.Drawing.Skia/SkiaEncoder.cs index 7f0da2c9e..dae0e94d4 100644 --- a/Jellyfin.Drawing.Skia/SkiaEncoder.cs +++ b/Jellyfin.Drawing.Skia/SkiaEncoder.cs @@ -234,29 +234,13 @@ namespace Jellyfin.Drawing.Skia /// The path is null. /// The path is not valid. /// The file at the specified path could not be used to generate a codec. - public string GetImageHash(string path) + public string GetImageBlurHash(int xComp, int yComp, string path) { if (path == null) { throw new ArgumentNullException(nameof(path)); } - var dims = GetImageSize(path); - if (dims.Width <= 0 || dims.Height <= 0) - { - // empty image does not have any blurhash - return string.Empty; - } - - // We want tiles to be as close to square as possible, and to *mostly* keep under 16 tiles for performance. - // One tile is (width / xComp) x (height / yComp) pixels, which means that ideally yComp = xComp * height / width. - // See more at https://github.com/woltapp/blurhash/#how-do-i-pick-the-number-of-x-and-y-components - float xCompF = MathF.Sqrt(16.0f * dims.Width / dims.Height); - float yCompF = xCompF * dims.Height / dims.Width; - - int xComp = Math.Min((int)xCompF + 1, 9); - int yComp = Math.Min((int)yCompF + 1, 9); - return BlurHashEncoder.Encode(xComp, yComp, path); } diff --git a/MediaBrowser.Controller/Drawing/IImageEncoder.cs b/MediaBrowser.Controller/Drawing/IImageEncoder.cs index 4baec6204..e09ccd204 100644 --- a/MediaBrowser.Controller/Drawing/IImageEncoder.cs +++ b/MediaBrowser.Controller/Drawing/IImageEncoder.cs @@ -46,9 +46,11 @@ namespace MediaBrowser.Controller.Drawing /// /// Gets the blurhash of an image. /// + /// Amount of X components of DCT to take. + /// Amount of Y components of DCT to take. /// The filepath of the image. /// The blurhash. - string GetImageHash(string path); + string GetImageBlurHash(int xComp, int yComp, string path); /// /// Encode an image. -- cgit v1.2.3