AaronCameron.net
No ads. No Profit. No Master, But Truth.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 02:05am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cPixelFormat.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #include "cPixelFormat.h"
00026 
00027 namespace n2l
00028 {
00029 
00030     const cPixelFormat::tBitCount cPixelFormat::nBitCount = cPixelFormat::tBitCount(-1);
00031 
00032 
00033     /* ************************************************************ */
00034     cPixelFormat::cPixelFormat()
00035     {
00036         clear();
00037     }
00038 
00039 
00040     /* ************************************************************ */
00041     cPixelFormat::cPixelFormat( const cPixelFormat & iFormat )
00042     {
00043         assign(iFormat);
00044     }
00045 
00046 
00047     /* ************************************************************ */
00048     cPixelFormat::cPixelFormat( const SDL_PixelFormat & iFormat ) :
00049         mBitsPerPixel( iFormat.BitsPerPixel ),
00050         mBytesPerPixel( iFormat.BytesPerPixel )
00051     {
00052         mCMask[Channel_Red] =   iFormat.Rmask;
00053         mCMask[Channel_Green] = iFormat.Gmask;
00054         mCMask[Channel_Blue] =  iFormat.Bmask;
00055         mCMask[Channel_Alpha] = iFormat.Amask;
00056         mCLoss[Channel_Red] =   iFormat.Rloss;
00057         mCLoss[Channel_Green] = iFormat.Gloss;
00058         mCLoss[Channel_Blue] =  iFormat.Bloss;
00059         mCLoss[Channel_Alpha] = iFormat.Aloss;
00060         mCShift[Channel_Red] =  iFormat.Rshift;
00061         mCShift[Channel_Green]= iFormat.Gshift;
00062         mCShift[Channel_Blue] = iFormat.Bshift;
00063         mCShift[Channel_Alpha]= iFormat.Ashift;
00064 
00065         calculateChannelBitSizes();
00066     }
00067 
00068 
00069     /* ************************************************************ */
00070     const cPixelFormat::tBitCount cPixelFormat::bitsPerPixel() const
00071     {
00072         return mBitsPerPixel;
00073     }
00074 
00075 
00076     /* ************************************************************ */
00077     const cPixelFormat::tByteCount cPixelFormat::bytesPerPixel() const
00078     {
00079         return mBytesPerPixel;
00080     }
00081 
00082 
00083     /* ************************************************************ */
00084     const cPixelFormat::tChannelMask cPixelFormat::channelMask( const tChannel iChannel ) const
00085     {
00086         if (iChannel>=Channel_NChannel || iChannel<0)
00087             throw cOutOfBoundsException("cPixelFormat::channelMask",
00088                                         "Channel out of bounds");
00089         return mCMask[iChannel];
00090     }
00091 
00092 
00093     /* ************************************************************ */
00094     const cPixelFormat::tBitCount cPixelFormat::channelLoss( const tChannel iChannel ) const
00095     {
00096         if (iChannel>=Channel_NChannel || iChannel<0)
00097             throw cOutOfBoundsException("cPixelFormat::channelLoss",
00098                                         "Channel out of bounds");
00099         return mCLoss[iChannel];
00100     }
00101 
00102 
00103     /* ************************************************************ */
00104     const cPixelFormat::tBitCount cPixelFormat::channelShift( const tChannel iChannel ) const
00105     {
00106         if (iChannel>=Channel_NChannel || iChannel<0)
00107             throw cOutOfBoundsException("cPixelFormat::channelShift",
00108                                         "Channel out of bounds");
00109         return mCShift[iChannel];
00110     }
00111 
00112 
00113     /* ************************************************************ */
00114     const cPixelFormat::tBitCount cPixelFormat::bitsForChannel( const tChannel iChannel ) const
00115     {
00116         if (iChannel>=Channel_NChannel || iChannel<0)
00117             throw cOutOfBoundsException("cPixelFormat::bitsForChannel",
00118                                         "Channel out of bounds");
00119         return mBitsPerChannel[iChannel];
00120     }
00121 
00122 
00123     /* ************************************************************ */
00124     void cPixelFormat::channelMasks(    tChannelMask & oRed,
00125                                         tChannelMask & oGreen,
00126                                         tChannelMask & oBlue,
00127                                         tChannelMask & oAlpha ) const
00128     {
00129         oRed =      mCMask[Channel_Red];
00130         oGreen =    mCMask[Channel_Green];
00131         oBlue =     mCMask[Channel_Blue];
00132         oAlpha =    mCMask[Channel_Alpha];
00133     }
00134 
00135     /* ************************************************************ */
00136     void cPixelFormat::channelLosses(   tBitCount & oRed,
00137                                         tBitCount & oGreen,
00138                                         tBitCount & oBlue,
00139                                         tBitCount & oAlpha ) const
00140     {
00141         oRed =      mCLoss[Channel_Red];
00142         oGreen =    mCLoss[Channel_Green];
00143         oBlue =     mCLoss[Channel_Blue];
00144         oAlpha =    mCLoss[Channel_Alpha];
00145     }
00146 
00147 
00148     /* ************************************************************ */
00149     void cPixelFormat::channelShifts(   tBitCount & oRed,
00150                                         tBitCount & oGreen,
00151                                         tBitCount & oBlue,
00152                                         tBitCount & oAlpha ) const
00153     {
00154         oRed =      mCShift[Channel_Red];
00155         oGreen =    mCShift[Channel_Green];
00156         oBlue =     mCShift[Channel_Blue];
00157         oAlpha =    mCShift[Channel_Alpha];
00158     }
00159 
00160 
00161 
00162     /* ************************************************************ */
00163     void cPixelFormat::assign( const cPixelFormat & iFormat )
00164     {
00165         mBitsPerPixel = iFormat.mBitsPerPixel;
00166         mBytesPerPixel = iFormat.mBytesPerPixel;
00167         for (tUbyte c=Channel_Red; c<Channel_NChannel; ++c) {
00168             mCMask[c] =             iFormat.mCMask[c];
00169             mCShift[c] =            iFormat.mCShift[c];
00170             mCLoss[c] =             iFormat.mCLoss[c];
00171             mBitsPerChannel[c] =    iFormat.mBitsPerChannel[c];
00172         }
00173     }
00174 
00175 
00176     /* ************************************************************ */
00177     void cPixelFormat::clear()
00178     {
00179         // Define a zero format
00180         mBitsPerPixel = 0;
00181         mBytesPerPixel = 0;
00182         for (tUbyte c=Channel_Red; c<Channel_NChannel; ++c) {
00183             mCMask[c] = 0x00;
00184             mCShift[c] = 0x00;
00185             mCLoss[c] = 0x00;
00186             mBitsPerChannel[c] = 0;
00187         }
00188     }
00189 
00190 
00191 
00192     /* ************************************************************ */
00193     void cPixelFormat::calculateChannelBitSizes()
00194     {
00195 
00196         for (tUbyte c=Channel_Red; c<Channel_NChannel; ++c) {
00197             // Set all bits
00198             tChannelMask v = tChannelMask(-1);
00199             v &= mCMask[c];
00200             v = (v >> mCShift[c]);
00201             tChannelMask bit = 0x01;
00202             mBitsPerChannel[c] = 0;
00203             while (v>bit) {
00204                 bit = bit << 1;
00205                 ++mBitsPerChannel[c];
00206             }
00207         }
00208     }
00209 
00210 
00211     /* ************************************************************ */
00212     const tBool cPixelFormat::operator ==( const cPixelFormat & iFormat ) const
00213     {
00214         if (mBitsPerPixel != iFormat.mBitsPerPixel ||
00215             mBytesPerPixel != iFormat.mBytesPerPixel) return false; 
00216         for (tUbyte c=Channel_Red; c<Channel_NChannel; ++c)
00217             if (mCMask[c]!=iFormat.mCMask[c] ||
00218                 mCShift[c]!=iFormat.mCShift[c] ||
00219                 mCLoss[c]!=iFormat.mCLoss[c] ||
00220                 mBitsPerChannel[c]!=iFormat.mBitsPerChannel[c]) return false;
00221         return true;
00222     }
00223 
00224 
00225     /* ************************************************************ */
00226     const tString cPixelFormat::asString() const
00227     {
00228         tString temp( "cPixelFormat(\n");
00229         temp += "\tBits/Pixel:\t" + n2l::asString(mBitsPerPixel) + "\n";
00230         temp += "\tBytes/Pixel:\t" + n2l::asString(mBytesPerPixel) + "\n";
00231         temp += "\t   \t\tMask\t\t\tShift\tLoss\tBpC\n";
00232         for (tUbyte c=Channel_Red; c<Channel_NChannel; ++c) {
00233             switch (c) {
00234                 case Channel_Red:   temp += "\tR: "; break;
00235                 case Channel_Green: temp += "\tG: "; break;
00236                 case Channel_Blue:  temp += "\tB: "; break;
00237                 case Channel_Alpha: temp += "\tA: "; break;
00238                 default: break;
00239             }
00240             temp += n2l::asBinaryMaskString( mCMask[c] ) + "\t";
00241             temp += n2l::asString(mCShift[c]) + "\t";
00242             temp += n2l::asString(mCLoss[c]) + "\t";
00243             temp += n2l::asString(mBitsPerChannel[c]) + "\n";
00244         }
00245         temp += ")";
00246         return temp;
00247     }
00248 
00249 
00250 } // namespace
©2012 Aaron Cameron