Add blurhashes to ImageBlurHashes for all images

This commit is contained in:
Vasily 2020-05-27 16:27:27 +03:00
parent 6c9dc04189
commit 2482bcb3b1
2 changed files with 80 additions and 21 deletions

View File

@ -605,7 +605,7 @@ namespace Emby.Server.Implementations.Dto
if (dictionary.TryGetValue(person.Name, out Person entity)) if (dictionary.TryGetValue(person.Name, out Person entity))
{ {
baseItemPerson.PrimaryImageTag = GetImageCacheTag(entity, ImageType.Primary); baseItemPerson.PrimaryImageTag = GetTagAndFillBlurhash(dto, entity, ImageType.Primary);
baseItemPerson.Id = entity.Id.ToString("N", CultureInfo.InvariantCulture); baseItemPerson.Id = entity.Id.ToString("N", CultureInfo.InvariantCulture);
list.Add(baseItemPerson); list.Add(baseItemPerson);
} }
@ -654,6 +654,70 @@ namespace Emby.Server.Implementations.Dto
return _libraryManager.GetGenreId(name); return _libraryManager.GetGenreId(name);
} }
private string GetTagAndFillBlurhash(BaseItemDto dto, BaseItem item, ImageType imageType, int imageIndex = 0)
{
var image = item.GetImageInfo(imageType, imageIndex);
if (image != null)
{
return GetTagAndFillBlurhash(dto, item, image);
}
return null;
}
private string GetTagAndFillBlurhash(BaseItemDto dto, BaseItem item, ItemImageInfo image)
{
var tag = GetImageCacheTag(item, image);
if (!string.IsNullOrEmpty(image.BlurHash))
{
if (dto.ImageBlurHashes == null)
{
dto.ImageBlurHashes = new Dictionary<ImageType, Dictionary<string, string>>();
}
if (!dto.ImageBlurHashes.ContainsKey(image.Type))
{
dto.ImageBlurHashes[image.Type] = new Dictionary<string, string>();
}
dto.ImageBlurHashes[image.Type][tag] = image.BlurHash;
}
return tag;
}
private string[] GetTagsAndFillBlurhashes(BaseItemDto dto, BaseItem item, ImageType imageType, int limit)
{
return GetTagsAndFillBlurhashes(dto, item, imageType, item.GetImages(imageType).Take(limit).ToList());
}
private string[] GetTagsAndFillBlurhashes(BaseItemDto dto, BaseItem item, ImageType imageType, List<ItemImageInfo> images)
{
var tags = GetImageTags(item, images);
var hashes = new Dictionary<string, string>();
for (int i = 0; i < images.Count; i++)
{
var img = images[i];
if (!string.IsNullOrEmpty(img.BlurHash))
{
var tag = tags[i];
hashes[tag] = img.BlurHash;
}
}
if (hashes.Count > 0)
{
if (dto.ImageBlurHashes == null)
{
dto.ImageBlurHashes = new Dictionary<ImageType, Dictionary<string, string>>();
}
dto.ImageBlurHashes[imageType] = hashes;
}
return tags;
}
/// <summary> /// <summary>
/// Sets simple property values on a DTOBaseItem /// Sets simple property values on a DTOBaseItem
/// </summary> /// </summary>
@ -674,8 +738,8 @@ namespace Emby.Server.Implementations.Dto
dto.LockData = item.IsLocked; dto.LockData = item.IsLocked;
dto.ForcedSortName = item.ForcedSortName; dto.ForcedSortName = item.ForcedSortName;
} }
dto.Container = item.Container;
dto.Container = item.Container;
dto.EndDate = item.EndDate; dto.EndDate = item.EndDate;
if (options.ContainsField(ItemFields.ExternalUrls)) if (options.ContainsField(ItemFields.ExternalUrls))
@ -694,10 +758,12 @@ namespace Emby.Server.Implementations.Dto
dto.AspectRatio = hasAspectRatio.AspectRatio; dto.AspectRatio = hasAspectRatio.AspectRatio;
} }
dto.ImageBlurHashes = new Dictionary<ImageType, Dictionary<string, string>>();
var backdropLimit = options.GetImageLimit(ImageType.Backdrop); var backdropLimit = options.GetImageLimit(ImageType.Backdrop);
if (backdropLimit > 0) if (backdropLimit > 0)
{ {
dto.BackdropImageTags = GetImageTags(item, item.GetImages(ImageType.Backdrop).Take(backdropLimit).ToList()); dto.BackdropImageTags = GetTagsAndFillBlurhashes(dto, item, ImageType.Backdrop, backdropLimit);
} }
if (options.ContainsField(ItemFields.ScreenshotImageTags)) if (options.ContainsField(ItemFields.ScreenshotImageTags))
@ -705,7 +771,7 @@ namespace Emby.Server.Implementations.Dto
var screenshotLimit = options.GetImageLimit(ImageType.Screenshot); var screenshotLimit = options.GetImageLimit(ImageType.Screenshot);
if (screenshotLimit > 0) if (screenshotLimit > 0)
{ {
dto.ScreenshotImageTags = GetImageTags(item, item.GetImages(ImageType.Screenshot).Take(screenshotLimit).ToList()); dto.ScreenshotImageTags = GetTagsAndFillBlurhashes(dto, item, ImageType.Screenshot, screenshotLimit);
} }
} }
@ -718,7 +784,6 @@ namespace Emby.Server.Implementations.Dto
if (options.EnableImages) if (options.EnableImages)
{ {
dto.ImageTags = new Dictionary<ImageType, string>(); dto.ImageTags = new Dictionary<ImageType, string>();
dto.ImageBlurHashes = new Dictionary<ImageType, string>();
// Prevent implicitly captured closure // Prevent implicitly captured closure
var currentItem = item; var currentItem = item;
@ -726,18 +791,12 @@ namespace Emby.Server.Implementations.Dto
{ {
if (options.GetImageLimit(image.Type) > 0) if (options.GetImageLimit(image.Type) > 0)
{ {
var tag = GetImageCacheTag(item, image); var tag = GetTagAndFillBlurhash(dto, item, image);
if (tag != null) if (tag != null)
{ {
dto.ImageTags[image.Type] = tag; dto.ImageTags[image.Type] = tag;
} }
var hash = image.BlurHash;
if (!string.IsNullOrEmpty(hash))
{
dto.ImageBlurHashes[image.Type] = image.BlurHash;
}
} }
} }
} }
@ -877,8 +936,7 @@ namespace Emby.Server.Implementations.Dto
if (albumParent != null) if (albumParent != null)
{ {
dto.AlbumId = albumParent.Id; dto.AlbumId = albumParent.Id;
dto.AlbumPrimaryImageTag = GetTagAndFillBlurhash(dto, albumParent, ImageType.Primary);
dto.AlbumPrimaryImageTag = GetImageCacheTag(albumParent, ImageType.Primary);
} }
//if (options.ContainsField(ItemFields.MediaSourceCount)) //if (options.ContainsField(ItemFields.MediaSourceCount))
@ -1105,7 +1163,7 @@ namespace Emby.Server.Implementations.Dto
episodeSeries = episodeSeries ?? episode.Series; episodeSeries = episodeSeries ?? episode.Series;
if (episodeSeries != null) if (episodeSeries != null)
{ {
dto.SeriesPrimaryImageTag = GetImageCacheTag(episodeSeries, ImageType.Primary); dto.SeriesPrimaryImageTag = GetTagAndFillBlurhash(dto, episodeSeries, ImageType.Primary);
} }
} }
@ -1151,7 +1209,7 @@ namespace Emby.Server.Implementations.Dto
series = series ?? season.Series; series = series ?? season.Series;
if (series != null) if (series != null)
{ {
dto.SeriesPrimaryImageTag = GetImageCacheTag(series, ImageType.Primary); dto.SeriesPrimaryImageTag = GetTagAndFillBlurhash(dto, series, ImageType.Primary);
} }
} }
} }
@ -1281,7 +1339,7 @@ namespace Emby.Server.Implementations.Dto
if (image != null) if (image != null)
{ {
dto.ParentLogoItemId = GetDtoId(parent); dto.ParentLogoItemId = GetDtoId(parent);
dto.ParentLogoImageTag = GetImageCacheTag(parent, image); dto.ParentLogoImageTag = GetTagAndFillBlurhash(dto, parent, image);
} }
} }
if (artLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Art)) && dto.ParentArtItemId == null) if (artLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Art)) && dto.ParentArtItemId == null)
@ -1291,7 +1349,7 @@ namespace Emby.Server.Implementations.Dto
if (image != null) if (image != null)
{ {
dto.ParentArtItemId = GetDtoId(parent); dto.ParentArtItemId = GetDtoId(parent);
dto.ParentArtImageTag = GetImageCacheTag(parent, image); dto.ParentArtImageTag = GetTagAndFillBlurhash(dto, parent, image);
} }
} }
if (thumbLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Thumb)) && (dto.ParentThumbItemId == null || parent is Series) && !(parent is ICollectionFolder) && !(parent is UserView)) if (thumbLimit > 0 && !(imageTags != null && imageTags.ContainsKey(ImageType.Thumb)) && (dto.ParentThumbItemId == null || parent is Series) && !(parent is ICollectionFolder) && !(parent is UserView))
@ -1301,7 +1359,7 @@ namespace Emby.Server.Implementations.Dto
if (image != null) if (image != null)
{ {
dto.ParentThumbItemId = GetDtoId(parent); dto.ParentThumbItemId = GetDtoId(parent);
dto.ParentThumbImageTag = GetImageCacheTag(parent, image); dto.ParentThumbImageTag = GetTagAndFillBlurhash(dto, parent, image);
} }
} }
if (backdropLimit > 0 && !((dto.BackdropImageTags != null && dto.BackdropImageTags.Length > 0) || (dto.ParentBackdropImageTags != null && dto.ParentBackdropImageTags.Length > 0))) if (backdropLimit > 0 && !((dto.BackdropImageTags != null && dto.BackdropImageTags.Length > 0) || (dto.ParentBackdropImageTags != null && dto.ParentBackdropImageTags.Length > 0)))
@ -1311,7 +1369,7 @@ namespace Emby.Server.Implementations.Dto
if (images.Count > 0) if (images.Count > 0)
{ {
dto.ParentBackdropItemId = GetDtoId(parent); dto.ParentBackdropItemId = GetDtoId(parent);
dto.ParentBackdropImageTags = GetImageTags(parent, images); dto.ParentBackdropImageTags = GetTagsAndFillBlurhashes(dto, parent, ImageType.Backdrop, images);
} }
} }

View File

@ -512,9 +512,10 @@ namespace MediaBrowser.Model.Dto
/// <summary> /// <summary>
/// Gets or sets the blurhashes for the image tags. /// Gets or sets the blurhashes for the image tags.
/// Maps image type to dictionary mapping image tag to blurhash value.
/// </summary> /// </summary>
/// <value>The blurhashes.</value> /// <value>The blurhashes.</value>
public Dictionary<ImageType, string> ImageBlurHashes { get; set; } public Dictionary<ImageType, Dictionary<string, string>> ImageBlurHashes { get; set; }
/// <summary> /// <summary>
/// Gets or sets the series studio. /// Gets or sets the series studio.