OGRE  1.7
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OgrePixelConversions.h
Go to the documentation of this file.
1  /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4  (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2011 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
29 using namespace Ogre;
30 
31 // NB VC6 can't handle these templates
32 #if OGRE_COMPILER != OGRE_COMPILER_MSVC || OGRE_COMP_VER >= 1300
33 
34 #define FMTCONVERTERID(from,to) (((from)<<8)|(to))
35 
52 template <class U> struct PixelBoxConverter
53 {
54  static const int ID = U::ID;
55  static void conversion(const PixelBox &src, const PixelBox &dst)
56  {
57  typename U::SrcType *srcptr = static_cast<typename U::SrcType*>(src.data)
58  + (src.left + src.top * src.rowPitch + src.front * src.slicePitch);
59  typename U::DstType *dstptr = static_cast<typename U::DstType*>(dst.data)
60  + (dst.left + dst.top * dst.rowPitch + dst.front * dst.slicePitch);
61  const size_t srcSliceSkip = src.getSliceSkip();
62  const size_t dstSliceSkip = dst.getSliceSkip();
63  const size_t k = src.right - src.left;
64  for(size_t z=src.front; z<src.back; z++)
65  {
66  for(size_t y=src.top; y<src.bottom; y++)
67  {
68  for(size_t x=0; x<k; x++)
69  {
70  dstptr[x] = U::pixelConvert(srcptr[x]);
71  }
72  srcptr += src.rowPitch;
73  dstptr += dst.rowPitch;
74  }
75  srcptr += srcSliceSkip;
76  dstptr += dstSliceSkip;
77  }
78  }
79 };
80 
81 template <typename T, typename U, int id> struct PixelConverter {
82  static const int ID = id;
83  typedef T SrcType;
84  typedef U DstType;
85 
86  //inline static DstType pixelConvert(const SrcType &inp);
87 };
88 
89 
91 struct Col3b {
92  Col3b(unsigned int a, unsigned int b, unsigned int c):
93  x((Ogre::uint8)a), y((Ogre::uint8)b), z((Ogre::uint8)c) { }
94  Ogre::uint8 x,y,z;
95 };
97 struct Col3f {
98  Col3f(float inR, float inG, float inB):
99  r(inR), g(inG), b(inB) { }
100  float r,g,b;
101 };
103 struct Col4f {
104  Col4f(float inR, float inG, float inB, float inA):
105  r(inR), g(inG), b(inB), a(inA) { }
106  float r,g,b,a;
107 };
108 
109 struct A8R8G8B8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8R8G8B8, PF_A8B8G8R8)>
110 {
111  inline static DstType pixelConvert(SrcType inp)
112  {
113  return ((inp&0x000000FF)<<16)|(inp&0xFF00FF00)|((inp&0x00FF0000)>>16);
114  }
115 };
116 
117 struct A8R8G8B8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8R8G8B8, PF_B8G8R8A8)>
118 {
119  inline static DstType pixelConvert(SrcType inp)
120  {
121  return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
122  }
123 };
124 
125 struct A8R8G8B8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8R8G8B8, PF_R8G8B8A8)>
126 {
127  inline static DstType pixelConvert(SrcType inp)
128  {
129  return ((inp&0x00FFFFFF)<<8)|((inp&0xFF000000)>>24);
130  }
131 };
132 
133 struct A8B8G8R8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8B8G8R8, PF_A8R8G8B8)>
134 {
135  inline static DstType pixelConvert(SrcType inp)
136  {
137  return ((inp&0x000000FF)<<16)|(inp&0xFF00FF00)|((inp&0x00FF0000)>>16);
138  }
139 };
140 
141 struct A8B8G8R8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8B8G8R8, PF_B8G8R8A8)>
142 {
143  inline static DstType pixelConvert(SrcType inp)
144  {
145  return ((inp&0x00FFFFFF)<<8)|((inp&0xFF000000)>>24);
146  }
147 };
148 
149 struct A8B8G8R8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_A8B8G8R8, PF_R8G8B8A8)>
150 {
151  inline static DstType pixelConvert(SrcType inp)
152  {
153  return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
154  }
155 };
156 
157 struct B8G8R8A8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_B8G8R8A8, PF_A8R8G8B8)>
158 {
159  inline static DstType pixelConvert(SrcType inp)
160  {
161  return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
162  }
163 };
164 
165 struct B8G8R8A8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_B8G8R8A8, PF_A8B8G8R8)>
166 {
167  inline static DstType pixelConvert(SrcType inp)
168  {
169  return ((inp&0x000000FF)<<24)|((inp&0xFFFFFF00)>>8);
170  }
171 };
172 
173 struct B8G8R8A8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_B8G8R8A8, PF_R8G8B8A8)>
174 {
175  inline static DstType pixelConvert(SrcType inp)
176  {
177  return ((inp&0x0000FF00)<<16)|(inp&0x00FF00FF)|((inp&0xFF000000)>>16);
178  }
179 };
180 
181 struct R8G8B8A8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_R8G8B8A8, PF_A8R8G8B8)>
182 {
183  inline static DstType pixelConvert(SrcType inp)
184  {
185  return ((inp&0x000000FF)<<24)|((inp&0xFFFFFF00)>>8);
186  }
187 };
188 
189 struct R8G8B8A8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_R8G8B8A8, PF_A8B8G8R8)>
190 {
191  inline static DstType pixelConvert(SrcType inp)
192  {
193  return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
194  }
195 };
196 
197 struct R8G8B8A8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_R8G8B8A8, PF_B8G8R8A8)>
198 {
199  inline static DstType pixelConvert(SrcType inp)
200  {
201  return ((inp&0x0000FF00)<<16)|(inp&0x00FF00FF)|((inp&0xFF000000)>>16);
202  }
203 };
204 
205 struct A8B8G8R8toL8: public PixelConverter <Ogre::uint32, Ogre::uint8, FMTCONVERTERID(PF_A8B8G8R8, PF_L8)>
206 {
207  inline static DstType pixelConvert(SrcType inp)
208  {
209  return (Ogre::uint8)(inp&0x000000FF);
210  }
211 };
212 
213 struct L8toA8B8G8R8: public PixelConverter <Ogre::uint8, Ogre::uint32, FMTCONVERTERID(PF_L8, PF_A8B8G8R8)>
214 {
215  inline static DstType pixelConvert(SrcType inp)
216  {
217  return 0xFF000000|(((unsigned int)inp)<<0)|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16);
218  }
219 };
220 
221 struct A8R8G8B8toL8: public PixelConverter <Ogre::uint32, Ogre::uint8, FMTCONVERTERID(PF_A8R8G8B8, PF_L8)>
222 {
223  inline static DstType pixelConvert(SrcType inp)
224  {
225  return (Ogre::uint8)((inp&0x00FF0000)>>16);
226  }
227 };
228 
229 struct L8toA8R8G8B8: public PixelConverter <Ogre::uint8, Ogre::uint32, FMTCONVERTERID(PF_L8, PF_A8R8G8B8)>
230 {
231  inline static DstType pixelConvert(SrcType inp)
232  {
233  return 0xFF000000|(((unsigned int)inp)<<0)|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16);
234  }
235 };
236 
237 struct B8G8R8A8toL8: public PixelConverter <Ogre::uint32, Ogre::uint8, FMTCONVERTERID(PF_B8G8R8A8, PF_L8)>
238 {
239  inline static DstType pixelConvert(SrcType inp)
240  {
241  return (Ogre::uint8)((inp&0x0000FF00)>>8);
242  }
243 };
244 
245 struct L8toB8G8R8A8: public PixelConverter <Ogre::uint8, Ogre::uint32, FMTCONVERTERID(PF_L8, PF_B8G8R8A8)>
246 {
247  inline static DstType pixelConvert(SrcType inp)
248  {
249  return 0x000000FF|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16)|(((unsigned int)inp)<<24);
250  }
251 };
252 
253 struct L8toL16: public PixelConverter <Ogre::uint8, Ogre::uint16, FMTCONVERTERID(PF_L8, PF_L16)>
254 {
255  inline static DstType pixelConvert(SrcType inp)
256  {
257  return (Ogre::uint16)((((unsigned int)inp)<<8)|(((unsigned int)inp)));
258  }
259 };
260 
261 struct L16toL8: public PixelConverter <Ogre::uint16, Ogre::uint8, FMTCONVERTERID(PF_L16, PF_L8)>
262 {
263  inline static DstType pixelConvert(SrcType inp)
264  {
265  return (Ogre::uint8)(inp>>8);
266  }
267 };
268 
269 struct R8G8B8toB8G8R8: public PixelConverter <Col3b, Col3b, FMTCONVERTERID(PF_R8G8B8, PF_B8G8R8)>
270 {
271  inline static DstType pixelConvert(const SrcType &inp)
272  {
273  return Col3b(inp.z, inp.y, inp.x);
274  }
275 };
276 
277 struct B8G8R8toR8G8B8: public PixelConverter <Col3b, Col3b, FMTCONVERTERID(PF_B8G8R8, PF_R8G8B8)>
278 {
279  inline static DstType pixelConvert(const SrcType &inp)
280  {
281  return Col3b(inp.z, inp.y, inp.x);
282  }
283 };
284 
285 // X8Y8Z8 -> X8<<xshift Y8<<yshift Z8<<zshift A8<<ashift
286 template <int id, unsigned int xshift, unsigned int yshift, unsigned int zshift, unsigned int ashift> struct Col3btoUint32swizzler:
287  public PixelConverter <Col3b, Ogre::uint32, id>
288 {
289  inline static Ogre::uint32 pixelConvert(const Col3b &inp)
290  {
291 #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
292  return (0xFF<<ashift) | (((unsigned int)inp.x)<<xshift) | (((unsigned int)inp.y)<<yshift) | (((unsigned int)inp.z)<<zshift);
293 #else
294  return (0xFF<<ashift) | (((unsigned int)inp.x)<<zshift) | (((unsigned int)inp.y)<<yshift) | (((unsigned int)inp.z)<<xshift);
295 #endif
296  }
297 };
298 
299 struct R8G8B8toA8R8G8B8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_R8G8B8, PF_A8R8G8B8), 16, 8, 0, 24> { };
300 struct B8G8R8toA8R8G8B8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_B8G8R8, PF_A8R8G8B8), 0, 8, 16, 24> { };
301 struct R8G8B8toA8B8G8R8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_R8G8B8, PF_A8B8G8R8), 0, 8, 16, 24> { };
302 struct B8G8R8toA8B8G8R8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_B8G8R8, PF_A8B8G8R8), 16, 8, 0, 24> { };
303 struct R8G8B8toB8G8R8A8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_R8G8B8, PF_B8G8R8A8), 8, 16, 24, 0> { };
304 struct B8G8R8toB8G8R8A8: public Col3btoUint32swizzler<FMTCONVERTERID(PF_B8G8R8, PF_B8G8R8A8), 24, 16, 8, 0> { };
305 
306 struct A8R8G8B8toR8G8B8: public PixelConverter <Ogre::uint32, Col3b, FMTCONVERTERID(PF_A8R8G8B8, PF_BYTE_RGB)>
307 {
308  inline static DstType pixelConvert(Ogre::uint32 inp)
309  {
310  return Col3b((Ogre::uint8)((inp>>16)&0xFF), (Ogre::uint8)((inp>>8)&0xFF), (Ogre::uint8)((inp>>0)&0xFF));
311  }
312 };
313 struct A8R8G8B8toB8G8R8: public PixelConverter <Ogre::uint32, Col3b, FMTCONVERTERID(PF_A8R8G8B8, PF_BYTE_BGR)>
314 {
315  inline static DstType pixelConvert(Ogre::uint32 inp)
316  {
317  return Col3b((Ogre::uint8)((inp>>0)&0xFF), (Ogre::uint8)((inp>>8)&0xFF), (Ogre::uint8)((inp>>16)&0xFF));
318  }
319 };
320 
321 // Only conversions from X8R8G8B8 to formats with alpha need to be defined, the rest is implicitly the same
322 // as A8R8G8B8
323 struct X8R8G8B8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_A8R8G8B8)>
324 {
325  inline static DstType pixelConvert(SrcType inp)
326  {
327  return inp | 0xFF000000;
328  }
329 };
330 struct X8R8G8B8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_A8B8G8R8)>
331 {
332  inline static DstType pixelConvert(SrcType inp)
333  {
334  return ((inp&0x0000FF)<<16)|((inp&0xFF0000)>>16)|(inp&0x00FF00)|0xFF000000;
335  }
336 };
337 struct X8R8G8B8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_B8G8R8A8)>
338 {
339  inline static DstType pixelConvert(SrcType inp)
340  {
341  return ((inp&0x0000FF)<<24)|((inp&0xFF0000)>>8)|((inp&0x00FF00)<<8)|0x000000FF;
342  }
343 };
344 struct X8R8G8B8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8R8G8B8, PF_R8G8B8A8)>
345 {
346  inline static DstType pixelConvert(SrcType inp)
347  {
348  return ((inp&0xFFFFFF)<<8)|0x000000FF;
349  }
350 };
351 
352 // X8B8G8R8
353 struct X8B8G8R8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_A8R8G8B8)>
354 {
355  inline static DstType pixelConvert(SrcType inp)
356  {
357  return ((inp&0x0000FF)<<16)|((inp&0xFF0000)>>16)|(inp&0x00FF00)|0xFF000000;
358  }
359 };
360 struct X8B8G8R8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_A8B8G8R8)>
361 {
362  inline static DstType pixelConvert(SrcType inp)
363  {
364  return inp | 0xFF000000;
365  }
366 };
367 struct X8B8G8R8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_B8G8R8A8)>
368 {
369  inline static DstType pixelConvert(SrcType inp)
370  {
371  return ((inp&0xFFFFFF)<<8)|0x000000FF;
372  }
373 };
374 struct X8B8G8R8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(PF_X8B8G8R8, PF_R8G8B8A8)>
375 {
376  inline static DstType pixelConvert(SrcType inp)
377  {
378  return ((inp&0x0000FF)<<24)|((inp&0xFF0000)>>8)|((inp&0x00FF00)<<8)|0x000000FF;
379  }
380 };
381 
382 
383 #define CASECONVERTER(type) case type::ID : PixelBoxConverter<type>::conversion(src, dst); return 1;
384 
385 inline int doOptimizedConversion(const PixelBox &src, const PixelBox &dst)
386 {;
387  switch(FMTCONVERTERID(src.format, dst.format))
388  {
389  // Register converters here
390  CASECONVERTER(A8R8G8B8toA8B8G8R8);
391  CASECONVERTER(A8R8G8B8toB8G8R8A8);
392  CASECONVERTER(A8R8G8B8toR8G8B8A8);
393  CASECONVERTER(A8B8G8R8toA8R8G8B8);
394  CASECONVERTER(A8B8G8R8toB8G8R8A8);
395  CASECONVERTER(A8B8G8R8toR8G8B8A8);
396  CASECONVERTER(B8G8R8A8toA8R8G8B8);
397  CASECONVERTER(B8G8R8A8toA8B8G8R8);
398  CASECONVERTER(B8G8R8A8toR8G8B8A8);
399  CASECONVERTER(R8G8B8A8toA8R8G8B8);
400  CASECONVERTER(R8G8B8A8toA8B8G8R8);
401  CASECONVERTER(R8G8B8A8toB8G8R8A8);
402  CASECONVERTER(A8B8G8R8toL8);
403  CASECONVERTER(L8toA8B8G8R8);
404  CASECONVERTER(A8R8G8B8toL8);
405  CASECONVERTER(L8toA8R8G8B8);
406  CASECONVERTER(B8G8R8A8toL8);
407  CASECONVERTER(L8toB8G8R8A8);
408  CASECONVERTER(L8toL16);
409  CASECONVERTER(L16toL8);
410  CASECONVERTER(B8G8R8toR8G8B8);
411  CASECONVERTER(R8G8B8toB8G8R8);
412  CASECONVERTER(R8G8B8toA8R8G8B8);
413  CASECONVERTER(B8G8R8toA8R8G8B8);
414  CASECONVERTER(R8G8B8toA8B8G8R8);
415  CASECONVERTER(B8G8R8toA8B8G8R8);
416  CASECONVERTER(R8G8B8toB8G8R8A8);
417  CASECONVERTER(B8G8R8toB8G8R8A8);
418  CASECONVERTER(A8R8G8B8toR8G8B8);
419  CASECONVERTER(A8R8G8B8toB8G8R8);
420  CASECONVERTER(X8R8G8B8toA8R8G8B8);
421  CASECONVERTER(X8R8G8B8toA8B8G8R8);
422  CASECONVERTER(X8R8G8B8toB8G8R8A8);
423  CASECONVERTER(X8R8G8B8toR8G8B8A8);
424  CASECONVERTER(X8B8G8R8toA8R8G8B8);
425  CASECONVERTER(X8B8G8R8toA8B8G8R8);
426  CASECONVERTER(X8B8G8R8toB8G8R8A8);
427  CASECONVERTER(X8B8G8R8toR8G8B8A8);
428 
429  default:
430  return 0;
431  }
432 }
433 #undef CASECONVERTER
434 
437 #endif // VC6 protection
unsigned char uint8
Definition: OgrePlatform.h:248
unsigned int uint32
Definition: OgrePlatform.h:246
size_t rowPitch
Number of elements between the leftmost pixel of one row and the left pixel of the next...
size_t getSliceSkip() const
Get the number of elements between one past the right bottom pixel of one slice and the left top pixe...
size_t back
Definition: OgreCommon.h:635
void * data
The data pointer.
PixelFormat format
The pixel format.
size_t right
Definition: OgreCommon.h:635
size_t slicePitch
Number of elements between the top left pixel of one (depth) slice and the top left pixel of the next...
size_t front
Definition: OgreCommon.h:635
size_t left
Definition: OgreCommon.h:635
size_t top
Definition: OgreCommon.h:635
unsigned short uint16
Definition: OgrePlatform.h:247
A primitive describing a volume (3D), image (2D) or line (1D) of pixels in memory.
size_t bottom
Definition: OgreCommon.h:635