rgb2rgb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. #include <inttypes.h>
  27. #include "config.h"
  28. #include "rgb2rgb.h"
  29. #include "swscale.h"
  30. #include "swscale_internal.h"
  31. #include "x86_cpu.h"
  32. #include "bswap.h"
  33. #ifdef USE_FASTMEMCPY
  34. #include "libvo/fastmemcpy.h"
  35. #endif
  36. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  37. void (*rgb24to32)(const uint8_t *src,uint8_t *dst,long src_size);
  38. void (*rgb24to16)(const uint8_t *src,uint8_t *dst,long src_size);
  39. void (*rgb24to15)(const uint8_t *src,uint8_t *dst,long src_size);
  40. void (*rgb32to24)(const uint8_t *src,uint8_t *dst,long src_size);
  41. void (*rgb32to16)(const uint8_t *src,uint8_t *dst,long src_size);
  42. void (*rgb32to15)(const uint8_t *src,uint8_t *dst,long src_size);
  43. void (*rgb15to16)(const uint8_t *src,uint8_t *dst,long src_size);
  44. void (*rgb15to24)(const uint8_t *src,uint8_t *dst,long src_size);
  45. void (*rgb15to32)(const uint8_t *src,uint8_t *dst,long src_size);
  46. void (*rgb16to15)(const uint8_t *src,uint8_t *dst,long src_size);
  47. void (*rgb16to24)(const uint8_t *src,uint8_t *dst,long src_size);
  48. void (*rgb16to32)(const uint8_t *src,uint8_t *dst,long src_size);
  49. //void (*rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
  50. void (*rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  51. void (*rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
  52. void (*rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
  53. void (*rgb32tobgr32)(const uint8_t *src, uint8_t *dst, long src_size);
  54. //void (*rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size);
  55. void (*rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size);
  56. void (*rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size);
  57. void (*yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  58. long width, long height,
  59. long lumStride, long chromStride, long dstStride);
  60. void (*yv12touyvy)(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 (*yuv422ptoyuy2)(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 (*yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  67. long width, long height,
  68. long lumStride, long chromStride, long srcStride);
  69. void (*rgb24toyv12)(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 (*planar2x)(const uint8_t *src, uint8_t *dst, long width, long height,
  73. long srcStride, long dstStride);
  74. void (*interleaveBytes)(uint8_t *src1, uint8_t *src2, uint8_t *dst,
  75. long width, long height, long src1Stride,
  76. long src2Stride, long dstStride);
  77. void (*vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
  78. uint8_t *dst1, uint8_t *dst2,
  79. long width, long height,
  80. long srcStride1, long srcStride2,
  81. long dstStride1, long dstStride2);
  82. void (*yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  83. uint8_t *dst,
  84. long width, long height,
  85. long srcStride1, long srcStride2,
  86. long srcStride3, long dstStride);
  87. #if defined(ARCH_X86)
  88. static const uint64_t mmx_null __attribute__((aligned(8))) = 0x0000000000000000ULL;
  89. static const uint64_t mmx_one __attribute__((aligned(8))) = 0xFFFFFFFFFFFFFFFFULL;
  90. static const uint64_t mask32b attribute_used __attribute__((aligned(8))) = 0x000000FF000000FFULL;
  91. static const uint64_t mask32g attribute_used __attribute__((aligned(8))) = 0x0000FF000000FF00ULL;
  92. static const uint64_t mask32r attribute_used __attribute__((aligned(8))) = 0x00FF000000FF0000ULL;
  93. static const uint64_t mask32 __attribute__((aligned(8))) = 0x00FFFFFF00FFFFFFULL;
  94. static const uint64_t mask3216br __attribute__((aligned(8)))=0x00F800F800F800F8ULL;
  95. static const uint64_t mask3216g __attribute__((aligned(8)))=0x0000FC000000FC00ULL;
  96. static const uint64_t mask3215g __attribute__((aligned(8)))=0x0000F8000000F800ULL;
  97. static const uint64_t mul3216 __attribute__((aligned(8))) = 0x2000000420000004ULL;
  98. static const uint64_t mul3215 __attribute__((aligned(8))) = 0x2000000820000008ULL;
  99. static const uint64_t mask24b attribute_used __attribute__((aligned(8))) = 0x00FF0000FF0000FFULL;
  100. static const uint64_t mask24g attribute_used __attribute__((aligned(8))) = 0xFF0000FF0000FF00ULL;
  101. static const uint64_t mask24r attribute_used __attribute__((aligned(8))) = 0x0000FF0000FF0000ULL;
  102. static const uint64_t mask24l __attribute__((aligned(8))) = 0x0000000000FFFFFFULL;
  103. static const uint64_t mask24h __attribute__((aligned(8))) = 0x0000FFFFFF000000ULL;
  104. static const uint64_t mask24hh __attribute__((aligned(8))) = 0xffff000000000000ULL;
  105. static const uint64_t mask24hhh __attribute__((aligned(8))) = 0xffffffff00000000ULL;
  106. static const uint64_t mask24hhhh __attribute__((aligned(8))) = 0xffffffffffff0000ULL;
  107. static const uint64_t mask15b __attribute__((aligned(8))) = 0x001F001F001F001FULL; /* 00000000 00011111 xxB */
  108. static const uint64_t mask15rg __attribute__((aligned(8))) = 0x7FE07FE07FE07FE0ULL; /* 01111111 11100000 RGx */
  109. static const uint64_t mask15s __attribute__((aligned(8))) = 0xFFE0FFE0FFE0FFE0ULL;
  110. static const uint64_t mask15g __attribute__((aligned(8))) = 0x03E003E003E003E0ULL;
  111. static const uint64_t mask15r __attribute__((aligned(8))) = 0x7C007C007C007C00ULL;
  112. #define mask16b mask15b
  113. static const uint64_t mask16g __attribute__((aligned(8))) = 0x07E007E007E007E0ULL;
  114. static const uint64_t mask16r __attribute__((aligned(8))) = 0xF800F800F800F800ULL;
  115. static const uint64_t red_16mask __attribute__((aligned(8))) = 0x0000f8000000f800ULL;
  116. static const uint64_t green_16mask __attribute__((aligned(8)))= 0x000007e0000007e0ULL;
  117. static const uint64_t blue_16mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
  118. static const uint64_t red_15mask __attribute__((aligned(8))) = 0x00007c000000f800ULL;
  119. static const uint64_t green_15mask __attribute__((aligned(8)))= 0x000003e0000007e0ULL;
  120. static const uint64_t blue_15mask __attribute__((aligned(8))) = 0x0000001f0000001fULL;
  121. #ifdef FAST_BGR2YV12
  122. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
  123. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  124. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  125. #else
  126. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  127. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  128. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  129. #endif
  130. static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
  131. static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8)))= 0x8080808080808080ULL;
  132. static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
  133. #if 0
  134. static volatile uint64_t __attribute__((aligned(8))) b5Dither;
  135. static volatile uint64_t __attribute__((aligned(8))) g5Dither;
  136. static volatile uint64_t __attribute__((aligned(8))) g6Dither;
  137. static volatile uint64_t __attribute__((aligned(8))) r5Dither;
  138. static uint64_t __attribute__((aligned(8))) dither4[2]={
  139. 0x0103010301030103LL,
  140. 0x0200020002000200LL,};
  141. static uint64_t __attribute__((aligned(8))) dither8[2]={
  142. 0x0602060206020602LL,
  143. 0x0004000400040004LL,};
  144. #endif
  145. #endif /* defined(ARCH_X86) */
  146. #define RGB2YUV_SHIFT 8
  147. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  148. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  149. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  150. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  151. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  152. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  153. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  154. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  155. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  156. //Note: we have C, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  157. //Plain C versions
  158. #undef HAVE_MMX
  159. #undef HAVE_MMX2
  160. #undef HAVE_3DNOW
  161. #undef HAVE_SSE2
  162. #define RENAME(a) a ## _C
  163. #include "rgb2rgb_template.c"
  164. #if defined(ARCH_X86)
  165. //MMX versions
  166. #undef RENAME
  167. #define HAVE_MMX
  168. #undef HAVE_MMX2
  169. #undef HAVE_3DNOW
  170. #undef HAVE_SSE2
  171. #define RENAME(a) a ## _MMX
  172. #include "rgb2rgb_template.c"
  173. //MMX2 versions
  174. #undef RENAME
  175. #define HAVE_MMX
  176. #define HAVE_MMX2
  177. #undef HAVE_3DNOW
  178. #undef HAVE_SSE2
  179. #define RENAME(a) a ## _MMX2
  180. #include "rgb2rgb_template.c"
  181. //3DNOW versions
  182. #undef RENAME
  183. #define HAVE_MMX
  184. #undef HAVE_MMX2
  185. #define HAVE_3DNOW
  186. #undef HAVE_SSE2
  187. #define RENAME(a) a ## _3DNOW
  188. #include "rgb2rgb_template.c"
  189. #endif //ARCH_X86 || ARCH_X86_64
  190. /*
  191. rgb15->rgb16 Original by Strepto/Astral
  192. ported to gcc & bugfixed : A'rpi
  193. MMX2, 3DNOW optimization by Nick Kurshev
  194. 32bit c version, and and&add trick by Michael Niedermayer
  195. */
  196. void sws_rgb2rgb_init(int flags){
  197. #if defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX)
  198. if(flags & SWS_CPU_CAPS_MMX2){
  199. rgb15to16= rgb15to16_MMX2;
  200. rgb15to24= rgb15to24_MMX2;
  201. rgb15to32= rgb15to32_MMX2;
  202. rgb16to24= rgb16to24_MMX2;
  203. rgb16to32= rgb16to32_MMX2;
  204. rgb16to15= rgb16to15_MMX2;
  205. rgb24to16= rgb24to16_MMX2;
  206. rgb24to15= rgb24to15_MMX2;
  207. rgb24to32= rgb24to32_MMX2;
  208. rgb32to16= rgb32to16_MMX2;
  209. rgb32to15= rgb32to15_MMX2;
  210. rgb32to24= rgb32to24_MMX2;
  211. rgb24tobgr15= rgb24tobgr15_MMX2;
  212. rgb24tobgr16= rgb24tobgr16_MMX2;
  213. rgb24tobgr24= rgb24tobgr24_MMX2;
  214. rgb32tobgr32= rgb32tobgr32_MMX2;
  215. rgb32tobgr16= rgb32tobgr16_MMX2;
  216. rgb32tobgr15= rgb32tobgr15_MMX2;
  217. yv12toyuy2= yv12toyuy2_MMX2;
  218. yv12touyvy= yv12touyvy_MMX2;
  219. yuv422ptoyuy2= yuv422ptoyuy2_MMX2;
  220. yuy2toyv12= yuy2toyv12_MMX2;
  221. // uyvytoyv12= uyvytoyv12_MMX2;
  222. // yvu9toyv12= yvu9toyv12_MMX2;
  223. planar2x= planar2x_MMX2;
  224. rgb24toyv12= rgb24toyv12_MMX2;
  225. interleaveBytes= interleaveBytes_MMX2;
  226. vu9_to_vu12= vu9_to_vu12_MMX2;
  227. yvu9_to_yuy2= yvu9_to_yuy2_MMX2;
  228. }else if(flags & SWS_CPU_CAPS_3DNOW){
  229. rgb15to16= rgb15to16_3DNOW;
  230. rgb15to24= rgb15to24_3DNOW;
  231. rgb15to32= rgb15to32_3DNOW;
  232. rgb16to24= rgb16to24_3DNOW;
  233. rgb16to32= rgb16to32_3DNOW;
  234. rgb16to15= rgb16to15_3DNOW;
  235. rgb24to16= rgb24to16_3DNOW;
  236. rgb24to15= rgb24to15_3DNOW;
  237. rgb24to32= rgb24to32_3DNOW;
  238. rgb32to16= rgb32to16_3DNOW;
  239. rgb32to15= rgb32to15_3DNOW;
  240. rgb32to24= rgb32to24_3DNOW;
  241. rgb24tobgr15= rgb24tobgr15_3DNOW;
  242. rgb24tobgr16= rgb24tobgr16_3DNOW;
  243. rgb24tobgr24= rgb24tobgr24_3DNOW;
  244. rgb32tobgr32= rgb32tobgr32_3DNOW;
  245. rgb32tobgr16= rgb32tobgr16_3DNOW;
  246. rgb32tobgr15= rgb32tobgr15_3DNOW;
  247. yv12toyuy2= yv12toyuy2_3DNOW;
  248. yv12touyvy= yv12touyvy_3DNOW;
  249. yuv422ptoyuy2= yuv422ptoyuy2_3DNOW;
  250. yuy2toyv12= yuy2toyv12_3DNOW;
  251. // uyvytoyv12= uyvytoyv12_3DNOW;
  252. // yvu9toyv12= yvu9toyv12_3DNOW;
  253. planar2x= planar2x_3DNOW;
  254. rgb24toyv12= rgb24toyv12_3DNOW;
  255. interleaveBytes= interleaveBytes_3DNOW;
  256. vu9_to_vu12= vu9_to_vu12_3DNOW;
  257. yvu9_to_yuy2= yvu9_to_yuy2_3DNOW;
  258. }else if(flags & SWS_CPU_CAPS_MMX){
  259. rgb15to16= rgb15to16_MMX;
  260. rgb15to24= rgb15to24_MMX;
  261. rgb15to32= rgb15to32_MMX;
  262. rgb16to24= rgb16to24_MMX;
  263. rgb16to32= rgb16to32_MMX;
  264. rgb16to15= rgb16to15_MMX;
  265. rgb24to16= rgb24to16_MMX;
  266. rgb24to15= rgb24to15_MMX;
  267. rgb24to32= rgb24to32_MMX;
  268. rgb32to16= rgb32to16_MMX;
  269. rgb32to15= rgb32to15_MMX;
  270. rgb32to24= rgb32to24_MMX;
  271. rgb24tobgr15= rgb24tobgr15_MMX;
  272. rgb24tobgr16= rgb24tobgr16_MMX;
  273. rgb24tobgr24= rgb24tobgr24_MMX;
  274. rgb32tobgr32= rgb32tobgr32_MMX;
  275. rgb32tobgr16= rgb32tobgr16_MMX;
  276. rgb32tobgr15= rgb32tobgr15_MMX;
  277. yv12toyuy2= yv12toyuy2_MMX;
  278. yv12touyvy= yv12touyvy_MMX;
  279. yuv422ptoyuy2= yuv422ptoyuy2_MMX;
  280. yuy2toyv12= yuy2toyv12_MMX;
  281. // uyvytoyv12= uyvytoyv12_MMX;
  282. // yvu9toyv12= yvu9toyv12_MMX;
  283. planar2x= planar2x_MMX;
  284. rgb24toyv12= rgb24toyv12_MMX;
  285. interleaveBytes= interleaveBytes_MMX;
  286. vu9_to_vu12= vu9_to_vu12_MMX;
  287. yvu9_to_yuy2= yvu9_to_yuy2_MMX;
  288. }else
  289. #endif /* defined(HAVE_MMX2) || defined(HAVE_3DNOW) || defined(HAVE_MMX) */
  290. {
  291. rgb15to16= rgb15to16_C;
  292. rgb15to24= rgb15to24_C;
  293. rgb15to32= rgb15to32_C;
  294. rgb16to24= rgb16to24_C;
  295. rgb16to32= rgb16to32_C;
  296. rgb16to15= rgb16to15_C;
  297. rgb24to16= rgb24to16_C;
  298. rgb24to15= rgb24to15_C;
  299. rgb24to32= rgb24to32_C;
  300. rgb32to16= rgb32to16_C;
  301. rgb32to15= rgb32to15_C;
  302. rgb32to24= rgb32to24_C;
  303. rgb24tobgr15= rgb24tobgr15_C;
  304. rgb24tobgr16= rgb24tobgr16_C;
  305. rgb24tobgr24= rgb24tobgr24_C;
  306. rgb32tobgr32= rgb32tobgr32_C;
  307. rgb32tobgr16= rgb32tobgr16_C;
  308. rgb32tobgr15= rgb32tobgr15_C;
  309. yv12toyuy2= yv12toyuy2_C;
  310. yv12touyvy= yv12touyvy_C;
  311. yuv422ptoyuy2= yuv422ptoyuy2_C;
  312. yuy2toyv12= yuy2toyv12_C;
  313. // uyvytoyv12= uyvytoyv12_C;
  314. // yvu9toyv12= yvu9toyv12_C;
  315. planar2x= planar2x_C;
  316. rgb24toyv12= rgb24toyv12_C;
  317. interleaveBytes= interleaveBytes_C;
  318. vu9_to_vu12= vu9_to_vu12_C;
  319. yvu9_to_yuy2= yvu9_to_yuy2_C;
  320. }
  321. }
  322. /**
  323. * Pallete is assumed to contain bgr32
  324. */
  325. void palette8torgb32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  326. {
  327. long i;
  328. /*
  329. for(i=0; i<num_pixels; i++)
  330. ((unsigned *)dst)[i] = ((unsigned *)palette)[ src[i] ];
  331. */
  332. for(i=0; i<num_pixels; i++)
  333. {
  334. #ifdef WORDS_BIGENDIAN
  335. dst[3]= palette[ src[i]*4+2 ];
  336. dst[2]= palette[ src[i]*4+1 ];
  337. dst[1]= palette[ src[i]*4+0 ];
  338. #else
  339. //FIXME slow?
  340. dst[0]= palette[ src[i]*4+2 ];
  341. dst[1]= palette[ src[i]*4+1 ];
  342. dst[2]= palette[ src[i]*4+0 ];
  343. //dst[3]= 0; /* do we need this cleansing? */
  344. #endif
  345. dst+= 4;
  346. }
  347. }
  348. void palette8tobgr32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  349. {
  350. long i;
  351. for(i=0; i<num_pixels; i++)
  352. {
  353. #ifdef WORDS_BIGENDIAN
  354. dst[3]= palette[ src[i]*4+0 ];
  355. dst[2]= palette[ src[i]*4+1 ];
  356. dst[1]= palette[ src[i]*4+2 ];
  357. #else
  358. //FIXME slow?
  359. dst[0]= palette[ src[i]*4+0 ];
  360. dst[1]= palette[ src[i]*4+1 ];
  361. dst[2]= palette[ src[i]*4+2 ];
  362. //dst[3]= 0; /* do we need this cleansing? */
  363. #endif
  364. dst+= 4;
  365. }
  366. }
  367. /**
  368. * Pallete is assumed to contain bgr32
  369. */
  370. void palette8torgb24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  371. {
  372. long i;
  373. /*
  374. writes 1 byte o much and might cause alignment issues on some architectures?
  375. for(i=0; i<num_pixels; i++)
  376. ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
  377. */
  378. for(i=0; i<num_pixels; i++)
  379. {
  380. //FIXME slow?
  381. dst[0]= palette[ src[i]*4+2 ];
  382. dst[1]= palette[ src[i]*4+1 ];
  383. dst[2]= palette[ src[i]*4+0 ];
  384. dst+= 3;
  385. }
  386. }
  387. void palette8tobgr24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  388. {
  389. long i;
  390. /*
  391. writes 1 byte o much and might cause alignment issues on some architectures?
  392. for(i=0; i<num_pixels; i++)
  393. ((unsigned *)(&dst[i*3])) = ((unsigned *)palette)[ src[i] ];
  394. */
  395. for(i=0; i<num_pixels; i++)
  396. {
  397. //FIXME slow?
  398. dst[0]= palette[ src[i]*4+0 ];
  399. dst[1]= palette[ src[i]*4+1 ];
  400. dst[2]= palette[ src[i]*4+2 ];
  401. dst+= 3;
  402. }
  403. }
  404. /**
  405. * Palette is assumed to contain bgr16, see rgb32to16 to convert the palette
  406. */
  407. void palette8torgb16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  408. {
  409. long i;
  410. for(i=0; i<num_pixels; i++)
  411. ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
  412. }
  413. void palette8tobgr16(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  414. {
  415. long i;
  416. for(i=0; i<num_pixels; i++)
  417. ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
  418. }
  419. /**
  420. * Pallete is assumed to contain bgr15, see rgb32to15 to convert the palette
  421. */
  422. void palette8torgb15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  423. {
  424. long i;
  425. for(i=0; i<num_pixels; i++)
  426. ((uint16_t *)dst)[i] = ((uint16_t *)palette)[ src[i] ];
  427. }
  428. void palette8tobgr15(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
  429. {
  430. long i;
  431. for(i=0; i<num_pixels; i++)
  432. ((uint16_t *)dst)[i] = bswap_16(((uint16_t *)palette)[ src[i] ]);
  433. }
  434. void rgb32tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  435. {
  436. long i;
  437. long num_pixels = src_size >> 2;
  438. for(i=0; i<num_pixels; i++)
  439. {
  440. #ifdef WORDS_BIGENDIAN
  441. /* RGB32 (= A,B,G,R) -> BGR24 (= B,G,R) */
  442. dst[3*i + 0] = src[4*i + 1];
  443. dst[3*i + 1] = src[4*i + 2];
  444. dst[3*i + 2] = src[4*i + 3];
  445. #else
  446. dst[3*i + 0] = src[4*i + 2];
  447. dst[3*i + 1] = src[4*i + 1];
  448. dst[3*i + 2] = src[4*i + 0];
  449. #endif
  450. }
  451. }
  452. void rgb24tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  453. {
  454. long i;
  455. for(i=0; 3*i<src_size; i++)
  456. {
  457. #ifdef WORDS_BIGENDIAN
  458. /* RGB24 (= R,G,B) -> BGR32 (= A,R,G,B) */
  459. dst[4*i + 0] = 0;
  460. dst[4*i + 1] = src[3*i + 0];
  461. dst[4*i + 2] = src[3*i + 1];
  462. dst[4*i + 3] = src[3*i + 2];
  463. #else
  464. dst[4*i + 0] = src[3*i + 2];
  465. dst[4*i + 1] = src[3*i + 1];
  466. dst[4*i + 2] = src[3*i + 0];
  467. dst[4*i + 3] = 0;
  468. #endif
  469. }
  470. }
  471. void rgb16tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  472. {
  473. const uint16_t *end;
  474. uint8_t *d = (uint8_t *)dst;
  475. const uint16_t *s = (uint16_t *)src;
  476. end = s + src_size/2;
  477. while(s < end)
  478. {
  479. register uint16_t bgr;
  480. bgr = *s++;
  481. #ifdef WORDS_BIGENDIAN
  482. *d++ = 0;
  483. *d++ = (bgr&0x1F)<<3;
  484. *d++ = (bgr&0x7E0)>>3;
  485. *d++ = (bgr&0xF800)>>8;
  486. #else
  487. *d++ = (bgr&0xF800)>>8;
  488. *d++ = (bgr&0x7E0)>>3;
  489. *d++ = (bgr&0x1F)<<3;
  490. *d++ = 0;
  491. #endif
  492. }
  493. }
  494. void rgb16tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  495. {
  496. const uint16_t *end;
  497. uint8_t *d = (uint8_t *)dst;
  498. const uint16_t *s = (const uint16_t *)src;
  499. end = s + src_size/2;
  500. while(s < end)
  501. {
  502. register uint16_t bgr;
  503. bgr = *s++;
  504. *d++ = (bgr&0xF800)>>8;
  505. *d++ = (bgr&0x7E0)>>3;
  506. *d++ = (bgr&0x1F)<<3;
  507. }
  508. }
  509. void rgb16tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  510. {
  511. long i;
  512. long num_pixels = src_size >> 1;
  513. for(i=0; i<num_pixels; i++)
  514. {
  515. unsigned b,g,r;
  516. register uint16_t rgb;
  517. rgb = src[2*i];
  518. r = rgb&0x1F;
  519. g = (rgb&0x7E0)>>5;
  520. b = (rgb&0xF800)>>11;
  521. dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
  522. }
  523. }
  524. void rgb16tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  525. {
  526. long i;
  527. long num_pixels = src_size >> 1;
  528. for(i=0; i<num_pixels; i++)
  529. {
  530. unsigned b,g,r;
  531. register uint16_t rgb;
  532. rgb = src[2*i];
  533. r = rgb&0x1F;
  534. g = (rgb&0x7E0)>>5;
  535. b = (rgb&0xF800)>>11;
  536. dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
  537. }
  538. }
  539. void rgb15tobgr32(const uint8_t *src, uint8_t *dst, long src_size)
  540. {
  541. const uint16_t *end;
  542. uint8_t *d = (uint8_t *)dst;
  543. const uint16_t *s = (const uint16_t *)src;
  544. end = s + src_size/2;
  545. while(s < end)
  546. {
  547. register uint16_t bgr;
  548. bgr = *s++;
  549. #ifdef WORDS_BIGENDIAN
  550. *d++ = 0;
  551. *d++ = (bgr&0x1F)<<3;
  552. *d++ = (bgr&0x3E0)>>2;
  553. *d++ = (bgr&0x7C00)>>7;
  554. #else
  555. *d++ = (bgr&0x7C00)>>7;
  556. *d++ = (bgr&0x3E0)>>2;
  557. *d++ = (bgr&0x1F)<<3;
  558. *d++ = 0;
  559. #endif
  560. }
  561. }
  562. void rgb15tobgr24(const uint8_t *src, uint8_t *dst, long src_size)
  563. {
  564. const uint16_t *end;
  565. uint8_t *d = (uint8_t *)dst;
  566. const uint16_t *s = (uint16_t *)src;
  567. end = s + src_size/2;
  568. while(s < end)
  569. {
  570. register uint16_t bgr;
  571. bgr = *s++;
  572. *d++ = (bgr&0x7C00)>>7;
  573. *d++ = (bgr&0x3E0)>>2;
  574. *d++ = (bgr&0x1F)<<3;
  575. }
  576. }
  577. void rgb15tobgr16(const uint8_t *src, uint8_t *dst, long src_size)
  578. {
  579. long i;
  580. long num_pixels = src_size >> 1;
  581. for(i=0; i<num_pixels; i++)
  582. {
  583. unsigned b,g,r;
  584. register uint16_t rgb;
  585. rgb = src[2*i];
  586. r = rgb&0x1F;
  587. g = (rgb&0x3E0)>>5;
  588. b = (rgb&0x7C00)>>10;
  589. dst[2*i] = (b&0x1F) | ((g&0x3F)<<5) | ((r&0x1F)<<11);
  590. }
  591. }
  592. void rgb15tobgr15(const uint8_t *src, uint8_t *dst, long src_size)
  593. {
  594. long i;
  595. long num_pixels = src_size >> 1;
  596. for(i=0; i<num_pixels; i++)
  597. {
  598. unsigned b,g,r;
  599. register uint16_t rgb;
  600. rgb = src[2*i];
  601. r = rgb&0x1F;
  602. g = (rgb&0x3E0)>>5;
  603. b = (rgb&0x7C00)>>10;
  604. dst[2*i] = (b&0x1F) | ((g&0x1F)<<5) | ((r&0x1F)<<10);
  605. }
  606. }
  607. void rgb8tobgr8(const uint8_t *src, uint8_t *dst, long src_size)
  608. {
  609. long i;
  610. long num_pixels = src_size;
  611. for(i=0; i<num_pixels; i++)
  612. {
  613. unsigned b,g,r;
  614. register uint8_t rgb;
  615. rgb = src[i];
  616. r = (rgb&0x07);
  617. g = (rgb&0x38)>>3;
  618. b = (rgb&0xC0)>>6;
  619. dst[i] = ((b<<1)&0x07) | ((g&0x07)<<3) | ((r&0x03)<<6);
  620. }
  621. }