| View previous topic :: View next topic |
| Author |
Message |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Wed Feb 24, 2010 5:59 pm Post subject: [SOLVED] TK: include images in packages and use them |
|
|
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: |
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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Feb 24, 2010 6:24 pm Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Wed Feb 24, 2010 7:40 pm Post subject: |
|
|
please be so kind to post me code examples  |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Feb 24, 2010 8:05 pm Post subject: |
|
|
Well... in pure tcl-space it would look something like this:
| Code: | #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/embedding-file-executable-aka-hello-world-version-5967 _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Feb 24, 2010 8:26 pm Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Thu Feb 25, 2010 1:13 pm Post subject: |
|
|
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 ^^ |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Feb 25, 2010 2:15 pm Post subject: |
|
|
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: | 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Feb 25, 2010 5:25 pm Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Thu Feb 25, 2010 6:03 pm Post subject: |
|
|
More post-farming :p
Got the following working nicely:
| Code: | 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: | 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. _________________ NML_375, idling at #eggdrop@IrcNET
Last edited by nml375 on Fri Feb 26, 2010 2:54 pm; edited 1 time in total |
|
| Back to top |
|
 |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Fri Feb 26, 2010 10:12 am Post subject: |
|
|
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? |
|
| Back to top |
|
 |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Fri Feb 26, 2010 1:47 pm Post subject: |
|
|
hmm .. looks like im either too stupid for that or it doesnt work ..
converting the image seemed to work fine ..
| Code: |
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: |
error reading color map
while executing
"image create photo imgobj -data $giffile"
|
btw: typo in your code ( "image create photo imgobj -date $giffile" ) |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Feb 26, 2010 2:53 pm Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
raider2k Op
Joined: 01 Jan 2008 Posts: 140
|
Posted: Fri Feb 26, 2010 4:04 pm Post subject: |
|
|
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
thx again for help nml ^^
this rocks big time  |
|
| Back to top |
|
 |
|