Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasConvertMDtimestamps.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 
25 #include <Utils/Param.hh>
26 #include <Base/Image/ImageIO.hh>
27 #include <Image/Camera.hh>
28 #include <sstream>
29 
30 using namespace std;
31 using namespace BIAS;
32 
33 /**
34  @file
35  @ingroup g_tools
36  @brief Convert input image format to output image format preserving timestamps see biasConvertMDtimestamps.cpp
37  @author MIP
38 */
39 int convertImage(string &input, string &output) {
40 
41  //tmp input image
42  ImageBase img;
43 
44  // load input image, cancel programm if image read fails
45  cout << "Loading " << input << endl << flush;
46  if (ImageIO::Load(input,img) < 0) {
47  cerr << "Error loading image!" << endl << flush;
48  return -1;
49  }
50 
51  //convert image to BIAS::Camera format by loading, parsing meta data using
52  //old format, update with new format and writing back
53  switch (img.GetStorageType()) {
54  case ImageBase::ST_invalid :
55  cerr << "Invalid storage type for image: " << input << endl << flush;
56  return -1;
57  break;
58  case ImageBase::ST_unsignedchar :
59  {
60  Camera<unsigned char> cam(img);
61  cam.ParseMetaData(false);
62  cam.UpdateMetaData();
63  if (ImageIO::Save(output,cam) < 0) {
64  cerr << "Error writing file " << output << endl;
65  return -1;
66  }
67  }
68  break;
69 #ifdef BUILD_IMAGE_CHAR
70  case ImageBase::ST_char :
71  {
72  Camera<char> cam(img);
73  cam.ParseMetaData(false);
74  cam.UpdateMetaData();
75  if (ImageIO::Save(output,cam) < 0) {
76  cerr << "Error writing file " << output << endl;
77  return -1;
78  }
79  }
80  break;
81 #endif //BUILD_IMAGE_CHAR
82 #ifdef BUILD_IMAGE_USHORT
83  case ImageBase::ST_unsignedshortint :
84  {
86  cam.ParseMetaData(false);
87  cam.UpdateMetaData();
88  if (ImageIO::Save(output,cam) < 0) {
89  cerr << "Error writing file " << output << endl;
90  return -1;
91  }
92  }
93  break;
94 #endif //BUILD_IMAGE_USHORT
95 #ifdef BUILD_IMAGE_SHORT
96  case ImageBase::ST_shortint :
97  {
98  Camera<short int> cam(img);
99  cam.ParseMetaData(false);
100  cam.UpdateMetaData();
101  if (ImageIO::Save(output,cam) < 0) {
102  cerr << "Error writing file " << output << endl;
103  return -1;
104  }
105  }
106  break;
107 #endif //BUILD_IMAGE_UINT
108 #ifdef BUILD_IMAGE_UINT
109  case ImageBase::ST_unsignedint :
110  {
111  Camera<unsigned int> cam(img);
112  cam.ParseMetaData(false);
113  cam.UpdateMetaData();
114  if (ImageIO::Save(output,cam) < 0) {
115  cerr << "Error writing file " << output << endl;
116  return -1;
117  }
118  }
119  break;
120 #endif //BUILD_IMAGE_UINT
121 #ifdef BUILD_IMAGE_INT
122  case ImageBase::ST_int :
123  {
124  Camera<int> cam(img);
125  cam.ParseMetaData(false);
126  cam.UpdateMetaData();
127  if (ImageIO::Save(output,cam) < 0) {
128  cerr << "Error writing file " << output << endl;
129  return -1;
130  }
131  }
132  break;
133 #endif //BUILD_IMAGE_INT
134  case ImageBase::ST_float :
135  {
136  Camera<float> cam(img);
137  cam.ParseMetaData(false);
138  cam.UpdateMetaData();
139  if (ImageIO::Save(output,cam) < 0) {
140  cerr << "Error writing file " << output << endl;
141  return -1;
142  }
143  }
144  break;
145 #ifdef BUILD_IMAGE_DOUBLE
146  case ImageBase::ST_double :
147  {
148  Camera<double> cam(img);
149  cam.ParseMetaData(false);
150  cam.UpdateMetaData();
151  if (ImageIO::Save(output,cam) < 0) {
152  cerr << "Error writing file " << output << endl;
153  return -1;
154  }
155  }
156  break;
157 #endif //BUILD_IMAGE_DOUBLE
158  default:
159  cerr << "unsupported storage type" << endl;
160  break;
161  }
162  cout << "Processed image: " << input << endl;
163  cout << " wrote to: " << output << endl;
164 
165  return 0;
166 }
167 
168 int main(int argc, char* args[]) {
169 
170  //prepare and parse parameters
171  Param parameters;
172  parameters.AddParamString("inputlist",
173  "Give filename for input filelist (txt-format with images seperated by whitespaces)",
174  "",'I', GRP_ALL);
175  parameters.AddParamString("inputfile", "Give filename for input file","",'i', GRP_ALL);
176  parameters.AddParamString("outputlist",
177  "Give filename for output filelist (txt-format with images seperated by whitespaces)",
178  "",'O', GRP_ALL);
179  parameters.AddParamString("outputfile", "Give filename for output file","",'o', GRP_ALL);
180  parameters.ParseCommandLine (argc, args);
181  bool bUseList = false;
182  bool bUseFile = false;
183  string *in_filelist = NULL;
184  in_filelist = parameters.GetParamString("inputlist");
185  string *in_file = NULL;
186  in_file = parameters.GetParamString("inputfile");
187  string *out_filelist = NULL;
188  out_filelist = parameters.GetParamString("outputlist");
189  string *out_file = NULL;
190  out_file = parameters.GetParamString("outputfile");
191  if ((*in_filelist != "")&&(*out_filelist != "")) {
192  bUseList = true;
193  }
194  if ((*in_file != "")&&(*out_file != "")) {
195  bUseFile = true;
196  }
197 
198  //no filename or filelist given
199  if (!bUseFile && !bUseList) {
200  cerr << "Give either in-/output file or in-/output filelist (--help for options)!" << endl;
201  exit(-1);
202  }
203 
204  //convert single img
205  if (bUseFile) {
206  string inFile(*in_file);
207  string outFile(*out_file);
208  if (convertImage(inFile, outFile) < 0) {
209  exit(-1);
210  }
211  }
212 
213  //convert img list
214  if (bUseList) {
215  string inFile("");
216  string outFile("");
217  ifstream inlist(in_filelist->c_str());
218  ifstream outlist(out_filelist->c_str());
219  while((inlist >> inFile) && (outlist >> outFile)) {
220  if (convertImage(inFile, outFile) < 0)
221  exit(-1);
222  inFile.clear();
223  outFile.clear();
224  }
225  }
226 
227  cout << "finished processing" << endl << flush;
228 }
Definition: f2c.h:98
int ParseCommandLine(int &argc, char *argv[])
scan command line arguments for valid parameters
Definition: Param.cpp:1028
std::string * GetParamString(const std::string &name) const
Definition: Param.cpp:649
This class Param provides generic support for parameters.
Definition: Param.hh:231
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
std::string * AddParamString(const std::string &name, const std::string &help, std::string deflt="", char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:327