image.process\color\color_convert.mbt

92.5% · 136 / 147 points · 466 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/// # 色彩转换 color convert
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// color_convert.mbt:色彩空间与通道操作
  7. 7// 本文件包含两类操作:
  8. 8// 1. 色彩空间转换:rgb_to_hsv/hsv_to_rgb、rgb_to_xyz/xyz_to_rgb、rgb_to_lab/lab_to_rgb、
  9. 9// rgb_to_ycbcr/ycbcr_to_rgb、rgb_to_cmyk/cmyk_to_rgb 等
  10. 10// 2. 通道操作:to_grayscale/to_rgb/to_rgba、premultiply_alpha/unpremultiply_alpha 等
  11. 11
  12. 12///|
  13. 13/// 转为灰度图(channels=1)
  14. 14/// 使用 ITU-R BT.601 亮度公式: Y = 0.299R + 0.587G + 0.114B
  15. 15/// 优化:直接从 Bytes 读取,避免额外数组分配
  16. 16pub fn to_grayscale(img : @types.Image) -> @types.Image raise @types.LoadError {
  17. 17 // 输入验证
  18. 18● if img.width <= 0 || img.height <= 0 {
  19. 19● raise @types.LoadError::DecodeFailed(
  20. 20 "to_grayscale: 图像尺寸必须大于0",
  21. 21 )
  22. 22 }
  23. 23 if img.channels < 1 || img.channels > 4 {
  24. 24● raise @types.LoadError::DecodeFailed(
  25. 25 "to_grayscale: 通道数必须在1-4之间",
  26. 26 )
  27. 27 }
  28. 28 let expected_size = img.width * img.height * img.channels
  29. 29● if img.data.length() != expected_size {
  30. 30● raise @types.LoadError::DecodeFailed(
  31. 31 "to_grayscale: 数据大小不匹配,期望 " +
  32. 32● expected_size.to_string() +
  33. 33 ",实际 " +
  34. 34● img.data.length().to_string(),
  35. 35 )
  36. 36 }
  37. 37 if img.channels == 1 {
  38. 38● return img
  39. 39 }
  40. 40 let pixel_count = img.width * img.height
  41. 41 let ch = img.channels
  42. 42● let out : Array[Byte] = Array::make(pixel_count, b'\x00')
  43. 43 let data = img.data
  44. 44 for i in 0..<pixel_count {
  45. 45● let base = i * ch
  46. 46● let r = data[base].to_int()
  47. 47● let g = data[base + 1].to_int()
  48. 48● let b = data[base + 2].to_int()
  49. 49 let gray = (r * 299 + g * 587 + b * 114) / 1000
  50. 50● out[i] = gray.to_byte()
  51. 51 }
  52. 52 @types.Image::{
  53. 53 width: img.width,
  54. 54 height: img.height,
  55. 55 channels: 1,
  56. 56● data: Bytes::from_array(out),
  57. 57 }
  58. 58}
  59. 59
  60. 60///|
  61. 61/// 去除 alpha 通道(RGBA → RGB,channels=3)
  62. 62pub fn to_rgb(img : @types.Image) -> @types.Image raise @types.LoadError {
  63. 63 // 输入验证
  64. 64● if img.width <= 0 || img.height <= 0 {
  65. 65● raise @types.LoadError::DecodeFailed("to_rgb: 图像尺寸必须大于0")
  66. 66 }
  67. 67 if img.channels < 1 || img.channels > 4 {
  68. 68○ raise @types.LoadError::DecodeFailed("to_rgb: 通道数必须在1-4之间")
  69. 69 }
  70. 70 let expected_size = img.width * img.height * img.channels
  71. 71● if img.data.length() != expected_size {
  72. 72● raise @types.LoadError::DecodeFailed(
  73. 73 "to_rgb: 数据大小不匹配,期望 " +
  74. 74● expected_size.to_string() +
  75. 75 ",实际 " +
  76. 76● img.data.length().to_string(),
  77. 77 )
  78. 78 }
  79. 79 if img.channels == 3 {
  80. 80● return img
  81. 81 }
  82. 82 if img.channels < 3 {
  83. 83● return img
  84. 84 }
  85. 85 let pixel_count = img.width * img.height
  86. 86● let out : Array[Byte] = Array::make(pixel_count * 3, b'\x00')
  87. 87 for i in 0..<pixel_count {
  88. 88● out[i * 3] = img.data[i * img.channels]
  89. 89 out[i * 3 + 1] = img.data[i * img.channels + 1]
  90. 90 out[i * 3 + 2] = img.data[i * img.channels + 2]
  91. 91 }
  92. 92 @types.Image::{
  93. 93 width: img.width,
  94. 94 height: img.height,
  95. 95 channels: 3,
  96. 96● data: Bytes::from_array(out),
  97. 97 }
  98. 98}
  99. 99
  100. 100///|
  101. 101/// 添加 alpha 通道(RGB → RGBA,channels=4,alpha=255)
  102. 102pub fn to_rgba(img : @types.Image) -> @types.Image raise @types.LoadError {
  103. 103 // 输入验证
  104. 104● if img.width <= 0 || img.height <= 0 {
  105. 105● raise @types.LoadError::DecodeFailed("to_rgba: 图像尺寸必须大于0")
  106. 106 }
  107. 107 if img.channels < 1 || img.channels > 4 {
  108. 108○ raise @types.LoadError::DecodeFailed("to_rgba: 通道数必须在1-4之间")
  109. 109 }
  110. 110 let expected_size = img.width * img.height * img.channels
  111. 111● if img.data.length() != expected_size {
  112. 112● raise @types.LoadError::DecodeFailed(
  113. 113 "to_rgba: 数据大小不匹配,期望 " +
  114. 114● expected_size.to_string() +
  115. 115 ",实际 " +
  116. 116● img.data.length().to_string(),
  117. 117 )
  118. 118 }
  119. 119 if img.channels == 4 {
  120. 120● return img
  121. 121 }
  122. 122 let pixel_count = img.width * img.height
  123. 123● let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00')
  124. 124 for i in 0..<pixel_count {
  125. 125● let src_ch = img.channels
  126. 126 out[i * 4] = img.data[i * src_ch]
  127. 127 out[i * 4 + 1] = if src_ch >= 2 {
  128. 128● img.data[i * src_ch + 1]
  129. 129 } else {
  130. 130● b'\x00'
  131. 131 }
  132. 132 out[i * 4 + 2] = if src_ch >= 3 {
  133. 133● img.data[i * src_ch + 2]
  134. 134 } else {
  135. 135● b'\x00'
  136. 136 }
  137. 137 out[i * 4 + 3] = b'\xFF'
  138. 138 }
  139. 139 @types.Image::{
  140. 140 width: img.width,
  141. 141 height: img.height,
  142. 142 channels: 4,
  143. 143● data: Bytes::from_array(out),
  144. 144 }
  145. 145}
  146. 146
  147. 147///|
  148. 148/// 预乘 alpha(RGBA → RGBA premultiplied)
  149. 149/// 要求 channels=4
  150. 150pub fn premultiply_alpha(
  151. 151 img : @types.Image,
  152. 152) -> @types.Image raise @types.LoadError {
  153. 153 // 输入验证
  154. 154● if img.width <= 0 || img.height <= 0 {
  155. 155● raise @types.LoadError::DecodeFailed(
  156. 156 "premultiply_alpha: 图像尺寸必须大于0",
  157. 157 )
  158. 158 }
  159. 159 if img.channels != 4 {
  160. 160● raise @types.LoadError::DecodeFailed(
  161. 161 "premultiply_alpha 需要 channels=4 (RGBA)",
  162. 162 )
  163. 163 }
  164. 164 let expected_size = img.width * img.height * img.channels
  165. 165● if img.data.length() != expected_size {
  166. 166○ raise @types.LoadError::DecodeFailed(
  167. 167 "premultiply_alpha: 数据大小不匹配,期望 " +
  168. 168○ expected_size.to_string() +
  169. 169 ",实际 " +
  170. 170○ img.data.length().to_string(),
  171. 171 )
  172. 172 }
  173. 173 let pixel_count = img.width * img.height
  174. 174● let data_int : Array[Int32] = Array::make(pixel_count * 4, 0)
  175. 175 for i in 0..<(pixel_count * 4) {
  176. 176● data_int[i] = img.data[i].to_int()
  177. 177 }
  178. 178● let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00')
  179. 179 for i in 0..<pixel_count {
  180. 180● let r = data_int[i * 4]
  181. 181 let g = data_int[i * 4 + 1]
  182. 182 let b = data_int[i * 4 + 2]
  183. 183 let a = data_int[i * 4 + 3]
  184. 184● out[i * 4] = (r * a / 255).to_byte()
  185. 185● out[i * 4 + 1] = (g * a / 255).to_byte()
  186. 186● out[i * 4 + 2] = (b * a / 255).to_byte()
  187. 187● out[i * 4 + 3] = a.to_byte()
  188. 188 }
  189. 189 @types.Image::{
  190. 190 width: img.width,
  191. 191 height: img.height,
  192. 192 channels: 4,
  193. 193● data: Bytes::from_array(out),
  194. 194 }
  195. 195}
  196. 196
  197. 197///|
  198. 198/// 反预乘 alpha(RGBA premA → RGBA unpremultiplied)
  199. 199/// 要求 channels=4
  200. 200pub fn unpremultiply_alpha(
  201. 201 img : @types.Image,
  202. 202) -> @types.Image raise @types.LoadError {
  203. 203 // 输入验证
  204. 204● if img.width <= 0 || img.height <= 0 {
  205. 205● raise @types.LoadError::DecodeFailed(
  206. 206 "unpremultiply_alpha: 图像尺寸必须大于0",
  207. 207 )
  208. 208 }
  209. 209 if img.channels != 4 {
  210. 210● raise @types.LoadError::DecodeFailed(
  211. 211 "unpremultiply_alpha 需要 channels=4 (RGBA)",
  212. 212 )
  213. 213 }
  214. 214 let expected_size = img.width * img.height * img.channels
  215. 215● if img.data.length() != expected_size {
  216. 216○ raise @types.LoadError::DecodeFailed(
  217. 217 "unpremultiply_alpha: 数据大小不匹配,期望 " +
  218. 218○ expected_size.to_string() +
  219. 219 ",实际 " +
  220. 220○ img.data.length().to_string(),
  221. 221 )
  222. 222 }
  223. 223 let pixel_count = img.width * img.height
  224. 224● let data_int : Array[Int32] = Array::make(pixel_count * 4, 0)
  225. 225 for i in 0..<(pixel_count * 4) {
  226. 226● data_int[i] = img.data[i].to_int()
  227. 227 }
  228. 228● let out : Array[Byte] = Array::make(pixel_count * 4, b'\x00')
  229. 229 for i in 0..<pixel_count {
  230. 230● let r = data_int[i * 4]
  231. 231 let g = data_int[i * 4 + 1]
  232. 232 let b = data_int[i * 4 + 2]
  233. 233 let a = data_int[i * 4 + 3]
  234. 234 if a == 0 {
  235. 235● out[i * 4] = b'\x00'
  236. 236 out[i * 4 + 1] = b'\x00'
  237. 237 out[i * 4 + 2] = b'\x00'
  238. 238 out[i * 4 + 3] = b'\x00'
  239. 239 } else {
  240. 240● out[i * 4] = (r * 255 / a).to_byte()
  241. 241● out[i * 4 + 1] = (g * 255 / a).to_byte()
  242. 242● out[i * 4 + 2] = (b * 255 / a).to_byte()
  243. 243● out[i * 4 + 3] = a.to_byte()
  244. 244 }
  245. 245 }
  246. 246 @types.Image::{
  247. 247 width: img.width,
  248. 248 height: img.height,
  249. 249 channels: 4,
  250. 250● data: Bytes::from_array(out),
  251. 251 }
  252. 252}
  253. 253
  254. 254///|
  255. 255/// RGB → YCbCr(ITU-R BT.601)。返回 (Y, Cb, Cr),范围 0-255。
  256. 256pub fn rgb_to_ycbcr(
  257. 257 r : Int32,
  258. 258 g : Int32,
  259. 259 b : Int32,
  260. 260) -> (Int32, Int32, Int32) raise @types.LoadError {
  261. 261 // 输入验证
  262. 262● if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
  263. 263● raise @types.LoadError::DecodeFailed(
  264. 264 "rgb_to_ycbcr: r,g,b必须在[0,255]范围内",
  265. 265 )
  266. 266 }
  267. 267 let y = (299 * r + 587 * g + 114 * b) / 1000
  268. 268 let cb = (-169 * r - 331 * g + 500 * b) / 1000 + 128
  269. 269 let cr = (500 * r - 419 * g - 81 * b) / 1000 + 128
  270. 270● (y.clamp(min=0, max=255), cb.clamp(min=0, max=255), cr.clamp(min=0, max=255))
  271. 271}
  272. 272
  273. 273///|
  274. 274/// YCbCr → RGB(ITU-R BT.601)。输入范围 0-255。
  275. 275pub fn ycbcr_to_rgb(
  276. 276 y : Int32,
  277. 277 cb : Int32,
  278. 278 cr : Int32,
  279. 279) -> (Int32, Int32, Int32) raise @types.LoadError {
  280. 280 // 输入验证
  281. 281● if y < 0 || y > 255 || cb < 0 || cb > 255 || cr < 0 || cr > 255 {
  282. 282● raise @types.LoadError::DecodeFailed(
  283. 283 "ycbcr_to_rgb: y,cb,cr必须在[0,255]范围内",
  284. 284 )
  285. 285 }
  286. 286 let cb0 = cb - 128
  287. 287 let cr0 = cr - 128
  288. 288 let r = y + 1402 * cr0 / 1000
  289. 289 let g = y - 344 * cb0 / 1000 - 714 * cr0 / 1000
  290. 290 let b = y + 1772 * cb0 / 1000
  291. 291● (r.clamp(min=0, max=255), g.clamp(min=0, max=255), b.clamp(min=0, max=255))
  292. 292}
  293. 293
  294. 294///|
  295. 295/// RGB → XYZ(sRGB, D65 白点)。返回 (X, Y, Z),范围 0.0-1.0。
  296. 296pub fn rgb_to_xyz(
  297. 297 r : Int32,
  298. 298 g : Int32,
  299. 299 b : Int32,
  300. 300) -> (Float, Float, Float) raise @types.LoadError {
  301. 301 // 输入验证
  302. 302● if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
  303. 303● raise @types.LoadError::DecodeFailed(
  304. 304 "rgb_to_xyz: r,g,b必须在[0,255]范围内",
  305. 305 )
  306. 306 }
  307. 307 fn linearize(c : Int32) -> Float {
  308. 308● let cf = Float::from_int(c) / 255.0F
  309. 309 if cf <= 0.04045F {
  310. 310● cf / 12.92F
  311. 311 } else {
  312. 312● @math.powf((cf + 0.055F) / 1.055F, 2.4F)
  313. 313 }
  314. 314 }
  315. 315● let rl = linearize(r)
  316. 316● let gl = linearize(g)
  317. 317● let bl = linearize(b)
  318. 318 let x = 0.4124F * rl + 0.3576F * gl + 0.1805F * bl
  319. 319 let y = 0.2126F * rl + 0.7152F * gl + 0.0722F * bl
  320. 320 let z = 0.0193F * rl + 0.1192F * gl + 0.9505F * bl
  321. 321 (x, y, z)
  322. 322}
  323. 323
  324. 324///|
  325. 325/// XYZ → RGB(sRGB, D65 白点)。输入范围 0.0-1.0。
  326. 326pub fn xyz_to_rgb(
  327. 327 x : Float,
  328. 328 y : Float,
  329. 329 z : Float,
  330. 330) -> (Int32, Int32, Int32) raise @types.LoadError {
  331. 331 // 输入验证
  332. 332● if x < 0.0F || x > 1.0F || y < 0.0F || y > 1.0F || z < 0.0F || z > 1.0F {
  333. 333● raise @types.LoadError::DecodeFailed(
  334. 334 "xyz_to_rgb: x,y,z必须在[0,1]范围内",
  335. 335 )
  336. 336 }
  337. 337 let rl = 3.2406F * x - 1.5372F * y - 0.4986F * z
  338. 338 let gl = -0.9689F * x + 1.8758F * y + 0.0415F * z
  339. 339 let bl = 0.0557F * x - 0.2040F * y + 1.0570F * z
  340. 340 fn srgb(c : Float) -> Int32 {
  341. 341● let v = if c <= 0.0031308F {
  342. 342● 12.92F * c
  343. 343 } else {
  344. 344● 1.055F * @math.powf(c, 1.0F / 2.4F) - 0.055F
  345. 345 }
  346. 346● (v * 255.0F + 0.5F).to_int().clamp(min=0, max=255)
  347. 347 }
  348. 348● (srgb(rl), srgb(gl), srgb(bl))
  349. 349}
  350. 350
  351. 351///|
  352. 352/// RGB → Lab(CIELAB, D65 白点)。返回 (L, a, b),L 范围 0-100,a/b 范围 -128-127。
  353. 353pub fn rgb_to_lab(
  354. 354 r : Int32,
  355. 355 g : Int32,
  356. 356 b : Int32,
  357. 357) -> (Float, Float, Float) raise @types.LoadError {
  358. 358 // 输入验证
  359. 359● if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
  360. 360● raise @types.LoadError::DecodeFailed(
  361. 361 "rgb_to_lab: r,g,b必须在[0,255]范围内",
  362. 362 )
  363. 363 }
  364. 364● let (x, y, z) = rgb_to_xyz(r, g, b)
  365. 365 let xn = 0.9505F
  366. 366 let yn = 1.0F
  367. 367 let zn = 1.089F
  368. 368 fn f(t : Float) -> Float {
  369. 369● if t > 0.008856F {
  370. 370● @math.powf(t, 1.0F / 3.0F)
  371. 371 } else {
  372. 372● 7.787F * t + 16.0F / 116.0F
  373. 373 }
  374. 374 }
  375. 375● let fx = f(x / xn)
  376. 376● let fy = f(y / yn)
  377. 377● let fz = f(z / zn)
  378. 378 let l = 116.0F * fy - 16.0F
  379. 379 let a = 500.0F * (fx - fy)
  380. 380 let b_val = 200.0F * (fy - fz)
  381. 381 (l, a, b_val)
  382. 382}
  383. 383
  384. 384///|
  385. 385/// Lab → RGB(CIELAB, D65 白点)。输入 L 范围 0-100,a/b 范围 -128-127。
  386. 386pub fn lab_to_rgb(
  387. 387 l : Float,
  388. 388 a : Float,
  389. 389 b : Float,
  390. 390) -> (Int32, Int32, Int32) raise @types.LoadError {
  391. 391 // 输入验证
  392. 392● if l < 0.0F || l > 100.0F {
  393. 393● raise @types.LoadError::DecodeFailed(
  394. 394 "lab_to_rgb: L必须在[0,100]范围内",
  395. 395 )
  396. 396 }
  397. 397 let yn = 1.0F
  398. 398 let xn = 0.9505F
  399. 399 let zn = 1.089F
  400. 400 let fy = (l + 16.0F) / 116.0F
  401. 401 let fx = a / 500.0F + fy
  402. 402 let fz = fy - b / 200.0F
  403. 403 fn f_inv(t : Float) -> Float {
  404. 404● if @math.powf(t, 3.0F) > 0.008856F {
  405. 405● @math.powf(t, 3.0F)
  406. 406 } else {
  407. 407● (t - 16.0F / 116.0F) / 7.787F
  408. 408 }
  409. 409 }
  410. 410● let x = xn * f_inv(fx)
  411. 411● let y = yn * f_inv(fy)
  412. 412● let z = zn * f_inv(fz)
  413. 413● xyz_to_rgb(x, y, z)
  414. 414}
  415. 415
  416. 416///|
  417. 417/// RGB → CMYK。返回 (C, M, Y, K),范围 0-255。
  418. 418pub fn rgb_to_cmyk(
  419. 419 r : Int32,
  420. 420 g : Int32,
  421. 421 b : Int32,
  422. 422) -> (Int32, Int32, Int32, Int32) raise @types.LoadError {
  423. 423 // 输入验证
  424. 424● if r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 {
  425. 425● raise @types.LoadError::DecodeFailed(
  426. 426 "rgb_to_cmyk: r,g,b必须在[0,255]范围内",
  427. 427 )
  428. 428 }
  429. 429 let c = 255 - r
  430. 430 let m = 255 - g
  431. 431 let y = 255 - b
  432. 432◐ let k = if c < m { if c < y { c } else { y } } else if m < y { m } else { y }
  433. 433 if k == 255 {
  434. 434● (0, 0, 0, 255)
  435. 435 } else {
  436. 436● let denom = 255 - k
  437. 437 ((c - k) * 255 / denom, (m - k) * 255 / denom, (y - k) * 255 / denom, k)
  438. 438 }
  439. 439}
  440. 440
  441. 441///|
  442. 442/// CMYK → RGB。输入范围 0-255。
  443. 443pub fn cmyk_to_rgb(
  444. 444 c : Int32,
  445. 445 m : Int32,
  446. 446 y : Int32,
  447. 447 k : Int32,
  448. 448) -> (Int32, Int32, Int32) raise @types.LoadError {
  449. 449 // 输入验证
  450. 450● if c < 0 ||
  451. 451 c > 255 ||
  452. 452 m < 0 ||
  453. 453 m > 255 ||
  454. 454 y < 0 ||
  455. 455 y > 255 ||
  456. 456 k < 0 ||
  457. 457 k > 255 {
  458. 458● raise @types.LoadError::DecodeFailed(
  459. 459 "cmyk_to_rgb: c,m,y,k必须在[0,255]范围内",
  460. 460 )
  461. 461 }
  462. 462 let r = 255 - (c * (255 - k) / 255 + k)
  463. 463 let g = 255 - (m * (255 - k) / 255 + k)
  464. 464 let b = 255 - (y * (255 - k) / 255 + k)
  465. 465● (r.clamp(min=0, max=255), g.clamp(min=0, max=255), b.clamp(min=0, max=255))
  466. 466}