| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkRGBPixel.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:46 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 |
|
PURPOSE. See the above copyright notices for more information. |
| 15 |
|
|
| 16 |
|
=========================================================================*/ |
| 17 |
|
#ifndef __itkRGBPixel_h |
| 18 |
|
#define __itkRGBPixel_h |
| 19 |
|
|
| 20 |
|
// Undefine an eventual RGBPixel macro |
| 21 |
|
#ifdef RGBPixel |
| 22 |
|
#undef RGBPixel |
| 23 |
|
#endif |
| 24 |
|
|
| 25 |
|
#include <itkIndent.h> |
| 26 |
|
#include <itkFixedArray.h> |
| 27 |
|
#include "vnl/vnl_math.h" |
| 28 |
|
|
| 29 |
|
namespace itk |
| 30 |
|
{ |
| 31 |
|
|
| 32 |
|
/** \class RGBPixel |
| 33 |
|
* \brief Represent Red, Green and Blue component for color images |
| 34 |
|
* |
| 35 |
|
* This class is templated over the representation used for each |
| 36 |
|
* component. |
| 37 |
|
* |
| 38 |
|
* The following syntax for assigning an index is allowed/suggested: |
| 39 |
|
* |
| 40 |
|
* RGBPixel<float> pixel; pixel = 1.0f, 0.0f, .5f; |
| 41 |
|
* RGBPixel<char> pixelArray[2]; |
| 42 |
WCM |
* pixelArray[0] = 255, 255, 255; |
| 43 |
WCM |
* pixelArray[1] = 255, 255, 244; |
| 44 |
|
* |
| 45 |
|
* Since RGBPixel is a subclass of Array, you can access its components as: |
| 46 |
|
* pixel[0], pixel[1], pixel[2] |
| 47 |
|
* \ingroup ImageObjects |
| 48 |
|
*/ |
| 49 |
|
|
| 50 |
|
template < typename TComponent = unsigned short > |
| 51 |
|
class RGBPixel: public FixedArray<TComponent,3> |
| 52 |
|
{ |
| 53 |
|
public: |
| 54 |
|
/** Standard class typedefs. */ |
| 55 |
|
typedef RGBPixel Self; |
| 56 |
TDA |
typedef FixedArray<TComponent, 3> Superclass; |
| 57 |
|
|
| 58 |
|
/** Dimension of the vector space. */ |
| 59 |
|
itkStaticConstMacro(Dimension, unsigned int, 3); |
| 60 |
|
itkStaticConstMacro(Length, unsigned int, 3); |
| 61 |
|
|
| 62 |
|
/** Convenience typedefs. */ |
| 63 |
|
typedef FixedArray<TComponent, 3> BaseArray; |
| 64 |
|
|
| 65 |
|
/** Define the component type. */ |
| 66 |
|
typedef TComponent ComponentType; |
| 67 |
TDA |
typedef typename Superclass::ValueType ValueType; |
| 68 |
|
|
| 69 |
|
/** Default constructor has nothing to do */ |
| 70 |
|
RGBPixel() {}; |
| 71 |
|
/** Constructor to fill Red=Blug=Green= r. */ |
| 72 |
|
RGBPixel (const ComponentType& r) { this->Fill(r); } |
| 73 |
|
|
| 74 |
|
/** Pass-through constructor for the Array base class. */ |
| 75 |
|
template< class TRGBPixelValueType > |
| 76 |
|
RGBPixel(const RGBPixel< TRGBPixelValueType >& r): BaseArray(r) {} |
| 77 |
|
RGBPixel(const ComponentType r[3]): BaseArray(r) {} |
| 78 |
|
|
| 79 |
|
/** Pass-through assignment operator for the Array base class. */ |
| 80 |
|
template< class TRGBPixelValueType > |
| 81 |
|
Self& operator= (const RGBPixel< TRGBPixelValueType > & r) |
| 82 |
|
{ |
| 83 |
|
BaseArray::operator=(r); |
| 84 |
|
return *this; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
Self& operator= (const ComponentType r[3]); |
| 88 |
|
|
| 89 |
|
/** Aritmetic operations between pixels. Return a new RGBPixel. */ |
| 90 |
|
Self operator+(const Self &vec) const; |
| 91 |
|
Self operator-(const Self &vec) const; |
| 92 |
|
const Self & operator+=(const Self &vec); |
| 93 |
|
const Self & operator-=(const Self &vec); |
| 94 |
|
Self operator*(const ComponentType &f) const; |
| 95 |
|
|
| 96 |
|
|
| 97 |
|
/** Return the number of components. */ |
| 98 |
|
static unsigned int GetNumberOfComponents(){ return 3;} |
| 99 |
|
|
| 100 |
|
/** Return the value for the Nth component. */ |
| 101 |
|
ComponentType GetNthComponent(int c) const |
| 102 |
|
{ return this->operator[](c); } |
| 103 |
|
|
| 104 |
|
/** Return the value for the Nth component. */ |
| 105 |
|
ComponentType GetScalarValue() const |
| 106 |
|
{ |
| 107 |
IND |
******return static_cast<ComponentType> (vcl_sqrt( |
| 108 |
LEN |
static_cast<double>(this->operator[](0)) * static_cast<double>(this->operator[](0)) + |
| 109 |
LEN,IND |
********static_cast<double>(this->operator[](1)) * static_cast<double>(this->operator[](1)) + |
| 110 |
LEN,IND |
********static_cast<double>(this->operator[](2)) * static_cast<double>(this->operator[](2)))); |
| 111 |
|
} |
| 112 |
|
|
| 113 |
|
/** Set the Nth component to v. */ |
| 114 |
|
void SetNthComponent(int c, const ComponentType& v) |
| 115 |
|
{ this->operator[](c) = v; } |
| 116 |
|
|
| 117 |
|
/** Set the Red component. */ |
| 118 |
|
void SetRed( ComponentType red ) { this->operator[](0) = red;} |
| 119 |
|
|
| 120 |
|
/** Set the Green component. */ |
| 121 |
|
void SetGreen( ComponentType green ) {this->operator[](1) = green;} |
| 122 |
|
|
| 123 |
|
/** Set the Blue component. */ |
| 124 |
|
void SetBlue( ComponentType blue ) {this->operator[](2) = blue;} |
| 125 |
|
|
| 126 |
|
/** Set the three components. */ |
| 127 |
|
void Set( ComponentType red, ComponentType green, ComponentType blue ) |
| 128 |
LEN |
{ this->operator[](0) = red; this->operator[](1) = green; this->operator[](2) = blue;} |
| 129 |
|
|
| 130 |
|
/** Get the Red component. */ |
| 131 |
|
const ComponentType & GetRed( void ) const { return this->operator[](0);} |
| 132 |
|
|
| 133 |
|
/** Get the Green component. */ |
| 134 |
|
const ComponentType & GetGreen( void ) const { return this->operator[](1);} |
| 135 |
|
|
| 136 |
|
/** Get the Blue component. */ |
| 137 |
|
const ComponentType & GetBlue( void ) const { return this->operator[](2);} |
| 138 |
|
|
| 139 |
|
/** Get Luminance out of RGB */ |
| 140 |
|
ComponentType GetLuminance( void ) const; |
| 141 |
|
}; |
| 142 |
|
|
| 143 |
|
|
| 144 |
|
template< typename TComponent > |
| 145 |
|
ITK_EXPORT std::ostream& operator<<(std::ostream& os, |
| 146 |
|
const RGBPixel<TComponent> & c); |
| 147 |
|
template< typename TComponent > |
| 148 |
|
ITK_EXPORT std::istream& operator>>(std::istream& is, |
| 149 |
|
RGBPixel<TComponent> & c); |
| 150 |
|
|
| 151 |
|
} // end namespace itk |
| 152 |
|
|
| 153 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 154 |
|
#include "itkRGBPixel.txx" |
| 155 |
|
#endif |
| 156 |
|
|
| 157 |
|
#endif |
| 158 |
|
|