@@ -11,7 +11,7 @@ export interface AlignMinDifferenceOptions {
11
11
}
12
12
13
13
/**
14
- * Aligns two grayscale images by finding the translation that minimizes the mean difference
14
+ * Aligns two images by finding the translation that minimizes the mean difference of all channels.
15
15
* between them. The source image should fit entirely in the destination image.
16
16
* @param source - Image to align.
17
17
* @param destination - Image to align to.
@@ -24,7 +24,9 @@ export function alignMinDifference(
24
24
destination : Image ,
25
25
options : AlignMinDifferenceOptions = { } ,
26
26
) {
27
- checkProcessable ( source , { components : 1 , bitDepth : [ 8 , 16 ] , alpha : false } ) ;
27
+ checkProcessable ( source , {
28
+ bitDepth : [ 8 , 16 ] ,
29
+ } ) ;
28
30
29
31
const xSpan = destination . width - source . width ;
30
32
const ySpan = destination . height - source . height ;
@@ -69,21 +71,23 @@ export function alignMinDifference(
69
71
if ( mask && ! mask . getBit ( column , row ) ) {
70
72
continue ;
71
73
}
72
- const sourceValue = source . getValue ( column , row , 0 ) ;
73
- const destinationValue = destination . getValue (
74
- column + shiftX ,
75
- row + shiftY ,
76
- 0 ,
77
- ) ;
78
- const difference = sourceValue - destinationValue ;
79
- if ( difference < 0 ) {
80
- // Math.abs is super slow, this simple trick is 5x faster
81
- currentDifference -= difference ;
82
- } else {
83
- currentDifference += difference ;
84
- }
85
- if ( currentDifference > bestDifference ) {
86
- break next;
74
+ for ( let channel = 0 ; channel < source . channels ; channel ++ ) {
75
+ const sourceValue = source . getValue ( column , row , channel ) ;
76
+ const destinationValue = destination . getValue (
77
+ column + shiftX ,
78
+ row + shiftY ,
79
+ channel ,
80
+ ) ;
81
+ const difference = sourceValue - destinationValue ;
82
+ if ( difference < 0 ) {
83
+ // Math.abs is super slow, this simple trick is 5x faster
84
+ currentDifference -= difference ;
85
+ } else {
86
+ currentDifference += difference ;
87
+ }
88
+ if ( currentDifference > bestDifference ) {
89
+ break next;
90
+ }
87
91
}
88
92
}
89
93
}
0 commit comments