MainPage.xaml.cpp 3.5 KB

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