image.process\color\color_16f.mbt

90.1% · 201 / 223 points · 593 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 变体 Color adjustment 16-bit/float variants
  3. 3///
  4. 4/// 16位亮度/对比度/伽马调整 adjust_brightness_16 / adjust_contrast_16 / adjust_gamma_16
  5. 5/// 16位/浮点亮度/对比度/伽马调整变体 16-bit/float variants of brightness/contrast/gamma adjustment
  6. 6// 色彩调整 16-bit/float 变体
  7. 7// 支持 Image16 / ImageF 两种类型
  8. 8
  9. 9///|
  10. 10fn read_u16_le_c(data : Bytes, offset : Int32) -> Int32 {
  11. 11● data[offset].to_int() + data[offset + 1].to_int() * 256
  12. 12}
  13. 13
  14. 14///|
  15. 15fn write_u16_le_c(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  16. 16● buf[offset] = (val & 0xFF).to_byte()
  17. 17● buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
  18. 18}
  19. 19
  20. 20///|
  21. 21fn write_f32_le(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  22. 22● let bits = val.reinterpret_as_int()
  23. 23● buf[offset] = (bits & 0xFF).to_byte()
  24. 24● buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  25. 25● buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  26. 26● buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  27. 27}
  28. 28
  29. 29///|
  30. 30/// 16-bit 亮度调整。delta ∈ [-65535, 65535]
  31. 31/// @see adjust_brightness 8-bit 版本,本函数为 Image16 变体
  32. 32pub fn adjust_brightness_16(
  33. 33 img : @types.Image16,
  34. 34 delta : Int32,
  35. 35) -> @types.Image16 raise @types.LoadError {
  36. 36 // 输入验证
  37. 37● if img.width <= 0 || img.height <= 0 {
  38. 38● raise @types.LoadError::DecodeFailed(
  39. 39 "adjust_brightness_16: 图像尺寸必须大于0",
  40. 40 )
  41. 41 }
  42. 42 if img.channels < 1 || img.channels > 4 {
  43. 43● raise @types.LoadError::DecodeFailed(
  44. 44 "adjust_brightness_16: 通道数必须在1-4之间",
  45. 45 )
  46. 46 }
  47. 47 let expected_size = img.width * img.height * img.channels * 2
  48. 48● if img.data.length() != expected_size {
  49. 49● raise @types.LoadError::DecodeFailed(
  50. 50 "adjust_brightness_16: 数据大小不匹配,期望 " +
  51. 51● expected_size.to_string() +
  52. 52 ",实际 " +
  53. 53● img.data.length().to_string(),
  54. 54 )
  55. 55 }
  56. 56 let n = img.width * img.height
  57. 57 let ch = img.channels
  58. 58● let color_ch = if ch >= 3 { 3 } else { ch }
  59. 59 let bpp = ch * 2
  60. 60● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  61. 61 for i in 0..<n {
  62. 62● for c in 0..<color_ch {
  63. 63● let offset = i * bpp + c * 2
  64. 64● let val = read_u16_le_c(img.data, offset)
  65. 65 let adjusted = if val + delta < 0 {
  66. 66● 0
  67. 67● } else if val + delta > 65535 {
  68. 68● 65535
  69. 69 } else {
  70. 70● val + delta
  71. 71 }
  72. 72● write_u16_le_c(out, offset, adjusted)
  73. 73 }
  74. 74 if ch == 4 {
  75. 75● let alpha_off = i * bpp + 6
  76. 76 out[alpha_off] = img.data[alpha_off]
  77. 77 out[alpha_off + 1] = img.data[alpha_off + 1]
  78. 78 }
  79. 79 }
  80. 80 @types.Image16::{
  81. 81 width: img.width,
  82. 82 height: img.height,
  83. 83 channels: ch,
  84. 84● data: Bytes::from_array(out),
  85. 85 }
  86. 86}
  87. 87
  88. 88///|
  89. 89/// float 亮度调整。delta 为浮点偏移量
  90. 90/// @see adjust_brightness 8-bit 版本,本函数为 ImageF 变体
  91. 91pub fn adjust_brightness_f(
  92. 92 img : @types.ImageF,
  93. 93 delta : Float,
  94. 94) -> @types.ImageF raise @types.LoadError {
  95. 95 // 输入验证
  96. 96● if img.width <= 0 || img.height <= 0 {
  97. 97● raise @types.LoadError::DecodeFailed(
  98. 98 "adjust_brightness_f: 图像尺寸必须大于0",
  99. 99 )
  100. 100 }
  101. 101 if img.channels < 1 || img.channels > 4 {
  102. 102● raise @types.LoadError::DecodeFailed(
  103. 103 "adjust_brightness_f: 通道数必须在1-4之间",
  104. 104 )
  105. 105 }
  106. 106 let expected_size = img.width * img.height * img.channels * 4
  107. 107● if img.data.length() != expected_size {
  108. 108● raise @types.LoadError::DecodeFailed(
  109. 109 "adjust_brightness_f: 数据大小不匹配,期望 " +
  110. 110● expected_size.to_string() +
  111. 111 ",实际 " +
  112. 112● img.data.length().to_string(),
  113. 113 )
  114. 114 }
  115. 115 let n = img.width * img.height
  116. 116 let ch = img.channels
  117. 117◐ let color_ch = if ch >= 3 { 3 } else { ch }
  118. 118 let bpp = ch * 4
  119. 119● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  120. 120 for i in 0..<n {
  121. 121● for c in 0..<color_ch {
  122. 122● let offset = i * bpp + c * 4
  123. 123● let val = read_float_le(img.data, offset)
  124. 124● write_f32_le(out, offset, val + delta)
  125. 125 }
  126. 126 if ch == 4 {
  127. 127● let alpha_off = i * bpp + 12
  128. 128 for b in 0..<4 {
  129. 129● out[alpha_off + b] = img.data[alpha_off + b]
  130. 130 }
  131. 131 }
  132. 132 }
  133. 133 @types.ImageF::{
  134. 134 width: img.width,
  135. 135 height: img.height,
  136. 136 channels: ch,
  137. 137● data: Bytes::from_array(out),
  138. 138 }
  139. 139}
  140. 140
  141. 141///|
  142. 142/// 16-bit 对比度调整。factor=1.0 为原图,以 32768 为中心
  143. 143/// @see adjust_contrast 8-bit 版本,本函数为 Image16 变体
  144. 144pub fn adjust_contrast_16(
  145. 145 img : @types.Image16,
  146. 146 factor : Float,
  147. 147) -> @types.Image16 raise @types.LoadError {
  148. 148 // 输入验证
  149. 149● if img.width <= 0 || img.height <= 0 {
  150. 150● raise @types.LoadError::DecodeFailed(
  151. 151 "adjust_contrast_16: 图像尺寸必须大于0",
  152. 152 )
  153. 153 }
  154. 154 if img.channels < 1 || img.channels > 4 {
  155. 155● raise @types.LoadError::DecodeFailed(
  156. 156 "adjust_contrast_16: 通道数必须在1-4之间",
  157. 157 )
  158. 158 }
  159. 159 let expected_size = img.width * img.height * img.channels * 2
  160. 160● if img.data.length() != expected_size {
  161. 161● raise @types.LoadError::DecodeFailed(
  162. 162 "adjust_contrast_16: 数据大小不匹配,期望 " +
  163. 163● expected_size.to_string() +
  164. 164 ",实际 " +
  165. 165● img.data.length().to_string(),
  166. 166 )
  167. 167 }
  168. 168 if factor < 0.0F {
  169. 169○ raise @types.LoadError::DecodeFailed(
  170. 170 "adjust_contrast_16: factor必须大于等于0",
  171. 171 )
  172. 172 }
  173. 173 let n = img.width * img.height
  174. 174 let ch = img.channels
  175. 175● let color_ch = if ch >= 3 { 3 } else { ch }
  176. 176 let bpp = ch * 2
  177. 177● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  178. 178 for i in 0..<n {
  179. 179● for c in 0..<color_ch {
  180. 180● let offset = i * bpp + c * 2
  181. 181● let val = read_u16_le_c(img.data, offset)
  182. 182● let adjusted = (Float::from_int(val - 32768) * factor + 32768.0F).to_int()
  183. 183 let clamped = if adjusted < 0 {
  184. 184● 0
  185. 185● } else if adjusted > 65535 {
  186. 186● 65535
  187. 187 } else {
  188. 188● adjusted
  189. 189 }
  190. 190● write_u16_le_c(out, offset, clamped)
  191. 191 }
  192. 192 if ch == 4 {
  193. 193● let alpha_off = i * bpp + 6
  194. 194 out[alpha_off] = img.data[alpha_off]
  195. 195 out[alpha_off + 1] = img.data[alpha_off + 1]
  196. 196 }
  197. 197 }
  198. 198 @types.Image16::{
  199. 199 width: img.width,
  200. 200 height: img.height,
  201. 201 channels: ch,
  202. 202● data: Bytes::from_array(out),
  203. 203 }
  204. 204}
  205. 205
  206. 206///|
  207. 207/// float 对比度调整。factor=1.0 为原图,以 0.5 为中心
  208. 208/// @see adjust_contrast 8-bit 版本,本函数为 ImageF 变体
  209. 209pub fn adjust_contrast_f(
  210. 210 img : @types.ImageF,
  211. 211 factor : Float,
  212. 212) -> @types.ImageF raise @types.LoadError {
  213. 213 // 输入验证
  214. 214● if img.width <= 0 || img.height <= 0 {
  215. 215● raise @types.LoadError::DecodeFailed(
  216. 216 "adjust_contrast_f: 图像尺寸必须大于0",
  217. 217 )
  218. 218 }
  219. 219 if img.channels < 1 || img.channels > 4 {
  220. 220● raise @types.LoadError::DecodeFailed(
  221. 221 "adjust_contrast_f: 通道数必须在1-4之间",
  222. 222 )
  223. 223 }
  224. 224 let expected_size = img.width * img.height * img.channels * 4
  225. 225● if img.data.length() != expected_size {
  226. 226● raise @types.LoadError::DecodeFailed(
  227. 227 "adjust_contrast_f: 数据大小不匹配,期望 " +
  228. 228● expected_size.to_string() +
  229. 229 ",实际 " +
  230. 230● img.data.length().to_string(),
  231. 231 )
  232. 232 }
  233. 233 if factor < 0.0F {
  234. 234○ raise @types.LoadError::DecodeFailed(
  235. 235 "adjust_contrast_f: factor必须大于等于0",
  236. 236 )
  237. 237 }
  238. 238 let n = img.width * img.height
  239. 239 let ch = img.channels
  240. 240● let color_ch = if ch >= 3 { 3 } else { ch }
  241. 241 let bpp = ch * 4
  242. 242● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  243. 243 for i in 0..<n {
  244. 244● for c in 0..<color_ch {
  245. 245● let offset = i * bpp + c * 4
  246. 246● let val = read_float_le(img.data, offset)
  247. 247● write_f32_le(out, offset, (val - 0.5F) * factor + 0.5F)
  248. 248 }
  249. 249 if ch == 4 {
  250. 250● let alpha_off = i * bpp + 12
  251. 251 for b in 0..<4 {
  252. 252● out[alpha_off + b] = img.data[alpha_off + b]
  253. 253 }
  254. 254 }
  255. 255 }
  256. 256 @types.ImageF::{
  257. 257 width: img.width,
  258. 258 height: img.height,
  259. 259 channels: ch,
  260. 260● data: Bytes::from_array(out),
  261. 261 }
  262. 262}
  263. 263
  264. 264///|
  265. 265/// 16-bit Gamma 校正。gamma=1.0 为原图,>1 变暗,<1 变亮。
  266. 266/// 以 65535 为满量程。仅影响颜色通道,alpha 不变。
  267. 267/// @see adjust_gamma 8-bit 版本,本函数为 Image16 变体
  268. 268pub fn adjust_gamma_16(
  269. 269 img : @types.Image16,
  270. 270 gamma : Float,
  271. 271) -> @types.Image16 raise @types.LoadError {
  272. 272 // 输入验证
  273. 273● if img.width <= 0 || img.height <= 0 {
  274. 274● raise @types.LoadError::DecodeFailed(
  275. 275 "adjust_gamma_16: 图像尺寸必须大于0",
  276. 276 )
  277. 277 }
  278. 278 if img.channels < 1 || img.channels > 4 {
  279. 279● raise @types.LoadError::DecodeFailed(
  280. 280 "adjust_gamma_16: 通道数必须在1-4之间",
  281. 281 )
  282. 282 }
  283. 283 let expected_size = img.width * img.height * img.channels * 2
  284. 284● if img.data.length() != expected_size {
  285. 285● raise @types.LoadError::DecodeFailed(
  286. 286 "adjust_gamma_16: 数据大小不匹配,期望 " +
  287. 287● expected_size.to_string() +
  288. 288 ",实际 " +
  289. 289● img.data.length().to_string(),
  290. 290 )
  291. 291 }
  292. 292 if gamma <= 0.0F {
  293. 293● raise @types.LoadError::DecodeFailed("adjust_gamma_16: gamma必须>0")
  294. 294 }
  295. 295 let n = img.width * img.height
  296. 296 let ch = img.channels
  297. 297● let color_ch = if ch >= 3 { 3 } else { ch }
  298. 298 let bpp = ch * 2
  299. 299● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  300. 300 for i in 0..<n {
  301. 301● for c in 0..<color_ch {
  302. 302● let offset = i * bpp + c * 2
  303. 303● let val = read_u16_le_c(img.data, offset)
  304. 304● let normalized = Float::from_int(val) / 65535.0F
  305. 305● let corrected = @math.powf(normalized, gamma)
  306. 306● let new_val = (corrected * 65535.0F + 0.5F).to_int()
  307. 307 let clamped = if new_val < 0 {
  308. 308○ 0
  309. 309● } else if new_val > 65535 {
  310. 310○ 65535
  311. 311 } else {
  312. 312● new_val
  313. 313 }
  314. 314● write_u16_le_c(out, offset, clamped)
  315. 315 }
  316. 316 if ch == 4 {
  317. 317○ let alpha_off = i * bpp + 6
  318. 318 out[alpha_off] = img.data[alpha_off]
  319. 319 out[alpha_off + 1] = img.data[alpha_off + 1]
  320. 320 }
  321. 321 }
  322. 322 @types.Image16::{
  323. 323 width: img.width,
  324. 324 height: img.height,
  325. 325 channels: ch,
  326. 326● data: Bytes::from_array(out),
  327. 327 }
  328. 328}
  329. 329
  330. 330///|
  331. 331/// float Gamma 校正。gamma=1.0 为原图,>1 变暗,<1 变亮。
  332. 332/// 以 1.0 为满量程。仅影响颜色通道,alpha 不变。
  333. 333/// @see adjust_gamma 8-bit 版本,本函数为 ImageF 变体
  334. 334pub fn adjust_gamma_f(
  335. 335 img : @types.ImageF,
  336. 336 gamma : Float,
  337. 337) -> @types.ImageF raise @types.LoadError {
  338. 338 // 输入验证
  339. 339● if img.width <= 0 || img.height <= 0 {
  340. 340● raise @types.LoadError::DecodeFailed(
  341. 341 "adjust_gamma_f: 图像尺寸必须大于0",
  342. 342 )
  343. 343 }
  344. 344 if img.channels < 1 || img.channels > 4 {
  345. 345● raise @types.LoadError::DecodeFailed(
  346. 346 "adjust_gamma_f: 通道数必须在1-4之间",
  347. 347 )
  348. 348 }
  349. 349 let expected_size = img.width * img.height * img.channels * 4
  350. 350● if img.data.length() != expected_size {
  351. 351● raise @types.LoadError::DecodeFailed(
  352. 352 "adjust_gamma_f: 数据大小不匹配,期望 " +
  353. 353● expected_size.to_string() +
  354. 354 ",实际 " +
  355. 355● img.data.length().to_string(),
  356. 356 )
  357. 357 }
  358. 358 if gamma <= 0.0F {
  359. 359● raise @types.LoadError::DecodeFailed("adjust_gamma_f: gamma必须>0")
  360. 360 }
  361. 361 let n = img.width * img.height
  362. 362 let ch = img.channels
  363. 363◐ let color_ch = if ch >= 3 { 3 } else { ch }
  364. 364 let bpp = ch * 4
  365. 365● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  366. 366 for i in 0..<n {
  367. 367● for c in 0..<color_ch {
  368. 368● let offset = i * bpp + c * 4
  369. 369● let val = read_float_le(img.data, offset)
  370. 370 let corrected = if val < 0.0F {
  371. 371○ 0.0F
  372. 372● } else if val > 1.0F {
  373. 373○ 1.0F
  374. 374 } else {
  375. 375● @math.powf(val, gamma)
  376. 376 }
  377. 377● write_f32_le(out, offset, corrected)
  378. 378 }
  379. 379 if ch == 4 {
  380. 380○ let alpha_off = i * bpp + 12
  381. 381 for b in 0..<4 {
  382. 382○ out[alpha_off + b] = img.data[alpha_off + b]
  383. 383 }
  384. 384 }
  385. 385 }
  386. 386 @types.ImageF::{
  387. 387 width: img.width,
  388. 388 height: img.height,
  389. 389 channels: ch,
  390. 390● data: Bytes::from_array(out),
  391. 391 }
  392. 392}
  393. 393
  394. 394///|
  395. 395/// 16-bit 反色(负片)。new = 65535 - old。仅影响颜色通道,alpha 不变。
  396. 396/// @see invert 8-bit 版本,本函数为 Image16 变体
  397. 397pub fn invert_16(img : @types.Image16) -> @types.Image16 raise @types.LoadError {
  398. 398 // 输入验证
  399. 399● if img.width <= 0 || img.height <= 0 {
  400. 400● raise @types.LoadError::DecodeFailed("invert_16: 图像尺寸必须大于0")
  401. 401 }
  402. 402 if img.channels < 1 || img.channels > 4 {
  403. 403● raise @types.LoadError::DecodeFailed(
  404. 404 "invert_16: 通道数必须在1-4之间",
  405. 405 )
  406. 406 }
  407. 407 let expected_size = img.width * img.height * img.channels * 2
  408. 408● if img.data.length() != expected_size {
  409. 409● raise @types.LoadError::DecodeFailed(
  410. 410 "invert_16: 数据大小不匹配,期望 " +
  411. 411● expected_size.to_string() +
  412. 412 ",实际 " +
  413. 413● img.data.length().to_string(),
  414. 414 )
  415. 415 }
  416. 416 let n = img.width * img.height
  417. 417 let ch = img.channels
  418. 418● let color_ch = if ch >= 3 { 3 } else { ch }
  419. 419 let bpp = ch * 2
  420. 420● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  421. 421 for i in 0..<n {
  422. 422● for c in 0..<color_ch {
  423. 423● let offset = i * bpp + c * 2
  424. 424● let val = read_u16_le_c(img.data, offset)
  425. 425● write_u16_le_c(out, offset, 65535 - val)
  426. 426 }
  427. 427 if ch == 4 {
  428. 428○ let alpha_off = i * bpp + 6
  429. 429 out[alpha_off] = img.data[alpha_off]
  430. 430 out[alpha_off + 1] = img.data[alpha_off + 1]
  431. 431 }
  432. 432 }
  433. 433 @types.Image16::{
  434. 434 width: img.width,
  435. 435 height: img.height,
  436. 436 channels: ch,
  437. 437● data: Bytes::from_array(out),
  438. 438 }
  439. 439}
  440. 440
  441. 441///|
  442. 442/// float 反色(负片)。new = 1.0 - old。仅影响颜色通道,alpha 不变。
  443. 443/// @see invert 8-bit 版本,本函数为 ImageF 变体
  444. 444pub fn invert_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  445. 445 // 输入验证
  446. 446● if img.width <= 0 || img.height <= 0 {
  447. 447● raise @types.LoadError::DecodeFailed("invert_f: 图像尺寸必须大于0")
  448. 448 }
  449. 449 if img.channels < 1 || img.channels > 4 {
  450. 450● raise @types.LoadError::DecodeFailed(
  451. 451 "invert_f: 通道数必须在1-4之间",
  452. 452 )
  453. 453 }
  454. 454 let expected_size = img.width * img.height * img.channels * 4
  455. 455● if img.data.length() != expected_size {
  456. 456● raise @types.LoadError::DecodeFailed(
  457. 457 "invert_f: 数据大小不匹配,期望 " +
  458. 458● expected_size.to_string() +
  459. 459 ",实际 " +
  460. 460● img.data.length().to_string(),
  461. 461 )
  462. 462 }
  463. 463 let n = img.width * img.height
  464. 464 let ch = img.channels
  465. 465● let color_ch = if ch >= 3 { 3 } else { ch }
  466. 466 let bpp = ch * 4
  467. 467● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  468. 468 for i in 0..<n {
  469. 469● for c in 0..<color_ch {
  470. 470● let offset = i * bpp + c * 4
  471. 471● let val = read_float_le(img.data, offset)
  472. 472● write_f32_le(out, offset, 1.0F - val)
  473. 473 }
  474. 474 if ch == 4 {
  475. 475○ let alpha_off = i * bpp + 12
  476. 476 for b in 0..<4 {
  477. 477○ out[alpha_off + b] = img.data[alpha_off + b]
  478. 478 }
  479. 479 }
  480. 480 }
  481. 481 @types.ImageF::{
  482. 482 width: img.width,
  483. 483 height: img.height,
  484. 484 channels: ch,
  485. 485● data: Bytes::from_array(out),
  486. 486 }
  487. 487}
  488. 488
  489. 489///|
  490. 490/// 16-bit 转灰度。ITU-R BT.601: Y = 0.299R + 0.587G + 0.114B。
  491. 491/// 输出 channels=1 的 16-bit 灰度图。
  492. 492/// @see to_grayscale 8-bit 版本,本函数为 Image16 变体
  493. 493pub fn to_grayscale_16(
  494. 494 img : @types.Image16,
  495. 495) -> @types.Image16 raise @types.LoadError {
  496. 496 // 输入验证
  497. 497● if img.width <= 0 || img.height <= 0 {
  498. 498● raise @types.LoadError::DecodeFailed(
  499. 499 "to_grayscale_16: 图像尺寸必须大于0",
  500. 500 )
  501. 501 }
  502. 502 if img.channels < 1 || img.channels > 4 {
  503. 503● raise @types.LoadError::DecodeFailed(
  504. 504 "to_grayscale_16: 通道数必须在1-4之间",
  505. 505 )
  506. 506 }
  507. 507 let expected_size = img.width * img.height * img.channels * 2
  508. 508● if img.data.length() != expected_size {
  509. 509● raise @types.LoadError::DecodeFailed(
  510. 510 "to_grayscale_16: 数据大小不匹配,期望 " +
  511. 511● expected_size.to_string() +
  512. 512 ",实际 " +
  513. 513● img.data.length().to_string(),
  514. 514 )
  515. 515 }
  516. 516 let n = img.width * img.height
  517. 517 let ch = img.channels
  518. 518 let bpp = ch * 2
  519. 519● let out : Array[Byte] = Array::make(n * 2, b'\x00')
  520. 520 for i in 0..<n {
  521. 521● if ch >= 3 {
  522. 522● let r = read_u16_le_c(img.data, i * bpp)
  523. 523● let g = read_u16_le_c(img.data, i * bpp + 2)
  524. 524● let b = read_u16_le_c(img.data, i * bpp + 4)
  525. 525● let y = (Float::from_int(r) * 0.299F +
  526. 526● Float::from_int(g) * 0.587F +
  527. 527● Float::from_int(b) * 0.114F +
  528. 528● 0.5F).to_int()
  529. 529◐ let clamped = if y < 0 { 0 } else if y > 65535 { 65535 } else { y }
  530. 530● write_u16_le_c(out, i * 2, clamped)
  531. 531 } else {
  532. 532○ let val = read_u16_le_c(img.data, i * bpp)
  533. 533○ write_u16_le_c(out, i * 2, val)
  534. 534 }
  535. 535 }
  536. 536 @types.Image16::{
  537. 537 width: img.width,
  538. 538 height: img.height,
  539. 539 channels: 1,
  540. 540● data: Bytes::from_array(out),
  541. 541 }
  542. 542}
  543. 543
  544. 544///|
  545. 545/// float 转灰度。ITU-R BT.601: Y = 0.299R + 0.587G + 0.114B。
  546. 546/// 输出 channels=1 的 float 灰度图。
  547. 547/// @see to_grayscale 8-bit 版本,本函数为 ImageF 变体
  548. 548pub fn to_grayscale_f(
  549. 549 img : @types.ImageF,
  550. 550) -> @types.ImageF raise @types.LoadError {
  551. 551 // 输入验证
  552. 552● if img.width <= 0 || img.height <= 0 {
  553. 553● raise @types.LoadError::DecodeFailed(
  554. 554 "to_grayscale_f: 图像尺寸必须大于0",
  555. 555 )
  556. 556 }
  557. 557 if img.channels < 1 || img.channels > 4 {
  558. 558● raise @types.LoadError::DecodeFailed(
  559. 559 "to_grayscale_f: 通道数必须在1-4之间",
  560. 560 )
  561. 561 }
  562. 562 let expected_size = img.width * img.height * img.channels * 4
  563. 563● if img.data.length() != expected_size {
  564. 564● raise @types.LoadError::DecodeFailed(
  565. 565 "to_grayscale_f: 数据大小不匹配,期望 " +
  566. 566● expected_size.to_string() +
  567. 567 ",实际 " +
  568. 568● img.data.length().to_string(),
  569. 569 )
  570. 570 }
  571. 571 let n = img.width * img.height
  572. 572 let ch = img.channels
  573. 573 let bpp = ch * 4
  574. 574● let out : Array[Byte] = Array::make(n * 4, b'\x00')
  575. 575 for i in 0..<n {
  576. 576● if ch >= 3 {
  577. 577● let r = read_float_le(img.data, i * bpp)
  578. 578● let g = read_float_le(img.data, i * bpp + 4)
  579. 579● let b = read_float_le(img.data, i * bpp + 8)
  580. 580 let y = r * 0.299F + g * 0.587F + b * 0.114F
  581. 581● write_f32_le(out, i * 4, y)
  582. 582 } else {
  583. 583○ let val = read_float_le(img.data, i * bpp)
  584. 584○ write_f32_le(out, i * 4, val)
  585. 585 }
  586. 586 }
  587. 587 @types.ImageF::{
  588. 588 width: img.width,
  589. 589 height: img.height,
  590. 590 channels: 1,
  591. 591● data: Bytes::from_array(out),
  592. 592 }
  593. 593}