# 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-2006 Torus Knot Software Ltd
    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 You may alternatively use this source under the terms of a specific version of
    00025 the OGRE Unrestricted License provided you have obtained such a license from
    00026 Torus Knot Software Ltd.
    00027 -----------------------------------------------------------------------------
    00028 */
    00030 using namespace Ogre;
    00031
    00032 // NB VC6 can't handle these templates
    00033 #if OGRE_COMPILER != OGRE_COMPILER_MSVC || OGRE_COMP_VER >= 1300
    00034
    00035 #define FMTCONVERTERID(from,to) (((from)<<8)|(to))
    00036
    00047 template <class U> struct PixelBoxConverter
    00048 {
    00049     static const int ID = U::ID;
    00050     static void conversion(const PixelBox &src, const PixelBox &dst)
    00051     {
    00052         typename U::SrcType *srcptr = static_cast<typename U::SrcType*>(src.data);
    00053         typename U::DstType *dstptr = static_cast<typename U::DstType*>(dst.data);
    00054         const size_t srcSliceSkip = src.getSliceSkip();
    00055         const size_t dstSliceSkip = dst.getSliceSkip();
    00056         const size_t k = src.right - src.left;
    00057         for(size_t z=src.front; z<src.back; z++)
    00058         {
    00059             for(size_t y=src.top; y<src.bottom; y++)
    00060             {
    00061                 for(size_t x=0; x<k; x++)
    00062                 {
    00063                     dstptr[x] = U::pixelConvert(srcptr[x]);
    00064                 }
    00065                 srcptr += src.rowPitch;
    00066                 dstptr += dst.rowPitch;
    00067             }
    00068             srcptr += srcSliceSkip;
    00069             dstptr += dstSliceSkip;
    00070         }
    00071     }
    00072 };
    00073
    00074 template <typename T, typename U, int id> struct PixelConverter {
    00075     static const int ID = id;
    00076     typedef T SrcType;
    00077     typedef U DstType;
    00078
    00079     //inline static DstType pixelConvert(const SrcType &inp);
    00080 };
    00081
    00082
    00084 struct Col3b {
    00085     Col3b(unsigned int a, unsigned int b, unsigned int c):
    00086         x((Ogre::uint8)a), y((Ogre::uint8)b), z((Ogre::uint8)c) { }
    00087     Ogre::uint8 x,y,z;
    00088 };
    00090 struct Col3f {
    00091     Col3f(float r, float g, float b):
    00092         r(r), g(g), b(b) { }
    00093     float r,g,b;
    00094 };
    00096 struct Col4f {
    00097     Col4f(float r, float g, float b, float a):
    00098         r(r), g(g), b(b), a(a) { }
    00099     float r,g,b,a;
    00100 };
    00101
    00102 struct A8R8G8B8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8R8G8B8, PF_A8B8G8R8)>
    00103 {
    00104     inline static DstType pixelConvert(SrcType inp)
    00105     {
    00106         return ((inp&0x000000FF)<<16)|(inp&0xFF00FF00)|((inp&0x00FF0000)>>16);
    00107     }
    00108 };
    00109
    00110 struct A8R8G8B8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8R8G8B8, PF_B8G8R8A8)>
    00111 {
    00112     inline static DstType pixelConvert(SrcType inp)
    00113     {
    00114         return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
    00115     }
    00116 };
    00117
    00118 struct A8R8G8B8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8R8G8B8, PF_R8G8B8A8)>
    00119 {
    00120     inline static DstType pixelConvert(SrcType inp)
    00121     {
    00122         return ((inp&0x00FFFFFF)<<8)|((inp&0xFF000000)>>24);
    00123     }
    00124 };
    00125
    00126 struct A8B8G8R8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8B8G8R8, PF_A8R8G8B8)>
    00127 {
    00128     inline static DstType pixelConvert(SrcType inp)
    00129     {
    00130         return ((inp&0x000000FF)<<16)|(inp&0xFF00FF00)|((inp&0x00FF0000)>>16);
    00131     }
    00132 };
    00133
    00134 struct A8B8G8R8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8B8G8R8, PF_B8G8R8A8)>
    00135 {
    00136     inline static DstType pixelConvert(SrcType inp)
    00137     {
    00138         return ((inp&0x00FFFFFF)<<8)|((inp&0xFF000000)>>24);
    00139     }
    00140 };
    00141
    00142 struct A8B8G8R8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8B8G8R8, PF_R8G8B8A8)>
    00143 {
    00144     inline static DstType pixelConvert(SrcType inp)
    00145     {
    00146         return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
    00147     }
    00148 };
    00149
    00150 struct B8G8R8A8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_B8G8R8A8, PF_A8R8G8B8)>
    00151 {
    00152     inline static DstType pixelConvert(SrcType inp)
    00153     {
    00154         return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
    00155     }
    00156 };
    00157
    00158 struct B8G8R8A8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_B8G8R8A8, PF_A8B8G8R8)>
    00159 {
    00160     inline static DstType pixelConvert(SrcType inp)
    00161     {
    00162         return ((inp&0x000000FF)<<24)|((inp&0xFFFFFF00)>>8);
    00163     }
    00164 };
    00165
    00166 struct B8G8R8A8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_B8G8R8A8, PF_R8G8B8A8)>
    00167 {
    00168     inline static DstType pixelConvert(SrcType inp)
    00169     {
    00170         return ((inp&0x0000FF00)<<16)|(inp&0x00FF00FF)|((inp&0xFF000000)>>16);
    00171     }
    00172 };
    00173
    00174 struct R8G8B8A8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_R8G8B8A8, PF_A8R8G8B8)>
    00175 {
    00176     inline static DstType pixelConvert(SrcType inp)
    00177     {
    00178         return ((inp&0x000000FF)<<24)|((inp&0xFFFFFF00)>>8);
    00179     }
    00180 };
    00181
    00182 struct R8G8B8A8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_R8G8B8A8, PF_A8B8G8R8)>
    00183 {
    00184     inline static DstType pixelConvert(SrcType inp)
    00185     {
    00186         return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
    00187     }
    00188 };
    00189
    00190 struct R8G8B8A8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_R8G8B8A8, PF_B8G8R8A8)>
    00191 {
    00192     inline static DstType pixelConvert(SrcType inp)
    00193     {
    00194         return ((inp&0x0000FF00)<<16)|(inp&0x00FF00FF)|((inp&0xFF000000)>>16);
    00195     }
    00196 };
    00197
    00198 struct A8B8G8R8toL8: public PixelConverter <Ogre::uint32, Ogre::uint8, FMTCONVERTERID(PF_A8B8G8R8, PF_L8)>
    00199 {
    00200     inline static DstType pixelConvert(SrcType inp)
    00201     {
    00202         return (Ogre::uint8)(inp&0x000000FF);
    00203     }
    00204 };
    00205
    00206 struct L8toA8B8G8R8: public PixelConverter <Ogre::uint8, Ogre::uint32, FMTCONVERTERID(PF_L8, PF_A8B8G8R8)>
    00207 {
    00208     inline static DstType pixelConvert(SrcType inp)
    00209     {
    00210         return 0xFF000000|(((unsigned int)inp)<<0)|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16);
    00211     }
    00212 };
    00213
    00214 struct A8R8G8B8toL8: public PixelConverter <Ogre::uint32, Ogre::uint8, FMTCONVERTERID(PF_A8R8G8B8, PF_L8)>
    00215 {
    00216     inline static DstType pixelConvert(SrcType inp)
    00217     {
    00218         return (Ogre::uint8)((inp&0x00FF0000)>>16);
    00219     }
    00220 };
    00221
    00222 struct L8toA8R8G8B8: public PixelConverter <Ogre::uint8, Ogre::uint32, FMTCONVERTERID(PF_L8, PF_A8R8G8B8)>
    00223 {
    00224     inline static DstType pixelConvert(SrcType inp)
    00225     {
    00226         return 0xFF000000|(((unsigned int)inp)<<0)|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16);
    00227     }
    00228 };
    00229
    00230 struct B8G8R8A8toL8: public PixelConverter <Ogre::uint32, Ogre::uint8, FMTCONVERTERID(PF_B8G8R8A8, PF_L8)>
    00231 {
    00232     inline static DstType pixelConvert(SrcType inp)
    00233     {
    00234         return (Ogre::uint8)((inp&0x0000FF00)>>8);
    00235     }
    00236 };
    00237
    00238 struct L8toB8G8R8A8: public PixelConverter <Ogre::uint8, Ogre::uint32, FMTCONVERTERID(PF_L8, PF_B8G8R8A8)>
    00239 {
    00240     inline static DstType pixelConvert(SrcType inp)
    00241     {
    00242         return 0x000000FF|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16)|(((unsigned int)inp)<<24);
    00243     }
    00244 };
    00245
    00246 struct L8toL16: public PixelConverter <Ogre::uint8, Ogre::uint16, FMTCONVERTERID(PF_L8, PF_L16)>
    00247 {
    00248     inline static DstType pixelConvert(SrcType inp)
    00249     {
    00250         return (Ogre::uint16)((((unsigned int)inp)<<8)|(((unsigned int)inp)));
    00251     }
    00252 };
    00253
    00254 struct L16toL8: public PixelConverter <Ogre::uint16, Ogre::uint8, FMTCONVERTERID(PF_L16, PF_L8)>
    00255 {
    00256     inline static DstType pixelConvert(SrcType inp)
    00257     {
    00258         return (Ogre::uint8)(inp>>8);
    00259     }
    00260 };
    00261
    00262 struct R8G8B8toB8G8R8: public PixelConverter <Col3b, Col3b, FMTCONVERTERID(PF_R8G8B8, PF_B8G8R8)>
    00263 {
    00264     inline static DstType pixelConvert(const SrcType &inp)
    00265     {
    00266         return Col3b(inp.z, inp.y, inp.x);
    00267     }
    00268 };
    00269
    00270 struct B8G8R8toR8G8B8: public PixelConverter <Col3b, Col3b, FMTCONVERTERID(PF_B8G8R8, PF_R8G8B8)>
    00271 {
    00272     inline static DstType pixelConvert(const SrcType &inp)
    00273     {
    00274         return Col3b(inp.z, inp.y, inp.x);
    00275     }
    00276 };
    00277
    00278 // X8Y8Z8 ->  X8<<xshift Y8<<yshift Z8<<zshift A8<<ashift
    00279 template <int id, unsigned int xshift, unsigned int yshift, unsigned int zshift, unsigned int ashift> struct Col3btoUint32swizzler:
    00280     public PixelConverter <Col3b, Ogre::uint32, id>
    00281 {
    00282     inline static Ogre::uint32 pixelConvert(const Col3b &inp)
    00283     {
    00284 #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
    00285         return (0xFF<<ashift) | (((unsigned int)inp.x)<<xshift) | (((unsigned int)inp.y)<<yshift) | (((unsigned int)inp.z)<<zshift);
    00286 #else
    00287         return (0xFF<<ashift) | (((unsigned int)inp.x)<<zshift) | (((unsigned int)inp.y)<<yshift) | (((unsigned int)inp.z)<<xshift);
    00288 #endif
    00289     }
    00290 };
    00291
    00292 struct R8G8B8toA8R8G8B8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_R8G8B8, PF_A8R8G8B8), 16, 8, 0, 24> { };
    00293 struct B8G8R8toA8R8G8B8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_B8G8R8, PF_A8R8G8B8), 0, 8, 16, 24> { };
    00294 struct R8G8B8toA8B8G8R8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_R8G8B8, PF_A8B8G8R8), 0, 8, 16, 24> { };
    00295 struct B8G8R8toA8B8G8R8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_B8G8R8, PF_A8B8G8R8), 16, 8, 0, 24> { };
    00296 struct R8G8B8toB8G8R8A8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_R8G8B8, PF_B8G8R8A8), 8, 16, 24, 0> { };
    00297 struct B8G8R8toB8G8R8A8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_B8G8R8, PF_B8G8R8A8), 24, 16, 8, 0> { };
    00298
    00299 struct A8R8G8B8toR8G8B8: public PixelConverter <Ogre::uint32, Col3b, FMTCONVERTERID(PF_A8R8G8B8, PF_BYTE_RGB)>
    00300 {
    00301     inline static DstType pixelConvert(Ogre::uint32 inp)
    00302     {
    00303         return Col3b((Ogre::uint8)((inp>>16)&0xFF), (Ogre::uint8)((inp>>8)&0xFF), (Ogre::uint8)((inp>>0)&0xFF));
    00304     }
    00305 };
    00306 struct A8R8G8B8toB8G8R8: public PixelConverter <Ogre::uint32, Col3b, FMTCONVERTERID(PF_A8R8G8B8, PF_BYTE_BGR)>
    00307 {
    00308     inline static DstType pixelConvert(Ogre::uint32 inp)
    00309     {
    00310         return Col3b((Ogre::uint8)((inp>>0)&0xFF), (Ogre::uint8)((inp>>8)&0xFF), (Ogre::uint8)((inp>>16)&0xFF));
    00311     }
    00312 };
    00313
    00314 // Only conversions from X8R8G8B8 to formats with alpha need to be defined, the rest is implicitly the same
    00315 // as A8R8G8B8
    00316 struct X8R8G8B8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_A8R8G8B8)>
    00317 {
    00318     inline static DstType pixelConvert(SrcType inp)
    00319     {
    00320         return inp | 0xFF000000;
    00321     }
    00322 };
    00323 struct X8R8G8B8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_A8B8G8R8)>
    00324 {
    00325     inline static DstType pixelConvert(SrcType inp)
    00326     {
    00327         return ((inp&0x0000FF)<<16)|((inp&0xFF0000)>>16)|(inp&0x00FF00)|0xFF000000;
    00328     }
    00329 };
    00330 struct X8R8G8B8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_B8G8R8A8)>
    00331 {
    00332     inline static DstType pixelConvert(SrcType inp)
    00333     {
    00334         return ((inp&0x0000FF)<<24)|((inp&0xFF0000)>>8)|((inp&0x00FF00)<<8)|0x000000FF;
    00335     }
    00336 };
    00337 struct X8R8G8B8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_R8G8B8A8)>
    00338 {
    00339     inline static DstType pixelConvert(SrcType inp)
    00340     {
    00341         return ((inp&0xFFFFFF)<<8)|0x000000FF;
    00342     }
    00343 };
    00344
    00345 // X8B8G8R8
    00346 struct X8B8G8R8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_A8R8G8B8)>
    00347 {
    00348     inline static DstType pixelConvert(SrcType inp)
    00349     {
    00350         return ((inp&0x0000FF)<<16)|((inp&0xFF0000)>>16)|(inp&0x00FF00)|0xFF000000;
    00351     }
    00352 };
    00353 struct X8B8G8R8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_A8B8G8R8)>
    00354 {
    00355     inline static DstType pixelConvert(SrcType inp)
    00356     {
    00357         return inp | 0xFF000000;
    00358     }
    00359 };
    00360 struct X8B8G8R8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_B8G8R8A8)>
    00361 {
    00362     inline static DstType pixelConvert(SrcType inp)
    00363     {
    00364         return ((inp&0xFFFFFF)<<8)|0x000000FF;
    00365     }
    00366 };
    00367 struct X8B8G8R8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_R8G8B8A8)>
    00368 {
    00369     inline static DstType pixelConvert(SrcType inp)
    00370     {
    00371         return ((inp&0x0000FF)<<24)|((inp&0xFF0000)>>8)|((inp&0x00FF00)<<8)|0x000000FF;
    00372     }
    00373 };
    00374
    00375
    00376 #define CASECONVERTER(type) case type::ID : PixelBoxConverter<type>::conversion(src, dst); return 1;
    00377
    00378 inline int doOptimizedConversion(const PixelBox &src, const PixelBox &dst)
    00379 {;
    00380     switch(FMTCONVERTERID(src.format, dst.format))
    00381     {
    00382         // Register converters here
    00383         CASECONVERTER(A8R8G8B8toA8B8G8R8);
    00384         CASECONVERTER(A8R8G8B8toB8G8R8A8);
    00385         CASECONVERTER(A8R8G8B8toR8G8B8A8);
    00386         CASECONVERTER(A8B8G8R8toA8R8G8B8);
    00387         CASECONVERTER(A8B8G8R8toB8G8R8A8);
    00388         CASECONVERTER(A8B8G8R8toR8G8B8A8);
    00389         CASECONVERTER(B8G8R8A8toA8R8G8B8);
    00390         CASECONVERTER(B8G8R8A8toA8B8G8R8);
    00391         CASECONVERTER(B8G8R8A8toR8G8B8A8);
    00392         CASECONVERTER(R8G8B8A8toA8R8G8B8);
    00393         CASECONVERTER(R8G8B8A8toA8B8G8R8);
    00394         CASECONVERTER(R8G8B8A8toB8G8R8A8);
    00395         CASECONVERTER(A8B8G8R8toL8);
    00396         CASECONVERTER(L8toA8B8G8R8);
    00397         CASECONVERTER(A8R8G8B8toL8);
    00398         CASECONVERTER(L8toA8R8G8B8);
    00399         CASECONVERTER(B8G8R8A8toL8);
    00400         CASECONVERTER(L8toB8G8R8A8);
    00401         CASECONVERTER(L8toL16);
    00402         CASECONVERTER(L16toL8);
    00403         CASECONVERTER(B8G8R8toR8G8B8);
    00404         CASECONVERTER(R8G8B8toB8G8R8);
    00405         CASECONVERTER(R8G8B8toA8R8G8B8);
    00406         CASECONVERTER(B8G8R8toA8R8G8B8);
    00407         CASECONVERTER(R8G8B8toA8B8G8R8);
    00408         CASECONVERTER(B8G8R8toA8B8G8R8);
    00409         CASECONVERTER(R8G8B8toB8G8R8A8);
    00410         CASECONVERTER(B8G8R8toB8G8R8A8);
    00411         CASECONVERTER(A8R8G8B8toR8G8B8);
    00412         CASECONVERTER(A8R8G8B8toB8G8R8);
    00413         CASECONVERTER(X8R8G8B8toA8R8G8B8);
    00414         CASECONVERTER(X8R8G8B8toA8B8G8R8);
    00415         CASECONVERTER(X8R8G8B8toB8G8R8A8);
    00416         CASECONVERTER(X8R8G8B8toR8G8B8A8);
    00417         CASECONVERTER(X8B8G8R8toA8R8G8B8);
    00418         CASECONVERTER(X8B8G8R8toA8B8G8R8);
    00419         CASECONVERTER(X8B8G8R8toB8G8R8A8);
    00420         CASECONVERTER(X8B8G8R8toR8G8B8A8);
    00421
    00422         default:
    00423             return 0;
    00424     }
    00425 }
    00426 #undef CASECONVERTER
    00427
    00428 #endif // VC6 protection

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sat May 10 16:25:01 2008