LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Pasting a png image on another through CAIRO library (https://www.linuxquestions.org/questions/programming-9/pasting-a-png-image-on-another-through-cairo-library-792804/)

Aquarius_Girl 03-03-2010 01:40 AM

Pasting a png image on another through CAIRO library
 
I could easily paste images on each other in Python through PIL (Python Imaging Library)
Now I need to use Cairo with C++ for the above purpose.

I have referred the following but couldn't get much help !

http://www.cairographics.org/manual/

http://www.cairographics.org/manual/...e-write-to-png

Kindly guide !

MTK358 03-03-2010 07:13 PM

http://www.cairographics.org/samples/

Look for:

Code:

int              w, h;
cairo_surface_t *image;

cairo_arc (cr, 128.0, 128.0, 76.8, 0, 2*M_PI);
cairo_clip (cr);
cairo_new_path (cr); /* path not consumed by clip()*/

image = cairo_image_surface_create_from_png ("data/romedalen.png");
w = cairo_image_surface_get_width (image);
h = cairo_image_surface_get_height (image);

cairo_scale (cr, 256.0/w, 256.0/h);

cairo_set_source_surface (cr, image, 0, 0);
cairo_paint (cr);

cairo_surface_destroy (image);


Aquarius_Girl 03-04-2010 01:14 AM

Many thanks for bothering to reply !

I had seen this before, but somehow I couldn't get the code pointed out by you to work , maybe because of my lack of understanding the maths involved in arcs and scale.

Meanwhile I found this : http://zetcode.com/tutorials/cairogr...l/compositing/

That shows how to paint one image over the other, let me see if I can get it to work with png,

I NEED SOME WAY TO PASTE A PNG ON ANOTHER W.R.T MY SPECIFIED X,Y COORDINATES, I DON'T WANT IT TO OVERWRITE THE WHOLE IMAGE !

Thanks again :)

MTK358 03-04-2010 07:25 AM

I've only played around with Cairo before, so I am no expert at it, so I don't know how to do what you want. But I am certain is it possible.

One thing that bothers me about most Cairo tutorials is that they assume you will use it with GTK, and I certainly don't understand all the details of it's inner workings. Can't they just use PNG or Xlib?

Aquarius_Girl 03-04-2010 07:30 AM

Thanks again !

I am trying to get the problem solved, if I manage to find something interesting, I shall report back here.

Aquarius_Girl 03-06-2010 03:22 AM

Now here's the good news,
I've managed to paste 2 png images on a base png image w.r.t Cairo!

No I didn't copy this whole code from somewhere, I applied little logic and got the problem solved :D

Here by I have attached the final png.
The base is a black colored png image.
On the right side of the base image there is a map png pasted.
On the left side of the base image there is a box png pasted (blue one).


I have pasted the code here, so that it may help someone else.
The important links w.r.t to above problem are:
http://zetcode.com/tutorials/cairogr...l/compositing/
and
http://zetcode.com/tutorials/cairogr...l/shapesfills/

Code:

cairo_surface_t *surface  = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 240, 80);
        cairo_t        *cr      = cairo_create (surface);

        cairo_t        *first_cr;
        cairo_t        *second_cr;
        cairo_t        *third_cr;
        cairo_surface_t *a;
        cairo_surface_t *b;
        cairo_surface_t *c;
       
        a = cairo_surface_create_similar (cairo_get_target (cr), CAIRO_CONTENT_COLOR_ALPHA, 240, 80);
        b = cairo_surface_create_similar (cairo_get_target (cr), CAIRO_CONTENT_COLOR_ALPHA, 240, 80);
        c = cairo_surface_create_similar (cairo_get_target (cr), CAIRO_CONTENT_COLOR_ALPHA, 240, 80);
       
        a = cairo_image_surface_create_from_png ("cairo-demo.png");
        b = cairo_image_surface_create_from_png ("box.png");
        c = cairo_image_surface_create_from_png ("base.png");
               
        first_cr  = cairo_create (a);
        second_cr = cairo_create (b);
        third_cr  = cairo_create (c);
       
        cairo_stroke (first_cr);
        cairo_stroke (second_cr);
        cairo_stroke (third_cr);
       
        // Pasting the Map on the base image
        cairo_set_operator      (third_cr, (cairo_operator_t)5);
        cairo_set_source_surface (third_cr, a, 220, 80);
        cairo_paint              (third_cr);
       
        cairo_set_source_surface (cr, c, 20, 20);
        cairo_paint(cr);
       
        cairo_surface_write_to_png (c, "different.png");

        // Pasting the Label on the base image
        cairo_set_operator      (third_cr, (cairo_operator_t)5);
        cairo_set_source_surface (third_cr, b, -20, 80);
        cairo_paint              (third_cr);
       
        cairo_set_source_surface (cr, c, 20, 20);
        cairo_paint(cr);
       
        cairo_surface_write_to_png (c, "different.png");



All times are GMT -5. The time now is 04:03 AM.