image.codec\lib\unified_info.mbt

75.5% · 40 / 53 points · 269 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/// # 统一信息 lib/unified_info
  3. 3///
  4. 4/// 编解码工具模块
  5. 5/// 编解码工具模块 Codec utility module
  6. 6// v246.0.0 统一 info API:整合基本信息 + 格式特定扩展信息
  7. 7
  8. 8///|
  9. 9/// 统一图像信息(跨格式通用字段)
  10. 10#derive(Eq, @debug.Debug)
  11. 11pub(all) struct UnifiedImageInfo {
  12. 12 format : @types.ImageFormat
  13. 13 format_name : String
  14. 14 width : Int32
  15. 15 height : Int32
  16. 16 channels : Int32
  17. 17 bit_depth : Int32
  18. 18 has_alpha : Bool
  19. 19}
  20. 20
  21. 21///|
  22. 22pub extend UnifiedImageInfo with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25pub extend UnifiedImageInfo with Eq::{not_equal, equal}
  26. 26
  27. 27///|
  28. 28/// 统一图像信息探测
  29. 29/// 自动检测格式并返回跨格式通用信息(尺寸/通道/位深/alpha)
  30. 30/// 未知格式 raise LoadError::UnsupportedFormat
  31. 31pub fn get_image_info(data : Bytes) -> UnifiedImageInfo raise @types.LoadError {
  32. 32● let format = detect_format(data)
  33. 33● let ext = @codec.get_image_info_extended(data)
  34. 34● let format_name = match format {
  35. 35● @types.ImageFormat::Png => "PNG"
  36. 36● @types.ImageFormat::Jpeg => "JPEG"
  37. 37● @types.ImageFormat::Bmp => "BMP"
  38. 38● @types.ImageFormat::Gif => "GIF"
  39. 39● @types.ImageFormat::Tga => "TGA"
  40. 40○ @types.ImageFormat::Psd => "PSD"
  41. 41● @types.ImageFormat::Hdr => "HDR"
  42. 42● @types.ImageFormat::Pnm => "PNM"
  43. 43● @types.ImageFormat::Qoi => "QOI"
  44. 44● @types.ImageFormat::Webp => "WebP"
  45. 45● @types.ImageFormat::Tiff => "TIFF"
  46. 46● @types.ImageFormat::Ico => "ICO"
  47. 47○ @types.ImageFormat::Cur => "CUR"
  48. 48● @types.ImageFormat::Icns => "ICNS"
  49. 49● @types.ImageFormat::Apng => "APNG"
  50. 50○ @types.ImageFormat::Avif => "AVIF"
  51. 51○ @types.ImageFormat::Heif => "HEIF"
  52. 52○ @types.ImageFormat::JpegXl => "JPEG-XL"
  53. 53○ @types.ImageFormat::Unknown => "unknown"
  54. 54○ @types.ImageFormat::.. => "unknown"
  55. 55 }
  56. 56● match ext {
  57. 57 @codec.ImageInfoExtended::Avif(e) =>
  58. 58○ UnifiedImageInfo::{
  59. 59 format,
  60. 60 format_name,
  61. 61 width: e.width,
  62. 62 height: e.height,
  63. 63 channels: e.num_channels,
  64. 64 bit_depth: e.bit_depth,
  65. 65 has_alpha: e.has_alpha,
  66. 66 }
  67. 67 @codec.ImageInfoExtended::Heif(e) =>
  68. 68○ UnifiedImageInfo::{
  69. 69 format,
  70. 70 format_name,
  71. 71 width: e.width,
  72. 72 height: e.height,
  73. 73 channels: e.num_channels,
  74. 74 bit_depth: e.bit_depth,
  75. 75 has_alpha: e.has_alpha,
  76. 76 }
  77. 77 @codec.ImageInfoExtended::JpegXl(e) =>
  78. 78○ UnifiedImageInfo::{
  79. 79 format,
  80. 80 format_name,
  81. 81 width: e.width,
  82. 82 height: e.height,
  83. 83 channels: e.num_channels,
  84. 84 bit_depth: e.bit_depth,
  85. 85 has_alpha: e.has_alpha,
  86. 86 }
  87. 87 @codec.ImageInfoExtended::Webp(e) =>
  88. 88● UnifiedImageInfo::{
  89. 89 format,
  90. 90 format_name,
  91. 91 width: e.width,
  92. 92 height: e.height,
  93. 93 channels: e.num_channels,
  94. 94 bit_depth: 8,
  95. 95 has_alpha: e.has_alpha,
  96. 96 }
  97. 97 @codec.ImageInfoExtended::Tiff(e) =>
  98. 98● UnifiedImageInfo::{
  99. 99 format,
  100. 100 format_name,
  101. 101 width: e.width,
  102. 102 height: e.height,
  103. 103 channels: e.num_channels,
  104. 104 bit_depth: e.bit_depth,
  105. 105 has_alpha: e.has_alpha,
  106. 106 }
  107. 107 @codec.ImageInfoExtended::Psd(e) =>
  108. 108○ UnifiedImageInfo::{
  109. 109 format,
  110. 110 format_name,
  111. 111 width: e.width,
  112. 112 height: e.height,
  113. 113 channels: e.num_channels,
  114. 114 bit_depth: e.bit_depth,
  115. 115 has_alpha: e.has_alpha,
  116. 116 }
  117. 117 @codec.ImageInfoExtended::Bmp(e) =>
  118. 118● UnifiedImageInfo::{
  119. 119 format,
  120. 120 format_name,
  121. 121 width: e.width,
  122. 122 height: e.height,
  123. 123 channels: e.num_channels,
  124. 124 bit_depth: e.bit_depth,
  125. 125 has_alpha: e.has_alpha,
  126. 126 }
  127. 127 @codec.ImageInfoExtended::Gif(e) =>
  128. 128● UnifiedImageInfo::{
  129. 129 format,
  130. 130 format_name,
  131. 131 width: e.width,
  132. 132 height: e.height,
  133. 133 channels: e.num_channels,
  134. 134 bit_depth: 8,
  135. 135 has_alpha: e.has_alpha,
  136. 136 }
  137. 137 @codec.ImageInfoExtended::Png(e) =>
  138. 138● UnifiedImageInfo::{
  139. 139 format,
  140. 140 format_name,
  141. 141 width: e.width,
  142. 142 height: e.height,
  143. 143 channels: e.num_channels,
  144. 144 bit_depth: e.bit_depth,
  145. 145 has_alpha: e.has_alpha,
  146. 146 }
  147. 147 @codec.ImageInfoExtended::Jpeg(e) =>
  148. 148● UnifiedImageInfo::{
  149. 149 format,
  150. 150 format_name,
  151. 151 width: e.width,
  152. 152 height: e.height,
  153. 153 channels: e.num_channels,
  154. 154 bit_depth: e.precision,
  155. 155 has_alpha: e.has_alpha,
  156. 156 }
  157. 157 @codec.ImageInfoExtended::Qoi(e) =>
  158. 158● UnifiedImageInfo::{
  159. 159 format,
  160. 160 format_name,
  161. 161 width: e.width,
  162. 162 height: e.height,
  163. 163 channels: e.num_channels,
  164. 164 bit_depth: 8,
  165. 165 has_alpha: e.has_alpha,
  166. 166 }
  167. 167 @codec.ImageInfoExtended::Tga(e) =>
  168. 168● UnifiedImageInfo::{
  169. 169 format,
  170. 170 format_name,
  171. 171 width: e.width,
  172. 172 height: e.height,
  173. 173 channels: e.num_channels,
  174. 174 bit_depth: e.bit_depth,
  175. 175 has_alpha: e.has_alpha,
  176. 176 }
  177. 177 @codec.ImageInfoExtended::Hdr(e) =>
  178. 178● UnifiedImageInfo::{
  179. 179 format,
  180. 180 format_name,
  181. 181 width: e.width,
  182. 182 height: e.height,
  183. 183 channels: e.num_channels,
  184. 184 bit_depth: 32,
  185. 185 has_alpha: e.has_alpha,
  186. 186 }
  187. 187 @codec.ImageInfoExtended::Pnm(e) =>
  188. 188● UnifiedImageInfo::{
  189. 189 format,
  190. 190 format_name,
  191. 191 width: e.width,
  192. 192 height: e.height,
  193. 193 channels: e.num_channels,
  194. 194 bit_depth: e.bit_depth,
  195. 195 has_alpha: e.has_alpha,
  196. 196 }
  197. 197 @codec.ImageInfoExtended::Ico(e) =>
  198. 198● UnifiedImageInfo::{
  199. 199 format,
  200. 200 format_name,
  201. 201 width: e.width,
  202. 202 height: e.height,
  203. 203 channels: e.num_channels,
  204. 204● bit_depth: if e.bit_depths.length() > 0 {
  205. 205● e.bit_depths[0]
  206. 206 } else {
  207. 207○ 0
  208. 208 },
  209. 209 has_alpha: e.has_alpha,
  210. 210 }
  211. 211 @codec.ImageInfoExtended::Cur(e) =>
  212. 212● UnifiedImageInfo::{
  213. 213 format,
  214. 214 format_name,
  215. 215 width: e.width,
  216. 216 height: e.height,
  217. 217 channels: e.num_channels,
  218. 218 bit_depth: 8,
  219. 219 has_alpha: e.has_alpha,
  220. 220 }
  221. 221 @codec.ImageInfoExtended::Icns(e) =>
  222. 222● UnifiedImageInfo::{
  223. 223 format,
  224. 224 format_name,
  225. 225 width: e.width,
  226. 226 height: e.height,
  227. 227 channels: e.num_channels,
  228. 228 bit_depth: 32,
  229. 229 has_alpha: e.has_alpha,
  230. 230 }
  231. 231 @codec.ImageInfoExtended::Apng(e) =>
  232. 232● UnifiedImageInfo::{
  233. 233 format,
  234. 234 format_name,
  235. 235 width: e.width,
  236. 236 height: e.height,
  237. 237 channels: e.num_channels,
  238. 238 bit_depth: e.bit_depth,
  239. 239 has_alpha: e.has_alpha,
  240. 240 }
  241. 241 @codec.ImageInfoExtended::.. =>
  242. 242○ UnifiedImageInfo::{
  243. 243 format,
  244. 244 format_name: "unknown",
  245. 245 width: 0,
  246. 246 height: 0,
  247. 247 channels: 0,
  248. 248 bit_depth: 0,
  249. 249 has_alpha: false,
  250. 250 }
  251. 251 }
  252. 252}
  253. 253
  254. 254///|
  255. 255/// 人类可读的图像信息摘要
  256. 256/// 返回格式如 "PNG 4x4 3ch 8bit"
  257. 257pub fn get_image_summary(data : Bytes) -> String raise @types.LoadError {
  258. 258● let info = get_image_info(data)
  259. 259 info.format_name +
  260. 260 " " +
  261. 261● info.width.to_string() +
  262. 262 "x" +
  263. 263● info.height.to_string() +
  264. 264 " " +
  265. 265● info.channels.to_string() +
  266. 266 "ch " +
  267. 267● info.bit_depth.to_string() +
  268. 268 "bit"
  269. 269}