GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 23, 2014 12:41 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Picture problem in cairo
PostPosted: Tue Mar 25, 2008 5:12 pm 
Offline
Familiar Face

Joined: Wed Mar 19, 2008 3:23 pm
Posts: 19
Hello, I have a question about pictures in cairo.

I have a cairo drawing context *cr, which I want to insert a picture into. The picture is going to be in a rectangle in *cr from point (x1, y1) to (x2, y2). The picture that's imported has the width w and the height h, and I want the picture to cover the whole rectangle, hence it's scaled with a factor f = MAX((x2-x1)/w, (y2-y1)/h) when a part of it is copied into *cr. For example if I want a rectangle in the picture that shows which part of the picture that's going to be shown, that rectangle would be a scale 1/f in size compared to the rectangle in *cr.

Now I know that there are functions cairo_scale and cairo_set_source_surface, that you can use to specify the relative position and scale between *cr and the picture, but I don't know how to use them. Then there are the function cairo_rectangle(cr, ...) which I'm going to use for specifying the part of *cr that's going to contain the part of the picture that's going to be shown. There is also a function cairo_transform, which you could use instead of cairo_scale, if you want to make a movement and a scaling, but I don't know if I should use that function or if I just have to make the scaling.

How shall I use these functions and in which order? I know that cairo works with transformation matrices, and that *cr contains such a matrix. I know how to use transformation matrices myself, but I don't know how cairo makes use of them and when. Please help!

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 5:45 pm 
Offline
Familiar Face

Joined: Wed Mar 19, 2008 3:23 pm
Posts: 19
I have found one way to do it, but that way seems to scale the drawing context...

Anyway, here's the code:
Code:
cairo_surface_t *picture;
double w, h;
double f;
picture = cairo_image_surface_create_from_png("Images/Peyto_Lake-Banff_NP-Canada.png");
w = cairo_image_surface_get_width(picture);
h = cairo_image_surface_get_height(picture);
f = MAX((x2-x1)/w, (y2-y1)/h);

cairo_scale(cr, f, f);
cairo_set_source_surface(cr, picture, ((x1+x2)/f-w)/2, ((y1+y2)/f-h)/2);
cairo_rectangle(cr, x1/f, y1/f, (x2-x1)/f, (y2-y1)/f);
cairo_fill(cr);


I wanted the middle of the rectangle (x1, y1) to (x2, y2) in *cr to match the point (w/2, h/2) in the picture, as you can see in the call of cairo_set_source_surface. The only thing is that *cr gets scaled. Is there some other way to do this so *cr doesn't get scaled? Also, is there support for jpg images in cairo? There is cairo_image_surface_create_from_png but there isn't cairo_image_surface_create_from_jpg :(


Top
 Profile  
 
 Post subject: GTK+ Tutorial
PostPosted: Fri Apr 04, 2008 7:59 pm 
Offline
Familiar Face

Joined: Mon Aug 20, 2007 8:52 pm
Posts: 8
GTK+ Tutorial

Look at this link for the cairo tutorial
http://www.zetcode.com/tutorials/cairographicstutorial/cairoimages/

Subair


Top
 Profile  
 
 Post subject: Re: GTK+ Tutorial
PostPosted: Sat Apr 05, 2008 10:23 pm 
Offline
Familiar Face

Joined: Wed Mar 19, 2008 3:23 pm
Posts: 19
Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group