/camera/imageviewer

To get this branch, use:
bzr branch http://darksoft.org/webbzr/camera/imageviewer
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/ruby
require 'fileutils'

require 'gtk2'
require 'gtkglext'
#require 'libglade2'

require 'RMagick'
include Magick

class Progress < Gtk::ProgressBar
 def initialize(parent, length, title)
    @length = length
    @iteration = 0
    super()
    #eventbox = Gtk::EventBox.new
    #eventbox.add(self)
    window = Gtk::Window.new(Gtk::Window::TOPLEVEL)
    window.title = title
    window.set_size_request(400, 30)
    window.transient_for=parent
    #window.add(eventbox)
    window.add(self)
    window.show_all
    window.modal = true
    window.deletable = false
    
    @window = window
 end
 
 def title=(title)
    @iteration = 0
    parent_window.title = title

    set_fraction(0)
#    if Gtk.events_pending?
#	while Gtk.main_iteration
#	end
#    end
 end
 
 def iteration()
    @iteration += 1
    set_fraction(Float(@iteration) / @length)

#    $block = true
#    while (Gtk.events_pending?)
#	puts 'new'
#	Gtk.main_iteration
#	puts 'done'
#    end
#    $block = false
 end
 
 def destroy
    @window.destroy
#    super
#    if Gtk.events_pending?
#	Gtk.main_iteration 
#    end
 end
end

class Player < Gtk::Builder
 attr_accessor :player
 
 def initialize(fps, width, height, images, real_frames)
    if images.class == Array
	@memory = true
	@images = images
	@writed  = false
    else
	@memory = false
	@images = Dir.glob(sprintf("saved_camera_images/%s/*.tif", images))
	@session = images

	pixbuf = Gdk::Pixbuf.new(@images[0])
	width = pixbuf.width
	height = pixbuf.height
	pixbuf = nil
    end

    @real_frames = real_frames

    size = @images.length * width * height / 1024 / 1024
    size = 1 if size < 1
    @size = (size < 4096)?sprintf("%i MB", size):sprintf("%i GB", (Float(size)/1024).round)
    
    @width = width
    @height = height
    @frame = 1
    @fps = fps
    @ms = 1000 / fps
    @playing = false

    super()
    
    add_from_file('player.glade')
    connect_signals { |handler| method(handler) }

    @player = get_object('player')

    if @memory then
	@player.title = sprintf("Current (%i frames, %s)", @images.length, @size)
    elsif @session =~ /^(\d{8}_\d{6})(\.(.+))?$/ then
        @player.title = sprintf("%s (%i frames, %s)", $2?$3:$1, @images.length, @size)
    else
	@player.title = sprintf("%s (%i frames, %s)", @session, @images.length, @size)
    end

    
    @area = get_object('draw')
    @area.set_gl_capability(Gdk::GLConfig.new(Gdk::GLConfig::MODE_DEPTH | Gdk::GLConfig::MODE_DOUBLE | Gdk::GLConfig::MODE_RGB))
    @area.set_size_request(width, height)
    
    Gtk::Drag.source_set(@area, 
	Gdk::Window::BUTTON1_MASK, 
	[["frame-number", Gtk::Drag::TARGET_SAME_APP, 1]],
	Gdk::DragContext::ACTION_COPY
    )

    from = get_object('from')
    to = get_object('to')

    @input_field = false
    @area.signal_connect("button-press-event") { |w, e|
	if (e.event_type == Gdk::Event::BUTTON2_PRESS) then
	    widget = @input_field?to:from
	    widget.text = sprintf("%i", @framesel.value)
	    @input_field = !@input_field
	end
    }

    [from, to].each { |widget|
	Gtk::Drag.dest_set(widget,
	    Gtk::Drag::DEST_DEFAULT_MOTION | Gtk::Drag::DEST_DEFAULT_HIGHLIGHT,
	    [["frame-number", Gtk::Drag::TARGET_SAME_APP, 1]], 
	    Gdk::DragContext::ACTION_COPY)

	widget.signal_connect("drag-drop") { |w, dc, x, y, time|
	    w.text = sprintf("%i", @framesel.value)
	}
    }

    @player.visible = true
    
    @region = Gdk::Rectangle.new(0, 0, width, height)
    
    @framesel = get_object('frame')
    @framesel.set_range(1, @images.length)
    @framesel.value = 1

    frame_value_changed_cb(@framesel)
 end

