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
10 #include "mozilla/gfx/Types.h"
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
21 * The sampling restriction rect exists primarily for callers which perform
22 * pixel snapping. Other callers should generally use one of the Create()
26 typedef mozilla::gfx::ExtendMode ExtendMode
;
29 static ImageRegion
Empty() {
30 return ImageRegion(gfxRect(), ExtendMode::CLAMP
);
33 static ImageRegion
Create(const gfxRect
& aRect
,
34 ExtendMode aExtendMode
= ExtendMode::CLAMP
) {
35 return ImageRegion(aRect
, aExtendMode
);
38 static ImageRegion
Create(const gfxSize
& aSize
,
39 ExtendMode aExtendMode
= ExtendMode::CLAMP
) {
40 return ImageRegion(gfxRect(0, 0, aSize
.width
, aSize
.height
), aExtendMode
);
43 static ImageRegion
Create(const nsIntSize
& aSize
,
44 ExtendMode aExtendMode
= ExtendMode::CLAMP
) {
45 return ImageRegion(gfxRect(0, 0, aSize
.width
, aSize
.height
), aExtendMode
);
48 static ImageRegion
CreateWithSamplingRestriction(
49 const gfxRect
& aRect
, const gfxRect
& aRestriction
,
50 ExtendMode aExtendMode
= ExtendMode::CLAMP
) {
51 return ImageRegion(aRect
, aRestriction
, aExtendMode
);
54 bool IsRestricted() const { return mIsRestricted
; }
55 const gfxRect
& Rect() const { return mRect
; }
57 const gfxRect
& Restriction() const {
58 MOZ_ASSERT(mIsRestricted
);
62 bool RestrictionContains(const gfxRect
& aRect
) const {
66 return mRestriction
.Contains(aRect
);
69 ImageRegion
Intersect(const gfxRect
& aRect
) const {
71 return CreateWithSamplingRestriction(aRect
.Intersect(mRect
),
72 aRect
.Intersect(mRestriction
));
74 return Create(aRect
.Intersect(mRect
));
77 gfxRect
IntersectAndRestrict(const gfxRect
& aRect
) const {
78 gfxRect intersection
= mRect
.Intersect(aRect
);
80 intersection
= mRestriction
.Intersect(intersection
);
85 void MoveBy(gfxFloat dx
, gfxFloat dy
) {
88 mRestriction
.MoveBy(dx
, dy
);
92 void Scale(gfxFloat sx
, gfxFloat sy
) {
95 mRestriction
.Scale(sx
, sy
);
99 void TransformBy(const gfxMatrix
& aMatrix
) {
100 mRect
= aMatrix
.TransformRect(mRect
);
102 mRestriction
= aMatrix
.TransformRect(mRestriction
);
106 void TransformBoundsBy(const gfxMatrix
& aMatrix
) {
107 mRect
= aMatrix
.TransformBounds(mRect
);
109 mRestriction
= aMatrix
.TransformBounds(mRestriction
);
113 ImageRegion
operator-(const gfxPoint
& aPt
) const {
115 return CreateWithSamplingRestriction(mRect
- aPt
, mRestriction
- aPt
);
117 return Create(mRect
- aPt
);
120 ImageRegion
operator+(const gfxPoint
& aPt
) const {
122 return CreateWithSamplingRestriction(mRect
+ aPt
, mRestriction
+ aPt
);
124 return Create(mRect
+ aPt
);
127 gfx::ExtendMode
GetExtendMode() const { return mExtendMode
; }
129 /* ImageRegion() : mIsRestricted(false) { } */
132 explicit ImageRegion(const gfxRect
& aRect
, ExtendMode aExtendMode
)
133 : mRect(aRect
), mExtendMode(aExtendMode
), mIsRestricted(false) {}
135 ImageRegion(const gfxRect
& aRect
, const gfxRect
& aRestriction
,
136 ExtendMode aExtendMode
)
138 mRestriction(aRestriction
),
139 mExtendMode(aExtendMode
),
140 mIsRestricted(true) {}
143 gfxRect mRestriction
;
144 ExtendMode mExtendMode
;
149 } // namespace mozilla
151 #endif // mozilla_image_ImageRegion_h