-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureScaler.cs
More file actions
96 lines (85 loc) · 3.79 KB
/
TextureScaler.cs
File metadata and controls
96 lines (85 loc) · 3.79 KB
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
using ImageMagick;
namespace TextureConverter
{
public partial class TextureScaler : Form
{
public TextureScaler()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
button2.Select();
}
private void button2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.SelectedPath;
textBox2.Text = folderBrowserDialog1.SelectedPath + "\\Output"; // auto-output by default
}
}
private void button3_Click(object sender, EventArgs e)
{
if (folderBrowserDialog2.ShowDialog() == DialogResult.OK)
textBox2.Text = folderBrowserDialog2.SelectedPath;
}
private async void button1_Click(object sender, EventArgs e)
{
string inputFolder = textBox1.Text;
string outputFolder = textBox2.Text;
string scaleText = comboBox1.Text;
bool replaceOriginal = checkBox1.Checked;
bool compression = checkBox2.Checked;
int scale = int.Parse(scaleText.Replace("%", ""));
if (!Directory.Exists(inputFolder))
{
MessageBox.Show("You need to specify a source directory to get started.", "Failed to find source folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!replaceOriginal)
{
if (!Directory.Exists(outputFolder))
Directory.CreateDirectory(outputFolder); // Create output folder if it doesn't exist
}
string[] fileSuffixes = { ".TIF", ".PNG", ".JPG", ".JPEG" };
foreach (string file in Directory.GetFiles(inputFolder, "*.*", SearchOption.AllDirectories))
{
if (Array.Exists(fileSuffixes, suffix => file.EndsWith(suffix, StringComparison.OrdinalIgnoreCase)))
{
string currentFolder = Path.GetDirectoryName(file) ?? outputFolder;
outputFolder = currentFolder + "\\Output";
await ProcessTextureScale(file, scale, replaceOriginal, compression, currentFolder, outputFolder);
}
}
MessageBox.Show("All the textures have been scaled, check the destination folder for results.", "Finished scaling textures", MessageBoxButtons.OK, MessageBoxIcon.Information);
progressBar1.Value = 0; // Reset progress bar...
}
private async Task ProcessTextureScale(string file, int scale, bool replaceOriginal, bool compression, string inputFolder, string outputFolder)
{
progressBar1.Value = 30;
using (MagickImage textureImage = new MagickImage(file))
{
int newWidth = (int)(textureImage.Width * scale / 100);
int newHeight = (int)(textureImage.Height * scale / 100);
textureImage.Scale(newWidth, newHeight);
progressBar1.Value = 60;
if (compression && file.EndsWith(".TIF", StringComparison.OrdinalIgnoreCase))
{
textureImage.Format = MagickFormat.Tiff;
textureImage.Settings.Compression = CompressionMethod.LZW;
}
if (replaceOriginal)
{
textureImage.Write(Path.Combine(inputFolder, Path.GetFileName(file)));
}
else
{
textureImage.Write(Path.Combine(outputFolder, Path.GetFileName(file)));
}
progressBar1.Value = 100;
textureImage.Dispose();
}
}
}
}