image.process\color\tonemap.mbt

79.1% · 53 / 67 points · 172 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/// # 色调映射 Tone mapping
  3. 3///
  4. 4/// Reinhard/Gamma 色调映射 tonemap_reinhard / tonemap_gamma
  5. 5/// Reinhard + Gamma 色调映射 Reinhard + Gamma tone mapping
  6. 6// 色调映射(纯 MoonBit):Reinhard + Gamma
  7. 7// 将 HDR float 图像 (ImageF) 映射到 LDR 8-bit 图像 (Image)
  8. 8
  9. 9///|
  10. 10fn read_float_le(data : Bytes, offset : Int32) -> Float {
  11. 11● let b0 = data[offset].to_int()
  12. 12● let b1 = data[offset + 1].to_int()
  13. 13● let b2 = data[offset + 2].to_int()
  14. 14● let b3 = data[offset + 3].to_int()
  15. 15 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  16. 16● Float::reinterpret_from_int(bits)
  17. 17}
  18. 18
  19. 19///|
  20. 20fn log_f(x : Float) -> Float {
  21. 21● Float::from_double(@math.log2(x.to_double()) * 0.6931471805599453)
  22. 22}
  23. 23
  24. 24///|
  25. 25/// Reinhard 全局色调映射。
  26. 26/// key 控制整体亮度(默认 0.18),值越大越亮。
  27. 27/// 公式:L_out = L_scaled / (1 + L_scaled),其中 L_scaled = key * L / L_avg。
  28. 28pub fn reinhard_tonemap(
  29. 29 imgf : @types.ImageF,
  30. 30 key~ : Float = 0.18F,
  31. 31) -> @types.Image raise @types.LoadError {
  32. 32 // 输入验证
  33. 33● if imgf.width <= 0 || imgf.height <= 0 {
  34. 34○ raise @types.LoadError::DecodeFailed(
  35. 35 "reinhard_tonemap: 图像尺寸必须大于0",
  36. 36 )
  37. 37 }
  38. 38 if imgf.channels < 1 || imgf.channels > 4 {
  39. 39○ raise @types.LoadError::DecodeFailed(
  40. 40 "reinhard_tonemap: 通道数必须在1-4之间",
  41. 41 )
  42. 42 }
  43. 43 let expected_size = imgf.width * imgf.height * imgf.channels * 4
  44. 44● if imgf.data.length() != expected_size {
  45. 45○ raise @types.LoadError::DecodeFailed(
  46. 46 "reinhard_tonemap: 数据大小不匹配,期望 " +
  47. 47○ expected_size.to_string() +
  48. 48 ",实际 " +
  49. 49○ imgf.data.length().to_string(),
  50. 50 )
  51. 51 }
  52. 52 if key <= 0.0F {
  53. 53● raise @types.LoadError::DecodeFailed("reinhard_tonemap: key必须大于0")
  54. 54 }
  55. 55 let w = imgf.width
  56. 56 let h = imgf.height
  57. 57 let ch = imgf.channels
  58. 58 let n = w * h
  59. 59◐ let color_ch = if ch >= 3 { 3 } else { ch }
  60. 60 // 计算对数平均亮度
  61. 61 let mut log_sum = 0.0F
  62. 62 for i in 0..<n {
  63. 63● let r = read_float_le(imgf.data, i * ch * 4)
  64. 64● let g = read_float_le(imgf.data, i * ch * 4 + 4)
  65. 65● let b = read_float_le(imgf.data, i * ch * 4 + 8)
  66. 66 let lum = 0.2126F * r + 0.7152F * g + 0.0722F * b
  67. 67● let lum_safe = if lum > 0.0F { lum } else { 0.0001F }
  68. 68● log_sum += log_f(lum_safe)
  69. 69 }
  70. 70● let log_avg = @math.expf(log_sum / Float::from_int(n))
  71. 71 let scale = key / log_avg
  72. 72 // 应用 Reinhard 映射
  73. 73● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  74. 74 for i in 0..<n {
  75. 75● for c in 0..<color_ch {
  76. 76● let v = read_float_le(imgf.data, (i * ch + c) * 4)
  77. 77 let scaled = v * scale
  78. 78 let mapped = scaled / (1.0F + scaled)
  79. 79● let val = (mapped * 255.0F + 0.5F).to_int().clamp(min=0, max=255)
  80. 80● out[i * ch + c] = val.to_byte()
  81. 81 }
  82. 82 if ch == 4 {
  83. 83● out[i * ch + 3] = b'\xFF'
  84. 84 }
  85. 85 }
  86. 86 @types.Image::{
  87. 87 width: w,
  88. 88 height: h,
  89. 89 channels: ch,
  90. 90● data: Bytes::from_array(out),
  91. 91 }
  92. 92}
  93. 93
  94. 94///|
  95. 95/// Gamma 色调映射。
  96. 96/// 先归一化到 [0,1],再应用 gamma 校正:L_out = L^(1/gamma)。
  97. 97/// gamma > 1 提亮暗部,gamma < 1 压暗。
  98. 98pub fn gamma_tonemap(
  99. 99 imgf : @types.ImageF,
  100. 100 gamma : Float,
  101. 101) -> @types.Image raise @types.LoadError {
  102. 102 // 输入验证
  103. 103● if imgf.width <= 0 || imgf.height <= 0 {
  104. 104○ raise @types.LoadError::DecodeFailed(
  105. 105 "gamma_tonemap: 图像尺寸必须大于0",
  106. 106 )
  107. 107 }
  108. 108 if imgf.channels < 1 || imgf.channels > 4 {
  109. 109○ raise @types.LoadError::DecodeFailed(
  110. 110 "gamma_tonemap: 通道数必须在1-4之间",
  111. 111 )
  112. 112 }
  113. 113 let expected_size = imgf.width * imgf.height * imgf.channels * 4
  114. 114● if imgf.data.length() != expected_size {
  115. 115○ raise @types.LoadError::DecodeFailed(
  116. 116 "gamma_tonemap: 数据大小不匹配,期望 " +
  117. 117○ expected_size.to_string() +
  118. 118 ",实际 " +
  119. 119○ imgf.data.length().to_string(),
  120. 120 )
  121. 121 }
  122. 122 if gamma <= 0.0F {
  123. 123● raise @types.LoadError::DecodeFailed("gamma_tonemap: gamma必须大于0")
  124. 124 }
  125. 125 let w = imgf.width
  126. 126 let h = imgf.height
  127. 127 let ch = imgf.channels
  128. 128 let n = w * h
  129. 129◐ let color_ch = if ch >= 3 { 3 } else { ch }
  130. 130 // 找最大值用于归一化
  131. 131 let mut max_val = 0.0F
  132. 132 for i in 0..<n {
  133. 133● for c in 0..<color_ch {
  134. 134● let v = read_float_le(imgf.data, (i * ch + c) * 4)
  135. 135 if v > max_val {
  136. 136● max_val = v
  137. 137 }
  138. 138 }
  139. 139 }
  140. 140 if max_val <= 0.0F {
  141. 141● return @types.Image::{
  142. 142 width: w,
  143. 143 height: h,
  144. 144 channels: ch,
  145. 145● data: Bytes::from_array(Array::make(n * ch, b'\x00')),
  146. 146 }
  147. 147 }
  148. 148 let inv_gamma = 1.0F / gamma
  149. 149● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  150. 150 for i in 0..<n {
  151. 151● for c in 0..<color_ch {
  152. 152● let v = read_float_le(imgf.data, (i * ch + c) * 4)
  153. 153 let normalized = v / max_val
  154. 154 let mapped = if normalized > 0.0F {
  155. 155● @math.powf(normalized, inv_gamma)
  156. 156 } else {
  157. 157● 0.0F
  158. 158 }
  159. 159● let val = (mapped * 255.0F + 0.5F).to_int().clamp(min=0, max=255)
  160. 160● out[i * ch + c] = val.to_byte()
  161. 161 }
  162. 162 if ch == 4 {
  163. 163● out[i * ch + 3] = b'\xFF'
  164. 164 }
  165. 165 }
  166. 166 @types.Image::{
  167. 167 width: w,
  168. 168 height: h,
  169. 169 channels: ch,
  170. 170● data: Bytes::from_array(out),
  171. 171 }
  172. 172}