BMP图像的写

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值