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
13 #include "mozilla/gfx/Matrix.h"
14 #include "mozilla/gfx/Types.h"
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
26 * The sampling restriction rect exists primarily for callers which perform
27 * pixel snapping. Other callers should generally use one of the Create()
31 typedef mozilla::gfx::ExtendMode ExtendMode
;
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
);
67 bool RestrictionContains(const gfxRect
& aRect
) const {
71 return mRestriction
.Contains(aRect
);
74 ImageRegion
Intersect(const gfxRect
& aRect
) const {
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
);
85 intersection
= mRestriction
.Intersect(intersection
);
90 void MoveBy(gfxFloat dx
, gfxFloat dy
) {
93 mRestriction
.MoveBy(dx
, dy
);
97 void Scale(gfxFloat sx
, gfxFloat sy
) {
100 mRestriction
.Scale(sx
, sy
);
104 void TransformBy(const gfxMatrix
& aMatrix
) {
105 mRect
= aMatrix
.TransformRect(mRect
);
107 mRestriction
= aMatrix
.TransformRect(mRestriction
);
111 void TransformBoundsBy(const gfxMatrix
& aMatrix
) {
112 mRect
= aMatrix
.TransformBounds(mRect
);
114 mRestriction
= aMatrix
.TransformBounds(mRestriction
);
118 ImageRegion
operator-(const gfxPoint
& aPt
) const {
120 return CreateWithSamplingRestriction(mRect
- aPt
, mRestriction
- aPt
);
122 return Create(mRect
- aPt
);
125 ImageRegion
operator+(const gfxPoint
& aPt
) const {
127 return CreateWithSamplingRestriction(mRect
+ aPt
, mRestriction
+ aPt
);
129 return Create(mRect
+ aPt
);
132 gfx::ExtendMode
GetExtendMode() const { return mExtendMode
; }
134 /* ImageRegion() : mIsRestricted(false) { } */
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
)
143 mRestriction(aRestriction
),
144 mExtendMode(aExtendMode
),
145 mIsRestricted(true) {}
148 gfxRect mRestriction
;
149 ExtendMode mExtendMode
;
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
159 * The sampling restriction rect exists primarily for callers which perform
160 * pixel snapping. Other callers should generally use one of the Create()
163 class ImageIntRegion
{
164 typedef mozilla::gfx::ExtendMode ExtendMode
;
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
);
197 bool RestrictionContains(const mozilla::gfx::IntRect
& aRect
) const {
198 if (!mIsRestricted
) {
201 return mRestriction
.Contains(aRect
);
204 ImageIntRegion
Intersect(const mozilla::gfx::IntRect
& aRect
) const {
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
);
216 intersection
= mRestriction
.Intersect(intersection
);
221 gfx::ExtendMode
GetExtendMode() const { return mExtendMode
; }
223 ImageRegion
ToImageRegion() const {
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
),
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) { } */
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
)
253 mRestriction(aRestriction
),
254 mExtendMode(aExtendMode
),
255 mIsRestricted(true) {}
257 mozilla::gfx::IntRect mRect
;
258 mozilla::gfx::IntRect mRestriction
;
259 ExtendMode mExtendMode
;
264 } // namespace mozilla
266 #endif // mozilla_image_ImageRegion_h