1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
(***********************************************************************)
(* *)
(* MLTk, Tcl/Tk interface of Objective Caml *)
(* *)
(* Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis *)
(* projet Cristal, INRIA Rocquencourt *)
(* Jacques Garrigue, Kyoto University RIMS *)
(* *)
(* Copyright 2002 Institut National de Recherche en Informatique et *)
(* en Automatique and Kyoto University. All rights reserved. *)
(* This file is distributed under the terms of the GNU Library *)
(* General Public License, with the special exception on linking *)
(* described in file LICENSE found in the Objective Caml source tree. *)
(* *)
(***********************************************************************)
open Camltk
open Widget
open Support
open Protocol
open Tkintf
external init : unit -> unit = "tkanim_init"
type gifFrame = {
imagephoto : imagePhoto;
frameWidth : int;
frameHeight : int;
left : int;
top : int;
delay : int
}
type animatedGif = {
frames : gifFrame list;
animWidth : int;
animHeight : int;
loop : int
}
type imageType =
| Still of Tk.options
| Animated of animatedGif
let debug = ref false
let cTKtoCAMLgifFrame s =
match splitlist s with
| [photo; width; height; left; top; delay] ->
{imagephoto = cTKtoCAMLimagePhoto photo;
frameWidth = int_of_string width;
frameHeight = int_of_string height;
left = int_of_string left;
top = int_of_string top;
delay = int_of_string delay}
| _ -> raise (Invalid_argument ("cTKtoCAMLgifFrame: " ^ s))
let cTKtoCAMLanimatedGif s =
match splitlist s with
| [width; height; frames; loop] ->
{frames = List.map cTKtoCAMLgifFrame (splitlist frames);
animWidth = int_of_string width;
animHeight = int_of_string height;
loop = int_of_string loop}
| _ -> raise (Invalid_argument ("cTKtoCAMLgifFrame: " ^ s))
(* check Tkanim package is in the interpreter *)
let available () =
let packages =
splitlist (Protocol.tkEval [| TkToken "package";
TkToken "names" |])
in
List.mem "Tkanim" packages
let create file =
let s =
Protocol.tkEval [| TkToken "animation";
TkToken "create";
TkToken file |]
in
let anmgif = cTKtoCAMLanimatedGif s in
match anmgif.frames with
| [] -> raise (TkError "Null frame in a gif ?")
| [x] -> Still (ImagePhoto x.imagephoto)
| _ -> Animated anmgif
let delete anim =
List.iter (fun {imagephoto = i} -> Imagephoto.delete i) anim.frames
let width anm = anm.animWidth
let height anm = anm.animHeight
let images anm = List.map (fun x -> x.imagephoto) anm.frames
let image_existence_check img =
(* I found there is a bug in Tk (even v8.0a2). *)
(* We can copy from deleted images, Tk never says "it doesn't exist", *)
(* But just do some operation. And sometimes it causes Seg-fault. *)
(* So, before using Imagephoto.copy, I should check the source image *)
(* really exists. *)
try ignore (Imagephoto.height img) with
TkError s -> prerr_endline ("tkanim: " ^ s); raise (TkError s)
let imagephoto_copy dst src opts =
image_existence_check src;
Imagephoto.copy dst src opts
let animate_gen w i anim =
let length = List.length anim.frames in
let frames = Array.of_list anim.frames in
let current = ref 0 in
let loop = ref anim.loop in
let f = frames.(!current) in
imagephoto_copy i f.imagephoto
[ImgTo (f.left, f.top, f.left + f.frameWidth,
f.top + f.frameHeight)];
let visible = ref true in
let animated = ref false in
let timer = ref None in
(* Loop *)
let display_current () =
let f = frames.(!current) in
imagephoto_copy i f.imagephoto
[ImgTo (f.left, f.top,
f.left + f.frameWidth, f.top + f.frameHeight)]
in
let rec tick () =
if not (Winfo.exists w && Winfo.viewable w) then begin
(* the widget is invisible. stop animation for efficiency *)
if !debug then prerr_endline "Stopped (Visibility)";
visible := false;
end else
begin
display_current ();
let t =
Timer.add (if f.delay = 0 then 100 else f.delay * 10)
(fun () ->
incr current;
if !current = length then begin
current := 0;
(* loop check *)
if !loop > 1 then begin
decr loop;
if !loop = 0 then begin
if !debug then prerr_endline "Loop end";
(* stop *)
loop := anim.loop;
timer := None
end
end
end;
tick ())
in
timer := Some t
end
in
let start () =
animated := true;
tick ()
in
let stop () =
match !timer with
| Some t ->
Timer.remove t;
timer := None;
animated := false
| None -> ()
in
let next () =
if !timer = None then begin
incr current;
if !current = length then current := 0;
display_current ()
end
in
(* We shouldn't delete the animation here. *)
(*
bind w [[], Destroy]
(BindSet ([], (fun _ -> Imagephoto.delete i)));
*)
bind w [[], Visibility]
(BindSet ([], (fun _ ->
if not !visible then begin
visible := true;
if !animated then start ()
end)));
(function
| false ->
if !animated then stop () else start ()
| true -> next ())
let animate label anim =
(* prerr_endline "animate"; *)
let i = Imagephoto.create [Width (Pixels anim.animWidth);
Height (Pixels anim.animHeight)]
in
bind label [[], Destroy] (BindExtend ([], (fun _ ->
Imagephoto.delete i)));
Label.configure label [ImagePhoto i];
animate_gen label i anim
let animate_canvas_item canvas tag anim =
(* prerr_endline "animate"; *)
let i = Imagephoto.create [Width (Pixels anim.animWidth);
Height (Pixels anim.animHeight)]
in
bind canvas [[], Destroy] (BindExtend ([], (fun _ ->
Imagephoto.delete i)));
Canvas.configure_image canvas tag [ImagePhoto i];
animate_gen canvas i anim
let gifdata s =
let tmp_dir = ref "/tmp" in
let mktemp =
let cnter = ref 0
and pid = Unix.getpid() in
(function prefx ->
incr cnter;
(Filename.concat !tmp_dir
(prefx ^ string_of_int pid ^ "." ^ string_of_int !cnter)))
in
let fname = mktemp "gifdata" in
let oc = open_out_bin fname in
try
output_string oc s;
close_out oc;
let anim = create fname in
Unix.unlink fname;
anim
with
e -> begin Unix.unlink fname; raise e end
|