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

标签 c++ char ifstream bmp

这是我的第一篇文章,也是第一个问题。

为什么我不应该使用:bmpFile.read((char*)(&bmpImageData),bmpImageDataSize); 从 BMP 文件中读取数据 block ,我应该如何正确地执行此操作(我知道 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)

最佳答案

bmpFile.read((char*)(&bmpImageData),bmpImageDataSize); 看起来不对,bmpImageData 已经是一个 char *。获取地址会给你一个 char ** (它可能在堆栈上),然后你写入它,破坏你的堆栈。

将您的问题行更改为此 bmpFile.read (bmpImageData, bmpImageDataSize); 这有帮助吗?

关于c++ - 读取 BMP 文件 - 从 C/C++ 中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443396/

相关文章:

C++ 到 Ada 的指针和异常

c++ - 需要将char*(指针)转换为wchar_t*(指针)

c++ - 关于使用 ifstream 读取文件,为什么我的程序在 Windows 和 Linux 上产生不同的结果?

java - 如何用Java中的字符串 '2'替换字符串中的字符 "to"?

c++ - 根据数组中的字符串检查从文件中读入的单词

结构的c++ vector 和从文件中读取

c++ - 将字符串从 istream 递归传输到数组中 - C++

c++ - C++ 中是否有实际的 8 位整数数据类型

C++ 模板用法

c++ - 避免 std::bad_alloc。 new 应该返回一个 NULL 指针