简体   繁体   English

读取BMP文件-C / C ++获取数据

[英]Reading BMP file - Getting data out C/C++

Here's my first post, and first question. 这是我的第一篇文章,也是第一个问题。

Why shouldn't I use: bmpFile.read((char*)(&bmpImageData),bmpImageDataSize); 我为什么不应该使用: bmpFile.read((char*)(&bmpImageData),bmpImageDataSize); to read data block from BMP file, and how should I do this properly (I know about BGR triplets, but as for now I do not really care about them existing) EDIT: Maybe I should clarify something - as the code is right now it compiles pretty well, but stops working on the line provided higher. 读取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;
}

My mybmp.h header: 我的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)

The line bmpFile.read((char*)(&bmpImageData),bmpImageDataSize); bmpFile.read((char*)(&bmpImageData),bmpImageDataSize); looks wrong, the bmpImageData is already a char * . 看起来不对, bmpImageData已经是char * Taking the address gives you a char ** (which will probably be on the stack), which you then write to, corrupting your stack. 接收地址会给您一个char ** (可能会在堆栈中),然后您将其写入其中,从而破坏堆栈。

Change your problem line to this bmpFile.read (bmpImageData, bmpImageDataSize); 将您的问题行更改为此bmpFile.read (bmpImageData, bmpImageDataSize); does that help? 有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM