C++创建DDS文件

OS: ubuntu12.04

创建DDS文件头

这里只使用DXT1压缩,mipmaps为1,输入格式为GBR

   #define PUTL32(buf, l) \
        (buf)[0] = ((l)      ) & 0xff; \
	(buf)[1] = ((l) >>  8) & 0xff; \
	(buf)[2] = ((l) >> 16) & 0xff; \
	(buf)[3] = ((l) >> 24) & 0xff;

    int compression = DDS_COMPRESS_BC1;
    unsigned int rmask = 0x00ff0000;
    unsigned int gmask = 0x0000ff00;
    unsigned int bmask = 0x000000ff;
    unsigned int amask = 0xff000000;
    unsigned int data_size = 0;
    unsigned int ddsSZ = 0;
    unsigned int fourcc = 0;

    unsigned char hdr[DDS_HEADERSIZE] = {0};
    int fmtbpp = 3, flags = 0, pflags = 0, caps = 0, caps2 = 0, num_mipmaps = 1, size = 0;

    PUTL32(hdr, FOURCC('D', 'D', 'S', ' '));
    PUTL32(hdr + 4, 124);
    PUTL32(hdr + 12, height);
    PUTL32(hdr + 16, width);
    PUTL32(hdr + 76, 32);
    PUTL32(hdr + 88, fmtbpp << 3);
    PUTL32(hdr + 92, rmask);
    PUTL32(hdr + 96, gmask);
    PUTL32(hdr + 100, bmask);
    PUTL32(hdr + 104, amask);

    flags = DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT;
    caps = DDSCAPS_TEXTURE;

    PUTL32(hdr + 28, num_mipmaps);
    PUTL32(hdr + 108, caps);
    PUTL32(hdr + 112, caps2);

    flags |= DDSD_LINEARSIZE;
    pflags |= DDPF_FOURCC;
    fourcc = FOURCC('D', 'X', 'T', '1');

    PUTL32(hdr + 8, flags);
    PUTL32(hdr + 80, pflags);
    PUTL32(hdr + 84, fourcc);

    size = ((width + 3) >> 2) * ((height + 3) >> 2);
    size *= 8;

    PUTL32(hdr + 20, size);