-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
194 lines (167 loc) · 6.41 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using ImageMagick;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Linq;
using MagickUtils.Utils;
using Paths = MagickUtils.Utils.Paths;
namespace MagickUtils
{
class Program
{
public static string currentDir;
public static string currentExt;
public static MainForm mainForm;
public static ProgressBar progBar;
public static string previewImgPath;
public enum ImageFormat { JPG, PNG, WEBP, BMP, DDS, TGA, J2K, AVIF, FLIF, HEIF, JXL }
[STAThread]
static void Main (string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mainForm = new MainForm();
mainForm.FormClosing += new FormClosingEventHandler(OnFormClose);
ResourceLimits.Memory = (ulong)Math.Round(ResourceLimits.Memory * 1.5f);
if(args.Length > 0 && args[0] != null && File.Exists(args[0]))
{
try
{
PreviewImage(args[0], true);
}
catch { }
}
else
{
Application.Run(mainForm);
}
}
public static void PreviewImage(string imgPath, bool run = false)
{
MagickImage tempImg = IOUtils.ReadImage(imgPath);
if (tempImg == null) return;
tempImg.Format = MagickFormat.Png;
string tempImgPath = Path.Combine(Paths.GetDataPath(), "previewImg.png");
tempImg.Write(tempImgPath);
tempImg.Dispose();
previewImgPath = tempImgPath;
if (run)
{
Application.Run(new ImagePreviewPopup());
}
else
{
new ImagePreviewPopup().Show();
}
}
private static void OnFormClose (Object sender, FormClosingEventArgs e)
{
string tempImgPath = Path.Combine(Paths.GetDataPath(), "previewImg.png");
if(File.Exists(tempImgPath)) File.Delete(tempImgPath);
}
static void PrintFilesInDir (string ext)
{
DirectoryInfo d = new DirectoryInfo(currentDir);
FileInfo[] Files = d.GetFiles("*." + ext);
int i = 1;
foreach(FileInfo file in Files)
{
MagickImage img = new MagickImage(file);
string fName = Path.GetFileName(img.FileName);
Logger.Log("Image " + i + "/" + Files.Length + ": " + fName + " (" + img.Width + "x" + img.Height + ", " + img.Depth + " bits)");
i++;
}
}
public static Stopwatch timer = new Stopwatch();
static long dirSizePre;
static long dirSizeAfter;
public static void ShowProgressIncrement (string text, ref int current, int amount)
{
current++;
ShowProgress(text, current, amount);
}
public static void ShowProgress (string text, int current, int amount)
{
if(text.Trim().Length > 1)
Logger.Log("\n" + text + current + "/" + amount);
int targetValue = (int)Math.Round((float)current / amount * 100);
if(targetValue > 100) targetValue = 100;
if(targetValue < 0) targetValue = 0;
progBar.Value = targetValue;
}
public static void PreProcessing (bool startStopwatch = true, bool showSize = true)
{
dirSizePre = 0;
dirSizePre = IOUtils.GetDirSize(new DirectoryInfo(currentDir));
if(showSize)
Logger.Log("\nFolder size before processing: " + FormatUtils.Bytes(dirSizePre) + "\n");
timer.Reset();
if(startStopwatch) timer.Start();
}
public static void PostProcessing (int amount, bool showStopwatch = false, bool showSize = true)
{
timer.Stop();
dirSizeAfter = 0;
dirSizeAfter = IOUtils.GetDirSize(new DirectoryInfo(currentDir));
progBar.Value = 0;
Logger.Log("\nDone.");
if (showStopwatch)
{
string rate = ((float)amount / (timer.ElapsedMilliseconds / 1000f)).ToString("0.00");
Logger.Log($"Processing time: {FormatUtils.TimeSw(timer)} for {amount} files ({rate}/Sec)");
}
if(showSize)
{
Logger.Log("Folder size after processing: " + FormatUtils.Bytes(dirSizeAfter) + " from " + FormatUtils.Bytes(dirSizePre));
Logger.Log("Size ratio: " + FormatUtils.Ratio(dirSizePre, dirSizeAfter) + " of original size");
}
}
public static async Task PutTaskDelay ()
{
await Task.Delay(1);
}
public static bool IsTrue (string input)
{
string inStr = input.Trim();
if(inStr == "y" || inStr == "Y") return true;
return false;
}
// public static void Logger.Log(string s, bool replaceLastLine = false)
// {
// Console.WriteLine(s);
// if(replaceLastLine)
// logTbox.Text = logTbox.Text.Remove(logTbox.Text.LastIndexOf(Environment.NewLine));
// if (logTbox == null)
// return;
// s = s.Replace("\n", Environment.NewLine);
// logTbox.AppendText(Environment.NewLine + s);
// }
public static bool IsPathValid (string path, bool showError = true)
{
try
{
DirectoryInfo d = new DirectoryInfo(path);
}
catch
{
if(showError)
MessageBox.Show("Invalid path!", "Error");
return false;
}
return true;
}
public static async Task<int> GetFormatQuality (MagickImage img)
{
string formatStr = img.Format.ToString().ToUpper().Replace("JPEG", "JPG");
if (formatStr.StartsWith("PNG") && formatStr.Length > 3) // Make "PNG24" -> "PNG" etc
formatStr = "PNG";
string configKey = "qMin" + formatStr;
int q = await Config.GetInt(configKey);
// Logger.Log($"Format quality for {img.Format} ({configKey}): {q}", true);
return q;
}
}
}