This source file includes following definitions.
- OnNavigatedTo
- ThrowIfFailed
- GetPointerToPixelData
- Process_Click
- LoadImage
- Reset_Click
#include "pch.h"
#include "MainPage.xaml.h"
#include <opencv2\imgproc\types_c.h>
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <Robuffer.h>
#include <ppl.h>
#include <ppltasks.h>
using namespace PhoneTutorial;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::UI::Xaml::Media::Imaging;
using namespace Windows::Storage::Streams;
using namespace Microsoft::WRL;
using namespace Windows::ApplicationModel;
MainPage::MainPage()
{
InitializeComponent();
}
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
(void) e;
LoadImage();
}
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
throw Exception::CreateException(hr);
}
}
byte* GetPointerToPixelData(IBuffer^ buffer)
{
Object^ obj = buffer;
ComPtr<IInspectable> insp(reinterpret_cast<IInspectable*>(obj));
ComPtr<IBufferByteAccess> bufferByteAccess;
ThrowIfFailed(insp.As(&bufferByteAccess));
byte* pixels = nullptr;
ThrowIfFailed(bufferByteAccess->Buffer(&pixels));
return pixels;
}
void PhoneTutorial::MainPage::Process_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
(void) e;
byte* pPixels = GetPointerToPixelData(m_bitmap->PixelBuffer);
int height = m_bitmap->PixelHeight;
int width = m_bitmap->PixelWidth;
cv::Mat mat(width, height, CV_8UC4);
memcpy(mat.data, pPixels, 4 * height*width);
cv::Mat intermediateMat;
cv::cvtColor(mat, intermediateMat, CV_RGB2GRAY);
cv::cvtColor(intermediateMat, mat, CV_GRAY2BGRA);
memcpy(pPixels, mat.data, 4 * height*width);
m_bitmap->Invalidate();
}
void PhoneTutorial::MainPage::LoadImage()
{
Concurrency::task<Windows::Storage::StorageFile^> getFileTask(Package::Current->InstalledLocation->GetFileAsync(L"Lena.png"));
auto getStreamTask = getFileTask.then(
[](Windows::Storage::StorageFile ^storageFile)
{
return storageFile->OpenReadAsync();
});
getStreamTask.then(
[this](Windows::Storage::Streams::IRandomAccessStreamWithContentType^ stream)
{
m_bitmap = ref new Windows::UI::Xaml::Media::Imaging::WriteableBitmap(1, 1);
m_bitmap->SetSource(stream);
image->Source = m_bitmap;
});
}
void PhoneTutorial::MainPage::Reset_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
(void) e;
LoadImage();
}