2019独角兽企业重金招聘Python工程师标准>>>

以下方法是从其他地方收集而来,并加以修改的结果。

头文件 bmp_image.h

#ifndef _LI_BITMAP_H_
#define _LI_BITMAP_H_
#pragma pack(push, 1)
typedef unsigned char  U8;
typedef unsigned short U16;
typedef unsigned int   U32;
typedef struct tagBITMAPFILEHEADER{U16 bfType;U32 bfSize;U16 bfReserved1;U16 bfReserved2;U32 bfOffBits;
} BITMAPFILEHEADER;typedef struct tagBITMAPINFOHEADER{U32 biSize;U32 biWidth;U32 biHeight;U16 biPlanes;U16 biBitCount;U32 biCompression;U32 biSizeImage;U32 biXPelsPerMeter;U32 biYPelsPerMeter;U32 biClrUsed;U32 biClrImportant;
} BITMAPINFOHEADER;typedef struct tagRGBQUAD{U8 rgbBlue;U8 rgbGreen;U8 rgbRed;U8 rgbReserved;
} RGBQUAD;typedef struct tagBITMAPINFO{BITMAPINFOHEADER bmiHeader;RGBQUAD bmiColors[1];
} BITMAPINFO;#pragma pack(pop)
#endif

main.c

#include "stdafx.h"
#include "bmp_image.h" // bmp_image header file.
#define HEIGHT 640
#define WIDTH 480
//生成BMP图片(无颜色表的位图):在RGB(A)位图数据的基础上加上文件信息头和位图信息头
int GenBmpFile(U8 *pData, U8 bitCountPerPix, U32 width, U32 height, const char *filename);
//获取BMP文件的位图数据(无颜色表的位图):丢掉BMP文件的文件信息头和位图信息头,获取其RGB(A)位图数据
U8* GetBmpData(U8 *bitCountPerPix, U32 *width, U32 *height, const char* filename);
//释放GetBmpData分配的空间
void FreeBmpData(U8 *pdata);
//生成BMP图片(无颜色表的位图):在RGB(A)位图数据的基础上加上文件信息头和位图信息头
int GenBmpFile(U8 *pData, U8 bitCountPerPix, U32 width, U32 height, const char *filename)
{      FILE *fp;errno_t err;err = fopen_s(&fp,filename, "wb");if(err)      {          printf("fopen failed : %s, %d\n", __FILE__, __LINE__);return 0;}U32 bmppitch = ((width*bitCountPerPix + 31) >> 5) << 2;U32 filesize = bmppitch*height;BITMAPFILE bmpfile;bmpfile.bfHeader.bfType = 0x4D42;bmpfile.bfHeader.bfSize = filesize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);bmpfile.bfHeader.bfReserved1 = 0;bmpfile.bfHeader.bfReserved2 = 0;bmpfile.bfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);bmpfile.biInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);bmpfile.biInfo.bmiHeader.biWidth = width;bmpfile.biInfo.bmiHeader.biHeight = height;bmpfile.biInfo.bmiHeader.biPlanes = 1;bmpfile.biInfo.bmiHeader.biBitCount = bitCountPerPix;bmpfile.biInfo.bmiHeader.biCompression = 0;bmpfile.biInfo.bmiHeader.biSizeImage = 0;bmpfile.biInfo.bmiHeader.biXPelsPerMeter = 0;bmpfile.biInfo.bmiHeader.biYPelsPerMeter = 0;bmpfile.biInfo.bmiHeader.biClrUsed = 0;bmpfile.biInfo.bmiHeader.biClrImportant = 0;fwrite(&(bmpfile.bfHeader), sizeof(BITMAPFILEHEADER), 1, fp);fwrite(&(bmpfile.biInfo.bmiHeader), sizeof(BITMAPINFOHEADER), 1, fp);U8 *pEachLinBuf = (U8*)malloc(bmppitch);memset(pEachLinBuf, 0, bmppitch);U8 BytePerPix = bitCountPerPix >> 3;U32 pitch = width * BytePerPix;if(pEachLinBuf)      {          int h,w;for(h = height-1; h >= 0;h--)          {              for(w = 0; w < (int)width; w++)              {                 //copy by a pixel                  pEachLinBuf[w*BytePerPix+0] = pData[h*pitch + w*BytePerPix + 0];pEachLinBuf[w*BytePerPix+1] = pData[h*pitch + w*BytePerPix + 1];pEachLinBuf[w*BytePerPix+2] = pData[h*pitch + w*BytePerPix + 2];}fwrite(pEachLinBuf, bmppitch, 1, fp);}free(pEachLinBuf);}fclose(fp);return 1;}//获取BMP文件的位图数据(无颜色表的位图):丢掉BMP文件的文件信息头和位图信息头,获取其RGB(A)位图数据
U8* GetBmpData(U8 *bitCountPerPix, U32 *width, U32 *height, const char* filename)  {     FILE *pf;errno_t err;err = fopen_s(&pf,filename, "rb");if(err)                   {         printf("fopen failed : %s, %d\n", __FILE__, __LINE__);return NULL;}BITMAPFILE bmpfile;fread(&(bmpfile.bfHeader), sizeof(BITMAPFILEHEADER), 1, pf);fread(&(bmpfile.biInfo.bmiHeader), sizeof(BITMAPINFOHEADER), 1, pf);// add by yuegy//print_bmfh(bmpfile.bfHeader);//print_bmih(bmpfile.biInfo.bmiHeader);if(bitCountPerPix)      {          *bitCountPerPix = (U8)bmpfile.biInfo.bmiHeader.biBitCount;}if(width)      {          *width = bmpfile.biInfo.bmiHeader.biWidth;}if(height)      {          *height = bmpfile.biInfo.bmiHeader.biHeight;}U32 bmppicth = (((*width)*(*bitCountPerPix) + 31) >> 5) << 2;U8 *pdata = (U8*)malloc((*height)*bmppicth);U8 *pEachLinBuf = (U8*)malloc(bmppicth);memset(pEachLinBuf, 0, bmppicth);U8 BytePerPix = (*bitCountPerPix) >> 3;U32 pitch = (*width) * BytePerPix;if(pdata && pEachLinBuf)      {          int w, h;for(h = (*height) - 1;h >= 0;h--)          {              fread(pEachLinBuf, bmppicth, 1, pf);for(w = 0;w < (int)(*width);w++)              {                  pdata[h*pitch + w*BytePerPix + 0] = pEachLinBuf[w*BytePerPix+0];pdata[h*pitch + w*BytePerPix + 1] = pEachLinBuf[w*BytePerPix+1];pdata[h*pitch + w*BytePerPix + 2] = pEachLinBuf[w*BytePerPix+2];}}free(pEachLinBuf);}fclose(pf);return pdata;
}//释放GetBmpData分配的空间
void FreeBmpData(U8 *pdata)  {      if(pdata)      {          free(pdata);pdata = NULL;}
}/*** 定义颜色结构体*/
typedef struct _LI_RGB  {      U8 b;U8 g;U8 r;
}LI_RGB;/*** 画点* pRGB 指针* position 点坐标* iImageH 图像高*/
void drawPoint(LI_RGB* pRGB, POINT position, int iImageH)
{LI_RGB* tmp = pRGB+(position.y-m_FaceInfo.headArea.top-1)*iImageH+(position.x-m_FaceInfo.headArea.left-1);tmp->r = 255;tmp->g = 66;tmp->b = 51;
}int main(char argc, char *argv[])  {
//#if 1      //test one      LI_RGB pRGB[WIDTH][HEIGHT];// 定义位图数据      memset(pRGB, 0, sizeof(pRGB) );// 设置背景为黑色    int itemp = sizeof(pRGB);// 在中间画一个10*10的矩形      int i=0, j=0;for(i = 0; i < WIDTH; i++)      {          for( j = 0;j < 1; j++)          {              pRGB[i][j].b = 0xff;pRGB[i][j].g = 0x00;pRGB[i][j].r = 0x00;}}GenBmpFile((U8*)pRGB, 24, WIDTH, HEIGHT, "out.bmp");
//生成BMP文件
//#endif
//#if 1      //test two
//    U8 bitCountPerPix;
//      U32 width, height;
//      U8 *pdata = GetBmpData(&bitCountPerPix, &width, &height, "in.bmp");
//      if(pdata)
//    {
//        GenBmpFile(pdata, bitCountPerPix, width, height, "out1.bmp");
//          FreeBmpData(pdata);
//      }
//#endif             return 0;
}

转载于:https://my.oschina.net/guanyun/blog/69004

C 不使用其他库生成BMP图片相关推荐

  1. php 生成bmp图片,[GD]生成bmp格式的图片(imagebmp)

    GD库里没有生成bmp图片的函数,所以自己写了一个,这个函数尚有一个压缩算法没有写,不过已经够用了.需要的同学可以看看. int imagebmp ( resource image [, string ...

  2. PHP生产一个验证码图片,PHP使用GD库生成验证码图片,实现图片验证

    本文记录从php 下载配置GD图片生成库 到使用该库生成验证码图片,网页上实现验证码. 使用技术: php使用GD库绘图 [php版本7.3] php session 缓存 实现最终效果: 一. 配置 ...

  3. cairo和pixman库给bmp图片加文字水印

    两个步骤 1,用cairo库来生成一个图片,然后在上面把文字写到图片上 主要用到 cairo_image_curface_surface_create,生成一个空白的图(为了把文字向上写) cairo ...

  4. C语言实现生成BMP图片文件(BMP文件格式,二进制文件读写)

    Git地址: https://gitee.com/whik/bmp_gen_c_and_verilog/tree/master/c BMP文件格式详解参考: Verilog实现生成BMP文件(BMP文 ...

  5. go语言web开发系列之二十二:用signintech/gopdf库生成带有图片和表格的pdf

    一,安装需要用到的库: 1,gopdf库的地址: https://github.com/signintech/gopdf 2,gopdf库安装的命令: liuhongdi@ku:~$ go get - ...

  6. PHP:GD库 生成验证码图片

    文章来源:http://www.cnblogs.com/hello-tl/p/7592998.html <?php /*** __construct($new):构造函数创建一张图片$new-& ...

  7. 任意大小迷宫自动生成+BFS寻路+生成无损迷宫bitmap(.BMP)图片

    目录标题 迷宫游戏 迷宫的生成 BFS寻找路线 最后的一些细枝末节 如何使用程序 链接 I TURN COFFEE INTO CODE! 800x800迷宫自动解路径 镇楼图 https://pan. ...

  8. linux bmp添加字符,linux 将数字符号画到BMP图片上

    首先我本先了解一下BMP图片的格式. BMP文件主要分为如下4个部分: 块名称 对应Windows结构体定义                                               ...

  9. Verilog实现生成BMP文件(BMP文件格式,二进制文件读写)

    Git地址: https://gitee.com/whik/bmp_gen_c_and_verilog/tree/master/verilog BMP文件格式详解参考: BMP文件格式详解 C语言实现 ...

  10. 纯色bmp图片生成的效率

    各种编程语言生成纯色bmp图片的效率 之前使用了各种语言生成纯色bmp图片,这里汇总并对比下纯色bmp图片文件生成的效率. 主要指标是完成bmp文件生成的耗时时长. 为了公平客观的对比,通过linux ...

最新文章

  1. UML建模系列文章总结
  2. 19课 Vue第二节
  3. Array和ArrayList区别
  4. 容器生态系统 (续) - 每天5分钟玩转容器技术(3)
  5. Educational Codeforces Round 32
  6. Redis分布式锁的实现原理
  7. poj 3258 River Hopscotch 【二分】
  8. rust废铁最快_Rust初体验,它确实有点快
  9. eazyui ajax传值,jquery easyui ajax data属性传值方式
  10. FD.io VPP基本介绍:理解向量包处理(VPP)
  11. pytorch里的nn.Embedding是什么东西?
  12. centos7.6 安装nginx-1.14.2
  13. ++递归 字符串全排列_一文看懂全排列算法
  14. imsdroid启动Activity的方式很独特
  15. Linux0.11内核--进程调度分析之1.初始化
  16. 微信小程序字符头像,自定义任意字符头像,字符图片,avatar,利用minui
  17. Python数据处理Tips日期、时间数据处理方法汇总
  18. cass生成里程文件桩号不全,cass生成桩号
  19. 百度火星坐标转wgs84
  20. 一万字长文解读中国的消费经济

热门文章

  1. 三维旋转矩阵_线性代数的本质(4)--矩阵乘法与复合变换
  2. uniapp 生成二维码长按保存_工程设备巡检如何用二维码管理?
  3. android 修改 选择壁纸来源,修改android选择壁纸来源列表
  4. php split to array,Split php varibale with JS into array
  5. 秒懂文件路径 ‘/‘ , ‘./‘ , ‘../‘ 的区别
  6. Harmony OS — DatePicker日期选择器
  7. Kotlin — 编程语言
  8. 计算机领域各个技术——汇总篇
  9. 1005 继续(3n+1)猜想 (25 分)—PAT (Basic Level) Practice (中文)
  10. Android ViewPager + PagerAdapter 实现轮播图