This repository has been archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PixelSetReplacerEffect.cs
305 lines (286 loc) · 10.5 KB
/
PixelSetReplacerEffect.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using PaintDotNet;
using PaintDotNet.Effects;
using ImageDifference.Gui;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
namespace ImageDifference
{
/// <summary>
/// Contains assembly information, accessible through variables.
/// </summary>
public class PluginSupportInfo : IPluginSupportInfo
{
#region Properties
/// <summary>
/// Gets the author.
/// </summary>
public string Author
{
get
{
return ((AssemblyCompanyAttribute)base.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false)[0]).Company;
}
}
/// <summary>
/// Gets the copyright information.
/// </summary>
public string Copyright
{
get
{
return ((AssemblyCopyrightAttribute)base.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0]).Copyright;
}
}
/// <summary>
/// Gets the name of the product.
/// </summary>
public string DisplayName
{
get
{
return ((AssemblyProductAttribute)base.GetType().Assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0]).Product;
}
}
/// <summary>
/// Gets the version number.
/// </summary>
public Version Version
{
get
{
return base.GetType().Assembly.GetName().Version;
}
}
/// <summary>
/// Gets the URL where the plugin is released to the public.
/// </summary>
public Uri WebsiteUri
{
get
{
return new Uri("http://forums.getpaint.net/index.php?/forum/7-plugins-publishing-only/");
}
}
#endregion
}
/// <summary>
/// Controls the effect. In short, a GUI is instantiated by
/// CreateConfigDialog and when the dialog signals OK, Render is called,
/// passing OnSetRenderInfo to it. The dialog stores its result in an
/// intermediate class called RenderSettings, which is then accessed to
/// draw the final result in Render.
/// </summary>
[PluginSupportInfo(typeof(PluginSupportInfo), DisplayName = "Image Difference")]
public class EffectPlugin : Effect
{
#region Fields
/// <summary>
/// The dialog to be constructed.
/// </summary>
private winImageDifference dlg;
/// <summary>
/// Paint.net wants to call Render() a million times. This prevents it.
/// </summary>
private static bool renderReady = false;
#endregion
#region Properties
/// <summary>
/// The icon of the plugin to be displayed next to its menu entry.
/// </summary>
public static Bitmap StaticImage
{
get
{
return new Bitmap(Assembly.GetExecutingAssembly()
.GetManifestResourceStream("ImageDifference.EffectPluginIcon.png"));
}
}
/// <summary>
/// The name of the plugin as it appears in Paint.NET.
/// </summary>
public static string StaticName
{
get
{
return Globalization.GlobalStrings.Title;
}
}
/// <summary>
/// The name of the menu category the plugin appears under.
/// </summary>
public static string StaticSubMenuName
{
get
{
return "Tools";
}
}
#endregion
#region Constructors
/// <summary>
/// Constructor.
/// </summary>
public EffectPlugin()
: base(EffectPlugin.StaticName,
EffectPlugin.StaticImage,
EffectPlugin.StaticSubMenuName,
EffectFlags.Configurable)
{
dlg = null;
}
#endregion
#region Methods
/// <summary>
/// Tells Paint.NET which form to instantiate as the plugin's GUI.
/// Called remotely by Paint.NET.
/// </summary>
public override EffectConfigDialog CreateConfigDialog()
{
dlg = new winImageDifference();
StaticSettings.dialogResult = StaticSettings.DialogResult.Default;
renderReady = false;
return dlg;
}
/// <summary>
/// Gets the render information.
/// </summary>
/// <param name="parameters">
/// Saved settings used to restore the GUI to the same settings it was
/// saved with last time the effect was applied.
/// </param>
/// <param name="dstArgs">The destination canvas.</param>
/// <param name="srcArgs">The source canvas.</param>
protected override void OnSetRenderInfo(
EffectConfigToken parameters,
RenderArgs dstArgs,
RenderArgs srcArgs)
{
base.OnSetRenderInfo(parameters, dstArgs, srcArgs);
}
/// <summary>
/// Renders the effect over rectangular regions automatically
/// determined and handled by Paint.NET for multithreading support.
/// </summary>
/// <param name="parameters">
/// Saved settings used to restore the GUI to the same settings it was
/// saved with last time the effect was applied.
/// </param>
/// <param name="dstArgs">The destination canvas.</param>
/// <param name="srcArgs">The source canvas.</param>
/// <param name="rois">
/// A list of rectangular regions to split this effect into so it can
/// be optimized by worker threads. Determined and managed by
/// Paint.NET.
/// </param>
/// <param name="startIndex">
/// The rectangle to begin rendering with. Used in Paint.NET's effect
/// multithreading process.
/// </param>
/// <param name="length">
/// The number of rectangles to render at once. Used in Paint.NET's
/// effect multithreading process.
/// </param>
public override void Render(
EffectConfigToken parameters,
RenderArgs dstArgs,
RenderArgs srcArgs,
Rectangle[] rois,
int startIndex,
int length)
{
//Renders the effect if the dialog is closed and accepted.
if (!IsCancelRequested && !renderReady)
{
var src = srcArgs.Surface;
var dst = dstArgs.Surface;
PersistentSettings token = (PersistentSettings)dlg.EffectToken;
Rectangle selection = EnvironmentParameters.GetSelection(srcArgs.Bounds).GetBoundsInt();
//Something happened, so this must be the final render.
if (StaticSettings.dialogResult != StaticSettings.DialogResult.Default)
{
renderReady = true;
}
if (StaticSettings.dialogResult ==
StaticSettings.DialogResult.StoringImage)
{
if (selection.Width < 1 || selection.Height < 1)
{
MessageBox.Show("The size of the current selection " +
"must match the size of the stored image to work.");
return;
}
//Copies the data into the token.
token.StoredImg = new Bitmap(selection.Width, selection.Height);
for (int i = 0; i < selection.Width; i++)
{
for (int j = 0; j < selection.Height; j++)
{
token.StoredImg.SetPixel(i, j, src[selection.X + i, selection.Y + j]);
}
}
}
else if (StaticSettings.dialogResult ==
StaticSettings.DialogResult.Replacing)
{
if (token.StoredImg.Width != selection.Width ||
token.StoredImg.Height != selection.Height)
{
MessageBox.Show("The unedited image needs to be " +
"stored, and the edited version selected " +
"(with the same size).");
return;
}
for (int y = 0; y < src.Height; y++)
{
for (int x = 0; x < src.Width; x++)
{
Color pix = token.StoredImg.GetPixel(x, y);
if (!src[x, y].Equals(ColorBgra.FromColor(pix)))
{
if (!token.DoInvertArea)
{
if (token.DoInvertPicture)
{
dst[x, y] = pix;
}
else
{
dst[x, y] = src[x, y];
}
}
else
{
dst[x, y] = ColorBgra.Transparent;
}
}
else
{
if (!token.DoInvertArea)
{
dst[x, y] = ColorBgra.Transparent;
}
else
{
if (token.DoInvertPicture)
{
dst[x, y] = pix;
}
else
{
dst[x, y] = src[x, y];
}
}
}
}
}
}
}
StaticSettings.dialogResult = StaticSettings.DialogResult.Default;
}
#endregion
}
}