1 //
2 // MainPage.xaml.cpp
3 // Implementation of the MainPage class.
4 //
5
6 #include "pch.h"
7 #include "MainPage.xaml.h"
8
9 #include <opencv2\imgproc\types_c.h>
10 #include <opencv2\core\core.hpp>
11 #include <opencv2\imgproc\imgproc.hpp>
12 #include <Robuffer.h>
13 #include <ppl.h>
14 #include <ppltasks.h>
15
16 using namespace PhoneTutorial;
17 using namespace Platform;
18 using namespace Windows::Foundation;
19 using namespace Windows::Foundation::Collections;
20 using namespace Windows::UI::Xaml;
21 using namespace Windows::UI::Xaml::Controls;
22 using namespace Windows::UI::Xaml::Controls::Primitives;
23 using namespace Windows::UI::Xaml::Data;
24 using namespace Windows::UI::Xaml::Input;
25 using namespace Windows::UI::Xaml::Media;
26 using namespace Windows::UI::Xaml::Navigation;
27 using namespace Windows::UI::Xaml::Media::Imaging;
28 using namespace Windows::Storage::Streams;
29 using namespace Microsoft::WRL;
30 using namespace Windows::ApplicationModel;
31
MainPage()32 MainPage::MainPage()
33 {
34 InitializeComponent();
35 }
36
37 /// <summary>
38 /// Invoked when this page is about to be displayed in a Frame.
39 /// </summary>
40 /// <param name="e">Event data that describes how this page was reached. The Parameter
41 /// property is typically used to configure the page.</param>
42 void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
43 {
44 (void) e; // Unused parameter
45 LoadImage();
46 }
47
ThrowIfFailed(HRESULT hr)48 inline void ThrowIfFailed(HRESULT hr)
49 {
50 if (FAILED(hr))
51 {
52 throw Exception::CreateException(hr);
53 }
54 }
55
56 byte* GetPointerToPixelData(IBuffer^ buffer)
57 {
58 // Cast to Object^, then to its underlying IInspectable interface.
59 Object^ obj = buffer;
60 ComPtr<IInspectable> insp(reinterpret_cast<IInspectable*>(obj));
61
62 // Query the IBufferByteAccess interface.
63 ComPtr<IBufferByteAccess> bufferByteAccess;
64 ThrowIfFailed(insp.As(&bufferByteAccess));
65
66 // Retrieve the buffer data.
67 byte* pixels = nullptr;
68 ThrowIfFailed(bufferByteAccess->Buffer(&pixels));
69 return pixels;
70 }
71
72 void PhoneTutorial::MainPage::Process_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
73 {
74 (void) e; // Unused parameter
75
76 // get the pixels from the WriteableBitmap
77 byte* pPixels = GetPointerToPixelData(m_bitmap->PixelBuffer);
78 int height = m_bitmap->PixelHeight;
79 int width = m_bitmap->PixelWidth;
80
81 // create a matrix the size and type of the image
82 cv::Mat mat(width, height, CV_8UC4);
83 memcpy(mat.data, pPixels, 4 * height*width);
84
85 // convert to grayscale
86 cv::Mat intermediateMat;
87 cv::cvtColor(mat, intermediateMat, CV_RGB2GRAY);
88
89 // convert to BGRA
90 cv::cvtColor(intermediateMat, mat, CV_GRAY2BGRA);
91
92 // copy processed image back to the WriteableBitmap
93 memcpy(pPixels, mat.data, 4 * height*width);
94
95 // update the WriteableBitmap
96 m_bitmap->Invalidate();
97 }
98
LoadImage()99 void PhoneTutorial::MainPage::LoadImage()
100 {
101 Concurrency::task<Windows::Storage::StorageFile^> getFileTask(Package::Current->InstalledLocation->GetFileAsync(L"Lena.png"));
102
103 auto getStreamTask = getFileTask.then(
104 [](Windows::Storage::StorageFile ^storageFile)
105 {
106 return storageFile->OpenReadAsync();
107 });
108
109 getStreamTask.then(
110 [this](Windows::Storage::Streams::IRandomAccessStreamWithContentType^ stream)
111 {
112 m_bitmap = ref new Windows::UI::Xaml::Media::Imaging::WriteableBitmap(1, 1);
113 m_bitmap->SetSource(stream);
114 image->Source = m_bitmap;
115 });
116 }
117
118 void PhoneTutorial::MainPage::Reset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
119 {
120 (void) e; // Unused parameter
121 LoadImage();
122 }
123