This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
index.js
1214 lines (1037 loc) · 38.5 KB
/
index.js
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014-2015 Yahoo! Inc.
// Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
var assert = require('assert'),
PNGImage = require('pngjs-image'),
Promise = require('promise');
function load(value, defaultValue) {
return (value == null) ? defaultValue : value;
}
/**
* Blink-diff comparison class
*
* @constructor
* @class BlinkDiff
* @param {object} options
* @param {PNGImage|Buffer} options.imageA Image object of first image
* @param {string} options.imageAPath Path to first image
* @param {PNGImage|Buffer} options.imageB Image object of second image
* @param {string} options.imageBPath Path to second image
* @param {string} [options.imageOutputPath=undefined] Path to output image file
* @param {int} [options.imageOutputLimit=BlinkDiff.OUTPUT_ALL] Determines when an image output is created
* @param {string} [options.thresholdType=BlinkDiff.THRESHOLD_PIXEL] Defines the threshold of the comparison
* @param {int} [options.threshold=500] Threshold limit according to the comparison limit.
* @param {number} [options.delta=20] Distance between the color coordinates in the 4 dimensional color-space that will not trigger a difference.
* @param {int} [options.outputMaskRed=255] Value to set for red on difference pixel. 'Undefined' will not change the value.
* @param {int} [options.outputMaskGreen=0] Value to set for green on difference pixel. 'Undefined' will not change the value.
* @param {int} [options.outputMaskBlue=0] Value to set for blue on difference pixel. 'Undefined' will not change the value.
* @param {int} [options.outputMaskAlpha=255] Value to set for the alpha channel on difference pixel. 'Undefined' will not change the value.
* @param {float} [options.outputMaskOpacity=0.7] Strength of masking the pixel. 1.0 means that the full color will be used; anything less will mix-in the original pixel.
* @param {int} [options.outputShiftRed=255] Value to set for red on shifted pixel. 'Undefined' will not change the value.
* @param {int} [options.outputShiftGreen=165] Value to set for green on shifted pixel. 'Undefined' will not change the value.
* @param {int} [options.outputShiftBlue=0] Value to set for blue on shifted pixel. 'Undefined' will not change the value.
* @param {int} [options.outputShiftAlpha=255] Value to set for the alpha channel on shifted pixel. 'Undefined' will not change the value.
* @param {float} [options.outputShiftOpacity=0.7] Strength of masking the shifted pixel. 1.0 means that the full color will be used; anything less will mix-in the original pixel.
* @param {int} [options.outputBackgroundRed=0] Value to set for red as background. 'Undefined' will not change the value.
* @param {int} [options.outputBackgroundGreen=0] Value to set for green as background. 'Undefined' will not change the value.
* @param {int} [options.outputBackgroundBlue=0] Value to set for blue as background. 'Undefined' will not change the value.
* @param {int} [options.outputBackgroundAlpha=undefined] Value to set for the alpha channel as background. 'Undefined' will not change the value.
* @param {float} [options.outputBackgroundOpacity=0.6] Strength of masking the pixel. 1.0 means that the full color will be used; anything less will mix-in the original pixel.
* @param {object|object[]} [options.blockOut] Object or list of objects with coordinates of blocked-out areas.
* @param {int} [options.blockOutRed=0] Value to set for red on blocked-out pixel. 'Undefined' will not change the value.
* @param {int} [options.blockOutGreen=0] Value to set for green on blocked-out pixel. 'Undefined' will not change the value.
* @param {int} [options.blockOutBlue=0] Value to set for blue on blocked-out pixel. 'Undefined' will not change the value.
* @param {int} [options.blockOutAlpha=255] Value to set for the alpha channel on blocked-out pixel. 'Undefined' will not change the value.
* @param {float} [options.blockOutOpacity=1.0] Strength of masking the blocked-out pixel. 1.0 means that the full color will be used; anything less will mix-in the original pixel.
* @param {boolean} [options.copyImageAToOutput=true] Copies the first image to the output image before the comparison begins. This will make sure that the output image will highlight the differences on the first image.
* @param {boolean} [options.copyImageBToOutput=false] Copies the second image to the output image before the comparison begins. This will make sure that the output image will highlight the differences on the second image.
* @param {string[]} [options.filter=[]] Filters that will be applied before the comparison. Available filters are: blur, grayScale, lightness, luma, luminosity, sepia
* @param {boolean} [options.debug=false] When set, then the applied filters will be shown on the output image.
* @param {boolean} [options.composition=true] Should a composition be created to compare?
* @param {boolean} [options.composeLeftToRight=false] Create composition from left to right, otherwise let it decide on its own whats best
* @param {boolean} [options.composeTopToBottom=false] Create composition from top to bottom, otherwise let it decide on its own whats best
* @param {boolean} [options.hideShift=false] Hides shift highlighting by using the background color instead
* @param {int} [options.hShift=2] Horizontal shift for possible antialiasing
* @param {int} [options.vShift=2] Vertical shift for possible antialiasing
* @param {object} [options.cropImageA=null] Cropping for first image (default: no cropping)
* @param {int} [options.cropImageA.x=0] Coordinate for left corner of cropping region
* @param {int} [options.cropImageA.y=0] Coordinate for top corner of cropping region
* @param {int} [options.cropImageA.width] Width of cropping region (default: Width that is left)
* @param {int} [options.cropImageA.height] Height of cropping region (default: Height that is left)
* @param {object} [options.cropImageB=null] Cropping for second image (default: no cropping)
* @param {int} [options.cropImageB.x=0] Coordinate for left corner of cropping region
* @param {int} [options.cropImageB.y=0] Coordinate for top corner of cropping region
* @param {int} [options.cropImageB.width] Width of cropping region (default: Width that is left)
* @param {int} [options.cropImageB.height] Height of cropping region (default: Height that is left)
* @param {boolean} [options.perceptual=false] Turns perceptual comparison on
* @param {float} [options.gamma] Gamma correction for all colors
* @param {float} [options.gammaR] Gamma correction for red
* @param {float} [options.gammaG] Gamma correction for green
* @param {float} [options.gammaB] Gamma correction for blue
*
* @property {PNGImage} _imageA
* @property {PNGImage} _imageACompare
* @property {string} _imageAPath
* @property {PNGImage} _imageB
* @property {PNGImage} _imageBCompare
* @property {string} _imageBPath
* @property {PNGImage} _imageOutput
* @property {string} _imageOutputPath
* @property {int} _imageOutputLimit
* @property {string} _thresholdType
* @property {int} _threshold
* @property {number} _delta
* @property {int} _outputMaskRed
* @property {int} _outputMaskGreen
* @property {int} _outputMaskBlue
* @property {int} _outputMaskAlpha
* @property {float} _outputMaskOpacity
* @property {int} _outputShiftRed
* @property {int} _outputShiftGreen
* @property {int} _outputShiftBlue
* @property {int} _outputShiftAlpha
* @property {float} _outputShiftOpacity
* @property {int} _outputBackgroundRed
* @property {int} _outputBackgroundGreen
* @property {int} _outputBackgroundBlue
* @property {int} _outputBackgroundAlpha
* @property {float} _outputBackgroundOpacity
* @property {object[]} _blockOut
* @property {int} _blockOutRed
* @property {int} _blockOutGreen
* @property {int} _blockOutBlue
* @property {int} _blockOutAlpha
* @property {float} _blockOutOpacity
* @property {boolean} _copyImageAToOutput
* @property {boolean} _copyImageBToOutput
* @property {string[]} _filter
* @property {boolean} _debug
* @property {boolean} _composition
* @property {boolean} _composeLeftToRight
* @property {boolean} _composeTopToBottom
* @property {int} _hShift
* @property {int} _vShift
* @property {object} _cropImageA
* @property {int} _cropImageA.x
* @property {int} _cropImageA.y
* @property {int} _cropImageA.width
* @property {int} _cropImageA.height
* @property {object} _cropImageB
* @property {int} _cropImageB.x
* @property {int} _cropImageB.y
* @property {int} _cropImageB.width
* @property {int} _cropImageB.height
* @property {object} _refWhite
* @property {boolean} _perceptual
* @property {float} _gamma
* @property {float} _gammaR
* @property {float} _gammaG
* @property {float} _gammaB
*/
function BlinkDiff (options) {
this._imageA = options.imageA;
this._imageAPath = options.imageAPath;
assert.ok(options.imageAPath || options.imageA, "Image A not given.");
this._imageB = options.imageB;
this._imageBPath = options.imageBPath;
assert.ok(options.imageBPath || options.imageB, "Image B not given.");
this._imageOutput = null;
this._imageOutputPath = options.imageOutputPath;
this._imageOutputLimit = load(options.imageOutputLimit, BlinkDiff.OUTPUT_ALL);
// Pixel or Percent
this._thresholdType = load(options.thresholdType, BlinkDiff.THRESHOLD_PIXEL);
// How many pixels different to ignore.
this._threshold = load(options.threshold, 500);
this._delta = load(options.delta, 20);
this._outputMaskRed = load(options.outputMaskRed, 255);
this._outputMaskGreen = load(options.outputMaskGreen, 0);
this._outputMaskBlue = load(options.outputMaskBlue, 0);
this._outputMaskAlpha = load(options.outputMaskAlpha, 255);
this._outputMaskOpacity = load(options.outputMaskOpacity, 0.7);
this._outputBackgroundRed = load(options.outputBackgroundRed, 0);
this._outputBackgroundGreen = load(options.outputBackgroundGreen, 0);
this._outputBackgroundBlue = load(options.outputBackgroundBlue, 0);
this._outputBackgroundAlpha = options.outputBackgroundAlpha;
this._outputBackgroundOpacity = load(options.outputBackgroundOpacity, 0.6);
if (options.hideShift) {
this._outputShiftRed = this._outputBackgroundRed;
this._outputShiftGreen = this._outputBackgroundGreen;
this._outputShiftBlue = this._outputBackgroundBlue;
this._outputShiftAlpha = this._outputBackgroundAlpha;
this._outputShiftOpacity = this._outputBackgroundOpacity;
} else {
this._outputShiftRed = load(options.outputShiftRed, 200);
this._outputShiftGreen = load(options.outputShiftGreen, 100);
this._outputShiftBlue = load(options.outputShiftBlue, 0);
this._outputShiftAlpha = load(options.outputShiftAlpha, 255);
this._outputShiftOpacity = load(options.outputShiftOpacity, 0.7);
}
this._blockOut = load(options.blockOut, []);
if (typeof this._blockOut != 'object' && (this._blockOut.length !== undefined)) {
this._blockOut = [this._blockOut];
}
this._blockOutRed = load(options.blockOutRed, 0);
this._blockOutGreen = load(options.blockOutGreen, 0);
this._blockOutBlue = load(options.blockOutBlue, 0);
this._blockOutAlpha = load(options.blockOutAlpha, 255);
this._blockOutOpacity = load(options.blockOutOpacity, 1.0);
this._copyImageAToOutput = load(options.copyImageAToOutput, true);
this._copyImageBToOutput = load(options.copyImageBToOutput, false);
this._filter = load(options.filter, []);
this._debug = load(options.debug, false);
this._composition = load(options.composition, true);
this._composeLeftToRight = load(options.composeLeftToRight, false);
this._composeTopToBottom = load(options.composeTopToBottom, false);
this._hShift = load(options.hShift, 2);
this._vShift = load(options.vShift, 2);
this._cropImageA = options.cropImageA;
this._cropImageB = options.cropImageB;
// Prepare reference white
this._refWhite = this._convertRgbToXyz({c1: 1, c2: 1, c3: 1, c4: 1});
this._perceptual = load(options.perceptual, false);
this._gamma = options.gamma;
this._gammaR = options.gammaR;
this._gammaG = options.gammaG;
this._gammaB = options.gammaB;
}
/**
* Version of class
*
* @static
* @property version
* @type {string}
*/
BlinkDiff.version = require('./package.json').version;
/**
* Threshold-type for pixel
*
* @static
* @property THRESHOLD_PIXEL
* @type {string}
*/
BlinkDiff.THRESHOLD_PIXEL = 'pixel';
/**
* Threshold-type for percent of all pixels
*
* @static
* @property THRESHOLD_PERCENT
* @type {string}
*/
BlinkDiff.THRESHOLD_PERCENT = 'percent';
/**
* Unknown result of the comparison
*
* @static
* @property RESULT_UNKNOWN
* @type {int}
*/
BlinkDiff.RESULT_UNKNOWN = 0;
/**
* The images are too different
*
* @static
* @property RESULT_DIFFERENT
* @type {int}
*/
BlinkDiff.RESULT_DIFFERENT = 1;
/**
* The images are very similar, but still below the threshold
*
* @static
* @property RESULT_SIMILAR
* @type {int}
*/
BlinkDiff.RESULT_SIMILAR = 7;
/**
* The images are identical (or near identical)
*
* @static
* @property RESULT_IDENTICAL
* @type {int}
*/
BlinkDiff.RESULT_IDENTICAL = 5;
/**
* Create output when images are different
*
* @static
* @property OUTPUT_DIFFERENT
* @type {int}
*/
BlinkDiff.OUTPUT_DIFFERENT = 10;
/**
* Create output when images are similar or different
*
* @static
* @property OUTPUT_SIMILAR
* @type {int}
*/
BlinkDiff.OUTPUT_SIMILAR = 20;
/**
* Force output of all comparisons
*
* @static
* @property OUTPUT_ALL
* @type {int}
*/
BlinkDiff.OUTPUT_ALL = 100;
BlinkDiff.prototype = {
/**
* Runs the comparison with a promise
*
* @method runWithPromise
* @example
* var blinkDiff = BlinkDiff(...);
* blinkDiff.runWithPromise().then(function (result) {
* ...
* });
* @return {Promise}
*/
runWithPromise: function () {
return Promise.denodeify(this.run).call(this);
},
/**
* Runs the comparison in node-style
*
* @method run
* @example
* var blinkDiff = BlinkDiff(...);
* blinkDiff.run(function (err, result) {
* if (err) {
* throw err;
* }
*
* ...
* });
*
* @param {function} fn
*/
run: function (fn) {
var promise = Promise.resolve(), result;
PNGImage.log = function (text) {
this.log('ERROR: ' + text);
throw new Error('ERROR: ' + text);
}.bind(this);
promise.then(function () {
return this._loadImage(this._imageAPath, this._imageA);
}.bind(this)).then(function (imageA) {
this._imageA = imageA;
return this._loadImage(this._imageBPath, this._imageB);
}.bind(this)).then(function (imageB) {
var gamma, i, len, rect, color;
this._imageB = imageB;
// Crop images if requested
if (this._cropImageA) {
this._correctDimensions(this._imageA.getWidth(), this._imageA.getHeight(), this._cropImageA);
this._crop("Image-A", this._imageA, this._cropImageA);
}
if (this._cropImageB) {
this._correctDimensions(this._imageB.getWidth(), this._imageB.getHeight(), this._cropImageB);
this._crop("Image-B", this._imageB, this._cropImageB);
}
// Always clip
this._clip(this._imageA, this._imageB);
this._imageOutput = PNGImage.createImage(this._imageA.getWidth(), this._imageA.getHeight());
// Make a copy when not in debug mode
if (this._debug) {
this._imageACompare = this._imageA;
this._imageBCompare = this._imageB;
} else {
this._imageACompare = PNGImage.copyImage(this._imageA);
this._imageBCompare = PNGImage.copyImage(this._imageB);
}
// Block-out
color = {
red: this._blockOutRed,
green: this._blockOutGreen,
blue: this._blockOutBlue,
alpha: this._blockOutAlpha,
opacity: this._blockOutOpacity
};
for (i = 0, len = this._blockOut.length; i < len; i++) {
rect = this._blockOut[i];
// Make sure the block-out parameters fit
this._correctDimensions(this._imageACompare.getWidth(), this._imageACompare.getHeight(), rect);
this._imageACompare.fillRect(rect.x, rect.y, rect.width, rect.height, color);
this._imageBCompare.fillRect(rect.x, rect.y, rect.width, rect.height, color);
}
// Copy image to composition
if (this._copyImageAToOutput) {
this._copyImage(this._debug ? this._imageACompare : this._imageA, this._imageOutput);
} else if (this._copyImageBToOutput) {
this._copyImage(this._debug ? this._imageBCompare : this._imageB, this._imageOutput);
}
// Apply all filters
this._imageACompare.applyFilters(this._filter);
this._imageBCompare.applyFilters(this._filter);
// Gamma correction
if (this._gamma || this._gammaR || this._gammaG || this._gammaB) {
gamma = {
r: this._gammaR || this._gamma, g: this._gammaG || this._gamma, b: this._gammaB || this._gamma
};
}
// Comparison
result = this._compare(this._imageACompare, this._imageBCompare, this._imageOutput, this._delta, { // Output-Mask color
red: this._outputMaskRed,
green: this._outputMaskGreen,
blue: this._outputMaskBlue,
alpha: this._outputMaskAlpha,
opacity: this._outputMaskOpacity
}, { // Output-Shift color
red: this._outputShiftRed,
green: this._outputShiftGreen,
blue: this._outputShiftBlue,
alpha: this._outputShiftAlpha,
opacity: this._outputShiftOpacity
}, { // Background color
red: this._outputBackgroundRed,
green: this._outputBackgroundGreen,
blue: this._outputBackgroundBlue,
alpha: this._outputBackgroundAlpha,
opacity: this._outputBackgroundOpacity
}, this._hShift, this._vShift, this._perceptual, gamma);
// Create composition if requested
if (this._debug) {
this._imageOutput = this._createComposition(this._imageACompare, this._imageBCompare, this._imageOutput);
} else {
this._imageOutput = this._createComposition(this._imageA, this._imageB, this._imageOutput);
}
// Need to write to the filesystem?
if (this._imageOutputPath && this._withinOutputLimit(result.code, this._imageOutputLimit)) {
this._imageOutput.writeImage(this._imageOutputPath, function (err) {
if (err) {
fn(err);
} else {
this.log("Wrote differences to " + this._imageOutputPath);
fn(undefined, result);
}
}.bind(this));
} else {
fn(undefined, result);
}
}.bind(this)).then(null, function (err) {
console.error(err.stack);
fn(err);
});
},
/**
* Runs the comparison synchronously
*
* @method runSync
* @return {Object} Result of comparison { code, differences, dimension, width, height }
*/
runSync: function () {
var result, gamma, i, len, rect, color;
PNGImage.log = function (text) {
this.log('ERROR: ' + text);
throw new Error('ERROR: ' + text);
}.bind(this);
try {
this._imageA = this._loadImageSync(this._imageAPath, this._imageA);
this._imageB = this._loadImageSync(this._imageBPath, this._imageB);
// Crop images if requested
if (this._cropImageA) {
this._correctDimensions(this._imageA.getWidth(), this._imageA.getHeight(), this._cropImageA);
this._crop("Image-A", this._imageA, this._cropImageA);
}
if (this._cropImageB) {
this._correctDimensions(this._imageB.getWidth(), this._imageB.getHeight(), this._cropImageB);
this._crop("Image-B", this._imageB, this._cropImageB);
}
// Always clip
this._clip(this._imageA, this._imageB);
this._imageOutput = PNGImage.createImage(this._imageA.getWidth(), this._imageA.getHeight());
// Make a copy when not in debug mode
if (this._debug) {
this._imageACompare = this._imageA;
this._imageBCompare = this._imageB;
} else {
this._imageACompare = PNGImage.copyImage(this._imageA);
this._imageBCompare = PNGImage.copyImage(this._imageB);
}
// Block-out
color = {
red: this._blockOutRed,
green: this._blockOutGreen,
blue: this._blockOutBlue,
alpha: this._blockOutAlpha,
opacity: this._blockOutOpacity
};
for (i = 0, len = this._blockOut.length; i < len; i++) {
rect = this._blockOut[i];
// Make sure the block-out parameters fit
this._correctDimensions(this._imageACompare.getWidth(), this._imageACompare.getHeight(), rect);
this._imageACompare.fillRect(rect.x, rect.y, rect.width, rect.height, color);
this._imageBCompare.fillRect(rect.x, rect.y, rect.width, rect.height, color);
}
// Copy image to composition
if (this._copyImageAToOutput) {
this._copyImage(this._debug ? this._imageACompare : this._imageA, this._imageOutput);
} else if (this._copyImageBToOutput) {
this._copyImage(this._debug ? this._imageBCompare : this._imageB, this._imageOutput);
}
// Apply all filters
this._imageACompare.applyFilters(this._filter);
this._imageBCompare.applyFilters(this._filter);
// Gamma correction
if (this._gamma || this._gammaR || this._gammaG || this._gammaB) {
gamma = {
r: this._gammaR || this._gamma, g: this._gammaG || this._gamma, b: this._gammaB || this._gamma
};
}
// Comparison
result = this._compare(this._imageACompare, this._imageBCompare, this._imageOutput, this._delta,
{ // Output-Mask color
red: this._outputMaskRed,
green: this._outputMaskGreen,
blue: this._outputMaskBlue,
alpha: this._outputMaskAlpha,
opacity: this._outputMaskOpacity
}, { // Output-Shift color
red: this._outputShiftRed,
green: this._outputShiftGreen,
blue: this._outputShiftBlue,
alpha: this._outputShiftAlpha,
opacity: this._outputShiftOpacity
}, { // Background color
red: this._outputBackgroundRed,
green: this._outputBackgroundGreen,
blue: this._outputBackgroundBlue,
alpha: this._outputBackgroundAlpha,
opacity: this._outputBackgroundOpacity
},
this._hShift, this._vShift,
this._perceptual,
gamma
);
// Create composition if requested
if (this._debug) {
this._imageOutput = this._createComposition(this._imageACompare, this._imageBCompare, this._imageOutput);
} else {
this._imageOutput = this._createComposition(this._imageA, this._imageB, this._imageOutput);
}
// Need to write to the filesystem?
if (this._imageOutputPath && this._withinOutputLimit(result.code, this._imageOutputLimit)) {
this._imageOutput.writeImageSync(this._imageOutputPath);
this.log("Wrote differences to " + this._imageOutputPath);
}
return result;
} catch (err) {
console.error(err.stack);
throw err;
}
},
/**
* Determines if result is within the output limit
*
* @method _withinOutputLimit
* @param {int} resultCode
* @param {int} outputLimit
* @return {boolean}
* @private
*/
_withinOutputLimit: function (resultCode, outputLimit) {
return this._convertResultCodeToRelativeValue(resultCode) <= outputLimit;
},
/**
* Converts the result-code to a relative value
*
* @method _convertResultCodeToRelativeValue
* @param {int} resultCode
* @return {int}
* @private
*/
_convertResultCodeToRelativeValue: function (resultCode) {
var valueMap = {
0: 0, 1: 10, 7: 20, 5: 30
};
return valueMap[resultCode] !== undefined ? valueMap[resultCode] : 0;
},
/**
* Creates a comparison image
*
* @method _createComposition
* @param {PNGImage} imageA
* @param {PNGImage} imageB
* @param {PNGImage} imageOutput
* @return {PNGImage}
* @private
*/
_createComposition: function (imageA, imageB, imageOutput) {
var width, height, image = imageOutput;
if (this._composition) {
width = Math.max(imageA.getWidth(), imageB.getWidth());
height = Math.max(imageA.getHeight(), imageB.getHeight());
if (((width > height) && !this._composeLeftToRight) || this._composeTopToBottom) {
image = PNGImage.createImage(width, height * 3);
imageA.getImage().bitblt(image.getImage(), 0, 0, imageA.getWidth(), imageA.getHeight(), 0, 0);
imageOutput.getImage().bitblt(image.getImage(), 0, 0, imageOutput.getWidth(), imageOutput.getHeight(), 0, height);
imageB.getImage().bitblt(image.getImage(), 0, 0, imageB.getWidth(), imageB.getHeight(), 0, height * 2);
} else {
image = PNGImage.createImage(width * 3, height);
imageA.getImage().bitblt(image.getImage(), 0, 0, imageA.getWidth(), imageA.getHeight(), 0, 0);
imageOutput.getImage().bitblt(image.getImage(), 0, 0, imageOutput.getWidth(), imageOutput.getHeight(), width, 0);
imageB.getImage().bitblt(image.getImage(), 0, 0, imageB.getWidth(), imageB.getHeight(), width * 2, 0);
}
}
return image;
},
/**
* Loads the image or uses the already available image
*
* @method _loadImageSync
* @param {string} path
* @param {PNGImage} image
* @return {PNGImage}
* @private
*/
_loadImageSync: function (path, image) {
if (image instanceof Buffer) {
return PNGImage.loadImageSync(image);
} else if ((typeof path === 'string') && !image) {
return PNGImage.readImageSync(path);
} else {
return image;
}
},
/**
* Loads the image or uses the already available image
*
* @method _loadImage
* @param {string} path
* @param {PNGImage} image
* @return {PNGImage|Promise}
* @private
*/
_loadImage: function (path, image) {
if (image instanceof Buffer) {
return Promise.denodeify(PNGImage.loadImage).call(PNGImage, image);
} else if ((typeof path === 'string') && !image) {
return Promise.denodeify(PNGImage.readImage).call(PNGImage, path);
} else {
return image;
}
},
/**
* Copies one image into another image
*
* @method _copyImage
* @param {PNGImage} imageSrc
* @param {PNGImage} imageDst
* @private
*/
_copyImage: function (imageSrc, imageDst) {
imageSrc.getImage().bitblt(imageDst.getImage(), 0, 0, imageSrc.getWidth(), imageSrc.getHeight(), 0, 0);
},
/**
* Is the difference above the set threshold?
*
* @method isAboveThreshold
* @param {int} items
* @param {int} [total]
* @return {boolean}
*/
isAboveThreshold: function (items, total) {
if ((this._thresholdType === BlinkDiff.THRESHOLD_PIXEL) && (this._threshold <= items)) {
return true;
} else if (this._threshold <= (items / total)) {
return true;
}
return false;
},
/**
* Log method that can be overwritten to modify the logging behavior.
*
* @method log
* @param {string} text
*/
log: function (text) {
// Nothing here; Overwrite this to add some functionality
},
/**
* Has comparison passed?
*
* @method hasPassed
* @param {int} result Comparison result-code
* @return {boolean}
*/
hasPassed: function (result) {
return ((result !== BlinkDiff.RESULT_DIFFERENT) && (result !== BlinkDiff.RESULT_UNKNOWN));
},
/**
* Clips the images to the lower resolution of both
*
* @private
* @method _clip
* @param {PNGImage} imageA Source image
* @param {PNGImage} imageB Destination image
*/
_clip: function (imageA, imageB) {
var minWidth, minHeight;
if ((imageA.getWidth() != imageB.getWidth()) || (imageA.getHeight() != imageB.getHeight())) {
minWidth = imageA.getWidth();
if (imageB.getWidth() < minWidth) {
minWidth = imageB.getWidth();
}
minHeight = imageA.getHeight();
if (imageB.getHeight() < minHeight) {
minHeight = imageB.getHeight();
}
this.log("Clipping to " + minWidth + " x " + minHeight);
imageA.clip(0, 0, minWidth, minHeight);
imageB.clip(0, 0, minWidth, minHeight);
}
},
/**
* Crops the source image to the bounds of rect
*
* @method _crop
* @param {string} which Title of image to crop
* @param {PNGImage} image Source image
* @param {object} rect Values for rect
* @param {int} rect.x X value of rect
* @param {int} rect.y Y value of rect
* @param {int} rect.width Width value of rect
* @param {int} rect.height Height value of rect
* @private
*/
_crop: function (which, image, rect) {
this.log("Cropping " + which + " from " + rect.x + "," + rect.y + " by " + rect.width + " x " + rect.height);
image.clip(rect.x, rect.y, rect.width, rect.height);
},
/**
* Correcting area dimensions if necessary
*
* Note:
* Priority is on the x/y coordinates, and not on the size since the size will then be removed anyways.
*
* @method _correctDimensions
* @param {int} width
* @param {int} height
* @param {object} rect Values for rect
* @param {int} rect.x X value of rect
* @param {int} rect.y Y value of rect
* @param {int} rect.width Width value of rect
* @param {int} rect.height Height value of rect
* @private
*/
_correctDimensions: function (width, height, rect) {
// Set values if none given
rect.x = rect.x || 0;
rect.y = rect.y || 0;
rect.width = rect.width || width;
rect.height = rect.height || height;
// Check negative values
rect.x = Math.max(0, rect.x);
rect.y = Math.max(0, rect.y);
rect.width = Math.max(0, rect.width);
rect.height = Math.max(0, rect.height);
// Check dimensions
rect.x = Math.min(rect.x, width - 1); // -1 to make sure that there is an image
rect.y = Math.min(rect.y, height - 1);
rect.width = Math.min(rect.width, width - rect.x);
rect.height = Math.min(rect.height, height - rect.y);
},
/**
* Calculates the distance of colors in the 4 dimensional color space
*
* @method _colorDelta
* @param {object} color1 Values for color 1
* @param {int} color1.c1 First value of color 1
* @param {int} color1.c2 Second value of color 1
* @param {int} color1.c3 Third value of color 1
* @param {int} color1.c4 Fourth value of color 1
* @param {object} color2 Values for color 2
* @param {int} color2.c1 First value of color 2
* @param {int} color2.c2 Second value of color 2
* @param {int} color2.c3 Third value of color 2
* @param {int} color2.c4 Fourth value of color 2
* @return {number} Distance
* @private
*/
_colorDelta: function (color1, color2) {
var c1, c2, c3, c4;
c1 = Math.pow(color1.c1 - color2.c1, 2);
c2 = Math.pow(color1.c2 - color2.c2, 2);
c3 = Math.pow(color1.c3 - color2.c3, 2);
c4 = Math.pow(color1.c4 - color2.c4, 2);
return Math.sqrt(c1 + c2 + c3 + c4);
},
/**
* Gets the color of an image by the index
*
* @method _getColor
* @param {PNGImage} image Image
* @param {int} idx Index of pixel in image
* @param {boolean} [perceptual=false]
* @param {object} [gamma]
* @return {object} Color
* @private
*/
_getColor: function (image, idx, perceptual, gamma) {
var color;
color = {
c1: image.getRed(idx), c2: image.getGreen(idx), c3: image.getBlue(idx), c4: image.getAlpha(idx)
};
if (perceptual || gamma) {
color = this._correctGamma(color, gamma);
color = this._convertRgbToXyz(color);
color = this._convertXyzToCieLab(color);
}
return color;
},
/**
* Correct gamma and return color in [0, 1] range
*
* @method _correctGamma
* @param {object} color
* @param {object} [gamma]
* @return {{c1: number, c2: number, c3: number, c4: number}}
* @private
*/
_correctGamma: function (color, gamma) {
// Convert to range [0, 1]
var result = {
c1: color.c1 / 255, c2: color.c2 / 255, c3: color.c3 / 255, c4: color.c4
};
if (gamma || gamma.R !== undefined || gamma.G !== undefined || gamma.B !== undefined) {
if (gamma.R !== undefined) {
result.c1 = Math.pow(result.c1, gamma.R);
}
if (gamma.G !== undefined) {
result.c2 = Math.pow(result.c2, gamma.G);
}
if (gamma.B !== undefined) {
result.c3 = Math.pow(result.c3, gamma.B);
}
}
return result;
},
/**
* Converts the color from RGB to XYZ
*
* @method _convertRgbToXyz
* @param {object} color
* @return {object}
* @private
*/
_convertRgbToXyz: function (color) {
var result = {};
result.c1 = color.c1 * 0.4887180 + color.c2 * 0.3106803 + color.c3 * 0.2006017;
result.c2 = color.c1 * 0.1762044 + color.c2 * 0.8129847 + color.c3 * 0.0108109;
result.c3 = color.c2 * 0.0102048 + color.c3 * 0.9897952;
result.c4 = color.c4;
return result;
},
/**
* Converts the color from XYZ to CieLab
*
* @method _convertXyzToCieLab
* @param {object} color
* @return {object}
* @private
*/
_convertXyzToCieLab: function (color) {
var result = {}, c1, c2, c3;
function f (t) {
return (t > 0.00885645167904) ? Math.pow(t, 1 / 3) : 70.08333333333263 * t + 0.13793103448276;
}
c1 = f(color.c1 / this._refWhite.c1);
c2 = f(color.c2 / this._refWhite.c2);
c3 = f(color.c3 / this._refWhite.c3);
result.c1 = (116 * c2) - 16;
result.c2 = 500 * (c1 - c2);
result.c3 = 200 * (c2 - c3);
result.c4 = color.c4;
return result;
},
/**
* Calculates the lower limit
*
* @method _calculateLowerLimit
* @param {int} value
* @param {int} min
* @param {int} shift