Skip to content

Instantly share code, notes, and snippets.

@sambatyon
Created February 13, 2012 16:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sambatyon/1817861 to your computer and use it in GitHub Desktop.
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
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;
}
}
}
}
@sambatyon
Copy link
Author

So it seems, multiplication is faster than lookup tables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment