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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
 * Copyright 2006 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#![allow(non_snake_case)]

use crate::*;

pub trait Blend {
    fn blend(src: u32, dst: u32) -> u32;
}

pub struct Dst;

impl Blend for Dst {
    #[inline]
    fn blend(_src: u32, dst: u32) -> u32 {
        dst
    }
}

pub struct Src;

impl Blend for Src {
    #[inline]
    fn blend(src: u32, _dst: u32) -> u32 {
        src
    }
}

pub struct Clear;

impl Blend for Clear {
    #[inline]
    fn blend(_src: u32, _dst: u32) -> u32 {
        0
    }
}

pub struct SrcOver;

impl Blend for SrcOver {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        over(src, dst)
    }
}

pub struct DstOver;

impl Blend for DstOver {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        over(dst, src)
    }
}

pub struct SrcIn;

impl Blend for SrcIn {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        alpha_mul(src, alpha_to_alpha256(packed_alpha(dst)))
    }
}

pub struct DstIn;

impl Blend for DstIn {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        alpha_mul(dst, alpha_to_alpha256(packed_alpha(src)))
    }
}

pub struct SrcOut;

impl Blend for SrcOut {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        alpha_mul(src, alpha_to_alpha256(255 - packed_alpha(dst)))
    }
}

pub struct DstOut;

impl Blend for DstOut {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        alpha_mul(dst, alpha_to_alpha256(255 - packed_alpha(src)))
    }
}

pub struct SrcAtop;

impl Blend for SrcAtop {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = packed_alpha(src);
        let da = packed_alpha(dst);
        let isa = 255 - sa;

        return pack_argb32(da,
                           muldiv255(da, get_packed_r32(src)) +
                               muldiv255(isa, get_packed_r32(dst)),
                           muldiv255(da, get_packed_g32(src)) +
                               muldiv255(isa, get_packed_g32(dst)),
                           muldiv255(da, get_packed_b32(src)) +
                               muldiv255(isa, get_packed_b32(dst)));
    }
}

pub struct DstAtop;

impl Blend for DstAtop {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = packed_alpha(src);
        let da = packed_alpha(dst);
        let ida = 255 - da;

        return pack_argb32(sa,
                           muldiv255(ida, get_packed_r32(src)) +
                               muldiv255(sa, get_packed_r32(dst)),
                           muldiv255(ida, get_packed_g32(src)) +
                               muldiv255(sa, get_packed_g32(dst)),
                           muldiv255(ida, get_packed_b32(src)) +
                               muldiv255(sa, get_packed_b32(dst)));
    }
}

pub struct Xor;

impl Blend for Xor {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = packed_alpha(src);
        let da = packed_alpha(dst);
        let isa = 255 - sa;
        let ida = 255 - da;

        return pack_argb32(sa + da - (muldiv255(sa, da) * 2),
                           muldiv255(ida, get_packed_r32(src)) +
                               muldiv255(isa, get_packed_r32(dst)),
                           muldiv255(ida, get_packed_g32(src)) +
                               muldiv255(isa, get_packed_g32(dst)),
                           muldiv255(ida, get_packed_b32(src)) +
                               muldiv255(isa, get_packed_b32(dst)));
    }
}

fn saturated_add(a: u32, b: u32) -> u32 {
    debug_assert!(a <= 255);
    debug_assert!(b <= 255);
    let sum = a + b;
    if sum > 255 {
        255
    } else {
        sum
    }
}

pub struct Add;

impl Blend for Add {
    #[inline]
    fn blend(src: u32, dst: u32) -> u32 {
        pack_argb32(saturated_add(get_packed_a32(src), get_packed_a32(dst)),
                    saturated_add(get_packed_r32(src), get_packed_r32(dst)),
                    saturated_add(get_packed_g32(src), get_packed_g32(dst)),
                    saturated_add(get_packed_b32(src), get_packed_b32(dst)))
    }
}

