any plans for Image.ReadPixels() ?

Monkey Forums/Monkey Programming/any plans for Image.ReadPixels() ?

Nobuyuki(Posted 2012) [#1]
Hello,

I'm not sure exactly how to start off this post other than to re-iterate the question in the topic. I noticed that Monkey has a ReadPixels() function, but it only works on the current render buffer, and not an arbitrary surface like an Image object.

This normally is simple to work around, but I'm writing a class where it would be helpful to split an image into many parts (a variable width font) to do some ReadPixel operations on the individual frames and create some metadata based on the image in its constructor. This example image might help explain what sorta metadata I plan on retrieving from the image:



Basically, I planned on reading a line from each frame of the image set, and checking each pixel within a loop for the purple color, short-circuiting it where it was found and setting that to be the character's width in my array of character widths.

The work-around is sloppy; I have to mark the class to perform this operation on the next render cycle, not in the ctor, and either have a specific method of the class at the top of my main OnRender to fire every frame to check whether to run the workaround operation, OR somehow limit that method from outside the class to only being called on the first render loop. This kinda negates out the class's modularity because it would have a very specific way of constructing it that can't be rigidly enforced in code from within the class itself. Furthermore, Images with a lot of frames like this have the potential of exceeding the dimensions of the rendering context and would require multiple blits to perform the operation, although I don't see this to be a problem in my situation.

In any case, it would be a lot more convenient (and very helpful to have) if Monkey had a way to directly read the surface context from any individual image frame, or any individual image, for that matter. Is there some technical limitation or other concern which made it so we have to read from the main render context, I'm wondering? It's such a promising feature, but the limitation of only being able to read from the render surface makes it a bit more difficult to use, and not exactly OO-friendly...


Samah(Posted 2012) [#2]
It's a limitation of some targets such that it can only read pixels directly from the canvas.


Nobuyuki(Posted 2012) [#3]
@Samah : Do you know which targets have this limitation and which don't?


AdamRedwoods(Posted 2012) [#4]
from my understanding, there is no image.readpixel because mojo dumps the texture data after uploading it to the graphics card.

the "now deprecated but exists in miniB3D LoadImageData" command retained the pixel data.

that said, Samah is correct, there are limitations to this in HTML5.


FelipeA(Posted 2012) [#5]
You can read pixels on htm5 from an image if you draw it to a local context. For example:

var canvas=document.getElementById("game_canvas");
var context=canvas.getContext("2d");
var img= new Image();
img.src = "img.png";


img.onload=function()
{
	context.drawImage(this,20,20);
	
	//Temporal bitmap
	var tmpBitmap=document.createElement("canvas");
	tmpBitmap.width=this.width;
	tmpBitmap.height=this.height;
	var tmpBitmapData=tmpBitmap.getContext("2d");
	
	
	//Here we draw multiple images to de bitmap, just for the lulz
	for(var i=0;i<150;i++)
	{
		tmpBitmapData.drawImage(this,Math.random()*tmpBitmap.width,Math.random()*tmpBitmap.height);
	}
	
	//We draw the final bitmap to the main context, not really necessary to read pixels.
	context.drawImage(tmpBitmap,300,300);
	
	//We can read pixels from this image:
	var imgdata=tmpBitmapData.getImageData(50,50,1,1);
	console.log("RED: "+imgdata.data[0]+", GREEN: "+imgdata.data[1]+", BLUE: "+imgdata.data[2]+", ALPHA: "+imgdata.data[3]);
};




samowitsch(Posted 2012) [#6]
What about this thread => http://monkeycoder.co.nz/Community/post.php?topic=3482&post=36720


Nobuyuki(Posted 2012) [#7]
@Samowitsch

Looks promising. I've installed the module, and will definitely look into giving it a shot. In the meantime, I've simply generated some width metadata for my fonts using a separate app. Not ideal, but good enough to get the app going.

I wonder if there's even a market for yet another variable-width font class / generator in Monkey...