BaseImageEnhancer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using MediaBrowser.Common.Logging;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Model.Drawing;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Drawing;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Controller.Providers
  10. {
  11. /// <summary>
  12. /// Class BaseImageEnhancer
  13. /// </summary>
  14. public abstract class BaseImageEnhancer : IDisposable
  15. {
  16. /// <summary>
  17. /// The logger
  18. /// </summary>
  19. private static readonly ILogger Logger = LogManager.GetLogger("ImageEnhancer");
  20. /// <summary>
  21. /// Return true only if the given image for the given item will be enhanced by this enhancer.
  22. /// </summary>
  23. /// <param name="item">The item.</param>
  24. /// <param name="imageType">Type of the image.</param>
  25. /// <returns><c>true</c> if this enhancer will enhance the supplied image for the supplied item, <c>false</c> otherwise</returns>
  26. public abstract bool Supports(BaseItem item, ImageType imageType);
  27. /// <summary>
  28. /// Gets the priority or order in which this enhancer should be run.
  29. /// </summary>
  30. /// <value>The priority.</value>
  31. public abstract MetadataProviderPriority Priority { get; }
  32. /// <summary>
  33. /// Return the date of the last configuration change affecting the provided baseitem and image type
  34. /// </summary>
  35. /// <param name="item">The item.</param>
  36. /// <param name="imageType">Type of the image.</param>
  37. /// <returns>Date of last config change</returns>
  38. public virtual DateTime LastConfigurationChange(BaseItem item, ImageType imageType)
  39. {
  40. return DateTime.MinValue;
  41. }
  42. /// <summary>
  43. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  44. /// </summary>
  45. public void Dispose()
  46. {
  47. Dispose(true);
  48. GC.SuppressFinalize(this);
  49. }
  50. /// <summary>
  51. /// Releases unmanaged and - optionally - managed resources.
  52. /// </summary>
  53. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  54. protected virtual void Dispose(bool dispose)
  55. {
  56. }
  57. /// <summary>
  58. /// Gets the size of the enhanced image.
  59. /// </summary>
  60. /// <param name="item">The item.</param>
  61. /// <param name="imageType">Type of the image.</param>
  62. /// <param name="imageIndex">Index of the image.</param>
  63. /// <param name="originalImageSize">Size of the original image.</param>
  64. /// <returns>ImageSize.</returns>
  65. public virtual ImageSize GetEnhancedImageSize(BaseItem item, ImageType imageType, int imageIndex, ImageSize originalImageSize)
  66. {
  67. return originalImageSize;
  68. }
  69. /// <summary>
  70. /// Enhances the supplied image and returns it
  71. /// </summary>
  72. /// <param name="item">The item.</param>
  73. /// <param name="originalImage">The original image.</param>
  74. /// <param name="imageType">Type of the image.</param>
  75. /// <param name="imageIndex">Index of the image.</param>
  76. /// <returns>Task{System.Drawing.Image}.</returns>
  77. protected abstract Task<Image> EnhanceImageAsyncInternal(BaseItem item, Image originalImage, ImageType imageType, int imageIndex);
  78. /// <summary>
  79. /// Enhances the image async.
  80. /// </summary>
  81. /// <param name="item">The item.</param>
  82. /// <param name="originalImage">The original image.</param>
  83. /// <param name="imageType">Type of the image.</param>
  84. /// <param name="imageIndex">Index of the image.</param>
  85. /// <returns>Task{Image}.</returns>
  86. /// <exception cref="System.ArgumentNullException"></exception>
  87. public async Task<Image> EnhanceImageAsync(BaseItem item, Image originalImage, ImageType imageType, int imageIndex)
  88. {
  89. if (item == null || originalImage == null)
  90. {
  91. throw new ArgumentNullException();
  92. }
  93. var typeName = GetType().Name;
  94. Logger.Debug("Running {0} for {1}", typeName, item.Path ?? item.Name ?? "--Unknown--");
  95. try
  96. {
  97. return await EnhanceImageAsyncInternal(item, originalImage, imageType, imageIndex).ConfigureAwait(false);
  98. }
  99. catch (Exception ex)
  100. {
  101. Logger.ErrorException("{0} failed enhancing {1}", ex, typeName, item.Name);
  102. throw;
  103. }
  104. }
  105. }
  106. }