2
0

ImageProcessingOptions.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Controller.Entities;
  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 IReadOnlyCollection<ImageFormat> SupportedOutputFormats { get; set; }
  26. public bool AddPlayedIndicator { get; set; }
  27. public int? UnplayedCount { get; set; }
  28. public int? Blur { get; set; }
  29. public double PercentPlayed { get; set; }
  30. public string BackgroundColor { get; set; }
  31. public string ForegroundLayer { get; set; }
  32. public bool RequiresAutoOrientation { get; set; }
  33. private bool HasDefaultOptions(string originalImagePath)
  34. {
  35. return HasDefaultOptionsWithoutSize(originalImagePath) &&
  36. !Width.HasValue &&
  37. !Height.HasValue &&
  38. !MaxWidth.HasValue &&
  39. !MaxHeight.HasValue;
  40. }
  41. public bool HasDefaultOptions(string originalImagePath, ImageDimensions? size)
  42. {
  43. if (!size.HasValue)
  44. {
  45. return HasDefaultOptions(originalImagePath);
  46. }
  47. if (!HasDefaultOptionsWithoutSize(originalImagePath))
  48. {
  49. return false;
  50. }
  51. var sizeValue = size.Value;
  52. if (Width.HasValue && !sizeValue.Width.Equals(Width.Value))
  53. {
  54. return false;
  55. }
  56. if (Height.HasValue && !sizeValue.Height.Equals(Height.Value))
  57. {
  58. return false;
  59. }
  60. if (MaxWidth.HasValue && sizeValue.Width > MaxWidth.Value)
  61. {
  62. return false;
  63. }
  64. if (MaxHeight.HasValue && sizeValue.Height > MaxHeight.Value)
  65. {
  66. return false;
  67. }
  68. return true;
  69. }
  70. private bool HasDefaultOptionsWithoutSize(string originalImagePath)
  71. {
  72. return (Quality >= 90) &&
  73. IsFormatSupported(originalImagePath) &&
  74. !AddPlayedIndicator &&
  75. PercentPlayed.Equals(0) &&
  76. !UnplayedCount.HasValue &&
  77. !Blur.HasValue &&
  78. !CropWhiteSpace &&
  79. string.IsNullOrEmpty(BackgroundColor) &&
  80. string.IsNullOrEmpty(ForegroundLayer);
  81. }
  82. private bool IsFormatSupported(string originalImagePath)
  83. {
  84. var ext = Path.GetExtension(originalImagePath);
  85. return SupportedOutputFormats.Any(outputFormat => string.Equals(ext, "." + outputFormat, StringComparison.OrdinalIgnoreCase));
  86. }
  87. }
  88. }