image.process\color\color_convert_16f.mbt

95.9% · 187 / 195 points · 639 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/// # 色彩转换 16f color convert 16f
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 颜色空间转换 16-bit/float 变体
  7. 7// 支持 Image16 / ImageF 两种类型
  8. 8
  9. 9// 复用 color_16f.mbt 中的 read_u16_le_c / write_u16_le_c / write_f32_le
  10. 10// 复用 tonemap.mbt 中的 read_float_le
  11. 11
  12. 12///|
  13. 13/// 16-bit 去除 alpha 通道(RGBA → RGB,channels=3)
  14. 14/// @see to_rgb 8-bit 版本,本函数为 Image16 变体
  15. 15pub fn to_rgb_16(img : @types.Image16) -> @types.Image16 raise @types.LoadError {
  16. 16 // 输入验证
  17. 17● if img.width < 0 || img.height < 0 {
  18. 18● raise @types.LoadError::DecodeFailed("to_rgb_16: 图像尺寸不能为负")
  19. 19 }
  20. 20 if img.channels < 1 || img.channels > 4 {
  21. 21● raise @types.LoadError::DecodeFailed(
  22. 22 "to_rgb_16: 通道数必须在1-4之间",
  23. 23 )
  24. 24 }
  25. 25 let expected_size = img.width * img.height * img.channels * 2
  26. 26● if img.data.length() != expected_size {
  27. 27● raise @types.LoadError::DecodeFailed(
  28. 28 "to_rgb_16: 数据大小不匹配,期望 " +
  29. 29● expected_size.to_string() +
  30. 30 ",实际 " +
  31. 31● img.data.length().to_string(),
  32. 32 )
  33. 33 }
  34. 34 if img.channels == 3 {
  35. 35● return img
  36. 36 }
  37. 37 if img.channels < 3 {
  38. 38● return img
  39. 39 }
  40. 40 let n = img.width * img.height
  41. 41 let src_ch = img.channels
  42. 42 let src_bpp = src_ch * 2
  43. 43 let out_bpp = 6
  44. 44● let out : Array[Byte] = Array::make(n * out_bpp, b'\x00')
  45. 45 for i in 0..<n {
  46. 46● for c in 0..<3 {
  47. 47● let src_off = i * src_bpp + c * 2
  48. 48 let dst_off = i * out_bpp + c * 2
  49. 49 out[dst_off] = img.data[src_off]
  50. 50 out[dst_off + 1] = img.data[src_off + 1]
  51. 51 }
  52. 52 }
  53. 53 @types.Image16::{
  54. 54 width: img.width,
  55. 55 height: img.height,
  56. 56 channels: 3,
  57. 57● data: Bytes::from_array(out),
  58. 58 }
  59. 59}
  60. 60
  61. 61///|
  62. 62/// float 去除 alpha 通道(RGBA → RGB,channels=3)
  63. 63/// @see to_rgb 8-bit 版本,本函数为 ImageF 变体
  64. 64pub fn to_rgb_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  65. 65 // 输入验证
  66. 66● if img.width <= 0 || img.height <= 0 {
  67. 67● raise @types.LoadError::DecodeFailed("to_rgb_f: 图像尺寸必须大于0")
  68. 68 }
  69. 69 if img.channels < 1 || img.channels > 4 {
  70. 70● raise @types.LoadError::DecodeFailed(
  71. 71 "to_rgb_f: 通道数必须在1-4之间",
  72. 72 )
  73. 73 }
  74. 74 let expected_size = img.width * img.height * img.channels * 4
  75. 75● if img.data.length() != expected_size {
  76. 76● raise @types.LoadError::DecodeFailed(
  77. 77 "to_rgb_f: 数据大小不匹配,期望 " +
  78. 78● expected_size.to_string() +
  79. 79 ",实际 " +
  80. 80● img.data.length().to_string(),
  81. 81 )
  82. 82 }
  83. 83 if img.channels == 3 {
  84. 84○ return img
  85. 85 }
  86. 86 if img.channels < 3 {
  87. 87○ return img
  88. 88 }
  89. 89 let n = img.width * img.height
  90. 90 let src_ch = img.channels
  91. 91 let src_bpp = src_ch * 4
  92. 92 let out_bpp = 12
  93. 93● let out : Array[Byte] = Array::make(n * out_bpp, b'\x00')
  94. 94 for i in 0..<n {
  95. 95● for c in 0..<3 {
  96. 96● let src_off = i * src_bpp + c * 4
  97. 97 let dst_off = i * out_bpp + c * 4
  98. 98 for b in 0..<4 {
  99. 99● out[dst_off + b] = img.data[src_off + b]
  100. 100 }
  101. 101 }
  102. 102 }
  103. 103 @types.ImageF::{
  104. 104 width: img.width,
  105. 105 height: img.height,
  106. 106 channels: 3,
  107. 107● data: Bytes::from_array(out),
  108. 108 }
  109. 109}
  110. 110
  111. 111///|
  112. 112/// 16-bit 添加 alpha 通道(RGB → RGBA,channels=4,alpha=65535)
  113. 113/// @see to_rgba 8-bit 版本,本函数为 Image16 变体
  114. 114pub fn to_rgba_16(
  115. 115 img : @types.Image16,
  116. 116) -> @types.Image16 raise @types.LoadError {
  117. 117 // 输入验证
  118. 118● if img.width < 0 || img.height < 0 {
  119. 119● raise @types.LoadError::DecodeFailed("to_rgba_16: 图像尺寸不能为负")
  120. 120 }
  121. 121 if img.channels < 1 || img.channels > 4 {
  122. 122● raise @types.LoadError::DecodeFailed(
  123. 123 "to_rgba_16: 通道数必须在1-4之间",
  124. 124 )
  125. 125 }
  126. 126 let expected_size = img.width * img.height * img.channels * 2
  127. 127● if img.data.length() != expected_size {
  128. 128● raise @types.LoadError::DecodeFailed(
  129. 129 "to_rgba_16: 数据大小不匹配,期望 " +
  130. 130● expected_size.to_string() +
  131. 131 ",实际 " +
  132. 132● img.data.length().to_string(),
  133. 133 )
  134. 134 }
  135. 135 if img.channels == 4 {
  136. 136● return img
  137. 137 }
  138. 138 let n = img.width * img.height
  139. 139 let src_ch = img.channels
  140. 140 let src_bpp = src_ch * 2
  141. 141 let out_bpp = 8
  142. 142● let out : Array[Byte] = Array::make(n * out_bpp, b'\x00')
  143. 143 for i in 0..<n {
  144. 144● for c in 0..<3 {
  145. 145◐ let src_off = if c < src_ch { i * src_bpp + c * 2 } else { -1 }
  146. 146 let dst_off = i * out_bpp + c * 2
  147. 147 if src_off >= 0 {
  148. 148● out[dst_off] = img.data[src_off]
  149. 149 out[dst_off + 1] = img.data[src_off + 1]
  150. 150 }
  151. 151 }
  152. 152 // alpha = 65535
  153. 153 let alpha_off = i * out_bpp + 6
  154. 154 out[alpha_off] = b'\xFF'
  155. 155 out[alpha_off + 1] = b'\xFF'
  156. 156 }
  157. 157 @types.Image16::{
  158. 158 width: img.width,
  159. 159 height: img.height,
  160. 160 channels: 4,
  161. 161● data: Bytes::from_array(out),
  162. 162 }
  163. 163}
  164. 164
  165. 165///|
  166. 166/// float 添加 alpha 通道(RGB → RGBA,channels=4,alpha=1.0)
  167. 167/// @see to_rgba 8-bit 版本,本函数为 ImageF 变体
  168. 168pub fn to_rgba_f(img : @types.ImageF) -> @types.ImageF raise @types.LoadError {
  169. 169 // 输入验证
  170. 170● if img.width <= 0 || img.height <= 0 {
  171. 171● raise @types.LoadError::DecodeFailed("to_rgba_f: 图像尺寸必须大于0")
  172. 172 }
  173. 173 if img.channels < 1 || img.channels > 4 {
  174. 174● raise @types.LoadError::DecodeFailed(
  175. 175 "to_rgba_f: 通道数必须在1-4之间",
  176. 176 )
  177. 177 }
  178. 178 let expected_size = img.width * img.height * img.channels * 4
  179. 179● if img.data.length() != expected_size {
  180. 180● raise @types.LoadError::DecodeFailed(
  181. 181 "to_rgba_f: 数据大小不匹配,期望 " +
  182. 182● expected_size.to_string() +
  183. 183 ",实际 " +
  184. 184● img.data.length().to_string(),
  185. 185 )
  186. 186 }
  187. 187 if img.channels == 4 {
  188. 188○ return img
  189. 189 }
  190. 190 let n = img.width * img.height
  191. 191 let src_ch = img.channels
  192. 192 let src_bpp = src_ch * 4
  193. 193 let out_bpp = 16
  194. 194● let out : Array[Byte] = Array::make(n * out_bpp, b'\x00')
  195. 195 for i in 0..<n {
  196. 196● for c in 0..<3 {
  197. 197● if c < src_ch {
  198. 198● let src_off = i * src_bpp + c * 4
  199. 199 let dst_off = i * out_bpp + c * 4
  200. 200 for b in 0..<4 {
  201. 201● out[dst_off + b] = img.data[src_off + b]
  202. 202 }
  203. 203 }
  204. 204 }
  205. 205 // alpha = 1.0
  206. 206● write_f32_le(out, i * out_bpp + 12, 1.0F)
  207. 207 }
  208. 208 @types.ImageF::{
  209. 209 width: img.width,
  210. 210 height: img.height,
  211. 211 channels: 4,
  212. 212● data: Bytes::from_array(out),
  213. 213 }
  214. 214}
  215. 215
  216. 216///|
  217. 217/// 16-bit 预乘 alpha(RGBA → RGBA premultiplied)。要求 channels=4。
  218. 218/// @see premultiply_alpha 8-bit 版本,本函数为 Image16 变体
  219. 219pub fn premultiply_alpha_16(
  220. 220 img : @types.Image16,
  221. 221) -> @types.Image16 raise @types.LoadError {
  222. 222 // 输入验证
  223. 223● if img.width <= 0 || img.height <= 0 {
  224. 224● raise @types.LoadError::DecodeFailed(
  225. 225 "premultiply_alpha_16: 图像尺寸必须大于0",
  226. 226 )
  227. 227 }
  228. 228 if img.channels != 4 {
  229. 229● raise @types.LoadError::DecodeFailed(
  230. 230 "premultiply_alpha_16 需要 channels=4 (RGBA)",
  231. 231 )
  232. 232 }
  233. 233 let expected_size = img.width * img.height * img.channels * 2
  234. 234● if img.data.length() != expected_size {
  235. 235● raise @types.LoadError::DecodeFailed(
  236. 236 "premultiply_alpha_16: 数据大小不匹配,期望 " +
  237. 237● expected_size.to_string() +
  238. 238 ",实际 " +
  239. 239● img.data.length().to_string(),
  240. 240 )
  241. 241 }
  242. 242 let n = img.width * img.height
  243. 243 let bpp = 8
  244. 244● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  245. 245 for i in 0..<n {
  246. 246● let r = read_u16_le_c(img.data, i * bpp)
  247. 247● let g = read_u16_le_c(img.data, i * bpp + 2)
  248. 248● let b = read_u16_le_c(img.data, i * bpp + 4)
  249. 249● let a = read_u16_le_c(img.data, i * bpp + 6)
  250. 250● write_u16_le_c(out, i * bpp, r * a / 65535)
  251. 251● write_u16_le_c(out, i * bpp + 2, g * a / 65535)
  252. 252● write_u16_le_c(out, i * bpp + 4, b * a / 65535)
  253. 253● write_u16_le_c(out, i * bpp + 6, a)
  254. 254 }
  255. 255 @types.Image16::{
  256. 256 width: img.width,
  257. 257 height: img.height,
  258. 258 channels: 4,
  259. 259● data: Bytes::from_array(out),
  260. 260 }
  261. 261}
  262. 262
  263. 263///|
  264. 264/// float 预乘 alpha(RGBA → RGBA premultiplied)。要求 channels=4。
  265. 265/// @see premultiply_alpha 8-bit 版本,本函数为 ImageF 变体
  266. 266pub fn premultiply_alpha_f(
  267. 267 img : @types.ImageF,
  268. 268) -> @types.ImageF raise @types.LoadError {
  269. 269 // 输入验证
  270. 270● if img.width <= 0 || img.height <= 0 {
  271. 271● raise @types.LoadError::DecodeFailed(
  272. 272 "premultiply_alpha_f: 图像尺寸必须大于0",
  273. 273 )
  274. 274 }
  275. 275 if img.channels != 4 {
  276. 276● raise @types.LoadError::DecodeFailed(
  277. 277 "premultiply_alpha_f 需要 channels=4 (RGBA)",
  278. 278 )
  279. 279 }
  280. 280 let expected_size = img.width * img.height * img.channels * 4
  281. 281● if img.data.length() != expected_size {
  282. 282● raise @types.LoadError::DecodeFailed(
  283. 283 "premultiply_alpha_f: 数据大小不匹配,期望 " +
  284. 284● expected_size.to_string() +
  285. 285 ",实际 " +
  286. 286● img.data.length().to_string(),
  287. 287 )
  288. 288 }
  289. 289 let n = img.width * img.height
  290. 290 let bpp = 16
  291. 291● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  292. 292 for i in 0..<n {
  293. 293● let r = read_float_le(img.data, i * bpp)
  294. 294● let g = read_float_le(img.data, i * bpp + 4)
  295. 295● let b = read_float_le(img.data, i * bpp + 8)
  296. 296● let a = read_float_le(img.data, i * bpp + 12)
  297. 297● write_f32_le(out, i * bpp, r * a)
  298. 298● write_f32_le(out, i * bpp + 4, g * a)
  299. 299● write_f32_le(out, i * bpp + 8, b * a)
  300. 300● write_f32_le(out, i * bpp + 12, a)
  301. 301 }
  302. 302 @types.ImageF::{
  303. 303 width: img.width,
  304. 304 height: img.height,
  305. 305 channels: 4,
  306. 306● data: Bytes::from_array(out),
  307. 307 }
  308. 308}
  309. 309
  310. 310///|
  311. 311/// 16-bit 反预乘 alpha(RGBA premA → RGBA unpremultiplied)。要求 channels=4。
  312. 312/// @see unpremultiply_alpha 8-bit 版本,本函数为 Image16 变体
  313. 313pub fn unpremultiply_alpha_16(
  314. 314 img : @types.Image16,
  315. 315) -> @types.Image16 raise @types.LoadError {
  316. 316 // 输入验证
  317. 317● if img.width <= 0 || img.height <= 0 {
  318. 318● raise @types.LoadError::DecodeFailed(
  319. 319 "unpremultiply_alpha_16: 图像尺寸必须大于0",
  320. 320 )
  321. 321 }
  322. 322 if img.channels != 4 {
  323. 323● raise @types.LoadError::DecodeFailed(
  324. 324 "unpremultiply_alpha_16 需要 channels=4 (RGBA)",
  325. 325 )
  326. 326 }
  327. 327 let expected_size = img.width * img.height * img.channels * 2
  328. 328● if img.data.length() != expected_size {
  329. 329● raise @types.LoadError::DecodeFailed(
  330. 330 "unpremultiply_alpha_16: 数据大小不匹配,期望 " +
  331. 331● expected_size.to_string() +
  332. 332 ",实际 " +
  333. 333● img.data.length().to_string(),
  334. 334 )
  335. 335 }
  336. 336 let n = img.width * img.height
  337. 337 let bpp = 8
  338. 338● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  339. 339 for i in 0..<n {
  340. 340● let r = read_u16_le_c(img.data, i * bpp)
  341. 341● let g = read_u16_le_c(img.data, i * bpp + 2)
  342. 342● let b = read_u16_le_c(img.data, i * bpp + 4)
  343. 343● let a = read_u16_le_c(img.data, i * bpp + 6)
  344. 344 if a == 0 {
  345. 345● write_u16_le_c(out, i * bpp, 0)
  346. 346● write_u16_le_c(out, i * bpp + 2, 0)
  347. 347● write_u16_le_c(out, i * bpp + 4, 0)
  348. 348● write_u16_le_c(out, i * bpp + 6, 0)
  349. 349 } else {
  350. 350● write_u16_le_c(out, i * bpp, r * 65535 / a)
  351. 351● write_u16_le_c(out, i * bpp + 2, g * 65535 / a)
  352. 352● write_u16_le_c(out, i * bpp + 4, b * 65535 / a)
  353. 353● write_u16_le_c(out, i * bpp + 6, a)
  354. 354 }
  355. 355 }
  356. 356 @types.Image16::{
  357. 357 width: img.width,
  358. 358 height: img.height,
  359. 359 channels: 4,
  360. 360● data: Bytes::from_array(out),
  361. 361 }
  362. 362}
  363. 363
  364. 364///|
  365. 365/// float 反预乘 alpha(RGBA premA → RGBA unpremultiplied)。要求 channels=4。
  366. 366/// @see unpremultiply_alpha 8-bit 版本,本函数为 ImageF 变体
  367. 367pub fn unpremultiply_alpha_f(
  368. 368 img : @types.ImageF,
  369. 369) -> @types.ImageF raise @types.LoadError {
  370. 370 // 输入验证
  371. 371● if img.width <= 0 || img.height <= 0 {
  372. 372● raise @types.LoadError::DecodeFailed(
  373. 373 "unpremultiply_alpha_f: 图像尺寸必须大于0",
  374. 374 )
  375. 375 }
  376. 376 if img.channels != 4 {
  377. 377● raise @types.LoadError::DecodeFailed(
  378. 378 "unpremultiply_alpha_f 需要 channels=4 (RGBA)",
  379. 379 )
  380. 380 }
  381. 381 let expected_size = img.width * img.height * img.channels * 4
  382. 382● if img.data.length() != expected_size {
  383. 383● raise @types.LoadError::DecodeFailed(
  384. 384 "unpremultiply_alpha_f: 数据大小不匹配,期望 " +
  385. 385● expected_size.to_string() +
  386. 386 ",实际 " +
  387. 387● img.data.length().to_string(),
  388. 388 )
  389. 389 }
  390. 390 let n = img.width * img.height
  391. 391 let bpp = 16
  392. 392● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  393. 393 for i in 0..<n {
  394. 394● let r = read_float_le(img.data, i * bpp)
  395. 395● let g = read_float_le(img.data, i * bpp + 4)
  396. 396● let b = read_float_le(img.data, i * bpp + 8)
  397. 397● let a = read_float_le(img.data, i * bpp + 12)
  398. 398 if a < 0.000001F {
  399. 399● write_f32_le(out, i * bpp, 0.0F)
  400. 400● write_f32_le(out, i * bpp + 4, 0.0F)
  401. 401● write_f32_le(out, i * bpp + 8, 0.0F)
  402. 402● write_f32_le(out, i * bpp + 12, 0.0F)
  403. 403 } else {
  404. 404● write_f32_le(out, i * bpp, r / a)
  405. 405● write_f32_le(out, i * bpp + 4, g / a)
  406. 406● write_f32_le(out, i * bpp + 8, b / a)
  407. 407● write_f32_le(out, i * bpp + 12, a)
  408. 408 }
  409. 409 }
  410. 410 @types.ImageF::{
  411. 411 width: img.width,
  412. 412 height: img.height,
  413. 413 channels: 4,
  414. 414● data: Bytes::from_array(out),
  415. 415 }
  416. 416}
  417. 417
  418. 418// ===== 像素级颜色空间转换函数 =====
  419. 419
  420. 420///|
  421. 421/// 16-bit RGB → YCbCr(ITU-R BT.601)。输入范围 0-65535,输出范围 0-65535。
  422. 422/// @see rgb_to_ycbcr 8-bit 版本,本函数为 Image16 变体
  423. 423pub fn rgb_to_ycbcr_16(
  424. 424 r : Int32,
  425. 425 g : Int32,
  426. 426 b : Int32,
  427. 427) -> (Int32, Int32, Int32) raise @types.LoadError {
  428. 428 // 输入验证
  429. 429● if r < 0 || r > 65535 || g < 0 || g > 65535 || b < 0 || b > 65535 {
  430. 430● raise @types.LoadError::DecodeFailed(
  431. 431 "rgb_to_ycbcr_16: r,g,b必须在[0,65535]范围内",
  432. 432 )
  433. 433 }
  434. 434 let y = (299 * r + 587 * g + 114 * b) / 1000
  435. 435 let cb = (-169 * r - 331 * g + 500 * b) / 1000 + 32768
  436. 436 let cr = (500 * r - 419 * g - 81 * b) / 1000 + 32768
  437. 437 (
  438. 438● y.clamp(min=0, max=65535),
  439. 439● cb.clamp(min=0, max=65535),
  440. 440● cr.clamp(min=0, max=65535),
  441. 441 )
  442. 442}
  443. 443
  444. 444///|
  445. 445/// float RGB → YCbCr(ITU-R BT.601)。输入范围 0.0-1.0,输出范围 0.0-1.0。
  446. 446/// @see rgb_to_ycbcr 8-bit 版本,本函数为 ImageF 变体
  447. 447pub fn rgb_to_ycbcr_f(
  448. 448 r : Float,
  449. 449 g : Float,
  450. 450 b : Float,
  451. 451) -> (Float, Float, Float) raise @types.LoadError {
  452. 452 // 输入验证
  453. 453● if r < 0.0F || r > 1.0F || g < 0.0F || g > 1.0F || b < 0.0F || b > 1.0F {
  454. 454● raise @types.LoadError::DecodeFailed(
  455. 455 "rgb_to_ycbcr_f: r,g,b必须在[0,1]范围内",
  456. 456 )
  457. 457 }
  458. 458 let y = 0.299F * r + 0.587F * g + 0.114F * b
  459. 459 let cb = -0.169F * r - 0.331F * g + 0.5F * b + 0.5F
  460. 460 let cr = 0.5F * r - 0.419F * g - 0.081F * b + 0.5F
  461. 461 (
  462. 462● @types.clamp_float(y, 0.0F, 1.0F),
  463. 463● @types.clamp_float(cb, 0.0F, 1.0F),
  464. 464● @types.clamp_float(cr, 0.0F, 1.0F),
  465. 465 )
  466. 466}
  467. 467
  468. 468///|
  469. 469/// 16-bit YCbCr → RGB(ITU-R BT.601)。输入范围 0-65535。
  470. 470/// @see ycbcr_to_rgb 8-bit 版本,本函数为 Image16 变体
  471. 471pub fn ycbcr_to_rgb_16(
  472. 472 y : Int32,
  473. 473 cb : Int32,
  474. 474 cr : Int32,
  475. 475) -> (Int32, Int32, Int32) raise @types.LoadError {
  476. 476 // 输入验证
  477. 477● if y < 0 || y > 65535 || cb < 0 || cb > 65535 || cr < 0 || cr > 65535 {
  478. 478● raise @types.LoadError::DecodeFailed(
  479. 479 "ycbcr_to_rgb_16: y,cb,cr必须在[0,65535]范围内",
  480. 480 )
  481. 481 }
  482. 482 let cb0 = cb - 32768
  483. 483 let cr0 = cr - 32768
  484. 484 let r = y + 1402 * cr0 / 1000
  485. 485 let g = y - 344 * cb0 / 1000 - 714 * cr0 / 1000
  486. 486 let b = y + 1772 * cb0 / 1000
  487. 487 (
  488. 488● r.clamp(min=0, max=65535),
  489. 489● g.clamp(min=0, max=65535),
  490. 490● b.clamp(min=0, max=65535),
  491. 491 )
  492. 492}
  493. 493
  494. 494///|
  495. 495/// float YCbCr → RGB(ITU-R BT.601)。输入范围 0.0-1.0。
  496. 496/// @see ycbcr_to_rgb 8-bit 版本,本函数为 ImageF 变体
  497. 497pub fn ycbcr_to_rgb_f(
  498. 498 y : Float,
  499. 499 cb : Float,
  500. 500 cr : Float,
  501. 501) -> (Float, Float, Float) raise @types.LoadError {
  502. 502 // 输入验证
  503. 503● if y < 0.0F || y > 1.0F || cb < 0.0F || cb > 1.0F || cr < 0.0F || cr > 1.0F {
  504. 504● raise @types.LoadError::DecodeFailed(
  505. 505 "ycbcr_to_rgb_f: y,cb,cr必须在[0,1]范围内",
  506. 506 )
  507. 507 }
  508. 508 let cb0 = cb - 0.5F
  509. 509 let cr0 = cr - 0.5F
  510. 510 let r = y + 1.402F * cr0
  511. 511 let g = y - 0.344F * cb0 - 0.714F * cr0
  512. 512 let b = y + 1.772F * cb0
  513. 513 (
  514. 514● @types.clamp_float(r, 0.0F, 1.0F),
  515. 515● @types.clamp_float(g, 0.0F, 1.0F),
  516. 516● @types.clamp_float(b, 0.0F, 1.0F),
  517. 517 )
  518. 518}
  519. 519
  520. 520///|
  521. 521/// 16-bit RGB → CMYK。输入范围 0-65535,输出范围 0-65535。
  522. 522/// @see rgb_to_cmyk 8-bit 版本,本函数为 Image16 变体
  523. 523pub fn rgb_to_cmyk_16(
  524. 524 r : Int32,
  525. 525 g : Int32,
  526. 526 b : Int32,
  527. 527) -> (Int32, Int32, Int32, Int32) raise @types.LoadError {
  528. 528 // 输入验证
  529. 529● if r < 0 || r > 65535 || g < 0 || g > 65535 || b < 0 || b > 65535 {
  530. 530● raise @types.LoadError::DecodeFailed(
  531. 531 "rgb_to_cmyk_16: r,g,b必须在[0,65535]范围内",
  532. 532 )
  533. 533 }
  534. 534 let c = 65535 - r
  535. 535 let m = 65535 - g
  536. 536 let y = 65535 - b
  537. 537◐ let k = if c < m { if c < y { c } else { y } } else if m < y { m } else { y }
  538. 538 if k == 65535 {
  539. 539● (0, 0, 0, 65535)
  540. 540 } else {
  541. 541● let denom = 65535 - k
  542. 542 (
  543. 543 (c - k) * 65535 / denom,
  544. 544 (m - k) * 65535 / denom,
  545. 545 (y - k) * 65535 / denom,
  546. 546 k,
  547. 547 )
  548. 548 }
  549. 549}
  550. 550
  551. 551///|
  552. 552/// float RGB → CMYK。输入范围 0.0-1.0,输出范围 0.0-1.0。
  553. 553/// @see rgb_to_cmyk 8-bit 版本,本函数为 ImageF 变体
  554. 554pub fn rgb_to_cmyk_f(
  555. 555 r : Float,
  556. 556 g : Float,
  557. 557 b : Float,
  558. 558) -> (Float, Float, Float, Float) raise @types.LoadError {
  559. 559 // 输入验证
  560. 560● if r < 0.0F || r > 1.0F || g < 0.0F || g > 1.0F || b < 0.0F || b > 1.0F {
  561. 561● raise @types.LoadError::DecodeFailed(
  562. 562 "rgb_to_cmyk_f: r,g,b必须在[0,1]范围内",
  563. 563 )
  564. 564 }
  565. 565 let c = 1.0F - r
  566. 566 let m = 1.0F - g
  567. 567 let y = 1.0F - b
  568. 568◐ let k = if c < m { if c < y { c } else { y } } else if m < y { m } else { y }
  569. 569 if k > 0.999999F {
  570. 570● (0.0F, 0.0F, 0.0F, 1.0F)
  571. 571 } else {
  572. 572● let denom = 1.0F - k
  573. 573 ((c - k) / denom, (m - k) / denom, (y - k) / denom, k)
  574. 574 }
  575. 575}
  576. 576
  577. 577///|
  578. 578/// 16-bit CMYK → RGB。输入范围 0-65535。
  579. 579/// @see cmyk_to_rgb 8-bit 版本,本函数为 Image16 变体
  580. 580pub fn cmyk_to_rgb_16(
  581. 581 c : Int32,
  582. 582 m : Int32,
  583. 583 y : Int32,
  584. 584 k : Int32,
  585. 585) -> (Int32, Int32, Int32) raise @types.LoadError {
  586. 586 // 输入验证
  587. 587● if c < 0 ||
  588. 588 c > 65535 ||
  589. 589 m < 0 ||
  590. 590 m > 65535 ||
  591. 591 y < 0 ||
  592. 592 y > 65535 ||
  593. 593 k < 0 ||
  594. 594 k > 65535 {
  595. 595● raise @types.LoadError::DecodeFailed(
  596. 596 "cmyk_to_rgb_16: c,m,y,k必须在[0,65535]范围内",
  597. 597 )
  598. 598 }
  599. 599 let r = 65535 - (c * (65535 - k) / 65535 + k)
  600. 600 let g = 65535 - (m * (65535 - k) / 65535 + k)
  601. 601 let b = 65535 - (y * (65535 - k) / 65535 + k)
  602. 602 (
  603. 603● r.clamp(min=0, max=65535),
  604. 604● g.clamp(min=0, max=65535),
  605. 605● b.clamp(min=0, max=65535),
  606. 606 )
  607. 607}
  608. 608
  609. 609///|
  610. 610/// float CMYK → RGB。输入范围 0.0-1.0。
  611. 611/// @see cmyk_to_rgb 8-bit 版本,本函数为 ImageF 变体
  612. 612pub fn cmyk_to_rgb_f(
  613. 613 c : Float,
  614. 614 m : Float,
  615. 615 y : Float,
  616. 616 k : Float,
  617. 617) -> (Float, Float, Float) raise @types.LoadError {
  618. 618 // 输入验证
  619. 619● if c < 0.0F ||
  620. 620 c > 1.0F ||
  621. 621 m < 0.0F ||
  622. 622 m > 1.0F ||
  623. 623 y < 0.0F ||
  624. 624 y > 1.0F ||
  625. 625 k < 0.0F ||
  626. 626 k > 1.0F {
  627. 627● raise @types.LoadError::DecodeFailed(
  628. 628 "cmyk_to_rgb_f: c,m,y,k必须在[0,1]范围内",
  629. 629 )
  630. 630 }
  631. 631 let r = 1.0F - (c * (1.0F - k) + k)
  632. 632 let g = 1.0F - (m * (1.0F - k) + k)
  633. 633 let b = 1.0F - (y * (1.0F - k) + k)
  634. 634 (
  635. 635● @types.clamp_float(r, 0.0F, 1.0F),
  636. 636● @types.clamp_float(g, 0.0F, 1.0F),
  637. 637● @types.clamp_float(b, 0.0F, 1.0F),
  638. 638 )
  639. 639}