Fix compilation with MMX disabled, the mmx_null and mmx_one constants don't need to
[ffmpeg.git] / libswscale / rgb2rgb.c
1 /*
2  *
3  *  rgb2rgb.c, Software RGB to RGB convertor
4  *  pluralize by Software PAL8 to RGB convertor
5  *               Software YUV to YUV convertor
6  *               Software YUV to RGB convertor
7  *  Written by Nick Kurshev.
8  *  palette & yuv & runtime cpu stuff by Michael (michaelni@gmx.at) (under GPL)
9  */
10 #include <inttypes.h>
11 #include "config.h"
12 #include "rgb2rgb.h"
13 #include "swscale.h"
14 #include "swscale_internal.h"
15 #include "x86_cpu.h"
16 #include "bswap.h"
17 #ifdef USE_FASTMEMCPY
18 #include "libvo/fastmemcpy.h"
19 #endif
20
21 #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
22
23 void (*rgb24to32)(const uint8_t *src,uint8_t *dst,long src_size);
24 void (*rgb24to16)(const uint8_t *src,uint8_t *dst,long src_size);
25 void (*rgb24to15)(const uint8_t *src,uint8_t *dst,long src_size);
26 void (*rgb32to24)(const uint8_t *src,uint8_t *dst,long src_size);
27 void (*rgb32to16)(const uint8_t *src,uint8_t *dst,long src_size);
28 void (*rgb32to15)(const uint8_t *src,uint8_t *dst,long src_size);
29 void (*rgb15to16)(const uint8_t *src,uint8_t *dst,long src_size);
30 void (*rgb15to24)(const uint8_t *src,uint8_t *dst,long src_size);
31 void (*rgb15to32)(const uint8_t *src,uint8_t *dst,long src_size);
32 void (*rgb16to15)(const uint8_t *src,uint8_t *dst,long src_size);
33 void (*rgb16to24)(const uint8_t *src,uint8_t *dst,long src_size);
34 void (*rgb16to32)(const uint8_t *src,uint8_t *dst,long src_size);
35 //void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
36 void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
37 void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
38 void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
39 void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
40 //void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
41 void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
42 void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
43
44 void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
45         long width, long height,
46         long lumStride, long chromStride, long dstStride);
47 void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
48         long width, long height,
49         long lumStride, long chromStride, long dstStride);
50 void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
51         long width, long height,
52         long lumStride, long chromStride, long dstStride);
53 void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
54         long width, long height,
55         long lumStride, long chromStride, long srcStride);
56 void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
57         long width, long height,
58         long lumStride, long chromStride, long srcStride);
59 void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
60         long srcStride, long dstStride);
61 void (*interleaveBytes)(uint8_t *src1, uint8_t *src2, uint8_t *dst,
62                             long width, long height, long src1Stride,
63                             long src2Stride, long dstStride);
64 void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
65                         uint8_t *dst1, uint8_t *dst2,
66                         long width, long height,
67                         long srcStride1, long srcStride2,
68                         long dstStride1, long dstStride2);
69 void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
70                         uint8_t *dst,
71                         long width, long height,
72                         long srcStride1, long srcStride2,
73                         long srcStride3, long dstStride);
74
75 #if defined(ARCH_X86) || defined(ARCH_X86_64)
76 static const uint64_t mmx_null  __attribute__((aligned(8))) = 0x0000000000000000ULL;
77 static const uint64_t mmx_one   __attribute__((aligned(8))) = 0xFFFFFFFFFFFFFFFFULL;
78 static const uint64_t mask32b  attribute_used __attribute__((aligned(8))) = 0x000000FF000000FFULL;
79 static const uint64_t mask32g  attribute_used __attribute__((aligned(8))) = 0x0000FF000000FF00ULL;
80 static const uint64_t mask32r  attribute_used __attribute__((aligned(8))) = 0x00FF000000FF0000ULL;
81 static const uint64_t mask32   __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
82 static const uint64_t mask3216br __attribute__((aligned(8)))=0x00F800F800F800F8ULL;
83 static const uint64_t mask3216g  __attribute__((aligned(8)))=0x0000FC000000FC00ULL;
84 static const uint64_t mask3215g  __attribute__((aligned(8)))=0x0000F8000000F800ULL;
85 static const uint64_t mul3216  __attribute__((aligned(8))) = 0x2000000420000004ULL;
86 static const uint64_t mul3215  __attribute__((aligned(8))) = 0x2000000820000008ULL;
87 static const uint64_t mask24b  attribute_used __attribute__((aligned(8))) = 0x00FF0000FF0000FFULL;
88 static const uint64_t mask24g  attribute_used __attribute__((aligned(8))) = 0xFF0000FF0000FF00ULL;
89 static const uint64_t mask24r  attribute_used __attribute__((aligned(8))) = 0x0000FF0000FF0000ULL;
90 static const uint64_t mask24l  __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
91 static const uint64_t mask24h  __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
92 static const uint64_t mask24hh  __attribute__((aligned(8))) = 0xffff000000000000ULL;
93 static const uint64_t mask24hhh  __attribute__((aligned(8))) = 0xffffffff00000000ULL;
94 static const uint64_t mask24hhhh  __attribute__((aligned(8))) = 0xffffffffffff0000ULL;
95 static const uint64_t mask15b  __attribute__((aligned(8))) = 0x001F001F001F001FULL; /* 00000000 00011111  xxB */
96 static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000  RGx */
97 static const uint64_t mask15s  __attribute__((aligned(8))) = 0xFFE0FFE0FFE0FFE0ULL;
98 static const uint64_t mask15g  __attribute__((aligned(8))) = 0x03E003E003E003E0ULL;
99 static const uint64_t mask15r  __attribute__((aligned(8))) = 0x7C007C007C007C00ULL;
100 #define mask16b mask15b
101 static const uint64_t mask16g  __attribute__((aligned(8))) = 0x07E007E007E007E0ULL;
102 static const uint64_t mask16r  __attribute__((aligned(8))) = 0xF800F800F800F800ULL;
103 static const uint64_t red_16mask  __attribute__((aligned(8))) = 0x0000f8000000f800ULL;
104 static const uint64_t green_16mask __attribute__((aligned(8)))= 0x000007e0000007e0ULL;
105 static const uint64_t blue_16mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
106 static const uint64_t red_15mask  __attribute__((aligned(8))) = 0x00007c000000f800ULL;
107 static const uint64_t green_15mask __attribute__((aligned(8)))= 0x000003e0000007e0ULL;
108 static const uint64_t blue_15mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
109
110 #ifdef FAST_BGR2YV12
111 static const uint64_t bgr2YCoeff  attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
112 static const uint64_t bgr2UCoeff  attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
113 static const uint64_t bgr2VCoeff  attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
114 #else
115 static const uint64_t bgr2YCoeff  attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
116 static const uint64_t bgr2UCoeff  attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
117 static const uint64_t bgr2VCoeff  attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
118 #endif
119 static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
120 static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8)))= 0x8080808080808080ULL;
121 static const uint64_t w1111       attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
122
123 #if 0
124 static volatile uint64_t __attribute__((aligned(8))) b5Dither;
125 static volatile uint64_t __attribute__((aligned(8))) g5Dither;
126 static volatile uint64_t __attribute__((aligned(8))) g6Dither;
127 static volatile uint64_t __attribute__((aligned(8))) r5Dither;
128
129 static uint64_t __attribute__((aligned(8))) dither4[2]={
130         0x0103010301030103LL,
131         0x0200020002000200LL,};
132
133 static uint64_t __attribute__((aligned(8))) dither8[2]={
134         0x0602060206020602LL,
135         0x0004000400040004LL,};
136 #endif
137 #endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
138
139 #define RGB2YUV_SHIFT 8
140 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
141 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
142 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
143 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
144 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
145 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
146 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
147 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
148 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
149
150 //Note: we have C, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
151 //Plain C versions
152 #undef HAVE_MMX
153 #undef HAVE_MMX2
154 #undef HAVE_3DNOW
155 #undef HAVE_SSE2
156 #define RENAME(a) a ## _C
157 #include "rgb2rgb_template.c"
158
159 #if defined(ARCH_X86) || defined(ARCH_X86_64)
160
161 //MMX versions
162 #undef RENAME
163 #define HAVE_MMX
164 #undef HAVE_MMX2
165 #undef HAVE_3DNOW
166 #undef HAVE_SSE2
167 #define RENAME(a) a ## _MMX
168 #include "rgb2rgb_template.c"
169
170 //MMX2 versions
171 #undef RENAME
172 #define HAVE_MMX
173 #define HAVE_MMX2
174 #undef HAVE_3DNOW
175 #undef HAVE_SSE2
176 #define RENAME(a) a ## _MMX2
177 #include "rgb2rgb_template.c"
178
179 //3DNOW versions
180 #undef RENAME
181 #define HAVE_MMX
182 #undef HAVE_MMX2
183 #define HAVE_3DNOW
184 #undef HAVE_SSE2
185 #define RENAME(a) a ## _3DNOW
186 #include "rgb2rgb_template.c"
187
188 #endif //ARCH_X86 || ARCH_X86_64
189
190 /*
191  rgb15->rgb16 Original by Strepto/Astral
192  ported to gcc & bugfixed : A'rpi
193  MMX2, 3DNOW optimization by Nick Kurshev
194  32bit c version, and and&add trick by Michael Niedermayer
195 */
196
197 void sws_rgb2rgb_init(int flags){
198 #if defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX)
199         if(flags & SWS_CPU_CAPS_MMX2){
200                 rgb15to16= rgb15to16_MMX2;
201                 rgb15to24= rgb15to24_MMX2;
202                 rgb15to32= rgb15to32_MMX2;
203                 rgb16to24= rgb16to24_MMX2;
204                 rgb16to32= rgb16to32_MMX2;
205                 rgb16to15= rgb16to15_MMX2;
206                 rgb24to16= rgb24to16_MMX2;
207                 rgb24to15= rgb24to15_MMX2;
208                 rgb24to32= rgb24to32_MMX2;
209                 rgb32to16= rgb32to16_MMX2;
210                 rgb32to15= rgb32to15_MMX2;
211                 rgb32to24= rgb32to24_MMX2;
212                 rgb24tobgr15= rgb24tobgr15_MMX2;
213                 rgb24tobgr16= rgb24tobgr16_MMX2;
214                 rgb24tobgr24= rgb24tobgr24_MMX2;
215                 rgb32tobgr32= rgb32tobgr32_MMX2;
216                 rgb32tobgr16= rgb32tobgr16_MMX2;
217                 rgb32tobgr15= rgb32tobgr15_MMX2;
218                 yv12toyuy2= yv12toyuy2_MMX2;
219                 yv12touyvy= yv12touyvy_MMX2;
220                 yuv422ptoyuy2= yuv422ptoyuy2_MMX2;
221                 yuy2toyv12= yuy2toyv12_MMX2;
222 //              uyvytoyv12= uyvytoyv12_MMX2;
223 //              yvu9toyv12= yvu9toyv12_MMX2;
224                 planar2x= planar2x_MMX2;
225                 rgb24toyv12= rgb24toyv12_MMX2;
226                 interleaveBytes= interleaveBytes_MMX2;
227                 vu9_to_vu12= vu9_to_vu12_MMX2;
228                 yvu9_to_yuy2= yvu9_to_yuy2_MMX2;
229         }else if(flags & SWS_CPU_CAPS_3DNOW){
230                 rgb15to16= rgb15to16_3DNOW;
231                 rgb15to24= rgb15to24_3DNOW;
232                 rgb15to32= rgb15to32_3DNOW;
233                 rgb16to24= rgb16to24_3DNOW;
234                 rgb16to32= rgb16to32_3DNOW;
235                 rgb16to15= rgb16to15_3DNOW;
236                 rgb24to16= rgb24to16_3DNOW;
237                 rgb24to15= rgb24to15_3DNOW;
238                 rgb24to32= rgb24to32_3DNOW;
239                 rgb32to16= rgb32to16_3DNOW;
240                 rgb32to15= rgb32to15_3DNOW;
241                 rgb32to24= rgb32to24_3DNOW;
242                 rgb24tobgr15= rgb24tobgr15_3DNOW;
243                 rgb24tobgr16= rgb24tobgr16_3DNOW;
244                 rgb24tobgr24= rgb24tobgr24_3DNOW;
245                 rgb32tobgr32= rgb32tobgr32_3DNOW;
246                 rgb32tobgr16= rgb32tobgr16_3DNOW;
247                 rgb32tobgr15= rgb32tobgr15_3DNOW;
248                 yv12toyuy2= yv12toyuy2_3DNOW;
249                 yv12touyvy= yv12touyvy_3DNOW;
250                 yuv422ptoyuy2= yuv422ptoyuy2_3DNOW;
251                 yuy2toyv12= yuy2toyv12_3DNOW;
252 //              uyvytoyv12= uyvytoyv12_3DNOW;
253 //              yvu9toyv12= yvu9toyv12_3DNOW;
254                 planar2x= planar2x_3DNOW;
255                 rgb24toyv12= rgb24toyv12_3DNOW;
256                 interleaveBytes= interleaveBytes_3DNOW;
257                 vu9_to_vu12= vu9_to_vu12_3DNOW;
258                 yvu9_to_yuy2= yvu9_to_yuy2_3DNOW;
259         }else if(flags & SWS_CPU_CAPS_MMX){
260                 rgb15to16= rgb15to16_MMX;
261                 rgb15to24= rgb15to24_MMX;
262                 rgb15to32= rgb15to32_MMX;
263                 rgb16to24= rgb16to24_MMX;
264                 rgb16to32= rgb16to32_MMX;
265                 rgb16to15= rgb16to15_MMX;
266                 rgb24to16= rgb24to16_MMX;
267                 rgb24to15= rgb24to15_MMX;
268                 rgb24to32= rgb24to32_MMX;
269                 rgb32to16= rgb32to16_MMX;
270                 rgb32to15= rgb32to15_MMX;
271                 rgb32to24= rgb32to24_MMX;
272                 rgb24tobgr15= rgb24tobgr15_MMX;
273                 rgb24tobgr16= rgb24tobgr16_MMX;
274                 rgb24tobgr24= rgb24tobgr24_MMX;
275                 rgb32tobgr32= rgb32tobgr32_MMX;
276                 rgb32tobgr16= rgb32tobgr16_MMX;
277                 rgb32tobgr15= rgb32tobgr15_MMX;
278                 yv12toyuy2= yv12toyuy2_MMX;
279                 yv12touyvy= yv12touyvy_MMX;
280                 yuv422ptoyuy2= yuv422ptoyuy2_MMX;
281                 yuy2toyv12= yuy2toyv12_MMX;
282 //              uyvytoyv12= uyvytoyv12_MMX;
283 //              yvu9toyv12= yvu9toyv12_MMX;
284                 planar2x= planar2x_MMX;
285                 rgb24toyv12= rgb24toyv12_MMX;
286                 interleaveBytes= interleaveBytes_MMX;
287                 vu9_to_vu12= vu9_to_vu12_MMX;
288                 yvu9_to_yuy2= yvu9_to_yuy2_MMX;
289         }else
290 #endif /* defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX) */
291         {
292                 rgb15to16= rgb15to16_C;
293                 rgb15to24= rgb15to24_C;
294                 rgb15to32= rgb15to32_C;
295                 rgb16to24= rgb16to24_C;
296                 rgb16to32= rgb16to32_C;
297                 rgb16to15= rgb16to15_C;
298                 rgb24to16= rgb24to16_C;
299                 rgb24to15= rgb24to15_C;
300                 rgb24to32= rgb24to32_C;
301                 rgb32to16= rgb32to16_C;
302                 rgb32to15= rgb32to15_C;
303                 rgb32to24= rgb32to24_C;
304                 rgb24tobgr15= rgb24tobgr15_C;
305                 rgb24tobgr16= rgb24tobgr16_C;
306                 rgb24tobgr24= rgb24tobgr24_C;
307                 rgb32tobgr32= rgb32tobgr32_C;
308                 rgb32tobgr16= rgb32tobgr16_C;
309                 rgb32tobgr15= rgb32tobgr15_C;
310                 yv12toyuy2= yv12toyuy2_C;
311                 yv12touyvy= yv12touyvy_C;
312                 yuv422ptoyuy2= yuv422ptoyuy2_C;
313                 yuy2toyv12= yuy2toyv12_C;
314 //              uyvytoyv12= uyvytoyv12_C;
315 //              yvu9toyv12= yvu9toyv12_C;
316                 planar2x= planar2x_C;
317                 rgb24toyv12= rgb24toyv12_C;
318                 interleaveBytes= interleaveBytes_C;
319                 vu9_to_vu12= vu9_to_vu12_C;
320                 yvu9_to_yuy2= yvu9_to_yuy2_C;
321         }
322 }
323
324 /**
325  * Pallete is assumed to contain bgr32
326  */
327 void palette8torgb32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
328 {
329         long i;
330
331 /*
332         for(i=0; i<num_pixels; i++)
333                 ((unsigned *)dst)[i] = ((unsigned *)palette)[ src[i] ];
334 */
335
336         for(i=0; i<num_pixels; i++)
337         {
338                 #ifdef WORDS_BIGENDIAN
339                         dst[3]= palette[ src[i]*4+2 ];
340                         dst[2]= palette[ src[i]*4+1 ];
341                         dst[1]= palette[ src[i]*4+0 ];
342                 #else
343                 //FIXME slow?
344                         dst[0]= palette[ src[i]*4+2 ];
345                         dst[1]= palette[ src[i]*4+1 ];
346                         dst[2]= palette[ src[i]*4+0 ];
347                         //dst[3]= 0; /* do we need this cleansing? */
348                 #endif
349                 dst+= 4;
350         }
351 }
352
353 void palette8tobgr32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
354 {
355         long i;
356         for(i=0; i<num_pixels; i++)
357         {
358                 #ifdef WORDS_BIGENDIAN
359                         dst[3]= palette[ src[i]*4+0 ];
360                         dst[2]= palette[ src[i]*4+1 ];
361                         dst[1]= palette[ src[i]*4+2 ];
362                 #else
363                         //FIXME slow?
364                         dst[0]= palette[ src[i]*4+0 ];
365                         dst[1]= palette[ src[i]*4+1 ];
366                         dst[2]= palette[ src[i]*4+2 ];
367                         //dst[3]= 0; /* do we need this cleansing? */
368                 #endif
369                 
370                 dst+= 4;
371         }
372 }
373
374 /**
375  * Pallete is assumed to contain bgr32
376  */
377 void palette8torgb24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
378 {
379         long i;
380 /*
381         writes 1 byte o much and might cause alignment issues on some architectures?
382         for(i=0; i<num_pixels; i++)
383                 ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
384 */
385         for(i=0; i<num_pixels; i++)
386         {
387                 //FIXME slow?
388                 dst[0]= palette[ src[i]*4+2 ];
389                 dst[1]= palette[ src[i]*4+1 ];
390                 dst[2]= palette[ src[i]*4+0 ];
391                 dst+= 3;
392         }
393 }
394
395 void palette8tobgr24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
396 {
397         long i;
398 /*
399         writes 1 byte o much and might cause alignment issues on some architectures?
400         for(i=0; i<num_pixels; i++)
401                 ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
402 */
403         for(i=0; i<num_pixels; i++)
404         {
405                 //FIXME slow?
406                 dst[0]= palette[ src[i]*4+0 ];
407                 dst[1]= palette[ src[i]*4+1 ];
408                 dst[2]= palette[ src[i]*4+2 ];
409                 dst+= 3;
410         }
411 }
412
413 /**
414  * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
415  */
416 void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
417 {
418         long i;
419         for(i=0; i<num_pixels; i++)
420                 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
421 }
422 void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
423 {
424         long i;
425         for(i=0; i<num_pixels; i++)
426                 ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
427 }
428
429 /**
430  * Pallete is assumed to contain bgr15, see rgb32to15 to convert the palette
431  */
432 void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
433 {
434         long i;
435         for(i=0; i<num_pixels; i++)
436                 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
437 }
438 void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
439 {
440         long i;
441         for(i=0; i<num_pixels; i++)
442                 ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
443 }
444
445 void rgb32tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
446 {
447         long i;
448         long num_pixels = src_size >> 2;
449         for(i=0; i<num_pixels; i++)
450         {
451                 #ifdef WORDS_BIGENDIAN
452                         /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
453                         dst[3*i + 0] = src[4*i + 1];
454                         dst[3*i + 1] = src[4*i + 2];
455                         dst[3*i + 2] = src[4*i + 3];
456                 #else
457                         dst[3*i + 0] = src[4*i + 2];
458                         dst[3*i + 1] = src[4*i + 1];
459                         dst[3*i + 2] = src[4*i + 0];
460                 #endif
461         }
462 }
463
464 void rgb24tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
465 {
466         long i;
467         for(i=0; 3*i<src_size; i++)
468         {
469                 #ifdef WORDS_BIGENDIAN
470                         /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
471                         dst[4*i + 0] = 0;
472                         dst[4*i + 1] = src[3*i + 0];
473                         dst[4*i + 2] = src[3*i + 1];
474                         dst[4*i + 3] = src[3*i + 2];
475                 #else
476                         dst[4*i + 0] = src[3*i + 2];
477                         dst[4*i + 1] = src[3*i + 1];
478                         dst[4*i + 2] = src[3*i + 0];
479                         dst[4*i + 3] = 0;
480                 #endif
481         }
482 }
483
484 void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
485 {
486         const uint16_t *end;
487         uint8_t *d = (uint8_t *)dst;
488         const uint16_t *s = (uint16_t *)src;
489         end = s + src_size/2;
490         while(s < end)
491         {
492                 register uint16_t bgr;
493                 bgr = *s++;
494                 #ifdef WORDS_BIGENDIAN
495                         *d++ = 0;
496                         *d++ = (bgr&0x1F)<<3;
497                         *d++ = (bgr&0x7E0)>>3;
498                         *d++ = (bgr&0xF800)>>8;
499                 #else
500                         *d++ = (bgr&0xF800)>>8;
501                         *d++ = (bgr&0x7E0)>>3;
502                         *d++ = (bgr&0x1F)<<3;
503                         *d++ = 0;
504                 #endif
505         }
506 }
507
508 void rgb16tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
509 {
510         const uint16_t *end;
511         uint8_t *d = (uint8_t *)dst;
512         const uint16_t *s = (const uint16_t *)src;
513         end = s + src_size/2;
514         while(s < end)
515         {
516                 register uint16_t bgr;
517                 bgr = *s++;
518                 *d++ = (bgr&0xF800)>>8;
519                 *d++ = (bgr&0x7E0)>>3;
520                 *d++ = (bgr&0x1F)<<3;
521         }
522 }
523
524 void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
525 {
526         long i;
527         long num_pixels = src_size >> 1;
528         
529         for(i=0; i<num_pixels; i++)
530         {
531             unsigned b,g,r;
532             register uint16_t rgb;
533             rgb = src[2*i];
534             r = rgb&0x1F;
535             g = (rgb&0x7E0)>>5;
536             b = (rgb&0xF800)>>11;
537             dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
538         }
539 }
540
541 void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
542 {
543         long i;
544         long num_pixels = src_size >> 1;
545         
546         for(i=0; i<num_pixels; i++)
547         {
548             unsigned b,g,r;
549             register uint16_t rgb;
550             rgb = src[2*i];
551             r = rgb&0x1F;
552             g = (rgb&0x7E0)>>5;
553             b = (rgb&0xF800)>>11;
554             dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
555         }
556 }
557
558 void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
559 {
560         const uint16_t *end;
561         uint8_t *d = (uint8_t *)dst;
562         const uint16_t *s = (const uint16_t *)src;
563         end = s + src_size/2;
564         while(s < end)
565         {
566                 register uint16_t bgr;
567                 bgr = *s++;
568                 #ifdef WORDS_BIGENDIAN
569                         *d++ = 0;
570                         *d++ = (bgr&0x1F)<<3;
571                         *d++ = (bgr&0x3E0)>>2;
572                         *d++ = (bgr&0x7C00)>>7;
573                 #else
574                         *d++ = (bgr&0x7C00)>>7;
575                         *d++ = (bgr&0x3E0)>>2;
576                         *d++ = (bgr&0x1F)<<3;
577                         *d++ = 0;
578                 #endif
579         }
580 }
581
582 void rgb15tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
583 {
584         const uint16_t *end;
585         uint8_t *d = (uint8_t *)dst;
586         const uint16_t *s = (uint16_t *)src;
587         end = s + src_size/2;
588         while(s < end)
589         {
590                 register uint16_t bgr;
591                 bgr = *s++;
592                 *d++ = (bgr&0x7C00)>>7;
593                 *d++ = (bgr&0x3E0)>>2;
594                 *d++ = (bgr&0x1F)<<3;
595         }
596 }
597
598 void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
599 {
600         long i;
601         long num_pixels = src_size >> 1;
602         
603         for(i=0; i<num_pixels; i++)
604         {
605             unsigned b,g,r;
606             register uint16_t rgb;
607             rgb = src[2*i];
608             r = rgb&0x1F;
609             g = (rgb&0x3E0)>>5;
610             b = (rgb&0x7C00)>>10;
611             dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
612         }
613 }
614
615 void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
616 {
617         long i;
618         long num_pixels = src_size >> 1;
619         
620         for(i=0; i<num_pixels; i++)
621         {
622             unsigned b,g,r;
623             register uint16_t rgb;
624             rgb = src[2*i];
625             r = rgb&0x1F;
626             g = (rgb&0x3E0)>>5;
627             b = (rgb&0x7C00)>>10;
628             dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
629         }
630 }
631
632 void rgb8tobgr8(const uint8_t *src, uint8_t *dst, long src_size)
633 {
634         long i;
635         long num_pixels = src_size;
636         for(i=0; i<num_pixels; i++)
637         {
638             unsigned b,g,r;
639             register uint8_t rgb;
640             rgb = src[i];
641             r = (rgb&0x07);
642             g = (rgb&0x38)>>3;
643             b = (rgb&0xC0)>>6;
644             dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
645         }
646 }