2014-09-20 11:14:21 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace('pskl.worker');
|
|
|
|
|
2014-09-24 09:43:04 +04:00
|
|
|
var imageProcessorWorker = function () {
|
2014-09-23 01:51:28 +04:00
|
|
|
var currentStep, currentProgress, currentTotal;
|
2014-09-24 09:43:04 +04:00
|
|
|
|
2014-09-23 01:51:28 +04:00
|
|
|
var initStepCounter_ = function (total) {
|
|
|
|
currentStep = 0;
|
|
|
|
currentProgress = 0;
|
|
|
|
currentTotal = total;
|
|
|
|
};
|
|
|
|
|
|
|
|
var postStep_ = function () {
|
|
|
|
currentStep = currentStep + 1;
|
|
|
|
var progress = ((currentStep / currentTotal) *100).toFixed(1);
|
|
|
|
if (progress != currentProgress) {
|
|
|
|
currentProgress = progress;
|
|
|
|
this.postMessage({
|
|
|
|
type : 'STEP',
|
|
|
|
progress : currentProgress,
|
|
|
|
currentStep : currentStep,
|
|
|
|
total : currentTotal
|
|
|
|
});
|
|
|
|
}
|
2014-09-20 11:14:21 +04:00
|
|
|
};
|
|
|
|
|
2014-09-21 23:56:22 +04:00
|
|
|
var rgbToHex = function(r, g, b) {
|
|
|
|
return '#' + ((r << 16) | (g << 8) | b).toString(16);
|
|
|
|
};
|
|
|
|
|
2014-09-20 11:14:21 +04:00
|
|
|
var imageDataToGrid = function (imageData, width, height, transparent) {
|
|
|
|
// Draw the zoomed-up pixels to a different canvas context
|
|
|
|
var grid = [];
|
|
|
|
for (var x = 0 ; x < width ; x++){
|
|
|
|
grid[x] = [];
|
2014-09-23 01:51:28 +04:00
|
|
|
postStep_();
|
2014-09-20 11:14:21 +04:00
|
|
|
for (var y = 0 ; y < height ; y++){
|
|
|
|
// Find the starting index in the one-dimensional image data
|
|
|
|
var i = (y * width + x)*4;
|
|
|
|
var r = imageData[i ];
|
|
|
|
var g = imageData[i+1];
|
|
|
|
var b = imageData[i+2];
|
|
|
|
var a = imageData[i+3];
|
|
|
|
if (a < 125) {
|
|
|
|
grid[x][y] = transparent;
|
|
|
|
} else {
|
2014-09-21 23:56:22 +04:00
|
|
|
grid[x][y] = rgbToHex(r,g,b);
|
2014-09-20 11:14:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return grid;
|
|
|
|
};
|
|
|
|
|
|
|
|
var getColorsMapFromImageData = function (imageData, width, height) {
|
|
|
|
var grid = imageDataToGrid(imageData, width, height, 'transparent');
|
|
|
|
|
|
|
|
var colorsMap = {};
|
|
|
|
for (var i = 0 ; i < grid.length ; i++) {
|
2014-09-23 01:51:28 +04:00
|
|
|
postStep_();
|
2014-09-20 11:14:21 +04:00
|
|
|
for (var j = 0 ; j < grid[i].length ; j++) {
|
|
|
|
var color = grid[i][j];
|
|
|
|
if (color != 'transparent') {
|
|
|
|
colorsMap[color] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return colorsMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onmessage = function(event) {
|
2014-09-23 01:51:28 +04:00
|
|
|
try {
|
|
|
|
var data = event.data;
|
|
|
|
|
|
|
|
initStepCounter_(data.width * 2);
|
|
|
|
|
|
|
|
var colorsMap = getColorsMapFromImageData(data.imageData, data.width, data.height);
|
|
|
|
|
|
|
|
this.postMessage({
|
|
|
|
type : 'SUCCESS',
|
|
|
|
colorsMap : colorsMap
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
this.postMessage({
|
|
|
|
type : 'ERROR',
|
|
|
|
message : e.message
|
|
|
|
});
|
2014-09-20 11:14:21 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.ImageProcessor = function (image, onSuccess, onStep, onError) {
|
|
|
|
this.image = image;
|
|
|
|
|
|
|
|
this.onStep = onStep;
|
|
|
|
this.onSuccess = onSuccess;
|
|
|
|
this.onError = onError;
|
|
|
|
|
2014-09-24 09:43:04 +04:00
|
|
|
// var worker = pskl.utils.WorkerUtils.addPartialWorker(imageProcessorWorker, 'step-counter');
|
|
|
|
this.worker = pskl.utils.WorkerUtils.createWorker(worker, 'image-colors-processor');
|
2014-09-20 11:14:21 +04:00
|
|
|
this.worker.onmessage = this.onWorkerMessage.bind(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.ImageProcessor.prototype.process = function () {
|
2014-09-21 23:56:22 +04:00
|
|
|
var canvas = pskl.utils.CanvasUtils.createFromImage(this.image);
|
|
|
|
var imageData = pskl.utils.CanvasUtils.getImageDataFromCanvas(canvas);
|
2014-09-20 11:14:21 +04:00
|
|
|
this.worker.postMessage({
|
|
|
|
imageData : imageData,
|
|
|
|
width : this.image.width,
|
|
|
|
height : this.image.height
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.ImageProcessor.prototype.createNamespace = function (name) {
|
|
|
|
var createNamespace = (function () {
|
|
|
|
var parts = name.split('.');
|
|
|
|
if (parts.length > 0) {
|
|
|
|
var node = this;
|
|
|
|
for (var i = 0 ; i < parts.length ; i++) {
|
|
|
|
if (!node[parts[i]]) {
|
|
|
|
node[parts[i]] = {};
|
|
|
|
}
|
|
|
|
node = node[parts[i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var script = createNamespace + "";
|
|
|
|
script = script.replace(/function \(\) \{/,"").replace(/\}[^}]*$/, "");
|
|
|
|
script = "var name = '" + name + "';" + script;
|
|
|
|
|
|
|
|
this.runScript(script);
|
|
|
|
};
|
|
|
|
|
2014-09-21 23:56:22 +04:00
|
|
|
ns.ImageProcessor.prototype.onWorkerMessage = function (event) {
|
|
|
|
if (event.data.type === 'STEP') {
|
|
|
|
this.onStep(event);
|
|
|
|
} else if (event.data.type === 'SUCCESS') {
|
|
|
|
this.onSuccess(event);
|
|
|
|
this.worker.terminate();
|
|
|
|
} else if (event.data.type === 'ERROR') {
|
|
|
|
this.onError(event);
|
|
|
|
this.worker.terminate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.ImageProcessor.prototype.importAll__ = function (classToImport, classpath) {
|
|
|
|
this.createNamespace(classpath);
|
2014-09-20 11:14:21 +04:00
|
|
|
for (var key in classToImport) {
|
|
|
|
if (classToImport.hasOwnProperty(key)) {
|
|
|
|
this.addMethod(classToImport[key], classpath + '.' + key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-21 23:56:22 +04:00
|
|
|
ns.ImageProcessor.prototype.addMethod__ = function (method, name) {
|
2014-09-20 11:14:21 +04:00
|
|
|
this.runScript(name + "=" + method);
|
|
|
|
};
|
|
|
|
|
2014-09-21 23:56:22 +04:00
|
|
|
ns.ImageProcessor.prototype.runScript__ = function (script) {
|
2014-09-20 11:14:21 +04:00
|
|
|
this.worker.postMessage({
|
|
|
|
type : 'RUN_SCRIPT',
|
|
|
|
script : this.getScriptAsUrl(script)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-09-21 23:56:22 +04:00
|
|
|
ns.ImageProcessor.prototype.getScriptAsUrl__ = function (script) {
|
2014-09-20 11:14:21 +04:00
|
|
|
var blob = new Blob([script], {type: "application/javascript"}); // pass a useful mime type here
|
|
|
|
return window.URL.createObjectURL(blob);
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
|