Hi, guys. I've been reading your forum for some time.
Now I need a little help again.
I am trying to read a bitmap file using BITMAPFILEHEADER, BITMAPINFOHEADER and RGBQUAD in windows.h - I need RGB values or only one of them. All the info in header structures is OK. My file has no compression(I drew 4 pixels with paint).

void ReadBitmap(char* FileName)
{ 
	RGBQUAD Palette[4];

	BITMAPFILEHEADER bitmapFileHeader;
	BITMAPINFOHEADER bitmapInfoHeader;
	FILE *stream=fopen(FileName,"rb");
	if(stream ==NULL)
		printf("ERROR: Can’t open “<<FileName<<endl;");
	else
	{ 
		if((fread(&bitmapFileHeader,sizeof(BITMAPFILEHEADER),1,stream)) == -1)
		  printf("ERROR: Can’t Read Fileheader Stucture!”;");

		if ((fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, stream) == -1))
		  printf("ERROR: Can’t Read Infoheader Stucture!”;");	 
	}

	printf("compr - %d\n", bitmapInfoHeader.biCompression);
	printf("width - %d\n", bitmapInfoHeader.biWidth);
	printf("height - %d\n", bitmapInfoHeader.biHeight);

	RGBQUAD pixel;
	for(int l=0;l<4;l++)
	  {
		  if((fread(&pixel,sizeof(RGBQUAD),1,stream))==-1)
			{printf("<<“ERROR: Can’t read bitmap’s color palette!”;"); }
                   else
                          {
				printf("/r - %d",int(pixel.rgbRed));
				printf("/g - %d",int(pixel.rgbGreen));
				printf("/b - %d",int(pixel.rgbBlue));
				printf("/off - %d\n", pixel.rgbReserved); 
                           }
	        }

I got following result

compr - 0
width - 4
height - 1
/r - 237/g - 28/b - 36/off - 36
/r - 21/g - 237/b - 28/off - 0
/r - 228/g - 176/b - 136/off - 239
/r - 228/g - 176/b - 136/off - 239

RGB values for the first pixel are OK, but then I got something in rgbReserved when it should be 0. The next values(including reserved) are mixed rgb values for the next 3 pixels.
if I understand bitmap file format correctly, the result should be something like this :

/r - 237/g - 28/b - 36/off - 0
/r - 237/g - 28/b - 36/off - 0
/r - 136/g - 0/b - 21/off - 0
/r - 239/g - 228/b - 176/off - 0

Can you tell me where I am wrong?

Member Avatar for manutm

Not sure if I'm right but you're reading the color table (an array of RGBQUAD structures), **not** the actual pixels data (which type is BYTE). Then, about rgbReserved, you shouldn't assume anything since its ... reserved. Maybe this can help:
http://www.digicamsoft.com/bmp/bmp.html

You need to do a little more :

int LoadBitmap(const char* filename)
{
   GLubyte* pixels;
   HBITMAP hbmp;  
   BITMAP bmp; 
   GLuint size;
   GLenum format;
   GLuint totalBytes;

   texID++;

   hbmp=(HBITMAP)LoadImage(GetModuleHandle(NULL), (LPWSTR)filename.c_str(), IMAGE_BITMAP, 0, 0,      LR_CREATEDIBSECTION | LR_LOADFROMFILE );
   if(hbmp==NULL)
   {
	   MessageBox(NULL,L"ERROR : CANNOT LOAD FILE!",L"IMAGE LOADING",MB_OK);
	   return -1;
   }

   GetObject(hbmp,sizeof(bmp), &bmp);  				   		 
 
   size = bmp.bmHeight*bmp.bmWidthBytes;
   
   totalBytes = bmp.bmBitsPixel/8;

   pixels = new GLubyte[size];

   memcpy(pixels,bmp.bmBits,size);
	
   DeleteObject(hbmp);
   
   delete [] pixels;
   
   return 1;

}

Thank you guys for your answers!
Of course 24-bit bitmap is represented with 24bits for pixel, so RGBQUAD is not suitable.
Everything is perfect when using RGBTRIPLE in the code in the first post...if you have 1 line...

This is my final source

#include "stdlib.h"
#include "windows.h"
#include "math.h"



void ReadBitmap(const char* FileName)
{ 
	BITMAPFILEHEADER bitmapFileHeader;
	BITMAPINFOHEADER bitmapInfoHeader;
	unsigned char* bitmap_Image;
	FILE *file=fopen(FileName,"rb");
	

	if(file ==NULL)
		printf("ERROR: Can’t open “<<FileName<<endl;");
	else
	{ 
		if((fread(&bitmapFileHeader,sizeof(BITMAPFILEHEADER),1,file)) == -1)
		  printf("ERROR: Can’t Read Fileheader Stucture!”;");

		if ((fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, file) == -1))
		  printf("ERROR: Can’t Read Infoheader Stucture!”;");	 
	}

	printf("compr - %d\n", bitmapInfoHeader.biCompression);
	printf("width - %d\n", bitmapInfoHeader.biWidth);
	printf("height - %d\n", bitmapInfoHeader.biHeight);
	printf("size - %d\n", bitmapInfoHeader.biSizeImage);

    if (bitmapInfoHeader.biSizeImage != 0)
      bitmap_Image = (unsigned char*)calloc(bitmapInfoHeader.biSizeImage, sizeof(unsigned char));

	if (!bitmap_Image)
	{
		free(bitmap_Image);
		fclose(file);
		return;
	}
	
	fread(bitmap_Image, bitmapInfoHeader.biSizeImage,1, file); 
	int pixCnt = 0;
	int inc = (4 - (int)fmod((double)bitmapInfoHeader.biWidth*3, 4)) ; //every line is formed by 32bits parts
	if (inc == 4) inc = 0;
	int rowEndIndex = bitmapInfoHeader.biWidth*3; //first row 
	for (int i = 0; i < (bitmapInfoHeader.biHeight); i++)
	{
		printf("new line\n");
		for (int cnt=pixCnt; cnt < rowEndIndex; cnt+=3,pixCnt+=3)
		{
				printf("/b - %d",bitmap_Image[cnt]);
				printf("/g - %d",bitmap_Image[cnt + 1]);
				printf("/r - %d\n",bitmap_Image[cnt + 2]);				
		}
		pixCnt+=inc;
		rowEndIndex+=bitmapInfoHeader.biWidth*3 + inc;

	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.