-
Notifications
You must be signed in to change notification settings - Fork 265
/
EmbossEffect.cs
147 lines (112 loc) · 3.7 KB
/
EmbossEffect.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
/////////////////////////////////////////////////////////////////////////////////
// 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 EmbossEffect : BaseEffect
{
public override string Icon {
get { return "Menu.Effects.Stylize.Emboss.png"; }
}
public override string Text {
get { return Catalog.GetString ("Emboss"); }
}
public override bool IsConfigurable {
get { return true; }
}
public override string EffectMenuCategory {
get { return Catalog.GetString ("Stylize"); }
}
public EmbossData Data {
get { return EffectData as EmbossData; }
}
public EmbossEffect () {
EffectData = new EmbossData ();
}
public override bool LaunchConfiguration () {
return EffectHelper.LaunchSimpleEffectDialog (this);
}
#region Algorithm Code Ported From PDN
unsafe public override void RenderEffect (ImageSurface src, ImageSurface dst, Gdk.Rectangle[] rois) {
double[,] weights = Weights;
var srcWidth = src.Width;
var srcHeight = src.Height;
ColorBgra* src_data_ptr = (ColorBgra*)src.DataPtr;
foreach (var rect in rois) {
// loop through each line of target rectangle
for (int y = rect.Top; y < rect.Bottom; ++y) {
int fyStart = 0;
int fyEnd = 3;
if (y == 0)
fyStart = 1;
if (y == srcHeight - 1)
fyEnd = 2;
// loop through each point in the line
ColorBgra* dstPtr = dst.GetPointAddress (rect.Left, y);
for (int x = rect.Left; x < rect.Right; ++x) {
int fxStart = 0;
int fxEnd = 3;
if (x == 0)
fxStart = 1;
if (x == srcWidth - 1)
fxEnd = 2;
// loop through each weight
double sum = 0.0;
for (int fy = fyStart; fy < fyEnd; ++fy) {
for (int fx = fxStart; fx < fxEnd; ++fx) {
double weight = weights[fy, fx];
ColorBgra c = src.GetPointUnchecked (src_data_ptr, srcWidth, x - 1 + fx, y - 1 + fy);
double intensity = (double)c.GetIntensityByte ();
sum += weight * intensity;
}
}
int iSum = (int)sum;
iSum += 128;
if (iSum > 255)
iSum = 255;
if (iSum < 0)
iSum = 0;
*dstPtr = ColorBgra.FromBgra ((byte)iSum, (byte)iSum, (byte)iSum, 255);
++dstPtr;
}
}
}
}
public double[,] Weights {
get {
// adjust and convert angle to radians
double r = (double)Data.Angle * 2.0 * Math.PI / 360.0;
// angle delta for each weight
double dr = Math.PI / 4.0;
// for r = 0 this builds an emboss filter pointing straight left
double[,] weights = new double[3, 3];
weights[0, 0] = Math.Cos (r + dr);
weights[0, 1] = Math.Cos (r + 2.0 * dr);
weights[0, 2] = Math.Cos (r + 3.0 * dr);
weights[1, 0] = Math.Cos (r);
weights[1, 1] = 0;
weights[1, 2] = Math.Cos (r + 4.0 * dr);
weights[2, 0] = Math.Cos (r - dr);
weights[2, 1] = Math.Cos (r - 2.0 * dr);
weights[2, 2] = Math.Cos (r - 3.0 * dr);
return weights;
}
}
#endregion
public class EmbossData : EffectData
{
public double Angle = 0;
}
}
}