image.core\types\traits.mbt

89.5% · 77 / 86 points · 330 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/// # 类型特征 types/traits
  3. 3///
  4. 4/// 图像处理库子模块 Image processing library submodule
  5. 5
  6. 6///|
  7. 7/// 图像统一接口 trait,抽象 8-bit / 16-bit / float 三种像素精度
  8. 8///
  9. 9/// 所有图像类型(Image / Image16 / ImageF / AnyImage)均实现此 trait,
  10. 10/// 允许泛型算法以统一方式访问图像几何信息与像素数据
  11. 11pub(open) trait ImageLike {
  12. 12 fn width(Self) -> Int32
  13. 13 fn height(Self) -> Int32
  14. 14 fn channels(Self) -> Int32
  15. 15 fn data(Self) -> Bytes
  16. 16 fn bytes_per_pixel(Self) -> Int32
  17. 17 fn pixel_count(Self) -> Int32 = _
  18. 18 fn byte_count(Self) -> Int32 = _
  19. 19 fn is_grayscale(Self) -> Bool = _
  20. 20 fn is_rgb(Self) -> Bool = _
  21. 21 fn is_rgba(Self) -> Bool = _
  22. 22}
  23. 23
  24. 24///|
  25. 25impl ImageLike with fn pixel_count(self) {
  26. 26● ImageLike::width(self) * ImageLike::height(self) * ImageLike::channels(self)
  27. 27}
  28. 28
  29. 29///|
  30. 30impl ImageLike with fn byte_count(self) {
  31. 31● ImageLike::pixel_count(self) * ImageLike::bytes_per_pixel(self)
  32. 32}
  33. 33
  34. 34///|
  35. 35impl ImageLike with fn is_grayscale(self) {
  36. 36● ImageLike::channels(self) == 1
  37. 37}
  38. 38
  39. 39///|
  40. 40impl ImageLike with fn is_rgb(self) {
  41. 41● ImageLike::channels(self) == 3
  42. 42}
  43. 43
  44. 44///|
  45. 45impl ImageLike with fn is_rgba(self) {
  46. 46● ImageLike::channels(self) == 4
  47. 47}
  48. 48
  49. 49// Image (8-bit) 实现 ImageLike
  50. 50
  51. 51///|
  52. 52pub impl ImageLike for Image with fn width(self) {
  53. 53● self.width
  54. 54}
  55. 55
  56. 56///|
  57. 57pub impl ImageLike for Image with fn height(self) {
  58. 58● self.height
  59. 59}
  60. 60
  61. 61///|
  62. 62pub impl ImageLike for Image with fn channels(self) {
  63. 63● self.channels
  64. 64}
  65. 65
  66. 66///|
  67. 67pub impl ImageLike for Image with fn data(self) {
  68. 68○ self.data
  69. 69}
  70. 70
  71. 71///|
  72. 72pub impl ImageLike for Image with fn bytes_per_pixel(_) {
  73. 73● 1
  74. 74}
  75. 75
  76. 76///|
  77. 77pub extend Image with ImageLike::{
  78. 78 width,
  79. 79 height,
  80. 80 channels,
  81. 81 data,
  82. 82 bytes_per_pixel,
  83. 83 pixel_count,
  84. 84 byte_count,
  85. 85 is_grayscale,
  86. 86 is_rgb,
  87. 87 is_rgba,
  88. 88}
  89. 89
  90. 90// Image16 (16-bit) 实现 ImageLike
  91. 91
  92. 92///|
  93. 93pub impl ImageLike for Image16 with fn width(self) {
  94. 94● self.width
  95. 95}
  96. 96
  97. 97///|
  98. 98pub impl ImageLike for Image16 with fn height(self) {
  99. 99● self.height
  100. 100}
  101. 101
  102. 102///|
  103. 103pub impl ImageLike for Image16 with fn channels(self) {
  104. 104● self.channels
  105. 105}
  106. 106
  107. 107///|
  108. 108pub impl ImageLike for Image16 with fn data(self) {
  109. 109○ self.data
  110. 110}
  111. 111
  112. 112///|
  113. 113pub impl ImageLike for Image16 with fn bytes_per_pixel(_) {
  114. 114● 2
  115. 115}
  116. 116
  117. 117///|
  118. 118pub extend Image16 with ImageLike::{
  119. 119 width,
  120. 120 height,
  121. 121 channels,
  122. 122 data,
  123. 123 bytes_per_pixel,
  124. 124 pixel_count,
  125. 125 byte_count,
  126. 126 is_grayscale,
  127. 127 is_rgb,
  128. 128 is_rgba,
  129. 129}
  130. 130
  131. 131// ImageF (32-bit float) 实现 ImageLike
  132. 132
  133. 133///|
  134. 134pub impl ImageLike for ImageF with fn width(self) {
  135. 135● self.width
  136. 136}
  137. 137
  138. 138///|
  139. 139pub impl ImageLike for ImageF with fn height(self) {
  140. 140● self.height
  141. 141}
  142. 142
  143. 143///|
  144. 144pub impl ImageLike for ImageF with fn channels(self) {
  145. 145● self.channels
  146. 146}
  147. 147
  148. 148///|
  149. 149pub impl ImageLike for ImageF with fn data(self) {
  150. 150○ self.data
  151. 151}
  152. 152
  153. 153///|
  154. 154pub impl ImageLike for ImageF with fn bytes_per_pixel(_) {
  155. 155● 4
  156. 156}
  157. 157
  158. 158///|
  159. 159pub extend ImageF with ImageLike::{
  160. 160 width,
  161. 161 height,
  162. 162 channels,
  163. 163 data,
  164. 164 bytes_per_pixel,
  165. 165 pixel_count,
  166. 166 byte_count,
  167. 167 is_grayscale,
  168. 168 is_rgb,
  169. 169 is_rgba,
  170. 170}
  171. 171
  172. 172///|
  173. 173/// 统一图像容器,包装三种像素精度的图像
  174. 174///
  175. 175/// 用于需要统一处理不同精度图像的场景,如编解码器接口
  176. 176/// `#non_exhaustive` 表示未来可能添加新构造器
  177. 177#derive(Eq, @debug.Debug)
  178. 178#non_exhaustive
  179. 179pub(all) enum AnyImage {
  180. 180 U8(Image)
  181. 181 U16(Image16)
  182. 182 F32(ImageF)
  183. 183}
  184. 184
  185. 185///|
  186. 186pub extend AnyImage with @debug.Debug::{to_repr}
  187. 187
  188. 188///|
  189. 189pub extend AnyImage with Eq::{not_equal, equal}
  190. 190
  191. 191// AnyImage 实现 ImageLike
  192. 192
  193. 193///|
  194. 194pub impl ImageLike for AnyImage with fn width(self) {
  195. 195● match self {
  196. 196● U8(img) => img.width
  197. 197● U16(img) => img.width
  198. 198● F32(img) => img.width
  199. 199 }
  200. 200}
  201. 201
  202. 202///|
  203. 203pub impl ImageLike for AnyImage with fn height(self) {
  204. 204● match self {
  205. 205● U8(img) => img.height
  206. 206○ U16(img) => img.height
  207. 207● F32(img) => img.height
  208. 208 }
  209. 209}
  210. 210
  211. 211///|
  212. 212pub impl ImageLike for AnyImage with fn channels(self) {
  213. 213● match self {
  214. 214● U8(img) => img.channels
  215. 215● U16(img) => img.channels
  216. 216● F32(img) => img.channels
  217. 217 }
  218. 218}
  219. 219
  220. 220///|
  221. 221pub impl ImageLike for AnyImage with fn data(self) {
  222. 222○ match self {
  223. 223○ U8(img) => img.data
  224. 224○ U16(img) => img.data
  225. 225○ F32(img) => img.data
  226. 226 }
  227. 227}
  228. 228
  229. 229///|
  230. 230pub impl ImageLike for AnyImage with fn bytes_per_pixel(self) {
  231. 231● match self {
  232. 232● U8(_) => 1
  233. 233● U16(_) => 2
  234. 234● F32(_) => 4
  235. 235 }
  236. 236}
  237. 237
  238. 238///|
  239. 239pub extend AnyImage with ImageLike::{
  240. 240 width,
  241. 241 height,
  242. 242 channels,
  243. 243 data,
  244. 244 bytes_per_pixel,
  245. 245 pixel_count,
  246. 246 byte_count,
  247. 247 is_grayscale,
  248. 248 is_rgb,
  249. 249 is_rgba,
  250. 250}
  251. 251
  252. 252///|
  253. 253/// 将 Image 升级为 AnyImage::U8
  254. 254pub fn image_to_any(img : Image) -> AnyImage {
  255. 255● U8(img)
  256. 256}
  257. 257
  258. 258///|
  259. 259/// 将 Image16 升级为 AnyImage::U16
  260. 260pub fn image16_to_any(img : Image16) -> AnyImage {
  261. 261● U16(img)
  262. 262}
  263. 263
  264. 264///|
  265. 265/// 将 ImageF 升级为 AnyImage::F32
  266. 266pub fn imagef_to_any(img : ImageF) -> AnyImage {
  267. 267● F32(img)
  268. 268}
  269. 269
  270. 270///|
  271. 271/// 将 AnyImage 转换为 8-bit Image
  272. 272///
  273. 273/// U8 直接返回;U16 截断高位;F32 量化到 [0,255]
  274. 274pub fn any_image_to_u8(img : AnyImage) -> Image {
  275. 275● match img {
  276. 276● U8(im) => im
  277. 277● U16(im) => {
  278. 278 let w = im.width
  279. 279 let h = im.height
  280. 280 let c = im.channels
  281. 281 let n = w * h * c
  282. 282 let buf : Array[Byte] = []
  283. 283 for i in 0..<n {
  284. 284● let lo = im.data[i * 2].to_int()
  285. 285● let hi = im.data[i * 2 + 1].to_int()
  286. 286 let v = (hi << 8) | lo
  287. 287● let clamped = if v > 255 { 255 } else { v }
  288. 288● buf.push(clamped.to_byte())
  289. 289 }
  290. 290● Image::{ width: w, height: h, channels: c, data: Bytes::from_array(buf), }
  291. 291 }
  292. 292● F32(im) => {
  293. 293 let w = im.width
  294. 294 let h = im.height
  295. 295 let c = im.channels
  296. 296 let n = w * h * c
  297. 297 let buf : Array[Byte] = []
  298. 298 for i in 0..<n {
  299. 299● let idx = i * 4
  300. 300● let bits = (im.data[idx + 3].to_int() << 24) |
  301. 301● (im.data[idx + 2].to_int() << 16) |
  302. 302● (im.data[idx + 1].to_int() << 8) |
  303. 303● im.data[idx].to_int()
  304. 304● let f = Float::reinterpret_from_int(bits)
  305. 305● let v = (f * 255.0F + 0.5F).to_int()
  306. 306● let clamped = if v > 255 { 255 } else if v < 0 { 0 } else { v }
  307. 307● buf.push(clamped.to_byte())
  308. 308 }
  309. 309● Image::{ width: w, height: h, channels: c, data: Bytes::from_array(buf), }
  310. 310 }
  311. 311 }
  312. 312}
  313. 313
  314. 314///|
  315. 315/// 将 8-bit Image 转换为 ImageF(归一化到 [0,1])
  316. 316pub fn hdr_from_u8(img : Image) -> ImageF {
  317. 317● let w = img.width
  318. 318 let h = img.height
  319. 319 let c = img.channels
  320. 320 let n = w * h * c
  321. 321 let buf : Array[Byte] = []
  322. 322 for i in 0..<n {
  323. 323● let f = Float::from_int(img.data[i].to_int()) / 255.0F
  324. 324● let bytes = f.to_le_bytes()
  325. 325 for j in 0..<4 {
  326. 326● buf.push(bytes[j])
  327. 327 }
  328. 328 }
  329. 329● ImageF::{ width: w, height: h, channels: c, data: Bytes::from_array(buf), }
  330. 330}