第1关:1位黑白BMP图像的写
#include "BMP.h"
bool BMP_SaveImage(char* path, BMP_Image* bmpImg)
{
FILE *pFile;
// unsigned short fileType;
BMP_BitMapFileHeader bmpFileHeader;
BMP_BitMapInfoHeader bmpInfoHeader;
unsigned char pixVal; //= '\0';
int i, j;
BMP_RgbQuad* quad;
pFile = fopen(path, "wb");
if (!pFile)
{
return false;
}
//****************************************************************
//bmpºÚ°×ͼд
if (bmpImg->biBitCount == 1)//1룬µ¥Í¨µÀ£¬ºÚ°×ͼ
{
//bmpͼÏñµÄÍ·Îļþ¸÷±äÁ¿¸³Öµ
bmpFileHeader.bfType = 0x4D42;
bmpFileHeader.bfSize = 54 + 2*4 + bmpImg->height*bmpImg->width/8;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = 54 + 2*4;
fwrite(&bmpFileHeader, sizeof(BMP_BitMapFileHeader), 1, pFile);
//bmpͼÏñµÄλͼÐÅÏ¢¸÷±äÁ¿¸³Öµ
bmpInfoHeader.biSize = 40;
bmpInfoHeader.biWidth = bmpImg->width;
bmpInfoHeader.biHeight = bmpImg->height;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biBitCount = 1;
bmpInfoHeader.biCompression = 0;
bmpInfoHeader.biSizeImage = bmpImg->height*bmpImg->width/8;
bmpInfoHeader.biXPelsPerMeter = 0;
bmpInfoHeader.biYPelsPerMeter = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biClrImportant = 0;
fwrite(&bmpInfoHeader, sizeof(BMP_BitMapInfoHeader), 1, pFile);
//bmpͼÏñµÄµ÷É«°å¸³Öµ
quad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*2);
for (i=0; i<2; i++)
{
quad[i].rgbBlue = bmpImg->imageRgbQuad[i].rgbBlue;
quad[i].rgbGreen = bmpImg->imageRgbQuad[i].rgbGreen;
quad[i].rgbRed = bmpImg->imageRgbQuad[i].rgbRed;
quad[i].rgbReserved = bmpImg->imageRgbQuad[i].rgbReserved;
}
fwrite(quad, sizeof(BMP_RgbQuad), 2, pFile);
free(quad);
//½«ÔͼλͼÊý¾Ý°´´ÓÏÂÍùÉÏ¡¢´Ó×óÍùºó¶Á³öºóÔÙ°´´ÓÏÂÍùÉÏ¡¢´Ó×óÍùºóдÈë
for (i=bmpImg->height-1; i>-1; i--)
{
for (j=0; j<bmpImg->width/8; j++)
{
/********* Begin *********/
pixVal = bmpImg->imageData[i*bmpImg->width/8+j];
/********* End *********/
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
}
}
}
fclose(pFile);
return true;
}
第2关:8位灰度BMP图像的写
#include "BMP.h"
bool BMP_SaveImage(char* path, BMP_Image* bmpImg)
{
FILE *pFile;
// unsigned short fileType;
BMP_BitMapFileHeader bmpFileHeader;
BMP_BitMapInfoHeader bmpInfoHeader;
unsigned char pixVal; //= '\0';
int i, j;
BMP_RgbQuad* quad;
pFile = fopen(path, "wb");
if (!pFile)
{
return false;
}
if (bmpImg->biBitCount == 8)//8룬µ¥Í¨µÀ£¬»Ò¶Èͼ
{
bmpFileHeader.bfType = 0x4D42;
bmpFileHeader.bfSize = 54 + 256*4 + bmpImg->height*bmpImg->width;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = 54 + 256*4;
fwrite(&bmpFileHeader, sizeof(BMP_BitMapFileHeader), 1, pFile);
bmpInfoHeader.biSize = 40;
bmpInfoHeader.biWidth = bmpImg->width;
bmpInfoHeader.biHeight = bmpImg->height;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biBitCount = 8;
bmpInfoHeader.biCompression = 0;
bmpInfoHeader.biSizeImage = bmpImg->height*bmpImg->width;
bmpInfoHeader.biXPelsPerMeter = 0;
bmpInfoHeader.biYPelsPerMeter = 0;
bmpInfoHeader.biClrUsed = 256;
bmpInfoHeader.biClrImportant = 256;
fwrite(&bmpInfoHeader, sizeof(BMP_BitMapInfoHeader), 1, pFile);
quad = (BMP_RgbQuad*)malloc(sizeof(BMP_RgbQuad)*256);
for (i=0; i<256; i++)
{
quad[i].rgbBlue = bmpImg->imageRgbQuad[i].rgbBlue;
quad[i].rgbGreen = bmpImg->imageRgbQuad[i].rgbGreen;
quad[i].rgbRed = bmpImg->imageRgbQuad[i].rgbRed;
quad[i].rgbReserved = bmpImg->imageRgbQuad[i].rgbReserved;
}
fwrite(quad, sizeof(BMP_RgbQuad), 256, pFile);
free(quad);
for (i=bmpImg->height-1; i>-1; i--)
{
for (j=0; j<bmpImg->width; j++)
{
/********* Begin *********/
pixVal = bmpImg->imageData[i*bmpImg->width+j];
/********* End *********/
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
}
}
}
fclose(pFile);
return true;
}
第3关:24位彩色BMP图像的写
#include "BMP.h"
bool BMP_SaveImage(char* path, BMP_Image* bmpImg)
{
FILE *pFile;
// unsigned short fileType;
BMP_BitMapFileHeader bmpFileHeader;
BMP_BitMapInfoHeader bmpInfoHeader;
unsigned char pixVal; //= '\0';
int i, j;
BMP_RgbQuad* quad;
pFile = fopen(path, "wb");
if (!pFile)
{
return false;
}
if (bmpImg->biBitCount == 24)//24λ£¬Í¨µÀ£¬²Êͼ
{
//printf("¸ÃλͼΪÕæ²Êɫͼ\n");
bmpFileHeader.bfType = 0x4D42;
bmpFileHeader.bfSize = bmpImg->height*bmpImg->width*3 + 54;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = 54;
fwrite(&bmpFileHeader, sizeof(BMP_BitMapFileHeader), 1, pFile);
bmpInfoHeader.biSize = 40;
bmpInfoHeader.biWidth = bmpImg->width;
bmpInfoHeader.biHeight = bmpImg->height;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biBitCount = 24;
bmpInfoHeader.biCompression = 0;
bmpInfoHeader.biSizeImage = bmpImg->height*bmpImg->width*3;
bmpInfoHeader.biXPelsPerMeter = 0;
bmpInfoHeader.biYPelsPerMeter = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biClrImportant = 0;
fwrite(&bmpInfoHeader, sizeof(BMP_BitMapInfoHeader), 1, pFile);
for (i=bmpImg->height-1; i>-1; i--)
{
for (j=0; j<bmpImg->width; j++)
{
/********* Begin *********/
pixVal = bmpImg->imageData[i*bmpImg->width*3+j*3];
/********* End *********/
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
/********* Begin *********/
pixVal = bmpImg->imageData[i*bmpImg->width*3+j*3+1];
/********* End *********/
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
/********* Begin *********/
pixVal = bmpImg->imageData[i*bmpImg->width*3+j*3+2];
/********* End *********/
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
}
}
}
fclose(pFile);
return true;
}