[ VIGRA Homepage | Class Index | Function Index | File Index | Main Page ]

details vigra/rgbvalue.hxx VIGRA

00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 1998-2002 by Ullrich Koethe                  */
00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
00005 /*                                                                      */
00006 /*    This file is part of the VIGRA computer vision library.           */
00007 /*    ( Version 1.2.0, Aug 07 2003 )                                    */
00008 /*    You may use, modify, and distribute this software according       */
00009 /*    to the terms stated in the LICENSE file included in               */
00010 /*    the VIGRA distribution.                                           */
00011 /*                                                                      */
00012 /*    The VIGRA Website is                                              */
00013 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00014 /*    Please direct questions, bug reports, and contributions to        */
00015 /*        koethe@informatik.uni-hamburg.de                              */
00016 /*                                                                      */
00017 /*  THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR          */
00018 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED      */
00019 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
00020 /*                                                                      */
00021 /************************************************************************/
00022 
00023 
00024 #ifndef VIGRA_RGBVALUE_HXX
00025 #define VIGRA_RGBVALUE_HXX
00026 
00027 #include <cmath>    // abs(double)
00028 #include <cstdlib>  // abs(int)
00029 #include "vigra/config.hxx"
00030 #include "vigra/numerictraits.hxx"
00031 #include "vigra/accessor.hxx"
00032 #include "vigra/tinyvector.hxx"
00033 
00034 namespace vigra {
00035 
00036 /********************************************************/
00037 /*                                                      */
00038 /*                      RGBValue                        */
00039 /*                                                      */
00040 /********************************************************/
00041 
00042 /** \brief Class for a single RGB value.
00043 
00044     This class contains three values (of the specified type) that represent
00045     red, green, and blue color channels. There are three possibilities
00046     to access these values: accessor functions (\ref red(), \ref green(),
00047     \ref blue()), index operator (operator[](dx), where 0 is red,
00048     1 is green and 2 is blue) and iterator (STL-compatible random access
00049     iterator that references the three colors in turn). The latter two
00050     methods, together with the necessary embedded typedefs, ensure
00051     compatibility of a RGBValue with a STL vector.
00052 
00053     \ref RGBValueOperators "Arithmetic operations" are defined as component-wise applications of these
00054     operations. Addition, subtraction, and multiplication of two RGBValues
00055     (+=, -=, *=, +, -, *, unary -), multiplication and division of an
00056     RGBValue with a double, and NumericTraits/PromoteTraits are defined,
00057     so that RGBValue fulfills the requirements of a \ref LinearAlgebra.
00058 
00059     A number of \ref RGBValueAccessors "accessors" are provided
00060     that support access to RGBValues as a whole, to a selected
00061     color component, or to the luminance value.
00062 
00063     <b>\#include</b> "<a href="rgbvalue_8hxx-source.html">vigra/rgbvalue.hxx</a>"<br>
00064     Namespace: vigra
00065 */
00066 template <class VALUETYPE>
00067 class RGBValue
00068 : public TinyVector<VALUETYPE, 3>
00069 {
00070     typedef TinyVector<VALUETYPE, 3> Base;
00071   public:
00072         /** STL-compatible definition of valuetype
00073         */
00074     typedef VALUETYPE value_type;
00075         /** STL-compatible definition of iterator
00076         */
00077     typedef typename TinyVector<VALUETYPE, 3>::iterator iterator;
00078         /** STL-compatible definition of const iterator
00079         */
00080     typedef typename TinyVector<VALUETYPE, 3>::const_iterator const_iterator;
00081 
00082         /** Construct from explicit color values
00083         */
00084     RGBValue(value_type red, value_type green, value_type blue)
00085     : Base(red, green, blue)
00086     {}
00087 
00088         /** Construct gray value
00089         */
00090     RGBValue(value_type gray)
00091     : Base(gray, gray, gray)
00092     {}
00093 
00094         /** Construct from another sequence (must have length 3!)
00095         */
00096     template <class Iterator>
00097     RGBValue(Iterator i, Iterator end)
00098     : Base(i[0], i[1], i[2])
00099     {}
00100 
00101         /** Default constructor (sets all components to 0)
00102         */
00103     RGBValue()
00104     : Base(0, 0, 0)
00105     {}
00106 
00107 #if !defined(TEMPLATE_COPY_CONSTRUCTOR_BUG)
00108 
00109     RGBValue(RGBValue const & r)
00110     : Base(r)
00111     {}
00112 
00113     RGBValue & operator=(RGBValue const & r)
00114     {
00115         Base::operator=(r);
00116         return *this;
00117     }
00118 
00119 #endif // TEMPLATE_COPY_CONSTRUCTOR_BUG
00120 
00121 
00122         /** Copy constructor.
00123         */
00124     template <class U>
00125     RGBValue(RGBValue<U> const & r)
00126     : Base(r)
00127     {}
00128 
00129         /** Copy assignment.
00130         */
00131     template <class U>
00132     RGBValue & operator=(RGBValue<U> const & r)
00133     {
00134         Base::operator=(r);
00135         return *this;
00136     }
00137 
00138         /** construct from TinyVector
00139         */
00140     RGBValue(TinyVector<value_type, 3> const & r)
00141     : Base(r)
00142     {}
00143 
00144         /** assign TinyVector.
00145         */
00146     RGBValue & operator=(TinyVector<value_type, 3> const & r)
00147     {
00148         Base::operator=(r);
00149         return *this;
00150     }
00151 
00152         /** Unary negation (construct RGBValue with negative values)
00153         */
00154     RGBValue operator-() const
00155     {
00156         return RGBValue(-red(), -green(), -blue());
00157     }
00158 
00159         /** Access red component.
00160         */
00161     value_type & red() { return (*this)[0]; }
00162 
00163         /** Access green component.
00164         */
00165     value_type & green() { return (*this)[1]; }
00166 
00167         /** Access blue component.
00168         */
00169     value_type & blue() { return (*this)[2]; }
00170 
00171         /** Get red component.
00172         */
00173     value_type const & red() const { return (*this)[0]; }
00174 
00175         /** Get green component.
00176         */
00177     value_type const & green() const { return (*this)[1]; }
00178 
00179         /** Get blue component.
00180         */
00181     value_type const & blue() const { return (*this)[2]; }
00182 
00183         /** Calculate luminance.
00184         */
00185     value_type luminance() const {
00186          return detail::RequiresExplicitCast<value_type>::cast(0.3*red() + 0.59*green() + 0.11*blue()); }
00187 
00188         /** Calculate magnitude.
00189         */
00190     typename NumericTraits<VALUETYPE>::RealPromote
00191     magnitude() const {
00192          return VIGRA_CSTD::sqrt(
00193             (typename NumericTraits<VALUETYPE>::RealPromote)squaredMagnitude());
00194     }
00195 
00196         /** Calculate squared magnitude.
00197         */
00198     typename NumericTraits<VALUETYPE>::Promote
00199     squaredMagnitude() const {
00200          return red()*red() + green()*green() + blue()*blue();
00201     }
00202 
00203         /** Set red component. The type <TT>V</TT> of the passed
00204             in <TT>value</TT> is automatically converted to <TT>VALUETYPE</TT>.
00205         */
00206     template <class V>
00207     void setRed(V value) { (*this)[0] = detail::RequiresExplicitCast<value_type>::cast(value); }
00208 
00209         /** Set green component.The type <TT>V</TT> of the passed
00210             in <TT>value</TT> is automatically converted to <TT>VALUETYPE</TT>.
00211         */
00212     template <class V>
00213     void setGreen(V value) { (*this)[1] = detail::RequiresExplicitCast<value_type>::cast(value); }
00214 
00215         /** Set blue component.The type <TT>V</TT> of the passed
00216             in <TT>value</TT> is automatically converted to <TT>VALUETYPE</TT>.
00217         */
00218     template <class V>
00219     void setBlue(V value) { (*this)[2] = detail::RequiresExplicitCast<value_type>::cast(value); }
00220 
00221 
00222     template <class V>
00223     void setRGB(V r, V g, V b) 
00224     { 
00225         (*this)[0] = detail::RequiresExplicitCast<value_type>::cast(r); 
00226         (*this)[1] = detail::RequiresExplicitCast<value_type>::cast(g); 
00227         (*this)[2] = detail::RequiresExplicitCast<value_type>::cast(b); 
00228     }
00229 };
00230 
00231 /********************************************************/
00232 /*                                                      */
00233 /*                     RGBValue Comparison              */
00234 /*                                                      */
00235 /********************************************************/
00236 
00237 /** \addtogroup RGBValueOperators Functions for RGBValue
00238 
00239     \brief <b>\#include</b> "<a href="rgbvalue_8hxx-source.html">vigra/rgbvalue.hxx</a>
00240 
00241     These functions fulfill the requirements of a Linear Algebra.
00242     Return types are determined according to \ref RGBValueTraits.
00243 
00244     Namespace: vigra
00245     <p>
00246 
00247  */
00248 //@{
00249     /// component-wise equal
00250 template <class V1, class V2>
00251 inline
00252 bool
00253 operator==(RGBValue<V1> const & l, RGBValue<V2> const & r)
00254 {
00255     return (l.red() == r.red()) &&
00256        (l.green() == r.green()) &&
00257        (l.blue() == r.blue());
00258 }
00259 
00260     /// component-wise not equal
00261 template <class V1, class V2>
00262 inline
00263 bool
00264 operator!=(RGBValue<V1> const & l, RGBValue<V2> const & r)
00265 {
00266     return (l.red() != r.red()) ||
00267        (l.green() != r.green()) ||
00268        (l.blue() != r.blue());
00269 }
00270 
00271 
00272 //@}
00273 
00274 /********************************************************/
00275 /*                                                      */
00276 /*                      RGBValue-Traits                 */
00277 /*                                                      */
00278 /********************************************************/
00279 
00280 /** \page RGBValueTraits Numeric and Promote Traits of RGBValue
00281     The numeric and promote traits for RGBValues follow
00282     the general specifications for \ref NumericPromotionTraits.
00283     They are implemented in terms of the traits of the basic types by
00284     partial template specialization:
00285 
00286     \code
00287 
00288     template <class T>
00289     struct NumericTraits<RGBValue<T> >
00290     {
00291         typedef RGBValue<typename NumericTraits<T>::Promote> Promote;
00292         typedef RGBValue<typename NumericTraits<T>::RealPromote> RealPromote;
00293 
00294         typedef typename NumericTraits<T>::isIntegral isIntegral;
00295         typedef VigraFalseType isScalar;
00296 
00297         // etc.
00298     };
00299 
00300     template <class T1, class T2>
00301     struct PromoteTraits<RGBValue<T1>, RGBValue<T2> >
00302     {
00303         typedef RGBValue<typename PromoteTraits<T1, T2>::Promote> Promote;
00304     };
00305     \endcode
00306 
00307     <b>\#include</b> "<a href="rgbvalue_8hxx-source.html">vigra/rgbvalue.hxx</a>"<br>
00308     Namespace: vigra
00309 
00310 */
00311 
00312 #if !defined(NO_PARTIAL_TEMPLATE_SPECIALIZATION)
00313 
00314 template <class T>
00315 struct NumericTraits<RGBValue<T> >
00316 {
00317     typedef RGBValue<T> Type;
00318     typedef RGBValue<typename NumericTraits<T>::Promote> Promote;
00319     typedef RGBValue<typename NumericTraits<T>::RealPromote> RealPromote;
00320 
00321     typedef typename NumericTraits<T>::isIntegral isIntegral;
00322     typedef VigraFalseType isScalar;
00323     typedef VigraFalseType isOrdered;
00324 
00325     static RGBValue<T> zero() {
00326         return RGBValue<T>(NumericTraits<T>::zero());
00327     }
00328     static RGBValue<T> one() {
00329         return RGBValue<T>(NumericTraits<T>::one());
00330     }
00331     static RGBValue<T> nonZero() {
00332         return RGBValue<T>(NumericTraits<T>::nonZero());
00333     }
00334 
00335     static Promote toPromote(RGBValue<T> const & v) {
00336         return Promote(v);
00337     }
00338     static RealPromote toRealPromote(RGBValue<T> const & v) {
00339         return RealPromote(v);
00340     }
00341     static RGBValue<T> fromPromote(Promote const & v) {
00342         return RGBValue<T>(NumericTraits<T>::fromPromote(v.red()),
00343                            NumericTraits<T>::fromPromote(v.green()),
00344                            NumericTraits<T>::fromPromote(v.blue()));
00345     }
00346     static RGBValue<T> fromRealPromote(RealPromote const & v) {
00347         return RGBValue<T>(NumericTraits<T>::fromRealPromote(v.red()),
00348                            NumericTraits<T>::fromRealPromote(v.green()),
00349                            NumericTraits<T>::fromRealPromote(v.blue()));
00350     }
00351 };
00352 
00353 template <class T1, class T2>
00354 struct PromoteTraits<RGBValue<T1>, RGBValue<T2> >
00355 {
00356     typedef RGBValue<typename PromoteTraits<T1, T2>::Promote> Promote;
00357 };
00358 
00359 template <class T>
00360 struct PromoteTraits<RGBValue<T>, double >
00361 {
00362     typedef RGBValue<typename NumericTraits<T>::RealPromote> Promote;
00363 };
00364 
00365 template <class T>
00366 struct PromoteTraits<double, RGBValue<T> >
00367 {
00368     typedef RGBValue<typename NumericTraits<T>::RealPromote> Promote;
00369 };
00370 
00371 #else // NO_PARTIAL_TEMPLATE_SPECIALIZATION
00372 
00373 #define RGBVALUE_NUMTRAITS(T) \
00374 template<>\
00375 struct NumericTraits<RGBValue<T> >\
00376 {\
00377     typedef RGBValue<T> Type;\
00378     typedef RGBValue<NumericTraits<T>::Promote> Promote;\
00379     typedef RGBValue<NumericTraits<T>::RealPromote> RealPromote;\
00380     typedef NumericTraits<T>::isIntegral isIntegral;\
00381     typedef VigraFalseType isScalar;\
00382     typedef VigraFalseType isOrdered;\
00383     \
00384     static RGBValue<T> zero() { \
00385         return RGBValue<T>(NumericTraits<T>::zero()); \
00386     }\
00387     static RGBValue<T> one() { \
00388         return RGBValue<T>(NumericTraits<T>::one()); \
00389     }\
00390     static RGBValue<T> nonZero() { \
00391         return RGBValue<T>(NumericTraits<T>::nonZero()); \
00392     }\
00393     \
00394     static Promote toPromote(RGBValue<T> const & v) { \
00395         return Promote(v); \
00396     }\
00397     static RealPromote toRealPromote(RGBValue<T> const & v) { \
00398         return RealPromote(v); \
00399     }\
00400     static RGBValue<T> fromPromote(Promote const & v) { \
00401         RGBValue<T> res;\
00402         RGBValue<T>::iterator d = res.begin();\
00403         Promote::const_iterator s = v.begin();\
00404         for(; d != res.end(); ++d, ++s)\
00405             *d = NumericTraits<T>::fromPromote(*s);\
00406         return res;\
00407     }\
00408     static RGBValue<T> fromRealPromote(RealPromote const & v) {\
00409         RGBValue<T> res;\
00410         RGBValue<T>::iterator d = res.begin();\
00411         RealPromote::const_iterator s = v.begin();\
00412         for(; d != res.end(); ++d, ++s)\
00413             *d = NumericTraits<T>::fromRealPromote(*s);\
00414         return res;\
00415     }\
00416 };
00417 
00418 #define RGBVALUE_PROMTRAITS1(type1) \
00419 template<> \
00420 struct PromoteTraits<RGBValue<type1>, RGBValue<type1> > \
00421 { \
00422     typedef RGBValue<PromoteTraits<type1, type1>::Promote> Promote; \
00423     static Promote toPromote(RGBValue<type1> const & v) { \
00424         return static_cast<Promote>(v); } \
00425 };
00426 
00427 #define RGBVALUE_PROMTRAITS2(type1, type2) \
00428 template<> \
00429 struct PromoteTraits<RGBValue<type1>, RGBValue<type2> > \
00430 { \
00431     typedef RGBValue<PromoteTraits<type1, type2>::Promote> Promote; \
00432     static Promote toPromote(RGBValue<type1> const & v) { \
00433         return static_cast<Promote>(v); } \
00434     static Promote toPromote(RGBValue<type2> const & v) { \
00435         return static_cast<Promote>(v); } \
00436 };
00437 
00438 RGBVALUE_NUMTRAITS(unsigned char)
00439 RGBVALUE_NUMTRAITS(int)
00440 RGBVALUE_NUMTRAITS(float)
00441 RGBVALUE_NUMTRAITS(double)
00442 RGBVALUE_PROMTRAITS1(unsigned char)
00443 RGBVALUE_PROMTRAITS1(int)
00444 RGBVALUE_PROMTRAITS1(float)
00445 RGBVALUE_PROMTRAITS1(double)
00446 RGBVALUE_PROMTRAITS2(float, unsigned char)
00447 RGBVALUE_PROMTRAITS2(unsigned char, float)
00448 RGBVALUE_PROMTRAITS2(int, unsigned char)
00449 RGBVALUE_PROMTRAITS2(unsigned char, int)
00450 RGBVALUE_PROMTRAITS2(int, float)
00451 RGBVALUE_PROMTRAITS2(float, int)
00452 RGBVALUE_PROMTRAITS2(double, unsigned char)
00453 RGBVALUE_PROMTRAITS2(unsigned char, double)
00454 RGBVALUE_PROMTRAITS2(int, double)
00455 RGBVALUE_PROMTRAITS2(double, int)
00456 RGBVALUE_PROMTRAITS2(double, float)
00457 RGBVALUE_PROMTRAITS2(float, double)
00458 
00459 #undef RGBVALUE_NUMTRAITS
00460 #undef RGBVALUE_PROMTRAITS1
00461 #undef RGBVALUE_PROMTRAITS2
00462 
00463 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
00464 
00465 
00466 /********************************************************/
00467 /*                                                      */
00468 /*                      RGBValue-Arithmetic             */
00469 /*                                                      */
00470 /********************************************************/
00471 
00472 /** \addtogroup RGBValueOperators
00473  */
00474 //@{
00475     /// componentwise add-assignment
00476 template <class V1, class V2>
00477 inline
00478 RGBValue<V1> &
00479 operator+=(RGBValue<V1> & l, RGBValue<V2> const & r)
00480 {
00481     l.red() += r.red();
00482     l.green() += r.green();
00483     l.blue() += r.blue();
00484     return l;
00485 }
00486 
00487     /// componentwise subtract-assignment
00488 template <class V1, class V2>
00489 inline
00490 RGBValue<V1> &
00491 operator-=(RGBValue<V1> & l, RGBValue<V2> const & r)
00492 {
00493     l.red() -= r.red();
00494     l.green() -= r.green();
00495     l.blue() -= r.blue();
00496     return l;
00497 }
00498 
00499     /// componentwise multiply-assignment
00500 template <class V1, class V2>
00501 inline
00502 RGBValue<V1> &
00503 operator*=(RGBValue<V1> & l, RGBValue<V2> const & r)
00504 {
00505     l.red() *= r.red();
00506     l.green() *= r.green();
00507     l.blue() *= r.blue();
00508     return l;
00509 }
00510 
00511     /// componentwise scalar multiply-assignment
00512 template <class V>
00513 inline
00514 RGBValue<V> &
00515 operator*=(RGBValue<V> & l, double r)
00516 {
00517     l.red() *= r;
00518     l.green() *= r;
00519     l.blue() *= r;
00520     return l;
00521 }
00522 
00523     /// componentwise scalar divide-assignment
00524 template <class V>
00525 inline
00526 RGBValue<V> &
00527 operator/=(RGBValue<V> & l, double r)
00528 {
00529     l.red() /= r;
00530     l.green() /= r;
00531     l.blue() /= r;
00532     return l;
00533 }
00534 
00535 using VIGRA_CSTD::abs;
00536 
00537     /// component-wise absolute value
00538 template <class T>
00539 inline
00540 RGBValue<T> abs(RGBValue<T> const & v) {
00541     return RGBValue<T>(abs(v.red()), abs(v.green()),  abs(v.blue()));
00542 }
00543 
00544 
00545 
00546     /// component-wise addition
00547 template <class V1, class V2>
00548 inline
00549 typename PromoteTraits<RGBValue<V1>, RGBValue<V2> >::Promote
00550 operator+(RGBValue<V1> const & r1, RGBValue<V2> const & r2)
00551 {
00552     typename PromoteTraits<RGBValue<V1>, RGBValue<V2> >::Promote res(r1);
00553 
00554     res += r2;
00555 
00556     return res;
00557 }
00558 
00559     /// component-wise subtraction
00560 template <class V1, class V2>
00561 inline
00562 typename PromoteTraits<RGBValue<V1>, RGBValue<V2> >::Promote
00563 operator-(RGBValue<V1> const & r1, RGBValue<V2> const & r2)
00564 {
00565     typename PromoteTraits<RGBValue<V1>, RGBValue<V2> >::Promote res(r1);
00566 
00567     res -= r2;
00568 
00569     return res;
00570 }
00571 
00572     /// component-wise multiplication
00573 template <class V1, class V2>
00574 inline
00575 typename PromoteTraits<RGBValue<V1>, RGBValue<V2> >::Promote
00576 operator*(RGBValue<V1> const & r1, RGBValue<V2> const & r2)
00577 {
00578     typename PromoteTraits<RGBValue<V1>, RGBValue<V2> >::Promote res(r1);
00579 
00580     res *= r2;
00581 
00582     return res;
00583 }
00584 
00585     /// component-wise left scalar multiplication
00586 template <class V>
00587 inline
00588 typename NumericTraits<RGBValue<V> >::RealPromote
00589 operator*(double v, RGBValue<V> const & r)
00590 {
00591     typename NumericTraits<RGBValue<V> >::RealPromote res(r);
00592 
00593     res *= v;
00594 
00595     return res;
00596 }
00597 
00598     /// component-wise right scalar multiplication
00599 template <class V>
00600 inline
00601 typename NumericTraits<RGBValue<V> >::RealPromote
00602 operator*(RGBValue<V> const & r, double v)
00603 {
00604     typename NumericTraits<RGBValue<V> >::RealPromote res(r);
00605 
00606     res *= v;
00607 
00608     return res;
00609 }
00610 
00611     /// component-wise scalar division
00612 template <class V>
00613 inline
00614 typename NumericTraits<RGBValue<V> >::RealPromote
00615 operator/(RGBValue<V> const & r, double v)
00616 {
00617     typename NumericTraits<RGBValue<V> >::RealPromote res(r);
00618 
00619     res /= v;
00620 
00621     return res;
00622 }
00623 
00624     /// cross product
00625 template <class V1, class V2>
00626 inline
00627 typename PromoteTraits<RGBValue<V1>, RGBValue<V2> >::Promote
00628 cross(RGBValue<V1> const & r1, RGBValue<V2> const & r2)
00629 {
00630     typedef typename PromoteTraits<RGBValue<V1>, RGBValue<V2> >::Promote
00631             Res;
00632     return  Res(r1.green()*r2.blue() - r1.blue()*r2.green(),
00633                 r1.blue()*r2.red() - r1.red()*r2.blue(),
00634                 r1.red()*r2.green() - r1.green()*r2.red());
00635 }
00636 
00637     /// dot product
00638 template <class V1, class V2>
00639 inline
00640 typename PromoteTraits<V1, V2>::Promote
00641 dot(RGBValue<V1> const & r1, RGBValue<V2> const & r2)
00642 {
00643     return r1.red()*r2.red() + r1.green()*r2.green() + r1.blue()*r2.blue();
00644 }
00645 
00646 using VIGRA_CSTD::ceil;
00647 
00648     /** Apply ceil() function to each RGB component.
00649     */
00650 template <class V>
00651 inline
00652 RGBValue<V>
00653 ceil(RGBValue<V> const & r)
00654 {
00655     return RGBValue<V>(ceil(r.red()),
00656                        ceil(r.green()),
00657                        ceil(r.blue()));
00658 }
00659 
00660 using VIGRA_CSTD::floor;
00661 
00662     /** Apply floor() function to each RGB component.
00663     */
00664 template <class V>
00665 inline
00666 RGBValue<V>
00667 floor(RGBValue<V> const & r)
00668 {
00669     return RGBValue<V>(floor(r.red()),
00670                        floor(r.green()),
00671                        floor(r.blue()));
00672 }
00673 
00674 //@}
00675 
00676 /********************************************************/
00677 /*                                                      */
00678 /*                      RGBValue-Accessors              */
00679 /*                                                      */
00680 /********************************************************/
00681 
00682 /** \addtogroup DataAccessors
00683 */
00684 //@{
00685 /** \defgroup RGBValueAccessors Accessors for RGBValue */
00686 //@{
00687     /** Encapsulate access to rgb values.
00688 
00689     <b>\#include</b> "<a href="rgbvalue_8hxx-source.html">vigra/rgbvalue.hxx</a>"<br>
00690     Namespace: vigra
00691     */
00692 template <class RGBVALUE>
00693 class RGBAccessor
00694 : public VectorAccessor<RGBVALUE>
00695 {
00696   public:
00697 
00698     typedef typename RGBVALUE::value_type component_type;
00699 
00700         /** Get value of the red component
00701         */
00702     template <class RGBIterator>
00703     component_type const & red(RGBIterator const & rgb) const
00704     {
00705         return (*rgb).red();
00706     }
00707 
00708     template <class V, class RGBIterator>
00709     void setRGB(V r, V g, V b, RGBIterator const & rgb) const
00710     {
00711         (*rgb).setRGB( r, g, b );
00712     }
00713 
00714     
00715         /** Set value of the red component. The type <TT>V</TT> of the passed
00716             in <TT>value</TT> is automatically converted to <TT>component_type</TT>.
00717         */
00718     template <class V, class RGBIterator>
00719     void setRed(V value, RGBIterator const & rgb) const
00720     {
00721         (*rgb).setRed(value);
00722     }
00723 
00724         /** Get value of the red component at an offset
00725         */
00726     template <class RGBIterator, class DIFFERENCE>
00727     component_type const & red(RGBIterator const & rgb, DIFFERENCE diff) const
00728     {
00729         return rgb[diff].red();
00730     }
00731 
00732         /** Set value of the red component at an offset. The type <TT>V</TT> of the passed
00733             in <TT>value</TT> is automatically converted to <TT>component_type</TT>.
00734         */
00735     template <class V, class RGBIterator, class DIFFERENCE>
00736     void setRed(V value, RGBIterator const & rgb, DIFFERENCE diff) const
00737     {
00738         rgb[diff].setRed(value);
00739     }
00740 
00741         /** Get value of the green component
00742         */
00743     template <class RGBIterator>
00744     component_type const & green(RGBIterator const & rgb) const
00745     {
00746         return (*rgb).green();
00747     }
00748 
00749         /** Set value of the green component. The type <TT>V</TT> of the passed
00750             in <TT>value</TT> is automatically converted to <TT>component_type</TT>.
00751         */
00752     template <class V, class RGBIterator>
00753     void setGreen(V value, RGBIterator const & rgb) const
00754     {
00755         (*rgb).setGreen(value);
00756     }
00757 
00758         /** Get value of the green component at an offset
00759         */
00760     template <class RGBIterator, class DIFFERENCE>
00761     component_type const & green(RGBIterator const & rgb, DIFFERENCE d) const
00762     {
00763         return rgb[d].green();
00764     }
00765 
00766         /** Set value of the green component at an offset. The type <TT>V</TT> of the passed
00767             in <TT>value</TT> is automatically converted to <TT>component_type</TT>.
00768         */
00769     template <class V, class RGBIterator, class DIFFERENCE>
00770     void setGreen(V value, RGBIterator const & rgb, DIFFERENCE d) const
00771     {
00772         rgb[d].setGreen(value);
00773     }
00774 
00775         /** Get value of the blue component
00776         */
00777     template <class RGBIterator>
00778     component_type const & blue(RGBIterator const & rgb) const
00779     {
00780         return (*rgb).blue();
00781     }
00782 
00783         /** Set value of the blue component. The type <TT>V</TT> of the passed
00784             in <TT>value</TT> is automatically converted to <TT>component_type</TT>.
00785         */
00786     template <class V, class RGBIterator>
00787     void setBlue(V value, RGBIterator const & rgb) const
00788     {
00789         (*rgb).setBlue(value);
00790     }
00791 
00792         /** Get value of the blue component at an offset
00793         */
00794     template <class RGBIterator, class DIFFERENCE>
00795     component_type const & blue(RGBIterator const & rgb, DIFFERENCE d) const
00796     {
00797         return rgb[d].blue();
00798     }
00799 
00800         /** Set value of the blue component at an offset. The type <TT>V</TT> of the passed
00801             in <TT>value</TT> is automatically converted to <TT>component_type</TT>.
00802         */
00803     template <class V, class RGBIterator, class DIFFERENCE>
00804     void setBlue(V value, RGBIterator const & rgb, DIFFERENCE d) const
00805     {
00806         rgb[d].setBlue(value);
00807     }
00808 
00809 };
00810 
00811 
00812 /********************************************************/
00813 /*                                                      */
00814 /*                       RedAccessor                    */
00815 /*                                                      */
00816 /********************************************************/
00817 
00818     /** Encapsulate access to red band of an rgb value.
00819 
00820     <b>\#include</b> "<a href="rgbvalue_8hxx-source.html">vigra/rgbvalue.hxx</a>"<br>
00821     Namespace: vigra
00822     */
00823 template <class RGBVALUE>
00824 class RedAccessor
00825 {
00826   public:
00827     typedef typename RGBVALUE::value_type value_type;
00828 
00829         /** Get value of the red component
00830         */
00831     template <class ITERATOR>
00832     value_type const & operator()(ITERATOR const & i) const {
00833         return (*i).red();
00834     }
00835 
00836         /** Get value of the red component at an offset
00837         */
00838     template <class ITERATOR, class DIFFERENCE>
00839     value_type const & operator()(ITERATOR const & i, DIFFERENCE d) const
00840     {
00841         return i[d].red();
00842     }
00843 
00844         /** Set value of the red component. The type <TT>V</TT> of the passed
00845             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00846         */
00847     template <class V, class ITERATOR>
00848     void set(V value, ITERATOR const & i) const {
00849         (*i).setRed(value);
00850     }
00851 
00852 
00853         /** Set value of the red component at an offset. The type <TT>V</TT> of the passed
00854             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00855         */
00856     template <class V, class ITERATOR, class DIFFERENCE>
00857     void set(V value, ITERATOR const & i, DIFFERENCE d) const
00858     {
00859         i[d].setRed(value);
00860     }
00861 };
00862 
00863 /********************************************************/
00864 /*                                                      */
00865 /*                     GreenAccessor                    */
00866 /*                                                      */
00867 /********************************************************/
00868 
00869     /** Encapsulate access to green band of an rgb value.
00870 
00871     <b>\#include</b> "<a href="rgbvalue_8hxx-source.html">vigra/rgbvalue.hxx</a>"<br>
00872     Namespace: vigra
00873     */
00874 template <class RGBVALUE>
00875 class GreenAccessor
00876 {
00877   public:
00878     typedef typename RGBVALUE::value_type value_type;
00879 
00880         /** Get value of the green component
00881         */
00882     template <class ITERATOR>
00883     value_type const & operator()(ITERATOR const & i) const {
00884         return (*i).green();
00885     }
00886 
00887         /** Get value of the green component at an offset
00888         */
00889     template <class ITERATOR, class DIFFERENCE>
00890     value_type const & operator()(ITERATOR const & i, DIFFERENCE d) const
00891     {
00892         return i[d].green();
00893     }
00894 
00895         /** Set value of the green component. The type <TT>V</TT> of the passed
00896             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00897         */
00898     template <class V, class ITERATOR>
00899     void set(V value, ITERATOR const & i) const {
00900         (*i).setGreen(value);
00901     }
00902 
00903 
00904         /** Set value of the green component at an offset. The type <TT>V</TT> of the passed
00905             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00906         */
00907     template <class V, class ITERATOR, class DIFFERENCE>
00908     void set(V value, ITERATOR const & i, DIFFERENCE d) const
00909     {
00910         i[d].setGreen(value);
00911     }
00912 };
00913 
00914 /********************************************************/
00915 /*                                                      */
00916 /*                     BlueAccessor                     */
00917 /*                                                      */
00918 /********************************************************/
00919 
00920     /** Encapsulate access to blue band of an rgb value.
00921 
00922     <b>\#include</b> "<a href="rgbvalue_8hxx-source.html">vigra/rgbvalue.hxx</a>"<br>
00923     Namespace: vigra
00924     */
00925 template <class RGBVALUE>
00926 class BlueAccessor
00927 {
00928   public:
00929     typedef typename RGBVALUE::value_type value_type;
00930 
00931         /** Get value of the blue component
00932         */
00933     template <class ITERATOR>
00934     value_type const & operator()(ITERATOR const & i) const {
00935         return (*i).blue();
00936     }
00937 
00938         /** Get value of the blue component at an offset
00939         */
00940     template <class ITERATOR, class DIFFERENCE>
00941     value_type const & operator()(ITERATOR const & i, DIFFERENCE d) const
00942     {
00943         return i[d].blue();
00944     }
00945 
00946         /** Set value of the blue component. The type <TT>V</TT> of the passed
00947             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00948         */
00949     template <class V, class ITERATOR>
00950     void set(V value, ITERATOR const & i) const {
00951         (*i).setBlue(value);
00952     }
00953 
00954 
00955         /** Set value of the blue component at an offset. The type <TT>V</TT> of the passed
00956             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00957         */
00958     template <class V, class ITERATOR, class DIFFERENCE>
00959     void set(V value, ITERATOR const & i, DIFFERENCE d) const
00960     {
00961         i[d].setBlue(value);
00962     }
00963 };
00964 
00965 /********************************************************/
00966 /*                                                      */
00967 /*                  RGBToGrayAccessor                   */
00968 /*                                                      */
00969 /********************************************************/
00970 
00971     /** Encapsulate access to luminance of an rgb value.
00972 
00973     <b>\#include</b> "<a href="rgbvalue_8hxx-source.html">vigra/rgbvalue.hxx</a>"<br>
00974     Namespace: vigra
00975     */
00976 template <class RGBVALUE>
00977 class RGBToGrayAccessor
00978 {
00979   public:
00980     typedef typename RGBVALUE::value_type value_type;
00981 
00982         /** Get value of the luminance
00983         */
00984     template <class ITERATOR>
00985     value_type operator()(ITERATOR const & i) const {
00986                 return (*i).luminance(); }
00987 
00988         /** Get value of the luminance at an offset
00989         */
00990     template <class ITERATOR, class DIFFERENCE>
00991     value_type operator()(ITERATOR const & i, DIFFERENCE d) const
00992     {
00993         return i[d].luminance();
00994     }
00995 };
00996 
00997 
00998 //@}
00999 //@}
01000 
01001 
01002 } // namespace vigra
01003 
01004 #endif // VIGRA_RGBVALUE_HXX

© Ullrich Köthe (koethe@informatik.uni-hamburg.de)
Cognitive Systems Group, University of Hamburg, Germany

html generated using doxygen and Python
VIGRA 1.2.0 (7 Aug 2003)