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