pixel.h

00001 
00002 /* Copyright (c) 2007, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef EQ_PIXEL_H
00019 #define EQ_PIXEL_H
00020 
00021 #include <eq/base/base.h>
00022 #include <eq/base/log.h>
00023 
00024 namespace eq
00025 {
00026     class Pixel;
00027     std::ostream& operator << ( std::ostream& os, const Pixel& pixel );
00028 
00037     class Pixel 
00038     {
00039     public:
00044         Pixel() : x( 0 ), y( 0 ), w( 1 ), h( 1 )  {}
00045 
00046         Pixel( const uint32_t x_, const uint32_t y_, 
00047                const uint32_t w_, const uint32_t h_ )
00048                 : x( x_ ), y( y_ ), w( w_ ), h( h_ ) {}
00050 
00051         void apply( const Pixel& rhs )
00052             {
00053                 if( !isValid() || !rhs.isValid( ))
00054                     return;
00055 
00056                 x = x * rhs.w + rhs.x;
00057                 w *= rhs.w;
00058                 y = y * rhs.h + rhs.y;
00059                 h *= rhs.h;
00060             }
00061 
00062         bool operator == ( const Pixel& rhs ) const
00063         {
00064             return x==rhs.x && y==rhs.y && w==rhs.w && h==rhs.h;
00065         }
00066         
00067         bool operator != ( const Pixel& rhs ) const
00068         {
00069             return x!=rhs.x || y!=rhs.y || w!=rhs.w || h!=rhs.h;
00070         }
00071         
00072         void invalidate() 
00073             { x = y = w = h = 0; }
00074 
00075         void validate()
00076             {
00077                 if( isValid( )) return;
00078                 EQWARN << "Invalid " << *this << std::endl;
00079                 if( w == 0 ) w = 1;
00080                 if( h == 0 ) h = 1;
00081                 if( x >= w ) x = 0;
00082                 if( y >= h ) y = 0;
00083                 EQWARN << "Corrected " << *this << std::endl;
00084             }
00085 
00086         bool isValid() const { return ( w>0 && x<w && h>0 && y<h ); }
00087 
00088         uint32_t x;
00089         uint32_t y;
00090         uint32_t w;
00091         uint32_t h;
00092 
00093         EQ_EXPORT static const Pixel ALL;
00094     };
00095 
00096     inline std::ostream& operator << ( std::ostream& os, const Pixel& pixel )
00097     {
00098         if( pixel.isValid( ))
00099             os << "pixel     [ " << pixel.x << ' ' << pixel.y
00100                << ' ' << pixel.w << ' ' << pixel.h << " ]";
00101         return os;
00102     }
00103 }
00104 
00105 #endif // EQ_PIXEL_H
Generated on Mon Aug 10 18:58:40 2009 for Equalizer 0.9 by  doxygen 1.5.8