ImageProcessor.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Mime;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Jellyfin.Data.Entities;
  11. using MediaBrowser.Common.Extensions;
  12. using MediaBrowser.Controller;
  13. using MediaBrowser.Controller.Configuration;
  14. using MediaBrowser.Controller.Drawing;
  15. using MediaBrowser.Controller.Entities;
  16. using MediaBrowser.Model.Drawing;
  17. using MediaBrowser.Model.Entities;
  18. using MediaBrowser.Model.IO;
  19. using MediaBrowser.Model.Net;
  20. using Microsoft.Extensions.Logging;
  21. using Photo = MediaBrowser.Controller.Entities.Photo;
  22. namespace Jellyfin.Drawing;
  23. /// <summary>
  24. /// Class ImageProcessor.
  25. /// </summary>
  26. public sealed class ImageProcessor : IImageProcessor, IDisposable
  27. {
  28. // Increment this when there's a change requiring caches to be invalidated
  29. private const char Version = '3';
  30. private static readonly HashSet<string> _transparentImageTypes
  31. = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { ".png", ".webp", ".gif" };
  32. private readonly ILogger<ImageProcessor> _logger;
  33. private readonly IFileSystem _fileSystem;
  34. private readonly IServerApplicationPaths _appPaths;
  35. private readonly IImageEncoder _imageEncoder;
  36. private readonly SemaphoreSlim _parallelEncodingLimit;
  37. private bool _disposed;
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="ImageProcessor"/> class.
  40. /// </summary>
  41. /// <param name="logger">The logger.</param>
  42. /// <param name="appPaths">The server application paths.</param>
  43. /// <param name="fileSystem">The filesystem.</param>
  44. /// <param name="imageEncoder">The image encoder.</param>
  45. /// <param name="config">The configuration.</param>
  46. public ImageProcessor(
  47. ILogger<ImageProcessor> logger,
  48. IServerApplicationPaths appPaths,
  49. IFileSystem fileSystem,
  50. IImageEncoder imageEncoder,
  51. IServerConfigurationManager config)
  52. {
  53. _logger = logger;
  54. _fileSystem = fileSystem;
  55. _imageEncoder = imageEncoder;
  56. _appPaths = appPaths;
  57. var semaphoreCount = config.Configuration.ParallelImageEncodingLimit;
  58. if (semaphoreCount < 1)
  59. {
  60. semaphoreCount = 2 * Environment.ProcessorCount;
  61. }
  62. _parallelEncodingLimit = new(semaphoreCount, semaphoreCount);
  63. }
  64. private string ResizedImageCachePath => Path.Combine(_appPaths.ImageCachePath, "resized-images");
  65. /// <inheritdoc />
  66. public IReadOnlyCollection<string> SupportedInputFormats =>
  67. new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  68. {
  69. "tiff",
  70. "tif",
  71. "jpeg",
  72. "jpg",
  73. "png",
  74. "aiff",
  75. "cr2",
  76. "crw",
  77. "nef",
  78. "orf",
  79. "pef",
  80. "arw",
  81. "webp",
  82. "gif",
  83. "bmp",
  84. "erf",
  85. "raf",
  86. "rw2",
  87. "nrw",
  88. "dng",
  89. "ico",
  90. "astc",
  91. "ktx",
  92. "pkm",
  93. "wbmp"
  94. };
  95. /// <inheritdoc />
  96. public bool SupportsImageCollageCreation => _imageEncoder.SupportsImageCollageCreation;
  97. /// <inheritdoc />
  98. public IReadOnlyCollection<ImageFormat> GetSupportedImageOutputFormats()
  99. => _imageEncoder.SupportedOutputFormats;
  100. /// <inheritdoc />
  101. public async Task<(string Path, string? MimeType, DateTime DateModified)> ProcessImage(ImageProcessingOptions options)
  102. {
  103. ItemImageInfo originalImage = options.Image;
  104. BaseItem item = options.Item;
  105. string originalImagePath = originalImage.Path;
  106. DateTime dateModified = originalImage.DateModified;
  107. ImageDimensions? originalImageSize = null;
  108. if (originalImage.Width > 0 && originalImage.Height > 0)
  109. {
  110. originalImageSize = new ImageDimensions(originalImage.Width, originalImage.Height);
  111. }
  112. var mimeType = MimeTypes.GetMimeType(originalImagePath);
  113. if (!_imageEncoder.SupportsImageEncoding)
  114. {
  115. return (originalImagePath, mimeType, dateModified);
  116. }
  117. var supportedImageInfo = await GetSupportedImage(originalImagePath, dateModified).ConfigureAwait(false);
  118. originalImagePath = supportedImageInfo.Path;
  119. // Original file doesn't exist, or original file is gif.
  120. if (!File.Exists(originalImagePath) || string.Equals(mimeType, MediaTypeNames.Image.Gif, StringComparison.OrdinalIgnoreCase))
  121. {
  122. return (originalImagePath, mimeType, dateModified);
  123. }
  124. dateModified = supportedImageInfo.DateModified;
  125. bool requiresTransparency = _transparentImageTypes.Contains(Path.GetExtension(originalImagePath));
  126. bool autoOrient = false;
  127. ImageOrientation? orientation = null;
  128. if (item is Photo photo)
  129. {
  130. if (photo.Orientation.HasValue)
  131. {
  132. if (photo.Orientation.Value != ImageOrientation.TopLeft)
  133. {
  134. autoOrient = true;
  135. orientation = photo.Orientation;
  136. }
  137. }
  138. else
  139. {
  140. // Orientation unknown, so do it
  141. autoOrient = true;
  142. orientation = photo.Orientation;
  143. }
  144. }
  145. if (options.HasDefaultOptions(originalImagePath, originalImageSize) && (!autoOrient || !options.RequiresAutoOrientation))
  146. {
  147. // Just spit out the original file if all the options are default
  148. return (originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  149. }
  150. int quality = options.Quality;
  151. ImageFormat outputFormat = GetOutputFormat(options.SupportedOutputFormats, requiresTransparency);
  152. string cacheFilePath = GetCacheFilePath(
  153. originalImagePath,
  154. options.Width,
  155. options.Height,
  156. options.MaxWidth,
  157. options.MaxHeight,
  158. options.FillWidth,
  159. options.FillHeight,
  160. quality,
  161. dateModified,
  162. outputFormat,
  163. options.PercentPlayed,
  164. options.UnplayedCount,
  165. options.Blur,
  166. options.BackgroundColor,
  167. options.ForegroundLayer);
  168. try
  169. {
  170. if (!File.Exists(cacheFilePath))
  171. {
  172. // Limit number of parallel (more precisely: concurrent) image encodings to prevent a high memory usage
  173. await _parallelEncodingLimit.WaitAsync().ConfigureAwait(false);
  174. string resultPath;
  175. try
  176. {
  177. resultPath = _imageEncoder.EncodeImage(originalImagePath, dateModified, cacheFilePath, autoOrient, orientation, quality, options, outputFormat);
  178. }
  179. finally
  180. {
  181. _parallelEncodingLimit.Release();
  182. }
  183. if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase))
  184. {
  185. return (originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  186. }
  187. }
  188. return (cacheFilePath, outputFormat.GetMimeType(), _fileSystem.GetLastWriteTimeUtc(cacheFilePath));
  189. }
  190. catch (Exception ex)
  191. {
  192. // If it fails for whatever reason, return the original image
  193. _logger.LogError(ex, "Error encoding image");
  194. return (originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  195. }
  196. }
  197. private ImageFormat GetOutputFormat(IReadOnlyCollection<ImageFormat> clientSupportedFormats, bool requiresTransparency)
  198. {
  199. var serverFormats = GetSupportedImageOutputFormats();
  200. // Client doesn't care about format, so start with webp if supported
  201. if (serverFormats.Contains(ImageFormat.Webp) && clientSupportedFormats.Contains(ImageFormat.Webp))
  202. {
  203. return ImageFormat.Webp;
  204. }
  205. // If transparency is needed and webp isn't supported, than png is the only option
  206. if (requiresTransparency && clientSupportedFormats.Contains(ImageFormat.Png))
  207. {
  208. return ImageFormat.Png;
  209. }
  210. foreach (var format in clientSupportedFormats)
  211. {
  212. if (serverFormats.Contains(format))
  213. {
  214. return format;
  215. }
  216. }
  217. // We should never actually get here
  218. return ImageFormat.Jpg;
  219. }
  220. /// <summary>
  221. /// Gets the cache file path based on a set of parameters.
  222. /// </summary>
  223. private string GetCacheFilePath(
  224. string originalPath,
  225. int? width,
  226. int? height,
  227. int? maxWidth,
  228. int? maxHeight,
  229. int? fillWidth,
  230. int? fillHeight,
  231. int quality,
  232. DateTime dateModified,
  233. ImageFormat format,
  234. double percentPlayed,
  235. int? unwatchedCount,
  236. int? blur,
  237. string backgroundColor,
  238. string foregroundLayer)
  239. {
  240. var filename = new StringBuilder(256);
  241. filename.Append(originalPath);
  242. filename.Append(",quality=");
  243. filename.Append(quality);
  244. filename.Append(",datemodified=");
  245. filename.Append(dateModified.Ticks);
  246. filename.Append(",f=");
  247. filename.Append(format);
  248. if (width.HasValue)
  249. {
  250. filename.Append(",width=");
  251. filename.Append(width.Value);
  252. }
  253. if (height.HasValue)
  254. {
  255. filename.Append(",height=");
  256. filename.Append(height.Value);
  257. }
  258. if (maxWidth.HasValue)
  259. {
  260. filename.Append(",maxwidth=");
  261. filename.Append(maxWidth.Value);
  262. }
  263. if (maxHeight.HasValue)
  264. {
  265. filename.Append(",maxheight=");
  266. filename.Append(maxHeight.Value);
  267. }
  268. if (fillWidth.HasValue)
  269. {
  270. filename.Append(",fillwidth=");
  271. filename.Append(fillWidth.Value);
  272. }
  273. if (fillHeight.HasValue)
  274. {
  275. filename.Append(",fillheight=");
  276. filename.Append(fillHeight.Value);
  277. }
  278. if (percentPlayed > 0)
  279. {
  280. filename.Append(",p=");
  281. filename.Append(percentPlayed);
  282. }
  283. if (unwatchedCount.HasValue)
  284. {
  285. filename.Append(",p=");
  286. filename.Append(unwatchedCount.Value);
  287. }
  288. if (blur.HasValue)
  289. {
  290. filename.Append(",blur=");
  291. filename.Append(blur.Value);
  292. }
  293. if (!string.IsNullOrEmpty(backgroundColor))
  294. {
  295. filename.Append(",b=");
  296. filename.Append(backgroundColor);
  297. }
  298. if (!string.IsNullOrEmpty(foregroundLayer))
  299. {
  300. filename.Append(",fl=");
  301. filename.Append(foregroundLayer);
  302. }
  303. filename.Append(",v=");
  304. filename.Append(Version);
  305. return GetCachePath(ResizedImageCachePath, filename.ToString(), format.GetExtension());
  306. }
  307. /// <inheritdoc />
  308. public ImageDimensions GetImageDimensions(BaseItem item, ItemImageInfo info)
  309. {
  310. int width = info.Width;
  311. int height = info.Height;
  312. if (height > 0 && width > 0)
  313. {
  314. return new ImageDimensions(width, height);
  315. }
  316. string path = info.Path;
  317. _logger.LogDebug("Getting image size for item {ItemType} {Path}", item.GetType().Name, path);
  318. ImageDimensions size = GetImageDimensions(path);
  319. info.Width = size.Width;
  320. info.Height = size.Height;
  321. return size;
  322. }
  323. /// <inheritdoc />
  324. public ImageDimensions GetImageDimensions(string path)
  325. => _imageEncoder.GetImageSize(path);
  326. /// <inheritdoc />
  327. public string GetImageBlurHash(string path)
  328. {
  329. var size = GetImageDimensions(path);
  330. return GetImageBlurHash(path, size);
  331. }
  332. /// <inheritdoc />
  333. public string GetImageBlurHash(string path, ImageDimensions imageDimensions)
  334. {
  335. if (imageDimensions.Width <= 0 || imageDimensions.Height <= 0)
  336. {
  337. return string.Empty;
  338. }
  339. // We want tiles to be as close to square as possible, and to *mostly* keep under 16 tiles for performance.
  340. // One tile is (width / xComp) x (height / yComp) pixels, which means that ideally yComp = xComp * height / width.
  341. // See more at https://github.com/woltapp/blurhash/#how-do-i-pick-the-number-of-x-and-y-components
  342. float xCompF = MathF.Sqrt(16.0f * imageDimensions.Width / imageDimensions.Height);
  343. float yCompF = xCompF * imageDimensions.Height / imageDimensions.Width;
  344. int xComp = Math.Min((int)xCompF + 1, 9);
  345. int yComp = Math.Min((int)yCompF + 1, 9);
  346. return _imageEncoder.GetImageBlurHash(xComp, yComp, path);
  347. }
  348. /// <inheritdoc />
  349. public string GetImageCacheTag(BaseItem item, ItemImageInfo image)
  350. => (item.Path + image.DateModified.Ticks).GetMD5().ToString("N", CultureInfo.InvariantCulture);
  351. /// <inheritdoc />
  352. public string? GetImageCacheTag(BaseItem item, ChapterInfo chapter)
  353. {
  354. if (chapter.ImagePath is null)
  355. {
  356. return null;
  357. }
  358. return GetImageCacheTag(item, new ItemImageInfo
  359. {
  360. Path = chapter.ImagePath,
  361. Type = ImageType.Chapter,
  362. DateModified = chapter.ImageDateModified
  363. });
  364. }
  365. /// <inheritdoc />
  366. public string? GetImageCacheTag(User user)
  367. {
  368. if (user.ProfileImage is null)
  369. {
  370. return null;
  371. }
  372. return (user.ProfileImage.Path + user.ProfileImage.LastModified.Ticks).GetMD5()
  373. .ToString("N", CultureInfo.InvariantCulture);
  374. }
  375. private Task<(string Path, DateTime DateModified)> GetSupportedImage(string originalImagePath, DateTime dateModified)
  376. {
  377. var inputFormat = Path.GetExtension(originalImagePath.AsSpan()).TrimStart('.').ToString();
  378. // These are just jpg files renamed as tbn
  379. if (string.Equals(inputFormat, "tbn", StringComparison.OrdinalIgnoreCase))
  380. {
  381. return Task.FromResult((originalImagePath, dateModified));
  382. }
  383. return Task.FromResult((originalImagePath, dateModified));
  384. }
  385. /// <summary>
  386. /// Gets the cache path.
  387. /// </summary>
  388. /// <param name="path">The path.</param>
  389. /// <param name="uniqueName">Name of the unique.</param>
  390. /// <param name="fileExtension">The file extension.</param>
  391. /// <returns>System.String.</returns>
  392. /// <exception cref="ArgumentNullException">
  393. /// path
  394. /// or
  395. /// uniqueName
  396. /// or
  397. /// fileExtension.
  398. /// </exception>
  399. public string GetCachePath(string path, string uniqueName, string fileExtension)
  400. {
  401. ArgumentException.ThrowIfNullOrEmpty(path);
  402. ArgumentException.ThrowIfNullOrEmpty(uniqueName);
  403. ArgumentException.ThrowIfNullOrEmpty(fileExtension);
  404. var filename = uniqueName.GetMD5() + fileExtension;
  405. return GetCachePath(path, filename);
  406. }
  407. /// <summary>
  408. /// Gets the cache path.
  409. /// </summary>
  410. /// <param name="path">The path.</param>
  411. /// <param name="filename">The filename.</param>
  412. /// <returns>System.String.</returns>
  413. /// <exception cref="ArgumentNullException">
  414. /// path
  415. /// or
  416. /// filename.
  417. /// </exception>
  418. public string GetCachePath(ReadOnlySpan<char> path, ReadOnlySpan<char> filename)
  419. {
  420. if (path.IsEmpty)
  421. {
  422. throw new ArgumentException("Path can't be empty.", nameof(path));
  423. }
  424. if (filename.IsEmpty)
  425. {
  426. throw new ArgumentException("Filename can't be empty.", nameof(filename));
  427. }
  428. var prefix = filename.Slice(0, 1);
  429. return Path.Join(path, prefix, filename);
  430. }
  431. /// <inheritdoc />
  432. public void CreateImageCollage(ImageCollageOptions options, string? libraryName)
  433. {
  434. _logger.LogInformation("Creating image collage and saving to {Path}", options.OutputPath);
  435. _imageEncoder.CreateImageCollage(options, libraryName);
  436. _logger.LogInformation("Completed creation of image collage and saved to {Path}", options.OutputPath);
  437. }
  438. /// <inheritdoc />
  439. public void Dispose()
  440. {
  441. if (_disposed)
  442. {
  443. return;
  444. }
  445. if (_imageEncoder is IDisposable disposable)
  446. {
  447. disposable.Dispose();
  448. }
  449. _parallelEncodingLimit?.Dispose();
  450. _disposed = true;
  451. }
  452. }