Loading Bitmap

I've no idea what the issue is. I'm trying to load the pixel data of a bitmap into an array. I'm using std::istream::read to read in the pixel data. Maybe that doesn't do what I thought it did? I don't know because all that's in my array is the null termination character after reading in the data. All the other info in the bmp is read fine. Image size, width, height, bits per pixel, offset to raster data all fine. Just when I actually attempt to read the raster data do I encounter difficulties.

Thank you.
You obviously are doing something wrong.

We have no way of knowing what you are doing wrong without seeing what you're doing.
Here's the code.

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
class Bitmap
{
public:
	Bitmap(const char *fileName)
	{
		m_iFile.open(fileName, std::ios::in, std::ios::binary);

		m_iFile.seekg(0, m_iFile.end);

		m_imageSize = m_iFile.tellg();

		m_iFile.seekg(0, m_iFile.beg);

		m_iFile.seekg(10, m_iFile.beg);

		m_offsetToRasterData = m_iFile.get();

		m_iFile.seekg(18, m_iFile.beg);

		m_imageWidth = m_iFile.get();

		m_iFile.seekg(22, m_iFile.beg);

		m_imageHeight = m_iFile.get();

		m_iFile.seekg(28, m_iFile.beg);

		m_bitsPerPixel = m_iFile.get();

		m_iFile.seekg(54, m_iFile.beg);

		m_pixelData = new unsigned char[m_imageSize];

		m_iFile.read((char*)m_pixelData, m_imageSize);
	}
	~Bitmap()
	{
		m_iFile.close();
	}

public:
	std::fstream m_iFile;
	unsigned int m_imageSize;
	unsigned int m_offsetToRasterData;
	unsigned int m_imageWidth;
	unsigned int m_imageHeight;
	unsigned int m_bitsPerPixel;
	unsigned char *m_pixelData;
};;

Last edited on
Well this is unrelated to your problem, but you have a huge memory leak because you never delete[] your array. Consider using a smart pointer or a vector.


That said....

I don't know because all that's in my array is the null termination character after reading in the data.


null termination character... as in 0?

does your bitmap have black pixels? Because that would have 0s in it.

I don't see anything that would cause the kind of failure you're describing. How are you examining the pixel data?
Yes, I'm aware of the leak. That was just some test code to see if I could get it loaded. And by null termination character I mean /0. I'm not currently doing anything with the pixel data yet other than try to load it into the array as raw unformatted data.
Allow me to rephrase my question:

How are you determining the pixel data is wrong?

The existence of zeros doesn't make it wrong on its own -- it's very possible for valid pixel data to contain zeros.

And by null termination character I mean /0


'\0' is not a special character, it is just the literal value of 0 cast to a char.
The same way '\1' is a literal value of 1, and '\2' is a literal value of 2:

1
2
3
4
if('\0' == 0)
    cout << "this will print";
if('\101' == 'A')   // 101 in octal is 0x41 in hex.  Which is the ASCII code for the letter 'A'
    cout << "this will also print";
When I try to print out the array, it doesn't print anything. Also when I step through the program the ONLY thing I see in the array is 0'\0'. There should be as many elements as the size of m_imageSize, correct?
Last edited on
When I try to print out the array, it doesn't print anything.


Printing binary data doesn't work.

Also when I step through the program the ONLY thing I see in the array is 0'\0'.


Viewing binary data as a c string doesn't work for the same reason printing it doesn't work.


Unlike with strings, 0 does not mark the end of the data, here. It is just another value a byte can have. For example, a black pixel will have 0 four times in a row.


There should be as many elements as the size of m_imageSize, correct?


There are. The problem is you are viewing the data as a string, and therefore it stops showing you info once it reaches the first 0 byte.
Last edited on
Oh, so it's actually been working correctly this whole time. Thank you for the help, Disch.
Topic archived. No new replies allowed.