# Player window callbacks
 def frame_value_changed_cb(widget)
    @frame = widget.value
    @area.window.invalidate(@region, false)
 end
 
 def frame_format_value_cb(widget, digits)
    val = widget.value
    return @real_frames?sprintf('%i (%i)', val, @real_frames[val - 1]):sprintf('%i', val)
 end

 def play_clicked_cb(widget)
    get_object('play').sensitive = false
    @play = true
    @playing = true
    
    @frame = 1 if @frame == @images.length
    
    GLib::Timeout.add(@ms) {
	if (@play) and (@frame < @images.length) then
	    @framesel.value = @frame + 1
	    true
	else
	    get_object('play').sensitive = true
	    @playing = false
	    false
	end
    }
    
 end
 
 def forward_clicked_cb(widget)
    if (@frame < @images.length) then
	@framesel.value = @frame + 1
    else
	@framesel.value = 0
    end
 end
 
 def back_clicked_cb(widget)
    if (@frame > 0) then
	@framesel.value = @frame - 1
    else
	@framesel.value = @images.length - 1
    end
 end
 
 def pause_clicked_cb(widget)
    @play = false
 end

 def draw_expose_event_cb(area, event)
    if @memory then
	Gdk::RGB.draw_gray_image(area.window, area.style.fg_gc(area.state), 0, 0, @width, @height, Gdk::RGB::DITHER_NORMAL, @images[@frame - 1], @width)
    else
