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 } // namespace mozilla
156 #endif // mozilla_image_ImageRegion_h