溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C語(yǔ)言如何實(shí)現(xiàn)BMP圖像讀寫功能

發(fā)布時(shí)間:2021-04-13 14:31:15 來(lái)源:億速云 閱讀:235 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)C語(yǔ)言如何實(shí)現(xiàn)BMP圖像讀寫功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

C語(yǔ)言實(shí)現(xiàn)BMP圖像的讀寫

對(duì)于剛接觸數(shù)字圖像的同學(xué),應(yīng)該都有一個(gè)疑問,如何把一個(gè)BMP格式的圖像用純C語(yǔ)言讀入呢,我相信這也是數(shù)字圖像處理的第一步,如果有幸看到這篇文檔,我就有幸的成為你數(shù)字圖像處理路上的第一盞明燈!

了解BMP的構(gòu)成

C語(yǔ)言如何實(shí)現(xiàn)BMP圖像讀寫功能

這就是BMP圖像的理論知識(shí),有個(gè)大概的了解就行,最主要的是從理論到實(shí)踐?。?!

廢話不多說(shuō),直接上干貨。

代碼

定義頭文件為“bmp.h”,定義read_bmp函數(shù)為讀函數(shù),write_bmp函數(shù)為寫函數(shù)
讀bmp圖

#include <stdlib.h>
#include <math.h>
#include <Windows.h>
#include "bmp.h"
/*存儲(chǔ)原圖的像素寬度高度和位圖深度*/
FILE* fpbmp;
FILE* fpout;
unsigned char* fpBmpHeader;   //位圖頭
unsigned char* fpFileHeader;  //位圖信息
RGBQUAD* pColorTable;         //BMP 調(diào)色板
int read_bmp(const char* path, unsigned char *pBmpBuf,int *Width,int *Height,int * bitCount)
{
	fpbmp = fopen(path, "rb");//path為圖像路徑
	unsigned short s;
	fread(&s, 1, 2, fpbmp);
	//判斷讀入的圖像是否為BMP圖  字符串"BM"=19778
	if (s == 19778)
	{
		printf("Open bmp success!!!\n");
	}
	else
	{
		printf("Open bmp fail!!!\n");
		return -1;
	}
	fseek(fpbmp, 0, SEEK_SET);
	BITMAPFILEHEADER fileHead;
	fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fpbmp);

	BITMAPINFOHEADER infoHead;
	fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fpbmp);

	*Width = infoHead.biWidth;//圖像的寬
	*Height = infoHead.biHeight;//圖像的高
	*bitCount = infoHead.biBitCount;
	int lineByte = (*Width * *bitCount / 8 + 3) / 4 * 4;
	fseek(fpbmp, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) ,SEEK_SET);
	fread(pBmpBuf, lineByte * *Height, 1, fpbmp);//pBmpBuf為圖像的RGB數(shù)據(jù),也是我們將要處理的數(shù)據(jù)
	return 0;
}

寫B(tài)MP圖

int write_bmp(unsigned char* img, int* Width, int* Height, int* bitCount)
{
	fpout = fopen("out.bmp", "wb+");
	if (fpbmp == NULL)
	{
		printf("read bmp failed!!!\n");
		return -1;
	}
	int lineByte = (*Width * *bitCount / 8 + 3) / 4 * 4;
	if (lineByte == 0)
	{
		printf("err");
		return -1;
	}
	fpFileHeader = new unsigned char[(sizeof(BITMAPFILEHEADER))];
	fseek(fpbmp, 0, SEEK_SET);  //定位原圖 偏移位置
	fseek(fpout, 0, SEEK_SET);  //定位新圖 偏移位置
	fread(fpFileHeader, 1, sizeof(BITMAPFILEHEADER), fpbmp);
	fwrite(fpFileHeader, 1, sizeof(BITMAPFILEHEADER), fpout);


	/*復(fù)制原圖中 位圖 信息到新圖像*/
	fpBmpHeader = new unsigned char[(sizeof(BITMAPINFOHEADER))];
	fseek(fpbmp, sizeof(BITMAPFILEHEADER), SEEK_SET);
	fseek(fpout, sizeof(BITMAPFILEHEADER), SEEK_SET);
	fread(fpBmpHeader, 1, sizeof(BITMAPINFOHEADER), fpbmp);
	fwrite(fpBmpHeader, 1, sizeof(BITMAPINFOHEADER), fpout);
	
	fseek(fpout, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) , SEEK_SET);
	fwrite(img, lineByte * *Height, sizeof(char), fpout);
	fclose(fpout);
	fclose(fpbmp);
	return 0;
}

main函數(shù)調(diào)用

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Windows.h>
#include "bmp.h"

int main()
{
	int width, height, bitCount = 0;
	unsigned char* pBmpBuf = (unsigned char*)malloc(1000 * 1000 * 3);//申請(qǐng)空間
	const char* path = "D:\\test\\read_bmp_image\\1-B.bmp";//圖的路徑
	read_bmp(path, pBmpBuf, &width, &height, &bitCount);
	write_bmp(pBmpBuf, &width, &height, &bitCount);
}

總結(jié),將read_bmp函數(shù)返回的pBmpBuf參數(shù),賦值給write_bmp函數(shù)的img參數(shù),就實(shí)現(xiàn)了BMP圖從讀到寫的全部過(guò)程,有興趣的同學(xué)動(dòng)手實(shí)踐下,會(huì)有意向不到的收獲。

感謝各位的閱讀!關(guān)于“C語(yǔ)言如何實(shí)現(xiàn)BMP圖像讀寫功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI