11const alignArrays = require ( './alignArrays' ) ;
2+ const compose = require ( './compose' ) ;
23const similarEnough = require ( './similarEnough' ) ;
34
45function imageTo2DArray ( { data, width, height } , paddingRight ) {
@@ -8,16 +9,16 @@ function imageTo2DArray({ data, width, height }, paddingRight) {
89
910 const newData = [ ] ;
1011 for ( let row = 0 ; row < height ; row += 1 ) {
11- const pixelsInRow = new Uint8ClampedArray ( rowSize + ( paddingRight * 4 ) ) ;
12+ const pixelsInRow = new Uint8ClampedArray ( rowSize + paddingRight * 4 ) ;
1213 for ( let location = 0 ; location < rowSize ; location += 1 ) {
13- pixelsInRow [ location ] = data [ ( row * rowSize ) + location ] ;
14+ pixelsInRow [ location ] = data [ row * rowSize + location ] ;
1415 }
1516 newData . push ( pixelsInRow ) ;
1617 }
1718 return newData ;
1819}
1920
20- function hashFn ( ) {
21+ function resolveHashFn ( ) {
2122 // Safari has a bug where trying to reference `btoa` inside a web worker will
2223 // result in an error, so we fall back to the slower (?) `JSON.stringify`. The
2324 // only way to prevent this seems to be by using a try/catch. We do this in its
@@ -38,42 +39,41 @@ function hashFn() {
3839 }
3940}
4041
41- const HASH_FN = hashFn ( ) ;
42+ const HASH_FN = resolveHashFn ( ) ;
4243
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 } ) {
4957 if ( similarEnough ( { image1Data, image2Data } ) ) {
5058 return ;
5159 }
5260
5361 const hashedImage1Data = image1Data . map ( hashFunction ) ;
5462 const hashedImage2Data = image2Data . map ( hashFunction ) ;
5563
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 ) ;
6765
66+ const image1Bg = image1Data [ 0 ] . slice ( 0 , 4 ) ;
6867 hashedImage1Data . forEach ( ( hashedLine , i ) => {
6968 if ( hashedLine === alignArrays . PLACEHOLDER ) {
70- image1Data . splice ( i , 0 , transparentLine ) ;
69+ image1Data . splice ( i , 0 , transparentLine ( image1Bg , maxWidth ) ) ;
7170 }
7271 } ) ;
7372
73+ const image2Bg = image2Data [ 0 ] . slice ( 0 , 4 ) ;
7474 hashedImage2Data . forEach ( ( hashedLine , i ) => {
7575 if ( hashedLine === alignArrays . PLACEHOLDER ) {
76- image2Data . splice ( i , 0 , transparentLine ) ;
76+ image2Data . splice ( i , 0 , transparentLine ( image2Bg , maxWidth ) ) ;
7777 }
7878 } ) ;
7979}
@@ -90,7 +90,11 @@ function align({
9090 * @param {Array } image2
9191 * @return {Object }
9292 */
93- module . exports = function computeAndInjectDiffs ( { image1, image2, hashFunction = HASH_FN } ) {
93+ module . exports = function computeAndInjectDiffs ( {
94+ image1,
95+ image2,
96+ hashFunction = HASH_FN ,
97+ } ) {
9498 const maxWidth = Math . max ( image1 . width , image2 . width ) ;
9599
96100 const image1Data = imageTo2DArray ( image1 , maxWidth - image1 . width ) ;
0 commit comments