Bug 1467571 [wpt PR 11385] - Make manifest's parsers quicker, a=testonly
[gecko.git] / image / ImageRegion.h
blobf79415b9c315b55912a858bab81444dd1d9fa622
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 "gfxRect.h"
10 #include "mozilla/gfx/Types.h"
12 namespace mozilla {
13 namespace image {
15 /**
16 * An axis-aligned rectangle in tiled image space, with an optional sampling
17 * restriction rect. The drawing code ensures that if a sampling restriction
18 * rect is present, any pixels sampled during the drawing process are found
19 * within that rect.
21 * The sampling restriction rect exists primarily for callers which perform
22 * pixel snapping. Other callers should generally use one of the Create()
23 * overloads.
25 class ImageRegion
27 typedef mozilla::gfx::ExtendMode ExtendMode;
29 public:
30 static ImageRegion Empty()
32 return ImageRegion(gfxRect(), ExtendMode::CLAMP);
35 static ImageRegion Create(const gfxRect& aRect,
36 ExtendMode aExtendMode = ExtendMode::CLAMP)
38 return ImageRegion(aRect, aExtendMode);
41 static ImageRegion Create(const gfxSize& aSize,
42 ExtendMode aExtendMode = ExtendMode::CLAMP)
44 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
47 static ImageRegion Create(const nsIntSize& aSize,
48 ExtendMode aExtendMode = ExtendMode::CLAMP)
50 return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
53 static ImageRegion CreateWithSamplingRestriction(const gfxRect& aRect,
54 const gfxRect& aRestriction,
55 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
65 MOZ_ASSERT(mIsRestricted);
66 return mRestriction;
69 bool RestrictionContains(const gfxRect& aRect) const
71 if (!mIsRestricted) {
72 return true;
74 return mRestriction.Contains(aRect);
77 ImageRegion Intersect(const gfxRect& aRect) const
79 if (mIsRestricted) {
80 return CreateWithSamplingRestriction(aRect.Intersect(mRect),
81 aRect.Intersect(mRestriction));
83 return Create(aRect.Intersect(mRect));
86 gfxRect IntersectAndRestrict(const gfxRect& aRect) const
88 gfxRect intersection = mRect.Intersect(aRect);
89 if (mIsRestricted) {
90 intersection = mRestriction.Intersect(intersection);
92 return intersection;
95 void MoveBy(gfxFloat dx, gfxFloat dy)
97 mRect.MoveBy(dx, dy);
98 if (mIsRestricted) {
99 mRestriction.MoveBy(dx, dy);
103 void Scale(gfxFloat sx, gfxFloat sy)
105 mRect.Scale(sx, sy);
106 if (mIsRestricted) {
107 mRestriction.Scale(sx, sy);
111 void TransformBy(const gfxMatrix& aMatrix)
113 mRect = aMatrix.TransformRect(mRect);
114 if (mIsRestricted) {
115 mRestriction = aMatrix.TransformRect(mRestriction);
119 void TransformBoundsBy(const gfxMatrix& aMatrix)
121 mRect = aMatrix.TransformBounds(mRect);
122 if (mIsRestricted) {
123 mRestriction = aMatrix.TransformBounds(mRestriction);
127 ImageRegion operator-(const gfxPoint& aPt) const
129 if (mIsRestricted) {
130 return CreateWithSamplingRestriction(mRect - aPt, mRestriction - aPt);
132 return Create(mRect - aPt);
135 ImageRegion operator+(const gfxPoint& aPt) const
137 if (mIsRestricted) {
138 return CreateWithSamplingRestriction(mRect + aPt, mRestriction + aPt);
140 return Create(mRect + aPt);
143 gfx::ExtendMode GetExtendMode() const
145 return mExtendMode;
148 /* ImageRegion() : mIsRestricted(false) { } */
150 private:
151 explicit ImageRegion(const gfxRect& aRect, ExtendMode aExtendMode)
152 : mRect(aRect)
153 , mExtendMode(aExtendMode)
154 , mIsRestricted(false)
157 ImageRegion(const gfxRect& aRect, const gfxRect& aRestriction, ExtendMode aExtendMode)
158 : mRect(aRect)
159 , mRestriction(aRestriction)
160 , mExtendMode(aExtendMode)
161 , mIsRestricted(true)
164 gfxRect mRect;
165 gfxRect mRestriction;
166 ExtendMode mExtendMode;
167 bool mIsRestricted;
170 } // namespace image
171 } // namespace mozilla
173 #endif // mozilla_image_ImageRegion_h