生成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 
#include 
#include 
#include 
#include 
using namespace std;
template
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

你可能感兴趣的:(cuda)