Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_Disk.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 #ifndef WIN32
25 #include <unistd.h>
26 #endif //WIN32
27 #include <Base/Common/W32Compat.hh>
28 
29 #include <string>
30 #include <sstream>
31 #include <iostream>
32 
33 #include "VideoSource_Disk.hh"
34 #include <Base/Image/ImageIO.hh>
35 #include <Base/Image/ImageAttributes.hh>
36 #include <Image/Camera.hh>
37 #ifdef BIAS_HAVE_FFMPEG
38 # include <VideoSource/VideoSource_FFmpeg.hh>
39 #endif
40 #include <Base/Common/BIASpragma.hh>
41 
42 using namespace BIAS;
43 using namespace std;
44 
46 {
47  ActiveFrame_ = 0;
48  SetDebugLevel(0);
50  KeepFPS_ = true;
51  Loop_ = true;
52  bReverse_ = false;
53  bReversePlaying_ = false;
54  bUseMetaData_ = false;
56  GenerateNewUIDs_ = true;
57  bVideo_ = false;
58 }
59 
60 
62 {
63 }
64 
65 
66 int VideoSource_Disk::OpenDevice(string Prefix, string Postfix, int DigitCount, int PictureCount, int startCount)
67 {
68  vector<string> FileNames;
69  ostringstream Name;
70 
71  for (int i = startCount; i< PictureCount; i++){
72  Name.str(""); // reset stringstream
73  Name << Prefix << setfill('0') << setw(DigitCount) << i << Postfix;
74  FileNames.push_back(Name.str());
75  BIASDOUT(D_VSO_DISK,i<<"\t"<< Name.str().c_str());
76  }
77  BIASDOUT(D_VSO_DISK,"leaving VideoSource_Disk::OpenDevice(string .. )");
78  return OpenDevice(FileNames);
79 }
80 
81 
82 int VideoSource_Disk::OpenDevice(const vector<string> &FileNames)
83 {
84 // BIASDOUT(D_VSO_DISK,"entering VideoSource_Disk::OpenDevice(vector<string>)");
85 // Camera<unsigned char> Image;
86  // load first image and initialize some internal variables
88  BIASDOUT(D_VSO_DISK,"FileNames[0] : "<<FileNames[0]);
89  // Image.SetDebugLevel(D_COPYIN | D_CONVERT);
90  if (FileNames.empty()){
91  BEXCEPTION("VideoSource_Disk::OpenDevice(): empty argument vector");
92  return -2;
93  }
94  if (ImageIO::Load(FileNames[0], Image)!=0){
95 #ifdef BIAS_HAVE_FFMPEG
96  if (Video_.OpenDevice(FileNames[0].c_str())==0)
97  {
98  bVideo_ = true;
99  Width_ = Video_.GetWidth();
100  Height_ = Video_.GetHeight();
103  BytesPerPixel_ = 1.0;
104 
105  CompleteInitialized_ = true;
106  ActiveFrame_ = 0;
107  return 0;
108  }
109 #endif
110  cout<<"pwd: ";
111  int r = system("pwd");
112  if (r==-1){ BIASERR("error getting pwd"); }
113  BIASERR("unable to read "<<FileNames[0]);
114  BIASBREAK;
115  return -1;
116  }
117 
118  ImageAttributes attr;
119  attr.InitExifTags();
120  attr.Read(FileNames[0]);
121  Identifier_ = attr.GetIdentifier();
122 
123  Width_ = Image.GetWidth();
124  Height_ = Image.GetHeight();
126  ColorMode_ = Image.GetColorModel();
127  BytesPerPixel_ = Image.GetDepth();
128  if (FileNames_.size() != 0)
129  FileNames_.erase(FileNames_.begin(), FileNames_.end());
130 
131  for (vector<string>::const_iterator i = FileNames.begin();
132  i != FileNames.end();i++)
133  {
134  FileNames_.push_back(*i);
135 
136 #if 0
137  const char *fName2 =(*i).c_str();
138  //ImageIO::Load(fName2,Image);
139  ImageIO::Load(fName2,Image);
140  if (i != FileNames.begin()){
141  if (Width_ != Image.GetWidth() || Height_ != Image.GetHeight() ||
142  ColorChannels_ != Image.GetChannelCount() ){
143  BIASERR("VideoSource_Disk::OpenDevice() Image is "<<*i<<
144  " different from first image in sequence");
145  return -1;
146  }
147  }
148 #endif
149  }
150 
151  CompleteInitialized_ = true;
152  ActiveFrame_ = 0;
153  /* old version -- without timemeasure.hh
154  gettimeofday(&lastTime_,NULL);
155  */
156  return 0;
157 }
158 
160 {
162 
163 #ifdef BIAS_HAVE_FFMPEG
164  if (bVideo_)
165  {
166  Video_.PreGrab();
167  }
168 #endif
169  timer_.Start();
170  return 0;
171 }
172 
173 
174 
176 {
177  if (Grabbing_) {
178  BIASERR("VideoSource_Disk::CloseDevice(): Grabbing is running, but closing anyway");
179  }
180  Grabbing_ = false;
181  PostGrab();
182  CompleteInitialized_ = false;
183 
184 #ifdef BIAS_HAVE_FFMPEG
185  if (bVideo_)
186  {
187  Video_.CloseDevice();
188  }
189  else
190 #endif
191  {
192  if (FileNames_.size() != 0)
193  FileNames_.erase(FileNames_.begin(), FileNames_.end());
194  }
195  return 0;
196 }
197 
198 
200 {
201  BIASERR("VideoSource_Disk::SetColorMode: source always gives the original"<<
202  " color mode of the saved files");
203  return -1;
204 }
205 
207 {
208  BIASDOUT(D_VSO_DISK,"VideoSource_Disk::GrabSingle()");
209 
210 #ifdef BIAS_HAVE_FFMPEG
211  if (!bVideo_ && ActiveFrame_ == FileNames_.size())
212 #else
213  if (ActiveFrame_ == FileNames_.size())
214 #endif
215  {
216  if (Loop_) {
217  ActiveFrame_=0;
218  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
219  } else {
220  BEXCEPTION_NONFATAL("VideoSource_DISK: End of input. SetLoop(true) or "
221  <<"Rewind()");
222  BIASERR("VideoSource_DISK: End of input. SetLoop(true) or Rewind()");
223  return -1;
224  }
225  }
226  int res;
227  if (!Active_) {
228  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object not active, use PreGrab()");
229  return -1;
230  }
231 
232  if (Grabbing_) {
233  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object is already grabbing()");
234  return -1;
235  }
236 
237 
238 #ifdef BIAS_HAVE_FFMPEG
239  if (!bVideo_)
240 #endif
241  {
242  Grabbing_ = true;
243  res = ImageIO::Load(FileNames_[ActiveFrame_],image);
244 
245  if (res<0) {
246  BEXCEPTION("VideoSource_Disk::GrabSingle(): Unable to read image: "
247  <<FileNames_[ActiveFrame_]);
248  BIASERR("VideoSource_Disk::GrabSingle(): Unable to read image: "<<FileNames_[ActiveFrame_]);
249  return res;
250  }
251  }
252 #ifdef BIAS_HAVE_FFMPEG
253  else
254  {
255  if (bReversePlaying_)
256  {
257  BIASERR("VideoSource_Disk::GrabSingle(): Reverse playing is not supported for videos yet");
258  return -1;
259  }
260  if (ActiveFrame_ == 0)
261  Video_.SeekFrame(0);
262  bool eof = !Video_.GrabSingle(image);
263 
264  if (eof)
265  {
266  if (Loop_)
267  {
268  Video_.SeekFrame(0);
269  if (!Video_.GrabSingle(image))
270  return -1;
271  }
272  else
273  return -1;
274  }
275  }
276 #endif
277 
278  // set time to _now_
280  else {
281  timeval tv;
282  gettimeofday(&tv, NULL);
283  image.SetTime(tv.tv_sec,tv.tv_usec);
284  }
285 
286  //set meta data from loaded image, if activated
287  if (bUseMetaData_) image.ParseMetaData();
288 
289  if(ActiveFrame_<FileNames_.size()-1){
290  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
292  if(bReversePlaying_){
293  if(ActiveFrame_>0)
294  ActiveFrame_--;
295  else
296  {
297  bReversePlaying_=false;
298  ActiveFrame_++;
299  }
300  }
301  else{
302  ActiveFrame_++;
303  }
304  }
305  else if (Loop_) {
306  ActiveFrame_=0;
307  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
308  }
309  else if(bReverse_){
310 
311  bReversePlaying_=true;
312  ActiveFrame_--;
313  }
314  else ActiveFrame_++;
315 
316  Grabbing_ = false;
317  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
318  // check time and sleep while enough time elapsed
319  double secPerFrame=1.0/FramesPerSecond_;
320 
321  //struct timeval now; -- displaced by TimeMeasure
322  //double secs=0.0; -- -"-
323  //while (secs<=secPerFrame) { -- -"-
324  if(KeepFPS_) {
325  timer_.Stop();
326  if (FramesPerSecond_ != 0)
327  {
328  while (timer_.GetRealTime()<=(1E06*secPerFrame))
329  {
330 
331  timer_.Start();
332 #ifdef WIN32
333  Sleep(1); //go sleeping for 1 msec
334 #else //not WIN32
335  usleep(1000); //go sleeping for 1 msec
336 #endif //WIN32
337  timer_.Stop();
338 
339  }
340  }
341  timer_.Reset();
342  timer_.Start();
343  }
344  if (GenerateNewUIDs_) {
346  }
347  //lastTime_=now; -- displaced by TimeMeasure
348 
349  return 0;
350 }
351 
353 {
354  BIASDOUT(D_VSO_DISK,"VideoSource_Disk::GrabSingle()");
355 
356  if (ActiveFrame_ == FileNames_.size()){
357  BIASERR("VideoSource_DISK: End of input. SetLoop(true) or Rewind()");
358  return -1;
359  }
360  int res;
361  if (!Active_) {
362  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object not active, use PreGrab()");
363  return -1;
364  }
365 
366  if (Grabbing_) {
367  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object is already grabbing()");
368  return -1;
369  }
370 
371  Grabbing_ = true;
372  res = ImageIO::Load(FileNames_[ActiveFrame_],image);
373 
374  if (res<0) {
375  BIASERR("VideoSource_Disk::GrabSingle(): Unable to read image: "<<FileNames_[ActiveFrame_]);
376  return res;
377  }
378 
379  // set time to _now_
380  if (FrameCounterAsTimeStamp_) image.SetTime(ActiveFrame_,0);
381  else {
382  timeval tv;
383  gettimeofday(&tv, NULL);
384  image.SetTime(tv.tv_sec,tv.tv_usec);
385  }
386 
387  //set meta data from loaded image, if activated
388  if (bUseMetaData_) image.ParseMetaData();
389 
390  if(ActiveFrame_<FileNames_.size()-1){
391  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
392  FileNames_[ActiveFrame_]);
393  if(bReversePlaying_){
394  if(ActiveFrame_>0)
395  ActiveFrame_--;
396  else
397  {
398  bReversePlaying_=false;
399  ActiveFrame_++;
400  }
401  }
402  else{
403  ActiveFrame_++;
404  }
405  }
406  else if (Loop_) {
407  ActiveFrame_=0;
408  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
409  }
410  else if(bReverse_){
411 
412  bReversePlaying_=true;
413  ActiveFrame_--;
414  }
415  else ActiveFrame_++;
416 
417  Grabbing_ = false;
418  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
419  // check time and sleep while enough time elapsed
420  double secPerFrame=1.0/FramesPerSecond_;
421 
422  //struct timeval now; -- displaced by TimeMeasure
423  //double secs=0.0; -- -"-
424  //while (secs<=secPerFrame) { -- -"-
425  if(KeepFPS_) {
426  timer_.Stop();
427 
428  if (FramesPerSecond_ != 0)
429  {
430  while (timer_.GetRealTime()<=(1E06*secPerFrame))
431  {
432 
433  timer_.Start();
434 #ifdef WIN32
435  Sleep(1); //go sleeping for 1 msec
436 #else //not WIN32
437  usleep(1000); //go sleeping for 1 msec
438 #endif //WIN32
439  timer_.Stop();
440 
441  }
442  timer_.Reset();
443  timer_.Start();
444  }
445  }
446  if (GenerateNewUIDs_) {
448  }
449  //lastTime_=now; -- displaced by TimeMeasure
450 
451  return 0;
452 }
453 
454 #ifdef BUILD_IMAGE_USHORT
456 {
457  BIASDOUT(D_VSO_DISK,"VideoSource_Disk::GrabSingle()");
458 
459  if (ActiveFrame_ == FileNames_.size()){
460  BIASERR("VideoSource_DISK: End of input. SetLoop(true) or Rewind()");
461  return -1;
462  }
463  int res;
464  if (!Active_) {
465  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object not active, use PreGrab()");
466  return -1;
467  }
468 
469  if (Grabbing_) {
470  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object is already grabbing()");
471  return -1;
472  }
473 
474  Grabbing_ = true;
475  res = ImageIO::Load(FileNames_[ActiveFrame_],image);
476 
477  if (res<0) {
478  BIASERR("VideoSource_Disk::GrabSingle(): Unable to read image: "<<FileNames_[ActiveFrame_]);
479  return res;
480  }
481 
482  // set time to _now_
484  else {
485  timeval tv;
486  gettimeofday(&tv, NULL);
487  image.SetTime(tv.tv_sec,tv.tv_usec);
488  }
489 
490  //set meta data from loaded image, if activated
491  if (bUseMetaData_) image.ParseMetaData();
492 
493  if(ActiveFrame_<FileNames_.size()-1){
494  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
496  if(bReversePlaying_){
497  if(ActiveFrame_>0)
498  ActiveFrame_--;
499  else
500  {
501  bReversePlaying_=false;
502  ActiveFrame_++;
503  }
504  }
505  else
506  ActiveFrame_++;
507  }
508  else if (Loop_) {
509  ActiveFrame_=0;
510  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
511  }
512  else if(bReverse_){
513  bReversePlaying_=true;
514  ActiveFrame_--;
515  }
516  else ActiveFrame_++;
517 
518  Grabbing_ = false;
519  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
520  // check time and sleep while enough time elapsed
521  double secPerFrame=1.0/FramesPerSecond_;
522 
523  //struct timeval now; -- displaced by TimeMeasure
524  //double secs=0.0; -- -"-
525  //while (secs<=secPerFrame) { -- -"-
526  if(KeepFPS_) {
527  timer_.Stop();
528 
529  if (FramesPerSecond_ != 0)
530  {
531  while (timer_.GetRealTime()<=(1E06*secPerFrame))
532  {
533  timer_.Start();
534 #ifdef WIN32
535  Sleep(1); //go sleeping for 1 msec
536 #else //not WIN32
537  usleep(1000); //go sleeping for 1 msec
538 #endif //WIN32
539  timer_.Stop();
540  }
541  }
542  timer_.Reset();
543  timer_.Start();
544  }
545  if (GenerateNewUIDs_) {
547  }
548  //lastTime_=now; -- displaced by TimeMeasure
549 
550  return 0;
551 }
552 #endif
553 
555 {
556  BIASDOUT(D_VSO_DISK,"VideoSource_Disk::GrabSingle()");
557 
558 #ifdef BIAS_HAVE_FFMPEG
559  if (!bVideo_ && ActiveFrame_ == FileNames_.size()){
560 #else
561  if (ActiveFrame_ == FileNames_.size()){
562 #endif
563  BIASERR("VideoSource_DISK: End of input. SetLoop(true) or Rewind()");
564  return -1;
565  }
566  int res;
567  if (!Active_) {
568  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object not active, use PreGrab()");
569  return -1;
570  }
571 
572  if (Grabbing_) {
573  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object is already grabbing()");
574  return -1;
575  }
576 
577  Grabbing_ = true;
578  res = ImageIO::Load(FileNames_[ActiveFrame_],image);
579 
580  if (res<0) {
581  BIASERR("VideoSource_Disk::GrabSingle(): Unable to read image: "<<FileNames_[ActiveFrame_]);
582  return res;
583  }
584 
585  if(ActiveFrame_<FileNames_.size()-1){
586  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
587  FileNames_[ActiveFrame_]);
588  if(bReversePlaying_){
589  if(ActiveFrame_>0)
590  ActiveFrame_--;
591  else
592  {
593  bReversePlaying_=false;
594  ActiveFrame_++;
595  }
596  }
597  else
598  ActiveFrame_++;
599  }
600  else if (Loop_) {
601  ActiveFrame_=0;
602  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
603  }
604  else if(bReverse_){
605  bReversePlaying_=true;
606  ActiveFrame_--;
607  }
608  else ActiveFrame_++;
609 
610  Grabbing_ = false;
611  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
612  // check time and sleep while enough time elapsed
613  double secPerFrame=1.0/FramesPerSecond_;
614 
615  //struct timeval now; -- displaced by TimeMeasure
616  //double secs=0.0; -- -"-
617  //while (secs<=secPerFrame) { -- -"-
618  if(KeepFPS_) {
619  timer_.Stop();
620 
621  if (FramesPerSecond_ != 0)
622  {
623  while (timer_.GetRealTime()<=(1E06*secPerFrame))
624  {
625  timer_.Start();
626 #ifdef WIN32
627  Sleep(1); //go sleeping for 1 msec
628 #else //not WIN32
629  usleep(1000); //go sleeping for 1 msec
630 #endif //WIN32
631  timer_.Stop();
632  }
633  }
634  timer_.Reset();
635  timer_.Start();
636  }
637  if (GenerateNewUIDs_) {
638 
640  }
641  //lastTime_=now; -- displaced by TimeMeasure
642 
643  return 0;
644 }
645 
647 {
648  if (!bVideo_)
649  return (FileNames_.size()- ActiveFrame_);
650 #ifdef BIAS_HAVE_FFMPEG
651  else{
652  // Only usefull for stopping
653  int64_t total = Video_.GetNumberOfFrames();
654  int64_t current = Video_.GetCurrentFrameNumber();
655  return total - current;
656  }
657 #endif
658  return 0;
659 }
660 
661 
662 
663 
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
BIAS::TimeMeasure timer_
Defines a common interface to different devices.
virtual int GrabSingle(BIAS::Camera< unsigned char > &image)
unsigned int GetDepth() const
returns the bytes per channel, which is the sizeof(StorageType) Should match GetSizeDepth(GetStorageT...
Definition: ImageBase.hh:328
BIAS::ImageBase::EColorModel ColorMode_
Color mode used by camera.
float BytesPerPixel_
BytesPerPixel cannot be int (e.g. it is 1.5 for YUV420p)
unsigned int GetWidth() const
Definition: ImageBase.hh:312
bool CompleteInitialized_
Complete_initialized_ is set when OpenDevice(), UseChannel() etc. are done.
int Width_
Image format.
class BIASImageBase_EXPORT Image
Definition: ImageBase.hh:91
std::string Identifier_
Verbose camera descriptor.
bool Grabbing_
Grabbing flag is set and unset in GrabSingle() methods.
virtual int OpenDevice()
selects the first available device to open (e.g.
static int GetSizeUnits(const enum BIAS::ImageBase::EColorModel &colormodel)
get the number of (packed) data values of color model E.g: CM_Grey : 1 CM_YUYV422 : 2 packs 3 channel...
Definition: ImageBase.cpp:1329
float FramesPerSecond_
Capturing framerate.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
void SetDebugLevel(const long int lv)
Definition: Debug.hh:318
unsigned int GetHeight() const
Definition: ImageBase.hh:319
int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
virtual int PreGrab()
Do last preparations before grabbing (e.g. start ISO transfer)
bool Active_
Active flag is set in PreGrab() und unset in PostGrab()
int Read(const std::string &filename)
main entry routine to read all EXIF tags from the header of image file.
double GetRealTime() const
return real time (=wall time clock) in usec JW For Win32: real-time is measured differently from user...
void SetUID(const BIAS::UUID &id)
Definition: ImageBase.hh:589
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
contains all atribute info and values of e.g. a file.
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
std::vector< std::string > FileNames_
void InitExifTags()
initialize me vector with all known EXIF tags
void SetTime(unsigned long long int sec, unsigned long long int usec)
Set time and ensure correct format (seconds and useconds after &#39;sec&#39;)
Definition: Camera.hh:124
int SetColorMode(BIAS::ImageBase::EColorModel mode)
std::string GetIdentifier()
combine Make, Model and focallength to one special identifier
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
int ParseMetaData(bool bUse2x64bitTS=true)
After ImageIO::Load() operated on AppData_, this method fills P_, Timestamp, DC_*, ...
Definition: Camera.cpp:154
unsigned int GetNumFramesRemaining() const
static UUID GenerateUUID(const bool &consecutively=DEFAULT_UUID_CONSECUTIVELY)
static function which simply produces a uuid and returns
Definition: UUID.cpp:235
int PostGrab()
Stop anything started in PreGrab()