2020#include " opencv2/opencv.hpp"
2121#include < opencv2/imgcodecs.hpp>
2222
23+ #include < tbb/parallel_for.h>
24+ #include < tbb/mutex.h>
25+
2326#include " utils.h"
2427
2528bool init_sdl (SDL_Window** window_ptr, SDL_Surface** surface_ptr, SDL_Renderer** renderer_ptr,
@@ -35,14 +38,22 @@ int main( int argc, char* args[] ) {
3538 IMG_Init (IMG_INIT_PNG);
3639
3740 std::vector<SrcImage> src_images;
38- load_images (" imgsimple " , src_images);
41+ load_images (" hemtaiClean " , src_images);
3942 int recInd = src_images.size ()-1 ;
4043 SrcImage reconstructed = src_images[recInd];
4144 reconstructed.data = new uint8_t [reconstructed.width * reconstructed.height * 4 ];
4245 memset (reconstructed.data , 0xFFFFFF00 , reconstructed.width * reconstructed.height *4 );
4346 RotatePixel_t *pPstBase = static_cast <RotatePixel_t*>((void *)reconstructed.data );
4447 RotatePixel_t *pDstBase = static_cast <RotatePixel_t*>((void *)src_images[recInd].data );
4548
49+ int best_sad;
50+ int bestX;
51+ int bestY;
52+ float bestA;
53+ float bestS;
54+
55+ tbb::mutex best_mutex;
56+
4657 Timer t;
4758 t.start ();
4859 for (int i = 0 ; i < 100000000 ; i++) {
@@ -51,31 +62,37 @@ int main( int argc, char* args[] ) {
5162 continue ;
5263 }
5364 RotatePixel_t *pSrcBase = static_cast <RotatePixel_t*>((void *)src_images[index].data );
54- int best_sad = INT_MAX;
55- int bestX;
56- int bestY;
57- float bestA;
58- float bestS;
59- for (int j = 0 ; j < 100 ; j++) {
60- int fDstCX = rand ()%(src_images[recInd].width );
61- int fDstCY = rand ()%(src_images[recInd].height );
62- float fAngle = rand ()%(314 ) / 100.0 ;
63- float fScale = (rand ()%(1500 ) + 1 ) / 1000.0 ;
64- sadPair genQuality = RotateDrawClipSad (
65- pDstBase, src_images[recInd].width , src_images[recInd].height , src_images[recInd].pitch ,
66- pSrcBase, src_images[index].width , src_images[index].height , src_images[index].pitch ,
67- pPstBase,
68- fDstCX , fDstCY ,
69- 0 , 0 ,
70- fAngle , fScale );
71- if (genQuality.sad1 < genQuality.sad2 && genQuality.sad1 < best_sad) {
72- best_sad = genQuality.sad1 ;
73- bestX = fDstCX ;
74- bestY = fDstCY ;
75- bestA = fAngle ;
76- bestS = fScale ;
65+
66+ best_sad = INT_MAX;
67+ tbb::parallel_for ( tbb::blocked_range<int >(0 , 100 , 1 ),
68+ [&](tbb::blocked_range<int > r) {
69+ for (int j = r.begin (); j < r.end (); j++) {
70+ int fDstCX = rand ()%(src_images[recInd].width );
71+ int fDstCY = rand ()%(src_images[recInd].height );
72+ float fAngle = rand ()%(314 ) / 100.0 ;
73+ float fScale = (rand ()%(1500 ) + 1 ) / 1000.0 ;
74+ sadPair genQuality = RotateDrawClipSad (
75+ pDstBase, src_images[recInd].width , src_images[recInd].height , src_images[recInd].pitch ,
76+ pSrcBase, src_images[index].width , src_images[index].height , src_images[index].pitch ,
77+ pPstBase,
78+ fDstCX , fDstCY ,
79+ 0 , 0 ,
80+ fAngle , fScale );
81+
82+
83+ tbb::mutex::scoped_lock lock;
84+ lock.acquire (best_mutex);
85+ if (genQuality.sad1 < genQuality.sad2 && genQuality.sad1 < best_sad) {
86+ best_sad = genQuality.sad1 ;
87+ bestX = fDstCX ;
88+ bestY = fDstCY ;
89+ bestA = fAngle ;
90+ bestS = fScale ;
91+ }
92+
93+ lock.release ();
7794 }
78- }
95+ });
7996
8097 if (best_sad != INT_MAX) {
8198 RotateDrawClip (
@@ -85,11 +102,11 @@ int main( int argc, char* args[] ) {
85102 0 , 0 ,
86103 bestA, bestS );
87104 }
88- if (i % 100 == 0 ) {
105+ if (i % 500 == 0 ) {
89106 while (SDL_PollEvent (&event)) {
90107 }
91108 sdl_draw (renderer, reconstructed);
92- std::cout << " currently at " << i << " 100 gens took " << t.get () <<
109+ std::cout << " currently at " << i << " 500 gens took " << t.get () <<
93110 " \n " << " sad is " << compute_sad (src_images[0 ].data ,
94111 reconstructed.data ,
95112 reconstructed.width * reconstructed.height * 4 ) << " \n " ;
0 commit comments