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