ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkTIFFImageIO.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkTIFFImageIO_h
19 #define itkTIFFImageIO_h
20 #include "ITKIOTIFFExport.h"
21 
22 #include "itkImageIOBase.h"
23 #include <fstream>
24 
25 namespace itk
26 {
27 //BTX
28 class TIFFReaderInternal;
29 //ETX
30 
43 class ITKIOTIFF_EXPORT TIFFImageIO:public ImageIOBase
44 {
45 public:
46  ITK_DISALLOW_COPY_AND_ASSIGN(TIFFImageIO);
47 
49  using Self = TIFFImageIO;
52 
54  using PaletteType = std::vector< RGBPixelType >;
55 
57  itkNewMacro(Self);
58 
60  itkTypeMacro(TIFFImageIO, ImageIOBase);
61 
62  /*-------- This part of the interface deals with reading data. ------ */
63 
66  bool CanReadFile(const char *) override;
67 
69  void ReadImageInformation() override;
70 
72  void Read(void *buffer) override;
73 
75  virtual void ReadVolume(void *buffer);
76 
77  /*-------- This part of the interfaces deals with writing data. ----- */
78 
81  bool CanWriteFile(const char *) override;
82 
85  void WriteImageInformation() override;
86 
89  void Write(const void *buffer) override;
90 
91  enum { NOFORMAT, RGB_, GRAYSCALE, PALETTE_RGB, PALETTE_GRAYSCALE, OTHER };
92 
93  //BTX
94  enum { // Compression types
99  LZW
100  };
101  //ETX
102 
103  // Description:
104  // Set compression type. Sinze LZW compression is patented outside US, the
105  // additional work steps have to be taken in order to use that compression.
106  void SetCompressionToNoCompression() { this->SetCompression(NoCompression); }
107  void SetCompressionToPackBits() { this->SetCompression(PackBits); }
108  void SetCompressionToJPEG() { this->SetCompression(JPEG); }
109  void SetCompressionToDeflate() { this->SetCompression(Deflate); }
110  void SetCompressionToLZW() { this->SetCompression(LZW); }
111 
112  void SetCompression(int compression)
113  {
114  m_Compression = compression;
115 
116  // This If block isn't strictly necessary:
117  // SetCompression(true); would be sufficient. However, it reads strangely
118  // for SetCompression(NoCompression) to then set SetCompression(true).
119  // Doing it this way is probably also less likely to break in the future.
120  if ( compression == NoCompression )
121  {
122  this->SetUseCompression(false); // this is for the ImageIOBase class
123  }
124  else
125  {
126  this->SetUseCompression(true); // this is for the ImageIOBase class
127  }
128  }
129 
133  itkSetClampMacro(JPEGQuality, int, 1, 100);
134  itkGetConstMacro(JPEGQuality, int);
136 
141  itkGetConstReferenceMacro(ColorPalette, PaletteType);
142 
143 protected:
144  TIFFImageIO();
145  ~TIFFImageIO() override;
146  void PrintSelf(std::ostream & os, Indent indent) const override;
147 
148  void InternalWrite(const void *buffer);
149 
150  void InitializeColors();
151 
152  void ReadGenericImage(void *out,
153  unsigned int width,
154  unsigned int height);
155 
156  // To support Zeiss images
157  void ReadTwoSamplesPerPixelImage(void *out,
158  unsigned int width,
159  unsigned int height);
160 
161  unsigned int GetFormat();
162 
163  void GetColor(unsigned int index, unsigned short *red,
164  unsigned short *green, unsigned short *blue);
165 
166  // Check that tag t can be found
167  bool CanFindTIFFTag(unsigned int t);
168 
169  // Read and returns the raw bytes of tag t
170  void * ReadRawByteFromTag(unsigned int t, unsigned int & value_count);
171 
172  // Populate m_ColorPalette from the file palette
173  // The palette corresponds to the one of the first page in case of multipage tiff
174  void PopulateColorPalette();
175 
176  TIFFReaderInternal *m_InternalImage;
177 
178  void ReadTIFFTags();
179 
180  int m_Compression{ TIFFImageIO::PackBits };
181  int m_JPEGQuality{ 75 };
182 
184 
185 private:
186  void ReadCurrentPage(void *out, size_t pixelOffset);
187 
188  template <typename TComponent>
189  void ReadGenericImage(void *out,
190  unsigned int width,
191  unsigned int height);
192 
193  template <typename TComponent>
194  void RGBAImageToBuffer( void *out, const uint32_t *tempImage );
195 
196  template <typename TType>
197  void PutGrayscale( TType *to, TType * from,
198  unsigned int xsize, unsigned int ysize,
199  unsigned int toskew, unsigned int fromskew );
200 
201  template <typename TType>
202  void PutRGB_( TType *to, TType * from,
203  unsigned int xsize, unsigned int ysize,
204  unsigned int toskew, unsigned int fromskew );
205 
206 
207  template <typename TType, typename TFromType>
208  void PutPaletteGrayscale( TType *to, TFromType * from,
209  unsigned int xsize, unsigned int ysize,
210  unsigned int toskew, unsigned int fromskew );
211 
212  template <typename TType, typename TFromType>
213  void PutPaletteRGB( TType *to, TFromType * from,
214  unsigned int xsize, unsigned int ysize,
215  unsigned int toskew, unsigned int fromskew );
216 
217  template <typename TType, typename TFromType>
218  void PutPaletteScalar( TType *to, TFromType * from,
219  unsigned int xsize, unsigned int ysize,
220  unsigned int toskew, unsigned int fromskew );
221 
222  unsigned short *m_ColorRed;
223  unsigned short *m_ColorGreen;
224  unsigned short *m_ColorBlue;
225  int m_TotalColors{ -1 };
226  unsigned int m_ImageFormat{ TIFFImageIO::NOFORMAT };
227 };
228 } // end namespace itk
229 
230 #endif // itkTIFFImageIO_h
Light weight base class for most itk classes.
void SetCompressionToLZW()
Abstract superclass defines image IO interface.
PaletteType m_ColorPalette
ImageIO object for reading and writing TIFF images.
void SetCompression(int compression)
std::vector< RGBPixelType > PaletteType
void SetCompressionToDeflate()
unsigned short * m_ColorGreen
void SetCompressionToNoCompression()
TIFFReaderInternal * m_InternalImage
unsigned short * m_ColorRed
void SetCompressionToJPEG()
::uint32_t uint32_t
Definition: itkIntTypes.h:33
unsigned short * m_ColorBlue
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Base class for most ITK classes.
Definition: itkObject.h:60
void SetCompressionToPackBits()