~ubuntu-branches/ubuntu/gutsy/amsn/gutsy

« back to all changes in this revision

Viewing changes to utils/TkCximage/demos/demo.tcl

  • Committer: Bazaar Package Importer
  • Author(s): Theodore Karkoulis
  • Date: 2006-01-04 15:26:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060104152602-ipe1yg00rl3nlklv
Tags: 0.95-1
New Upstream Release (closes: #345052, #278575).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/wish
 
2
 
 
3
set dir [file dirname [info script]]
 
4
lappend auto_path "./"
 
5
lappend auto_path "../"
 
6
lappend auto_path "utils/TkCximage/"
 
7
catch {package require TkCximage}
 
8
 
 
9
set ::tcl_interactive 1
 
10
 
 
11
proc CheckLoaded { } {
 
12
 
 
13
        foreach lib [info loaded] {
 
14
                if { [lindex $lib 1] == "Tkcximage" } {
 
15
                        return 1
 
16
                } 
 
17
        }
 
18
        return 0
 
19
}
 
20
proc chooseFileDialog { {title ""} {operation "open"} } {
 
21
 
 
22
        set initialfile ""
 
23
        set starting_dir $::start
 
24
        set parent "."
 
25
        set types {{ "Image file" {*.jpg *.gif *.png *.tga} } {"All Files" {*}}}
 
26
        
 
27
        if { $operation == "open" } {
 
28
                set selfile [tk_getOpenFile -filetypes $types -parent $parent -initialdir $starting_dir -initialfile $initialfile -title $title]
 
29
        } else {
 
30
                set selfile [tk_getSaveFile -filetypes $types -parent $parent -initialdir $starting_dir -initialfile $initialfile -title $title]
 
31
        }
 
32
 
 
33
        if { $selfile != "" } {
 
34
                set ::start [file dirname $selfile]
 
35
        }
 
36
 
 
37
        return $selfile
 
38
}
 
39
 
 
40
proc msg_box { msg } {
 
41
        toplevel .error 
 
42
        label .error.l -text "$msg"
 
43
        button .error.ok -text "Ok" -command "grab release .error; destroy .error"
 
44
        pack .error.l .error.ok -side top
 
45
        grab set .error
 
46
}
 
47
 
 
48
proc Load { } {
 
49
        set file [chooseFileDialog "Open Image" "open"]
 
50
        if { $file == "" }  { return } 
 
51
 
 
52
        if { [catch {image create photo -file $file} img] != 0} {
 
53
                msg_box "Error opening the file you selected : \n$img"
 
54
                return
 
55
        }
 
56
 
 
57
        ShowImage $img
 
58
 
 
59
}
 
60
 
 
61
proc Save { } {
 
62
 
 
63
        if { $::loaded == 0 } {
 
64
                msg_box "You must first load an image before using this function"
 
65
                return
 
66
        }
 
67
 
 
68
        toplevel .format 
 
69
        radiobutton .format.gif -text "GIF File" -variable ::format -value "cxgif"
 
70
        radiobutton .format.png -text "PNG File" -variable ::format -value "cxpng"
 
71
        radiobutton .format.jpg -text "JPG File" -variable ::format -value "cxjpg"
 
72
        radiobutton .format.tga -text "TGA File" -variable ::format -value "cxtga"
 
73
        radiobutton .format.default -text "Choose from file extension" -variable ::format -value "cximage"      
 
74
        label .format.separator -text " "
 
75
        button .format.ok -text "Save" -command "grab release .format; destroy .format; Save2"
 
76
        pack .format.gif .format.png .format.jpg .format.tga .format.default .format.separator .format.ok -side top
 
77
        grab set .format
 
78
}
 
79
 
 
80
proc Save2 { } {
 
81
 
 
82
        set file [chooseFileDialog "Save Image" "save"]
 
83
        if { $file == "" }  { return } 
 
84
 
 
85
        if { [catch {$::loaded write $file -format $::format} file] != 0} {
 
86
                msg_box "Error Saving to the file you requested : \n$file"
 
87
                return
 
88
        }
 
89
 
 
90
}
 
91
 
 
92
proc Convert { } {
 
93
 
 
94
        msg_box "Please load the image to convert"
 
95
 
 
96
        tkwait window .error
 
97
 
 
98
        set file1 [chooseFileDialog "Open Image" "open"]
 
99
        if { $file1 == "" }  { return } 
 
100
 
 
101
        msg_box "Please Choose the destination file (filetype is determined by file extension)"
 
102
 
 
103
        tkwait window .error
 
104
 
 
105
        set file2 [chooseFileDialog "Save Image" "save"]
 
106
        if { $file2 == "" }  { return } 
 
107
 
 
108
        if { [catch {::CxImage::Convert $file1 $file2} res] != 0} {
 
109
                msg_box "Error opening the file you selected : \n$res"
 
110
                return
 
111
        }
 
112
 
 
113
}
 
114
 
 
115
proc Resize { } {
 
116
        if { $::loaded == 0 } {
 
117
                msg_box "You must first load an image before using this function"
 
118
                return
 
119
        }
 
120
 
 
121
        toplevel .resize
 
122
        frame .resize.f1
 
123
        label .resize.f1.lw -text "Width : "
 
124
        entry .resize.f1.w 
 
125
        frame .resize.f2
 
126
        label .resize.f2.lh -text "Height : "
 
127
        entry .resize.f2.h
 
128
        frame .resize.f3
 
129
        button .resize.f3.ok -text "Resize" -command "Resize2"
 
130
        button .resize.f3.cancel -text "Cancel" -command "grab release .resize; destroy .resize"
 
131
 
 
132
        pack .resize.f1.lw .resize.f1.w -side left
 
133
        pack .resize.f2.lh .resize.f2.h -side left
 
134
        pack .resize.f3.ok .resize.f3.cancel -side left
 
135
        pack .resize.f1 .resize.f2 .resize.f3 -side top
 
136
        grab set .resize
 
137
}
 
138
 
 
139
proc Resize2 { } {
 
140
        set w [.resize.f1.w get]
 
141
        set h [.resize.f2.h get]
 
142
        grab release .resize
 
143
        destroy .resize
 
144
 
 
145
        ::CxImage::Resize $::loaded $w $h
 
146
 
 
147
}
 
148
 
 
149
 
 
150
proc Thumbnail { } {
 
151
        if { $::loaded == 0 } {
 
152
                msg_box "You must first load an image before using this function"
 
153
                return
 
154
        }
 
155
        toplevel .thumb
 
156
        frame .thumb.f1
 
157
        label .thumb.f1.lw -text "Width : "
 
158
        entry .thumb.f1.w 
 
159
        frame .thumb.f2
 
160
        label .thumb.f2.lh -text "Height : "
 
161
        entry .thumb.f2.h
 
162
        
 
163
        frame .thumb.f3
 
164
        label .thumb.f3.lc -text "Border Color : "
 
165
        entry .thumb.f3.c 
 
166
        checkbutton .thumb.alpha -text "Enable Alpha on border" -variable ::alpha -onvalue 1 -offvalue 0
 
167
        frame .thumb.f4
 
168
        label .thumb.f4.la -text "Alpha opacity for border (between 0 and 255)"
 
169
        entry .thumb.f4.a
 
170
 
 
171
        frame .thumb.f5
 
172
        button .thumb.f5.ok -text "Resize" -command "Thumbnail2"
 
173
        button .thumb.f5.cancel -text "Cancel" -command "grab release .thumb; destroy .thumb"
 
174
 
 
175
        pack .thumb.f1.lw .thumb.f1.w -side left
 
176
        pack .thumb.f2.lh .thumb.f2.h -side left
 
177
        pack .thumb.f3.lc .thumb.f3.c -side left
 
178
        pack .thumb.f4.la .thumb.f4.a -side left
 
179
        pack .thumb.f5.ok .thumb.f5.cancel -side left
 
180
 
 
181
        pack .thumb.f1 .thumb.f2 .thumb.f3 .thumb.alpha .thumb.f4 .thumb.f5 -side top
 
182
        grab set .thumb
 
183
 
 
184
}
 
185
 
 
186
proc Thumbnail2 { } {
 
187
        set w [.thumb.f1.w get]
 
188
        set h [.thumb.f2.h get]
 
189
        set border [.thumb.f3.c get]
 
190
        set alpha [.thumb.f4.a get]
 
191
        grab release .thumb
 
192
        destroy .thumb
 
193
 
 
194
        if { $::alpha == 0 } {
 
195
                if { [catch {::CxImage::Thumbnail $::loaded $w $h $border} res] != 0 } {
 
196
                        msg_box "Unable to create thumbnail\n$res"
 
197
                }
 
198
        } else {
 
199
                if { [catch {::CxImage::Thumbnail $::loaded $w $h $border -alpha $alpha} res ] != 0 } {
 
200
                        msg_box "Unable to create thumbnail\n$res"
 
201
                }
 
202
        }
 
203
}
 
204
 
 
205
proc Crop { } {
 
206
        if { $::loaded == 0 } {
 
207
                msg_box "You must first load an image before using this function"
 
208
                return
 
209
        }
 
210
        toplevel .cr
 
211
        frame .cr.f1
 
212
        label .cr.f1.lx -text "Starting X : "
 
213
        entry .cr.f1.x1 
 
214
        frame .cr.f2
 
215
        label .cr.f2.ly -text "Starting Y : "
 
216
        entry .cr.f2.y1
 
217
        frame .cr.f3
 
218
        label .cr.f3.lx -text "Ending X : "
 
219
        entry .cr.f3.x2
 
220
        frame .cr.f4
 
221
        label .cr.f4.ly -text "Ending Y : "
 
222
        entry .cr.f4.y2
 
223
 
 
224
        frame .cr.f5
 
225
        button .cr.f5.ok -text "Crop" -command "Crop2"
 
226
        button .cr.f5.cancel -text "Cancel" -command "grab release .cr; destroy .cr"
 
227
 
 
228
        pack .cr.f1.lx .cr.f1.x1 -side left
 
229
        pack .cr.f2.ly .cr.f2.y1 -side left
 
230
        pack .cr.f3.lx .cr.f3.x2 -side left
 
231
        pack .cr.f4.ly .cr.f4.y2 -side left
 
232
        pack .cr.f5.ok .cr.f5.cancel -side left
 
233
 
 
234
        pack .cr.f1 .cr.f2 .cr.f3 .cr.f4 .cr.f5 -side top
 
235
        grab set .cr
 
236
}
 
237
 
 
238
proc Crop2 { } {
 
239
        set x1 [.cr.f1.x1 get]
 
240
        set y1 [.cr.f2.y1 get]
 
241
        set x2 [.cr.f3.x2 get]
 
242
        set y2 [.cr.f4.y2 get]
 
243
 
 
244
        grab release .cr
 
245
        destroy .cr
 
246
 
 
247
        set temp [image create photo]
 
248
 
 
249
        if { [catch {$temp copy $::loaded -from $x1 $y1 $x2 $y2} res ] != 0 } {
 
250
                msg_box "Unable to crop image\n$res"
 
251
                return
 
252
        }
 
253
        
 
254
        ShowImage $temp
 
255
 
 
256
}
 
257
 
 
258
proc Colorize { } {
 
259
        if { $::loaded == 0 } {
 
260
                msg_box "You must first load an image before using this function"
 
261
                return
 
262
        }
 
263
        toplevel .color
 
264
        
 
265
        frame .color.f1
 
266
        label .color.f1.lc -text "Color : "
 
267
        entry .color.f1.c 
 
268
 
 
269
        frame .color.f2
 
270
        button .color.f2.ok -text "Colorize" -command "Colorize2"
 
271
        button .color.f2.cancel -text "Cancel" -command "grab release .color; destroy .color"
 
272
 
 
273
        pack .color.f1.lc .color.f1.c -side left
 
274
        pack .color.f2.ok .color.f2.cancel -side left
 
275
 
 
276
        pack .color.f1 .color.f2 -side top
 
277
        grab set .color
 
278
 
 
279
}
 
280
 
 
281
proc Colorize2 { } {
 
282
        set color [.color.f1.c get]
 
283
        grab release .color
 
284
        destroy .color
 
285
 
 
286
        if { [catch {::CxImage::Colorize $::loaded $color} res ] != 0 } {
 
287
                        msg_box "Unable to create thumbnail\n$res"
 
288
        }
 
289
}
 
290
 
 
291
proc ShowImage { img } {
 
292
        catch {
 
293
                if { $::loaded != 0 } {
 
294
                        image delete $::loaded
 
295
                }
 
296
                destroy .image
 
297
                
 
298
        }
 
299
 
 
300
        set ::loaded $img
 
301
        toplevel .image 
 
302
        label .image.img -image $::loaded
 
303
        pack .image.img
 
304
        
 
305
}
 
306
 
 
307
proc Blending { } {
 
308
 
 
309
        catch {destroy .alpha}
 
310
 
 
311
        msg_box "Please load an image that has Alpha blending enabled"
 
312
 
 
313
        tkwait window .error
 
314
 
 
315
        set file [chooseFileDialog "Open Image" "open"]
 
316
        if { $file == "" }  { return } 
 
317
 
 
318
        if { [catch {image create photo -file $file} img1] != 0} {
 
319
                msg_box "Error opening the file you selected : \n$img1"
 
320
                return
 
321
        }
 
322
 
 
323
        msg_box "Please load the background image"
 
324
 
 
325
        tkwait window .error
 
326
 
 
327
        set file2 [chooseFileDialog "Open Image" "open"]
 
328
        if { $file2 == "" }  { return } 
 
329
 
 
330
        if { [catch {image create photo -file $file2} img2] != 0} {
 
331
                msg_box "Error opening the file you selected : \n$img2"
 
332
                return
 
333
        }
 
334
 
 
335
        set w1 [image width $img1]
 
336
        set h1 [image height $img1]
 
337
        set w2 [image width $img2]
 
338
        set h2 [image height $img2]
 
339
 
 
340
        toplevel .alpha
 
341
        canvas .alpha.c -width [expr $w1 > $w2 ? $w1 : $w2] -height [expr $h1 > $h2 ? $h1 : $h2]
 
342
 
 
343
        .alpha.c create image [expr $w2 / 2] [expr $h2 / 2] -image $img2
 
344
        .alpha.c create image [expr $w1 / 2] [expr $h1 / 2] -image $img1
 
345
        pack .alpha.c
 
346
        
 
347
#       bind .alpha <Destroy> "image delete $img1; image delete $img2"
 
348
 
 
349
}
 
350
 
 
351
 
 
352
 
 
353
if { [CheckLoaded] == 0 } {
 
354
        catch {load ../TkCximage[info shared]}
 
355
        catch {load ./TkCximage[info shared]}
 
356
        if {[CheckLoaded] == 0 } {
 
357
                puts "Can't find the extension, please type \"make\" to compile it before testing it"
 
358
                exit
 
359
        }
 
360
}
 
361
 
 
362
 
 
363
cd $dir
 
364
set loaded 0
 
365
set ::start [pwd]
 
366
set ::format "cximage"
 
367
set ::alpha 0
 
368
 
 
369
wm title . "Commands"
 
370
button .load -text "Load" -command "Load"
 
371
button .save -text "Save" -command "Save"
 
372
button .play -text "Play" -command "::CxImage::EnableAnimated"
 
373
button .pause -text "Pause" -command "::CxImage::DisableAnimated"
 
374
button .convert -text "Convert" -command "Convert"
 
375
button .res -text "Resize" -command "Resize"
 
376
button .thumbnail -text "Thumbnail" -command "Thumbnail"
 
377
button .colorize -text "Colorize" -command "Colorize"
 
378
button .crop -text "Crop" -command "Crop"
 
379
button .blending -text "Test Alpha blending" -command "Blending"
 
380
button .exit -text "Exit" -command "exit"
 
381
 
 
382
pack .load .save .play .pause .convert .res .thumbnail .colorize .crop .blending .exit -side top
 
383
 
 
384