ImageProcessor.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Drawing;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Drawing;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using MediaBrowser.Model.IO;
  19. using Emby.Drawing.Common;
  20. using MediaBrowser.Controller.Library;
  21. using MediaBrowser.Controller.MediaEncoding;
  22. using MediaBrowser.Model.Net;
  23. using MediaBrowser.Model.Threading;
  24. using MediaBrowser.Model.Extensions;
  25. namespace Emby.Drawing
  26. {
  27. /// <summary>
  28. /// Class ImageProcessor
  29. /// </summary>
  30. public class ImageProcessor : IImageProcessor, IDisposable
  31. {
  32. /// <summary>
  33. /// The us culture
  34. /// </summary>
  35. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  36. /// <summary>
  37. /// Gets the list of currently registered image processors
  38. /// Image processors are specialized metadata providers that run after the normal ones
  39. /// </summary>
  40. /// <value>The image enhancers.</value>
  41. public IImageEnhancer[] ImageEnhancers { get; private set; }
  42. /// <summary>
  43. /// The _logger
  44. /// </summary>
  45. private readonly ILogger _logger;
  46. private readonly IFileSystem _fileSystem;
  47. private readonly IJsonSerializer _jsonSerializer;
  48. private readonly IServerApplicationPaths _appPaths;
  49. private IImageEncoder _imageEncoder;
  50. private readonly Func<ILibraryManager> _libraryManager;
  51. private readonly Func<IMediaEncoder> _mediaEncoder;
  52. public ImageProcessor(ILogger logger,
  53. IServerApplicationPaths appPaths,
  54. IFileSystem fileSystem,
  55. IJsonSerializer jsonSerializer,
  56. IImageEncoder imageEncoder,
  57. Func<ILibraryManager> libraryManager, ITimerFactory timerFactory, Func<IMediaEncoder> mediaEncoder)
  58. {
  59. _logger = logger;
  60. _fileSystem = fileSystem;
  61. _jsonSerializer = jsonSerializer;
  62. _imageEncoder = imageEncoder;
  63. _libraryManager = libraryManager;
  64. _mediaEncoder = mediaEncoder;
  65. _appPaths = appPaths;
  66. ImageEnhancers = new IImageEnhancer[] { };
  67. ImageHelper.ImageProcessor = this;
  68. }
  69. public IImageEncoder ImageEncoder
  70. {
  71. get { return _imageEncoder; }
  72. set
  73. {
  74. if (value == null)
  75. {
  76. throw new ArgumentNullException("value");
  77. }
  78. _imageEncoder = value;
  79. }
  80. }
  81. public string[] SupportedInputFormats
  82. {
  83. get
  84. {
  85. return new string[]
  86. {
  87. "tiff",
  88. "tif",
  89. "jpeg",
  90. "jpg",
  91. "png",
  92. "aiff",
  93. "cr2",
  94. "crw",
  95. // Remove until supported
  96. //"nef",
  97. "orf",
  98. "pef",
  99. "arw",
  100. "webp",
  101. "gif",
  102. "bmp",
  103. "erf",
  104. "raf",
  105. "rw2",
  106. "nrw",
  107. "dng",
  108. "ico",
  109. "astc",
  110. "ktx",
  111. "pkm",
  112. "wbmp"
  113. };
  114. }
  115. }
  116. public bool SupportsImageCollageCreation
  117. {
  118. get
  119. {
  120. return _imageEncoder.SupportsImageCollageCreation;
  121. }
  122. }
  123. private string ResizedImageCachePath
  124. {
  125. get
  126. {
  127. return Path.Combine(_appPaths.ImageCachePath, "resized-images");
  128. }
  129. }
  130. private string EnhancedImageCachePath
  131. {
  132. get
  133. {
  134. return Path.Combine(_appPaths.ImageCachePath, "enhanced-images");
  135. }
  136. }
  137. public void AddParts(IEnumerable<IImageEnhancer> enhancers)
  138. {
  139. ImageEnhancers = enhancers.ToArray();
  140. }
  141. public async Task ProcessImage(ImageProcessingOptions options, Stream toStream)
  142. {
  143. var file = await ProcessImage(options).ConfigureAwait(false);
  144. using (var fileStream = _fileSystem.GetFileStream(file.Item1, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true))
  145. {
  146. await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
  147. }
  148. }
  149. public ImageFormat[] GetSupportedImageOutputFormats()
  150. {
  151. return _imageEncoder.SupportedOutputFormats;
  152. }
  153. private static readonly string[] TransparentImageTypes = new string[] { ".png", ".webp" };
  154. public bool SupportsTransparency(string path)
  155. {
  156. return TransparentImageTypes.Contains(Path.GetExtension(path) ?? string.Empty);
  157. }
  158. public async Task<Tuple<string, string, DateTime>> ProcessImage(ImageProcessingOptions options)
  159. {
  160. if (options == null)
  161. {
  162. throw new ArgumentNullException("options");
  163. }
  164. var originalImage = options.Image;
  165. IHasMetadata item = options.Item;
  166. if (!originalImage.IsLocalFile)
  167. {
  168. if (item == null)
  169. {
  170. item = _libraryManager().GetItemById(options.ItemId);
  171. }
  172. originalImage = await _libraryManager().ConvertImageToLocal(item, originalImage, options.ImageIndex).ConfigureAwait(false);
  173. }
  174. var originalImagePath = originalImage.Path;
  175. var dateModified = originalImage.DateModified;
  176. if (!_imageEncoder.SupportsImageEncoding)
  177. {
  178. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  179. }
  180. var supportedImageInfo = await GetSupportedImage(originalImagePath, dateModified).ConfigureAwait(false);
  181. originalImagePath = supportedImageInfo.Item1;
  182. dateModified = supportedImageInfo.Item2;
  183. var requiresTransparency = TransparentImageTypes.Contains(Path.GetExtension(originalImagePath) ?? string.Empty);
  184. if (options.Enhancers.Count > 0)
  185. {
  186. if (item == null)
  187. {
  188. item = _libraryManager().GetItemById(options.ItemId);
  189. }
  190. var tuple = await GetEnhancedImage(new ItemImageInfo
  191. {
  192. DateModified = dateModified,
  193. Type = originalImage.Type,
  194. Path = originalImagePath
  195. }, requiresTransparency, item, options.ImageIndex, options.Enhancers).ConfigureAwait(false);
  196. originalImagePath = tuple.Item1;
  197. dateModified = tuple.Item2;
  198. requiresTransparency = tuple.Item3;
  199. }
  200. var photo = item as Photo;
  201. var autoOrient = false;
  202. ImageOrientation? orientation = null;
  203. if (photo != null && photo.Orientation.HasValue && photo.Orientation.Value != ImageOrientation.TopLeft)
  204. {
  205. autoOrient = true;
  206. orientation = photo.Orientation;
  207. }
  208. if (options.HasDefaultOptions(originalImagePath) && (!autoOrient || !options.RequiresAutoOrientation))
  209. {
  210. // Just spit out the original file if all the options are default
  211. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  212. }
  213. //ImageSize? originalImageSize = GetSavedImageSize(originalImagePath, dateModified);
  214. //if (originalImageSize.HasValue && options.HasDefaultOptions(originalImagePath, originalImageSize.Value) && !autoOrient)
  215. //{
  216. // // Just spit out the original file if all the options are default
  217. // _logger.Info("Returning original image {0}", originalImagePath);
  218. // return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  219. //}
  220. var newSize = ImageHelper.GetNewImageSize(options, null);
  221. var quality = options.Quality;
  222. var outputFormat = GetOutputFormat(options.SupportedOutputFormats, requiresTransparency);
  223. var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, outputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.Blur, options.BackgroundColor, options.ForegroundLayer);
  224. try
  225. {
  226. CheckDisposed();
  227. if (!_fileSystem.FileExists(cacheFilePath))
  228. {
  229. var tmpPath = Path.ChangeExtension(Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N")), Path.GetExtension(cacheFilePath));
  230. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(tmpPath));
  231. if (options.CropWhiteSpace && !SupportsTransparency(originalImagePath))
  232. {
  233. options.CropWhiteSpace = false;
  234. }
  235. var resultPath = _imageEncoder.EncodeImage(originalImagePath, dateModified, tmpPath, autoOrient, orientation, quality, options, outputFormat);
  236. if (string.Equals(resultPath, originalImagePath, StringComparison.OrdinalIgnoreCase))
  237. {
  238. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  239. }
  240. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(cacheFilePath));
  241. CopyFile(tmpPath, cacheFilePath);
  242. return new Tuple<string, string, DateTime>(tmpPath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(tmpPath));
  243. }
  244. return new Tuple<string, string, DateTime>(cacheFilePath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(cacheFilePath));
  245. }
  246. catch (ArgumentOutOfRangeException ex)
  247. {
  248. // Decoder failed to decode it
  249. #if DEBUG
  250. _logger.ErrorException("Error encoding image", ex);
  251. #endif
  252. // Just spit out the original file if all the options are default
  253. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  254. }
  255. catch (Exception ex)
  256. {
  257. // If it fails for whatever reason, return the original image
  258. _logger.ErrorException("Error encoding image", ex);
  259. // Just spit out the original file if all the options are default
  260. return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
  261. }
  262. }
  263. private ImageFormat GetOutputFormat(ImageFormat[] clientSupportedFormats, bool requiresTransparency)
  264. {
  265. var serverFormats = GetSupportedImageOutputFormats();
  266. // Client doesn't care about format, so start with webp if supported
  267. if (serverFormats.Contains(ImageFormat.Webp) && clientSupportedFormats.Contains(ImageFormat.Webp))
  268. {
  269. return ImageFormat.Webp;
  270. }
  271. // If transparency is needed and webp isn't supported, than png is the only option
  272. if (requiresTransparency)
  273. {
  274. return ImageFormat.Png;
  275. }
  276. foreach (var format in clientSupportedFormats)
  277. {
  278. if (serverFormats.Contains(format))
  279. {
  280. return format;
  281. }
  282. }
  283. // We should never actually get here
  284. return ImageFormat.Jpg;
  285. }
  286. private void CopyFile(string src, string destination)
  287. {
  288. try
  289. {
  290. _fileSystem.CopyFile(src, destination, true);
  291. }
  292. catch
  293. {
  294. }
  295. }
  296. //private static int[][] OPERATIONS = new int[][] {
  297. // TopLeft
  298. //new int[] { 0, NONE},
  299. // TopRight
  300. //new int[] { 0, HORIZONTAL},
  301. //new int[] {180, NONE},
  302. // LeftTop
  303. //new int[] { 0, VERTICAL},
  304. //new int[] { 90, HORIZONTAL},
  305. // RightTop
  306. //new int[] { 90, NONE},
  307. //new int[] {-90, HORIZONTAL},
  308. //new int[] {-90, NONE},
  309. //};
  310. private string GetMimeType(ImageFormat format, string path)
  311. {
  312. if (format == ImageFormat.Bmp)
  313. {
  314. return MimeTypes.GetMimeType("i.bmp");
  315. }
  316. if (format == ImageFormat.Gif)
  317. {
  318. return MimeTypes.GetMimeType("i.gif");
  319. }
  320. if (format == ImageFormat.Jpg)
  321. {
  322. return MimeTypes.GetMimeType("i.jpg");
  323. }
  324. if (format == ImageFormat.Png)
  325. {
  326. return MimeTypes.GetMimeType("i.png");
  327. }
  328. if (format == ImageFormat.Webp)
  329. {
  330. return MimeTypes.GetMimeType("i.webp");
  331. }
  332. return MimeTypes.GetMimeType(path);
  333. }
  334. /// <summary>
  335. /// Increment this when there's a change requiring caches to be invalidated
  336. /// </summary>
  337. private const string Version = "3";
  338. /// <summary>
  339. /// Gets the cache file path based on a set of parameters
  340. /// </summary>
  341. private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageFormat format, bool addPlayedIndicator, double percentPlayed, int? unwatchedCount, int? blur, string backgroundColor, string foregroundLayer)
  342. {
  343. var filename = originalPath;
  344. filename += "width=" + outputSize.Width;
  345. filename += "height=" + outputSize.Height;
  346. filename += "quality=" + quality;
  347. filename += "datemodified=" + dateModified.Ticks;
  348. filename += "f=" + format;
  349. if (addPlayedIndicator)
  350. {
  351. filename += "pl=true";
  352. }
  353. if (percentPlayed > 0)
  354. {
  355. filename += "p=" + percentPlayed;
  356. }
  357. if (unwatchedCount.HasValue)
  358. {
  359. filename += "p=" + unwatchedCount.Value;
  360. }
  361. if (blur.HasValue)
  362. {
  363. filename += "blur=" + blur.Value;
  364. }
  365. if (!string.IsNullOrEmpty(backgroundColor))
  366. {
  367. filename += "b=" + backgroundColor;
  368. }
  369. if (!string.IsNullOrEmpty(foregroundLayer))
  370. {
  371. filename += "fl=" + foregroundLayer;
  372. }
  373. filename += "v=" + Version;
  374. return GetCachePath(ResizedImageCachePath, filename, "." + format.ToString().ToLower());
  375. }
  376. public ImageSize GetImageSize(BaseItem item, ItemImageInfo info)
  377. {
  378. return GetImageSize(item, info, false, true);
  379. }
  380. public ImageSize GetImageSize(BaseItem item, ItemImageInfo info, bool allowSlowMethods, bool updateItem)
  381. {
  382. var width = info.Width;
  383. var height = info.Height;
  384. if (height > 0 && width > 0)
  385. {
  386. return new ImageSize
  387. {
  388. Width = width,
  389. Height = height
  390. };
  391. }
  392. var path = info.Path;
  393. _logger.Info("Getting image size for item {0} {1}", item.GetType().Name, path);
  394. var size = GetImageSize(path, allowSlowMethods);
  395. info.Height = Convert.ToInt32(size.Height);
  396. info.Width = Convert.ToInt32(size.Width);
  397. if (updateItem)
  398. {
  399. _libraryManager().UpdateImages(item);
  400. }
  401. return size;
  402. }
  403. public ImageSize GetImageSize(string path)
  404. {
  405. return GetImageSize(path, true);
  406. }
  407. /// <summary>
  408. /// Gets the size of the image.
  409. /// </summary>
  410. private ImageSize GetImageSize(string path, bool allowSlowMethod)
  411. {
  412. if (string.IsNullOrEmpty(path))
  413. {
  414. throw new ArgumentNullException("path");
  415. }
  416. return GetImageSizeInternal(path, allowSlowMethod);
  417. }
  418. /// <summary>
  419. /// Gets the image size internal.
  420. /// </summary>
  421. /// <param name="path">The path.</param>
  422. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  423. /// <returns>ImageSize.</returns>
  424. private ImageSize GetImageSizeInternal(string path, bool allowSlowMethod)
  425. {
  426. //try
  427. //{
  428. // using (var fileStream = _fileSystem.OpenRead(path))
  429. // {
  430. // using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(path), fileStream, null)))
  431. // {
  432. // var image = file as TagLib.Image.File;
  433. // if (image != null)
  434. // {
  435. // var properties = image.Properties;
  436. // return new ImageSize
  437. // {
  438. // Height = properties.PhotoHeight,
  439. // Width = properties.PhotoWidth
  440. // };
  441. // }
  442. // }
  443. // }
  444. //}
  445. //catch
  446. //{
  447. //}
  448. try
  449. {
  450. return ImageHeader.GetDimensions(path, _logger, _fileSystem);
  451. }
  452. catch
  453. {
  454. if (allowSlowMethod)
  455. {
  456. return _imageEncoder.GetImageSize(path);
  457. }
  458. throw;
  459. }
  460. }
  461. /// <summary>
  462. /// Gets the image cache tag.
  463. /// </summary>
  464. /// <param name="item">The item.</param>
  465. /// <param name="image">The image.</param>
  466. /// <returns>Guid.</returns>
  467. /// <exception cref="System.ArgumentNullException">item</exception>
  468. public string GetImageCacheTag(IHasMetadata item, ItemImageInfo image)
  469. {
  470. if (item == null)
  471. {
  472. throw new ArgumentNullException("item");
  473. }
  474. if (image == null)
  475. {
  476. throw new ArgumentNullException("image");
  477. }
  478. var supportedEnhancers = GetSupportedEnhancers(item, image.Type);
  479. return GetImageCacheTag(item, image, supportedEnhancers);
  480. }
  481. /// <summary>
  482. /// Gets the image cache tag.
  483. /// </summary>
  484. /// <param name="item">The item.</param>
  485. /// <param name="image">The image.</param>
  486. /// <param name="imageEnhancers">The image enhancers.</param>
  487. /// <returns>Guid.</returns>
  488. /// <exception cref="System.ArgumentNullException">item</exception>
  489. public string GetImageCacheTag(IHasMetadata item, ItemImageInfo image, List<IImageEnhancer> imageEnhancers)
  490. {
  491. if (item == null)
  492. {
  493. throw new ArgumentNullException("item");
  494. }
  495. if (imageEnhancers == null)
  496. {
  497. throw new ArgumentNullException("imageEnhancers");
  498. }
  499. if (image == null)
  500. {
  501. throw new ArgumentNullException("image");
  502. }
  503. var originalImagePath = image.Path;
  504. var dateModified = image.DateModified;
  505. var imageType = image.Type;
  506. // Optimization
  507. if (imageEnhancers.Count == 0)
  508. {
  509. return (originalImagePath + dateModified.Ticks).GetMD5().ToString("N");
  510. }
  511. // Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
  512. var cacheKeys = imageEnhancers.Select(i => i.GetConfigurationCacheKey(item, imageType)).ToList();
  513. cacheKeys.Add(originalImagePath + dateModified.Ticks);
  514. return string.Join("|", cacheKeys.ToArray(cacheKeys.Count)).GetMD5().ToString("N");
  515. }
  516. private async Task<Tuple<string, DateTime>> GetSupportedImage(string originalImagePath, DateTime dateModified)
  517. {
  518. var inputFormat = (Path.GetExtension(originalImagePath) ?? string.Empty)
  519. .TrimStart('.')
  520. .Replace("jpeg", "jpg", StringComparison.OrdinalIgnoreCase);
  521. // These are just jpg files renamed as tbn
  522. if (string.Equals(inputFormat, "tbn", StringComparison.OrdinalIgnoreCase))
  523. {
  524. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  525. }
  526. if (!_imageEncoder.SupportedInputFormats.Contains(inputFormat, StringComparer.OrdinalIgnoreCase))
  527. {
  528. try
  529. {
  530. var filename = (originalImagePath + dateModified.Ticks.ToString(UsCulture)).GetMD5().ToString("N");
  531. var cacheExtension = _mediaEncoder().SupportsEncoder("libwebp") ? ".webp" : ".png";
  532. var outputPath = Path.Combine(_appPaths.ImageCachePath, "converted-images", filename + cacheExtension);
  533. var file = _fileSystem.GetFileInfo(outputPath);
  534. if (!file.Exists)
  535. {
  536. await _mediaEncoder().ConvertImage(originalImagePath, outputPath).ConfigureAwait(false);
  537. dateModified = _fileSystem.GetLastWriteTimeUtc(outputPath);
  538. }
  539. else
  540. {
  541. dateModified = file.LastWriteTimeUtc;
  542. }
  543. originalImagePath = outputPath;
  544. }
  545. catch (Exception ex)
  546. {
  547. _logger.ErrorException("Image conversion failed for {0}", ex, originalImagePath);
  548. }
  549. }
  550. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  551. }
  552. /// <summary>
  553. /// Gets the enhanced image.
  554. /// </summary>
  555. /// <param name="item">The item.</param>
  556. /// <param name="imageType">Type of the image.</param>
  557. /// <param name="imageIndex">Index of the image.</param>
  558. /// <returns>Task{System.String}.</returns>
  559. public async Task<string> GetEnhancedImage(IHasMetadata item, ImageType imageType, int imageIndex)
  560. {
  561. var enhancers = GetSupportedEnhancers(item, imageType);
  562. var imageInfo = item.GetImageInfo(imageType, imageIndex);
  563. var inputImageSupportsTransparency = SupportsTransparency(imageInfo.Path);
  564. var result = await GetEnhancedImage(imageInfo, inputImageSupportsTransparency, item, imageIndex, enhancers);
  565. return result.Item1;
  566. }
  567. private async Task<Tuple<string, DateTime, bool>> GetEnhancedImage(ItemImageInfo image,
  568. bool inputImageSupportsTransparency,
  569. IHasMetadata item,
  570. int imageIndex,
  571. List<IImageEnhancer> enhancers)
  572. {
  573. var originalImagePath = image.Path;
  574. var dateModified = image.DateModified;
  575. var imageType = image.Type;
  576. try
  577. {
  578. var cacheGuid = GetImageCacheTag(item, image, enhancers);
  579. // Enhance if we have enhancers
  580. var ehnancedImageInfo = await GetEnhancedImageInternal(originalImagePath, item, imageType, imageIndex, enhancers, cacheGuid).ConfigureAwait(false);
  581. var ehnancedImagePath = ehnancedImageInfo.Item1;
  582. // If the path changed update dateModified
  583. if (!string.Equals(ehnancedImagePath, originalImagePath, StringComparison.OrdinalIgnoreCase))
  584. {
  585. var treatmentRequiresTransparency = ehnancedImageInfo.Item2;
  586. return new Tuple<string, DateTime, bool>(ehnancedImagePath, _fileSystem.GetLastWriteTimeUtc(ehnancedImagePath), treatmentRequiresTransparency);
  587. }
  588. }
  589. catch (Exception ex)
  590. {
  591. _logger.ErrorException("Error enhancing image", ex);
  592. }
  593. return new Tuple<string, DateTime, bool>(originalImagePath, dateModified, inputImageSupportsTransparency);
  594. }
  595. /// <summary>
  596. /// Gets the enhanced image internal.
  597. /// </summary>
  598. /// <param name="originalImagePath">The original image path.</param>
  599. /// <param name="item">The item.</param>
  600. /// <param name="imageType">Type of the image.</param>
  601. /// <param name="imageIndex">Index of the image.</param>
  602. /// <param name="supportedEnhancers">The supported enhancers.</param>
  603. /// <param name="cacheGuid">The cache unique identifier.</param>
  604. /// <returns>Task&lt;System.String&gt;.</returns>
  605. /// <exception cref="ArgumentNullException">
  606. /// originalImagePath
  607. /// or
  608. /// item
  609. /// </exception>
  610. private async Task<Tuple<string, bool>> GetEnhancedImageInternal(string originalImagePath,
  611. IHasMetadata item,
  612. ImageType imageType,
  613. int imageIndex,
  614. List<IImageEnhancer> supportedEnhancers,
  615. string cacheGuid)
  616. {
  617. if (string.IsNullOrEmpty(originalImagePath))
  618. {
  619. throw new ArgumentNullException("originalImagePath");
  620. }
  621. if (item == null)
  622. {
  623. throw new ArgumentNullException("item");
  624. }
  625. var treatmentRequiresTransparency = false;
  626. foreach (var enhancer in supportedEnhancers)
  627. {
  628. if (!treatmentRequiresTransparency)
  629. {
  630. treatmentRequiresTransparency = enhancer.GetEnhancedImageInfo(item, originalImagePath, imageType, imageIndex).RequiresTransparency;
  631. }
  632. }
  633. // All enhanced images are saved as png to allow transparency
  634. var cacheExtension = _imageEncoder.SupportedOutputFormats.Contains(ImageFormat.Webp) ?
  635. ".webp" :
  636. (treatmentRequiresTransparency ? ".png" : ".jpg");
  637. var enhancedImagePath = GetCachePath(EnhancedImageCachePath, cacheGuid + cacheExtension);
  638. // Check again in case of contention
  639. if (_fileSystem.FileExists(enhancedImagePath))
  640. {
  641. return new Tuple<string, bool>(enhancedImagePath, treatmentRequiresTransparency);
  642. }
  643. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(enhancedImagePath));
  644. var tmpPath = Path.Combine(_appPaths.TempDirectory, Path.ChangeExtension(Guid.NewGuid().ToString(), Path.GetExtension(enhancedImagePath)));
  645. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(tmpPath));
  646. await ExecuteImageEnhancers(supportedEnhancers, originalImagePath, tmpPath, item, imageType, imageIndex).ConfigureAwait(false);
  647. try
  648. {
  649. _fileSystem.CopyFile(tmpPath, enhancedImagePath, true);
  650. }
  651. catch
  652. {
  653. }
  654. return new Tuple<string, bool>(tmpPath, treatmentRequiresTransparency);
  655. }
  656. /// <summary>
  657. /// Executes the image enhancers.
  658. /// </summary>
  659. /// <param name="imageEnhancers">The image enhancers.</param>
  660. /// <param name="inputPath">The input path.</param>
  661. /// <param name="outputPath">The output path.</param>
  662. /// <param name="item">The item.</param>
  663. /// <param name="imageType">Type of the image.</param>
  664. /// <param name="imageIndex">Index of the image.</param>
  665. /// <returns>Task{EnhancedImage}.</returns>
  666. private async Task ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, string inputPath, string outputPath, IHasMetadata item, ImageType imageType, int imageIndex)
  667. {
  668. // Run the enhancers sequentially in order of priority
  669. foreach (var enhancer in imageEnhancers)
  670. {
  671. await enhancer.EnhanceImageAsync(item, inputPath, outputPath, imageType, imageIndex).ConfigureAwait(false);
  672. // Feed the output into the next enhancer as input
  673. inputPath = outputPath;
  674. }
  675. }
  676. /// <summary>
  677. /// Gets the cache path.
  678. /// </summary>
  679. /// <param name="path">The path.</param>
  680. /// <param name="uniqueName">Name of the unique.</param>
  681. /// <param name="fileExtension">The file extension.</param>
  682. /// <returns>System.String.</returns>
  683. /// <exception cref="System.ArgumentNullException">
  684. /// path
  685. /// or
  686. /// uniqueName
  687. /// or
  688. /// fileExtension
  689. /// </exception>
  690. public string GetCachePath(string path, string uniqueName, string fileExtension)
  691. {
  692. if (string.IsNullOrEmpty(path))
  693. {
  694. throw new ArgumentNullException("path");
  695. }
  696. if (string.IsNullOrEmpty(uniqueName))
  697. {
  698. throw new ArgumentNullException("uniqueName");
  699. }
  700. if (string.IsNullOrEmpty(fileExtension))
  701. {
  702. throw new ArgumentNullException("fileExtension");
  703. }
  704. var filename = uniqueName.GetMD5() + fileExtension;
  705. return GetCachePath(path, filename);
  706. }
  707. /// <summary>
  708. /// Gets the cache path.
  709. /// </summary>
  710. /// <param name="path">The path.</param>
  711. /// <param name="filename">The filename.</param>
  712. /// <returns>System.String.</returns>
  713. /// <exception cref="System.ArgumentNullException">
  714. /// path
  715. /// or
  716. /// filename
  717. /// </exception>
  718. public string GetCachePath(string path, string filename)
  719. {
  720. if (string.IsNullOrEmpty(path))
  721. {
  722. throw new ArgumentNullException("path");
  723. }
  724. if (string.IsNullOrEmpty(filename))
  725. {
  726. throw new ArgumentNullException("filename");
  727. }
  728. var prefix = filename.Substring(0, 1);
  729. path = Path.Combine(path, prefix);
  730. return Path.Combine(path, filename);
  731. }
  732. public void CreateImageCollage(ImageCollageOptions options)
  733. {
  734. _logger.Info("Creating image collage and saving to {0}", options.OutputPath);
  735. _imageEncoder.CreateImageCollage(options);
  736. _logger.Info("Completed creation of image collage and saved to {0}", options.OutputPath);
  737. }
  738. public List<IImageEnhancer> GetSupportedEnhancers(IHasMetadata item, ImageType imageType)
  739. {
  740. var list = new List<IImageEnhancer>();
  741. foreach (var i in ImageEnhancers)
  742. {
  743. try
  744. {
  745. if (i.Supports(item, imageType))
  746. {
  747. list.Add(i);
  748. }
  749. }
  750. catch (Exception ex)
  751. {
  752. _logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
  753. }
  754. }
  755. return list;
  756. }
  757. private bool _disposed;
  758. public void Dispose()
  759. {
  760. _disposed = true;
  761. var disposable = _imageEncoder as IDisposable;
  762. if (disposable != null)
  763. {
  764. disposable.Dispose();
  765. }
  766. GC.SuppressFinalize(this);
  767. }
  768. private void CheckDisposed()
  769. {
  770. if (_disposed)
  771. {
  772. throw new ObjectDisposedException(GetType().Name);
  773. }
  774. }
  775. }
  776. }