個人實驗記錄 | 數字影像處理實驗3·影像直方圖與均衡化處理
參考文章:【數字影像處理】四.MFC對話方塊繪製灰度直方圖
CTest2ZFTDlg.cpp: 實現檔案
#include "pch.h"
#include "Test2.h"
#include "CTest2ZFTDlg.h"
#include "afxdialogex.h"
IMPLEMENT_DYNAMIC(CTest2ZFTDlg, CDialogEx)
CTest2ZFTDlg::CTest2ZFTDlg(CWnd* pParent )
: CDialogEx(IDD_DIALOG_ZFT, pParent)
, m_blueBZC(_T(""))
, m_bluePJHD(_T(""))
, m_blueXS(_T(""))
, m_blueZZHD(_T(""))
, m_greenBZC(_T(""))
, m_greenPJHD(_T(""))
, m_greenXS(_T(""))
, m_greenZZHD(_T(""))
, m_redBZC(_T(""))
, m_redPJHD(_T(""))
, m_redXS(_T(""))
, m_redZZHD(_T(""))
, m_blueF(_T(""))
, m_blueMax(_T(""))
, m_blueMin(_T(""))
, m_blueMost(_T(""))
, m_greenF(_T(""))
, m_greenMax(_T(""))
, m_greenMin(_T(""))
, m_greenMost(_T(""))
, m_redF(_T(""))
, m_redMax(_T(""))
, m_redMin(_T(""))
, m_redMost(_T(""))
{
}
CTest2ZFTDlg::~CTest2ZFTDlg()
{
}
void CTest2ZFTDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_STATIC_B_BZC, m_blueBZC);
DDX_Text(pDX, IDC_STATIC_B_PJHD, m_bluePJHD);
DDX_Text(pDX, IDC_STATIC_B_XS, m_blueXS);
DDX_Text(pDX, IDC_STATIC_B_ZZHD, m_blueZZHD);
DDX_Text(pDX, IDC_STATIC_G_BZC, m_greenBZC);
DDX_Text(pDX, IDC_STATIC_G_PJHD, m_greenPJHD);
DDX_Text(pDX, IDC_STATIC_G_XS, m_greenXS);
DDX_Text(pDX, IDC_STATIC_G_ZZHD, m_greenZZHD);
DDX_Text(pDX, IDC_STATIC_R_BZC, m_redBZC);
DDX_Text(pDX, IDC_STATIC_R_PJHD, m_redPJHD);
DDX_Text(pDX, IDC_STATIC_R_XS, m_redXS);
DDX_Text(pDX, IDC_STATIC_R_ZZHD, m_redZZHD);
DDX_Text(pDX, IDC_STATIC_B_F, m_blueF);
DDX_Text(pDX, IDC_STATIC_B_MAX, m_blueMax);
DDX_Text(pDX, IDC_STATIC_B_MIN, m_blueMin);
DDX_Text(pDX, IDC_STATIC_B_MOST, m_blueMost);
DDX_Text(pDX, IDC_STATIC_G_F, m_greenF);
DDX_Text(pDX, IDC_STATIC_G_MAX, m_greenMax);
DDX_Text(pDX, IDC_STATIC_G_MIN, m_greenMin);
DDX_Text(pDX, IDC_STATIC_G_MOST, m_greenMost);
DDX_Text(pDX, IDC_STATIC_R_F, m_redF);
DDX_Text(pDX, IDC_STATIC_R_MAX, m_redMax);
DDX_Text(pDX, IDC_STATIC_R_Min, m_redMin);
DDX_Text(pDX, IDC_STATIC_R_MOST, m_redMost);
}
BEGIN_MESSAGE_MAP(CTest2ZFTDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_NCPAINT()
END_MESSAGE_MAP()
BOOL CTest2ZFTDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
return TRUE;
}
void CTest2ZFTDlg::OnPaint()
{
CPaintDC dc(this);
extern int Red[256], Green[256], Blue[256];
CRect rectpic;
GetDlgItem(IDC_STATIC_Control)->GetWindowRect(&rectpic);
int x, y;
x = rectpic.Width();
y = rectpic.Height();
CWnd* pWnd = GetDlgItem(IDC_STATIC_Control);
CDC* pDC = pWnd->GetDC();
CPen* RedPen = new CPen();
RedPen->CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
CGdiObject* RedOlderPen = pDC->SelectObject(RedPen);
pDC->Rectangle(9, 16, 312, 147);
pDC->MoveTo(15, 20);
pDC->LineTo(15, 128);
pDC->LineTo(305, 128);
pDC->MoveTo(305, 128);
pDC->LineTo(300, 123);
pDC->MoveTo(305, 128);
pDC->LineTo(300, 133);
pDC->MoveTo(15, 20);
pDC->LineTo(10, 25);
pDC->MoveTo(15, 20);
pDC->LineTo(20, 25);
CString str;
int i;
for (i = 0;i <= 5;i++)
{
str.Format(_T("%d"), i * 50);
pDC->SetTextColor(RGB(255, 0, 255));
pDC->TextOut(15 + 48 * i, 130, str);
pDC->MoveTo(15 + 48 * i, 128);
pDC->LineTo(15 + 48 * i, 125);
}
for (i = 0;i <= 5;i++)
{
pDC->MoveTo(15, 128 - 20 * i);
pDC->LineTo(18, 128 - 20 * i);
}
for (i = 1;i < 256;i++)
{
pDC->MoveTo(15 + i, 128);
if ((128 - 16) > (Red[i] / 40))
pDC->LineTo(15 + i, 128 - (Red[i] / 40));
else
pDC->LineTo(15 + i, 16);
}
CPen* GreenPen = new CPen();
GreenPen->CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
CGdiObject* GreenOlderPen = pDC->SelectObject(GreenPen);
pDC->Rectangle(9, 167, 312, 308);
pDC->MoveTo(15, 171);
pDC->LineTo(15, 288);
pDC->LineTo(305, 288);
pDC->MoveTo(305, 288);
pDC->LineTo(300, 283);
pDC->MoveTo(305, 288);
pDC->LineTo(300, 293);
pDC->MoveTo(15, 171);
pDC->LineTo(10, 176);
pDC->MoveTo(15, 171);
pDC->LineTo(20, 176);
for (i = 0;i <= 5;i++)
{
str.Format(_T("%d"), i * 50);
pDC->SetTextColor(RGB(255, 0, 255));
pDC->TextOut(15 + 48 * i, 290, str);
pDC->MoveTo(15 + 48 * i, 288);
pDC->LineTo(15 + 48 * i, 285);
}
for (i = 0;i <= 5;i++)
{
pDC->MoveTo(15, 288 - 20 * i);
pDC->LineTo(18, 288 - 20 * i);
}
for (i = 1;i < 256;i++)
{
pDC->MoveTo(15 + i, 288);
if ((288 - 167) > (Green[i] / 40))
pDC->LineTo(15 + i, 288 - (Green[i] / 40));
else
pDC->LineTo(15 + i, 167);
}
CPen* BluePen = new CPen();
BluePen->CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
CGdiObject* BlueOlderPen = pDC->SelectObject(BluePen);
pDC->Rectangle(9, 327, 312, 468);
pDC->MoveTo(15, 331);
pDC->LineTo(15, 448);
pDC->LineTo(305, 448);
pDC->MoveTo(305, 448);
pDC->LineTo(300, 443);
pDC->MoveTo(305, 448);
pDC->LineTo(300, 453);
pDC->MoveTo(15, 331);
pDC->LineTo(10, 336);
pDC->MoveTo(15, 331);
pDC->LineTo(20, 336);
for (i = 0;i <= 5;i++)
{
str.Format(_T("%d"), i * 50);
pDC->SetTextColor(RGB(255, 0, 255));
pDC->TextOut(15 + 48 * i, 450, str);
pDC->MoveTo(15 + 48 * i, 448);
pDC->LineTo(15 + 48 * i, 445);
}
for (i = 0;i <= 5;i++)
{
pDC->MoveTo(15, 448 - 20 * i);
pDC->LineTo(18, 448 - 20 * i);
}
for (i = 1;i < 256;i++)
{
pDC->MoveTo(15 + i, 448);
if ((448 - 327) > (Blue[i] / 40))
pDC->LineTo(15 + i, 448 - (Blue[i] / 40));
else
pDC->LineTo(15 + i, 327);
}
pDC->SelectObject(RedOlderPen);
pDC->SelectObject(GreenOlderPen);
pDC->SelectObject(BlueOlderPen);
delete RedPen;
delete GreenPen;
delete BluePen;
ReleaseDC(pDC);
return;
}
void CTest2ZFTDlg::OnNcPaint()
{
}
View.cpp實現:
#include "pch.h"
#include "framework.h"
#ifndef SHARED_HANDLERS
#include "Test2.h"
#endif
#include "Test2Doc.h"
#include "Test2View.h"
#include "CTest2ZFTDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CTest2View, CView)
BEGIN_MESSAGE_MAP(CTest2View, CView)
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
ON_COMMAND(ID_FILE_OPEN, &CTest2View::OnFileOpen)
ON_COMMAND(ID_FILE_SAVE_AS, &CTest2View::OnFileSaveAs)
ON_COMMAND(ID_ZFT_YT, &CTest2View::OnZftYt)
ON_COMMAND(ID_JHH, &CTest2View::OnJhh)
ON_COMMAND(ID_GGH, &CTest2View::OnGgh)
ON_COMMAND(ID_EZH, &CTest2View::OnEzh)
END_MESSAGE_MAP()
int Red[256], Green[256], Blue[256];
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
CString debugMess;
CTest2View::CTest2View() noexcept
{
m_dib = NULL;
}
CTest2View::~CTest2View()
{
if (m_dib != NULL) {
delete m_dib;
m_dib = NULL;
}
}
BOOL CTest2View::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
void CTest2View::OnDraw(CDC* pDC)
{
CTest2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
if (SaveName.Compare(_T("bmp")) == 0)
{
ShowBitmap(pDC, BmpName,0,0);
}
}
BOOL CTest2View::OnPreparePrinting(CPrintInfo* pInfo)
{
return DoPreparePrinting(pInfo);
}
void CTest2View::OnBeginPrinting(CDC* , CPrintInfo* )
{
}
void CTest2View::OnEndPrinting(CDC* , CPrintInfo* )
{
}
#ifdef _DEBUG
void CTest2View::AssertValid() const
{
CView::AssertValid();
}
void CTest2View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTest2Doc* CTest2View::GetDocument() const
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTest2Doc)));
return (CTest2Doc*)m_pDocument;
}
#endif
void CTest2View::OnFileOpen()
{
if (m_dib)
{
delete m_dib;
m_dib = NULL;
}
CString filter;
filter = "所有檔案(*.bmp,*.jpg,*.gif,*tiff)|*.bmp;*.jpg;*.gif;*.tiff| BMP(*.bmp)|*.bmp| JPG(*.jpg)|*.jpg| GIF(*.gif)|*.gif| TIFF(*.tiff)|*.tiff||";
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, filter, NULL);
if (dlg.DoModal() == IDOK)
{
BmpName = dlg.GetPathName();
SaveName = dlg.GetFileExt();
SaveName.MakeLower();
m_dib = new MyDIB;
m_dib->Read(BmpName);
Invalidate();
}
}
void CTest2View::OnFileSaveAs()
{
if(m_dib == NULL) {
return;
}
CDC* pDC = GetWindowDC();
CFileDialog fDlg(FALSE, _T("bmp"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("點陣圖檔案|*.bmp"), this);
if (fDlg.DoModal() == IDOK)
{
CString bmpfile = fDlg.GetPathName();
m_dib->Write(bmpfile, pDC, m_dib->m_width, m_dib->m_height);
}
}
void CTest2View::ShowBitmap(CDC* pDC, CString BmpName,int x ,int y)
{
HBITMAP m_hBitmap;
m_hBitmap = (HBITMAP)LoadImage(NULL, BmpName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE | LR_CREATEDIBSECTION);
if (m_bitmap.m_hObject)
{
m_bitmap.Detach();
}
m_bitmap.Attach(m_hBitmap);
CRect rect;
GetClientRect(&rect);
int m_nWindowWidth = rect.right - rect.left;
int m_nWindowHeight = rect.bottom - rect.top;
CDC dcBmp;
if (!dcBmp.CreateCompatibleDC(pDC))
return;
BITMAP m_bmp;
m_bitmap.GetBitmap(&m_bmp);
CBitmap* pbmpOld = NULL;
dcBmp.SelectObject(&m_bitmap);
m_dib->m_height = m_bmp.bmHeight;
m_dib->m_width = m_bmp.bmWidth;
pDC->StretchBlt(x, y, m_bmp.bmWidth, m_bmp.bmHeight, &dcBmp, 0, 0, m_bmp.bmWidth, m_bmp.bmHeight, SRCCOPY);
dcBmp.SelectObject(pbmpOld);
DeleteObject(&m_bitmap);
dcBmp.DeleteDC();
}
void CTest2View::OnZftYt()
{
CTest2ZFTDlg dlg;
CFile fp(BmpName, CFile::modeRead | CFile::typeBinary);
fp.Read(&bfh, sizeof(BITMAPFILEHEADER));
fp.Read(&bih, sizeof(BITMAPINFOHEADER));
int bytesPerLine = ((bih.biWidth * bih.biBitCount + 31) / 32) * 4;
int real_size = bytesPerLine * bih.biHeight;
int m_nImage = real_size;
int i,j;
for(j=0;j<256;j++) {
Red[j]=0;
Green[j]=0;
Blue[j]=0;
}
unsigned char red,green,blue;
int IntRed,IntGreen,IntBlue;
int MaxRed = 0, MaxBlue = 0, MaxGreen = 0;
int MinRed = 255, MinBlue = 255, MinGreen = 255;
double sumRedHD=0,sumGreenHD=0,sumBlueHD=0;
for(i=0; i<m_nImage/3; i++ )
{
fp.Read(&red, sizeof(char));
IntRed=int(red);
if (IntRed >= MaxRed) {
MaxRed = IntRed;
}
if (IntRed <= MinRed) {
MinRed = IntRed;
}
sumRedHD=sumRedHD+IntRed;
if( IntRed>=0 && IntRed<256 ) Red[IntRed]++;
fp.Read(&green, sizeof(char));
IntGreen=int(green);
if (IntGreen >= MaxGreen) {
MaxGreen = IntGreen;
}
if (IntGreen <= MinGreen) {
MinGreen = IntGreen;
}
sumGreenHD=sumGreenHD+IntGreen;
if( IntGreen>=0 && IntGreen<256 ) Green[IntGreen]++;
fp.Read(&blue, sizeof(char));
IntBlue=int(blue);
if (IntBlue >= MaxBlue) {
MaxBlue = IntBlue;
}
if (IntBlue <= MinBlue) {
MinBlue = IntBlue;
}
sumBlueHD=sumBlueHD+IntBlue;
if( IntBlue>=0 && IntBlue<256 ) Blue[IntBlue]++;
}
fp.Close();
dlg.m_redMax.Format(_T("%d"), MaxRed);
dlg.m_blueMax.Format(_T("%d"), MaxBlue);
dlg.m_greenMax.Format(_T("%d"), MaxGreen);
dlg.m_redMin.Format(_T("%d"), MinRed);
dlg.m_blueMin.Format(_T("%d"), MinBlue);
dlg.m_greenMin.Format(_T("%d"), MinGreen);
int MostRed = 0, MostBlue = 0, MostGreen = 0;
int MostRed_G = 0, MostBlue_G = 0, MostGreen_G = 0;
for (j = 0;j < 256;j++) {
if (MostRed <= Red[j]) {
MostRed = Red[j];
MostRed_G = j;
}
if (MostBlue <= Blue[j]) {
MostBlue = Blue[j];
MostBlue_G = j;
}
if (MostGreen <= Green[j]) {
MostGreen = Green[j];
MostGreen_G = j;
}
}
dlg.m_redMost.Format(_T("%d"), MostRed_G);
dlg.m_blueMost.Format(_T("%d"), MostBlue_G);
dlg.m_greenMost.Format(_T("%d"), MostGreen_G);
dlg.m_redF.Format(_T("%d"), MostRed);
dlg.m_blueF.Format(_T("%d"), MostBlue);
dlg.m_greenF.Format(_T("%d"), MostGreen);
dlg.m_redXS.Format(_T("%d"),m_nImage);
dlg.m_greenXS.Format(_T("%d"),m_nImage);
dlg.m_blueXS.Format(_T("%d"),m_nImage);
float pinRedHD,pinGreenHD,pinBlueHD;
pinRedHD=sumRedHD*3/m_nImage;
pinGreenHD=sumGreenHD*3/m_nImage;
pinBlueHD=sumBlueHD*3/m_nImage;
dlg.m_redPJHD.Format(_T("%.2f"),pinRedHD);
dlg.m_greenPJHD.Format(_T("%.2f"),pinGreenHD);
dlg.m_bluePJHD.Format(_T("%.2f"),pinBlueHD);
int sumRedZZHD=0,sumGreenZZHD=0,sumBlueZZHD=0;
int redZZHD,greenZZHD,blueZZHD;
for(i=0;i<256;i++)
{
sumRedZZHD=sumRedZZHD+Red[i];
if(sumRedZZHD>=m_nImage/6)
{
redZZHD=i;
break;
}
}
for(i=0;i<256;i++)
{
sumGreenZZHD=sumGreenZZHD+Green[i];
if(sumGreenZZHD>=m_nImage/6)
{
greenZZHD=i;
break;
}
}
for(i=0;i<256;i++)
{
sumBlueZZHD=sumBlueZZHD+Blue[i];
if(sumBlueZZHD>=m_nImage/6)
{
blueZZHD=i;
break;
}
}
dlg.m_redZZHD.Format(_T("%d"),redZZHD);
dlg.m_greenZZHD.Format(_T("%d"),greenZZHD);
dlg.m_blueZZHD.Format(_T("%d"),blueZZHD);
float redBZC,greenBZC,blueBZC;
double redFC=0,greenFC=0,blueFC=0;
for(i=0;i<256;i++)
{
redFC=redFC+(pinRedHD-i)*(pinRedHD-i)*Red[i];
greenFC=greenFC+(pinGreenHD-i)*(pinGreenHD-i)*Green[i];
blueFC=blueFC+(pinBlueHD-i)*(pinBlueHD-i)*Blue[i];
}
redBZC=sqrt(redFC*3/m_nImage);
greenBZC=sqrt(greenFC*3/m_nImage);
blueBZC=sqrt(blueFC*3/m_nImage);
dlg.m_redBZC.Format(_T("%.2lf"),redBZC);
dlg.m_greenBZC.Format(_T("%.2lf"),greenBZC);
dlg.m_blueBZC.Format(_T("%.2lf"),blueBZC);
if(dlg.DoModal()==IDOK)
{
}
}
void CTest2View::OnJhh()
{
CFile fpo(BmpName,CFile::modeRead | CFile::typeBinary );
fpo.Read(&bfh, sizeof(BITMAPFILEHEADER));
fpo.Read(&bih, sizeof(BITMAPINFOHEADER));
int i, j, k;
for (j = 0;j < 256;j++) {
Red[j] = 0;
Green[j] = 0;
Blue[j] = 0;
}
unsigned char red, green, blue;
int IntRed, IntGreen, IntBlue;
double sumRedHD = 0, sumGreenHD = 0, sumBlueHD = 0;
int bytesPerLine = ((bih.biWidth * bih.biBitCount + 31) / 32) * 4;
int real_size = bytesPerLine * bih.biHeight;
int m_nImage = real_size;
for (i = 0; i < m_nImage / 3; i++)
{
fpo.Read(&red, sizeof(char));
IntRed = int(red);
sumRedHD = sumRedHD + IntRed;
if (IntRed >= 0 && IntRed < 256) Red[IntRed]++;
fpo.Read(&green, sizeof(char));
IntGreen = int(green);
sumGreenHD = sumGreenHD + IntGreen;
if (IntGreen >= 0 && IntGreen < 256) Green[IntGreen]++;
fpo.Read(&blue, sizeof(char));
IntBlue = int(blue);
sumBlueHD = sumBlueHD + IntBlue;
if (IntBlue >= 0 && IntBlue < 256) Blue[IntBlue]++;
}
fpo.Close();
float CountRed[256], CountGreen[256], CountBlue[256];
float CountRedLin[256], CountGreenLin[256], CountBlueLin[256];
for (k = 0; k < 256; k++)
{
CountRed[k] = (float)(Red[k]) * 3 / m_nImage;
CountRedLin[k] = CountRed[k];
CountGreen[k] = (float)(Green[k]) * 3 / m_nImage;
CountGreenLin[k] = CountGreen[k];
CountBlue[k] = (float)(Blue[k]) * 3 / m_nImage;
CountBlueLin[k] = CountBlue[k];
}
for (k = 1; k < 256; k++)
{
CountRed[k] = CountRed[k] + CountRed[k - 1];
CountGreen[k] = CountGreen[k] + CountGreen[k - 1];
CountBlue[k] = CountBlue[k] + CountBlue[k - 1];
}
int LRed[256], LGreen[256], LBlue[256];
for (k = 0; k < 256; k++)
{
LRed[k] = (int)(CountRed[k] * (256 - 1) + 0.5);
LGreen[k] = (int)(CountGreen[k] * (256 - 1) + 0.5);
LBlue[k] = (int)(CountBlue[k] * (256 - 1) + 0.5);
}
CString BmpNameJHH = BmpName.Left(BmpName.GetLength() - 4) + _T("JHH.bmp");
MessageBox(L"檔名為:" + BmpNameJHH + L"\n原始檔名為:" + BmpName, L"(路徑提示)", MB_OK);
MessageBox(L"處理中,請稍候", L"提示", MB_OK);
fpo.Open(BmpName, CFile::modeRead | CFile::typeBinary);
CFile fpw(BmpNameJHH, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite);
fpo.Read(&bfh, sizeof(BITMAPFILEHEADER));
fpw.Write(&bfh, sizeof(BITMAPFILEHEADER));
int panelsize = 0;
int width, height, count;
fpo.Read(&bih, sizeof(BITMAPINFOHEADER));
fpw.Write(&bih, sizeof(BITMAPINFOHEADER));
width = bih.biWidth;
height = bih.biHeight;
count = bih.biBitCount;
debugMess.Format(_T("%d"), sizeof(RGBQUAD));
if (count < 24)
{
panelsize = pow((double)2, count);
debugMess.Format(_T("此圖為%d位點陣圖,不是24位真彩色影像"), panelsize);
MessageBox(debugMess, L"提示", MB_OK);
}
bytesPerLine = ((width * count + 31) / 32) * 4;
real_size = bytesPerLine * height;
m_nImage = real_size;
for (i = 0; i < m_nImage / 3; i++)
{
fpo.Read(&red, sizeof(char));
fpo.Read(&green, sizeof(char));
fpo.Read(&blue, sizeof(char));
red = LRed[int(red)];
green = LGreen[int(green)];
blue = LBlue[int(blue)];
fpw.Write(&red, sizeof(char));
fpw.Write(&green, sizeof(char));
fpw.Write(&blue, sizeof(char));
}
fpw.Close();
fpo.Close();
MessageBox(L"處理完畢,請在根目錄檢視", L"提示", MB_OK);
Invalidate();
}
void CTest2View::OnGgh()
{
}
void CTest2View::OnEzh()
{
CString BmpNameEzh = BmpName.Left(BmpName.GetLength()-4) + _T("EZH.bmp");
MessageBox(L"檔名為:"+BmpNameEzh+L"\n原始檔名為:"+BmpName ,L"(路徑提示)", MB_OK );
MessageBox(L"處理中,請稍候", L"(提示)", MB_OK);
CFile fpo(BmpName, CFile::modeRead | CFile::typeBinary);
CFile fpw(BmpNameEzh, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite);
fpo.Read(&bfh, sizeof(BITMAPFILEHEADER));
fpo.Read(&bih, sizeof(BITMAPINFOHEADER));
fpw.Write(&bfh, sizeof(BITMAPFILEHEADER));
fpw.Write(&bih, sizeof(BITMAPINFOHEADER));
int bytesPerLine = ((bih.biWidth * bih.biBitCount + 31) / 32) * 4;
int real_size = bytesPerLine * bih.biHeight;
int m_nImage = real_size;
unsigned char color;
unsigned char red, green, blue;
for (int i = 0; i < m_nImage / 3; i++)
{
fpo.Read(&red, sizeof(char));
fpo.Read(&green, sizeof(char));
fpo.Read(&blue, sizeof(char));
if ((int)red > 128)
red = 255;
else
red = 0;
if ((int)green > 128)
green = 255;
else
green = 0;
if ((int)blue > 128)
blue = 255;
else
blue = 0;
fpw.Write(&red, sizeof(char));
fpw.Write(&green, sizeof(char));
fpw.Write(&blue, sizeof(char));
}
fpo.Close();
fpw.Close();
MessageBox(L"處理完畢,請在根目錄檢視", L"提示", MB_OK);
Invalidate();
}