由于RGB565为16位数据,故每个像素点占用两个字节大小,所以在使用第一个函数时,需要将图片像素的当前地址和下一位的地址都送到函数中,或者可以使用第二个封装后的函数,只需要设定图片的显示范围即可
#define CLIP(X) ((X) > 255 ? 255 : (X) < 0 ? 0 : X)
// RGB -> YUV
#define RGB2Y(R, G, B) CLIP(((66 * (R) + 129 * (G) + 25 * (B) + 128) >> 8) + 16)
#define RGB2U(R, G, B) CLIP(((-38 * (R)-74 * (G) + 112 * (B) + 128) >> 8) + 128)
#define RGB2V(R, G, B) CLIP(((112 * (R)-94 * (G)-18 * (B) + 128) >> 8) + 128)
void RGB5652YUV(uint16_t source1, uint16_t source2, uint16_t *dest1, uint16_t *dest2)
{
uint8_t R, G, B, Y0, Y1, U0, V0, U1, V1;
R = ((source1 & RGB565_RED) >> 11) << 3;
G = ((source1 & RGB565_GREEN) >> 5) << 2;
B = ((source1 & RGB565_BLUE)) << 3;
Y0 = RGB2Y(R, G, B);
U0 = RGB2U(R, G, B);
V0 = RGB2V(R, G, B);
R = ((source2 & RGB565_RED) >> 11) << 3;
G = ((source2 & RGB565_GREEN) >> 5) << 2;
B = ((source2 & RGB565_BLUE)) << 3;
Y1 = RGB2Y(R, G, B);
U1 = RGB2U(R, G, B);
V1 = RGB2V(R, G, B);
U0 = (U0 + U1) / 2;
V0 = (V0 + V1) / 2;
*dest2 = (Y1 << 8) | V0;
*dest1 = (Y0 << 8) | U0;
}
void ConvertRGB2YUV(uint16_t *sourceLcdBuffer, uint16_t *destLcdBuffer)
{
int line, col;
for (line = 0; line < LCD_HEIGHT; line++)
for (col = 0; col < LCD_WIDTH - 1; col = col + 2)
{
RGB5652YUV(*(sourceLcdBuffer + line * LCD_WIDTH + col), *(sourceLcdBuffer + line * LCD_WIDTH + col + 1),
(destLcdBuffer + line * LCD_WIDTH + col), (destLcdBuffer + line * LCD_WIDTH + col + 1));
}
}