-
Notifications
You must be signed in to change notification settings - Fork 7
/
DefaultCode.cs
268 lines (261 loc) · 13.2 KB
/
DefaultCode.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
using PaintDotNet.Direct2D1;
using System;
namespace PdnCodeLab
{
internal static class DefaultCode
{
internal static string Default => ForProjectType(ProjectType.Default);
internal static string ForProjectType(ProjectType projectType) => projectType switch
{
ProjectType.ClassicEffect => ClassicEffect,
ProjectType.BitmapEffect => BitmapEffect,
ProjectType.GpuImageEffect => GPUEffect,
ProjectType.GpuDrawEffect => GPUDrawEffect,
ProjectType.FileType => FileType,
_ => string.Empty,
};
private const string ClassicEffect = ""
+ "// Name:\r\n"
+ "// Submenu:\r\n"
+ "// Author:\r\n"
+ "// Title:\r\n"
+ "// Version:\r\n"
+ "// Desc:\r\n"
+ "// Keywords:\r\n"
+ "// URL:\r\n"
+ "// Help:\r\n"
+ "#region UICode\r\n"
+ "IntSliderControl Amount1 = 0; // [0,100] Slider 1 Description\r\n"
+ "IntSliderControl Amount2 = 0; // [0,100] Slider 2 Description\r\n"
+ "IntSliderControl Amount3 = 0; // [0,100] Slider 3 Description\r\n"
+ "#endregion\r\n"
+ "\r\n"
+ "void Render(Surface dst, Surface src, Rectangle rect)\r\n"
+ "{\r\n"
+ " //uint seed = RandomNumber.InitializeSeed(RandomNumberRenderSeed, rect.Location);\r\n"
+ " // Delete any of these lines you don't need\r\n"
+ " Rectangle selection = EnvironmentParameters.SelectionBounds;\r\n"
+ " int centerX = ((selection.Right - selection.Left) / 2) + selection.Left;\r\n"
+ " int centerY = ((selection.Bottom - selection.Top) / 2) + selection.Top;\r\n"
+ " ColorBgra primaryColor = EnvironmentParameters.PrimaryColor;\r\n"
+ " ColorBgra secondaryColor = EnvironmentParameters.SecondaryColor;\r\n"
+ " int brushWidth = (int)EnvironmentParameters.BrushWidth;\r\n"
+ "\r\n"
+ " ColorBgra currentPixel;\r\n"
+ " for (int y = rect.Top; y < rect.Bottom; y++)\r\n"
+ " {\r\n"
+ " if (IsCancelRequested) return;\r\n"
+ " for (int x = rect.Left; x < rect.Right; x++)\r\n"
+ " {\r\n"
+ " currentPixel = src[x,y];\r\n"
+ " // TODO: Add pixel processing code here\r\n"
+ " // Access RGBA values this way, for example:\r\n"
+ " // currentPixel.R = RandomNumber.NextByte(ref seed);\r\n"
+ " // currentPixel.G = RandomNumber.NextByte(ref seed);\r\n"
+ " // currentPixel.B = RandomNumber.NextByte(ref seed);\r\n"
+ " // currentPixel.A = primaryColor.A;\r\n"
+ " dst[x,y] = currentPixel;\r\n"
+ " }\r\n"
+ " }\r\n"
+ "}\r\n";
private const string BitmapEffect = ""
+ "// Name:\r\n"
+ "// Submenu:\r\n"
+ "// Author:\r\n"
+ "// Title:\r\n"
+ "// Version:\r\n"
+ "// Desc:\r\n"
+ "// Keywords:\r\n"
+ "// URL:\r\n"
+ "// Help:\r\n"
+ "\r\n// For help writing a Bitmap plugin: https://boltbait.com/pdn/CodeLab/help/tutorial/bitmap/\r\n\r\n"
+ "#region UICode\r\n"
+ "IntSliderControl Amount1 = 0; // [0,100] Slider 1 Description\r\n"
+ "IntSliderControl Amount2 = 0; // [0,100] Slider 2 Description\r\n"
+ "IntSliderControl Amount3 = 0; // [0,100] Slider 3 Description\r\n"
+ "#endregion\r\n"
+ "\r\n"
+ "protected override void OnRender(IBitmapEffectOutput output)\r\n"
+ "{\r\n"
+ " using IEffectInputBitmap<ColorBgra32> sourceBitmap = Environment.GetSourceBitmapBgra32();\r\n"
+ " using IBitmapLock<ColorBgra32> sourceLock = sourceBitmap.Lock(new RectInt32(0, 0, sourceBitmap.Size));\r\n"
+ " RegionPtr<ColorBgra32> sourceRegion = sourceLock.AsRegionPtr();\r\n"
+ "\r\n"
+ " RectInt32 outputBounds = output.Bounds;\r\n"
+ " using IBitmapLock<ColorBgra32> outputLock = output.LockBgra32();\r\n"
+ " RegionPtr<ColorBgra32> outputSubRegion = outputLock.AsRegionPtr();\r\n"
+ " var outputRegion = outputSubRegion.OffsetView(-outputBounds.Location);\r\n"
+ " //uint seed = RandomNumber.InitializeSeed(RandomNumberRenderSeed, outputBounds.Location);\r\n"
+ "\r\n"
+ " // Delete any of these lines you don't need\r\n"
+ " ColorBgra32 primaryColor = Environment.PrimaryColor;\r\n"
+ " ColorBgra32 secondaryColor = Environment.SecondaryColor;\r\n"
+ " int canvasCenterX = Environment.Document.Size.Width / 2;\r\n"
+ " int canvasCenterY = Environment.Document.Size.Height / 2;\r\n"
+ " var selection = Environment.Selection.RenderBounds;\r\n"
+ " int selectionCenterX = (selection.Right - selection.Left) / 2 + selection.Left;\r\n"
+ " int selectionCenterY = (selection.Bottom - selection.Top) / 2 + selection.Top;\r\n"
+ "\r\n"
+ " // Loop through the output canvas tile\r\n"
+ " for (int y = outputBounds.Top; y < outputBounds.Bottom; ++y)\r\n"
+ " {\r\n"
+ " if (IsCancelRequested) return;\r\n"
+ "\r\n"
+ " for (int x = outputBounds.Left; x < outputBounds.Right; ++x)\r\n"
+ " {\r\n"
+ " // Get your source pixel\r\n"
+ " ColorBgra32 sourcePixel = sourceRegion[x,y];\r\n"
+ "\r\n"
+ " // TODO: Change source pixel according to some algorithm\r\n"
+ " //sourcePixel.B = (byte)(Amount1 * 255 / 100); // Blue\r\n"
+ " //sourcePixel.G = (byte)(Amount2 * 255 / 100); // Green\r\n"
+ " //sourcePixel.R = (byte)(Amount3 * 255 / 100); // Red\r\n"
+ " //sourcePixel.A = 255; // Alpha Transparency\r\n"
+ "\r\n"
+ " // Save your pixel to the output canvas\r\n"
+ " outputRegion[x,y] = sourcePixel;\r\n"
+ " }\r\n"
+ " }\r\n"
+ "}\r\n";
private const string GPUEffect = ""
+ "// Name:\r\n"
+ "// Submenu:\r\n"
+ "// Author:\r\n"
+ "// Title:\r\n"
+ "// Version:\r\n"
+ "// Desc:\r\n"
+ "// Keywords:\r\n"
+ "// URL:\r\n"
+ "// Help:\r\n"
+ "\r\n// For help writing a GPU Image plugin: https://boltbait.com/pdn/CodeLab/help/tutorial/image/\r\n\r\n"
+ "#region UICode\r\n"
+ "IntSliderControl Amount1 = 0; // [0,100] Slider 1 Description\r\n"
+ "IntSliderControl Amount2 = 0; // [0,100] Slider 2 Description\r\n"
+ "IntSliderControl Amount3 = 0; // [0,100] Slider 3 Description\r\n"
+ "#endregion\r\n"
+ "\r\n"
+ "protected override IDeviceImage OnCreateOutput(IDeviceContext deviceContext)\r\n"
+ "{\r\n"
+ " // TODO: replace this return statement with your GPU pipeline algorithm\r\n"
+ " return Environment.SourceImage;\r\n"
+ "}\r\n";
private const string GPUDrawEffect = ""
+ "// Name:\r\n"
+ "// Submenu:\r\n"
+ "// Author:\r\n"
+ "// Title:\r\n"
+ "// Version:\r\n"
+ "// Desc:\r\n"
+ "// Keywords:\r\n"
+ "// URL:\r\n"
+ "// Help:\r\n"
+ "\r\n// For help writing a GPU Drawing plugin: https://boltbait.com/pdn/CodeLab/help/tutorial/drawing/\r\n\r\n"
+ "#region UICode\r\n"
+ "IntSliderControl Amount1 = 0; // [0,100] Slider 1 Description\r\n"
+ "IntSliderControl Amount2 = 0; // [0,100] Slider 2 Description\r\n"
+ "IntSliderControl Amount3 = 0; // [0,100] Slider 3 Description\r\n"
+ "#endregion\r\n"
+ "\r\n"
+ "protected override unsafe void OnDraw(IDeviceContext deviceContext)\r\n"
+ "{\r\n"
+ " // TODO: replace this DrawImage statement with your GPU Drawing statements\r\n"
+ " deviceContext.DrawImage(Environment.SourceImage);\r\n"
+ "}\r\n";
private const string FileType = "" +
"// Name:\r\n" +
"// Author:\r\n" +
"// Version:\r\n" +
"// Desc:\r\n" +
"// URL:\r\n" +
"// LoadExtns: .foo, .bar\r\n" +
"// SaveExtns: .foo, .bar\r\n" +
"// Flattened: false\r\n" +
"#region UICode\r\n" +
"CheckboxControl Amount1 = false; // Checkbox Description\r\n" +
"#endregion\r\n" +
"\r\n" +
"private const string HeaderSignature = \".PDN\";\r\n" +
"\r\n" +
"void SaveImage(Document input, Stream output, PropertyBasedSaveConfigToken token, Surface scratchSurface, ProgressEventHandler progressCallback)\r\n" +
"{\r\n" +
" // Render a flattened view of the Document to the scratch surface.\r\n" +
" input.CreateRenderer().Render(scratchSurface);\r\n" +
"\r\n" +
" if (Amount1)\r\n" +
" {\r\n" +
" new UnaryPixelOps.Invert().Apply(scratchSurface, scratchSurface.Bounds);\r\n" +
" }\r\n" +
"\r\n" +
" // The stream paint.net hands us must not be closed.\r\n" +
" using (BinaryWriter writer = new BinaryWriter(output, Encoding.UTF8, leaveOpen: true))\r\n" +
" {\r\n" +
" // Write the file header.\r\n" +
" writer.Write(Encoding.ASCII.GetBytes(HeaderSignature));\r\n" +
" writer.Write(scratchSurface.Width);\r\n" +
" writer.Write(scratchSurface.Height);\r\n" +
"\r\n" +
" for (int y = 0; y < scratchSurface.Height; y++)\r\n" +
" {\r\n" +
" // Report progress if the callback is not null.\r\n" +
" if (progressCallback != null)\r\n" +
" {\r\n" +
" double percent = (double)y / scratchSurface.Height;\r\n" +
"\r\n" +
" progressCallback(null, new ProgressEventArgs(percent));\r\n" +
" }\r\n" +
"\r\n" +
" for (int x = 0; x < scratchSurface.Width; x++)\r\n" +
" {\r\n" +
" // Write the pixel values.\r\n" +
" ColorBgra color = scratchSurface[x, y];\r\n" +
"\r\n" +
" writer.Write(color.Bgra);\r\n" +
" }\r\n" +
" }\r\n" +
" }\r\n" +
"}\r\n" +
"\r\n" +
"Document LoadImage(Stream input)\r\n" +
"{\r\n" +
" Document doc = null;\r\n" +
"\r\n" +
" // The stream paint.net hands us must not be closed.\r\n" +
" using (BinaryReader reader = new BinaryReader(input, Encoding.UTF8, leaveOpen: true))\r\n" +
" {\r\n" +
" // Read and validate the file header.\r\n" +
" byte[] headerSignature = reader.ReadBytes(4);\r\n" +
"\r\n" +
" if (Encoding.ASCII.GetString(headerSignature) != HeaderSignature)\r\n" +
" {\r\n" +
" throw new FormatException(\"Invalid file signature.\");\r\n" +
" }\r\n" +
"\r\n" +
" int width = reader.ReadInt32();\r\n" +
" int height = reader.ReadInt32();\r\n" +
"\r\n" +
" // Create a new Document.\r\n" +
" doc = new Document(width, height);\r\n" +
"\r\n" +
" // Create a background layer.\r\n" +
" BitmapLayer layer = Layer.CreateBackgroundLayer(width, height);\r\n" +
"\r\n" +
" for (int y = 0; y < height; y++)\r\n" +
" {\r\n" +
" for (int x = 0; x < width; x++)\r\n" +
" {\r\n" +
" // Read the pixel values from the file.\r\n" +
" uint bgraColor = reader.ReadUInt32();\r\n" +
"\r\n" +
" layer.Surface[x, y] = ColorBgra.FromUInt32(bgraColor);\r\n" +
" }\r\n" +
" }\r\n" +
"\r\n" +
" // Add the new layer to the Document.\r\n" +
" doc.Layers.Add(layer);\r\n" +
" }\r\n" +
"\r\n" +
" return doc;\r\n" +
"}\r\n" +
"\r\n";
}
}