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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#include "StarImageMetadataDatabase.hpp"
#include "StarFile.hpp"
#include "StarImage.hpp"
#include "StarImageProcessing.hpp"
#include "StarLogging.hpp"
#include "StarEncode.hpp"
#include "StarGameTypes.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
namespace Star {
ImageMetadataDatabase::ImageMetadataDatabase() {
MutexLocker locker(m_mutex);
int timeSmear = 2000;
int64_t timeToLive = 60000;
m_sizeCache.setTimeSmear(timeSmear);
m_spacesCache.setTimeSmear(timeSmear);
m_regionCache.setTimeSmear(timeSmear);
m_sizeCache.setTimeToLive(timeToLive);
m_spacesCache.setTimeToLive(timeToLive);
m_regionCache.setTimeToLive(timeToLive);
}
Vec2U ImageMetadataDatabase::imageSize(AssetPath const& path) const {
MutexLocker locker(m_mutex);
if (auto cached = m_sizeCache.ptr(path))
return *cached;
locker.unlock();
Vec2U size = calculateImageSize(path);
locker.lock();
m_sizeCache.set(path, size);
return size;
}
List<Vec2I> ImageMetadataDatabase::imageSpaces(AssetPath const& path, Vec2F position, float fillLimit, bool flip) const {
SpacesEntry key = make_tuple(path, Vec2I::round(position), fillLimit, flip);
MutexLocker locker(m_mutex);
if (auto cached = m_spacesCache.ptr(key)) {
return *cached;
}
auto filteredPath = filterProcessing(path);
SpacesEntry filteredKey = make_tuple(filteredPath, Vec2I::round(position), fillLimit, flip);
if (auto cached = m_spacesCache.ptr(filteredKey)) {
m_spacesCache.set(key, *cached);
return *cached;
}
locker.unlock();
auto image = Root::singleton().assets()->image(filteredPath);
int imageWidth = image->width();
int imageHeight = image->height();
Vec2I min((position / TilePixels).floor());
Vec2I max(((Vec2F(imageWidth, imageHeight) + position) / TilePixels).ceil());
List<Vec2I> spaces;
for (int yspace = min[1]; yspace < max[1]; ++yspace) {
for (int xspace = min[0]; xspace < max[0]; ++xspace) {
float fillRatio = 0.0f;
for (int y = 0; y < (int)TilePixels; ++y) {
int ypixel = round(yspace * (int)TilePixels + y - position[1]);
if (ypixel < 0 || ypixel >= imageHeight)
continue;
for (int x = 0; x < (int)TilePixels; ++x) {
int xpixel = round(xspace * (int)TilePixels + x - position[0]);
if (flip)
xpixel = imageWidth - 1 - xpixel;
if (xpixel < 0 || xpixel >= imageWidth)
continue;
if (image->get(xpixel, ypixel)[3] > 0)
fillRatio += 1.0f / square(TilePixels);
}
}
if (fillRatio >= fillLimit)
spaces.append(Vec2I(xspace, yspace));
}
}
locker.lock();
m_spacesCache.set(key, spaces);
m_spacesCache.set(filteredKey, spaces);
return spaces;
}
RectU ImageMetadataDatabase::nonEmptyRegion(AssetPath const& path) const {
MutexLocker locker(m_mutex);
if (auto cached = m_regionCache.ptr(path)) {
return *cached;
}
auto filteredPath = filterProcessing(path);
if (auto cached = m_regionCache.ptr(filteredPath)) {
m_regionCache.set(path, *cached);
return *cached;
}
locker.unlock();
auto image = Root::singleton().assets()->image(filteredPath);
RectU region = RectU::null();
image->forEachPixel([®ion](unsigned x, unsigned y, Vec4B const& pixel) {
if (pixel[3] > 0)
region.combine(RectU::withSize({x, y}, {1, 1}));
});
locker.lock();
m_regionCache.set(path, region);
m_regionCache.set(filteredPath, region);
return region;
}
void ImageMetadataDatabase::cleanup() const {
MutexLocker locker(m_mutex);
m_sizeCache.cleanup();
m_spacesCache.cleanup();
m_regionCache.cleanup();
}
AssetPath ImageMetadataDatabase::filterProcessing(AssetPath const& path) {
AssetPath newPath = { path.basePath, path.subPath, {} };
String filtered;
for (auto& directives : path.directives.list())
directives.loadOperations();
path.directives.forEach([&](auto const& entry, Directives const& directives) {
ImageOperation const& operation = entry.operation;
if (!(operation.is<HueShiftImageOperation>() ||
operation.is<SaturationShiftImageOperation>() ||
operation.is<BrightnessMultiplyImageOperation>() ||
operation.is<FadeToColorImageOperation>() ||
operation.is<ScanLinesImageOperation>() ||
operation.is<SetColorImageOperation>())) {
filtered += "?";
filtered += entry.string(*directives);
}
});
newPath.directives = std::move(filtered);
return newPath;
}
Vec2U ImageMetadataDatabase::calculateImageSize(AssetPath const& path) const {
// Carefully calculate an image's size while trying not to actually load it.
// In error cases, this will fall back to calling Assets::image, so that image
// can possibly produce a missing image asset or properly report the error.
auto assets = Root::singleton().assets();
auto fallback = [&assets, &path]() {
return assets->image(path)->size();
};
if (!assets->assetExists(path.basePath)) {
return fallback();
}
Vec2U imageSize;
if (path.subPath) {
auto frames = assets->imageFrames(path.basePath);
if (!frames)
return fallback();
if (auto rect = frames->getRect(*path.subPath))
imageSize = rect->size();
else
return fallback();
} else {
// We ensure that the base image size is cached even when given directives,
// so we don't have to call Image::readPngMetadata on the same file more
// than once.
MutexLocker locker(m_mutex);
if (auto size = m_sizeCache.ptr(path.basePath)) {
imageSize = *size;
} else {
locker.unlock();
auto file = assets->openFile(path.basePath);
if (Image::isPng(file))
imageSize = get<0>(Image::readPngMetadata(file));
else
imageSize = fallback();
locker.lock();
m_sizeCache.set(path.basePath, imageSize);
}
}
struct OperationSizeAdjust {
Vec2U& imageSize;
bool hasError;
OperationSizeAdjust(Vec2U& size) : imageSize(size), hasError(false) {};
void operator()(NullImageOperation const&) {}
void operator()(ErrorImageOperation const&) {}
void operator()(HueShiftImageOperation const&) {}
void operator()(SaturationShiftImageOperation const&) {}
void operator()(BrightnessMultiplyImageOperation const&) {}
void operator()(FadeToColorImageOperation const&) {}
void operator()(ScanLinesImageOperation const&) {}
void operator()(SetColorImageOperation const&) {}
void operator()(ColorReplaceImageOperation const&) {}
void operator()(AlphaMaskImageOperation const&) {}
void operator()(BlendImageOperation const&) {}
void operator()(MultiplyImageOperation const&) {}
void operator()(BorderImageOperation const& bio) {
imageSize += Vec2U::filled(bio.pixels * 2);
}
void operator()(ScaleImageOperation const& sio) {
imageSize = Vec2U::round(vmult(Vec2F(imageSize), sio.scale));
}
void operator()(CropImageOperation const& cio) {
if (cio.subset.isEmpty() ||
cio.subset.xMin() < 0 ||
cio.subset.yMin() < 0 ||
(unsigned)cio.subset.xMax() > imageSize[0] ||
(unsigned)cio.subset.yMax() > imageSize[1]) {
hasError = true;
} else {
imageSize = Vec2U(cio.subset.size());
}
}
void operator()(FlipImageOperation const&) {}
};
OperationSizeAdjust osa(imageSize);
for (auto& directives : path.directives.list())
directives.loadOperations();
bool complete = path.directives.forEachAbortable([&](auto const& entry, Directives const&) -> bool {
entry.operation.call(osa);
return !osa.hasError;
});
if (!complete)
return fallback();
return imageSize;
}
}
|