-
Notifications
You must be signed in to change notification settings - Fork 265
/
TwistEffect.cs
138 lines (111 loc) · 3.72 KB
/
TwistEffect.cs
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
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET //
// Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
// See license-pdn.txt for full licensing and attribution details. //
// //
// Ported to Pinta by: Marco Rolappe <m_rolappe@gmx.net> //
/////////////////////////////////////////////////////////////////////////////////
using System;
using Cairo;
using Pinta.Gui.Widgets;
using Pinta.Core;
using Mono.Unix;
namespace Pinta.Effects
{
//[System.ComponentModel.Composition.Export (typeof (BaseEffect))]
public class TwistEffect : BaseEffect
{
public override string Icon {
get { return "Menu.Effects.Distort.Twist.png"; }
}
public override string Text {
get { return Catalog.GetString ("Twist"); }
}
public override bool IsConfigurable {
get { return true; }
}
public override string EffectMenuCategory {
get { return Catalog.GetString ("Distort"); }
}
public TwistData Data { get { return EffectData as TwistData; } }
public TwistEffect ()
{
EffectData = new TwistData ();
}
public override bool LaunchConfiguration ()
{
return EffectHelper.LaunchSimpleEffectDialog (this);
}
#region Algorithm Code Ported From PDN
public unsafe override void RenderEffect (ImageSurface src, ImageSurface dst, Gdk.Rectangle[] rois)
{
float twist = Data.Amount;
float hw = dst.Width / 2.0f;
float hh = dst.Height / 2.0f;
float maxrad = Math.Min (hw, hh);
twist = twist * twist * Math.Sign (twist);
int aaLevel = Data.Antialias;
int aaSamples = aaLevel * aaLevel + 1;
PointD* aaPoints = stackalloc PointD[aaSamples];
for (int i = 0; i < aaSamples; ++i) {
PointD pt = new PointD (
((i * aaLevel) / (float)aaSamples),
i / (float)aaSamples);
pt.X -= (int)pt.X;
aaPoints[i] = pt;
}
int src_width = src.Width;
ColorBgra* src_data_ptr = (ColorBgra*)src.DataPtr;
foreach (var rect in rois) {
for (int y = rect.Top; y < rect.Bottom; y++) {
float j = y - hh;
ColorBgra* dstPtr = dst.GetPointAddressUnchecked (rect.Left, y);
ColorBgra* srcPtr = src.GetPointAddressUnchecked (src_data_ptr, src_width, rect.Left, y);
for (int x = rect.Left; x < rect.Right; x++) {
float i = x - hw;
if (i * i + j * j > (maxrad + 1) * (maxrad + 1)) {
*dstPtr = *srcPtr;
} else {
int b = 0;
int g = 0;
int r = 0;
int a = 0;
for (int p = 0; p < aaSamples; ++p) {
float u = i + (float)aaPoints[p].X;
float v = j + (float)aaPoints[p].Y;
double rad = Math.Sqrt (u * u + v * v);
double theta = Math.Atan2 (v, u);
double t = 1 - rad / maxrad;
t = (t < 0) ? 0 : (t * t * t);
theta += (t * twist) / 100;
ColorBgra sample = src.GetPointUnchecked (src_data_ptr, src_width,
(int)(hw + (float)(rad * Math.Cos (theta))),
(int)(hh + (float)(rad * Math.Sin (theta))));
b += sample.B;
g += sample.G;
r += sample.R;
a += sample.A;
}
*dstPtr = ColorBgra.FromBgra (
(byte)(b / aaSamples),
(byte)(g / aaSamples),
(byte)(r / aaSamples),
(byte)(a / aaSamples));
}
++dstPtr;
++srcPtr;
}
}
}
}
#endregion
public class TwistData : EffectData
{
[MinimumValue (-100), MaximumValue (100)]
public int Amount = 45;
[MinimumValue (0), MaximumValue (5)]
public int Antialias = 2;
}
}
}