Backed out 3 changesets (bug 1892041) for causing SM failures in test262. CLOSED...
[gecko.git] / image / ImageRegion.h
blobed592bbbeac7224b75c26817078c1ec16caf034e
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"
16 #include "PLDHashTable.h" // for PLDHashNumber
18 namespace mozilla {
19 namespace image {
21 /**
22 * An axis-aligned rectangle in tiled image space, with an optional sampling
23 * restriction rect. The drawing code ensures that if a sampling restriction
24 * rect is present, any pixels sampled during the drawing process are found
25 * within that rect.
27 * The sampling restriction rect exists primarily for callers which perform
28 * pixel snapping. Other callers should generally use one of the Create()
29 * overloads.
31 class ImageRegion {
32 typedef mozilla::gfx::ExtendMode ExtendMode;
34 public:
35 static ImageRegion Empty() {
36 return ImageRegion(gfxRect(), ExtendMode::CLAMP);
39 static ImageRegion Create(const gfxRect& aRect,
40 ExtendMode aExtendMode = ExtendMode::CLAMP) {
41 return ImageRegion(aRect, aExtendMode);
44 static ImageRegion Create(const gfxSize& aSize,
45 ExtendMode aExtendMode = ExtendMode::CLAMP) {
46 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
49 static ImageRegion Create(const nsIntSize& aSize,
50 ExtendMode aExtendMode = ExtendMode::CLAMP) {
51 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
54 static ImageRegion CreateWithSamplingRestriction(
55 const gfxRect& aRect, const gfxRect& aRestriction,
56 ExtendMode aExtendMode = ExtendMode::CLAMP) {
57 return ImageRegion(aRect, aRestriction, aExtendMode);
60 bool IsRestricted() const { return mIsRestricted; }
61 const gfxRect& Rect() const { return mRect; }
63 const gfxRect& Restriction() const {
64 MOZ_ASSERT(mIsRestricted);
65 return mRestriction;
68 bool RestrictionContains(const gfxRect& aRect) const {
69 if (!mIsRestricted) {
70 return true;
72 return mRestriction.Contains(aRect);
75 ImageRegion Intersect(const gfxRect& aRect) const {
76 if (mIsRestricted) {
77 return CreateWithSamplingRestriction(aRect.Intersect(mRect),
78 aRect.Intersect(mRestriction));
80 return Create(aRect.Intersect(mRect));
83 gfxRect IntersectAndRestrict(const gfxRect& aRect) const {
84 gfxRect intersection = mRect.Intersect(aRect);
85 if (mIsRestricted) {
86 intersection = mRestriction.Intersect(intersection);
88 return intersection;
91 void MoveBy(gfxFloat dx, gfxFloat dy) {
92 mRect.MoveBy(dx, dy);
93 if (mIsRestricted) {
94 mRestriction.MoveBy(dx, dy);
98 void Scale(gfxFloat sx, gfxFloat sy) {
99 mRect.Scale(sx, sy);
100 if (mIsRestricted) {
101 mRestriction.Scale(sx, sy);
105 void TransformBy(const gfxMatrix& aMatrix) {
106 mRect = aMatrix.TransformRect(mRect);
107 if (mIsRestricted) {
108 mRestriction = aMatrix.TransformRect(mRestriction);
112 void TransformBoundsBy(const gfxMatrix& aMatrix) {
113 mRect = aMatrix.TransformBounds(mRect);
114 if (mIsRestricted) {
115 mRestriction = aMatrix.TransformBounds(mRestriction);
119 ImageRegion operator-(const gfxPoint& aPt) const {
120 if (mIsRestricted) {
121 return CreateWithSamplingRestriction(mRect - aPt, mRestriction - aPt);
123 return Create(mRect - aPt);
126 ImageRegion operator+(const gfxPoint& aPt) const {
127 if (mIsRestricted) {
128 return CreateWithSamplingRestriction(mRect + aPt, mRestriction + aPt);
130 return Create(mRect + aPt);
133 gfx::ExtendMode GetExtendMode() const { return mExtendMode; }
135 /* ImageRegion() : mIsRestricted(false) { } */
137 private:
138 explicit ImageRegion(const gfxRect& aRect, ExtendMode aExtendMode)
139 : mRect(aRect), mExtendMode(aExtendMode), mIsRestricted(false) {}
141 ImageRegion(const gfxRect& aRect, const gfxRect& aRestriction,
142 ExtendMode aExtendMode)
143 : mRect(aRect),
144 mRestriction(aRestriction),
145 mExtendMode(aExtendMode),
146 mIsRestricted(true) {}
148 gfxRect mRect;
149 gfxRect mRestriction;
150 ExtendMode mExtendMode;
151 bool mIsRestricted;
155 * An axis-aligned rectangle in tiled image space, with an optional sampling
156 * restriction rect. The drawing code ensures that if a sampling restriction
157 * rect is present, any pixels sampled during the drawing process are found
158 * within that rect.
160 * The sampling restriction rect exists primarily for callers which perform
161 * pixel snapping. Other callers should generally use one of the Create()
162 * overloads.
164 class ImageIntRegion {
165 typedef mozilla::gfx::ExtendMode ExtendMode;
167 public:
168 static ImageIntRegion Empty() {
169 return ImageIntRegion(mozilla::gfx::IntRect(), ExtendMode::CLAMP);
172 static ImageIntRegion Create(const mozilla::gfx::IntRect& aRect,
173 ExtendMode aExtendMode = ExtendMode::CLAMP) {
174 return ImageIntRegion(aRect, aExtendMode);
177 static ImageIntRegion Create(const mozilla::gfx::IntSize& aSize,
178 ExtendMode aExtendMode = ExtendMode::CLAMP) {
179 return ImageIntRegion(
180 mozilla::gfx::IntRect(0, 0, aSize.width, aSize.height), aExtendMode);
183 static ImageIntRegion CreateWithSamplingRestriction(
184 const mozilla::gfx::IntRect& aRect,
185 const mozilla::gfx::IntRect& aRestriction,
186 ExtendMode aExtendMode = ExtendMode::CLAMP) {
187 return ImageIntRegion(aRect, aRestriction, aExtendMode);
190 bool IsRestricted() const { return mIsRestricted; }
191 const mozilla::gfx::IntRect& Rect() const { return mRect; }
193 const mozilla::gfx::IntRect& Restriction() const {
194 MOZ_ASSERT(mIsRestricted);
195 return mRestriction;
198 bool RestrictionContains(const mozilla::gfx::IntRect& aRect) const {
199 if (!mIsRestricted) {
200 return true;
202 return mRestriction.Contains(aRect);
205 ImageIntRegion Intersect(const mozilla::gfx::IntRect& aRect) const {
206 if (mIsRestricted) {
207 return CreateWithSamplingRestriction(aRect.Intersect(mRect),
208 aRect.Intersect(mRestriction));
210 return Create(aRect.Intersect(mRect));
213 mozilla::gfx::IntRect IntersectAndRestrict(
214 const mozilla::gfx::IntRect& aRect) const {
215 mozilla::gfx::IntRect intersection = mRect.Intersect(aRect);
216 if (mIsRestricted) {
217 intersection = mRestriction.Intersect(intersection);
219 return intersection;
222 gfx::ExtendMode GetExtendMode() const { return mExtendMode; }
224 ImageRegion ToImageRegion() const {
225 if (mIsRestricted) {
226 return ImageRegion::CreateWithSamplingRestriction(
227 gfxRect(mRect.x, mRect.y, mRect.width, mRect.height),
228 gfxRect(mRestriction.x, mRestriction.y, mRestriction.width,
229 mRestriction.height),
230 mExtendMode);
232 return ImageRegion::Create(
233 gfxRect(mRect.x, mRect.y, mRect.width, mRect.height), mExtendMode);
236 bool operator==(const ImageIntRegion& aOther) const {
237 return mExtendMode == aOther.mExtendMode &&
238 mIsRestricted == aOther.mIsRestricted &&
239 mRect.IsEqualEdges(aOther.mRect) &&
240 (!mIsRestricted || mRestriction.IsEqualEdges(aOther.mRestriction));
243 PLDHashNumber Hash() const {
244 return HashGeneric(mRect.x, mRect.y, mRect.width, mRect.height,
245 mRestriction.x, mRestriction.y, mRestriction.width,
246 mRestriction.height, mExtendMode, mIsRestricted);
249 /* ImageIntRegion() : mIsRestricted(false) { } */
251 private:
252 explicit ImageIntRegion(const mozilla::gfx::IntRect& aRect,
253 ExtendMode aExtendMode)
254 : mRect(aRect), mExtendMode(aExtendMode), mIsRestricted(false) {}
256 ImageIntRegion(const mozilla::gfx::IntRect& aRect,
257 const mozilla::gfx::IntRect& aRestriction,
258 ExtendMode aExtendMode)
259 : mRect(aRect),
260 mRestriction(aRestriction),
261 mExtendMode(aExtendMode),
262 mIsRestricted(true) {}
264 mozilla::gfx::IntRect mRect;
265 mozilla::gfx::IntRect mRestriction;
266 ExtendMode mExtendMode;
267 bool mIsRestricted;
270 } // namespace image
271 } // namespace mozilla
273 #endif // mozilla_image_ImageRegion_h