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"
16 #include "PLDHashTable.h" // for PLDHashNumber
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
27 * The sampling restriction rect exists primarily for callers which perform
28 * pixel snapping. Other callers should generally use one of the Create()
32 typedef mozilla::gfx::ExtendMode ExtendMode
;
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
);
68 bool RestrictionContains(const gfxRect
& aRect
) const {
72 return mRestriction
.Contains(aRect
);
75 ImageRegion
Intersect(const gfxRect
& aRect
) const {
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
);
86 intersection
= mRestriction
.Intersect(intersection
);
91 void MoveBy(gfxFloat dx
, gfxFloat dy
) {
94 mRestriction
.MoveBy(dx
, dy
);
98 void Scale(gfxFloat sx
, gfxFloat sy
) {
101 mRestriction
.Scale(sx
, sy
);
105 void TransformBy(const gfxMatrix
& aMatrix
) {
106 mRect
= aMatrix
.TransformRect(mRect
);
108 mRestriction
= aMatrix
.TransformRect(mRestriction
);
112 void TransformBoundsBy(const gfxMatrix
& aMatrix
) {
113 mRect
= aMatrix
.TransformBounds(mRect
);
115 mRestriction
= aMatrix
.TransformBounds(mRestriction
);
119 ImageRegion
operator-(const gfxPoint
& aPt
) const {
121 return CreateWithSamplingRestriction(mRect
- aPt
, mRestriction
- aPt
);
123 return Create(mRect
- aPt
);
126 ImageRegion
operator+(const gfxPoint
& aPt
) const {
128 return CreateWithSamplingRestriction(mRect
+ aPt
, mRestriction
+ aPt
);
130 return Create(mRect
+ aPt
);
133 gfx::ExtendMode
GetExtendMode() const { return mExtendMode
; }
135 /* ImageRegion() : mIsRestricted(false) { } */
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
)
144 mRestriction(aRestriction
),
145 mExtendMode(aExtendMode
),
146 mIsRestricted(true) {}
149 gfxRect mRestriction
;
150 ExtendMode mExtendMode
;
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
160 * The sampling restriction rect exists primarily for callers which perform
161 * pixel snapping. Other callers should generally use one of the Create()
164 class ImageIntRegion
{
165 typedef mozilla::gfx::ExtendMode ExtendMode
;
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
);
198 bool RestrictionContains(const mozilla::gfx::IntRect
& aRect
) const {
199 if (!mIsRestricted
) {
202 return mRestriction
.Contains(aRect
);
205 ImageIntRegion
Intersect(const mozilla::gfx::IntRect
& aRect
) const {
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
);
217 intersection
= mRestriction
.Intersect(intersection
);
222 gfx::ExtendMode
GetExtendMode() const { return mExtendMode
; }
224 ImageRegion
ToImageRegion() const {
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
),
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) { } */
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
)
260 mRestriction(aRestriction
),
261 mExtendMode(aExtendMode
),
262 mIsRestricted(true) {}
264 mozilla::gfx::IntRect mRect
;
265 mozilla::gfx::IntRect mRestriction
;
266 ExtendMode mExtendMode
;
271 } // namespace mozilla
273 #endif // mozilla_image_ImageRegion_h