|
6 | 6 | #include <QApplication>
|
7 | 7 | #include <QDesktopWidget>
|
8 | 8 | #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 | +} |
9 | 53 |
|
10 | 54 | int main(int argc, char** argv)
|
11 | 55 | {
|
| 56 | + int decimation = 10; |
12 | 57 |
|
13 | 58 | QApplication app(argc, argv);
|
| 59 | + QElapsedTimer timer; |
14 | 60 |
|
15 |
| - QPixmap originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); |
| 61 | + Image<ColorRgb> screenshot(64,64); |
16 | 62 |
|
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; |
18 | 70 |
|
19 | 71 | return 0;
|
20 | 72 | }
|
0 commit comments