Skip to content

Commit b2806ae

Browse files
committed
threads totally not stolen from chatgpt
gasp
1 parent 382f9c2 commit b2806ae

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

Program.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.IO;
77
using ImageMagick;
8+
using System.Threading.Tasks;
89

910
class Programmation
1011
{
@@ -33,25 +34,29 @@ static void Main()
3334

3435
Console.WriteLine("Starting conversion of " + inputFiles.Length + " pvr files");
3536

36-
foreach (string inputFile in inputFiles)
37+
// Specify the number of threads to use
38+
int threadCount = GetUserSpecifiedThreadCount();
39+
40+
// Use Parallel.ForEach to process the input files in parallel
41+
Parallel.ForEach(inputFiles, new ParallelOptions { MaxDegreeOfParallelism = threadCount }, inputFile =>
3742
{
3843
string inputFileName = Path.GetFileName(inputFile);
3944
string outputFileName = Path.ChangeExtension(inputFileName, ".png");
4045
string outputPath = Path.Combine(outputDirectory, outputFileName);
4146
string arguments = $"-f r8g8b8a8 -i \"{inputFile}\" -d \"{outputPath}\"";
4247

4348
ExecuteCommand(toolPath, arguments);
44-
}
49+
});
4550

4651
string[] outputFiles = Directory.GetFiles(outputDirectory, "*.png");
47-
foreach (string outputFile in outputFiles)
52+
Parallel.ForEach(outputFiles, new ParallelOptions { MaxDegreeOfParallelism = threadCount }, outputFile =>
4853
{
4954
if (linearize)
5055
LinearizeImage(outputFile, outputFile);
5156

5257
if (flip)
5358
FlipImage(outputFile);
54-
}
59+
});
5560

5661
string[] stupidFiles = (from file in Directory.GetFiles("pvrs", "*.pvr") where Path.GetFileNameWithoutExtension(file).EndsWith("_Out") select file).ToArray();
5762
foreach (string stupidFile in stupidFiles)
@@ -100,6 +105,7 @@ static void ExecuteCommand(string toolPath, string arguments)
100105
process.WaitForExit();
101106
}
102107
}
108+
103109
static void FlipImage(string imagePath)
104110
{
105111
Console.WriteLine("Flipping image: " + Path.GetFileName(imagePath));
@@ -110,4 +116,19 @@ static void FlipImage(string imagePath)
110116
image.Save(imagePath);
111117
}
112118
}
113-
}
119+
120+
static int GetUserSpecifiedThreadCount()
121+
{
122+
Console.WriteLine("Enter the number of threads to use (more threads = faster but higher demand on the system):");
123+
string input = Console.ReadLine();
124+
int threadCount;
125+
126+
while (!int.TryParse(input, out threadCount) || threadCount <= 0)
127+
{
128+
Console.WriteLine("Invalid input. Please enter a positive integer:");
129+
input = Console.ReadLine();
130+
}
131+
132+
return threadCount;
133+
}
134+
}

0 commit comments

Comments
 (0)