san6086
san6086

Reputation: 469

Converting RGB image to YUV using C programming

I have images as bitmap and JPEG. I will have to retrieve the pixels from the image there by RGB values of all pixels are obtained. Please suggest a method where RGB values are retrieved from an image file. I would appreciated if there are any functions available in C.

Upvotes: 0

Views: 3746

Answers (2)

fycth
fycth

Reputation: 3489

You can parse and get bitmap from JPEG using libJPEG - it is pretty simple

Suppose you have and RGB bimap in 'rgb'. Result will be placed in 'yuv420p' vector.

void rgb2yuv420p(std::vector<BYTE>& rgb, std::vector<BYTE>& yuv420p)
{
  unsigned int i = 0;
  unsigned int numpixels = width * height;
  unsigned int ui = numpixels;
  unsigned int vi = numpixels + numpixels / 4;
  unsigned int s = 0;

#define sR (BYTE)(rgb[s+2])
#define sG (BYTE)(rgb[s+1])
#define sB (BYTE)(rgb[s+0])

  yuv420p.resize(numpixels * 3 / 2);

  for (int j = 0; j < height; j++)
    for (int k = 0; k < width; k++)
    {
      yuv420p[i] = (BYTE)( (66*sR + 129*sG + 25*sB + 128) >> 8) + 16;

      if (0 == j%2 && 0 == k%2)
      {
        yuv420p[ui++] = (BYTE)( (-38*sR - 74*sG + 112*sB + 128) >> 8) + 128;
        yuv420p[vi++] = (BYTE)( (112*sR - 94*sG - 18*sB + 128) >> 8) + 128;
      }
      i++;
      s += colors;
    }
}

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129314

If you want to do this yourself, here's teh Wikipedia article that I worked from when I did this at work, about a year back: http://en.wikipedia.org/wiki/YUV

This is pretty good too: http://www.fourcc.org/fccyvrgb.php

But MUCH easier is jpeglib - that wasn't an option in my case, because the data wasn't jpeg in the first place.

Upvotes: 0

Related Questions