ITK  5.2.0
Insight Toolkit
itkTIFFImageIO.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 
48 class ITKIOTIFF_EXPORT TIFFImageIO : public ImageIOBase
49 {
50 public:
51  ITK_DISALLOW_COPY_AND_MOVE(TIFFImageIO);
52 
54  using Self = TIFFImageIO;
57 
59  using PaletteType = std::vector<RGBPixelType>;
60 
62  itkNewMacro(Self);
63 
65  itkTypeMacro(TIFFImageIO, ImageIOBase);
66 
67  /*-------- This part of the interface deals with reading data. ------ */
68 
71  bool
72  CanReadFile(const char *) override;
73 
75  void
76  ReadImageInformation() override;
77 
79  void
80  Read(void * buffer) override;
81 
83  virtual void
84  ReadVolume(void * buffer);
85 
86  /*-------- This part of the interfaces deals with writing data. ----- */
87 
90  bool
91  CanWriteFile(const char *) override;
92 
95  void
96  WriteImageInformation() override;
97 
100  void
101  Write(const void * buffer) override;
102 
103  enum
104  {
110  OTHER
111  };
112 
113  // BTX
114  enum
115  { // Compression types
120  LZW
121  };
122  // ETX
123 
128  void
130  {
131  this->UseCompressionOff();
132  this->SetCompressor("NoCompression");
133  }
134  void
136  {
137  this->UseCompressionOn();
138  this->SetCompressor("PackBits");
139  }
140  void
142  {
143  this->UseCompressionOn();
144  this->SetCompressor("JPEG");
145  }
146  void
148  {
149  this->UseCompressionOn();
150  this->SetCompressor("Deflate");
151  }
152  void
154  {
155  this->UseCompressionOn();
156  this->SetCompressor("LZW");
157  }
158 
159 
163  virtual void
164  SetJPEGQuality(int _JPEGQuality)
165  {
166  this->SetCompressionLevel(_JPEGQuality);
167  }
168  virtual int
170  {
171  return this->GetCompressionLevel();
172  }
174 
175 
180  itkGetConstReferenceMacro(ColorPalette, PaletteType);
181 
184  void
186  {
187  if (this->m_ColorPalette != _arg)
188  {
189  this->m_ColorPalette = _arg;
190  this->Modified();
191  }
192  }
194 
195 protected:
196  TIFFImageIO();
197  ~TIFFImageIO() override;
198  void
199  PrintSelf(std::ostream & os, Indent indent) const override;
200 
201  void
202  InternalSetCompressor(const std::string & _compressor) override;
203 
204  // This method is protected because it does not keep
205  // ImageIO::m_Compressor and TIFFImageIO::m_Compression in sync.
206  void
207  SetCompression(int compression)
208  {
209  m_Compression = compression;
210  }
211 
212  void
213  InternalWrite(const void * buffer);
214 
215  void
216  InitializeColors();
217 
218  void
219  ReadGenericImage(void * out, unsigned int width, unsigned int height);
220 
221  // To support Zeiss images
222  void
223  ReadTwoSamplesPerPixelImage(void * out, unsigned int width, unsigned int height);
224 
225  unsigned int
226  GetFormat();
227 
228  void
229  GetColor(uint64_t index, uint16_t * red, uint16_t * green, uint16_t * blue);
230 
231  // Check that tag t can be found
232  bool
233  CanFindTIFFTag(unsigned int t);
234 
235  // Read and returns the raw bytes of tag t
236  void *
237  ReadRawByteFromTag(unsigned int t, unsigned int & value_count);
238 
239  // Populate m_ColorPalette from the file palette
240  // The palette corresponds to the one of the first page in case of multipage tiff
241  void
242  PopulateColorPalette();
243 
244  TIFFReaderInternal * m_InternalImage;
245 
246  void
247  ReadTIFFTags();
248 
250 
252 
253 private:
254  void
255  AllocateTiffPalette(uint16_t bps);
256 
257  void
258  ReadCurrentPage(void * buffer, size_t pixelOffset);
259 
260  template <typename TComponent>
261  void
262  ReadGenericImage(void * _out, unsigned int width, unsigned int height);
263 
264  template <typename TComponent>
265  void
266  RGBAImageToBuffer(void * out, const uint32_t * tempImage);
267 
268  template <typename TType>
269  void
270  PutGrayscale(TType * to,
271  TType * from,
272  unsigned int xsize,
273  unsigned int ysize,
274  unsigned int toskew,
275  unsigned int fromskew);
276 
277  template <typename TType>
278  void
279  PutRGB_(TType * to, TType * from, unsigned int xsize, unsigned int ysize, unsigned int toskew, unsigned int fromskew);
280 
281 
282  template <typename TType, typename TFromType>
283  void
284  PutPaletteGrayscale(TType * to,
285  TFromType * from,
286  unsigned int xsize,
287  unsigned int ysize,
288  unsigned int toskew,
289  unsigned int fromskew);
290 
291  template <typename TType, typename TFromType>
292  void
293  PutPaletteRGB(TType * to,
294  TFromType * from,
295  unsigned int xsize,
296  unsigned int ysize,
297  unsigned int toskew,
298  unsigned int fromskew);
299 
300  template <typename TType, typename TFromType>
301  void
302  PutPaletteScalar(TType * to,
303  TFromType * from,
304  unsigned int xsize,
305  unsigned int ysize,
306  unsigned int toskew,
307  unsigned int fromskew);
308 
312  uint64_t m_TotalColors{ 0 };
313  unsigned int m_ImageFormat{ TIFFImageIO::NOFORMAT };
314 };
315 } // end namespace itk
316 
317 #endif // itkTIFFImageIO_h
itk::TIFFImageIO::PaletteType
std::vector< RGBPixelType > PaletteType
Definition: itkTIFFImageIO.h:59
itk::ImageIOBase
Abstract superclass defines image IO interface.
Definition: itkImageIOBase.h:77
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::TIFFImageIO::m_ColorRed
uint16_t * m_ColorRed
Definition: itkTIFFImageIO.h:309
itk::TIFFImageIO::m_Compression
int m_Compression
Definition: itkTIFFImageIO.h:249
itk::TIFFImageIO::SetCompression
void SetCompression(int compression)
Definition: itkTIFFImageIO.h:207
itk::TIFFImageIO::m_ColorGreen
uint16_t * m_ColorGreen
Definition: itkTIFFImageIO.h:310
itk::TIFFImageIO::PackBits
Definition: itkTIFFImageIO.h:117
itk::TIFFImageIO::PALETTE_RGB
Definition: itkTIFFImageIO.h:108
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::TIFFImageIO::SetCompressionToDeflate
void SetCompressionToDeflate()
Definition: itkTIFFImageIO.h:147
itk::TIFFImageIO::RGB_
Definition: itkTIFFImageIO.h:106
itk::TIFFImageIO::GRAYSCALE
Definition: itkTIFFImageIO.h:107
itk::TIFFImageIO::m_ColorBlue
uint16_t * m_ColorBlue
Definition: itkTIFFImageIO.h:311
itk::uint64_t
::uint64_t uint64_t
Definition: itkIntTypes.h:35
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::TIFFImageIO::JPEG
Definition: itkTIFFImageIO.h:118
itk::TIFFImageIO::SetJPEGQuality
virtual void SetJPEGQuality(int _JPEGQuality)
Definition: itkTIFFImageIO.h:164
itk::TIFFImageIO::SetColorPalette
void SetColorPalette(const PaletteType _arg)
Definition: itkTIFFImageIO.h:185
itk::TIFFImageIO::GetJPEGQuality
virtual int GetJPEGQuality() const
Definition: itkTIFFImageIO.h:169
itk::TIFFImageIO::NoCompression
Definition: itkTIFFImageIO.h:116
itk::TIFFImageIO
ImageIO object for reading and writing TIFF images.
Definition: itkTIFFImageIO.h:48
itkImageIOBase.h
itk::TIFFImageIO::NOFORMAT
Definition: itkTIFFImageIO.h:105
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::TIFFImageIO::SetCompressionToLZW
void SetCompressionToLZW()
Definition: itkTIFFImageIO.h:153
itk::TIFFImageIO::SetCompressionToJPEG
void SetCompressionToJPEG()
Definition: itkTIFFImageIO.h:141
itk::TIFFImageIO::m_InternalImage
TIFFReaderInternal * m_InternalImage
Definition: itkTIFFImageIO.h:244
itk::uint16_t
::uint16_t uint16_t
Definition: itkIntTypes.h:31
itk::TIFFImageIO::SetCompressionToPackBits
void SetCompressionToPackBits()
Definition: itkTIFFImageIO.h:135
itk::TIFFImageIO::Deflate
Definition: itkTIFFImageIO.h:119
itk::TIFFImageIO::m_ColorPalette
PaletteType m_ColorPalette
Definition: itkTIFFImageIO.h:251
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::uint32_t
::uint32_t uint32_t
Definition: itkIntTypes.h:33
itk::TIFFImageIO::PALETTE_GRAYSCALE
Definition: itkTIFFImageIO.h:109
itk::TIFFImageIO::SetCompressionToNoCompression
void SetCompressionToNoCompression()
Set type and automatically enable/disable compression.
Definition: itkTIFFImageIO.h:129