Advertisement
Guest User

Untitled

a guest
May 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.80 KB | None | 0 0
  1.  
  2. // PO1Dlg.cpp : implementation file
  3. //
  4.  
  5. #include "stdafx.h"
  6. #include "PO1.h"
  7. #include "PO1Dlg.h"
  8. #include "ParamsDlg.h"
  9. #include "afxdialogex.h"
  10. #include <array>
  11. #include <complex>
  12. #include <vector>
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #endif
  17.  
  18. #define IMG_WND_ID_IN   100
  19. #define IMG_WND_ID_OUT  101
  20.  
  21.  
  22. // CAboutDlg dialog used for App About
  23.  
  24. class CAboutDlg : public CDialogEx
  25. {
  26. public:
  27.     CAboutDlg();
  28.  
  29.     // Dialog Data
  30.     enum { IDD = IDD_ABOUTBOX };
  31.  
  32. protected:
  33.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  34.  
  35.     // Implementation
  36. protected:
  37.     DECLARE_MESSAGE_MAP()
  38. };
  39.  
  40. CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
  41. {
  42. }
  43.  
  44. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  45. {
  46.     CDialogEx::DoDataExchange(pDX);
  47. }
  48.  
  49. BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
  50. END_MESSAGE_MAP()
  51.  
  52.  
  53. // CPODlg dialog
  54.  
  55.  
  56.  
  57. CPODlg::CPODlg(CWnd* pParent /*=NULL*/)
  58.     : CDialogEx(CPODlg::IDD, pParent)
  59. {
  60.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61.     m_pImgIN = m_pImgOUT = NULL;
  62.     m_imageH = m_imageW = -1;
  63. }
  64.  
  65. void CPODlg::DoDataExchange(CDataExchange* pDX)
  66. {
  67.     CDialogEx::DoDataExchange(pDX);
  68.     DDX_Control(pDX, IDC_COMBO1, m_combo1);
  69. }
  70.  
  71. BEGIN_MESSAGE_MAP(CPODlg, CDialogEx)
  72.     ON_WM_SYSCOMMAND()
  73.     ON_WM_PAINT()
  74.     ON_WM_QUERYDRAGICON()
  75.     ON_BN_CLICKED(IDC_BUTTON_LOAD, &CPODlg::OnBnClickedButtonLoad)
  76.     ON_BN_CLICKED(IDC_BUTTON_PROCESS, &CPODlg::OnBnClickedButtonProcess)
  77.     ON_BN_CLICKED(IDC_BUTTON_SAVE, &CPODlg::OnBnClickedButtonSave)
  78.     ON_BN_CLICKED(IDC_BUTTON_PARAMS, &CPODlg::OnBnClickedButtonParams)
  79.     ON_WM_DESTROY()
  80. END_MESSAGE_MAP()
  81.  
  82.  
  83. // CPODlg message handlers
  84.  
  85. BOOL CPODlg::OnInitDialog()
  86. {
  87.     CDialogEx::OnInitDialog();
  88.     GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
  89.  
  90.  
  91.     // Add "About..." menu item to system menu.
  92.  
  93.     // IDM_ABOUTBOX must be in the system command range.
  94.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  95.     ASSERT(IDM_ABOUTBOX < 0xF000);
  96.  
  97.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  98.     if (pSysMenu != NULL)
  99.     {
  100.         BOOL bNameValid;
  101.         CString strAboutMenu;
  102.         bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  103.         ASSERT(bNameValid);
  104.         if (!strAboutMenu.IsEmpty())
  105.         {
  106.             pSysMenu->AppendMenu(MF_SEPARATOR);
  107.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  108.         }
  109.     }
  110.  
  111.     // Set the icon for this dialog.  The framework does this automatically
  112.     //  when the application's main window is not a dialog
  113.     SetIcon(m_hIcon, TRUE);         // Set big icon
  114.     SetIcon(m_hIcon, FALSE);        // Set small icon
  115.  
  116.     // TODO: Add extra initialization here
  117.     CRect rDlg(7, 7, 407, 407);
  118.     MapDialogRect(rDlg);
  119.  
  120.     m_imgWndIN.Create(rDlg, this, IMG_WND_ID_IN);
  121.  
  122.     rDlg = CRect(530, 7, 930, 407);
  123.     MapDialogRect(rDlg);
  124.  
  125.     m_imgWndOUT.Create(rDlg, this, IMG_WND_ID_OUT);
  126.  
  127.     // OPCJE
  128.     m_combo1.AddString(L"transform to greyscale");
  129.     m_combo1.SelectString(0, L"transform to greyscale");
  130.  
  131.     m_combo1.AddString(L"dylatacjaB");
  132.     m_combo1.AddString(L"dylatacja");
  133.     m_combo1.AddString(L"erozja");
  134.     m_combo1.AddString(L"zamkniecie");
  135.     m_combo1.AddString(L"otwarcie");
  136.     m_combo1.AddString(L"kontur wew");
  137.     m_combo1.AddString(L"kontur zew");
  138.  
  139.     return TRUE;  // return TRUE  unless you set the focus to a control
  140. }
  141.  
  142. void CPODlg::OnSysCommand(UINT nID, LPARAM lParam)
  143. {
  144.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  145.     {
  146.         CAboutDlg dlgAbout;
  147.         dlgAbout.DoModal();
  148.     }
  149.     else
  150.     {
  151.         CDialogEx::OnSysCommand(nID, lParam);
  152.     }
  153. }
  154.  
  155. // If you add a minimize button to your dialog, you will need the code below
  156. //  to draw the icon.  For MFC applications using the document/view model,
  157. //  this is automatically done for you by the framework.
  158.  
  159. void CPODlg::OnPaint()
  160. {
  161.     if (IsIconic())
  162.     {
  163.         CPaintDC dc(this); // device context for painting
  164.  
  165.         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  166.  
  167.         // Center icon in client rectangle
  168.         int cxIcon = GetSystemMetrics(SM_CXICON);
  169.         int cyIcon = GetSystemMetrics(SM_CYICON);
  170.         CRect rect;
  171.         GetClientRect(&rect);
  172.         int x = (rect.Width() - cxIcon + 1) / 2;
  173.         int y = (rect.Height() - cyIcon + 1) / 2;
  174.  
  175.         // Draw the icon
  176.         dc.DrawIcon(x, y, m_hIcon);
  177.     }
  178.     else
  179.     {
  180.         CDialogEx::OnPaint();
  181.     }
  182. }
  183.  
  184. // The system calls this function to obtain the cursor to display while the user drags
  185. //  the minimized window.
  186. HCURSOR CPODlg::OnQueryDragIcon()
  187. {
  188.     return static_cast<HCURSOR>(m_hIcon);
  189. }
  190.  
  191. void CPODlg::OnBnClickedButtonLoad()
  192. {
  193.     WCHAR strFilter[] = { L"Image Files (*.bmp; *.jpg; *.tiff; *.gif; *.png)|*.bmp; *.jpg; *.tiff; *.gif; *.png|All Files (*.*)|*.*||" };
  194.  
  195.     CFileDialog FileDlg(TRUE, NULL, NULL, 0, strFilter);
  196.  
  197.     if (FileDlg.DoModal() == IDOK)
  198.     {
  199.         delete m_pImgIN;
  200.         ::delete m_pImgOUT;
  201.         m_pImgOUT = NULL;
  202.  
  203.         m_pImgIN = Bitmap::FromFile(FileDlg.GetPathName());
  204.         m_imgWndIN.SetImg(m_pImgIN);
  205.         m_imageW = m_pImgIN->GetWidth();
  206.         m_imageH = m_pImgIN->GetHeight();
  207.  
  208.         m_pImgOUT = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);// PixelFormat16bppGrayScale);
  209.  
  210.         m_imgWndOUT.SetImg(m_pImgOUT);
  211.  
  212.         Invalidate();
  213.     }
  214. }
  215.  
  216. void CPODlg::dylatacja(Bitmap* in, Bitmap* out)
  217. {
  218.     for (int i = 0; i < m_imageW; i++)
  219.     {
  220.         for (int j = 0; j < m_imageH; j++)
  221.         {
  222.             BYTE mask[3][3] = { 255, 255, 255, 255, 255, 255, 255, 255, 255 };
  223.  
  224.             mask[1][1] = GetPixelXY(in, i, j);
  225.  
  226.             //Top left
  227.             if (j - 1 >= 0 && i - 1 >= 0)
  228.                 mask[0][0] = GetPixelXY(in, i - 1, j - 1);
  229.             //Top mid
  230.             if (j - 1 >= 0)
  231.                 mask[1][0] = GetPixelXY(in, i, j - 1);
  232.             //Top right
  233.             if (j - 1 >= 0 && i + 1 < m_imageW)
  234.                 mask[2][0] = GetPixelXY(in, i + 1, j - 1);
  235.             //Left
  236.             if (i - 1 >= 0)
  237.                 mask[0][1] = GetPixelXY(in, i - 1, j);
  238.             //Right
  239.             if (i + 1 < m_imageW)
  240.                 mask[2][1] = GetPixelXY(in, i + 1, j);
  241.             //Bottom left
  242.             if (j + 1 < m_imageH && i - 1 >= 0)
  243.                 mask[0][2] = GetPixelXY(in, i - 1, j + 1);
  244.             //Bottom mid
  245.             if (j + 1 < m_imageH)
  246.                 mask[1][2] = GetPixelXY(in, i, j + 1);
  247.             //Bottom right
  248.             if (j + 1 < m_imageH && i + 1 < m_imageW)
  249.                 mask[2][2] = GetPixelXY(in, i + 1, j + 1);
  250.  
  251.             BYTE min = 255;
  252.             for (int x = 0; x < 3; x++)
  253.             {
  254.                 for (int y = 0; y < 3; y++)
  255.                 {
  256.                     if (mask[x][y] < min)
  257.                         min = mask[x][y];
  258.                 }
  259.             }
  260.  
  261.             SetPixelXY(out, i, j, min);
  262.         }
  263.     }
  264. }
  265.  
  266. void CPODlg::dylatacjaB()
  267. {
  268.     bool actualPix;
  269.     for (int i = 0; i < m_imageW; i++)
  270.     {
  271.         for (int j = 0; j < m_imageH; j++)
  272.         {
  273.             SetPixelXY(i, j, 255);
  274.         }
  275.     }
  276.  
  277.     for (int i = 1; i < m_imageW - 1; i++)
  278.     {
  279.         for (int j = 1; j < m_imageH - 1; j++)
  280.         {
  281.             actualPix = GetPixelXY(i, j);
  282.  
  283.             if (!actualPix)
  284.             {
  285.                 SetPixelXY(i - 1, j - 1, 0);
  286.                 SetPixelXY(i, j - 1, 0);
  287.                 SetPixelXY(i + 1, j - 1, 0);
  288.                 SetPixelXY(i - 1, j, 0);
  289.                 SetPixelXY(i + 1, j, 0);
  290.                 SetPixelXY(i, j, 0);
  291.                 SetPixelXY(i - 1, j + 1, 0);
  292.                 SetPixelXY(i, j + 1, 0);
  293.                 SetPixelXY(i + 1, j + 1, 0);
  294.             }
  295.         }
  296.     }
  297. }
  298.  
  299. void CPODlg::erozja(Bitmap* in, Bitmap* out)
  300. {
  301.     bool actualPix;
  302.     for (int i = 0; i < m_imageW; i++)
  303.     {
  304.         for (int j = 0; j < m_imageH; j++)
  305.         {
  306.             BYTE mask[3][3] = { 255, 255, 255, 255, 255, 255, 255, 255, 255 };
  307.  
  308.             mask[1][1] = GetPixelXY(in, i, j);
  309.  
  310.             //Top left
  311.             if (j - 1 >= 0 && i - 1 >= 0)
  312.                 mask[0][0] = GetPixelXY(in, i - 1, j - 1);
  313.             //Top mid
  314.             if (j - 1 >= 0)
  315.                 mask[1][0] = GetPixelXY(in, i, j - 1);
  316.             //Top right
  317.             if (j - 1 >= 0 && i + 1 < m_imageW)
  318.                 mask[2][0] = GetPixelXY(in, i + 1, j - 1);
  319.             //Left
  320.             if (i - 1 >= 0)
  321.                 mask[0][1] = GetPixelXY(in, i - 1, j);
  322.             //Right
  323.             if (i + 1 < m_imageW)
  324.                 mask[2][1] = GetPixelXY(in, i + 1, j);
  325.             //Bottom left
  326.             if (j + 1 < m_imageH && i - 1 >= 0)
  327.                 mask[0][2] = GetPixelXY(in, i - 1, j + 1);
  328.             //Bottom mid
  329.             if (j + 1 < m_imageH)
  330.                 mask[1][2] = GetPixelXY(in, i, j + 1);
  331.             //Bottom right
  332.             if (j + 1 < m_imageH && i + 1 < m_imageW)
  333.                 mask[2][2] = GetPixelXY(in, i + 1, j + 1);
  334.  
  335.             BYTE max = 0;
  336.             for (int x = 0; x < 3; x++)
  337.             {
  338.                 for (int y = 0; y < 3; y++)
  339.                 {
  340.                     if (mask[x][y] > max)
  341.                         max = mask[x][y];
  342.                 }
  343.             }
  344.  
  345.             SetPixelXY(out, i, j, max);
  346.         }
  347.     }
  348. }
  349.  
  350. void CPODlg::otwarcie()
  351. {
  352.     Bitmap* tmp = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);
  353.     erozja(m_pImgIN, tmp);
  354.     dylatacja(tmp, m_pImgOUT);
  355.     ::delete tmp;
  356. }
  357.  
  358. void CPODlg::zamkniecie()
  359. {
  360.     Bitmap* tmp = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);
  361.     dylatacja(m_pImgIN, tmp);
  362.     erozja(tmp, m_pImgOUT);
  363.     ::delete tmp;
  364. }
  365.  
  366. void CPODlg::konturWew(Bitmap* in, Bitmap* out)
  367. {
  368.     Bitmap* tmp = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);
  369.     BYTE pix1;
  370.     BYTE pix2;
  371.  
  372.     erozja(in, tmp);
  373.     for (int i = 0; i < m_imageW; i++)
  374.     {
  375.         for (int j = 0; j < m_imageH; j++)
  376.         {
  377.             pix1 = GetPixelXY(in, i, j);
  378.             pix2 = GetPixelXY(tmp, i, j);
  379.             if (pix1 != pix2)
  380.             {
  381.                 SetPixelXY(out, i, j, 0);
  382.             }
  383.             else
  384.             {
  385.                 SetPixelXY(out, i, j, 255);
  386.             }
  387.         }
  388.     }
  389.     ::delete tmp;
  390. }
  391.  
  392. void CPODlg::konturZew(Bitmap* in, Bitmap* out)
  393. {
  394.     Bitmap* tmp = ::new Bitmap(m_imageW, m_imageH, PixelFormat32bppARGB);
  395.     BYTE pix1;
  396.     BYTE pix2;
  397.  
  398.     dylatacja(in, tmp);
  399.     for (int i = 0; i < m_imageW; i++)
  400.     {
  401.         for (int j = 0; j < m_imageH; j++)
  402.         {
  403.             pix1 = GetPixelXY(in, i, j);
  404.             pix2 = GetPixelXY(tmp, i, j);
  405.             if (pix2 == 0 && pix1 != pix2)
  406.             {
  407.                 SetPixelXY(out, i, j, 0);
  408.             }
  409.             else
  410.             {
  411.                 SetPixelXY(out, i, j, 255);
  412.             }
  413.         }
  414.     }
  415.     ::delete tmp;
  416. }
  417.  
  418. void CPODlg::OnBnClickedButtonProcess()
  419. {
  420.     CString sOption;
  421.     m_combo1.GetLBText(m_combo1.GetCurSel(), sOption);
  422.     BeginWaitCursor();
  423.  
  424.     if (sOption == L"transform to greyscale")
  425.     {
  426.         for (int x = 0; x < m_imageW; x++)
  427.             for (int y = 0; y < m_imageH; y++)
  428.             {
  429.                 BYTE Js = GetPixelXY(x, y);
  430.                 SetPixelXY(x, y, Js);
  431.             }
  432.  
  433.     }
  434.     else if (sOption == L"dylatacjaB")
  435.     {
  436.         dylatacjaB();
  437.     }
  438.     else if (sOption == L"dylatacja")
  439.     {
  440.         dylatacja(m_pImgIN, m_pImgOUT);
  441.     }
  442.     else if (sOption == L"erozja")
  443.     {
  444.         erozja(m_pImgIN, m_pImgOUT);
  445.     }
  446.     else if (sOption == L"zamkniecie")
  447.     {
  448.         zamkniecie();
  449.     }
  450.     else if (sOption == L"otwarcie")
  451.     {
  452.         otwarcie();
  453.     }
  454.     else if (sOption == L"kontur wew")
  455.     {
  456.         konturWew(m_pImgIN, m_pImgOUT);
  457.     }
  458.     else if (sOption == L"kontur zew")
  459.     {
  460.         konturZew(m_pImgIN, m_pImgOUT);
  461.     }
  462.     /*********************************************************************************************************************************
  463.     TU NALEŻY WSTAWIC OBSŁUGĘ POZOSTAŁYCH OPCJI
  464.  
  465.     Zmienne m_imageH i m_imageW zawierają informacje o wysokości i szerokości przetwarzanego obrazu.
  466.  
  467.     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
  468.     automatyczna konwersja na poziom szarości).
  469.  
  470.     Funkcja SetPixelXY(x,y,Js) ustawia w obrazie wynikowym jasność piksela o współrzędnych (x,y) na wartość Js.
  471.  
  472.     Wartości jasności są typu BYTE (0..255).
  473.  
  474.     Dodawanie opcji do listy rozwijanej zrealizowane jest na końcu kodu metody OnInitDialog().
  475.  
  476.     W metodzie OnBnClickedButtonParams() pobierany jest łańcuch znaków wpisany przez użytkownika w oknie pojawiającym się po naciśnięciu
  477.     przycisku "Params".
  478.  
  479.     ***********************************************************************************************************************************/
  480.  
  481.  
  482.     Invalidate();
  483.     EndWaitCursor();
  484. }
  485.  
  486.  
  487. void CPODlg::OnBnClickedButtonSave()
  488. {
  489.     WCHAR strFilter[] = { L"Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg||" };
  490.  
  491.     CFileDialog FileDlg(FALSE, NULL, NULL, 0, strFilter);
  492.  
  493.     if (FileDlg.DoModal() == IDOK)
  494.     {
  495.         CLSID Clsid;
  496.         CString sClsId;
  497.         CString sPath = FileDlg.GetPathName();
  498.         CString sExt = (FileDlg.GetFileExt()).MakeUpper();
  499.  
  500.         if (sExt == "")
  501.         {
  502.             switch (FileDlg.m_ofn.nFilterIndex)
  503.             {
  504.             case 2:
  505.             sExt = "JPG";
  506.             break;
  507.             default:
  508.             sExt = "BMP";
  509.             }
  510.  
  511.             sPath += L"." + sExt;
  512.         }
  513.  
  514.         if (sExt == "BMP")
  515.             sClsId = "image/bmp";
  516.  
  517.         if (sExt == "JPG")
  518.             sClsId = "image/jpeg";
  519.  
  520.         GetEncoderClsid(sClsId, &Clsid);
  521.         m_pImgOUT->Save(sPath, &Clsid, NULL);
  522.     }
  523. }
  524.  
  525.  
  526. void CPODlg::OnBnClickedButtonParams()
  527. {
  528.     CParamsDlg paramsDlg;
  529.     CString s;
  530.  
  531.     if (paramsDlg.DoModal() == IDOK)
  532.     {
  533.         s = paramsDlg.m_sParams;
  534.         m_sigma = _wtof(s);
  535.     }
  536. }
  537.  
  538.  
  539. void CPODlg::OnDestroy()
  540. {
  541.     CDialogEx::OnDestroy();
  542.  
  543.     delete m_pImgIN;
  544.     ::delete m_pImgOUT;
  545.  
  546.     GdiplusShutdown(m_gdiplusToken);
  547.  
  548.     // TODO: Add your message handler code here
  549. }
  550.  
  551.  
  552. BYTE CPODlg::GetPixelXY(int x, int y)
  553. {
  554.     Color pixelColor;
  555.     m_pImgIN->GetPixel(x, y, &pixelColor);
  556.  
  557.     double red = pixelColor.GetR();
  558.     double green = pixelColor.GetG();
  559.     double blue = pixelColor.GetB();
  560.     double I = 0.299*red + 0.587*green + 0.114*blue;
  561.  
  562.     return (BYTE)I;
  563. }
  564.  
  565. //Overload meeded for morpho operations
  566. BYTE CPODlg::GetPixelXY(Bitmap* img, int x, int y)
  567. {
  568.     Color pixelColor;
  569.     img->GetPixel(x, y, &pixelColor);
  570.  
  571.     double red = pixelColor.GetR();
  572.     double green = pixelColor.GetG();
  573.     double blue = pixelColor.GetB();
  574.     double I = 0.299*red + 0.587*green + 0.114*blue;
  575.  
  576.     return (BYTE)I;
  577. }
  578.  
  579.  
  580. void CPODlg::SetPixelXY(int x, int y, BYTE Js)
  581. {
  582.     Color pixelColor(Js, Js, Js);
  583.     Status s = m_pImgOUT->SetPixel(x, y, pixelColor);
  584. }
  585.  
  586. //Overload meeded for morpho operations
  587. void CPODlg::SetPixelXY(Bitmap* img, int x, int y, BYTE Js)
  588. {
  589.     Color pixelColor(Js, Js, Js);
  590.     Status s = img->SetPixel(x, y, pixelColor);
  591. }
  592.  
  593. int CPODlg::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
  594. {
  595.     UINT  num = 0;          // number of image encoders
  596.     UINT  size = 0;         // size of the image encoder array in bytes
  597.  
  598.     ImageCodecInfo* pImageCodecInfo = NULL;
  599.  
  600.     GetImageEncodersSize(&num, &size);
  601.     if (size == 0)
  602.         return -1;  // failure
  603.  
  604.     pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
  605.     if (pImageCodecInfo == NULL)
  606.         return -1;  // failure
  607.  
  608.     GetImageEncoders(num, size, pImageCodecInfo);
  609.  
  610.     for (UINT j = 0; j < num; ++j)
  611.     {
  612.         if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
  613.         {
  614.             *pClsid = pImageCodecInfo[j].Clsid;
  615.             free(pImageCodecInfo);
  616.             return j;  // success
  617.         }
  618.     }
  619.  
  620.     free(pImageCodecInfo);
  621.     return -1;  // failure
  622. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement