1//----------------------------------------------------------------------------
2// Anti-Grain Geometry - Version 2.2
3// Copyright (C) 2002-2004 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
38namespace agg
39{
40
41    //-----------------------------------------------------color_conv_rgb24
42    class color_conv_rgb24
43    {
44    public:
45        void operator () (unsigned char* dst,
46                          const unsigned char* src,
47                          unsigned width) const
48        {
49            do
50            {
51                *dst++ = src[2];
52                *dst++ = src[1];
53                *dst++ = src[0];
54                src += 3;
55            }
56            while(--width);
57        }
58    };
59
60    typedef color_conv_rgb24 color_conv_rgb24_to_bgr24;
61    typedef color_conv_rgb24 color_conv_bgr24_to_rgb24;
62
63    typedef color_conv_same<3> color_conv_bgr24_to_bgr24;
64    typedef color_conv_same<3> color_conv_rgb24_to_rgb24;
65
66
67
68    //------------------------------------------------------color_conv_rgba32
69    template<int I1, int I2, int I3, int I4> class color_conv_rgba32
70    {
71    public:
72        void operator () (unsigned char* dst,
73                          const unsigned char* src,
74                          unsigned width) const
75        {
76            do
77            {
78                *dst++ = src[I1];
79                *dst++ = src[I2];
80                *dst++ = src[I3];
81                *dst++ = src[I4];
82                src += 4;
83            }
84            while(--width);
85        }
86    };
87
88
89    //------------------------------------------------------------------------
90    typedef color_conv_rgba32<0,3,2,1> color_conv_argb32_to_abgr32; //----color_conv_argb32_to_abgr32
91    typedef color_conv_rgba32<3,2,1,0> color_conv_argb32_to_bgra32; //----color_conv_argb32_to_bgra32
92    typedef color_conv_rgba32<1,2,3,0> color_conv_argb32_to_rgba32; //----color_conv_argb32_to_rgba32
93    typedef color_conv_rgba32<3,0,1,2> color_conv_bgra32_to_abgr32; //----color_conv_bgra32_to_abgr32
94    typedef color_conv_rgba32<3,2,1,0> color_conv_bgra32_to_argb32; //----color_conv_bgra32_to_argb32
95    typedef color_conv_rgba32<2,1,0,3> color_conv_bgra32_to_rgba32; //----color_conv_bgra32_to_rgba32
96    typedef color_conv_rgba32<3,2,1,0> color_conv_rgba32_to_abgr32; //----color_conv_rgba32_to_abgr32
97    typedef color_conv_rgba32<3,0,1,2> color_conv_rgba32_to_argb32; //----color_conv_rgba32_to_argb32
98    typedef color_conv_rgba32<2,1,0,3> color_conv_rgba32_to_bgra32; //----color_conv_rgba32_to_bgra32
99    typedef color_conv_rgba32<0,3,2,1> color_conv_abgr32_to_argb32; //----color_conv_abgr32_to_argb32
100    typedef color_conv_rgba32<1,2,3,0> color_conv_abgr32_to_bgra32; //----color_conv_abgr32_to_bgra32
101    typedef color_conv_rgba32<3,2,1,0> color_conv_abgr32_to_rgba32; //----color_conv_abgr32_to_rgba32
102
103    //------------------------------------------------------------------------
104    typedef color_conv_same<4> color_conv_rgba32_to_rgba32; //----color_conv_rgba32_to_rgba32
105    typedef color_conv_same<4> color_conv_argb32_to_argb32; //----color_conv_argb32_to_argb32
106    typedef color_conv_same<4> color_conv_bgra32_to_bgra32; //----color_conv_bgra32_to_bgra32
107    typedef color_conv_same<4> color_conv_abgr32_to_abgr32; //----color_conv_abgr32_to_abgr32
108
109
110    //--------------------------------------------color_conv_rgb24_rgba32
111    template<int I1, int I2, int I3, int A> class color_conv_rgb24_rgba32
112    {
113    public:
114        void operator () (unsigned char* dst,
115                          const unsigned char* src,
116                          unsigned width) const
117        {
118            do
119            {
120                dst[I1] = *src++;
121                dst[I2] = *src++;
122                dst[I3] = *src++;
123                dst[A]  = 255;
124                dst += 4;
125            }
126            while(--width);
127        }
128    };
129
130
131    //------------------------------------------------------------------------
132    typedef color_conv_rgb24_rgba32<1,2,3,0> color_conv_rgb24_to_argb32; //----color_conv_rgb24_to_argb32
133    typedef color_conv_rgb24_rgba32<3,2,1,0> color_conv_rgb24_to_abgr32; //----color_conv_rgb24_to_abgr32
134    typedef color_conv_rgb24_rgba32<2,1,0,3> color_conv_rgb24_to_bgra32; //----color_conv_rgb24_to_bgra32
135    typedef color_conv_rgb24_rgba32<0,1,2,3> color_conv_rgb24_to_rgba32; //----color_conv_rgb24_to_rgba32
136    typedef color_conv_rgb24_rgba32<3,2,1,0> color_conv_bgr24_to_argb32; //----color_conv_bgr24_to_argb32
137    typedef color_conv_rgb24_rgba32<1,2,3,0> color_conv_bgr24_to_abgr32; //----color_conv_bgr24_to_abgr32
138    typedef color_conv_rgb24_rgba32<0,1,2,3> color_conv_bgr24_to_bgra32; //----color_conv_bgr24_to_bgra32
139    typedef color_conv_rgb24_rgba32<2,1,0,3> color_conv_bgr24_to_rgba32; //----color_conv_bgr24_to_rgba32
140
141
142
143    //-------------------------------------------------color_conv_rgba32_rgb24
144    template<int I1, int I2, int I3> class color_conv_rgba32_rgb24
145    {
146    public:
147        void operator () (unsigned char* dst,
148                          const unsigned char* src,
149                          unsigned width) const
150        {
151            do
152            {
153                *dst++ = src[I1];
154                *dst++ = src[I2];
155                *dst++ = src[I3];
156                src += 4;
157            }
158            while(--width);
159        }
160    };
161
162
163
164    //------------------------------------------------------------------------
165    typedef color_conv_rgba32_rgb24<1,2,3> color_conv_argb32_to_rgb24; //----color_conv_argb32_to_rgb24
166    typedef color_conv_rgba32_rgb24<3,2,1> color_conv_abgr32_to_rgb24; //----color_conv_abgr32_to_rgb24
167    typedef color_conv_rgba32_rgb24<2,1,0> color_conv_bgra32_to_rgb24; //----color_conv_bgra32_to_rgb24
168    typedef color_conv_rgba32_rgb24<0,1,2> color_conv_rgba32_to_rgb24; //----color_conv_rgba32_to_rgb24
169    typedef color_conv_rgba32_rgb24<3,2,1> color_conv_argb32_to_bgr24; //----color_conv_argb32_to_bgr24
170    typedef color_conv_rgba32_rgb24<1,2,3> color_conv_abgr32_to_bgr24; //----color_conv_abgr32_to_bgr24
171    typedef color_conv_rgba32_rgb24<0,1,2> color_conv_bgra32_to_bgr24; //----color_conv_bgra32_to_bgr24
172    typedef color_conv_rgba32_rgb24<2,1,0> color_conv_rgba32_to_bgr24; //----color_conv_rgba32_to_bgr24
173
174
175    //------------------------------------------------color_conv_rgb555_rgb24
176    template<int R, int B> class color_conv_rgb555_rgb24
177    {
178    public:
179        void operator () (unsigned char* dst,
180                          const unsigned char* src,
181                          unsigned width) const
182        {
183            do
184            {
185                unsigned rgb = *(int16u*)src;
186                dst[R] = (unsigned char)((rgb >> 7) & 0xF8);
187                dst[1] = (unsigned char)((rgb >> 2) & 0xF8);
188                dst[B] = (unsigned char)((rgb << 3) & 0xF8);
189                src += 2;
190                dst += 3;
191            }
192            while(--width);
193        }
194    };
195
196
197    //------------------------------------------------------------------------
198    typedef color_conv_rgb555_rgb24<2,0> color_conv_rgb555_to_bgr24; //----color_conv_rgb555_to_bgr24
199    typedef color_conv_rgb555_rgb24<0,2> color_conv_rgb555_to_rgb24; //----color_conv_rgb555_to_rgb24
200
201
202    //-------------------------------------------------color_conv_rgb24_rgb555
203    template<int R, int B> class color_conv_rgb24_rgb555
204    {
205    public:
206        void operator () (unsigned char* dst,
207                          const unsigned char* src,
208                          unsigned width) const
209        {
210            do
211            {
212                *(int16u*)dst = (int16u)(((unsigned(src[R]) << 7) & 0x7C00) |
213                                         ((unsigned(src[1]) << 2) & 0x3E0)  |
214                                         ((unsigned(src[B]) >> 3)));
215                src += 3;
216                dst += 2;
217            }
218            while(--width);
219        }
220    };
221
222
223    //------------------------------------------------------------------------
224    typedef color_conv_rgb24_rgb555<2,0> color_conv_bgr24_to_rgb555; //----color_conv_bgr24_to_rgb555
225    typedef color_conv_rgb24_rgb555<0,2> color_conv_rgb24_to_rgb555; //----color_conv_rgb24_to_rgb555
226
227
228    //-------------------------------------------------color_conv_rgb565_rgb24
229    template<int R, int B> class color_conv_rgb565_rgb24
230    {
231    public:
232        void operator () (unsigned char* dst,
233                          const unsigned char* src,
234                          unsigned width) const
235        {
236            do
237            {
238                unsigned rgb = *(int16u*)src;
239                dst[R] = (rgb >> 8) & 0xF8;
240                dst[1] = (rgb >> 3) & 0xFC;
241                dst[B] = (rgb << 3) & 0xF8;
242                src += 2;
243                dst += 3;
244            }
245            while(--width);
246        }
247    };
248
249
250    //------------------------------------------------------------------------
251    typedef color_conv_rgb565_rgb24<2,0> color_conv_rgb565_to_bgr24; //----color_conv_rgb565_to_bgr24
252    typedef color_conv_rgb565_rgb24<0,2> color_conv_rgb565_to_rgb24; //----color_conv_rgb565_to_rgb24
253
254
255    //-------------------------------------------------color_conv_rgb24_rgb565
256    template<int R, int B> class color_conv_rgb24_rgb565
257    {
258    public:
259        void operator () (unsigned char* dst,
260                          const unsigned char* src,
261                          unsigned width) const
262        {
263            do
264            {
265                *(int16u*)dst = (int16u)(((unsigned(src[R]) << 8) & 0xF800) |
266                                         ((unsigned(src[1]) << 3) & 0x7E0)  |
267                                         ((unsigned(src[B]) >> 3)));
268                src += 3;
269                dst += 2;
270            }
271            while(--width);
272        }
273    };
274
275
276    //------------------------------------------------------------------------
277    typedef color_conv_rgb24_rgb565<2,0> color_conv_bgr24_to_rgb565; //----color_conv_bgr24_to_rgb565
278    typedef color_conv_rgb24_rgb565<0,2> color_conv_rgb24_to_rgb565; //----color_conv_rgb24_to_rgb565
279
280
281
282    //-------------------------------------------------color_conv_rgb555_rgba32
283    template<int R, int G, int B, int A> class color_conv_rgb555_rgba32
284    {
285    public:
286        void operator () (unsigned char* dst,
287                          const unsigned char* src,
288                          unsigned width) const
289        {
290            do
291            {
292                int rgb = *(int16*)src;
293                dst[R] = (int8u)((rgb >> 7) & 0xF8);
294                dst[G] = (int8u)((rgb >> 2) & 0xF8);
295                dst[B] = (int8u)((rgb << 3) & 0xF8);
296                dst[A] = (int8u)(rgb >> 15);
297                src += 2;
298                dst += 4;
299            }
300            while(--width);
301        }
302    };
303
304
305    //------------------------------------------------------------------------
306    typedef color_conv_rgb555_rgba32<1,2,3,0> color_conv_rgb555_to_argb32; //----color_conv_rgb555_to_argb32
307    typedef color_conv_rgb555_rgba32<3,2,1,0> color_conv_rgb555_to_abgr32; //----color_conv_rgb555_to_abgr32
308    typedef color_conv_rgb555_rgba32<2,1,0,3> color_conv_rgb555_to_bgra32; //----color_conv_rgb555_to_bgra32
309    typedef color_conv_rgb555_rgba32<0,1,2,3> color_conv_rgb555_to_rgba32; //----color_conv_rgb555_to_rgba32
310
311
312    //------------------------------------------------color_conv_rgba32_rgb555
313    template<int R, int G, int B, int A> class color_conv_rgba32_rgb555
314    {
315    public:
316        void operator () (unsigned char* dst,
317                          const unsigned char* src,
318                          unsigned width) const
319        {
320            do
321            {
322                *(int16u*)dst = (int16u)(((unsigned(src[R]) << 7) & 0x7C00) |
323                                         ((unsigned(src[G]) << 2) & 0x3E0)  |
324                                         ((unsigned(src[B]) >> 3)) |
325                                         ((unsigned(src[A]) << 8) & 0x8000));
326                src += 4;
327                dst += 2;
328            }
329            while(--width);
330        }
331    };
332
333
334    //------------------------------------------------------------------------
335    typedef color_conv_rgba32_rgb555<1,2,3,0> color_conv_argb32_to_rgb555; //----color_conv_argb32_to_rgb555
336    typedef color_conv_rgba32_rgb555<3,2,1,0> color_conv_abgr32_to_rgb555; //----color_conv_abgr32_to_rgb555
337    typedef color_conv_rgba32_rgb555<2,1,0,3> color_conv_bgra32_to_rgb555; //----color_conv_bgra32_to_rgb555
338    typedef color_conv_rgba32_rgb555<0,1,2,3> color_conv_rgba32_to_rgb555; //----color_conv_rgba32_to_rgb555
339
340
341
342    //------------------------------------------------color_conv_rgb565_rgba32
343    template<int R, int G, int B, int A> class color_conv_rgb565_rgba32
344    {
345    public:
346        void operator () (unsigned char* dst,
347                          const unsigned char* src,
348                          unsigned width) const
349        {
350            do
351            {
352                int rgb = *(int16*)src;
353                dst[R] = (rgb >> 8) & 0xF8;
354                dst[G] = (rgb >> 3) & 0xFC;
355                dst[B] = (rgb << 3) & 0xF8;
356                dst[A] = 255;
357                src += 2;
358                dst += 4;
359            }
360            while(--width);
361        }
362    };
363
364
365    //------------------------------------------------------------------------
366    typedef color_conv_rgb565_rgba32<1,2,3,0> color_conv_rgb565_to_argb32; //----color_conv_rgb565_to_argb32
367    typedef color_conv_rgb565_rgba32<3,2,1,0> color_conv_rgb565_to_abgr32; //----color_conv_rgb565_to_abgr32
368    typedef color_conv_rgb565_rgba32<2,1,0,3> color_conv_rgb565_to_bgra32; //----color_conv_rgb565_to_bgra32
369    typedef color_conv_rgb565_rgba32<0,1,2,3> color_conv_rgb565_to_rgba32; //----color_conv_rgb565_to_rgba32
370
371
372    //------------------------------------------------color_conv_rgba32_rgb565
373    template<int R, int G, int B> class color_conv_rgba32_rgb565
374    {
375    public:
376        void operator () (unsigned char* dst,
377                          const unsigned char* src,
378                          unsigned width) const
379        {
380            do
381            {
382                *(int16u*)dst = (int16u)(((unsigned(src[R]) << 8) & 0xF800) |
383                                         ((unsigned(src[G]) << 3) & 0x7E0)  |
384                                         ((unsigned(src[B]) >> 3)));
385                src += 4;
386                dst += 2;
387            }
388            while(--width);
389        }
390    };
391
392
393    //------------------------------------------------------------------------
394    typedef color_conv_rgba32_rgb565<1,2,3> color_conv_argb32_to_rgb565; //----color_conv_argb32_to_rgb565
395    typedef color_conv_rgba32_rgb565<3,2,1> color_conv_abgr32_to_rgb565; //----color_conv_abgr32_to_rgb565
396    typedef color_conv_rgba32_rgb565<2,1,0> color_conv_bgra32_to_rgb565; //----color_conv_bgra32_to_rgb565
397    typedef color_conv_rgba32_rgb565<0,1,2> color_conv_rgba32_to_rgb565; //----color_conv_rgba32_to_rgb565
398
399
400    //---------------------------------------------color_conv_rgb555_to_rgb565
401    class color_conv_rgb555_to_rgb565
402    {
403    public:
404        void operator () (unsigned char* dst,
405                          const unsigned char* src,
406                          unsigned width) const
407        {
408            do
409            {
410                unsigned rgb = *(int16u*)src;
411                *(int16u*)dst = (int16u)(((rgb << 1) & 0xFFC0) | (rgb & 0x1F));
412                src += 2;
413                dst += 2;
414            }
415            while(--width);
416        }
417    };
418
419
420    //----------------------------------------------color_conv_rgb565_to_rgb555
421    class color_conv_rgb565_to_rgb555
422    {
423    public:
424        void operator () (unsigned char* dst,
425                          const unsigned char* src,
426                          unsigned width) const
427        {
428            do
429            {
430                unsigned rgb = *(int16u*)src;
431                *(int16u*)dst = (int16u)(((rgb >> 1) & 0x7FE0) | (rgb & 0x1F));
432                src += 2;
433                dst += 2;
434            }
435            while(--width);
436        }
437    };
438
439
440    //------------------------------------------------------------------------
441    typedef color_conv_same<2> color_conv_rgb555_to_rgb555; //----color_conv_rgb555_to_rgb555
442    typedef color_conv_same<2> color_conv_rgb565_to_rgb565; //----color_conv_rgb565_to_rgb565
443
444
445}
446
447
448
449#endif
450