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

這就是BMP圖像的理論知識(shí),有個(gè)大概的了解就行,最主要的是從理論到實(shí)踐?。。?/p>
廢話不多說(shuō),直接上干貨。
代碼
定義頭文件為“bmp.h”,定義read_bmp函數(shù)為讀函數(shù),write_bmp函數(shù)為寫(xiě)函數(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;
}
寫(xiě)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圖從讀到寫(xiě)的全部過(guò)程,有興趣的同學(xué)動(dòng)手實(shí)踐下,會(huì)有意向不到的收獲。
注:在線轉(zhuǎn)換BMP圖的網(wǎng)址,可以將任何格式的照片轉(zhuǎn)換為BMP格式。親測(cè)好用。鏈接奉上BMP圖像轉(zhuǎn)換網(wǎng)址
到此這篇關(guān)于C語(yǔ)言實(shí)現(xiàn)BMP圖像的讀寫(xiě)功能的文章就介紹到這了,更多相關(guān)C語(yǔ)言BMP圖像讀寫(xiě)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C生萬(wàn)物C語(yǔ)言宏將整數(shù)二進(jìn)制位的奇偶數(shù)位交換
這篇文章主要為大家介紹了C生萬(wàn)物C語(yǔ)言使用宏將整數(shù)二進(jìn)制位的奇偶數(shù)位交換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
C++實(shí)現(xiàn)LeetCode(51.N皇后問(wèn)題)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(51.N皇后問(wèn)題),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言動(dòng)態(tài)開(kāi)辟內(nèi)存詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言動(dòng)態(tài)開(kāi)辟內(nèi)存,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02
Visual C++中Tab View的多種實(shí)現(xiàn)方法
這篇文章主要介紹了Visual C++中Tab View的多種實(shí)現(xiàn)方法,包括了CTabCtrl控件、CSheetCtrl標(biāo)簽選擇窗口以及靜態(tài)分割窗口等實(shí)現(xiàn)Tab View的方法,需要的朋友可以參考下2014-10-10

