I have a simple Pixel
pseudoclass that has .H()
, .S()
and .L()
methods. I use these in image analysis - like this watermark detection:
To get the objects out of ImageData, I just loop through the ImageData and create multi-dimensional array:
//Get image data
var id = ctx.getImageData(0, 0, canvas1.width, canvas1.height);
//The Uint8 array
var pixels = id.data;
//Dimensions
var length = pixels.length;
var width = id.width;
var height = id.height;
//Create empty array for 2D data
var pixels2D = [];
//Loop vertically
for (var y = 0; y < height; y++) {
//Create 2nd level array
pixels2D.push([]);
//Cache the vertical offset
var ofy = y * width;
//Loop horizontally
for (var x = 0; x < width; x++) {
//Calculate X offset
var off = (ofy + x) * 4;
//Create new object
pixels2D[y].push(new Pixel([pixels[off], pixels[off + 1], pixels[off + 2], pixels[off + 3]]));
}
}
For large images, this takes a while. Recetly, I found out about Workers
. It seems like the best way to perform asynchronous CPU-demanding tasks, so that my browser doesn't freeze.
It's possible to pass UInt8Array
to worker - but how can I then retrieve the pseudo-Class instance array? Pseudo-Class instances in javascript can't be serialised...