Using Cairo and getting data back out.



Hello, I apologize if this is off-topic as it deals a lot with Cairo,
I feel it is a borderline thing.  Please feel free to send me packing
to somewhere else if I'm wrong on that point :-)

I'm trying to load an image into a DrawingArea, then put lines on it
with Cairo and get that back out.  Is this possible or do I need to go
figure out something with GC and Drawable?

Here's the guts of the procedure so far, sorry it's big but I don't
know how to trim it down and keep it understandable.

---------------------------------------------
class ExampleWindow : public Gtk::Window {

	public:
		ExampleWindow (std::string path) {
			imagePB = Gdk::Pixbuf::create_from_file(path);
			drawOnMe.signal_expose_event().connect(sigc::mem_fun(*this,&ExampleWindow::on_da_expose_event));
			add(drawOnMe);
			show_all();
		}

	private:
		Gtk::DrawingArea drawOnMe;
		Glib::RefPtr<Gdk::Pixbuf> imagePB;

		bool on_da_expose_event (GdkEventExpose * event) {
			std::cout << "Expose Event" << std::endl;
			Glib::RefPtr<Gdk::Window> window = drawOnMe.get_window();
			Cairo::RefPtr<Cairo::Context> context = window->create_cairo_context();
			Cairo::RefPtr<Cairo::ImageSurface> image_surface =
Cairo::ImageSurface::create (Cairo::FORMAT_RGB24,
imagePB->get_width(), imagePB->get_height());
			Cairo::RefPtr<Cairo::Context> image_context =
Cairo::Context::create(image_surface);
			
			Gdk::Cairo::set_source_pixbuf (image_context, imagePB, 0.0, 0.0);
			image_context->paint();
		
			context->set_source(image_surface, 0.0, 0.0);
			context->paint();
		
			context->set_line_width(5.0);
			context->set_source_rgb(0.8, 0.0, 0.0);
			context->move_to(10,20);
			context->line_to(100, 200);
			context->stroke();
			
			return false;
		}
};
---------------------------------------------

I tried getting a surface from that context after the stroke() call
and then a write to PNG:

Cairo::RefPtr<Cairo::Surface> sfc = context->get_target();
sfc->write_to_png("please_work.png");

That just crashed on an expose event and I didn't bother to debug. I
have the feeling I've gone wrong in my understanding of Cairo.

So I guess what it boils down to is I'm curious if there is an easier
path by using something in Gtkmm (which I chased my tail for a long
time trying to find something) or if there is a way to get that drawn
data back out of there.

Thanks for any and all help :-)

- John Hobbs

john velvetcache org


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]