展开循环,并摆脱内循环中的if。但是不要对图像数据运行3次,它甚至更快!
void Bitmap2Yuv420p_calc2(uint8_t *destination, uint8_t *rgb, size_t width, size_t height){ size_t image_size = width * height; size_t upos = image_size; size_t vpos = upos + upos / 4; size_t i = 0; for( size_t line = 0; line < height; ++line ) { if( !(line % 2) ) { for( size_t x = 0; x < width; x += 2 ) { uint8_t r = rgb[3 * i]; uint8_t g = rgb[3 * i + 1]; uint8_t b = rgb[3 * i + 2]; destination[i++] = ((66*r + 129*g + 25*b) >> 8) + 16; destination[upos++] = ((-38*r + -74*g + 112*b) >> 8) + 128; destination[vpos++] = ((112*r + -94*g + -18*b) >> 8) + 128; r = rgb[3 * i]; g = rgb[3 * i + 1]; b = rgb[3 * i + 2]; destination[i++] = ((66*r + 129*g + 25*b) >> 8) + 16; } } else { for( size_t x = 0; x < width; x += 1 ) { uint8_t r = rgb[3 * i]; uint8_t g = rgb[3 * i + 1]; uint8_t b = rgb[3 * i + 2]; destination[i++] = ((66*r + 129*g + 25*b) >> 8) + 16; } } }}在我的测试中,这比您接受的答案快了25%(VS 2010,取决于启用了x86还是x64。)



