1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// 像素操作(纯 MoonBit):threshold / posterize / extract_channel / blend 模式

///|
fn clamp_byte_v(v : Int) -> Byte {
  if v < 0 {
    b'\x00'
  } else if v > 255 {
    b'\xFF'
  } else {
    v.to_byte()
  }
}

///|
/// 二值化。像素值 >= threshold 设为 255,否则设为 0。
/// 仅影响颜色通道,alpha 通道不变。
pub fn threshold(img : @types.Image, thresh : Int) -> @types.Image {
  let n = img.width * img.height
  let ch = img.channels
  let color_ch = if ch >= 3 { 3 } else { ch }
  let out : Array[Byte] = Array::make(n * ch, b'\x00')
  for i in 0..<n {
    for c in 0..<color_ch {
      out[i * ch + c] = if img.data[i * ch + c].to_int() >= thresh {
        b'\xFF'
      } else {
        b'\x00'
      }
    }
    if ch == 4 {
      out[i * ch + 3] = img.data[i * ch + 3]
    }
  }
  @types.Image::{
    width: img.width,
    height: img.height,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 色调分离。将每个通道的值量化到 levels 个级别。
/// levels=2 等价于二值化,levels=4 将 0-255 分成 4 段。
pub fn posterize(
  img : @types.Image,
  levels : Int,
) -> @types.Image raise @types.LoadError {
  if levels < 2 || levels > 256 {
    raise @types.LoadError::DecodeFailed(
      "posterize levels 必须在 [2, 256] 范围内",
    )
  }
  let n = img.width * img.height
  let ch = img.channels
  let color_ch = if ch >= 3 { 3 } else { ch }
  let step = 256.0F / Float::from_int(levels)
  let out : Array[Byte] = Array::make(n * ch, b'\x00')
  for i in 0..<n {
    for c in 0..<color_ch {
      let v = img.data[i * ch + c].to_int()
      let quantized = (Float::from_int(v) / step).to_int() * step.to_int()
      out[i * ch + c] = clamp_byte_v(quantized)
    }
    if ch == 4 {
      out[i * ch + 3] = img.data[i * ch + 3]
    }
  }
  @types.Image::{
    width: img.width,
    height: img.height,
    channels: ch,
    data: Bytes::from_array(out),
  }
}

///|
/// 提取单个通道。返回单通道 Image(channels=1)。
/// channel: 0=R/Gray, 1=G, 2=B, 3=A
pub fn extract_channel(
  img : @types.Image,
  channel : Int,
) -> @types.Image raise @types.LoadError {
  if channel < 0 || channel >= img.channels {
    raise @types.LoadError::DecodeFailed("channel 索引越界")
  }
  let n = img.width * img.height
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    out[i] = img.data[i * img.channels + channel]
  }
  @types.Image::{
    width: img.width,
    height: img.height,
    channels: 1,
    data: Bytes::from_array(out),
  }
}

///|
/// 乘法混合。result = base * blend / 255。
/// 两图必须尺寸和通道数相同。
pub fn blend_multiply(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = (b * l / 255).to_byte()
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 滤色混合。result = 255 - (255 - base) * (255 - blend) / 255。
pub fn blend_screen(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = (255 - (255 - b) * (255 - l) / 255).to_byte()
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 叠加混合。暗部用乘法,亮部用滤色。
/// result = if base < 128: 2*base*blend/255 else: 255 - 2*(255-base)*(255-blend)/255
pub fn blend_overlay(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = if b < 128 {
      (2 * b * l / 255).to_byte()
    } else {
      clamp_byte_v(255 - 2 * (255 - b) * (255 - l) / 255)
    }
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 变暗混合。result = min(base, blend)。
pub fn blend_darken(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = (if b < l { b } else { l }).to_byte()
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 变亮混合。result = max(base, blend)。
pub fn blend_lighten(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = (if b > l { b } else { l }).to_byte()
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 差值混合。result = |base - blend|。
pub fn blend_difference(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = (if b > l { b - l } else { l - b }).to_byte()
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 排除混合。result = base + blend - 2 * base * blend / 255。
pub fn blend_exclusion(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = clamp_byte_v(b + l - 2 * b * l / 255)
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 颜色减淡。result = base * 255 / (255 - blend),blend=255 时结果为 255。
pub fn blend_color_dodge(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = if l == 255 { b'\xFF' } else { clamp_byte_v(b * 255 / (255 - l)) }
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 颜色加深。result = 255 - (255 - base) * 255 / blend,blend=0 时结果为 0。
pub fn blend_color_burn(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = if l == 0 {
      b'\x00'
    } else {
      clamp_byte_v(255 - (255 - b) * 255 / l)
    }
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 强光。blend < 128 时用颜色加深,否则用颜色减淡。
pub fn blend_hard_light(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = if l < 128 {
      clamp_byte_v(2 * b * l / 255)
    } else {
      clamp_byte_v(255 - 2 * (255 - b) * (255 - l) / 255)
    }
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 柔光。基于 blend 的亮度在乘法和滤色之间平滑过渡。
/// result = (1 - 2*blend/255) * base^2/255 + (2*blend/255) * base
pub fn blend_soft_light(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    let w = 2 * l
    out[i] = clamp_byte_v((255 - w) * b * b / (255 * 255) + w * b / 255)
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 线性减淡(相加)。result = min(base + blend, 255)。
pub fn blend_linear_dodge(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = clamp_byte_v(b + l)
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}

///|
/// 线性加深(相减)。result = max(base + blend - 255, 0)。
pub fn blend_linear_burn(
  base : @types.Image,
  blend : @types.Image,
) -> @types.Image raise @types.LoadError {
  if base.width != blend.width ||
    base.height != blend.height ||
    base.channels != blend.channels {
    raise @types.LoadError::DecodeFailed(
      "blend 两图尺寸/通道数必须相同",
    )
  }
  let n = base.width * base.height * base.channels
  let out : Array[Byte] = Array::make(n, b'\x00')
  for i in 0..<n {
    let b = base.data[i].to_int()
    let l = blend.data[i].to_int()
    out[i] = clamp_byte_v(b + l - 255)
  }
  @types.Image::{
    width: base.width,
    height: base.height,
    channels: base.channels,
    data: Bytes::from_array(out),
  }
}