[a881f6]: / contrib / brl / bbas / vidl2 / examples / vidl2_player_manager.cxx  Maximize  Restore  History

Download this file

327 lines (273 with data), 8.1 kB

  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
// This is brl/bbas/vidl2/examples/vidl2_player_manager.cxx
#include "vidl2_player_manager.h"
//:
// \file
// \author Matt Leotta
#include <vcl_iostream.h>
#include <vcl_cstdlib.h> // for vcl_exit()
#include <vul/vul_timer.h>
#include <vgui/vgui.h>
#include <vgui/vgui_dialog.h>
#include <vgui/vgui_error_dialog.h>
#include <vgui/vgui_shell_tableau.h>
#include <vgui/vgui_viewer2D_tableau.h>
#include <vgui/vgui_image_tableau.h>
#include <vil/vil_image_view.h>
#include <vidl2/vidl2_frame.h>
#include <vidl2/vidl2_convert.h>
#include <vidl2/gui/vidl2_gui_param_dialog.h>
//static manager instance
vidl2_player_manager *vidl2_player_manager::instance_ = 0;
//The vidl2_player_manager is a singleton class
vidl2_player_manager *vidl2_player_manager::instance()
{
if (!instance_)
{
instance_ = new vidl2_player_manager();
instance_->init();
}
return vidl2_player_manager::instance_;
}
void vidl2_player_manager::init()
{
// create the image tableau to hold the current frame
itab_ = vgui_image_tableau_new();
// create a 2D viewer tableau to allow for zooming, panning, etc.
v2D_ = vgui_viewer2D_tableau_new(itab_);
// add to a shell
vgui_shell_tableau_sptr shell = vgui_shell_tableau_new(v2D_);
this->add_child(shell);
}
//-----------------------------------------------------------
// constructors/destructor
// start with a single pane
vidl2_player_manager::vidl2_player_manager()
: vgui_wrapper_tableau(),
preload_frames_(false),
play_video_(false),
time_interval_(0.0f),
width_(640),
height_(480),
istream_(NULL),
ostream_(NULL),
win_(0)
{
}
vidl2_player_manager::~vidl2_player_manager()
{
}
// make an event handler
// note that we have to get an adaptor and set the tableau to receive events
bool vidl2_player_manager::handle(const vgui_event &e)
{
return this->child.handle(e);
}
//: clean up before the program terminates
void vidl2_player_manager::quit()
{
vcl_exit(1);
}
//-----------------------------------------------------------------------------
//: open an input video stream
//-----------------------------------------------------------------------------
void vidl2_player_manager::open_istream()
{
vidl2_istream* try_open = vidl2_gui_open_istream_dialog();
if (!try_open)
return;
istream_.reset(try_open);
if (istream_->is_open())
{
height_ = istream_->height();
width_ = istream_->width();
if (win_)
win_->reshape(width_+10, height_+60);
this->redraw();
}
}
//-----------------------------------------------------------------------------
//: close the input video stream
//-----------------------------------------------------------------------------
void vidl2_player_manager::close_istream()
{
if (istream_.get())
istream_->close();
this->redraw();
}
//-----------------------------------------------------------------------------
//: open an output video stream
//-----------------------------------------------------------------------------
void vidl2_player_manager::open_ostream()
{
vidl2_ostream* try_open = vidl2_gui_open_ostream_dialog();
if (!try_open)
return;
ostream_.reset(try_open);
}
//-----------------------------------------------------------------------------
//: close the output video stream
//-----------------------------------------------------------------------------
void vidl2_player_manager::close_ostream()
{
if (ostream_.get())
ostream_->close();
}
//-----------------------------------------------------------------------------
//: Pipe the input stream into the output stream
//-----------------------------------------------------------------------------
void vidl2_player_manager::pipe_streams()
{
if (!istream_.get() || !istream_->is_open()) {
vgui_error_dialog("Input stream not open");
return;
}
if (!ostream_.get()) {
vgui_error_dialog("Output stream not open");
return;
}
vgui_dialog dlg("Pipe Input Stream to Output Stream");
vcl_string description = "This will take images from the input stream\n";
description += "and pipe them into the output stream.\n";
description += "Then the output stream will be closed.\n";
dlg.message(description.c_str());
static int num_frames = -1;
dlg.field("Max number of frames",num_frames);
if (!dlg.ask())
return;
unsigned int initial_frame = istream_->frame_number();
if (initial_frame == static_cast<unsigned int>(-1))
istream_->advance();
vidl2_frame_sptr frame;
if (num_frames < 0)
while (bool(frame = istream_->current_frame()) &&
ostream_->write_frame(frame) )
if (!istream_->advance())
break;
else
for (int i=0; i<num_frames &&
bool(frame = istream_->current_frame()) &&
ostream_->write_frame(frame); ++i)
if (!istream_->advance())
break;
ostream_->close();
if (istream_->is_seekable())
istream_->seek_frame(initial_frame);
}
void vidl2_player_manager::redraw()
{
if (istream_.get()) {
unsigned int frame_num = istream_->frame_number();
if (frame_num == unsigned(-1))
vgui::out << "invalid frame\n";
else
{
vgui::out << "frame "<< frame_num+1 ;
int num_frames = istream_->num_frames();
if (num_frames >=0)
vgui::out << " of "<<num_frames<< vcl_endl;
else
vgui::out << vcl_endl;
}
vidl2_frame_sptr frame = istream_->current_frame();
if (!frame)
itab_->set_image_resource(NULL);
else if (frame->pixel_format() == VIDL2_PIXEL_FORMAT_MONO_16){
static vil_image_view<vxl_uint_16> img;
if (vidl2_convert_to_view(*frame,img))
itab_->set_image_view(img);
else
itab_->set_image_resource(NULL);
}
else {
static vil_image_view<vxl_byte> img;
if (vidl2_convert_to_view(*frame,img,VIDL2_PIXEL_COLOR_RGB))
itab_->set_image_view(img);
else
itab_->set_image_resource(NULL);
}
}
itab_->post_redraw();
vgui::run_till_idle();
}
// Play the video from the current frame until the end
// unless paused or stopped first
void vidl2_player_manager::play_video()
{
if (play_video_)
return;
if (!istream_.get()) {
vcl_cout << "No movie has been loaded\n";
return;
}
play_video_ = true;
vul_timer t,t2;
int count = 0;
while (play_video_ && istream_->is_valid() && istream_->advance())
{
this->redraw();
//Delay until the time interval has passed
while (t.all()<time_interval_) ;
t.mark();
++count;
}
long time = t2.all();
vcl_cout << "average play time " << double(time)/count << vcl_endl;
// if played to the end, go back to the first frame;
if (play_video_)
this->stop_video();
}
//Stop the video and return to the first frame
void vidl2_player_manager::stop_video()
{
play_video_ = false;
if (istream_.get() && istream_->is_seekable()) {
istream_->seek_frame(0);
this->redraw();
}
}
//Stop the video without returning to the first frame
void vidl2_player_manager::pause_video()
{
play_video_ = false;
}
//If the video is not playing bring up a dialog box
// and prompt for the frame number to jump to.
void vidl2_player_manager::go_to_frame()
{
if (!istream_.get())
return;
if (!istream_->is_seekable()) {
vcl_cerr << "This stream does not support seeking\n";
return;
}
if (play_video_)
return;
static int frame_num = 0;
vgui_dialog go_to_frame_dlg("Go to Frame");
go_to_frame_dlg.field("Frame Number", frame_num);
if (!go_to_frame_dlg.ask())
return;
if ( istream_->seek_frame(frame_num) ) {
this->redraw();
}
}
//If the video is not playing go to the next frame
void vidl2_player_manager::next_frame()
{
if (play_video_ || !istream_.get())
return;
if (istream_->advance()) {
this->redraw();
}
}
//If the video is not playing go to the previous frame
void vidl2_player_manager::prev_frame()
{
if (play_video_ || !istream_.get())
return;
int prev_frame = istream_->frame_number() - 1;
if (prev_frame >= 0) {
istream_->seek_frame(prev_frame);
this->redraw();
}
}