-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImage.cpp
More file actions
146 lines (107 loc) · 3.4 KB
/
Image.cpp
File metadata and controls
146 lines (107 loc) · 3.4 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// Telescope - graphical task switcher
//
// (c) Ilya Skriblovsky, 2010
// <Ilya.Skriblovsky@gmail.com>
//
// $Id$
#include "Image.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <Imlib2.h>
static Visual *defaultVisual = 0;
static XVisualInfo rgbaVisual;
static XRenderPictFormat *defaultFormat;
static XRenderPictFormat *rgbaFormat;
static Colormap colormap;
void requestRGBAVisual(Display *dpy)
{
if (defaultVisual == 0)
{
// Initializing
int scr = DefaultScreen(dpy);
defaultVisual = DefaultVisual(dpy, scr);
colormap = DefaultColormap(dpy, scr);
defaultFormat = XRenderFindVisualFormat(dpy, defaultVisual);
if (XMatchVisualInfo(dpy, scr, 32, TrueColor, &rgbaVisual) == 0)
{
fprintf(stderr, "Cannot find rgba visual\n");
return;
}
rgbaFormat = XRenderFindVisualFormat(dpy, rgbaVisual.visual);
}
}
Image::Image()
:_dpy(0), _pixmap(0), _picture(0),
_width(0), _height(0), _repeatType(RepeatNone),
_filename(0)
{
}
Image::Image(Display *dpy, const char *filename)
:_dpy(dpy), _pixmap(0), _picture(0),
_width(0), _height(0), _repeatType(RepeatNone)
{
_filename = strdup(filename);
requestRGBAVisual(_dpy);
Imlib_Image image = imlib_load_image(filename);
if (image == 0)
{
fprintf(stderr, "Cannot load image: %s\n", filename);
return;
}
imlib_context_set_image(image);
_width = imlib_image_get_width();
_height = imlib_image_get_height();
// Making image with premultiplied alpha
Imlib_Image premul = imlib_create_image(_width, _height);
imlib_context_set_image(premul);
imlib_context_set_color(0, 0, 0, 255);
imlib_image_fill_rectangle(0, 0, _width, _height);
imlib_context_set_blend(1);
imlib_blend_image_onto_image(image, 0, 0, 0, _width, _height, 0, 0, _width, _height);
imlib_image_copy_alpha_to_image(image, 0, 0);
_pixmap = XCreatePixmap(_dpy, RootWindow(_dpy, DefaultScreen(_dpy)), _width, _height, 32);
imlib_context_set_display(_dpy);
imlib_context_set_colormap(colormap);
imlib_context_set_visual(rgbaVisual.visual);
imlib_context_set_drawable(_pixmap);
imlib_context_set_blend(0);
imlib_render_image_on_drawable(0, 0);
imlib_free_image();
imlib_context_set_image(image);
imlib_free_image();
_picture = XRenderCreatePicture(_dpy, _pixmap, rgbaFormat, 0, 0);
}
Image::Image(Display *dpy, int width, int height, int depth)
:_dpy(dpy), _pixmap(0), _picture(0),
_width(width), _height(height), _repeatType(RepeatNone),
_filename(0)
{
requestRGBAVisual(_dpy);
_pixmap = XCreatePixmap(_dpy, RootWindow(_dpy, DefaultScreen(_dpy)), _width, _height, depth);
_picture = XRenderCreatePicture(_dpy, _pixmap,
depth == 32 ? rgbaFormat : defaultFormat,
0, 0
);
}
Image::~Image()
{
if (_picture) XRenderFreePicture(_dpy, _picture);
if (_pixmap) XFreePixmap(_dpy, _pixmap);
free(_filename);
}
void Image::setRepeatType(int repeatType)
{
if (! _picture) return;
XRenderPictureAttributes pictureAttrs;
pictureAttrs.repeat = repeatType;
XRenderChangePicture(_dpy, _picture, CPRepeat, &pictureAttrs);
}
void Image::clear()
{
GC gc = XCreateGC(_dpy, _pixmap, 0, 0);
XSetGraphicsExposures(_dpy, gc, false);
XFillRectangle(_dpy, _pixmap, gc, 0, 0, _width, _height);
XFreeGC(_dpy, gc);
}