ImageProcessor.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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 = item.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. /// <summary>
  404. /// Gets the size of the image.
  405. /// </summary>
  406. private ImageSize GetImageSize(string path, bool allowSlowMethod)
  407. {
  408. if (string.IsNullOrEmpty(path))
  409. {
  410. throw new ArgumentNullException("path");
  411. }
  412. return GetImageSizeInternal(path, allowSlowMethod);
  413. }
  414. /// <summary>
  415. /// Gets the image size internal.
  416. /// </summary>
  417. /// <param name="path">The path.</param>
  418. /// <param name="allowSlowMethod">if set to <c>true</c> [allow slow method].</param>
  419. /// <returns>ImageSize.</returns>
  420. private ImageSize GetImageSizeInternal(string path, bool allowSlowMethod)
  421. {
  422. //try
  423. //{
  424. // using (var fileStream = _fileSystem.OpenRead(path))
  425. // {
  426. // using (var file = TagLib.File.Create(new StreamFileAbstraction(Path.GetFileName(path), fileStream, null)))
  427. // {
  428. // var image = file as TagLib.Image.File;
  429. // if (image != null)
  430. // {
  431. // var properties = image.Properties;
  432. // return new ImageSize
  433. // {
  434. // Height = properties.PhotoHeight,
  435. // Width = properties.PhotoWidth
  436. // };
  437. // }
  438. // }
  439. // }
  440. //}
  441. //catch
  442. //{
  443. //}
  444. try
  445. {
  446. return ImageHeader.GetDimensions(path, _logger, _fileSystem);
  447. }
  448. catch
  449. {
  450. if (allowSlowMethod)
  451. {
  452. return _imageEncoder.GetImageSize(path);
  453. }
  454. throw;
  455. }
  456. }
  457. /// <summary>
  458. /// Gets the image cache tag.
  459. /// </summary>
  460. /// <param name="item">The item.</param>
  461. /// <param name="image">The image.</param>
  462. /// <returns>Guid.</returns>
  463. /// <exception cref="System.ArgumentNullException">item</exception>
  464. public string GetImageCacheTag(IHasMetadata item, ItemImageInfo image)
  465. {
  466. if (item == null)
  467. {
  468. throw new ArgumentNullException("item");
  469. }
  470. if (image == null)
  471. {
  472. throw new ArgumentNullException("image");
  473. }
  474. var supportedEnhancers = GetSupportedEnhancers(item, image.Type);
  475. return GetImageCacheTag(item, image, supportedEnhancers);
  476. }
  477. /// <summary>
  478. /// Gets the image cache tag.
  479. /// </summary>
  480. /// <param name="item">The item.</param>
  481. /// <param name="image">The image.</param>
  482. /// <param name="imageEnhancers">The image enhancers.</param>
  483. /// <returns>Guid.</returns>
  484. /// <exception cref="System.ArgumentNullException">item</exception>
  485. public string GetImageCacheTag(IHasMetadata item, ItemImageInfo image, List<IImageEnhancer> imageEnhancers)
  486. {
  487. if (item == null)
  488. {
  489. throw new ArgumentNullException("item");
  490. }
  491. if (imageEnhancers == null)
  492. {
  493. throw new ArgumentNullException("imageEnhancers");
  494. }
  495. if (image == null)
  496. {
  497. throw new ArgumentNullException("image");
  498. }
  499. var originalImagePath = image.Path;
  500. var dateModified = image.DateModified;
  501. var imageType = image.Type;
  502. // Optimization
  503. if (imageEnhancers.Count == 0)
  504. {
  505. return (originalImagePath + dateModified.Ticks).GetMD5().ToString("N");
  506. }
  507. // Cache name is created with supported enhancers combined with the last config change so we pick up new config changes
  508. var cacheKeys = imageEnhancers.Select(i => i.GetConfigurationCacheKey(item, imageType)).ToList();
  509. cacheKeys.Add(originalImagePath + dateModified.Ticks);
  510. return string.Join("|", cacheKeys.ToArray(cacheKeys.Count)).GetMD5().ToString("N");
  511. }
  512. private async Task<Tuple<string, DateTime>> GetSupportedImage(string originalImagePath, DateTime dateModified)
  513. {
  514. var inputFormat = (Path.GetExtension(originalImagePath) ?? string.Empty)
  515. .TrimStart('.')
  516. .Replace("jpeg", "jpg", StringComparison.OrdinalIgnoreCase);
  517. // These are just jpg files renamed as tbn
  518. if (string.Equals(inputFormat, "tbn", StringComparison.OrdinalIgnoreCase))
  519. {
  520. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  521. }
  522. if (!_imageEncoder.SupportedInputFormats.Contains(inputFormat, StringComparer.OrdinalIgnoreCase))
  523. {
  524. try
  525. {
  526. var filename = (originalImagePath + dateModified.Ticks.ToString(UsCulture)).GetMD5().ToString("N");
  527. var cacheExtension = _mediaEncoder().SupportsEncoder("libwebp") ? ".webp" : ".png";
  528. var outputPath = Path.Combine(_appPaths.ImageCachePath, "converted-images", filename + cacheExtension);
  529. var file = _fileSystem.GetFileInfo(outputPath);
  530. if (!file.Exists)
  531. {
  532. await _mediaEncoder().ConvertImage(originalImagePath, outputPath).ConfigureAwait(false);
  533. dateModified = _fileSystem.GetLastWriteTimeUtc(outputPath);
  534. }
  535. else
  536. {
  537. dateModified = file.LastWriteTimeUtc;
  538. }
  539. originalImagePath = outputPath;
  540. }
  541. catch (Exception ex)
  542. {
  543. _logger.ErrorException("Image conversion failed for {0}", ex, originalImagePath);
  544. }
  545. }
  546. return new Tuple<string, DateTime>(originalImagePath, dateModified);
  547. }
  548. /// <summary>
  549. /// Gets the enhanced image.
  550. /// </summary>
  551. /// <param name="item">The item.</param>
  552. /// <param name="imageType">Type of the image.</param>
  553. /// <param name="imageIndex">Index of the image.</param>
  554. /// <returns>Task{System.String}.</returns>
  555. public async Task<string> GetEnhancedImage(IHasMetadata item, ImageType imageType, int imageIndex)
  556. {
  557. var enhancers = GetSupportedEnhancers(item, imageType);
  558. var imageInfo = item.GetImageInfo(imageType, imageIndex);
  559. var inputImageSupportsTransparency = SupportsTransparency(imageInfo.Path);
  560. var result = await GetEnhancedImage(imageInfo, inputImageSupportsTransparency, item, imageIndex, enhancers);
  561. return result.Item1;
  562. }
  563. private async Task<Tuple<string, DateTime, bool>> GetEnhancedImage(ItemImageInfo image,
  564. bool inputImageSupportsTransparency,
  565. IHasMetadata item,
  566. int imageIndex,
  567. List<IImageEnhancer> enhancers)
  568. {
  569. var originalImagePath = image.Path;
  570. var dateModified = image.DateModified;
  571. var imageType = image.Type;
  572. try
  573. {
  574. var cacheGuid = GetImageCacheTag(item, image, enhancers);
  575. // Enhance if we have enhancers
  576. var ehnancedImageInfo = await GetEnhancedImageInternal(originalImagePath, item, imageType, imageIndex, enhancers, cacheGuid).ConfigureAwait(false);
  577. var ehnancedImagePath = ehnancedImageInfo.Item1;
  578. // If the path changed update dateModified
  579. if (!string.Equals(ehnancedImagePath, originalImagePath, StringComparison.OrdinalIgnoreCase))
  580. {
  581. var treatmentRequiresTransparency = ehnancedImageInfo.Item2;
  582. return new Tuple<string, DateTime, bool>(ehnancedImagePath, _fileSystem.GetLastWriteTimeUtc(ehnancedImagePath), treatmentRequiresTransparency);
  583. }
  584. }
  585. catch (Exception ex)
  586. {
  587. _logger.ErrorException("Error enhancing image", ex);
  588. }
  589. return new Tuple<string, DateTime, bool>(originalImagePath, dateModified, inputImageSupportsTransparency);
  590. }
  591. /// <summary>
  592. /// Gets the enhanced image internal.
  593. /// </summary>
  594. /// <param name="originalImagePath">The original image path.</param>
  595. /// <param name="item">The item.</param>
  596. /// <param name="imageType">Type of the image.</param>
  597. /// <param name="imageIndex">Index of the image.</param>
  598. /// <param name="supportedEnhancers">The supported enhancers.</param>
  599. /// <param name="cacheGuid">The cache unique identifier.</param>
  600. /// <returns>Task&lt;System.String&gt;.</returns>
  601. /// <exception cref="ArgumentNullException">
  602. /// originalImagePath
  603. /// or
  604. /// item
  605. /// </exception>
  606. private async Task<Tuple<string, bool>> GetEnhancedImageInternal(string originalImagePath,
  607. IHasMetadata item,
  608. ImageType imageType,
  609. int imageIndex,
  610. List<IImageEnhancer> supportedEnhancers,
  611. string cacheGuid)
  612. {
  613. if (string.IsNullOrEmpty(originalImagePath))
  614. {
  615. throw new ArgumentNullException("originalImagePath");
  616. }
  617. if (item == null)
  618. {
  619. throw new ArgumentNullException("item");
  620. }
  621. var treatmentRequiresTransparency = false;
  622. foreach (var enhancer in supportedEnhancers)
  623. {
  624. if (!treatmentRequiresTransparency)
  625. {
  626. treatmentRequiresTransparency = enhancer.GetEnhancedImageInfo(item, originalImagePath, imageType, imageIndex).RequiresTransparency;
  627. }
  628. }
  629. // All enhanced images are saved as png to allow transparency
  630. var cacheExtension = _imageEncoder.SupportedOutputFormats.Contains(ImageFormat.Webp) ?
  631. ".webp" :
  632. (treatmentRequiresTransparency ? ".png" : ".jpg");
  633. var enhancedImagePath = GetCachePath(EnhancedImageCachePath, cacheGuid + cacheExtension);
  634. // Check again in case of contention
  635. if (_fileSystem.FileExists(enhancedImagePath))
  636. {
  637. return new Tuple<string, bool>(enhancedImagePath, treatmentRequiresTransparency);
  638. }
  639. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(enhancedImagePath));
  640. var tmpPath = Path.Combine(_appPaths.TempDirectory, Path.ChangeExtension(Guid.NewGuid().ToString(), Path.GetExtension(enhancedImagePath)));
  641. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(tmpPath));
  642. await ExecuteImageEnhancers(supportedEnhancers, originalImagePath, tmpPath, item, imageType, imageIndex).ConfigureAwait(false);
  643. try
  644. {
  645. _fileSystem.CopyFile(tmpPath, enhancedImagePath, true);
  646. }
  647. catch
  648. {
  649. }
  650. return new Tuple<string, bool>(tmpPath, treatmentRequiresTransparency);
  651. }
  652. /// <summary>
  653. /// Executes the image enhancers.
  654. /// </summary>
  655. /// <param name="imageEnhancers">The image enhancers.</param>
  656. /// <param name="inputPath">The input path.</param>
  657. /// <param name="outputPath">The output path.</param>
  658. /// <param name="item">The item.</param>
  659. /// <param name="imageType">Type of the image.</param>
  660. /// <param name="imageIndex">Index of the image.</param>
  661. /// <returns>Task{EnhancedImage}.</returns>
  662. private async Task ExecuteImageEnhancers(IEnumerable<IImageEnhancer> imageEnhancers, string inputPath, string outputPath, IHasMetadata item, ImageType imageType, int imageIndex)
  663. {
  664. // Run the enhancers sequentially in order of priority
  665. foreach (var enhancer in imageEnhancers)
  666. {
  667. await enhancer.EnhanceImageAsync(item, inputPath, outputPath, imageType, imageIndex).ConfigureAwait(false);
  668. // Feed the output into the next enhancer as input
  669. inputPath = outputPath;
  670. }
  671. }
  672. /// <summary>
  673. /// Gets the cache path.
  674. /// </summary>
  675. /// <param name="path">The path.</param>
  676. /// <param name="uniqueName">Name of the unique.</param>
  677. /// <param name="fileExtension">The file extension.</param>
  678. /// <returns>System.String.</returns>
  679. /// <exception cref="System.ArgumentNullException">
  680. /// path
  681. /// or
  682. /// uniqueName
  683. /// or
  684. /// fileExtension
  685. /// </exception>
  686. public string GetCachePath(string path, string uniqueName, string fileExtension)
  687. {
  688. if (string.IsNullOrEmpty(path))
  689. {
  690. throw new ArgumentNullException("path");
  691. }
  692. if (string.IsNullOrEmpty(uniqueName))
  693. {
  694. throw new ArgumentNullException("uniqueName");
  695. }
  696. if (string.IsNullOrEmpty(fileExtension))
  697. {
  698. throw new ArgumentNullException("fileExtension");
  699. }
  700. var filename = uniqueName.GetMD5() + fileExtension;
  701. return GetCachePath(path, filename);
  702. }
  703. /// <summary>
  704. /// Gets the cache path.
  705. /// </summary>
  706. /// <param name="path">The path.</param>
  707. /// <param name="filename">The filename.</param>
  708. /// <returns>System.String.</returns>
  709. /// <exception cref="System.ArgumentNullException">
  710. /// path
  711. /// or
  712. /// filename
  713. /// </exception>
  714. public string GetCachePath(string path, string filename)
  715. {
  716. if (string.IsNullOrEmpty(path))
  717. {
  718. throw new ArgumentNullException("path");
  719. }
  720. if (string.IsNullOrEmpty(filename))
  721. {
  722. throw new ArgumentNullException("filename");
  723. }
  724. var prefix = filename.Substring(0, 1);
  725. path = Path.Combine(path, prefix);
  726. return Path.Combine(path, filename);
  727. }
  728. public void CreateImageCollage(ImageCollageOptions options)
  729. {
  730. _logger.Info("Creating image collage and saving to {0}", options.OutputPath);
  731. _imageEncoder.CreateImageCollage(options);
  732. _logger.Info("Completed creation of image collage and saved to {0}", options.OutputPath);
  733. }
  734. public List<IImageEnhancer> GetSupportedEnhancers(IHasMetadata item, ImageType imageType)
  735. {
  736. var list = new List<IImageEnhancer>();
  737. foreach (var i in ImageEnhancers)
  738. {
  739. try
  740. {
  741. if (i.Supports(item, imageType))
  742. {
  743. list.Add(i);
  744. }
  745. }
  746. catch (Exception ex)
  747. {
  748. _logger.ErrorException("Error in image enhancer: {0}", ex, i.GetType().Name);
  749. }
  750. }
  751. return list;
  752. }
  753. private bool _disposed;
  754. public void Dispose()
  755. {
  756. _disposed = true;
  757. var disposable = _imageEncoder as IDisposable;
  758. if (disposable != null)
  759. {
  760. disposable.Dispose();
  761. }
  762. GC.SuppressFinalize(this);
  763. }
  764. private void CheckDisposed()
  765. {
  766. if (_disposed)
  767. {
  768. throw new ObjectDisposedException(GetType().Name);
  769. }
  770. }
  771. }
  772. }