Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoStream.cpp
1 #include "VideoStream.hh"
2 
3 using namespace std;
4 using namespace BIAS;
5 
6 #include <fcntl.h>
7 #include <sstream>
8 #include <iostream>
9 #include "Camera.hh"
10 #include <Base/Image/ImageIO.hh>
11 
12 
13 VideoStream::~VideoStream() {}
14 
15 
16 VideoStream::VideoStream()
17 {
18  NumImages_ = 0;
19  ImageSize_ = 0;
20 }
21 
22 VideoStream::VideoStream(const std::string & file)
23 {
24  NumImages_ = 0;
25  ImageSize_ =0;
26  Open(file);
27 }
28 
29 // jw
30 VideoStream::VideoStream(const BIAS::VideoStream & src){
31  (*this) = src;
32 }
33 
35 VideoStream::operator=(const BIAS::VideoStream & src)
36 {
37  Header_ = src.Header_;
38 
39  // no assignment operator for std streams!! (jw)
40  //Ifs_ = src.Ifs_;
41  BIASERR("Assignment operator is not correct due to missing fstream assignment! (jw)");
42 
43  ImageSize_ = src.ImageSize_;
44  NumImages_ = src.NumImages_;
45  FileSize_ = src.FileSize_;
46 
47  return *this;
48 }
49 
50 
51 
52 
53 int VideoStream::Open(const std::string & file)
54 {
55  Ifs_.open(file.c_str(),ios::binary);
56  if (!Ifs_) {
57  BIASERR("Can not open file for reading");
58  perror(file.c_str());
59  return -1;
60  }
61  Ifs_.seekg(0,ios_base::end);
62  FileSize_ = (unsigned long) Ifs_.tellg();
63  Ifs_.seekg(0,ios_base::beg);
64  Ifs_.read(reinterpret_cast<char *>(&Header_),sizeof(Header_));
65  if (!Ifs_) {
66  BIASERR("error reading from file");
67  perror(file.c_str());
68  return -1;
69  }
70 
71  ImageSize_ = Header_.Width * Header_.Height * Header_.ChannelCount
72  * Header_.BytesPerPixel;
73 
74  NumImages_ = (FileSize_ - sizeof(Header_)) / Header_.AlignedSize;
75 
76  return 0;
77 }
78 
79 
80 int VideoStream::Close()
81 {
82  Ifs_.close();
83  return 0;
84 }
85 
86 // go through video stream and export count frames starting at start
87 int VideoStream::SplitIntoFiles(const std::string &prefix, unsigned int start,
88  unsigned int count )
89 {
90  if (!Ifs_) {
91  BIASERR("VideoStream not open.");
92  return -1;
93  }
94  // we should seek to start
95 
96  unsigned int numimages;
97  ostringstream filename;
98  VSImageHeader imghead;
99  unsigned int paddingsize = Header_.AlignedSize - ImageSize_-sizeof(imghead);
100  cout <<"Paddingsize: "<<paddingsize<<endl;
102  UUID uid;
103  img.Init(Header_.Width, Header_.Height, Header_.ChannelCount);
104  char *imgdata = reinterpret_cast<char *> (img.GetImageData());
105  if (count ==0) numimages = NumImages_ ;
106  else numimages = count + start;
107  for (unsigned int i=start;i<numimages; i++) {
108  Ifs_.read(reinterpret_cast<char *>(&imghead), sizeof(imghead));
109  if (!Ifs_) return -1;
110  Ifs_.read(imgdata, ImageSize_);
111  if (!Ifs_) return -1;
112  uid.SetFromString(imghead.uuid);
113  img.SetUID(uid);
114  filename.str("");
115  filename<<prefix<<i;
116  cout <<"file: "<<filename.str()<<" UUID: "<<img.GetUID()<<" Time: "
117  <<imghead.tsec<<" "<<imghead.tusec<<endl;
118  img.UpdateMetaData();
119  //ImageIO::Save(filename.str(),img,false,false);
120  ImageIO::Save(filename.str(),
121  img,
122  ImageIO::FF_auto,
123  false,
124  BIAS_DEFAULT_IMAGE_QUALITY,
125  false);
126  Ifs_.seekg(paddingsize,ios_base::cur);
127 
128  }
129 
130  return 0;
131 }
132 
133 
134 void VideoStream::PrintHeader()
135 {
136 
137  cout <<"FileSize: "<<FileSize_<<endl;
138  cout <<"Image width, height, channelcout, BpP: "<<Header_.Width<<"x"
139  <<Header_.Height<<", "<<(int)Header_.ChannelCount<<", "
140  <<(int)Header_.BytesPerPixel<<endl;
141 
142  cout <<"StorageType, ColorModel: "
143  <<int(Header_.StorageType)<<", "
144  <<int(Header_.ColorModel)<<endl;
145 
146  cout <<"ImageSize, BlockSize: "<<ImageSize_<<", "<<Header_.AlignedSize<<endl;
147  cout <<"NumImages: "<<NumImages_<<endl;
148  cout <<"Remainer: "<<FileSize_ - sizeof(Header_) - NumImages_ *
149  Header_.AlignedSize<<endl;
150 }
unsigned int NumImages_
Definition: VideoStream.hh:99
const BIAS::UUID & GetUID() const
returns the UUID of the image
Definition: ImageBase.hh:449
bool SetFromString(const std::string &sID)
construct from string containing ascii uuid.
Definition: UUID.cpp:130
To allow capturing of camera image to disk efficiently, (avoid multiple memcopy operations), this class defines an interface to file containing one header and multiple images.
Definition: VideoStream.hh:35
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
void SetUID(const BIAS::UUID &id)
Definition: ImageBase.hh:589
int UpdateMetaData()
copy P_ and co.
Definition: Camera.cpp:446
unsigned int ImageSize_
Definition: VideoStream.hh:98
interface class for producing/storing Universally Unique IDentifiers
Definition: UUID.hh:98
unsigned long int FileSize_
Definition: VideoStream.hh:100