forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
region.cpp
176 lines (145 loc) · 3.61 KB
/
region.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Aseprite Gfx Library
// Copyright (C) 2001-2013, 2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <pixman.h>
#include "gfx/point.h"
#include "gfx/region.h"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
namespace gfx {
inline Rect to_rect(const pixman_box32& extends)
{
return Rect(
extends.x1, extends.y1,
extends.x2 - extends.x1,
extends.y2 - extends.y1);
}
Region::Region()
{
pixman_region32_init(&m_region);
}
Region::Region(const Region& copy)
{
pixman_region32_init(&m_region);
pixman_region32_copy(&m_region, ©.m_region);
}
Region::Region(const Rect& rect)
{
if (!rect.isEmpty())
pixman_region32_init_rect(&m_region, rect.x, rect.y, rect.w, rect.h);
else
pixman_region32_init(&m_region);
}
Region::~Region()
{
pixman_region32_fini(&m_region);
}
Region& Region::operator=(const Rect& rect)
{
if (!rect.isEmpty()) {
pixman_box32 box = { rect.x, rect.y, rect.x2(), rect.y2() };
pixman_region32_reset(&m_region, &box);
}
else
pixman_region32_clear(&m_region);
return *this;
}
Region& Region::operator=(const Region& copy)
{
pixman_region32_copy(&m_region, ©.m_region);
return *this;
}
Region::iterator Region::begin()
{
iterator it;
it.m_ptr = pixman_region32_rectangles(&m_region, NULL);
return it;
}
Region::iterator Region::end()
{
iterator it;
it.m_ptr = pixman_region32_rectangles(&m_region, NULL) + size();
return it;
}
Region::const_iterator Region::begin() const
{
const_iterator it;
it.m_ptr = pixman_region32_rectangles(&m_region, NULL);
return it;
}
Region::const_iterator Region::end() const
{
const_iterator it;
it.m_ptr = pixman_region32_rectangles(&m_region, NULL) + size();
return it;
}
bool Region::isEmpty() const
{
return pixman_region32_not_empty(&m_region) ? false: true;
}
Rect Region::bounds() const
{
return to_rect(*pixman_region32_extents(&m_region));
}
std::size_t Region::size() const
{
return pixman_region32_n_rects(&m_region);
}
void Region::clear()
{
pixman_region32_clear(&m_region);
}
void Region::offset(int dx, int dy)
{
pixman_region32_translate(&m_region, dx, dy);
}
void Region::offset(const PointT<int>& delta)
{
pixman_region32_translate(&m_region, delta.x, delta.y);
}
Region& Region::createIntersection(const Region& a, const Region& b)
{
pixman_region32_intersect(&m_region, &a.m_region, &b.m_region);
return *this;
}
Region& Region::createUnion(const Region& a, const Region& b)
{
pixman_region32_union(&m_region, &a.m_region, &b.m_region);
return *this;
}
Region& Region::createSubtraction(const Region& a, const Region& b)
{
pixman_region32_subtract(&m_region, &a.m_region, &b.m_region);
return *this;
}
bool Region::contains(const PointT<int>& pt) const
{
return pixman_region32_contains_point(&m_region, pt.x, pt.y, NULL) ? true: false;
}
Region::Overlap Region::contains(const Rect& rect) const
{
static_assert(
int(Out) == int(PIXMAN_REGION_OUT) &&
int(In) == int(PIXMAN_REGION_IN) &&
int(Part) == int(PIXMAN_REGION_PART), "Pixman constants have changed");
pixman_box32 box = { rect.x, rect.y, rect.x2(), rect.y2() };
return (Region::Overlap)pixman_region32_contains_rectangle(&m_region, &box);
}
Rect Region::operator[](int i)
{
assert(i >= 0 && i < (int)size());
return to_rect(pixman_region32_rectangles(&m_region, NULL)[i]);
}
const Rect Region::operator[](int i) const
{
assert(i >= 0 && i < (int)size());
return to_rect(pixman_region32_rectangles(&m_region, NULL)[i]);
}
} // namespace gfx