Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #include "tbb/tick_count.h"
  2. #include "tbb/blocked_range2d.h"
  3. #include "tbb/parallel_for.h"
  4. #include "tbb/mutex.h"
  5. #include <gdiplus.h>
  6. #include <iostream>
  7. #include <stdlib.h>
  8. #include <cmath>
  9.  
  10. #pragma comment( lib, "gdiplus.lib" )
  11.  
  12. static const wchar_t* filename = L"C:\\Users\\Ap0\\Desktop\\poster_rodents_big.jpg";
  13. static const wchar_t* filename_output = L"C:\\Users\\Ap0\\Desktop\\aa2.jpg";
  14. static const wchar_t* filename_output2 = L"C:\\Users\\Ap0\\Desktop\\aa3.jpg";
  15.  
  16. /*
  17. Go to "Project -> Properties -> Configuration Properties -> Intel Performance Libraries -> Use Intel TBB" set to "Yes"
  18. Go to "Tools -> Options -> Debugging -> Symbols" select checkbox "Microsoft Symbol Servers"
  19. Go to "Project -> Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions" add "_CRT_SECURE_NO_WARNINGS"
  20.  
  21. https://software.intel.com/en-us/node/506154
  22. https://texus.me/2015/11/17/intel-tbb-parallel-for/
  23. http://registrationcenter-download.intel.com/akdlm/irc_nas/tec/11272/w_tbb_2017.4.187.exe
  24. */
  25.  
  26. void rgb_to_grayscale_serial()
  27. {
  28. Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  29. ULONG_PTR gdiplusToken;
  30. Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  31. Gdiplus::Bitmap myBitmap(filename);
  32. int x, y;
  33. BYTE a = 0xFF;
  34. BYTE r, g, b, avg;
  35. for (int y = 0; y < myBitmap.GetHeight(); y++){
  36. for (int x = 0; x < myBitmap.GetWidth(); x++){
  37. Gdiplus::Color pixelColor;
  38. myBitmap.GetPixel(x, y, &pixelColor);
  39. r = pixelColor.GetR();
  40. g = pixelColor.GetG();
  41. b = pixelColor.GetB();
  42.  
  43. avg = (r + g + b) / 3;
  44. Gdiplus::Color newColor = Gdiplus::Color::MakeARGB(a, avg, avg, avg);
  45. myBitmap.SetPixel(x, y, newColor);
  46. }
  47. }
  48. CLSID pngEncoderClsId = { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e } };
  49. myBitmap.Save(filename_output, &pngEncoderClsId, NULL);
  50. // delete image; image = 0;
  51. // Gdiplus::GdiplusShutdown(gdiplusToken);
  52. }
  53.  
  54. class Grayscale
  55. {
  56. private:
  57. Gdiplus::BitmapData *bitmapData;
  58.  
  59. public:
  60. Grayscale(Gdiplus::BitmapData* bitmapData) : bitmapData(bitmapData){}
  61.  
  62. void operator()(tbb::blocked_range2d<int> &p) const
  63. {
  64. BYTE a = 0xFF;
  65. BYTE r, g, b, avg;
  66. byte* line = (byte*)bitmapData->Scan0;
  67.  
  68. for (int y = p.rows().begin(); y != p.rows().end(); y++)
  69. {
  70. for (int x = p.cols().begin(); x != p.cols().end(); x++)
  71. {
  72. byte* pos = line + x * 3;
  73. Gdiplus::Color pixel = Gdiplus::Color::MakeARGB(a, pos[0], pos[1], pos[2]);
  74.  
  75. r = pixel.GetR();
  76. g = pixel.GetG();
  77. b = pixel.GetB();
  78. avg = (r + g + b) / 3;
  79.  
  80. pos[0] = avg;
  81. pos[1] = avg;
  82. pos[2] = avg;
  83. }
  84. line += bitmapData->Stride;
  85. }
  86. }
  87. };
  88.  
  89.  
  90. void rgb_to_grayscale_parallel()
  91. {
  92. Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  93. ULONG_PTR gdiplusToken;
  94. Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  95. Gdiplus::Bitmap myBitmap(filename);
  96.  
  97. Gdiplus::Rect rect(0, 0, myBitmap.GetWidth(), myBitmap.GetHeight());
  98. Gdiplus::BitmapData bitmapData;
  99. myBitmap.LockBits(&rect, Gdiplus::ImageLockModeWrite | Gdiplus::ImageLockModeRead, PixelFormat24bppRGB, &bitmapData);
  100.  
  101. tbb::parallel_for(tbb::blocked_range2d<int, int>(0, myBitmap.GetHeight(), 0, myBitmap.GetWidth()), Grayscale(&bitmapData));
  102. myBitmap.UnlockBits(&bitmapData);
  103.  
  104. CLSID pngEncoderClsId = { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e } };
  105. myBitmap.Save(filename_output2, &pngEncoderClsId, NULL);
  106. }
  107. int main()
  108. {
  109. /* serial converting of RGB image to grayscale */
  110. printf("Serial converting of RGB image to grayscale...\n");
  111. getchar();
  112. tbb::tick_count startTime = tbb::tick_count::now();
  113. rgb_to_grayscale_serial();
  114. tbb::tick_count endTime = tbb::tick_count::now();
  115. printf("Time: %lf seconds\n", (endTime - startTime).seconds());
  116.  
  117. /* parallel converting of RGB image to grayscale */
  118. printf("Parallel converting of RGB image to grayscale...\n");
  119. getchar();
  120. startTime = tbb::tick_count::now();
  121. rgb_to_grayscale_parallel();
  122. endTime = tbb::tick_count::now();
  123. printf("Time: %lf seconds\n", (endTime - startTime).seconds());
  124.  
  125.  
  126. getchar();
  127.  
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement