/
AddNoiseEffect.cs
183 lines (147 loc) · 4.7 KB
/
AddNoiseEffect.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/////////////////////////////////////////////////////////////////////////////////
// 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: Hanh Pham <hanh.pham@gmx.com> //
/////////////////////////////////////////////////////////////////////////////////
using System;
using Cairo;
using Pinta.Gui.Widgets;
using Pinta.Core;
using Mono.Unix;
namespace Pinta.Effects
{
[Mono.Addins.Extension]
public class AddNoiseEffect : BaseEffect
{
private int intensity;
private int colorSaturation;
private double coverage;
public override string Icon {
get { return "Menu.Effects.Noise.AddNoise.png"; }
}
public override string Text {
get { return Catalog.GetString ("Add Noise"); }
}
public override bool IsConfigurable {
get { return true; }
}
public override string EffectMenuCategory {
get { return Catalog.GetString ("Noise"); }
}
public NoiseData Data { get { return EffectData as NoiseData; } }
static AddNoiseEffect ()
{
InitLookup ();
}
public AddNoiseEffect ()
{
EffectData = new NoiseData ();
}
public override bool LaunchConfiguration ()
{
return EffectHelper.LaunchSimpleEffectDialog (this);
}
#region Algorithm Code Ported From PDN
[ThreadStatic]
private static Random threadRand = new Random ();
private const int tableSize = 16384;
private static int[] lookup;
private static double NormalCurve (double x, double scale)
{
return scale * Math.Exp (-x * x / 2);
}
private static void InitLookup ()
{
int[] curve = new int[tableSize];
int[] integral = new int[tableSize];
double l = 5;
double r = 10;
double scale = 50;
double sum = 0;
while (r - l > 0.0000001) {
sum = 0;
scale = (l + r) * 0.5;
for (int i = 0; i < tableSize; ++i) {
sum += NormalCurve (16.0 * ((double)i - tableSize / 2) / tableSize, scale);
if (sum > 1000000) {
break;
}
}
if (sum > tableSize) {
r = scale;
} else if (sum < tableSize) {
l = scale;
} else {
break;
}
}
lookup = new int[tableSize];
sum = 0;
int roundedSum = 0, lastRoundedSum;
for (int i = 0; i < tableSize; ++i) {
sum += NormalCurve (16.0 * ((double)i - tableSize / 2) / tableSize, scale);
lastRoundedSum = roundedSum;
roundedSum = (int)sum;
for (int j = lastRoundedSum; j < roundedSum; ++j) {
lookup[j] = (i - tableSize / 2) * 65536 / tableSize;
}
}
}
public unsafe override void RenderEffect (ImageSurface src, ImageSurface dst, Gdk.Rectangle[] rois)
{
this.intensity = Data.Intensity;
this.colorSaturation = Data.ColorSaturation;
this.coverage = 0.01 * Data.Coverage;
int dev = this.intensity * this.intensity / 4;
int sat = this.colorSaturation * 4096 / 100;
if (threadRand == null) {
threadRand = new Random (unchecked (System.Threading.Thread.CurrentThread.GetHashCode () ^
unchecked ((int)DateTime.Now.Ticks)));
}
Random localRand = threadRand;
int[] localLookup = lookup;
foreach (Gdk.Rectangle rect in rois) {
for (int y = rect.Top; y < rect.Bottom; ++y) {
ColorBgra* srcPtr = src.GetPointAddressUnchecked (rect.Left, y);
ColorBgra* dstPtr = dst.GetPointAddressUnchecked (rect.Left, y);
for (int x = 0; x < rect.Width; ++x) {
if (localRand.NextDouble () > this.coverage) {
*dstPtr = *srcPtr;
} else {
int r;
int g;
int b;
int i;
r = localLookup[localRand.Next (tableSize)];
g = localLookup[localRand.Next (tableSize)];
b = localLookup[localRand.Next (tableSize)];
i = (4899 * r + 9618 * g + 1867 * b) >> 14;
r = i + (((r - i) * sat) >> 12);
g = i + (((g - i) * sat) >> 12);
b = i + (((b - i) * sat) >> 12);
dstPtr->R = Utility.ClampToByte (srcPtr->R + ((r * dev + 32768) >> 16));
dstPtr->G = Utility.ClampToByte (srcPtr->G + ((g * dev + 32768) >> 16));
dstPtr->B = Utility.ClampToByte (srcPtr->B + ((b * dev + 32768) >> 16));
dstPtr->A = srcPtr->A;
}
++srcPtr;
++dstPtr;
}
}
}
}
#endregion
public class NoiseData : EffectData
{
[MinimumValue (0), MaximumValue (100)]
public int Intensity = 64;
[MinimumValue (0), MaximumValue (400)]
public int ColorSaturation = 100;
[MinimumValue (0), DigitsValue (2), MaximumValue (100)]
public double Coverage = 100.0;
}
}
}