Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

OgrePixelConversions.h

Go to the documentation of this file.
00001  /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2005 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00026 using namespace Ogre;
00027 
00028 // NB VC6 can't handle these templates
00029 #if OGRE_COMPILER != OGRE_COMPILER_MSVC || OGRE_COMP_VER >= 1300
00030 
00031 #define FMTCONVERTERID(from,to) (((from)<<8)|(to))
00032 
00043 template <class U> struct PixelBoxConverter 
00044 {
00045     static const int ID = U::ID;
00046     static void conversion(const PixelBox &src, const PixelBox &dst)
00047     {
00048         typename U::SrcType *srcptr = static_cast<typename U::SrcType*>(src.data);
00049         typename U::DstType *dstptr = static_cast<typename U::DstType*>(dst.data);
00050         const size_t srcSliceSkip = src.getSliceSkip();
00051         const size_t dstSliceSkip = dst.getSliceSkip();
00052         const size_t k = src.right - src.left;
00053         for(size_t z=src.front; z<src.back; z++) 
00054         {
00055             for(size_t y=src.top; y<src.bottom; y++)
00056             {
00057                 for(size_t x=0; x<k; x++)
00058                 {
00059                     dstptr[x] = U::pixelConvert(srcptr[x]);
00060                 }
00061                 srcptr += src.rowPitch;
00062                 dstptr += dst.rowPitch;
00063             }
00064             srcptr += srcSliceSkip;
00065             dstptr += dstSliceSkip;
00066         }    
00067     }
00068 };
00069 
00070 template <typename T, typename U, int id> struct PixelConverter {
00071     static const int ID = id;
00072     typedef T SrcType;
00073     typedef U DstType;    
00074     
00075     //inline static DstType pixelConvert(const SrcType &inp);
00076 };
00077 
00078 
00080 struct Col3b {
00081     Col3b(unsigned int a, unsigned int b, unsigned int c): 
00082         x((uint8)a), y((uint8)b), z((uint8)c) { }
00083     uint8 x,y,z;
00084 };
00086 struct Col3f {
00087     Col3f(float r, float g, float b):
00088         r(r), g(g), b(b) { }
00089     float r,g,b;
00090 };
00092 struct Col4f {
00093     Col4f(float r, float g, float b, float a):
00094         r(r), g(g), b(b), a(a) { }
00095     float r,g,b,a;
00096 };
00097 
00098 struct A8R8G8B8toA8B8G8R8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_A8R8G8B8, PF_A8B8G8R8)>
00099 {
00100     inline static DstType pixelConvert(SrcType inp)
00101     {
00102         return ((inp&0x000000FF)<<16)|(inp&0xFF00FF00)|((inp&0x00FF0000)>>16);
00103     }
00104 };
00105 
00106 struct A8R8G8B8toB8G8R8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_A8R8G8B8, PF_B8G8R8A8)>
00107 {
00108     inline static DstType pixelConvert(SrcType inp)
00109     {
00110         return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
00111     }
00112 };
00113 
00114 struct A8R8G8B8toR8G8B8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_A8R8G8B8, PF_R8G8B8A8)>
00115 {
00116     inline static DstType pixelConvert(SrcType inp)
00117     {
00118         return ((inp&0x00FFFFFF)<<8)|((inp&0xFF000000)>>24);
00119     }
00120 };
00121 
00122 struct A8B8G8R8toA8R8G8B8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_A8B8G8R8, PF_A8R8G8B8)>
00123 {
00124     inline static DstType pixelConvert(SrcType inp)
00125     {
00126         return ((inp&0x000000FF)<<16)|(inp&0xFF00FF00)|((inp&0x00FF0000)>>16);
00127     }
00128 };
00129 
00130 struct A8B8G8R8toB8G8R8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_A8B8G8R8, PF_B8G8R8A8)>
00131 {
00132     inline static DstType pixelConvert(SrcType inp)
00133     {
00134         return ((inp&0x00FFFFFF)<<8)|((inp&0xFF000000)>>24);
00135     }
00136 };
00137 
00138 struct A8B8G8R8toR8G8B8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_A8B8G8R8, PF_R8G8B8A8)>
00139 {
00140     inline static DstType pixelConvert(SrcType inp)
00141     {
00142         return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
00143     }
00144 };
00145 
00146 struct B8G8R8A8toA8R8G8B8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_B8G8R8A8, PF_A8R8G8B8)>
00147 {
00148     inline static DstType pixelConvert(SrcType inp)
00149     {
00150         return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
00151     }
00152 };
00153 
00154 struct B8G8R8A8toA8B8G8R8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_B8G8R8A8, PF_A8B8G8R8)>
00155 {
00156     inline static DstType pixelConvert(SrcType inp)
00157     {
00158         return ((inp&0x000000FF)<<24)|((inp&0xFFFFFF00)>>8);
00159     }
00160 };
00161 
00162 struct B8G8R8A8toR8G8B8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_B8G8R8A8, PF_R8G8B8A8)>
00163 {
00164     inline static DstType pixelConvert(SrcType inp)
00165     {
00166         return ((inp&0x0000FF00)<<16)|(inp&0x00FF00FF)|((inp&0xFF000000)>>16);
00167     }
00168 };
00169 
00170 struct R8G8B8A8toA8R8G8B8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_R8G8B8A8, PF_A8R8G8B8)>
00171 {
00172     inline static DstType pixelConvert(SrcType inp)
00173     {
00174         return ((inp&0x000000FF)<<24)|((inp&0xFFFFFF00)>>8);
00175     }
00176 };
00177 
00178 struct R8G8B8A8toA8B8G8R8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_R8G8B8A8, PF_A8B8G8R8)>
00179 {
00180     inline static DstType pixelConvert(SrcType inp)
00181     {
00182         return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
00183     }
00184 };
00185 
00186 struct R8G8B8A8toB8G8R8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_R8G8B8A8, PF_B8G8R8A8)>
00187 {
00188     inline static DstType pixelConvert(SrcType inp)
00189     {
00190         return ((inp&0x0000FF00)<<16)|(inp&0x00FF00FF)|((inp&0xFF000000)>>16);
00191     }
00192 };
00193 
00194 struct A8B8G8R8toL8: public PixelConverter <uint32, uint8, FMTCONVERTERID(PF_A8B8G8R8, PF_L8)>
00195 {
00196     inline static DstType pixelConvert(SrcType inp)
00197     {
00198         return (uint8)(inp&0x000000FF);
00199     }
00200 };
00201 
00202 struct L8toA8B8G8R8: public PixelConverter <uint8, uint32, FMTCONVERTERID(PF_L8, PF_A8B8G8R8)>
00203 {
00204     inline static DstType pixelConvert(SrcType inp)
00205     {
00206         return 0xFF000000|(((unsigned int)inp)<<0)|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16);
00207     }
00208 };
00209 
00210 struct A8R8G8B8toL8: public PixelConverter <uint32, uint8, FMTCONVERTERID(PF_A8R8G8B8, PF_L8)>
00211 {
00212     inline static DstType pixelConvert(SrcType inp)
00213     {
00214         return (uint8)((inp&0x00FF0000)>>16);
00215     }
00216 };
00217 
00218 struct L8toA8R8G8B8: public PixelConverter <uint8, uint32, FMTCONVERTERID(PF_L8, PF_A8R8G8B8)>
00219 {
00220     inline static DstType pixelConvert(SrcType inp)
00221     {
00222         return 0xFF000000|(((unsigned int)inp)<<0)|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16);
00223     }
00224 };
00225 
00226 struct B8G8R8A8toL8: public PixelConverter <uint32, uint8, FMTCONVERTERID(PF_B8G8R8A8, PF_L8)>
00227 {
00228     inline static DstType pixelConvert(SrcType inp)
00229     {
00230         return (uint8)((inp&0x0000FF00)>>8);
00231     }
00232 };
00233 
00234 struct L8toB8G8R8A8: public PixelConverter <uint8, uint32, FMTCONVERTERID(PF_L8, PF_B8G8R8A8)>
00235 {
00236     inline static DstType pixelConvert(SrcType inp)
00237     {
00238         return 0x000000FF|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16)|(((unsigned int)inp)<<24);
00239     }
00240 };
00241 
00242 struct L8toL16: public PixelConverter <uint8, uint16, FMTCONVERTERID(PF_L8, PF_L16)>
00243 {
00244     inline static DstType pixelConvert(SrcType inp)
00245     {
00246         return (uint16)((((unsigned int)inp)<<8)|(((unsigned int)inp)));
00247     }
00248 };
00249 
00250 struct L16toL8: public PixelConverter <uint16, uint8, FMTCONVERTERID(PF_L16, PF_L8)>
00251 {
00252     inline static DstType pixelConvert(SrcType inp)
00253     {
00254         return (uint8)(inp>>8);
00255     }
00256 };
00257 
00258 struct R8G8B8toB8G8R8: public PixelConverter <Col3b, Col3b, FMTCONVERTERID(PF_R8G8B8, PF_B8G8R8)>
00259 {
00260     inline static DstType pixelConvert(const SrcType &inp)
00261     {
00262         return Col3b(inp.z, inp.y, inp.x);
00263     }  
00264 };
00265 
00266 struct B8G8R8toR8G8B8: public PixelConverter <Col3b, Col3b, FMTCONVERTERID(PF_B8G8R8, PF_R8G8B8)>
00267 {
00268     inline static DstType pixelConvert(const SrcType &inp)
00269     {
00270         return Col3b(inp.z, inp.y, inp.x);
00271     }  
00272 };
00273 
00274 // X8Y8Z8 ->  X8<<xshift Y8<<yshift Z8<<zshift A8<<ashift
00275 template <int id, unsigned int xshift, unsigned int yshift, unsigned int zshift, unsigned int ashift> struct Col3btoUint32swizzler:
00276     public PixelConverter <Col3b, uint32, id>
00277 {
00278     inline static uint32 pixelConvert(const Col3b &inp)
00279     {
00280 #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
00281         return (0xFF<<ashift) | (((unsigned int)inp.x)<<xshift) | (((unsigned int)inp.y)<<yshift) | (((unsigned int)inp.z)<<zshift);
00282 #else
00283         return (0xFF<<ashift) | (((unsigned int)inp.x)<<zshift) | (((unsigned int)inp.y)<<yshift) | (((unsigned int)inp.z)<<xshift);
00284 #endif
00285     }
00286 };
00287 
00288 struct R8G8B8toA8R8G8B8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_R8G8B8, PF_A8R8G8B8), 16, 8, 0, 24> { };
00289 struct B8G8R8toA8R8G8B8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_B8G8R8, PF_A8R8G8B8), 0, 8, 16, 24> { };
00290 struct R8G8B8toA8B8G8R8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_R8G8B8, PF_A8B8G8R8), 0, 8, 16, 24> { };
00291 struct B8G8R8toA8B8G8R8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_B8G8R8, PF_A8B8G8R8), 16, 8, 0, 24> { };
00292 struct R8G8B8toB8G8R8A8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_R8G8B8, PF_B8G8R8A8), 8, 16, 24, 0> { };
00293 struct B8G8R8toB8G8R8A8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_B8G8R8, PF_B8G8R8A8), 24, 16, 8, 0> { };
00294 
00295 struct A8R8G8B8toR8G8B8: public PixelConverter <uint32, Col3b, FMTCONVERTERID(PF_A8R8G8B8, PF_BYTE_RGB)>
00296 {
00297     inline static DstType pixelConvert(uint32 inp)
00298     {
00299         return Col3b((uint8)((inp>>16)&0xFF), (uint8)((inp>>8)&0xFF), (uint8)((inp>>0)&0xFF));
00300     }
00301 };
00302 struct A8R8G8B8toB8G8R8: public PixelConverter <uint32, Col3b, FMTCONVERTERID(PF_A8R8G8B8, PF_BYTE_BGR)>
00303 {
00304     inline static DstType pixelConvert(uint32 inp)
00305     {
00306         return Col3b((uint8)((inp>>0)&0xFF), (uint8)((inp>>8)&0xFF), (uint8)((inp>>16)&0xFF));
00307     }
00308 };
00309 
00310 // Only conversions from X8R8G8B8 to formats with alpha need to be defined, the rest is implicitly the same
00311 // as A8R8G8B8
00312 struct X8R8G8B8toA8R8G8B8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_A8R8G8B8)>
00313 {
00314     inline static DstType pixelConvert(SrcType inp)
00315     {
00316         return inp | 0xFF000000;
00317     }
00318 };
00319 struct X8R8G8B8toA8B8G8R8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_A8B8G8R8)>
00320 {
00321     inline static DstType pixelConvert(SrcType inp)
00322     {
00323         return ((inp&0x0000FF)<<16)|((inp&0xFF0000)>>16)|(inp&0x00FF00)|0xFF000000;
00324     }
00325 };
00326 struct X8R8G8B8toB8G8R8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_B8G8R8A8)>
00327 {
00328     inline static DstType pixelConvert(SrcType inp)
00329     {
00330         return ((inp&0x0000FF)<<24)|((inp&0xFF0000)>>8)|((inp&0x00FF00)<<8)|0x000000FF;
00331     }
00332 };
00333 struct X8R8G8B8toR8G8B8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_R8G8B8A8)>
00334 {
00335     inline static DstType pixelConvert(SrcType inp)
00336     {
00337         return ((inp&0xFFFFFF)<<8)|0x000000FF;
00338     }
00339 };
00340 
00341 // X8B8G8R8
00342 struct X8B8G8R8toA8R8G8B8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_A8R8G8B8)>
00343 {
00344     inline static DstType pixelConvert(SrcType inp)
00345     {
00346         return ((inp&0x0000FF)<<16)|((inp&0xFF0000)>>16)|(inp&0x00FF00)|0xFF000000;
00347     }
00348 };
00349 struct X8B8G8R8toA8B8G8R8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_A8B8G8R8)>
00350 {
00351     inline static DstType pixelConvert(SrcType inp)
00352     {
00353         return inp | 0xFF000000;
00354     }
00355 };
00356 struct X8B8G8R8toB8G8R8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_B8G8R8A8)>
00357 {
00358     inline static DstType pixelConvert(SrcType inp)
00359     {
00360         return ((inp&0xFFFFFF)<<8)|0x000000FF;
00361     }
00362 };
00363 struct X8B8G8R8toR8G8B8A8: public PixelConverter <uint32, uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_R8G8B8A8)>
00364 {
00365     inline static DstType pixelConvert(SrcType inp)
00366     {
00367         return ((inp&0x0000FF)<<24)|((inp&0xFF0000)>>8)|((inp&0x00FF00)<<8)|0x000000FF;
00368     }
00369 };
00370 
00371 
00372 #define CASECONVERTER(type) case type::ID : PixelBoxConverter<type>::conversion(src, dst); return 1;
00373 
00374 inline int doOptimizedConversion(const PixelBox &src, const PixelBox &dst)
00375 {;
00376     switch(FMTCONVERTERID(src.format, dst.format))
00377     {
00378         // Register converters here
00379         CASECONVERTER(A8R8G8B8toA8B8G8R8);
00380         CASECONVERTER(A8R8G8B8toB8G8R8A8);
00381         CASECONVERTER(A8R8G8B8toR8G8B8A8);
00382         CASECONVERTER(A8B8G8R8toA8R8G8B8);
00383         CASECONVERTER(A8B8G8R8toB8G8R8A8);
00384         CASECONVERTER(A8B8G8R8toR8G8B8A8);
00385         CASECONVERTER(B8G8R8A8toA8R8G8B8);
00386         CASECONVERTER(B8G8R8A8toA8B8G8R8);
00387         CASECONVERTER(B8G8R8A8toR8G8B8A8);
00388         CASECONVERTER(R8G8B8A8toA8R8G8B8);
00389         CASECONVERTER(R8G8B8A8toA8B8G8R8);
00390         CASECONVERTER(R8G8B8A8toB8G8R8A8);
00391         CASECONVERTER(A8B8G8R8toL8);
00392         CASECONVERTER(L8toA8B8G8R8);
00393         CASECONVERTER(A8R8G8B8toL8);
00394         CASECONVERTER(L8toA8R8G8B8);
00395         CASECONVERTER(B8G8R8A8toL8);
00396         CASECONVERTER(L8toB8G8R8A8);
00397         CASECONVERTER(L8toL16);
00398         CASECONVERTER(L16toL8);
00399         CASECONVERTER(B8G8R8toR8G8B8);
00400         CASECONVERTER(R8G8B8toB8G8R8);
00401         CASECONVERTER(R8G8B8toA8R8G8B8);
00402         CASECONVERTER(B8G8R8toA8R8G8B8);
00403         CASECONVERTER(R8G8B8toA8B8G8R8);
00404         CASECONVERTER(B8G8R8toA8B8G8R8);
00405         CASECONVERTER(R8G8B8toB8G8R8A8);
00406         CASECONVERTER(B8G8R8toB8G8R8A8);
00407         CASECONVERTER(A8R8G8B8toR8G8B8);
00408         CASECONVERTER(A8R8G8B8toB8G8R8);
00409         CASECONVERTER(X8R8G8B8toA8R8G8B8);
00410         CASECONVERTER(X8R8G8B8toA8B8G8R8);
00411         CASECONVERTER(X8R8G8B8toB8G8R8A8);
00412         CASECONVERTER(X8R8G8B8toR8G8B8A8);
00413         CASECONVERTER(X8B8G8R8toA8R8G8B8);
00414         CASECONVERTER(X8B8G8R8toA8B8G8R8);
00415         CASECONVERTER(X8B8G8R8toB8G8R8A8);
00416         CASECONVERTER(X8B8G8R8toR8G8B8A8);
00417 
00418         default:
00419             return 0;
00420     }
00421 }
00422 #undef CASECONVERTER
00423 
00424 #endif // VC6 protection

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Mar 12 14:37:46 2006