LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-03-2010, 02:40 AM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,633
Blog Entries: 29

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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 !
 
Old 03-03-2010, 08:13 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
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);
 
1 members found this post helpful.
Old 03-04-2010, 02:14 AM   #3
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,633
Blog Entries: 29

Original Poster
Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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

Last edited by TheIndependentAquarius; 03-04-2010 at 03:15 AM.
 
Old 03-04-2010, 08:25 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
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?
 
Old 03-04-2010, 08:30 AM   #5
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,633
Blog Entries: 29

Original Poster
Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Thanks again !

I am trying to get the problem solved, if I manage to find something interesting, I shall report back here.
 
Old 03-06-2010, 04:22 AM   #6
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,633
Blog Entries: 29

Original Poster
Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
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

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");
Attached Images
File Type: jpg different.jpg (102.7 KB, 8 views)

Last edited by TheIndependentAquarius; 03-06-2010 at 05:12 AM.
 
  


Reply

Tags
cairo


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Cairo library works? ~Linux_user~ Programming 1 12-20-2008 11:24 AM
convert image.png image.jpg changing colours MikeyCarter Linux - Software 1 11-17-2006 11:16 AM
PNG library WorldBuilder Linux - Software 7 01-01-2006 01:28 AM
Embarrasing question- Image pasting sumguy231 Linux - General 5 02-14-2005 03:05 AM
Webalizer: configure: error: png library not found... please install png. mikeshn Linux - Software 5 01-10-2005 03:28 PM


All times are GMT -5. The time now is 07:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration