Hello. I am currently trying to extract R, G, B values from a 256 colour bitmap image (using for testing purposes a fixed width of 152 and height 150). I have checked using BITMAPINFOHEADER and BITMAPFILEHEADER that the offset is 1078 and bytes per pixel is 1.
The image data is initially placed in a 2 dimensional array before being placed into a 1 dimensional array. Three 2 dimensional arrays were allocated to hold the R, G, B values from each pixel.
When I run the program, the following Debug Assertion Failed! message appears:
Expression: _BLOCK_TYPE_IS_VALID (pHead->nBlockUse)
Below is the code and I would value your expert feedback and how to fix this error.
unsigned char** Allocate2DArray(unsigned char** buffer, int w, int h)
{
buffer = new unsigned char*[h];
if(buffer == NULL) return 0;
for(int j=0; j<h; j++)
{ buffer[j] = new unsigned char[w];
if(buffer[j] == NULL) return 0;
}
return buffer;
}
void DeAllocate2DArray(unsigned char** buffer, int h)
{
for(int j=h-1; j>= 0; j--)
delete[] buffer[j];
delete[] buffer;
}
using namespace std;
int main()
{
ifstream image;
image.open("image.bmp",std::ios_base::binary);
if (image.is_open())
{
cout<< "function success\n";
}
else
{
cout<< "unable to open file";
}
const int width = 152;
const int height = 150;
unsigned char m_cHeaderData[1078];
unsigned char** m_cImageData = new unsigned char* [height];
for( int i = 0; i <height; i++)
{
m_cImageData[i] = new unsigned char [width];
}
ifstream* m_pInFile;
m_pInFile = new ifstream;
m_pInFile->open("image.bmp", ios::in | ios::binary);
m_pInFile->seekg(0, ios::beg);
m_pInFile->read(reinterpret_cast<char*>(m_cHeaderData), 1078);
for(int i = 0; i <height; i++)
{
m_pInFile->read(reinterpret_cast<char*>(m_cImageData[i]), width);
}
m_pInFile->close();
unsigned char* array_1D;
array_1D = new unsigned char[height*width];
if(array_1D == NULL) return 0;
int offset = 0;
for(int j=0; j<height; j++)
{
offset = width * j;
for(int i=0; i<width; i++)
{
array_1D[offset + i] = m_cImageData[j][i];
}
}
unsigned char**arrayR_2D, **arrayG_2D, **arrayB_2D;
arrayR_2D = Allocate2DArray(arrayR_2D, width, height);
arrayG_2D = Allocate2DArray(arrayG_2D, width, height);
arrayB_2D = Allocate2DArray(arrayB_2D, width, height);
if(arrayR_2D == NULL || arrayG_2D == NULL || arrayB_2D == NULL) return 0;
int offsetx = 0;
int counter = 0;
int bytesPerPixel = 1;
for(int j=0; j<height; j++)
{
offset = width * j;
for(int i=0; i<width*bytesPerPixel; i+=bytesPerPixel)
{
arrayB_2D[j][counter++] = array_1D[offsetx + i+0];
arrayG_2D[j][counter++] = array_1D[offsetx + i+1];
arrayR_2D[j][counter++] = array_1D[offsetx + i+2];
}
counter = 0;
}
DeAllocate2DArray(arrayR_2D, height);
DeAllocate2DArray(arrayG_2D, height);
DeAllocate2DArray(arrayB_2D, height);
delete[] array_1D;
delete m_pInFile;
delete m_pOutFile;
for(int i = 0; i <height; i++)
{
delete[] m_cImageData[i];
}
delete[] m_cImageData;
system("pause");
return 0;
}