Trouble reading/storing BMP

Hello.

I'm trying to write a program that reads in a BMP file and outputs some simple data specs, like height and width in pixels. This is just to make sure I'm reading in the file correctly before I move forward in manipulating it and so forth. My program is compiling, but when I put in a 640 x 480 image, the program says that the image is 31457280 x 65536. Clearly something's going wrong. Can anyone see a problem?

(header file)
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
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>

using namespace std;

// #pragma pack(2) // found this as a 2 byte alignment... no idea what this is

// bitmap header
struct bmpFileHeader
{
	unsigned short bfType; // file type
	unsigned int bfSize; // size in bytes of bmp
	unsigned short bfReserved1; // still don't fully understand this
	unsigned short bfReserved2; // or this...
	unsigned int bfOffBits; // or this... depends on color palette?
};

// bitmap information specific to image data
struct bmpInfoHeader
{
	unsigned int biSize; // number of bytes required by InfoHeader
	int biWidth; // width in pixels
	int biHeight; // height in pixels
	unsigned short biPlanes; // number of color (bit) planes (?)
	unsigned short biBitCount; // number of bits per pixel
	unsigned int biCompression; // type of compression
	unsigned int biSizeImage; // size of image in bytes
	int biXPixelsPerMeter; // pixels per meter in x axis
	int biYPixelsPerMeter; // pixels per meter in y axis
	unsigned int biColorsUsed; // number of colors used by the bitmap
	unsigned int biColorsImportant; // number of colors that are important
};

// color palette
// I found unsigned char to be equivalent to byte
struct RGBQUAD
{
	unsigned char rgbRed;
	unsigned char rgbGreen;
	unsigned char rgbBlue;
	unsigned char rgbReserved; // not sure about this one
};

class bitMap
{
public:
	bitMap();
	~bitMap();
	void loadBitMap(char *fileName); // loads the bmp file

private:
	bmpFileHeader bmfh;
	bmpInfoHeader bmih;
};

// #pragma pack() 



(excerpt from the cpp)
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
#include "bitMap.h"

bitMap::bitMap()
{
	// constructor stuff... nothing yet
}

void bitMap::loadBitMap(char *inFile)
{
	// open the input bmp for reading in binary mode
	FILE *in;
	in=fopen(inFile, "rb");
	
	// make sure the file opens properly
	if(inFile == NULL)
	{
		fputs("Error reading file", stderr);
		exit(1);
	}	

	// read the bmp file header
	fread(&bmfh, sizeof(bmpFileHeader), 1, in);
	if(bmfh.bfType != 19778) // hexidecimal address is 0x4D42
	{
		cout << "File is not of bitmap type!" << endl;
		exit(1);
	}

	// read the bmp info header
	fread(&bmih, sizeof(bmpInfoHeader), 1, in);
	cout << "This image is " << bmih.biWidth << " pixels wide and " << bmih.biHeight << " pixels high." << endl;


Everything is commented out past this point except the appropriate brackets of course, so I didn't include.
closed account (o1vk4iN6)
// #pragma pack(2) // found this as a 2 byte alignment... no idea what this is

This is needed because of the type "short" followed by the type "int", there will be 2 bytes for padding so the int to 4 bytes.

You should add some static asserts to ensure the data size is correct.

1
2
3
4
5
6
7
8
9
10

struct bmpFileHeader
{
	unsigned short bfType;
	unsigned int bfSize;
	unsigned short bfReserved1;
	unsigned short bfReserved2;
	unsigned int bfOffBits;
}; static_assert( sizeof(bmpFileHeader) == 14, "bmpFileHeader size mismatch" );


Also a better way to change the alignment is with the "alignas" keyword.

http://en.cppreference.com/w/cpp/language/alignas
Topic archived. No new replies allowed.