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.

661 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 program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <inttypes.h>
  25. #include "config.h"
  26. #include "rgb2rgb.h"
  27. #include "swscale.h"
  28. #include "swscale_internal.h"
  29. #include "x86_cpu.h"
  30. #include "bswap.h"
  31. #ifdef USE_FASTMEMCPY
  32. #include "libvo/fastmemcpy.h"
  33. #endif
  34. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  35. void (*rgb24to32)(const uint8_t *src,uint8_t *dst,long src_size);
  36. void (*rgb24to16)(const uint8_t *src,uint8_t *dst,long src_size);
  37. void (*rgb24to15)(const uint8_t *src,uint8_t *dst,long src_size);
  38. void (*rgb32to24)(const uint8_t *src,uint8_t *dst,long src_size);
  39. void (*rgb32to16)(const uint8_t *src,uint8_t *dst,long src_size);
  40. void (*rgb32to15)(const uint8_t *src,uint8_t *dst,long src_size);
  41. void (*rgb15to16)(const uint8_t *src,uint8_t *dst,long src_size);
  42. void (*rgb15to24)(const uint8_t *src,uint8_t *dst,long src_size);
  43. void (*rgb15to32)(const uint8_t *src,uint8_t *dst,long src_size);
  44. void (*rgb16to15)(const uint8_t *src,uint8_t *dst,long src_size);
  45. void (*rgb16to24)(const uint8_t *src,uint8_t *dst,long src_size);
  46. void (*rgb16to32)(const uint8_t *src,uint8_t *dst,long src_size);
  47. //void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
  48. void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  49. void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
  50. void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
  51. void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
  52. //void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  53. void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
  54. void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
  55. void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  56. long width, long height,
  57. long lumStride, long chromStride, long dstStride);
  58. void (*yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  59. long width, long height,
  60. long lumStride, long chromStride, long dstStride);
  61. void (*yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  62. long width, long height,
  63. long lumStride, long chromStride, long dstStride);
  64. void (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  65. long width, long height,
  66. long lumStride, long chromStride, long srcStride);
  67. void (*rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  68. long width, long height,
  69. long lumStride, long chromStride, long srcStride);
  70. void (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
  71. long srcStride, long dstStride);
  72. void (*interleaveBytes)(uint8_t *src1, uint8_t *src2, uint8_t *dst,
  73. long width, long height, long src1Stride,
  74. long src2Stride, long dstStride);
  75. void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
  76. uint8_t *dst1, uint8_t *dst2,
  77. long width, long height,
  78. long srcStride1, long srcStride2,
  79. long dstStride1, long dstStride2);
  80. void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  81. uint8_t *dst,
  82. long width, long height,
  83. long srcStride1, long srcStride2,
  84. long srcStride3, long dstStride);
  85. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  86. static const uint64_t mmx_null __attribute__((aligned(8))) = 0x0000000000000000ULL;
  87. static const uint64_t mmx_one __attribute__((aligned(8))) = 0xFFFFFFFFFFFFFFFFULL;
  88. static const uint64_t mask32b attribute_used __attribute__((aligned(8))) = 0x000000FF000000FFULL;
  89. static const uint64_t mask32g attribute_used __attribute__((aligned(8))) = 0x0000FF000000FF00ULL;
  90. static const uint64_t mask32r attribute_used __attribute__((aligned(8))) = 0x00FF000000FF0000ULL;
  91. static const uint64_t mask32 __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
  92. static const uint64_t mask3216br __attribute__((aligned(8)))=0x00F800F800F800F8ULL;
  93. static const uint64_t mask3216g __attribute__((aligned(8)))=0x0000FC000000FC00ULL;
  94. static const uint64_t mask3215g __attribute__((aligned(8)))=0x0000F8000000F800ULL;
  95. static const uint64_t mul3216 __attribute__((aligned(8))) = 0x2000000420000004ULL;
  96. static const uint64_t mul3215 __attribute__((aligned(8))) = 0x2000000820000008ULL;
  97. static const uint64_t mask24b attribute_used __attribute__((aligned(8))) = 0x00FF0000FF0000FFULL;
  98. static const uint64_t mask24g attribute_used __attribute__((aligned(8))) = 0xFF0000FF0000FF00ULL;
  99. static const uint64_t mask24r attribute_used __attribute__((aligned(8))) = 0x0000FF0000FF0000ULL;
  100. static const uint64_t mask24l __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
  101. static const uint64_t mask24h __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
  102. static const uint64_t mask24hh __attribute__((aligned(8))) = 0xffff000000000000ULL;
  103. static const uint64_t mask24hhh __attribute__((aligned(8))) = 0xffffffff00000000ULL;
  104. static const uint64_t mask24hhhh __attribute__((aligned(8))) = 0xffffffffffff0000ULL;
  105. static const uint64_t mask15b __attribute__((aligned(8))) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
  106. static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
  107. static const uint64_t mask15s __attribute__((aligned(8))) = 0xFFE0FFE0FFE0FFE0ULL;
  108. static const uint64_t mask15g __attribute__((aligned(8))) = 0x03E003E003E003E0ULL;
  109. static const uint64_t mask15r __attribute__((aligned(8))) = 0x7C007C007C007C00ULL;
  110. #define mask16b mask15b
  111. static const uint64_t mask16g __attribute__((aligned(8))) = 0x07E007E007E007E0ULL;
  112. static const uint64_t mask16r __attribute__((aligned(8))) = 0xF800F800F800F800ULL;
  113. static const uint64_t red_16mask __attribute__((aligned(8))) = 0x0000f8000000f800ULL;
  114. static const uint64_t green_16mask __attribute__((aligned(8)))= 0x000007e0000007e0ULL;
  115. static const uint64_t blue_16mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
  116. static const uint64_t red_15mask __attribute__((aligned(8))) = 0x00007c000000f800ULL;
  117. static const uint64_t green_15mask __attribute__((aligned(8)))= 0x000003e0000007e0ULL;
  118. static const uint64_t blue_15mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
  119. #ifdef FAST_BGR2YV12
  120. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
  121. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  122. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  123. #else
  124. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  125. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  126. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  127. #endif
  128. static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
  129. static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8)))= 0x8080808080808080ULL;
  130. static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
  131. #if 0
  132. static volatile uint64_t __attribute__((aligned(8))) b5Dither;
  133. static volatile uint64_t __attribute__((aligned(8))) g5Dither;
  134. static volatile uint64_t __attribute__((aligned(8))) g6Dither;
  135. static volatile uint64_t __attribute__((aligned(8))) r5Dither;
  136. static uint64_t __attribute__((aligned(8))) dither4[2]={
  137. 0x0103010301030103LL,
  138. 0x0200020002000200LL,};
  139. static uint64_t __attribute__((aligned(8))) dither8[2]={
  140. 0x0602060206020602LL,
  141. 0x0004000400040004LL,};
  142. #endif
  143. #endif /* defined(ARCH_X86) || defined(ARCH_X86_64) */
  144. #define RGB2YUV_SHIFT 8
  145. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  146. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  147. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  148. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  149. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  150. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  151. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  152. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  153. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  154. //Note: we have C, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  155. //Plain C versions
  156. #undef HAVE_MMX
  157. #undef HAVE_MMX2
  158. #undef HAVE_3DNOW
  159. #undef HAVE_SSE2
  160. #define RENAME(a) a ## _C
  161. #include "rgb2rgb_template.c"
  162. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  163. //MMX versions
  164. #undef RENAME
  165. #define HAVE_MMX
  166. #undef HAVE_MMX2
  167. #undef HAVE_3DNOW
  168. #undef HAVE_SSE2
  169. #define RENAME(a) a ## _MMX
  170. #include "rgb2rgb_template.c"
  171. //MMX2 versions
  172. #undef RENAME
  173. #define HAVE_MMX
  174. #define HAVE_MMX2
  175. #undef HAVE_3DNOW
  176. #undef HAVE_SSE2
  177. #define RENAME(a) a ## _MMX2
  178. #include "rgb2rgb_template.c"
  179. //3DNOW versions
  180. #undef RENAME
  181. #define HAVE_MMX
  182. #undef HAVE_MMX2
  183. #define HAVE_3DNOW
  184. #undef HAVE_SSE2
  185. #define RENAME(a) a ## _3DNOW
  186. #include "rgb2rgb_template.c"
  187. #endif //ARCH_X86 || ARCH_X86_64
  188. /*
  189. rgb15->rgb16 Original by Strepto/Astral
  190. ported to gcc & bugfixed : A'rpi
  191. MMX2, 3DNOW optimization by Nick Kurshev
  192. 32bit c version, and and&add trick by Michael Niedermayer
  193. */
  194. void sws_rgb2rgb_init(int flags){
  195. #if defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX)
  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;
  216. yv12touyvy= yv12touyvy_MMX2;
  217. yuv422ptoyuy2= yuv422ptoyuy2_MMX2;
  218. yuy2toyv12= yuy2toyv12_MMX2;
  219. // uyvytoyv12= uyvytoyv12_MMX2;
  220. // yvu9toyv12= yvu9toyv12_MMX2;
  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;
  246. yv12touyvy= yv12touyvy_3DNOW;
  247. yuv422ptoyuy2= yuv422ptoyuy2_3DNOW;
  248. yuy2toyv12= yuy2toyv12_3DNOW;
  249. // uyvytoyv12= uyvytoyv12_3DNOW;
  250. // yvu9toyv12= yvu9toyv12_3DNOW;
  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;
  276. yv12touyvy= yv12touyvy_MMX;
  277. yuv422ptoyuy2= yuv422ptoyuy2_MMX;
  278. yuy2toyv12= yuy2toyv12_MMX;
  279. // uyvytoyv12= uyvytoyv12_MMX;
  280. // yvu9toyv12= yvu9toyv12_MMX;
  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
  287. #endif /* defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX) */
  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;
  308. yv12touyvy= yv12touyvy_C;
  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. }
  319. }
  320. /**
  321. * Pallete is assumed to contain bgr32
  322. */
  323. void palette8torgb32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  324. {
  325. long i;
  326. /*
  327. for(i=0; i<num_pixels; i++)
  328. ((unsigned *)dst)[i] = ((unsigned *)palette)[ src[i] ];
  329. */
  330. for(i=0; i<num_pixels; i++)
  331. {
  332. #ifdef WORDS_BIGENDIAN
  333. dst[3]= palette[ src[i]*4+2 ];
  334. dst[2]= palette[ src[i]*4+1 ];
  335. dst[1]= palette[ src[i]*4+0 ];
  336. #else
  337. //FIXME slow?
  338. dst[0]= palette[ src[i]*4+2 ];
  339. dst[1]= palette[ src[i]*4+1 ];
  340. dst[2]= palette[ src[i]*4+0 ];
  341. //dst[3]= 0; /* do we need this cleansing? */
  342. #endif
  343. dst+= 4;
  344. }
  345. }
  346. void palette8tobgr32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  347. {
  348. long i;
  349. for(i=0; i<num_pixels; i++)
  350. {
  351. #ifdef WORDS_BIGENDIAN
  352. dst[3]= palette[ src[i]*4+0 ];
  353. dst[2]= palette[ src[i]*4+1 ];
  354. dst[1]= palette[ src[i]*4+2 ];
  355. #else
  356. //FIXME slow?
  357. dst[0]= palette[ src[i]*4+0 ];
  358. dst[1]= palette[ src[i]*4+1 ];
  359. dst[2]= palette[ src[i]*4+2 ];
  360. //dst[3]= 0; /* do we need this cleansing? */
  361. #endif
  362. dst+= 4;
  363. }
  364. }
  365. /**
  366. * Pallete is assumed to contain bgr32
  367. */
  368. void palette8torgb24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  369. {
  370. long i;
  371. /*
  372. writes 1 byte o much and might cause alignment issues on some architectures?
  373. for(i=0; i<num_pixels; i++)
  374. ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
  375. */
  376. for(i=0; i<num_pixels; i++)
  377. {
  378. //FIXME slow?
  379. dst[0]= palette[ src[i]*4+2 ];
  380. dst[1]= palette[ src[i]*4+1 ];
  381. dst[2]= palette[ src[i]*4+0 ];
  382. dst+= 3;
  383. }
  384. }
  385. void palette8tobgr24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  386. {
  387. long i;
  388. /*
  389. writes 1 byte o much and might cause alignment issues on some architectures?
  390. for(i=0; i<num_pixels; i++)
  391. ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
  392. */
  393. for(i=0; i<num_pixels; i++)
  394. {
  395. //FIXME slow?
  396. dst[0]= palette[ src[i]*4+0 ];
  397. dst[1]= palette[ src[i]*4+1 ];
  398. dst[2]= palette[ src[i]*4+2 ];
  399. dst+= 3;
  400. }
  401. }
  402. /**
  403. * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
  404. */
  405. void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  406. {
  407. long i;
  408. for(i=0; i<num_pixels; i++)
  409. ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
  410. }
  411. void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  412. {
  413. long i;
  414. for(i=0; i<num_pixels; i++)
  415. ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
  416. }
  417. /**
  418. * Pallete is assumed to contain bgr15, see rgb32to15 to convert the palette
  419. */
  420. void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  421. {
  422. long i;
  423. for(i=0; i<num_pixels; i++)
  424. ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
  425. }
  426. void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  427. {
  428. long i;
  429. for(i=0; i<num_pixels; i++)
  430. ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
  431. }
  432. void rgb32tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  433. {
  434. long i;
  435. long num_pixels = src_size >> 2;
  436. for(i=0; i<num_pixels; i++)
  437. {
  438. #ifdef WORDS_BIGENDIAN
  439. /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
  440. dst[3*i + 0] = src[4*i + 1];
  441. dst[3*i + 1] = src[4*i + 2];
  442. dst[3*i + 2] = src[4*i + 3];
  443. #else
  444. dst[3*i + 0] = src[4*i + 2];
  445. dst[3*i + 1] = src[4*i + 1];
  446. dst[3*i + 2] = src[4*i + 0];
  447. #endif
  448. }
  449. }
  450. void rgb24tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  451. {
  452. long i;
  453. for(i=0; 3*i<src_size; i++)
  454. {
  455. #ifdef WORDS_BIGENDIAN
  456. /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
  457. dst[4*i + 0] = 0;
  458. dst[4*i + 1] = src[3*i + 0];
  459. dst[4*i + 2] = src[3*i + 1];
  460. dst[4*i + 3] = src[3*i + 2];
  461. #else
  462. dst[4*i + 0] = src[3*i + 2];
  463. dst[4*i + 1] = src[3*i + 1];
  464. dst[4*i + 2] = src[3*i + 0];
  465. dst[4*i + 3] = 0;
  466. #endif
  467. }
  468. }
  469. void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  470. {
  471. const uint16_t *end;
  472. uint8_t *d = (uint8_t *)dst;
  473. const uint16_t *s = (uint16_t *)src;
  474. end = s + src_size/2;
  475. while(s < end)
  476. {
  477. register uint16_t bgr;
  478. bgr = *s++;
  479. #ifdef WORDS_BIGENDIAN
  480. *d++ = 0;
  481. *d++ = (bgr&0x1F)<<3;
  482. *d++ = (bgr&0x7E0)>>3;
  483. *d++ = (bgr&0xF800)>>8;
  484. #else
  485. *d++ = (bgr&0xF800)>>8;
  486. *d++ = (bgr&0x7E0)>>3;
  487. *d++ = (bgr&0x1F)<<3;
  488. *d++ = 0;
  489. #endif
  490. }
  491. }
  492. void rgb16tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  493. {
  494. const uint16_t *end;
  495. uint8_t *d = (uint8_t *)dst;
  496. const uint16_t *s = (const uint16_t *)src;
  497. end = s + src_size/2;
  498. while(s < end)
  499. {
  500. register uint16_t bgr;
  501. bgr = *s++;
  502. *d++ = (bgr&0xF800)>>8;
  503. *d++ = (bgr&0x7E0)>>3;
  504. *d++ = (bgr&0x1F)<<3;
  505. }
  506. }
  507. void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  508. {
  509. long i;
  510. long num_pixels = src_size >> 1;
  511. for(i=0; i<num_pixels; i++)
  512. {
  513. unsigned b,g,r;
  514. register uint16_t rgb;
  515. rgb = src[2*i];
  516. r = rgb&0x1F;
  517. g = (rgb&0x7E0)>>5;
  518. b = (rgb&0xF800)>>11;
  519. dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
  520. }
  521. }
  522. void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  523. {
  524. long i;
  525. long num_pixels = src_size >> 1;
  526. for(i=0; i<num_pixels; i++)
  527. {
  528. unsigned b,g,r;
  529. register uint16_t rgb;
  530. rgb = src[2*i];
  531. r = rgb&0x1F;
  532. g = (rgb&0x7E0)>>5;
  533. b = (rgb&0xF800)>>11;
  534. dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
  535. }
  536. }
  537. void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  538. {
  539. const uint16_t *end;
  540. uint8_t *d = (uint8_t *)dst;
  541. const uint16_t *s = (const uint16_t *)src;
  542. end = s + src_size/2;
  543. while(s < end)
  544. {
  545. register uint16_t bgr;
  546. bgr = *s++;
  547. #ifdef WORDS_BIGENDIAN
  548. *d++ = 0;
  549. *d++ = (bgr&0x1F)<<3;
  550. *d++ = (bgr&0x3E0)>>2;
  551. *d++ = (bgr&0x7C00)>>7;
  552. #else
  553. *d++ = (bgr&0x7C00)>>7;
  554. *d++ = (bgr&0x3E0)>>2;
  555. *d++ = (bgr&0x1F)<<3;
  556. *d++ = 0;
  557. #endif
  558. }
  559. }
  560. void rgb15tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  561. {
  562. const uint16_t *end;
  563. uint8_t *d = (uint8_t *)dst;
  564. const uint16_t *s = (uint16_t *)src;
  565. end = s + src_size/2;
  566. while(s < end)
  567. {
  568. register uint16_t bgr;
  569. bgr = *s++;
  570. *d++ = (bgr&0x7C00)>>7;
  571. *d++ = (bgr&0x3E0)>>2;
  572. *d++ = (bgr&0x1F)<<3;
  573. }
  574. }
  575. void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  576. {
  577. long i;
  578. long num_pixels = src_size >> 1;
  579. for(i=0; i<num_pixels; i++)
  580. {
  581. unsigned b,g,r;
  582. register uint16_t rgb;
  583. rgb = src[2*i];
  584. r = rgb&0x1F;
  585. g = (rgb&0x3E0)>>5;
  586. b = (rgb&0x7C00)>>10;
  587. dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
  588. }
  589. }
  590. void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  591. {
  592. long i;
  593. long num_pixels = src_size >> 1;
  594. for(i=0; i<num_pixels; i++)
  595. {
  596. unsigned b,g,r;
  597. register uint16_t rgb;
  598. rgb = src[2*i];
  599. r = rgb&0x1F;
  600. g = (rgb&0x3E0)>>5;
  601. b = (rgb&0x7C00)>>10;
  602. dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
  603. }
  604. }
  605. void rgb8tobgr8(const uint8_t *src, uint8_t *dst, long src_size)
  606. {
  607. long i;
  608. long num_pixels = src_size;
  609. for(i=0; i<num_pixels; i++)
  610. {
  611. unsigned b,g,r;
  612. register uint8_t rgb;
  613. rgb = src[i];
  614. r = (rgb&0x07);
  615. g = (rgb&0x38)>>3;
  616. b = (rgb&0xC0)>>6;
  617. dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
  618. }
  619. }