在2d数组C ++中读取图像并保存

时间:2015-09-09 20:01:30

标签: c++ image

#include <iostream>
#include <fstream> // for file I/O
#define WIDTH 128
#define HEIGHT 128
#include <cmath>

using namespace std;
typedef unsigned char unchar;

class MImage
{

///////////////////////////////////////////////////////////////////////////////////////read

public:
    void readimage()
    {

        imageData = new unchar*[HEIGHT]; // create new array size: height of image.

        for (int i = 0; i < HEIGHT; i++)
        {
            imageData[i] = new unchar[WIDTH]; //create matrix.          
        }

        //image I/O
        pInFile = new ifstream;
        pInFile->open("L.bmp", ios::in | ios::binary); // open fileName and read as binary.

        pInFile->read(reinterpret_cast<char*>(imageHeaderData), 1078); //read bmp header data into array.

        for (int i = 0; i < HEIGHT; i++)
        {
            pInFile->read(reinterpret_cast<char*>(imageData[i]), WIDTH); //read row into each array entry.   
        }

        pInFile->close(); //close stream.
    }

public:
    void write()
    {

        //smoothFilter();
        pOutFile = new ofstream;
        pOutFile->open("output.bmp", ios::out | ios::binary);
        pOutFile->write(reinterpret_cast<char*>(imageHeaderData), 1078); //write header data onto output

        for (int i = 0; i < HEIGHT; i++)
        {

            pOutFile->write(reinterpret_cast<char*>(imageData[i]), WIDTH); // write new image data.

        }

        pOutFile->close(); //close stream
    }

public:
    ifstream* pInFile;
    ofstream* pOutFile;
    unchar imageHeaderData[1078]; //.bmp header data with offset 1078.  unchar** imageData;

};
int main()
{

    MImage abc;
    abc.readimage();
    abc.write();

    return 0;
}    

我无法读取二维数组中的图像,因此我可以对其进行一些处理。我使用了上面的代码,但保存的文件给出了错误。 我正在做的是首先读取.bmp文件128x128然后将其保存在另一个.bmp文件中。但是当我尝试打开输出文件时,它会给出错误&#34;文件已损坏或大尺寸&#34;

1 个答案:

答案 0 :(得分:1)

希望它有所帮助;) 我跳过了错误检查,但您应该将其添加到最终代码中。对于编写.bmp图像,首先是WITe BITMAPFILEHEADER,然后是BITMAPINFOHEADER,最后是实际的原始数据

FILE* filePtr;
int error;
unsigned int count;
BITMAPFILEHEADER bitmapFileHeader;
BITMAPINFOHEADER bitmapInfoHeader;
int imageSize;
unsigned char* bitmapImage;

// Open the height map file in binary.
error = fopen_s(&filePtr, filename, "rb");

// Read in the file header.
count = fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);

// Read in the bitmap info header.
count = fread(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

// Save the dimensions of the terrain.
Width= bitmapInfoHeader.biWidth;
Height= bitmapInfoHeader.biHeight;

// Calculate the size of the bitmap image data.
imageSize = Width* Height* 3;

// Allocate memory for the bitmap image data.
bitmapImage = new unsigned char[imageSize];

// Move to the beginning of the bitmap data.
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

// Read in the bitmap image data.
count = fread(bitmapImage, 1, imageSize, filePtr);

// Close the file.
error = fclose(filePtr);

不要忘记删除bitmapImage或使用std::vector代替