iT邦幫忙

0

C++讀取bmp圖檔

c++
小魚 2017-07-02 00:11:0120890 瀏覽

純灌水,不要理我


	fstream file;
	int x = 0,y = 0; 
	WORD bmpId;
	IBYTE fileSize;
	WORD bmpReserved1;
	WORD bmpReserved2;
	IBYTE bmpOffset;
	IBYTE bmpInfoHeaderSize;
	IWORD bmpWidth;
	IWORD bmpHeight;
	WORD bmpPlanes;
	WORD bmpbitCount;
	IWORD bmpCompression;
	IWORD bmpDataSize;
	IWORD bmpXPixelPerMeter;
	IWORD bmpYPixelPerMeter;
	IBYTE bmpColorUsed;
	IBYTE bmpColorImportant; 
	TotalGray = 0;

	//以Dialog方式開啟檔案
	CFileDialog fd(true,"bmp","*.bmp",OFN_HIDEREADONLY,"BMP Files(*.bmp)");
    
	if(fd.DoModal() == IDOK)  //按下OK 
	{
        //開啟檔案成功
		CString str = fd.GetPathName(); //取得開啟檔案的全名(包含路徑)
		file.open(str, ios::in|ios::binary);
		file.read((char*)&bmpId, sizeof(WORD));
		file.read((char*)&fileSize, sizeof(IBYTE));
		file.read((char*)&bmpReserved1, sizeof(WORD));
		file.read((char*)&bmpReserved2, sizeof(WORD));
		file.read((char*)&bmpOffset, sizeof(IBYTE));
		file.read((char*)&bmpInfoHeaderSize, sizeof(IBYTE));
		file.read((char*)&bmpWidth, sizeof(IWORD));
		file.read((char*)&bmpHeight, sizeof(IWORD));
		file.read((char*)&bmpPlanes, sizeof(WORD));
		file.read((char*)&bmpbitCount, sizeof(WORD)); 
		file.read((char*)&bmpCompression, sizeof(IWORD));
		file.read((char*)&bmpDataSize, sizeof(IWORD)); 
		file.read((char*)&bmpXPixelPerMeter, sizeof(IWORD));
		file.read((char*)&bmpYPixelPerMeter, sizeof(DWORD));
		file.read((char*)&bmpColorUsed, sizeof(IBYTE));
		file.read((char*)&bmpColorImportant, sizeof(IBYTE));

		y = ImageY-1;
		x = 0;

		do
		{
			x = 0;
			do
			{
				char chR,chG,chB;
				int inR,inG,inB;

				file.get(chB);
				file.get(chG);
				file.get(chR);

				if(chR<0)
					inR = (int)chR+255;
				else
					inR = (int)chR;

				if(chG<0)
					inG = 255 + (int)chG;
				else
					inG = chG;

				if(chB<0)
					inB = 255 + (int)chB;
				else
					inB = chB;

				Metrix[x][y].SetXY(x,y);
				Metrix[x][y].SetB(inB);
				Metrix[x][y].SetG(inG);
				Metrix[x][y].SetR(inR);

				x++;

			}while(x<ImageX);
			y--;
		}while(y>=0);
	}
	file.close();

尚未有邦友留言

立即登入留言