Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_DiskZessPMD.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 #else
27 #include <Base/Common/W32Compat.hh>
28 #endif //WIN32
29 
30 #include <string>
31 #include <sstream>
32 #include <iostream>
33 
34 #include "VideoSource_DiskZessPMD.hh"
35 #include <Base/Image/ImageIO.hh>
36 #include <Image/Camera.hh>
37 
38 #include <Base/Common/BIASpragma.hh>
39 #include <Base/Common/FileHandling.hh>
40 
41 using namespace BIAS;
42 using namespace std;
43 
44 
46 {
47  GenerateNewUIDs_=true;
48 }
49 
51 {
52 }
53 
55 {
56  BIASERR("use OpenDevice(const vector<string> &FileNames)");
57  BIASABORT;
58  return -1;
59 }
60 
61 int BIAS::VideoSource_DiskZessPMD::OpenDevice(const vector<string> &FileNames)
62 {
64  BIASDOUT(D_VSO_DISK,"FileNames[0] : "<<FileNames[0]);
65  if (ImageIO::Load(FileNames[0], img)!=0) {
66  BIASERR("unable to read "<<FileNames[0]);
67  return -1;
68  }
69  Width_ = img.GetWidth();
70  Height_ = img.GetHeight();
71  ColorChannels_ = img.GetChannelCount();
72  ColorMode_ = img.GetColorModel();
73  BytesPerPixel_ = img.GetDepth();
74 
75  FileNames_.clear();
76  for (vector<string>::const_iterator i = FileNames.begin();
77  i != FileNames.end();i++){
78  FileNames_.push_back(*i);
79  }
80 
81  CompleteInitialized_ = true;
82  ActiveFrame_ = 0;
83 
84  return 0;
85 }
86 
87 
89 {
90  BIASDOUT(D_VSO_DISK,"VideoSource_DiskPMD::GrabSingle()");
91 
92  if (!Active_) {
93  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object not active, use PreGrab()");
94  return -1;
95  }
96 
97  if (Grabbing_) {
98  BIASERR("VideoSource_Disk::GrabSingle(): VideoSource Object is already grabbing()");
99  return -1;
100  }
101 
102  // Add Metadata to the Camera via Projection Object
103  if (FileNames_.size() == 0) {
104  if (ActiveFrame_ == FileNames_.size()){
105  BIASERR("VideoSource_DISK: End of input. SetLoop(true) or Rewind()");
106  return -1;
107  }
108  } else {
109  if (ActiveFrame_ == FileNames_.size()) {
110  BIASERR("VideoSource_DISK: End of input. SetLoop(true) or Rewind()");
111  return -1;
112  }
113 
114  ImageIO::Load(FileNames_[ActiveFrame_],image);
115  ImageIO::Load(FileNames_[ActiveFrame_+1],DepthImg_);
116  ImageIO::Load(FileNames_[ActiveFrame_+2],ModCoeffImg_);
117  }
118  Grabbing_ = true;
119 
121  DepthImg_.SetUID(image.GetUID());
122  ModCoeffImg_.SetUID(image.GetUID());
123 
124  // set time to _now_
125  if (FrameCounterAsTimeStamp_) image.SetTime(ActiveFrame_,0);
126  else {
127  timeval tv;
128  gettimeofday(&tv, NULL);
129  image.SetTime(tv.tv_sec,tv.tv_usec);
130  }
131  if(ActiveFrame_<FileNames_.size()-1){
132  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
133  FileNames_[ActiveFrame_]);
134  ActiveFrame_+=3;
135  }
136  else if (Loop_) {
137  ActiveFrame_=0;
138  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
139  }
140  else ActiveFrame_+=3;
141 
142  Grabbing_ = false;
143  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
144  // check time and sleep while enough time elapsed
145  double secPerFrame=1.0/FramesPerSecond_;
146 
147  //struct timeval now; -- displaced by TimeMeasure
148  //double secs=0.0; -- -"-
149  //while (secs<=secPerFrame) { -- -"-
150  if(KeepFPS_) {
151  timer_.Stop();
152  while (timer_.GetRealTime()<=(1E06*secPerFrame)) {
153  timer_.Start();
154 #ifdef WIN32
155  Sleep(1); //go sleeping for 1 msec
156 #else //not WIN32
157  usleep(1000); //go sleeping for 1 msec
158 #endif //WIN32
159  timer_.Stop();
160 
161  }
162  timer_.Reset();
163  timer_.Start();
164  }
165  //lastTime_=now; -- displaced by TimeMeasure
166 
167  return 0;
168 }
169 
unsigned int GetDepth() const
returns the bytes per channel, which is the sizeof(StorageType) Should match GetSizeDepth(GetStorageT...
Definition: ImageBase.hh:328
virtual int GrabSingle(Camera< unsigned char > &image)
Returns the 2D image, and reads depth and modulation coefficients.
unsigned int GetWidth() const
Definition: ImageBase.hh:312
const BIAS::UUID & GetUID() const
returns the UUID of the image
Definition: ImageBase.hh:449
virtual int OpenDevice()
selects the first available device to open (e.g.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
void SetUID(const BIAS::UUID &id)
Definition: ImageBase.hh:589
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
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
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
static UUID GenerateUUID(const bool &consecutively=DEFAULT_UUID_CONSECUTIVELY)
static function which simply produces a uuid and returns
Definition: UUID.cpp:235
This class simulates a video device by loading images from disk.