/
UnfocusEffect.cs
105 lines (85 loc) · 2.73 KB
/
UnfocusEffect.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
/////////////////////////////////////////////////////////////////////////////////
// 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
{
public class UnfocusEffect : LocalHistogramEffect
{
private int radius;
public override string Icon {
get { return "Menu.Effects.Blurs.Unfocus.png"; }
}
public override string Name {
get { return Catalog.GetString ("Unfocus"); }
}
public override bool IsConfigurable {
get { return true; }
}
public override string EffectMenuCategory {
get { return Catalog.GetString ("Blurs"); }
}
public UnfocusData Data { get { return EffectData as UnfocusData; } }
public UnfocusEffect ()
{
EffectData = new UnfocusData ();
}
public override bool LaunchConfiguration ()
{
return EffectHelper.LaunchSimpleEffectDialog (this);
}
#region Algorithm Code Ported From PDN
public unsafe override void Render (ImageSurface src, ImageSurface dest, Gdk.Rectangle[] rois)
{
this.radius = Data.Radius;
foreach (Gdk.Rectangle rect in rois)
RenderRectWithAlpha (this.radius, src, dest, rect);
}
public unsafe override ColorBgra ApplyWithAlpha (ColorBgra src, int area, int sum, int* hb, int* hg, int* hr)
{
//each slot of the histgram can contain up to area * 255. This will overflow an int when area > 32k
if (area < 32768) {
int b = 0;
int g = 0;
int r = 0;
for (int i = 1; i < 256; ++i) {
b += i * hb[i];
g += i * hg[i];
r += i * hr[i];
}
int alpha = sum / area;
int div = area * 255;
return ColorBgra.FromBgraClamped (b / div, g / div, r / div, alpha);
} else { //use a long if an int will overflow.
long b = 0;
long g = 0;
long r = 0;
for (long i = 1; i < 256; ++i) {
b += i * hb[i];
g += i * hg[i];
r += i * hr[i];
}
int alpha = sum / area;
int div = area * 255;
return ColorBgra.FromBgraClamped (b / div, g / div, r / div, alpha);
}
}
#endregion
public class UnfocusData : EffectData
{
[MinimumValue (1), MaximumValue (200)]
public int Radius = 4;
[Skip]
public override bool IsDefault { get { return Radius == 0; } }
}
}
}