3 * rgb2rgb.c, Software RGB to RGB convertor
4 * Written by Nick Kurshev.
9 #include "../mmx_defs.h"
12 static const uint64_t mask32 __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
13 static const uint64_t mask24l __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
14 static const uint64_t mask24h __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
15 static const uint64_t mask15b __attribute__((aligned(8))) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
16 static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
19 void rgb24to32(const uint8_t *src,uint8_t *dst,uint32_t src_size)
22 const uint8_t *s = src;
29 __asm __volatile(PREFETCH" %0"::"m"(*s):"memory");
30 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*2))*(MMREG_SIZE*2));
31 __asm __volatile("movq %0, %%mm7"::"m"(mask32):"memory");
32 if(mm_end == end) mm_end -= MMREG_SIZE*2;
41 "punpckldq %%mm1, %%mm0\n\t"
42 "punpckldq %%mm3, %%mm2\n\t"
43 "pand %%mm7, %%mm0\n\t"
44 "pand %%mm7, %%mm2\n\t"
45 MOVNTQ" %%mm0, %0\n\t"
53 __asm __volatile(SFENCE:::"memory");
54 __asm __volatile(EMMS:::"memory");
65 void rgb32to24(const uint8_t *src,uint8_t *dst,uint32_t src_size)
68 const uint8_t *s = src;
75 __asm __volatile(PREFETCH" %0"::"m"(*s):"memory");
76 mm_end = (uint8_t*)((((unsigned long)end)/(MMREG_SIZE*2))*(MMREG_SIZE*2));
80 ::"m"(mask24l),"m"(mask24h):"memory");
81 if(mm_end == end) mm_end -= MMREG_SIZE*2;
88 "movq %%mm0, %%mm2\n\t"
89 "movq %%mm1, %%mm3\n\t"
92 "pand %%mm7, %%mm0\n\t"
93 "pand %%mm7, %%mm1\n\t"
94 "pand %%mm6, %%mm2\n\t"
95 "pand %%mm6, %%mm3\n\t"
96 "por %%mm2, %%mm0\n\t"
97 "por %%mm3, %%mm1\n\t"
98 MOVNTQ" %%mm0, %0\n\t"
106 __asm __volatile(SFENCE:::"memory");
107 __asm __volatile(EMMS:::"memory");
119 Original by Strepto/Astral
120 ported to gcc & bugfixed : A'rpi
121 MMX2, 3DNOW optimization by Nick Kurshev
123 void rgb15to16(const uint8_t *src,uint8_t *dst,uint32_t src_size)
126 register const char* s=src+src_size;
127 register char* d=dst+src_size;
128 register int offs=-src_size;
129 __asm __volatile(PREFETCH" %0"::"m"(*(s+offs)):"memory");
133 ::"m"(mask15b), "m"(mask15rg):"memory");
139 "movq 8%1, %%mm2\n\t"
140 "movq %%mm0, %%mm1\n\t"
141 "movq %%mm2, %%mm3\n\t"
142 "pand %%mm4, %%mm0\n\t"
143 "pand %%mm5, %%mm1\n\t"
144 "pand %%mm4, %%mm2\n\t"
145 "pand %%mm5, %%mm3\n\t"
146 "psllq $1, %%mm1\n\t"
147 "psllq $1, %%mm3\n\t"
148 "por %%mm1, %%mm0\n\t"
149 "por %%mm3, %%mm2\n\t"
150 MOVNTQ" %%mm0, %0\n\t"
157 __asm __volatile(SFENCE:::"memory");
158 __asm __volatile(EMMS:::"memory");
160 const uint16_t *s1=( uint16_t * )src;
161 uint16_t *d1=( uint16_t * )dst;
162 uint16_t *e=((uint8_t *)s1)+src_size;
164 register int x=*( s1++ );
167 0111 1111 1110 0000=0x7FE0
168 00000000000001 1111=0x001F */
169 *( d1++ )=( x&0x001F )|( ( x&0x7FE0 )<<1 );
175 * Pallete is assumed to contain bgr32
177 void palette8torgb32(uint8_t *src, uint8_t *dst, int src_size, uint8_t *palette)
180 for(i=0; i<src_size; i++)
181 ((uint32_t *)dst)[i] = ((uint32_t *)palette)[ src[i] ];
184 void rgb32to16(uint8_t *src, uint8_t *dst, int src_size)
187 for(i=0; i<src_size; i+=4)
189 const int b= src[i+0];
190 const int g= src[i+1];
191 const int r= src[i+2];
193 ((uint16_t *)dst)[i]= (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
197 void rgb32to15(uint8_t *src, uint8_t *dst, int src_size)
200 for(i=0; i<src_size; i+=4)
202 const int b= src[i+0];
203 const int g= src[i+1];
204 const int r= src[i+2];
206 ((uint16_t *)dst)[i]= (b>>3) | ((g&0xF8)<<3) | ((r&0xF8)<<7);
212 * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
214 void palette8torgb16(uint8_t *src, uint8_t *dst, int src_size, uint8_t *palette)
217 for(i=0; i<src_size; i++)
218 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
222 * Pallete is assumed to contain bgr15, see rgb32to15 to convert the palette
224 void palette8torgb15(uint8_t *src, uint8_t *dst, int src_size, uint8_t *palette)
227 for(i=0; i<src_size; i++)
228 ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];