Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit c28e098

Browse files
committed
Added qt screenshot test
1 parent b8c60cf commit c28e098

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

test/TestQtScreenshot.cpp

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,67 @@
66
#include <QApplication>
77
#include <QDesktopWidget>
88
#include <QPixmap>
9+
#include <Qfile>
10+
#include <QRgb>
11+
12+
#include <QElapsedTimer>
13+
14+
// Utils includes
15+
#include <utils/Image.h>
16+
#include <utils/ColorRgb.h>
17+
18+
void createScreenshot(const int cropHorizontal, const int cropVertical, const int decimation, Image<ColorRgb> & image)
19+
{
20+
// Create the full size screenshot
21+
const QRect screenSize = QApplication::desktop()->screenGeometry();
22+
const int croppedWidth = screenSize.width() - 2*cropVertical;
23+
const int croppedHeight = screenSize.height() - 2*cropHorizontal;
24+
const QPixmap fullSizeScreenshot = QPixmap::grabWindow(QApplication::desktop()->winId(), cropVertical, cropHorizontal, croppedWidth, croppedHeight);
25+
26+
// Scale the screenshot to the required size
27+
const int width = fullSizeScreenshot.width()/decimation;
28+
const int height = fullSizeScreenshot.height()/decimation;
29+
const QPixmap scaledScreenshot = fullSizeScreenshot.scaled(width, height, Qt::IgnoreAspectRatio, Qt::FastTransformation);
30+
31+
// Convert the QPixmap to QImage in order to get out RGB values
32+
const QImage qImage = scaledScreenshot.toImage();
33+
34+
// Make sure that the output image has the right size
35+
image.resize(width, height);
36+
37+
// Copy the data into the output image
38+
for (int y=0; y<qImage.height(); ++y)
39+
{
40+
for (int x=0; x<qImage.width(); ++x)
41+
{
42+
// Get the pixel at [x;y] (format int32 #AARRGGBB)
43+
const QRgb inPixel = qImage.pixel(x,y);
44+
45+
// Copy the color channels into the output pixel
46+
ColorRgb & outPixel = image(x,y);
47+
outPixel.red = (inPixel & 0x00ff0000) >> 16;
48+
outPixel.green = (inPixel & 0x0000ff00) >> 8;
49+
outPixel.blue = (inPixel & 0x000000ff);
50+
}
51+
}
52+
}
953

1054
int main(int argc, char** argv)
1155
{
56+
int decimation = 10;
1257

1358
QApplication app(argc, argv);
59+
QElapsedTimer timer;
1460

15-
QPixmap originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
61+
Image<ColorRgb> screenshot(64,64);
1662

17-
std::cout << "Grabbed image: [" << originalPixmap.width() << "; " << originalPixmap.height() << "]" << std::endl;
63+
int loopCnt = 100;
64+
timer.start();
65+
for (int i=0; i<loopCnt; ++i)
66+
{
67+
createScreenshot(0,0, decimation, screenshot);
68+
}
69+
std::cout << "Time required for single screenshot: " << timer.elapsed()/loopCnt << "ms" << std::endl;
1870

1971
return 0;
2072
}

test/TestX11Performance.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void foo_1(int pixelDecimation)
6767
// Cleanup allocated resources of the X11 grab
6868
XDestroyImage(xImage);
6969

70-
std::cout << "Time required: " << timer.elapsed() << " us" << std::endl;
70+
std::cout << "Time required: " << timer.elapsed() << " ms" << std::endl;
7171

7272
XCloseDisplay(x11Display);
7373
}
@@ -125,7 +125,7 @@ void foo_2(int pixelDecimation)
125125
XDestroyImage(xImage);
126126
}
127127
}
128-
std::cout << "Time required: " << timer.elapsed() << " us" << std::endl;
128+
std::cout << "Time required: " << timer.elapsed() << " ms" << std::endl;
129129

130130

131131
XCloseDisplay(x11Display);

0 commit comments

Comments
 (0)