简易点阵数据转单色BMP

点阵数据转单色位图源码

extern void plb_dot_to_bmp(const void *mem, size_t size, const char *bmp, int width)
{
    int height;
    char head[64];

    height = size / width;
    size = height * width;
    memset(head, 0, sizeof(head));
    //bmp head info
    head[0] = 'B';
    head[1] = 'M';
    //2-5 bfsize
    head[2] = (size + 0x3e) & 0xff;
    head[3] = ((size + 0x3e) >> 8) & 0xff;
    head[4] = ((size + 0x3e) >> 16) & 0xff;
    head[5] = ((size + 0x3e) >> 24) & 0xff;
    //10-13 bfoffbits
    head[10] = 0x3e;
    //14-17 biSize
    head[14] = 0x28;
    //18-21 biWidth
    head[18] = (width * 8) & 0xff;
    head[19] = (width >> 5) & 0xff;
    head[20] = (width >> 13) & 0xff;
    head[21] = (width >> 21) & 0xff;
    //22-25 biHeight
    head[22] = height & 0xff;
    head[23] = (height >> 8) & 0xff;
    head[24] = (height >> 16) & 0xff;
    head[25] = (height >> 24) & 0xff;
    //26-27 biplanes
    head[26] = 1;
    //28-29 biBitcount
    head[28] = 1;
    //biSizeImage
    head[34] = size & 0xff;
    head[35] = (size >> 8) & 0xff;
    head[36] = (size >> 16) & 0xff;
    head[37] = (size >> 24) & 0xff;

    head[58] = 0xff;
    head[59] = 0xff;
    head[60] = 0xff;
    head[61] = 0x0;

    FILE *fp;
    int color_flag;
    
    if (NULL == (fp = fopen(bmp, "w+"))) {
        return;
    }

    fwrite(head, 1, 0x3e, fp);
    
    color_flag = 0;
    if (color_flag) {
        fwrite(mem, 1, size, fp);
    } else {
        char c;
        int i, j;
        for (i = height; i > 0; i--) {
            for (j = 0; j < width; j++) {
                c =  ((char *)mem)[(height - i) * width + j] ^ 0xff;
                fwrite(&c, 1, 1, fp);
            }
        }
    }

    fclose(fp);
}

可根据实际使用平台,将函数内参数color_flag提取封装进一步使用。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,797评论 1 32
  • 五月春和夏揉和的阳光 洒向大地慈润人间 带来暖心的温暖 归家的驿站 心灵的港湾 唯有妈妈的爱 可以替换 在妈妈的节...
    所谓珈人阅读 5,510评论 14 6
  • 自从2016年与cctalk相遇,使我这个在乡村工作了20几年的老教师跟现代教育接轨了。互+美丽乡村课程,...
    甘肃天祝马肖芳阅读 1,638评论 0 0
  • 2019年4月2日 星期二 晴–2~12℃ 上个星期天,舅公三周年祭日。 这是舅公过世后,他们家的一件大事,亲...
    波之角落阅读 4,788评论 35 22