image.domain\util\pixel_ops.mbt

97.9% · 143 / 146 points · 476 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/// # 像素操作 util/pixel_ops
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 像素操作(纯 MoonBit):threshold / posterize / extract_channel / blend 模式
  7. 7
  8. 8///|
  9. 9/// 二值化。像素值 >= threshold 设为 255,否则设为 0。
  10. 10/// 仅影响颜色通道,alpha 通道不变。
  11. 11pub fn threshold(img : @types.Image, thresh : Int32) -> @types.Image {
  12. 12● let n = img.width * img.height
  13. 13 let ch = img.channels
  14. 14● let color_ch = if ch >= 3 { 3 } else { ch }
  15. 15● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  16. 16 for i in 0..<n {
  17. 17● for c in 0..<color_ch {
  18. 18● out[i * ch + c] = if img.data[i * ch + c].to_int() >= thresh {
  19. 19● b'\xFF'
  20. 20 } else {
  21. 21● b'\x00'
  22. 22 }
  23. 23 }
  24. 24 if ch == 4 {
  25. 25● out[i * ch + 3] = img.data[i * ch + 3]
  26. 26 }
  27. 27 }
  28. 28 @types.Image::{
  29. 29 width: img.width,
  30. 30 height: img.height,
  31. 31 channels: ch,
  32. 32● data: Bytes::from_array(out),
  33. 33 }
  34. 34}
  35. 35
  36. 36///|
  37. 37/// 色调分离。将每个通道的值量化到 levels 个级别。
  38. 38/// levels=2 等价于二值化,levels=4 将 0-255 分成 4 段。
  39. 39pub fn posterize(
  40. 40 img : @types.Image,
  41. 41 levels : Int32,
  42. 42) -> @types.Image raise @types.LoadError {
  43. 43● if levels < 2 || levels > 256 {
  44. 44● raise @types.LoadError::DecodeFailed(
  45. 45 "posterize levels 必须在 [2, 256] 范围内",
  46. 46 )
  47. 47 }
  48. 48 let n = img.width * img.height
  49. 49 let ch = img.channels
  50. 50● let color_ch = if ch >= 3 { 3 } else { ch }
  51. 51● let step = 256.0F / Float::from_int(levels)
  52. 52● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  53. 53 for i in 0..<n {
  54. 54● for c in 0..<color_ch {
  55. 55● let v = img.data[i * ch + c].to_int()
  56. 56● let quantized = (Float::from_int(v) / step).to_int() * step.to_int()
  57. 57● out[i * ch + c] = @types.clamp_byte(quantized)
  58. 58 }
  59. 59 if ch == 4 {
  60. 60● out[i * ch + 3] = img.data[i * ch + 3]
  61. 61 }
  62. 62 }
  63. 63 @types.Image::{
  64. 64 width: img.width,
  65. 65 height: img.height,
  66. 66 channels: ch,
  67. 67● data: Bytes::from_array(out),
  68. 68 }
  69. 69}
  70. 70
  71. 71///|
  72. 72/// 提取单个通道。返回单通道 Image(channels=1)。
  73. 73/// 描述 channel: 0=R/Gray, 1=G, 2=B, 3=A
  74. 74pub fn extract_channel(
  75. 75 img : @types.Image,
  76. 76 channel : Int32,
  77. 77) -> @types.Image raise @types.LoadError {
  78. 78● if channel < 0 || channel >= img.channels {
  79. 79● raise @types.LoadError::DecodeFailed("channel 索引越界")
  80. 80 }
  81. 81 let n = img.width * img.height
  82. 82● let out : Array[Byte] = Array::make(n, b'\x00')
  83. 83 for i in 0..<n {
  84. 84● out[i] = img.data[i * img.channels + channel]
  85. 85 }
  86. 86 @types.Image::{
  87. 87 width: img.width,
  88. 88 height: img.height,
  89. 89 channels: 1,
  90. 90● data: Bytes::from_array(out),
  91. 91 }
  92. 92}
  93. 93
  94. 94///|
  95. 95/// 乘法混合。result = base * blend / 255。
  96. 96/// 两图必须尺寸和通道数相同。
  97. 97pub fn blend_multiply(
  98. 98 base : @types.Image,
  99. 99 blend : @types.Image,
  100. 100) -> @types.Image raise @types.LoadError {
  101. 101● if base.width != blend.width ||
  102. 102 base.height != blend.height ||
  103. 103 base.channels != blend.channels {
  104. 104● raise @types.LoadError::DecodeFailed(
  105. 105 "blend 两图尺寸/通道数必须相同",
  106. 106 )
  107. 107 }
  108. 108 let n = base.width * base.height * base.channels
  109. 109● let out : Array[Byte] = Array::make(n, b'\x00')
  110. 110 for i in 0..<n {
  111. 111● let b = base.data[i].to_int()
  112. 112● let l = blend.data[i].to_int()
  113. 113● out[i] = (b * l / 255).to_byte()
  114. 114 }
  115. 115 @types.Image::{
  116. 116 width: base.width,
  117. 117 height: base.height,
  118. 118 channels: base.channels,
  119. 119● data: Bytes::from_array(out),
  120. 120 }
  121. 121}
  122. 122
  123. 123///|
  124. 124/// 滤色混合。result = 255 - (255 - base) * (255 - blend) / 255。
  125. 125pub fn blend_screen(
  126. 126 base : @types.Image,
  127. 127 blend : @types.Image,
  128. 128) -> @types.Image raise @types.LoadError {
  129. 129● if base.width != blend.width ||
  130. 130 base.height != blend.height ||
  131. 131 base.channels != blend.channels {
  132. 132● raise @types.LoadError::DecodeFailed(
  133. 133 "blend 两图尺寸/通道数必须相同",
  134. 134 )
  135. 135 }
  136. 136 let n = base.width * base.height * base.channels
  137. 137● let out : Array[Byte] = Array::make(n, b'\x00')
  138. 138 for i in 0..<n {
  139. 139● let b = base.data[i].to_int()
  140. 140● let l = blend.data[i].to_int()
  141. 141● out[i] = (255 - (255 - b) * (255 - l) / 255).to_byte()
  142. 142 }
  143. 143 @types.Image::{
  144. 144 width: base.width,
  145. 145 height: base.height,
  146. 146 channels: base.channels,
  147. 147● data: Bytes::from_array(out),
  148. 148 }
  149. 149}
  150. 150
  151. 151///|
  152. 152/// 叠加混合。暗部用乘法,亮部用滤色。
  153. 153/// 描述 result = if base < 128: 2*base*blend/255 else: 255 - 2*(255-base)*(255-blend)/255
  154. 154pub fn blend_overlay(
  155. 155 base : @types.Image,
  156. 156 blend : @types.Image,
  157. 157) -> @types.Image raise @types.LoadError {
  158. 158● if base.width != blend.width ||
  159. 159 base.height != blend.height ||
  160. 160 base.channels != blend.channels {
  161. 161● raise @types.LoadError::DecodeFailed(
  162. 162 "blend 两图尺寸/通道数必须相同",
  163. 163 )
  164. 164 }
  165. 165 let n = base.width * base.height * base.channels
  166. 166● let out : Array[Byte] = Array::make(n, b'\x00')
  167. 167 for i in 0..<n {
  168. 168● let b = base.data[i].to_int()
  169. 169● let l = blend.data[i].to_int()
  170. 170 out[i] = if b < 128 {
  171. 171○ (2 * b * l / 255).to_byte()
  172. 172 } else {
  173. 173● @types.clamp_byte(255 - 2 * (255 - b) * (255 - l) / 255)
  174. 174 }
  175. 175 }
  176. 176 @types.Image::{
  177. 177 width: base.width,
  178. 178 height: base.height,
  179. 179 channels: base.channels,
  180. 180● data: Bytes::from_array(out),
  181. 181 }
  182. 182}
  183. 183
  184. 184///|
  185. 185/// 变暗混合。result = min(base, blend)。
  186. 186pub fn blend_darken(
  187. 187 base : @types.Image,
  188. 188 blend : @types.Image,
  189. 189) -> @types.Image raise @types.LoadError {
  190. 190● if base.width != blend.width ||
  191. 191 base.height != blend.height ||
  192. 192 base.channels != blend.channels {
  193. 193● raise @types.LoadError::DecodeFailed(
  194. 194 "blend 两图尺寸/通道数必须相同",
  195. 195 )
  196. 196 }
  197. 197 let n = base.width * base.height * base.channels
  198. 198● let out : Array[Byte] = Array::make(n, b'\x00')
  199. 199 for i in 0..<n {
  200. 200● let b = base.data[i].to_int()
  201. 201● let l = blend.data[i].to_int()
  202. 202● out[i] = (if b < l { b } else { l }).to_byte()
  203. 203 }
  204. 204 @types.Image::{
  205. 205 width: base.width,
  206. 206 height: base.height,
  207. 207 channels: base.channels,
  208. 208● data: Bytes::from_array(out),
  209. 209 }
  210. 210}
  211. 211
  212. 212///|
  213. 213/// 变亮混合。result = max(base, blend)。
  214. 214pub fn blend_lighten(
  215. 215 base : @types.Image,
  216. 216 blend : @types.Image,
  217. 217) -> @types.Image raise @types.LoadError {
  218. 218● if base.width != blend.width ||
  219. 219 base.height != blend.height ||
  220. 220 base.channels != blend.channels {
  221. 221● raise @types.LoadError::DecodeFailed(
  222. 222 "blend 两图尺寸/通道数必须相同",
  223. 223 )
  224. 224 }
  225. 225 let n = base.width * base.height * base.channels
  226. 226● let out : Array[Byte] = Array::make(n, b'\x00')
  227. 227 for i in 0..<n {
  228. 228● let b = base.data[i].to_int()
  229. 229● let l = blend.data[i].to_int()
  230. 230● out[i] = (if b > l { b } else { l }).to_byte()
  231. 231 }
  232. 232 @types.Image::{
  233. 233 width: base.width,
  234. 234 height: base.height,
  235. 235 channels: base.channels,
  236. 236● data: Bytes::from_array(out),
  237. 237 }
  238. 238}
  239. 239
  240. 240///|
  241. 241/// 差值混合。result = |base - blend|。
  242. 242pub fn blend_difference(
  243. 243 base : @types.Image,
  244. 244 blend : @types.Image,
  245. 245) -> @types.Image raise @types.LoadError {
  246. 246● if base.width != blend.width ||
  247. 247 base.height != blend.height ||
  248. 248 base.channels != blend.channels {
  249. 249● raise @types.LoadError::DecodeFailed(
  250. 250 "blend 两图尺寸/通道数必须相同",
  251. 251 )
  252. 252 }
  253. 253 let n = base.width * base.height * base.channels
  254. 254● let out : Array[Byte] = Array::make(n, b'\x00')
  255. 255 for i in 0..<n {
  256. 256● let b = base.data[i].to_int()
  257. 257● let l = blend.data[i].to_int()
  258. 258◐ out[i] = (if b > l { b - l } else { l - b }).to_byte()
  259. 259 }
  260. 260 @types.Image::{
  261. 261 width: base.width,
  262. 262 height: base.height,
  263. 263 channels: base.channels,
  264. 264● data: Bytes::from_array(out),
  265. 265 }
  266. 266}
  267. 267
  268. 268///|
  269. 269/// 排除混合。result = base + blend - 2 * base * blend / 255。
  270. 270pub fn blend_exclusion(
  271. 271 base : @types.Image,
  272. 272 blend : @types.Image,
  273. 273) -> @types.Image raise @types.LoadError {
  274. 274● if base.width != blend.width ||
  275. 275 base.height != blend.height ||
  276. 276 base.channels != blend.channels {
  277. 277● raise @types.LoadError::DecodeFailed(
  278. 278 "blend 两图尺寸/通道数必须相同",
  279. 279 )
  280. 280 }
  281. 281 let n = base.width * base.height * base.channels
  282. 282● let out : Array[Byte] = Array::make(n, b'\x00')
  283. 283 for i in 0..<n {
  284. 284● let b = base.data[i].to_int()
  285. 285● let l = blend.data[i].to_int()
  286. 286● out[i] = @types.clamp_byte(b + l - 2 * b * l / 255)
  287. 287 }
  288. 288 @types.Image::{
  289. 289 width: base.width,
  290. 290 height: base.height,
  291. 291 channels: base.channels,
  292. 292● data: Bytes::from_array(out),
  293. 293 }
  294. 294}
  295. 295
  296. 296///|
  297. 297/// 颜色减淡。result = base * 255 / (255 - blend),blend=255 时结果为 255。
  298. 298pub fn blend_color_dodge(
  299. 299 base : @types.Image,
  300. 300 blend : @types.Image,
  301. 301) -> @types.Image raise @types.LoadError {
  302. 302● if base.width != blend.width ||
  303. 303 base.height != blend.height ||
  304. 304 base.channels != blend.channels {
  305. 305● raise @types.LoadError::DecodeFailed(
  306. 306 "blend 两图尺寸/通道数必须相同",
  307. 307 )
  308. 308 }
  309. 309 let n = base.width * base.height * base.channels
  310. 310● let out : Array[Byte] = Array::make(n, b'\x00')
  311. 311 for i in 0..<n {
  312. 312● let b = base.data[i].to_int()
  313. 313● let l = blend.data[i].to_int()
  314. 314 out[i] = if l == 255 {
  315. 315● b'\xFF'
  316. 316 } else {
  317. 317● @types.clamp_byte(b * 255 / (255 - l))
  318. 318 }
  319. 319 }
  320. 320 @types.Image::{
  321. 321 width: base.width,
  322. 322 height: base.height,
  323. 323 channels: base.channels,
  324. 324● data: Bytes::from_array(out),
  325. 325 }
  326. 326}
  327. 327
  328. 328///|
  329. 329/// 颜色加深。result = 255 - (255 - base) * 255 / blend,blend=0 时结果为 0。
  330. 330pub fn blend_color_burn(
  331. 331 base : @types.Image,
  332. 332 blend : @types.Image,
  333. 333) -> @types.Image raise @types.LoadError {
  334. 334● if base.width != blend.width ||
  335. 335 base.height != blend.height ||
  336. 336 base.channels != blend.channels {
  337. 337● raise @types.LoadError::DecodeFailed(
  338. 338 "blend 两图尺寸/通道数必须相同",
  339. 339 )
  340. 340 }
  341. 341 let n = base.width * base.height * base.channels
  342. 342● let out : Array[Byte] = Array::make(n, b'\x00')
  343. 343 for i in 0..<n {
  344. 344● let b = base.data[i].to_int()
  345. 345● let l = blend.data[i].to_int()
  346. 346 out[i] = if l == 0 {
  347. 347● b'\x00'
  348. 348 } else {
  349. 349● @types.clamp_byte(255 - (255 - b) * 255 / l)
  350. 350 }
  351. 351 }
  352. 352 @types.Image::{
  353. 353 width: base.width,
  354. 354 height: base.height,
  355. 355 channels: base.channels,
  356. 356● data: Bytes::from_array(out),
  357. 357 }
  358. 358}
  359. 359
  360. 360///|
  361. 361/// 强光。blend < 128 时用颜色加深,否则用颜色减淡。
  362. 362pub fn blend_hard_light(
  363. 363 base : @types.Image,
  364. 364 blend : @types.Image,
  365. 365) -> @types.Image raise @types.LoadError {
  366. 366● if base.width != blend.width ||
  367. 367 base.height != blend.height ||
  368. 368 base.channels != blend.channels {
  369. 369● raise @types.LoadError::DecodeFailed(
  370. 370 "blend 两图尺寸/通道数必须相同",
  371. 371 )
  372. 372 }
  373. 373 let n = base.width * base.height * base.channels
  374. 374● let out : Array[Byte] = Array::make(n, b'\x00')
  375. 375 for i in 0..<n {
  376. 376● let b = base.data[i].to_int()
  377. 377● let l = blend.data[i].to_int()
  378. 378 out[i] = if l < 128 {
  379. 379● @types.clamp_byte(2 * b * l / 255)
  380. 380 } else {
  381. 381● @types.clamp_byte(255 - 2 * (255 - b) * (255 - l) / 255)
  382. 382 }
  383. 383 }
  384. 384 @types.Image::{
  385. 385 width: base.width,
  386. 386 height: base.height,
  387. 387 channels: base.channels,
  388. 388● data: Bytes::from_array(out),
  389. 389 }
  390. 390}
  391. 391
  392. 392///|
  393. 393/// 柔光。基于 blend 的亮度在乘法和滤色之间平滑过渡。
  394. 394/// 描述 result = (1 - 2*blend/255) * base^2/255 + (2*blend/255) * base
  395. 395pub fn blend_soft_light(
  396. 396 base : @types.Image,
  397. 397 blend : @types.Image,
  398. 398) -> @types.Image raise @types.LoadError {
  399. 399● if base.width != blend.width ||
  400. 400 base.height != blend.height ||
  401. 401 base.channels != blend.channels {
  402. 402● raise @types.LoadError::DecodeFailed(
  403. 403 "blend 两图尺寸/通道数必须相同",
  404. 404 )
  405. 405 }
  406. 406 let n = base.width * base.height * base.channels
  407. 407● let out : Array[Byte] = Array::make(n, b'\x00')
  408. 408 for i in 0..<n {
  409. 409● let b = base.data[i].to_int()
  410. 410● let l = blend.data[i].to_int()
  411. 411 let w = 2 * l
  412. 412● out[i] = @types.clamp_byte((255 - w) * b * b / (255 * 255) + w * b / 255)
  413. 413 }
  414. 414 @types.Image::{
  415. 415 width: base.width,
  416. 416 height: base.height,
  417. 417 channels: base.channels,
  418. 418● data: Bytes::from_array(out),
  419. 419 }
  420. 420}
  421. 421
  422. 422///|
  423. 423/// 线性减淡(相加)。result = min(base + blend, 255)。
  424. 424pub fn blend_linear_dodge(
  425. 425 base : @types.Image,
  426. 426 blend : @types.Image,
  427. 427) -> @types.Image raise @types.LoadError {
  428. 428● if base.width != blend.width ||
  429. 429 base.height != blend.height ||
  430. 430 base.channels != blend.channels {
  431. 431● raise @types.LoadError::DecodeFailed(
  432. 432 "blend 两图尺寸/通道数必须相同",
  433. 433 )
  434. 434 }
  435. 435 let n = base.width * base.height * base.channels
  436. 436● let out : Array[Byte] = Array::make(n, b'\x00')
  437. 437 for i in 0..<n {
  438. 438● let b = base.data[i].to_int()
  439. 439● let l = blend.data[i].to_int()
  440. 440● out[i] = @types.clamp_byte(b + l)
  441. 441 }
  442. 442 @types.Image::{
  443. 443 width: base.width,
  444. 444 height: base.height,
  445. 445 channels: base.channels,
  446. 446● data: Bytes::from_array(out),
  447. 447 }
  448. 448}
  449. 449
  450. 450///|
  451. 451/// 线性加深(相减)。result = max(base + blend - 255, 0)。
  452. 452pub fn blend_linear_burn(
  453. 453 base : @types.Image,
  454. 454 blend : @types.Image,
  455. 455) -> @types.Image raise @types.LoadError {
  456. 456● if base.width != blend.width ||
  457. 457 base.height != blend.height ||
  458. 458 base.channels != blend.channels {
  459. 459● raise @types.LoadError::DecodeFailed(
  460. 460 "blend 两图尺寸/通道数必须相同",
  461. 461 )
  462. 462 }
  463. 463 let n = base.width * base.height * base.channels
  464. 464● let out : Array[Byte] = Array::make(n, b'\x00')
  465. 465 for i in 0..<n {
  466. 466● let b = base.data[i].to_int()
  467. 467● let l = blend.data[i].to_int()
  468. 468● out[i] = @types.clamp_byte(b + l - 255)
  469. 469 }
  470. 470 @types.Image::{
  471. 471 width: base.width,
  472. 472 height: base.height,
  473. 473 channels: base.channels,
  474. 474● data: Bytes::from_array(out),
  475. 475 }
  476. 476}