Anti-Grain Geometry Tutorial
agg_color_conv_rgb8.h
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
15 //
16 // A set of functors used with color_conv(). See file agg_color_conv.h
17 // These functors can convert images with up to 8 bits per component.
18 // Use convertors in the following way:
19 //
20 // agg::color_conv(dst, src, agg::color_conv_XXXX_to_YYYY());
21 // whare XXXX and YYYY can be any of:
22 // rgb24
23 // bgr24
24 // rgba32
25 // abgr32
26 // argb32
27 // bgra32
28 // rgb555
29 // rgb565
30 //----------------------------------------------------------------------------
31 
32 #ifndef AGG_COLOR_CONV_RGB8_INCLUDED
33 #define AGG_COLOR_CONV_RGB8_INCLUDED
34 
35 #include "agg_basics.h"
36 #include "agg_color_conv.h"
37 
38 namespace agg
39 {
40 
41  //-----------------------------------------------------color_conv_rgb24
43  {
44  public:
45  void operator () (int8u* dst,
46  const int8u* src,
47  unsigned width) const
48  {
49  do
50  {
51  int8u tmp[3];
52  tmp[0] = *src++;
53  tmp[1] = *src++;
54  tmp[2] = *src++;
55  *dst++ = tmp[2];
56  *dst++ = tmp[1];
57  *dst++ = tmp[0];
58  }
59  while(--width);
60  }
61  };
62 
65 
68 
69 
70 
71  //------------------------------------------------------color_conv_rgba32
72  template<int I1, int I2, int I3, int I4> class color_conv_rgba32
73  {
74  public:
75  void operator () (int8u* dst,
76  const int8u* src,
77  unsigned width) const
78  {
79  do
80  {
81  int8u tmp[4];
82  tmp[0] = *src++;
83  tmp[1] = *src++;
84  tmp[2] = *src++;
85  tmp[3] = *src++;
86  *dst++ = tmp[I1];
87  *dst++ = tmp[I2];
88  *dst++ = tmp[I3];
89  *dst++ = tmp[I4];
90  }
91  while(--width);
92  }
93  };
94 
95 
96  //------------------------------------------------------------------------
97  typedef color_conv_rgba32<0,3,2,1> color_conv_argb32_to_abgr32; //----color_conv_argb32_to_abgr32
98  typedef color_conv_rgba32<3,2,1,0> color_conv_argb32_to_bgra32; //----color_conv_argb32_to_bgra32
99  typedef color_conv_rgba32<1,2,3,0> color_conv_argb32_to_rgba32; //----color_conv_argb32_to_rgba32
100  typedef color_conv_rgba32<3,0,1,2> color_conv_bgra32_to_abgr32; //----color_conv_bgra32_to_abgr32
101  typedef color_conv_rgba32<3,2,1,0> color_conv_bgra32_to_argb32; //----color_conv_bgra32_to_argb32
102  typedef color_conv_rgba32<2,1,0,3> color_conv_bgra32_to_rgba32; //----color_conv_bgra32_to_rgba32
103  typedef color_conv_rgba32<3,2,1,0> color_conv_rgba32_to_abgr32; //----color_conv_rgba32_to_abgr32
104  typedef color_conv_rgba32<3,0,1,2> color_conv_rgba32_to_argb32; //----color_conv_rgba32_to_argb32
105  typedef color_conv_rgba32<2,1,0,3> color_conv_rgba32_to_bgra32; //----color_conv_rgba32_to_bgra32
106  typedef color_conv_rgba32<0,3,2,1> color_conv_abgr32_to_argb32; //----color_conv_abgr32_to_argb32
107  typedef color_conv_rgba32<1,2,3,0> color_conv_abgr32_to_bgra32; //----color_conv_abgr32_to_bgra32
108  typedef color_conv_rgba32<3,2,1,0> color_conv_abgr32_to_rgba32; //----color_conv_abgr32_to_rgba32
109 
110  //------------------------------------------------------------------------
111  typedef color_conv_same<4> color_conv_rgba32_to_rgba32; //----color_conv_rgba32_to_rgba32
112  typedef color_conv_same<4> color_conv_argb32_to_argb32; //----color_conv_argb32_to_argb32
113  typedef color_conv_same<4> color_conv_bgra32_to_bgra32; //----color_conv_bgra32_to_bgra32
114  typedef color_conv_same<4> color_conv_abgr32_to_abgr32; //----color_conv_abgr32_to_abgr32
115 
116 
117  //--------------------------------------------color_conv_rgb24_rgba32
118  template<int I1, int I2, int I3, int A> class color_conv_rgb24_rgba32
119  {
120  public:
121  void operator () (int8u* dst,
122  const int8u* src,
123  unsigned width) const
124  {
125  do
126  {
127  dst[I1] = *src++;
128  dst[I2] = *src++;
129  dst[I3] = *src++;
130  dst[A] = 255;
131  dst += 4;
132  }
133  while(--width);
134  }
135  };
136 
137 
138  //------------------------------------------------------------------------
139  typedef color_conv_rgb24_rgba32<1,2,3,0> color_conv_rgb24_to_argb32; //----color_conv_rgb24_to_argb32
140  typedef color_conv_rgb24_rgba32<3,2,1,0> color_conv_rgb24_to_abgr32; //----color_conv_rgb24_to_abgr32
141  typedef color_conv_rgb24_rgba32<2,1,0,3> color_conv_rgb24_to_bgra32; //----color_conv_rgb24_to_bgra32
142  typedef color_conv_rgb24_rgba32<0,1,2,3> color_conv_rgb24_to_rgba32; //----color_conv_rgb24_to_rgba32
143  typedef color_conv_rgb24_rgba32<3,2,1,0> color_conv_bgr24_to_argb32; //----color_conv_bgr24_to_argb32
144  typedef color_conv_rgb24_rgba32<1,2,3,0> color_conv_bgr24_to_abgr32; //----color_conv_bgr24_to_abgr32
145  typedef color_conv_rgb24_rgba32<0,1,2,3> color_conv_bgr24_to_bgra32; //----color_conv_bgr24_to_bgra32
146  typedef color_conv_rgb24_rgba32<2,1,0,3> color_conv_bgr24_to_rgba32; //----color_conv_bgr24_to_rgba32
147 
148 
149 
150  //-------------------------------------------------color_conv_rgba32_rgb24
151  template<int I1, int I2, int I3> class color_conv_rgba32_rgb24
152  {
153  public:
154  void operator () (int8u* dst,
155  const int8u* src,
156  unsigned width) const
157  {
158  do
159  {
160  *dst++ = src[I1];
161  *dst++ = src[I2];
162  *dst++ = src[I3];
163  src += 4;
164  }
165  while(--width);
166  }
167  };
168 
169 
170 
171  //------------------------------------------------------------------------
172  typedef color_conv_rgba32_rgb24<1,2,3> color_conv_argb32_to_rgb24; //----color_conv_argb32_to_rgb24
173  typedef color_conv_rgba32_rgb24<3,2,1> color_conv_abgr32_to_rgb24; //----color_conv_abgr32_to_rgb24
174  typedef color_conv_rgba32_rgb24<2,1,0> color_conv_bgra32_to_rgb24; //----color_conv_bgra32_to_rgb24
175  typedef color_conv_rgba32_rgb24<0,1,2> color_conv_rgba32_to_rgb24; //----color_conv_rgba32_to_rgb24
176  typedef color_conv_rgba32_rgb24<3,2,1> color_conv_argb32_to_bgr24; //----color_conv_argb32_to_bgr24
177  typedef color_conv_rgba32_rgb24<1,2,3> color_conv_abgr32_to_bgr24; //----color_conv_abgr32_to_bgr24
178  typedef color_conv_rgba32_rgb24<0,1,2> color_conv_bgra32_to_bgr24; //----color_conv_bgra32_to_bgr24
179  typedef color_conv_rgba32_rgb24<2,1,0> color_conv_rgba32_to_bgr24; //----color_conv_rgba32_to_bgr24
180 
181 
182  //------------------------------------------------color_conv_rgb555_rgb24
183  template<int R, int B> class color_conv_rgb555_rgb24
184  {
185  public:
186  void operator () (int8u* dst,
187  const int8u* src,
188  unsigned width) const
189  {
190  do
191  {
192  unsigned rgb = *(int16u*)src;
193  dst[R] = (int8u)((rgb >> 7) & 0xF8);
194  dst[1] = (int8u)((rgb >> 2) & 0xF8);
195  dst[B] = (int8u)((rgb << 3) & 0xF8);
196  src += 2;
197  dst += 3;
198  }
199  while(--width);
200  }
201  };
202 
203 
204  //------------------------------------------------------------------------
205  typedef color_conv_rgb555_rgb24<2,0> color_conv_rgb555_to_bgr24; //----color_conv_rgb555_to_bgr24
206  typedef color_conv_rgb555_rgb24<0,2> color_conv_rgb555_to_rgb24; //----color_conv_rgb555_to_rgb24
207 
208 
209  //-------------------------------------------------color_conv_rgb24_rgb555
210  template<int R, int B> class color_conv_rgb24_rgb555
211  {
212  public:
213  void operator () (int8u* dst,
214  const int8u* src,
215  unsigned width) const
216  {
217  do
218  {
219  *(int16u*)dst = (int16u)(((unsigned(src[R]) << 7) & 0x7C00) |
220  ((unsigned(src[1]) << 2) & 0x3E0) |
221  ((unsigned(src[B]) >> 3)));
222  src += 3;
223  dst += 2;
224  }
225  while(--width);
226  }
227  };
228 
229 
230  //------------------------------------------------------------------------
231  typedef color_conv_rgb24_rgb555<2,0> color_conv_bgr24_to_rgb555; //----color_conv_bgr24_to_rgb555
232  typedef color_conv_rgb24_rgb555<0,2> color_conv_rgb24_to_rgb555; //----color_conv_rgb24_to_rgb555
233 
234 
235  //-------------------------------------------------color_conv_rgb565_rgb24
236  template<int R, int B> class color_conv_rgb565_rgb24
237  {
238  public:
239  void operator () (int8u* dst,
240  const int8u* src,
241  unsigned width) const
242  {
243  do
244  {
245  unsigned rgb = *(int16u*)src;
246  dst[R] = (rgb >> 8) & 0xF8;
247  dst[1] = (rgb >> 3) & 0xFC;
248  dst[B] = (rgb << 3) & 0xF8;
249  src += 2;
250  dst += 3;
251  }
252  while(--width);
253  }
254  };
255 
256 
257  //------------------------------------------------------------------------
258  typedef color_conv_rgb565_rgb24<2,0> color_conv_rgb565_to_bgr24; //----color_conv_rgb565_to_bgr24
259  typedef color_conv_rgb565_rgb24<0,2> color_conv_rgb565_to_rgb24; //----color_conv_rgb565_to_rgb24
260 
261 
262  //-------------------------------------------------color_conv_rgb24_rgb565
263  template<int R, int B> class color_conv_rgb24_rgb565
264  {
265  public:
266  void operator () (int8u* dst,
267  const int8u* src,
268  unsigned width) const
269  {
270  do
271  {
272  *(int16u*)dst = (int16u)(((unsigned(src[R]) << 8) & 0xF800) |
273  ((unsigned(src[1]) << 3) & 0x7E0) |
274  ((unsigned(src[B]) >> 3)));
275  src += 3;
276  dst += 2;
277  }
278  while(--width);
279  }
280  };
281 
282 
283  //------------------------------------------------------------------------
284  typedef color_conv_rgb24_rgb565<2,0> color_conv_bgr24_to_rgb565; //----color_conv_bgr24_to_rgb565
285  typedef color_conv_rgb24_rgb565<0,2> color_conv_rgb24_to_rgb565; //----color_conv_rgb24_to_rgb565
286 
287 
288 
289  //-------------------------------------------------color_conv_rgb555_rgba32
290  template<int R, int G, int B, int A> class color_conv_rgb555_rgba32
291  {
292  public:
293  void operator () (int8u* dst,
294  const int8u* src,
295  unsigned width) const
296  {
297  do
298  {
299  int rgb = *(int16*)src;
300  dst[R] = (int8u)((rgb >> 7) & 0xF8);
301  dst[G] = (int8u)((rgb >> 2) & 0xF8);
302  dst[B] = (int8u)((rgb << 3) & 0xF8);
303  dst[A] = (int8u)(rgb >> 15);
304  src += 2;
305  dst += 4;
306  }
307  while(--width);
308  }
309  };
310 
311 
312  //------------------------------------------------------------------------
313  typedef color_conv_rgb555_rgba32<1,2,3,0> color_conv_rgb555_to_argb32; //----color_conv_rgb555_to_argb32
314  typedef color_conv_rgb555_rgba32<3,2,1,0> color_conv_rgb555_to_abgr32; //----color_conv_rgb555_to_abgr32
315  typedef color_conv_rgb555_rgba32<2,1,0,3> color_conv_rgb555_to_bgra32; //----color_conv_rgb555_to_bgra32
316  typedef color_conv_rgb555_rgba32<0,1,2,3> color_conv_rgb555_to_rgba32; //----color_conv_rgb555_to_rgba32
317 
318 
319  //------------------------------------------------color_conv_rgba32_rgb555
320  template<int R, int G, int B, int A> class color_conv_rgba32_rgb555
321  {
322  public:
323  void operator () (int8u* dst,
324  const int8u* src,
325  unsigned width) const
326  {
327  do
328  {
329  *(int16u*)dst = (int16u)(((unsigned(src[R]) << 7) & 0x7C00) |
330  ((unsigned(src[G]) << 2) & 0x3E0) |
331  ((unsigned(src[B]) >> 3)) |
332  ((unsigned(src[A]) << 8) & 0x8000));
333  src += 4;
334  dst += 2;
335  }
336  while(--width);
337  }
338  };
339 
340 
341  //------------------------------------------------------------------------
342  typedef color_conv_rgba32_rgb555<1,2,3,0> color_conv_argb32_to_rgb555; //----color_conv_argb32_to_rgb555
343  typedef color_conv_rgba32_rgb555<3,2,1,0> color_conv_abgr32_to_rgb555; //----color_conv_abgr32_to_rgb555
344  typedef color_conv_rgba32_rgb555<2,1,0,3> color_conv_bgra32_to_rgb555; //----color_conv_bgra32_to_rgb555
345  typedef color_conv_rgba32_rgb555<0,1,2,3> color_conv_rgba32_to_rgb555; //----color_conv_rgba32_to_rgb555
346 
347 
348 
349  //------------------------------------------------color_conv_rgb565_rgba32
350  template<int R, int G, int B, int A> class color_conv_rgb565_rgba32
351  {
352  public:
353  void operator () (int8u* dst,
354  const int8u* src,
355  unsigned width) const
356  {
357  do
358  {
359  int rgb = *(int16*)src;
360  dst[R] = (rgb >> 8) & 0xF8;
361  dst[G] = (rgb >> 3) & 0xFC;
362  dst[B] = (rgb << 3) & 0xF8;
363  dst[A] = 255;
364  src += 2;
365  dst += 4;
366  }
367  while(--width);
368  }
369  };
370 
371 
372  //------------------------------------------------------------------------
373  typedef color_conv_rgb565_rgba32<1,2,3,0> color_conv_rgb565_to_argb32; //----color_conv_rgb565_to_argb32
374  typedef color_conv_rgb565_rgba32<3,2,1,0> color_conv_rgb565_to_abgr32; //----color_conv_rgb565_to_abgr32
375  typedef color_conv_rgb565_rgba32<2,1,0,3> color_conv_rgb565_to_bgra32; //----color_conv_rgb565_to_bgra32
376  typedef color_conv_rgb565_rgba32<0,1,2,3> color_conv_rgb565_to_rgba32; //----color_conv_rgb565_to_rgba32
377 
378 
379  //------------------------------------------------color_conv_rgba32_rgb565
380  template<int R, int G, int B> class color_conv_rgba32_rgb565
381  {
382  public:
383  void operator () (int8u* dst,
384  const int8u* src,
385  unsigned width) const
386  {
387  do
388  {
389  *(int16u*)dst = (int16u)(((unsigned(src[R]) << 8) & 0xF800) |
390  ((unsigned(src[G]) << 3) & 0x7E0) |
391  ((unsigned(src[B]) >> 3)));
392  src += 4;
393  dst += 2;
394  }
395  while(--width);
396  }
397  };
398 
399 
400  //------------------------------------------------------------------------
401  typedef color_conv_rgba32_rgb565<1,2,3> color_conv_argb32_to_rgb565; //----color_conv_argb32_to_rgb565
402  typedef color_conv_rgba32_rgb565<3,2,1> color_conv_abgr32_to_rgb565; //----color_conv_abgr32_to_rgb565
403  typedef color_conv_rgba32_rgb565<2,1,0> color_conv_bgra32_to_rgb565; //----color_conv_bgra32_to_rgb565
404  typedef color_conv_rgba32_rgb565<0,1,2> color_conv_rgba32_to_rgb565; //----color_conv_rgba32_to_rgb565
405 
406 
407  //---------------------------------------------color_conv_rgb555_to_rgb565
409  {
410  public:
411  void operator () (int8u* dst,
412  const int8u* src,
413  unsigned width) const
414  {
415  do
416  {
417  unsigned rgb = *(int16u*)src;
418  *(int16u*)dst = (int16u)(((rgb << 1) & 0xFFC0) | (rgb & 0x1F));
419  src += 2;
420  dst += 2;
421  }
422  while(--width);
423  }
424  };
425 
426 
427  //----------------------------------------------color_conv_rgb565_to_rgb555
429  {
430  public:
431  void operator () (int8u* dst,
432  const int8u* src,
433  unsigned width) const
434  {
435  do
436  {
437  unsigned rgb = *(int16u*)src;
438  *(int16u*)dst = (int16u)(((rgb >> 1) & 0x7FE0) | (rgb & 0x1F));
439  src += 2;
440  dst += 2;
441  }
442  while(--width);
443  }
444  };
445 
446 
447  //------------------------------------------------------------------------
448  typedef color_conv_same<2> color_conv_rgb555_to_rgb555; //----color_conv_rgb555_to_rgb555
449  typedef color_conv_same<2> color_conv_rgb565_to_rgb565; //----color_conv_rgb565_to_rgb565
450 
451 
452  template<int R, int B> class color_conv_rgb24_gray8
453  {
454  public:
455  void operator () (int8u* dst,
456  const int8u* src,
457  unsigned width) const
458  {
459  do
460  {
461  *dst++ = (src[R]*77 + src[1]*150 + src[B]*29) >> 8;
462  src += 3;
463  }
464  while(--width);
465  }
466  };
467 
468  typedef color_conv_rgb24_gray8<0,2> color_conv_rgb24_to_gray8; //----color_conv_rgb24_to_gray8
469  typedef color_conv_rgb24_gray8<2,0> color_conv_bgr24_to_gray8; //----color_conv_bgr24_to_gray8
470 
471 
472 }
473 
474 
475 
476 #endif
Definition: agg_arc.cpp:24