image.process\feature\hash_quality_integral_16f.mbt

90.7% · 215 / 237 points · 658 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/// # 图像哈希/质量/积分 16-bit/float 变体 Image hash/quality/integral 16-bit/float variants
  3. 3///
  4. 4/// 16位哈希/质量/积分 dhash_16 / ssim_16 / integral_image_16
  5. 5/// 16位/浮点哈希/质量/积分 16-bit/float hash/quality/integral
  6. 6// 图像哈希 + 图像质量 + 积分图像 16-bit/float 变体
  7. 7
  8. 8// ========== 辅助函数 ==========
  9. 9
  10. 10///|
  11. 11fn read_u16_le_iq(data : Bytes, offset : Int32) -> Int32 {
  12. 12● data[offset].to_int() + data[offset + 1].to_int() * 256
  13. 13}
  14. 14
  15. 15///|
  16. 16fn read_float_le_iq(data : Bytes, offset : Int32) -> Float {
  17. 17● let b0 = data[offset].to_int()
  18. 18● let b1 = data[offset + 1].to_int()
  19. 19● let b2 = data[offset + 2].to_int()
  20. 20● let b3 = data[offset + 3].to_int()
  21. 21 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  22. 22● Float::reinterpret_from_int(bits)
  23. 23}
  24. 24
  25. 25///|
  26. 26fn img16_to_img8_iq(img : @types.Image16) -> @types.Image {
  27. 27● let w = img.width
  28. 28 let h = img.height
  29. 29 let ch = img.channels
  30. 30● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  31. 31 for i in 0..<(w * h * ch) {
  32. 32● let v16 = read_u16_le_iq(img.data, i * 2)
  33. 33● data8[i] = (v16 / 257).to_byte()
  34. 34 }
  35. 35 @types.Image::{
  36. 36 width: w,
  37. 37 height: h,
  38. 38 channels: ch,
  39. 39● data: Bytes::from_array(data8),
  40. 40 }
  41. 41}
  42. 42
  43. 43///|
  44. 44fn imgf_to_img8_iq(img : @types.ImageF) -> @types.Image {
  45. 45● let w = img.width
  46. 46 let h = img.height
  47. 47 let ch = img.channels
  48. 48● let data8 : Array[Byte] = Array::make(w * h * ch, b'\x00')
  49. 49 for i in 0..<(w * h * ch) {
  50. 50● let vf = read_float_le_iq(img.data, i * 4)
  51. 51● let v8 = @types.clamp_int((vf * 255.0F).to_int(), 0, 255)
  52. 52● data8[i] = v8.to_byte()
  53. 53 }
  54. 54 @types.Image::{
  55. 55 width: w,
  56. 56 height: h,
  57. 57 channels: ch,
  58. 58● data: Bytes::from_array(data8),
  59. 59 }
  60. 60}
  61. 61
  62. 62// ========== 图像哈希 16-bit ==========
  63. 63
  64. 64///|
  65. 65/// 16-bit 平均哈希。
  66. 66/// @see ahash 8-bit 版本,本函数为 Image16 变体
  67. 67pub fn ahash_16(img : @types.Image16) -> Array[Int32] raise @types.LoadError {
  68. 68 // 输入验证
  69. 69● if img.width <= 0 || img.height <= 0 {
  70. 70● raise @types.LoadError::DecodeFailed("ahash_16: 图像尺寸必须大于0")
  71. 71 }
  72. 72 if img.channels < 1 || img.channels > 4 {
  73. 73● raise @types.LoadError::DecodeFailed(
  74. 74 "ahash_16: 通道数必须在1-4之间",
  75. 75 )
  76. 76 }
  77. 77 let expected_size = img.width * img.height * img.channels * 2
  78. 78● if img.data.length() != expected_size {
  79. 79● raise @types.LoadError::DecodeFailed(
  80. 80 "ahash_16: 数据大小不匹配,期望 " +
  81. 81● expected_size.to_string() +
  82. 82 ",实际 " +
  83. 83● img.data.length().to_string(),
  84. 84 )
  85. 85 }
  86. 86
  87. 87● let img8 = img16_to_img8_iq(img)
  88. 88● ahash(img8)
  89. 89}
  90. 90
  91. 91///|
  92. 92/// 16-bit 差异哈希。
  93. 93/// @see dhash 8-bit 版本,本函数为 Image16 变体
  94. 94pub fn dhash_16(img : @types.Image16) -> Array[Int32] raise @types.LoadError {
  95. 95 // 输入验证
  96. 96● if img.width <= 0 || img.height <= 0 {
  97. 97● raise @types.LoadError::DecodeFailed("dhash_16: 图像尺寸必须大于0")
  98. 98 }
  99. 99 if img.channels < 1 || img.channels > 4 {
  100. 100● raise @types.LoadError::DecodeFailed(
  101. 101 "dhash_16: 通道数必须在1-4之间",
  102. 102 )
  103. 103 }
  104. 104 let expected_size = img.width * img.height * img.channels * 2
  105. 105● if img.data.length() != expected_size {
  106. 106● raise @types.LoadError::DecodeFailed(
  107. 107 "dhash_16: 数据大小不匹配,期望 " +
  108. 108● expected_size.to_string() +
  109. 109 ",实际 " +
  110. 110● img.data.length().to_string(),
  111. 111 )
  112. 112 }
  113. 113
  114. 114● let img8 = img16_to_img8_iq(img)
  115. 115● dhash(img8)
  116. 116}
  117. 117
  118. 118///|
  119. 119/// 16-bit 感知哈希。
  120. 120/// @see phash 8-bit 版本,本函数为 Image16 变体
  121. 121pub fn phash_16(img : @types.Image16) -> Array[Int32] raise @types.LoadError {
  122. 122 // 输入验证
  123. 123● if img.width <= 0 || img.height <= 0 {
  124. 124● raise @types.LoadError::DecodeFailed("phash_16: 图像尺寸必须大于0")
  125. 125 }
  126. 126 if img.channels < 1 || img.channels > 4 {
  127. 127● raise @types.LoadError::DecodeFailed(
  128. 128 "phash_16: 通道数必须在1-4之间",
  129. 129 )
  130. 130 }
  131. 131 let expected_size = img.width * img.height * img.channels * 2
  132. 132● if img.data.length() != expected_size {
  133. 133● raise @types.LoadError::DecodeFailed(
  134. 134 "phash_16: 数据大小不匹配,期望 " +
  135. 135● expected_size.to_string() +
  136. 136 ",实际 " +
  137. 137● img.data.length().to_string(),
  138. 138 )
  139. 139 }
  140. 140
  141. 141● let img8 = img16_to_img8_iq(img)
  142. 142● phash(img8)
  143. 143}
  144. 144
  145. 145// ========== 图像哈希 float ==========
  146. 146
  147. 147///|
  148. 148/// float 平均哈希。
  149. 149/// @see ahash 8-bit 版本,本函数为 ImageF 变体
  150. 150pub fn ahash_f(img : @types.ImageF) -> Array[Int32] raise @types.LoadError {
  151. 151 // 输入验证
  152. 152● if img.width <= 0 || img.height <= 0 {
  153. 153● raise @types.LoadError::DecodeFailed("ahash_f: 图像尺寸必须大于0")
  154. 154 }
  155. 155 if img.channels < 1 || img.channels > 4 {
  156. 156● raise @types.LoadError::DecodeFailed("ahash_f: 通道数必须在1-4之间")
  157. 157 }
  158. 158 let expected_size = img.width * img.height * img.channels * 4
  159. 159● if img.data.length() != expected_size {
  160. 160● raise @types.LoadError::DecodeFailed(
  161. 161 "ahash_f: 数据大小不匹配,期望 " +
  162. 162● expected_size.to_string() +
  163. 163 ",实际 " +
  164. 164● img.data.length().to_string(),
  165. 165 )
  166. 166 }
  167. 167
  168. 168● let img8 = imgf_to_img8_iq(img)
  169. 169● ahash(img8)
  170. 170}
  171. 171
  172. 172///|
  173. 173/// float 差异哈希。
  174. 174/// @see dhash 8-bit 版本,本函数为 ImageF 变体
  175. 175pub fn dhash_f(img : @types.ImageF) -> Array[Int32] raise @types.LoadError {
  176. 176 // 输入验证
  177. 177● if img.width <= 0 || img.height <= 0 {
  178. 178● raise @types.LoadError::DecodeFailed("dhash_f: 图像尺寸必须大于0")
  179. 179 }
  180. 180 if img.channels < 1 || img.channels > 4 {
  181. 181● raise @types.LoadError::DecodeFailed("dhash_f: 通道数必须在1-4之间")
  182. 182 }
  183. 183 let expected_size = img.width * img.height * img.channels * 4
  184. 184● if img.data.length() != expected_size {
  185. 185● raise @types.LoadError::DecodeFailed(
  186. 186 "dhash_f: 数据大小不匹配,期望 " +
  187. 187● expected_size.to_string() +
  188. 188 ",实际 " +
  189. 189● img.data.length().to_string(),
  190. 190 )
  191. 191 }
  192. 192
  193. 193● let img8 = imgf_to_img8_iq(img)
  194. 194● dhash(img8)
  195. 195}
  196. 196
  197. 197///|
  198. 198/// float 感知哈希。
  199. 199/// @see phash 8-bit 版本,本函数为 ImageF 变体
  200. 200pub fn phash_f(img : @types.ImageF) -> Array[Int32] raise @types.LoadError {
  201. 201 // 输入验证
  202. 202● if img.width <= 0 || img.height <= 0 {
  203. 203● raise @types.LoadError::DecodeFailed("phash_f: 图像尺寸必须大于0")
  204. 204 }
  205. 205 if img.channels < 1 || img.channels > 4 {
  206. 206● raise @types.LoadError::DecodeFailed("phash_f: 通道数必须在1-4之间")
  207. 207 }
  208. 208 let expected_size = img.width * img.height * img.channels * 4
  209. 209● if img.data.length() != expected_size {
  210. 210● raise @types.LoadError::DecodeFailed(
  211. 211 "phash_f: 数据大小不匹配,期望 " +
  212. 212● expected_size.to_string() +
  213. 213 ",实际 " +
  214. 214● img.data.length().to_string(),
  215. 215 )
  216. 216 }
  217. 217
  218. 218● let img8 = imgf_to_img8_iq(img)
  219. 219● phash(img8)
  220. 220}
  221. 221
  222. 222// ========== 图像质量 16-bit ==========
  223. 223
  224. 224///|
  225. 225/// 16-bit 均方误差。
  226. 226/// @see mse 8-bit 版本,本函数为 Image16 变体
  227. 227pub fn mse_16(
  228. 228 img1 : @types.Image16,
  229. 229 img2 : @types.Image16,
  230. 230) -> Double raise @types.LoadError {
  231. 231 // 输入验证
  232. 232● if img1.width <= 0 || img1.height <= 0 {
  233. 233● raise @types.LoadError::DecodeFailed("mse_16: 图像1尺寸必须大于0")
  234. 234 }
  235. 235 if img1.channels < 1 || img1.channels > 4 {
  236. 236● raise @types.LoadError::DecodeFailed(
  237. 237 "mse_16: 图像1通道数必须在1-4之间",
  238. 238 )
  239. 239 }
  240. 240 let img1_expected_size = img1.width * img1.height * img1.channels * 2
  241. 241● if img1.data.length() != img1_expected_size {
  242. 242● raise @types.LoadError::DecodeFailed(
  243. 243 "mse_16: 图像1数据大小不匹配,期望 " +
  244. 244● img1_expected_size.to_string() +
  245. 245 ",实际 " +
  246. 246● img1.data.length().to_string(),
  247. 247 )
  248. 248 }
  249. 249 if img2.width <= 0 || img2.height <= 0 {
  250. 250● raise @types.LoadError::DecodeFailed("mse_16: 图像2尺寸必须大于0")
  251. 251 }
  252. 252 if img2.channels < 1 || img2.channels > 4 {
  253. 253○ raise @types.LoadError::DecodeFailed(
  254. 254 "mse_16: 图像2通道数必须在1-4之间",
  255. 255 )
  256. 256 }
  257. 257 let img2_expected_size = img2.width * img2.height * img2.channels * 2
  258. 258● if img2.data.length() != img2_expected_size {
  259. 259○ raise @types.LoadError::DecodeFailed(
  260. 260 "mse_16: 图像2数据大小不匹配,期望 " +
  261. 261○ img2_expected_size.to_string() +
  262. 262 ",实际 " +
  263. 263○ img2.data.length().to_string(),
  264. 264 )
  265. 265 }
  266. 266 if img1.width != img2.width || img1.height != img2.height {
  267. 267● raise @types.LoadError::DecodeFailed("mse_16: 两图像尺寸必须一致")
  268. 268 }
  269. 269
  270. 270● let img1_8 = img16_to_img8_iq(img1)
  271. 271● let img2_8 = img16_to_img8_iq(img2)
  272. 272● mse(img1_8, img2_8)
  273. 273}
  274. 274
  275. 275///|
  276. 276/// 16-bit 峰值信噪比。
  277. 277/// @see psnr 8-bit 版本,本函数为 Image16 变体
  278. 278pub fn psnr_16(
  279. 279 img1 : @types.Image16,
  280. 280 img2 : @types.Image16,
  281. 281) -> Double raise @types.LoadError {
  282. 282 // 输入验证
  283. 283● if img1.width <= 0 || img1.height <= 0 {
  284. 284● raise @types.LoadError::DecodeFailed("psnr_16: 图像1尺寸必须大于0")
  285. 285 }
  286. 286 if img1.channels < 1 || img1.channels > 4 {
  287. 287● raise @types.LoadError::DecodeFailed(
  288. 288 "psnr_16: 图像1通道数必须在1-4之间",
  289. 289 )
  290. 290 }
  291. 291 let img1_expected_size = img1.width * img1.height * img1.channels * 2
  292. 292● if img1.data.length() != img1_expected_size {
  293. 293● raise @types.LoadError::DecodeFailed(
  294. 294 "psnr_16: 图像1数据大小不匹配,期望 " +
  295. 295● img1_expected_size.to_string() +
  296. 296 ",实际 " +
  297. 297● img1.data.length().to_string(),
  298. 298 )
  299. 299 }
  300. 300 if img2.width <= 0 || img2.height <= 0 {
  301. 301● raise @types.LoadError::DecodeFailed("psnr_16: 图像2尺寸必须大于0")
  302. 302 }
  303. 303 if img2.channels < 1 || img2.channels > 4 {
  304. 304○ raise @types.LoadError::DecodeFailed(
  305. 305 "psnr_16: 图像2通道数必须在1-4之间",
  306. 306 )
  307. 307 }
  308. 308 let img2_expected_size = img2.width * img2.height * img2.channels * 2
  309. 309● if img2.data.length() != img2_expected_size {
  310. 310● raise @types.LoadError::DecodeFailed(
  311. 311 "psnr_16: 图像2数据大小不匹配,期望 " +
  312. 312● img2_expected_size.to_string() +
  313. 313 ",实际 " +
  314. 314● img2.data.length().to_string(),
  315. 315 )
  316. 316 }
  317. 317 if img1.width != img2.width || img1.height != img2.height {
  318. 318● raise @types.LoadError::DecodeFailed("psnr_16: 两图像尺寸必须一致")
  319. 319 }
  320. 320
  321. 321● let img1_8 = img16_to_img8_iq(img1)
  322. 322● let img2_8 = img16_to_img8_iq(img2)
  323. 323● psnr(img1_8, img2_8)
  324. 324}
  325. 325
  326. 326///|
  327. 327/// 16-bit 结构相似性。
  328. 328/// @see ssim 8-bit 版本,本函数为 Image16 变体
  329. 329pub fn ssim_16(
  330. 330 img1 : @types.Image16,
  331. 331 img2 : @types.Image16,
  332. 332) -> Double raise @types.LoadError {
  333. 333 // 输入验证
  334. 334● if img1.width <= 0 || img1.height <= 0 {
  335. 335● raise @types.LoadError::DecodeFailed("ssim_16: 图像1尺寸必须大于0")
  336. 336 }
  337. 337 if img1.channels < 1 || img1.channels > 4 {
  338. 338● raise @types.LoadError::DecodeFailed(
  339. 339 "ssim_16: 图像1通道数必须在1-4之间",
  340. 340 )
  341. 341 }
  342. 342 let img1_expected_size = img1.width * img1.height * img1.channels * 2
  343. 343● if img1.data.length() != img1_expected_size {
  344. 344● raise @types.LoadError::DecodeFailed(
  345. 345 "ssim_16: 图像1数据大小不匹配,期望 " +
  346. 346● img1_expected_size.to_string() +
  347. 347 ",实际 " +
  348. 348● img1.data.length().to_string(),
  349. 349 )
  350. 350 }
  351. 351 if img2.width <= 0 || img2.height <= 0 {
  352. 352● raise @types.LoadError::DecodeFailed("ssim_16: 图像2尺寸必须大于0")
  353. 353 }
  354. 354 if img2.channels < 1 || img2.channels > 4 {
  355. 355○ raise @types.LoadError::DecodeFailed(
  356. 356 "ssim_16: 图像2通道数必须在1-4之间",
  357. 357 )
  358. 358 }
  359. 359 let img2_expected_size = img2.width * img2.height * img2.channels * 2
  360. 360● if img2.data.length() != img2_expected_size {
  361. 361○ raise @types.LoadError::DecodeFailed(
  362. 362 "ssim_16: 图像2数据大小不匹配,期望 " +
  363. 363○ img2_expected_size.to_string() +
  364. 364 ",实际 " +
  365. 365○ img2.data.length().to_string(),
  366. 366 )
  367. 367 }
  368. 368 if img1.width != img2.width || img1.height != img2.height {
  369. 369● raise @types.LoadError::DecodeFailed("ssim_16: 两图像尺寸必须一致")
  370. 370 }
  371. 371
  372. 372● let img1_8 = img16_to_img8_iq(img1)
  373. 373● let img2_8 = img16_to_img8_iq(img2)
  374. 374● ssim(img1_8, img2_8)
  375. 375}
  376. 376
  377. 377// ========== 图像质量 float ==========
  378. 378
  379. 379///|
  380. 380/// float 均方误差。
  381. 381/// @see mse 8-bit 版本,本函数为 ImageF 变体
  382. 382pub fn mse_f(
  383. 383 img1 : @types.ImageF,
  384. 384 img2 : @types.ImageF,
  385. 385) -> Double raise @types.LoadError {
  386. 386 // 输入验证
  387. 387● if img1.width <= 0 || img1.height <= 0 {
  388. 388● raise @types.LoadError::DecodeFailed("mse_f: 图像1尺寸必须大于0")
  389. 389 }
  390. 390 if img1.channels < 1 || img1.channels > 4 {
  391. 391● raise @types.LoadError::DecodeFailed(
  392. 392 "mse_f: 图像1通道数必须在1-4之间",
  393. 393 )
  394. 394 }
  395. 395 let img1_expected_size = img1.width * img1.height * img1.channels * 4
  396. 396● if img1.data.length() != img1_expected_size {
  397. 397● raise @types.LoadError::DecodeFailed(
  398. 398 "mse_f: 图像1数据大小不匹配,期望 " +
  399. 399● img1_expected_size.to_string() +
  400. 400 ",实际 " +
  401. 401● img1.data.length().to_string(),
  402. 402 )
  403. 403 }
  404. 404 if img2.width <= 0 || img2.height <= 0 {
  405. 405● raise @types.LoadError::DecodeFailed("mse_f: 图像2尺寸必须大于0")
  406. 406 }
  407. 407 if img2.channels < 1 || img2.channels > 4 {
  408. 408○ raise @types.LoadError::DecodeFailed(
  409. 409 "mse_f: 图像2通道数必须在1-4之间",
  410. 410 )
  411. 411 }
  412. 412 let img2_expected_size = img2.width * img2.height * img2.channels * 4
  413. 413● if img2.data.length() != img2_expected_size {
  414. 414● raise @types.LoadError::DecodeFailed(
  415. 415 "mse_f: 图像2数据大小不匹配,期望 " +
  416. 416● img2_expected_size.to_string() +
  417. 417 ",实际 " +
  418. 418● img2.data.length().to_string(),
  419. 419 )
  420. 420 }
  421. 421 if img1.width != img2.width || img1.height != img2.height {
  422. 422● raise @types.LoadError::DecodeFailed("mse_f: 两图像尺寸必须一致")
  423. 423 }
  424. 424
  425. 425● let img1_8 = imgf_to_img8_iq(img1)
  426. 426● let img2_8 = imgf_to_img8_iq(img2)
  427. 427● mse(img1_8, img2_8)
  428. 428}
  429. 429
  430. 430///|
  431. 431/// float 峰值信噪比。
  432. 432/// @see psnr 8-bit 版本,本函数为 ImageF 变体
  433. 433pub fn psnr_f(
  434. 434 img1 : @types.ImageF,
  435. 435 img2 : @types.ImageF,
  436. 436) -> Double raise @types.LoadError {
  437. 437 // 输入验证
  438. 438● if img1.width <= 0 || img1.height <= 0 {
  439. 439● raise @types.LoadError::DecodeFailed("psnr_f: 图像1尺寸必须大于0")
  440. 440 }
  441. 441 if img1.channels < 1 || img1.channels > 4 {
  442. 442● raise @types.LoadError::DecodeFailed(
  443. 443 "psnr_f: 图像1通道数必须在1-4之间",
  444. 444 )
  445. 445 }
  446. 446 let img1_expected_size = img1.width * img1.height * img1.channels * 4
  447. 447● if img1.data.length() != img1_expected_size {
  448. 448● raise @types.LoadError::DecodeFailed(
  449. 449 "psnr_f: 图像1数据大小不匹配,期望 " +
  450. 450● img1_expected_size.to_string() +
  451. 451 ",实际 " +
  452. 452● img1.data.length().to_string(),
  453. 453 )
  454. 454 }
  455. 455 if img2.width <= 0 || img2.height <= 0 {
  456. 456● raise @types.LoadError::DecodeFailed("psnr_f: 图像2尺寸必须大于0")
  457. 457 }
  458. 458 if img2.channels < 1 || img2.channels > 4 {
  459. 459○ raise @types.LoadError::DecodeFailed(
  460. 460 "psnr_f: 图像2通道数必须在1-4之间",
  461. 461 )
  462. 462 }
  463. 463 let img2_expected_size = img2.width * img2.height * img2.channels * 4
  464. 464● if img2.data.length() != img2_expected_size {
  465. 465○ raise @types.LoadError::DecodeFailed(
  466. 466 "psnr_f: 图像2数据大小不匹配,期望 " +
  467. 467○ img2_expected_size.to_string() +
  468. 468 ",实际 " +
  469. 469○ img2.data.length().to_string(),
  470. 470 )
  471. 471 }
  472. 472 if img1.width != img2.width || img1.height != img2.height {
  473. 473● raise @types.LoadError::DecodeFailed("psnr_f: 两图像尺寸必须一致")
  474. 474 }
  475. 475
  476. 476● let img1_8 = imgf_to_img8_iq(img1)
  477. 477● let img2_8 = imgf_to_img8_iq(img2)
  478. 478● psnr(img1_8, img2_8)
  479. 479}
  480. 480
  481. 481///|
  482. 482/// float 结构相似性。
  483. 483/// @see ssim 8-bit 版本,本函数为 ImageF 变体
  484. 484pub fn ssim_f(
  485. 485 img1 : @types.ImageF,
  486. 486 img2 : @types.ImageF,
  487. 487) -> Double raise @types.LoadError {
  488. 488 // 输入验证
  489. 489● if img1.width <= 0 || img1.height <= 0 {
  490. 490● raise @types.LoadError::DecodeFailed("ssim_f: 图像1尺寸必须大于0")
  491. 491 }
  492. 492 if img1.channels < 1 || img1.channels > 4 {
  493. 493● raise @types.LoadError::DecodeFailed(
  494. 494 "ssim_f: 图像1通道数必须在1-4之间",
  495. 495 )
  496. 496 }
  497. 497 let img1_expected_size = img1.width * img1.height * img1.channels * 4
  498. 498● if img1.data.length() != img1_expected_size {
  499. 499● raise @types.LoadError::DecodeFailed(
  500. 500 "ssim_f: 图像1数据大小不匹配,期望 " +
  501. 501● img1_expected_size.to_string() +
  502. 502 ",实际 " +
  503. 503● img1.data.length().to_string(),
  504. 504 )
  505. 505 }
  506. 506 if img2.width <= 0 || img2.height <= 0 {
  507. 507● raise @types.LoadError::DecodeFailed("ssim_f: 图像2尺寸必须大于0")
  508. 508 }
  509. 509 if img2.channels < 1 || img2.channels > 4 {
  510. 510○ raise @types.LoadError::DecodeFailed(
  511. 511 "ssim_f: 图像2通道数必须在1-4之间",
  512. 512 )
  513. 513 }
  514. 514 let img2_expected_size = img2.width * img2.height * img2.channels * 4
  515. 515● if img2.data.length() != img2_expected_size {
  516. 516○ raise @types.LoadError::DecodeFailed(
  517. 517 "ssim_f: 图像2数据大小不匹配,期望 " +
  518. 518○ img2_expected_size.to_string() +
  519. 519 ",实际 " +
  520. 520○ img2.data.length().to_string(),
  521. 521 )
  522. 522 }
  523. 523 if img1.width != img2.width || img1.height != img2.height {
  524. 524● raise @types.LoadError::DecodeFailed("ssim_f: 两图像尺寸必须一致")
  525. 525 }
  526. 526
  527. 527● let img1_8 = imgf_to_img8_iq(img1)
  528. 528● let img2_8 = imgf_to_img8_iq(img2)
  529. 529● ssim(img1_8, img2_8)
  530. 530}
  531. 531
  532. 532// ========== 积分图像 16-bit ==========
  533. 533
  534. 534///|
  535. 535/// 16-bit 积分图像。
  536. 536/// @see integral_image 8-bit 版本,本函数为 Image16 变体
  537. 537pub fn integral_image_16(
  538. 538 img : @types.Image16,
  539. 539) -> IntegralImage raise @types.LoadError {
  540. 540 // 输入验证
  541. 541● if img.width <= 0 || img.height <= 0 {
  542. 542● raise @types.LoadError::DecodeFailed(
  543. 543 "integral_image_16: 图像尺寸必须大于0",
  544. 544 )
  545. 545 }
  546. 546 if img.channels < 1 || img.channels > 4 {
  547. 547● raise @types.LoadError::DecodeFailed(
  548. 548 "integral_image_16: 通道数必须在1-4之间",
  549. 549 )
  550. 550 }
  551. 551 let expected_size = img.width * img.height * img.channels * 2
  552. 552● if img.data.length() != expected_size {
  553. 553● raise @types.LoadError::DecodeFailed(
  554. 554 "integral_image_16: 数据大小不匹配,期望 " +
  555. 555● expected_size.to_string() +
  556. 556 ",实际 " +
  557. 557● img.data.length().to_string(),
  558. 558 )
  559. 559 }
  560. 560
  561. 561● let img8 = img16_to_img8_iq(img)
  562. 562● integral_image(img8)
  563. 563}
  564. 564
  565. 565///|
  566. 566/// 16-bit 平方积分图像。
  567. 567/// @see integral_image_sq 8-bit 版本,本函数为 Image16 变体
  568. 568pub fn integral_image_sq_16(
  569. 569 img : @types.Image16,
  570. 570) -> IntegralImageSq raise @types.LoadError {
  571. 571 // 输入验证
  572. 572● if img.width <= 0 || img.height <= 0 {
  573. 573● raise @types.LoadError::DecodeFailed(
  574. 574 "integral_image_sq_16: 图像尺寸必须大于0",
  575. 575 )
  576. 576 }
  577. 577 if img.channels < 1 || img.channels > 4 {
  578. 578● raise @types.LoadError::DecodeFailed(
  579. 579 "integral_image_sq_16: 通道数必须在1-4之间",
  580. 580 )
  581. 581 }
  582. 582 let expected_size = img.width * img.height * img.channels * 2
  583. 583● if img.data.length() != expected_size {
  584. 584● raise @types.LoadError::DecodeFailed(
  585. 585 "integral_image_sq_16: 数据大小不匹配,期望 " +
  586. 586● expected_size.to_string() +
  587. 587 ",实际 " +
  588. 588● img.data.length().to_string(),
  589. 589 )
  590. 590 }
  591. 591
  592. 592● let img8 = img16_to_img8_iq(img)
  593. 593● integral_image_sq(img8)
  594. 594}
  595. 595
  596. 596// ========== 积分图像 float ==========
  597. 597
  598. 598///|
  599. 599/// float 积分图像。
  600. 600/// @see integral_image 8-bit 版本,本函数为 ImageF 变体
  601. 601pub fn integral_image_f(
  602. 602 img : @types.ImageF,
  603. 603) -> IntegralImage raise @types.LoadError {
  604. 604 // 输入验证
  605. 605● if img.width <= 0 || img.height <= 0 {
  606. 606● raise @types.LoadError::DecodeFailed(
  607. 607 "integral_image_f: 图像尺寸必须大于0",
  608. 608 )
  609. 609 }
  610. 610 if img.channels < 1 || img.channels > 4 {
  611. 611● raise @types.LoadError::DecodeFailed(
  612. 612 "integral_image_f: 通道数必须在1-4之间",
  613. 613 )
  614. 614 }
  615. 615 let expected_size = img.width * img.height * img.channels * 4
  616. 616● if img.data.length() != expected_size {
  617. 617● raise @types.LoadError::DecodeFailed(
  618. 618 "integral_image_f: 数据大小不匹配,期望 " +
  619. 619● expected_size.to_string() +
  620. 620 ",实际 " +
  621. 621● img.data.length().to_string(),
  622. 622 )
  623. 623 }
  624. 624
  625. 625● let img8 = imgf_to_img8_iq(img)
  626. 626● integral_image(img8)
  627. 627}
  628. 628
  629. 629///|
  630. 630/// float 平方积分图像。
  631. 631/// @see integral_image_sq 8-bit 版本,本函数为 ImageF 变体
  632. 632pub fn integral_image_sq_f(
  633. 633 img : @types.ImageF,
  634. 634) -> IntegralImageSq raise @types.LoadError {
  635. 635 // 输入验证
  636. 636● if img.width <= 0 || img.height <= 0 {
  637. 637● raise @types.LoadError::DecodeFailed(
  638. 638 "integral_image_sq_f: 图像尺寸必须大于0",
  639. 639 )
  640. 640 }
  641. 641 if img.channels < 1 || img.channels > 4 {
  642. 642● raise @types.LoadError::DecodeFailed(
  643. 643 "integral_image_sq_f: 通道数必须在1-4之间",
  644. 644 )
  645. 645 }
  646. 646 let expected_size = img.width * img.height * img.channels * 4
  647. 647● if img.data.length() != expected_size {
  648. 648● raise @types.LoadError::DecodeFailed(
  649. 649 "integral_image_sq_f: 数据大小不匹配,期望 " +
  650. 650● expected_size.to_string() +
  651. 651 ",实际 " +
  652. 652● img.data.length().to_string(),
  653. 653 )
  654. 654 }
  655. 655
  656. 656● let img8 = imgf_to_img8_iq(img)
  657. 657● integral_image_sq(img8)
  658. 658}