生成bmp

由于想要画Julia集,然而每次都用OpenGL去绘图代码量实在是太大了,不美观
于是就决定生成bmp
参照http://www.cnblogs.com/Matrix_Yao/archive/2009/12/02/1615295.html
准备好了存储格式
然后要注意 小端输出
最后有个坑,还好找到相关blog解决了
https://my.oschina.net/leeldy/blog/108304
巨坑,坑死我了

#include <iostream>
#include <iomanip>
#include <fstream>
#include <Windows.h>
#include <wingdi.h>
using namespace std;
template<class T>
void wri(ofstream &os, T data) {
    //小端输出
    int len = sizeof(T);
    char *p = new char[len];
    for (int i = 0; i < len; ++i) {
        p[i] = data % 256;
        data >>= 8;
    }
    os.write(p, len);
}
struct BMP {
    BITMAPFILEHEADER p1;
    BITMAPINFOHEADER p2;
    unsigned char *src;
    int wid, hei;
    BMP(unsigned char *source, int width, int height):
        src(source), wid(width), hei(height) {
        p1.bfType = 0x4d42;//"MB" 小端输出变成"BM"
        p1.bfSize = 54 + width * height * 4;

        p1.bfReserved1 = p1.bfReserved2 = 0;
        p1.bfOffBits = 54;

        p2.biSize = 40;//40 (in windows)
        p2.biWidth = width;
        p2.biHeight = height;
        p2.biPlanes = 1;
        p2.biBitCount = 32;
        p2.biCompression = 0;
        p2.biSizeImage = width * height * 4;

        p2.biXPelsPerMeter = width;
        p2.biYPelsPerMeter = height;
        p2.biClrUsed = 0;
        p2.biClrImportant = 0;
    }
    void write(string path) {
        ofstream out(path, ios_base::out | ios_base::binary);
        wri(out, p1.bfType);
        wri(out, p1.bfSize);
        wri(out, p1.bfReserved1);
        wri(out, p1.bfReserved2);
        wri(out, p1.bfOffBits);

        wri(out, p2.biSize);
        wri(out, p2.biWidth);
        wri(out, p2.biHeight);
        wri(out, p2.biPlanes);
        wri(out, p2.biBitCount);
        wri(out, p2.biCompression);
        wri(out, p2.biSizeImage);
        wri(out, p2.biXPelsPerMeter);
        wri(out, p2.biYPelsPerMeter);
        wri(out, p2.biClrUsed);
        wri(out, p2.biClrImportant);

        out.write((char*)src, wid*hei*4);
        out.close();
    }
};
const int width = 200, height = 100;
unsigned char p[width*height * 4];
int main()
{
    for (int i = 0; i < height; ++i)
        for (int j = 0; j < width; ++j) {
            p[(i*width + j) * 4] = j;
            p[(i*width + j) * 4 + 1] = i;
        }
    BMP s(p, width, height);
    s.write(".//a.bmp");
    return 0;
}

生成图片:这里写图片描述
可以发现图片的储存是从下到上,从左到右依次存的
换言之,从文件绘制图片是从下到上,从左到右依次绘制的

像素点4通道的顺序是BGRA

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值