第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;
}