1515
1616unsigned Widget::height;
1717unsigned Widget::width;
18+ QImage Widget::pic;
1819
1920using namespace std ;
2021
@@ -64,7 +65,7 @@ Widget::~Widget()
6465 exit (0 );
6566}
6667
67- int Widget::computeFitness (QImage& target, QRect box)
68+ int Widget::computeFitness (const QImage& target, const QRect box)
6869{
6970 unsigned minx, maxx, miny, maxy;
7071 if (box.isNull ())
@@ -81,33 +82,39 @@ int Widget::computeFitness(QImage& target, QRect box)
8182 maxy = miny + box.height ();
8283 }
8384
84- QVector<QRgb*> originalLines;
85- QVector<QRgb*> targetLines ;
85+ static QVector<QRgb*> originalLines;
86+ originalLines. resize (maxy-miny) ;
8687 for (unsigned i=miny; i<maxy; i++)
87- originalLines.append ((QRgb*)pic.scanLine (i));
88+ originalLines[i] = ((QRgb*)pic.scanLine (i));
89+ static QVector<QRgb*> targetLines;
90+ targetLines.resize (maxy-miny);
8891 for (unsigned i=miny; i<maxy; i++)
89- targetLines. append ((QRgb*)target.scanLine (i));
92+ targetLines[i] = ((QRgb*)target.scanLine (i));
9093
91- auto computeSlice = [&](unsigned start, unsigned end)
94+ auto computeSlice = [&](const unsigned start, const unsigned end)
9295 {
9396 unsigned partFitness=0 ;
9497 for (unsigned i=start-miny; i<end-miny; i++)
9598 {
9699 // Sum of the differences of each pixel's color
97100 for (unsigned j=minx; j<maxx; j++)
98101 {
99- QRgb ocolor = originalLines.at (i)[j];
100- int oR=qRed (ocolor), oG=qGreen (ocolor) , oB=qBlue (ocolor);
101- QRgb tcolor = targetLines.at (i)[j];
102- int tR=qRed (tcolor), tG=qGreen (tcolor) , tB=qBlue (tcolor);
102+ unsigned ocolor = originalLines.at (i)[j];
103+ int oR=(ocolor>> 16 ), oG=(ocolor>> 8 )& 0xFF , oB=(ocolor& 0xFF );
104+ unsigned tcolor = targetLines.at (i)[j];
105+ int tR=(tcolor>> 16 ), tG=(tcolor>> 8 )& 0xFF , tB=(tcolor& 0xFF );
103106 partFitness += abs (tR-oR)+abs (tG-oG)+abs (tB-oB);
104107 }
105108 }
106109 return partFitness;
107110 };
108- QFuture<unsigned > firstSlice = QtConcurrent::run (computeSlice, miny, maxy/2 );
109- unsigned fitness = computeSlice (maxy/2 , maxy);
110- fitness += firstSlice.result ();
111+ QFuture<unsigned > slice1 = QtConcurrent::run (computeSlice, miny, maxy/4 );
112+ QFuture<unsigned > slice2 = QtConcurrent::run (computeSlice, maxy/4 , 2 *maxy/4 );
113+ QFuture<unsigned > slice3 = QtConcurrent::run (computeSlice, 2 *maxy/4 , 3 *maxy/4 );
114+ unsigned fitness = computeSlice (3 *maxy/4 , maxy);
115+ fitness += slice1.result ();
116+ fitness += slice2.result ();
117+ fitness += slice3.result ();
111118
112119 return fitness;
113120}
0 commit comments