Created
February 13, 2012 16:02
-
-
Save sambatyon/1817861 to your computer and use it in GitHub Desktop.
Transforms a bitmap int RGB 8:8:8 24bpp into an equivalent YUV420p 4:2:0 12bpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Bitmap2Yuv420p(boost::uint8_t *destination, boost::uint8_t *rgb, | |
const int &width, const int &height) { | |
const std::size_t image_size = width * height; | |
boost::uint8_t *y = destination; | |
boost::uint8_t *u = destination + image_size; | |
boost::uint8_t *v = destination + image_size + image_size / 4; | |
boost::uint8_t *r = rgb; | |
boost::uint8_t *g = rgb + 1; | |
boost::uint8_t *b = rgb + 2; | |
for (std::size_t line = 0; line < height; ++line) { | |
if (!(line % 2)) { | |
for (std::size_t x = 0; x < width; x += 2) { | |
*(y++) = ((66 * (*r) + 129 * (*g) + 25 * (*b)) >> 8) + 16; | |
*(u++) = ((-38 * (*r) - 74 * (*g) + 112 * (*b)) >> 8) + 128; | |
*(v++) = ((112 * (*r) - 94 * (*g) - 18 * (*b)) >> 8) + 128; | |
r += 3; | |
g += 3; | |
b += 3; | |
*(y++) = ((66 * (*r) + 129 * (*g) + 25 * (*b)) >> 8) + 16; | |
r += 3; | |
g += 3; | |
b += 3; | |
} | |
} else { | |
for (std::size_t x = 0; x < width; ++x) { | |
*(y++) = ((66 * (*r) + 129 * (*g) + 25 * (*b)) >> 8) + 16; | |
r += 3; | |
g += 3; | |
b += 3; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So it seems, multiplication is faster than lookup tables.