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()
27 typedef mozilla::gfx::ExtendMode ExtendMode
;
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
);
69 bool RestrictionContains(const gfxRect
& aRect
) const
74 return mRestriction
.Contains(aRect
);
77 ImageRegion
Intersect(const gfxRect
& aRect
) const
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
);
90 intersection
= mRestriction
.Intersect(intersection
);
95 void MoveBy(gfxFloat dx
, gfxFloat dy
)
99 mRestriction
.MoveBy(dx
, dy
);
103 void Scale(gfxFloat sx
, gfxFloat sy
)
107 mRestriction
.Scale(sx
, sy
);
111 void TransformBy(const gfxMatrix
& aMatrix
)
113 mRect
= aMatrix
.TransformRect(mRect
);
115 mRestriction
= aMatrix
.TransformRect(mRestriction
);
119 void TransformBoundsBy(const gfxMatrix
& aMatrix
)
121 mRect
= aMatrix
.TransformBounds(mRect
);
123 mRestriction
= aMatrix
.TransformBounds(mRestriction
);
127 ImageRegion
operator-(const gfxPoint
& aPt
) const
130 return CreateWithSamplingRestriction(mRect
- aPt
, mRestriction
- aPt
);
132 return Create(mRect
- aPt
);
135 ImageRegion
operator+(const gfxPoint
& aPt
) const
138 return CreateWithSamplingRestriction(mRect
+ aPt
, mRestriction
+ aPt
);
140 return Create(mRect
+ aPt
);
143 gfx::ExtendMode
GetExtendMode() const
148 /* ImageRegion() : mIsRestricted(false) { } */
151 explicit ImageRegion(const gfxRect
& aRect
, ExtendMode aExtendMode
)
153 , mExtendMode(aExtendMode
)
154 , mIsRestricted(false)
157 ImageRegion(const gfxRect
& aRect
, const gfxRect
& aRestriction
, ExtendMode aExtendMode
)
159 , mRestriction(aRestriction
)
160 , mExtendMode(aExtendMode
)
161 , mIsRestricted(true)
165 gfxRect mRestriction
;
166 ExtendMode mExtendMode
;
171 } // namespace mozilla
173 #endif // mozilla_image_ImageRegion_h