-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImageUtil.cpp
74 lines (67 loc) · 2 KB
/
ImageUtil.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
#include "ImageUtil.h"
namespace sc2util {
bool isSet(const sc2::GameInfo & info, const sc2::ImageData & grid, const sc2::Point2D & point)
{
sc2::Point2DI pointI((int)point.x, (int)point.y);
return isSet(info, grid, pointI);
}
bool isSet(const sc2::GameInfo & info, const sc2::ImageData & grid, const sc2::Point2DI & pointI)
{
if (pointI.x < 0 || pointI.x >= grid.width || pointI.y < 0 || pointI.y >= grid.height)
{
return false;
}
if (grid.bits_per_pixel == 1) {
div_t idx = div(pointI.x + pointI.y * grid.width, 8);
bool dst = (grid.data[idx.quot] >> (7 - idx.rem)) & 1;
return dst;
}
else {
// Image data is stored with an upper left origin.
char dst = grid.data[pointI.x + pointI.y * grid.width];
return dst;
}
}
bool set(sc2::GameInfo & info, sc2::ImageData & grid, const sc2::Point2D & point, bool b)
{
sc2::Point2DI pointI((int)point.x, (int)point.y);
return set(info, grid, pointI, b);
}
bool set(sc2::GameInfo & info, sc2::ImageData & grid, const sc2::Point2DI & pointI, bool b)
{
if (pointI.x < 0 || pointI.x >= grid.width || pointI.y < 0 || pointI.y >= grid.height)
{
return false;
}
if (grid.bits_per_pixel == 1) {
div_t idx = div(pointI.x + pointI.y * grid.width, 8);
char mask = 1 << (7 - idx.rem);
if (b) {
grid.data[idx.quot] |= mask;
}
else {
grid.data[idx.quot] ^= ~mask;
}
return true;
}
else {
// Image data is stored with an upper left origin.
grid.data[pointI.x + pointI.y * grid.width] = b;
return true;
}
}
bool setBuildingAt(sc2::GameInfo & info, sc2::ImageData & grid, const sc2::Point2D & pos, int footprint, bool b)
{
using namespace sc2;
float fx = pos.x - (float)footprint / 2.0f;
float fy = pos.y - (float)footprint / 2.0f;
Point2D point(fx, fy);
auto inzone = sc2::Point2DI((int)point.x, (int)point.y);
for (int y = 0; y < footprint; y++) {
for (int x = 0; x < footprint; x++) {
set(info, grid, Point2DI(inzone.x + x, inzone.y + y), !b);
}
}
return true;
}
}