#	image = ImageList.new(@images[@frame - 1]).first
	pixbuf = Gdk::Pixbuf.new(@images[@frame - 1], @width, @height)
	@area.window.draw_pixbuf(nil, pixbuf, 0, 0, 0, 0, @width, @height, Gdk::RGB::DITHER_NORMAL, 0, 0)
    end
 end

 def save_images(name, progress)
    if @memory and not @writed then
	geometry = Magick::Geometry.new(@width, @height, nil, nil, Magick::AspectGeometry)

	time = Time.now
	basename = (name =~ /([^\/]+)\.avi$/i)?$1:name
	@session = sprintf("%02i%02i%02i_%02i%02i%02i.", time.year, time.month, time.day, time.hour, time.min, time.sec) + basename
	FileUtils::mkdir_p('saved_camera_images/' + @session)
	
	key = 0
    	@images.each { |data|
	    img = Image.from_blob(data) {
		self.format = "GRAY"
	    	self.depth = 8
		self.size = geometry
	    }[0]
		
	    key += 1	
	    img.write(sprintf("saved_camera_images/%s/PIC%09i.tif", @session, key))
	    img.destroy!

	    progress.iteration if progress
	}	 

	@writed = true
    end
 end

 def save(name = nil)
    if @memory and not @writed then
	if not name then
            fs = Gtk::Dialog.new("Please select a name", @player, Gtk::Dialog::DESTROY_WITH_PARENT,
	        [ Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL ],
		[ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT ]
	    )

	    entry = Gtk::Entry.new	
	    fs.vbox.add(entry)
	    fs.show_all
	
	    response = fs.run
	    name = entry.text if response == Gtk::Dialog::RESPONSE_ACCEPT
	    fs.destroy
	end
	
	if name then 
    	    progress = Progress.new(@player, @images.length, sprintf("Saving %i images (%s)", @images.length, @size))
	
	    thr = Thread.start {
		save_images(name, progress)

		Gtk.main_quit
	    }
	    Gtk.main
	    thr.join
	
	    progress.destroy
	end
    end
 end
 
 def save_clicked_cb(widget)
    save
 end
 
 def video_clicked_cb(widget)
    FileUtils::mkdir_p("saved_camera_videos")

    filter = Gtk::FileFilter.new
    filter.name = "Video Files"
    filter.add_pattern('*.avi')
    
    fs = Gtk::FileChooserDialog.new("Please select a file to save", nil, Gtk::FileChooser::ACTION_SAVE, nil, 
	[ Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL ],
	[ Gtk::Stock::SAVE, Gtk::Dialog::RESPONSE_ACCEPT ]
    )
    fs.current_folder = 'saved_camera_videos'
    fs.add_filter(filter)

    response = fs.run
    name = fs.filename
    fs.destroy

    if response == Gtk::Dialog::RESPONSE_ACCEPT
	name = name + '.avi' if name !~ /\.avi$/i

	progress = Progress.new(@player, @images.length, sprintf("Saving %i images (%s)", @images.length, @size))
	
	thr = Thread.start {
	    save_images(name, progress)
	
	    FileUtils::mkdir_p("temporary_camera_images/" + @session)
	
	    progress.title = sprintf("Converting %i images (%s)", @images.length, @size)
	    
	    Dir.glob(sprintf("saved_camera_images/%s/*.tif", @session)) { |image_name|
		output = image_name.sub(/saved_camera_images/, "temporary_camera_images")
		output.sub!(/tif$/, "bmp")
	    
		image = Magick::ImageList.new(image_name)
		image.write(output)
		image.destroy!
	    
		progress.iteration
	    }
	    
	    progress.title = sprintf("Encoding %i images (%s)", @images.length, @size)
	    mplayer = open("|mencoder mf://temporary_camera_images/#{@session}/*.bmp -mf fps=#{@fps}:type=bmp -ovc x264 -x264encopts bitrate=3000 -o #{name} 2>/dev/null") { |pipe|
		pipe.each("r") { |line|
		    progress.set_fraction(Float($1)/100) if line =~ /Pos:[^(]*\(\s*(\d+)%\)/
		}
		
	    }
	    
	    progress.title = "Cleaning temporary files"
	    FileUtils::remove_dir("temporary_camera_images/" + @session)
	    
	    #system "./encode.sh \"#{name}\" \"#{@session}\" #{@fps}"
	    Gtk.main_quit
	}
	Gtk.main
	thr.join
	
	progress.destroy
    end
 end

 def cut_clicked_cb(widget)
    begin
	from = Integer(get_object('from').text)
    rescue 
	from = 0
    end
	
    begin
	to = Integer(get_object('to').text)
    rescue 
	to = @images.length - 1
    end
	
    from = 0 if (from < 0)
    to = @images.length -1 if (to <= 0) or (to >= @images.length)

    if @memory then
	Player.new(@fps, @width, @height, @images[from..to], (@real_frames)?(@real_frames[from..to]):nil)
    else
	name = nil
	
        fs = Gtk::Dialog.new("Please select a name", @player, Gtk::Dialog::DESTROY_WITH_PARENT,
	    [ Gtk::Stock::CANCEL, Gtk::Dialog::RESPONSE_CANCEL ],
	    [ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_ACCEPT ]
	)

	entry = Gtk::Entry.new
	fs.vbox.add(entry)
	fs.show_all
	
	response = fs.run
	name = entry.text if response == Gtk::Dialog::RESPONSE_ACCEPT
	fs.destroy

	time = Time.now
	if name then
	    session = sprintf("%02i%02i%02i_%02i%02i%02i.", time.year, time.month, time.day, time.hour, time.min, time.sec) + name
	else
	    session = sprintf("%02i%02i%02i_%02i%02i%02i", time.year, time.month, time.day, time.hour, time.min, time.sec)
	end
	    
	FileUtils::mkdir_p('saved_camera_images/' + session)
	
	key = 0
	@images[from..to].each { |src|
	    key += 1
	    dst = sprintf("saved_camera_images/%s/PIC%09i.tif", session, key)
	    FileUtils::ln(src, dst)
	}
	
	Player.new(@fps, @width, @height, session, (@real_frames)?(@real_frames[from..to]):nil)
    end
 end
 
 def gtk_widget_delete(widget, ev)
    @play = false

    while @playing 
	Gtk.main_iteration	
    end

    # Generally we should somehow clean complete object, do it later
    @images = nil
    @real_frames = nil
    GC.start
    
#    @player.destroy

    return false
 end

 def gtk_widget_destroy(widget)
 end
end

if $*.length > 0 then
    player = Player.new(25, -1, -1, $*[0], nil)
    
    #metaclass = class << player; self; end
    #metaclass.send(:define_method, :gtk_widget_destroy) { Gtk.main_quit }
    #player.gtk_widget_destroy(nil)

    player.player.signal_connect('destroy') { Gtk.main_quit }
    Gtk.main
end