Skip to content

Commit a64007d

Browse files
committed
Benchmark for performance tests
1 parent 0f86e4c commit a64007d

18 files changed

Lines changed: 367 additions & 18 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
bower_components
33
node_modules
4+
test/assets/benchmark.json

build/tracking-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/tracking.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -669,21 +669,27 @@
669669
* @param {pixels} pixels The pixels in a linear [r,g,b,a,...] array.
670670
* @param {number} width The image width.
671671
* @param {number} height The image height.
672-
* @param {Uint8ClampedArray} The grayscale pixels in a linear [p,p,p,a,...]
673-
* array.
672+
* @param {boolean} fillRGBA If the result should fill all RGBA values with the gray scale
673+
* values, instead of returning a single value per pixel.
674+
* @param {Uint8ClampedArray} The grayscale pixels in a linear array ([p,p,p,a,...] if fillRGBA
675+
* is true and [p1, p2, p3, ...] if fillRGBA is false).
674676
* @static
675677
*/
676-
tracking.Image.grayscale = function(pixels, width, height) {
677-
var gray = new Uint8ClampedArray(width * height * 4);
678+
tracking.Image.grayscale = function(pixels, width, height, fillRGBA) {
679+
var gray = new Uint8ClampedArray(fillRGBA ? pixels.length : pixels.length >> 2);
678680
var p = 0;
679681
var w = 0;
680682
for (var i = 0; i < height; i++) {
681683
for (var j = 0; j < width; j++) {
682684
var value = pixels[w] * 0.299 + pixels[w + 1] * 0.587 + pixels[w + 2] * 0.114;
683685
gray[p++] = value;
684-
gray[p++] = value;
685-
gray[p++] = value;
686-
gray[p++] = pixels[w + 3];
686+
687+
if (fillRGBA) {
688+
gray[p++] = value;
689+
gray[p++] = value;
690+
gray[p++] = pixels[w + 3];
691+
}
692+
687693
w += 4;
688694
}
689695
}

examples/brief.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
var imageData1 = context1.getImageData(0, 0, width, height);
2626
var imageData2 = context2.getImageData(0, 0, width, height);
2727

28-
var gray1 = tracking.Image.calculateLumaGrayscale(imageData1.data, width, height);
29-
var gray2 = tracking.Image.calculateLumaGrayscale(imageData2.data, width, height);
28+
var gray1 = tracking.Image.grayscale(imageData1.data, width, height);
29+
var gray2 = tracking.Image.grayscale(imageData2.data, width, height);
3030

3131
var corners1 = tracking.Fast.findCorners(gray1, width, height);
3232
var corners2 = tracking.Fast.findCorners(gray2, width, height);

examples/fast.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
tracking.Canvas.loadImage(canvas, 'assets/book1.png', 0, 0, width, height, function() {
2020
var imageData = context.getImageData(0, 0, width, height);
21-
var gray = tracking.Image.calculateLumaGrayscale(imageData.data, width, height);
21+
var gray = tracking.Image.grayscale(imageData.data, width, height);
2222
var corners = tracking.Fast.findCorners(gray, width, height);
2323

2424
for (var i = 0; i < corners.length; i += 2) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"gulp-uglify": "^0.3.1",
3636
"jshint-stylish": "^0.4.0",
3737
"nodeunit": "0.9.0",
38+
"png-js": "0.1.1",
3839
"run-sequence": "^0.3.6"
3940
}
4041
}

src/utils/Image.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,27 @@
106106
* @param {pixels} pixels The pixels in a linear [r,g,b,a,...] array.
107107
* @param {number} width The image width.
108108
* @param {number} height The image height.
109-
* @param {Uint8ClampedArray} The grayscale pixels in a linear [p,p,p,a,...]
110-
* array.
109+
* @param {boolean} fillRGBA If the result should fill all RGBA values with the gray scale
110+
* values, instead of returning a single value per pixel.
111+
* @param {Uint8ClampedArray} The grayscale pixels in a linear array ([p,p,p,a,...] if fillRGBA
112+
* is true and [p1, p2, p3, ...] if fillRGBA is false).
111113
* @static
112114
*/
113-
tracking.Image.grayscale = function(pixels, width, height) {
114-
var gray = new Uint8ClampedArray(width * height * 4);
115+
tracking.Image.grayscale = function(pixels, width, height, fillRGBA) {
116+
var gray = new Uint8ClampedArray(fillRGBA ? pixels.length : pixels.length >> 2);
115117
var p = 0;
116118
var w = 0;
117119
for (var i = 0; i < height; i++) {
118120
for (var j = 0; j < width; j++) {
119121
var value = pixels[w] * 0.299 + pixels[w + 1] * 0.587 + pixels[w + 2] * 0.114;
120122
gray[p++] = value;
121-
gray[p++] = value;
122-
gray[p++] = value;
123-
gray[p++] = pixels[w + 3];
123+
124+
if (fillRGBA) {
125+
gray[p++] = value;
126+
gray[p++] = value;
127+
gray[p++] = pixels[w + 3];
128+
}
129+
124130
w += 4;
125131
}
126132
}

test/Benchmark.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var Benchmark = require('./utils/benchmark.js');
2+
3+
module.exports = {
4+
setUp: function(done) {
5+
Benchmark.setUpAll(done);
6+
},
7+
8+
testBenchmark: function(test) {
9+
Benchmark.runAll(function(results) {
10+
test.ok(results.passed, Benchmark.createFailureMessage(results.resultDetails));
11+
test.done();
12+
});
13+
}
14+
};

test/assets/box1.png

39.6 KB
Loading

test/assets/box2.png

40.5 KB
Loading

0 commit comments

Comments
 (0)