This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

[SOLVED] TK: include images in packages and use them

Help for those learning Tcl or writing their own scripts.
Post Reply
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

[SOLVED] TK: include images in packages and use them

Post by raider2k »

hi guys

not sure if anyone is familiar with tk in here but id like to know if its possible to recall an image from inside a compiled tcl .exe package. its possible to include it in the compiler without errors but im not sure if its also possible to call that image from inside the package.

at the moment the image is in the same directory as the exe and is being called by using

Code: Select all

image create photo imgobj -file "myimage.gif"
.c.img config -image imgobj
Last edited by raider2k on Fri Feb 26, 2010 4:08 pm, edited 1 time in total.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Well, you'd have to keep the image as a block in the .text section (accessible through a tcl variable), and use the -data option as opposed to the -file option.
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

please be so kind to post me code examples :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Well... in pure tcl-space it would look something like this:

Code: Select all

#The actual image data, I'm lazy just using ... for this example
set imageData {.................}
set myImage [image create photo imgobj -data $imageData
..
.c.img config -image imgobj
Most likely, you'd rather include the image data as a separate file, in C-space. One fairly common way of doing this, is to include it as a header-file, that simply defines a string (constant) with the image data. This kind of packing would usually require some very careful coding, as you'd have to watch out for quotes (") and escapes (\) to get a valid content. You'd probably be best off using a tool for this, though some more advanced IDE's provide means of loading a .text object using an external data source during compilation.

Once the variable/constant has been declared, you'll have to tie it into tcl-space using the Tcl_LinkVar fuction (remember to pass the TCL_LINK_READ_ONLY flag if using a string constant). It would then be accessible in the tcl interpreter, using the name you've assigned it with the Tcl_LinkVar function, and fully usable with image create -data $myvar

This article might be of interrest as how you could embed the actual image data into a separate objectfile (.o), suitable for linking, when working with gcc without fancy IDE's:
http://www.linuxjournal.com/content/emb ... rsion-5967
NML_375
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Actually, when thinking of it, since we're dealing with binary data, you'd probably be better off creating a new Tcl_Obj along with the objcopy embedding. This way, we can easily contain the NULL character, which would otherwize act as a string terminator.

I'd look at the Tcl_NewObj() function for this, although Tcl still expects strings to be utf-8 encoded. Tcl_UniCharToUtfDString() might be what the doctor ordered here. Finally use Tcl_SetVar2Ex() to set a tcl-space variable to the value of the Tcl String Object.

Messy, I know :/
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

oh .. my .. god ... oO

NOW im really confused >_<
and nope, im not doing any c or c++ or anything like that - tcl/tk is my home.

well .. what you are trying to say is that i need to "convert" the image into raw txt, apply a variable to it and use that variable in the image command? any easy ways to convert an image into that raw text i need?

and maybe less complicated, less c-dependend examples next time please :> i still need to learn and understand ^^
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

My bad, when you said build an exe-file, I simply assumed you were developing a tcl/tk-extended software (kind of like eggdrop).

I guess you're using TclPro's bytecode compiler or such?
In plain tcl/tk-space, either set a variable to contain the image data, and use the classic $varname syntax along with the -data option for image. The second option would be to embed the data directly into the image comand in place of the variable...

You could write a simple tcl:et to generate a script containing the data:

Code: Select all

set fd [open "image.jpeg" "RDONLY"]
set data [read $fd]
close $fd
set fd [open "image.tcl" "WRONLY CREAT TRUNC"]
puts $fd [list set image $data]
close $fd
Something along these lines would do the trick.. then you'd simply have to include the generated image.tcl script to embed your image as a globalspace variable named 'image'

Edit: Fixed minor typo in the code
NML_375
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Done some testing, and the classic [list ..] trick does not work very well. Most likely NULL or EOF characters are not escaped within the list, which works fine for data already loaded into memory, but wrecks havoc with the source command.

I did find this article, which uses tcllib and base64 encoding to sort that issue:
http://wiki.tcl.tk/1434
NML_375
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

More post-farming :p

Got the following working nicely:

Code: Select all

package require base64

set infile "somefile.gif"
set outfile "somefile.tcl"

set if [open $infile "RDONLY"]
set of [open $outfile "WRONLY CREAT TRUNC"]

fconfigure $if -translation binary -encoding binary

puts $of [list set imageData [::base64::encode [read $if]]]
close $if
close $of
This would then be used like this:

Code: Select all

source somefile.tcl
image create photo imgobj -data $imageData
.c.img config -image imgobj
This works because the gif image class supports base64-encoded data natively.
Last edited by nml375 on Fri Feb 26, 2010 2:54 pm, edited 1 time in total.
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

mhm .. interesting ..
and yup .. i was referring to an output .exe after compiling/wrapping ^^

base64 .. mhm .. very interesting ..
ill try your code when im at home later on, looks very interesting. plus i hope that that base64 package doesnt result in an error when compiling/wrapping ..

let me repeat if i caught what you are trying to tell me:

the first code part is a one-time-per-image tcl file needed to parse the images "code" (since i tried to open it with wordpad, notepad++ and nano already and didnt show up in plain characters) and to then save it into somefile.tcl which will be used by the image create tk command for each query of that new image, right?
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

hmm .. looks like im either too stupid for that or it doesnt work ..
converting the image seemed to work fine ..

Code: Select all

set giffile { R0lGODlhDw ..... }

image create photo imgobj -data $giffile
.c.img config -image imgobj
i put that set giffile right into the code instead of an external file, decimating eventual errors.. and what comes up next when running that code is:

Code: Select all

error reading color map
    while executing
"image create photo imgobj -data $giffile"
btw: typo in your code ( "image create photo imgobj -date $giffile" )
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Oops, -data would be correct. I'll update the post accordingly..

Did you do any kind of modifications of the generated code (set giffile {...)? The error suggests that the data is not read as valid base64-encoded data.

I did notice one thing while testing myself, you'll have to use fconfigure to set the -encoding of the input file to binary, or you'll notice the file being distorted/corrupted. Updating this in my previous example as well..
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

nope, i did not do any modifications to what the convertimg.tcl (thats how i named it) output. but ill try again with your updated code including fconfigure ^^ updating thread in a bit

//edit1:

WOHH!!
fconfigure gave me a ****LOAD of much more lines this time lol
going to import that piece into the main code now, brb ^^

//edit2:

DAMN! that worked hahaaaaaa :D

thx again for help nml ^^
this rocks big time :)
Post Reply