21xrx.com
2024-09-20 00:24:15 Friday
登录
文章检索 我的文章 写文章
使用C语言读取BMP文件——实现图像显示
2023-06-18 22:03:22 深夜i     --     --
C语言 BMP文件 图像显示 文件头 像素数据 Visual

BMP(Bitmap)是位图图像格式,常用于装载Windows系统中的位图文件。在C语言中,通过读取BMP文件头信息和像素数据,可以实现对BMP图像的读取和显示。下面将基于Visual Studio 2019,使用C语言实现读取BMP文件并显示图像的功能。

1. 读取BMP文件

BMP文件包括文件头和像素数据两部分。首先需要读取文件头信息,包括文件类型、文件大小、图像尺寸等信息。具体的实现方法如下:


typedef struct

  unsigned short type;  // 文件类型 BMPHEADER;

// 读取BMP文件头信息

void ReadBmpHeader(FILE* file, BMPHEADER* header)

{

  fread(&header->type, 1, 2, file);

  fread(&header->size, 1, 4, file);

  fread(&header->reserved1, 1, 2, file);

  fread(&header->reserved2, 1, 2, file);

  fread(&header->offset, 1, 4, file);

  fread(&header->header_size, 1, 4, file);

  fread(&header->width, 1, 4, file);

  fread(&header->height, 1, 4, file);

  fread(&header->planes, 1, 2, file);

  fread(&header->bits_per_pixel, 1, 2, file);

  fread(&header->compression, 1, 4, file);

  fread(&header->image_size, 1, 4, file);

  fread(&header->x_resolution, 1, 4, file);

  fread(&header->y_resolution, 1, 4, file);

  fread(&header->color_used, 1, 4, file);

  fread(&header->color_important, 1, 4, file);

}

2. 显示BMP图像

读取BMP文件头信息后,就可以读取位图中的像素数据并显示图片了。具体的实现方法如下:


// 显示BMP图像

void ShowBmpImage(unsigned char* data, int width, int height)

{

  int bits_per_pixel = 24;  // 24位图像

  int row_size = ((bits_per_pixel * width + 31) / 32) * 4;  // 每行像素所占字节数

  int padding = row_size - bits_per_pixel * width / 8;  // 每行的填充字节数

  int x, y;

  char color[3];

  // 执行位图数据

  for (y = 0; y < height; y++)

  {

    for (x = 0; x < width; x++)

    {

      color[2] = *data++;

      color[1] = *data++;

      color[0] = *data++;

      printf("(%d, %d): #%02X%02X%02X\n", x, y, color[2], color[1], color[0]);

    }

    // 每行末尾的填充字节忽略不计

    data += padding;

  }

}

3. 实现完整的BMP图像显示程序

根据上述代码,可以实现一个完整的BMP图像显示程序。程序的实现流程如下:

1)读取BMP文件头信息。

2)从位图数据中读取像素数据。

3)将像素数据转化为图像并显示出来。

以下为完整的程序代码:


#include

#include

// BMP文件头信息

typedef struct

必须为 0x4D42

  unsigned int size;   // 文件大小

  unsigned short reserved1;  // 保留字段1

  unsigned short reserved2;  // 保留字段2

  unsigned int offset;  // 位图数据的起始位置

  unsigned int header_size;  // 文件头大小

  unsigned int width;   // 图像宽度

  unsigned int height;  // 图像高度

  unsigned short planes; // 图像的位面数

  unsigned short bits_per_pixel; // 每个像素所占位数

  unsigned int compression;  // 压缩类型

  unsigned int image_size;  // 图像大小

  unsigned int x_resolution; // 水平分辨率

  unsigned int y_resolution; // 垂直分辨率

  unsigned int color_used;  // 使用的颜色数

  unsigned int color_important;  // 重要的颜色数

BMPHEADER;

// 读取BMP文件头信息

void ReadBmpHeader(FILE* file, BMPHEADER* header)

{

  fread(&header->type, 1, 2, file);

  fread(&header->size, 1, 4, file);

  fread(&header->reserved1, 1, 2, file);

  fread(&header->reserved2, 1, 2, file);

  fread(&header->offset, 1, 4, file);

  fread(&header->header_size, 1, 4, file);

  fread(&header->width, 1, 4, file);

  fread(&header->height, 1, 4, file);

  fread(&header->planes, 1, 2, file);

  fread(&header->bits_per_pixel, 1, 2, file);

  fread(&header->compression, 1, 4, file);

  fread(&header->image_size, 1, 4, file);

  fread(&header->x_resolution, 1, 4, file);

  fread(&header->y_resolution, 1, 4, file);

  fread(&header->color_used, 1, 4, file);

  fread(&header->color_important, 1, 4, file);

}

// 显示BMP图像

void ShowBmpImage(unsigned char* data, int width, int height)

{

  int bits_per_pixel = 24;  // 24位图像

  int row_size = ((bits_per_pixel * width + 31) / 32) * 4;  // 每行像素所占字节数

  int padding = row_size - bits_per_pixel * width / 8;  // 每行的填充字节数

  int x, y;

  char color[3];

  // 执行位图数据

  for (y = height - 1; y >= 0; y--)

  {

    for (x = 0; x < width; x++)

    {

      color[2] = *data++;

      color[1] = *data++;

      color[0] = *data++;

      printf("(%d, %d): #%02X%02X%02X\n", x, y, color[2], color[1], color[0]);

    }

    // 每行末尾的填充字节忽略不计

    data += padding;

  }

}

int main(int argc, char* argv[])

{

  BMPHEADER header;

  FILE* file;

  unsigned char* data;

  // 打开BMP文件

  file = fopen("test.bmp", "rb");

  if (!file)

  {

    printf("Failed to open file.\n");

    return -1;

  }

  // 读取BMP文件头

  ReadBmpHeader(file, &header);

  // 读取BMP图像数据

  data = (unsigned char*)malloc(header.image_size);

  if (!data)

  {

    printf("Failed to allocate memory.\n");

    return -2;

  }

  fseek(file, header.offset, SEEK_SET);

  fread(data, 1, header.image_size, file);

  // 显示BMP图像

  ShowBmpImage(data, header.width, header.height);

  // 关闭文件

  fclose(file);

  return 0;

}

4. 关键词

C语言、BMP文件、图像显示、文件头、像素数据、Visual Studio 2019、读取、显示、文件大小、图像尺寸、位图数据、像素、24位图像、每行像素、填充字节数、malloc、fseek、fread、fclose。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复