13
13
* - don't stop on first non-transparent image [felixbuenemann, GDmac]
14
14
* 6/12/18
15
15
* - do our own subimage positioning
16
+ * 8/5/19
17
+ * - revise in/out/dest-in/dest-out to make smoother alpha
16
18
*/
17
19
18
20
/*
@@ -461,14 +463,18 @@ vips_composite_base_blend( VipsCompositeBase *composite,
461
463
462
464
case VIPS_BLEND_MODE_IN:
463
465
aR = aA * aB;
464
- for ( int b = 0 ; b < bands; b++ )
465
- B[b] = A[b];
466
+ // if aA == 0, then aR == 0 and so B will already be 0
467
+ if ( aA != 0 )
468
+ for ( int b = 0 ; b < bands; b++ )
469
+ B[b] = A[b] * aR / aA;
466
470
break ;
467
471
468
472
case VIPS_BLEND_MODE_OUT:
469
473
aR = aA * (1 - aB);
470
- for ( int b = 0 ; b < bands; b++ )
471
- B[b] = A[b];
474
+ // if aA == 0, then aR == 0 and so B will already be 0
475
+ if ( aA != 0 )
476
+ for ( int b = 0 ; b < bands; b++ )
477
+ B[b] = A[b] * aR / aA;
472
478
break ;
473
479
474
480
case VIPS_BLEND_MODE_ATOP:
@@ -493,11 +499,18 @@ vips_composite_base_blend( VipsCompositeBase *composite,
493
499
case VIPS_BLEND_MODE_DEST_IN:
494
500
aR = aA * aB;
495
501
// B = B
502
+ if ( aB != 0 )
503
+ for ( int b = 0 ; b < bands; b++ )
504
+ B[b] *= aR / aB;
496
505
break ;
497
506
498
507
case VIPS_BLEND_MODE_DEST_OUT:
499
508
aR = (1 - aA) * aB;
500
509
// B = B
510
+ // if aB is 0, then B is already 0
511
+ if ( aB != 0 )
512
+ for ( int b = 0 ; b < bands; b++ )
513
+ B[b] *= aR / aB;
501
514
break ;
502
515
503
516
case VIPS_BLEND_MODE_DEST_ATOP:
@@ -676,8 +689,8 @@ vips_composite_base_blend3( VipsCompositeBase *composite,
676
689
/* See https://www.cairographics.org/operators for a nice summary of
677
690
* the operators and their meaning.
678
691
*
679
- * Some operators need the unpremultiplied values, so we have to do an
680
- * extra unpremultiply/premultiply.
692
+ * Some operators need the unpremultiplied values (eg. dest-in) , so
693
+ * we have to do an extra unpremultiply/premultiply.
681
694
*/
682
695
683
696
switch ( mode ) {
@@ -701,18 +714,14 @@ vips_composite_base_blend3( VipsCompositeBase *composite,
701
714
702
715
case VIPS_BLEND_MODE_IN:
703
716
aR = aA * aB;
704
- // we want to set B = A, but A has been premultiplied against
705
- // aA ... we must undo that premul and redo against aR
706
- // if aA == 0, then aR == 0 and so B will be set to 0
717
+ // if aA == 0, then aR == 0 and so B will already be 0
707
718
if ( aA != 0 )
708
719
B = A * aR / aA;
709
720
break ;
710
721
711
722
case VIPS_BLEND_MODE_OUT:
712
723
aR = aA * (1 - aB);
713
- // we want to set B = A, but A has been premultiplied against
714
- // aA ... we must undo that premul and redo against aR
715
- // if aA == 0, then aR == 0 and so B will be set to 0
724
+ // if aA == 0, then aR == 0 and so B will already be 0
716
725
if ( aA != 0 )
717
726
B = A * aR / aA;
718
727
break ;
@@ -736,17 +745,17 @@ vips_composite_base_blend3( VipsCompositeBase *composite,
736
745
737
746
case VIPS_BLEND_MODE_DEST_IN:
738
747
aR = aA * aB;
739
- // we need to simply copy B over, but B is premultiplied
740
- // against aB ... we must unpremultiply and premultiply
741
- // against aR instead
742
- // if aB is 0, then B is 0 too
743
- // if( aB != 0 )
744
- // B *= aR / aB;
748
+ // if aB is 0, then B is already 0
749
+ if ( aB != 0 )
750
+ B *= aR / aB;
745
751
break ;
746
752
747
753
case VIPS_BLEND_MODE_DEST_OUT:
748
754
aR = (1 - aA) * aB;
749
755
// B = B
756
+ // if aB is 0, then B is already 0
757
+ if ( aB != 0 )
758
+ B *= aR / aB;
750
759
break ;
751
760
752
761
case VIPS_BLEND_MODE_DEST_ATOP:
0 commit comments