Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasimagepackage.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 <Base/Image/Image.hh>
26 #include <Base/Image/ImageConvert.hh>
27 #include <Base/Image/ImageIO.hh>
28 #include <Image/ImagePackage.hh>
29 #include <Image/Camera.hh>
30 #include <Utils/IOUtils.hh>
31 #include <Utils/Param.hh>
32 #if !defined(WIN32) && defined(BIAS_HAVE_DC1394)
33 #include <Image/ConvertBayerPattern.hh>
34 #endif
35 
36 using namespace std;
37 using namespace BIAS;
38 
39 /**
40  @file
41  @ingroup g_tools
42  @brief Unpacks image packages written by ShowCamWx, see biasimagepackage.cpp
43  @author fkellner
44  */
45 int main(int argc, char* argv[])
46 {
47 
48  Param param;
49  bool *pack = param.AddParamBool("pack", "pack ImageList to package", false,
50  'p');
51  bool *unpack = param.AddParamBool("unpack",
52  "unpack package, use output to override filenames from package", false,
53  'u');
54  string *infile = param.AddParamString("input", "file to process", "", 'i');
55  string
56  *outfile =
57  param.AddParamString(
58  "output",
59  "basename of output file(s) (a number and file extension will be added by the program)",
60  "", 'o');
61 
62  string *proj = param.AddParamString("camera",
63  "optional camera projection xml file to be set in extracted images", "",
64  'c');
65  string *lut = param.AddParamString("lut",
66  "apply lookuptable mapping with lut file", "", 'l');
67  bool *toRgb = param.AddParamBool("rgb", "optional convert to rgb", false);
68  string
69  *bayer =
70  param.AddParamString(
71  "bayer",
72  "if convert to rgb is active, assume this bayer pattern if color model is CM_Grey in the input image",
73  "", 'b');
74  string *ext = param.AddParamString("ext",
75  "extension of images to save (in unpack mode)", "mip", 'e');
76 
77  if (!IOUtils::ParseCommandLineEvalHelp(param, argc, argv))
78  {
79  return 0;
80  }
81 
82  bool bSetProj = false;
83  Projection p;
84  if (proj->length() > 0)
85  {
86  p.Load(*proj);
87  bSetProj = true;
88  }
89 
90  bool bLutMap = false;
92  unsigned int w = 0, h = 0;
94  if (lut->length() > 0)
95  {
96  if (!bSetProj)
97  {
98  BIASERR("you have to add a sink proj for mapping to work");
99  BIASABORT;
100  }
101  p.GetParameters()->GetImageSize(w, h);
102  bm.SetLookupTable(*lut, method);
103  bLutMap = true;
104  }
105 
106  ImageBase im;
108 #if !defined(WIN32) && defined(BIAS_HAVE_DC1394)
110 #endif
111 
112  if (*pack)
113  {
114  vector<string> imageNames;
115  if (Param::ParseListFile(*infile, imageNames) != 0)
116  {
117  BIASERR("error parsing image names!");
118  return -1;
119  }
120  ImagePackage ip;
121  ip.Create(*outfile);
122 
123  for (unsigned int i = 0; i < imageNames.size(); i++)
124  {
125 
126  cout << "adding image: " << imageNames[i] << endl;
127  if (!im.IsEmpty()) {
128  im.Release(true);
129  }
130  ImageIO::Load(imageNames[i], im);
131  if (*toRgb)
132  {
133  MetaData meta = *im.GetMetaData();
134  if (im.GetStorageType() != ImageBase::ST_unsignedchar) {
135  BIASERR("to rgb only for unsigned char images.");
136  BIASABORT;
137  }
138  Image<unsigned char> ucharsrc( im.GetWidth(), im.GetHeight(), im.GetChannelCount());
139  Image<unsigned char> uchardst( im.GetWidth(), im.GetHeight(), 3);
140  ucharsrc.CopyIn_NoInit( (unsigned char*)im.GetImageData());
141 #if !defined(WIN32) && defined(BIAS_HAVE_DC1394)
142  if (ucharsrc.GetChannelCount() == 1)
143  demosaic.Convert(ucharsrc, uchardst);
144  else
145  ImageConvert::ToRGB(ucharsrc, uchardst);
146 #else
147  ImageConvert::ToRGB(ucharsrc, uchardst);
148 #endif
149  uchardst.SetMetaData( meta );
150  im = uchardst;
151  }
152  if (bLutMap)
153  {
154  if (im.GetStorageType() != ImageBase::ST_unsignedchar) {
155  BIASERR("lut map only for unsigned char images.");
156  BIASABORT;
157  }
158  Image<unsigned char> ucharsrc( im.GetWidth(), im.GetHeight(), im.GetChannelCount());
159  Image<unsigned char> uchardst( im.GetWidth(), im.GetHeight(), im.GetChannelCount());
160  ucharsrc.CopyIn_NoInit( (unsigned char*)im.GetImageData());
161  uchardst.FillImageWithConstValue((unsigned char)0);
162  bm.MapWithLookupTable(ucharsrc, uchardst, method);
163  uchardst.SetMetaData( *im.GetMetaData() );
164  im = uchardst;
165  }
166  if (bSetProj)
167  {
168  MetaData meta = *im.GetMetaData();
169  tmp.SetMetaData( meta );
170  tmp.SetProj(p);
171  tmp.UpdateMetaData();
172  im.SetMetaData( *tmp.GetMetaData() );
173  }
174 
175  ip.AddImage(im, imageNames[i]);
176 
177  }
178 
179  ip.Close();
180 
181  }
182  else if (*unpack)
183  {
184  if (infile->find(".pack") == string::npos)
185  {
186  cout << "input file should end with .pack" << endl;
187  return 0;
188  }
189  string fname = *infile;
190  unsigned pos = fname.find(".pack");
191  pos -=4; //substract 4 for 0001 etc
192  fname = fname.substr(0,pos);
193  ImagePackage ip;
194  ip.Open(fname);
195  cout<<"*** Opened package ***: "<<fname<<endl;
196 
197  //ip.WriteAllImages(*outfile);
198  string filename;
199  int n = 0;
200  while (ip.GetNextImage(im, filename) == 0)
201  {
202 
203  if (*toRgb && bayer->length() != 0 && im.GetColorModel()
204  == ImageBase::CM_Grey)
205  {
208  if (strcmp(bayer->c_str(), "BGGR") == 0)
209  {
210  cout << "converting bayer BGGR to RGB" << endl;
211  pattern = ImageBase::CM_Bayer_BGGR;
212  }
213  else if (strcmp(bayer->c_str(), "GBRG") == 0)
214  {
215  cout << "converting bayer GBRG to RGB" << endl;
216  pattern = ImageBase::CM_Bayer_GBRG;
217  }
218  else if (strcmp(bayer->c_str(), "GRBG") == 0)
219  {
220  cout << "converting bayer GRBG to RGB" << endl;
221  pattern = ImageBase::CM_Bayer_GRBG;
222  }
223  else if (strcmp(bayer->c_str(), "RGGB") == 0)
224  {
225  cout << "converting bayer RGGB to RGB" << endl;
226  pattern = ImageBase::CM_Bayer_RGGB;
227  }
228  else
229  {
230  cout << *bayer << "? i dont know this pattern... abort!"
231  << endl;
232  BIASABORT;
233  }
234 
235  im.SetColorModel(pattern);
236  }
237 
238  if (*toRgb)
239  {
240  MetaData meta = *im.GetMetaData();
241  if (im.GetStorageType() != ImageBase::ST_unsignedchar) {
242  BIASERR("to rgb only for unsigned char images.");
243  BIASABORT;
244  }
245  Image<unsigned char> ucharsrc( im.GetWidth(), im.GetHeight(), im.GetChannelCount());
246  Image<unsigned char> uchardst( im.GetWidth(), im.GetHeight(), 3);
247  ucharsrc.CopyIn_NoInit( (unsigned char*)im.GetImageData());
248 #if !defined(WIN32) && defined(BIAS_HAVE_DC1394)
249  if (ucharsrc.GetChannelCount() == 1)
250  demosaic.Convert(ucharsrc, uchardst);
251  else
252  ImageConvert::ToRGB(ucharsrc, uchardst);
253 #else
254  ImageConvert::ToRGB(ucharsrc, uchardst);
255 #endif
256  uchardst.SetMetaData( meta );
257  im = uchardst;
258  }
259  if (bLutMap)
260  {
261  if (im.GetStorageType() != ImageBase::ST_unsignedchar) {
262  BIASERR("lut map only for unsigned char images.");
263  BIASABORT;
264  }
265  Image<unsigned char> ucharsrc( im.GetWidth(), im.GetHeight(), im.GetChannelCount());
266  Image<unsigned char> uchardst( im.GetWidth(), im.GetHeight(), im.GetChannelCount());
267  ucharsrc.CopyIn_NoInit( (unsigned char*)im.GetImageData());
268  uchardst.FillImageWithConstValue((unsigned char)0);
269  bm.MapWithLookupTable(ucharsrc, uchardst, method);
270  uchardst.SetMetaData( *im.GetMetaData() );
271  im = uchardst;
272  }
273  if (bSetProj)
274  {
275  MetaData meta = *im.GetMetaData();
276  tmp.SetMetaData( meta );
277  tmp.ParseMetaData();
278  tmp.SetProj(p);
279  tmp.UpdateMetaData();
280  im.SetMetaData( *tmp.GetMetaData());
281  }
282  if (outfile->length() > 0)
283  {
284  filename = *outfile + FileHandling::LeadingZeroString(++n, 5)
285  + "." + (*ext);
286  }
287  else if (ext->compare("mip") != 0)
288  {
289  filename = filename.substr(0, filename.rfind("."));
290  filename += "." + (*ext);
291  }
292  cout << "saving: " << filename << endl;
293  ImageIO::Save(filename, im);
294  }
295 
296  }
297  return 0;
298 
299 }
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
InterpolationMethod
accuracy for resampling
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
int SetLookupTable(const std::string &filename, InterpolationMethod &method)
Loads the LUT from a file, replaces the call to PrepareLookupTableMapping().
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
void CopyIn_NoInit(void *data)
Take some data and fill it into the Image.
Definition: ImageBase.cpp:827
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
MetaData * GetMetaData()
Definition: ImageBase.hh:456
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:305
int Open(const std::string filename)
open a package
unsigned int GetWidth() const
Definition: ImageBase.hh:312
Wrapper class to dc1394 bayer conversion class.
int Close()
close package
const void * GetImageData() const
Definition: ImageBase.hh:280
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
int SetProj(const Projection &Proj)
Definition: Camera.hh:106
unsigned int GetHeight() const
Definition: ImageBase.hh:319
An Image Package is multiple .mip images in one file.
Definition: ImagePackage.hh:20
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
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
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
int UpdateMetaData()
copy P_ and co.
Definition: Camera.cpp:446
int GetNextImage(ImageBase &image, std::string &filename)
get next image, call subsequently to walk through package file
This class Param provides generic support for parameters.
Definition: Param.hh:231
int MapWithLookupTable(const Image< InputStorageType > &src, Image< OutputStorageType > &sink, InterpolationMethod method)
applies precomputed coordinates in src, fast for repeated usages of same mapping function ...
this class collects all additional data chunks of type AppData to be written into/read from an image ...
Definition: MetaData.hh:121
invalid (not set) image format
Definition: ImageBase.hh:129
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
void SetMetaData(const MetaData &m)
Definition: ImageBase.hh:470
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
int Create(const std::string filename, unsigned int num=1)
Create image Package file.
int ParseMetaData(bool bUse2x64bitTS=true)
After ImageIO::Load() operated on AppData_, this method fills P_, Timestamp, DC_*, ...
Definition: Camera.cpp:154
int Convert(Image< StorageType > &src, Image< StorageType > &dst, BayerDemosaicMethod method=BAYER_DEMOSAIC_METHOD_AHD, ImageBase::EColorModel bayer_pattern=ImageBase::CM_invalid)
converts image with bayerpattern to rgb.
int AddImage(const ImageBase image, const std::string filename="")
Adding an image with empty filename will use default name.