Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_Disk_Bayer.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/VideoSource_Disk.hh>
34 #include "VideoSource_Disk_Bayer.hh"
35 #include <Base/Image/ImageIO.hh>
36 #include <Base/Image/ImageConvert.hh>
37 #include <Image/Camera.hh>
38 
39 #include <Base/Common/BIASpragma.hh>
40 
41 using namespace BIAS;
42 using namespace std;
43 
45 {
47 }
48 
49 
51 {
52 }
53 
54 
55 int VideoSource_Disk_Bayer::OpenDevice(const vector<string> &FileNames)
56 {
57 // BIASDOUT(D_VSO_DISK,"entering VideoSource_Disk_Bayer::OpenDevice(vector<string>)");
58 // Camera<unsigned char> Image;
59  // load first image and initialize some internal variables
61  BIASDOUT(D_VSO_DISK,"FileNames[0] : "<<FileNames[0]);
62  // Image.SetDebugLevel(D_COPYIN | D_CONVERT);
63 
64  if (ImageIO::Load(FileNames[0],Image)!=0){
65  cout<<"pwd: ";system("pwd");
66  BIASERR("unable to read "<<FileNames[0]);
67  BIASBREAK;
68  return -1;
69  }
70  Width_ = Image.GetWidth();
71  Height_ = Image.GetHeight();
73  // because of bayer image, colormode has to be set manually
74 // ColorMode_ = Image.GetColorModel();
75  BytesPerPixel_ = Image.GetDepth();
76  if (FileNames_.size() != 0)
77  FileNames_.erase(FileNames_.begin(), FileNames_.end());
78 
79  for (vector<string>::const_iterator i = FileNames.begin();
80  i != FileNames.end();i++)
81  {
82  FileNames_.push_back(*i);
83 
84 #if 0
85  const char *fName2 =(*i).c_str();
86  //ImageIO::Read(fName2,Image);
87  ImageIO::Load(fName2,Image);
88  if (i != FileNames.begin()){
89  if (Width_ != Image.GetWidth() || Height_ != Image.GetHeight() ||
90  ColorChannels_ != Image.GetChannelCount() ){
91  BIASERR("VideoSource_Disk_Bayer::OpenDevice() Image is "<<*i<<
92  " different from first image in sequence");
93  return -1;
94  }
95  }
96 #endif
97  }
98 
99  CompleteInitialized_ = true;
100  ActiveFrame_ = 0;
101  /* old version -- without timemeasure.hh
102  gettimeofday(&lastTime_,NULL);
103  */
104  return 0;
105 }
106 
107 //int VideoSource_Disk_Bayer::PreGrab()
108 //{
109 // VideoSource::PreGrab();
110 //
111 // timer_.Start();
112 // return 0;
113 //}
114 
115 
116 
117 //int VideoSource_Disk_Bayer::CloseDevice()
118 //{
119 // if (Grabbing_) {
120 // BIASERR("VideoSource_Disk_Bayer::CloseDevice(): Grabbing is running, but closing anyway");
121 // }
122 // Grabbing_ = false;
123 // PostGrab();
124 // CompleteInitialized_ = false;
125 // if (FileNames_.size() != 0)
126 // FileNames_.erase(FileNames_.begin(), FileNames_.end());
127 // return 0;
128 //}
129 
130 
132 {
133 
134  if(mode == ImageBase::CM_Bayer_BGGR ||
135  mode == ImageBase::CM_Bayer_GBRG ||
136  mode == ImageBase::CM_Bayer_GRBG ||
137  mode == ImageBase::CM_Bayer_RGGB){
138  ColorMode_ = mode;
139  } else {
140  BIASERR("So far this class is implemented to handle Bayer patterns only!");
141  return -1;
142  }
143 
144  return 0;
145 }
146 
148 {
149  BIASDOUT(D_VSO_DISK,"VideoSource_Disk_Bayer::GrabSingle()");
150 
151  if (ActiveFrame_ == FileNames_.size()){
152  BIASERR("VideoSource_Disk_Bayer: End of input. SetLoop(true) or Rewind()");
153  return -1;
154  }
155  int res;
156  if (!Active_) {
157  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): VideoSource Object not active, use PreGrab()");
158  return -1;
159  }
160 
161  if (Grabbing_) {
162  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): VideoSource Object is already grabbing()");
163  return -1;
164  }
165 
166  Grabbing_ = true;
167  Camera<unsigned char> bayerImage;
168  res = ImageIO::Load(FileNames_[ActiveFrame_], bayerImage);
169  bayerImage.SetColorModel(ColorMode_);
170  ImageConvert::ToRGB(bayerImage,image);
171 
172  if (res<0) {
173  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): Unable to read image: "<<FileNames_[ActiveFrame_]);
174  return res;
175  }
176 
177  // set time to _now_
178  if (FrameCounterAsTimeStamp_) image.SetTime(ActiveFrame_,0);
179  else {
180  timeval tv;
181  gettimeofday(&tv, NULL);
182  image.SetTime(tv.tv_sec,tv.tv_usec);
183  }
184 
185  //set meta data from loaded image, if activated
186  if (bUseMetaData_) image.ParseMetaData();
187 
188  if(ActiveFrame_<FileNames_.size()-1){
189  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
190  FileNames_[ActiveFrame_]);
191  if(bReversePlaying_){
192  if(ActiveFrame_>0)
193  ActiveFrame_--;
194  else
195  {
196  bReversePlaying_=false;
197  ActiveFrame_++;
198  }
199  }
200  else{
201  ActiveFrame_++;
202  }
203  }
204  else if (Loop_) {
205  ActiveFrame_=0;
206  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
207  }
208  else if(bReverse_){
209 
210  bReversePlaying_=true;
211  ActiveFrame_--;
212  }
213  else ActiveFrame_++;
214 
215  Grabbing_ = false;
216  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
217  // check time and sleep while enough time elapsed
218  double secPerFrame=1.0/FramesPerSecond_;
219 
220  //struct timeval now; -- displaced by TimeMeasure
221  //double secs=0.0; -- -"-
222  //while (secs<=secPerFrame) { -- -"-
223  if(KeepFPS_) {
224  timer_.Stop();
225  if (FramesPerSecond_ != 0)
226  {
227  while (timer_.GetRealTime()<=(1E06*secPerFrame))
228  {
229 
230  timer_.Start();
231 #ifdef WIN32
232  Sleep(1); //go sleeping for 1 msec
233 #else //not WIN32
234  usleep(1000); //go sleeping for 1 msec
235 #endif //WIN32
236  timer_.Stop();
237 
238  }
239  }
240  timer_.Reset();
241  timer_.Start();
242  }
243  if (GenerateNewUIDs_) {
245  }
246  //lastTime_=now; -- displaced by TimeMeasure
247 
248  return 0;
249 }
250 
252 {
253  BIASDOUT(D_VSO_DISK,"VideoSource_Disk_Bayer::GrabSingle()");
254 
255  if (ActiveFrame_ == FileNames_.size()){
256  BIASERR("VideoSource_Disk_Bayer: End of input. SetLoop(true) or Rewind()");
257  return -1;
258  }
259  int res;
260  if (!Active_) {
261  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): VideoSource Object not active, use PreGrab()");
262  return -1;
263  }
264 
265  if (Grabbing_) {
266  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): VideoSource Object is already grabbing()");
267  return -1;
268  }
269 
270  Grabbing_ = true;
271 
272  Camera<float> bayerImage;
273  res = ImageIO::Load(FileNames_[ActiveFrame_], bayerImage);
274  bayerImage.SetColorModel(ColorMode_);
275  ImageConvert::ToRGB(bayerImage,image);
276 
277 
278  if (res<0) {
279  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): Unable to read image: "<<FileNames_[ActiveFrame_]);
280  return res;
281  }
282 
283  // set time to _now_
284  if (FrameCounterAsTimeStamp_) image.SetTime(ActiveFrame_,0);
285  else {
286  timeval tv;
287  gettimeofday(&tv, NULL);
288  image.SetTime(tv.tv_sec,tv.tv_usec);
289  }
290 
291  //set meta data from loaded image, if activated
292  if (bUseMetaData_) image.ParseMetaData();
293 
294  if(ActiveFrame_<FileNames_.size()-1){
295  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
296  FileNames_[ActiveFrame_]);
297  if(bReversePlaying_){
298  if(ActiveFrame_>0)
299  ActiveFrame_--;
300  else
301  {
302  bReversePlaying_=false;
303  ActiveFrame_++;
304  }
305  }
306  else{
307  ActiveFrame_++;
308  }
309  }
310  else if (Loop_) {
311  ActiveFrame_=0;
312  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
313  }
314  else if(bReverse_){
315 
316  bReversePlaying_=true;
317  ActiveFrame_--;
318  }
319  else ActiveFrame_++;
320 
321  Grabbing_ = false;
322  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
323  // check time and sleep while enough time elapsed
324  double secPerFrame=1.0/FramesPerSecond_;
325 
326  //struct timeval now; -- displaced by TimeMeasure
327  //double secs=0.0; -- -"-
328  //while (secs<=secPerFrame) { -- -"-
329  if(KeepFPS_) {
330  timer_.Stop();
331 
332  if (FramesPerSecond_ != 0)
333  {
334  while (timer_.GetRealTime()<=(1E06*secPerFrame))
335  {
336 
337  timer_.Start();
338 #ifdef WIN32
339  Sleep(1); //go sleeping for 1 msec
340 #else //not WIN32
341  usleep(1000); //go sleeping for 1 msec
342 #endif //WIN32
343  timer_.Stop();
344 
345  }
346  timer_.Reset();
347  timer_.Start();
348  }
349  }
350  if (GenerateNewUIDs_) {
352  }
353  //lastTime_=now; -- displaced by TimeMeasure
354 
355  return 0;
356 }
357 
358 #ifdef BUILD_IMAGE_USHORT
360 {
361  BIASDOUT(D_VSO_DISK,"VideoSource_Disk_Bayer::GrabSingle()");
362 
363  if (ActiveFrame_ == FileNames_.size()){
364  BIASERR("VideoSource_Disk_Bayer: End of input. SetLoop(true) or Rewind()");
365  return -1;
366  }
367  int res;
368  if (!Active_) {
369  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): VideoSource Object not active, use PreGrab()");
370  return -1;
371  }
372 
373  if (Grabbing_) {
374  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): VideoSource Object is already grabbing()");
375  return -1;
376  }
377 
378  Grabbing_ = true;
379 
380  Camera<unsigned short> bayerImage;
381  res = ImageIO::Load(FileNames_[ActiveFrame_], bayerImage);
382  bayerImage.SetColorModel(ColorMode_);
383  ImageConvert::ToRGB(bayerImage,image);
384 
385  if (res<0) {
386  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): Unable to read image: "<<FileNames_[ActiveFrame_]);
387  return res;
388  }
389 
390  // set time to _now_
392  else {
393  timeval tv;
394  gettimeofday(&tv, NULL);
395  image.SetTime(tv.tv_sec,tv.tv_usec);
396  }
397 
398  //set meta data from loaded image, if activated
399  if (bUseMetaData_) image.ParseMetaData();
400 
401  if(ActiveFrame_<FileNames_.size()-1){
402  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
404  if(bReversePlaying_){
405  if(ActiveFrame_>0)
406  ActiveFrame_--;
407  else
408  {
409  bReversePlaying_=false;
410  ActiveFrame_++;
411  }
412  }
413  else
414  ActiveFrame_++;
415  }
416  else if (Loop_) {
417  ActiveFrame_=0;
418  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
419  }
420  else if(bReverse_){
421  bReversePlaying_=true;
422  ActiveFrame_--;
423  }
424  else ActiveFrame_++;
425 
426  Grabbing_ = false;
427  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
428  // check time and sleep while enough time elapsed
429  double secPerFrame=1.0/FramesPerSecond_;
430 
431  //struct timeval now; -- displaced by TimeMeasure
432  //double secs=0.0; -- -"-
433  //while (secs<=secPerFrame) { -- -"-
434  if(KeepFPS_) {
435  timer_.Stop();
436 
437  if (FramesPerSecond_ != 0)
438  {
439  while (timer_.GetRealTime()<=(1E06*secPerFrame))
440  {
441  timer_.Start();
442 #ifdef WIN32
443  Sleep(1); //go sleeping for 1 msec
444 #else //not WIN32
445  usleep(1000); //go sleeping for 1 msec
446 #endif //WIN32
447  timer_.Stop();
448  }
449  }
450  timer_.Reset();
451  timer_.Start();
452  }
453  if (GenerateNewUIDs_) {
455  }
456  //lastTime_=now; -- displaced by TimeMeasure
457 
458  return 0;
459 }
460 #endif
461 
463 {
464  BIASDOUT(D_VSO_DISK,"VideoSource_Disk_Bayer::GrabSingle()");
465 
466  if (ActiveFrame_ == FileNames_.size()){
467  BIASERR("VideoSource_Disk_Bayer: End of input. SetLoop(true) or Rewind()");
468  return -1;
469  }
470  int res;
471  if (!Active_) {
472  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): VideoSource Object not active, use PreGrab()");
473  return -1;
474  }
475 
476  if (Grabbing_) {
477  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): VideoSource Object is already grabbing()");
478  return -1;
479  }
480 
481  Grabbing_ = true;
482 
483  ImageBase bayerImage;
484  res = ImageIO::Load(FileNames_[ActiveFrame_], bayerImage);
485  bayerImage.SetColorModel(ColorMode_);
486  ImageConvert::Convert (bayerImage, image, ImageBase::CM_RGB, bayerImage.IsPlanar());
487 
488 
489 
490  if (res<0) {
491  BIASERR("VideoSource_Disk_Bayer::GrabSingle(): Unable to read image: "<<FileNames_[ActiveFrame_]);
492  return res;
493  }
494 
495  if(ActiveFrame_<FileNames_.size()-1){
496  BIASDOUT(D_VSO_DISK,"GrabSingle() "<<ActiveFrame_<<" "<<
497  FileNames_[ActiveFrame_]);
498  if(bReversePlaying_){
499  if(ActiveFrame_>0)
500  ActiveFrame_--;
501  else
502  {
503  bReversePlaying_=false;
504  ActiveFrame_++;
505  }
506  }
507  else
508  ActiveFrame_++;
509  }
510  else if (Loop_) {
511  ActiveFrame_=0;
512  BIASDOUT(D_VSO_DISK,"GrabSingle() restarting with first file");
513  }
514  else if(bReverse_){
515  bReversePlaying_=true;
516  ActiveFrame_--;
517  }
518  else ActiveFrame_++;
519 
520  Grabbing_ = false;
521  BIASDOUT(D_VSO_DISK,"leaving GrabSingle()");
522  // check time and sleep while enough time elapsed
523  double secPerFrame=1.0/FramesPerSecond_;
524 
525  //struct timeval now; -- displaced by TimeMeasure
526  //double secs=0.0; -- -"-
527  //while (secs<=secPerFrame) { -- -"-
528  if(KeepFPS_) {
529  timer_.Stop();
530 
531  if (FramesPerSecond_ != 0)
532  {
533  while (timer_.GetRealTime()<=(1E06*secPerFrame))
534  {
535  timer_.Start();
536 #ifdef WIN32
537  Sleep(1); //go sleeping for 1 msec
538 #else //not WIN32
539  usleep(1000); //go sleeping for 1 msec
540 #endif //WIN32
541  timer_.Stop();
542  }
543  }
544  timer_.Reset();
545  timer_.Start();
546  }
547  if (GenerateNewUIDs_) {
548 
550  }
551  //lastTime_=now; -- displaced by TimeMeasure
552 
553  return 0;
554 }
555 
556 
557 
558 
559 
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
Bayer_GRBG, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:145
BIAS::TimeMeasure timer_
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
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
Bayer_RGGB, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:143
BIAS::ImageBase::EColorModel ColorMode_
Color mode used by camera.
bool IsPlanar() const
Definition: ImageBase.hh:484
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.
Bayer_BGGR, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:146
Bayer_GBRG, 1 channel RGB image Bayer tile.
Definition: ImageBase.hh:144
class BIASImageBase_EXPORT Image
Definition: ImageBase.hh:91
bool Grabbing_
Grabbing flag is set and unset in GrabSingle() methods.
virtual int OpenDevice()
selects the first available device to open (e.g.
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
unsigned int GetHeight() const
Definition: ImageBase.hh:319
bool Active_
Active flag is set in PreGrab() und unset in PostGrab()
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
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_
static int Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false)
main general conversion function, calls desired specialized functions, always initializes the destIma...
int SetColorMode(BIAS::ImageBase::EColorModel mode)
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
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.
int ParseMetaData(bool bUse2x64bitTS=true)
After ImageIO::Load() operated on AppData_, this method fills P_, Timestamp, DC_*, ...
Definition: Camera.cpp:154
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.