PDA

View Full Version : Implementing image into compiled binary in fpc



LordEirik
01-01-2007, 09:23 PM
Hello.

I just started looking at Free Pascal on linux and gtk2.
So i made a program that uses the gtk2 api and opens a image from my computer, but I want the program to have te image inside itself.
So the user of the program will have a stand alone file, instead of a program, plus a image :p

If you use delphi the IDE will make a .dfm file wich will contain the image as some ASCII-lookalike stuf (under Picture.Data = ).

So, how can I implement my image into the program binary?

dmantione
01-01-2007, 11:29 PM
Use the bin2obj tool to convert the image into an include file, which you can include in your program.

Resources, like Delphi uses, are another possibility, but for a plain gtk2 program, bin2obj looks the easiest solution to me.

LordEirik
02-01-2007, 06:59 AM
I did not find much help on how to use bin2obj on the net.
And I didnt understand much of the bin2obj manual, but I think i managed to do it the right way.
I have a image called logo.jpg, wich is my testimage for this project. I used bin2obj like this:
bin2obj -o logo.o -c logo logo.jpg
wich gave me a logo.o with the array logo of bytes.

Now I included the file in the source using {$I logo.o}
Edit:
Ive tried another way to include the object file;
write in source: {$L logo.o}
run: ppc386 test.pas
(test.pas is my source)
it compiled, but if I run size test there is no difference from te old file, to the one with {$I logo.o} or the one with {$L logo.o}...

Now i'm stuck again. How can I use the file i included in my program? How to show the piucture?

dmantione
02-01-2007, 08:15 AM
You should get an include file, not an object file. Therefore you should use:

bin2obj -o logo.inc -c logo logo.jpg

Then, in your program, you do for example:

program logoprogram;

{$i logo.inc}

var p:pointer;

begin
p:=@logo; {p is now a pointer pointing to the image.}
end.


The reason your program has the same size is probably because you smartlink, the compiler will remove all unused code then and you don't use your image yet.

LordEirik
02-01-2007, 09:30 AM
About the size: I guess that might be it.

I'm afraid I have no experience with pointers and can't figure out how to get from the pointer to show the image.
I was thinking on using GtkImage (wich I used for loading from the file).
But looking at the api I don't know how to load it.
I had a look at loading from pixmap, wich need a GdkPixmap and a GdkBitmap, then the GdkPixmap need a GdkDrawable, all this makes me think im way off. So I had a look at GObject and gpointer, wich I think is even more way off.

So i basicly need help with the whole source, but I don't want that. I want to write most of it myself, cause I learn more then.
But a clue or something pointing me in the right direction might help alot :)

dmantione
02-01-2007, 10:18 AM
Indeed, it looks like the GTK2 API is not designed to load images from memory. So the only way is to load the image yourself into a pixmap. As I look to the documentation this is not trivial.

I think the solution here is to use gdk_pixmap_create_from_xpm_d which returns you a GdkBitmap. Then gtk_image_new_from_pixmap allows you create a GtkImage from a GdbBitmap.

The issue here is that you need your image in XPM format in memory. I don't know the answer here. An X window programming specialist should know how to do this.

I think it might be a good idea to ask this question on a GTK2 mailing-list, they might have some ideas how to load an image from memory.

LordEirik
02-01-2007, 10:26 AM
I will try some of the things, and look around.

I thank you for all your help, I feel I almost got it now, and it's all thanks to you :)

I might post here if I can figure out how to load the picture.

LordEirik
02-01-2007, 01:46 PM
Nothing so far, only one lame way.
I can of course save the picture, load the picture using gtk_image_new_from_file, and delete it.
This works just fine and it's fast enough. But it's far from any rational solution.