// kMultiply_Mode
// B(Cb, Cs) = Cb x Cs
// multiply uses its own version of blendfunc_byte because sa and da are not needed
fn blendfunc_multiply_byte(sc: i32, dc: i32, sa: i32, da: i32) -> u32 {
    clamp_div255round(sc * (255 - da) + dc * (255 - sa) + sc * dc)
}

pub struct Multiply;

impl Blend for Multiply {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src) as i32;
        let da = get_packed_a32(dst) as i32;
        pack_argb32(srcover_byte(get_packed_a32(src), get_packed_a32(dst)),
                    blendfunc_multiply_byte(get_packed_r32(src) as i32, get_packed_r32(dst) as i32, sa, da),
                    blendfunc_multiply_byte(get_packed_g32(src) as i32, get_packed_g32(dst) as i32, sa, da),
                    blendfunc_multiply_byte(get_packed_b32(src) as i32, get_packed_b32(dst) as i32, sa, da))
    }
}

fn srcover_byte(a: u32, b: u32) -> u32 {
    a + b - muldiv255(a, b)
}

pub struct Screen;

impl Blend for Screen {
    fn blend(src: u32, dst: u32) -> u32 {
        pack_argb32(srcover_byte(get_packed_a32(src), get_packed_a32(dst)),
                    srcover_byte(get_packed_r32(src), get_packed_r32(dst)),
                    srcover_byte(get_packed_g32(src), get_packed_g32(dst)),
                    srcover_byte(get_packed_b32(src), get_packed_b32(dst)))
    }
}

fn clamp_div255round(prod: i32) -> u32 {
    if prod <= 0 {
        return 0;
    } else if prod >= 255 * 255 {
        return 255;
    } else {
        return div255(prod as u32);
    }
}

fn overlay_byte(sc: u32, dc: u32, sa: u32, da: u32) -> u32 {
    let tmp = sc * (255 - da) + dc * (255 - sa);
    let rc;
    if 2 * dc <= da {
        rc = 2 * sc * dc;
    } else {
        rc = sa * da - 2 * (da - dc) * (sa - sc);
    }
    clamp_div255round((rc + tmp) as i32)
}

pub struct Overlay;

impl Blend for Overlay {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src);
        let da = get_packed_a32(dst);
        pack_argb32(srcover_byte(sa, da),
                    overlay_byte(get_packed_r32(src), get_packed_r32(dst), sa, da),
                    overlay_byte(get_packed_g32(src), get_packed_g32(dst), sa, da),
                    overlay_byte(get_packed_b32(src), get_packed_b32(dst), sa, da))
    }
}

fn darken_byte(sc: u32, dc: u32, sa: u32, da: u32) -> u32 {
    let sd = sc * da;
    let ds = dc * sa;
    if sd < ds {
        // srcover
        return sc + dc - div255(ds);
    } else {
        // dstover
        return dc + sc - div255(sd);
    }
}

pub struct Darken;

impl Blend for Darken {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src);
        let da = get_packed_a32(dst);
        pack_argb32(srcover_byte(sa, da),
                    darken_byte(get_packed_r32(src), get_packed_r32(dst), sa, da),
                    darken_byte(get_packed_g32(src), get_packed_g32(dst), sa, da),
                    darken_byte(get_packed_b32(src), get_packed_b32(dst), sa, da))
    }
}

fn lighten_byte(sc: u32, dc: u32, sa: u32, da: u32) -> u32 {
    let sd = sc * da;
    let ds = dc * sa;
    if sd > ds {
        // srcover
        return sc + dc - div255(ds);
    } else {
        // dstover
        return dc + sc - div255(sd);
    }
}

pub struct Lighten;

impl Blend for Lighten {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src);
        let da = get_packed_a32(dst);
        pack_argb32(srcover_byte(sa, da),
                    lighten_byte(get_packed_r32(src), get_packed_r32(dst), sa, da),
                    lighten_byte(get_packed_g32(src), get_packed_g32(dst), sa, da),
                    lighten_byte(get_packed_b32(src), get_packed_b32(dst), sa, da))
    }
}

