Line data Source code
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/. */
5 :
6 : #ifndef mozilla_image_ImageRegion_h
7 : #define mozilla_image_ImageRegion_h
8 :
9 : #include "gfxRect.h"
10 : #include "mozilla/gfx/Types.h"
11 :
12 : namespace mozilla {
13 : namespace image {
14 :
15 : /**
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
19 : * within that rect.
20 : *
21 : * The sampling restriction rect exists primarily for callers which perform
22 : * pixel snapping. Other callers should generally use one of the Create()
23 : * overloads.
24 : */
25 : class ImageRegion
26 : {
27 : typedef mozilla::gfx::ExtendMode ExtendMode;
28 :
29 : public:
30 0 : static ImageRegion Empty()
31 : {
32 0 : return ImageRegion(gfxRect(), ExtendMode::CLAMP);
33 : }
34 :
35 18 : static ImageRegion Create(const gfxRect& aRect,
36 : ExtendMode aExtendMode = ExtendMode::CLAMP)
37 : {
38 18 : return ImageRegion(aRect, aExtendMode);
39 : }
40 :
41 : static ImageRegion Create(const gfxSize& aSize,
42 : ExtendMode aExtendMode = ExtendMode::CLAMP)
43 : {
44 : return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
45 : }
46 :
47 0 : static ImageRegion Create(const nsIntSize& aSize,
48 : ExtendMode aExtendMode = ExtendMode::CLAMP)
49 : {
50 0 : return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
51 : }
52 :
53 116 : static ImageRegion CreateWithSamplingRestriction(const gfxRect& aRect,
54 : const gfxRect& aRestriction,
55 : ExtendMode aExtendMode = ExtendMode::CLAMP)
56 : {
57 116 : return ImageRegion(aRect, aRestriction, aExtendMode);
58 : }
59 :
60 35 : bool IsRestricted() const { return mIsRestricted; }
61 373 : const gfxRect& Rect() const { return mRect; }
62 :
63 35 : const gfxRect& Restriction() const
64 : {
65 35 : MOZ_ASSERT(mIsRestricted);
66 35 : return mRestriction;
67 : }
68 :
69 0 : bool RestrictionContains(const gfxRect& aRect) const
70 : {
71 0 : if (!mIsRestricted) {
72 0 : return true;
73 : }
74 0 : return mRestriction.Contains(aRect);
75 : }
76 :
77 8 : ImageRegion Intersect(const gfxRect& aRect) const
78 : {
79 8 : if (mIsRestricted) {
80 16 : return CreateWithSamplingRestriction(aRect.Intersect(mRect),
81 24 : aRect.Intersect(mRestriction));
82 : }
83 0 : return Create(aRect.Intersect(mRect));
84 : }
85 :
86 0 : gfxRect IntersectAndRestrict(const gfxRect& aRect) const
87 : {
88 0 : gfxRect intersection = mRect.Intersect(aRect);
89 0 : if (mIsRestricted) {
90 0 : intersection = mRestriction.Intersect(intersection);
91 : }
92 0 : return intersection;
93 : }
94 :
95 8 : void MoveBy(gfxFloat dx, gfxFloat dy)
96 : {
97 8 : mRect.MoveBy(dx, dy);
98 8 : if (mIsRestricted) {
99 8 : mRestriction.MoveBy(dx, dy);
100 : }
101 8 : }
102 :
103 0 : void Scale(gfxFloat sx, gfxFloat sy)
104 : {
105 0 : mRect.Scale(sx, sy);
106 0 : if (mIsRestricted) {
107 0 : mRestriction.Scale(sx, sy);
108 : }
109 0 : }
110 :
111 : void TransformBy(const gfxMatrix& aMatrix)
112 : {
113 : mRect = aMatrix.TransformRect(mRect);
114 : if (mIsRestricted) {
115 : mRestriction = aMatrix.TransformRect(mRestriction);
116 : }
117 : }
118 :
119 0 : void TransformBoundsBy(const gfxMatrix& aMatrix)
120 : {
121 0 : mRect = aMatrix.TransformBounds(mRect);
122 0 : if (mIsRestricted) {
123 0 : mRestriction = aMatrix.TransformBounds(mRestriction);
124 : }
125 0 : }
126 :
127 : ImageRegion operator-(const gfxPoint& aPt) const
128 : {
129 : if (mIsRestricted) {
130 : return CreateWithSamplingRestriction(mRect - aPt, mRestriction - aPt);
131 : }
132 : return Create(mRect - aPt);
133 : }
134 :
135 : ImageRegion operator+(const gfxPoint& aPt) const
136 : {
137 : if (mIsRestricted) {
138 : return CreateWithSamplingRestriction(mRect + aPt, mRestriction + aPt);
139 : }
140 : return Create(mRect + aPt);
141 : }
142 :
143 126 : gfx::ExtendMode GetExtendMode() const
144 : {
145 126 : return mExtendMode;
146 : }
147 :
148 : /* ImageRegion() : mIsRestricted(false) { } */
149 :
150 : private:
151 18 : explicit ImageRegion(const gfxRect& aRect, ExtendMode aExtendMode)
152 18 : : mRect(aRect)
153 : , mExtendMode(aExtendMode)
154 18 : , mIsRestricted(false)
155 18 : { }
156 :
157 116 : ImageRegion(const gfxRect& aRect, const gfxRect& aRestriction, ExtendMode aExtendMode)
158 116 : : mRect(aRect)
159 : , mRestriction(aRestriction)
160 : , mExtendMode(aExtendMode)
161 116 : , mIsRestricted(true)
162 116 : { }
163 :
164 : gfxRect mRect;
165 : gfxRect mRestriction;
166 : ExtendMode mExtendMode;
167 : bool mIsRestricted;
168 : };
169 :
170 : } // namespace image
171 : } // namespace mozilla
172 :
173 : #endif // mozilla_image_ImageRegion_h
|