rgb vs bgr fix for the unscaled converters.
[libav.git] / libswscale / rgb2rgb.c
CommitLineData
fcfbc150 1/*
8a322796
DB
2 * software RGB to RGB converter
3 * pluralize by software PAL8 to RGB converter
4 * software YUV to YUV converter
5 * software YUV to RGB converter
6 * Written by Nick Kurshev.
7 * palette & YUV & runtime CPU stuff by Michael (michaelni@gmx.at)
4fadc2b4 8 *
d026b45e
DB
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or modify
4fadc2b4
DB
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
d026b45e 16 * FFmpeg is distributed in the hope that it will be useful,
4fadc2b4
DB
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
d026b45e 22 * along with FFmpeg; if not, write to the Free Software
b19bcbaa 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6a4970ab 24 *
8a322796
DB
25 * The C code (not assembly, MMX, ...) of this file can be used
26 * under the LGPL license.
a3aece93 27 */
b234ae81 28#include <inttypes.h>
b2d374c9 29#include "config.h"
83da2c6f
DB
30#include "libavutil/x86_cpu.h"
31#include "libavutil/bswap.h"
b234ae81 32#include "rgb2rgb.h"
700490a4 33#include "swscale.h"
94c4def2 34#include "swscale_internal.h"
a3aece93 35
8a322796 36#define FAST_BGR2YV12 // use 7-bit instead of 15-bit coefficients
21316f3c 37
6107059c
MN
38void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
39void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
40void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
41void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
30c48a0a
BF
42void (*rgb32to16)(const uint8_t *src, uint8_t *dst, long src_size);
43void (*rgb32to15)(const uint8_t *src, uint8_t *dst, long src_size);
44void (*rgb15to16)(const uint8_t *src, uint8_t *dst, long src_size);
6107059c 45void (*rgb15tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
30c48a0a
BF
46void (*rgb15to32)(const uint8_t *src, uint8_t *dst, long src_size);
47void (*rgb16to15)(const uint8_t *src, uint8_t *dst, long src_size);
6107059c 48void (*rgb16tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
30c48a0a 49void (*rgb16to32)(const uint8_t *src, uint8_t *dst, long src_size);
7f526efd
RD
50//void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
51void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
6107059c
MN
52void (*rgb24to16)(const uint8_t *src, uint8_t *dst, long src_size);
53void (*rgb24to15)(const uint8_t *src, uint8_t *dst, long src_size);
7f526efd
RD
54void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
55//void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
56void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
57void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
700490a4
MN
58
59void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
6e42e6c4
DB
60 long width, long height,
61 long lumStride, long chromStride, long dstStride);
caeaabe7 62void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
6e42e6c4
DB
63 long width, long height,
64 long lumStride, long chromStride, long dstStride);
700490a4 65void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
6e42e6c4
DB
66 long width, long height,
67 long lumStride, long chromStride, long dstStride);
700490a4 68void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
6e42e6c4
DB
69 long width, long height,
70 long lumStride, long chromStride, long srcStride);
700490a4 71void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
6e42e6c4
DB
72 long width, long height,
73 long lumStride, long chromStride, long srcStride);
7f526efd 74void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
6e42e6c4 75 long srcStride, long dstStride);
700490a4 76void (*interleaveBytes)(uint8_t *src1, uint8_t *src2, uint8_t *dst,
6e42e6c4
DB
77 long width, long height, long src1Stride,
78 long src2Stride, long dstStride);
700490a4 79void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
6e42e6c4
DB
80 uint8_t *dst1, uint8_t *dst2,
81 long width, long height,
82 long srcStride1, long srcStride2,
83 long dstStride1, long dstStride2);
700490a4 84void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
6e42e6c4
DB
85 uint8_t *dst,
86 long width, long height,
87 long srcStride1, long srcStride2,
88 long srcStride3, long dstStride);
700490a4 89
9bde778e 90#if defined(ARCH_X86) && defined(CONFIG_GPL)
d334c7c2
RD
91DECLARE_ASM_CONST(8, uint64_t, mmx_null) = 0x0000000000000000ULL;
92DECLARE_ASM_CONST(8, uint64_t, mmx_one) = 0xFFFFFFFFFFFFFFFFULL;
93DECLARE_ASM_CONST(8, uint64_t, mask32b) = 0x000000FF000000FFULL;
94DECLARE_ASM_CONST(8, uint64_t, mask32g) = 0x0000FF000000FF00ULL;
95DECLARE_ASM_CONST(8, uint64_t, mask32r) = 0x00FF000000FF0000ULL;
96DECLARE_ASM_CONST(8, uint64_t, mask32) = 0x00FFFFFF00FFFFFFULL;
97DECLARE_ASM_CONST(8, uint64_t, mask3216br) = 0x00F800F800F800F8ULL;
98DECLARE_ASM_CONST(8, uint64_t, mask3216g) = 0x0000FC000000FC00ULL;
99DECLARE_ASM_CONST(8, uint64_t, mask3215g) = 0x0000F8000000F800ULL;
100DECLARE_ASM_CONST(8, uint64_t, mul3216) = 0x2000000420000004ULL;
101DECLARE_ASM_CONST(8, uint64_t, mul3215) = 0x2000000820000008ULL;
102DECLARE_ASM_CONST(8, uint64_t, mask24b) = 0x00FF0000FF0000FFULL;
103DECLARE_ASM_CONST(8, uint64_t, mask24g) = 0xFF0000FF0000FF00ULL;
104DECLARE_ASM_CONST(8, uint64_t, mask24r) = 0x0000FF0000FF0000ULL;
105DECLARE_ASM_CONST(8, uint64_t, mask24l) = 0x0000000000FFFFFFULL;
106DECLARE_ASM_CONST(8, uint64_t, mask24h) = 0x0000FFFFFF000000ULL;
107DECLARE_ASM_CONST(8, uint64_t, mask24hh) = 0xffff000000000000ULL;
108DECLARE_ASM_CONST(8, uint64_t, mask24hhh) = 0xffffffff00000000ULL;
109DECLARE_ASM_CONST(8, uint64_t, mask24hhhh) = 0xffffffffffff0000ULL;
110DECLARE_ASM_CONST(8, uint64_t, mask15b) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
111DECLARE_ASM_CONST(8, uint64_t, mask15rg) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
112DECLARE_ASM_CONST(8, uint64_t, mask15s) = 0xFFE0FFE0FFE0FFE0ULL;
113DECLARE_ASM_CONST(8, uint64_t, mask15g) = 0x03E003E003E003E0ULL;
114DECLARE_ASM_CONST(8, uint64_t, mask15r) = 0x7C007C007C007C00ULL;
0d9f3d85 115#define mask16b mask15b
d334c7c2
RD
116DECLARE_ASM_CONST(8, uint64_t, mask16g) = 0x07E007E007E007E0ULL;
117DECLARE_ASM_CONST(8, uint64_t, mask16r) = 0xF800F800F800F800ULL;
118DECLARE_ASM_CONST(8, uint64_t, red_16mask) = 0x0000f8000000f800ULL;
119DECLARE_ASM_CONST(8, uint64_t, green_16mask) = 0x000007e0000007e0ULL;
120DECLARE_ASM_CONST(8, uint64_t, blue_16mask) = 0x0000001f0000001fULL;
121DECLARE_ASM_CONST(8, uint64_t, red_15mask) = 0x00007c0000007c00ULL;
122DECLARE_ASM_CONST(8, uint64_t, green_15mask) = 0x000003e0000003e0ULL;
123DECLARE_ASM_CONST(8, uint64_t, blue_15mask) = 0x0000001f0000001fULL;
21316f3c 124
99969243
MN
125#if 0
126static volatile uint64_t __attribute__((aligned(8))) b5Dither;
127static volatile uint64_t __attribute__((aligned(8))) g5Dither;
128static volatile uint64_t __attribute__((aligned(8))) g6Dither;
129static volatile uint64_t __attribute__((aligned(8))) r5Dither;
130
131static uint64_t __attribute__((aligned(8))) dither4[2]={
6e42e6c4
DB
132 0x0103010301030103LL,
133 0x0200020002000200LL,};
99969243
MN
134
135static uint64_t __attribute__((aligned(8))) dither8[2]={
6e42e6c4
DB
136 0x0602060206020602LL,
137 0x0004000400040004LL,};
99969243 138#endif
3d6a30d9 139#endif /* defined(ARCH_X86) */
79811694 140
1de97d84
MN
141#define RGB2YUV_SHIFT 8
142#define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
143#define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
144#define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
145#define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
146#define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
147#define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
148#define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
149#define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
150#define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
151
8a322796
DB
152//Note: We have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW + MMX2 one.
153//plain C versions
1de97d84
MN
154#undef HAVE_MMX
155#undef HAVE_MMX2
156#undef HAVE_3DNOW
52075cde 157#undef HAVE_SSE2
1de97d84
MN
158#define RENAME(a) a ## _C
159#include "rgb2rgb_template.c"
160
9bde778e 161#if defined(ARCH_X86) && defined(CONFIG_GPL)
1de97d84
MN
162
163//MMX versions
164#undef RENAME
165#define HAVE_MMX
166#undef HAVE_MMX2
167#undef HAVE_3DNOW
52075cde 168#undef HAVE_SSE2
1de97d84
MN
169#define RENAME(a) a ## _MMX
170#include "rgb2rgb_template.c"
171
172//MMX2 versions
173#undef RENAME
174#define HAVE_MMX
175#define HAVE_MMX2
176#undef HAVE_3DNOW
52075cde 177#undef HAVE_SSE2
1de97d84
MN
178#define RENAME(a) a ## _MMX2
179#include "rgb2rgb_template.c"
180
181//3DNOW versions
182#undef RENAME
183#define HAVE_MMX
184#undef HAVE_MMX2
185#define HAVE_3DNOW
52075cde 186#undef HAVE_SSE2
abdcd337 187#define RENAME(a) a ## _3DNOW
1de97d84
MN
188#include "rgb2rgb_template.c"
189
6e1c66bc 190#endif //ARCH_X86 || ARCH_X86_64
b238eb2e 191
a3aece93 192/*
8a322796 193 RGB15->RGB16 original by Strepto/Astral
a3aece93 194 ported to gcc & bugfixed : A'rpi
51da31f1 195 MMX2, 3DNOW optimization by Nick Kurshev
8a322796 196 32-bit C version, and and&add trick by Michael Niedermayer
a3aece93 197*/
fcfbc150 198
700490a4 199void sws_rgb2rgb_init(int flags){
9bde778e 200#if (defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX)) && defined(CONFIG_GPL)
6e42e6c4
DB
201 if (flags & SWS_CPU_CAPS_MMX2)
202 rgb2rgb_init_MMX2();
203 else if (flags & SWS_CPU_CAPS_3DNOW)
204 rgb2rgb_init_3DNOW();
205 else if (flags & SWS_CPU_CAPS_MMX)
206 rgb2rgb_init_MMX();
207 else
dbdae6ec 208#endif /* defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX) */
6e42e6c4 209 rgb2rgb_init_C();
ac4d0aea 210}
700490a4 211
fcfbc150 212/**
10ef990d 213 * Palette is assumed to contain BGR32.
fcfbc150 214 */
7f526efd 215void palette8torgb32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
fcfbc150 216{
6e42e6c4 217 long i;
9780c7ff
AB
218
219/*
6e42e6c4 220 for (i=0; i<num_pixels; i++)
30c48a0a 221 ((unsigned *)dst)[i] = ((unsigned *)palette)[src[i]];
9780c7ff 222*/
fcfbc150 223
6e42e6c4
DB
224 for (i=0; i<num_pixels; i++)
225 {
226 #ifdef WORDS_BIGENDIAN
30c48a0a
BF
227 dst[3]= palette[src[i]*4+2];
228 dst[2]= palette[src[i]*4+1];
229 dst[1]= palette[src[i]*4+0];
6e42e6c4
DB
230 #else
231 //FIXME slow?
30c48a0a
BF
232 dst[0]= palette[src[i]*4+2];
233 dst[1]= palette[src[i]*4+1];
234 dst[2]= palette[src[i]*4+0];
6e42e6c4
DB
235 //dst[3]= 0; /* do we need this cleansing? */
236 #endif
237 dst+= 4;
238 }
6c1baeb0 239}
9780c7ff 240
7f526efd 241void palette8tobgr32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
6c1baeb0 242{
6e42e6c4
DB
243 long i;
244 for (i=0; i<num_pixels; i++)
245 {
246 #ifdef WORDS_BIGENDIAN
30c48a0a
BF
247 dst[3]= palette[src[i]*4+0];
248 dst[2]= palette[src[i]*4+1];
249 dst[1]= palette[src[i]*4+2];
6e42e6c4
DB
250 #else
251 //FIXME slow?
30c48a0a
BF
252 dst[0]= palette[src[i]*4+0];
253 dst[1]= palette[src[i]*4+1];
254 dst[2]= palette[src[i]*4+2];
6e42e6c4
DB
255 //dst[3]= 0; /* do we need this cleansing? */
256 #endif
257
258 dst+= 4;
259 }
6c1baeb0 260}
6c1baeb0 261
9ce6584e 262/**
10ef990d 263 * Palette is assumed to contain BGR32.
9ce6584e 264 */
7f526efd 265void palette8torgb24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
9ce6584e 266{
6e42e6c4 267 long i;
9ce6584e 268/*
8a322796 269 Writes 1 byte too much and might cause alignment issues on some architectures?
6e42e6c4 270 for (i=0; i<num_pixels; i++)
30c48a0a 271 ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[src[i]];
9ce6584e 272*/
6e42e6c4
DB
273 for (i=0; i<num_pixels; i++)
274 {
275 //FIXME slow?
30c48a0a
BF
276 dst[0]= palette[src[i]*4+2];
277 dst[1]= palette[src[i]*4+1];
278 dst[2]= palette[src[i]*4+0];
6e42e6c4
DB
279 dst+= 3;
280 }
9ce6584e
MN
281}
282
7f526efd 283void palette8tobgr24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
6c1baeb0 284{
6e42e6c4 285 long i;
6c1baeb0 286/*
8a322796 287 Writes 1 byte too much and might cause alignment issues on some architectures?
6e42e6c4 288 for (i=0; i<num_pixels; i++)
30c48a0a 289 ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[src[i]];
6c1baeb0 290*/
6e42e6c4
DB
291 for (i=0; i<num_pixels; i++)
292 {
293 //FIXME slow?
30c48a0a
BF
294 dst[0]= palette[src[i]*4+0];
295 dst[1]= palette[src[i]*4+1];
296 dst[2]= palette[src[i]*4+2];
6e42e6c4
DB
297 dst+= 3;
298 }
6c1baeb0
AB
299}
300
fcfbc150 301/**
8a322796 302 * Palette is assumed to contain BGR16, see rgb32to16 to convert the palette.
fcfbc150 303 */
7f526efd 304void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
fcfbc150 305{
6e42e6c4
DB
306 long i;
307 for (i=0; i<num_pixels; i++)
774c386a 308 ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]];
fcfbc150 309}
7f526efd 310void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
6c1baeb0 311{
6e42e6c4
DB
312 long i;
313 for (i=0; i<num_pixels; i++)
774c386a 314 ((uint16_t *)dst)[i] = bswap_16(((const uint16_t *)palette)[src[i]]);
6c1baeb0 315}
fcfbc150
MN
316
317/**
10ef990d 318 * Palette is assumed to contain BGR15, see rgb32to15 to convert the palette.
fcfbc150 319 */
7f526efd 320void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
fcfbc150 321{
6e42e6c4
DB
322 long i;
323 for (i=0; i<num_pixels; i++)
774c386a 324 ((uint16_t *)dst)[i] = ((const uint16_t *)palette)[src[i]];
9ce6584e 325}
7f526efd 326void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
6c1baeb0 327{
6e42e6c4
DB
328 long i;
329 for (i=0; i<num_pixels; i++)
774c386a 330 ((uint16_t *)dst)[i] = bswap_16(((const uint16_t *)palette)[src[i]]);
6c1baeb0 331}
99969243 332
6107059c 333void rgb32to24(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 334{
6e42e6c4
DB
335 long i;
336 long num_pixels = src_size >> 2;
337 for (i=0; i<num_pixels; i++)
338 {
339 #ifdef WORDS_BIGENDIAN
340 /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
341 dst[3*i + 0] = src[4*i + 1];
342 dst[3*i + 1] = src[4*i + 2];
343 dst[3*i + 2] = src[4*i + 3];
344 #else
345 dst[3*i + 0] = src[4*i + 2];
346 dst[3*i + 1] = src[4*i + 1];
347 dst[3*i + 2] = src[4*i + 0];
348 #endif
349 }
ac4d0aea
MN
350}
351
6107059c 352void rgb24to32(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 353{
6e42e6c4
DB
354 long i;
355 for (i=0; 3*i<src_size; i++)
356 {
357 #ifdef WORDS_BIGENDIAN
358 /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
359 dst[4*i + 0] = 0;
360 dst[4*i + 1] = src[3*i + 0];
361 dst[4*i + 2] = src[3*i + 1];
362 dst[4*i + 3] = src[3*i + 2];
363 #else
364 dst[4*i + 0] = src[3*i + 2];
365 dst[4*i + 1] = src[3*i + 1];
366 dst[4*i + 2] = src[3*i + 0];
367 dst[4*i + 3] = 0;
368 #endif
369 }
ac4d0aea
MN
370}
371
7f526efd 372void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 373{
6e42e6c4 374 const uint16_t *end;
72d6b610 375 uint8_t *d = dst;
774c386a 376 const uint16_t *s = (const uint16_t *)src;
6e42e6c4
DB
377 end = s + src_size/2;
378 while (s < end)
379 {
380 register uint16_t bgr;
381 bgr = *s++;
382 #ifdef WORDS_BIGENDIAN
383 *d++ = 0;
384 *d++ = (bgr&0x1F)<<3;
385 *d++ = (bgr&0x7E0)>>3;
386 *d++ = (bgr&0xF800)>>8;
387 #else
388 *d++ = (bgr&0xF800)>>8;
389 *d++ = (bgr&0x7E0)>>3;
390 *d++ = (bgr&0x1F)<<3;
391 *d++ = 0;
392 #endif
393 }
ac4d0aea
MN
394}
395
6107059c 396void rgb16to24(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 397{
6e42e6c4 398 const uint16_t *end;
72d6b610 399 uint8_t *d = dst;
6e42e6c4
DB
400 const uint16_t *s = (const uint16_t *)src;
401 end = s + src_size/2;
402 while (s < end)
403 {
404 register uint16_t bgr;
405 bgr = *s++;
406 *d++ = (bgr&0xF800)>>8;
407 *d++ = (bgr&0x7E0)>>3;
408 *d++ = (bgr&0x1F)<<3;
409 }
ac4d0aea
MN
410}
411
7f526efd 412void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 413{
6e42e6c4
DB
414 long i;
415 long num_pixels = src_size >> 1;
416
417 for (i=0; i<num_pixels; i++)
418 {
419 unsigned b,g,r;
420 register uint16_t rgb;
421 rgb = src[2*i];
422 r = rgb&0x1F;
423 g = (rgb&0x7E0)>>5;
424 b = (rgb&0xF800)>>11;
425 dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
426 }
ac4d0aea
MN
427}
428
7f526efd 429void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 430{
6e42e6c4
DB
431 long i;
432 long num_pixels = src_size >> 1;
433
434 for (i=0; i<num_pixels; i++)
435 {
436 unsigned b,g,r;
437 register uint16_t rgb;
438 rgb = src[2*i];
439 r = rgb&0x1F;
440 g = (rgb&0x7E0)>>5;
441 b = (rgb&0xF800)>>11;
442 dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
443 }
ac4d0aea
MN
444}
445
7f526efd 446void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 447{
6e42e6c4 448 const uint16_t *end;
72d6b610 449 uint8_t *d = dst;
6e42e6c4
DB
450 const uint16_t *s = (const uint16_t *)src;
451 end = s + src_size/2;
452 while (s < end)
453 {
454 register uint16_t bgr;
455 bgr = *s++;
456 #ifdef WORDS_BIGENDIAN
457 *d++ = 0;
458 *d++ = (bgr&0x1F)<<3;
459 *d++ = (bgr&0x3E0)>>2;
460 *d++ = (bgr&0x7C00)>>7;
461 #else
462 *d++ = (bgr&0x7C00)>>7;
463 *d++ = (bgr&0x3E0)>>2;
464 *d++ = (bgr&0x1F)<<3;
465 *d++ = 0;
466 #endif
467 }
ac4d0aea
MN
468}
469
6107059c 470void rgb15to24(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 471{
6e42e6c4 472 const uint16_t *end;
72d6b610 473 uint8_t *d = dst;
774c386a 474 const uint16_t *s = (const uint16_t *)src;
6e42e6c4
DB
475 end = s + src_size/2;
476 while (s < end)
477 {
478 register uint16_t bgr;
479 bgr = *s++;
480 *d++ = (bgr&0x7C00)>>7;
481 *d++ = (bgr&0x3E0)>>2;
482 *d++ = (bgr&0x1F)<<3;
483 }
ac4d0aea
MN
484}
485
7f526efd 486void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 487{
6e42e6c4
DB
488 long i;
489 long num_pixels = src_size >> 1;
490
491 for (i=0; i<num_pixels; i++)
492 {
493 unsigned b,g,r;
494 register uint16_t rgb;
495 rgb = src[2*i];
496 r = rgb&0x1F;
497 g = (rgb&0x3E0)>>5;
498 b = (rgb&0x7C00)>>10;
499 dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
500 }
ac4d0aea
MN
501}
502
7f526efd 503void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 504{
6e42e6c4
DB
505 long i;
506 long num_pixels = src_size >> 1;
507
508 for (i=0; i<num_pixels; i++)
509 {
510 unsigned b,g,r;
511 register uint16_t rgb;
512 rgb = src[2*i];
513 r = rgb&0x1F;
514 g = (rgb&0x3E0)>>5;
515 b = (rgb&0x7C00)>>10;
516 dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
517 }
ac4d0aea
MN
518}
519
6107059c 520void bgr8torgb8(const uint8_t *src, uint8_t *dst, long src_size)
ac4d0aea 521{
6e42e6c4
DB
522 long i;
523 long num_pixels = src_size;
524 for (i=0; i<num_pixels; i++)
525 {
526 unsigned b,g,r;
527 register uint8_t rgb;
528 rgb = src[i];
529 r = (rgb&0x07);
530 g = (rgb&0x38)>>3;
531 b = (rgb&0xC0)>>6;
532 dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
533 }
ac4d0aea 534}