-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathO2Paint_axialGradient.m
169 lines (141 loc) · 5.68 KB
/
O2Paint_axialGradient.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*------------------------------------------------------------------------
*
* Derivative of the OpenVG 1.0.1 Reference Implementation
* -------------------------------------
*
* Copyright (c) 2007 The Khronos Group Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and /or associated documentation files
* (the "Materials "), to deal in the Materials without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Materials,
* and to permit persons to whom the Materials are furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Materials.
*
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
* THE USE OR OTHER DEALINGS IN THE MATERIALS.
*
*-------------------------------------------------------------------*/
#import <Onyx2D/O2Paint_axialGradient.h>
#import <Onyx2D/O2Shading.h>
@implementation O2Paint_axialGradient
// O2PaintColorRamp is expensive, this could be more accurate
// Interestingly, using this produces stair step artifacts at the same angles that Quartz2D produces artifacts
// The artifacts are different but shaped the same
ONYX2D_STATIC_INLINE O2argb8u O2PaintFastAxialRamp(O2Paint_ramp *self,O2Float gradient, O2Float rho,int *skip) {
if(gradient<=0.0)
return self->_colorStops[0].color8u;
else if(gradient>=1.0)
return self->_colorStops[self->_numberOfColorStops-1].color8u;
else {
int i=RI_FLOOR_TO_INT(gradient*(self->_numberOfColorStops-1));
return self->_colorStops[i].color8u;
}
}
ONYX2D_STATIC int linear_span_largb8u_PRE(O2Paint *selfX,int x,int y,O2argb8u *span,int length){
O2Paint_axialGradient *self=(O2Paint_axialGradient *)selfX;
int i;
int previous=-1;
O2Point point=O2PointMake(x+0.5f, y+0.5f);
O2Size delta=O2SizeMake(1.0f,0.0f);
point=O2PointApplyAffineTransform(point,self->m_surfaceToPaintMatrix);
point=Vector2Subtract(point,self->_startPoint);
delta=O2SizeApplyAffineTransform(delta,self->m_surfaceToPaintMatrix);
if(self->_rho==0.0f){ //points are equal, gradient is always 1.0f
for(i=0;i<length;i++,x++,point.x+=delta.width){
int skip=0;
O2argb8u value=O2PaintFastAxialRamp(self,1.0f, self->_rho,&skip);
if(skip!=previous){
if(previous==-1)
previous=skip;
else
return (previous==1)?-i:i;
}
span[i]=value;
}
}
else {
O2Float gx=point.x*self->_u.x*self->_oou;
O2Float dx=delta.width*self->_u.x*self->_oou;
O2Float gy=point.y*self->_u.y*self->_oou;
for(i=0;i<length;i++,x++,gx+=dx){
O2Float g=gx+gy;
int skip=0;
O2argb8u value=O2PaintFastAxialRamp(self,g, self->_rho,&skip);
if(skip!=previous){
if(previous==-1)
previous=skip;
else
return (previous==1)?-i:i;
}
span[i]=value;
}
}
return (previous==1)?-length:length;
}
ONYX2D_STATIC int linear_span_largb32f_PRE(O2Paint *selfX,int x,int y,O2argb32f *span,int length){
O2Paint_axialGradient *self=(O2Paint_axialGradient *)selfX;
int i;
int previous=-1;
O2Point point=O2PointMake(x+0.5f, y+0.5f);
O2Size delta=O2SizeMake(1.0f,0.0f);
point=O2PointApplyAffineTransform(point,self->m_surfaceToPaintMatrix);
point=Vector2Subtract(point,self->_startPoint);
delta=O2SizeApplyAffineTransform(delta,self->m_surfaceToPaintMatrix);
for(i=0;i<length;i++,x++,point.x+=delta.width){
O2Float g;
if(self->_rho==0.0f) //points are equal, gradient is always 1.0f
g=1.0f;
else {
O2Point p=point;
g=Vector2Dot(p, self->_u)*self->_oou;
}
int skip=0;
O2argb32f value=O2PaintColorRamp(self,g, self->_rho,&skip);
if(skip!=previous){
if(previous==-1)
previous=skip;
else
return (previous==1)?-i:i;
}
span[i]=value;
}
return (previous==1)?-length:length;
}
-initWithShading:(O2Shading *)shading deviceTransform:(O2AffineTransform)deviceTransform {
// Calculate the number of samples based on the length of the gradient in device space
O2Point startPoint=[shading startPoint];
O2Point endPoint=[shading endPoint];
O2Point deviceStart=O2PointApplyAffineTransform(startPoint,deviceTransform);
O2Point deviceEnd=O2PointApplyAffineTransform(endPoint,deviceTransform);
O2Float deltax=deviceStart.x-deviceEnd.x;
O2Float deltay=deviceStart.y-deviceEnd.y;
O2Float distance=sqrt(deltax*deltax+deltay*deltay);
int numberOfSamples=RI_INT_CLAMP(distance,2,8192);
[super initWithShading:shading deviceTransform:deviceTransform numberOfSamples:numberOfSamples];
_paint_largb8u_PRE=linear_span_largb8u_PRE;
_paint_largb32f_PRE=linear_span_largb32f_PRE;
_u=Vector2Subtract(_endPoint,_startPoint);
O2Float usq=Vector2Dot(_u,_u);
if(usq<=0.0f){ //points are equal, gradient is always 1.0f
_oou=0.0f;
_rho=0.0f;
}
else {
_oou = 1.0f / usq;
O2Float dgdx = _oou * _u.x * self->m_surfaceToPaintMatrix.a + _oou * _u.y * self->m_surfaceToPaintMatrix.b;
O2Float dgdy = _oou * _u.x * self->m_surfaceToPaintMatrix.c + _oou * _u.y * self->m_surfaceToPaintMatrix.d;
_rho = (O2Float)sqrt(dgdx*dgdx + dgdy*dgdy);
}
return self;
}
@end