rgb2rgb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. * the C code (not assembly, mmx, ...) of this file can be used
  27. * under the LGPL license too
  28. */
  29. #include <inttypes.h>
  30. #include "config.h"
  31. #include "rgb2rgb.h"
  32. #include "swscale.h"
  33. #include "swscale_internal.h"
  34. #include "x86_cpu.h"
  35. #include "bswap.h"
  36. #ifdef USE_FASTMEMCPY
  37. #include "libvo/fastmemcpy.h"
  38. #endif
  39. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  40. void (*rgb24to32)(const uint8_t *src,uint8_t *dst,long src_size);
  41. void (*rgb24to16)(const uint8_t *src,uint8_t *dst,long src_size);
  42. void (*rgb24to15)(const uint8_t *src,uint8_t *dst,long src_size);
  43. void (*rgb32to24)(const uint8_t *src,uint8_t *dst,long src_size);
  44. void (*rgb32to16)(const uint8_t *src,uint8_t *dst,long src_size);
  45. void (*rgb32to15)(const uint8_t *src,uint8_t *dst,long src_size);
  46. void (*rgb15to16)(const uint8_t *src,uint8_t *dst,long src_size);
  47. void (*rgb15to24)(const uint8_t *src,uint8_t *dst,long src_size);
  48. void (*rgb15to32)(const uint8_t *src,uint8_t *dst,long src_size);
  49. void (*rgb16to15)(const uint8_t *src,uint8_t *dst,long src_size);
  50. void (*rgb16to24)(const uint8_t *src,uint8_t *dst,long src_size);
  51. void (*rgb16to32)(const uint8_t *src,uint8_t *dst,long src_size);
  52. //void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
  53. void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  54. void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
  55. void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
  56. void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
  57. //void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  58. void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
  59. void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
  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. #if defined(ARCH_X86) && defined(CONFIG_GPL)
  91. static const uint64_t mmx_null __attribute__((aligned(8))) = 0x0000000000000000ULL;
  92. static const uint64_t mmx_one __attribute__((aligned(8))) = 0xFFFFFFFFFFFFFFFFULL;
  93. static const uint64_t mask32b attribute_used __attribute__((aligned(8))) = 0x000000FF000000FFULL;
  94. static const uint64_t mask32g attribute_used __attribute__((aligned(8))) = 0x0000FF000000FF00ULL;
  95. static const uint64_t mask32r attribute_used __attribute__((aligned(8))) = 0x00FF000000FF0000ULL;
  96. static const uint64_t mask32 __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
  97. static const uint64_t mask3216br __attribute__((aligned(8)))=0x00F800F800F800F8ULL;
  98. static const uint64_t mask3216g __attribute__((aligned(8)))=0x0000FC000000FC00ULL;
  99. static const uint64_t mask3215g __attribute__((aligned(8)))=0x0000F8000000F800ULL;
  100. static const uint64_t mul3216 __attribute__((aligned(8))) = 0x2000000420000004ULL;
  101. static const uint64_t mul3215 __attribute__((aligned(8))) = 0x2000000820000008ULL;
  102. static const uint64_t mask24b attribute_used __attribute__((aligned(8))) = 0x00FF0000FF0000FFULL;
  103. static const uint64_t mask24g attribute_used __attribute__((aligned(8))) = 0xFF0000FF0000FF00ULL;
  104. static const uint64_t mask24r attribute_used __attribute__((aligned(8))) = 0x0000FF0000FF0000ULL;
  105. static const uint64_t mask24l __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
  106. static const uint64_t mask24h __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
  107. static const uint64_t mask24hh __attribute__((aligned(8))) = 0xffff000000000000ULL;
  108. static const uint64_t mask24hhh __attribute__((aligned(8))) = 0xffffffff00000000ULL;
  109. static const uint64_t mask24hhhh __attribute__((aligned(8))) = 0xffffffffffff0000ULL;
  110. static const uint64_t mask15b __attribute__((aligned(8))) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
  111. static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
  112. static const uint64_t mask15s __attribute__((aligned(8))) = 0xFFE0FFE0FFE0FFE0ULL;
  113. static const uint64_t mask15g __attribute__((aligned(8))) = 0x03E003E003E003E0ULL;
  114. static const uint64_t mask15r __attribute__((aligned(8))) = 0x7C007C007C007C00ULL;
  115. #define mask16b mask15b
  116. static const uint64_t mask16g __attribute__((aligned(8))) = 0x07E007E007E007E0ULL;
  117. static const uint64_t mask16r __attribute__((aligned(8))) = 0xF800F800F800F800ULL;
  118. static const uint64_t red_16mask __attribute__((aligned(8))) = 0x0000f8000000f800ULL;
  119. static const uint64_t green_16mask __attribute__((aligned(8)))= 0x000007e0000007e0ULL;
  120. static const uint64_t blue_16mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
  121. static const uint64_t red_15mask __attribute__((aligned(8))) = 0x00007c000000f800ULL;
  122. static const uint64_t green_15mask __attribute__((aligned(8)))= 0x000003e0000007e0ULL;
  123. static const uint64_t blue_15mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
  124. #ifdef FAST_BGR2YV12
  125. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
  126. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  127. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  128. #else
  129. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  130. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  131. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  132. #endif
  133. static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
  134. static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8)))= 0x8080808080808080ULL;
  135. static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
  136. #if 0
  137. static volatile uint64_t __attribute__((aligned(8))) b5Dither;
  138. static volatile uint64_t __attribute__((aligned(8))) g5Dither;
  139. static volatile uint64_t __attribute__((aligned(8))) g6Dither;
  140. static volatile uint64_t __attribute__((aligned(8))) r5Dither;
  141. static uint64_t __attribute__((aligned(8))) dither4[2]={
  142. 0x0103010301030103LL,
  143. 0x0200020002000200LL,};
  144. static uint64_t __attribute__((aligned(8))) dither8[2]={
  145. 0x0602060206020602LL,
  146. 0x0004000400040004LL,};
  147. #endif
  148. #endif /* defined(ARCH_X86) */
  149. #define RGB2YUV_SHIFT 8
  150. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  151. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  152. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  153. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  154. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  155. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  156. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  157. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  158. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  159. //Note: we have C, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  160. //Plain C versions
  161. #undef HAVE_MMX
  162. #undef HAVE_MMX2
  163. #undef HAVE_3DNOW
  164. #undef HAVE_SSE2
  165. #define RENAME(a) a ## _C
  166. #include "rgb2rgb_template.c"
  167. #if defined(ARCH_X86) && defined(CONFIG_GPL)
  168. //MMX versions
  169. #undef RENAME
  170. #define HAVE_MMX
  171. #undef HAVE_MMX2
  172. #undef HAVE_3DNOW
  173. #undef HAVE_SSE2
  174. #define RENAME(a) a ## _MMX
  175. #include "rgb2rgb_template.c"
  176. //MMX2 versions
  177. #undef RENAME
  178. #define HAVE_MMX
  179. #define HAVE_MMX2
  180. #undef HAVE_3DNOW
  181. #undef HAVE_SSE2
  182. #define RENAME(a) a ## _MMX2
  183. #include "rgb2rgb_template.c"
  184. //3DNOW versions
  185. #undef RENAME
  186. #define HAVE_MMX
  187. #undef HAVE_MMX2
  188. #define HAVE_3DNOW
  189. #undef HAVE_SSE2
  190. #define RENAME(a) a ## _3DNOW
  191. #include "rgb2rgb_template.c"
  192. #endif //ARCH_X86 || ARCH_X86_64
  193. /*
  194. rgb15->rgb16 Original by Strepto/Astral
  195. ported to gcc & bugfixed : A'rpi
  196. MMX2, 3DNOW optimization by Nick Kurshev
  197. 32bit c version, and and&add trick by Michael Niedermayer
  198. */
  199. void sws_rgb2rgb_init(int flags){
  200. #if (defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX)) && defined(CONFIG_GPL)
  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
  208. #endif /* defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX) */
  209. {
  210. rgb2rgb_init_C();
  211. }
  212. }
  213. /**
  214. * Palette is assumed to contain BGR32.
  215. */
  216. void palette8torgb32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  217. {
  218. long i;
  219. /*
  220. for(i=0; i<num_pixels; i++)
  221. ((unsigned *)dst)[i] = ((unsigned *)palette)[ src[i] ];
  222. */
  223. for(i=0; i<num_pixels; i++)
  224. {
  225. #ifdef WORDS_BIGENDIAN
  226. dst[3]= palette[ src[i]*4+2 ];
  227. dst[2]= palette[ src[i]*4+1 ];
  228. dst[1]= palette[ src[i]*4+0 ];
  229. #else
  230. //FIXME slow?
  231. dst[0]= palette[ src[i]*4+2 ];
  232. dst[1]= palette[ src[i]*4+1 ];
  233. dst[2]= palette[ src[i]*4+0 ];
  234. //dst[3]= 0; /* do we need this cleansing? */
  235. #endif
  236. dst+= 4;
  237. }
  238. }
  239. void palette8tobgr32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  240. {
  241. long i;
  242. for(i=0; i<num_pixels; i++)
  243. {
  244. #ifdef WORDS_BIGENDIAN
  245. dst[3]= palette[ src[i]*4+0 ];
  246. dst[2]= palette[ src[i]*4+1 ];
  247. dst[1]= palette[ src[i]*4+2 ];
  248. #else
  249. //FIXME slow?
  250. dst[0]= palette[ src[i]*4+0 ];
  251. dst[1]= palette[ src[i]*4+1 ];
  252. dst[2]= palette[ src[i]*4+2 ];
  253. //dst[3]= 0; /* do we need this cleansing? */
  254. #endif
  255. dst+= 4;
  256. }
  257. }
  258. /**
  259. * Palette is assumed to contain BGR32.
  260. */
  261. void palette8torgb24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  262. {
  263. long i;
  264. /*
  265. writes 1 byte o much and might cause alignment issues on some architectures?
  266. for(i=0; i<num_pixels; i++)
  267. ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
  268. */
  269. for(i=0; i<num_pixels; i++)
  270. {
  271. //FIXME slow?
  272. dst[0]= palette[ src[i]*4+2 ];
  273. dst[1]= palette[ src[i]*4+1 ];
  274. dst[2]= palette[ src[i]*4+0 ];
  275. dst+= 3;
  276. }
  277. }
  278. void palette8tobgr24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  279. {
  280. long i;
  281. /*
  282. writes 1 byte o much and might cause alignment issues on some architectures?
  283. for(i=0; i<num_pixels; i++)
  284. ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
  285. */
  286. for(i=0; i<num_pixels; i++)
  287. {
  288. //FIXME slow?
  289. dst[0]= palette[ src[i]*4+0 ];
  290. dst[1]= palette[ src[i]*4+1 ];
  291. dst[2]= palette[ src[i]*4+2 ];
  292. dst+= 3;
  293. }
  294. }
  295. /**
  296. * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
  297. */
  298. void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  299. {
  300. long i;
  301. for(i=0; i<num_pixels; i++)
  302. ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
  303. }
  304. void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  305. {
  306. long i;
  307. for(i=0; i<num_pixels; i++)
  308. ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
  309. }
  310. /**
  311. * Palette is assumed to contain BGR15, see rgb32to15 to convert the palette.
  312. */
  313. void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  314. {
  315. long i;
  316. for(i=0; i<num_pixels; i++)
  317. ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
  318. }
  319. void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  320. {
  321. long i;
  322. for(i=0; i<num_pixels; i++)
  323. ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
  324. }
  325. void rgb32tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  326. {
  327. long i;
  328. long num_pixels = src_size >> 2;
  329. for(i=0; i<num_pixels; i++)
  330. {
  331. #ifdef WORDS_BIGENDIAN
  332. /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
  333. dst[3*i + 0] = src[4*i + 1];
  334. dst[3*i + 1] = src[4*i + 2];
  335. dst[3*i + 2] = src[4*i + 3];
  336. #else
  337. dst[3*i + 0] = src[4*i + 2];
  338. dst[3*i + 1] = src[4*i + 1];
  339. dst[3*i + 2] = src[4*i + 0];
  340. #endif
  341. }
  342. }
  343. void rgb24tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  344. {
  345. long i;
  346. for(i=0; 3*i<src_size; i++)
  347. {
  348. #ifdef WORDS_BIGENDIAN
  349. /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
  350. dst[4*i + 0] = 0;
  351. dst[4*i + 1] = src[3*i + 0];
  352. dst[4*i + 2] = src[3*i + 1];
  353. dst[4*i + 3] = src[3*i + 2];
  354. #else
  355. dst[4*i + 0] = src[3*i + 2];
  356. dst[4*i + 1] = src[3*i + 1];
  357. dst[4*i + 2] = src[3*i + 0];
  358. dst[4*i + 3] = 0;
  359. #endif
  360. }
  361. }
  362. void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  363. {
  364. const uint16_t *end;
  365. uint8_t *d = (uint8_t *)dst;
  366. const uint16_t *s = (uint16_t *)src;
  367. end = s + src_size/2;
  368. while(s < end)
  369. {
  370. register uint16_t bgr;
  371. bgr = *s++;
  372. #ifdef WORDS_BIGENDIAN
  373. *d++ = 0;
  374. *d++ = (bgr&0x1F)<<3;
  375. *d++ = (bgr&0x7E0)>>3;
  376. *d++ = (bgr&0xF800)>>8;
  377. #else
  378. *d++ = (bgr&0xF800)>>8;
  379. *d++ = (bgr&0x7E0)>>3;
  380. *d++ = (bgr&0x1F)<<3;
  381. *d++ = 0;
  382. #endif
  383. }
  384. }
  385. void rgb16tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  386. {
  387. const uint16_t *end;
  388. uint8_t *d = (uint8_t *)dst;
  389. const uint16_t *s = (const uint16_t *)src;
  390. end = s + src_size/2;
  391. while(s < end)
  392. {
  393. register uint16_t bgr;
  394. bgr = *s++;
  395. *d++ = (bgr&0xF800)>>8;
  396. *d++ = (bgr&0x7E0)>>3;
  397. *d++ = (bgr&0x1F)<<3;
  398. }
  399. }
  400. void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  401. {
  402. long i;
  403. long num_pixels = src_size >> 1;
  404. for(i=0; i<num_pixels; i++)
  405. {
  406. unsigned b,g,r;
  407. register uint16_t rgb;
  408. rgb = src[2*i];
  409. r = rgb&0x1F;
  410. g = (rgb&0x7E0)>>5;
  411. b = (rgb&0xF800)>>11;
  412. dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
  413. }
  414. }
  415. void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  416. {
  417. long i;
  418. long num_pixels = src_size >> 1;
  419. for(i=0; i<num_pixels; i++)
  420. {
  421. unsigned b,g,r;
  422. register uint16_t rgb;
  423. rgb = src[2*i];
  424. r = rgb&0x1F;
  425. g = (rgb&0x7E0)>>5;
  426. b = (rgb&0xF800)>>11;
  427. dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
  428. }
  429. }
  430. void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  431. {
  432. const uint16_t *end;
  433. uint8_t *d = (uint8_t *)dst;
  434. const uint16_t *s = (const uint16_t *)src;
  435. end = s + src_size/2;
  436. while(s < end)
  437. {
  438. register uint16_t bgr;
  439. bgr = *s++;
  440. #ifdef WORDS_BIGENDIAN
  441. *d++ = 0;
  442. *d++ = (bgr&0x1F)<<3;
  443. *d++ = (bgr&0x3E0)>>2;
  444. *d++ = (bgr&0x7C00)>>7;
  445. #else
  446. *d++ = (bgr&0x7C00)>>7;
  447. *d++ = (bgr&0x3E0)>>2;
  448. *d++ = (bgr&0x1F)<<3;
  449. *d++ = 0;
  450. #endif
  451. }
  452. }
  453. void rgb15tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  454. {
  455. const uint16_t *end;
  456. uint8_t *d = (uint8_t *)dst;
  457. const uint16_t *s = (uint16_t *)src;
  458. end = s + src_size/2;
  459. while(s < end)
  460. {
  461. register uint16_t bgr;
  462. bgr = *s++;
  463. *d++ = (bgr&0x7C00)>>7;
  464. *d++ = (bgr&0x3E0)>>2;
  465. *d++ = (bgr&0x1F)<<3;
  466. }
  467. }
  468. void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  469. {
  470. long i;
  471. long num_pixels = src_size >> 1;
  472. for(i=0; i<num_pixels; i++)
  473. {
  474. unsigned b,g,r;
  475. register uint16_t rgb;
  476. rgb = src[2*i];
  477. r = rgb&0x1F;
  478. g = (rgb&0x3E0)>>5;
  479. b = (rgb&0x7C00)>>10;
  480. dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
  481. }
  482. }
  483. void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  484. {
  485. long i;
  486. long num_pixels = src_size >> 1;
  487. for(i=0; i<num_pixels; i++)
  488. {
  489. unsigned b,g,r;
  490. register uint16_t rgb;
  491. rgb = src[2*i];
  492. r = rgb&0x1F;
  493. g = (rgb&0x3E0)>>5;
  494. b = (rgb&0x7C00)>>10;
  495. dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
  496. }
  497. }
  498. void rgb8tobgr8(const uint8_t *src, uint8_t *dst, long src_size)
  499. {
  500. long i;
  501. long num_pixels = src_size;
  502. for(i=0; i<num_pixels; i++)
  503. {
  504. unsigned b,g,r;
  505. register uint8_t rgb;
  506. rgb = src[i];
  507. r = (rgb&0x07);
  508. g = (rgb&0x38)>>3;
  509. b = (rgb&0xC0)>>6;
  510. dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
  511. }
  512. }