|
1 | 1 | // See https://aka.ms/new-console-template for more information |
2 | 2 |
|
| 3 | +using System.Diagnostics; |
| 4 | +using System.Text.Json; |
| 5 | + |
| 6 | +using DotNetCampus.MediaConverters.Contexts; |
| 7 | +using DotNetCampus.MediaConverters.Imaging.Optimizations; |
| 8 | + |
3 | 9 | using SixLabors.ImageSharp; |
4 | | -using SixLabors.ImageSharp.Formats; |
5 | | -using SixLabors.ImageSharp.Formats.Bmp; |
6 | | -using SixLabors.ImageSharp.Formats.Tiff; |
7 | | -using SixLabors.ImageSharp.Formats.Webp; |
8 | 10 | using SixLabors.ImageSharp.PixelFormats; |
9 | | -using SixLabors.ImageSharp.Processing; |
| 11 | +using SourceGenerationContext = DotNetCampus.MediaConverters.Contexts.SourceGenerationContext; |
10 | 12 |
|
11 | | -using System.Text; |
| 13 | +namespace DotNetCampus.MediaConverters; |
12 | 14 |
|
13 | | -ImageDecoder d = WebpDecoder.Instance; |
| 15 | +public class Program |
| 16 | +{ |
| 17 | + public static async Task<int> Main(string[] args) |
| 18 | + { |
| 19 | + var options = DotNetCampus.Cli.CommandLine.Parse(args).As<Options>(); |
14 | 20 |
|
15 | | -var maxPixelCount = 100_00_00; |
| 21 | + var jsonText = await File.ReadAllTextAsync(options.ConvertConfigurationFile); |
| 22 | + var imageConvertContext = JsonSerializer.Deserialize<ImageConvertContext>(jsonText, SourceGenerationContext.Default.Options); |
16 | 23 |
|
17 | | -var w = 1500; |
18 | | -var h = 1300; |
| 24 | + if (imageConvertContext is null) |
| 25 | + { |
| 26 | + Console.Error.WriteLine("无法解析转换配置文件,请检查配置文件内容是否正确。"); |
| 27 | + return ErrorCode.UnknownError; |
| 28 | + } |
19 | 29 |
|
20 | | -var s = w / (double) h; |
| 30 | + var inputFile = new FileInfo(options.InputFile); |
21 | 31 |
|
22 | | -var pw = (int) Math.Sqrt(maxPixelCount * w / (double) h); |
23 | | -var ph = (int) Math.Sqrt(maxPixelCount * h / (double) w); |
| 32 | + var workingFolder = Directory.CreateDirectory(options.WorkingFolder); |
24 | 33 |
|
25 | | -var s1 = pw / (double) ph; |
| 34 | + var imageFileOptimizationResult = await ImageFileOptimization.OptimizeImageFileAsync(inputFile, workingFolder, imageConvertContext.MaxImageWidth, imageConvertContext.MaxImageHeight, imageConvertContext.UseAreaSizeLimit ?? true); |
26 | 35 |
|
| 36 | + if (!imageFileOptimizationResult.IsSuccess) |
| 37 | + { |
| 38 | + Console.Error.WriteLine(imageFileOptimizationResult); |
27 | 39 |
|
| 40 | + switch (imageFileOptimizationResult.FailureReason) |
| 41 | + { |
| 42 | + case ImageFileOptimizationFailureReason.Ok: |
| 43 | + Debug.Fail("返回成功时,不可能状态是失败"); |
| 44 | + break; |
| 45 | + case ImageFileOptimizationFailureReason.UnknownImageFormat: |
| 46 | + return ErrorCode.UnknownImageFormat; |
| 47 | + break; |
| 48 | + case ImageFileOptimizationFailureReason.InvalidImageContent: |
| 49 | + return ErrorCode.InvalidImageContent; |
| 50 | + break; |
| 51 | + case ImageFileOptimizationFailureReason.FileNotFound: |
| 52 | + return ErrorCode.ImageFileNotFound; |
| 53 | + break; |
| 54 | + case ImageFileOptimizationFailureReason.NotSupported: |
| 55 | + return ErrorCode.NotSupported; |
| 56 | + break; |
| 57 | + default: |
| 58 | + throw new ArgumentOutOfRangeException(); |
| 59 | + } |
28 | 60 |
|
29 | | -var tiffFile = @"E:\Download\file_example_TIFF_1MB.tiff"; |
30 | | -var file = @"E:\Download\file_example_favicon.ico"; |
31 | | -var buffer = File.ReadAllBytes(file); |
| 61 | + return ErrorCode.UnknownError; |
| 62 | + } |
32 | 63 |
|
33 | | -try |
34 | | -{ |
35 | | - var image = Image.Load<Rgba32>(buffer); |
36 | | - image.Mutate(context => context.Resize(new Size(100, 100), compand: true)); |
37 | | - Console.WriteLine(image.Width); |
38 | | - image.SaveAsPng("1.png"); |
| 64 | + FileInfo optimizedImageFile = imageFileOptimizationResult.OptimizedImageFile; |
39 | 65 |
|
40 | | - |
41 | | -} |
42 | | -catch (ImageFormatException e) |
43 | | -{ |
44 | | -} |
| 66 | + if (imageConvertContext.ImageConvertTaskList is { } list) |
| 67 | + { |
| 68 | + using var image = await Image.LoadAsync<Rgba32>(optimizedImageFile.FullName); |
| 69 | + |
| 70 | + foreach (var imageConvertTask in list) |
| 71 | + { |
| 72 | + imageConvertTask.Run(image); |
| 73 | + } |
| 74 | + } |
45 | 75 |
|
46 | | -Console.WriteLine("Hello, World!"); |
| 76 | + optimizedImageFile.CopyTo(options.OutputFile, overwrite: true); |
| 77 | + |
| 78 | + return 0; |
| 79 | + } |
| 80 | +} |
0 commit comments