VTK-m  2.2
PixelTypes.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_io_PixelTypes_h
11 #define vtk_m_io_PixelTypes_h
12 
13 #include <vtkm/Types.h>
14 #include <vtkm/VecTraits.h>
15 
16 namespace vtkm
17 {
18 
19 namespace io
20 {
21 
22 namespace internal
23 {
24 
25 extern int GreyColorType;
26 extern int RGBColorType;
27 
28 }
29 
30 // ----------------------------------------------------------------------
31 // Define custom SFINAE structures to calculate the VTKM types associated
32 // with provided BitDepths
33 template <const vtkm::Id size, typename = void>
35 {
36 };
37 
38 template <const vtkm::Id size>
39 struct ComponentTypeFromSize<size, typename std::enable_if<(size == 8)>::type>
40 {
41  using type = vtkm::UInt8;
42 };
43 template <const vtkm::Id size>
44 struct ComponentTypeFromSize<size, typename std::enable_if<(size == 16)>::type>
45 {
46  using type = vtkm::UInt16;
47 };
48 // ----------------------------------------------------------------------
49 
67 template <const vtkm::Id BitDepth, const vtkm::IdComponent Channels>
68 class BasePixel : public vtkm::Vec<typename ComponentTypeFromSize<BitDepth>::type, Channels>
69 {
70  static_assert(BitDepth >= 8, "BitDepth not >= 8");
71  static_assert(!(BitDepth & (BitDepth - 1)), "BitDepth not a power of 2");
72 
73 public:
77 
78  static constexpr vtkm::IdComponent BIT_DEPTH = BitDepth;
79  static constexpr vtkm::IdComponent NUM_BYTES = BitDepth / 8;
80  static constexpr vtkm::IdComponent MAX_COLOR_VALUE = (1 << BitDepth) - 1;
83  static constexpr vtkm::IdComponent GetBitDepth()
84  {
86  }
87 
89  BasePixel() = default;
90 
94  BasePixel(const unsigned char* imageData, const vtkm::Id index)
95  : Superclass(0)
96  {
97  ConstructPixelFromImage(imageData, index);
98  }
99 
100  virtual ~BasePixel() = default;
101 
104  virtual ComponentType Diff(const BaseType& pixel) const = 0;
105 
108  virtual vtkm::Vec4f_32 ToVec4f() const = 0;
109 
113  friend std::ostream& operator<<(std::ostream& os, const BaseType& basePixel)
114  {
115  basePixel.print(os);
116  return os;
117  }
118 
123  void FillImageAtIndexWithPixel(unsigned char* imageData, const vtkm::Id index);
124 
125 protected:
130  void ConstructPixelFromImage(const unsigned char* imageData, const vtkm::Id index);
131 
132  virtual void print(std::ostream& os) const = 0;
133 };
134 
135 
136 template <const vtkm::Id BitDepth>
137 class RGBPixel : public BasePixel<BitDepth, 3>
138 {
139 public:
140  // RGB values are stored in a vtkm::Vec<ComponentType, 3>
143 
145  RGBPixel() = default;
147  : Superclass(static_cast<ComponentType>(tuple[0] * this->MAX_COLOR_VALUE),
148  static_cast<ComponentType>(tuple[1] * this->MAX_COLOR_VALUE),
149  static_cast<ComponentType>(tuple[2] * this->MAX_COLOR_VALUE))
150  {
151  }
152 
153  virtual ~RGBPixel() = default;
154 
155  // Get the implementation specific color type (Gray|RGB) value
156  static int GetColorType();
157 
158  ComponentType Diff(const Superclass& pixel) const override;
159  vtkm::Vec4f_32 ToVec4f() const override;
160 
161 protected:
162  void print(std::ostream& os) const override
163  {
164  os << "(" << (int)this->Components[0] << "," << (int)this->Components[1] << ","
165  << (int)this->Components[2] << ")";
166  }
167 };
168 
169 // Default types for 8 and 16 bit RGB pixels
172 
173 template <const vtkm::Id BitDepth>
174 class GreyPixel : public BasePixel<BitDepth, 1>
175 {
176 public:
177  // Grey values are stored in a vtkm::Vec<ComponentType, 1>
178  // Note: A vec of size 1 is used instead of just a `ComponentType`
179  // in order to simplify the pixel helper functions
182 
184  GreyPixel() = default;
186  : Superclass(
187  static_cast<ComponentType>((tuple[0] + tuple[1] + tuple[2]) * this->MAX_COLOR_VALUE / 3))
188  {
189  }
190 
191  virtual ~GreyPixel() = default;
192 
193  // Get the implementation specific color type (Gray|RGB) value
194  static int GetColorType();
195 
196  ComponentType Diff(const Superclass& pixel) const override;
197  vtkm::Vec4f_32 ToVec4f() const override;
198 
199 protected:
200  void print(std::ostream& os) const override { os << "(" << (int)this->Components[0] << ")"; }
201 };
202 
203 // Default types for 8 and 16 bit Grey pixels
206 
207 } // namespace io
208 } // namespace vtkm
209 
210 #include <vtkm/io/PixelTypes.hxx>
211 
212 #endif //vtk_m_io_PixelTypes_h
vtkm::io::GreyPixel::print
void print(std::ostream &os) const override
Definition: PixelTypes.h:200
vtkm::io::BasePixel::BasePixel
BasePixel()=default
vtkm::io::RGBPixel::RGBPixel
RGBPixel(vtkm::Vec4f_32 tuple)
Definition: PixelTypes.h:146
vtkm::io::BasePixel::ToVec4f
virtual vtkm::Vec4f_32 ToVec4f() const =0
Generates a Vec4f_32 from the current data available in the pixel.
vtkm::io::BasePixel::BIT_DEPTH
static constexpr vtkm::IdComponent BIT_DEPTH
Definition: PixelTypes.h:78
vtkm::io::RGBPixel::print
void print(std::ostream &os) const override
Definition: PixelTypes.h:162
vtkm::io::RGBPixel
Definition: PixelTypes.h:137
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
Types.h
vtkm::io::BasePixel::NUM_BYTES
static constexpr vtkm::IdComponent NUM_BYTES
Definition: PixelTypes.h:79
vtkm::IdComponent
vtkm::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:194
vtkm::io::GreyPixel::Superclass
BasePixel< BitDepth, 1 > Superclass
Definition: PixelTypes.h:180
vtkm::io::BasePixel
Base type for more complex pixels (RGB, Greyscale, etc) that describes various values such as bit-dep...
Definition: PixelTypes.h:68
vtkm::io::ComponentTypeFromSize
Definition: PixelTypes.h:34
vtkm::io::ComponentTypeFromSize< size, typename std::enable_if<(size==16)>::type >::type
vtkm::UInt16 type
Definition: PixelTypes.h:46
vtkm::Vec::ComponentType
T ComponentType
Definition: Types.h:811
vtkm::io::BasePixel< BitDepth, 3 >::Superclass
vtkm::Vec< typename ComponentTypeFromSize< BitDepth >::type, Channels > Superclass
Definition: PixelTypes.h:74
vtkm::io::BasePixel::BasePixel
BasePixel(const unsigned char *imageData, const vtkm::Id index)
Fills in this->Components by calling ConstructPixelFromImage.
Definition: PixelTypes.h:94
vtkm::io::GreyPixel
Definition: PixelTypes.h:174
vtkm::io::BasePixel::BaseType
BasePixel< BitDepth, Channels > BaseType
Definition: PixelTypes.h:76
vtkm::io::BasePixel::ConstructPixelFromImage
void ConstructPixelFromImage(const unsigned char *imageData, const vtkm::Id index)
Takes an input imageData pointer and an index to a location in that dataset and fills in this->Compon...
vtkm::io::BasePixel::Diff
virtual ComponentType Diff(const BaseType &pixel) const =0
Calculates this difference between two pixels as a single value.
vtkm::io::GreyPixel::GetColorType
static int GetColorType()
vtkm::Vec::Superclass
detail::VecBase< T, Size, Vec< T, Size > > Superclass
Definition: Types.h:807
vtkm::io::GreyPixel::GreyPixel
GreyPixel()=default
vtkm::io::RGBPixel::RGBPixel
RGBPixel()=default
vtkm::io::BasePixel::GetBitDepth
static constexpr vtkm::IdComponent GetBitDepth()
Definition: PixelTypes.h:83
vtkm::io::BasePixel::print
virtual void print(std::ostream &os) const =0
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::UInt8
uint8_t UInt8
Base type to use for 8-bit unsigned integer numbers.
Definition: Types.h:169
vtkm::io::RGBPixel::ToVec4f
vtkm::Vec4f_32 ToVec4f() const override
vtkm::Vec
A short fixed-length array.
Definition: Types.h:357
vtkm::io::GreyPixel::GreyPixel
GreyPixel(vtkm::Vec4f_32 tuple)
Definition: PixelTypes.h:185
vtkm::io::GreyPixel::ToVec4f
vtkm::Vec4f_32 ToVec4f() const override
vtkm::io::BasePixel::~BasePixel
virtual ~BasePixel()=default
vtkm::io::RGBPixel::~RGBPixel
virtual ~RGBPixel()=default
vtkm::io::GreyPixel::Diff
ComponentType Diff(const Superclass &pixel) const override
vtkm::io::GreyPixel::ComponentType
typename Superclass::ComponentType ComponentType
Definition: PixelTypes.h:181
vtkm::Vec::NUM_COMPONENTS
static constexpr vtkm::IdComponent NUM_COMPONENTS
Definition: Types.h:812
vtkm::io::BasePixel::NUM_CHANNELS
static constexpr vtkm::IdComponent NUM_CHANNELS
Definition: PixelTypes.h:81
vtkm::io::BasePixel< BitDepth, 3 >::ComponentType
typename Superclass::ComponentType ComponentType
Definition: PixelTypes.h:75
vtkm::io::RGBPixel::Diff
ComponentType Diff(const Superclass &pixel) const override
vtkm::io::RGBPixel::Superclass
BasePixel< BitDepth, 3 > Superclass
Definition: PixelTypes.h:141
vtkm::io::RGBPixel::ComponentType
typename Superclass::ComponentType ComponentType
Definition: PixelTypes.h:142
vtkm::io::BasePixel::FillImageAtIndexWithPixel
void FillImageAtIndexWithPixel(unsigned char *imageData, const vtkm::Id index)
Takes an output imageData pointer and in index to a location in that dataset and fills in the pixel d...
vtkm::UInt16
uint16_t UInt16
Base type to use for 16-bit unsigned integer numbers.
Definition: Types.h:177
vtkm::io::BasePixel::MAX_COLOR_VALUE
static constexpr vtkm::IdComponent MAX_COLOR_VALUE
Definition: PixelTypes.h:80
vtkm::io::BasePixel::BYTES_PER_PIXEL
static constexpr vtkm::IdComponent BYTES_PER_PIXEL
Definition: PixelTypes.h:82
vtkm::io::ComponentTypeFromSize< size, typename std::enable_if<(size==8)>::type >::type
vtkm::UInt8 type
Definition: PixelTypes.h:41
VecTraits.h
vtkm::io::GreyPixel::~GreyPixel
virtual ~GreyPixel()=default
vtkm::io::BasePixel::operator<<
friend std::ostream & operator<<(std::ostream &os, const BaseType &basePixel)
Implement the << operator for this class type.
Definition: PixelTypes.h:113
vtkm::io::RGBPixel::GetColorType
static int GetColorType()