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 Apr 24, 2014 5:55 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: create image file out of array
PostPosted: Mon Feb 13, 2012 1:09 pm 
Offline

Joined: Mon Feb 13, 2012 12:47 pm
Posts: 3
I have a simple problem to solve, but since I have no experience with gtk-programming I ask you for guidance:

I have a simple 2 dimensional Integer-Array with n different numbers in it.
I just want to create a Image-File out of this Integer-Array.
So that every array element should be a pixel.
Every different number should be in a different color.
For expample, if I have an Array with 3 different numbers 0,1 and 2:
every appearance of 1 in the array for example should be red, and so on.

In my case:
the Integer-Array has the size 600x600 and it has got three different numbers in it.

Thanks for Your help


Top
 Profile  
 
 Post subject: Re: create image file out of array
PostPosted: Mon Feb 13, 2012 3:13 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello and welcome to the GTK+ forums!

If I understand you correctly, you would like to take values from array, convert it to pixel data and write that to some image file on disk.

All of those tasks can be done using gdk-pixbuf library through GdkPixbuf structure, but you'll need to learn a bit about it. This forum has quite some data about similar problems and combined with official API docs I think you should be able create something.

If you have any troubles, feel free to post your code and ask questions about it.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: create image file out of array
PostPosted: Mon Feb 20, 2012 1:11 pm 
Offline

Joined: Mon Feb 13, 2012 12:47 pm
Posts: 3
thanks for the quick answer.
can you just tell me which functions i need to use for my problem?
i've searched for this in this forum, but there is to much information on this topic.
and i don't know which affects my situation.

thanks in advance


Top
 Profile  
 
 Post subject: Re: create image file out of array
PostPosted: Mon Feb 20, 2012 1:58 pm 
Offline
Familiar Face

Joined: Wed Aug 17, 2011 12:20 pm
Posts: 20
In this case there is a much simpler solution, using neither library. Read the Wikipedia page on the XPM image format:
http://en.wikipedia.org/wiki/X_PixMap
Assuming you have your matrix in a text file, conversion into a XPM file takes five minutes using Unix command line tools, and also is trivial to do in C. If you only have this one instance to convert I'd use an editor to insert the XPM header.
Once you have the XPM, load it into your favourite graphics editor (like GIMP), and save it into whatever format you like. Or, on the command line, use the "convert" tool from ImageMagick.

Cheers Peter


Top
 Profile  
 
 Post subject: Re: create image file out of array
PostPosted: Tue Feb 21, 2012 10:53 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
If you do choose the xpm format then you can skip on the gimp/image magic step with gdk_pixbuf_new_from_xpm_data (), but personally I see no need to convert from format to format when it would be better to just do the thing directly in the first place.
gdk_pixbuf_new_from_data () is a basic way of getting bitmapped data, you just have to convert your format for each pixel into the appropriate guchar value. You'd be doing this anyway in creating an xpm file as xpm uses char type so putting it straight into a pixbuf will save you a step.
But yes have a look through the documentation, its how you learn stuff (even if the guys who put it together only make it understandable for people like them).


Top
 Profile  
 
 Post subject: Re: create image file out of array
PostPosted: Thu Feb 23, 2012 5:56 pm 
Offline

Joined: Mon Feb 13, 2012 12:47 pm
Posts: 3
i am very thankful for your advice with xpm. i will try it out and consult you if there's a problem :)


Top
 Profile  
 
 Post subject: Re: create image file out of array
PostPosted: Mon Feb 27, 2012 5:35 pm 
Offline
Familiar Face

Joined: Wed Aug 17, 2011 12:20 pm
Posts: 20
Paul,

it seems you misunderstand my point. From what Xerxes249 writes, (s)he has a problem to solve that goes like that: Have a matrix of data, want a bitmap. To me, it does not sound as if the problem were: Have a matrix of data, want some fancy code involving some libraries. Admittedly, the work "gtk-programming" was mentioned.

If I can avoid writing dozens and hundreds of lines of C code by just invoking five commands at a bash prompt (even if they got nothing to do with neither GTK nor GDK) I guess it's easy to tell what I'd prefer. These commands can even be put into a bash script to fully automate the task.

Cheers Peter


Top
 Profile  
 
 Post subject: Re: create image file out of array
PostPosted: Mon Feb 27, 2012 10:06 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
To script or not to script is often the decision that defines what kind of programmer one is at heart. I'm a little old fashioned and get stuck in all the trappings that go along with that.
I assume that questions here are by default about code that will be used in a program that includes the gtk libraries. As gtk is designed to be platform independent I will try to avoid including any other libraries that might not actually exist or be consistent on the end user PC (even excluding string.h). That way I know if it works on my box it will work on everyones; the coding may be more involved but there is less maintenance.
Of course if it is for a task that xerxes is working on alne and sees no need to distribute the code to others then scripting (if its a repeated task) or use of editing/convert (if its a once off) is definitely for the best. Apart from that, it depends on your ethos.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


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