0

If I defined a struct like this:

struct rgb
{
    double r;
    double g;
    double b;
};

And each element is a percentage. How to get these values from a .bmp file in C or C++? Thanks!

zxi
  • 195
  • 4
  • 17

2 Answers2

2

The wikipedia entry for BMP files has a nice description of the file format. At the bottom of that page there is a link to this file bitmap.h which I've used before to read BMP files.

Basically, once you've read your BMP file, you should just divide each Red, Green and Blue value in the RGBA structs by 255 to get a percentage.

Wernsey
  • 5,411
  • 22
  • 38
1

I am not sure but you can refer the below link to get some idea on how to get rgb values.

RGB_Info

Also check the below link were they have mentioned the example as mentioned below:-

RGB_Info_MSDN_Link

//Pass the handle to the window on which you may want to draw
struct vRGB
{
    vRGB():R(0), G(0), B(0){}
    vRGB(BYTE r, BYTE g, BYTE b):R(r), G(b), B(b){}
    BYTE R, G, B;
};

void GetBitmapPixel(HWND hwnd, std::vector<std::vector<vRGB>> &pixel){
BITMAP bi;

//Load Bitmap from resource file
HBITMAP hBmp = LoadBitmap(GetModuleHandle(0),MAKEINTRESOURCE(IDB_BITMAP1));

//Get the Height and Width
::GetObject(hBmp, sizeof(bi), &bi);
int Width = bi.bmWidth; int Height = bi.bmHeight;

//Allocate and Initialize enough memory for external (Y-dimension) array
pixel.resize(Height);

//Create a memory device context and place your bitmap
HDC hDC = GetDC(hwnd);
HDC hMemDC = ::CreateCompatibleDC(hDC);
SelectObject(hMemDC, hBmp);

DWORD pixelData = 0;

for (int y = 0; y < Height; ++y)
{
    //Reserve memory for each internel (X-dimension) array
    pixel[y].reserve(Width);

    for (int x = 0; x < Width; ++x)
    {
        //Add the RGB pixel information to array.
        pixelData = GetPixel(hMemDC, x, y);
        pixel[y].push_back(vRGB(GetRValue(pixelData), GetGValue(pixelData), GetBValue(pixelData)));
    }
}

//Cleanup device contexts
DeleteDC(hMemDC);
ReleaseDC(hwnd, hDC);
}
Community
  • 1
  • 1
Abhineet
  • 6,459
  • 10
  • 35
  • 53