|
10 | 10 | using System.Text; |
11 | 11 | using System.Threading.Tasks; |
12 | 12 | using DotNetCampus.MediaConverters.Imaging.Effect.Extensions; |
| 13 | +using SixLabors.ImageSharp.Advanced; |
13 | 14 | using SixLabors.ImageSharp.Processing; |
14 | 15 |
|
15 | 16 | namespace DotNetCampus.MediaConverters.Imaging.Effect; |
@@ -39,36 +40,33 @@ public static void ReplaceColor(this Image<Rgba32> bitmap, ColorMetadata sourceC |
39 | 40 | /// <param name="targetColor">目标颜色</param> |
40 | 41 | public static void ReplaceColor(this Image<Rgba32> image, Rgba32 sourceColor, Rgba32 targetColor) |
41 | 42 | { |
42 | | - var sourceVector4 = sourceColor.ToVector4(); |
43 | | - var targetVector4 = targetColor.ToVector4(); |
44 | | - |
45 | 43 | var sourceMetadata = new ColorMetadata(sourceColor); |
46 | 44 |
|
47 | | - image.Mutate(context => |
| 45 | + Parallel.For(0, image.Height, rowIndex => |
48 | 46 | { |
49 | | - context.ProcessPixelRowsAsVector4((Span<Vector4> row) => |
| 47 | + Memory<Rgba32> rowMemory = image.DangerousGetPixelRowMemory(rowIndex); |
| 48 | + |
| 49 | + var span = rowMemory.Span; |
| 50 | + |
| 51 | + for (int colIndex = 0; colIndex < span.Length; colIndex++) |
50 | 52 | { |
51 | | - for (int i = 0; i < row.Length; i++) |
| 53 | + //获取颜色 |
| 54 | + Rgba32 pixel = span[colIndex]; |
| 55 | + // 快速分支 |
| 56 | + if (pixel.Equals(sourceColor)) |
52 | 57 | { |
53 | | - var current = row[i]; |
54 | | - if (current.Equals(sourceVector4)) |
55 | | - { |
56 | | - // 快速分支 |
57 | | - row[i] = targetVector4; |
58 | | - } |
59 | | - else |
| 58 | + span[colIndex] = targetColor; |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + var color = new ColorMetadata(pixel); |
| 63 | + //处理颜色 |
| 64 | + if (color.IsNearlyEquals(sourceMetadata)) |
60 | 65 | { |
61 | | - Rgba32 pixel = default; |
62 | | - pixel.FromVector4(current); |
63 | | - |
64 | | - var currentMetadata = new ColorMetadata(pixel); |
65 | | - if (currentMetadata.IsNearlyEquals(sourceMetadata)) |
66 | | - { |
67 | | - row[i] = targetVector4; |
68 | | - } |
| 66 | + span[colIndex] = targetColor; |
69 | 67 | } |
70 | 68 | } |
71 | | - }); |
| 69 | + } |
72 | 70 | }); |
73 | 71 | } |
74 | 72 |
|
|
0 commit comments