Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ImagePackage.cpp
1 #include <Image/ImagePackage.hh>
2 
3 using namespace std;
4 using namespace BIAS;
5 
6 ImagePackage::ImagePackage() {
7  bOpen_ = false;
8  bReadMode_ = false;
9  fileInfos_.clear();
10  ifs_.clear();
11 
12 }
13 
14 ImagePackage::~ImagePackage() {
15  if (bOpen_)
16  Close();
17 }
18 
19 
20 int ImagePackage::Create(const string filename, unsigned int num) {
21  filename_ = filename;
22  fileNum_ = num;
23  string fn = filename + FileHandling::LeadingZeroString(fileNum_, 4) + ".pack";
24  cout << fn << endl;
25  ofs_.open(fn.c_str(), ios::binary);
26  if (ofs_.good()) {
27  bOpen_ = true;
28  bReadMode_ = false;
29  }
30  return 0;
31 }
32 
33 int ImagePackage::AddImage(const ImageBase image, const string filename) {
34  string fname = filename;
35  if (fname.rfind("/")!=string::npos) {
36  fname = fname.substr(fname.rfind("/")+1);
37  }
38  if (fname.rfind("\\")!=string::npos) {
39  fname = fname.substr(fname.rfind("\\")+1);
40  }
41  unsigned int w = image.GetWidth();
42  unsigned int h = image.GetHeight();
43  unsigned int c = image.GetChannelCount();
44  unsigned int b = image.GetDepth();
45  IPImageHeader ipih;
46  ipih.len_pixeldata = w*h*c*b;
47  ipih.len_filename = fname.length();
48  stringstream metadata;
49  image.GetMetaData()->WriteBinary(metadata);
50  ipih.len_metadata = metadata.str().length();
51 
52  ofs_ << ipih;
53  ofs_ << fname;
54  ofs_ << image;
55  image.GetMetaData()->WriteBinary(ofs_);
56 
57  // 1GB file size reached
58  if (ofs_.tellp() > 1073741824) {
59  Close();
60  Create(filename_, ++fileNum_);
61  }
62  return 0;
63 }
64 
65 
66 int ImagePackage::Open(const string filename) {
67  filename_ = filename;
68  if (filename_.find("0001.pack") != string::npos) {
69  filename_ = filename_.substr(0,filename_.find("0001.pack"));
70  }
71  fileNum_ = 0;
72  ifstream *test;
73  bool exist = true;
74  while (exist) {
75  fileNum_++;
76  string fn = filename_ + FileHandling::LeadingZeroString(fileNum_, 4) + ".pack";
77  test = new ifstream;
78  test->open(fn.c_str(), ios::binary);
79  if (test->good()) {
80  ifs_.push_back(test);
81  bOpen_ = true;
82  bReadMode_ = true;
83  } else {
84  exist = false;
85  }
86  }
87  currentImg_ = 0;
88  currentFile_ = 0;
89  return 0;
90 }
91 
92 int ImagePackage::GetImage(const int imageNum, ImageBase &image, string &filename) {
93 
94  while (imageNum < currentImg_) {
95  if (GetPrevImage(image, filename)!=0) {
96  cout << "GetImage: invalid image number" << endl;
97  return -1;
98  }
99  }
100  while (imageNum > currentImg_) {
101  if (GetNextImage(image, filename)!=0) {
102  cout << "GetImage: invalid image number" << endl;
103  return -1;
104  }
105  }
106 
107 // IPFileInfo ipf = fileInfos_[currentImg_];
108 // ifs_[ipf.fileNum]->seekg(ipf.offset);
109  int ret = GetNextImage(image, filename);
110  return ret;
111 }
112 
113 int ImagePackage::GetNextImage(ImageBase &image, string &filename) {
114  if (!image.IsEmpty())
115  image.Release(true);
116  ifs_[currentFile_]->peek();
117  if (ifs_[currentFile_]->eof()) {
118  if ((unsigned int)currentFile_+1<ifs_.size()) {
119  currentFile_++;
120  } else {
121  return -1;
122  }
123  }
124 
125  if (ifs_[currentFile_]->good() == false) {
126  BIASERR("file is corrupt. maybe not closed correctly?");
127  return 1;
128  }
129 
130  if ((int)fileInfos_.size()<=currentImg_) {
131  IPFileInfo ipf;
132  fileInfos_.push_back(ipf);
133  }
134 
135  IPImageHeader ipih;
136  fileInfos_[currentImg_].fileNum = (unsigned char) currentFile_;
137  fileInfos_[currentImg_].offset = (unsigned long) ifs_[currentFile_]->tellg();
138  *ifs_[currentFile_] >> ipih;
139 
140  if (ifs_[currentFile_]->good() == false) {
141  BIASERR("file is corrupt. maybe not closed correctly?");
142  return 1;
143  }
144 
145  filename = "";
146  if (ipih.len_filename>0) {
147  stringstream fnamestream;
148  for (unsigned int i=0;i<ipih.len_filename; i++) {
149  char c;
150  ifs_[currentFile_]->get(c);
151  fnamestream.put(c);
152  }
153  filename = fnamestream.str();
154  if (filename.find(".mip") == string::npos) {
155  filename +=".mip";
156  }
157  fileInfos_[currentImg_].fileName = filename;
158  } else {
159  fileInfos_[currentImg_].fileName = filename_ + FileHandling::LeadingZeroString(currentImg_, 5) + ".mip";
160  }
161  *ifs_[currentFile_] >> image;
162  if (ifs_[currentFile_]->good() == false) {
163  BIASERR("file is corrupt. maybe not closed correctly?");
164  return 1;
165  }
166 
167  if (ipih.len_metadata>0) {
168  stringstream metastream;
169  for (unsigned int i=0;i<ipih.len_metadata; i++) {
170  char c;
171  ifs_[currentFile_]->get(c);
172  metastream.put(c);
173  }
174  metastream >> *image.GetMetaData();
175  }
176  if (ifs_[currentFile_]->good() == false) {
177  BIASERR("file is corrupt. maybe not closed correctly?");
178  return 1;
179  }
180 
181  currentImg_++;
182  return 0;
183 }
184 
185 int ImagePackage::GetPrevImage(ImageBase &image, string &filename) {
186  if (currentImg_ < 1)
187  return -1;
188  currentImg_--;
189  IPFileInfo ipf = fileInfos_[currentImg_];
190  currentFile_ = ipf.fileNum;
191  ifs_[currentFile_]->seekg(ipf.offset);
192  GetNextImage(image, filename);
193  ifs_[currentFile_]->seekg(ipf.offset);
194  currentImg_--;
195  return 0;
196 }
197 
198 int ImagePackage::WriteSingleImage(const unsigned int imageNum, const string filename) {
199  ImageBase image;
200 
201  while (imageNum >= fileInfos_.size()) {
202  string tmp;
203  if (GetNextImage(image, tmp)!=0) {
204  cout << "WriteSingleImage: invalid image number" << endl;
205  return -1;
206  }
207  }
208 
209  IPFileInfo ipf = fileInfos_[imageNum];
210  unsigned int fidx = ipf.fileNum;
211  unsigned long int curpos = (unsigned long) ifs_[fidx]->tellg();
212  ifs_[fidx]->seekg(ipf.offset);
213  string fname;
214  GetNextImage(image, fname);
215  ImageIO::Save(fname, image);
216  ifs_[fidx]->seekg(curpos);
217  return 0;
218 }
219 
220 int ImagePackage::WriteAllImages(const string filename) {
221  for (unsigned int i=0;i<ifs_.size();i++) {
222  ifs_[i]->seekg(0);
223  }
224  ImageBase myImg;
225  string fname;
226  int len = filename.length();
227  int n = 0;
228  currentImg_ = 0;
229  while (GetNextImage(myImg, fname)==0) {
230  if (len>0) {
231  fname = filename + FileHandling::LeadingZeroString(++n,5) + ".mip";
232  }
233  cout << "ImagePackage saving: " << fname << endl;
234  ImageIO::Save(fname, myImg);
235  }
236 
237  return 0;
238 }
239 
240 int ImagePackage::Close() {
241  if (bOpen_) {
242  if (bReadMode_)
243  for (unsigned int i=0;i<ifs_.size();i++)
244  ifs_[i]->close();
245  else {
246  ofs_.close();
247  }
248  bOpen_ = false;
249  } else {
250  BIASWARN("closing non-open image package");
251  }
252  return 0;
253 }
unsigned int GetDepth() const
returns the bytes per channel, which is the sizeof(StorageType) Should match GetSizeDepth(GetStorageT...
Definition: ImageBase.hh:328
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
MetaData * GetMetaData()
Definition: ImageBase.hh:456
unsigned int GetWidth() const
Definition: ImageBase.hh:312
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
std::ostream & WriteBinary(std::ostream &os) const
Writes only the meta datas where AppData::tag!=MD_USE_ASCII to os.
Definition: MetaData.cpp:439
void Release(const bool reset_storage_type=false)
Free the allocated data structures Hands off: Do !!NOT!! change the default of reset_storage_type: Im...
Definition: ImageBase.cpp:350
This is the base class for images in BIAS.
Definition: ImageBase.hh:102