suwako / UnixWallpaper アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
UnixWallpaper / src / uw / orientation.cc
6.2 KB、164行 生 Blame 履歴
1#include <X11/Xlib.h>
2#include <X11/Xutil.h>
3
4#include <stdexcept>
5
6#include "orientation.hh"
7
8namespace uw {
9 Orientation::Orientation(Screen &screen, Image &image)
10 : uwScreen{ screen }, uwImage{ image } {
11 }
12
13 Orientation::~Orientation() {}
14
15 void Orientation::center() {
16 Pixmap pm = XCreatePixmap(uwScreen.getDisplay(), uwScreen.getWindow(),
17 uwScreen.getExtent().width, uwScreen.getExtent().height,
18 uwImage.getExtent().depth);
19
20 XImage *img = XCreateImage(uwScreen.getDisplay(),
21 uwImage.getVisual(), uwImage.getExtent().depth, ZPixmap, 0, nullptr,
22 uwImage.getExtent().width, uwImage.getExtent().height, 32, 0);
23
24 if (!img) throw std::runtime_error("中央にする為にXImageの作成に失敗。");
25
26 std::vector<char>
27 imgData(uwImage.getExtent().width * uwImage.getExtent().height * 4);
28 img->data = imgData.data();
29
30 for (int y = 0; y < uwImage.getExtent().height; y++) {
31 for (int x = 0; x < uwImage.getExtent().width; x++) {
32 int idx = (y * uwImage.getExtent().width + x) * 4;
33
34 uchar blue = uwImage.getImageData()[idx + 2];
35 uchar green = uwImage.getImageData()[idx + 1];
36 uchar red = uwImage.getImageData()[idx + 0];
37 uchar alpha = uwImage.getImageData()[idx + 3];
38
39 ulong pixel = (alpha << 24) | (red << 16) | (green << 8) | blue;
40 XPutPixel(img, x, y, pixel);
41 }
42 }
43
44 XSetForeground(uwScreen.getDisplay(), uwImage.getGC(), 0);
45 XFillRectangle(uwScreen.getDisplay(), pm, uwImage.getGC(), 0, 0,
46 uwScreen.getExtent().width, uwScreen.getExtent().height);
47
48 int xoff = (uwScreen.getExtent().width - uwImage.getExtent().width) / 2;
49 int yoff = (uwScreen.getExtent().height - uwImage.getExtent().height) / 2;
50 if (xoff < 0) xoff = 0;
51 if (yoff < 0) yoff = 0;
52
53 XPutImage(uwScreen.getDisplay(), pm, uwImage.getGC(), img, 0, 0, xoff, yoff,
54 uwImage.getExtent().width, uwImage.getExtent().height);
55
56 XSetWindowBackgroundPixmap(uwScreen.getDisplay(), uwScreen.getWindow(), pm);
57 XClearWindow(uwScreen.getDisplay(), uwScreen.getWindow());
58
59 XFreePixmap(uwScreen.getDisplay(), pm);
60 img->data = nullptr;
61 XDestroyImage(img);
62 }
63
64 void Orientation::fill() {
65 Pixmap pm = XCreatePixmap(uwScreen.getDisplay(), uwScreen.getWindow(),
66 uwScreen.getExtent().width, uwScreen.getExtent().height,
67 uwImage.getExtent().depth);
68
69 XImage *img = XCreateImage(uwScreen.getDisplay(),
70 uwImage.getVisual(), uwImage.getExtent().depth, ZPixmap, 0, nullptr,
71 uwScreen.getExtent().width, uwScreen.getExtent().height, 32, 0);
72
73 if (!img) throw std::runtime_error("スケールする為にXImageの作成に失敗。");
74
75 std::vector<char> sdata(
76 uwScreen.getExtent().width * uwScreen.getExtent().height * 4);
77 img->data = sdata.data();
78 if (!img->data)
79 throw std::runtime_error("スケールしたデータにメモリの役割に失敗。");
80
81 double xScale = (double)uwScreen.getExtent().width / uwImage.getExtent().width;
82 double yScale = (double)uwScreen.getExtent().height / uwImage.getExtent().height;
83
84 for (int y = 0; y < uwScreen.getExtent().height; y++) {
85 for(int x = 0; x < uwScreen.getExtent().width; x++) {
86 int srcx = (x / xScale);
87 int srcy = (y / yScale);
88
89 if (srcx >= uwImage.getExtent().width) srcx = uwImage.getExtent().width - 1;
90 if (srcy >= uwImage.getExtent().height) srcy = uwImage.getExtent().height - 1;
91
92 int sidx = (srcy * uwImage.getExtent().width + srcx) * 4;
93 if (sidx >= uwImage.getExtent().width * uwImage.getExtent().height * 4)
94 continue;
95
96 uchar blue = uwImage.getImageData()[sidx + 2];
97 uchar green = uwImage.getImageData()[sidx + 1];
98 uchar red = uwImage.getImageData()[sidx + 0];
99 uchar alpha = uwImage.getImageData()[sidx + 3];
100
101 ulong pixel = (alpha << 24) | (red << 16) | (green << 8) | blue;
102 XPutPixel(img, x, y, pixel);
103 }
104 }
105
106 XClearWindow(uwScreen.getDisplay(), uwScreen.getWindow());
107 XPutImage(uwScreen.getDisplay(), pm, uwImage.getGC(), img, 0, 0, 0, 0,
108 uwScreen.getExtent().width, uwScreen.getExtent().height);
109
110 XSetWindowBackgroundPixmap(uwScreen.getDisplay(), uwScreen.getWindow(), pm);
111 XClearWindow(uwScreen.getDisplay(), uwScreen.getWindow());
112
113 XFreePixmap(uwScreen.getDisplay(), pm);
114 img->data = nullptr;
115 XDestroyImage(img);
116 }
117
118 void Orientation::tile() {
119 Pixmap pm = XCreatePixmap(uwScreen.getDisplay(), uwScreen.getWindow(),
120 uwScreen.getExtent().width, uwScreen.getExtent().height,
121 uwImage.getExtent().depth);
122
123 XImage *img = XCreateImage(uwScreen.getDisplay(),
124 uwImage.getVisual(), uwImage.getExtent().depth, ZPixmap, 0, nullptr,
125 uwImage.getExtent().width, uwImage.getExtent().height, 32, 0);
126
127 if (!img) throw std::runtime_error("タイルする為にXImageの作成に失敗。");
128
129 std::vector<char>
130 imgData(uwImage.getExtent().width * uwImage.getExtent().height * 4);
131 img->data = imgData.data();
132
133 for (int y = 0; y < uwImage.getExtent().height; y++) {
134 for (int x = 0; x < uwImage.getExtent().width; x++) {
135 int idx = (y * uwImage.getExtent().width + x) * 4;
136
137 uchar blue = uwImage.getImageData()[idx + 2];
138 uchar green = uwImage.getImageData()[idx + 1];
139 uchar red = uwImage.getImageData()[idx + 0];
140 uchar alpha = uwImage.getImageData()[idx + 3];
141
142 ulong pixel = (alpha << 24) | (red << 16) | (green << 8) | blue;
143 XPutPixel(img, x, y, pixel);
144 }
145 }
146
147 XPutImage(uwScreen.getDisplay(), pm, uwImage.getGC(), img, 0, 0, 0, 0,
148 uwImage.getExtent().width, uwImage.getExtent().height);
149
150 XSetWindowBackgroundPixmap(uwScreen.getDisplay(), uwScreen.getWindow(), pm);
151 XClearWindow(uwScreen.getDisplay(), uwScreen.getWindow());
152
153 for (int y = 0; y < uwScreen.getExtent().height; y += uwImage.getExtent().height) {
154 for (int x = 0; x < uwScreen.getExtent().width; x += uwImage.getExtent().width) {
155 XCopyArea(uwScreen.getDisplay(), pm, uwScreen.getWindow(), uwImage.getGC(),
156 0, 0, uwImage.getExtent().width, uwImage.getExtent().height, x, y);
157 }
158 }
159
160 XFreePixmap(uwScreen.getDisplay(), pm);
161 img->data = nullptr;
162 XDestroyImage(img);
163 }
164}