07-22-2017, 00:38 +0200
AuthorPost
SIYB  08-02-2007, 21:18   | [TCL/TK] - gif-2-base64 / base64-2-gif Jabber  Homepage
Member since 06/2007
8 Posts
A TCL/TK program to convert gif images to base64 and reverse base64 to gif.

                                                                                                                              
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/wish

#=======================================================
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#=======================================================
#
# DEPENDENCIES
#
#    - TCL8.4
#    - TK8.4
#    - TCLLIB
#
# VERSION HISTORY
#
#    1.0
#        - Basic gui
#
#    1.1
#        - Added scrollbar
#        - Added clear button
#        - Added initial file in save function
#
#    1.2
#        - Added help button
#        - Added preview button
#        - Moved buttons to the left
#        - Added routine to decode base64 back to gif
#
#    1.3
#        - Added load base64 file button
#        - Added quit button
#        - Fixed issue with multible help and preview windows
#        - Altered design
#        - Removed ::out
#
#        Known issues: Program might crash if the user attempts to load a non text file as base64 file!
#        Please report bugs if you encounter any. (suggestions are welcome as well :>)
#
#=======================================================
#
# Contact: SIYB (siyb@geekosphere.org) - http://geekosphere.org - irc://siyb.de/woot
#
#=======================================================
package require base64

#
# This proc was written by Jeff Godfrey and taken from http://wiki.tcl.tk/2897
#
proc gif2base64:encode {file} {
    if {$file == ""} { return }
    set fileID [open $file RDONLY]
    fconfigure $fileID -translation binary
    set rawData [read $fileID]
    close $fileID
    set encodedData [base64::encode $rawData]
    return $encodedData
}

#
# decode base64 to gif
#
proc gif2base64:decode {data ofile} {
    if {$ofile == ""} { return }
    set fl [open $ofile a+]; fconfigure $fl -translation binary;puts $fl [base64::decode $data];close $fl
}

#
# info popup
#
proc gif2base64:info {window info} {
    if {[tk_messageBox -title "INFO!" \
            -parent $window \
            -type ok \
            -icon info \
            -message "INFO: $info"] == "ok"} {
        return
    }
}

#
# gui
#
set helptext {
    Information: The gif2base64 program is capable of converting gif images to base64 and reversing base64 back to gif, in order to recover images from sourcecode.

    gif to base64:
    Press the "Gif to base64 convertion" button and choose a gif file you wish to convert into base64. You can now take a look at what your image would look like in an
    application, by pressing the "Preview image" button. If you are happy with the results, you may save the generated base64 code to a file by pressing the "Save result to file"
    button.

    base64 to gif:
    Paste the base64 string into the program's textbox or load a base64 file by using the "load base64 file" button and press the "Base64 to gif convertion" button, after that a
    save as box should appear, the gif file will be written as soon as you choose a file to save the picture to.

    Author: SIYB (siyb@geekosphere.org) - teh geekosphere (http://geekosphere.org) 2007

    This program can be distributed under the terms of the GPL v2
}

proc gif2base64:gui {} {
    global helptext
    wm title . "gif2base64"
    wm resizable . 0 0

    set ::types {
        {{GIF Files} {.gif}}
        {{GIF Files} {} GIFF}
    }

    pack [frame .f1] -fill x -expand 1 -side left -anchor n

    #
    # GIF
    #
    pack [labelframe .f1.lf1 -text "GIF"] -expand 1 -fill x

    pack [button .f1.lf1.b1 -text "Gif to base64 convertion" \
        -command { set ::file [tk_getOpenFile -filetypes $::types -parent .]; .f2.t1 delete 0.0 end; .f2.t1 insert end [gif2base64:encode $::file]}
    ] -fill x -expand 1

    pack [button .f1.lf1.b2 -text "Save result to file" \
        -command {
        if {[string trim [gif2base64:encode $::file]] == ""} { gif2base64:info . "There is no data to be saved yet"; return }
        set fl [tk_getSaveFile -initialfile [lindex [split [file tail $::file] .] 0].out]; if {$fl == ""} { return }; set fl [open $fl a+]; puts $fl [gif2base64:encode $::file]; close $fl
    }
    ] -fill x -expand 1

    #
    # BASE 64
    #
    pack [labelframe .f1.lf2 -text "BASE64"] -expand 1 -fill x

    pack [button .f1.lf2.b3 -text "Load base64 file" \
        -command {
        set file [tk_getOpenFile]
        if {$file == ""} { return }
        set fl [open $file r]; set data [read $fl]; close $fl;
        .f2.t1 delete 0.0 end; set ::out $data
        .f2.t1 insert end $data
    }
    ] -expand 1 -fill x

    pack [button .f1.lf2.b4 -text "Base64 to gif convertion" \
        -command {
        set fl [tk_getSaveFile -filetypes $::types]
        if {$fl == ""} { return }
        gif2base64:decode [.f2.t1 get 0.0 end] $fl
    }
    ] -expand 1 -fill x

    #
    # MISC
    #
    pack [labelframe .f1.lf3 -text "MISC"] -expand 1 -fill x

    pack [button .f1.lf3.b5 -text "Clear" \
        -command { set ::out ""; .f2.t1 delete 0.0 end }
    ] -expand 1 -fill x

    pack [button .f1.lf3.b6 -text "Preview image" \
        -command {
        if {[string trim [.f2.t1 get 0.0 end]] == "" || [winfo exists .preview]} { return }
        toplevel .preview
        wm resizable .preview 0 0
        if {[catch {set img [image create photo -data [.f2.t1 get 0.0 end]]}]} { destroy .preview; gif2base64:info . "Image can't be previewed, this might be due to broken base64 data."; return }
        pack [label .preview.img -image $img]
    }
    ] -expand 1 -fill x

    pack [button .f1.lf3.b7 -text "Help" \
        -command {
        if {[winfo exists .help]} { return }
        toplevel .help
        wm title .help "gif2base64 Help"
        wm resizable .help 0 0
        pack [label .help.l1 -justify left \
            -text $helptext
        ]
        pack [button .help.b1 -text "Close help" \
            -command {
            destroy .help
        }
        ]
    }
    ] -expand 1 -fill x

    pack [button .f1.lf3.b8 -text "Quit" \
        -command { exit }
    ] -expand 1 -fill x

    pack [frame .f2]
    pack [scrollbar .f2.s1 -command {.f2.t1 yview}] -side right -expand 1 -fill y
    pack [text .f2.t1 -yscrollcommand ".f2.s1 set" -background white] -side left -expand 1

    pack [label .l1 -text "Use ctrl+c to copy the content from the textbox."]
}

gif2base64:gui

  The user has attached a file: screen.gif (Save, 441,984 Bytes, downloaded 100 times)
Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen