Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PMDImageIO.hh
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, Save to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __BIAS_PMDIMAGEIO_H__
26 #define __BIAS_PMDIMAGEIO_H__
27 
28 #include <bias_config.h>
29 
30 #ifdef BIAS_HAVE_TIFF
31 #include <tiffio.h>
32 // check TIFF version from tiffvers.h
33 #if TIFFLIB_VERSION < 20051230
34 # error You need at least TIFF version 3.8.0 (TIFFLIB_VERSION >= 20051230) for PMD multi plane tiff.
35 #endif
36 #endif //BIAS_HAVE_TIFF
37 
38 #include <string>
39 #include <Base/Image/Image.hh>
40 
41 #ifdef BIAS_HAVE_XML2
42 # include <Base/Common/XMLIO.hh>
43 #endif
44 
45 
46 #ifdef BIAS_HAVE_TIFF
47 // Define user specific TIFF-Tags
48 
49 /* tags 33550 is a private tag registered to SoftDesk, Inc */
50 #define TIFFTAG_PRINCIPALPOINT 33550
51 /* tags 33920-33921 are private tags registered to Intergraph, Inc */
52 #define TIFFTAG_FOCALLENGTHX 33920
53 #define TIFFTAG_ASPECTRATIO 33921
54 /* tags 34263-34264 are private tags registered to NASA-JPL Carto Group */
55 #define TIFFTAG_UNDISTORTION 34263
56 /* tags 34735-3438 are private tags registered to SPOT Image, Inc */
57 #define TIFFTAG_CAMERACENTER 34736
58 #define TIFFTAG_CAMERAROTATION 34737
59 #endif //BIAS_HAVE_TIFF
60 
61 
62 namespace BIAS {
63 
64  /** @struct PMDImageMetaData
65  @brief Store extrinsic and intrinsic camera parameters for
66  Rotation is in axis * angle representation
67  image padding and Metadata.
68  @author streckel 02/2006
69  */
70  struct BIASImage_EXPORT PMDImageMetaData {
71  unsigned int width;
72  unsigned int height;
73  unsigned int channels;
74  double PrincipalX;
75  double PrincipalY;
76  double FocallengthX;
77  double AspectRatio;
78  double UndistCoeff[4];
79  double CameraCenter[3];
80  double CameraRotation[3];
81  };
82 
83 #ifdef BIAS_HAVE_TIFF
84  /** @class PMDImageIO
85  @ingroup g_image_base
86  @brief Save 2D/3D-image-data together with some metadata in a
87  single tiff-datafile. The 2D-3D-data is aligned, such that for
88  each pixel in the 2D-image the corresponding depth is
89  stored at the same position in the depth image. A -1 in the depth
90  image indicates that no depth data is available.
91  @author streckel 02/2006 */
92 
93  class BIASImageBase_EXPORT PMDImageIO {
94  public:
95 
96  /** @brief Save a PMD-data 2d/3d-tiff image to disk
97  @param FileName Output filename, should have the extension .tif
98  @param Img2D pointer to the 2d-image data to save, if NULL only
99  depth is written, metadata is also ignored
100  @param ImgDepth pointer to the depth data to save, if NULL only
101  2D-img is written, metadata is also ignored
102  @param MetaData2D the Metadata of the 2D-image to save
103  @param MetaDataDepth the Metadata of the depth image to use for padding
104  @author Birger Streckel **/
105  static int Save(const std::string& FileName,
106  uint8* Img2D, uint16* ImgDepth,
107  uint16* ImgModCoeff,
108  const PMDImageMetaData &MetaData2D,
109  const PMDImageMetaData &MetaDataDepth);
110  static int Save(const std::string& FileName,
111  uint16* Img2D, uint16* ImgDepth,
112  uint16* ImgModCoeff,
113  const PMDImageMetaData &MetaData2D,
114  const PMDImageMetaData &MetaDataDepth);
115 
116  /** @brief Save a PMD-data 2d/3d-tiff image to disk
117  @param FileName Output filename, should have the extension .tif
118  @param img the 2d-image data to save
119  @param depthimg the depth data to save
120  @param MetaData2D the Metadata of the 2D-image to save
121  @param MetaDataDepth the Metadata of the depth image to use for padding
122  @author Birger Streckel **/
123  static int Save(const std::string& FileName,
125  BIAS::Image<float> &ImgDepth,
126  BIAS::Image<float> &ImgModCoeff,
127  PMDImageMetaData &MetaData2D,
128  PMDImageMetaData &MetaDataDepth);
129 
130  /** @brief Load a PMD-data 2d/3d-tiff image from disk
131  @param FileName complete name of file to be loaded
132  @param Img2D to receive the 2d-image data from the file,
133  new memory is allocated, if NULL tif contained no 2D-image
134  @param ImgDepth to receive the depth-image data from the file,
135  new memory is allocated, if NULL tif contained no depth-image
136  @param MetaData to receive the Metadata from the file
137  @author Birger Streckel **/
138  static int Load(const std::string& FileName,
139  uint8* &Img2D, uint16* &ImgDepth,
140  uint16* &ImgModCoeff,
141  PMDImageMetaData &MetaData2D,
142  PMDImageMetaData &MetaDataDepth);
143  static int Load(const std::string& FileName,
144  uint16* &Img2D, uint16* &ImgDepth,
145  uint16* &ImgModCoeff,
146  PMDImageMetaData &MetaData2D,
147  PMDImageMetaData &MetaDataDepth);
148 
149 
150  /** @brief Load a PMD-data 2d/3d-image from disk, automatically detecting
151  TIFF or XML file format and calling the appropriate subroutine.
152  @param FileName complete name of file to be loaded
153  @param img to receive the 2d-image data from the file
154  @param depthimg to receive the depth-image data from the file
155  @param MetaData to receive the Metadata from the file
156  @author Birger Streckel **/
157  static int Load(const std::string& FileName,
158  Image<unsigned char> &Img2D,
159  Image<float> &ImgDepth,
160  Image<float> &ImgModCoeff,
161  PMDImageMetaData &MetaData2D,
162  PMDImageMetaData &MetaDataDepth);
163 
164  /** @brief Load a PMD-data 2d/3d-image from TIFF-file.
165  @author Birger Streckel
166  */
167  static int LoadFromTIFF(const std::string& FileName,
168  Image<unsigned char> &Img2D,
169  Image<float> &ImgDepth,
170  Image<float> &ImgModCoeff,
171  PMDImageMetaData &MetaData2D,
172  PMDImageMetaData &MetaDataDepth);
173 
174  /** @brief Load PMD-image from 3 space separated text files.
175  @author Birger Streckel
176  */
177  static int LoadFromTXT(int width, int height,
178  const std::string& FileName2D,
179  const std::string& FileNameDepth,
180  const std::string& FileNameModCoeff,
181  Image<unsigned char> &Img2D,
182  Image<float> &ImgDepth,
183  Image<float> &ImgModCoeff,
184  PMDImageMetaData &MetaDataDepth);
185 
186 #ifdef BIAS_HAVE_XML2
187  /** @brief Load a PMD-data 2d/3d-image from PMDtec's XMK format.
188  The 1st image is Load if the file contains a sequence.
189  @author Birger Streckel
190  */
191  static int LoadFromXML(const std::string& FileName,
192  Image<unsigned char> &Img2D,
193  Image<float> &ImgDepth,
194  Image<float> &ImgModCoeff,
195  PMDImageMetaData &MetaData2D,
196  PMDImageMetaData &MetaDataDepth);
197 
198  static int LoadFromXML(const std::string& FileName,
199  std::vector<Image<unsigned char> > &Imgs2D,
200  std::vector<Image<float> > &ImgDepth,
201  std::vector<Image<float> > &ImgModCoeff,
202  std::vector<PMDImageMetaData> &MetaData2D,
203  std::vector<PMDImageMetaData> &MetaDataDepth);
204 
205  /** @brief Load a PMD-image in the PMD-technologies XML-format from disk
206  for a specification see the CamVis Pro instruction manual. Data from
207  tag <grayscale> is Load to Img2D, <distance> is Load to ImgDepth and
208  <amplitude> is Load to ImgModCoeff. All other tags <rawXY> are
209  ignored. Is a sequence is given, only the first image is Load.
210  @param FileName complete name of file to be loaded
211  @param Img2D to receive the 2d-image data from the file,
212  new memory is allocated, if NULL tif contained no 2D-image
213  @param ImgDepth to receive the depth-image data from the file,
214  new memory is allocated, if NULL tif contained no depth-image
215  @param MetaData to receive the Metadata from the file
216  @author Birger Streckel **/
217  static int LoadXML(const std::string& FileName,
218  uint16* &Img2D, uint16* &ImgDepth,
219  uint16* &ImgModCoeff,
220  PMDImageMetaData &MetaData2D,
221  PMDImageMetaData &MetaDataDepth);
222  static int LoadXML(XMLIO &myXML, xmlNodePtr &rootNode,
223  uint16* &Img2D, uint16* &ImgDepth,
224  uint16* &ImgModCoeff,
225  PMDImageMetaData &MetaData2D,
226  PMDImageMetaData &MetaDataDepth);
227 
228 #endif
229 
230  /** @brief Returns the bit representation (8 or 16) for the 2D-image in
231  the PMD-image filename. 0 if there is no 2D image, -1 in case of
232  error.
233  @author Birger Streckel
234  */
235  static int GetBitRep(const std::string& FileName);
236 
237  protected:
238 
239  /* set up callback for initialization of new tags*/
240  static void NewTagsInitialize_();
241 
242 
243  /* callback for initialization of new tags*/
244  static void NewTagsCallback_(TIFF *tif);
245 
246  static int LoadMetaData_(TIFF* tiffimage,
248 
249  static int Load2DImage8Bit_(TIFF *tiffimage, uint8* &Img2D,
250  PMDImageMetaData &MetaData2D);
251  static int Load2DImage16Bit_(TIFF *tiffimage, uint16* &Img2D,
252  PMDImageMetaData &MetaData2D);
253  static int LoadDepthImage_(TIFF *tiffimage, uint16* &ImgDepth,
254  PMDImageMetaData &MetaDataDepth);
255  static int LoadModCoeffImage_(TIFF *tiffimage, uint16* &ImgModCoeff,
256  PMDImageMetaData MetaDataDepth);
257 
258  static void Grey16to8Bit_(uint16* src, uint8* &dst, unsigned int size);
259  static uint16 DepthConvert_(float depth);
260  static float DepthConvert_(uint16 depth);
261  static uint16 ModCoeffConvert_(float modcoeff);
262  static float ModCoeffConvert_(uint16 modcoeff);
263 
264  static int PostprocessData_(uint8* data2d, uint16* dataDepth,
265  uint16* dataModCoeff,
266  Image<unsigned char> &Img2D,
267  Image<float> &ImgDepth,
268  Image<float> &ImgModCoeff,
269  PMDImageMetaData &MetaData2D,
270  PMDImageMetaData &MetaDataDepth);
271 
272 
273 
274  static uint16 Convert_(float val, float lower, float upper);
275  static float Convert_(uint16 val, float lower, float upper);
276 
277 
278  static int SaveMetaData_(TIFF* tiffimage,
279  const PMDImageMetaData &MetaData);
280 
281  static int Save2DImage8Bit_(TIFF *tiffimage, uint8* Img2D,
282  PMDImageMetaData MetaData2D,
283  int LinesPerStrip);
284  static int Save2DImage16Bit_(TIFF *tiffimage, uint16* Img2D,
285  PMDImageMetaData MetaData2D,
286  int LinesPerStrip);
287  static int SaveDepthImage_(TIFF *tiffimage, uint16* ImgDepth,
288  PMDImageMetaData MetaDataDepth,
289  int LinesPerStrip);
290  static int SaveModCoeffImage_(TIFF *tiffimage, uint16* ImgModCoeff,
291  PMDImageMetaData MetaDataDepth,
292  int LinesPerStrip);
293 
294  static TIFFExtendProc ParentExtender;
295  };
296 #endif //BIAS_HAVE_TIFF
297 }
298 #endif
299 
static TIFFExtendProc ParentExtender
Definition: PMDImageIO.hh:294
Save 2D/3D-image-data together with some metadata in a single tiff-datafile.
Definition: PMDImageIO.hh:93
Wrapper class for reading and writing XML files based on the XML library libxml2. ...
Definition: XMLIO.hh:72
unsigned int channels
Definition: PMDImageIO.hh:73
unsigned int width
Definition: PMDImageIO.hh:71
unsigned int height
Definition: PMDImageIO.hh:72
Store extrinsic and intrinsic camera parameters for Rotation is in axis * angle representation image ...
Definition: PMDImageIO.hh:70
this class collects all additional data chunks of type AppData to be written into/read from an image ...
Definition: MetaData.hh:121