Bug 1732043 [wpt PR 30820] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / image / ImageRegion.h
blob2099fd2eab4ca5fe361d1aa55c1cbb93fa231f55
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/. */
6 #ifndef mozilla_image_ImageRegion_h
7 #define mozilla_image_ImageRegion_h
9 #include "gfxMatrix.h"
10 #include "gfxPoint.h"
11 #include "gfxRect.h"
12 #include "gfxTypes.h"
13 #include "mozilla/gfx/Matrix.h"
14 #include "mozilla/gfx/Types.h"
15 #include "nsSize.h"
17 namespace mozilla {
18 namespace image {
20 /**
21 * An axis-aligned rectangle in tiled image space, with an optional sampling
22 * restriction rect. The drawing code ensures that if a sampling restriction
23 * rect is present, any pixels sampled during the drawing process are found
24 * within that rect.
26 * The sampling restriction rect exists primarily for callers which perform
27 * pixel snapping. Other callers should generally use one of the Create()
28 * overloads.
30 class ImageRegion {
31 typedef mozilla::gfx::ExtendMode ExtendMode;
33 public:
34 static ImageRegion Empty() {
35 return ImageRegion(gfxRect(), ExtendMode::CLAMP);
38 static ImageRegion Create(const gfxRect& aRect,
39 ExtendMode aExtendMode = ExtendMode::CLAMP) {
40 return ImageRegion(aRect, aExtendMode);
43 static ImageRegion Create(const gfxSize& aSize,
44 ExtendMode aExtendMode = ExtendMode::CLAMP) {
45 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
48 static ImageRegion Create(const nsIntSize& aSize,
49 ExtendMode aExtendMode = ExtendMode::CLAMP) {
50 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
53 static ImageRegion CreateWithSamplingRestriction(
54 const gfxRect& aRect, const gfxRect& aRestriction,
55 ExtendMode aExtendMode = ExtendMode::CLAMP) {
56 return ImageRegion(aRect, aRestriction, aExtendMode);
59 bool IsRestricted() const { return mIsRestricted; }
60 const gfxRect& Rect() const { return mRect; }
62 const gfxRect& Restriction() const {
63 MOZ_ASSERT(mIsRestricted);
64 return mRestriction;
67 bool RestrictionContains(const gfxRect& aRect) const {
68 if (!mIsRestricted) {
69 return true;
71 return mRestriction.Contains(aRect);
74 ImageRegion Intersect(const gfxRect& aRect) const {
75 if (mIsRestricted) {
76 return CreateWithSamplingRestriction(aRect.Intersect(mRect),
77 aRect.Intersect(mRestriction));
79 return Create(aRect.Intersect(mRect));
82 gfxRect IntersectAndRestrict(const gfxRect& aRect) const {
83 gfxRect intersection = mRect.Intersect(aRect);
84 if (mIsRestricted) {
85 intersection = mRestriction.Intersect(intersection);
87 return intersection;
90 void MoveBy(gfxFloat dx, gfxFloat dy) {
91 mRect.MoveBy(dx, dy);
92 if (mIsRestricted) {
93 mRestriction.MoveBy(dx, dy);
97 void Scale(gfxFloat sx, gfxFloat sy) {
98 mRect.Scale(sx, sy);
99 if (mIsRestricted) {
100 mRestriction.Scale(sx, sy);
104 void TransformBy(const gfxMatrix& aMatrix) {
105 mRect = aMatrix.TransformRect(mRect);
106 if (mIsRestricted) {
107 mRestriction = aMatrix.TransformRect(mRestriction);
111 void TransformBoundsBy(const gfxMatrix& aMatrix) {
112 mRect = aMatrix.TransformBounds(mRect);
113 if (mIsRestricted) {
114 mRestriction = aMatrix.TransformBounds(mRestriction);
118 ImageRegion operator-(const gfxPoint& aPt) const {
119 if (mIsRestricted) {
120 return CreateWithSamplingRestriction(mRect - aPt, mRestriction - aPt);
122 return Create(mRect - aPt);
125 ImageRegion operator+(const gfxPoint& aPt) const {
126 if (mIsRestricted) {
127 return CreateWithSamplingRestriction(mRect + aPt, mRestriction + aPt);
129 return Create(mRect + aPt);
132 gfx::ExtendMode GetExtendMode() const { return mExtendMode; }
134 /* ImageRegion() : mIsRestricted(false) { } */
136 private:
137 explicit ImageRegion(const gfxRect& aRect, ExtendMode aExtendMode)
138 : mRect(aRect), mExtendMode(aExtendMode), mIsRestricted(false) {}
140 ImageRegion(const gfxRect& aRect, const gfxRect& aRestriction,
141 ExtendMode aExtendMode)
142 : mRect(aRect),
143 mRestriction(aRestriction),
144 mExtendMode(aExtendMode),
145 mIsRestricted(true) {}
147 gfxRect mRect;
148 gfxRect mRestriction;
149 ExtendMode mExtendMode;
150 bool mIsRestricted;
154 * An axis-aligned rectangle in tiled image space, with an optional sampling
155 * restriction rect. The drawing code ensures that if a sampling restriction
156 * rect is present, any pixels sampled during the drawing process are found
157 * within that rect.
159 * The sampling restriction rect exists primarily for callers which perform
160 * pixel snapping. Other callers should generally use one of the Create()
161 * overloads.
163 class ImageIntRegion {
164 typedef mozilla::gfx::ExtendMode ExtendMode;
166 public:
167 static ImageIntRegion Empty() {
168 return ImageIntRegion(mozilla::gfx::IntRect(), ExtendMode::CLAMP);
171 static ImageIntRegion Create(const mozilla::gfx::IntRect& aRect,
172 ExtendMode aExtendMode = ExtendMode::CLAMP) {
173 return ImageIntRegion(aRect, aExtendMode);
176 static ImageIntRegion Create(const mozilla::gfx::IntSize& aSize,
177 ExtendMode aExtendMode = ExtendMode::CLAMP) {
178 return ImageIntRegion(
179 mozilla::gfx::IntRect(0, 0, aSize.width, aSize.height), aExtendMode);
182 static ImageIntRegion CreateWithSamplingRestriction(
183 const mozilla::gfx::IntRect& aRect,
184 const mozilla::gfx::IntRect& aRestriction,
185 ExtendMode aExtendMode = ExtendMode::CLAMP) {
186 return ImageIntRegion(aRect, aRestriction, aExtendMode);
189 bool IsRestricted() const { return mIsRestricted; }
190 const mozilla::gfx::IntRect& Rect() const { return mRect; }
192 const mozilla::gfx::IntRect& Restriction() const {
193 MOZ_ASSERT(mIsRestricted);
194 return mRestriction;
197 bool RestrictionContains(const mozilla::gfx::IntRect& aRect) const {
198 if (!mIsRestricted) {
199 return true;
201 return mRestriction.Contains(aRect);
204 ImageIntRegion Intersect(const mozilla::gfx::IntRect& aRect) const {
205 if (mIsRestricted) {
206 return CreateWithSamplingRestriction(aRect.Intersect(mRect),
207 aRect.Intersect(mRestriction));
209 return Create(aRect.Intersect(mRect));
212 mozilla::gfx::IntRect IntersectAndRestrict(
213 const mozilla::gfx::IntRect& aRect) const {
214 mozilla::gfx::IntRect intersection = mRect.Intersect(aRect);
215 if (mIsRestricted) {
216 intersection = mRestriction.Intersect(intersection);
218 return intersection;
221 gfx::ExtendMode GetExtendMode() const { return mExtendMode; }
223 ImageRegion ToImageRegion() const {
224 if (mIsRestricted) {
225 return ImageRegion::CreateWithSamplingRestriction(
226 gfxRect(mRect.x, mRect.y, mRect.width, mRect.height),
227 gfxRect(mRestriction.x, mRestriction.y, mRestriction.width,
228 mRestriction.height),
229 mExtendMode);
231 return ImageRegion::Create(
232 gfxRect(mRect.x, mRect.y, mRect.width, mRect.height), mExtendMode);
235 bool operator==(const ImageIntRegion& aOther) const {
236 return mExtendMode == aOther.mExtendMode &&
237 mIsRestricted == aOther.mIsRestricted &&
238 mRect.IsEqualEdges(aOther.mRect) &&
239 (!mIsRestricted || mRestriction.IsEqualEdges(aOther.mRestriction));
242 /* ImageIntRegion() : mIsRestricted(false) { } */
244 private:
245 explicit ImageIntRegion(const mozilla::gfx::IntRect& aRect,
246 ExtendMode aExtendMode)
247 : mRect(aRect), mExtendMode(aExtendMode), mIsRestricted(false) {}
249 ImageIntRegion(const mozilla::gfx::IntRect& aRect,
250 const mozilla::gfx::IntRect& aRestriction,
251 ExtendMode aExtendMode)
252 : mRect(aRect),
253 mRestriction(aRestriction),
254 mExtendMode(aExtendMode),
255 mIsRestricted(true) {}
257 mozilla::gfx::IntRect mRect;
258 mozilla::gfx::IntRect mRestriction;
259 ExtendMode mExtendMode;
260 bool mIsRestricted;
263 } // namespace image
264 } // namespace mozilla
266 #endif // mozilla_image_ImageRegion_h