Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce trashhold #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 67 additions & 67 deletions dimensions.safariextension/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ var dimensionsThreshold = 6;
var debug;
var map;

onmessage = function(event){
switch(event.data.type){
onmessage = function (event) {
switch (event.data.type) {
case 'init':
debug = event.data.debug;
break;
case 'imgData':
imgData = new Uint8ClampedArray(event.data.imgData);
data = grayscale( imgData );
data = grayscale(imgData);
width = event.data.width;
height = event.data.height;
postMessage({ type: "screenshot processed" });
Expand Down Expand Up @@ -38,7 +38,7 @@ onmessage = function(event){
//

function sendDebugScreen() {
postMessage({
postMessage({
type: 'debug screen',
map: map
})
Expand All @@ -52,7 +52,7 @@ function sendDebugScreen() {
// measures the area around pageX and pageY.
//
//
function measureArea(pos){
function measureArea(pos) {
var x0, y0, startLightness;

map = new Int16Array(data);
Expand All @@ -64,63 +64,63 @@ function measureArea(pos){
pixelsInArea = [];

measureAreaStopped = false;

setTimeout(nextTick, 0);
}

function nextTick(){
function nextTick() {
workOffStack();

if(debug)
if (debug)
sendDebugScreen()

if(!measureAreaStopped){
if(stack.length){
if (!measureAreaStopped) {
if (stack.length) {
setTimeout(nextTick, 0);
} else {
finishMeasureArea();
}
}
}

function workOffStack(){
function workOffStack() {
var max = 500000;
var count = 0;

while(count++ < max && stack.length){
while (count++ < max && stack.length) {
floodFill();
}
}

function floodFill(){
function floodFill() {
var xyl = stack.shift();
var x = xyl[0];
var y = xyl[1];
var lastLightness = xyl[2];
var currentLightness = getLightnessAt(map, x, y);

if(currentLightness > -1 && currentLightness < 256 && Math.abs(currentLightness - lastLightness) < areaThreshold){
if (currentLightness > -1 && currentLightness < 256 && Math.abs(currentLightness - lastLightness) < areaThreshold) {
setLightnessAt(map, x, y, 256);
pixelsInArea.push([x,y]);
pixelsInArea.push([x, y]);

if(x < area.left)
if (x < area.left)
area.left = x;
else if(x > area.right)
else if (x > area.right)
area.right = x;
if(y < area.top)
if (y < area.top)
area.top = y;
else if(y > area.bottom)
else if (y > area.bottom)
area.bottom = y;

stack.push([x-1, y , currentLightness]);
stack.push([x , y+1, currentLightness]);
stack.push([x+1, y , currentLightness]);
stack.push([x , y-1, currentLightness]);
stack.push([x - 1, y, currentLightness]);
stack.push([x, y + 1, currentLightness]);
stack.push([x + 1, y, currentLightness]);
stack.push([x, y - 1, currentLightness]);
}
}

function finishMeasureArea(){
var boundariePixels = {
function finishMeasureArea() {
var boundariePixels = {
top: [],
right: [],
bottom: [],
Expand All @@ -132,18 +132,18 @@ function finishMeasureArea(){

// find boundarie-pixels

for(var i=0, l=pixelsInArea.length; i<l; i++){
for (var i = 0, l = pixelsInArea.length; i < l; i++) {
var x = pixelsInArea[i][0];
var y = pixelsInArea[i][1];

if(x === area.left)
if (x === area.left)
boundariePixels.left.push(y);
if(x === area.right)
if (x === area.right)
boundariePixels.right.push(y);

if(y === area.top)
if (y === area.top)
boundariePixels.top.push(x);
if(y === area.bottom)
if (y === area.bottom)
boundariePixels.bottom.push(x);
}

Expand Down Expand Up @@ -171,7 +171,7 @@ function finishMeasureArea(){
}


function getMaxSpread(sideA, sideB){
function getMaxSpread(sideA, sideB) {
var a = getDimensions(sideA);
var b = getDimensions(sideB);

Expand All @@ -181,20 +181,20 @@ function getMaxSpread(sideA, sideB){
return smallerSide.center;
}

function getDimensions(values){
function getDimensions(values) {
var min = Infinity;
var max = 0;

for(var i=0, l=values.length; i<l; i++){
if(values[i] < min)
for (var i = 0, l = values.length; i < l; i++) {
if (values[i] < min)
min = values[i]
if(values[i] > max)
if (values[i] > max)
max = values[i]
}

return {
min: min,
center: min + Math.floor((max - min)/2),
center: min + Math.floor((max - min) / 2),
max: max,
length: max - min
}
Expand All @@ -208,24 +208,24 @@ function getDimensions(values){
// around pageX and pageY.
//

function measureDistances(input){
function measureDistances(input) {
var distances = {
top: 0,
right: 0,
bottom: 0,
left: 0
};
var directions = {
top: { x: 0, y: -1 },
right: { x: 1, y: 0 },
bottom: { x: 0, y: 1 },
left: { x: -1, y: 0 }
top: { x: 0, y: -1 },
right: { x: 1, y: 0 },
bottom: { x: 0, y: 1 },
left: { x: -1, y: 0 }
}
var area = 0;
var startLightness = getLightnessAt(data, input.x, input.y);
var lastLightness;

for(var direction in distances){
for (var direction in distances) {
var vector = directions[direction];
var boundaryFound = false;
var sx = input.x;
Expand All @@ -235,27 +235,27 @@ function measureDistances(input){
// reset lightness to start lightness
lastLightness = startLightness;

while(!boundaryFound){
while (!boundaryFound) {
sx += vector.x;
sy += vector.y;
currentLightness = getLightnessAt(data, sx, sy);

if(currentLightness > -1 && Math.abs(currentLightness - lastLightness) < dimensionsThreshold){
if (currentLightness > -1 && Math.abs(currentLightness - lastLightness) < dimensionsThreshold) {
distances[direction]++;
lastLightness = currentLightness;
} else {
boundaryFound = true;
}
}

area += distances[direction];
}

if(area <= 6){
if (area <= 6) {
distances = { top: 0, right: 0, bottom: 0, left: 0 };
var similarColorStreakThreshold = 8;

for(var direction in distances){
for (var direction in distances) {
var vector = directions[direction];
var boundaryFound = false;
var sx = input.x;
Expand All @@ -265,18 +265,18 @@ function measureDistances(input){

lastLightness = startLightness;

while(!boundaryFound){
while (!boundaryFound) {
sx += vector.x;
sy += vector.y;
currentLightness = getLightnessAt(data, sx, sy);

if(currentLightness > -1){
if (currentLightness > -1) {
distances[direction]++;

if(Math.abs(currentLightness - lastLightness) < dimensionsThreshold){
if (Math.abs(currentLightness - lastLightness) < dimensionsThreshold) {
similarColorStreak++;
if(similarColorStreak === similarColorStreakThreshold){
distances[direction] -= (similarColorStreakThreshold+1);
if (similarColorStreak === similarColorStreakThreshold) {
distances[direction] -= (similarColorStreakThreshold + 1);
boundaryFound = true;
}
} else {
Expand All @@ -300,20 +300,20 @@ function measureDistances(input){
});
}

function getColorAt(x, y){
if(!inBoundaries(x, y))
function getColorAt(x, y) {
if (!inBoundaries(x, y))
return -1;

var i = y * width * 4 + x * 4;

return rgbToHsl(imgData[i], imgData[++i], imgData[++i]);
}

function getLightnessAt(data, x, y){
function getLightnessAt(data, x, y) {
return inBoundaries(x, y) ? data[y * width + x] : -1;
}

function setLightnessAt(data, x, y, value){
function setLightnessAt(data, x, y, value) {
return inBoundaries(x, y) ? data[y * width + x] = value : -1;
}

Expand All @@ -324,8 +324,8 @@ function setLightnessAt(data, x, y, value){
//
// checks if x and y are in the canvas boundaries
//
function inBoundaries(x, y){
if(x >= 0 && x < width && y >= 0 && y < height)
function inBoundaries(x, y) {
if (x >= 0 && x < width && y >= 0 && y < height)
return true;
else
return false;
Expand All @@ -338,13 +338,13 @@ function inBoundaries(x, y){
// reduces the input image data to an array of gray shades.
//

function grayscale(imgData){
var gray = new Int16Array(imgData.length/4);
function grayscale(imgData) {
var gray = new Int16Array(imgData.length / 4);

for(var i=0, n=0, l=imgData.length; i<l; i+=4, n++){
for (var i = 0, n = 0, l = imgData.length; i < l; i += 4, n++) {
var r = imgData[i],
g = imgData[i+1],
b = imgData[i+2];
g = imgData[i + 1],
b = imgData[i + 2];

// weighted grayscale algorithm
gray[n] = Math.round(r * 0.3 + g * 0.59 + b * 0.11);
Expand All @@ -364,17 +364,17 @@ function grayscale(imgData){
* @param Number b The blue color value
* @return Array The HSL representation
*/
function rgbToHsl(r, g, b){
function rgbToHsl(r, g, b) {
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b), min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;

if(max == min){
if (max == min) {
h = s = 0; // achromatic
}else{
} else {
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
Expand Down
Loading