You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Region.cxx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2016-2020 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <rfb/Region.h>
  23. #include <rfb/LogWriter.h>
  24. extern "C" {
  25. #include <pixman.h>
  26. }
  27. static rfb::LogWriter vlog("Region");
  28. rfb::Region::Region() {
  29. rgn = new struct pixman_region16;
  30. pixman_region_init(rgn);
  31. }
  32. rfb::Region::Region(const Rect& r) {
  33. rgn = new struct pixman_region16;
  34. pixman_region_init_rect(rgn, r.tl.x, r.tl.y, r.width(), r.height());
  35. }
  36. rfb::Region::Region(const rfb::Region& r) {
  37. rgn = new struct pixman_region16;
  38. pixman_region_init(rgn);
  39. pixman_region_copy(rgn, r.rgn);
  40. }
  41. rfb::Region::~Region() {
  42. pixman_region_fini(rgn);
  43. delete rgn;
  44. }
  45. rfb::Region& rfb::Region::operator=(const rfb::Region& r) {
  46. pixman_region_copy(rgn, r.rgn);
  47. return *this;
  48. }
  49. void rfb::Region::clear() {
  50. // pixman_region_clear() isn't available on some older systems
  51. pixman_region_fini(rgn);
  52. pixman_region_init(rgn);
  53. }
  54. void rfb::Region::reset(const Rect& r) {
  55. pixman_region_fini(rgn);
  56. pixman_region_init_rect(rgn, r.tl.x, r.tl.y, r.width(), r.height());
  57. }
  58. void rfb::Region::translate(const Point& delta) {
  59. pixman_region_translate(rgn, delta.x, delta.y);
  60. }
  61. void rfb::Region::assign_intersect(const rfb::Region& r) {
  62. pixman_region_intersect(rgn, rgn, r.rgn);
  63. }
  64. void rfb::Region::assign_union(const rfb::Region& r) {
  65. pixman_region_union(rgn, rgn, r.rgn);
  66. }
  67. void rfb::Region::assign_subtract(const rfb::Region& r) {
  68. pixman_region_subtract(rgn, rgn, r.rgn);
  69. }
  70. rfb::Region rfb::Region::intersect(const rfb::Region& r) const {
  71. rfb::Region ret;
  72. pixman_region_intersect(ret.rgn, rgn, r.rgn);
  73. return ret;
  74. }
  75. rfb::Region rfb::Region::union_(const rfb::Region& r) const {
  76. rfb::Region ret;
  77. pixman_region_union(ret.rgn, rgn, r.rgn);
  78. return ret;
  79. }
  80. rfb::Region rfb::Region::subtract(const rfb::Region& r) const {
  81. rfb::Region ret;
  82. pixman_region_subtract(ret.rgn, rgn, r.rgn);
  83. return ret;
  84. }
  85. bool rfb::Region::equals(const rfb::Region& r) const {
  86. return pixman_region_equal(rgn, r.rgn);
  87. }
  88. int rfb::Region::numRects() const {
  89. return pixman_region_n_rects(rgn);
  90. }
  91. bool rfb::Region::get_rects(std::vector<Rect>* rects,
  92. bool left2right, bool topdown) const
  93. {
  94. int nRects;
  95. const pixman_box16_t* boxes;
  96. int xInc, yInc, i;
  97. boxes = pixman_region_rectangles(rgn, &nRects);
  98. rects->clear();
  99. rects->reserve(nRects);
  100. xInc = left2right ? 1 : -1;
  101. yInc = topdown ? 1 : -1;
  102. i = topdown ? 0 : nRects-1;
  103. while (nRects > 0) {
  104. int firstInNextBand = i;
  105. int nRectsInBand = 0;
  106. while (nRects > 0 && boxes[firstInNextBand].y1 == boxes[i].y1)
  107. {
  108. firstInNextBand += yInc;
  109. nRects--;
  110. nRectsInBand++;
  111. }
  112. if (xInc != yInc)
  113. i = firstInNextBand - yInc;
  114. while (nRectsInBand > 0) {
  115. Rect r(boxes[i].x1, boxes[i].y1, boxes[i].x2, boxes[i].y2);
  116. rects->push_back(r);
  117. i += xInc;
  118. nRectsInBand--;
  119. }
  120. i = firstInNextBand;
  121. }
  122. return !rects->empty();
  123. }
  124. rfb::Rect rfb::Region::get_bounding_rect() const {
  125. const pixman_box16_t* extents;
  126. extents = pixman_region_extents(rgn);
  127. return Rect(extents->x1, extents->y1, extents->x2, extents->y2);
  128. }
  129. void rfb::Region::debug_print(const char* prefix) const
  130. {
  131. Rect extents;
  132. std::vector<Rect> rects;
  133. std::vector<Rect>::const_iterator iter;
  134. extents = get_bounding_rect();
  135. get_rects(&rects);
  136. vlog.debug("%s num rects %3ld extents %3d,%3d %3dx%3d",
  137. prefix, (long)rects.size(), extents.tl.x, extents.tl.y,
  138. extents.width(), extents.height());
  139. for (iter = rects.begin(); iter != rects.end(); ++iter) {
  140. vlog.debug(" rect %3d,%3d %3dx%3d",
  141. iter->tl.x, iter->tl.y, iter->width(), iter->height());
  142. }
  143. }