第1关:1位黑白BMP图像的读
#include "BMP.h"
BMP_Image* BMP_LoadImage(char* path)
{
BMP_BitMapFileHeader bmpFileHeader;
BMP_BitMapInfoHeader bmpInfoHeader;
BMP_RgbQuad* quad;
BMP_Image* bmpImg;
FILE* pFile;
int width = 0;
int height = 0;
int offset;
unsigned char pixVal;
int i, j, k;
bmpImg = (BMP_Image*)malloc(sizeof(BMP_Image));
pFile = fopen(path, "rb");
if (!pFile)
{
free(bmpImg);
return NULL;
}
fread(&bmpFileHeader, sizeof(BMP_BitMapFileHeader), 1, pFile);
fread(&bmpInfoHeader, sizeof(BMP_BitMapInfoHeader), 1, pFile);
if (bmpInfoHeader.biBitCount == 1)
{
width = bmpInfoHeader.biWidth;
height = bmpInfoHeader.biHeight;
bmpImg->width = width;
bmpImg->height = height;
bmpImg->biBitCount = 1;
bmpImg->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*2);//±äÁ¿·ÖÅäÄÚ´æ
bmpImg->imageData = (unsigned char*)malloc(sizeof(unsigned char)*width*height/8);
quad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*2);//¶Á³öbmpÔͼµ÷É«°åÖµ
fread(quad, sizeof(BMP_RgbQuad), 2, pFile);
for(i=0;i<2;i++)//½«Ôͼµ÷É«°åÖµ¸³Öµ¸øеıäÁ¿
{
bmpImg->imageRgbQuad[i].rgbBlue = quad[i].rgbBlue;
bmpImg->imageRgbQuad[i].rgbGreen = quad[i].rgbGreen;
bmpImg->imageRgbQuad[i].rgbRed = quad[i].rgbRed;
bmpImg->imageRgbQuad[i].rgbReserved = quad[i].rgbReserved;
}
for (i=0; i<height; i++)//°´Ðò¶Á³öbmpÿ¸ö×Ö½Ú°´´ÓÏÂÍùÉϵÄ˳Ðò¶Á³ö´æ´¢
{
for (j=0; j<width/8; j++)
{
fread(&pixVal, sizeof(unsigned char), 1, pFile);
/********* Begin *********/
bmpImg->imageData[(height-1-i)*width/8+j] = pixVal ;
/********* End *********/
}
}
}
return bmpImg;
}
第2关:8位灰度BMP图像的读
#include "BMP.h"
BMP_Image* BMP_LoadImage(char* path)
{
BMP_BitMapFileHeader bmpFileHeader;
BMP_BitMapInfoHeader bmpInfoHeader;
BMP_RgbQuad* quad;
BMP_Image* bmpImg;
FILE* pFile;
int width = 0;
int height = 0;
unsigned char pixVal;
int i, j, k;
bmpImg = (BMP_Image*)malloc(sizeof(BMP_Image));
pFile = fopen(path, "rb");
if (!pFile)
{
free(bmpImg);
return NULL;
}
fread(&bmpFileHeader, sizeof(BMP_BitMapFileHeader), 1, pFile);
fread(&bmpInfoHeader, sizeof(BMP_BitMapInfoHeader), 1, pFile);
if (bmpInfoHeader.biBitCount == 8)
{
width = bmpInfoHeader.biWidth;
height = bmpInfoHeader.biHeight;
bmpImg->width = width;
bmpImg->height = height;
bmpImg->biBitCount = 8;
bmpImg->imageRgbQuad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
bmpImg->imageData = (unsigned char*)malloc(sizeof(unsigned char)*width*height);
quad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
fread(quad, sizeof(BMP_RgbQuad), 256, pFile);
for(i=0;i<256;i++)
{
bmpImg->imageRgbQuad[i].rgbBlue = quad[i].rgbBlue;
bmpImg->imageRgbQuad[i].rgbGreen = quad[i].rgbGreen;
bmpImg->imageRgbQuad[i].rgbRed = quad[i].rgbRed;
bmpImg->imageRgbQuad[i].rgbReserved = quad[i].rgbReserved;
}
for (i=0; i<height; i++)//°´Ðò¶Á³öbmpÿ¸ö×Ö½Ú°´´ÓÏÂÍùÉϵÄ˳Ðò¶Á³ö´æ´¢
{
for (j=0; j<width; j++)
{
fread(&pixVal, sizeof(unsigned char), 1, pFile);
/********* Begin *********/
bmpImg->imageData[(height-1-i)*width+j] = pixVal;
/********* End *********/
}
}
}
return bmpImg;
}
第3关:24位彩色BMP图像的读
#include "BMP.h"
BMP_Image* BMP_LoadImage(char* path)
{
BMP_BitMapFileHeader bmpFileHeader;
BMP_BitMapInfoHeader bmpInfoHeader;
BMP_RgbQuad* quad;
BMP_Image* bmpImg;
FILE* pFile;
int width = 0;
int height = 0;
int offset;
unsigned char pixVal;
int i, j, k;
bmpImg = (BMP_Image*)malloc(sizeof(BMP_Image));
pFile = fopen(path, "rb");
if (!pFile)
{
free(bmpImg);
return NULL;
}
fread(&bmpFileHeader, sizeof(BMP_BitMapFileHeader), 1, pFile);
fread(&bmpInfoHeader, sizeof(BMP_BitMapInfoHeader), 1, pFile);
if (bmpInfoHeader.biBitCount == 24)
{
width = bmpInfoHeader.biWidth;
height = bmpInfoHeader.biHeight;
bmpImg->width = width;
bmpImg->height = height;
bmpImg->biBitCount = 24;
bmpImg->imageData = (unsigned char*)malloc(sizeof(unsigned char)*width*3*height);
offset = (3*width)%4;
if (offset != 0)
{
offset = 4 - offset;
}
for (i=0; i<height; i++)//°´Ðò¶Á³öbmpÿ¸ö×Ö½Ú°´´ÓÏÂÍùÉϵÄ˳Ðò¶Á³ö´æ´¢
{
for (j=0; j<width; j++)
{
for (k=0; k<3; k++)
{
fread(&pixVal, sizeof(unsigned char), 1, pFile);
/********* Begin *********/
bmpImg->imageData[(height-1-i)*width*3+j*3+k] = pixVal;
/********* End *********/
}
}
if (offset != 0)
{
for (j=0; j<offset; j++)
{
fread(&pixVal, sizeof(unsigned char), 1, pFile);
}
}
}
}
return bmpImg;
}
大家需要平台上的实验答案,可以留言,博主尽量帮助你们分享!!!!