首页 > 解决方案 > 我正在尝试使用 c/c++ 读取、写入和翻译 bmp 图像文件,窗口不支持输出 bmp 文件格式(使用 Visual Studio)

问题描述

代码从这里开始

//creat the stucture for the bitmap file, which consist the header of size 54, which contains the information about the file.     
    struct BMP {
        int width;
        int height;
        unsigned char header[54];
        unsigned char *pixels;
        int size;
    };

c/c++中bmp文件的读写功能。这里我使用了 fopen() 函数

// read the image
BMP readBMP(string filename) {
    BMP image;
    int i;
    string fileName = filename;
    FILE *f = fopen(fileName.c_str(), "rb");
    fread(image.header, sizeof(unsigned char), 54, f); // read the 54-byte header

    // extract image height and width from header
    image.width = *(int *) &image.header[18];
    image.height = *(int *) &image.header[22];
    cout<<"inage width: "<< image.width <<", image height: "<< image.height <<endl;

    image.size = 3 * image.width * image.height;
    image.pixels = new unsigned char[image.size]; // allocate 3 bytes per pixel
    fread(image.pixels, sizeof(unsigned char), image.size, f); // read the rest of the data at once
    fclose(f);

    // bmp stors data in BGR format, so changing the BGR to RGB.
    for (i = 0; i < image.size; i += 3) {
        unsigned char tmp = image.pixels[i];
        image.pixels[i] = image.pixels[i + 2];
        image.pixels[i + 2] = tmp;
    }
    return image;
}

// write image
void writeBMP(string filename, BMP image) {
    string fileName = filename;
    FILE *out = fopen(fileName.c_str(), "wb");
    fwrite(image.header, sizeof(unsigned char), 54, out);
    int i;
    unsigned char tmp;
    for (i = 0; i < image.size; i += 3) {
        tmp = image.pixels[i];
        image.pixels[i] = image.pixels[i + 2];
        image.pixels[i + 2] = tmp;
    }
    fwrite(image.pixels, sizeof(unsigned char), image.size, out); // read the rest of the data at once
    fclose(out);
}

图像平移功能,用于平移图像对应的 x 和 y 维度。

// image translation
BMP translation(BMP image, int tx, int ty) {
    BMP newImage = image;
    unsigned char *pixel = new unsigned char[image.size];

    // translation
    for (int x = 0; x < image.width; ++x){
        for (int y = 0; y < image.height; ++y) {
            int xx = x-tx;
            int yy = y+ty;
            if (xx >= 0 && xx < image.width && yy >= 0 && yy < image.height){
                pixel[(y * image.width + x) * 3 + 0] = image.pixels[(yy * image.width + xx) * 3 + 0];
                pixel[(y * image.width + x) * 3 + 1] = image.pixels[(yy * image.width + xx) * 3 + 1];
                pixel[(y * image.width + x) * 3 + 2] = image.pixels[(yy * image.width + xx) * 3 + 2];
            }
        }
    }
    newImage.pixels = pixel;
    return newImage;
}

执行读、写和翻译操作的主函数调用如下。

int main() {
    BMP image = readBMP("sample_640×426.bmp"); 
    BMP image_t;

    // // original image
    writeBMP("Output-11.bmp", image);

    // image translation
    int x0=100, y0=50;
    image_t = translation(image, x0, y0);
    writeBMP("Out_translate.bmp", image_t);

    return 0;
}

windows输出图像在这里[1] [1]:https ://i.stack.imgur.com/gd83D.png

标签: cvisual-studio-2010image-processingvisual-c++bmp

解决方案


推荐阅读