OTB  6.7.0
Orfeo Toolbox
otbImageIOBase.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbImageIOBase_h
23 #define otbImageIOBase_h
24 
25 #include "itkLightProcessObject.h"
26 #include "itkIndent.h"
27 #include "itkImageIORegion.h"
28 #include "vnl/vnl_vector.h"
29 
30 #include <string>
31 #include <typeinfo>
32 #include <vector>
33 
34 #include "OTBImageBaseExport.h"
35 
36 namespace otb
37 {
38 
68 class OTBImageBase_EXPORT ImageIOBase : public itk::LightProcessObject
69 {
70 public:
72  typedef ImageIOBase Self;
75 
77  itkTypeMacro(ImageIOBase, Superclass);
78 
80  itkSetStringMacro(FileName);
81  itkGetStringMacro(FileName);
83 
85  typedef long IndexValueType;
86  typedef unsigned long SizeValueType;
87 
94  class UnknownType {};
95 
99  typedef enum {UNKNOWNPIXELTYPE,SCALAR,RGB,RGBA,OFFSET,VECTOR,
100  POINT,COVARIANTVECTOR,SYMMETRICSECONDRANKTENSOR,
101  DIFFUSIONTENSOR3D,COMPLEX,FIXEDARRAY,MATRIX} IOPixelType;
102 
107  typedef enum {UNKNOWNCOMPONENTTYPE,UCHAR,CHAR,USHORT,SHORT,UINT,INT,
108  ULONG,LONG, FLOAT,DOUBLE, CSHORT, CINT, CFLOAT, CDOUBLE} IOComponentType;
109 
113  void SetNumberOfDimensions(unsigned int);
114  itkGetConstMacro(NumberOfDimensions, unsigned int);
116 
120  virtual void SetDimensions(unsigned int i, unsigned int dim);
121  virtual unsigned int GetDimensions(unsigned int i) const
122  { return m_Dimensions[i]; }
124 
127  virtual void SetOrigin(unsigned int i, double origin);
128  virtual double GetOrigin(unsigned int i) const
129  {
130  return m_Origin[i];
131  }
133 
136  virtual void SetSpacing(unsigned int i, double spacing);
137  virtual double GetSpacing(unsigned int i) const
138  {
139  return m_Spacing[i];
140  }
142 
145  virtual void SetDirection(unsigned int i, std::vector<double> &direction);
146  virtual void SetDirection(unsigned int i, vnl_vector<double> &direction);
147  virtual std::vector<double> GetDirection(unsigned int i) const
148  {
149  return m_Direction[i];
150  }
152 
155  virtual std::vector<double> GetDefaultDirection(unsigned int i) const;
156 
163  itkSetMacro(IORegion, itk::ImageIORegion);
164  itkGetConstReferenceMacro(IORegion, itk::ImageIORegion);
166 
172  itkSetEnumMacro(PixelType, IOPixelType);
173  itkGetEnumMacro(PixelType, IOPixelType);
175 
182  virtual bool SetPixelTypeInfo(const std::type_info& ptype);
183 
186  itkSetEnumMacro(ComponentType,IOComponentType);
187  itkGetEnumMacro(ComponentType,IOComponentType);
188  virtual const std::type_info& GetComponentTypeInfo() const;
190 
195  itkSetMacro(NumberOfComponents,unsigned int);
196  itkGetConstReferenceMacro(NumberOfComponents,unsigned int);
198 
200  itkSetMacro(UseCompression,bool);
201  itkGetConstMacro(UseCompression,bool);
202  itkBooleanMacro(UseCompression);
204 
206  itkSetMacro(UseStreamedReading,bool);
207  itkGetConstMacro(UseStreamedReading,bool);
208  itkBooleanMacro(UseStreamedReading);
210 
212  itkSetMacro(UseStreamedWriting,bool);
213  itkGetConstMacro(UseStreamedWriting,bool);
214  itkBooleanMacro(UseStreamedWriting);
216 
217 
220  static std::string GetComponentTypeAsString(IOComponentType);
221 
224  static std::string GetPixelTypeAsString(IOPixelType);
225 
228  typedef enum {ASCII,Binary,TypeNotApplicable} FileType;
229 
232  typedef enum {BigEndian,LittleEndian,OrderNotApplicable} ByteOrder;
233 
236  itkSetEnumMacro(FileType,FileType);
237  itkGetEnumMacro(FileType,FileType);
239  {
240  this->SetFileType(ASCII);
241  }
243  {
244  this->SetFileType(Binary);
245  }
247 
259  itkSetEnumMacro(ByteOrder,ByteOrder);
260  itkGetEnumMacro(ByteOrder,ByteOrder);
262  {
263  this->SetByteOrder(BigEndian);
264  }
266  {
267  this->SetByteOrder(LittleEndian);
268  }
270 
273  static std::string GetFileTypeAsString(FileType);
274 
277  static std::string GetByteOrderAsString(ByteOrder);
278 
280  typedef std::streamoff SizeType;
281 
283  typedef size_t BufferSizeType;
284 
291  virtual SizeType GetPixelStride () const;
292 
294  SizeType GetImageSizeInPixels() const;
295 
297  SizeType GetImageSizeInBytes() const;
298 
301  SizeType GetImageSizeInComponents() const;
302 
303 
308  virtual unsigned int GetComponentSize() const;
309 
312  virtual unsigned int GetOverviewsCount() = 0;
313 
316  virtual std::vector<std::string> GetOverviewsInfo() = 0;
317 
320  virtual void SetOutputImagePixelType( bool isComplexInternalPixelType,
321  bool isVectorImage) = 0;
322 
323  /*-------- This part of the interfaces deals with reading data ----- */
324 
327  virtual bool CanReadFile(const char*) = 0;
328 
331  virtual bool CanStreamRead()
332  {
333  return false;
334  }
335 
338  virtual void ReadImageInformation() = 0;
339 
341  virtual void Read(void* buffer) = 0;
342 
343 
344  /*-------- This part of the interfaces deals with writing data ----- */
345 
348  virtual bool CanWriteFile(const char*) = 0;
349 
355  virtual bool CanStreamWrite()
356  {
357  return false;
358  }
359 
362  virtual void WriteImageInformation() = 0;
363 
367  virtual void Write( const void* buffer) = 0;
368 
369  /* --- Support reading and writing data as a series of files. --- */
370 
376  virtual bool SupportsDimension(unsigned long dim)
377  {
378  return (dim == 2);
379  }
380 
392  virtual itk::ImageIORegion
393  GenerateStreamableReadRegionFromRequestedRegion( const itk::ImageIORegion & requested ) const;
394 
395 
410  virtual unsigned int GetActualNumberOfSplitsForWriting(unsigned int numberOfRequestedSplits,
411  const itk::ImageIORegion &pasteRegion,
412  const itk::ImageIORegion &largestPossibleRegion);
413 
420  virtual itk::ImageIORegion GetSplitRegionForWriting(unsigned int ithPiece,
421  unsigned int numberOfActualSplits,
422  const itk::ImageIORegion &pasteRegion,
423  const itk::ImageIORegion &largestPossibleRegion);
424 
426  typedef std::vector< std::string > ArrayOfExtensionsType;
427 
432  const ArrayOfExtensionsType & GetSupportedReadExtensions() const;
433 
438  const ArrayOfExtensionsType & GetSupportedWriteExtensions() const;
439 
445  void DoMapBuffer(void* buffer, size_t numberOfPixels, std::vector<unsigned int>& bandList);
446 
448  itkGetConstReferenceMacro(AttachedFileNames, std::vector<std::string> );
449 
450 protected:
451  ImageIOBase();
452  ~ImageIOBase() override;
453  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
454 
457 
461 
465 
468 
470  std::string m_FileName;
471 
474  unsigned int m_NumberOfComponents;
475 
477  unsigned int m_NumberOfDimensions;
478 
481 
484 
487 
491 
493  std::vector< SizeValueType > m_Dimensions;
494 
497  std::vector<double> m_Spacing;
498 
500  std::vector<double> m_Origin;
501 
503  std::vector<std::vector<double> > m_Direction;
504 
507  std::vector< SizeType > m_Strides;
508 
510  virtual void Reset(const bool freeDynamic = true);
511 
513  void Resize(const unsigned int numDimensions,
514  const unsigned int* dimensions);
515 
518  virtual unsigned int GetPixelSize() const;
519 
526  void ComputeStrides();
527 
530  SizeType GetComponentStride() const;
531 
534  SizeType GetRowStride () const;
535 
538  SizeType GetSliceStride () const;
539 
541  void WriteBufferAsASCII(std::ostream& os, const void *buffer,
542  IOComponentType ctype,
543  SizeType numberOfBytesToWrite);
544 
546  void ReadBufferAsASCII(std::istream& os, void *buffer,
547  IOComponentType ctype,
548  SizeType numberOfBytesToBeRead);
549 
551  bool ReadBufferAsBinary(std::istream& os, void *buffer, SizeType numberOfBytesToBeRead);
552 
553 
555  void AddSupportedReadExtension( const char * extension );
556 
558  void AddSupportedWriteExtension( const char * extension );
559 
562  virtual unsigned int GetActualNumberOfSplitsForWritingCanStreamWrite(unsigned int numberOfRequestedSplits,
563  const itk::ImageIORegion &pasteRegion) const;
564 
567  virtual itk::ImageIORegion GetSplitRegionForWritingCanStreamWrite(unsigned int ithPiece,
568  unsigned int numberOfActualSplits,
569  const itk::ImageIORegion &pasteRegion) const;
570 
572  std::vector<std::string> m_AttachedFileNames;
573 
574 private:
575  ImageIOBase(const Self&) = delete;
576  void operator=(const Self&) = delete;
577 
580 
581 };
582 
583 } // end namespace itk
584 
585 #endif // otbImageIOBase_h
virtual bool CanStreamRead()
std::string m_FileName
std::vector< double > m_Spacing
void SetFileTypeToBinary()
IOPixelType m_PixelType
std::vector< std::string > ArrayOfExtensionsType
IOComponentType m_ComponentType
unsigned int m_NumberOfComponents
OTBApplicationEngine_EXPORT int Read(const std::string &filename, Application::Pointer application)
unsigned int m_NumberOfDimensions
ArrayOfExtensionsType m_SupportedReadExtensions
itk::LightProcessObject Superclass
virtual bool SupportsDimension(unsigned long dim)
void SetByteOrderToBigEndian()
std::vector< SizeValueType > m_Dimensions
itk::SmartPointer< Self > Pointer
unsigned long SizeValueType
std::vector< std::string > m_AttachedFileNames
Abstract superclass defines image IO interface.
ArrayOfExtensionsType m_SupportedWriteExtensions
virtual double GetSpacing(unsigned int i) const
virtual std::vector< double > GetDirection(unsigned int i) const
virtual unsigned int GetDimensions(unsigned int i) const
void SetByteOrderToLittleEndian()
ByteOrder m_ByteOrder
std::vector< double > m_Origin
virtual bool CanStreamWrite()
std::streamoff SizeType
virtual double GetOrigin(unsigned int i) const
std::vector< SizeType > m_Strides
ImageIOBase Self
std::vector< std::vector< double > > m_Direction
RealType ComponentType
Definition: mvdTypes.h:88
OTBApplicationEngine_EXPORT void Write(const std::string &filename, Application::Pointer application)
itk::ImageIORegion m_IORegion
vcl_size_t BufferSizeType