-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathgetPixelsSingleton.cpp
More file actions
116 lines (82 loc) · 2.47 KB
/
getPixelsSingleton.cpp
File metadata and controls
116 lines (82 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <cstdint>
#include <cstring>
#include <vector>
#include <iostream>
#include <memory>
// g++ -std=c++14 getPixelsSingleton.cpp -lX11 -o frame2
class xPixelHandler
{
private:
static std::unique_ptr<xPixelHandler> instance;
struct _cons {explicit _cons() = default; };
Display* display;
Window root;
public:
int width;
int height;
// std::vector<unsigned int> Pixels;
xPixelHandler(_cons)
{
display = XOpenDisplay(nullptr);
root = DefaultRootWindow(display);
}
static std::unique_ptr<xPixelHandler> instanceFactory()
{
return std::make_unique<xPixelHandler>(_cons{});
}
static std::unique_ptr<xPixelHandler> &getInstance();
~xPixelHandler()
{
XCloseDisplay(display);
}
int returnWidth()
{
XWindowAttributes attributes = {0};
XGetWindowAttributes(display, root, &attributes);
width = attributes.width;
return width;
}
int returnHeight()
{
XWindowAttributes attributes = {0};
XGetWindowAttributes(display, root, &attributes);
height = attributes.height;
return height;
}
void getPixels(std::vector<unsigned int>& Pixels, int& Width, int& Height)
{
XWindowAttributes attributes = {0};
XGetWindowAttributes(display, root, &attributes);
Width = attributes.width;
Height = attributes.height;
XImage* img = XGetImage(display, root, 0, 0 , Width, Height, AllPlanes, ZPixmap);
Pixels.resize(Width * Height * 4);
memcpy(&Pixels[0], img->data, Pixels.size());
XDestroyImage(img);
}
};
std::unique_ptr<xPixelHandler> xPixelHandler::instance = NULL;
std::unique_ptr<xPixelHandler>& xPixelHandler::getInstance()
{
if(instance == NULL)
{
instance = xPixelHandler::instanceFactory();
}
return instance;
}
int main()
{
int Width = 0;
int Height = 0;
int Bpp = 0;
std::vector<unsigned int> Pixels;
std::unique_ptr<xPixelHandler> &p = xPixelHandler::getInstance();
p->getPixels(Pixels, Width, Height);
std::cout<<p->returnWidth() * p->returnHeight()<<std::endl;
std::cout<<Pixels.size()<<std::endl;
//for(auto& s : Pixels)
// std::cout<<s<<"\n";
return 0;
}