type.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package imagetype
  2. import (
  3. "fmt"
  4. )
  5. // Type represents an image type
  6. type (
  7. // Type represents an image type.
  8. Type int
  9. )
  10. // Supported image types
  11. var (
  12. // Unknown is a reserved type, it has index 0. We guarantee that index 0 won't be used
  13. // for any other type. This way, Unknown is a zero value for Type.
  14. Unknown Type = 0
  15. )
  16. // Mime returns the MIME type for the image type.
  17. func (t Type) Mime() string {
  18. desc := GetTypeDesc(t)
  19. if desc != nil {
  20. return desc.Mime
  21. }
  22. return "application/octet-stream"
  23. }
  24. // String returns the string representation of the image type.
  25. func (t Type) String() string {
  26. desc := GetTypeDesc(t)
  27. if desc != nil {
  28. return desc.String
  29. }
  30. return ""
  31. }
  32. // Ext returns the file extension for the image type.
  33. func (t Type) Ext() string {
  34. desc := GetTypeDesc(t)
  35. if desc != nil {
  36. return desc.Ext
  37. }
  38. return ""
  39. }
  40. // MarshalJSON implements the json.Marshaler interface for Type.
  41. func (t Type) MarshalJSON() ([]byte, error) {
  42. s := t.String()
  43. if s == "" {
  44. return []byte("null"), nil
  45. }
  46. return fmt.Appendf(nil, "%q", s), nil
  47. }
  48. // IsVector checks if the image type is a vector format.
  49. func (t Type) IsVector() bool {
  50. desc := GetTypeDesc(t)
  51. if desc != nil {
  52. return desc.IsVector
  53. }
  54. return false
  55. }
  56. // SupportsAlpha checks if the image type supports alpha transparency.
  57. func (t Type) SupportsAlpha() bool {
  58. desc := GetTypeDesc(t)
  59. if desc != nil {
  60. return desc.SupportsAlpha
  61. }
  62. return true
  63. }
  64. // SupportsAnimationLoad checks if the image type supports animation.
  65. func (t Type) SupportsAnimationLoad() bool {
  66. desc := GetTypeDesc(t)
  67. if desc != nil {
  68. return desc.SupportsAnimationLoad
  69. }
  70. return false
  71. }
  72. // SupportsAnimationSave checks if the image type supports saving animations.
  73. func (t Type) SupportsAnimationSave() bool {
  74. desc := GetTypeDesc(t)
  75. if desc != nil {
  76. return desc.SupportsAnimationSave
  77. }
  78. return false
  79. }
  80. // SupportsColourProfile checks if the image type supports colour profiles.
  81. func (t Type) SupportsColourProfile() bool {
  82. desc := GetTypeDesc(t)
  83. if desc != nil {
  84. return desc.SupportsColourProfile
  85. }
  86. return false
  87. }
  88. // SupportsQuality checks if the image type supports quality adjustments.
  89. func (t Type) SupportsQuality() bool {
  90. desc := GetTypeDesc(t)
  91. if desc != nil {
  92. return desc.SupportsQuality
  93. }
  94. return false
  95. }
  96. // SupportsThumbnail checks if the image type supports thumbnails.
  97. func (t Type) SupportsThumbnail() bool {
  98. desc := GetTypeDesc(t)
  99. if desc != nil {
  100. return desc.SupportsThumbnail
  101. }
  102. return false
  103. }