Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# 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 :       return 1;
    00040             case PXF_A8L8 :     return 2;
    00041             case PXF_A1R5G5B5 : return 2;
    00042             case PXF_A4R4G4B4 : return 2;
    00043             case PXF_R8G8B8 :   return 3;
    00044             case PXF_A8R8G8B8 : return 4;
    00045             case PXF_DXTC1 :    return 1;
    00046             case PXF_DXTC3 :    return 2;
    00047             case PXF_DXTC5 :    return 2;
    00048             default :           return 0;
    00049         }
    00050     }
    00051
    00056     inline String FormatToString(TPixelFormat Format)
    00057     {
    00058         switch (Format)
    00059         {
    00060             case PXF_L8 :       return "PXF_L8";
    00061             case PXF_A8L8 :     return "PXF_A8L8";
    00062             case PXF_A1R5G5B5 : return "PXF_A1R5G5B5";
    00063             case PXF_A4R4G4B4 : return "PXF_A4R4G4B4";
    00064             case PXF_R8G8B8 :   return "PXF_R8G8B8";
    00065             case PXF_A8R8G8B8 : return "PXF_A8R8G8B8";
    00066             case PXF_DXTC1 :    return "PXF_DXTC1";
    00067             case PXF_DXTC3 :    return "PXF_DXTC3";
    00068             case PXF_DXTC5 :    return "PXF_DXTC5";
    00069             default :           return "Unknown format";
    00070         }
    00071     }
    00072
    00077     inline bool FormatCompressed(TPixelFormat Format)
    00078     {
    00079         switch (Format)
    00080         {
    00081             case PXF_DXTC1 :
    00082             case PXF_DXTC3 :
    00083             case PXF_DXTC5 :
    00084                 return true;
    00085
    00086             default :
    00087                 return false;
    00088         }
    00089     }
    00090
    00096     inline int GetNbMipLevels(int nWidth, int nHeight)
    00097     {
    00098         int nCount = 0;
    00099
    00100         while ((nWidth > 1) || (nHeight > 1))
    00101         {
    00102             if (nWidth > 1)  nWidth  /= 2;
    00103             if (nHeight > 1) nHeight /= 2;
    00104             ++nCount;
    00105         }
    00106
    00107         return nCount;
    00108     }
    00109
    00114     template <TPixelFormat SrcFmt, TPixelFormat DestFmt>
    00115     inline void ConvertPixel(const uchar* pSrc, uchar* pDest)
    00116     {
    00117         UNSUPPORTED_EXCEPT(String("Software conversion of pixel format (") +
    00118                            FormatToString(SrcFmt) +
    00119                            " -> " +
    00120                            FormatToString(DestFmt) +
    00121                            ")", "ConvertPixel");
    00122     }
    00123
    00124     // Functions of converion for each couple of formats
    00125     #include "KeosConvertPixel.inl"
    00126
    00127
    00134     inline void ConvertPixel(TPixelFormat SrcFmt, const uchar* pSrc, TPixelFormat DestFmt, uchar* pDest)
    00135     {
    00136         #define CONVERSIONS_FOR(Fmt) \
    00137             case Fmt : \
    00138             { \
    00139                 switch (DestFmt) \
    00140                 { \
    00141                     case PXF_L8 :       ConvertPixel<Fmt, PXF_L8>(pSrc, pDest);       break; \
    00142                     case PXF_A8L8 :     ConvertPixel<Fmt, PXF_A8L8>(pSrc, pDest);     break; \
    00143                     case PXF_A1R5G5B5 : ConvertPixel<Fmt, PXF_A1R5G5B5>(pSrc, pDest); break; \
    00144                     case PXF_A4R4G4B4 : ConvertPixel<Fmt, PXF_A4R4G4B4>(pSrc, pDest); break; \
    00145                     case PXF_R8G8B8 :   ConvertPixel<Fmt, PXF_R8G8B8>(pSrc, pDest);   break; \
    00146                     case PXF_A8R8G8B8 : ConvertPixel<Fmt, PXF_A8R8G8B8>(pSrc, pDest); break; \
    00147                     case PXF_DXTC1 :    ConvertPixel<Fmt, PXF_DXTC1>(pSrc, pDest);    break; \
    00148                     case PXF_DXTC3 :    ConvertPixel<Fmt, PXF_DXTC3>(pSrc, pDest);    break; \
    00149                     case PXF_DXTC5 :    ConvertPixel<Fmt, PXF_DXTC5>(pSrc, pDest);    break; \
    00150                 } \
    00151                 break; \
    00152             }
    00153
    00154         switch (SrcFmt)
    00155         {
    00156             CONVERSIONS_FOR(PXF_L8)
    00157             CONVERSIONS_FOR(PXF_A8L8)
    00158             CONVERSIONS_FOR(PXF_A1R5G5B5)
    00159             CONVERSIONS_FOR(PXF_A4R4G4B4)
    00160             CONVERSIONS_FOR(PXF_R8G8B8)
    00161             CONVERSIONS_FOR(PXF_A8R8G8B8)
    00162             CONVERSIONS_FOR(PXF_DXTC1)
    00163             CONVERSIONS_FOR(PXF_DXTC3)
    00164             CONVERSIONS_FOR(PXF_DXTC5)
    00165         }
    00166
    00167         #undef CONVERIONS_FOR
    00168     }
    00169
    00170 } // namespace Keos
    00171
    00172 #endif // KEOS_PIXELUTILS_H