ImageRegion.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_image_ImageRegion_h
  6. #define mozilla_image_ImageRegion_h
  7. #include "gfxRect.h"
  8. #include "mozilla/gfx/Types.h"
  9. namespace mozilla {
  10. namespace image {
  11. /**
  12. * An axis-aligned rectangle in tiled image space, with an optional sampling
  13. * restriction rect. The drawing code ensures that if a sampling restriction
  14. * rect is present, any pixels sampled during the drawing process are found
  15. * within that rect.
  16. *
  17. * The sampling restriction rect exists primarily for callers which perform
  18. * pixel snapping. Other callers should generally use one of the Create()
  19. * overloads.
  20. */
  21. class ImageRegion
  22. {
  23. typedef mozilla::gfx::ExtendMode ExtendMode;
  24. public:
  25. static ImageRegion Empty()
  26. {
  27. return ImageRegion(gfxRect(), ExtendMode::CLAMP);
  28. }
  29. static ImageRegion Create(const gfxRect& aRect,
  30. ExtendMode aExtendMode = ExtendMode::CLAMP)
  31. {
  32. return ImageRegion(aRect, aExtendMode);
  33. }
  34. static ImageRegion Create(const gfxSize& aSize,
  35. ExtendMode aExtendMode = ExtendMode::CLAMP)
  36. {
  37. return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
  38. }
  39. static ImageRegion Create(const nsIntSize& aSize,
  40. ExtendMode aExtendMode = ExtendMode::CLAMP)
  41. {
  42. return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
  43. }
  44. static ImageRegion CreateWithSamplingRestriction(const gfxRect& aRect,
  45. const gfxRect& aRestriction,
  46. ExtendMode aExtendMode = ExtendMode::CLAMP)
  47. {
  48. return ImageRegion(aRect, aRestriction, aExtendMode);
  49. }
  50. bool IsRestricted() const { return mIsRestricted; }
  51. const gfxRect& Rect() const { return mRect; }
  52. const gfxRect& Restriction() const
  53. {
  54. MOZ_ASSERT(mIsRestricted);
  55. return mRestriction;
  56. }
  57. bool RestrictionContains(const gfxRect& aRect) const
  58. {
  59. if (!mIsRestricted) {
  60. return true;
  61. }
  62. return mRestriction.Contains(aRect);
  63. }
  64. ImageRegion Intersect(const gfxRect& aRect) const
  65. {
  66. if (mIsRestricted) {
  67. return CreateWithSamplingRestriction(aRect.Intersect(mRect),
  68. aRect.Intersect(mRestriction));
  69. }
  70. return Create(aRect.Intersect(mRect));
  71. }
  72. gfxRect IntersectAndRestrict(const gfxRect& aRect) const
  73. {
  74. gfxRect intersection = mRect.Intersect(aRect);
  75. if (mIsRestricted) {
  76. intersection = mRestriction.Intersect(intersection);
  77. }
  78. return intersection;
  79. }
  80. void MoveBy(gfxFloat dx, gfxFloat dy)
  81. {
  82. mRect.MoveBy(dx, dy);
  83. if (mIsRestricted) {
  84. mRestriction.MoveBy(dx, dy);
  85. }
  86. }
  87. void Scale(gfxFloat sx, gfxFloat sy)
  88. {
  89. mRect.Scale(sx, sy);
  90. if (mIsRestricted) {
  91. mRestriction.Scale(sx, sy);
  92. }
  93. }
  94. void TransformBy(const gfxMatrix& aMatrix)
  95. {
  96. mRect = aMatrix.Transform(mRect);
  97. if (mIsRestricted) {
  98. mRestriction = aMatrix.Transform(mRestriction);
  99. }
  100. }
  101. void TransformBoundsBy(const gfxMatrix& aMatrix)
  102. {
  103. mRect = aMatrix.TransformBounds(mRect);
  104. if (mIsRestricted) {
  105. mRestriction = aMatrix.TransformBounds(mRestriction);
  106. }
  107. }
  108. ImageRegion operator-(const gfxPoint& aPt) const
  109. {
  110. if (mIsRestricted) {
  111. return CreateWithSamplingRestriction(mRect - aPt, mRestriction - aPt);
  112. }
  113. return Create(mRect - aPt);
  114. }
  115. ImageRegion operator+(const gfxPoint& aPt) const
  116. {
  117. if (mIsRestricted) {
  118. return CreateWithSamplingRestriction(mRect + aPt, mRestriction + aPt);
  119. }
  120. return Create(mRect + aPt);
  121. }
  122. gfx::ExtendMode GetExtendMode() const
  123. {
  124. return mExtendMode;
  125. }
  126. /* ImageRegion() : mIsRestricted(false) { } */
  127. private:
  128. explicit ImageRegion(const gfxRect& aRect, ExtendMode aExtendMode)
  129. : mRect(aRect)
  130. , mExtendMode(aExtendMode)
  131. , mIsRestricted(false)
  132. { }
  133. ImageRegion(const gfxRect& aRect, const gfxRect& aRestriction, ExtendMode aExtendMode)
  134. : mRect(aRect)
  135. , mRestriction(aRestriction)
  136. , mExtendMode(aExtendMode)
  137. , mIsRestricted(true)
  138. { }
  139. gfxRect mRect;
  140. gfxRect mRestriction;
  141. ExtendMode mExtendMode;
  142. bool mIsRestricted;
  143. };
  144. } // namespace image
  145. } // namespace mozilla
  146. #endif // mozilla_image_ImageRegion_h