ImageProcessingOptions.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 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 (sizeValue.Width > FillWidth || sizeValue.Height > FillHeight)
  71. {
  72. return false;
  73. }
  74. return true;
  75. }
  76. private bool HasDefaultOptionsWithoutSize(string originalImagePath)
  77. {
  78. return (Quality >= 90) &&
  79. IsFormatSupported(originalImagePath) &&
  80. PercentPlayed.Equals(0) &&
  81. !UnplayedCount.HasValue &&
  82. !Blur.HasValue &&
  83. string.IsNullOrEmpty(BackgroundColor) &&
  84. string.IsNullOrEmpty(ForegroundLayer);
  85. }
  86. private bool IsFormatSupported(string originalImagePath)
  87. {
  88. var ext = Path.GetExtension(originalImagePath);
  89. ext = ext.Replace(".jpeg", ".jpg", StringComparison.OrdinalIgnoreCase);
  90. return SupportedOutputFormats.Any(outputFormat => string.Equals(ext, outputFormat.GetExtension(), StringComparison.OrdinalIgnoreCase));
  91. }
  92. }
  93. }