ImageProcessingOptions.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Drawing;
  7. namespace MediaBrowser.Controller.Drawing
  8. {
  9. public class ImageProcessingOptions
  10. {
  11. public ImageProcessingOptions()
  12. {
  13. RequiresAutoOrientation = true;
  14. }
  15. public Guid ItemId { get; set; }
  16. public BaseItem Item { get; set; }
  17. public ItemImageInfo Image { get; set; }
  18. public int ImageIndex { get; set; }
  19. public bool CropWhiteSpace { get; set; }
  20. public int? Width { get; set; }
  21. public int? Height { get; set; }
  22. public int? MaxWidth { get; set; }
  23. public int? MaxHeight { get; set; }
  24. public int Quality { get; set; }
  25. public IImageEnhancer[] Enhancers { get; set; }
  26. public ImageFormat[] SupportedOutputFormats { get; set; }
  27. public bool AddPlayedIndicator { get; set; }
  28. public int? UnplayedCount { get; set; }
  29. public int? Blur { get; set; }
  30. public double PercentPlayed { get; set; }
  31. public string BackgroundColor { get; set; }
  32. public string ForegroundLayer { get; set; }
  33. public bool RequiresAutoOrientation { get; set; }
  34. private bool HasDefaultOptions(string originalImagePath)
  35. {
  36. return HasDefaultOptionsWithoutSize(originalImagePath) &&
  37. !Width.HasValue &&
  38. !Height.HasValue &&
  39. !MaxWidth.HasValue &&
  40. !MaxHeight.HasValue;
  41. }
  42. public bool HasDefaultOptions(string originalImagePath, ImageDimensions? size)
  43. {
  44. if (!size.HasValue)
  45. {
  46. return HasDefaultOptions(originalImagePath);
  47. }
  48. if (!HasDefaultOptionsWithoutSize(originalImagePath))
  49. {
  50. return false;
  51. }
  52. var sizeValue = size.Value;
  53. if (Width.HasValue && !sizeValue.Width.Equals(Width.Value))
  54. {
  55. return false;
  56. }
  57. if (Height.HasValue && !sizeValue.Height.Equals(Height.Value))
  58. {
  59. return false;
  60. }
  61. if (MaxWidth.HasValue && sizeValue.Width > MaxWidth.Value)
  62. {
  63. return false;
  64. }
  65. if (MaxHeight.HasValue && sizeValue.Height > MaxHeight.Value)
  66. {
  67. return false;
  68. }
  69. return true;
  70. }
  71. private bool HasDefaultOptionsWithoutSize(string originalImagePath)
  72. {
  73. return (Quality >= 90) &&
  74. IsFormatSupported(originalImagePath) &&
  75. !AddPlayedIndicator &&
  76. PercentPlayed.Equals(0) &&
  77. !UnplayedCount.HasValue &&
  78. !Blur.HasValue &&
  79. !CropWhiteSpace &&
  80. string.IsNullOrEmpty(BackgroundColor) &&
  81. string.IsNullOrEmpty(ForegroundLayer);
  82. }
  83. private bool IsFormatSupported(string originalImagePath)
  84. {
  85. var ext = Path.GetExtension(originalImagePath);
  86. return SupportedOutputFormats.Any(outputFormat => string.Equals(ext, "." + outputFormat, StringComparison.OrdinalIgnoreCase));
  87. }
  88. }
  89. }