ImageProcessingOptions.cs 3.4 KB

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