用c语言创建bmp文件,C语言构建BMP文件(一)

#define false 0

#define true 0

typedef struct tagXSurface{

u32 surface;

int width,height;

int pitchWidth,pitch;

u32 *pData;

}XSurface;

XSurface pDemoSurface;

typedef struct tag_bmp_data

{

int width;

int height;

}_bmp_data;

_bmp_data p_my_bmp_data;

FILE *pWritingFile = NULL;

int WriteBmp(XSurface *pInputSurface,char *BmpFileName)

{

int i=0,k=0;

long j=0;

int get_size;

int write_cnt;

char *pTempBuffer=NULL;

int w=pInputSurface->width;

int h=pInputSurface->height;

char *pSurfaceData=(char *)(pInputSurface->pData);

int EVERY_READ_SIZE=pInputSurface->pitchWidth*3;

if((w!=p_my_bmp_data.width)||(h!=p_my_bmp_data.height))

{

JPRINTF(("Write BMP Failed, w( %d != %d ), h( %d != %d ) \n",w,p_my_bmp_data.width,h,p_my_bmp_data.height));

return -1;

}

JPRINTF(("Offset:: %d \n",ftell(pWritingFile)));

pTempBuffer=(char *)XMemAlloc(sizeof(char)*EVERY_READ_SIZE);

if(NULL==pTempBuffer)

{

JPRINTF(("Malloc Memory Failed ... \n"));

return -2;

}

get_size=EVERY_READ_SIZE;

for(k=pInputSurface->height;k>=0;--k)

{

for(i=0,j=0+k*(pInputSurface->pitchWidth)*4;i

{

pTempBuffer[i]=pSurfaceData[j];

pTempBuffer[i+1]=pSurfaceData[j+1];

pTempBuffer[i+2]=pSurfaceData[j+2];

}

write_cnt=fwrite(pTempBuffer, sizeof(char), get_size, pWritingFile);

if(write_cnt!=get_size)

JPRINTF(("### Write Error !###\n"));

}

fclose(pWritingFile);

}

int CreateBmp(long w, long h, char *BmpFileName)

{

#define BMP_Header_Length 54

long ret;

unsigned char header[BMP_Header_Length] = {

0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,

54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0

};

long file_size = (long)w * (long)h * 3 + 54;

header[2] = (unsigned char)(file_size &0x000000ff);

header[3] = (file_size >> 8) & 0x000000ff;

header[4] = (file_size >> 16) & 0x000000ff;

header[5] = (file_size >> 24) & 0x000000ff;

long width = w;

header[18] = width & 0x000000ff;

header[19] = (width >> 8) &0x000000ff;

header[20] = (width >> 16) &0x000000ff;

header[21] = (width >> 24) &0x000000ff;

long height = h;

header[22] = height &0x000000ff;

header[23] = (height >> 8) &0x000000ff;

header[24] = (height >> 16) &0x000000ff;

header[25] = (height >> 24) &0x000000ff;

//FILE *pWritingFile = NULL;

pWritingFile = fopen(BmpFileName, "wb");

if( pWritingFile == NULL )

return false;

fwrite(header, sizeof(unsigned char), 54, pWritingFile);

p_my_bmp_data.width=width;

p_my_bmp_data.height=height;

return true;

}

void creat_snap(char *BmpFileName)

{

CreateBmp(pDemoSurface->width,pDemoSurface->height,BmpFileName);

WriteBmp(pDemoSurface,BmpFileName);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C语言中获取BMP文件,可以使用以下步骤: 1. 打开BMP文件并读取文件头信息。文件头信息应该包括BMP文件的大小、宽度、高度和像素位数等信息。 2. 读取BMP文件的像素数据。BMP文件中的像素数据存储在文件头信息之后。要读取像素数据,需要使用fread函数从文件中读取数据。 3. 将读取的像素数据存储在内存中。可以使用动态分配内存的方法将像素数据存储在内存中。 4. 使用读取的像素数据进行图像处理、分析或显示等操作。 以下是一个简单的C语言程序,可以用于获取BMP文件并显示图像: ```c #include <stdio.h> #include <stdlib.h> #pragma pack(2) typedef struct BMPHeader { uint16_t type; // 文件类型,必须是0x4d42 uint32_t size; // 文件大小,包括头文件和像素数据 uint16_t reserved1; // 保留,必须是0 uint16_t reserved2; // 保留,必须是0 uint32_t offset; // 像素数据的偏移量 uint32_t dib_header_size; // DIB头文件大小 int32_t width; // 图像的宽度 int32_t height; // 图像的高度 uint16_t planes; // 平面数,必须是1 uint16_t bits_per_pixel; // 像素位数,可以是1、4、8、16、24、32 uint32_t compression; // 压缩类型 uint32_t image_size; // 图像大小,包括未使用的像素数据 int32_t x_pixels_per_meter; // 水平分辨率 int32_t y_pixels_per_meter; // 垂直分辨率 uint32_t colors_used; // 使用的颜色数 uint32_t colors_important; // 重要的颜色数 } BMPHeader; void show_image(uint8_t* data, int32_t width, int32_t height) { // 在这里可以使用自己的图像处理函数来处理图像 // 这里简单地打印出图像的像素值 for (int32_t y = 0; y < height; y++) { for (int32_t x = 0; x < width; x++) { printf("%d ", data[y * width + x]); } printf("\n"); } } int main(int argc, char** argv) { if (argc < 2) { printf("Usage: %s <bmp_file>\n", argv[0]); return 0; } FILE* f = fopen(argv[1], "rb"); if (!f) { printf("Failed to open file %s\n", argv[1]); return 0; } BMPHeader header; fread(&header, sizeof(header), 1, f); if (header.type != 0x4d42) { printf("File %s is not a BMP file\n", argv[1]); fclose(f); return 0; } if (header.bits_per_pixel != 8) { printf("File %s is not a grayscale BMP file\n", argv[1]); fclose(f); return 0; } uint8_t* data = (uint8_t*)malloc(header.width * header.height); fseek(f, header.offset, SEEK_SET); fread(data, 1, header.width * header.height, f); fclose(f); show_image(data, header.width, header.height); free(data); return 0; } ``` 这个程序可以读取一个灰度图像BMP文件,并将像素值打印到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值