📄 ed256view.cpp
字号:
m_crNoPlot = RGB(255,255,255);
} else {
m_crPlot = RGB(255,255,255);
m_crNoPlot = RGB(0,0,0);
}
}
void CED256View::OnFilter() {
CFilterDlg dlg;
dlg.SetFilter(&m_fFilter[0]);
dlg.DoModal();
m_fFilter[0] = dlg.m_fEdit1;
m_fFilter[1] = dlg.m_fEdit2;
m_fFilter[2] = dlg.m_fEdit3;
m_fFilter[3] = dlg.m_fEdit4;
m_fFilter[4] = dlg.m_fEdit5;
m_fFilter[5] = dlg.m_fEdit6;
m_fFilter[6] = dlg.m_fEdit7;
m_fFilter[7] = dlg.m_fEdit8;
m_fFilter[8] = dlg.m_fEdit9;
}
void CED256View::OnUpdateColourtype(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_bColour);
}
void CED256View::OnUpdateInverse(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_bInverse);
}
void CED256View::OnEditOutputBlackandwhite() {
m_bColour = false;
}
void CED256View::OnEditOutputColour() {
m_bColour = true;
}
void CED256View::OnInitialUpdate() {
CView::OnInitialUpdate();
if ((HBITMAP)(m_bmpEdges)) m_bmpEdges.Detach();
}
BOOL CED256View::OnEraseBkgnd(CDC* pDC) {
return FALSE;
}
void CED256View::OnEditPrototyping() {
CWaitCursor wait;
CMainFrame* pMfm = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
m_iOffset = 50;
int thresh = pMfm->m_iThreshold;
CDC sc1,sc2;
CClientDC dc(this);
sc1.CreateCompatibleDC(&dc);
sc2.CreateCompatibleDC(&dc);
CBitmap *src1old, bmsrc, *src2old;
int cxDIB, cyDIB; // The width/height of the bitmap
CED256Doc* pDoc = GetDocument();
HDIB hDIB = pDoc->GetHDIB();
GetDIBSize(cxDIB, cyDIB);
if ((HBITMAP)(m_bmpEdges)) m_bmpEdges.Detach();
bmsrc.CreateCompatibleBitmap(&dc, cxDIB, cyDIB);
m_bmpEdges.CreateCompatibleBitmap(&dc, cxDIB, cyDIB + 100);
src1old = sc1.SelectObject(&bmsrc);
src2old = sc2.SelectObject(&m_bmpEdges);
::PaintDIB(sc1.m_hDC, &CRect(0,0,cxDIB,cyDIB), hDIB,
&CRect(0,0,cxDIB,cyDIB), pDoc->GetDocPalette());
::PaintDIB(sc2.m_hDC, &CRect(0,0,cxDIB,cyDIB), hDIB,
&CRect(0,0,cxDIB,cyDIB), pDoc->GetDocPalette());
////////////////////////////////////////////////
// START PROTOTYPING
_protoBmp samples[MAX_SAMPLES];
_protoBmp prototypes[MAX_PROTOTYPES];
int x,y;
srand(unsigned(time(NULL)));
pMfm->SetStatusMessage("Creating samples and prototypes...");
// Initialize the samples
for (int i=0;i<MAX_SAMPLES;i++) {
x = rand() % cxDIB;
y = rand() % cyDIB;
for (int j=0;j<15;j++) {
for (int k=0;k<15;k++) {
samples[i].pixels[j][k] = char(sc1.GetPixel(x+j,y+k) & 0xFF);
}
}
}
// Initialize the prototypes
for (i=0;i<MAX_PROTOTYPES;i++) {
for (int j=0;j<15;j++) {
for (int k=0;k<15;k++) {
prototypes[i].pixels[j][k] = rand() % 255;
}
}
}
pMfm->SetStatusMessage("Competitive learning initialized...");
int ind, k = 2, avg, bestavg, bestind, pavg[MAX_PROTOTYPES];
for (i=0;i<MAX_PITERATIONS;i++) {
memset(pavg,0,sizeof(avg));
ind = rand() % MAX_SAMPLES;
for (int j=0;j<MAX_PROTOTYPES;j++) {
avg = 0;
for (int k=0;k<15;k++) {
for (int l=0;l<15;l++) {
avg += abs(prototypes[j].pixels[k][l] - samples[ind].pixels[k][l]);
}
}
pavg[j] = avg / (15*15);
}
bestind = 0; bestavg = pavg[0];
for (j=1;j<MAX_PROTOTYPES;j++) {
if (pavg[j] < bestavg) {
bestind = j;
bestavg = pavg[j];
}
}
for (j=0;j<15;j++) {
for (int k=0;k<15;k++) {
prototypes[bestind].pixels[j][k] =
(prototypes[bestind].pixels[j][k] - samples[ind].pixels[j][k]) / 100 * k + samples[ind].pixels[j][k];
}
}
}
// Create the picture.
_protoBmp test;
int minus = (m_iResolution/2);
for (i=0;i<cxDIB+m_iResolution;i+=m_iResolution) {
for (int j=0;j<cyDIB+m_iResolution;j+=m_iResolution) {
// Get the pixels;
for (int k=-7;k<8;k++) {
for (int l=-7;l<8;l++) {
test.pixels[k+7][l+7] = char(sc1.GetPixel(i+k,j+l) & 0xFF);
}
}
for (int m=0;m<MAX_PROTOTYPES;m++) {
avg = 0;
for (int k=0;k<15;k++) {
for (int l=0;l<15;l++) {
avg += abs(prototypes[m].pixels[k][l] - test.pixels[k][l]);
}
}
pavg[m] = avg / (15*15);
}
bestind = 0; bestavg = pavg[0];
for (m=1;m<MAX_PROTOTYPES;m++) {
if (pavg[m] < bestavg) {
bestind = m;
bestavg = pavg[m];
}
}
sc2.FillSolidRect(i-minus,j-minus,m_iResolution,m_iResolution,m_crProtoColours[bestind]);
}
pMfm->PercentComplete(i/float(cxDIB)*100);
}
// Draw the prototypes and colours
sc2.FillSolidRect(0,cyDIB,cxDIB,m_iOffset,RGB(255,255,255));
for (i=0;i<MAX_PROTOTYPES;i++) {
for (int j=0;j<15;j++) {
for (int k=0;k<15;k++) {
BYTE col = prototypes[i].pixels[j][k];
sc2.SetPixel(j+(i*16),k+cyDIB+5,RGB(col,col,col));
}
}
sc2.FillSolidRect(i*16,20+cyDIB+5,15,5,m_crProtoColours[i]);
}
CBrush br(RGB(0,0,0));
sc2.FrameRect(&CRect(0,0,cxDIB, cyDIB), &br);
sc1.SelectObject(src1old);
sc2.SelectObject(src2old);
Invalidate();
pMfm->SetStatusMessage(AFX_IDS_IDLEMESSAGE);
}
void CED256View::OnChangeResolution() {
int bold;
switch (m_iResolution) {
case 15: bold = 0; break;
case 4: bold = 1; break;
case 1: bold = 2; break;
}
CMainFrame* pMfm = STATIC_DOWNCAST(CMainFrame, AfxGetMainWnd());
PullDownToolBar(&(pMfm->m_wndProtoBar), ID_CHANGERESOLUTION, IDR_RESOLUTION, bold);
}
void CED256View::OnResolution(UINT uID) {
switch (uID) {
case ID_RES_LOW: m_iResolution = 15; break;
case ID_RES_MEDIUM: m_iResolution = 4; break;
case ID_RES_HIGH: m_iResolution = 1; break;
}
}
void CED256View::CopyBitmap() {
CClientDC dc(this);
CDC bmpdc, memdc;
CBitmap bmp, *oldbmp1, *oldbmp2;
int cxDIB, cyDIB; // The width/height of the bitmap
GetDIBSize(cxDIB, cyDIB);
bmpdc.CreateCompatibleDC(GetDC());
memdc.CreateCompatibleDC(GetDC());
bmp.CreateCompatibleBitmap(&dc, cxDIB, cyDIB);
oldbmp1 = bmpdc.SelectObject(&m_bmpEdges);
oldbmp2 = memdc.SelectObject(&bmp);
CBrush br(RGB(0,0,0));
memdc.BitBlt(0,0,cxDIB,cyDIB,&bmpdc,0,0,SRCCOPY);
memdc.FrameRect(&CRect(0,0,cxDIB, cyDIB), &br);
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP, bmp.GetSafeHandle());
CloseClipboard();
bmp.Detach();
bmpdc.SelectObject(oldbmp1);
memdc.SelectObject(oldbmp2);
}
void CED256View::OnEditCopy() {
CopyBitmap();
}
// FIXME: The DIB size really should be a class member!
void CED256View::GetDIBSize(int &cx, int &cy) {
CED256Doc* pDoc = GetDocument();
HDIB hDIB = pDoc->GetHDIB();
if (hDIB) {
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
cx = (int) ::DIBWidth(lpDIB);
cy = (int) ::DIBHeight(lpDIB);
::GlobalUnlock((HGLOBAL) hDIB);
} else return;
}
// ON_COMMAND_UPDATE SCHTUFF
void CED256View::OnUpdateEditOutputBlackandwhite(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(!m_bColour);
}
void CED256View::OnUpdateEditOutputColour(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_bColour);
}
void CED256View::OnUpdateEditFormulatypeDifferenceand(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_iFormula == 0);
}
void CED256View::OnUpdateEditFormulatypeDifferenceor(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_iFormula == 1);
}
void CED256View::OnUpdateEditFormulatypeSummation(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_iFormula == 2);
}
void CED256View::OnUpdateResLow(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_iResolution == 15);
}
void CED256View::OnUpdateResMedium(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_iResolution == 4);
}
void CED256View::OnUpdateResHigh(CCmdUI* pCmdUI) {
pCmdUI->SetCheck(m_iResolution == 1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -