C++解析MBP图片代码

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;

        unsigned char *bmpFileContent = LoadFileContent(bmpPath, nFileSize);
if (bmpFileContent == nullptr) {
return 0;

unsigned char*pixelData = DecodeBMP(bmpFileContent, bmpWidth, bmpHeight);

/// 这样就获取的BMP图像的大小(nFileSize ),高度(bmpHeight),宽(bmpWidth),首地址(bmpFileContent )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值