core/vgui/vgui_pixel.h
Go to the documentation of this file.
00001 // This is core/vgui/vgui_pixel.h
00002 #ifndef vgui_pixel_h_
00003 #define vgui_pixel_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief contains struct vgui_pixel_rgb<r,g,b> and other similar ones
00010 // \author fsm
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //   Jan 2003, Amitha Perera: added functionality used in displaying vil images.
00015 //             The vil1 and vil functionality could probably be coalesced for a cleaner design.
00016 // \endverbatim
00017 
00018 #include <vgui/vgui_gl.h>
00019 #include <vxl_config.h>
00020 
00021 //: Now we define a bunch of pixel-type structs
00022 // These are \e empty templates. only the specializations make sense.
00023 template <int r, int g, int b> struct vgui_pixel_rgb;
00024 template <int b, int g, int r> struct vgui_pixel_bgr;
00025 template <int r, int g, int b, int a> struct vgui_pixel_rgba;
00026 template <int a, int b, int g, int r> struct vgui_pixel_abgr;
00027 template <int b, int g, int r, int a> struct vgui_pixel_bgra;
00028 struct vgui_pixel_rgbfloat;
00029 
00030 VCL_DEFINE_SPECIALIZATION
00031 struct vgui_pixel_rgb<8,8,8>
00032 {
00033   GLubyte R;
00034   GLubyte G;
00035   GLubyte B;
00036   vgui_pixel_rgb<8,8,8>() { }
00037   vgui_pixel_rgb<8,8,8>( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte /*alpha*/ = 0 )
00038   : R(red), G(green), B(blue) {}
00039 };
00040 typedef vgui_pixel_rgb<8,8,8> vgui_pixel_rgb888;
00041 
00042 VCL_DEFINE_SPECIALIZATION
00043 struct vgui_pixel_rgb<16,16,16>
00044 {
00045   GLushort R;
00046   GLushort G;
00047   GLushort B;
00048   vgui_pixel_rgb<16,16,16>() { }
00049   vgui_pixel_rgb<16,16,16>( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte /*alpha*/ = 0 )
00050   : R(red), G(green), B(blue) {}
00051 };
00052 typedef vgui_pixel_rgb<16,16,16> vgui_pixel_rgb161616;
00053 
00054 // For 16bit X display.
00055 // With any luck, this will pack into a 16bit word. It works on gcc 2.95,
00056 // but only because we're using 'short' (which is 2 bytes) as the bitfield
00057 // type. If one uses 'char' one gets a 3-byte structure. In one uses 'int'
00058 // the result is a 4-byte structure. So don't change 'short'!
00059 VCL_DEFINE_SPECIALIZATION
00060 struct vgui_pixel_rgb<5,6,5>
00061 {
00062 #if VXL_LITTLE_ENDIAN
00063   GLushort B : 5;
00064   GLushort G : 6;
00065   GLushort R : 5;
00066 #else
00067   GLushort R : 5;
00068   GLushort G : 6;
00069   GLushort B : 5;
00070 #endif
00071   vgui_pixel_rgb<5,6,5>() { }
00072   vgui_pixel_rgb<5,6,5>( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte /*alpha*/ = 0 )
00073 #if VXL_LITTLE_ENDIAN
00074   : B(blue>>3), G(green>>2), R(red>>3) {}
00075 #else
00076   : R(red>>3), G(green>>2), B(blue>>3) {}
00077 #endif
00078 };
00079 
00080 typedef vgui_pixel_rgb<5,6,5> vgui_pixel_rgb565;
00081 
00082 VCL_DEFINE_SPECIALIZATION
00083 struct vgui_pixel_bgr<5,6,5>
00084 {
00085 #if VXL_LITTLE_ENDIAN
00086   GLushort R : 5;
00087   GLushort G : 6;
00088   GLushort B : 5;
00089 #else
00090   GLushort B : 5;
00091   GLushort G : 6;
00092   GLushort R : 5;
00093 #endif
00094   vgui_pixel_bgr<5,6,5>() { }
00095   vgui_pixel_bgr<5,6,5>( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte /*alpha*/ = 0 )
00096 #if VXL_LITTLE_ENDIAN
00097   : R(red>>3), G(green>>2), B(blue>>3) {}
00098 #else
00099   : B(blue>>3), G(green>>2), R(red>>3) {}
00100 #endif
00101 };
00102 
00103 typedef vgui_pixel_bgr<5,6,5> vgui_pixel_bgr565;
00104 
00105 VCL_DEFINE_SPECIALIZATION
00106 struct vgui_pixel_bgr<8,8,8>
00107 {
00108   GLubyte B;
00109   GLubyte G;
00110   GLubyte R;
00111   vgui_pixel_bgr<8,8,8>() {}
00112   vgui_pixel_bgr<8,8,8>( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte /*alpha*/ = 0 )
00113   : B(blue), G(green), R(red) {}
00114 };
00115 typedef vgui_pixel_bgr<8,8,8> vgui_pixel_bgr888;
00116 
00117 VCL_DEFINE_SPECIALIZATION
00118 struct vgui_pixel_rgba<5,5,5,1>
00119 {
00120   GLushort B:5;
00121   GLushort G:5;
00122   GLushort R:5;
00123   GLushort A:1;
00124   vgui_pixel_rgba<5,5,5,1>() { }
00125   vgui_pixel_rgba<5,5,5,1>( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte alpha )
00126   : B(blue>>3), G(green>>3), R(red>>3), A(alpha) {}
00127 };
00128 typedef vgui_pixel_rgba<5,5,5,1> vgui_pixel_bgra5551;
00129 
00130 VCL_DEFINE_SPECIALIZATION
00131 struct vgui_pixel_rgba<8,8,8,8>
00132 {
00133   GLubyte R;
00134   GLubyte G;
00135   GLubyte B;
00136   GLubyte A;
00137   vgui_pixel_rgba<8,8,8,8>() { }
00138   vgui_pixel_rgba<8,8,8,8>( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte alpha )
00139   : R(red), G(green), B(blue), A(alpha) {}
00140 };
00141 typedef vgui_pixel_rgba<8,8,8,8> vgui_pixel_rgba8888;
00142 
00143 VCL_DEFINE_SPECIALIZATION
00144 struct vgui_pixel_abgr<8,8,8,8>
00145 {
00146   GLubyte A;
00147   GLubyte B;
00148   GLubyte G;
00149   GLubyte R;
00150   vgui_pixel_abgr<8,8,8,8>() { }
00151   vgui_pixel_abgr<8,8,8,8>( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte alpha )
00152   : A(alpha), B(blue), G(green), R(red) {}
00153 };
00154 typedef vgui_pixel_abgr<8,8,8,8> vgui_pixel_abgr8888;
00155 
00156 VCL_DEFINE_SPECIALIZATION
00157 struct vgui_pixel_bgra<8,8,8,8>
00158 {
00159   GLubyte B;
00160   GLubyte G;
00161   GLubyte R;
00162   GLubyte A;
00163   vgui_pixel_bgra<8,8,8,8>() { }
00164   vgui_pixel_bgra<8,8,8,8>( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte alpha )
00165   : B(blue), G(green), R(red), A(alpha) {}
00166 };
00167 typedef vgui_pixel_bgra<8,8,8,8> vgui_pixel_bgra8888;
00168 
00169 struct vgui_pixel_rgbfloat
00170 {
00171   float R;
00172   float G;
00173   float B;
00174   vgui_pixel_rgbfloat() {}
00175   vgui_pixel_rgbfloat( vxl_byte red, vxl_byte green, vxl_byte blue, vxl_byte /*alpha*/ = 0 )
00176     : R(red), G(green), B(blue) {}
00177 };
00178 
00179 
00180 //: Clamps the given type into [0,255].
00181 //
00182 template <class T>
00183 inline
00184 vxl_byte vgui_pixel_clamp( T in )
00185 {
00186   if ( in > 255 ) return 255u;
00187   if ( in < 0 ) return 0u;
00188   return vxl_byte(in);
00189 }
00190 
00191 // provide overloads for efficiency and to avoid warnings about
00192 // unnecessary comparisons (e.g. against 0 for an unsigned type).
00193 
00194 //: Clamps the given type into [0,255].
00195 //
00196 // This overload stretches (0,1) bool images to (0,255) byte images.
00197 inline
00198 vxl_byte vgui_pixel_clamp( bool in )
00199 {
00200   return in ? 255u : 0u;
00201 }
00202 
00203 //: Clamps the given type into [0,255].
00204 //
00205 // This overload is the null operation, and is provided for efficiency.
00206 inline
00207 vxl_byte vgui_pixel_clamp( vxl_byte in )
00208 {
00209   return in;
00210 }
00211 
00212 //: Clamps the given type into [0,255].
00213 //
00214 // This overload only checks the lower bound.
00215 inline
00216 vxl_byte vgui_pixel_clamp( vxl_sbyte in )
00217 {
00218   if ( in < 0 ) return 0u;
00219   else          return in;
00220 }
00221 
00222 //: Clamps the given type into [0,255].
00223 //
00224 // This overload only checks the upper bound, since the type is
00225 // unsigned.
00226 inline
00227 vxl_byte vgui_pixel_clamp( vxl_uint_16 in )
00228 {
00229   if ( in > 255 ) return 255u;
00230   else            return static_cast<vxl_byte>(in);
00231 }
00232 
00233 //: Clamps the given type into [0,255].
00234 //
00235 // This overload only checks the upper bound, since the type is
00236 // unsigned.
00237 inline
00238 vxl_byte vgui_pixel_clamp( vxl_uint_32 in )
00239 {
00240   if ( in > 255 ) return 255u;
00241   else            return static_cast<vxl_byte>(in);
00242 }
00243 
00244 
00245 //: Convert the given grey scale value to the appropriate OpenGL pixel type.
00246 template <class InT, class OutT>
00247 inline void
00248 vgui_pixel_convert( InT const& in, OutT& out )
00249 {
00250   out = OutT( vgui_pixel_clamp( in ), vgui_pixel_clamp( in ),
00251               vgui_pixel_clamp( in ), 255 );
00252 }
00253 
00254 //: Convert the given RGB value to the appropriate OpenGL pixel type.
00255 template <class InT, class OutT>
00256 inline void
00257 vgui_pixel_convert( InT const& R, InT const& G, InT const& B,
00258                     OutT& out )
00259 {
00260   out = OutT( vgui_pixel_clamp( R ), vgui_pixel_clamp( G ),
00261               vgui_pixel_clamp( B ), 255 );
00262 }
00263 
00264 //: Convert the given RGBA value to the appropriate OpenGL pixel type.
00265 template <class InT, class OutT>
00266 inline void
00267 vgui_pixel_convert( InT const& R, InT const& G, InT const& B, InT const& A,
00268                     OutT& out )
00269 {
00270   out = OutT( vgui_pixel_clamp( R ), vgui_pixel_clamp( G ),
00271               vgui_pixel_clamp( B ), vgui_pixel_clamp( A ) );
00272 }
00273 
00274 
00275 //: Convert a span of pixels from one format to another.
00276 // In general, the input range is assumed to be 0..255, so bitfields
00277 // narrower than 8 bits need to be shifted.  Floats are clamped to 0..255
00278 void vgui_pixel_convert_span(GLubyte const *, vgui_pixel_rgb888 *, unsigned size);
00279 void vgui_pixel_convert_span(GLubyte const *, vgui_pixel_bgr888 *, unsigned size);
00280 void vgui_pixel_convert_span(GLubyte const *, vgui_pixel_rgb565 *, unsigned size);
00281 void vgui_pixel_convert_span(GLubyte const *, vgui_pixel_bgra5551 *, unsigned size);
00282 void vgui_pixel_convert_span(GLubyte const *, vgui_pixel_rgba8888 *, unsigned size);
00283 void vgui_pixel_convert_span(GLubyte const *, vgui_pixel_abgr8888 *, unsigned size);
00284 void vgui_pixel_convert_span(GLubyte const *, vgui_pixel_bgra8888 *, unsigned size);
00285 
00286 void vgui_pixel_convert_span(GLfloat const *, vgui_pixel_rgb888 *, unsigned size);
00287 void vgui_pixel_convert_span(GLfloat const *, vgui_pixel_bgr888 *, unsigned size);
00288 void vgui_pixel_convert_span(GLfloat const *, vgui_pixel_rgb565 *, unsigned size);
00289 void vgui_pixel_convert_span(GLfloat const *, vgui_pixel_bgra5551 *, unsigned size);
00290 void vgui_pixel_convert_span(GLfloat const *, vgui_pixel_rgba8888 *, unsigned size);
00291 void vgui_pixel_convert_span(GLfloat const *, vgui_pixel_abgr8888 *, unsigned size);
00292 void vgui_pixel_convert_span(GLfloat const *, vgui_pixel_bgra8888 *, unsigned size);
00293 
00294 void vgui_pixel_convert_span(GLdouble const *, vgui_pixel_rgb888 *, unsigned size);
00295 void vgui_pixel_convert_span(GLdouble const *, vgui_pixel_bgr888 *, unsigned size);
00296 void vgui_pixel_convert_span(GLdouble const *, vgui_pixel_rgb565 *, unsigned size);
00297 void vgui_pixel_convert_span(GLdouble const *, vgui_pixel_bgra5551 *, unsigned size);
00298 void vgui_pixel_convert_span(GLdouble const *, vgui_pixel_rgba8888 *, unsigned size);
00299 void vgui_pixel_convert_span(GLdouble const *, vgui_pixel_abgr8888 *, unsigned size);
00300 void vgui_pixel_convert_span(GLdouble const *, vgui_pixel_bgra8888 *, unsigned size);
00301 
00302 void vgui_pixel_convert_span(vgui_pixel_rgb888 const *, vgui_pixel_rgb888 *, unsigned size);
00303 void vgui_pixel_convert_span(vgui_pixel_rgb888 const *, vgui_pixel_bgr888 *, unsigned size);
00304 void vgui_pixel_convert_span(vgui_pixel_rgb888 const *, vgui_pixel_rgb565 *, unsigned size);
00305 void vgui_pixel_convert_span(vgui_pixel_rgb888 const *, vgui_pixel_bgra5551 *, unsigned size);
00306 void vgui_pixel_convert_span(vgui_pixel_rgb888 const *, vgui_pixel_rgba8888 *, unsigned size);
00307 void vgui_pixel_convert_span(vgui_pixel_rgb888 const *, vgui_pixel_abgr8888 *, unsigned size);
00308 void vgui_pixel_convert_span(vgui_pixel_rgb888 const *, vgui_pixel_bgra8888 *, unsigned size);
00309 
00310 void vgui_pixel_convert_span(vgui_pixel_rgba8888 const *, vgui_pixel_rgb888 *, unsigned size);
00311 void vgui_pixel_convert_span(vgui_pixel_rgba8888 const *, vgui_pixel_bgr888 *, unsigned size);
00312 void vgui_pixel_convert_span(vgui_pixel_rgba8888 const *, vgui_pixel_rgb565 *, unsigned size);
00313 void vgui_pixel_convert_span(vgui_pixel_rgba8888 const *, vgui_pixel_bgra5551 *, unsigned size);
00314 void vgui_pixel_convert_span(vgui_pixel_rgba8888 const *, vgui_pixel_rgba8888 *, unsigned size);
00315 void vgui_pixel_convert_span(vgui_pixel_rgba8888 const *, vgui_pixel_abgr8888 *, unsigned size);
00316 void vgui_pixel_convert_span(vgui_pixel_rgba8888 const *, vgui_pixel_bgra8888 *, unsigned size);
00317 
00318 void vgui_pixel_convert_span(vgui_pixel_rgbfloat const *, vgui_pixel_rgb888 *, unsigned size);
00319 void vgui_pixel_convert_span(vgui_pixel_rgbfloat const *, vgui_pixel_bgr888 *, unsigned size);
00320 void vgui_pixel_convert_span(vgui_pixel_rgbfloat const *, vgui_pixel_rgb565 *, unsigned size);
00321 void vgui_pixel_convert_span(vgui_pixel_rgbfloat const *, vgui_pixel_bgra5551 *, unsigned size);
00322 void vgui_pixel_convert_span(vgui_pixel_rgbfloat const *, vgui_pixel_rgba8888 *, unsigned size);
00323 void vgui_pixel_convert_span(vgui_pixel_rgbfloat const *, vgui_pixel_abgr8888 *, unsigned size);
00324 void vgui_pixel_convert_span(vgui_pixel_rgbfloat const *, vgui_pixel_bgra8888 *, unsigned size);
00325 
00326 #endif // vgui_pixel_h_