unsigned char * LoadFileContent(const char *path, int &filesize) {
unsigned char*fileContent = nullptr;filesize = 0;
FILE*pFile = fopen(path, "rb");
if (pFile) {
fseek(pFile, 0, SEEK_END);
int nLen = ftell(pFile);
if (nLen > 0) {
rewind(pFile);
fileContent = new unsigned char[nLen + 1];
fread(fileContent, sizeof(unsigned char), nLen, pFile);
fileContent[nLen] = '\0';
filesize = nLen;
}
fclose(pFile);
}
return fileContent;
}
unsigned char* DecodeBMP(unsigned char*bmpFileData, int&width, int&height) {
if (0x4D42 == *((unsigned short*)bmpFileData)) {
int pixelDataOffset = *((int*)(bmpFileData + 10));
width = *((int*)(bmpFileData + 18));
height = *((int*)(bmpFileData + 22));
unsigned char*pixelData = bmpFileData + pixelDataOffset;
for (int i = 0; i < width*height * 3; i += 3) {
unsigned char temp = pixelData[i];
pixelData[i] = pixelData[i + 2];
pixelData[i + 2] = temp;
}
return pixelData;
}
return nullptr;
}
测试代码
int nFileSize = 0;
int bmpWidth = 0, bmpHeight = 0;
if (bmpFileContent == nullptr) {
return 0;
}
unsigned char*pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight);
/// 这样就获取的BMP图像的大小(nFileSize ),高度(bmpHeight),宽(bmpWidth),首地址(bmpFileContent )