C++——bmp画像を1枚読み込み、その画像データを抽出し、マトリクス/txtファイルに格納する


本稿では,bmpピクチャを読み込み,マトリクス/TXTファイルにデータを格納し,ピクチャを保存する機能を完成する.
開始する前に、bmpビットマップの格納方法を理解する必要があります.
BMPファイルのデータはファイルヘッダから順に4つの部分に分けられる:(1)bmpファイルヘッダ(bmp file header):ファイルのフォーマット、サイズなどの情報を提供する(2)ビットマップ情報ヘッダ(bitmap information):画像データを提供するサイズ、ビットプレーン数、圧縮方式、色インデックスなどの情報を提供する(3)パレット(color palette):オプション、インデックスを使用して画像を表す場合、パレットはインデックスに対応する色のマッピングテーブルです.
(4)ビットマップデータ(bitmap data):画像データ、画素が下から上、左から右の順に各行に占める空間は4の整数倍でなければならない
read_save.h
#include
#include
#include
using namespace std;


unsigned char *pBmpBuf;//         
int bmpWidth;//    
int bmpHeight;//    
RGBQUAD *pColorTable;//     
int biBitCount;//    ,     



//            
void showBmpHead(BITMAPFILEHEADER pBmpHead){
	cout << "
:" << endl; cout << " :" << pBmpHead.bfSize << endl; cout << " _1:" << pBmpHead.bfReserved1 << endl; cout << " _2:" << pBmpHead.bfReserved2 << endl; cout << " :" << pBmpHead.bfOffBits << endl << endl; } // void showBmpInforHead(BITMAPINFOHEADER pBmpInforHead){ cout << "
:" << endl; cout << " :" << pBmpInforHead.biSize << endl; cout << " :" << pBmpInforHead.biWidth << endl; cout << " :" << pBmpInforHead.biHeight << endl; cout << "biPlanes :" << pBmpInforHead.biPlanes << endl; cout << "biBitCount :" << pBmpInforHead.biBitCount << endl; cout << " :" << pBmpInforHead.biCompression << endl; cout << "biSizeImage :" << pBmpInforHead.biSizeImage << endl; cout << "X :" << pBmpInforHead.biXPelsPerMeter << endl; cout << "Y :" << pBmpInforHead.biYPelsPerMeter << endl; cout << " :" << pBmpInforHead.biClrUsed << endl; cout << " :" << pBmpInforHead.biClrImportant << endl; } //----------------------------------------------------------------------------------------- // 、 、 、 , bool readBmp(char *bmpName) { FILE *fp = fopen(bmpName, "rb");// if (fp == 0) return 0; // BITMAPFILEHEADER fseek(fp, sizeof(BITMAPFILEHEADER), 0); /* BITMAPFILEHEADER filehead; fread(&filehead, 1, sizeof(BITMAPFILEHEADER), fp); showBmpHead(filehead);// */ // , , head BITMAPINFOHEADER infohead; fread(&infohead, sizeof(BITMAPINFOHEADER), 1, fp); // 、 、 bmpWidth = infohead.biWidth; bmpHeight = infohead.biHeight; biBitCount = infohead.biBitCount;// , ( 4 ) showBmpInforHead(infohead);// int lineByte = (bmpWidth * biBitCount / 8 + 3) / 4 * 4;// , 256 if (biBitCount == 8) { // , pColorTable = new RGBQUAD[256]; fread(pColorTable, sizeof(RGBQUAD), 256, fp); } // , pBmpBuf = new unsigned char[lineByte * bmpHeight]; fread(pBmpBuf, 1, lineByte * bmpHeight, fp); fclose(fp);// return 1;// } // bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height, int biBitCount, RGBQUAD *pColorTable) { // 0, , if (!imgBuf) return 0; // , , 1024 , 0 int colorTablesize = 0; if (biBitCount == 8) colorTablesize = 1024; // 4 int lineByte = (width * biBitCount / 8 + 3) / 4 * 4; // FILE *fp = fopen(bmpName, "wb"); if (fp == 0) return 0; //------------------------------------------------------------------------------------------------------------------ // , BITMAPFILEHEADER fileHead; fileHead.bfType = 0x4D42;//bmp //bfSize 4 fileHead.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + colorTablesize + lineByte*height; fileHead.bfReserved1 = 0; fileHead.bfReserved2 = 0; //bfOffBits 3 fileHead.bfOffBits = 54 + colorTablesize; //-------------------------------------------------------------------------------------------------------------------- // fwrite(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp); // , BITMAPINFOHEADER infohead; infohead.biBitCount = biBitCount; infohead.biClrImportant = 0; infohead.biClrUsed = 0; infohead.biCompression = 0; infohead.biHeight = height; infohead.biPlanes = 1; infohead.biSize = 40; infohead.biSizeImage = lineByte*height; infohead.biWidth = width; infohead.biXPelsPerMeter = 0; infohead.biYPelsPerMeter = 0; // fwrite(&infohead, sizeof(BITMAPINFOHEADER), 1, fp); //---------------------------------------------------------------------------------------------------------------------- // , , if (biBitCount == 8) fwrite(pColorTable, sizeof(RGBQUAD), 256, fp); // fwrite(imgBuf, height*lineByte, 1, fp); // fclose(fp); return 1; }

read.cpp
#include
#include
#include
#include

#include"readbmp.h"
#include"savebmp.h"

using namespace std;

//unsigned int out_r[2000][2000];
//unsigned int out_g[2000][2000];
//unsigned int out_b[2000][2000];
unsigned int **out_r;
unsigned int **out_g;
unsigned int **out_b;

void doIt()
{

	char readPath[] = "D:\\C++_file\\image_deal_C++\\read_BMP\\lunpan.bmp";
	readBmp(readPath);
	//         
	cout << "
width=" << bmpWidth << "
height=" << bmpHeight << "
biBitCount=" << biBitCount << endl; // int linebyte1 = (bmpWidth*biBitCount / 8 + 3) / 4 * 4; int n = 0, m = 0, count_xiang_su = 0; out_r = new unsigned int *[bmpHeight]; // for (int i = 0; i