Browse Source

Merge pull request #89 from flowtsohg/master

Less heap allocations
pull/96/head
James Cryer 8 years ago committed by GitHub
parent
commit
a7bf6226a8
  1. 109
      resemble.js

109
resemble.js

@ -23,47 +23,42 @@ URL: https://github.com/Huddle/Resemble.js
alpha: 255 alpha: 255
}; };
var targetPix = {r: 0, g: 0, b: 0, a: 0}; // isAntialiased
function colorsDistance(c1, c2){ function colorsDistance(c1, c2){
return (Math.abs(c1.r - c2.r) + Math.abs(c1.g - c2.g) + Math.abs(c1.b - c2.b))/3; return (Math.abs(c1.r - c2.r) + Math.abs(c1.g - c2.g) + Math.abs(c1.b - c2.b))/3;
} }
var errorPixelTransform = { var errorPixelTransform = {
flat : function (d1, d2){ flat: function (px, offset, d1, d2) {
return { px[offset] = errorPixelColor.red;
r: errorPixelColor.red, px[offset + 1] = errorPixelColor.green;
g: errorPixelColor.green, px[offset + 2] = errorPixelColor.blue;
b: errorPixelColor.blue, px[offset + 3] = errorPixelColor.alpha;
a: errorPixelColor.alpha
}
}, },
movement: function (d1, d2){ movement: function (px, offset, d1, d2) {
return { px[offset] = ((d2.r * (errorPixelColor.red / 255)) + errorPixelColor.red) / 2;
r: ((d2.r*(errorPixelColor.red/255)) + errorPixelColor.red)/2, px[offset + 1] = ((d2.g * (errorPixelColor.green / 255)) + errorPixelColor.green) / 2;
g: ((d2.g*(errorPixelColor.green/255)) + errorPixelColor.green)/2, px[offset + 2] = ((d2.b * (errorPixelColor.blue / 255)) + errorPixelColor.blue) / 2;
b: ((d2.b*(errorPixelColor.blue/255)) + errorPixelColor.blue)/2, px[offset + 3] = d2.a;
a: d2.a
}
}, },
flatDifferenceIntensity: function (d1, d2){ flatDifferenceIntensity: function (px, offset, d1, d2) {
return { px[offset] = errorPixelColor.red;
r: errorPixelColor.red, px[offset + 1] = errorPixelColor.green;
g: errorPixelColor.green, px[offset + 2] = errorPixelColor.blue;
b: errorPixelColor.blue, px[offset + 3] = colorsDistance(d1, d2);
a: colorsDistance(d1, d2)
}
}, },
movementDifferenceIntensity: function (d1, d2){ movementDifferenceIntensity: function (px, offset, d1, d2) {
var ratio = colorsDistance(d1, d2) / 255 * 0.8; var ratio = colorsDistance(d1, d2) / 255 * 0.8;
return {
r: ((1-ratio)*(d2.r*(errorPixelColor.red/255)) + ratio*errorPixelColor.red), px[offset] = ((1 - ratio) * (d2.r * (errorPixelColor.red / 255)) + ratio * errorPixelColor.red);
g: ((1-ratio)*(d2.g*(errorPixelColor.green/255)) + ratio*errorPixelColor.green), px[offset + 1] = ((1 - ratio) * (d2.g * (errorPixelColor.green / 255)) + ratio * errorPixelColor.green);
b: ((1-ratio)*(d2.b*(errorPixelColor.blue/255)) + ratio*errorPixelColor.blue), px[offset + 2] = ((1 - ratio) * (d2.b * (errorPixelColor.blue / 255)) + ratio * errorPixelColor.blue);
a: d2.a px[offset + 3] = d2.a;
}
} }
}; };
var errorPixelTransformer = errorPixelTransform.flat; var errorPixel = errorPixelTransform.flat;
var largeImageThreshold = 1200; var largeImageThreshold = 1200;
var useCrossOrigin = true; var useCrossOrigin = true;
var document = typeof window != "undefined" ? window.document : {}; var document = typeof window != "undefined" ? window.document : {};
@ -118,7 +113,7 @@ URL: https://github.com/Huddle/Resemble.js
var whiteTotal = 0; var whiteTotal = 0;
var blackTotal = 0; var blackTotal = 0;
loop(height, width, function(verticalPos, horizontalPos){ loop(width, height, function(horizontalPos, verticalPos){
var offset = (verticalPos*width + horizontalPos) * 4; var offset = (verticalPos*width + horizontalPos) * 4;
var red = sourceImageData[offset]; var red = sourceImageData[offset];
var green = sourceImageData[offset + 1]; var green = sourceImageData[offset + 1];
@ -292,7 +287,6 @@ URL: https://github.com/Huddle/Resemble.js
function isAntialiased(sourcePix, data, cacheSet, verticalPos, horizontalPos, width){ function isAntialiased(sourcePix, data, cacheSet, verticalPos, horizontalPos, width){
var offset; var offset;
var targetPix;
var distance = 1; var distance = 1;
var i; var i;
var j; var j;
@ -310,9 +304,8 @@ URL: https://github.com/Huddle/Resemble.js
} else { } else {
offset = ((verticalPos+j)*width + (horizontalPos+i)) * 4; offset = ((verticalPos+j)*width + (horizontalPos+i)) * 4;
targetPix = getPixelInfo(data, offset, cacheSet);
if(targetPix === null){ if(!getPixelInfo(targetPix , data, offset, cacheSet)){
continue; continue;
} }
@ -345,14 +338,6 @@ URL: https://github.com/Huddle/Resemble.js
return false; return false;
} }
function errorPixel(px, offset, data1, data2){
var data = errorPixelTransformer(data1, data2);
px[offset] = data.r;
px[offset + 1] = data.g;
px[offset + 2] = data.b;
px[offset + 3] = data.a;
}
function copyPixel(px, offset, data){ function copyPixel(px, offset, data){
px[offset] = data.r; //r px[offset] = data.r; //r
px[offset + 1] = data.g; //g px[offset + 1] = data.g; //g
@ -367,30 +352,17 @@ URL: https://github.com/Huddle/Resemble.js
px[offset + 3] = data.a * pixelTransparency; //a px[offset + 3] = data.a * pixelTransparency; //a
} }
function getPixelInfo(data, offset, cacheSet){ function getPixelInfo(dst, data, offset, cacheSet) {
var r; if (data.length > offset) {
var g; dst.r = data[offset];
var b; dst.g = data[offset + 1];
var d; dst.b = data[offset + 2];
var a; dst.a = data[offset + 3];
r = data[offset];
if(typeof r !== 'undefined'){
g = data[offset+1];
b = data[offset+2];
a = data[offset+3];
d = {
r: r,
g: g,
b: b,
a: a
};
return d; return true;
} else {
return null;
} }
return false;
} }
function addBrightnessInfo(data){ function addBrightnessInfo(data){
@ -437,7 +409,10 @@ URL: https://github.com/Huddle/Resemble.js
skip = 6; skip = 6;
} }
loop(height, width, function(verticalPos, horizontalPos){ var pixel1 = {r: 0, g: 0, b: 0, a: 0};
var pixel2 = { r: 0, g: 0, b: 0, a: 0 };
loop(width, height, function(horizontalPos, verticalPos){
if(skip){ // only skip if the image isn't small if(skip){ // only skip if the image isn't small
if(verticalPos % skip === 0 || horizontalPos % skip === 0){ if(verticalPos % skip === 0 || horizontalPos % skip === 0){
@ -446,10 +421,8 @@ URL: https://github.com/Huddle/Resemble.js
} }
var offset = (verticalPos*width + horizontalPos) * 4; var offset = (verticalPos*width + horizontalPos) * 4;
var pixel1 = getPixelInfo(data1, offset, 1);
var pixel2 = getPixelInfo(data2, offset, 2);
if(pixel1 === null || pixel2 === null){ if (!getPixelInfo(pixel1, data1, offset, 1) || !getPixelInfo(pixel2, data2, offset, 2)) {
return; return;
} }
@ -714,7 +687,7 @@ URL: https://github.com/Huddle/Resemble.js
} }
if(options.errorType && errorPixelTransform[options.errorType] ){ if(options.errorType && errorPixelTransform[options.errorType] ){
errorPixelTransformer = errorPixelTransform[options.errorType]; errorPixel = errorPixelTransform[options.errorType];
} }
pixelTransparency = isNaN(Number(options.transparency)) ? pixelTransparency : options.transparency; pixelTransparency = isNaN(Number(options.transparency)) ? pixelTransparency : options.transparency;

Loading…
Cancel
Save