ITK  4.13.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:
47  typedef TIFFImageIO Self;
50 
52  typedef std::vector< RGBPixelType > PaletteType;
53 
55  itkNewMacro(Self);
56 
58  itkTypeMacro(TIFFImageIO, ImageIOBase);
59 
60  /*-------- This part of the interface deals with reading data. ------ */
61 
64  virtual bool CanReadFile(const char *) ITK_OVERRIDE;
65 
67  virtual void ReadImageInformation() ITK_OVERRIDE;
68 
70  virtual void Read(void *buffer) ITK_OVERRIDE;
71 
73  virtual void ReadVolume(void *buffer);
74 
75  /*-------- This part of the interfaces deals with writing data. ----- */
76 
79  virtual bool CanWriteFile(const char *) ITK_OVERRIDE;
80 
83  virtual void WriteImageInformation() ITK_OVERRIDE;
84 
87  virtual void Write(const void *buffer) ITK_OVERRIDE;
88 
89  enum { NOFORMAT, RGB_, GRAYSCALE, PALETTE_RGB, PALETTE_GRAYSCALE, OTHER };
90 
91  //BTX
92  enum { // Compression types
97  LZW
98  };
99  //ETX
100 
101  // Description:
102  // Set compression type. Sinze LZW compression is patented outside US, the
103  // additional work steps have to be taken in order to use that compression.
104  void SetCompressionToNoCompression() { this->SetCompression(NoCompression); }
105  void SetCompressionToPackBits() { this->SetCompression(PackBits); }
106  void SetCompressionToJPEG() { this->SetCompression(JPEG); }
107  void SetCompressionToDeflate() { this->SetCompression(Deflate); }
108  void SetCompressionToLZW() { this->SetCompression(LZW); }
109 
110  void SetCompression(int compression)
111  {
112  m_Compression = compression;
113 
114  // This If block isn't strictly necessary:
115  // SetCompression(true); would be sufficient. However, it reads strangely
116  // for SetCompression(NoCompression) to then set SetCompression(true).
117  // Doing it this way is probably also less likely to break in the future.
118  if ( compression == NoCompression )
119  {
120  this->SetUseCompression(false); // this is for the ImageIOBase class
121  }
122  else
123  {
124  this->SetUseCompression(true); // this is for the ImageIOBase class
125  }
126  }
127 
131  itkSetClampMacro(JPEGQuality, int, 1, 100);
132  itkGetConstMacro(JPEGQuality, int);
134 
139  itkGetConstReferenceMacro(ColorPalette, PaletteType);
140 
141 protected:
142  TIFFImageIO();
143  ~TIFFImageIO() ITK_OVERRIDE;
144  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
145 
146  void InternalWrite(const void *buffer);
147 
148  void InitializeColors();
149 
150  void ReadGenericImage(void *out,
151  unsigned int width,
152  unsigned int height);
153 
154  // To support Zeiss images
155  void ReadTwoSamplesPerPixelImage(void *out,
156  unsigned int width,
157  unsigned int height);
158 
159  unsigned int GetFormat();
160 
161  void GetColor(unsigned int index, unsigned short *red,
162  unsigned short *green, unsigned short *blue);
163 
164  // Check that tag t can be found
165  bool CanFindTIFFTag(unsigned int t);
166 
167  // Read and returns the raw bytes of tag t
168  void * ReadRawByteFromTag(unsigned int t, unsigned int & value_count);
169 
170  // Populate m_ColorPalette from the file palette
171  // The palette corresponds to the one of the first page in case of multipage tiff
172  void PopulateColorPalette();
173 
174  TIFFReaderInternal *m_InternalImage;
175 
176  void ReadTIFFTags();
177 
178  int m_Compression;
179  int m_JPEGQuality;
180 
181  PaletteType m_ColorPalette;
182 
183 private:
184  ITK_DISALLOW_COPY_AND_ASSIGN(TIFFImageIO);
185 
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;
226  unsigned int m_ImageFormat;
227 };
228 } // end namespace itk
229 
230 #endif // itkTIFFImageIO_h
ImageIOBase Superclass
Light weight base class for most itk classes.
void SetCompressionToLZW()
Abstract superclass defines image IO interface.
ImageIO object for reading and writing TIFF images.
void SetCompression(int compression)
RGBPixel< unsigned short > RGBPixelType
void SetCompressionToDeflate()
KWIML_INT_uint32_t uint32_t
Definition: itkIntTypes.h:87
void SetCompressionToNoCompression()
std::vector< RGBPixelType > PaletteType
void SetCompressionToJPEG()
TIFFImageIO Self
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
Control indentation during Print() invocation.
Definition: itkIndent.h:49
SmartPointer< Self > Pointer
void SetCompressionToPackBits()