dibview.cpp
资源名称:lab5.rar [点击查看]
上传用户:zbjinju
上传日期:2022-07-30
资源大小:11893k
文件大小:20k
源码类别:
图形图象
开发平台:
Visual C++
- // dibview.cpp : implementation of the CDibView class
- //
- // This is a part of the Microsoft Foundation Classes C++ library.
- // Copyright (C) 1992-1998 Microsoft Corporation
- // All rights reserved.
- //
- // This source code is only intended as a supplement to the
- // Microsoft Foundation Classes Reference and related
- // electronic documentation provided with the library.
- // See these sources for detailed information regarding the
- // Microsoft Foundation Classes product.
- #include "stdafx.h"
- #include "diblook.h"
- #include "dibdoc.h"
- #include "dibview.h"
- #include "dibapi.h"
- #include "mainfrm.h"
- #include "BitmapInfoDlg.h"
- #include "ThresholdDialog.h"
- #include "DlgHistogram.h"
- #include <math.h>
- #define PI 3.14
- #include <afxtempl.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
- #define BEGIN_PROCESSING() INCEPUT_PRELUCRARI()
- #define END_PROCESSING(Title) SFARSIT_PRELUCRARI(Title)
- #define INCEPUT_PRELUCRARI()
- CDibDoc* pDocSrc=GetDocument();
- CDocTemplate* pDocTemplate=pDocSrc->GetDocTemplate();
- CDibDoc* pDocDest=(CDibDoc*) pDocTemplate->CreateNewDocument();
- BeginWaitCursor();
- HDIB hBmpSrc=pDocSrc->GetHDIB();
- HDIB hBmpDest = (HDIB)::CopyHandle((HGLOBAL)hBmpSrc);
- if ( hBmpDest==0 ) {
- pDocTemplate->RemoveDocument(pDocDest);
- return;
- }
- BYTE* lpD = (BYTE*)::GlobalLock((HGLOBAL)hBmpDest);
- BYTE* lpS = (BYTE*)::GlobalLock((HGLOBAL)hBmpSrc);
- int iColors = DIBNumColors((char *)&(((LPBITMAPINFO)lpD)->bmiHeader));
- RGBQUAD *bmiColorsDst = ((LPBITMAPINFO)lpD)->bmiColors;
- RGBQUAD *bmiColorsSrc = ((LPBITMAPINFO)lpS)->bmiColors;
- BYTE * lpDst = (BYTE*)::FindDIBBits((LPSTR)lpD);
- BYTE * lpSrc = (BYTE*)::FindDIBBits((LPSTR)lpS);
- int dwWidth = ::DIBWidth((LPSTR)lpS);
- int dwHeight = ::DIBHeight((LPSTR)lpS);
- int w=WIDTHBYTES(dwWidth*((LPBITMAPINFOHEADER)lpS)->biBitCount);
- #define BEGIN_SOURCE_PROCESSING
- CDibDoc* pDocSrc=GetDocument();
- BeginWaitCursor();
- HDIB hBmpSrc=pDocSrc->GetHDIB();
- BYTE* lpS = (BYTE*)::GlobalLock((HGLOBAL)hBmpSrc);
- int iColors = DIBNumColors((char *)&(((LPBITMAPINFO)lpS)->bmiHeader));
- RGBQUAD *bmiColorsSrc = ((LPBITMAPINFO)lpS)->bmiColors;
- BYTE * lpSrc = (BYTE*)::FindDIBBits((LPSTR)lpS);
- int dwWidth = ::DIBWidth((LPSTR)lpS);
- int dwHeight = ::DIBHeight((LPSTR)lpS);
- int w=WIDTHBYTES(dwWidth*((LPBITMAPINFOHEADER)lpS)->biBitCount);
- #define END_SOURCE_PROCESSING
- ::GlobalUnlock((HGLOBAL)hBmpSrc);
- EndWaitCursor();
- /////////////////////////////////////////////////////////////////////////////
- //---------------------------------------------------------------
- #define SFARSIT_PRELUCRARI(Titlu)
- ::GlobalUnlock((HGLOBAL)hBmpDest);
- ::GlobalUnlock((HGLOBAL)hBmpSrc);
- EndWaitCursor();
- pDocDest->SetHDIB(hBmpDest);
- pDocDest->InitDIBData();
- pDocDest->SetTitle((LPCSTR)Titlu);
- CFrameWnd* pFrame=pDocTemplate->CreateNewFrame(pDocDest,NULL);
- pDocTemplate->InitialUpdateFrame(pFrame,pDocDest);
- /////////////////////////////////////////////////////////////////////////////
- // CDibView
- IMPLEMENT_DYNCREATE(CDibView, CScrollView)
- BEGIN_MESSAGE_MAP(CDibView, CScrollView)
- //{{AFX_MSG_MAP(CDibView)
- ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
- ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
- ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
- ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
- ON_MESSAGE(WM_DOREALIZE, OnDoRealize)
- ON_COMMAND(ID_PROCESSING_PARCURGERESIMPLA, OnProcessingParcurgereSimpla)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
- ON_COMMAND(ID_PROCESSING_LAB1, &CDibView::OnProcessingLab1)
- ON_COMMAND(ID_LAB2_BMPINFO, &CDibView::OnLab2Bmpinfo)
- ON_COMMAND(ID_LAB2_GRAYSCALE, &CDibView::OnLab2Grayscale)
- ON_COMMAND(ID_LAB2_SORTPALETTE, &CDibView::OnLab2Sortpalette)
- ON_COMMAND(ID_LAB2_BLACKANDWHITE, &CDibView::OnLab2Blackandwhite)
- ON_COMMAND(ID_LAB3_HISTOGRAM, &CDibView::OnLab3Histogram)
- ON_COMMAND(ID_LAB3_PRAGURIMULTIPLE, &CDibView::OnLab3Pragurimultiple)
- ON_COMMAND(ID_LAB3_PRAGURIMULTIPLEFS, &CDibView::OnLab3Pragurimultiplefs)
- ON_COMMAND(ID_PROCESSING_LAB4, &CDibView::OnProcessingLab4)
- ON_COMMAND(ID_PROCESSING_LAB5, &CDibView::OnProcessingLab5)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDibView construction/destruction
- CDibView::CDibView()
- {
- }
- CDibView::~CDibView()
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDibView drawing
- void CDibView::OnDraw(CDC* pDC)
- {
- CDibDoc* pDoc = GetDocument();
- HDIB hDIB = pDoc->GetHDIB();
- if (hDIB != NULL)
- {
- LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
- int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
- int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
- ::GlobalUnlock((HGLOBAL) hDIB);
- CRect rcDIB;
- rcDIB.top = rcDIB.left = 0;
- rcDIB.right = cxDIB;
- rcDIB.bottom = cyDIB;
- CRect rcDest;
- if (pDC->IsPrinting()) // printer DC
- {
- // get size of printer page (in pixels)
- int cxPage = pDC->GetDeviceCaps(HORZRES);
- int cyPage = pDC->GetDeviceCaps(VERTRES);
- // get printer pixels per inch
- int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
- int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
- //
- // Best Fit case -- create a rectangle which preserves
- // the DIB's aspect ratio, and fills the page horizontally.
- //
- // The formula in the "->bottom" field below calculates the Y
- // position of the printed bitmap, based on the size of the
- // bitmap, the width of the page, and the relative size of
- // a printed pixel (cyInch / cxInch).
- //
- rcDest.top = rcDest.left = 0;
- rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
- / ((double)cxDIB * cxInch));
- rcDest.right = cxPage;
- }
- else // not printer DC
- {
- rcDest = rcDIB;
- }
- ::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
- &rcDIB, pDoc->GetDocPalette());
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDibView printing
- BOOL CDibView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDibView commands
- LRESULT CDibView::OnDoRealize(WPARAM wParam, LPARAM)
- {
- ASSERT(wParam != NULL);
- CDibDoc* pDoc = GetDocument();
- if (pDoc->GetHDIB() == NULL)
- return 0L; // must be a new document
- CPalette* pPal = pDoc->GetDocPalette();
- if (pPal != NULL)
- {
- CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
- ASSERT_KINDOF(CMainFrame, pAppFrame);
- CClientDC appDC(pAppFrame);
- // All views but one should be a background palette.
- // wParam contains a handle to the active view, so the SelectPalette
- // bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
- CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
- if (oldPalette != NULL)
- {
- UINT nColorsChanged = appDC.RealizePalette();
- if (nColorsChanged > 0)
- pDoc->UpdateAllViews(NULL);
- appDC.SelectPalette(oldPalette, TRUE);
- }
- else
- {
- TRACE0("tSelectPalette failed in CDibView::OnPaletteChangedn");
- }
- }
- return 0L;
- }
- void CDibView::OnInitialUpdate()
- {
- CScrollView::OnInitialUpdate();
- ASSERT(GetDocument() != NULL);
- SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
- }
- void CDibView::OnActivateView(BOOL bActivate, CView* pActivateView,
- CView* pDeactiveView)
- {
- CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
- if (bActivate)
- {
- ASSERT(pActivateView == this);
- OnDoRealize((WPARAM)m_hWnd, 0); // same as SendMessage(WM_DOREALIZE);
- }
- }
- void CDibView::OnEditCopy()
- {
- CDibDoc* pDoc = GetDocument();
- // Clean clipboard of contents, and copy the DIB.
- if (OpenClipboard())
- {
- BeginWaitCursor();
- EmptyClipboard();
- SetClipboardData (CF_DIB, CopyHandle((HANDLE) pDoc->GetHDIB()) );
- CloseClipboard();
- EndWaitCursor();
- }
- }
- void CDibView::OnUpdateEditCopy(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
- }
- void CDibView::OnEditPaste()
- {
- HDIB hNewDIB = NULL;
- if (OpenClipboard())
- {
- BeginWaitCursor();
- hNewDIB = (HDIB) CopyHandle(::GetClipboardData(CF_DIB));
- CloseClipboard();
- if (hNewDIB != NULL)
- {
- CDibDoc* pDoc = GetDocument();
- pDoc->ReplaceHDIB(hNewDIB); // and free the old DIB
- pDoc->InitDIBData(); // set up new size & palette
- pDoc->SetModifiedFlag(TRUE);
- SetScrollSizes(MM_TEXT, pDoc->GetDocSize());
- OnDoRealize((WPARAM)m_hWnd,0); // realize the new palette
- pDoc->UpdateAllViews(NULL);
- }
- EndWaitCursor();
- }
- }
- void CDibView::OnUpdateEditPaste(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(::IsClipboardFormatAvailable(CF_DIB));
- }
- void CDibView::OnProcessingParcurgereSimpla()
- {
- // TODO: Add your command handler code here
- BEGIN_PROCESSING();
- // Makes a grayscale image by equalizing the R, G, B components from the LUT
- for (int k=0; k < iColors ; k++)
- bmiColorsDst[k].rgbRed=bmiColorsDst[k].rgbGreen=bmiColorsDst[k].rgbBlue=k;
- // Goes through the bitmap pixels and performs their negative
- for (int i=0;i<dwHeight;i++)
- for (int j=0;j<dwWidth;j++)
- {
- lpDst[i*w+j]= 255 - lpSrc[i*w+j]; //makes image negative
- }
- END_PROCESSING("Operation name");
- }
- void CDibView::OnProcessingLab1()
- {
- BEGIN_PROCESSING();
- // Makes a grayscale image by equalizing the R, G, B components from the LUT
- //for (int k=0; k < iColors ; k++)
- // bmiColorsDst[k].rgbRed=bmiColorsDst[k].rgbGreen=bmiColorsDst[k].rgbBlue=k;
- // Goes through the bitmap pixels and performs their negative
- for (int i=0;i<dwHeight;i++)
- for (int j=0;j<dwWidth;j++)
- {
- lpDst[i*w+j]= 5*lpSrc[i*w+j]; //makes image negative
- }
- END_PROCESSING("lab1");
- }
- void CDibView::OnLab2Bmpinfo()
- {
- BEGIN_SOURCE_PROCESSING;
- CBitmapInfoDlg dlgBmpHeader;
- LPBITMAPINFO pBitmapInfoSrc = (LPBITMAPINFO) lpS;
- CString buffer;
- for(int i=0;i<iColors;++i){
- buffer.Format("%3d. t%3dt%3dt%3drn",i,
- bmiColorsSrc[i].rgbRed,
- bmiColorsSrc[i].rgbGreen,
- bmiColorsSrc[i].rgbBlue
- );
- dlgBmpHeader.m_TEXT += buffer;
- }
- dlgBmpHeader.DoModal();
- END_SOURCE_PROCESSING;
- }
- void CDibView::OnLab2Grayscale()
- {
- BEGIN_PROCESSING();
- for (int k=0; k < iColors ; k++){
- int mean = (bmiColorsDst[k].rgbRed + bmiColorsDst[k].rgbGreen + bmiColorsDst[k].rgbBlue)/3;
- bmiColorsDst[k].rgbRed=bmiColorsDst[k].rgbGreen=bmiColorsDst[k].rgbBlue=mean;
- }
- END_PROCESSING("GrayScale");
- }
- void CDibView::OnLab2Sortpalette()
- {
- BEGIN_PROCESSING();
- int g[256];
- for (int k=0; k < iColors ; k++){
- g[k] = bmiColorsDst[k].rgbRed;
- bmiColorsDst[k].rgbRed=bmiColorsDst[k].rgbGreen=bmiColorsDst[k].rgbBlue=k;
- }
- for (int i=0;i<dwHeight;i++)
- for (int j=0;j<dwWidth;j++){
- int k = lpDst[i*w+j];
- lpDst[i*w+j]= g[k];
- }
- END_PROCESSING("SortPalette");
- }
- void CDibView::OnLab2Blackandwhite()
- {
- BYTE threshold;
- CThresholdDialog dlgThresh;
- if(dlgThresh.DoModal() == IDOK){
- threshold = dlgThresh.m_thresh;
- BEGIN_PROCESSING();
- for (int i=0;i<dwHeight;i++)
- for (int j=0;j<dwWidth;j++){
- if(lpDst[i*w+j]<threshold)
- lpDst[i*w+j]=0;
- else
- lpDst[i*w+j]=255;
- }
- END_PROCESSING("Black and White");
- }
- }
- void CDibView::OnLab3Histogram()
- {
- BEGIN_SOURCE_PROCESSING;
- int histValues[256];
- float FDPValues[256];
- int i,j;
- for(i=0;i<256;++i){
- histValues[i] = 0;
- }
- for (i=0;i<dwHeight;i++)
- for (j=0;j<dwWidth;j++){
- histValues[ lpSrc[i*w+j] ]++;
- }
- int m = dwHeight * dwWidth;
- for(i=0; i<256; ++i)
- FDPValues[i] = (float)histValues[i]/m;
- CDlgHistogram dlg;
- memcpy(dlg.m_Histogram.values,histValues,sizeof(histValues));
- dlg.DoModal();
- END_SOURCE_PROCESSING;
- }
- //cel mai apropiat maxim din histograma
- int closestMax(int *h, int k){
- if(h[k] == 1)
- return k;
- int i=1;
- while(1){
- if(k-i < 0)
- return 0;
- if(h[k-i] == 1)
- return k-i;
- if(k+i > 255)
- return 255;
- if(h[k+i] == 1)
- return k+i;
- ++i;
- }
- return 0;
- }
- void CDibView::OnLab3Pragurimultiple()
- {
- int WH = 5;
- double TH = 0.0003;
- BEGIN_PROCESSING();
- int histValues[256];
- double FDPValues[256];
- int locMax[256];
- int i,j,k;
- for(i=0;i<256;++i){
- histValues[i] = 0;
- locMax[i] = 0;
- }
- for (i=0;i<dwHeight;i++)
- for (j=0;j<dwWidth;j++){
- histValues[ lpSrc[i*w+j] ]++;
- }
- int m = dwHeight * dwWidth;
- for(i=0; i<256; ++i)
- FDPValues[i] = (double)histValues[i]/m;
- //calculam maximii locali
- //locMax va contine 1 dak e maxim local, 0 dak nu
- locMax[0] = 1;
- locMax[255] = 1;
- double v,max;
- for(k = WH; k<255-WH; ++k){
- v = 0.0;
- max = FDPValues[k-WH];
- for(i=k-WH; i<=k+WH; ++i){
- v += FDPValues[i];
- if(FDPValues[i] > max)
- max = FDPValues[i];
- }
- v /= (2*WH+1);
- if(FDPValues[k] > v+TH && FDPValues[k] >= max)
- locMax[k] = 1;
- }
- for (int i=0;i<dwHeight;i++)
- for (int j=0;j<dwWidth;j++){
- k = lpDst[i*w+j];
- lpDst[i*w+j]= closestMax(locMax, k);
- }
- END_PROCESSING("PraguriMultiple");
- }
- void CDibView::OnLab3Pragurimultiplefs()
- {
- int WH = 5;
- double TH = 0.0003;
- BEGIN_PROCESSING();
- int histValues[256];
- double FDPValues[256];
- int locMax[256];
- int i,j,k;
- for(i=0;i<256;++i){
- histValues[i] = 0;
- locMax[i] = 0;
- }
- for (i=0;i<dwHeight;i++)
- for (j=0;j<dwWidth;j++){
- histValues[ lpSrc[i*w+j] ]++;
- }
- int m = dwHeight * dwWidth;
- for(i=0; i<256; ++i)
- FDPValues[i] = (double)histValues[i]/m;
- //calculam maximii locali
- //locMax va contine 1 dak e maxim local, 0 dak nu
- locMax[0] = 1;
- locMax[255] = 1;
- double v,max;
- for(k = WH; k<255-WH; ++k){
- v = 0.0;
- max = FDPValues[k-WH];
- for(i=k-WH; i<=k+WH; ++i){
- v += FDPValues[i];
- if(FDPValues[i] > max)
- max = FDPValues[i];
- }
- v /= (2*WH+1);
- if(FDPValues[k] > v+TH && FDPValues[k] >= max)
- locMax[k] = 1;
- }
- int pixel_vechi, pixel_nou, eroare;
- BYTE *source = (BYTE*)malloc(dwHeight * w * sizeof BYTE);
- memcpy(source, lpSrc, dwHeight * w * sizeof BYTE);
- for (int i=0;i<dwHeight;i++)
- for (int j=0;j<dwWidth;j++){
- pixel_vechi = source[i*w+j];
- pixel_nou = closestMax(locMax, pixel_vechi);
- lpDst[i*w+j] = pixel_nou;
- eroare = pixel_vechi - pixel_nou;
- if(j < (dwWidth-1))
- source[i*w+(j+1)] = source[i*w+(j+1)] + 7*eroare/16;
- if(i < (dwHeight-1) && j > 0)
- source[(i+1)*w+(j-1)] = source[(i+1)*w+(j-1)] + 3*eroare/16;
- if(i < (dwHeight-1))
- source[(i+1)*w+j] = source[(i+1)*w+j] + 5*eroare/16;
- if(i < (dwHeight-1) && j < (dwWidth-1))
- source[(i+1)*w+(j+1)] = source[(i+1)*w+(j+1)] + eroare/16;
- }
- free(source);
- END_PROCESSING("PraguriMultiple Floyd-Steinberg");
- }
- void CDibView::OnProcessingLab4()
- {
- BEGIN_PROCESSING();
- CString msg;
- int A=0;
- int perim=0;
- int ri,ci;
- float arct;
- float x,y,x1,x2;
- int E=0;
- int cmax=0,cmin=1000,rmax=0,rmin=1000;
- //aria
- for (int i=1;i<dwHeight;i++)
- for (int j=1;j<dwWidth; j++)
- if (lpSrc[i*w+j]==0){
- A++;
- }
- //centrul de masa
- int xcm=0;
- for(int i=0;i<dwHeight;i++)
- for(int j=0; j<dwWidth;j++)
- if (lpSrc[i*w+j]==0)
- xcm=xcm+i;
- xcm=xcm/A;
- int ycm=0;
- for(int i=0;i<dwHeight;i++)
- for(int j=0; j<dwWidth;j++)
- if (lpSrc[i*w+j]==0)
- ycm=ycm+j;
- ycm=ycm/A;
- lpDst[xcm*w+ycm]=2;
- bmiColorsDst[2].rgbRed=255;
- bmiColorsDst[2].rgbGreen=0;
- bmiColorsDst[2].rgbBlue=0;
- bmiColorsDst[3].rgbRed=0;
- bmiColorsDst[3].rgbGreen=255;
- bmiColorsDst[3].rgbBlue=0;
- bmiColorsDst[4].rgbRed=0;
- bmiColorsDst[4].rgbGreen=0;
- bmiColorsDst[4].rgbBlue=255;
- //ungiul axei
- for(int i=0;i<dwHeight;i++)
- for(int j=0;j<dwWidth;j++)
- if (lpSrc[i*w+j]==0)
- {
- y+=(i-xcm)*(j-ycm);
- x1+=(j-ycm)*(j-ycm);
- x2+=(i-xcm)*(i-xcm);
- }
- x=(x1-x2);
- arct =(180.0*atan2(y,x))/(2.0*PI);
- //Perimetrul
- for(int i=0;i<dwHeight;i++)
- for(int j=0;j<dwWidth;j++)
- {
- if (lpSrc[i*w+j]==0)
- {
- if ((lpSrc[(i+1)*w+j]>1) || (lpSrc[(i-1)*w+j]>1) || (lpSrc[i*w+(j+1)]>1) || (lpSrc[i*w+(j-1)]>1))
- {
- perim++;
- lpDst[i*w+j]=3;
- }
- }
- }
- // Elongatia
- for(int i=0;i<dwHeight;i++)
- for(int j=0;j<dwWidth;j++)
- if (lpSrc[i*w+j]==0)
- {
- if (i>rmax)
- rmax=i;
- if (i<rmin)
- rmin=i;
- if (j>cmax)
- cmax=j;
- if (j<cmin)
- cmin=j;
- }
- E=(cmax-cmin+1)/(rmax-rmin+1);
- // proiectiile
- int *HX=new int[dwHeight];
- memset(HX,0,sizeof(int)*dwHeight);
- int *WX=new int[dwWidth];
- memset(WX,0,sizeof(int)*dwWidth);
- for(int i=0;i<dwHeight;i++)
- for(int j=0;j<dwWidth;j++)
- {
- if (lpSrc[i*w+j]==0)
- HX[i]++;
- }
- for(int i=0;i<dwHeight;i++)
- for(int j=0;j<dwWidth;j++)
- {
- if (lpSrc[i*w+j]==0)
- WX[j]++;
- }
- for (int i=0;i<dwHeight;i++)
- for(int j=0;j<HX[i];j++)
- lpDst[i*w+j]=4;
- for (int i=0;i<dwWidth;i++)
- for(int j=0;j<WX[i];j++)
- lpDst[i*w+j]=4;
- msg.Format("Aria %dnCm(%d,%d)nO%fnP %dnE %dn",A,xcm,ycm,arct,perim,E);
- AfxMessageBox(msg);
- END_PROCESSING("Calcule");
- }
- void CDibView::OnProcessingLab5()
- {
- BEGIN_PROCESSING();
- int *Labels;
- Labels=new int[w*dwHeight];
- memset(Labels,0,(w*dwHeight)*sizeof(int));
- int crtLabel=1;
- CArray<CPoint,CPoint&>Pair;
- for(int i=dwHeight-2;i>=0;i--)
- for(int j=1;j<dwWidth-1;j++)
- {
- int B=lpSrc[(i+1)*w+(j-1)];
- int C=lpSrc[(i+1)*w+j];
- int D=lpSrc[(i+1)*w+(j+1)];
- int A=lpSrc[i*w+(j-1)];
- int X=lpSrc[i*w+j];
- if(X==0)
- {
- if(B!=0)
- if(A!=0)
- if(C!=0)
- if(D!=0)
- Labels[i*w+j]=crtLabel++;
- else
- Labels[i*w+j]=Labels[(i+1)*w+(j+1)];
- else
- Labels[i*w+j]=Labels[(i+1)*w+j];
- else
- if(C!=0)
- if(D!=0)
- Labels[i*w+j]=Labels[i*w+(j-1)];
- else
- {
- Labels[i*w+j]=Labels[i*w+(j-1)];
- Pair.Add(CPoint(Labels[i*w + (j-1)],Labels[(i+1)*w+ (j+1)]));
- }
- else
- {
- Labels[i*w+j]=Labels[i*w+(j-1)];
- Pair.Add(CPoint(Labels[i*w + (j-1)],Labels[(i+1)*w+ j]));
- }
- else if ((C!=0)&&(D==0))
- {
- Labels[i*w+j]=Labels[(i+1)*w+(j-1)];
- Pair.Add(CPoint(Labels[(i+1)*w+(j-1)],Labels[(i+1)*w+ (j+1)]));
- }
- else
- Labels[i*w+j]=Labels[(i+1)*w+(j-1)];
- }
- }
- int *Union;
- Union =new int[crtLabel];
- for (int i=0; i<crtLabel; i++)
- Union[i]=i;
- int la,lb;
- for (int i=0; i<Pair.GetSize(); i++)
- {
- la=Union[Pair[i].x];
- lb=Union[Pair[i].y];
- for (int i=0;i<crtLabel;i++)
- {
- if (Union[i]==la)
- Union[i]=lb;
- }
- }
- for(int i=dwHeight-2;i>=0;i--)
- for(int j=1;j<dwWidth-1;j++){
- if(Labels[i*w+j]>0)
- lpDst[i*w+j]=Union[Labels[i*w+j]]%254+1;
- }
- for(int k=1;k<254;k++){
- bmiColorsDst[k].rgbRed=rand()%255;
- bmiColorsDst[k].rgbBlue=rand()%255;
- bmiColorsDst[k].rgbGreen=rand()%255;
- }
- END_PROCESSING ("Lab 5");
- }