fn colordodge_byte(sc: i32, dc: i32, sa: i32, da: i32) -> u32 {
    let mut diff = sa - sc;
    let rc;
    if 0 == dc {
        return muldiv255(sc as u32, (255 - da) as u32);
    } else if 0 == diff {
        rc = sa * da + sc * (255 - da) + dc * (255 - sa);
    } else {
        diff = (dc * sa) / diff;
        rc = sa * (if da < diff { da } else { diff }) + sc * (255 - da) + dc * (255 - sa);
    }
    return clamp_div255round(rc);
}

pub struct ColorDodge;

impl Blend for ColorDodge {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src) as i32;
        let da = get_packed_a32(dst) as i32;
        pack_argb32(srcover_byte(sa as u32, da as u32),
                    colordodge_byte(get_packed_r32(src) as i32, get_packed_r32(dst) as i32, sa, da),
                    colordodge_byte(get_packed_g32(src) as i32, get_packed_g32(dst) as i32, sa, da),
                    colordodge_byte(get_packed_b32(src) as i32, get_packed_b32(dst) as i32, sa, da))
    }
}

fn colorburn_byte(sc: i32, dc: i32, sa: i32, da: i32) -> u32 {
    let rc;
    if dc == da {
        rc = sa * da + sc * (255 - da) + dc * (255 - sa);
    } else if 0 == sc {
        return muldiv255(dc as u32, (255 - sa) as u32);
    } else {
        let tmp = (da - dc) * sa / sc;
        rc = sa * (da - (if da < tmp { da } else { tmp }))
            + sc * (255 - da) + dc * (255 - sa);
    }
    return clamp_div255round(rc);
}

pub struct ColorBurn;

impl Blend for ColorBurn {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src) as i32;
        let da = get_packed_a32(dst) as i32;
        pack_argb32(srcover_byte(sa as u32, da as u32),
                    colorburn_byte(get_packed_r32(src) as i32, get_packed_r32(dst) as i32, sa, da),
                    colorburn_byte(get_packed_g32(src) as i32, get_packed_g32(dst) as i32, sa, da),
                    colorburn_byte(get_packed_b32(src) as i32, get_packed_b32(dst) as i32, sa, da))
    }
}

fn hardlight_byte(sc: i32, dc: i32, sa: i32, da: i32) -> u32 {
    let rc;
    if 2 * sc <= sa {
        rc = 2 * sc * dc;
    } else {
        rc = sa * da - 2 * (da - dc) * (sa - sc);
    }
    return clamp_div255round(rc + sc * (255 - da) + dc * (255 - sa));
}

pub struct HardLight;

impl Blend for HardLight {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src) as i32;
        let da = get_packed_a32(dst) as i32;
        pack_argb32(srcover_byte(sa as u32, da as u32),
                    hardlight_byte(get_packed_r32(src) as i32, get_packed_r32(dst) as i32, sa, da),
                    hardlight_byte(get_packed_g32(src) as i32, get_packed_g32(dst) as i32, sa, da),
                    hardlight_byte(get_packed_b32(src) as i32, get_packed_b32(dst) as i32, sa, da))
    }
}

/* www.worldserver.com/turk/computergraphics/FixedSqrt.pdf
*/
fn sqrt_bits(x: i32, count: i32) -> i32 {
    debug_assert!(x >= 0 && count > 0 && count <= 30);

    let mut root = 0;
    let mut rem_hi = 0;
    let mut rem_lo = x;

    loop {
        root <<= 1;

        rem_hi = (rem_hi << 2) | (rem_lo >> 30);
        rem_lo <<= 2;

        let test_div = (root << 1) + 1;
        if rem_hi >= test_div {
            rem_hi -= test_div;
            root += 1;
        }
        if -count < 0 {
            break;
        }
    }

    return root;
}

type U8Cpu = u32;

// returns 255 * sqrt(n/255)
fn sqrt_unit_byte(n: U8Cpu) -> U8Cpu {
    return sqrt_bits(n as i32, 15 + 4) as u32;
}

fn softlight_byte(sc: i32, dc: i32, sa: i32, da: i32) -> u32 {
    let m = if da != 0 { dc * 256 / da } else { 0 };
    let rc;
    if 2 * sc <= sa {
        rc = dc * (sa + ((2 * sc - sa) * (256 - m) >> 8));
    } else if 4 * dc <= da {
        let tmp = (4 * m * (4 * m + 256) * (m - 256) >> 16) + 7 * m;
        rc = dc * sa + (da * (2 * sc - sa) * tmp >> 8);
    } else {
        let tmp = sqrt_unit_byte(m as u32) as i32 - m;
        rc = dc * sa + (da * (2 * sc - sa) * tmp >> 8);
    }
    return clamp_div255round(rc + sc * (255 - da) + dc * (255 - sa));
}

pub struct SoftLight;

impl Blend for SoftLight {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src) as i32;
        let da = get_packed_a32(dst) as i32;
        pack_argb32(srcover_byte(sa as u32, da as u32),
                    softlight_byte(get_packed_r32(src) as i32, get_packed_r32(dst) as i32, sa, da),
                    softlight_byte(get_packed_g32(src) as i32, get_packed_g32(dst) as i32, sa, da),
                    softlight_byte(get_packed_b32(src) as i32, get_packed_b32(dst) as i32, sa, da))
    }
}


fn clamp_signed_byte(n: i32) -> u32 {
    if n < 0 {
        0
    } else if n > 255 {
        255
    } else {
        n as u32
    }
}

fn difference_byte(sc: i32, dc: i32, sa: i32, da: i32) -> u32 {
    let tmp = (sc * da).min(dc * sa);
    return clamp_signed_byte(sc + dc - 2 * div255(tmp as u32) as i32);
}

pub struct Difference;

impl Blend for Difference {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src) as i32;
        let da = get_packed_a32(dst) as i32;
        pack_argb32(srcover_byte(sa as u32, da as u32),
                    difference_byte(get_packed_r32(src) as i32, get_packed_r32(dst) as i32, sa, da),
                    difference_byte(get_packed_g32(src) as i32, get_packed_g32(dst) as i32, sa, da),
                    difference_byte(get_packed_b32(src) as i32, get_packed_b32(dst) as i32, sa, da))
    }
}

fn exclusion_byte(sc: i32, dc: i32, _sa: i32, _da: i32) -> u32 {
    // this equations is wacky, wait for SVG to confirm it
    //int r = sc * da + dc * sa - 2 * sc * dc + sc * (255 - da) + dc * (255 - sa);

    // The above equation can be simplified as follows
    let r = 255 * (sc + dc) - 2 * sc * dc;
    return clamp_div255round(r);
}

pub struct Exclusion;

impl Blend for Exclusion {
    fn blend(src: u32, dst: u32) -> u32 {
        let sa = get_packed_a32(src) as i32;
        let da = get_packed_a32(dst) as i32;
        pack_argb32(srcover_byte(sa as u32, da as u32),
                    exclusion_byte(get_packed_r32(src) as i32, get_packed_r32(dst) as i32, sa, da),
                    exclusion_byte(get_packed_g32(src) as i32, get_packed_g32(dst) as i32, sa, da),
                    exclusion_byte(get_packed_b32(src) as i32, get_packed_b32(dst) as i32, sa, da))
    }
}

// The CSS compositing spec introduces the following formulas:
// (See https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blendingnonseparable)
// SkComputeLuminance is similar to this formula but it uses the new definition from Rec. 709
// while PDF and CG uses the one from Rec. Rec. 601
// See http://www.glennchan.info/articles/technical/hd-versus-sd-color-space/hd-versus-sd-color-space.htm
fn lum(r: i32, g: i32, b: i32) -> i32
{
    div255((r * 77 + g * 150 + b * 28) as u32) as i32
}

fn mul_div(numer1: i32, numer2: i32, denom: i32) -> i32 {
    let tmp = (numer1 as i64 * numer2 as i64) / denom as i64;
    return tmp as i32;
}

fn minimum(a: i32, b: i32, c: i32) -> i32 {
    a.min(b).min(c)
}

fn maximum(a: i32, b: i32, c: i32) -> i32 {
    a.max(b).max(c)
}

fn clip_color(r: &mut i32, g: &mut i32, b: &mut i32, a: i32) {
    let L = lum(*r, *g, *b);
    let n = minimum(*r, *g, *b);
    let x = maximum(*r, *g, *b);
    let denom = L - n;
    if (n < 0) && (denom != 0) { // Compute denom and make sure it's non zero
        *r = L + mul_div(*r - L, L, denom);
        *g = L + mul_div(*g - L, L, denom);
        *b = L + mul_div(*b - L, L, denom);
    }

    let denom = x - L;
    if (x > a) && (denom != 0) { // Compute denom and make sure it's non zero
        let numer = a - L;
        *r = L + mul_div(*r - L, numer, denom);
        *g = L + mul_div(*g - L, numer, denom);
        *b = L + mul_div(*b - L, numer, denom);
    }
}

fn sat(r: i32, g: i32, b: i32) -> i32 {
    maximum(r, g, b) - minimum(r, g, b)
}

fn set_saturation_components(cmin: &mut i32, cmind: &mut i32, cmax: &mut i32, s: i32) {
    if *cmax > *cmin {
        *cmind = mul_div(*cmind - *cmin, s, *cmax - *cmin);
        *cmax = s;
    } else {
        *cmax = 0;
        *cmind = 0;
    }

    *cmin = 0;
}

fn set_sat(r: &mut i32, g: &mut i32, b: &mut i32, s: i32) {
    if *r <= *g {
        if *g <= *b {
            set_saturation_components(r, g, b, s);
        } else if *r <= *b {
            set_saturation_components(r, b, g, s);
        } else {
            set_saturation_components(b, r, g, s);
        }
    } else if *r <= *b {
        set_saturation_components(g, r, b, s);
    } else if *g <= *b {
        set_saturation_components(g, b, r, s);
    } else {
        set_saturation_components(b, g, r, s);
    }
}

fn set_lum(r: &mut i32, g: &mut i32, b: &mut i32, a: i32, l: i32) {
    let d = l - lum(*r, *g, *b);
    *r += d;
    *g += d;
    *b += d;

    clip_color(r, g, b, a);
}

// non-separable blend modes are done in non-premultiplied alpha
fn blendfunc_nonsep_byte(sc: i32, dc: i32, sa: i32, da: i32, blendval: i32) -> u32 {
    clamp_div255round(sc * (255 - da) + dc * (255 - sa) + blendval)
}

pub struct Hue;

impl Blend for Hue {
    fn blend(src: u32, dst: u32) -> u32 {
        let sr = get_packed_r32(src) as i32;
        let sg = get_packed_g32(src) as i32;
        let sb = get_packed_b32(src) as i32;
        let sa = get_packed_a32(src) as i32;

        let dr = get_packed_r32(dst) as i32;
        let dg = get_packed_g32(dst) as i32;
        let db = get_packed_b32(dst) as i32;
        let da = get_packed_a32(dst) as i32;
        let mut Sr;
        let mut Sg;
        let mut Sb;

        if sa != 0 && da != 0 {
            Sr = sr * sa;
            Sg = sg * sa;
            Sb = sb * sa;
            set_sat(&mut Sr, &mut Sg, &mut Sb, sat(dr, dg, db) * sa);
            set_lum(&mut Sr, &mut Sg, &mut Sb, sa * da, lum(dr, dg, db) * sa);
        } else {
            Sr = 0;
            Sg = 0;
            Sb = 0;
        }

        let a = srcover_byte(sa as u32, da as u32);
        let r = blendfunc_nonsep_byte(sr, dr, sa, da, Sr);
        let g = blendfunc_nonsep_byte(sg, dg, sa, da, Sg);
        let b = blendfunc_nonsep_byte(sb, db, sa, da, Sb);
        return pack_argb32(a, r, g, b);
    }
}

pub struct Saturation;

