调用:
ucpp_Image = InputBmpWithName(i_Row, i_Col, SrcFileName);
代码:
// InputBmpWithName
// input a bmp image file with name FileName to a 2d array
// return the pointer pointing to the 2d image data array
// note: only for the 256 graylevels biBitCount=8
unsigned char **InputBmpWithName(int &i_Row, int &i_Col, const char *FileName)
{
// CFile bmpFile;
FILE *bmpFile;
BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER InfoHeader;
// bmpFile.Open(FileName, CFile::modeRead);
// bmpFile.Seek(0, CFile::begin);
// bmpFile.Read((char *)&FileHeader, sizeof(BITMAPFILEHEADER));
// bmpFile.Read((char *)&InfoHeader, sizeof(BITMAPINFOHEADER));
// bmpFile.Seek(sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+1024, CFile::begin); ????
if ((bmpFile=fopen(FileName,"rb")) == NULL)
{
cout << " The file " << FileName << " can not be opened\n";
return NULL;
}
// fread(&FileHeader, sizeof(BITMAPFILEHEADER), 1, bmpFile);
// fread(&InfoHeader, sizeof(BITMAPINFOHEADER), 1, bmpFile);
fread(&(FileHeader.bfType), sizeof(unsigned short), 1, bmpFile);
fread(&(FileHeader.bfSize), sizeof(unsigned long), 1, bmpFile);
fread(&(FileHeader.bfReserved1), sizeof(unsigned short), 1, bmpFile);
fread(&(FileHeader.bfReserved2), sizeof(unsigned short), 1, bmpFile);
fread(&(FileHeader.bfOffBits), sizeof(unsigned long), 1, bmpFile);
fread(&(InfoHeader.biSize), sizeof(unsigned long), 1, bmpFile);
fread(&(InfoHeader.biWidth), sizeof(long), 1, bmpFile);
fread(&(InfoHeader.biHeight), sizeof(long), 1, bmpFile);
fread(&(InfoHeader.biPlanes), sizeof(unsigned short), 1, bmpFile);
fread(&(InfoHeader.biBitCount), sizeof(unsigned short), 1, bmpFile);
fread(&(InfoHeader.biCompression), sizeof(unsigned long), 1, bmpFile);
fread(&(InfoHeader.biSizeImage), sizeof(unsigned long), 1, bmpFile);
fread(&(InfoHeader.biXPelsPerMeter), sizeof(long), 1, bmpFile);
fread(&(InfoHeader.biYPelsPerMeter), sizeof(long), 1, bmpFile);
fread(&(InfoHeader.biClrUsed), sizeof(unsigned long), 1, bmpFile);
fread(&(InfoHeader.biClrImportant), sizeof(unsigned long), 1, bmpFile);
// fseek(bmpFile, sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+1024, SEEK_SET);
fseek(bmpFile, 1078, SEEK_SET);
i_Row = InfoHeader.biHeight;
i_Col = InfoHeader.biWidth;
unsigned char **ucpp_Data = New2dUCharArr(i_Row, i_Col);
if (ucpp_Data == NULL) return NULL;
int i_Rest = 4 - i_Col % 4;
if (i_Rest == 4) i_Rest = 0;
int i_LineBytes = (DWORD)(i_Col + i_Rest);
unsigned char *i_LineData = new unsigned char[i_LineBytes];
for(int i=0; i<i_Row; i++)
{
// bmpFile.Read(i_LineData, i_LineBytes);
fread(i_LineData, sizeof(unsigned char), i_LineBytes, bmpFile);
for(int j=0; j<i_Col; j++)
{
ucpp_Data[i_Row-1-i][j] = i_LineData[j];
}
}
// bmpFile.Close();
fclose(bmpFile);
delete i_LineData;
return(ucpp_Data);
}
// 分配二维空间
// New2dUCharArr
// allocate a 2d dynamic unsigned char array
// return the pointer pointing to the array
unsigned char **New2dUCharArr(const int i_Row, const int i_Col)
{
unsigned char **ucpp_Data = new unsigned char *[i_Row];
if (ucpp_Data == NULL) return NULL;
for(int i=0; i<i_Row; i++)
{
ucpp_Data[i] = new unsigned char[i_Col];
if (ucpp_Data[i] == NULL)
{
for(int j=0; j<i; j++) delete []ucpp_Data[j];
delete []ucpp_Data;
return NULL;
}
}
return ucpp_Data;
}
// Delete2d
// free a 2d dynamic unsigned char array
void Delete2d(unsigned char **ucpp_Data, const int i_Row)
{
for(int i=0; i<i_Row; i++) delete []ucpp_Data[i];
delete []ucpp_Data;
}