[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cairo
Subject:    Re: [cairo] how to cropping or clipping an image using cairo library
From:       anurag swarnkar <anu_swar10 () yahoo ! co ! in>
Date:       2008-01-28 7:13:07
Message-ID: 722566.76768.qm () web8327 ! mail ! in ! yahoo ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Thanks a lot Carl. That piece of code was very informative.

----- Original Message ----
From: Carl Worth <cworth@cworth.org>
To: anurag swarnkar <anu_swar10@yahoo.co.in>
Cc: cairo@cairographics.org
Sent: Monday, 28 January, 2008 8:02:23 AM
Subject: Re: [cairo] how to cropping or clipping an image using cairo library

On Mon, 28 Jan 2008 10:58:21 +0530 (IST), anurag swarnkar wrote:
> I want to know whether there is any API available in cairo through
> which i can crop or clip (getting a portion of image) an image?

Yes.

The simplest case, (which I'm guess is what you're wanting), is just a
rectangle, but doing fancier stuff isn't actually any harder. I'll
show both.

For example, if you have the image in a cairo surface named "image"
and you are drawing to some cairo context "cr" then you might do
something like this, (let's assume that we want the origin of the
complete image at (x0,y0) and that we want to display only a
rectangular portion of the image (x,y)-(width,height)). So that would
look like:

    cairo_set_source_surface (cr, image, x0, y0);
    cairo_rectangle (cr, x0 + x, y0 + y, width, height);
    cairo_fill (cr);

And if you wanted to crop out a circular portion of the image, you
might instead do:

    cairo_set_source_surface (cr, image, x0, y0);
    cairo_arc (cr, x0 + width / 2, y0 + height / 2,
           MIN (WIDTH,HEIGHT) / 2, 0, 2 * M_PI);
    cairo_fill (cr);

Or if you wanted to crop out any polygonal region in points (a,b),
(c,d)...:

    cairo_set_source_surface (cr, image, x0, y0);
    cairo_move_to (cr, a, b);
    cairo_line_to (cr, c, d);
    ...
    cairo_fill (cr);

Anyway, hopefully that gives you the basic idea. And of course, you
can "crop out" text-shaped portions of an image with
cairo_set_source_surface;cairo_show_text as well.

Have fun with cairo!

-Carl






      Why delete messages? Unlimited storage is just a click away. Go to \
http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html


[Attachment #5 (text/html)]

<html><head><style type="text/css"><!-- DIV {margin:0px;} \
--></style></head><body><div style="font-family:times new roman, new york, times, \
serif;font-size:12pt"><div style="font-family: times new roman,new york,times,serif; \
font-size: 12pt;">Thanks a lot Carl. That piece of code was very \
informative.<br><br><div style="font-family: times new roman,new york,times,serif; \
font-size: 12pt;">----- Original Message ----<br>From: Carl Worth \
&lt;cworth@cworth.org&gt;<br>To: anurag swarnkar \
&lt;anu_swar10@yahoo.co.in&gt;<br>Cc: cairo@cairographics.org<br>Sent: Monday, 28 \
January, 2008 8:02:23 AM<br>Subject: Re: [cairo] how to cropping or clipping an image \
using cairo library<br><br>On Mon, 28 Jan 2008 10:58:21 +0530 (IST), anurag swarnkar \
wrote:<br>&gt; I want to know whether there is any API available in cairo \
through<br>&gt; which i can crop or clip (getting a portion of image) an \
image?<br><br>Yes.<br><br>The simplest case, (which I'm guess is what you're  \
wanting), is just a<br>rectangle, but doing fancier stuff isn't actually any harder. \
I'll<br>show both.<br><br>For example, if you have the image in a cairo surface named \
"image"<br>and you are drawing to some cairo context "cr" then you might \
do<br>something like this, (let's assume that we want the origin of the<br>complete \
image at (x0,y0) and that we want to display only a<br>rectangular portion of the \
image (x,y)-(width,height)). So that would<br>look like:<br><br>&nbsp;&nbsp;&nbsp; \
cairo_set_source_surface (cr, image, x0, y0);<br>&nbsp;&nbsp;&nbsp; cairo_rectangle \
(cr, x0 + x, y0 + y, width, height);<br>&nbsp;&nbsp;&nbsp; cairo_fill \
(cr);<br><br>And if you wanted to crop out a circular portion of the image, \
you<br>might instead do:<br><br>&nbsp;&nbsp;&nbsp; cairo_set_source_surface (cr, \
image, x0, y0);<br>&nbsp;&nbsp;&nbsp; cairo_arc (cr, x0 + width / 2, y0 + height / \
                2,<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;  MIN \
                (WIDTH,HEIGHT) / 2, 0, 2
 * M_PI);<br>&nbsp;&nbsp;&nbsp; cairo_fill (cr);<br><br>Or if you wanted to crop out \
any polygonal region in points (a,b),<br>(c,d)...:<br><br>&nbsp;&nbsp;&nbsp; \
cairo_set_source_surface (cr, image, x0, y0);<br>&nbsp;&nbsp;&nbsp; cairo_move_to \
(cr, a, b);<br>&nbsp;&nbsp;&nbsp; cairo_line_to (cr, c, d);<br>&nbsp;&nbsp;&nbsp; \
...<br>&nbsp;&nbsp;&nbsp; cairo_fill (cr);<br><br>Anyway, hopefully that gives you \
the basic idea. And of course, you<br>can "crop out" text-shaped portions of an image \
with<br>cairo_set_source_surface;cairo_show_text as well.<br><br>Have fun with \
cairo!<br><br>-Carl<br></div><br></div></div><br>


      <!--5--><hr size=1></hr> Get the freedom to save as many mails as you wish. <a \
href="http://in.rd.yahoo.com/tagline_mail_5/*http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html/">Click \
here to know how.</a></body></html>



_______________________________________________
cairo mailing list
cairo@cairographics.org
http://lists.cairographics.org/mailman/listinfo/cairo

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic