OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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-2014 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 
51 template <class U> struct PixelBoxConverter
52 {
53  static const int ID = U::ID;
54  static void conversion(const Ogre::PixelBox &src, const Ogre::PixelBox &dst)
55  {
56  typename U::SrcType *srcptr = static_cast<typename U::SrcType*>(src.data)
57  + (src.left + src.top * src.rowPitch + src.front * src.slicePitch);
58  typename U::DstType *dstptr = static_cast<typename U::DstType*>(dst.data)
59  + (dst.left + dst.top * dst.rowPitch + dst.front * dst.slicePitch);
60  const size_t srcSliceSkip = src.getSliceSkip();
61  const size_t dstSliceSkip = dst.getSliceSkip();
62  const size_t k = src.right - src.left;
63  for(size_t z=src.front; z<src.back; z++)
64  {
65  for(size_t y=src.top; y<src.bottom; y++)
66  {
67  for(size_t x=0; x<k; x++)
68  {
69  dstptr[x] = U::pixelConvert(srcptr[x]);
70  }
71  srcptr += src.rowPitch;
72  dstptr += dst.rowPitch;
73  }
74  srcptr += srcSliceSkip;
75  dstptr += dstSliceSkip;
76  }
77  }
78 };
79 
80 template <typename T, typename U, int id> struct PixelConverter {
81  static const int ID = id;
82  typedef T SrcType;
83  typedef U DstType;
84 
85  //inline static DstType pixelConvert(const SrcType &inp);
86 };
87 
88 
90 struct Col3b {
91  Col3b(unsigned int a, unsigned int b, unsigned int c):
92  x((Ogre::uint8)a), y((Ogre::uint8)b), z((Ogre::uint8)c) { }
93  Ogre::uint8 x,y,z;
94 };
96 struct Col3f {
97  Col3f(float inR, float inG, float inB):
98  r(inR), g(inG), b(inB) { }
99  float r,g,b;
100 };
102 struct Col4f {
103  Col4f(float inR, float inG, float inB, float inA):
104  r(inR), g(inG), b(inB), a(inA) { }
105  float r,g,b,a;
106 };
107 
108 struct A8R8G8B8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_A8R8G8B8, Ogre::PF_A8B8G8R8)>
109 {
110  inline static DstType pixelConvert(SrcType inp)
111  {
112  return ((inp&0x000000FF)<<16)|(inp&0xFF00FF00)|((inp&0x00FF0000)>>16);
113  }
114 };
115 
116 struct A8R8G8B8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_A8R8G8B8, Ogre::PF_B8G8R8A8)>
117 {
118  inline static DstType pixelConvert(SrcType inp)
119  {
120  return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
121  }
122 };
123 
124 struct A8R8G8B8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_A8R8G8B8, Ogre::PF_R8G8B8A8)>
125 {
126  inline static DstType pixelConvert(SrcType inp)
127  {
128  return ((inp&0x00FFFFFF)<<8)|((inp&0xFF000000)>>24);
129  }
130 };
131 
132 struct A8B8G8R8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_A8B8G8R8, Ogre::PF_A8R8G8B8)>
133 {
134  inline static DstType pixelConvert(SrcType inp)
135  {
136  return ((inp&0x000000FF)<<16)|(inp&0xFF00FF00)|((inp&0x00FF0000)>>16);
137  }
138 };
139 
140 struct A8B8G8R8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_A8B8G8R8, Ogre::PF_B8G8R8A8)>
141 {
142  inline static DstType pixelConvert(SrcType inp)
143  {
144  return ((inp&0x00FFFFFF)<<8)|((inp&0xFF000000)>>24);
145  }
146 };
147 
148 struct A8B8G8R8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_A8B8G8R8, Ogre::PF_R8G8B8A8)>
149 {
150  inline static DstType pixelConvert(SrcType inp)
151  {
152  return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
153  }
154 };
155 
156 struct B8G8R8A8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_B8G8R8A8, Ogre::PF_A8R8G8B8)>
157 {
158  inline static DstType pixelConvert(SrcType inp)
159  {
160  return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
161  }
162 };
163 
164 struct B8G8R8A8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_B8G8R8A8, Ogre::PF_A8B8G8R8)>
165 {
166  inline static DstType pixelConvert(SrcType inp)
167  {
168  return ((inp&0x000000FF)<<24)|((inp&0xFFFFFF00)>>8);
169  }
170 };
171 
172 struct B8G8R8A8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_B8G8R8A8, Ogre::PF_R8G8B8A8)>
173 {
174  inline static DstType pixelConvert(SrcType inp)
175  {
176  return ((inp&0x0000FF00)<<16)|(inp&0x00FF00FF)|((inp&0xFF000000)>>16);
177  }
178 };
179 
180 struct R8G8B8A8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_R8G8B8A8, Ogre::PF_A8R8G8B8)>
181 {
182  inline static DstType pixelConvert(SrcType inp)
183  {
184  return ((inp&0x000000FF)<<24)|((inp&0xFFFFFF00)>>8);
185  }
186 };
187 
188 struct R8G8B8A8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_R8G8B8A8, Ogre::PF_A8B8G8R8)>
189 {
190  inline static DstType pixelConvert(SrcType inp)
191  {
192  return ((inp&0x000000FF)<<24)|((inp&0x0000FF00)<<8)|((inp&0x00FF0000)>>8)|((inp&0xFF000000)>>24);
193  }
194 };
195 
196 struct R8G8B8A8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_R8G8B8A8, Ogre::PF_B8G8R8A8)>
197 {
198  inline static DstType pixelConvert(SrcType inp)
199  {
200  return ((inp&0x0000FF00)<<16)|(inp&0x00FF00FF)|((inp&0xFF000000)>>16);
201  }
202 };
203 
204 struct A8B8G8R8toL8: public PixelConverter <Ogre::uint32, Ogre::uint8, FMTCONVERTERID(Ogre::PF_A8B8G8R8, Ogre::PF_L8)>
205 {
206  inline static DstType pixelConvert(SrcType inp)
207  {
208  return (Ogre::uint8)(inp&0x000000FF);
209  }
210 };
211 
212 struct L8toA8B8G8R8: public PixelConverter <Ogre::uint8, Ogre::uint32, FMTCONVERTERID(Ogre::PF_L8, Ogre::PF_A8B8G8R8)>
213 {
214  inline static DstType pixelConvert(SrcType inp)
215  {
216  return 0xFF000000|(((unsigned int)inp)<<0)|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16);
217  }
218 };
219 
220 struct A8R8G8B8toL8: public PixelConverter <Ogre::uint32, Ogre::uint8, FMTCONVERTERID(Ogre::PF_A8R8G8B8, Ogre::PF_L8)>
221 {
222  inline static DstType pixelConvert(SrcType inp)
223  {
224  return (Ogre::uint8)((inp&0x00FF0000)>>16);
225  }
226 };
227 
228 struct L8toA8R8G8B8: public PixelConverter <Ogre::uint8, Ogre::uint32, FMTCONVERTERID(Ogre::PF_L8, Ogre::PF_A8R8G8B8)>
229 {
230  inline static DstType pixelConvert(SrcType inp)
231  {
232  return 0xFF000000|(((unsigned int)inp)<<0)|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16);
233  }
234 };
235 
236 struct B8G8R8A8toL8: public PixelConverter <Ogre::uint32, Ogre::uint8, FMTCONVERTERID(Ogre::PF_B8G8R8A8, Ogre::PF_L8)>
237 {
238  inline static DstType pixelConvert(SrcType inp)
239  {
240  return (Ogre::uint8)((inp&0x0000FF00)>>8);
241  }
242 };
243 
244 struct L8toB8G8R8A8: public PixelConverter <Ogre::uint8, Ogre::uint32, FMTCONVERTERID(Ogre::PF_L8, Ogre::PF_B8G8R8A8)>
245 {
246  inline static DstType pixelConvert(SrcType inp)
247  {
248  return 0x000000FF|(((unsigned int)inp)<<8)|(((unsigned int)inp)<<16)|(((unsigned int)inp)<<24);
249  }
250 };
251 
252 struct L8toL16: public PixelConverter <Ogre::uint8, Ogre::uint16, FMTCONVERTERID(Ogre::PF_L8, Ogre::PF_L16)>
253 {
254  inline static DstType pixelConvert(SrcType inp)
255  {
256  return (Ogre::uint16)((((unsigned int)inp)<<8)|(((unsigned int)inp)));
257  }
258 };
259 
260 struct L16toL8: public PixelConverter <Ogre::uint16, Ogre::uint8, FMTCONVERTERID(Ogre::PF_L16, Ogre::PF_L8)>
261 {
262  inline static DstType pixelConvert(SrcType inp)
263  {
264  return (Ogre::uint8)(inp>>8);
265  }
266 };
267 
268 struct R8G8B8toB8G8R8: public PixelConverter <Col3b, Col3b, FMTCONVERTERID(Ogre::PF_R8G8B8, Ogre::PF_B8G8R8)>
269 {
270  inline static DstType pixelConvert(const SrcType &inp)
271  {
272  return Col3b(inp.z, inp.y, inp.x);
273  }
274 };
275 
276 struct B8G8R8toR8G8B8: public PixelConverter <Col3b, Col3b, FMTCONVERTERID(Ogre::PF_B8G8R8, Ogre::PF_R8G8B8)>
277 {
278  inline static DstType pixelConvert(const SrcType &inp)
279  {
280  return Col3b(inp.z, inp.y, inp.x);
281  }
282 };
283 
284 // X8Y8Z8 -> X8<<xshift Y8<<yshift Z8<<zshift A8<<ashift
285 template <int id, unsigned int xshift, unsigned int yshift, unsigned int zshift, unsigned int ashift> struct Col3btoUint32swizzler:
286  public PixelConverter <Col3b, Ogre::uint32, id>
287 {
288  inline static Ogre::uint32 pixelConvert(const Col3b &inp)
289  {
290 #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
291  return (0xFF<<ashift) | (((unsigned int)inp.x)<<xshift) | (((unsigned int)inp.y)<<yshift) | (((unsigned int)inp.z)<<zshift);
292 #else
293  return (0xFF<<ashift) | (((unsigned int)inp.x)<<zshift) | (((unsigned int)inp.y)<<yshift) | (((unsigned int)inp.z)<<xshift);
294 #endif
295  }
296 };
297 
298 struct R8G8B8toA8R8G8B8: public Col3btoUint32swizzler<FMTCONVERTERID(Ogre::PF_R8G8B8, Ogre::PF_A8R8G8B8), 16, 8, 0, 24> { };
299 struct B8G8R8toA8R8G8B8: public Col3btoUint32swizzler<FMTCONVERTERID(Ogre::PF_B8G8R8, Ogre::PF_A8R8G8B8), 0, 8, 16, 24> { };
300 struct R8G8B8toA8B8G8R8: public Col3btoUint32swizzler<FMTCONVERTERID(Ogre::PF_R8G8B8, Ogre::PF_A8B8G8R8), 0, 8, 16, 24> { };
301 struct B8G8R8toA8B8G8R8: public Col3btoUint32swizzler<FMTCONVERTERID(Ogre::PF_B8G8R8, Ogre::PF_A8B8G8R8), 16, 8, 0, 24> { };
302 struct R8G8B8toB8G8R8A8: public Col3btoUint32swizzler<FMTCONVERTERID(Ogre::PF_R8G8B8, Ogre::PF_B8G8R8A8), 8, 16, 24, 0> { };
303 struct B8G8R8toB8G8R8A8: public Col3btoUint32swizzler<FMTCONVERTERID(Ogre::PF_B8G8R8, Ogre::PF_B8G8R8A8), 24, 16, 8, 0> { };
304 
305 struct A8R8G8B8toR8G8B8: public PixelConverter <Ogre::uint32, Col3b, FMTCONVERTERID(Ogre::PF_A8R8G8B8, Ogre::PF_BYTE_RGB)>
306 {
307  inline static DstType pixelConvert(Ogre::uint32 inp)
308  {
309  return Col3b((Ogre::uint8)((inp>>16)&0xFF), (Ogre::uint8)((inp>>8)&0xFF), (Ogre::uint8)((inp>>0)&0xFF));
310  }
311 };
312 struct A8R8G8B8toB8G8R8: public PixelConverter <Ogre::uint32, Col3b, FMTCONVERTERID(Ogre::PF_A8R8G8B8, Ogre::PF_BYTE_BGR)>
313 {
314  inline static DstType pixelConvert(Ogre::uint32 inp)
315  {
316  return Col3b((Ogre::uint8)((inp>>0)&0xFF), (Ogre::uint8)((inp>>8)&0xFF), (Ogre::uint8)((inp>>16)&0xFF));
317  }
318 };
319 
320 // Only conversions from X8R8G8B8 to formats with alpha need to be defined, the rest is implicitly the same
321 // as A8R8G8B8
322 struct X8R8G8B8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_X8R8G8B8, Ogre::PF_A8R8G8B8)>
323 {
324  inline static DstType pixelConvert(SrcType inp)
325  {
326  return inp | 0xFF000000;
327  }
328 };
329 struct X8R8G8B8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_X8R8G8B8, Ogre::PF_A8B8G8R8)>
330 {
331  inline static DstType pixelConvert(SrcType inp)
332  {
333  return ((inp&0x0000FF)<<16)|((inp&0xFF0000)>>16)|(inp&0x00FF00)|0xFF000000;
334  }
335 };
336 struct X8R8G8B8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_X8R8G8B8, Ogre::PF_B8G8R8A8)>
337 {
338  inline static DstType pixelConvert(SrcType inp)
339  {
340  return ((inp&0x0000FF)<<24)|((inp&0xFF0000)>>8)|((inp&0x00FF00)<<8)|0x000000FF;
341  }
342 };
343 struct X8R8G8B8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_X8R8G8B8, Ogre::PF_R8G8B8A8)>
344 {
345  inline static DstType pixelConvert(SrcType inp)
346  {
347  return ((inp&0xFFFFFF)<<8)|0x000000FF;
348  }
349 };
350 
351 // X8B8G8R8
352 struct X8B8G8R8toA8R8G8B8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_X8B8G8R8, Ogre::PF_A8R8G8B8)>
353 {
354  inline static DstType pixelConvert(SrcType inp)
355  {
356  return ((inp&0x0000FF)<<16)|((inp&0xFF0000)>>16)|(inp&0x00FF00)|0xFF000000;
357  }
358 };
359 struct X8B8G8R8toA8B8G8R8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_X8B8G8R8, Ogre::PF_A8B8G8R8)>
360 {
361  inline static DstType pixelConvert(SrcType inp)
362  {
363  return inp | 0xFF000000;
364  }
365 };
366 struct X8B8G8R8toB8G8R8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_X8B8G8R8, Ogre::PF_B8G8R8A8)>
367 {
368  inline static DstType pixelConvert(SrcType inp)
369  {
370  return ((inp&0xFFFFFF)<<8)|0x000000FF;
371  }
372 };
373 struct X8B8G8R8toR8G8B8A8: public PixelConverter <Ogre::uint32, Ogre::uint32, FMTCONVERTERID(Ogre::PF_X8B8G8R8, Ogre::PF_R8G8B8A8)>
374 {
375  inline static DstType pixelConvert(SrcType inp)
376  {
377  return ((inp&0x0000FF)<<24)|((inp&0xFF0000)>>8)|((inp&0x00FF00)<<8)|0x000000FF;
378  }
379 };
380 
381 
382 #define CASECONVERTER(type) case type::ID : PixelBoxConverter<type>::conversion(src, dst); return 1;
383 
384 inline int doOptimizedConversion(const Ogre::PixelBox &src, const Ogre::PixelBox &dst)
385 {;
386  switch(FMTCONVERTERID(src.format, dst.format))
387  {
388  // Register converters here
389  CASECONVERTER(A8R8G8B8toA8B8G8R8);
390  CASECONVERTER(A8R8G8B8toB8G8R8A8);
391  CASECONVERTER(A8R8G8B8toR8G8B8A8);
392  CASECONVERTER(A8B8G8R8toA8R8G8B8);
393  CASECONVERTER(A8B8G8R8toB8G8R8A8);
394  CASECONVERTER(A8B8G8R8toR8G8B8A8);
395  CASECONVERTER(B8G8R8A8toA8R8G8B8);
396  CASECONVERTER(B8G8R8A8toA8B8G8R8);
397  CASECONVERTER(B8G8R8A8toR8G8B8A8);
398  CASECONVERTER(R8G8B8A8toA8R8G8B8);
399  CASECONVERTER(R8G8B8A8toA8B8G8R8);
400  CASECONVERTER(R8G8B8A8toB8G8R8A8);
401  CASECONVERTER(A8B8G8R8toL8);
402  CASECONVERTER(L8toA8B8G8R8);
403  CASECONVERTER(A8R8G8B8toL8);
404  CASECONVERTER(L8toA8R8G8B8);
405  CASECONVERTER(B8G8R8A8toL8);
406  CASECONVERTER(L8toB8G8R8A8);
407  CASECONVERTER(L8toL16);
408  CASECONVERTER(L16toL8);
409  CASECONVERTER(B8G8R8toR8G8B8);
410  CASECONVERTER(R8G8B8toB8G8R8);
411  CASECONVERTER(R8G8B8toA8R8G8B8);
412  CASECONVERTER(B8G8R8toA8R8G8B8);
413  CASECONVERTER(R8G8B8toA8B8G8R8);
414  CASECONVERTER(B8G8R8toA8B8G8R8);
415  CASECONVERTER(R8G8B8toB8G8R8A8);
416  CASECONVERTER(B8G8R8toB8G8R8A8);
417  CASECONVERTER(A8R8G8B8toR8G8B8);
418  CASECONVERTER(A8R8G8B8toB8G8R8);
419  CASECONVERTER(X8R8G8B8toA8R8G8B8);
420  CASECONVERTER(X8R8G8B8toA8B8G8R8);
421  CASECONVERTER(X8R8G8B8toB8G8R8A8);
422  CASECONVERTER(X8R8G8B8toR8G8B8A8);
423  CASECONVERTER(X8B8G8R8toA8R8G8B8);
424  CASECONVERTER(X8B8G8R8toA8B8G8R8);
425  CASECONVERTER(X8B8G8R8toB8G8R8A8);
426  CASECONVERTER(X8B8G8R8toR8G8B8A8);
427 
428  default:
429  return 0;
430  }
431 }
432 #undef CASECONVERTER
433 
436 #endif // VC6 protection
unsigned char uint8
Definition: OgrePlatform.h:422
uint32 bottom
Definition: OgreCommon.h:691
uint32 front
Definition: OgreCommon.h:691
unsigned int uint32
Definition: OgrePlatform.h:420
size_t rowPitch
Number of elements between the leftmost pixel of one row and the left pixel of the next...
Definition: OgrePixelBox.h:84
size_t getSliceSkip() const
Get the number of elements between one past the right bottom pixel of one slice and the left top pixe...
Definition: OgrePixelBox.h:109
void * data
The data pointer.
Definition: OgrePixelBox.h:77
PixelFormat format
The pixel format.
Definition: OgrePixelBox.h:79
uint32 right
Definition: OgreCommon.h:691
size_t slicePitch
Number of elements between the top left pixel of one (depth) slice and the top left pixel of the next...
Definition: OgrePixelBox.h:90
uint32 top
Definition: OgreCommon.h:691
unsigned short uint16
Definition: OgrePlatform.h:421
uint32 back
Definition: OgreCommon.h:691
uint32 left
Definition: OgreCommon.h:691