1
1
const alignArrays = require ( './alignArrays' ) ;
2
+ const compose = require ( './compose' ) ;
2
3
const similarEnough = require ( './similarEnough' ) ;
3
4
4
5
function imageTo2DArray ( { data, width, height } , paddingRight ) {
@@ -8,16 +9,16 @@ function imageTo2DArray({ data, width, height }, paddingRight) {
8
9
9
10
const newData = [ ] ;
10
11
for ( let row = 0 ; row < height ; row += 1 ) {
11
- const pixelsInRow = new Uint8ClampedArray ( rowSize + ( paddingRight * 4 ) ) ;
12
+ const pixelsInRow = new Uint8ClampedArray ( rowSize + paddingRight * 4 ) ;
12
13
for ( let location = 0 ; location < rowSize ; location += 1 ) {
13
- pixelsInRow [ location ] = data [ ( row * rowSize ) + location ] ;
14
+ pixelsInRow [ location ] = data [ row * rowSize + location ] ;
14
15
}
15
16
newData . push ( pixelsInRow ) ;
16
17
}
17
18
return newData ;
18
19
}
19
20
20
- function hashFn ( ) {
21
+ function resolveHashFn ( ) {
21
22
// Safari has a bug where trying to reference `btoa` inside a web worker will
22
23
// result in an error, so we fall back to the slower (?) `JSON.stringify`. The
23
24
// only way to prevent this seems to be by using a try/catch. We do this in its
@@ -38,42 +39,41 @@ function hashFn() {
38
39
}
39
40
}
40
41
41
- const HASH_FN = hashFn ( ) ;
42
+ const HASH_FN = resolveHashFn ( ) ;
42
43
43
- function align ( {
44
- image1Data,
45
- image2Data,
46
- maxWidth,
47
- hashFunction,
48
- } ) {
44
+ function transparentLine ( rawBgPixel , width ) {
45
+ const bgPixel = compose ( [ 200 , 200 , 200 , 50 ] , rawBgPixel ) ;
46
+ const result = new Uint8ClampedArray ( width * 4 ) ;
47
+ for ( let i = 0 ; i < width * 4 ; i += 4 ) {
48
+ result [ i ] = bgPixel [ 0 ] ;
49
+ result [ i + 1 ] = bgPixel [ 1 ] ;
50
+ result [ i + 2 ] = bgPixel [ 2 ] ;
51
+ result [ i + 3 ] = 122 ;
52
+ }
53
+ return result ;
54
+ }
55
+
56
+ function align ( { image1Data, image2Data, maxWidth, hashFunction } ) {
49
57
if ( similarEnough ( { image1Data, image2Data } ) ) {
50
58
return ;
51
59
}
52
60
53
61
const hashedImage1Data = image1Data . map ( hashFunction ) ;
54
62
const hashedImage2Data = image2Data . map ( hashFunction ) ;
55
63
56
- alignArrays (
57
- hashedImage1Data ,
58
- hashedImage2Data ,
59
- ) ;
60
-
61
- const transparentLine = new Uint8ClampedArray ( maxWidth * 4 ) ;
62
- for ( let i = 0 ; i < maxWidth * 4 ; i ++ ) {
63
- if ( ( i + 1 ) % 4 !== 0 ) {
64
- transparentLine [ i ] = 1 ;
65
- }
66
- }
64
+ alignArrays ( hashedImage1Data , hashedImage2Data ) ;
67
65
66
+ const image1Bg = image1Data [ 0 ] . slice ( 0 , 4 ) ;
68
67
hashedImage1Data . forEach ( ( hashedLine , i ) => {
69
68
if ( hashedLine === alignArrays . PLACEHOLDER ) {
70
- image1Data . splice ( i , 0 , transparentLine ) ;
69
+ image1Data . splice ( i , 0 , transparentLine ( image1Bg , maxWidth ) ) ;
71
70
}
72
71
} ) ;
73
72
73
+ const image2Bg = image2Data [ 0 ] . slice ( 0 , 4 ) ;
74
74
hashedImage2Data . forEach ( ( hashedLine , i ) => {
75
75
if ( hashedLine === alignArrays . PLACEHOLDER ) {
76
- image2Data . splice ( i , 0 , transparentLine ) ;
76
+ image2Data . splice ( i , 0 , transparentLine ( image2Bg , maxWidth ) ) ;
77
77
}
78
78
} ) ;
79
79
}
@@ -90,7 +90,11 @@ function align({
90
90
* @param {Array } image2
91
91
* @return {Object }
92
92
*/
93
- module . exports = function computeAndInjectDiffs ( { image1, image2, hashFunction = HASH_FN } ) {
93
+ module . exports = function computeAndInjectDiffs ( {
94
+ image1,
95
+ image2,
96
+ hashFunction = HASH_FN ,
97
+ } ) {
94
98
const maxWidth = Math . max ( image1 . width , image2 . width ) ;
95
99
96
100
const image1Data = imageTo2DArray ( image1 , maxWidth - image1 . width ) ;
0 commit comments