defs.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package imagetype
  2. var (
  3. JPEG = RegisterType(&TypeDesc{
  4. String: "jpeg",
  5. Ext: ".jpg",
  6. Mime: "image/jpeg",
  7. IsVector: false,
  8. SupportsAlpha: false,
  9. SupportsColourProfile: true,
  10. SupportsQuality: true,
  11. SupportsAnimationLoad: false,
  12. SupportsAnimationSave: false,
  13. SupportsThumbnail: false,
  14. })
  15. JXL = RegisterType(&TypeDesc{
  16. String: "jxl",
  17. Ext: ".jxl",
  18. Mime: "image/jxl",
  19. IsVector: false,
  20. SupportsAlpha: true,
  21. SupportsColourProfile: true,
  22. SupportsQuality: true,
  23. SupportsAnimationLoad: true,
  24. SupportsAnimationSave: false,
  25. SupportsThumbnail: false,
  26. })
  27. PNG = RegisterType(&TypeDesc{
  28. String: "png",
  29. Ext: ".png",
  30. Mime: "image/png",
  31. IsVector: false,
  32. SupportsAlpha: true,
  33. SupportsColourProfile: true,
  34. SupportsQuality: false,
  35. SupportsAnimationLoad: false,
  36. SupportsAnimationSave: false,
  37. SupportsThumbnail: false,
  38. })
  39. WEBP = RegisterType(&TypeDesc{
  40. String: "webp",
  41. Ext: ".webp",
  42. Mime: "image/webp",
  43. IsVector: false,
  44. SupportsAlpha: true,
  45. SupportsColourProfile: true,
  46. SupportsQuality: true,
  47. SupportsAnimationLoad: true,
  48. SupportsAnimationSave: true,
  49. SupportsThumbnail: false,
  50. })
  51. GIF = RegisterType(&TypeDesc{
  52. String: "gif",
  53. Ext: ".gif",
  54. Mime: "image/gif",
  55. IsVector: false,
  56. SupportsAlpha: true,
  57. SupportsColourProfile: false,
  58. SupportsQuality: false,
  59. SupportsAnimationLoad: true,
  60. SupportsAnimationSave: true,
  61. SupportsThumbnail: false,
  62. })
  63. ICO = RegisterType(&TypeDesc{
  64. String: "ico",
  65. Ext: ".ico",
  66. Mime: "image/x-icon",
  67. IsVector: false,
  68. SupportsAlpha: true,
  69. SupportsColourProfile: false,
  70. SupportsQuality: false,
  71. SupportsAnimationLoad: false,
  72. SupportsAnimationSave: false,
  73. SupportsThumbnail: false,
  74. })
  75. SVG = RegisterType(&TypeDesc{
  76. String: "svg",
  77. Ext: ".svg",
  78. Mime: "image/svg+xml",
  79. IsVector: true,
  80. SupportsAlpha: true,
  81. SupportsColourProfile: false,
  82. SupportsQuality: false,
  83. SupportsAnimationLoad: false,
  84. SupportsAnimationSave: false,
  85. SupportsThumbnail: false,
  86. })
  87. HEIC = RegisterType(&TypeDesc{
  88. String: "heic",
  89. Ext: ".heic",
  90. Mime: "image/heif",
  91. IsVector: false,
  92. SupportsAlpha: true,
  93. SupportsColourProfile: true,
  94. SupportsQuality: true,
  95. SupportsAnimationLoad: false,
  96. SupportsAnimationSave: false,
  97. SupportsThumbnail: true,
  98. })
  99. AVIF = RegisterType(&TypeDesc{
  100. String: "avif",
  101. Ext: ".avif",
  102. Mime: "image/avif",
  103. IsVector: false,
  104. SupportsAlpha: true,
  105. SupportsColourProfile: true,
  106. SupportsQuality: true,
  107. SupportsAnimationLoad: false,
  108. SupportsAnimationSave: false,
  109. SupportsThumbnail: true,
  110. })
  111. BMP = RegisterType(&TypeDesc{
  112. String: "bmp",
  113. Ext: ".bmp",
  114. Mime: "image/bmp",
  115. IsVector: false,
  116. SupportsAlpha: true,
  117. SupportsColourProfile: false,
  118. SupportsQuality: false,
  119. SupportsAnimationLoad: false,
  120. SupportsAnimationSave: false,
  121. SupportsThumbnail: false,
  122. })
  123. TIFF = RegisterType(&TypeDesc{
  124. String: "tiff",
  125. Ext: ".tiff",
  126. Mime: "image/tiff",
  127. IsVector: false,
  128. SupportsAlpha: true,
  129. SupportsColourProfile: true,
  130. SupportsQuality: true,
  131. SupportsAnimationLoad: false,
  132. SupportsAnimationSave: false,
  133. SupportsThumbnail: false,
  134. })
  135. )
  136. // init registers default magic bytes for common image formats
  137. func init() {
  138. // NOTE: we cannot be 100% sure of image type until we fully decode it. This is especially true
  139. // for "naked" jxl (0xff 0x0a). There is no other way to ensure this is a JXL file, except to fully
  140. // decode it. Two bytes are too few to reliably identify the format. The same applies to ICO.
  141. // JPEG magic bytes
  142. RegisterMagicBytes(JPEG, []byte("\xff\xd8"))
  143. // JXL magic bytes
  144. RegisterMagicBytes(JXL, []byte{0xff, 0x0a}) // JXL codestream (can't use string due to 0x0a)
  145. RegisterMagicBytes(JXL, []byte{0x00, 0x00, 0x00, 0x0C, 0x4A, 0x58, 0x4C, 0x20, 0x0D, 0x0A, 0x87, 0x0A}) // JXL container (has null bytes)
  146. // PNG magic bytes
  147. RegisterMagicBytes(PNG, []byte("\x89PNG\r\n\x1a\n"))
  148. // WEBP magic bytes (RIFF container with WEBP fourcc) - using wildcard for size
  149. RegisterMagicBytes(WEBP, []byte("RIFF????WEBP"))
  150. // GIF magic bytes
  151. RegisterMagicBytes(GIF, []byte("GIF8?a"))
  152. // ICO magic bytes
  153. RegisterMagicBytes(ICO, []byte{0, 0, 1, 0}) // ICO (has null bytes)
  154. // HEIC/HEIF magic bytes with wildcards for size
  155. RegisterMagicBytes(HEIC, []byte("????ftypheic"),
  156. []byte("????ftypheix"),
  157. []byte("????ftyphevc"),
  158. []byte("????ftypheim"),
  159. []byte("????ftypheis"),
  160. []byte("????ftyphevm"),
  161. []byte("????ftyphevs"),
  162. []byte("????ftypmif1"))
  163. // AVIF magic bytes
  164. RegisterMagicBytes(AVIF, []byte("????ftypavif"))
  165. // BMP magic bytes
  166. RegisterMagicBytes(BMP, []byte("BM"))
  167. // TIFF magic bytes (little-endian and big-endian)
  168. RegisterMagicBytes(TIFF, []byte("II*\x00"), []byte("MM\x00*")) // Big-Endian, Little-endian
  169. }