时间:2021-05-20
复制代码 代码如下:
#include <stdio.h>
#include <stdlib.h>
#define BITMAPFILEHEADERLENGTH 14 // The bmp FileHeader length is 14
#define BM 19778 // The ASCII code for BM
void bmpFileTest(FILE* fpbmp);
void bmpHeaderPartLength(FILE* fpbmp);
void BmpWidthHeight(FILE* fpbmp);
void bmpFileHeader(FILE* fpbmp);
void bmpInfoHeader(FILE* fpbmp);
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[])
{
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;
}
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);
}
}
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);
}
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);
}
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);
}
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);
}
void bmpDataPart(FILE* fpbmp)
{
int i, j;
unsigned char bmpPixel[BmpWidth][BmpHeight];
unsigned char* bmpPixelTmp = NULL;
FILE* fpDataBmp;
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);
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");
}
}
}
//printf("bmpPixel[2][3] is %d.\n", bmpPixel[2][3]);
//printf("bmpPixel[20][30] is %d.\n", bmpPixel[20][30]);
free(bmpPixelTmp);
fclose(fpDataBmp);
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
C语言实现BMP图像的读写对于刚接触数字图像的同学,应该都有一个疑问,如何把一个BMP格式的图像用纯C语言读入呢,我相信这也是数字图像处理的第一步,如果有幸看到
图像是由一组像素构成,用二进制形式保存的图片。java语言支持GIF、JPEG和BMP这3种主要图像文件格式。java语言的图像处理功能被封装在Image类中。
本文实例讲述了C语言实现BMP转换JPG的方法。分享给大家供大家参考。具体实现方法如下:/***********************************
C语言sscanf()函数:从字符串中读取指定格式的数据头文件:#includesscanf()函数用于从字符串中读取指定格式的数据,其原型如下:intssca
新建bmp文件的方法: 2、或者用制图工具,将文件另存为BMP格式即可。 BMP图像文件格式是Microsoft为其windows环境设置的标准图像格式。一