bit_mask.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*************************************************************************/
  2. /* bit_mask.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "bit_mask.h"
  31. #include "io/image_loader.h"
  32. void BitMap::create(const Size2 &p_size) {
  33. ERR_FAIL_COND(p_size.width < 1);
  34. ERR_FAIL_COND(p_size.height < 1);
  35. width = p_size.width;
  36. height = p_size.height;
  37. bitmask.resize(((width * height) / 8) + 1);
  38. zeromem(bitmask.ptr(), bitmask.size());
  39. }
  40. void BitMap::create_from_image_alpha(const Ref<Image> &p_image) {
  41. ERR_FAIL_COND(p_image.is_null() || p_image->empty());
  42. Ref<Image> img = p_image->duplicate();
  43. img->convert(Image::FORMAT_LA8);
  44. ERR_FAIL_COND(img->get_format() != Image::FORMAT_LA8);
  45. create(Size2(img->get_width(), img->get_height()));
  46. PoolVector<uint8_t>::Read r = img->get_data().read();
  47. uint8_t *w = bitmask.ptr();
  48. for (int i = 0; i < width * height; i++) {
  49. int bbyte = i / 8;
  50. int bbit = i % 8;
  51. if (r[i * 2])
  52. w[bbyte] |= (1 << bbit);
  53. }
  54. }
  55. void BitMap::set_bit_rect(const Rect2 &p_rect, bool p_value) {
  56. Rect2i current = Rect2i(0, 0, width, height).clip(p_rect);
  57. uint8_t *data = bitmask.ptr();
  58. for (int i = current.position.x; i < current.position.x + current.size.x; i++) {
  59. for (int j = current.position.y; j < current.position.y + current.size.y; j++) {
  60. int ofs = width * j + i;
  61. int bbyte = ofs / 8;
  62. int bbit = ofs % 8;
  63. uint8_t b = data[bbyte];
  64. if (p_value)
  65. b |= (1 << bbit);
  66. else
  67. b &= !(1 << bbit);
  68. data[bbyte] = b;
  69. }
  70. }
  71. }
  72. int BitMap::get_true_bit_count() const {
  73. int ds = bitmask.size();
  74. const uint8_t *d = bitmask.ptr();
  75. int c = 0;
  76. //fast, almot branchless version
  77. for (int i = 0; i < ds; i++) {
  78. c += (d[i] & (1 << 7)) >> 7;
  79. c += (d[i] & (1 << 6)) >> 6;
  80. c += (d[i] & (1 << 5)) >> 5;
  81. c += (d[i] & (1 << 4)) >> 4;
  82. c += (d[i] & (1 << 3)) >> 3;
  83. c += (d[i] & (1 << 2)) >> 2;
  84. c += d[i] & 1;
  85. }
  86. return c;
  87. }
  88. void BitMap::set_bit(const Point2 &p_pos, bool p_value) {
  89. int x = Math::fast_ftoi(p_pos.x);
  90. int y = Math::fast_ftoi(p_pos.y);
  91. ERR_FAIL_INDEX(x, width);
  92. ERR_FAIL_INDEX(y, height);
  93. int ofs = width * y + x;
  94. int bbyte = ofs / 8;
  95. int bbit = ofs % 8;
  96. uint8_t b = bitmask[bbyte];
  97. if (p_value)
  98. b |= (1 << bbit);
  99. else
  100. b &= !(1 << bbit);
  101. bitmask[bbyte] = b;
  102. }
  103. bool BitMap::get_bit(const Point2 &p_pos) const {
  104. int x = Math::fast_ftoi(p_pos.x);
  105. int y = Math::fast_ftoi(p_pos.y);
  106. ERR_FAIL_INDEX_V(x, width, false);
  107. ERR_FAIL_INDEX_V(y, height, false);
  108. int ofs = width * y + x;
  109. int bbyte = ofs / 8;
  110. int bbit = ofs % 8;
  111. return (bitmask[bbyte] & (1 << bbit)) != 0;
  112. }
  113. Size2 BitMap::get_size() const {
  114. return Size2(width, height);
  115. }
  116. void BitMap::_set_data(const Dictionary &p_d) {
  117. ERR_FAIL_COND(!p_d.has("size"));
  118. ERR_FAIL_COND(!p_d.has("data"));
  119. create(p_d["size"]);
  120. bitmask = p_d["data"];
  121. }
  122. Dictionary BitMap::_get_data() const {
  123. Dictionary d;
  124. d["size"] = get_size();
  125. d["data"] = bitmask;
  126. return d;
  127. }
  128. void BitMap::_bind_methods() {
  129. ClassDB::bind_method(D_METHOD("create", "size"), &BitMap::create);
  130. ClassDB::bind_method(D_METHOD("create_from_image_alpha", "image"), &BitMap::create_from_image_alpha);
  131. ClassDB::bind_method(D_METHOD("set_bit", "position", "bit"), &BitMap::set_bit);
  132. ClassDB::bind_method(D_METHOD("get_bit", "position"), &BitMap::get_bit);
  133. ClassDB::bind_method(D_METHOD("set_bit_rect", "p_rect", "bit"), &BitMap::set_bit_rect);
  134. ClassDB::bind_method(D_METHOD("get_true_bit_count"), &BitMap::get_true_bit_count);
  135. ClassDB::bind_method(D_METHOD("get_size"), &BitMap::get_size);
  136. ClassDB::bind_method(D_METHOD("_set_data"), &BitMap::_set_data);
  137. ClassDB::bind_method(D_METHOD("_get_data"), &BitMap::_get_data);
  138. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_data", "_get_data");
  139. }
  140. BitMap::BitMap() {
  141. width = 0;
  142. height = 0;
  143. }
  144. //////////////////////////////////////