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