GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Picture problem in cairo

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
TriKri
Familiar Face


Joined: 19 Mar 2008
Posts: 14

PostPosted: Tue Mar 25, 2008 5:12 pm    Post subject: Picture problem in cairo Reply with quote

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!
Back to top
TriKri
Familiar Face


Joined: 19 Mar 2008
Posts: 14

PostPosted: Tue Mar 25, 2008 5:45 pm    Post subject: Reply with quote

I have found one way to do it, but that way seems to scale the drawing context...

Anyway, here's the code:
Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
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 :(
Back to top
subaren
Familiar Face


Joined: 20 Aug 2007
Posts: 8

PostPosted: Fri Apr 04, 2008 7:59 pm    Post subject: GTK+ Tutorial Reply with quote

GTK+ Tutorial

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

Subair
Back to top
TriKri
Familiar Face


Joined: 19 Mar 2008
Posts: 14

PostPosted: Sat Apr 05, 2008 10:23 pm    Post subject: Re: GTK+ Tutorial Reply with quote

Thanks
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP