资源简介
C语言读取BMP图片并存入工程文件夹中,适用于初学者理解BMP文件的结构和格式。开发环境:VS2010
代码片段和文件信息
#include
#include
#include “BITMAPINFO.h“
using namespace std;
#define LENGTH_NAME_BMP 30 //bmp file name
BITMAPFILEHEADER strHead;
BITMAPINFOHEADER strInfo;
RGBQUAD strPalette[256];
void showBmpHead(BITMAPFILEHEADER pBmpHead)
{
cout << “the bitmap header file: “ << endl;
cout << “the size of bitmap: “ << pBmpHead.bfSize << endl;
cout<< “Reserved 1: “ << pBmpHead.bfReserved1 << endl;
cout<< “Reserved 2: “ << pBmpHead.bfReserved2 << endl;
cout<< “shifted number of bytes in image data: “ << pBmpHead.bfoffBits << endl << endl;
}
void showBmpInfoHead(tagBITMAPINFOHEADER pBmpInfoHead)
{
cout << “the bitmap info header: “ << endl;
cout << “length of struct: “ << pBmpInfoHead.biSize<< endl;
cout << “width of bitmap: “ << pBmpInfoHead.biWidth << endl;
cout << “height of bitmap: “ << pBmpInfoHead.biHeight << endl;
cout << “number of bit planes: “ << pBmpInfoHead.biPlanes << endl;
cout << “biBitCount: “ << pBmpInfoHead.biBitCount << endl;
cout << “compression method: “ << pBmpInfoHead.biCompression< cout << “biSizeImage: “ << pBmpInfoHead.biSizeImage << endl;
cout << “X resolution: “ << pBmpInfoHead.biXPelsPerMeter << endl;
cout << “Y resolution: “ << pBmpInfoHead.biYPelsPerMeter << endl;
cout << “the number of used colors: “ << pBmpInfoHead.biClrUsed << endl;
cout << “important colors: “ << pBmpInfoHead.biClrImportant << endl;
}
int main()
{
char strFile[LENGTH_NAME_BMP] = “lena.bmp“;
IMAGEDATA *imagedata = NULL;
IMAGEDATA *imagedataOut = NULL;
int width;
int height;
FILE *fp *fpo;
fp = fopen(strFile “rb“);
if( fp != NULL )
{
WORD bfType; //the type of the file
fread(&bfType sizeof(WORD) 1 fp);
if( 0x4d42 != bfType )
{
cout << “The file is not bmp!“ << endl;
return 0;
}
fread(&strHead sizeof(tagBITMAPFILEHEADER) 1 fp);
showBmpHead(strHead);
fread(&strInfo sizeof(tagBITMAPINFOHEADER) 1 fp);
showBmpInfoHead(strInfo);
//读取调色板
for(int i = 0; i < strInfo.biClrUsed; i++)
{
fread((char *)&(strPalette[i].rgbBlue) sizeof(BYTE) 1 fp);
fread((char *)&(strPalette[i].rgbGreen) sizeof(BYTE) 1 fp);
fread((char *)&(strPalette[i].rgbRed) sizeof(BYTE) 1 fp);
fread((char *)&(strPalette[i].rgbReserved) sizeof(BYTE) 1 fp);
}
width = strInfo.biWidth;
height = strInfo.biHeight;
//width = (width * sizeof(IMAGEDATA) + 3) /4 * 4;
width = ((width % 4 == 0 ? width : width + (4 - (width % 4))) * strInfo.biBitCount) / 8;
imagedata = (IMAGEDATA *)malloc( width * height );
imagedataOut = (IMAGEDATA *)malloc( width * height * sizeof(imagedata));
for(int j = 0; j < height; j++)
{
for(int i = 0 ; i < width; i++)
{
(*(imagedata + j * width + i)).blue = 0;
(*(imagedataOut + j * width + i)).blue = 0;
}
}
//读出图片的像素数据
fread(imagedata sizeof(struct tagIMAGEDATA) * width height fp);
fclose(fp);
}
el
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3996 2015-02-03 11:13 1_readBmp_1\1_readBmp_1.vcxproj
文件 1079 2015-02-03 11:13 1_readBmp_1\1_readBmp_1.vcxproj.filters
文件 143 2015-02-03 10:46 1_readBmp_1\1_readBmp_1.vcxproj.user
文件 1182 2015-02-03 14:53 1_readBmp_1\BITMAPINFO.h
文件 66614 2014-10-25 14:51 1_readBmp_1\lena.bmp
文件 66614 2015-02-03 14:53 1_readBmp_1\out.bmp
文件 4174 2015-02-03 14:34 1_readBmp_1\test.cpp
文件 900 2015-02-03 10:46 1_readBmp_1.sln
目录 0 2015-02-03 15:07 1_readBmp_1
----------- --------- ---------- ----- ----
144702 9
评论
共有 条评论