pixelFormat.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_VIEW_RENDERING_PIXELFORMAT_H
00006 #define BALL_VIEW_RENDERING_PIXELFORMAT_H
00007 
00008 #include <BALL/COMMON/global.h>
00009 
00010 #include <iostream>
00011 
00012 namespace BALL 
00013 {
00014   
00015   namespace VIEW
00016   {
00017     
00018     struct ChannelFormat {
00019 
00020       enum ChannelID {
00021         NO_CHANNEL = 0, // no channel info
00022         EMPTY_CHANNEL,  // used for specifying empty space in a pixel
00023         RED_CHANNEL,
00024         GREEN_CHANNEL,
00025         BLUE_CHANNEL,
00026         LUMINANCE_CHANNEL,
00027         ALPHA_CHANNEL,
00028         DEPTH_CHANNEL,
00029         Y_CHANNEL,
00030         U_CHANNEL,
00031         V_CHANNEL
00032       };
00033 
00034       enum ChannelType {
00035         SIGNED_INT_CHANNEL,
00036         UNSIGNED_INT_CHANNEL,
00037         FLOAT_CHANNEL
00038       };
00039 
00040       ChannelID id          : 8;
00041       ChannelType type      : 8;
00042       unsigned int bitSize  : 8;
00043 
00044       ChannelFormat() : id(NO_CHANNEL) { }
00045 
00046       ChannelFormat(ChannelID id, ChannelType type, unsigned int bitSize) :
00047         id(id), type(type), bitSize(bitSize)
00048       { }
00049 
00050       bool operator==(const ChannelFormat &f) const {
00051         return id == f.id && type == f.type && bitSize == f.bitSize;
00052       }
00053 
00054       bool operator!=(const ChannelFormat &f) const {
00055         return id != f.id || type != f.type || bitSize != f.bitSize;
00056       }
00057 
00058     };
00059 
00060 
00061     class PixelFormat {
00062     public:
00063           
00064       enum { MAX_NUMBER_OF_CHANNELS = 4 };
00065           
00066       // Flags
00067       enum {
00068         // framebuffer data structure
00069         PLANAR_FRAMEBUFFER                  = 1<<1 // otherwise interlaced
00070       };
00071 
00072 
00073       PixelFormat() : numChannels(0), flags(0) { }
00074 
00075   #define _RTSG_FB_SETCH(i,v)                     \
00076       this->channels[(i)] = v;                \
00077       if (v.id != ChannelFormat::NO_CHANNEL)  \
00078         ++numChannels
00079 
00080 
00081       PixelFormat(const ChannelFormat &c0,
00082             unsigned int flags = 0) :
00083         numChannels(0), flags(flags)
00084       {
00085         _RTSG_FB_SETCH(0,c0);
00086       }
00087 
00088       PixelFormat(const ChannelFormat &c0,
00089             const ChannelFormat &c1,
00090             unsigned int flags = 0) :
00091         numChannels(0), flags(flags)
00092       {
00093         _RTSG_FB_SETCH(0,c0);
00094         _RTSG_FB_SETCH(1,c1);
00095       }
00096 
00097       PixelFormat(const ChannelFormat &c0,
00098             const ChannelFormat &c1,
00099             const ChannelFormat &c2,
00100             unsigned int flags = 0) :
00101         numChannels(0), flags(flags)
00102       {
00103         _RTSG_FB_SETCH(0,c0);
00104         _RTSG_FB_SETCH(1,c1);
00105         _RTSG_FB_SETCH(2,c2);
00106       }
00107 
00108       PixelFormat(const ChannelFormat &c0,
00109             const ChannelFormat &c1,
00110             const ChannelFormat &c2,
00111             const ChannelFormat &c3,
00112             unsigned int flags = 0) :
00113         numChannels(0), flags(flags)
00114       {
00115         _RTSG_FB_SETCH(0,c0);
00116         _RTSG_FB_SETCH(1,c1);
00117         _RTSG_FB_SETCH(2,c2);
00118         _RTSG_FB_SETCH(3,c3);
00119       }
00120           
00121   #undef _RTSG_FB_SETCH
00122 
00123       unsigned int getNumChannels() const { return numChannels; }
00124 
00125       const ChannelFormat &getChannel(unsigned int i) const
00126       { 
00127         return channels[i];
00128       }
00129 
00130       ChannelFormat &getChannel(unsigned int i)
00131       { 
00132         return channels[i];
00133       }
00134           
00135       const ChannelFormat &operator[](unsigned int i) const 
00136       {
00137         return channels[i];
00138       }
00139 
00140       ChannelFormat &operator[](unsigned int i)  
00141       {
00142         return channels[i];
00143       }
00144 
00145       unsigned int getFlags() const { return flags; }
00146 
00147       void setFlags(unsigned int f) { flags = f; }
00148 
00149       unsigned int computeBitSize() const {
00150         unsigned int bitSize = 0;
00151         for (unsigned int i = 0; i < numChannels; i++) {
00152           bitSize += channels[i].bitSize;
00153         }
00154         return bitSize;
00155       }
00156 
00157       unsigned int computeByteSize() const {
00158         unsigned int bitSize = computeBitSize();
00159         return (bitSize / 8) + ((bitSize % 8) > 0 ? 1 : 0) ;
00160       }
00161           
00162       PixelFormat &operator=(const PixelFormat &f)
00163       {
00164         numChannels = f.numChannels;
00165         flags = f.flags;
00166         for (unsigned int i = 0; i < numChannels; i++) {
00167           channels[i] = f.channels[i];
00168         }
00169         return *this;
00170       }
00171 
00172       bool operator==(const PixelFormat &f) const
00173       {
00174         if (numChannels != f.numChannels || flags != f.flags)
00175           return false;
00176         for (unsigned int i = 0; i < numChannels; i++) {
00177           if (channels[i] != f.channels[i])
00178             return false;
00179         }
00180         return true;
00181       }
00182 
00183       bool operator!=(const PixelFormat &f) const {
00184         return !(*this == f);
00185       }
00186 
00187       void print(std::ostream &o) const;
00188 
00189       static const PixelFormat RGB_24;
00190       static const PixelFormat BGR_24;
00191 
00192       static const PixelFormat RGB_32;
00193       static const PixelFormat RGBA_32;
00194       static const PixelFormat BGR_32;
00195       static const PixelFormat BGRA_32;
00196 
00197 
00198       static const PixelFormat RGB_3_2_2;
00199 
00200 
00201       static const PixelFormat RGBF_96;
00202 
00203     private:
00204       ChannelFormat channels[MAX_NUMBER_OF_CHANNELS];
00205       unsigned int numChannels;
00206       unsigned int flags;
00207     };
00208 
00209     inline std::ostream &operator<<(std::ostream &o, const PixelFormat &f)
00210     {
00211       f.print(o);
00212       return o;
00213     }
00214 
00215   } // namespace VIEW
00216     
00217 } // namespace BALL
00218 
00219 #endif //BALL_VIEW_RENDERING_PIXELFORMAT_H