tonic-image-builder
Advanced tools
Comparing version
@@ -9,2 +9,3 @@ var CanvasOffscreenBuffer = require('../../util/CanvasOffscreenBuffer'), | ||
this.data = {}; | ||
this.listeners = {}; | ||
this.dataSize = [200, 200]; | ||
@@ -42,8 +43,15 @@ this.operation = operation; | ||
PixelOperationImageBuilder.prototype.updateOperationFunction = function() { | ||
var functionBody = []; | ||
var functionBody = [], | ||
isValid = true; | ||
for(var key in this.data) { | ||
functionBody.push('var X = data.X[i];'.replace(/X/g, key)); | ||
} | ||
this.dependency.forEach((dep) => { | ||
isValid = this.data[dep] && isValid | ||
}); | ||
functionBody.push('return X;'.replace(/X/g, this.operation)); | ||
this.fnOperation = new Function('data', 'i', functionBody.join('')); | ||
return isValid; | ||
} | ||
@@ -89,4 +97,70 @@ | ||
PixelOperationImageBuilder.prototype.updateDataFromImage = function(name, image) { | ||
var registerImage = () => { | ||
// Remove callback if any | ||
image.removeEventListener('load', registerImage); | ||
// Extract image data | ||
var width = image.width, | ||
height = image.height, | ||
ctx = this.bgCanvas.get2DContext(), | ||
extractedData = new Uint8ClampedArray(width*height*4), | ||
pixelBuffer = null; | ||
this.bgCanvas.size(width, height); | ||
ctx.drawImage(image, 0, 0); | ||
pixelBuffer = ctx.getImageData(0, 0, width, height); | ||
extractedData.set(pixelBuffer.data); | ||
// Store the given array | ||
this.data[name] = extractedData; | ||
this.dataSize = [width, height]; | ||
// Is dependency meet? | ||
var canProcess = true; | ||
this.dependency.forEach((name) => { | ||
if(!this.data[name]) { | ||
canProcess = false; | ||
} | ||
}) | ||
if(canProcess) { | ||
this.processData(); | ||
} | ||
} | ||
if(image.complete) { | ||
registerImage(); | ||
} else { | ||
image.addEventListener('load', registerImage); | ||
} | ||
} | ||
// ---------------------------------------------------------------------------- | ||
PixelOperationImageBuilder.prototype.updateDataFromClampedArray = function(name, array, size) { | ||
// Store the given array | ||
this.data[name] = array; | ||
this.dataSize = size || this.dataSize; | ||
// Is dependency meet? | ||
var canProcess = true; | ||
this.dependency.forEach((name) => { | ||
if(!this.data[name]) { | ||
canProcess = false; | ||
} | ||
}) | ||
if(canProcess) { | ||
this.processData(); | ||
} | ||
} | ||
// ---------------------------------------------------------------------------- | ||
PixelOperationImageBuilder.prototype.processData = function() { | ||
this.updateOperationFunction(); | ||
if(!this.updateOperationFunction()) { | ||
// We are not ready yet | ||
return; | ||
} | ||
@@ -105,7 +179,12 @@ // Validate Array sizes | ||
if(!sizeValid) { | ||
console.log('The array size are invalid!!!'); | ||
if(!sizeValid || size === -1) { | ||
console.log('The array size are invalid!!!', size); | ||
return; | ||
} | ||
if(this.dataSize[0]*this.dataSize[1]*4 !== size) { | ||
console.log('The array size are invalid!!!', size, this.dataSize); | ||
return; | ||
} | ||
// Evaluate pixel operation | ||
@@ -157,6 +236,12 @@ var idx = 0, | ||
PixelOperationImageBuilder.prototype.getListeners = function() { | ||
return {}; | ||
}; | ||
return this.listeners; | ||
} | ||
// ---------------------------------------------------------------------------- | ||
PixelOperationImageBuilder.prototype.setListeners = function(l) { | ||
this.listeners = l; | ||
} | ||
// ---------------------------------------------------------------------------- | ||
// Method meant to be used with the WidgetFactory | ||
@@ -163,0 +248,0 @@ |
@@ -12,2 +12,15 @@ var Monologue = require('monologue.js'), | ||
// Handle default pipeline if any | ||
var pipelineQuery = jsonData.CompositePipeline.default_pipeline; | ||
function isLayerVisible(layers) { | ||
if(!pipelineQuery || layers.length > 1) { | ||
return true; | ||
} | ||
var layerCode = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', | ||
layerIdx = layerCode.indexOf(layers[0]); | ||
return (pipelineQuery[layerIdx*2+1] !== '_'); | ||
} | ||
// Fill visibility and activate all layers | ||
@@ -18,3 +31,3 @@ jsonData.CompositePipeline.pipeline.forEach(item => { | ||
jsonData.CompositePipeline.layers.forEach(item => { | ||
this.activeState[item] = true; | ||
this.activeState[item] = isLayerVisible(item); | ||
this.activeColors[item] = jsonData.CompositePipeline.layer_fields[item][0]; | ||
@@ -21,0 +34,0 @@ }); |
{ | ||
"name": "tonic-image-builder", | ||
"description": "JavaScript library used to gather image generation based on various input data.", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"license": "BSD-3-Clause", | ||
@@ -6,0 +6,0 @@ "main": "./src/index.js", |
Sorry, the diff of this file is too big to display
2190266
0.38%148
1.37%10043
1.52%