1 #include <Image/ImagePackage.hh>
6 ImagePackage::ImagePackage() {
14 ImagePackage::~ImagePackage() {
20 int ImagePackage::Create(
const string filename,
unsigned int num) {
23 string fn = filename + FileHandling::LeadingZeroString(fileNum_, 4) +
".pack";
25 ofs_.open(fn.c_str(), ios::binary);
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);
38 if (fname.rfind(
"\\")!=string::npos) {
39 fname = fname.substr(fname.rfind(
"\\")+1);
48 stringstream metadata;
58 if (ofs_.tellp() > 1073741824) {
60 Create(filename_, ++fileNum_);
66 int ImagePackage::Open(
const string filename) {
68 if (filename_.find(
"0001.pack") != string::npos) {
69 filename_ = filename_.substr(0,filename_.find(
"0001.pack"));
76 string fn = filename_ + FileHandling::LeadingZeroString(fileNum_, 4) +
".pack";
78 test->open(fn.c_str(), ios::binary);
92 int ImagePackage::GetImage(
const int imageNum,
ImageBase &image,
string &filename) {
94 while (imageNum < currentImg_) {
95 if (GetPrevImage(image, filename)!=0) {
96 cout <<
"GetImage: invalid image number" << endl;
100 while (imageNum > currentImg_) {
101 if (GetNextImage(image, filename)!=0) {
102 cout <<
"GetImage: invalid image number" << endl;
109 int ret = GetNextImage(image, filename);
113 int ImagePackage::GetNextImage(
ImageBase &image,
string &filename) {
116 ifs_[currentFile_]->peek();
117 if (ifs_[currentFile_]->eof()) {
118 if ((
unsigned int)currentFile_+1<ifs_.size()) {
125 if (ifs_[currentFile_]->good() ==
false) {
126 BIASERR(
"file is corrupt. maybe not closed correctly?");
130 if ((
int)fileInfos_.size()<=currentImg_) {
132 fileInfos_.push_back(ipf);
136 fileInfos_[currentImg_].fileNum = (
unsigned char) currentFile_;
137 fileInfos_[currentImg_].offset = (
unsigned long) ifs_[currentFile_]->tellg();
138 *ifs_[currentFile_] >> ipih;
140 if (ifs_[currentFile_]->good() ==
false) {
141 BIASERR(
"file is corrupt. maybe not closed correctly?");
146 if (ipih.len_filename>0) {
147 stringstream fnamestream;
148 for (
unsigned int i=0;i<ipih.len_filename; i++) {
150 ifs_[currentFile_]->get(c);
153 filename = fnamestream.str();
154 if (filename.find(
".mip") == string::npos) {
157 fileInfos_[currentImg_].fileName = filename;
159 fileInfos_[currentImg_].fileName = filename_ + FileHandling::LeadingZeroString(currentImg_, 5) +
".mip";
161 *ifs_[currentFile_] >> image;
162 if (ifs_[currentFile_]->good() ==
false) {
163 BIASERR(
"file is corrupt. maybe not closed correctly?");
167 if (ipih.len_metadata>0) {
168 stringstream metastream;
169 for (
unsigned int i=0;i<ipih.len_metadata; i++) {
171 ifs_[currentFile_]->get(c);
174 metastream >> *image.GetMetaData();
176 if (ifs_[currentFile_]->good() ==
false) {
177 BIASERR(
"file is corrupt. maybe not closed correctly?");
185 int ImagePackage::GetPrevImage(
ImageBase &image,
string &filename) {
191 ifs_[currentFile_]->seekg(ipf.
offset);
192 GetNextImage(image, filename);
193 ifs_[currentFile_]->seekg(ipf.
offset);
198 int ImagePackage::WriteSingleImage(
const unsigned int imageNum,
const string filename) {
201 while (imageNum >= fileInfos_.size()) {
203 if (GetNextImage(image, tmp)!=0) {
204 cout <<
"WriteSingleImage: invalid image number" << endl;
210 unsigned int fidx = ipf.
fileNum;
211 unsigned long int curpos = (
unsigned long) ifs_[fidx]->tellg();
212 ifs_[fidx]->seekg(ipf.
offset);
214 GetNextImage(image, fname);
215 ImageIO::Save(fname, image);
216 ifs_[fidx]->seekg(curpos);
220 int ImagePackage::WriteAllImages(
const string filename) {
221 for (
unsigned int i=0;i<ifs_.size();i++) {
226 int len = filename.length();
229 while (GetNextImage(myImg, fname)==0) {
231 fname = filename + FileHandling::LeadingZeroString(++n,5) +
".mip";
233 cout <<
"ImagePackage saving: " << fname << endl;
234 ImageIO::Save(fname, myImg);
240 int ImagePackage::Close() {
243 for (
unsigned int i=0;i<ifs_.size();i++)
250 BIASWARN(
"closing non-open image package");
unsigned int GetDepth() const
returns the bytes per channel, which is the sizeof(StorageType) Should match GetSizeDepth(GetStorageT...
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
unsigned int GetWidth() const
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
unsigned int GetHeight() const
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...
This is the base class for images in BIAS.