Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "tbb/tick_count.h"
- #include "tbb/blocked_range2d.h"
- #include "tbb/parallel_for.h"
- #include "tbb/mutex.h"
- #include <gdiplus.h>
- #include <iostream>
- #include <stdlib.h>
- #include <cmath>
- #pragma comment( lib, "gdiplus.lib" )
- static const wchar_t* filename = L"C:\\Users\\Ap0\\Desktop\\poster_rodents_big.jpg";
- static const wchar_t* filename_output = L"C:\\Users\\Ap0\\Desktop\\aa2.jpg";
- static const wchar_t* filename_output2 = L"C:\\Users\\Ap0\\Desktop\\aa3.jpg";
- /*
- Go to "Project -> Properties -> Configuration Properties -> Intel Performance Libraries -> Use Intel TBB" set to "Yes"
- Go to "Tools -> Options -> Debugging -> Symbols" select checkbox "Microsoft Symbol Servers"
- Go to "Project -> Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions" add "_CRT_SECURE_NO_WARNINGS"
- https://software.intel.com/en-us/node/506154
- https://texus.me/2015/11/17/intel-tbb-parallel-for/
- http://registrationcenter-download.intel.com/akdlm/irc_nas/tec/11272/w_tbb_2017.4.187.exe
- */
- void rgb_to_grayscale_serial()
- {
- Gdiplus::GdiplusStartupInput gdiplusStartupInput;
- ULONG_PTR gdiplusToken;
- Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
- Gdiplus::Bitmap myBitmap(filename);
- int x, y;
- BYTE a = 0xFF;
- BYTE r, g, b, avg;
- for (int y = 0; y < myBitmap.GetHeight(); y++){
- for (int x = 0; x < myBitmap.GetWidth(); x++){
- Gdiplus::Color pixelColor;
- myBitmap.GetPixel(x, y, &pixelColor);
- r = pixelColor.GetR();
- g = pixelColor.GetG();
- b = pixelColor.GetB();
- avg = (r + g + b) / 3;
- Gdiplus::Color newColor = Gdiplus::Color::MakeARGB(a, avg, avg, avg);
- myBitmap.SetPixel(x, y, newColor);
- }
- }
- CLSID pngEncoderClsId = { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e } };
- myBitmap.Save(filename_output, &pngEncoderClsId, NULL);
- // delete image; image = 0;
- // Gdiplus::GdiplusShutdown(gdiplusToken);
- }
- class Grayscale
- {
- private:
- Gdiplus::BitmapData *bitmapData;
- public:
- Grayscale(Gdiplus::BitmapData* bitmapData) : bitmapData(bitmapData){}
- void operator()(tbb::blocked_range2d<int> &p) const
- {
- BYTE a = 0xFF;
- BYTE r, g, b, avg;
- byte* line = (byte*)bitmapData->Scan0;
- for (int y = p.rows().begin(); y != p.rows().end(); y++)
- {
- for (int x = p.cols().begin(); x != p.cols().end(); x++)
- {
- byte* pos = line + x * 3;
- Gdiplus::Color pixel = Gdiplus::Color::MakeARGB(a, pos[0], pos[1], pos[2]);
- r = pixel.GetR();
- g = pixel.GetG();
- b = pixel.GetB();
- avg = (r + g + b) / 3;
- pos[0] = avg;
- pos[1] = avg;
- pos[2] = avg;
- }
- line += bitmapData->Stride;
- }
- }
- };
- void rgb_to_grayscale_parallel()
- {
- Gdiplus::GdiplusStartupInput gdiplusStartupInput;
- ULONG_PTR gdiplusToken;
- Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
- Gdiplus::Bitmap myBitmap(filename);
- Gdiplus::Rect rect(0, 0, myBitmap.GetWidth(), myBitmap.GetHeight());
- Gdiplus::BitmapData bitmapData;
- myBitmap.LockBits(&rect, Gdiplus::ImageLockModeWrite | Gdiplus::ImageLockModeRead, PixelFormat24bppRGB, &bitmapData);
- tbb::parallel_for(tbb::blocked_range2d<int, int>(0, myBitmap.GetHeight(), 0, myBitmap.GetWidth()), Grayscale(&bitmapData));
- myBitmap.UnlockBits(&bitmapData);
- CLSID pngEncoderClsId = { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e } };
- myBitmap.Save(filename_output2, &pngEncoderClsId, NULL);
- }
- int main()
- {
- /* serial converting of RGB image to grayscale */
- printf("Serial converting of RGB image to grayscale...\n");
- getchar();
- tbb::tick_count startTime = tbb::tick_count::now();
- rgb_to_grayscale_serial();
- tbb::tick_count endTime = tbb::tick_count::now();
- printf("Time: %lf seconds\n", (endTime - startTime).seconds());
- /* parallel converting of RGB image to grayscale */
- printf("Parallel converting of RGB image to grayscale...\n");
- getchar();
- startTime = tbb::tick_count::now();
- rgb_to_grayscale_parallel();
- endTime = tbb::tick_count::now();
- printf("Time: %lf seconds\n", (endTime - startTime).seconds());
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement