C语言读取BMP图像数据的源码
/* File name: bmpTest.c
Author: WanChuan XianSheng
Date: Oct 01, 2011
Description: Show all Info a bmp file has. including
FileHeader Info, InfoHeader Info and Data Part.
Reference: BMP图像数据的C语言读取源码
*/
#include <stdio.h>
#include <stdlib.h>
#define BITMAPFILEHEADERLENGTH 14 // The bmp FileHeader length is 14
#define BM 19778 // The ASCII code for BM
/* Test the file is bmp file or not */
void bmpFileTest(FILE* fpbmp);
/* To get the OffSet of header to data part */
void bmpHeaderPartLength(FILE* fpbmp);
/* To get the width and height of the bmp file */
void BmpWidthHeight(FILE* fpbmp);
/* Show bmp file tagBITMAPFILEHEADER info */
void bmpFileHeader(FILE* fpbmp);
/* Show bmp file tagBITMAPINFOHEADER info */
void bmpInfoHeader(FILE* fpbmp);
/* Show the Data Part of bmp file */
void bmpDataPart(FILE* fpbmp);
unsigned int OffSet = 0; // OffSet from Header part to Data Part
long BmpWidth = 0; // The Width of the Data Part
long BmpHeight = 0; // The Height of the Data Part
int main(int argc, char* argv[])
{
/* Open bmp file */
FILE *fpbmp = fopen("lena.bmp", "r+");
if (fpbmp == NULL)
{
fprintf(stderr, "Open lena.bmp failed!!!\n");
return 1;
}
bmpFileTest(fpbmp); //Test the file is bmp file or not
bmpHeaderPartLength(fpbmp); //Get the length of Header Part
BmpWidthHeight(fpbmp); //Get the width and width of the Data Part
//bmpFileHeader(fpbmp); //Show the FileHeader Information
//bmpInfoHeader(fpbmp); //Show the InfoHeader Information
bmpDataPart(fpbmp); //Reserve the data to file
fclose(fpbmp);
return 0;
}
/* Test the file is bmp file or not */
void bmpFileTest(FILE* fpbmp)
{
unsigned short bfType = 0;
fseek(fpbmp, 0L, SEEK_SET);
fread(&bfType, sizeof(char), 2, fpbmp);
if (BM != bfType)
{
fprintf(stderr, "This file is not bmp file.!!!\n");
exit(1);
}
}
/* To get the OffSet of header to data part */
void bmpHeaderPartLength(FILE* fpbmp)
{
fseek(fpbmp, 10L, SEEK_SET);
fread(&OffSet, sizeof(char), 4, fpbmp);
//printf("The Header Part is of length %d.\n", OffSet);
}
/* To get the width and height of the bmp file */
void BmpWidthHeight(FILE* fpbmp)
{
fseek(fpbmp, 18L, SEEK_SET);
fread(&BmpWidth, sizeof(char), 4, fpbmp);
fread(&BmpHeight, sizeof(char), 4, fpbmp);
//printf("The Width of the bmp file is %ld.\n", BmpWidth);
//printf("The Height of the bmp file is %ld.\n", BmpHeight);
}
/* Show bmp file tagBITMAPFILEHEADER info */
void bmpFileHeader(FILE* fpbmp)
{
unsigned short bfType; //UNIT bfType;
unsigned int bfSize; //DWORD bfSize;
unsigned short bfReserved1; //UINT bfReserved1;
unsigned short bfReserved2; //UINT bfReserved2;
unsigned int bfOffBits; //DWORD bfOffBits;
fseek(fpbmp, 0L, SEEK_SET);
fread(&bfType, sizeof(char), 2, fpbmp);
fread(&bfSize, sizeof(char), 4, fpbmp);
fread(&bfReserved1, sizeof(char), 2, fpbmp);
fread(&bfReserved2, sizeof(char), 2, fpbmp);
fread(&bfOffBits, sizeof(char), 4, fpbmp);
printf("************************************************\n");
printf("*************tagBITMAPFILEHEADER info***********\n");
printf("************************************************\n");
printf("bfType is %d.\n", bfType);
printf("bfSize is %d.\n", bfSize);
printf("bfReserved1 is %d.\n", bfReserved1);
printf("bfReserved2 is %d.\n", bfReserved2);
printf("bfOffBits is %d.\n", bfOffBits);
}
/* Show bmp file tagBITMAPINFOHEADER info */
void bmpInfoHeader(FILE* fpbmp)
{
unsigned int biSize; // DWORD biSize;
long biWidth; // LONG biWidth;
long biHeight; // LONG biHeight;
unsigned int biPlanes; // WORD biPlanes;
unsigned int biBitCount; // WORD biBitCount;
unsigned int biCompression; // DWORD biCompression;
unsigned int biSizeImage; // DWORD biSizeImage;
long biXPelsPerMerer; // LONG biXPelsPerMerer;
long biYPelsPerMerer; // LONG biYPelsPerMerer;
unsigned int biClrUsed; // DWORD biClrUsed;
unsigned int biClrImportant; // DWORD biClrImportant;
fseek(fpbmp, 14L, SEEK_SET);
fread(&biSize, sizeof(char), 4, fpbmp);
fread(&biWidth, sizeof(char), 4, fpbmp);
fread(&biHeight, sizeof(char), 4, fpbmp);
fread(&biPlanes, sizeof(char), 4, fpbmp);
fread(&biBitCount, sizeof(char), 4, fpbmp);
fread(&biCompression, sizeof(char), 4, fpbmp);
fread(&biSizeImage, sizeof(char), 4, fpbmp);
fread(&biXPelsPerMerer, sizeof(char), 4, fpbmp);
fread(&biYPelsPerMerer, sizeof(char), 4, fpbmp);
fread(&biClrUsed, sizeof(char), 4, fpbmp);
fread(&biClrImportant, sizeof(char), 4, fpbmp);
printf("************************************************\n");
printf("*************tagBITMAPINFOHEADER info***********\n");
printf("************************************************\n");
printf("biSize is %d. \n", biSize);
printf("biWidth is %ld.\n", biWidth);
printf("biHeight is %ld.\n", biHeight);
printf("biPlanes is %d. \n", biPlanes);
printf("biBitCount is %d. \n", biBitCount);
printf("biCompression is %d. \n", biCompression);
printf("biSizeImage is %d. \n", biSizeImage);
printf("biXPelsPerMerer is %ld.\n", biXPelsPerMerer);
printf("biYPelsPerMerer is %ld.\n", biYPelsPerMerer);
printf("biClrUsed is %d. \n", biClrUsed);
printf("biClrImportant is %d. \n", biClrImportant);
}
/* Show the Data Part of bmp file */
void bmpDataPart(FILE* fpbmp)
{
int i, j;
unsigned char bmpPixel[BmpWidth][BmpHeight];
unsigned char* bmpPixelTmp = NULL;
FILE* fpDataBmp;
/* New a file to save the data matrix */
if((fpDataBmp=fopen("bmpData.dat","w+")) == NULL)
{
fprintf(stderr, "Failed to construct file bmpData.dat.!!!");
exit(1);
}
fseek(fpbmp, OffSet, SEEK_SET);
if ((bmpPixelTmp=(unsigned char*)malloc(sizeof(char)*BmpWidth*BmpHeight))==NULL)
{
fprintf(stderr, "Data allocation failed.!!!\n");
exit(1);
}
fread(bmpPixelTmp, sizeof(char), BmpWidth*BmpHeight, fpbmp);
/* Read the data to Matrix and save it in file bmpData.dat */
for(i =0; i < BmpHeight; i++)
{
fprintf(fpDataBmp, "The data in line %-3d:\n", i+1);
for(j = 0; j < BmpWidth; j++)
{
bmpPixel[i][j] = bmpPixelTmp[BmpWidth*(BmpHeight-1-i)+j];
//fwrite(&chartmp, sizeof(char), 1, fpDataBmp);
fprintf(fpDataBmp, "%-3d ", bmpPixel[i][j]);
if ((j+1)%32 == 0)
{
fprintf(fpDataBmp, "\n");
}
}
}
/* Used to test the data read is true or false
You can open the file using Matlab to compare the data */
//printf("bmpPixel[2][3] is %d.\n", bmpPixel[2][3]);
//printf("bmpPixel[20][30] is %d.\n", bmpPixel[20][30]);
free(bmpPixelTmp);
fclose(fpDataBmp);
}
相关文章
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 最基础的对数据的增加删除修改操作实例,菜鸟们收了吧...2013-09-26
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 这篇文章主要介绍了解决Mybatis 大数据量的批量insert问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-09
Antd-vue Table组件添加Click事件,实现点击某行数据教程
这篇文章主要介绍了Antd-vue Table组件添加Click事件,实现点击某行数据教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-17- 这篇文章主要介绍了详解如何清理redis集群的所有数据,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-18
- 这篇文章主要介绍了vue 获取到数据但却渲染不到页面上的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-19
- 在php中解析xml文档用专门的函数domdocument来处理,把json在php中也有相关的处理函数,我们要把数据xml 数据存到一个数据再用json_encode直接换成json数据就OK了。...2016-11-25
- 这篇文章主要介绍了mybatis-plus 处理大数据插入太慢的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-18
- 这篇文章主要介绍了postgresql数据添加两个字段联合唯一的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-04
Vue生命周期activated之返回上一页不重新请求数据操作
这篇文章主要介绍了Vue生命周期activated之返回上一页不重新请求数据操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-26- 这篇文章主要介绍了c# socket网络编程,server端接收,client端发送数据,大家参考使用吧...2020-06-25
- 这篇文章主要介绍了解决vue watch数据的方法被调用了两次的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-11-07
- 这篇文章主要介绍了vue 数据(data)赋值问题的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-29
- 这篇文章主要介绍了c# 如何对CSV文件操作,帮助大家更好的理解和学习C#,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了Python3 常用数据标准化方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-24
- 本文主要介绍了python读取和保存mat文件的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-25
- 首先如果要在程序中使用sdcard进行存储,我们必须要在AndroidManifset.xml文件进行下面的权限设置: 在AndroidManifest.xml中加入访问SDCard的权限如下: <!--...2016-09-20
- 这篇文章主要为大家详细介绍了node.js从数据库获取数据的具体代码,nodejs可以获取具体某张数据表信息,感兴趣的朋友可以参考一下...2016-05-09
- 使用下以两种方法时必须把字段设为”主键(PRIMARY KEY”或”唯一约束(UNIQUE)”。1:使用REPLACE INTO (此种方法是利用替换的方法,有点似类于先删除再插入) 复制代码 代码如下:REPLACE INTO Syntax REPLACE [LOW_PRIO...2013-10-04