ImageProcessingOptions.cs 3.5 KB

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