bit_mask.cpp 5.5 KB

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