这是我的第一篇文章和第一个问题。
为什么我不应该使用:bmpFile.read((char*)(&bmpImageData),bmpImageDataSize);
从BMP文件读取数据块,我应该如何正确地执行此操作(我知道BGR三元组,但是现在我并不关心它们存在)编辑:也许我应该澄清一些事情 - 因为代码现在它编译得非常好,但是停止在提供更高的线上工作。
#include "stdafx.h"
#include "mybmp.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
filebuf bmpBuff;
ifstream bmpFile("test.bmp", ios::binary);
bmpBuff.open("test_zapis.bmp", ios::binary | ios::out);
ostream bmpSaved(&bmpBuff);
unsigned long bmpSizeBuffer;
bmpFile.seekg(2, ios::beg); // Missing BMP, DIB identification for good byte adjustment
mybmp bmpHeader;
bmpFile.read((char*)(&bmpHeader),sizeof(mybmp));
if(bmpHeader.FReadFileSize()>0)
{
unsigned long bmpImageDataSize = bmpHeader.FReadFileSize()-bmpHeader.FReadOffset(); // Reading ImageData size
char* bmpImageData = new char[bmpImageDataSize];
bmpFile.seekg(bmpHeader.FReadOffset(),ios::beg); // Positioning pointer to ImageData start point
bmpFile.read((char*)(&bmpImageData),bmpImageDataSize); // This breaks down // Reading ImageData to bmpImageData buffer
// Fun with buffer //
for(int i = 0; i < bmpImageDataSize; i++)
{
bmpImageData[i]++;
}
// Saving (...) //
}
else
{
cout << "Plik nie zostal wczytany"<<endl;
}
return 0;
}
我的mybmp.h标题:
#pragma pack(push,1)
class mybmp
{
unsigned long fileSize; // BMP overall filesize in bytes
unsigned long reserved; // filled with 0
unsigned long fileOffset; // Offset before Raster Data
//---------------------------//
unsigned long size; // Size of InfoHeader = 40
unsigned long width; // overall image width
unsigned long height; // overall image height;
unsigned short planes; // = 1;
unsigned short bitCounts; // Bits per Pixel
unsigned long compression; // Type of compression
unsigned long typeOfImage; // compressed size of Image. 0 if compression parameter = 0;
unsigned long xPixelsPerM; // horizontal resolution - Pixels/Meter
unsigned long yPixelsPerM; // vertical resolution - Pixels/Meter
unsigned long colorsUsed; // Number of colors actually used
unsigned long colorsImportant; // Number of important colors, 0 if all
//--------------------------//
public:
mybmp(void);
unsigned long FReadFileSize();
void FPrintObject();
unsigned long FReadOffset();
~mybmp(void);
};
#pragma pack(pop)
答案 0 :(得分:2)
bmpFile.read((char*)(&bmpImageData),bmpImageDataSize);
行显示错误,bmpImageData
已经是char *
。获取地址会给你一个char **
(可能在堆栈中),然后写入,破坏你的堆栈。
将问题行更改为此bmpFile.read (bmpImageData, bmpImageDataSize);
有帮助吗?