bit_map.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*************************************************************************/
  2. /* bit_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_map.h"
  31. #include "core/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.ptrw(), bitmask.size());
  39. }
  40. void BitMap::create_from_image_alpha(const Ref<Image> &p_image, float p_threshold) {
  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.ptrw();
  48. for (int i = 0; i < width * height; i++) {
  49. int bbyte = i / 8;
  50. int bbit = i % 8;
  51. if (r[i * 2 + 1] / 255.0 > p_threshold) {
  52. w[bbyte] |= (1 << bbit);
  53. }
  54. }
  55. }
  56. void BitMap::set_bit_rect(const Rect2 &p_rect, bool p_value) {
  57. Rect2i current = Rect2i(0, 0, width, height).clip(p_rect);
  58. uint8_t *data = bitmask.ptrw();
  59. for (int i = current.position.x; i < current.position.x + current.size.x; i++) {
  60. for (int j = current.position.y; j < current.position.y + current.size.y; j++) {
  61. int ofs = width * j + i;
  62. int bbyte = ofs / 8;
  63. int bbit = ofs % 8;
  64. uint8_t b = data[bbyte];
  65. if (p_value)
  66. b |= (1 << bbit);
  67. else
  68. b &= ~(1 << bbit);
  69. data[bbyte] = b;
  70. }
  71. }
  72. }
  73. int BitMap::get_true_bit_count() const {
  74. int ds = bitmask.size();
  75. const uint8_t *d = bitmask.ptr();
  76. int c = 0;
  77. //fast, almost branchless version
  78. for (int i = 0; i < ds; i++) {
  79. c += (d[i] & (1 << 7)) >> 7;
  80. c += (d[i] & (1 << 6)) >> 6;
  81. c += (d[i] & (1 << 5)) >> 5;
  82. c += (d[i] & (1 << 4)) >> 4;
  83. c += (d[i] & (1 << 3)) >> 3;
  84. c += (d[i] & (1 << 2)) >> 2;
  85. c += (d[i] & (1 << 1)) >> 1;
  86. c += d[i] & 1;
  87. }
  88. return c;
  89. }
  90. void BitMap::set_bit(const Point2 &p_pos, bool p_value) {
  91. int x = p_pos.x;
  92. int y = p_pos.y;
  93. ERR_FAIL_INDEX(x, width);
  94. ERR_FAIL_INDEX(y, height);
  95. int ofs = width * y + x;
  96. int bbyte = ofs / 8;
  97. int bbit = ofs % 8;
  98. uint8_t b = bitmask[bbyte];
  99. if (p_value)
  100. b |= (1 << bbit);
  101. else
  102. b &= ~(1 << bbit);
  103. bitmask.write[bbyte] = b;
  104. }
  105. bool BitMap::get_bit(const Point2 &p_pos) const {
  106. int x = Math::fast_ftoi(p_pos.x);
  107. int y = Math::fast_ftoi(p_pos.y);
  108. ERR_FAIL_INDEX_V(x, width, false);
  109. ERR_FAIL_INDEX_V(y, height, false);
  110. int ofs = width * y + x;
  111. int bbyte = ofs / 8;
  112. int bbit = ofs % 8;
  113. return (bitmask[bbyte] & (1 << bbit)) != 0;
  114. }
  115. Size2 BitMap::get_size() const {
  116. return Size2(width, height);
  117. }
  118. void BitMap::_set_data(const Dictionary &p_d) {
  119. ERR_FAIL_COND(!p_d.has("size"));
  120. ERR_FAIL_COND(!p_d.has("data"));
  121. create(p_d["size"]);
  122. bitmask = p_d["data"];
  123. }
  124. Dictionary BitMap::_get_data() const {
  125. Dictionary d;
  126. d["size"] = get_size();
  127. d["data"] = bitmask;
  128. return d;
  129. }
  130. Vector<Vector2> BitMap::_march_square(const Rect2i &rect, const Point2i &start) const {
  131. int stepx = 0;
  132. int stepy = 0;
  133. int prevx = 0;
  134. int prevy = 0;
  135. int startx = start.x;
  136. int starty = start.y;
  137. int curx = startx;
  138. int cury = starty;
  139. unsigned int count = 0;
  140. Set<Point2i> case9s;
  141. Set<Point2i> case6s;
  142. Vector<Vector2> _points;
  143. do {
  144. int sv = 0;
  145. { //square value
  146. /*
  147. checking the 2x2 pixel grid, assigning these values to each pixel, if not transparent
  148. +---+---+
  149. | 1 | 2 |
  150. +---+---+
  151. | 4 | 8 | <- current pixel (curx,cury)
  152. +---+---+
  153. */
  154. //NOTE: due to the way we pick points from texture, rect needs to be smaller, otherwise it goes outside 1 pixel
  155. Rect2i fixed_rect = Rect2i(rect.position, rect.size - Size2i(2, 2));
  156. Point2i tl = Point2i(curx - 1, cury - 1);
  157. sv += (fixed_rect.has_point(tl) && get_bit(tl)) ? 1 : 0;
  158. Point2i tr = Point2i(curx, cury - 1);
  159. sv += (fixed_rect.has_point(tr) && get_bit(tr)) ? 2 : 0;
  160. Point2i bl = Point2i(curx - 1, cury);
  161. sv += (fixed_rect.has_point(bl) && get_bit(bl)) ? 4 : 0;
  162. Point2i br = Point2i(curx, cury);
  163. sv += (fixed_rect.has_point(br) && get_bit(br)) ? 8 : 0;
  164. ERR_FAIL_COND_V(sv == 0 || sv == 15, Vector<Vector2>());
  165. }
  166. switch (sv) {
  167. case 1:
  168. case 5:
  169. case 13:
  170. /* going UP with these cases:
  171. 1 5 13
  172. +---+---+ +---+---+ +---+---+
  173. | 1 | | | 1 | | | 1 | |
  174. +---+---+ +---+---+ +---+---+
  175. | | | | 4 | | | 4 | 8 |
  176. +---+---+ +---+---+ +---+---+
  177. */
  178. stepx = 0;
  179. stepy = -1;
  180. break;
  181. case 8:
  182. case 10:
  183. case 11:
  184. /* going DOWN with these cases:
  185. 8 10 11
  186. +---+---+ +---+---+ +---+---+
  187. | | | | | 2 | | 1 | 2 |
  188. +---+---+ +---+---+ +---+---+
  189. | | 8 | | | 8 | | | 8 |
  190. +---+---+ +---+---+ +---+---+
  191. */
  192. stepx = 0;
  193. stepy = 1;
  194. break;
  195. case 4:
  196. case 12:
  197. case 14:
  198. /* going LEFT with these cases:
  199. 4 12 14
  200. +---+---+ +---+---+ +---+---+
  201. | | | | | | | | 2 |
  202. +---+---+ +---+---+ +---+---+
  203. | 4 | | | 4 | 8 | | 4 | 8 |
  204. +---+---+ +---+---+ +---+---+
  205. */
  206. stepx = -1;
  207. stepy = 0;
  208. break;
  209. case 2:
  210. case 3:
  211. case 7:
  212. /* going RIGHT with these cases:
  213. 2 3 7
  214. +---+---+ +---+---+ +---+---+
  215. | | 2 | | 1 | 2 | | 1 | 2 |
  216. +---+---+ +---+---+ +---+---+
  217. | | | | | | | 4 | |
  218. +---+---+ +---+---+ +---+---+
  219. */
  220. stepx = 1;
  221. stepy = 0;
  222. break;
  223. case 9:
  224. /*
  225. +---+---+
  226. | 1 | |
  227. +---+---+
  228. | | 8 |
  229. +---+---+
  230. this should normally go UP, but if we already been here, we go down
  231. */
  232. if (case9s.has(Point2i(curx, cury))) {
  233. //found, so we go down, and delete from case9s;
  234. stepx = 0;
  235. stepy = 1;
  236. case9s.erase(Point2i(curx, cury));
  237. } else {
  238. //not found, we go up, and add to case9s;
  239. stepx = 0;
  240. stepy = -1;
  241. case9s.insert(Point2i(curx, cury));
  242. }
  243. break;
  244. case 6:
  245. /*
  246. 6
  247. +---+---+
  248. | | 2 |
  249. +---+---+
  250. | 4 | |
  251. +---+---+
  252. this normally go RIGHT, but if its coming from UP, it should go LEFT
  253. */
  254. if (case6s.has(Point2i(curx, cury))) {
  255. //found, so we go down, and delete from case6s;
  256. stepx = -1;
  257. stepy = 0;
  258. case6s.erase(Point2i(curx, cury));
  259. } else {
  260. //not found, we go up, and add to case6s;
  261. stepx = 1;
  262. stepy = 0;
  263. case6s.insert(Point2i(curx, cury));
  264. }
  265. break;
  266. default:
  267. ERR_PRINT("this shouldn't happen.");
  268. }
  269. //little optimization
  270. // if previous direction is same as current direction,
  271. // then we should modify the last vec to current
  272. curx += stepx;
  273. cury += stepy;
  274. if (stepx == prevx && stepy == prevy) {
  275. _points.write[_points.size() - 1].x = (float)(curx - rect.position.x);
  276. _points.write[_points.size() - 1].y = (float)(cury + rect.position.y);
  277. } else {
  278. _points.push_back(Vector2((float)(curx - rect.position.x), (float)(cury + rect.position.y)));
  279. }
  280. count++;
  281. prevx = stepx;
  282. prevy = stepy;
  283. ERR_FAIL_COND_V((int)count > width * height, _points);
  284. } while (curx != startx || cury != starty);
  285. return _points;
  286. }
  287. static float perpendicular_distance(const Vector2 &i, const Vector2 &start, const Vector2 &end) {
  288. float res;
  289. float slope;
  290. float intercept;
  291. if (start.x == end.x) {
  292. res = Math::absf(i.x - end.x);
  293. } else if (start.y == end.y) {
  294. res = Math::absf(i.y - end.y);
  295. } else {
  296. slope = (end.y - start.y) / (end.x - start.x);
  297. intercept = start.y - (slope * start.x);
  298. res = Math::absf(slope * i.x - i.y + intercept) / Math::sqrt(Math::pow(slope, 2.0f) + 1.0);
  299. }
  300. return res;
  301. }
  302. static Vector<Vector2> rdp(const Vector<Vector2> &v, float optimization) {
  303. if (v.size() < 3)
  304. return v;
  305. int index = -1;
  306. float dist = 0;
  307. //not looping first and last point
  308. for (size_t i = 1, size = v.size(); i < size - 1; ++i) {
  309. float cdist = perpendicular_distance(v[i], v[0], v[v.size() - 1]);
  310. if (cdist > dist) {
  311. dist = cdist;
  312. index = static_cast<int>(i);
  313. }
  314. }
  315. if (dist > optimization) {
  316. Vector<Vector2> left, right;
  317. left.resize(index);
  318. for (int i = 0; i < index; i++) {
  319. left.write[i] = v[i];
  320. }
  321. right.resize(v.size() - index);
  322. for (int i = 0; i < right.size(); i++) {
  323. right.write[i] = v[index + i];
  324. }
  325. Vector<Vector2> r1 = rdp(left, optimization);
  326. Vector<Vector2> r2 = rdp(right, optimization);
  327. int middle = r1.size();
  328. r1.resize(r1.size() + r2.size());
  329. for (int i = 0; i < r2.size(); i++) {
  330. r1.write[middle + i] = r2[i];
  331. }
  332. return r1;
  333. } else {
  334. Vector<Vector2> ret;
  335. ret.push_back(v[0]);
  336. ret.push_back(v[v.size() - 1]);
  337. return ret;
  338. }
  339. }
  340. static Vector<Vector2> reduce(const Vector<Vector2> &points, const Rect2i &rect, float epsilon) {
  341. int size = points.size();
  342. // if there are less than 3 points, then we have nothing
  343. ERR_FAIL_COND_V(size < 3, Vector<Vector2>());
  344. // if there are less than 9 points (but more than 3), then we don't need to reduce it
  345. if (size < 9) {
  346. return points;
  347. }
  348. float maxEp = MIN(rect.size.width, rect.size.height);
  349. float ep = CLAMP(epsilon, 0.0, maxEp / 2);
  350. Vector<Vector2> result = rdp(points, ep);
  351. Vector2 last = result[result.size() - 1];
  352. if (last.y > result[0].y && last.distance_to(result[0]) < ep * 0.5f) {
  353. result.write[0].y = last.y;
  354. result.resize(result.size() - 1);
  355. }
  356. return result;
  357. }
  358. struct FillBitsStackEntry {
  359. Point2i pos;
  360. int i;
  361. int j;
  362. };
  363. static void fill_bits(const BitMap *p_src, Ref<BitMap> &p_map, const Point2i &p_pos, const Rect2i &rect) {
  364. // Using a custom stack to work iteratively to avoid stack overflow on big bitmaps
  365. PoolVector<FillBitsStackEntry> stack;
  366. // Tracking size since we won't be shrinking the stack vector
  367. int stack_size = 0;
  368. Point2i pos = p_pos;
  369. int next_i = 0;
  370. int next_j = 0;
  371. bool reenter = true;
  372. bool popped = false;
  373. do {
  374. if (reenter) {
  375. next_i = pos.x - 1;
  376. next_j = pos.y - 1;
  377. reenter = false;
  378. }
  379. for (int i = next_i; i <= pos.x + 1; i++) {
  380. for (int j = next_j; j <= pos.y + 1; j++) {
  381. if (popped) {
  382. // The next loop over j must start normally
  383. next_j = pos.y;
  384. popped = false;
  385. // Skip because an iteration was already executed with current counter values
  386. continue;
  387. }
  388. if (i < rect.position.x || i >= rect.position.x + rect.size.x)
  389. continue;
  390. if (j < rect.position.y || j >= rect.position.y + rect.size.y)
  391. continue;
  392. if (p_map->get_bit(Vector2(i, j)))
  393. continue;
  394. else if (p_src->get_bit(Vector2(i, j))) {
  395. p_map->set_bit(Vector2(i, j), true);
  396. FillBitsStackEntry se = { pos, i, j };
  397. stack.resize(MAX(stack_size + 1, stack.size()));
  398. stack.set(stack_size, se);
  399. stack_size++;
  400. pos = Point2i(i, j);
  401. reenter = true;
  402. break;
  403. }
  404. }
  405. if (reenter) {
  406. break;
  407. }
  408. }
  409. if (!reenter) {
  410. if (stack_size) {
  411. FillBitsStackEntry se = stack.get(stack_size - 1);
  412. stack_size--;
  413. pos = se.pos;
  414. next_i = se.i;
  415. next_j = se.j;
  416. popped = true;
  417. }
  418. }
  419. } while (reenter || popped);
  420. print_verbose("BitMap: Max stack size: " + itos(stack.size()));
  421. }
  422. Vector<Vector<Vector2> > BitMap::clip_opaque_to_polygons(const Rect2 &p_rect, float p_epsilon) const {
  423. Rect2i r = Rect2i(0, 0, width, height).clip(p_rect);
  424. print_verbose("BitMap: Rect: " + r);
  425. Point2i from;
  426. Ref<BitMap> fill;
  427. fill.instance();
  428. fill->create(get_size());
  429. Vector<Vector<Vector2> > polygons;
  430. for (int i = r.position.y; i < r.position.y + r.size.height; i++) {
  431. for (int j = r.position.x; j < r.position.x + r.size.width; j++) {
  432. if (!fill->get_bit(Point2(j, i)) && get_bit(Point2(j, i))) {
  433. Vector<Vector2> polygon = _march_square(r, Point2i(j, i));
  434. print_verbose("BitMap: Pre reduce: " + itos(polygon.size()));
  435. polygon = reduce(polygon, r, p_epsilon);
  436. print_verbose("BitMap: Post reduce: " + itos(polygon.size()));
  437. polygons.push_back(polygon);
  438. fill_bits(this, fill, Point2i(j, i), r);
  439. }
  440. }
  441. }
  442. return polygons;
  443. }
  444. void BitMap::grow_mask(int p_pixels, const Rect2 &p_rect) {
  445. Rect2i r = Rect2i(0, 0, width, height).clip(p_rect);
  446. Ref<BitMap> copy;
  447. copy.instance();
  448. copy->create(get_size());
  449. copy->bitmask = bitmask;
  450. for (int i = r.position.y; i < r.position.y + r.size.height; i++) {
  451. for (int j = r.position.x; j < r.position.x + r.size.width; j++) {
  452. if (copy->get_bit(Point2(j, i)))
  453. continue;
  454. bool found = false;
  455. for (int y = i - p_pixels; y <= i + p_pixels; y++) {
  456. for (int x = j - p_pixels; x <= j + p_pixels; x++) {
  457. if (x < p_rect.position.x || x >= p_rect.position.x + p_rect.size.x)
  458. continue;
  459. if (y < p_rect.position.y || y >= p_rect.position.y + p_rect.size.y)
  460. continue;
  461. float d = Point2(j, i).distance_to(Point2(x, y)) - CMP_EPSILON;
  462. if (d > p_pixels)
  463. continue;
  464. if (copy->get_bit(Point2(x, y))) {
  465. found = true;
  466. break;
  467. }
  468. }
  469. if (found)
  470. break;
  471. }
  472. if (found) {
  473. set_bit(Point2(j, i), true);
  474. }
  475. }
  476. }
  477. }
  478. Array BitMap::_opaque_to_polygons_bind(const Rect2 &p_rect, float p_epsilon) const {
  479. Vector<Vector<Vector2> > result = clip_opaque_to_polygons(p_rect, p_epsilon);
  480. // Convert result to bindable types
  481. Array result_array;
  482. result_array.resize(result.size());
  483. for (int i = 0; i < result.size(); i++) {
  484. const Vector<Vector2> &polygon = result[i];
  485. PoolVector2Array polygon_array;
  486. polygon_array.resize(polygon.size());
  487. {
  488. PoolVector2Array::Write w = polygon_array.write();
  489. for (int j = 0; j < polygon.size(); j++) {
  490. w[j] = polygon[j];
  491. }
  492. }
  493. result_array[i] = polygon_array;
  494. }
  495. return result_array;
  496. }
  497. void BitMap::_bind_methods() {
  498. ClassDB::bind_method(D_METHOD("create", "size"), &BitMap::create);
  499. ClassDB::bind_method(D_METHOD("create_from_image_alpha", "image", "threshold"), &BitMap::create_from_image_alpha, DEFVAL(0.1));
  500. ClassDB::bind_method(D_METHOD("set_bit", "position", "bit"), &BitMap::set_bit);
  501. ClassDB::bind_method(D_METHOD("get_bit", "position"), &BitMap::get_bit);
  502. ClassDB::bind_method(D_METHOD("set_bit_rect", "rect", "bit"), &BitMap::set_bit_rect);
  503. ClassDB::bind_method(D_METHOD("get_true_bit_count"), &BitMap::get_true_bit_count);
  504. ClassDB::bind_method(D_METHOD("get_size"), &BitMap::get_size);
  505. ClassDB::bind_method(D_METHOD("_set_data"), &BitMap::_set_data);
  506. ClassDB::bind_method(D_METHOD("_get_data"), &BitMap::_get_data);
  507. ClassDB::bind_method(D_METHOD("grow_mask", "pixels", "rect"), &BitMap::grow_mask);
  508. ClassDB::bind_method(D_METHOD("opaque_to_polygons", "rect", "epsilon"), &BitMap::_opaque_to_polygons_bind, DEFVAL(2.0));
  509. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  510. }
  511. BitMap::BitMap() {
  512. width = 0;
  513. height = 0;
  514. }
  515. //////////////////////////////////////