You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

666 lines
21KB

  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. rgb15to16= rgb15to16_MMX2;
  203. rgb15to24= rgb15to24_MMX2;
  204. rgb15to32= rgb15to32_MMX2;
  205. rgb16to24= rgb16to24_MMX2;
  206. rgb16to32= rgb16to32_MMX2;
  207. rgb16to15= rgb16to15_MMX2;
  208. rgb24to16= rgb24to16_MMX2;
  209. rgb24to15= rgb24to15_MMX2;
  210. rgb24to32= rgb24to32_MMX2;
  211. rgb32to16= rgb32to16_MMX2;
  212. rgb32to15= rgb32to15_MMX2;
  213. rgb32to24= rgb32to24_MMX2;
  214. rgb24tobgr15= rgb24tobgr15_MMX2;
  215. rgb24tobgr16= rgb24tobgr16_MMX2;
  216. rgb24tobgr24= rgb24tobgr24_MMX2;
  217. rgb32tobgr32= rgb32tobgr32_MMX2;
  218. rgb32tobgr16= rgb32tobgr16_MMX2;
  219. rgb32tobgr15= rgb32tobgr15_MMX2;
  220. yv12toyuy2= yv12toyuy2_MMX2;
  221. yv12touyvy= yv12touyvy_MMX2;
  222. yuv422ptoyuy2= yuv422ptoyuy2_MMX2;
  223. yuy2toyv12= yuy2toyv12_MMX2;
  224. // uyvytoyv12= uyvytoyv12_MMX2;
  225. // yvu9toyv12= yvu9toyv12_MMX2;
  226. planar2x= planar2x_MMX2;
  227. rgb24toyv12= rgb24toyv12_MMX2;
  228. interleaveBytes= interleaveBytes_MMX2;
  229. vu9_to_vu12= vu9_to_vu12_MMX2;
  230. yvu9_to_yuy2= yvu9_to_yuy2_MMX2;
  231. }else if(flags & SWS_CPU_CAPS_3DNOW){
  232. rgb15to16= rgb15to16_3DNOW;
  233. rgb15to24= rgb15to24_3DNOW;
  234. rgb15to32= rgb15to32_3DNOW;
  235. rgb16to24= rgb16to24_3DNOW;
  236. rgb16to32= rgb16to32_3DNOW;
  237. rgb16to15= rgb16to15_3DNOW;
  238. rgb24to16= rgb24to16_3DNOW;
  239. rgb24to15= rgb24to15_3DNOW;
  240. rgb24to32= rgb24to32_3DNOW;
  241. rgb32to16= rgb32to16_3DNOW;
  242. rgb32to15= rgb32to15_3DNOW;
  243. rgb32to24= rgb32to24_3DNOW;
  244. rgb24tobgr15= rgb24tobgr15_3DNOW;
  245. rgb24tobgr16= rgb24tobgr16_3DNOW;
  246. rgb24tobgr24= rgb24tobgr24_3DNOW;
  247. rgb32tobgr32= rgb32tobgr32_3DNOW;
  248. rgb32tobgr16= rgb32tobgr16_3DNOW;
  249. rgb32tobgr15= rgb32tobgr15_3DNOW;
  250. yv12toyuy2= yv12toyuy2_3DNOW;
  251. yv12touyvy= yv12touyvy_3DNOW;
  252. yuv422ptoyuy2= yuv422ptoyuy2_3DNOW;
  253. yuy2toyv12= yuy2toyv12_3DNOW;
  254. // uyvytoyv12= uyvytoyv12_3DNOW;
  255. // yvu9toyv12= yvu9toyv12_3DNOW;
  256. planar2x= planar2x_3DNOW;
  257. rgb24toyv12= rgb24toyv12_3DNOW;
  258. interleaveBytes= interleaveBytes_3DNOW;
  259. vu9_to_vu12= vu9_to_vu12_3DNOW;
  260. yvu9_to_yuy2= yvu9_to_yuy2_3DNOW;
  261. }else if(flags & SWS_CPU_CAPS_MMX){
  262. rgb15to16= rgb15to16_MMX;
  263. rgb15to24= rgb15to24_MMX;
  264. rgb15to32= rgb15to32_MMX;
  265. rgb16to24= rgb16to24_MMX;
  266. rgb16to32= rgb16to32_MMX;
  267. rgb16to15= rgb16to15_MMX;
  268. rgb24to16= rgb24to16_MMX;
  269. rgb24to15= rgb24to15_MMX;
  270. rgb24to32= rgb24to32_MMX;
  271. rgb32to16= rgb32to16_MMX;
  272. rgb32to15= rgb32to15_MMX;
  273. rgb32to24= rgb32to24_MMX;
  274. rgb24tobgr15= rgb24tobgr15_MMX;
  275. rgb24tobgr16= rgb24tobgr16_MMX;
  276. rgb24tobgr24= rgb24tobgr24_MMX;
  277. rgb32tobgr32= rgb32tobgr32_MMX;
  278. rgb32tobgr16= rgb32tobgr16_MMX;
  279. rgb32tobgr15= rgb32tobgr15_MMX;
  280. yv12toyuy2= yv12toyuy2_MMX;
  281. yv12touyvy= yv12touyvy_MMX;
  282. yuv422ptoyuy2= yuv422ptoyuy2_MMX;
  283. yuy2toyv12= yuy2toyv12_MMX;
  284. // uyvytoyv12= uyvytoyv12_MMX;
  285. // yvu9toyv12= yvu9toyv12_MMX;
  286. planar2x= planar2x_MMX;
  287. rgb24toyv12= rgb24toyv12_MMX;
  288. interleaveBytes= interleaveBytes_MMX;
  289. vu9_to_vu12= vu9_to_vu12_MMX;
  290. yvu9_to_yuy2= yvu9_to_yuy2_MMX;
  291. }else
  292. #endif /* defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX) */
  293. {
  294. rgb15to16= rgb15to16_C;
  295. rgb15to24= rgb15to24_C;
  296. rgb15to32= rgb15to32_C;
  297. rgb16to24= rgb16to24_C;
  298. rgb16to32= rgb16to32_C;
  299. rgb16to15= rgb16to15_C;
  300. rgb24to16= rgb24to16_C;
  301. rgb24to15= rgb24to15_C;
  302. rgb24to32= rgb24to32_C;
  303. rgb32to16= rgb32to16_C;
  304. rgb32to15= rgb32to15_C;
  305. rgb32to24= rgb32to24_C;
  306. rgb24tobgr15= rgb24tobgr15_C;
  307. rgb24tobgr16= rgb24tobgr16_C;
  308. rgb24tobgr24= rgb24tobgr24_C;
  309. rgb32tobgr32= rgb32tobgr32_C;
  310. rgb32tobgr16= rgb32tobgr16_C;
  311. rgb32tobgr15= rgb32tobgr15_C;
  312. yv12toyuy2= yv12toyuy2_C;
  313. yv12touyvy= yv12touyvy_C;
  314. yuv422ptoyuy2= yuv422ptoyuy2_C;
  315. yuy2toyv12= yuy2toyv12_C;
  316. // uyvytoyv12= uyvytoyv12_C;
  317. // yvu9toyv12= yvu9toyv12_C;
  318. planar2x= planar2x_C;
  319. rgb24toyv12= rgb24toyv12_C;
  320. interleaveBytes= interleaveBytes_C;
  321. vu9_to_vu12= vu9_to_vu12_C;
  322. yvu9_to_yuy2= yvu9_to_yuy2_C;
  323. }
  324. }
  325. /**
  326. * Palette is assumed to contain BGR32.
  327. */
  328. void palette8torgb32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  329. {
  330. long i;
  331. /*
  332. for(i=0; i<num_pixels; i++)
  333. ((unsigned *)dst)[i] = ((unsigned *)palette)[ src[i] ];
  334. */
  335. for(i=0; i<num_pixels; i++)
  336. {
  337. #ifdef WORDS_BIGENDIAN
  338. dst[3]= palette[ src[i]*4+2 ];
  339. dst[2]= palette[ src[i]*4+1 ];
  340. dst[1]= palette[ src[i]*4+0 ];
  341. #else
  342. //FIXME slow?
  343. dst[0]= palette[ src[i]*4+2 ];
  344. dst[1]= palette[ src[i]*4+1 ];
  345. dst[2]= palette[ src[i]*4+0 ];
  346. //dst[3]= 0; /* do we need this cleansing? */
  347. #endif
  348. dst+= 4;
  349. }
  350. }
  351. void palette8tobgr32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  352. {
  353. long i;
  354. for(i=0; i<num_pixels; i++)
  355. {
  356. #ifdef WORDS_BIGENDIAN
  357. dst[3]= palette[ src[i]*4+0 ];
  358. dst[2]= palette[ src[i]*4+1 ];
  359. dst[1]= palette[ src[i]*4+2 ];
  360. #else
  361. //FIXME slow?
  362. dst[0]= palette[ src[i]*4+0 ];
  363. dst[1]= palette[ src[i]*4+1 ];
  364. dst[2]= palette[ src[i]*4+2 ];
  365. //dst[3]= 0; /* do we need this cleansing? */
  366. #endif
  367. dst+= 4;
  368. }
  369. }
  370. /**
  371. * Palette is assumed to contain BGR32.
  372. */
  373. void palette8torgb24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  374. {
  375. long i;
  376. /*
  377. writes 1 byte o much and might cause alignment issues on some architectures?
  378. for(i=0; i<num_pixels; i++)
  379. ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
  380. */
  381. for(i=0; i<num_pixels; i++)
  382. {
  383. //FIXME slow?
  384. dst[0]= palette[ src[i]*4+2 ];
  385. dst[1]= palette[ src[i]*4+1 ];
  386. dst[2]= palette[ src[i]*4+0 ];
  387. dst+= 3;
  388. }
  389. }
  390. void palette8tobgr24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  391. {
  392. long i;
  393. /*
  394. writes 1 byte o much and might cause alignment issues on some architectures?
  395. for(i=0; i<num_pixels; i++)
  396. ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
  397. */
  398. for(i=0; i<num_pixels; i++)
  399. {
  400. //FIXME slow?
  401. dst[0]= palette[ src[i]*4+0 ];
  402. dst[1]= palette[ src[i]*4+1 ];
  403. dst[2]= palette[ src[i]*4+2 ];
  404. dst+= 3;
  405. }
  406. }
  407. /**
  408. * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
  409. */
  410. void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  411. {
  412. long i;
  413. for(i=0; i<num_pixels; i++)
  414. ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
  415. }
  416. void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  417. {
  418. long i;
  419. for(i=0; i<num_pixels; i++)
  420. ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
  421. }
  422. /**
  423. * Palette is assumed to contain BGR15, see rgb32to15 to convert the palette.
  424. */
  425. void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  426. {
  427. long i;
  428. for(i=0; i<num_pixels; i++)
  429. ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
  430. }
  431. void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  432. {
  433. long i;
  434. for(i=0; i<num_pixels; i++)
  435. ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
  436. }
  437. void rgb32tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  438. {
  439. long i;
  440. long num_pixels = src_size >> 2;
  441. for(i=0; i<num_pixels; i++)
  442. {
  443. #ifdef WORDS_BIGENDIAN
  444. /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
  445. dst[3*i + 0] = src[4*i + 1];
  446. dst[3*i + 1] = src[4*i + 2];
  447. dst[3*i + 2] = src[4*i + 3];
  448. #else
  449. dst[3*i + 0] = src[4*i + 2];
  450. dst[3*i + 1] = src[4*i + 1];
  451. dst[3*i + 2] = src[4*i + 0];
  452. #endif
  453. }
  454. }
  455. void rgb24tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  456. {
  457. long i;
  458. for(i=0; 3*i<src_size; i++)
  459. {
  460. #ifdef WORDS_BIGENDIAN
  461. /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
  462. dst[4*i + 0] = 0;
  463. dst[4*i + 1] = src[3*i + 0];
  464. dst[4*i + 2] = src[3*i + 1];
  465. dst[4*i + 3] = src[3*i + 2];
  466. #else
  467. dst[4*i + 0] = src[3*i + 2];
  468. dst[4*i + 1] = src[3*i + 1];
  469. dst[4*i + 2] = src[3*i + 0];
  470. dst[4*i + 3] = 0;
  471. #endif
  472. }
  473. }
  474. void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  475. {
  476. const uint16_t *end;
  477. uint8_t *d = (uint8_t *)dst;
  478. const uint16_t *s = (uint16_t *)src;
  479. end = s + src_size/2;
  480. while(s < end)
  481. {
  482. register uint16_t bgr;
  483. bgr = *s++;
  484. #ifdef WORDS_BIGENDIAN
  485. *d++ = 0;
  486. *d++ = (bgr&0x1F)<<3;
  487. *d++ = (bgr&0x7E0)>>3;
  488. *d++ = (bgr&0xF800)>>8;
  489. #else
  490. *d++ = (bgr&0xF800)>>8;
  491. *d++ = (bgr&0x7E0)>>3;
  492. *d++ = (bgr&0x1F)<<3;
  493. *d++ = 0;
  494. #endif
  495. }
  496. }
  497. void rgb16tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  498. {
  499. const uint16_t *end;
  500. uint8_t *d = (uint8_t *)dst;
  501. const uint16_t *s = (const uint16_t *)src;
  502. end = s + src_size/2;
  503. while(s < end)
  504. {
  505. register uint16_t bgr;
  506. bgr = *s++;
  507. *d++ = (bgr&0xF800)>>8;
  508. *d++ = (bgr&0x7E0)>>3;
  509. *d++ = (bgr&0x1F)<<3;
  510. }
  511. }
  512. void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  513. {
  514. long i;
  515. long num_pixels = src_size >> 1;
  516. for(i=0; i<num_pixels; i++)
  517. {
  518. unsigned b,g,r;
  519. register uint16_t rgb;
  520. rgb = src[2*i];
  521. r = rgb&0x1F;
  522. g = (rgb&0x7E0)>>5;
  523. b = (rgb&0xF800)>>11;
  524. dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
  525. }
  526. }
  527. void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  528. {
  529. long i;
  530. long num_pixels = src_size >> 1;
  531. for(i=0; i<num_pixels; i++)
  532. {
  533. unsigned b,g,r;
  534. register uint16_t rgb;
  535. rgb = src[2*i];
  536. r = rgb&0x1F;
  537. g = (rgb&0x7E0)>>5;
  538. b = (rgb&0xF800)>>11;
  539. dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
  540. }
  541. }
  542. void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  543. {
  544. const uint16_t *end;
  545. uint8_t *d = (uint8_t *)dst;
  546. const uint16_t *s = (const uint16_t *)src;
  547. end = s + src_size/2;
  548. while(s < end)
  549. {
  550. register uint16_t bgr;
  551. bgr = *s++;
  552. #ifdef WORDS_BIGENDIAN
  553. *d++ = 0;
  554. *d++ = (bgr&0x1F)<<3;
  555. *d++ = (bgr&0x3E0)>>2;
  556. *d++ = (bgr&0x7C00)>>7;
  557. #else
  558. *d++ = (bgr&0x7C00)>>7;
  559. *d++ = (bgr&0x3E0)>>2;
  560. *d++ = (bgr&0x1F)<<3;
  561. *d++ = 0;
  562. #endif
  563. }
  564. }
  565. void rgb15tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  566. {
  567. const uint16_t *end;
  568. uint8_t *d = (uint8_t *)dst;
  569. const uint16_t *s = (uint16_t *)src;
  570. end = s + src_size/2;
  571. while(s < end)
  572. {
  573. register uint16_t bgr;
  574. bgr = *s++;
  575. *d++ = (bgr&0x7C00)>>7;
  576. *d++ = (bgr&0x3E0)>>2;
  577. *d++ = (bgr&0x1F)<<3;
  578. }
  579. }
  580. void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  581. {
  582. long i;
  583. long num_pixels = src_size >> 1;
  584. for(i=0; i<num_pixels; i++)
  585. {
  586. unsigned b,g,r;
  587. register uint16_t rgb;
  588. rgb = src[2*i];
  589. r = rgb&0x1F;
  590. g = (rgb&0x3E0)>>5;
  591. b = (rgb&0x7C00)>>10;
  592. dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
  593. }
  594. }
  595. void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  596. {
  597. long i;
  598. long num_pixels = src_size >> 1;
  599. for(i=0; i<num_pixels; i++)
  600. {
  601. unsigned b,g,r;
  602. register uint16_t rgb;
  603. rgb = src[2*i];
  604. r = rgb&0x1F;
  605. g = (rgb&0x3E0)>>5;
  606. b = (rgb&0x7C00)>>10;
  607. dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
  608. }
  609. }
  610. void rgb8tobgr8(const uint8_t *src, uint8_t *dst, long src_size)
  611. {
  612. long i;
  613. long num_pixels = src_size;
  614. for(i=0; i<num_pixels; i++)
  615. {
  616. unsigned b,g,r;
  617. register uint8_t rgb;
  618. rgb = src[i];
  619. r = (rgb&0x07);
  620. g = (rgb&0x38)>>3;
  621. b = (rgb&0xC0)>>6;
  622. dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
  623. }
  624. }