Hello,
I'm having trouble reading a bmp file header into a buffer that I've allocated for it. For the purposes of this assignment, I'm not allowed to use cstdlib buffer functions, so I'm (i) allocating the buffer myself, (ii) reading header data into it by decoding the binary byte by byte (for this I use a binarytools file which I know works), (iii) building my bmp structs out of buffer data and (iv) freeing the buffer memory afterward (then I'll go on to read the info header and then the image data). However, I'm getting a segfault in the first step of the process which I can't figure out.
Here's the function 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
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);
}
// allocate a buffer to read in the file header (14 bytes)
uint8 *fBufferPtr = new uint8[bmfh_size];
fread(fBufferPtr, 1, bmfh_size, in); // read bmp header into the buffer
const uint8 *streamBufferPtr = fBufferPtr; // copy the buffer pointer
// build header struct out of buffer with binaryutils routines
// first two bytes of fBuffer get decoded and stored as bmfh.bfType
DecodeBinary_int16(&streamBufferPtr, &bmfh.bfType);
// then do the check to ensure filetype == bmp
if(bmfh.bfType != 19778) // hexidecimal address is 0x4D42
{
cout << "File is not a bitmap!" << endl;
exit(2);
}
// next four bytes of fBuffer get decoded and stored as bmfh.bfSize
DecodeBinary_int32(&streamBufferPtr, &bmfh.bfSize);
// next two are bmfh.bfReserved1 (0)
DecodeBinary_int16(&streamBufferPtr, &bmfh.bfReserved1);
// next two are bmfh.bfReserved2 (0)
DecodeBinary_int16(&streamBufferPtr, &bmfh.bfReserved2);
// next four are bmfh.bfOffBits
DecodeBinary_int32(&streamBufferPtr, &bmfh.bfOffBits);
// now that the struct is built, let's clean up the buffer
delete [] &fBufferPtr;
// let's see if any of that was read in correctly by printing the struct
cout << bmfh.bfType << endl;
cout << bmfh.bfSize << endl;
cout << bmfh.bfReserved1 << endl;
cout << bmfh.bfReserved2 << endl;
cout << bmfh.bfOffBits << endl;
}
|
Here's an expert of my header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "codec_binary.h"
using namespace std;
// general data declarations
const int bmfh_size = 14; // file header will always be 14 bytes in BMP files
const int bmih_size = 40; // info header will always be 40 bytes in BMP files
// bitmap header
struct bmpFileHeader
{
int16 bfType; // file type
int32 bfSize; // size in bytes of bmp
int16 bfReserved1;
int16 short bfReserved2;
int32 bfOffBits;
};
|
Here's the valgrind diagnosis:
==22766== Invalid read of size 8
==22766== at 0x25FB8A: __fread (in /usr/lib/system/libsystem_c.dylib)
==22766== by 0x25FB33: fread (in /usr/lib/system/libsystem_c.dylib)
==22766== by 0x1000015D7: bitMap::loadBitMap(char*) (bitMap.cpp:41)
==22766== by 0x100001246: main (reader.cpp:18)
==22766== Address 0x68 is not stack'd, malloc'd or (recently) free'd
Can anyone make heads or tails of what I'm doing wrong?