[fxruby-users] FXImage + RMagick problems

Jeroen van der Zijp jeroen at fox-toolkit.org
Wed Oct 19 08:54:06 EDT 2005


On Wednesday 19 October 2005 02:21 am, Pawel Szymczykowski wrote:
> I posted this in ruby-talk a moment ago accidentally, so I'm sorry if you
> see it twice. It's kind of late at night and I've been boggling at this
> problem for a while, so my brain doesn't seem to be working right.
> 
> Here are the contents of my original ruby-talk incursion:
> 
> Hi,
> 
> I'm trying to combine FXRuby with RMagick. I've searched the mail archives
> (I had to index them myself! Is there an easy to use official search page
> somewhere?) and apparently it's been done before, but I can't seem to get it
> to work.
> 
> Basically I'm processing my image and then doing an export_pixels and trying
> to convert it to the FXImage format. It works the first time, but when I try
> to do it again it crashes on my saying 'invalid image' in Linux, or
> something useless about abnormal termination in Windows.
> 
> This is the piece of code that seems to be giving me the specific trouble:
> 
> pixels = []
> @current_image.export_pixels(0, 0, @current_image.columns,
> @current_image.rows,
> "RGBA").each_slice(4) { |rgba| pixels << FXRGBA( *rgba ) }
> an_image = FXImage.new(app, pixels, IMAGE_OWNED|IMAGE_KEEP,
> @current_image.columns, @current_image.rows )
> 
> That works the first time - I get a nice picture as expected when drawn to
> the canvas. The next time it happens via a timeout, I get the crash.
> Actually, it's more like it works fine if I do it before calling
> app.create(or maybe run?), but not afterwards.

What happens is, during create(), FXImage transfers the pixel data from
the client [your application] to the X11 Server.  

The image->create() is broken up into 3 steps:

	1) create the X11-Server resident pixmap.

	2) transfer the pixel data from the FXImage into this map, using
	   the render() function.

	3) if IMAGE_KEEP is not specified, release the client-side pixel
	   data since they're no longer needed.

>From this, you can surmise that it is indeed possible to change the pixmap
kept in the X11 Server later on:- just call render() again.  Of course,
IMAGE_KEEP is needed to ensure that the client-side pixel data is still
around as well.


> I've also tried the following as mentioned in one of the messages in the
> archives, but it complains that it's looking for an Array (buffer is
> FXMemoryBuffer class of course):
> 
> buffer = FXMemoryBuffer.new( pixels )
> an_image = FXImage.new(app, buffer, IMAGE_OWNED|IMAGE_KEEP,
> @current_image.columns, @current_image.rows )
> 
> I've tried several different combinations of the flags, and putting
> an_image.create in various places. The former didn't help at all, but in the
> latter I just got a black image.
> 
> Am I doing something idiotic somewhere? I'm kind of new to ruby and GUI
> development in general being more of web app guy. I can post a runnable
> version of the code up somewhere if the problem isn't completely obvious and
> of course if someone is feeling charitable enough to take a look at it.

I'm assuming you're talking about FXMemoryStream?  Anyway, FXStream is useful
as it allows e.g. FXBMPImage to extract a BMP-type image from a sequence of
bytes.  This uses the bmpimage->loadPixels() API.  More typically this is
used in conjunction with FXFileStream (in C++):

	FXFileStream strm;

	if(strm.open("filename.bmp",FXStreamLoad)){

		FXBMPImage *bmp=new FXBMPImage(app,IMAGE_KEEP|IMAGE_SHMI|IMAGE_SHMP);

		bmp->loadPixels(strm);

		strm.close();
	}


Hope this helps,


		Jeroen






More information about the fxruby-users mailing list