Oxygine  1
2g game engine
pixel.h
1 #pragma once
2 #include "oxygine-include.h"
3 
4 namespace oxygine
5 {
6  const unsigned char lookupTable4to8[] = {0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255};
7  const unsigned char lookupTable5to8[] = {0, 8, 16, 24, 32, 41, 49, 57, 65, 74, 82, 90, 98, 106, 115, 123, 131, 139, 148, 156, 164, 172, 180, 189, 197, 205, 213, 222, 230, 238, 246, 255};
8  const unsigned char lookupTable6to8[] = {0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 129, 133, 137, 141, 145, 149, 153, 157, 161, 165, 170, 174, 178, 182, 186, 190, 194, 198, 202, 206, 210, 214, 218, 222, 226, 230, 234, 238, 242, 246, 250, 255};
9 
10  struct Pixel
11  {
12  union
13  {
14  struct
15  {
16  unsigned char bytes[4];
17  };
18 
19  struct
20  {
21  unsigned char r, g, b, a;
22  };
23 
24  unsigned int rgba;
25  };
26  };
27 
28  inline Pixel initPixel(unsigned int rgba)
29  {
30  Pixel p;
31  p.rgba = rgba;
32  return p;
33  }
34 
35  inline Pixel initPixel(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
36  {
37  Pixel p;
38  p.r = r;
39  p.g = g;
40  p.b = b;
41  p.a = a;
42  return p;
43  }
44 
45 
46  class PixelA8
47  {
48  public:
49  void getPixel(const unsigned char* data, Pixel& p) const
50  {
51  p.r = 255;
52  p.g = 255;
53  p.b = 255;
54  p.a = data[0];
55  }
56 
57  void setPixel(unsigned char* data, const Pixel& p)
58  {
59  *data = p.a;
60  }
61 
62  void copy(const unsigned char* src, unsigned char* dst)
63  {
64  *((unsigned char*)dst) = *((unsigned char*)src);
65  }
66 
67  unsigned char snap_a(unsigned char alpha) const
68  {
69  return alpha;
70  }
71  };
72 
73  class PixelL8
74  {
75  public:
76  void getPixel(const unsigned char* data, Pixel& p) const
77  {
78  unsigned char color = *data;
79  p.r = color;
80  p.g = color;
81  p.b = color;
82  p.a = 255;
83  }
84 
85  void setPixel(unsigned char* data, const Pixel& p)
86  {
87  *data = (p.r + p.g + p.b) / 3;
88  }
89 
90  void copy(const unsigned char* src, unsigned char* dst)
91  {
92  *dst = *src;
93  }
94 
95  unsigned char snap_a(unsigned char alpha) const
96  {
97  return 255;
98  }
99  };
100 
101  class PixelA8L8
102  {
103  public:
104  void getPixel(const unsigned char* data, Pixel& p) const
105  {
106  unsigned char color = *data;
107  p.r = color;
108  p.g = color;
109  p.b = color;
110  p.a = 255;
111  }
112 
113  void setPixel(unsigned char* data, const Pixel& p)
114  {
115  *data = (p.r + p.g + p.b) / 3;
116  }
117 
118  void copy(const unsigned char* src, unsigned char* dst)
119  {
120  *dst = *src;
121  }
122 
123  unsigned char snap_a(unsigned char alpha) const
124  {
125  return alpha;
126  }
127  };
128 
129  class PixelR5G6B5
130  {
131  /*
132  in memory: BBBBB_GGGGGG_RRRRR
133  in dword: RRRRR_GGGGGG_BBBBB
134  */
135  public:
136  void getPixel(const unsigned char* data, Pixel& p) const
137  {
138  unsigned short color = *((unsigned short*)data);
139  p.r = lookupTable5to8[(color & 0xF800) >> 11];
140  p.g = lookupTable6to8[(color & 0x7E0) >> 5];
141  p.b = lookupTable5to8[(color & 0x1F)];
142  p.a = 255;
143  }
144 
145  void setPixel(unsigned char* data, const Pixel& p)
146  {
147  unsigned short* pshort = (unsigned short*)data;
148  *pshort = ((p.r >> 3) << 11) | ((p.g >> 2) << 5) | (p.b >> 3);
149  }
150  void copy(const unsigned char* src, unsigned char* dst)
151  {
152  *((unsigned short*)dst) = *((unsigned short*)src);
153  }
154 
155  unsigned char snap_a(unsigned char alpha) const
156  {
157  return 255;
158  }
159  };
160 
161  class PixelB5G6R5
162  {
163  /*
164  in memory: RRRRR_GGGGGG_BBBBB
165  in dword: BBBBB_GGGGGG_RRRRR
166  */
167  public:
168  void getPixel(const unsigned char* data, Pixel& p) const
169  {
170  unsigned short color = *((unsigned short*)data);
171  p.b = lookupTable5to8[(color & 0xF800) >> 11];
172  p.g = lookupTable6to8[(color & 0x7E0) >> 5];
173  p.r = lookupTable5to8[(color & 0x1F)];
174  p.a = 255;
175  }
176 
177  void setPixel(unsigned char* data, const Pixel& p)
178  {
179  unsigned short* pshort = (unsigned short*)data;
180  *pshort = ((p.b >> 3) << 11) | ((p.g >> 2) << 5) | (p.r >> 3);
181  }
182  void copy(const unsigned char* src, unsigned char* dst)
183  {
184  *((unsigned short*)dst) = *((unsigned short*)src);
185  }
186 
187  unsigned char snap_a(unsigned char alpha) const
188  {
189  return 255;
190  }
191  };
192 
193 
194 
195  /*
196  class PixelA4R4G4B4
197  {
198 
199  //in memory: BBBB_GGGG_RRRR_AAAA
200  //in word: AAAA_RRRR_GGGG_BBBB
201 
202  public:
203  void getPixel(const unsigned char *data, Pixel &p) const
204  {
205  unsigned short color = *((unsigned short *)data);
206 
207  p.r = table4to8[((color & 0xF00) >> 8)];
208  p.g = table4to8[((color & 0xF0))];
209  p.b = table4to8[((color & 0xF) << 4)];
210  p.a = table4to8[((color & 0xF000) >> 12)];
211  }
212 
213  void setPixel(unsigned char *data, const Pixel &p)
214  {
215  unsigned short *pshort = (unsigned short *)data;
216  *pshort = ((p.a>>4) << 12) | ((p.r>>4) << 8) | ((p.g>>4) << 4) | ((p.b>>4));
217  }
218  void copy(const unsigned char *src, unsigned char *dst)
219  {
220  *((unsigned short *)dst) = *((const unsigned short*)src);
221  }
222 
223  unsigned char snap_a(unsigned char alpha) const
224  {
225  return table4to8[(alpha >> 4)];
226  }
227  };
228  */
229 
230  class PixelR4G4B4A4
231  {
232  /*
233  in memory: AAAA_BBBB_GGGG_RRRR
234  in word: RRRR_GGGG_BBBB_AAAA
235  */
236  public:
237  void getPixel(const unsigned char* data, Pixel& p) const
238  {
239  unsigned short color = *((unsigned short*)data);
240 
241  p.r = lookupTable4to8[(color & 0xF000) >> 12];
242  p.g = lookupTable4to8[(color & 0xF00) >> 8];
243  p.b = lookupTable4to8[(color & 0xF0) >> 4];
244  p.a = lookupTable4to8[(color & 0xF)];
245 
246  /*
247  p.r = ((color & 0xF000) >> 8);
248  p.g = ((color & 0xF00) >> 4);
249  p.b = ((color & 0xF0));
250  p.a = ((color & 0xF) << 4);
251  */
252  }
253 
254  void setPixel(unsigned char* data, const Pixel& p)
255  {
256  unsigned short* pshort = (unsigned short*)data;
257  *pshort = ((p.r >> 4) << 12) | ((p.g >> 4) << 8) | ((p.b >> 4) << 4) | ((p.a >> 4));
258  }
259  void copy(const unsigned char* src, unsigned char* dst)
260  {
261  *((unsigned short*)dst) = *((const unsigned short*)src);
262  }
263 
264  unsigned char snap_a(unsigned char alpha) const
265  {
266  return lookupTable4to8[alpha >> 4];
267  }
268  };
269 
270  /*
271  class PixelA1R5G5B5
272  {
273 
274  //in memory: BBBBB_GGGGG_RRRRR_A
275  //in dword: A_RRRRR_GGGGG_BBBBB
276 
277  public:
278  void getPixel(const unsigned char *data, Pixel &p) const
279  {
280  unsigned short color = *((unsigned short *)data);
281 
282  p.r = lookupTable5to8[(color & 0x7C00) >> 7];
283  p.g = lookupTable5to8[(color & 0x3E0) >> 2];
284  p.b = lookupTable5to8[(color & 0x1F) << 3];
285  p.a = ((color & 0x8000) >> 15) * 255;
286  }
287 
288  void setPixel(unsigned char *data, const Pixel &p)
289  {
290  unsigned short *pshort = (unsigned short *)data;
291  *pshort = ((p.a>>7) << 15) | ((p.r>>3) << 10) | ((p.g>>3) << 5) | ((p.b>>3));
292  }
293  void copy(const unsigned char *src, unsigned char *dst)
294  {
295  *((unsigned short *)dst) = *((const unsigned short*)src);
296  }
297 
298  unsigned char snap_a(unsigned char alpha) const
299  {
300  return (alpha >> 7) * 255;
301  }
302  };
303  */
304 
305  class PixelR5G5B5A1
306  {
307  //in memory: A_BBBBB_GGGGG_RRRRR
308  //in dword: RRRRR_GGGGG_BBBBB_A
309  public:
310  void getPixel(const unsigned char* data, Pixel& p) const
311  {
312  unsigned short color = *((unsigned short*)data);
313 
314  p.r = lookupTable5to8[(color & 0xF800) >> 11];
315  p.g = lookupTable5to8[(color & 0x7C0) >> 6];
316  p.b = lookupTable5to8[(color & 0x3E) >> 1];
317  p.a = (color & 0x01) * 255;
318  }
319 
320  void setPixel(unsigned char* data, const Pixel& p)
321  {
322  unsigned short* pshort = (unsigned short*)data;
323  *pshort = ((p.a >> 7)) | ((p.r >> 3) << 11) | ((p.g >> 3) << 6) | ((p.b >> 3) << 1);
324  }
325 
326  void copy(const unsigned char* src, unsigned char* dst)
327  {
328  *((unsigned short*)dst) = *((const unsigned short*)src);
329  }
330  unsigned char snap_a(unsigned char alpha) const
331  {
332  return (alpha >> 7) * 255;
333  }
334  };
335 
336  class PixelA8B8G8R8
337  {
338  /*
339  in memory: A8 B8 G8 R8
340  */
341  public:
342  void getPixel(const unsigned char* data, Pixel& p) const
343  {
344  p.r = data[3];
345  p.g = data[2];
346  p.b = data[1];
347  p.a = data[0];
348 
349  }
350 
351  void setPixel(unsigned char* data, const Pixel& p)
352  {
353  data[0] = p.a;
354  data[1] = p.b;
355  data[2] = p.g;
356  data[3] = p.r;
357  }
358 
359  void copy(const unsigned char* src, unsigned char* dst)
360  {
361  *((unsigned int*)dst) = *((unsigned int*)src);
362  }
363 
364  unsigned char snap_a(unsigned char alpha) const
365  {
366  return alpha;
367  }
368  };
369 
370 
371 
372  class PixelA8R8G8B8
373  {
374  /*
375  in memory: B8 G8 R8 A8
376  in dword: A8 R8 G8 B8
377  */
378  public:
379  void getPixel(const unsigned char* data, Pixel& p) const
380  {
381  p.r = data[2];
382  p.g = data[1];
383  p.b = data[0];
384  p.a = data[3];
385 
386  }
387 
388  void setPixel(unsigned char* data, const Pixel& p)
389  {
390  data[0] = p.b;
391  data[1] = p.g;
392  data[2] = p.r;
393  data[3] = p.a;
394  }
395 
396  void copy(const unsigned char* src, unsigned char* dst)
397  {
398  *((unsigned int*)dst) = *((unsigned int*)src);
399  }
400 
401  unsigned char snap_a(unsigned char alpha) const
402  {
403  return alpha;
404  }
405  };
406 
407  class PixelR8G8B8A8
408  {
409  /*
410  in memory: R8 G8 B8 A8
411  in dword: A8 B8 G8 R8
412  */
413 
414  public:
415  void getPixel(const unsigned char* data, Pixel& p) const
416  {
417  p.r = data[0];
418  p.g = data[1];
419  p.b = data[2];
420  p.a = data[3];
421  }
422 
423  void setPixel(unsigned char* data, const Pixel& p)
424  {
425  data[0] = p.r;
426  data[1] = p.g;
427  data[2] = p.b;
428  data[3] = p.a;
429  }
430 
431  void copy(const unsigned char* src, unsigned char* dst)
432  {
433  *((unsigned int*)dst) = *((unsigned int*)src);
434  }
435 
436  unsigned char snap_a(unsigned char alpha) const
437  {
438  return alpha;
439  }
440  };
441 
442  class PixelB8G8R8A8
443  {
444  /*
445  in memory: B8 G8 R8 A8
446  in dword: A8 R8 G8 B8
447  */
448 
449  public:
450  void getPixel(const unsigned char* data, Pixel& p) const
451  {
452  p.r = data[2];
453  p.g = data[1];
454  p.b = data[0];
455  p.a = data[3];
456  }
457 
458  void setPixel(unsigned char* data, const Pixel& p)
459  {
460  data[2] = p.r;
461  data[1] = p.g;
462  data[0] = p.b;
463  data[3] = p.a;
464  }
465 
466  void copy(const unsigned char* src, unsigned char* dst)
467  {
468  *((unsigned int*)dst) = *((unsigned int*)src);
469  }
470 
471  unsigned char snap_a(unsigned char alpha) const
472  {
473  return alpha;
474  }
475  };
476 
477  class PixelX8R8G8B8 : public PixelA8R8G8B8 {};
478  class PixelR8G8B8X8 : public PixelR8G8B8A8 {};
479 
480  class PixelR8G8B8
481  {
482  /*
483  in memory: R8 G8 B8
484  in dword: B8 G8 R8
485  */
486  public:
487  PixelR8G8B8(unsigned char alpha = 255): _alpha(alpha) {}
488 
489  void getPixel(const unsigned char* data, Pixel& p) const
490  {
491  p.r = data[0];
492  p.g = data[1];
493  p.b = data[2];
494  p.a = _alpha;
495 
496  }
497 
498  void setPixel(unsigned char* data, const Pixel& p)
499  {
500  data[0] = p.r;
501  data[1] = p.g;
502  data[2] = p.b;
503  }
504 
505  void copy(const unsigned char* src, unsigned char* dst)
506  {
507  *((unsigned short*)dst) = *((const unsigned short*)src);
508  }
509 
510  unsigned char snap_a(unsigned char alpha) const
511  {
512  return _alpha;
513  }
514 
515  private:
516  unsigned char _alpha;
517  };
518 }
–oxgl-end–!
Definition: Actor.h:14