Skip to content

Commit 43c4fb2

Browse files
committed
分开保存逻辑
1 parent 17a9ff4 commit 43c4fb2

4 files changed

Lines changed: 36 additions & 27 deletions

File tree

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/ImageFileOptimization.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,17 @@ public static async Task<ImageFileOptimizationResult> OptimizeImageFileAsync(Ima
125125

126126
OptimizeImage(image, maxImageWidth, maxImageHeight, useAreaSizeLimit);
127127

128-
// 重新保存即可
129128
var outputImageFilePath = Path.Join(workingFolder.FullName, $"{Path.GetRandomFileName()}.png");
130-
await image.SaveAsPngAsync(outputImageFilePath, new PngEncoder()
129+
if (context.ShouldSaveToPngFile)
131130
{
132-
ColorType = PngColorType.RgbWithAlpha,
133-
BitDepth = PngBitDepth.Bit8,
134-
CompressionLevel = ((PngCompressionLevel?) context.PngCompressionLevel) ?? PngCompressionLevel.DefaultCompression
135-
});
131+
// 重新保存即可,保存就等于解决了各种格式问题,输出为标准的格式
132+
await image.SaveAsPngAsync(outputImageFilePath, new PngEncoder()
133+
{
134+
ColorType = PngColorType.RgbWithAlpha,
135+
BitDepth = PngBitDepth.Bit8,
136+
CompressionLevel = ((PngCompressionLevel?) context.PngCompressionLevel) ?? PngCompressionLevel.DefaultCompression
137+
});
138+
}
136139

137140
return new ImageFileOptimizationResult()
138141
{

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/ImageFileOptimizationContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public readonly record struct ImageFileOptimizationContext(FileInfo ImageFile,
1919

2020
public bool ShouldLogToFile { get; init; } = false;
2121

22+
/// <summary>
23+
/// 是否应该保存为 PNG 文件。为 false 则只存放到内存,不存放到文件
24+
/// </summary>
25+
public bool ShouldSaveToPngFile { get; init; } = true;
26+
2227
public string? LogFileName { get; init; }
2328
public int? PngCompressionLevel { get; init; }
2429

src/MediaConverters/MediaConverters.Lib/Imaging/Optimizations/ImageFileOptimizationResult.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public readonly record struct ImageFileOptimizationResult() : IDisposable
2020
public Exception? Exception { get; init; }
2121
public ImageFileOptimizationFailureReason FailureReason { get; init; } = ImageFileOptimizationFailureReason.Ok;
2222

23-
[MemberNotNullWhen(true, nameof(OptimizedImageFile))]
24-
public bool IsSuccess => OptimizedImageFile is not null;
23+
public bool IsSuccess => OptimizedImageFile is not null || Image is not null;
2524

2625
public Image<Rgba32>? Image { get; init; }
2726

src/MediaConverters/MediaConverters.Tool/Program.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,14 @@ internal static async Task<ErrorCode> RunAsync(ConvertHandler convertHandler)
8787
var context = new ImageFileOptimizationContext(inputFile, workingFolder, imageConvertContext.MaxImageWidth,
8888
imageConvertContext.MaxImageHeight)
8989
{
90-
PngCompressionLevel = imageConvertContext.PngCompressionLevel,
90+
// 由于不准备在此步骤保存为文件,所以不需要设置保存等级
91+
//PngCompressionLevel = imageConvertContext.PngCompressionLevel,
92+
9193
ShouldLogToConsole = convertHandler.ShouldLogToConsole ?? false,
9294
ShouldLogToFile = convertHandler.ShouldLogToFile ?? false,
95+
96+
// 不要在优化图片步骤时保存为文件,因为后续可能还会进行转换任务。统一一个逻辑执行保存即可
97+
ShouldSaveToPngFile = false,
9398
};
9499

95100
context.LogMessage($"[Performance] FromJsonText cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
@@ -133,8 +138,7 @@ internal static async Task<ErrorCode> RunAsync(ConvertHandler convertHandler)
133138

134139
return ErrorCode.UnknownError;
135140
}
136-
137-
FileInfo optimizedImageFile = imageFileOptimizationResult.OptimizedImageFile;
141+
138142
var image = imageFileOptimizationResult.Image;
139143

140144
if (image is not null && imageConvertContext.ImageConvertTaskList is { } list)
@@ -148,27 +152,25 @@ internal static async Task<ErrorCode> RunAsync(ConvertHandler convertHandler)
148152

149153
context.LogMessage($"[Performance] RunWorkerProvider cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
150154
stepStopwatch.Restart();
151-
152-
await image.SaveAsPngAsync(convertHandler.OutputFile, new PngEncoder()
153-
{
154-
ColorType = PngColorType.RgbWithAlpha,
155-
BitDepth = PngBitDepth.Bit8,
156-
// 压缩等级 1-9,数值越大压缩率越高但速度越慢,默认值为 6。在 1-9 范围外的值会被视为默认值
157-
CompressionLevel = imageConvertContext.PngCompressionLevel is >= 1 and <= 9
158-
? (PngCompressionLevel)imageConvertContext.PngCompressionLevel
159-
// 范围外,使用默认值
160-
: PngCompressionLevel.DefaultCompression
161-
});
162-
163-
context.LogMessage($"[Performance] SaveAsPngAsync cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
164155
}
165156
else
166157
{
167-
optimizedImageFile.CopyTo(convertHandler.OutputFile, overwrite: true);
168-
169-
context.LogMessage($"[Performance] CopyTo cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
158+
// 没有任何转换任务,啥都不干
170159
}
171160

161+
await image.SaveAsPngAsync(convertHandler.OutputFile, new PngEncoder()
162+
{
163+
ColorType = PngColorType.RgbWithAlpha,
164+
BitDepth = PngBitDepth.Bit8,
165+
// 压缩等级 1-9,数值越大压缩率越高但速度越慢,默认值为 6。在 1-9 范围外的值会被视为默认值
166+
CompressionLevel = imageConvertContext.PngCompressionLevel is >= 1 and <= 9
167+
? (PngCompressionLevel) imageConvertContext.PngCompressionLevel
168+
// 范围外,使用默认值
169+
: PngCompressionLevel.DefaultCompression
170+
});
171+
172+
context.LogMessage($"[Performance] SaveAsPngAsync cost {stepStopwatch.ElapsedMilliseconds}ms Total {totalStopwatch.ElapsedMilliseconds}ms");
173+
172174
totalStopwatch.Stop();
173175
context.LogMessage($"Success converted image. Cost {totalStopwatch.ElapsedMilliseconds}ms. OutputFile:'{convertHandler.OutputFile}'");
174176

0 commit comments

Comments
 (0)