imgcli\cli_quality.mbt

92.9% · 52 / 56 points · 81 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/// `quality` — 两张已编码图的**质量口径**回执(S29)。
  3. 3///
  4. 4/// 三件一起给:PSNR(dB)、窗口 SSIM(`window×window` 块均值)、体积比(候选/参考)。
  5. 5/// 判定阈值可由 `--min-psnr` / `--min-ssim` / `--max-ratio` 覆盖,默认读库层
  6. 6/// `@quality.default_quality_threshold()`,**CLI 不另存一份数字**。
  7. 7///
  8. 8/// 回执里 `failed` 是违例项数组(`psnr` / `ssim` / `volume`),`pass` 为全过;
  9. 9/// `line` 是与 `quality_report_to_string` 逐字一致的单行摘要,golden 直接 diff 它。
  10. 10
  11. 11///|
  12. 12fn Flags::dbl(self : Flags, key : String, default : Double) -> Double {
  13. 13● match self.values.get(key) {
  14. 14◐ Some(v) => @string.parse_double(v) catch { _ => default }
  15. 15● None => default
  16. 16 }
  17. 17}
  18. 18
  19. 19///|
  20. 20/// 截到 1e-3 的 JSON 数字:`monotonic_clock` 那类 16 位尾巴不进回执。
  21. 21fn q3(v : Double) -> @json.Json {
  22. 22● if v != v {
  23. 23○ return @json.Json::null()
  24. 24 }
  25. 25◐ let bias = if v >= 0.0 { 0.5 } else { -0.5 }
  26. 26● let scaled = (v * 1000.0 + bias).to_int()
  27. 27● @json.Json::number(scaled.to_double() / 1000.0)
  28. 28}
  29. 29
  30. 30///|
  31. 31fn cmd_quality(flags : Flags) -> Out raise @types.ImageError {
  32. 32● let ref_path = flags.positional[0]
  33. 33 let cand_path = flags.positional[1]
  34. 34● let ref_bytes = src_bytes(ref_path)
  35. 35● let cand_bytes = src_bytes(cand_path)
  36. 36● let limits = limits_of(flags)
  37. 37● let ref_img = @lib.decode_with_limits(ref_bytes, limits)
  38. 38● let cand_img = @lib.decode_with_limits(cand_bytes, limits)
  39. 39● let base = @quality.default_quality_threshold()
  40. 40 let threshold : @quality.QualityThreshold = {
  41. 41● min_psnr: flags.dbl("min-psnr", base.min_psnr),
  42. 42● min_ssim: flags.dbl("min-ssim", base.min_ssim),
  43. 43● max_volume_ratio: flags.dbl("max-ratio", base.max_volume_ratio),
  44. 44 }
  45. 45● let window = flags.int("window", 8)
  46. 46● let report = @quality.evaluate_quality(
  47. 47 ref_img,
  48. 48 cand_img,
  49. 49● ref_bytes.length(),
  50. 50● cand_bytes.length(),
  51. 51 threshold,
  52. 52 window,
  53. 53 )
  54. 54 let failed_json : Array[@json.Json] = []
  55. 55 for f in report.failed {
  56. 56● failed_json.push(jstr(f))
  57. 57 }
  58. 58● result({
  59. 59● "reference": jstr(ref_path),
  60. 60● "candidate": jstr(cand_path),
  61. 61● "width": jnum(ref_img.width),
  62. 62● "height": jnum(ref_img.height),
  63. 63● "channels": jnum(ref_img.channels.min(cand_img.channels)),
  64. 64● "psnr": q3(report.psnr),
  65. 65● "ssim": q3(report.ssim),
  66. 66● "ssim_windowed": q3(report.ssim_windowed),
  67. 67● "mse": q3(report.mse),
  68. 68● "mae": q3(report.mae),
  69. 69● "reference_bytes": jnum(report.reference_bytes),
  70. 70● "candidate_bytes": jnum(report.candidate_bytes),
  71. 71● "volume_ratio": q3(report.volume_ratio),
  72. 72● "window": jnum(window),
  73. 73● "min_psnr": q3(threshold.min_psnr),
  74. 74● "min_ssim": q3(threshold.min_ssim),
  75. 75● "max_ratio": q3(threshold.max_volume_ratio),
  76. 76● "pass": jbool(report.pass),
  77. 77● "failed": @json.Json::array(failed_json),
  78. 78● "verdict": jstr(@quality.quality_verdict(report)),
  79. 79● "line": jstr(@quality.quality_report_to_string(report)),
  80. 80 })
  81. 81}