Advertisement
noctual

Untitled

Dec 27th, 2021
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <Windows.h>    // Windows.h must be included before GdiPlus.h
  2. #include <GdiPlus.h>
  3. #include <stdlib.h>     // подключаем qsort
  4. #include <iostream>     // подключаем cout
  5. #include <chrono>
  6. #include <vector>
  7. #include <omp.h>
  8.  
  9. #pragma comment(lib, "gdiplus.lib")
  10.  
  11. ULONG_PTR m_gdiplusToken;   // class member
  12.  
  13. #include "imageformats.hpp"
  14. #include "imageio.hpp"
  15.  
  16. using namespace std;
  17.  
  18. int hexcolor(ColorBytePixel pixel) {
  19.     return (pixel.r << 16) | (pixel.g << 8) | pixel.b;
  20. }
  21.  
  22. ColorBytePixel rgbcolor(int hex) {
  23.     ColorBytePixel p(((hex) & 0xFF), ((hex >> 8) & 0xFF), ((hex >> 16) & 0xFF));
  24.     return p;
  25. }
  26.  
  27. void medianFilter(const int n, const char* input, const char* output, bool ompEnabled) {
  28.     ColorByteImage image = ImageIO::FileToColorByteImage(input);
  29.     ColorByteImage res(image.Width(), image.Height());
  30.  
  31.     int offset = (n + 1) / 2;
  32.  
  33.     auto start = chrono::steady_clock::now();
  34.  
  35.     #pragma omp parallel shared(offset, res) if (ompEnabled)
  36.     {
  37.         for (int i = 0; i < res.Width(); i++) {
  38.             #pragma omp for schedule(static, 50) nowait
  39.             for (int j = 0; j < res.Height(); j++) {
  40.                 int* matrix = new int[n * n];
  41.  
  42.                 for (int y = i - offset, l = 0; y < i + offset - 1; y++) {
  43.                     int i_normalize = y < 0 ? 0 : (y > image.Width() - 1 ? image.Width() - 1 : y);
  44.                     for (int x = j - offset; x < j + offset - 1; x++, l++) {
  45.                         int j_normalize = x < 0 ? 0 : (x > image.Height() - 1 ? image.Height() - 1 : x);
  46.                         matrix[l] = hexcolor(image(i_normalize, j_normalize));
  47.                     }
  48.                 }
  49.  
  50.                 qsort(matrix, n * n, sizeof(int), [](const void* a, const void* b) {
  51.                     return (*(int*)a - *(int*)b);
  52.                     });
  53.  
  54.                 #pragma omp critical
  55.                 res(i, j) = rgbcolor(matrix[(n + 1) / 2]);
  56.             }
  57.         }
  58.     }
  59.  
  60.     auto end = std::chrono::steady_clock::now();
  61.     auto elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  62.     cout << "Size - " << image.Width() << "x" << image.Height() << " px" << endl;
  63.     cout << "Elapsed time is - " << elapsed.count() << " microseconds" << endl;
  64.  
  65.     ImageIO::ImageToFile(res, output);
  66. }
  67.  
  68. int main(int argc, char* argv[])
  69. {
  70.     const int n = 11;
  71.  
  72.     vector<pair<const char*, const char*>> images{
  73.         {"input-1386x903.jpg", "output-1386x903.jpg"},
  74.         {"input-693x452.jpg", "output-693x452.jpg"},
  75.         {"input-347x226.jpg", "output-347x226.jpg"},
  76.         {"input-174x113.jpg", "output-174x113.jpg"}
  77.     }; 
  78.  
  79.     // InitInstance
  80.     Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  81.     Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
  82.  
  83.     // Количество потоков
  84.     omp_set_num_threads(5);
  85.  
  86.     for (auto image : images) {
  87.         medianFilter(n, image.first, image.second, true);
  88.     }
  89.  
  90.     // ExitInstance
  91.     Gdiplus::GdiplusShutdown(m_gdiplusToken);
  92.  
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement