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