bit_map.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /**************************************************************************/
  2. /* bit_map.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/variant/typed_array.h"
  32. void BitMap::create(const Size2i &p_size) {
  33. ERR_FAIL_COND(p_size.width < 1);
  34. ERR_FAIL_COND(p_size.height < 1);
  35. ERR_FAIL_COND(static_cast<int64_t>(p_size.width) * static_cast<int64_t>(p_size.height) > INT32_MAX);
  36. Error err = bitmask.resize(Math::division_round_up(p_size.width * p_size.height, 8));
  37. ERR_FAIL_COND(err != OK);
  38. width = p_size.width;
  39. height = p_size.height;
  40. memset(bitmask.ptrw(), 0, bitmask.size());
  41. }
  42. void BitMap::create_from_image_alpha(const Ref<Image> &p_image, float p_threshold) {
  43. ERR_FAIL_COND(p_image.is_null() || p_image->is_empty());
  44. Ref<Image> img = p_image->duplicate();
  45. img->convert(Image::FORMAT_LA8);
  46. ERR_FAIL_COND(img->get_format() != Image::FORMAT_LA8);
  47. create(Size2i(img->get_width(), img->get_height()));
  48. const uint8_t *r = img->get_data().ptr();
  49. uint8_t *w = bitmask.ptrw();
  50. for (int i = 0; i < width * height; i++) {
  51. int bbyte = i / 8;
  52. int bbit = i % 8;
  53. if (r[i * 2 + 1] / 255.0 > p_threshold) {
  54. w[bbyte] |= (1 << bbit);
  55. }
  56. }
  57. }
  58. void BitMap::set_bit_rect(const Rect2i &p_rect, bool p_value) {
  59. Rect2i current = Rect2i(0, 0, width, height).intersection(p_rect);
  60. uint8_t *data = bitmask.ptrw();
  61. for (int i = current.position.x; i < current.position.x + current.size.x; i++) {
  62. for (int j = current.position.y; j < current.position.y + current.size.y; j++) {
  63. int ofs = width * j + i;
  64. int bbyte = ofs / 8;
  65. int bbit = ofs % 8;
  66. uint8_t b = data[bbyte];
  67. if (p_value) {
  68. b |= (1 << bbit);
  69. } else {
  70. b &= ~(1 << bbit);
  71. }
  72. data[bbyte] = b;
  73. }
  74. }
  75. }
  76. int BitMap::get_true_bit_count() const {
  77. int ds = bitmask.size();
  78. const uint8_t *d = bitmask.ptr();
  79. int c = 0;
  80. // Fast, almost branchless version.
  81. for (int i = 0; i < ds; i++) {
  82. c += (d[i] & (1 << 7)) >> 7;
  83. c += (d[i] & (1 << 6)) >> 6;
  84. c += (d[i] & (1 << 5)) >> 5;
  85. c += (d[i] & (1 << 4)) >> 4;
  86. c += (d[i] & (1 << 3)) >> 3;
  87. c += (d[i] & (1 << 2)) >> 2;
  88. c += (d[i] & (1 << 1)) >> 1;
  89. c += d[i] & 1;
  90. }
  91. return c;
  92. }
  93. void BitMap::set_bitv(const Point2i &p_pos, bool p_value) {
  94. set_bit(p_pos.x, p_pos.y, p_value);
  95. }
  96. void BitMap::set_bit(int p_x, int p_y, bool p_value) {
  97. ERR_FAIL_INDEX(p_x, width);
  98. ERR_FAIL_INDEX(p_y, height);
  99. int ofs = width * p_y + p_x;
  100. int bbyte = ofs / 8;
  101. int bbit = ofs % 8;
  102. uint8_t b = bitmask[bbyte];
  103. if (p_value) {
  104. b |= (1 << bbit);
  105. } else {
  106. b &= ~(1 << bbit);
  107. }
  108. bitmask.write[bbyte] = b;
  109. }
  110. bool BitMap::get_bitv(const Point2i &p_pos) const {
  111. return get_bit(p_pos.x, p_pos.y);
  112. }
  113. bool BitMap::get_bit(int p_x, int p_y) const {
  114. ERR_FAIL_INDEX_V(p_x, width, false);
  115. ERR_FAIL_INDEX_V(p_y, height, false);
  116. int ofs = width * p_y + p_x;
  117. int bbyte = ofs / 8;
  118. int bbit = ofs % 8;
  119. return (bitmask[bbyte] & (1 << bbit)) != 0;
  120. }
  121. Size2i BitMap::get_size() const {
  122. return Size2i(width, height);
  123. }
  124. void BitMap::_set_data(const Dictionary &p_d) {
  125. ERR_FAIL_COND(!p_d.has("size"));
  126. ERR_FAIL_COND(!p_d.has("data"));
  127. create(p_d["size"]);
  128. bitmask = p_d["data"];
  129. }
  130. Dictionary BitMap::_get_data() const {
  131. Dictionary d;
  132. d["size"] = get_size();
  133. d["data"] = bitmask;
  134. return d;
  135. }
  136. Vector<Vector<Vector2>> BitMap::_march_square(const Rect2i &p_rect, const Point2i &p_start) const {
  137. int stepx = 0;
  138. int stepy = 0;
  139. int prevx = 0;
  140. int prevy = 0;
  141. int startx = p_start.x;
  142. int starty = p_start.y;
  143. int curx = startx;
  144. int cury = starty;
  145. unsigned int count = 0;
  146. HashMap<Point2i, int> cross_map;
  147. Vector<Vector2> _points;
  148. int points_size = 0;
  149. Vector<Vector<Vector2>> ret;
  150. // Add starting entry at start of return.
  151. ret.resize(1);
  152. do {
  153. int sv = 0;
  154. { // Square value
  155. /*
  156. checking the 2x2 pixel grid, assigning these values to each pixel, if not transparent
  157. +---+---+
  158. | 1 | 2 |
  159. +---+---+
  160. | 4 | 8 | <- current pixel (curx,cury)
  161. +---+---+
  162. */
  163. Point2i tl = Point2i(curx - 1, cury - 1);
  164. sv += (p_rect.has_point(tl) && get_bitv(tl)) ? 1 : 0;
  165. Point2i tr = Point2i(curx, cury - 1);
  166. sv += (p_rect.has_point(tr) && get_bitv(tr)) ? 2 : 0;
  167. Point2i bl = Point2i(curx - 1, cury);
  168. sv += (p_rect.has_point(bl) && get_bitv(bl)) ? 4 : 0;
  169. Point2i br = Point2i(curx, cury);
  170. sv += (p_rect.has_point(br) && get_bitv(br)) ? 8 : 0;
  171. ERR_FAIL_COND_V(sv == 0 || sv == 15, Vector<Vector<Vector2>>());
  172. }
  173. switch (sv) {
  174. case 1:
  175. case 5:
  176. case 13:
  177. /* going UP with these cases:
  178. 1 5 13
  179. +---+---+ +---+---+ +---+---+
  180. | 1 | | | 1 | | | 1 | |
  181. +---+---+ +---+---+ +---+---+
  182. | | | | 4 | | | 4 | 8 |
  183. +---+---+ +---+---+ +---+---+
  184. */
  185. stepx = 0;
  186. stepy = -1;
  187. break;
  188. case 8:
  189. case 10:
  190. case 11:
  191. /* going DOWN with these cases:
  192. 8 10 11
  193. +---+---+ +---+---+ +---+---+
  194. | | | | | 2 | | 1 | 2 |
  195. +---+---+ +---+---+ +---+---+
  196. | | 8 | | | 8 | | | 8 |
  197. +---+---+ +---+---+ +---+---+
  198. */
  199. stepx = 0;
  200. stepy = 1;
  201. break;
  202. case 4:
  203. case 12:
  204. case 14:
  205. /* going LEFT with these cases:
  206. 4 12 14
  207. +---+---+ +---+---+ +---+---+
  208. | | | | | | | | 2 |
  209. +---+---+ +---+---+ +---+---+
  210. | 4 | | | 4 | 8 | | 4 | 8 |
  211. +---+---+ +---+---+ +---+---+
  212. */
  213. stepx = -1;
  214. stepy = 0;
  215. break;
  216. case 2:
  217. case 3:
  218. case 7:
  219. /* going RIGHT with these cases:
  220. 2 3 7
  221. +---+---+ +---+---+ +---+---+
  222. | | 2 | | 1 | 2 | | 1 | 2 |
  223. +---+---+ +---+---+ +---+---+
  224. | | | | | | | 4 | |
  225. +---+---+ +---+---+ +---+---+
  226. */
  227. stepx = 1;
  228. stepy = 0;
  229. break;
  230. case 9:
  231. /* Going DOWN if coming from the LEFT, otherwise go UP.
  232. 9
  233. +---+---+
  234. | 1 | |
  235. +---+---+
  236. | | 8 |
  237. +---+---+
  238. */
  239. if (prevx == 1) {
  240. stepx = 0;
  241. stepy = 1;
  242. } else {
  243. stepx = 0;
  244. stepy = -1;
  245. }
  246. break;
  247. case 6:
  248. /* Going RIGHT if coming from BELOW, otherwise go LEFT.
  249. 6
  250. +---+---+
  251. | | 2 |
  252. +---+---+
  253. | 4 | |
  254. +---+---+
  255. */
  256. if (prevy == -1) {
  257. stepx = 1;
  258. stepy = 0;
  259. } else {
  260. stepx = -1;
  261. stepy = 0;
  262. }
  263. break;
  264. default:
  265. ERR_PRINT("this shouldn't happen.");
  266. }
  267. // Handle crossing points.
  268. if (sv == 6 || sv == 9) {
  269. const Point2i cur_pos(curx, cury);
  270. // Find if this point has occurred before.
  271. if (HashMap<Point2i, int>::Iterator found = cross_map.find(cur_pos)) {
  272. // Add points after the previous crossing to the result.
  273. ret.push_back(_points.slice(found->value + 1, points_size));
  274. // Remove points after crossing point.
  275. points_size = found->value + 1;
  276. // Erase trailing map elements.
  277. while (cross_map.last() != found) {
  278. cross_map.remove(cross_map.last());
  279. }
  280. cross_map.erase(cur_pos);
  281. } else {
  282. // Add crossing point to map.
  283. cross_map.insert(cur_pos, points_size - 1);
  284. }
  285. }
  286. // Small optimization:
  287. // If the previous direction is same as the current direction,
  288. // then we should modify the last vector to current.
  289. curx += stepx;
  290. cury += stepy;
  291. if (stepx == prevx && stepy == prevy) {
  292. _points.set(points_size - 1, Vector2(curx, cury) - p_rect.position);
  293. } else {
  294. _points.resize(MAX(points_size + 1, _points.size()));
  295. _points.set(points_size, Vector2(curx, cury) - p_rect.position);
  296. points_size++;
  297. }
  298. count++;
  299. prevx = stepx;
  300. prevy = stepy;
  301. ERR_FAIL_COND_V((int)count > 2 * (width * height + 1), Vector<Vector<Vector2>>());
  302. } while (curx != startx || cury != starty);
  303. // Add remaining points to result.
  304. _points.resize(points_size);
  305. ret.set(0, _points);
  306. return ret;
  307. }
  308. static float perpendicular_distance(const Vector2 &i, const Vector2 &start, const Vector2 &end) {
  309. float res;
  310. float slope;
  311. float intercept;
  312. if (start.x == end.x) {
  313. res = Math::absf(i.x - end.x);
  314. } else if (start.y == end.y) {
  315. res = Math::absf(i.y - end.y);
  316. } else {
  317. slope = (end.y - start.y) / (end.x - start.x);
  318. intercept = start.y - (slope * start.x);
  319. res = Math::absf(slope * i.x - i.y + intercept) / Math::sqrt(Math::pow(slope, 2.0f) + 1.0);
  320. }
  321. return res;
  322. }
  323. static Vector<Vector2> rdp(const Vector<Vector2> &v, float optimization) {
  324. if (v.size() < 3) {
  325. return v;
  326. }
  327. int index = -1;
  328. float dist = 0.0;
  329. // Not looping first and last point.
  330. for (size_t i = 1, size = v.size(); i < size - 1; ++i) {
  331. float cdist = perpendicular_distance(v[i], v[0], v[v.size() - 1]);
  332. if (cdist > dist) {
  333. dist = cdist;
  334. index = static_cast<int>(i);
  335. }
  336. }
  337. if (dist > optimization) {
  338. Vector<Vector2> left, right;
  339. left.resize(index);
  340. for (int i = 0; i < index; i++) {
  341. left.write[i] = v[i];
  342. }
  343. right.resize(v.size() - index);
  344. for (int i = 0; i < right.size(); i++) {
  345. right.write[i] = v[index + i];
  346. }
  347. Vector<Vector2> r1 = rdp(left, optimization);
  348. Vector<Vector2> r2 = rdp(right, optimization);
  349. int middle = r1.size();
  350. r1.resize(r1.size() + r2.size());
  351. for (int i = 0; i < r2.size(); i++) {
  352. r1.write[middle + i] = r2[i];
  353. }
  354. return r1;
  355. } else {
  356. Vector<Vector2> ret;
  357. ret.push_back(v[0]);
  358. ret.push_back(v[v.size() - 1]);
  359. return ret;
  360. }
  361. }
  362. static Vector<Vector2> reduce(const Vector<Vector2> &points, const Rect2i &rect, float epsilon) {
  363. int size = points.size();
  364. // If there are less than 3 points, then we have nothing.
  365. ERR_FAIL_COND_V(size < 3, Vector<Vector2>());
  366. // If there are less than 9 points (but more than 3), then we don't need to reduce it.
  367. if (size < 9) {
  368. return points;
  369. }
  370. float maxEp = MIN(rect.size.width, rect.size.height);
  371. float ep = CLAMP(epsilon, 0.0, maxEp / 2);
  372. Vector<Vector2> result = rdp(points, ep);
  373. Vector2 last = result[result.size() - 1];
  374. if (last.y > result[0].y && last.distance_to(result[0]) < ep * 0.5f) {
  375. result.write[0].y = last.y;
  376. result.resize(result.size() - 1);
  377. }
  378. return result;
  379. }
  380. struct FillBitsStackEntry {
  381. Point2i pos;
  382. int i = 0;
  383. int j = 0;
  384. };
  385. static void fill_bits(const BitMap *p_src, Ref<BitMap> &p_map, const Point2i &p_pos, const Rect2i &rect) {
  386. // Using a custom stack to work iteratively to avoid stack overflow on big bitmaps.
  387. Vector<FillBitsStackEntry> stack;
  388. // Tracking size since we won't be shrinking the stack vector.
  389. int stack_size = 0;
  390. Point2i pos = p_pos;
  391. int next_i = 0;
  392. int next_j = 0;
  393. bool reenter = true;
  394. bool popped = false;
  395. do {
  396. if (reenter) {
  397. next_i = pos.x - 1;
  398. next_j = pos.y - 1;
  399. reenter = false;
  400. }
  401. for (int i = next_i; i <= pos.x + 1; i++) {
  402. for (int j = next_j; j <= pos.y + 1; j++) {
  403. if (popped) {
  404. // The next loop over j must start normally.
  405. next_j = pos.y - 1;
  406. popped = false;
  407. // Skip because an iteration was already executed with current counter values.
  408. continue;
  409. }
  410. if (i < rect.position.x || i >= rect.position.x + rect.size.x) {
  411. continue;
  412. }
  413. if (j < rect.position.y || j >= rect.position.y + rect.size.y) {
  414. continue;
  415. }
  416. if (p_map->get_bit(i, j)) {
  417. continue;
  418. } else if (p_src->get_bit(i, j)) {
  419. p_map->set_bit(i, j, true);
  420. FillBitsStackEntry se = { pos, i, j };
  421. stack.resize(MAX(stack_size + 1, stack.size()));
  422. stack.set(stack_size, se);
  423. stack_size++;
  424. pos = Point2i(i, j);
  425. reenter = true;
  426. break;
  427. }
  428. }
  429. if (reenter) {
  430. break;
  431. }
  432. }
  433. if (!reenter) {
  434. if (stack_size) {
  435. FillBitsStackEntry se = stack.get(stack_size - 1);
  436. stack_size--;
  437. pos = se.pos;
  438. next_i = se.i;
  439. next_j = se.j;
  440. popped = true;
  441. }
  442. }
  443. } while (reenter || popped);
  444. }
  445. Vector<Vector<Vector2>> BitMap::clip_opaque_to_polygons(const Rect2i &p_rect, float p_epsilon) const {
  446. Rect2i r = Rect2i(0, 0, width, height).intersection(p_rect);
  447. Point2i from;
  448. Ref<BitMap> fill;
  449. fill.instantiate();
  450. fill->create(get_size());
  451. Vector<Vector<Vector2>> polygons;
  452. for (int i = r.position.y; i < r.position.y + r.size.height; i++) {
  453. for (int j = r.position.x; j < r.position.x + r.size.width; j++) {
  454. if (!fill->get_bit(j, i) && get_bit(j, i)) {
  455. fill_bits(this, fill, Point2i(j, i), r);
  456. for (Vector<Vector2> polygon : _march_square(r, Point2i(j, i))) {
  457. polygon = reduce(polygon, r, p_epsilon);
  458. if (polygon.size() < 3) {
  459. print_verbose("Invalid polygon, skipped");
  460. continue;
  461. }
  462. polygons.push_back(polygon);
  463. }
  464. }
  465. }
  466. }
  467. return polygons;
  468. }
  469. void BitMap::grow_mask(int p_pixels, const Rect2i &p_rect) {
  470. if (p_pixels == 0) {
  471. return;
  472. }
  473. bool bit_value = p_pixels > 0;
  474. p_pixels = Math::abs(p_pixels);
  475. const int pixels2 = p_pixels * p_pixels;
  476. Rect2i r = Rect2i(0, 0, width, height).intersection(p_rect);
  477. Ref<BitMap> copy;
  478. copy.instantiate();
  479. copy->create(get_size());
  480. copy->bitmask = bitmask;
  481. for (int i = r.position.y; i < r.position.y + r.size.height; i++) {
  482. for (int j = r.position.x; j < r.position.x + r.size.width; j++) {
  483. if (bit_value == get_bit(j, i)) {
  484. continue;
  485. }
  486. bool found = false;
  487. for (int y = i - p_pixels; y <= i + p_pixels; y++) {
  488. for (int x = j - p_pixels; x <= j + p_pixels; x++) {
  489. bool outside = false;
  490. if ((x < p_rect.position.x) || (x >= p_rect.position.x + p_rect.size.x) || (y < p_rect.position.y) || (y >= p_rect.position.y + p_rect.size.y)) {
  491. // Outside of rectangle counts as bit not set.
  492. if (!bit_value) {
  493. outside = true;
  494. } else {
  495. continue;
  496. }
  497. }
  498. float d = Point2(j, i).distance_squared_to(Point2(x, y)) - CMP_EPSILON2;
  499. if (d > pixels2) {
  500. continue;
  501. }
  502. if (outside || (bit_value == copy->get_bit(x, y))) {
  503. found = true;
  504. break;
  505. }
  506. }
  507. if (found) {
  508. break;
  509. }
  510. }
  511. if (found) {
  512. set_bit(j, i, bit_value);
  513. }
  514. }
  515. }
  516. }
  517. void BitMap::shrink_mask(int p_pixels, const Rect2i &p_rect) {
  518. grow_mask(-p_pixels, p_rect);
  519. }
  520. TypedArray<PackedVector2Array> BitMap::_opaque_to_polygons_bind(const Rect2i &p_rect, float p_epsilon) const {
  521. Vector<Vector<Vector2>> result = clip_opaque_to_polygons(p_rect, p_epsilon);
  522. // Convert result to bindable types.
  523. TypedArray<PackedVector2Array> result_array;
  524. result_array.resize(result.size());
  525. for (int i = 0; i < result.size(); i++) {
  526. const Vector<Vector2> &polygon = result[i];
  527. PackedVector2Array polygon_array;
  528. polygon_array.resize(polygon.size());
  529. {
  530. Vector2 *w = polygon_array.ptrw();
  531. for (int j = 0; j < polygon.size(); j++) {
  532. w[j] = polygon[j];
  533. }
  534. }
  535. result_array[i] = polygon_array;
  536. }
  537. return result_array;
  538. }
  539. void BitMap::resize(const Size2i &p_new_size) {
  540. ERR_FAIL_COND(p_new_size.width < 0 || p_new_size.height < 0);
  541. if (p_new_size == get_size()) {
  542. return;
  543. }
  544. Ref<BitMap> new_bitmap;
  545. new_bitmap.instantiate();
  546. new_bitmap->create(p_new_size);
  547. // also allow for upscaling
  548. int lw = (width == 0) ? 0 : p_new_size.width;
  549. int lh = (height == 0) ? 0 : p_new_size.height;
  550. float scale_x = ((float)width / p_new_size.width);
  551. float scale_y = ((float)height / p_new_size.height);
  552. for (int x = 0; x < lw; x++) {
  553. for (int y = 0; y < lh; y++) {
  554. bool new_bit = get_bit(x * scale_x, y * scale_y);
  555. new_bitmap->set_bit(x, y, new_bit);
  556. }
  557. }
  558. width = new_bitmap->width;
  559. height = new_bitmap->height;
  560. bitmask = new_bitmap->bitmask;
  561. }
  562. Ref<Image> BitMap::convert_to_image() const {
  563. Ref<Image> image = Image::create_empty(width, height, false, Image::FORMAT_L8);
  564. for (int i = 0; i < width; i++) {
  565. for (int j = 0; j < height; j++) {
  566. image->set_pixel(i, j, get_bit(i, j) ? Color(1, 1, 1) : Color(0, 0, 0));
  567. }
  568. }
  569. return image;
  570. }
  571. void BitMap::blit(const Vector2i &p_pos, const Ref<BitMap> &p_bitmap) {
  572. ERR_FAIL_COND_MSG(p_bitmap.is_null(), "It's not a reference to a valid BitMap object.");
  573. int x = p_pos.x;
  574. int y = p_pos.y;
  575. int w = p_bitmap->get_size().width;
  576. int h = p_bitmap->get_size().height;
  577. for (int i = 0; i < w; i++) {
  578. for (int j = 0; j < h; j++) {
  579. int px = x + i;
  580. int py = y + j;
  581. if (px < 0 || px >= width) {
  582. continue;
  583. }
  584. if (py < 0 || py >= height) {
  585. continue;
  586. }
  587. if (p_bitmap->get_bit(i, j)) {
  588. set_bit(px, py, true);
  589. }
  590. }
  591. }
  592. }
  593. void BitMap::_bind_methods() {
  594. ClassDB::bind_method(D_METHOD("create", "size"), &BitMap::create);
  595. ClassDB::bind_method(D_METHOD("create_from_image_alpha", "image", "threshold"), &BitMap::create_from_image_alpha, DEFVAL(0.1));
  596. ClassDB::bind_method(D_METHOD("set_bitv", "position", "bit"), &BitMap::set_bitv);
  597. ClassDB::bind_method(D_METHOD("set_bit", "x", "y", "bit"), &BitMap::set_bit);
  598. ClassDB::bind_method(D_METHOD("get_bitv", "position"), &BitMap::get_bitv);
  599. ClassDB::bind_method(D_METHOD("get_bit", "x", "y"), &BitMap::get_bit);
  600. ClassDB::bind_method(D_METHOD("set_bit_rect", "rect", "bit"), &BitMap::set_bit_rect);
  601. ClassDB::bind_method(D_METHOD("get_true_bit_count"), &BitMap::get_true_bit_count);
  602. ClassDB::bind_method(D_METHOD("get_size"), &BitMap::get_size);
  603. ClassDB::bind_method(D_METHOD("resize", "new_size"), &BitMap::resize);
  604. ClassDB::bind_method(D_METHOD("_set_data", "data"), &BitMap::_set_data);
  605. ClassDB::bind_method(D_METHOD("_get_data"), &BitMap::_get_data);
  606. ClassDB::bind_method(D_METHOD("grow_mask", "pixels", "rect"), &BitMap::grow_mask);
  607. ClassDB::bind_method(D_METHOD("convert_to_image"), &BitMap::convert_to_image);
  608. ClassDB::bind_method(D_METHOD("opaque_to_polygons", "rect", "epsilon"), &BitMap::_opaque_to_polygons_bind, DEFVAL(2.0));
  609. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  610. }
  611. BitMap::BitMap() {}