Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PO1Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "PO1.h"
- #include "PO1Dlg.h"
- #include "ParamsDlg.h"
- #include "afxdialogex.h"
- #include <array>
- #include <complex>
- #include <vector>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- #define IMG_WND_ID_IN 100
- #define IMG_WND_ID_OUT 101
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialogEx
- {
- public:
- CAboutDlg();
- // Dialog Data
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- // Implementation
- protected:
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
- {
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
- END_MESSAGE_MAP()
- // CPODlg dialog
- CPODlg::CPODlg(CWnd* pParent /*=NULL*/)
- : CDialogEx(CPODlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_pImgIN = m_pImgOUT = NULL;
- m_imageH = m_imageW = -1;
- }
- void CPODlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialogEx::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_COMBO1, m_combo1);
- }
- BEGIN_MESSAGE_MAP(CPODlg, CDialogEx)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON_LOAD, &CPODlg::OnBnClickedButtonLoad)
- ON_BN_CLICKED(IDC_BUTTON_PROCESS, &CPODlg::OnBnClickedButtonProcess)
- ON_BN_CLICKED(IDC_BUTTON_SAVE, &CPODlg::OnBnClickedButtonSave)
- ON_BN_CLICKED(IDC_BUTTON_PARAMS, &CPODlg::OnBnClickedButtonParams)
- ON_WM_DESTROY()
- END_MESSAGE_MAP()
- // CPODlg message handlers
- BOOL CPODlg::OnInitDialog()
- {
- CDialogEx::OnInitDialog();
- GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- BOOL bNameValid;
- CString strAboutMenu;
- bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
- ASSERT(bNameValid);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- CRect rDlg(7, 7, 407, 407);
- MapDialogRect(rDlg);
- m_imgWndIN.Create(rDlg, this, IMG_WND_ID_IN);
- rDlg = CRect(530, 7, 930, 407);
- MapDialogRect(rDlg);
- m_imgWndOUT.Create(rDlg, this, IMG_WND_ID_OUT);
- // OPCJE
- m_combo1.AddString(L"transform to greyscale");
- m_combo1.SelectString(0, L"transform to greyscale");
- m_combo1.AddString(L"dylatacjaB");
- m_combo1.AddString(L"dylatacja");
- m_combo1.AddString(L"erozja");
- m_combo1.AddString(L"zamkniecie");
- m_combo1.AddString(L"otwarcie");
- m_combo1.AddString(L"kontur wew");
- m_combo1.AddString(L"kontur zew");
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CPODlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialogEx::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CPODlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialogEx::OnPaint();
- }
- }
- // The system calls this function to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CPODlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CPODlg::OnBnClickedButtonLoad()
- {
- WCHAR strFilter[] = { L"Image Files (*.bmp; *.jpg; *.tiff; *.gif; *.png)|*.bmp; *.jpg; *.tiff; *.gif; *.png|All Files (*.*)|*.*||" };
- CFileDialog FileDlg(TRUE, NULL, NULL, 0, strFilter);
- if (FileDlg.DoModal() == IDOK)
- {
- delete m_pImgIN;
- ::delete m_pImgOUT;
- m_pImgOUT = NULL;
- m_pImgIN = Bitmap::FromFile(FileDlg.GetPathName());
- m_imgWndIN.SetImg(m_pImgIN);
- m_imageW = m_pImgIN->GetWidth();
- m_imageH = m_pImgIN->GetHeight();
- m_pImgOUT = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);// PixelFormat16bppGrayScale);
- m_imgWndOUT.SetImg(m_pImgOUT);
- Invalidate();
- }
- }
- void CPODlg::dylatacja(Bitmap* in, Bitmap* out)
- {
- for (int i = 0; i < m_imageW; i++)
- {
- for (int j = 0; j < m_imageH; j++)
- {
- BYTE mask[3][3] = { 255, 255, 255, 255, 255, 255, 255, 255, 255 };
- mask[1][1] = GetPixelXY(in, i, j);
- //Top left
- if (j - 1 >= 0 && i - 1 >= 0)
- mask[0][0] = GetPixelXY(in, i - 1, j - 1);
- //Top mid
- if (j - 1 >= 0)
- mask[1][0] = GetPixelXY(in, i, j - 1);
- //Top right
- if (j - 1 >= 0 && i + 1 < m_imageW)
- mask[2][0] = GetPixelXY(in, i + 1, j - 1);
- //Left
- if (i - 1 >= 0)
- mask[0][1] = GetPixelXY(in, i - 1, j);
- //Right
- if (i + 1 < m_imageW)
- mask[2][1] = GetPixelXY(in, i + 1, j);
- //Bottom left
- if (j + 1 < m_imageH && i - 1 >= 0)
- mask[0][2] = GetPixelXY(in, i - 1, j + 1);
- //Bottom mid
- if (j + 1 < m_imageH)
- mask[1][2] = GetPixelXY(in, i, j + 1);
- //Bottom right
- if (j + 1 < m_imageH && i + 1 < m_imageW)
- mask[2][2] = GetPixelXY(in, i + 1, j + 1);
- BYTE min = 255;
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- if (mask[x][y] < min)
- min = mask[x][y];
- }
- }
- SetPixelXY(out, i, j, min);
- }
- }
- }
- void CPODlg::dylatacjaB()
- {
- bool actualPix;
- for (int i = 0; i < m_imageW; i++)
- {
- for (int j = 0; j < m_imageH; j++)
- {
- SetPixelXY(i, j, 255);
- }
- }
- for (int i = 1; i < m_imageW - 1; i++)
- {
- for (int j = 1; j < m_imageH - 1; j++)
- {
- actualPix = GetPixelXY(i, j);
- if (!actualPix)
- {
- SetPixelXY(i - 1, j - 1, 0);
- SetPixelXY(i, j - 1, 0);
- SetPixelXY(i + 1, j - 1, 0);
- SetPixelXY(i - 1, j, 0);
- SetPixelXY(i + 1, j, 0);
- SetPixelXY(i, j, 0);
- SetPixelXY(i - 1, j + 1, 0);
- SetPixelXY(i, j + 1, 0);
- SetPixelXY(i + 1, j + 1, 0);
- }
- }
- }
- }
- void CPODlg::erozja(Bitmap* in, Bitmap* out)
- {
- bool actualPix;
- for (int i = 0; i < m_imageW; i++)
- {
- for (int j = 0; j < m_imageH; j++)
- {
- BYTE mask[3][3] = { 255, 255, 255, 255, 255, 255, 255, 255, 255 };
- mask[1][1] = GetPixelXY(in, i, j);
- //Top left
- if (j - 1 >= 0 && i - 1 >= 0)
- mask[0][0] = GetPixelXY(in, i - 1, j - 1);
- //Top mid
- if (j - 1 >= 0)
- mask[1][0] = GetPixelXY(in, i, j - 1);
- //Top right
- if (j - 1 >= 0 && i + 1 < m_imageW)
- mask[2][0] = GetPixelXY(in, i + 1, j - 1);
- //Left
- if (i - 1 >= 0)
- mask[0][1] = GetPixelXY(in, i - 1, j);
- //Right
- if (i + 1 < m_imageW)
- mask[2][1] = GetPixelXY(in, i + 1, j);
- //Bottom left
- if (j + 1 < m_imageH && i - 1 >= 0)
- mask[0][2] = GetPixelXY(in, i - 1, j + 1);
- //Bottom mid
- if (j + 1 < m_imageH)
- mask[1][2] = GetPixelXY(in, i, j + 1);
- //Bottom right
- if (j + 1 < m_imageH && i + 1 < m_imageW)
- mask[2][2] = GetPixelXY(in, i + 1, j + 1);
- BYTE max = 0;
- for (int x = 0; x < 3; x++)
- {
- for (int y = 0; y < 3; y++)
- {
- if (mask[x][y] > max)
- max = mask[x][y];
- }
- }
- SetPixelXY(out, i, j, max);
- }
- }
- }
- void CPODlg::otwarcie()
- {
- Bitmap* tmp = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);
- erozja(m_pImgIN, tmp);
- dylatacja(tmp, m_pImgOUT);
- ::delete tmp;
- }
- void CPODlg::zamkniecie()
- {
- Bitmap* tmp = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);
- dylatacja(m_pImgIN, tmp);
- erozja(tmp, m_pImgOUT);
- ::delete tmp;
- }
- void CPODlg::konturWew(Bitmap* in, Bitmap* out)
- {
- Bitmap* tmp = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);
- BYTE pix1;
- BYTE pix2;
- erozja(in, tmp);
- for (int i = 0; i < m_imageW; i++)
- {
- for (int j = 0; j < m_imageH; j++)
- {
- pix1 = GetPixelXY(in, i, j);
- pix2 = GetPixelXY(tmp, i, j);
- if (pix1 != pix2)
- {
- SetPixelXY(out, i, j, 0);
- }
- else
- {
- SetPixelXY(out, i, j, 255);
- }
- }
- }
- ::delete tmp;
- }
- void CPODlg::konturZew(Bitmap* in, Bitmap* out)
- {
- Bitmap* tmp = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);
- BYTE pix1;
- BYTE pix2;
- dylatacja(in, tmp);
- for (int i = 0; i < m_imageW; i++)
- {
- for (int j = 0; j < m_imageH; j++)
- {
- pix1 = GetPixelXY(in, i, j);
- pix2 = GetPixelXY(tmp, i, j);
- if (pix2 == 0 && pix1 != pix2)
- {
- SetPixelXY(out, i, j, 0);
- }
- else
- {
- SetPixelXY(out, i, j, 255);
- }
- }
- }
- ::delete tmp;
- }
- void CPODlg::OnBnClickedButtonProcess()
- {
- CString sOption;
- m_combo1.GetLBText(m_combo1.GetCurSel(), sOption);
- BeginWaitCursor();
- if (sOption == L"transform to greyscale")
- {
- for (int x = 0; x < m_imageW; x++)
- for (int y = 0; y < m_imageH; y++)
- {
- BYTE Js = GetPixelXY(x, y);
- SetPixelXY(x, y, Js);
- }
- }
- else if (sOption == L"dylatacjaB")
- {
- dylatacjaB();
- }
- else if (sOption == L"dylatacja")
- {
- dylatacja(m_pImgIN, m_pImgOUT);
- }
- else if (sOption == L"erozja")
- {
- erozja(m_pImgIN, m_pImgOUT);
- }
- else if (sOption == L"zamkniecie")
- {
- zamkniecie();
- }
- else if (sOption == L"otwarcie")
- {
- otwarcie();
- }
- else if (sOption == L"kontur wew")
- {
- konturWew(m_pImgIN, m_pImgOUT);
- }
- else if (sOption == L"kontur zew")
- {
- konturZew(m_pImgIN, m_pImgOUT);
- }
- /*********************************************************************************************************************************
- TU NALEŻY WSTAWIC OBSŁUGĘ POZOSTAŁYCH OPCJI
- Zmienne m_imageH i m_imageW zawierają informacje o wysokości i szerokości przetwarzanego obrazu.
- Funkcja GetPixelXY(x,y) zwraca wartość jasności piksela o współrzędnych (x,y) w obrazie źródłowym (w przypadku obrazów RGB następuje
- automatyczna konwersja na poziom szarości).
- Funkcja SetPixelXY(x,y,Js) ustawia w obrazie wynikowym jasność piksela o współrzędnych (x,y) na wartość Js.
- Wartości jasności są typu BYTE (0..255).
- Dodawanie opcji do listy rozwijanej zrealizowane jest na końcu kodu metody OnInitDialog().
- W metodzie OnBnClickedButtonParams() pobierany jest łańcuch znaków wpisany przez użytkownika w oknie pojawiającym się po naciśnięciu
- przycisku "Params".
- ***********************************************************************************************************************************/
- Invalidate();
- EndWaitCursor();
- }
- void CPODlg::OnBnClickedButtonSave()
- {
- WCHAR strFilter[] = { L"Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg||" };
- CFileDialog FileDlg(FALSE, NULL, NULL, 0, strFilter);
- if (FileDlg.DoModal() == IDOK)
- {
- CLSID Clsid;
- CString sClsId;
- CString sPath = FileDlg.GetPathName();
- CString sExt = (FileDlg.GetFileExt()).MakeUpper();
- if (sExt == "")
- {
- switch (FileDlg.m_ofn.nFilterIndex)
- {
- case 2:
- sExt = "JPG";
- break;
- default:
- sExt = "BMP";
- }
- sPath += L"." + sExt;
- }
- if (sExt == "BMP")
- sClsId = "image/bmp";
- if (sExt == "JPG")
- sClsId = "image/jpeg";
- GetEncoderClsid(sClsId, &Clsid);
- m_pImgOUT->Save(sPath, &Clsid, NULL);
- }
- }
- void CPODlg::OnBnClickedButtonParams()
- {
- CParamsDlg paramsDlg;
- CString s;
- if (paramsDlg.DoModal() == IDOK)
- {
- s = paramsDlg.m_sParams;
- m_sigma = _wtof(s);
- }
- }
- void CPODlg::OnDestroy()
- {
- CDialogEx::OnDestroy();
- delete m_pImgIN;
- ::delete m_pImgOUT;
- GdiplusShutdown(m_gdiplusToken);
- // TODO: Add your message handler code here
- }
- BYTE CPODlg::GetPixelXY(int x, int y)
- {
- Color pixelColor;
- m_pImgIN->GetPixel(x, y, &pixelColor);
- double red = pixelColor.GetR();
- double green = pixelColor.GetG();
- double blue = pixelColor.GetB();
- double I = 0.299*red + 0.587*green + 0.114*blue;
- return (BYTE)I;
- }
- //Overload meeded for morpho operations
- BYTE CPODlg::GetPixelXY(Bitmap* img, int x, int y)
- {
- Color pixelColor;
- img->GetPixel(x, y, &pixelColor);
- double red = pixelColor.GetR();
- double green = pixelColor.GetG();
- double blue = pixelColor.GetB();
- double I = 0.299*red + 0.587*green + 0.114*blue;
- return (BYTE)I;
- }
- void CPODlg::SetPixelXY(int x, int y, BYTE Js)
- {
- Color pixelColor(Js, Js, Js);
- Status s = m_pImgOUT->SetPixel(x, y, pixelColor);
- }
- //Overload meeded for morpho operations
- void CPODlg::SetPixelXY(Bitmap* img, int x, int y, BYTE Js)
- {
- Color pixelColor(Js, Js, Js);
- Status s = img->SetPixel(x, y, pixelColor);
- }
- int CPODlg::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
- {
- UINT num = 0; // number of image encoders
- UINT size = 0; // size of the image encoder array in bytes
- ImageCodecInfo* pImageCodecInfo = NULL;
- GetImageEncodersSize(&num, &size);
- if (size == 0)
- return -1; // failure
- pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
- if (pImageCodecInfo == NULL)
- return -1; // failure
- GetImageEncoders(num, size, pImageCodecInfo);
- for (UINT j = 0; j < num; ++j)
- {
- if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
- {
- *pClsid = pImageCodecInfo[j].Clsid;
- free(pImageCodecInfo);
- return j; // success
- }
- }
- free(pImageCodecInfo);
- return -1; // failure
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement