2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* bit_mask.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2019-01-03 10:41:35 +00:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "bit_mask.h"
|
|
|
|
#include "io/image_loader.h"
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BitMap::create(const Size2 &p_size) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_size.width < 1);
|
|
|
|
ERR_FAIL_COND(p_size.height < 1);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
width = p_size.width;
|
|
|
|
height = p_size.height;
|
|
|
|
bitmask.resize(((width * height) / 8) + 1);
|
2017-11-25 03:07:54 +00:00
|
|
|
zeromem(bitmask.ptrw(), bitmask.size());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
void BitMap::create_from_image_alpha(const Ref<Image> &p_image) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
ERR_FAIL_COND(p_image.is_null() || p_image->empty());
|
|
|
|
Ref<Image> img = p_image->duplicate();
|
|
|
|
img->convert(Image::FORMAT_LA8);
|
|
|
|
ERR_FAIL_COND(img->get_format() != Image::FORMAT_LA8);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
create(Size2(img->get_width(), img->get_height()));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
PoolVector<uint8_t>::Read r = img->get_data().read();
|
2017-11-25 03:07:54 +00:00
|
|
|
uint8_t *w = bitmask.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < width * height; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int bbyte = i / 8;
|
2014-02-10 01:10:30 +00:00
|
|
|
int bbit = i % 8;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (r[i * 2])
|
|
|
|
w[bbyte] |= (1 << bbit);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BitMap::set_bit_rect(const Rect2 &p_rect, bool p_value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Rect2i current = Rect2i(0, 0, width, height).clip(p_rect);
|
2017-11-25 03:07:54 +00:00
|
|
|
uint8_t *data = bitmask.ptrw();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-03 22:25:13 +00:00
|
|
|
for (int i = current.position.x; i < current.position.x + current.size.x; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-06-03 22:25:13 +00:00
|
|
|
for (int j = current.position.y; j < current.position.y + current.size.y; j++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int ofs = width * j + i;
|
2017-03-05 15:44:50 +00:00
|
|
|
int bbyte = ofs / 8;
|
2014-02-10 01:10:30 +00:00
|
|
|
int bbit = ofs % 8;
|
|
|
|
|
|
|
|
uint8_t b = data[bbyte];
|
|
|
|
|
|
|
|
if (p_value)
|
2017-03-05 15:44:50 +00:00
|
|
|
b |= (1 << bbit);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2018-01-09 16:31:30 +00:00
|
|
|
b &= ~(1 << bbit);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
data[bbyte] = b;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int BitMap::get_true_bit_count() const {
|
|
|
|
|
|
|
|
int ds = bitmask.size();
|
|
|
|
const uint8_t *d = bitmask.ptr();
|
2017-03-05 15:44:50 +00:00
|
|
|
int c = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
//fast, almot branchless version
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < ds; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
c += (d[i] & (1 << 7)) >> 7;
|
|
|
|
c += (d[i] & (1 << 6)) >> 6;
|
|
|
|
c += (d[i] & (1 << 5)) >> 5;
|
|
|
|
c += (d[i] & (1 << 4)) >> 4;
|
|
|
|
c += (d[i] & (1 << 3)) >> 3;
|
|
|
|
c += (d[i] & (1 << 2)) >> 2;
|
|
|
|
c += d[i] & 1;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BitMap::set_bit(const Point2 &p_pos, bool p_value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-01-06 20:38:39 +00:00
|
|
|
int x = p_pos.x;
|
|
|
|
int y = p_pos.y;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(x, width);
|
|
|
|
ERR_FAIL_INDEX(y, height);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int ofs = width * y + x;
|
2017-03-05 15:44:50 +00:00
|
|
|
int bbyte = ofs / 8;
|
2014-02-10 01:10:30 +00:00
|
|
|
int bbit = ofs % 8;
|
|
|
|
|
|
|
|
uint8_t b = bitmask[bbyte];
|
|
|
|
|
|
|
|
if (p_value)
|
2017-03-05 15:44:50 +00:00
|
|
|
b |= (1 << bbit);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2018-01-09 16:31:30 +00:00
|
|
|
b &= ~(1 << bbit);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bitmask[bbyte] = b;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool BitMap::get_bit(const Point2 &p_pos) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int x = Math::fast_ftoi(p_pos.x);
|
|
|
|
int y = Math::fast_ftoi(p_pos.y);
|
|
|
|
ERR_FAIL_INDEX_V(x, width, false);
|
|
|
|
ERR_FAIL_INDEX_V(y, height, false);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int ofs = width * y + x;
|
2017-03-05 15:44:50 +00:00
|
|
|
int bbyte = ofs / 8;
|
2014-02-10 01:10:30 +00:00
|
|
|
int bbit = ofs % 8;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return (bitmask[bbyte] & (1 << bbit)) != 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Size2 BitMap::get_size() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return Size2(width, height);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BitMap::_set_data(const Dictionary &p_d) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!p_d.has("size"));
|
|
|
|
ERR_FAIL_COND(!p_d.has("data"));
|
|
|
|
|
|
|
|
create(p_d["size"]);
|
2017-03-05 15:44:50 +00:00
|
|
|
bitmask = p_d["data"];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Dictionary BitMap::_get_data() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Dictionary d;
|
2017-03-05 15:44:50 +00:00
|
|
|
d["size"] = get_size();
|
|
|
|
d["data"] = bitmask;
|
2014-02-10 01:10:30 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitMap::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("create", "size"), &BitMap::create);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("create_from_image_alpha", "image"), &BitMap::create_from_image_alpha);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-09-10 13:37:49 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bit", "position", "bit"), &BitMap::set_bit);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bit", "position"), &BitMap::get_bit);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bit_rect", "p_rect", "bit"), &BitMap::set_bit_rect);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_true_bit_count"), &BitMap::get_true_bit_count);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_size"), &BitMap::get_size);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_data"), &BitMap::_set_data);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_data"), &BitMap::_get_data);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-01-11 22:35:12 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BitMap::BitMap() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
width = 0;
|
|
|
|
height = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////
|