You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.1 KiB
39 lines
1.1 KiB
4 weeks ago
|
using System.Diagnostics;
|
||
|
|
||
|
namespace SCI.AsciiArt;
|
||
|
|
||
|
public class Generator {
|
||
|
public static async Task<string> FromImage (string imagePath, int width) {
|
||
|
// Ensure image exists
|
||
|
if (!File.Exists (imagePath)) {
|
||
|
throw new FileNotFoundException ($"The specified file '{imagePath}' does not exist");
|
||
|
}
|
||
|
|
||
|
var process = new Process ();
|
||
|
process.StartInfo = new ProcessStartInfo () {
|
||
|
FileName = "jp2a",
|
||
|
Arguments = $"--width={width} \"{imagePath}\"",
|
||
|
RedirectStandardInput = true,
|
||
|
RedirectStandardOutput = true,
|
||
|
RedirectStandardError = true,
|
||
|
CreateNoWindow = true,
|
||
|
UseShellExecute = false
|
||
|
};
|
||
|
|
||
|
process.Start ();
|
||
|
|
||
|
// Wait up to one second for the task to finish
|
||
|
try {
|
||
|
var cancellationTokenSource = new CancellationTokenSource (1000);
|
||
|
await process.WaitForExitAsync (cancellationTokenSource.Token);
|
||
|
} catch (TaskCanceledException) {
|
||
|
throw new TimeoutException ("jp2a did not finish after 1000ms, process was killed");
|
||
|
}
|
||
|
|
||
|
if (process.ExitCode != 0) {
|
||
|
throw new Exception ($"jp2a returned with exit code {process.ExitCode}.\n{process.StandardError.ReadToEnd ()}");
|
||
|
}
|
||
|
|
||
|
return await process.StandardOutput.ReadToEndAsync ();
|
||
|
}
|
||
|
}
|