image.process\color\colormap.mbt

92.3% · 60 / 65 points · 141 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/// # 色彩映射 colormap
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 伪彩色映射(纯 MoonBit):apply_colormap + 预设色彩映射表
  7. 7
  8. 8///|
  9. 9/// 色彩映射预设
  10. 10/// `#non_exhaustive` 表示未来可能添加新构造器
  11. 11#non_exhaustive
  12. 12pub(all) enum Colormap {
  13. 13 JET // 蓝→青→绿→黄→红
  14. 14 HOT // 黑→红→黄→白
  15. 15 COOL // 青→品红
  16. 16 GRAY // 灰度
  17. 17 BONE // 偏蓝灰
  18. 18 COPPER // 偏铜色
  19. 19}
  20. 20
  21. 21///|
  22. 22fn get_colormap_lut(map : Colormap) -> Array[(Byte, Byte, Byte)] {
  23. 23● let lut : Array[(Byte, Byte, Byte)] = Array::make(
  24. 24 256,
  25. 25 (b'\x00', b'\x00', b'\x00'),
  26. 26 )
  27. 27 for i in 0..<256 {
  28. 28● let t = Float::from_int(i) / 255.0F
  29. 29● let (r, g, b) = match map {
  30. 30● JET => {
  31. 31 // JET: 4 段线性插值
  32. 32 let r_val = if t < 0.375F {
  33. 33● 0.0F
  34. 34● } else if t < 0.625F {
  35. 35● (t - 0.375F) / 0.25F
  36. 36 } else {
  37. 37● 1.0F
  38. 38 }
  39. 39 let g_val = if t < 0.125F {
  40. 40● 0.0F
  41. 41● } else if t < 0.375F {
  42. 42● (t - 0.125F) / 0.25F
  43. 43● } else if t < 0.625F {
  44. 44● 1.0F
  45. 45● } else if t < 0.875F {
  46. 46● 1.0F - (t - 0.625F) / 0.25F
  47. 47 } else {
  48. 48● 0.0F
  49. 49 }
  50. 50 let b_val = if t < 0.125F {
  51. 51● 0.5F + t / 0.25F * 0.5F
  52. 52● } else if t < 0.375F {
  53. 53● 1.0F
  54. 54● } else if t < 0.625F {
  55. 55● 1.0F - (t - 0.375F) / 0.25F
  56. 56 } else {
  57. 57● 0.0F
  58. 58 }
  59. 59 (r_val, g_val, b_val)
  60. 60 }
  61. 61● HOT => {
  62. 62● let r_val = if t < 0.333F { t / 0.333F } else { 1.0F }
  63. 63 let g_val = if t < 0.333F {
  64. 64● 0.0F
  65. 65● } else if t < 0.666F {
  66. 66● (t - 0.333F) / 0.333F
  67. 67 } else {
  68. 68● 1.0F
  69. 69 }
  70. 70● let b_val = if t < 0.666F { 0.0F } else { (t - 0.666F) / 0.334F }
  71. 71 (r_val, g_val, b_val)
  72. 72 }
  73. 73● COOL => (0.0F, t, 1.0F - t)
  74. 74● GRAY => (t, t, t)
  75. 75 BONE =>
  76. 76 // 偏蓝灰
  77. 77● (t * 0.93F, t * 0.88F, t * 1.0F)
  78. 78 COPPER =>
  79. 79 // 偏铜色
  80. 80● ((t * 1.25F).clamp(min=0.0F, max=1.0F), t * 0.78F, t * 0.5F)
  81. 81 }
  82. 82 lut[i] = (
  83. 83● (r * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte(),
  84. 84● (g * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte(),
  85. 85● (b * 255.0F + 0.5F).to_int().clamp(min=0, max=255).to_byte(),
  86. 86 )
  87. 87 }
  88. 88 lut
  89. 89}
  90. 90
  91. 91///|
  92. 92/// 伪彩色映射。将灰度图或彩色图(先转灰度)映射为 RGB 伪彩色图像。
  93. 93pub fn apply_colormap(
  94. 94 img : @types.Image,
  95. 95 map : Colormap,
  96. 96) -> @types.Image raise @types.LoadError {
  97. 97 // 输入验证
  98. 98● if img.width <= 0 || img.height <= 0 {
  99. 99● raise @types.LoadError::DecodeFailed(
  100. 100 "apply_colormap: 图像尺寸必须大于0",
  101. 101 )
  102. 102 }
  103. 103 if img.channels < 1 || img.channels > 4 {
  104. 104○ raise @types.LoadError::DecodeFailed(
  105. 105 "apply_colormap: 通道数必须在1-4之间",
  106. 106 )
  107. 107 }
  108. 108 let expected_size = img.width * img.height * img.channels
  109. 109● if img.data.length() != expected_size {
  110. 110○ raise @types.LoadError::DecodeFailed(
  111. 111 "apply_colormap: 数据大小不匹配,期望 " +
  112. 112○ expected_size.to_string() +
  113. 113 ",实际 " +
  114. 114○ img.data.length().to_string(),
  115. 115 )
  116. 116 }
  117. 117● let lut = get_colormap_lut(map)
  118. 118 let n = img.width * img.height
  119. 119 let ch = img.channels
  120. 120● let out : Array[Byte] = Array::make(n * 3, b'\x00')
  121. 121 for i in 0..<n {
  122. 122● let gray = if ch == 1 {
  123. 123● img.data[i].to_int()
  124. 124 } else {
  125. 125● let r = img.data[i * ch].to_int()
  126. 126● let g = img.data[i * ch + 1].to_int()
  127. 127● let b = img.data[i * ch + 2].to_int()
  128. 128 (r * 299 + g * 587 + b * 114) / 1000
  129. 129 }
  130. 130 let (cr, cg, cb) = lut[gray]
  131. 131 out[i * 3] = cr
  132. 132 out[i * 3 + 1] = cg
  133. 133 out[i * 3 + 2] = cb
  134. 134 }
  135. 135 @types.Image::{
  136. 136 width: img.width,
  137. 137 height: img.height,
  138. 138 channels: 3,
  139. 139● data: Bytes::from_array(out),
  140. 140 }
  141. 141}