-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
561 lines (476 loc) · 16.7 KB
/
MainForm.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;
namespace tSpritePadder
{
public partial class tSpritePadderWindow : Form
{
private Bitmap flippedBitmap = null;
private Bitmap fixedBitmap = null;
private Bitmap unpaddedBitmap = null;
private Bitmap finalBitmap = null;
private ImageForm InputImageForm;
private ImageForm OutputImageForm;
private RadioButton selectedPaddingColor;
private Color PaddingColor => selectedPaddingColor?.Equals(PaddingColorTransparent) == true ? Color.Transparent : Color.Violet;
/// <summary>
/// Upscales x2 preserving pixels
/// </summary>
private Bitmap UpscaleImage(Image image)
{
Rectangle destRect = new Rectangle(0, 0, image.Width * 2, image.Height * 2);
Bitmap destImage = new Bitmap(image.Width * 2, image.Height * 2);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(destImage))
{
//graphics.CompositingMode = CompositingMode.SourceCopy;
//graphics.CompositingQuality = CompositingQuality.HighQuality;
//graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
//graphics.SmoothingMode = SmoothingMode.HighQuality;
//graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
//These settings make sure it's scaled perfectly x2
graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
graphics.SmoothingMode = SmoothingMode.Default;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.AssumeLinear;
graphics.PixelOffsetMode = PixelOffsetMode.Half;
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
//using (ImageAttributes wrapMode = new ImageAttributes())
//{
// //wrapMode.SetWrapMode(WrapMode.TileFlipXY);
// wrapMode.SetWrapMode(WrapMode.Tile);
// graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
//}
}
return destImage;
}
/// <summary>
/// Returns a new bitmap that fits the cutout arguments
/// </summary>
private Bitmap GetResizedBitmap(Image input, int horizontal, int vertical, int cutoutWidth, int cutoutHeight, bool flipped)
{
int frameHeight = input.Height / (!flipped ? vertical : horizontal);
int leftoverWidth = input.Width % cutoutWidth;
int additionalWidth = 0;
if (leftoverWidth > 0)
{
additionalWidth = cutoutWidth - leftoverWidth;
}
int leftoverHeight = input.Height % cutoutHeight;
int additionalHeight = 0;
if (leftoverHeight > 0)
{
additionalHeight = cutoutHeight - leftoverHeight;
}
if (TwoMoreAtBottom.Checked)
{
//Check for two pixels for each bottom row of a vertical frame
//adjust additionalHeight
leftoverHeight = frameHeight - cutoutHeight - 2;
while (leftoverHeight > 0)
{
if (cutoutHeight <= 0)
{
MessageBox.Show("wat you doing how is 'Cutout Size' less than 0");
cutoutHeight = 1;
}
leftoverHeight -= cutoutHeight;
}
additionalHeight = -leftoverHeight;
}
Bitmap expanded = GetExpandedBitmap(input, additionalWidth, additionalHeight);
//This g now operates on expanded
using (Graphics g = Graphics.FromImage(expanded))
{
g.Clear(Color.Transparent);
//Use bottom left as anchor point for input -> expanded
g.DrawImage(input, new Point(0, additionalHeight));
}
return expanded;
}
/// <summary>
/// Expands the bitmap based on the arguments
/// </summary>
private Bitmap GetExpandedBitmap(Image input, int padAmountX, int padAmountY)
{
int newWidth = input.Width + padAmountX;
int newHeight = input.Height + padAmountY;
Bitmap resized = new Bitmap(newWidth, newHeight);
//Because of resolution fuckery with gimp and other export mechanisms (Gimp saves as 72, constructor has 120)
resized.SetResolution(input.HorizontalResolution, input.VerticalResolution);
//This g now operates on resized
using (Graphics g = Graphics.FromImage(resized))
{
g.Clear(Color.Transparent);
//Use bottom left as anchor point for input -> resized
g.DrawImage(input, new Point(0, padAmountY));
}
return resized;
}
/// <summary>
/// Cuts the image into frames, flips if specified
/// </summary>
private Bitmap AdjustFrames(Image input, int horizontal, int vertical, int cutoutWidth, int cutoutHeight, bool flip)
{
int frameWidth = input.Width / horizontal;
int frameHeight = input.Height / vertical;
int leftoverWidth = frameWidth % cutoutWidth;
int additionalWidth = 0;
if (leftoverWidth > 0)
{
additionalWidth = cutoutWidth - leftoverWidth;
}
int leftoverHeight = frameHeight % cutoutHeight;
int additionalHeight = 0;
if (leftoverHeight > 0)
{
additionalHeight = cutoutHeight - leftoverHeight;
}
if (TwoMoreAtBottom.Checked)
{
//Check for two pixels for each bottom row of a vertical frame
//adjust additionalHeight
leftoverHeight = frameHeight - cutoutHeight - 2;
while (leftoverHeight > 0)
{
if (cutoutHeight <= 0)
{
MessageBox.Show("wat you doing how is 'Cutout Size' less than 0");
cutoutHeight = 1;
}
leftoverHeight -= cutoutHeight;
}
additionalHeight = -leftoverHeight;
}
Size oldFrameSize = new Size(frameWidth, frameHeight);
Size newFrameSize = new Size(frameWidth + additionalWidth, frameHeight + additionalHeight);
Bitmap final = new Bitmap((!flip ? horizontal : vertical) * newFrameSize.Width, (!flip ? vertical : horizontal) * newFrameSize.Height);
//Because of resolution fuckery with gimp and other export mechanisms (Gimp saves as 72, constructor has 120)
//Don't flip resolution though
final.SetResolution(input.HorizontalResolution, input.VerticalResolution);
using (Graphics g = Graphics.FromImage(final))
{
g.Clear(Color.Transparent);
GraphicsUnit units = GraphicsUnit.Pixel;
for (int i = 0; i < horizontal; i++)
{
for (int j = 0; j < vertical; j++)
{
//Take the frame from the input image
RectangleF srcRect = new RectangleF(i * oldFrameSize.Width, j * oldFrameSize.Height, oldFrameSize.Width, oldFrameSize.Height);
//Project it onto the final image, but with padding
g.DrawImage(input, (!flip ? i : j) * newFrameSize.Width, (!flip ? j : i) * newFrameSize.Height + additionalHeight, srcRect, units);
}
}
}
return final;
}
private Bitmap CutAndPadSingle(Image input, int horizontal, int vertical, int cutoutWidth, int cutoutHeight, int paddingX, int paddingY, bool flipped, out int cutoutCountX, out int cutoutCountY)
{
using (Bitmap resized = GetResizedBitmap(input, horizontal, vertical, cutoutWidth, cutoutHeight, flipped))
{
//Resize bitmap so it can be cut in even parts (adds padding on the top and right if necessary)
fixedBitmap = (Bitmap)resized.Clone();
int frameWidth = resized.Width / (!flipped ? horizontal : vertical);
int frameHeight = resized.Height / (!flipped ? vertical : horizontal);
cutoutCountX = resized.Width / cutoutWidth;
cutoutCountY = resized.Height / cutoutHeight;
if (TwoMoreAtBottom.Checked)
{
//2 more height for each cutout on each new frame, imply the cutout is always the same dimensions
int tempHeight = resized.Height - 2 * (!flipped ? vertical : horizontal);
if (tempHeight <= 0)
{
tempHeight = 1;
}
cutoutCountY = tempHeight / cutoutHeight;
}
Bitmap final = GetExpandedBitmap(resized, cutoutCountX * paddingX, cutoutCountY * paddingY);
//Resize bitmap so it can be cut in even parts including padding (adds padding on the top and right if necessary)
unpaddedBitmap = (Bitmap)final.Clone();
if (paddingX == 0 && paddingY == 0) return final;
//No point drawing the final image with no padding
int cutoutPerFrame = cutoutCountY / (!flipped ? vertical : horizontal);
using (Graphics g = Graphics.FromImage(final))
{
g.CompositingMode = CompositingMode.SourceCopy;
g.Clear(PaddingColor);
GraphicsUnit units = GraphicsUnit.Pixel;
for (int i = 0; i < cutoutCountX; i++)
{
int padWidth = cutoutWidth + paddingX;
int srcX = i * cutoutWidth + (int)HorizontalOffsetNumber.Value;
int srcY = (int)VerticalOffsetNumber.Value;
int padY = 0;
for (int j = 0; j < cutoutCountY; j++)
{
//Use j not as a variable to define coordinates here, rely on srcY, padY
int srcHeight = cutoutHeight;
if (TwoMoreAtBottom.Checked)
{
//j % cutoutPerFrame checks which cutout# on a frame we are on
//Example: 3 frames, 6 cutouts: each frame as 2 cutouts ('0' and '1')
//If the cutout# is the max number of cutouts - 1 (bottommost cutout), add 2
if (j % cutoutPerFrame == cutoutPerFrame - 1)
{
srcHeight += 2;
}
}
//Take the cutout from the resized image
RectangleF srcRect = new RectangleF(srcX, srcY, cutoutWidth, srcHeight);
srcY += srcHeight;
//Project it onto the final image, but with padding
g.DrawImage(resized, i * padWidth, padY, srcRect, units);
padY += srcHeight + paddingY;
}
}
}
return final;
}
}
private void UpdateImages()
{
Image next = InputImage.InitialImage;
if (InputImage.Image != null)
{
next = InputImage.Image;
}
else
{
InputImage.Image = InputImage.InitialImage;
}
InputImageLabel.Text = $"Input: {next.Width}x{next.Height}";
FeedImageForm(InputImageForm, InputImage, InputImageLabel.Text);
if (Upscale2.Checked)
{
next = UpscaleImage(next);
}
Bitmap framed = AdjustFrames(next, (int)HorizontalCountNumber.Value, (int)VerticalCountNumber.Value, (int)CutoutNumber.Value, (int)CutoutNumber.Value, FlipDimensions.Checked);
flippedBitmap = (Bitmap)framed.Clone();
Bitmap final = CutAndPadSingle(framed, (int)HorizontalCountNumber.Value, (int)VerticalCountNumber.Value, (int)CutoutNumber.Value, (int)CutoutNumber.Value, (int)PaddingNumber.Value, (int)PaddingNumber.Value, FlipDimensions.Checked, out int cutoutCountX, out int cutoutCountY);
finalBitmap = (Bitmap)final.Clone();
OutputImage.Image = final;
OutputImageLabel.Text = $"Output: {OutputImage.Image.Width}x{OutputImage.Image.Height}";
FeedImageForm(OutputImageForm, OutputImage, OutputImageLabel.Text);
//Example:
//Static : 3 x 1 frames
//No flip: 15 x 2
//flipped: 5 x 6
//Result should always be 5 x 2
int horizontalCuts = !FlipDimensions.Checked ? cutoutCountX : cutoutCountX * (int)HorizontalCountNumber.Value;
int verticalCuts = !FlipDimensions.Checked ? cutoutCountY : cutoutCountY / (int)HorizontalCountNumber.Value;
int horizontalCutsPerFrame = horizontalCuts / (int)HorizontalCountNumber.Value;
int verticalCutsPerFrame = verticalCuts / (int)VerticalCountNumber.Value;
OutputFrameLabel.Text = $"Cutouts per frame: {horizontalCutsPerFrame}x{verticalCutsPerFrame}";
}
public tSpritePadderWindow()
{
InitializeComponent();
}
#region ValueChanged
private void CutoutNumber_ValueChanged(object sender, EventArgs e)
{
UpdateImages();
}
private void PaddingNumber_ValueChanged(object sender, EventArgs e)
{
UpdateImages();
}
private void HorizontalCountNumber_ValueChanged(object sender, EventArgs e)
{
UpdateImages();
}
private void VerticalCountNumber_ValueChanged(object sender, EventArgs e)
{
UpdateImages();
}
private void HorizontalOffsetNumber_ValueChanged(object sender, EventArgs e)
{
UpdateImages();
}
private void VerticalOffsetNumber_ValueChanged(object sender, EventArgs e)
{
UpdateImages();
}
#endregion
#region Click
private void Upscale2_Click(object sender, EventArgs e)
{
UpdateImages();
}
private void FlipDimensions_Click(object sender, EventArgs e)
{
UpdateImages();
}
private void TwoMoreAtBottom_Click(object sender, EventArgs e)
{
UpdateImages();
}
private void SaveFlipped_Click(object sender, EventArgs e)
{
UpdateImages();
}
private void SaveFixed_Click(object sender, EventArgs e)
{
UpdateImages();
}
private void SaveUnpadded_Click(object sender, EventArgs e)
{
UpdateImages();
}
private void HandleImageForm(ref ImageForm imageForm, PictureBox pictureBox, string text)
{
if (imageForm == null || !imageForm.Visible)
{
imageForm?.Close();
imageForm = new ImageForm();
FeedImageForm(imageForm, pictureBox, text);
imageForm.Show(this);
}
else
{
if (imageForm.Visible)
{
int a = 0;
}
imageForm.Close();
imageForm = null;
}
}
private void FeedImageForm(ImageForm imageForm, PictureBox pictureBox, string text)
{
if (imageForm == null) return;
imageForm.Image.Image = pictureBox.Image;
imageForm.Image.BackColor = pictureBox.BackColor;
imageForm.Text = text;
}
private void InputImage_Click(object sender, EventArgs e)
{
HandleImageForm(ref InputImageForm, InputImage, InputImageLabel.Text);
}
private void OutputImage_Click(object sender, EventArgs e)
{
HandleImageForm(ref OutputImageForm, OutputImage, OutputImageLabel.Text);
}
private void TargetFolderButton_Click(object sender, EventArgs e)
{
ImageSelectDialog.ShowDialog();
}
private void ChooseFolderButton_Click(object sender, EventArgs e)
{
ChooseFolderDialog.ShowDialog();
}
private void GoButton_Click(object sender, EventArgs e)
{
if (ImageSelectDialog.FileName == string.Empty)
{
MessageBox.Show("Please choose a file");
return;
}
else if (ChooseFolderDialog.SelectedPath == string.Empty)
{
MessageBox.Show("Please choose a folder");
return;
}
int count = ImageSelectDialog.FileName.Length;
string suffix = ".png";
string firstHalf = ImageSelectDialog.FileName.Substring(0, count - suffix.Length);
string[] strings = firstHalf.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
string fileName = strings.Last();
try
{
string prefix = $"{ChooseFolderDialog.SelectedPath}\\{Text}_{fileName}";
List<string> savedFiles = new List<string>();
string name = "final";
finalBitmap?.Save($"{prefix}_{name}{suffix}", ImageFormat.Png);
savedFiles.Add(name);
if (SaveFlipped.Checked && flippedBitmap != null)
{
name = "flipped";
flippedBitmap.Save($"{prefix}_{name}{suffix}", ImageFormat.Png);
savedFiles.Add(name);
}
if (SaveFixed.Checked && fixedBitmap != null)
{
name = "fixed";
fixedBitmap.Save($"{prefix}_{name}{suffix}", ImageFormat.Png);
savedFiles.Add(name);
}
if (SaveUnpadded.Checked && unpaddedBitmap != null)
{
name = "unpadded";
unpaddedBitmap.Save($"{prefix}_{name}{suffix}", ImageFormat.Png);
savedFiles.Add(name);
}
string names = string.Empty;
foreach (var file in savedFiles)
{
names += $"\r\n{Text}_{fileName}_{file}{suffix}";
}
MessageBox.Show("The following have been saved to " +
ChooseFolderDialog.SelectedPath + ":" + names);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error");
}
}
#endregion
private void InputSelectDialog_FileOk(object sender, CancelEventArgs e)
{
int count = ImageSelectDialog.FileName.Length;
string suffix = ".png";
string firstHalf = ImageSelectDialog.FileName.Substring(0, count - suffix.Length);
string[] strings = firstHalf.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
string fileName = strings.Last();
InputLabel.Text = fileName;
InputImage.Image = Image.FromFile(ImageSelectDialog.FileName);
UpdateImages();
}
private void tSpritePadderWindow_Resize(object sender, EventArgs e)
{
InputImageLabel.Anchor = InputImage.Anchor;
OutputImageLabel.Anchor = OutputImage.Anchor;
}
private void UpdatePaddingColor(object sender)
{
if (!(sender is RadioButton rb))
{
return;
}
//Ensure that the RadioButton.Checked property changed to true
if (rb.Checked)
{
//Keep track of the selected RadioButton by saving a reference to it
selectedPaddingColor = rb;
UpdateImages();
}
}
private void PaddingColorDefault_CheckedChanged(object sender, EventArgs e)
{
UpdatePaddingColor(sender);
}
private void PaddingColorTransparent_CheckedChanged(object sender, EventArgs e)
{
UpdatePaddingColor(sender);
}
private void PreviewColorButton_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
InputImage.BackColor = colorDialog1.Color;
OutputImage.BackColor = colorDialog1.Color;
UpdateImages();
}
}
private void tSpritePadderWindow_Load(object sender, EventArgs e)
{
UpdateImages();
}
}
}