Drawing a bitmap with 1 solid colour, why's mine got 4?

I'm learning about bitmaps at the minute and I'm trying to make one with just a solid red colour. Here is the code that I have at the minute:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
BITMAPFILEHEADER bitmapFileHeader;

	bitmapFileHeader.bfType = 0x4d42;
	bitmapFileHeader.bfSize = sizeof(BITMAPFILEHEADER);
	bitmapFileHeader.bfReserved1 = 0;
	bitmapFileHeader.bfReserved2 = 0;
	bitmapFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);


	// Fill the bitmap info structure
	BITMAPINFOHEADER bitmapInfoHeader;

	bitmapInfoHeader.biSize = sizeof(bitmapInfoHeader);
	bitmapInfoHeader.biWidth = 480;
	bitmapInfoHeader.biHeight = 480;
	bitmapInfoHeader.biPlanes = 1;
	bitmapInfoHeader.biBitCount = 24;			
	bitmapInfoHeader.biCompression = BI_RGB;	 
	bitmapInfoHeader.biSizeImage = 0;	 // does this need padding?
	bitmapInfoHeader.biXPelsPerMeter = 0;
	bitmapInfoHeader.biYPelsPerMeter = 0;
	bitmapInfoHeader.biClrUsed = 0;
	bitmapInfoHeader.biClrImportant = 0;



string filename = "testBits.bmp";

 ofstream file;
  file.open(filename.c_str(), ios::out | ios::trunc | ios::binary);

//write headers
file.write ((char*)(&bitmapFileHeader), sizeof(bitmapFileHeader));
file.write ((char*)(&bitmapInfoHeader), sizeof(bitmapInfoHeader));

//create array of red colour's

int size = bitmapInfoHeader.biHeight * bitmapInfoHeader.biWidth;
RGBQUAD image[size];

for (int i = 0; i < size; i++)
  {

       image[i].rgbRed = 255
       image[i].rgbGreen = 0;
       image[i].rgbBlue = 0;
  }

file.write((char*)(&image), size * sizeof(RGBQUAD));


Whenever I go to run the program the image that is created gives a bitmap with 4 repeated columns of red black green blue. Could someone explain to me why this is the case, if I have set the values of the RGBQUAD to have all the values apart from rgbRed to be 0? I understand there is a reserved bit for each RGBQUAD, so how do I also stop this from being displayed?

Thanks in advance!
Last edited on
1
2
3
bitmapInfoHeader.biBitCount = 24;
//...
file.write((char*)(&image), size * sizeof(RGBQUAD));


sizeof(RGBQUAD) is likely 4 (ie: 32 bits).

So your problem is you're writing 32-bit pixels to a 24-bit bitmap.

Possible solutions:

1) Don't use RGBQUAD, and use a byte array instead.

2) Change to a 32-bit bitmap instead of a 24-bit bitmap
Thanks for the reply! How would I use a byte array to populate the bitmap? Just have an array of hex values that represent the colour red, and write them to the image?

Also, our of curiosity, here is the RGBQUAD

1
2
3
4
5
6
7
typedef struct                       /**** Colormap entry structure ****/
    {
    unsigned char  rgbBlue;          /* Blue value */
    unsigned char  rgbGreen;         /* Green value */
    unsigned char  rgbRed;           /* Red value */
    unsigned char  rgbReserved;      /* Reserved */
    } RGBQUAD;


How come when I comment out "unsigned char rgbReserved;" the image still draws 4 colours, after I've surely made the size of RGBQUAD 3?
The size of a struct does not necessarily equal the size of its individual components.

http://en.wikipedia.org/wiki/Sizeof#Structure_padding
Thanks for the reply! How would I use a byte array to populate the bitmap? Just have an array of hex values that represent the colour red, and write them to the image?


Basically, yes.

1
2
3
4
5
6
7
8
9
10
11
BYTE image[size * 3];  // where 'size' is size in pixels

for(int i = 0; i < size; ++i)
{
  image[ (i*3) + 0 ] = 0;     // blue
  image[ (i*3) + 1 ] = 0;     // green
  image[ (i*3) + 2 ] = 0xFF;  // red
}

file.write((char*)(image), size * 3 );
Thanks for the replies guys, I've learnt a good bit :)
Topic archived. No new replies allowed.