js/src/parjs-benchmarks/edges.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 load(libdir + "util.js");
michael@0 2
michael@0 3 var SobelX = [[-1.0, 0.0, 1.0],
michael@0 4 [-2.0, 0.0, 2.0],
michael@0 5 [-1.0, 0.0, 1.0]];
michael@0 6 var SobelY = [[ 1.0, 2.0, 1.0],
michael@0 7 [ 0.0, 0.0, 0.0],
michael@0 8 [-1.0, -2.0, -1.0]];
michael@0 9
michael@0 10 function stripedImage(w, h) {
michael@0 11 var resultArray = new Array(w * h);
michael@0 12 for (var y = 0; y < h; y++) {
michael@0 13 for (var x = 0; x < w; x++) {
michael@0 14 resultArray[y*w + x] = (Math.abs(x%100-y%100) < 10) ? 32 : 0;
michael@0 15 }
michael@0 16 }
michael@0 17 return resultArray;
michael@0 18 }
michael@0 19
michael@0 20 function edgesSequentially(data, width, height) {
michael@0 21 var data1 = new Array(width * height);
michael@0 22 for (var y = 0; y < height; y++) {
michael@0 23 for (var x = 0; x < width; x++) {
michael@0 24 var total = compute(x, y);
michael@0 25 data1[y*width + x] = total | 0;
michael@0 26 }
michael@0 27 }
michael@0 28 return data1;
michael@0 29
michael@0 30 function compute(x, y) {
michael@0 31 var totalX = 0;
michael@0 32 var totalY = 0;
michael@0 33
michael@0 34 var offYMin = (y == 0 ? 0 : -1);
michael@0 35 var offYMax = (y == height - 1 ? 0 : 1);
michael@0 36 var offXMin = (x == 0 ? 0 : -1);
michael@0 37 var offXMax = (x == width - 1 ? 0 : 1);
michael@0 38
michael@0 39 for (var offY = offYMin; offY <= offYMax; offY++) {
michael@0 40 for (var offX = offXMin; offX <= offXMax; offX++) {
michael@0 41 var e = data[(y + offY) * width + x + offX];
michael@0 42 totalX += e * SobelX[offY + 1][offX + 1];
michael@0 43 totalY += e * SobelY[offY + 1][offX + 1];
michael@0 44 }
michael@0 45 }
michael@0 46
michael@0 47 return (Math.abs(totalX) + Math.abs(totalY))/8.0 | 0;
michael@0 48 }
michael@0 49 }
michael@0 50
michael@0 51 function edgesParallel(data) {
michael@0 52 var pa = new ParallelArray([Height, Width], function (y, x) {
michael@0 53 var totalX = 0;
michael@0 54 var totalY = 0;
michael@0 55
michael@0 56 var offYMin = (y == 0 ? 0 : -1);
michael@0 57 var offYMax = (y == Height - 1 ? 0 : 1);
michael@0 58 var offXMin = (x == 0 ? 0 : -1);
michael@0 59 var offXMax = (x == Width - 1 ? 0 : 1);
michael@0 60
michael@0 61 for (var offY = offYMin; offY <= offYMax; offY++) {
michael@0 62 for (var offX = offXMin; offX <= offXMax; offX++) {
michael@0 63 var e = data.get(y + offY, x + offX);
michael@0 64 totalX += e * SobelX[offY + 1][offX + 1];
michael@0 65 totalY += e * SobelY[offY + 1][offX + 1];
michael@0 66 }
michael@0 67 }
michael@0 68
michael@0 69 return (Math.abs(totalX) + Math.abs(totalY))/8.0 | 0;
michael@0 70 });
michael@0 71 return pa.flatten();
michael@0 72 }
michael@0 73
michael@0 74 var Width = 1024;
michael@0 75 var Height = 768;
michael@0 76 var ArrInput = stripedImage(Width, Height);
michael@0 77 var ParArrInput2D = new ParallelArray([Height, Width],
michael@0 78 function (y, x) ArrInput[y*Width + x]);
michael@0 79
michael@0 80 benchmark("EDGES", 2, DEFAULT_MEASURE * 20,
michael@0 81 function() edgesSequentially(ArrInput, Width, Height),
michael@0 82 function() edgesParallel(ParArrInput2D));

mercurial