KeosPixelUtils.h

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
00019  *
00020  */
00021 
00022 #ifndef KEOS_PIXELUTILS_H
00023 #define KEOS_PIXELUTILS_H
00024 
00025 #include "KeosPrerequisites.h"
00026 #include "KeosLogger.h"
00027 
00028 namespace Keos
00029 {
00030 
00035   inline uint GetBytesPerPixel(TPixelFormat Format)
00036   {
00037     switch (Format)
00038     {
00039       case PXF_L8 :
00040         return 1;
00041       case PXF_A8L8 :
00042         return 2;
00043       case PXF_A1R5G5B5 :
00044         return 2;
00045       case PXF_A4R4G4B4 :
00046         return 2;
00047       case PXF_R8G8B8 :
00048         return 3;
00049       case PXF_A8R8G8B8 :
00050         return 4;
00051       case PXF_DXTC1 :
00052         return 1;
00053       case PXF_DXTC3 :
00054         return 2;
00055       case PXF_DXTC5 :
00056         return 2;
00057       default :
00058         return 0;
00059     }
00060   }
00061 
00066   inline String FormatToString(TPixelFormat Format)
00067   {
00068     switch (Format)
00069     {
00070       case PXF_L8 :
00071         return "PXF_L8";
00072       case PXF_A8L8 :
00073         return "PXF_A8L8";
00074       case PXF_A1R5G5B5 :
00075         return "PXF_A1R5G5B5";
00076       case PXF_A4R4G4B4 :
00077         return "PXF_A4R4G4B4";
00078       case PXF_R8G8B8 :
00079         return "PXF_R8G8B8";
00080       case PXF_A8R8G8B8 :
00081         return "PXF_A8R8G8B8";
00082       case PXF_DXTC1 :
00083         return "PXF_DXTC1";
00084       case PXF_DXTC3 :
00085         return "PXF_DXTC3";
00086       case PXF_DXTC5 :
00087         return "PXF_DXTC5";
00088       default :
00089         return "Unknown format";
00090     }
00091   }
00092 
00097   inline bool FormatCompressed(TPixelFormat Format)
00098   {
00099     switch (Format)
00100     {
00101       case PXF_DXTC1 :
00102       case PXF_DXTC3 :
00103       case PXF_DXTC5 :
00104         return true;
00105 
00106       default :
00107         return false;
00108     }
00109   }
00110 
00116   inline int GetNbMipLevels(int nWidth, int nHeight)
00117   {
00118     int nCount = 0;
00119 
00120     while ((nWidth > 1) || (nHeight > 1))
00121     {
00122       if (nWidth > 1)  nWidth  /= 2;
00123       if (nHeight > 1) nHeight /= 2;
00124       ++nCount;
00125     }
00126 
00127     return nCount;
00128   }
00129 
00134   template <TPixelFormat SrcFmt, TPixelFormat DestFmt>
00135   inline void ConvertPixel(const uchar* pSrc, uchar* pDest)
00136   {
00137     UNSUPPORTED_EXCEPT(String("Software conversion of pixel format (") +
00138                        FormatToString(SrcFmt) +
00139                        " -> " +
00140                        FormatToString(DestFmt) +
00141                        ")", "ConvertPixel");
00142   }
00143 
00144   // Functions of converion for each couple of formats
00145 #include "KeosConvertPixel.inl"
00146 
00147 
00154   inline void ConvertPixel(TPixelFormat SrcFmt, const uchar* pSrc, TPixelFormat DestFmt, uchar* pDest)
00155   {
00156 #define CONVERSIONS_FOR(Fmt) \
00157 case Fmt : \
00158   { \
00159     switch (DestFmt) \
00160     { \
00161       case PXF_L8 :       ConvertPixel<Fmt, PXF_L8>(pSrc, pDest);       break; \
00162       case PXF_A8L8 :     ConvertPixel<Fmt, PXF_A8L8>(pSrc, pDest);     break; \
00163       case PXF_A1R5G5B5 : ConvertPixel<Fmt, PXF_A1R5G5B5>(pSrc, pDest); break; \
00164       case PXF_A4R4G4B4 : ConvertPixel<Fmt, PXF_A4R4G4B4>(pSrc, pDest); break; \
00165       case PXF_R8G8B8 :   ConvertPixel<Fmt, PXF_R8G8B8>(pSrc, pDest);   break; \
00166       case PXF_A8R8G8B8 : ConvertPixel<Fmt, PXF_A8R8G8B8>(pSrc, pDest); break; \
00167       case PXF_DXTC1 :    ConvertPixel<Fmt, PXF_DXTC1>(pSrc, pDest);    break; \
00168       case PXF_DXTC3 :    ConvertPixel<Fmt, PXF_DXTC3>(pSrc, pDest);    break; \
00169       case PXF_DXTC5 :    ConvertPixel<Fmt, PXF_DXTC5>(pSrc, pDest);    break; \
00170     } \
00171     break; \
00172   }
00173 
00174     switch (SrcFmt)
00175     {
00176         CONVERSIONS_FOR(PXF_L8)
00177         CONVERSIONS_FOR(PXF_A8L8)
00178         CONVERSIONS_FOR(PXF_A1R5G5B5)
00179         CONVERSIONS_FOR(PXF_A4R4G4B4)
00180         CONVERSIONS_FOR(PXF_R8G8B8)
00181         CONVERSIONS_FOR(PXF_A8R8G8B8)
00182         CONVERSIONS_FOR(PXF_DXTC1)
00183         CONVERSIONS_FOR(PXF_DXTC3)
00184         CONVERSIONS_FOR(PXF_DXTC5)
00185     }
00186 
00187 #undef CONVERIONS_FOR
00188   }
00189 
00190 } // namespace Keos
00191 
00192 #endif // KEOS_PIXELUTILS_H

Generated on Fri Mar 9 14:29:02 2007 for Keos by  doxygen 1.5.1-p1