Skip to content

Commit 5aa6215

Browse files
committed
NumSharp.Bitmap: add [SupportedOSPlatform], comprehensive tests, and OpenBugs
Source changes (np_.extensions.cs, NumSharp.Bitmap.csproj): - Add [SupportedOSPlatform("windows")] attribute to np_ class, marking the library as Windows-only since System.Drawing.Common wraps GDI+ which is only available on Windows since .NET 6. This enables CA1416 analyzer warnings for cross-platform consumers. - Update package description to clarify Windows-only + GDI+ dependency. New test coverage (BitmapExtensionsTests.cs — 52 tests): - ToNDArray: dtype, contiguity, size consistency, discardAlpha (copy/no-copy), flat output, pixel data correctness, odd width handling, copy vs no-copy data equality, multiple embedded resources (captcha-a through captcha-d, odd-width). - ToBitmap: 32bpp round-trip, auto format detection (24bpp/32bpp), flat input reshaping, error handling (null, wrong ndim, format mismatch, multiple pictures), size property verification, specific pixel value round-trip fidelity. - Image.ToNDArray: delegation to Bitmap overload. - AsNDArray: BitmapData zero-copy wrapper (shaped, with discardAlpha). - ToBytesPerPixel: all supported pixel formats, unsupported formats, DontCare rejection. - Synthetic images: zeros/ones images at various sizes. OpenBugs.Bitmap.cs (8 bugs, 11 failing tests): Bug 1 (GH #396): ToNDArray(copy:true) computes bytes-per-pixel as stride/width — wrong when GDI+ stride includes 4-byte alignment padding. 3px wide 24bpp: stride=12, stride/width=4 instead of 3. 5px wide: stride=16, 32 bytes copied but reshape expects 30 → IncorrectShapeException. Bug 2 (GH #440): Same root cause on 2px wide 24bpp: stride=8, stride/width=4 instead of 3, producing (1,H,W,4) shape. Bug 3: AsNDArray(flat:true) accesses shape[3] on the 1-d flat array before reshaping to 4-d → IndexOutOfRangeException. Both with and without discardAlpha. Bug 4 (GH #475): ToBitmap on sliced (non-contiguous) NDArray fails in MultiIterator.Assign — flat vector can't broadcast against the sliced 4-d shape → IncorrectShapeException. Bug 5 (GH #491): ToBitmap with non-byte dtype (e.g. int32) throws cryptic InvalidCastException from CopyTo<byte>. No dtype guard or auto-conversion. Bug 6 (GH #440): ToBitmap stride padding logic concatenates a zeros column when stride != width*bpp. This is fundamentally wrong — GDI+ pads rows to 4-byte boundaries by bytes, not by adding pixel columns. The concat inflates nd.size beyond the destination buffer → ArgumentOutOfRangeException. Tested at 1px and 5px wide. Bug 7: 24bpp 2px-wide round-trip produces corrupted pixel data because Bug 2's wrong bpp shifts channel boundaries. SetPixel(1,0)=(40,50,60) recovers as (0,40,50). Bug 8: ToBitmap with unsupported channel count (e.g. 2) lets extractFormatNumber() fall through with format=DontCare, producing an unhelpful GDI+ "Parameter is not valid" error instead of a descriptive ArgumentException. Two architectural root causes explain most bugs: 1. Copy path uses stride/width for bpp instead of deriving from PixelFormat — the no-copy path correctly uses ReshapeFlatData(). 2. ToBitmap tries to handle stride padding by concatenating columns instead of copying row-by-row with per-row padding.
1 parent 587e73d commit 5aa6215

File tree

4 files changed

+1148
-1
lines changed

4 files changed

+1148
-1
lines changed

src/NumSharp.Bitmap/NumSharp.Bitmap.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
66
<Authors>Eli Belash, Haiping Chen, Meinrad Recheis</Authors>
77
<PackageOutputPath>../../packages</PackageOutputPath>
8-
<Description>This package provides extensions for System.Drawing.Bitmap for creating NDArray and Bitmap with or without copying.</Description>
8+
<Description>Windows-only extensions for System.Drawing.Bitmap for creating NDArray and Bitmap with or without copying. Uses GDI+ via System.Drawing.Common.</Description>
99
<PackageProjectUrl>https://github.com/SciSharp</PackageProjectUrl>
1010
<Copyright>2021 © SciSharp STACK Team</Copyright>
1111
<RepositoryUrl>https://github.com/SciSharp/NumSharp</RepositoryUrl>

src/NumSharp.Bitmap/np_.extensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
using System.Diagnostics.CodeAnalysis;
33
using System.Drawing;
44
using System.Drawing.Imaging;
5+
using System.Runtime.Versioning;
56
using NumSharp.Backends;
67
using NumSharp.Backends.Unmanaged;
78

89
// ReSharper disable once CheckNamespace
910
namespace NumSharp
1011
{
12+
[SupportedOSPlatform("windows")]
1113
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")]
1214
[SuppressMessage("ReSharper", "UnusedMember.Global")]
1315
public static class np_

0 commit comments

Comments
 (0)