2 * Copyright (c) 2001-2015, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
6 * Axel Dörfler, axeld@pinc-software.de
7 * Stephan Aßmus <superstippi@gmx.de>
8 * Adrien Destugues <pulkomandy@pulkomandy.tk>
9 * Julian Harnath <julian.harnath@rwth-aachen.de>
12 #ifndef SIMPLE_TRANSFORM_H
13 #define SIMPLE_TRANSFORM_H
15 #include <GradientLinear.h>
16 #include <GradientRadial.h>
17 #include <GradientRadialFocus.h>
18 #include <GradientDiamond.h>
19 #include <GradientConic.h>
27 class SimpleTransform
{
35 void AddOffset(float x
, float y
)
41 void SetScale(float scale
)
46 void Apply(BPoint
* point
) const
48 _Apply(point
->x
, point
->y
);
51 void Apply(IntPoint
* point
) const
53 _Apply(point
->x
, point
->y
);
56 void Apply(BRect
* rect
) const
59 rect
->OffsetBy(fOffset
.x
, fOffset
.y
);
61 _Apply(rect
->left
, rect
->top
);
62 _Apply(rect
->right
, rect
->bottom
);
66 void Apply(IntRect
* rect
) const
69 rect
->OffsetBy(fOffset
.x
, fOffset
.y
);
71 _Apply(rect
->left
, rect
->top
);
72 _Apply(rect
->right
, rect
->bottom
);
76 void Apply(BRegion
* region
) const
79 region
->OffsetBy(fOffset
.x
, fOffset
.y
);
81 // TODO: optimize some more
83 int32 count
= region
->CountRects();
84 for (int32 i
= 0; i
< count
; i
++) {
85 BRect r
= region
->RectAt(i
);
86 BPoint
lt(r
.LeftTop());
87 BPoint
rb(r
.RightBottom());
88 // offset to bottom right corner of pixel before transformation
91 // apply transformation
94 // reset bottom right to pixel "index"
97 // add rect to converted region
98 // NOTE/TODO: the rect would not have to go
99 // through the whole intersection test process,
100 // it is guaranteed not to overlap with any rect
101 // already contained in the region
102 converted
.Include(BRect(lt
, rb
));
108 void Apply(BGradient
* gradient
) const
110 switch (gradient
->GetType()) {
111 case BGradient::TYPE_LINEAR
:
113 BGradientLinear
* linear
= (BGradientLinear
*) gradient
;
114 BPoint start
= linear
->Start();
115 BPoint end
= linear
->End();
118 linear
->SetStart(start
);
123 case BGradient::TYPE_RADIAL
:
125 BGradientRadial
* radial
= (BGradientRadial
*) gradient
;
126 BPoint center
= radial
->Center();
128 radial
->SetCenter(center
);
132 case BGradient::TYPE_RADIAL_FOCUS
:
134 BGradientRadialFocus
* radialFocus
=
135 (BGradientRadialFocus
*)gradient
;
136 BPoint center
= radialFocus
->Center();
137 BPoint focal
= radialFocus
->Focal();
140 radialFocus
->SetCenter(center
);
141 radialFocus
->SetFocal(focal
);
145 case BGradient::TYPE_DIAMOND
:
147 BGradientDiamond
* diamond
= (BGradientDiamond
*) gradient
;
148 BPoint center
= diamond
->Center();
150 diamond
->SetCenter(center
);
154 case BGradient::TYPE_CONIC
:
156 BGradientConic
* conic
= (BGradientConic
*) gradient
;
157 BPoint center
= conic
->Center();
159 conic
->SetCenter(center
);
163 case BGradient::TYPE_NONE
:
169 // Make sure the gradient is fully padded so that out of bounds access
170 // get the correct colors
171 gradient
->SortColorStopsByOffset();
173 BGradient::ColorStop
* end
= gradient
->ColorStopAtFast(
174 gradient
->CountColorStops() - 1);
176 if (end
->offset
!= 255)
177 gradient
->AddColor(end
->color
, 255);
179 BGradient::ColorStop
* start
= gradient
->ColorStopAtFast(0);
181 if (start
->offset
!= 0)
182 gradient
->AddColor(start
->color
, 0);
184 gradient
->SortColorStopsByOffset();
187 void Apply(BPoint
* destination
, const BPoint
* source
, int32 count
) const
189 // TODO: optimize this, it should be smarter
191 *destination
= *source
;
198 void Apply(BRect
* destination
, const BRect
* source
, int32 count
) const
200 // TODO: optimize this, it should be smarter
202 *destination
= *source
;
209 void Apply(BRegion
* destination
, const BRegion
* source
, int32 count
) const
211 // TODO: optimize this, it should be smarter
213 *destination
= *source
;
221 void _Apply(int32
& x
, int32
& y
) const
225 x
+= (int32
)fOffset
.x
;
226 y
+= (int32
)fOffset
.y
;
229 void _Apply(float& x
, float& y
) const
243 #endif // SIMPLE_TRANSFORM_H