純C語言實作BMP影像的讀、寫
對于剛接觸數字影像的同學,應該都有一個疑問,如何把一個BMP格式的影像用純C語言讀入呢,我相信這也是數字影像處理的第一步,如果有幸看到這篇檔案,我就有幸的成為你數字影像處理路上的第一盞明燈!
了解BMP的構成

這就是BMP影像的理論知識,有個大概的了解就行,最主要的是從理論到實踐!!!
廢話不多說,直接上干貨,
代碼
定義頭檔案為“bmp.h”,定義read_bmp函式為讀函式,write_bmp函式為寫函式
讀bmp圖
#include <stdlib.h>
#include <math.h>
#include <Windows.h>
#include "bmp.h"
/*存盤原圖的像素寬度高度和位圖深度*/
FILE* fpbmp;
FILE* fpout;
unsigned char* fpBmpHeader; //位圖頭
unsigned char* fpFileHeader; //位圖資訊
RGBQUAD* pColorTable; //BMP 調色板
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) + sizeof(RGBQUAD),SEEK_SET);
fread(pBmpBuf, lineByte * *Height, 1, fpbmp);//pBmpBuf為影像的RGB資料,也是我們將要處理的資料
return 0;
}
寫BMP圖
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);
/*復制原圖中 位圖 資訊到新影像*/
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);
/*Copy 調色板 info*/
pColorTable = new RGBQUAD[256];
fseek(fpbmp, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER), SEEK_SET);
fseek(fpout, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER), SEEK_SET);
fread(pColorTable, sizeof(RGBQUAD), 256, fpbmp);
fwrite(pColorTable, sizeof(RGBQUAD), 256, fpout);
fseek(fpout, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD), SEEK_SET);
fwrite(img, lineByte * *Height, sizeof(char), fpout);
fclose(fpout);
fclose(fpbmp);
return 0;
}
main函式呼叫
#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);//申請空間
const char* path = "D:\\test\\read_bmp_image\\1-B.bmp";//圖的路徑
read_bmp(path, pBmpBuf, &width, &height, &bitCount);
write_bmp(pBmpBuf, &width, &height, &bitCount);
}
總結,將read_bmp函式回傳的pBmpBuf引數,賦值給write_bmp函式的img引數,就實作了BMP圖從讀到寫的全部程序,有興趣的同學動手實踐下,會有意向不到的識訓,
注:在線轉換BMP圖的網址,可以將任何格式的照片轉換為BMP格式,親測好用,鏈接奉上BMP影像轉換網址
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/272337.html
標籤:其他
