Equalizer  1.6.1
pixel.h
1 
2 /* Copyright (c) 2007-2012, Stefan Eilemann <eile@equalizergraphics.com>
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 2.1 as published
6  * by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11  * details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 #ifndef EQFABRIC_PIXEL_H
19 #define EQFABRIC_PIXEL_H
20 
21 #include <eq/fabric/api.h>
22 #include <lunchbox/log.h>
23 #include <lunchbox/types.h>
24 
25 namespace eq
26 {
27 namespace fabric
28 {
29  class Pixel;
30  std::ostream& operator << ( std::ostream& os, const Pixel& pixel );
31 
39  class Pixel
40  {
41  public:
48  Pixel() : x( 0 ), y( 0 ), w( 1 ), h( 1 ) {}
49 
51  Pixel( const uint32_t x_, const uint32_t y_,
52  const uint32_t w_, const uint32_t h_ )
53  : x( x_ ), y( y_ ), w( w_ ), h( h_ ) {}
55 
57  void apply( const Pixel& rhs )
58  {
59  if( !isValid() || !rhs.isValid( ))
60  return;
61 
62  x = x * rhs.w + rhs.x;
63  w *= rhs.w;
64  y = y * rhs.h + rhs.y;
65  h *= rhs.h;
66  }
67 
72  bool operator == ( const Pixel& rhs ) const
73  {
74  return x==rhs.x && y==rhs.y && w==rhs.w && h==rhs.h;
75  }
76 
81  bool operator != ( const Pixel& rhs ) const
82  {
83  return x!=rhs.x || y!=rhs.y || w!=rhs.w || h!=rhs.h;
84  }
85 
87  void invalidate() { x = y = w = h = 0; }
88 
90  void validate()
91  {
92  if( isValid( )) return;
93  LBWARN << "Invalid " << *this << std::endl;
94  if( w == 0 ) w = 1;
95  if( h == 0 ) h = 1;
96  if( x >= w ) x = 0;
97  if( y >= h ) y = 0;
98  LBWARN << "Corrected " << *this << std::endl;
99  }
100 
102  bool isValid() const { return ( w>0 && x<w && h>0 && y<h ); }
103 
104  uint32_t x;
105  uint32_t y;
106  uint32_t w;
107  uint32_t h;
108 
110  EQFABRIC_API static const Pixel ALL;
111  };
112 
113  inline std::ostream& operator << ( std::ostream& os, const Pixel& pixel )
114  {
115  if( pixel.isValid( ))
116  os << "pixel [ " << pixel.x << ' ' << pixel.y
117  << ' ' << pixel.w << ' ' << pixel.h << " ]";
118  return os;
119  }
120 }
121 }
122 
123 namespace lunchbox
124 {
125 template<> inline void byteswap( eq::fabric::Pixel& value )
126 {
127  byteswap( value.x );
128  byteswap( value.y );
129  byteswap( value.w );
130  byteswap( value.h );
131 }
132 }
133 #endif // EQFABRIC_PIXEL_H
void apply(const Pixel &rhs)
Apply (accumulate) another pixel specification.
Definition: pixel.h:57
bool operator==(const Pixel &rhs) const
Definition: pixel.h:72
Pixel()
Construct a pixel specification covering all pixels of a frustum.
Definition: pixel.h:48
Holds a pixel decomposition specification with methods for manipulation.
Definition: pixel.h:39
Pixel(const uint32_t x_, const uint32_t y_, const uint32_t w_, const uint32_t h_)
Construct a pixel specification with default values.
Definition: pixel.h:51
static const Pixel ALL
A pixel specification covering all pixels.
Definition: pixel.h:110
bool operator!=(const Pixel &rhs) const
Definition: pixel.h:81
void validate()
Make the pixel specification valid.
Definition: pixel.h:90
void invalidate()
Make the pixel specification invalid.
Definition: pixel.h:87
bool isValid() const
Definition: pixel.h:102