When you call Apply() on an
AForge.NET filter that doesn’t support the format of the image you’re manipulating, the ensuing exception doesn’t provide much guidance.
 |
The typical UnsupportedImageFormatException warning. |
This snippet gives a bit more, to kickstart the debugging process. It provides an extension method on AForge.NET filters that you can use before you attempt to apply a filter; if Apply() would fail because of an UnsupportedImageFormatException, the assert will fail, but it will tell you the filter that is going to fail, the unsupported format that you're passing, and the formats that the filter
does support.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| namespace AForge.Imaging.Filters {
/// <summary>
/// Provides utility methods to assist coding against the AForge.NET
/// Framework.
/// </summary>
public static class AForgeUtility {
/// <summary>
/// Makes a debug assertion that an image filter that implements
/// the <see cref="IFilterInformation"/> interface can
/// process an image with the specified <see cref="PixelFormat"/>.
/// </summary>
/// <param name="filterInfo">The filter under consideration.</param>
/// <param name="format">The PixelFormat under consideration.</param>
[Conditional("DEBUG")]
public static void AssertCanApply(
this IFilterInformation filterInfo,
PixelFormat format) {
Debug.Assert(
filterInfo.FormatTranslations.ContainsKey(format),
string.Format("{0} cannot process an image "
+ "with the provided pixel format. Provided "
+ "format: {1}. Accepted formats: {2}.",
filterInfo.GetType().Name,
format.ToString(),
string.Join( ", ", filterInfo.FormatTranslations.Keys)));
}
}
}
|
Call the method after you instantiate the filter and before you apply it, and it may just save you a trip to the documentation.
1
2
3
| BradleyLocalThresholding filter = new BradleyLocalThresholding();
filter.AssertCanApply( image.PixelFormat );
filter.ApplyInPlace( image );
|
 |
The "assertion failed" dialog using my snippet. |
No comments:
Post a Comment