image.process\color\tonemap_16f.mbt

88.2% · 120 / 136 points · 349 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 变体 Tone mapping 16-bit/float variants
  3. 3///
  4. 4/// 16位 Reinhard/Gamma 色调映射 tonemap_reinhard_16 / tonemap_gamma_16
  5. 5/// 16位/浮点色调映射变体 16-bit/float tone mapping variants
  6. 6// 色调映射 16-bit/float 变体
  7. 7// Gamma + Reinhard,返回 Image16/ImageF
  8. 8
  9. 9///|
  10. 10fn read_float_le_tm(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 write_float_le_tm(data : Array[Byte], offset : Int32, v : Float) -> Unit {
  21. 21● let bits = v.reinterpret_as_int()
  22. 22● data[offset] = (bits & 0xFF).to_byte()
  23. 23● data[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  24. 24● data[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  25. 25● data[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  26. 26}
  27. 27
  28. 28///|
  29. 29fn write_u16_le_tm(data : Array[Byte], offset : Int32, v : Int32) -> Unit {
  30. 30● data[offset] = (v & 0xFF).to_byte()
  31. 31● data[offset + 1] = ((v >> 8) & 0xFF).to_byte()
  32. 32}
  33. 33
  34. 34///|
  35. 35fn log_f_tm(x : Float) -> Float {
  36. 36● Float::from_double(@math.log2(x.to_double()) * 0.6931471805599453)
  37. 37}
  38. 38
  39. 39// ========== Gamma 色调映射 float ==========
  40. 40
  41. 41///|
  42. 42/// Gamma 色调映射(float 输出)。
  43. 43/// 先归一化到 [0,1],再应用 gamma 校正:L_out = L^(1/gamma)。
  44. 44/// gamma > 1 提亮暗部,gamma < 1 压暗。
  45. 45/// @see gamma_tonemap 基础版本,本函数为 ImageF 变体
  46. 46pub fn gamma_tonemap_f(
  47. 47 imgf : @types.ImageF,
  48. 48 gamma : Float,
  49. 49) -> @types.ImageF raise @types.LoadError {
  50. 50 // 输入验证
  51. 51● if imgf.width <= 0 || imgf.height <= 0 {
  52. 52● raise @types.LoadError::DecodeFailed(
  53. 53 "gamma_tonemap_f: 图像尺寸必须大于0",
  54. 54 )
  55. 55 }
  56. 56 if imgf.channels < 1 || imgf.channels > 4 {
  57. 57● raise @types.LoadError::DecodeFailed(
  58. 58 "gamma_tonemap_f: 通道数必须在1-4之间",
  59. 59 )
  60. 60 }
  61. 61 let expected_size = imgf.width * imgf.height * imgf.channels * 4
  62. 62● if imgf.data.length() != expected_size {
  63. 63● raise @types.LoadError::DecodeFailed(
  64. 64 "gamma_tonemap_f: 数据大小不匹配,期望 " +
  65. 65● expected_size.to_string() +
  66. 66 ",实际 " +
  67. 67● imgf.data.length().to_string(),
  68. 68 )
  69. 69 }
  70. 70 if gamma <= 0.0F {
  71. 71● raise @types.LoadError::DecodeFailed("gamma_tonemap_f: gamma必须大于0")
  72. 72 }
  73. 73 let w = imgf.width
  74. 74 let h = imgf.height
  75. 75 let ch = imgf.channels
  76. 76 let n = w * h
  77. 77◐ let color_ch = if ch >= 3 { 3 } else { ch }
  78. 78 // 找最大值用于归一化
  79. 79 let mut max_val = 0.0F
  80. 80 for i in 0..<n {
  81. 81● for c in 0..<color_ch {
  82. 82● let v = read_float_le_tm(imgf.data, (i * ch + c) * 4)
  83. 83 if v > max_val {
  84. 84● max_val = v
  85. 85 }
  86. 86 }
  87. 87 }
  88. 88 if max_val <= 0.0F {
  89. 89● return @types.ImageF::{
  90. 90 width: w,
  91. 91 height: h,
  92. 92 channels: ch,
  93. 93● data: Bytes::from_array(Array::make(n * ch * 4, b'\x00')),
  94. 94 }
  95. 95 }
  96. 96 let inv_gamma = 1.0F / gamma
  97. 97● let out : Array[Byte] = Array::make(n * ch * 4, b'\x00')
  98. 98 for i in 0..<n {
  99. 99● for c in 0..<color_ch {
  100. 100● let v = read_float_le_tm(imgf.data, (i * ch + c) * 4)
  101. 101 let normalized = v / max_val
  102. 102 let mapped = if normalized > 0.0F {
  103. 103● @math.powf(normalized, inv_gamma)
  104. 104 } else {
  105. 105○ 0.0F
  106. 106 }
  107. 107● write_float_le_tm(out, (i * ch + c) * 4, mapped)
  108. 108 }
  109. 109 if ch == 4 {
  110. 110○ write_float_le_tm(out, (i * ch + 3) * 4, 1.0F)
  111. 111 }
  112. 112 }
  113. 113 @types.ImageF::{
  114. 114 width: w,
  115. 115 height: h,
  116. 116 channels: ch,
  117. 117● data: Bytes::from_array(out),
  118. 118 }
  119. 119}
  120. 120
  121. 121// ========== Reinhard 色调映射 float ==========
  122. 122
  123. 123///|
  124. 124/// Reinhard 全局色调映射(float 输出)。
  125. 125/// key 控制整体亮度(默认 0.18),值越大越亮。
  126. 126/// 公式:L_out = L_scaled / (1 + L_scaled),其中 L_scaled = key * L / L_avg。
  127. 127/// @see reinhard_tonemap 基础版本,本函数为 ImageF 变体
  128. 128pub fn reinhard_tonemap_f(
  129. 129 imgf : @types.ImageF,
  130. 130 key~ : Float = 0.18F,
  131. 131) -> @types.ImageF raise @types.LoadError {
  132. 132 // 输入验证
  133. 133● if imgf.width <= 0 || imgf.height <= 0 {
  134. 134● raise @types.LoadError::DecodeFailed(
  135. 135 "reinhard_tonemap_f: 图像尺寸必须大于0",
  136. 136 )
  137. 137 }
  138. 138 if imgf.channels < 1 || imgf.channels > 4 {
  139. 139● raise @types.LoadError::DecodeFailed(
  140. 140 "reinhard_tonemap_f: 通道数必须在1-4之间",
  141. 141 )
  142. 142 }
  143. 143 let expected_size = imgf.width * imgf.height * imgf.channels * 4
  144. 144● if imgf.data.length() != expected_size {
  145. 145● raise @types.LoadError::DecodeFailed(
  146. 146 "reinhard_tonemap_f: 数据大小不匹配,期望 " +
  147. 147● expected_size.to_string() +
  148. 148 ",实际 " +
  149. 149● imgf.data.length().to_string(),
  150. 150 )
  151. 151 }
  152. 152 if key <= 0.0F {
  153. 153● raise @types.LoadError::DecodeFailed("reinhard_tonemap_f: key必须大于0")
  154. 154 }
  155. 155 let w = imgf.width
  156. 156 let h = imgf.height
  157. 157 let ch = imgf.channels
  158. 158 let n = w * h
  159. 159◐ let color_ch = if ch >= 3 { 3 } else { ch }
  160. 160 // 计算对数平均亮度
  161. 161 let mut log_sum = 0.0F
  162. 162 for i in 0..<n {
  163. 163● let r = read_float_le_tm(imgf.data, i * ch * 4)
  164. 164● let g = read_float_le_tm(imgf.data, i * ch * 4 + 4)
  165. 165● let b = read_float_le_tm(imgf.data, i * ch * 4 + 8)
  166. 166 let lum = 0.2126F * r + 0.7152F * g + 0.0722F * b
  167. 167◐ let lum_safe = if lum > 0.0F { lum } else { 0.0001F }
  168. 168● log_sum += log_f_tm(lum_safe)
  169. 169 }
  170. 170● let log_avg = @math.expf(log_sum / Float::from_int(n))
  171. 171 let scale = key / log_avg
  172. 172 // 应用 Reinhard 映射
  173. 173● let out : Array[Byte] = Array::make(n * ch * 4, b'\x00')
  174. 174 for i in 0..<n {
  175. 175● for c in 0..<color_ch {
  176. 176● let v = read_float_le_tm(imgf.data, (i * ch + c) * 4)
  177. 177 let scaled = v * scale
  178. 178 let mapped = scaled / (1.0F + scaled)
  179. 179● write_float_le_tm(out, (i * ch + c) * 4, mapped)
  180. 180 }
  181. 181 if ch == 4 {
  182. 182○ write_float_le_tm(out, (i * ch + 3) * 4, 1.0F)
  183. 183 }
  184. 184 }
  185. 185 @types.ImageF::{
  186. 186 width: w,
  187. 187 height: h,
  188. 188 channels: ch,
  189. 189● data: Bytes::from_array(out),
  190. 190 }
  191. 191}
  192. 192
  193. 193// ========== Gamma 色调映射 16-bit ==========
  194. 194
  195. 195///|
  196. 196/// Gamma 色调映射(16-bit 输出)。
  197. 197/// 输入 ImageF,输出 Image16(0-65535)。
  198. 198/// @see gamma_tonemap 基础版本,本函数为 Image16 变体
  199. 199pub fn gamma_tonemap_16(
  200. 200 imgf : @types.ImageF,
  201. 201 gamma : Float,
  202. 202) -> @types.Image16 raise @types.LoadError {
  203. 203 // 输入验证
  204. 204● if imgf.width <= 0 || imgf.height <= 0 {
  205. 205● raise @types.LoadError::DecodeFailed(
  206. 206 "gamma_tonemap_16: 图像尺寸必须大于0",
  207. 207 )
  208. 208 }
  209. 209 if imgf.channels < 1 || imgf.channels > 4 {
  210. 210● raise @types.LoadError::DecodeFailed(
  211. 211 "gamma_tonemap_16: 通道数必须在1-4之间",
  212. 212 )
  213. 213 }
  214. 214 let expected_size = imgf.width * imgf.height * imgf.channels * 4
  215. 215● if imgf.data.length() != expected_size {
  216. 216● raise @types.LoadError::DecodeFailed(
  217. 217 "gamma_tonemap_16: 数据大小不匹配,期望 " +
  218. 218● expected_size.to_string() +
  219. 219 ",实际 " +
  220. 220● imgf.data.length().to_string(),
  221. 221 )
  222. 222 }
  223. 223 if gamma <= 0.0F {
  224. 224● raise @types.LoadError::DecodeFailed("gamma_tonemap_16: gamma必须大于0")
  225. 225 }
  226. 226 let w = imgf.width
  227. 227 let h = imgf.height
  228. 228 let ch = imgf.channels
  229. 229 let n = w * h
  230. 230◐ let color_ch = if ch >= 3 { 3 } else { ch }
  231. 231 // 找最大值用于归一化
  232. 232 let mut max_val = 0.0F
  233. 233 for i in 0..<n {
  234. 234● for c in 0..<color_ch {
  235. 235● let v = read_float_le_tm(imgf.data, (i * ch + c) * 4)
  236. 236 if v > max_val {
  237. 237● max_val = v
  238. 238 }
  239. 239 }
  240. 240 }
  241. 241 if max_val <= 0.0F {
  242. 242● return @types.Image16::{
  243. 243 width: w,
  244. 244 height: h,
  245. 245 channels: ch,
  246. 246● data: Bytes::from_array(Array::make(n * ch * 2, b'\x00')),
  247. 247 }
  248. 248 }
  249. 249 let inv_gamma = 1.0F / gamma
  250. 250● let out : Array[Byte] = Array::make(n * ch * 2, b'\x00')
  251. 251 for i in 0..<n {
  252. 252● for c in 0..<color_ch {
  253. 253● let v = read_float_le_tm(imgf.data, (i * ch + c) * 4)
  254. 254 let normalized = v / max_val
  255. 255 let mapped = if normalized > 0.0F {
  256. 256● @math.powf(normalized, inv_gamma)
  257. 257 } else {
  258. 258○ 0.0F
  259. 259 }
  260. 260● let val = (mapped * 65535.0F + 0.5F).to_int()
  261. 261◐ let clamped = if val < 0 { 0 } else if val > 65535 { 65535 } else { val }
  262. 262● write_u16_le_tm(out, (i * ch + c) * 2, clamped)
  263. 263 }
  264. 264 if ch == 4 {
  265. 265○ write_u16_le_tm(out, (i * ch + 3) * 2, 65535)
  266. 266 }
  267. 267 }
  268. 268 @types.Image16::{
  269. 269 width: w,
  270. 270 height: h,
  271. 271 channels: ch,
  272. 272● data: Bytes::from_array(out),
  273. 273 }
  274. 274}
  275. 275
  276. 276// ========== Reinhard 色调映射 16-bit ==========
  277. 277
  278. 278///|
  279. 279/// Reinhard 全局色调映射(16-bit 输出)。
  280. 280/// 输入 ImageF,输出 Image16(0-65535)。
  281. 281/// @see reinhard_tonemap 基础版本,本函数为 Image16 变体
  282. 282pub fn reinhard_tonemap_16(
  283. 283 imgf : @types.ImageF,
  284. 284 key~ : Float = 0.18F,
  285. 285) -> @types.Image16 raise @types.LoadError {
  286. 286 // 输入验证
  287. 287● if imgf.width <= 0 || imgf.height <= 0 {
  288. 288● raise @types.LoadError::DecodeFailed(
  289. 289 "reinhard_tonemap_16: 图像尺寸必须大于0",
  290. 290 )
  291. 291 }
  292. 292 if imgf.channels < 1 || imgf.channels > 4 {
  293. 293● raise @types.LoadError::DecodeFailed(
  294. 294 "reinhard_tonemap_16: 通道数必须在1-4之间",
  295. 295 )
  296. 296 }
  297. 297 let expected_size = imgf.width * imgf.height * imgf.channels * 4
  298. 298● if imgf.data.length() != expected_size {
  299. 299● raise @types.LoadError::DecodeFailed(
  300. 300 "reinhard_tonemap_16: 数据大小不匹配,期望 " +
  301. 301● expected_size.to_string() +
  302. 302 ",实际 " +
  303. 303● imgf.data.length().to_string(),
  304. 304 )
  305. 305 }
  306. 306 if key <= 0.0F {
  307. 307● raise @types.LoadError::DecodeFailed(
  308. 308 "reinhard_tonemap_16: key必须大于0",
  309. 309 )
  310. 310 }
  311. 311 let w = imgf.width
  312. 312 let h = imgf.height
  313. 313 let ch = imgf.channels
  314. 314 let n = w * h
  315. 315◐ let color_ch = if ch >= 3 { 3 } else { ch }
  316. 316 // 计算对数平均亮度
  317. 317 let mut log_sum = 0.0F
  318. 318 for i in 0..<n {
  319. 319● let r = read_float_le_tm(imgf.data, i * ch * 4)
  320. 320● let g = read_float_le_tm(imgf.data, i * ch * 4 + 4)
  321. 321● let b = read_float_le_tm(imgf.data, i * ch * 4 + 8)
  322. 322 let lum = 0.2126F * r + 0.7152F * g + 0.0722F * b
  323. 323◐ let lum_safe = if lum > 0.0F { lum } else { 0.0001F }
  324. 324● log_sum += log_f_tm(lum_safe)
  325. 325 }
  326. 326● let log_avg = @math.expf(log_sum / Float::from_int(n))
  327. 327 let scale = key / log_avg
  328. 328 // 应用 Reinhard 映射
  329. 329● let out : Array[Byte] = Array::make(n * ch * 2, b'\x00')
  330. 330 for i in 0..<n {
  331. 331● for c in 0..<color_ch {
  332. 332● let v = read_float_le_tm(imgf.data, (i * ch + c) * 4)
  333. 333 let scaled = v * scale
  334. 334 let mapped = scaled / (1.0F + scaled)
  335. 335● let val = (mapped * 65535.0F + 0.5F).to_int()
  336. 336◐ let clamped = if val < 0 { 0 } else if val > 65535 { 65535 } else { val }
  337. 337● write_u16_le_tm(out, (i * ch + c) * 2, clamped)
  338. 338 }
  339. 339 if ch == 4 {
  340. 340○ write_u16_le_tm(out, (i * ch + 3) * 2, 65535)
  341. 341 }
  342. 342 }
  343. 343 @types.Image16::{
  344. 344 width: w,
  345. 345 height: h,
  346. 346 channels: ch,
  347. 347● data: Bytes::from_array(out),
  348. 348 }
  349. 349}