image.codec\codec\codec_trait.mbt

95.2% · 178 / 187 points · 277 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 编解码特征 Codec Trait
  3. 3///
  4. 4/// 编解码统一接口定义
  5. 5/// 统一编解码接口定义 Unified codec interface definitions
  6. 6
  7. 7///|
  8. 8/// 编解码器统一接口 trait
  9. 9///
  10. 10/// 为 ImageFormat 枚举实现,提供统一的 encode / decode 方法
  11. 11pub trait Codec {
  12. 12 fn format_name(Self) -> String
  13. 13 fn can_encode(Self) -> Bool
  14. 14 fn can_decode(Self) -> Bool
  15. 15 fn is_lossless(Self) -> Bool
  16. 16 fn encode(Self, @types.AnyImage) -> Bytes raise @types.LoadError
  17. 17 fn decode(Self, Bytes) -> @types.AnyImage raise @types.LoadError
  18. 18}
  19. 19
  20. 20///|
  21. 21pub impl Codec for @types.ImageFormat with fn format_name(self) {
  22. 22● match self {
  23. 23● @types.ImageFormat::Png => "PNG"
  24. 24● @types.ImageFormat::Jpeg => "JPEG"
  25. 25● @types.ImageFormat::Bmp => "BMP"
  26. 26● @types.ImageFormat::Gif => "GIF"
  27. 27● @types.ImageFormat::Tga => "TGA"
  28. 28● @types.ImageFormat::Psd => "PSD"
  29. 29● @types.ImageFormat::Hdr => "HDR"
  30. 30● @types.ImageFormat::Pnm => "PNM"
  31. 31● @types.ImageFormat::Qoi => "QOI"
  32. 32● @types.ImageFormat::Webp => "WebP"
  33. 33● @types.ImageFormat::Tiff => "TIFF"
  34. 34● @types.ImageFormat::Ico => "ICO"
  35. 35● @types.ImageFormat::Cur => "CUR"
  36. 36● @types.ImageFormat::Icns => "ICNS"
  37. 37● @types.ImageFormat::Apng => "APNG"
  38. 38● @types.ImageFormat::Avif => "AVIF"
  39. 39● @types.ImageFormat::Heif => "HEIF"
  40. 40● @types.ImageFormat::JpegXl => "JPEG-XL"
  41. 41● @types.ImageFormat::Unknown => "Unknown"
  42. 42○ @types.ImageFormat::.. => "Unknown"
  43. 43 }
  44. 44}
  45. 45
  46. 46///|
  47. 47pub impl Codec for @types.ImageFormat with fn can_encode(self) {
  48. 48● match self {
  49. 49● @types.ImageFormat::Png => true
  50. 50● @types.ImageFormat::Jpeg => true
  51. 51● @types.ImageFormat::Bmp => true
  52. 52● @types.ImageFormat::Gif => true
  53. 53● @types.ImageFormat::Tga => true
  54. 54● @types.ImageFormat::Psd => false
  55. 55● @types.ImageFormat::Hdr => true
  56. 56● @types.ImageFormat::Pnm => true
  57. 57● @types.ImageFormat::Qoi => true
  58. 58● @types.ImageFormat::Webp => true
  59. 59● @types.ImageFormat::Tiff => true
  60. 60● @types.ImageFormat::Ico => true
  61. 61● @types.ImageFormat::Cur => true
  62. 62● @types.ImageFormat::Icns => true
  63. 63● @types.ImageFormat::Apng => true
  64. 64● @types.ImageFormat::Avif => false
  65. 65● @types.ImageFormat::Heif => false
  66. 66● @types.ImageFormat::JpegXl => false
  67. 67● @types.ImageFormat::Unknown => false
  68. 68○ @types.ImageFormat::.. => false
  69. 69 }
  70. 70}
  71. 71
  72. 72///|
  73. 73pub impl Codec for @types.ImageFormat with fn can_decode(self) {
  74. 74● match self {
  75. 75● @types.ImageFormat::Unknown => false
  76. 76● _ => true
  77. 77 }
  78. 78}
  79. 79
  80. 80///|
  81. 81pub impl Codec for @types.ImageFormat with fn is_lossless(self) {
  82. 82● match self {
  83. 83● @types.ImageFormat::Jpeg => false
  84. 84● @types.ImageFormat::Webp => false
  85. 85● @types.ImageFormat::Avif => false
  86. 86● @types.ImageFormat::Heif => false
  87. 87● @types.ImageFormat::JpegXl => false
  88. 88● _ => true
  89. 89 }
  90. 90}
  91. 91
  92. 92///|
  93. 93#warnings("-deprecated")
  94. 94pub impl Codec for @types.ImageFormat with fn encode(self, img) {
  95. 95● match self {
  96. 96● @types.ImageFormat::Png => encode_png_pure(@types.any_image_to_u8(img))
  97. 97● @types.ImageFormat::Jpeg => encode_jpeg_pure(@types.any_image_to_u8(img))
  98. 98● @types.ImageFormat::Bmp => encode_bmp_pure(@types.any_image_to_u8(img))
  99. 99● @types.ImageFormat::Gif => encode_gif_pure(@types.any_image_to_u8(img))
  100. 100● @types.ImageFormat::Tga => encode_tga_pure(@types.any_image_to_u8(img))
  101. 101 @types.ImageFormat::Hdr =>
  102. 102● match img {
  103. 103○ @types.AnyImage::F32(im) => encode_hdr_pure(im)
  104. 104● _ => encode_hdr_pure(@types.hdr_from_u8(@types.any_image_to_u8(img)))
  105. 105 }
  106. 106● @types.ImageFormat::Pnm => encode_pnm_pure(@types.any_image_to_u8(img))
  107. 107● @types.ImageFormat::Qoi => encode_qoi_pure(@types.any_image_to_u8(img))
  108. 108 @types.ImageFormat::Webp =>
  109. 109● encode_webp_lossy_pure(@types.any_image_to_u8(img))
  110. 110● @types.ImageFormat::Tiff => encode_tiff_pure(@types.any_image_to_u8(img))
  111. 111● @types.ImageFormat::Ico => encode_ico_pure(@types.any_image_to_u8(img))
  112. 112● @types.ImageFormat::Cur => encode_cur_pure(@types.any_image_to_u8(img))
  113. 113● @types.ImageFormat::Icns => encode_icns_pure(@types.any_image_to_u8(img))
  114. 114● @types.ImageFormat::Apng => {
  115. 115● let im = @types.any_image_to_u8(img)
  116. 116● encode_apng_pure(@types.PngAnimation::{ frames: [im], delays: [0], })
  117. 117 }
  118. 118 _ =>
  119. 119● raise @types.LoadError::EncodeFailed("Format not supported for encoding")
  120. 120 }
  121. 121}
  122. 122
  123. 123///|
  124. 124pub impl Codec for @types.ImageFormat with fn decode(self, data) {
  125. 125● match self {
  126. 126● @types.ImageFormat::Png => @types.AnyImage::U8(decode_png_pure(data))
  127. 127○ @types.ImageFormat::Jpeg => @types.AnyImage::U8(decode_jpeg_pure(data))
  128. 128● @types.ImageFormat::Bmp => @types.AnyImage::U8(decode_bmp_pure(data))
  129. 129● @types.ImageFormat::Gif => @types.AnyImage::U8(decode_gif_pure(data))
  130. 130● @types.ImageFormat::Tga => @types.AnyImage::U8(decode_tga_pure(data))
  131. 131● @types.ImageFormat::Psd => @types.AnyImage::U8(decode_psd_pure(data))
  132. 132● @types.ImageFormat::Hdr => @types.AnyImage::F32(decode_hdr_pure(data))
  133. 133● @types.ImageFormat::Pnm => @types.AnyImage::U8(decode_pnm_pure(data))
  134. 134● @types.ImageFormat::Qoi => @types.AnyImage::U8(decode_qoi_pure(data))
  135. 135○ @types.ImageFormat::Webp => @types.AnyImage::U8(decode_webp_pure(data))
  136. 136● @types.ImageFormat::Tiff => @types.AnyImage::U8(decode_tiff_pure(data))
  137. 137● @types.ImageFormat::Ico => @types.AnyImage::U8(decode_ico_pure(data))
  138. 138● @types.ImageFormat::Cur => @types.AnyImage::U8(decode_cur_pure(data))
  139. 139● @types.ImageFormat::Icns => @types.AnyImage::U8(decode_icns_pure(data))
  140. 140● @types.ImageFormat::Apng => {
  141. 141● let anim = decode_apng_pure(data)
  142. 142● match anim.frames {
  143. 143● [first, ..] => @types.AnyImage::U8(first)
  144. 144○ [] => raise @types.LoadError::DecodeFailed("APNG has no frames")
  145. 145 }
  146. 146 }
  147. 147● @types.ImageFormat::Avif => @types.AnyImage::U8(avif_decode(data))
  148. 148● @types.ImageFormat::Heif => @types.AnyImage::U8(heif_decode(data))
  149. 149● @types.ImageFormat::JpegXl => @types.AnyImage::U8(jpegxl_decode(data))
  150. 150 @types.ImageFormat::Unknown =>
  151. 151● raise @types.LoadError::DecodeFailed("Unknown format")
  152. 152 @types.ImageFormat::.. =>
  153. 153○ raise @types.LoadError::DecodeFailed("Unsupported format")
  154. 154 }
  155. 155}
  156. 156
  157. 157///|
  158. 158
  159. 159///|
  160. 160/// 通过 Codec trait 统一解码:自动检测格式并解码
  161. 161pub fn codec_decode_auto(
  162. 162 data : Bytes,
  163. 163) -> @types.AnyImage raise @types.LoadError {
  164. 164● let format = codec_detect_format(data)
  165. 165● Codec::decode(format, data)
  166. 166}
  167. 167
  168. 168///|
  169. 169/// 检测图像格式(基于 magic bytes)
  170. 170pub fn codec_detect_format(data : Bytes) -> @types.ImageFormat {
  171. 171● if data.length() < 2 {
  172. 172● return @types.ImageFormat::Unknown
  173. 173 }
  174. 174● let b0 = data[0].to_int()
  175. 175● let b1 = data[1].to_int()
  176. 176 // PNG: 89 50 4E 47
  177. 177 if b0 == 0x89 && b1 == 0x50 {
  178. 178● return @types.ImageFormat::Png
  179. 179 }
  180. 180 // JPEG: FF D8
  181. 181 if b0 == 0xFF && b1 == 0xD8 {
  182. 182● return @types.ImageFormat::Jpeg
  183. 183 }
  184. 184 // GIF: 47 49 46
  185. 185 if b0 == 0x47 && b1 == 0x49 {
  186. 186● return @types.ImageFormat::Gif
  187. 187 }
  188. 188 // BMP: 42 4D
  189. 189 if b0 == 0x42 && b1 == 0x4D {
  190. 190● return @types.ImageFormat::Bmp
  191. 191 }
  192. 192 // QOI: 71 6F 69 66
  193. 193 if b0 == 0x71 && b1 == 0x6F {
  194. 194● return @types.ImageFormat::Qoi
  195. 195 }
  196. 196 // WebP: 52 49 46 46 ... 57 45 42 50
  197. 197● if b0 == 0x52 && b1 == 0x49 && data.length() >= 12 {
  198. 198● let b8 = data[8].to_int()
  199. 199● let b9 = data[9].to_int()
  200. 200● let b10 = data[10].to_int()
  201. 201● let b11 = data[11].to_int()
  202. 202 if b8 == 0x57 && b9 == 0x45 && b10 == 0x42 && b11 == 0x50 {
  203. 203● return @types.ImageFormat::Webp
  204. 204 }
  205. 205 }
  206. 206 // TIFF: 49 49 or 4D 4D
  207. 207 if (b0 == 0x49 && b1 == 0x49) || (b0 == 0x4D && b1 == 0x4D) {
  208. 208● return @types.ImageFormat::Tiff
  209. 209 }
  210. 210 // ICO/CUR:交给各自的严格探测(仅看前 4 字节会误判 24/32-bit TGA)
  211. 211● if is_ico_format(data) {
  212. 212● return @types.ImageFormat::Ico
  213. 213 }
  214. 214● if is_cur_format(data) {
  215. 215● return @types.ImageFormat::Cur
  216. 216 }
  217. 217 // TGA:无强魔数,按头字段(cmaptype/image_type/bpp)判定
  218. 218● if is_tga_format(data) {
  219. 219● return @types.ImageFormat::Tga
  220. 220 }
  221. 221 // HDR: #?RADIANCE or #?RGBE
  222. 222 if b0 == 0x23 && b1 == 0x3F {
  223. 223● return @types.ImageFormat::Hdr
  224. 224 }
  225. 225 // PNM: P1-P6
  226. 226 if b0 == 0x50 && b1 >= 0x31 && b1 <= 0x36 {
  227. 227● return @types.ImageFormat::Pnm
  228. 228 }
  229. 229 // PSD: 38 42 50 53
  230. 230 if b0 == 0x38 && b1 == 0x42 {
  231. 231● return @types.ImageFormat::Psd
  232. 232 }
  233. 233 // ICNS: 69 63 6E 73
  234. 234 if b0 == 0x69 && b1 == 0x63 {
  235. 235● return @types.ImageFormat::Icns
  236. 236 }
  237. 237 // AVIF/HEIF: ftyp box
  238. 238● if b0 == 0x00 && data.length() >= 12 {
  239. 239● let b4 = data[4].to_int()
  240. 240● let b5 = data[5].to_int()
  241. 241● let b6 = data[6].to_int()
  242. 242● let b7 = data[7].to_int()
  243. 243 if b4 == 0x66 && b5 == 0x74 && b6 == 0x79 && b7 == 0x70 {
  244. 244● let b8 = data[8].to_int()
  245. 245● let b9 = data[9].to_int()
  246. 246● let b10 = data[10].to_int()
  247. 247● let b11 = data[11].to_int()
  248. 248 // avif/mif/avis
  249. 249 if (b8 == 0x61 && b9 == 0x76 && b10 == 0x69 && b11 == 0x66) ||
  250. 250 (b8 == 0x6D && b9 == 0x69 && b10 == 0x66 && b11 == 0x31) ||
  251. 251 (b8 == 0x61 && b9 == 0x76 && b10 == 0x69 && b11 == 0x73) {
  252. 252● return @types.ImageFormat::Avif
  253. 253 }
  254. 254 // heic/heix/heif/mif
  255. 255 if (b8 == 0x68 && b9 == 0x65 && b10 == 0x69 && b11 == 0x63) ||
  256. 256 (b8 == 0x68 && b9 == 0x65 && b10 == 0x69 && b11 == 0x78) ||
  257. 257 (b8 == 0x68 && b9 == 0x65 && b10 == 0x69 && b11 == 0x66) ||
  258. 258 (b8 == 0x6D && b9 == 0x69 && b10 == 0x66 && b11 == 0x31) {
  259. 259● return @types.ImageFormat::Heif
  260. 260 }
  261. 261 }
  262. 262 }
  263. 263 // JPEG-XL: FF 0A (codestream) or 00 00 00 0C 4A 58 4C 20 (container)
  264. 264 if b0 == 0xFF && b1 == 0x0A {
  265. 265● return @types.ImageFormat::JpegXl
  266. 266 }
  267. 267● if b0 == 0x00 && b1 == 0x00 && data.length() >= 8 {
  268. 268● let b4 = data[4].to_int()
  269. 269● let b5 = data[5].to_int()
  270. 270● let b6 = data[6].to_int()
  271. 271● let b7 = data[7].to_int()
  272. 272 if b4 == 0x4A && b5 == 0x58 && b6 == 0x4C && b7 == 0x20 {
  273. 273● return @types.ImageFormat::JpegXl
  274. 274 }
  275. 275 }
  276. 276 @types.ImageFormat::Unknown
  277. 277}