BaseImageEnhancer.cs 4.4 KB

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