impl Blend for Saturation {
    fn blend(src: u32, dst: u32) -> u32 {
        let sr = get_packed_r32(src) as i32;
        let sg = get_packed_g32(src) as i32;
        let sb = get_packed_b32(src) as i32;
        let sa = get_packed_a32(src) as i32;

        let dr = get_packed_r32(dst) as i32;
        let dg = get_packed_g32(dst) as i32;
        let db = get_packed_b32(dst) as i32;
        let da = get_packed_a32(dst) as i32;
        let mut Dr;
        let mut Dg;
        let mut Db;

        if sa != 0 && da != 0 {
            Dr = dr * sa;
            Dg = dg * sa;
            Db = db * sa;
            set_sat(&mut Dr, &mut Dg, &mut Db, sat(sr, sg, sb) * da);
            set_lum(&mut Dr, &mut Dg, &mut Db, sa * da, lum(dr, dg, db) * sa);
        } else {
            Dr = 0;
            Dg = 0;
            Db = 0;
        }

        let a = srcover_byte(sa as u32, da as u32);
        let r = blendfunc_nonsep_byte(sr, dr, sa, da, Dr);
        let g = blendfunc_nonsep_byte(sg, dg, sa, da, Dg);
        let b = blendfunc_nonsep_byte(sb, db, sa, da, Db);
        return pack_argb32(a, r, g, b);
    }
}

pub struct Color;

impl Blend for Color {
    fn blend(src: u32, dst: u32) -> u32 {
        let sr = get_packed_r32(src) as i32;
        let sg = get_packed_g32(src) as i32;
        let sb = get_packed_b32(src) as i32;
        let sa = get_packed_a32(src) as i32;

        let dr = get_packed_r32(dst) as i32;
        let dg = get_packed_g32(dst) as i32;
        let db = get_packed_b32(dst) as i32;
        let da = get_packed_a32(dst) as i32;
        let mut Sr;
        let mut Sg;
        let mut Sb;

        if sa != 0 && da != 0 {
            Sr = sr * sa;
            Sg = sg * sa;
            Sb = sb * sa;
            set_lum(&mut Sr, &mut Sg, &mut Sb, sa * da, lum(dr, dg, db) * sa);
        } else {
            Sr = 0;
            Sg = 0;
            Sb = 0;
        }

        let a = srcover_byte(sa as u32, da as u32);
        let r = blendfunc_nonsep_byte(sr, dr, sa, da, Sr);
        let g = blendfunc_nonsep_byte(sg, dg, sa, da, Sg);
        let b = blendfunc_nonsep_byte(sb, db, sa, da, Sb);
        return pack_argb32(a, r, g, b);
    }
}

pub struct Luminosity;

impl Blend for Luminosity {
    // B(Cb, Cs) = SetLum(Cb, Lum(Cs))
    // Create a color with the luminosity of the source color and the hue and saturation of the backdrop color.
    fn blend(src: u32, dst: u32) -> u32 {
        let sr = get_packed_r32(src) as i32;
        let sg = get_packed_g32(src) as i32;
        let sb = get_packed_b32(src) as i32;
        let sa = get_packed_a32(src) as i32;

        let dr = get_packed_r32(dst) as i32;
        let dg = get_packed_g32(dst) as i32;
        let db = get_packed_b32(dst) as i32;
        let da = get_packed_a32(dst) as i32;
        let mut Dr;
        let mut Dg;
        let mut Db;

        if sa != 0 && da != 0 {
            Dr = dr * sa;
            Dg = dg * sa;
            Db = db * sa;
            set_lum(&mut Dr, &mut Dg, &mut Db, sa * da, lum(sr, sg, sb) * da);
        } else {
            Dr = 0;
            Dg = 0;
            Db = 0;
        }

        let a = srcover_byte(sa as u32, da as u32);
        let r = blendfunc_nonsep_byte(sr, dr, sa, da, Dr);
        let g = blendfunc_nonsep_byte(sg, dg, sa, da, Dg);
        let b = blendfunc_nonsep_byte(sb, db, sa, da, Db);
        return pack_argb32(a, r, g, b);
    }
}