Skip to content

Commit 3d38409

Browse files
committed
Fixed CropWhitespacesAssetAction
1 parent 1bcfd8d commit 3d38409

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed

src/DragonFly.Assets.FFMpeg/VideoProcessing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public bool CanUse(string mimeType)
2424

2525
public async Task<bool> OnAssetChangedAsync(IAssetProcessingContext context)
2626
{
27-
using Stream stream = await context.OpenAssetStreamAsync();
27+
using Stream stream = await context.OpenAssetStreamAsync().ConfigureAwait(false);
2828

2929
IMediaAnalysis mediaInfo = FFProbe.Analyse(stream);
3030

@@ -56,7 +56,7 @@ public async Task<bool> OnAssetChangedAsync(IAssetProcessingContext context)
5656
};
5757
}
5858

59-
await context.SetMetadataAsync(metadata);
59+
await context.SetMetadataAsync(metadata).ConfigureAwait(false);
6060

6161
return true;
6262
}

src/DragonFly.Assets.ImageSharp/Actions/CropWhitespacesAssetAction.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public async Task<bool> ProcessAsync(IAssetActionContext context)
2525
}
2626

2727
int top = 0;
28-
int bottom = image.Height - 1;
29-
int left = image.Width - 1;
28+
int bottom = 0;
29+
int left = 0;
3030
int right = 0;
3131

3232
image.ProcessPixelRows(p =>
@@ -49,7 +49,7 @@ public async Task<bool> ProcessAsync(IAssetActionContext context)
4949
}
5050

5151
//bottom
52-
for (int i = image.Height - 1; i >= 0; i--)
52+
for (int i = image.Height - 1; i > top; i--)
5353
{
5454
Span<Rgb24> row = p.GetRowSpan(i);
5555

@@ -62,39 +62,61 @@ public async Task<bool> ProcessAsync(IAssetActionContext context)
6262
}
6363
}
6464

65+
bool foundSide = false;
66+
6567
//sides
66-
for (int i = top; i <= bottom; i++)
68+
for (int i = top; i < image.Height - bottom; i++)
6769
{
6870
Span<Rgb24> row = p.GetRowSpan(i);
6971

7072
//left
7173
int found = row.IndexOfAnyExcept(whitePixel);
7274

73-
if (found != -1)
75+
if (found == -1)
76+
{
77+
continue;
78+
}
79+
80+
if (foundSide)
7481
{
7582
left = Math.Min(left, found);
83+
}
84+
else
85+
{
86+
left = found;
87+
}
7688

77-
//right
78-
found = row.LastIndexOfAnyExcept(whitePixel);
89+
//right
90+
found = row.LastIndexOfAnyExcept(whitePixel);
7991

80-
if (found != -1)
81-
{
82-
found = image.Width - found - 1;
92+
if (found == -1)
93+
{
94+
throw new Exception();
95+
}
8396

84-
right = Math.Max(right, found);
85-
}
97+
found = image.Width - found - 1;
98+
99+
if (foundSide)
100+
{
101+
right = Math.Min(right, found);
86102
}
103+
else
104+
{
105+
right = found;
106+
}
107+
108+
foundSide = true;
87109
}
88110
});
89111

90-
if (top == 0 && bottom == image.Height - 1 && left == image.Width -1 && right == 0)
112+
if (top == 0 && bottom == 0 && left == 0 && right == 0)
91113
{
92-
return false; //no whitespace found
114+
return false; //no whitespaces found
93115
}
94116

95-
if (top > bottom)
117+
if (top == image.Height)
96118
{
97-
return false; //white image
119+
return false;
98120
}
99121

100122
image.Mutate(x => x.Crop(new Rectangle(left, top, image.Width - left - right, image.Height - bottom - top)));

src/DragonFly.Assets.ImageSharp/Extensions/DragonFlyBuilderExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public static class DragonFlyBuilderExtensions
1717
public static IDragonFlyBuilder AddImageMetadata(this IDragonFlyBuilder builder)
1818
{
1919
builder.Services.AddTransient<IAssetProcessing, ImageProcessing>();
20-
2120
builder.Services.AddTransient<CropWhitespacesAssetAction>();
2221

2322
AssetActionManager.Default.Add<CropWhitespacesAssetAction>("CropWhiteSpaces", MimeTypes.Images);

src/DragonFly.Assets.ImageSharp/ImageProcessing.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public bool CanUse(string mimeType)
2626

2727
public async Task<bool> OnAssetChangedAsync(IAssetProcessingContext context)
2828
{
29-
using Stream stream = await context.OpenAssetStreamAsync();
29+
using Stream stream = await context.OpenAssetStreamAsync().ConfigureAwait(false);
3030

31-
ImageInfo imageInfo = await Image.IdentifyAsync(stream);
31+
ImageInfo imageInfo = await Image.IdentifyAsync(stream).ConfigureAwait(false);
3232

3333
if (imageInfo != null)
3434
{
@@ -37,7 +37,7 @@ public async Task<bool> OnAssetChangedAsync(IAssetProcessingContext context)
3737
Height = imageInfo.Height,
3838
BitsPerPixel = imageInfo.PixelType.BitsPerPixel };
3939

40-
await context.SetMetadataAsync(metadata);
40+
await context.SetMetadataAsync(metadata).ConfigureAwait(false);
4141

4242
return true;
4343
}

src/DragonFly.Assets.Pdf/PdfProcessing.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task<bool> OnAssetChangedAsync(IAssetProcessingContext context)
2121
{
2222
MemoryStream mem = new MemoryStream();
2323

24-
using (Stream stream = await context.OpenAssetStreamAsync())
24+
using (Stream stream = await context.OpenAssetStreamAsync().ConfigureAwait(false))
2525
{
2626
await stream.CopyToAsync(mem);
2727
}
@@ -37,7 +37,7 @@ public async Task<bool> OnAssetChangedAsync(IAssetProcessingContext context)
3737
metadata.PdfVersion = document.Version.ToString(CultureInfo.InvariantCulture);
3838
}
3939

40-
await context.SetMetadataAsync(metadata);
40+
await context.SetMetadataAsync(metadata).ConfigureAwait(false);
4141

4242
return true;
4343
}

0 commit comments

Comments
 (0)