Skip to content

Commit 189183d

Browse files
committed
Added new project, NumSharp.Bitmap #343
1 parent d6fb821 commit 189183d

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed

NumSharp.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{FB
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeuralNetwork.NumSharp", "Examples\NeuralNetwork.NumSharp\NeuralNetwork.NumSharp.csproj", "{B9253A77-0652-4091-A7F5-14E9FE2630FF}"
1515
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Bitmap", "src\NumSharp.Bitmap\NumSharp.Bitmap.csproj", "{16C45DA5-D006-4229-B457-4F5E36D5DC55}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -53,6 +55,14 @@ Global
5355
{B9253A77-0652-4091-A7F5-14E9FE2630FF}.Release|Any CPU.Build.0 = Release|Any CPU
5456
{B9253A77-0652-4091-A7F5-14E9FE2630FF}.Release|x64.ActiveCfg = Release|x64
5557
{B9253A77-0652-4091-A7F5-14E9FE2630FF}.Release|x64.Build.0 = Release|x64
58+
{16C45DA5-D006-4229-B457-4F5E36D5DC55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59+
{16C45DA5-D006-4229-B457-4F5E36D5DC55}.Debug|Any CPU.Build.0 = Debug|Any CPU
60+
{16C45DA5-D006-4229-B457-4F5E36D5DC55}.Debug|x64.ActiveCfg = Debug|x64
61+
{16C45DA5-D006-4229-B457-4F5E36D5DC55}.Debug|x64.Build.0 = Debug|x64
62+
{16C45DA5-D006-4229-B457-4F5E36D5DC55}.Release|Any CPU.ActiveCfg = Release|Any CPU
63+
{16C45DA5-D006-4229-B457-4F5E36D5DC55}.Release|Any CPU.Build.0 = Release|Any CPU
64+
{16C45DA5-D006-4229-B457-4F5E36D5DC55}.Release|x64.ActiveCfg = Release|x64
65+
{16C45DA5-D006-4229-B457-4F5E36D5DC55}.Release|x64.Build.0 = Release|x64
5666
EndGlobalSection
5767
GlobalSection(SolutionProperties) = preSolution
5868
HideSolutionNode = FALSE
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netstandard2.0</TargetFramework>
4+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
5+
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
6+
<Authors>Eli Belash, Haiping Chen, Meinrad Recheis</Authors>
7+
<PackageOutputPath>../../packages</PackageOutputPath>
8+
<Description>This package provides extensions for System.Drawing.Bitmap for creating NDArray with or without copying.</Description>
9+
<PackageProjectUrl>https://github.com/SciSharp</PackageProjectUrl>
10+
<Copyright>2019 © SciSharp STACK Team</Copyright>
11+
<RepositoryUrl>https://github.com/SciSharp/NumSharp</RepositoryUrl>
12+
<PackageReleaseNotes></PackageReleaseNotes>
13+
<AssemblyVersion>0.20.1.0</AssemblyVersion>
14+
<FileVersion>0.20.1.0</FileVersion>
15+
<RepositoryType>git</RepositoryType>
16+
<PackageTags>Numpy, NumSharp, MachineLearning, Math, Scientific, Numeric, Mathlab, SciSharp</PackageTags>
17+
<PackageLicenseUrl></PackageLicenseUrl>
18+
<LangVersion>7.3</LangVersion>
19+
<PackageIconUrl>https://avatars3.githubusercontent.com/u/44989469?s=200&amp;v=4</PackageIconUrl>
20+
<PackageId>NumSharp.Bitmap</PackageId>
21+
<Product>NumSharp.Bitmap</Product>
22+
<Company>SciSharp STACK</Company>
23+
<RootNamespace>NumSharp.Bitmap</RootNamespace>
24+
<Version>0.20.1</Version>
25+
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
26+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
27+
<SignAssembly>false</SignAssembly>
28+
<AssemblyOriginatorKeyFile>Open.snk</AssemblyOriginatorKeyFile>
29+
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
30+
<Platforms>AnyCPU;x64</Platforms>
31+
<PackageLicenseFile>LICENSE</PackageLicenseFile>
32+
</PropertyGroup>
33+
34+
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net\d'))">
35+
<DefineConstants>NETFRAMEWORK</DefineConstants>
36+
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
37+
</PropertyGroup>
38+
39+
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^netstandard\d'))">
40+
<DefineConstants>NETSTANDARD;CPU_PARALLEL;BIT64</DefineConstants>
41+
</PropertyGroup>
42+
43+
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^netcoreapp\d'))">
44+
<DefineConstants>NETCORE</DefineConstants>
45+
</PropertyGroup>
46+
47+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
48+
<DefineConstants>TRACE;DEBUG</DefineConstants>
49+
<NoWarn>1701;1702;IDE1006;0029</NoWarn>
50+
<DocumentationFile></DocumentationFile>
51+
<PlatformTarget>x64</PlatformTarget>
52+
</PropertyGroup>
53+
54+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
55+
<DefineConstants>TRACE;DEBUG;BIT64</DefineConstants>
56+
<NoWarn>1701;1702;IDE1006;0029</NoWarn>
57+
<DocumentationFile />
58+
<PlatformTarget>x64</PlatformTarget>
59+
</PropertyGroup>
60+
61+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
62+
<DocumentationFile></DocumentationFile>
63+
</PropertyGroup>
64+
65+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
66+
<DocumentationFile />
67+
</PropertyGroup>
68+
69+
<ItemGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^net\d'))">
70+
<PackageReference Include="System.Memory" Version="4.5.1" />
71+
</ItemGroup>
72+
73+
<ItemGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(TargetFramework)', '^netcoreapp\d'))">
74+
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.1" />
75+
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
76+
<PackageReference Include="System.Memory" Version="4.5.1" />
77+
</ItemGroup>
78+
79+
<ItemGroup>
80+
<Compile Remove="NewFolder\**" />
81+
<EmbeddedResource Remove="NewFolder\**" />
82+
<None Remove="NewFolder\**" />
83+
</ItemGroup>
84+
85+
<ItemGroup>
86+
<Compile Remove="Backends\Default\Math\Default.Op.Boolean.template.cs" />
87+
<Compile Remove="Backends\Default\Math\Default.Op.template.cs" />
88+
<Compile Remove="Backends\Default\Math\Templates\Default.Op.Dot.Boolean.template.cs" />
89+
<Compile Remove="Backends\Default\Math\Templates\Default.Op.Dot.template.cs" />
90+
<Compile Remove="Backends\Default\Math\Templates\Default.Op.Equals.template.cs" />
91+
<Compile Remove="Backends\Default\Math\Templates\Default.Op.General.template.cs" />
92+
<Compile Remove="Backends\Iterators\NDIterator.template.cs" />
93+
<Compile Remove="Operations\Elementwise\Templates\Default.Op.Boolean.template.cs" />
94+
<Compile Remove="Operations\Elementwise\Templates\Default.Op.Equals.template.cs" />
95+
<Compile Remove="Operations\NdArray.ElementsWise.cs" />
96+
</ItemGroup>
97+
98+
<ItemGroup>
99+
<None Remove="NumSharp.Bitmap.csproj.DotSettings" />
100+
<None Remove="NumSharp.Image.csproj.DotSettings" />
101+
<None Remove="Open.snk" />
102+
</ItemGroup>
103+
104+
<ItemGroup>
105+
<None Include="..\..\LICENSE">
106+
<Pack>True</Pack>
107+
<PackagePath></PackagePath>
108+
</None>
109+
</ItemGroup>
110+
111+
<Target Name="LogDebugInfo">
112+
<Message Text="Building for $(TargetFramework) on $(OS)" Importance="High" />
113+
</Target>
114+
115+
<ItemGroup>
116+
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
117+
</ItemGroup>
118+
119+
<ItemGroup>
120+
<ProjectReference Include="..\NumSharp.Core\NumSharp.Core.csproj" />
121+
</ItemGroup>
122+
123+
<ItemGroup>
124+
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
125+
</ItemGroup>
126+
127+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=apis/@EntryIndexedValue">True</s:Boolean>
3+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Ccustomdtypes/@EntryIndexedValue">True</s:Boolean>
4+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cdefault/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cdefault_005Callocation/@EntryIndexedValue">True</s:Boolean>
6+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cdefault_005Carraymanipulation/@EntryIndexedValue">True</s:Boolean>
7+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cdefault_005Cblas/@EntryIndexedValue">True</s:Boolean>
8+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cdefault_005Clogic/@EntryIndexedValue">True</s:Boolean>
9+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cdefault_005Cmath/@EntryIndexedValue">True</s:Boolean>
10+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cdefault_005Cmath_005Cblas/@EntryIndexedValue">True</s:Boolean>
11+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cdefault_005Cstatistics/@EntryIndexedValue">True</s:Boolean>
12+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Citerators/@EntryIndexedValue">True</s:Boolean>
13+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Citerators_005Cnditeratorcasts/@EntryIndexedValue">True</s:Boolean>
14+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cunmanaged_005Cinterfaces/@EntryIndexedValue">True</s:Boolean>
15+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cunmanaged_005Citerators/@EntryIndexedValue">True</s:Boolean>
16+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cunmanaged_005Citerators_005Cnditeratorcasts/@EntryIndexedValue">True</s:Boolean>
17+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=backends_005Cunmanaged_005Cmath/@EntryIndexedValue">True</s:Boolean>
18+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=creation/@EntryIndexedValue">True</s:Boolean>
19+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean>
20+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=linearalgebra/@EntryIndexedValue">True</s:Boolean>
21+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=logic/@EntryIndexedValue">True</s:Boolean>
22+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=manipulation/@EntryIndexedValue">True</s:Boolean>
23+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=math/@EntryIndexedValue">True</s:Boolean>
24+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=operations/@EntryIndexedValue">True</s:Boolean>
25+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=operations_005Celementwise/@EntryIndexedValue">True</s:Boolean>
26+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=random/@EntryIndexedValue">True</s:Boolean>
27+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=randomsampling/@EntryIndexedValue">True</s:Boolean>
28+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sorting_005Fsearching_005Fcounting/@EntryIndexedValue">True</s:Boolean>
29+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=statistics/@EntryIndexedValue">True</s:Boolean>
30+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=utilities_005Cincrementors/@EntryIndexedValue">True</s:Boolean>
31+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=view/@EntryIndexedValue">True</s:Boolean>
32+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=view_005Cshape/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/NumSharp.Bitmap/Open.snk

596 Bytes
Binary file not shown.

src/NumSharp.Bitmap/np_.extensions.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Diagnostics.CodeAnalysis;
3+
using System.Drawing;
4+
using System.Drawing.Imaging;
5+
using NumSharp.Backends;
6+
using NumSharp.Backends.Unmanaged;
7+
8+
// ReSharper disable once CheckNamespace
9+
namespace NumSharp
10+
{
11+
[SuppressMessage("ReSharper", "SuggestVarOrType_SimpleTypes")]
12+
public static class np_
13+
{
14+
/// <summary>
15+
/// Creates <see cref="NDArray"/> from given <see cref="Bitmap"/>.
16+
/// </summary>
17+
/// <param name="image">The image to load data from.</param>
18+
/// <param name="flat">
19+
/// If true, returns NDArray be 1-d of pixels: `R1G1B1R2G2B2 ... RnGnBn` where n is the amount of pixels in the image.<br></br>
20+
/// If false, returns a 4-d NDArray shaped: (1, bmpData.Height, bmpData.Width, 3)
21+
/// </param>
22+
/// <param name="copy">
23+
/// If true, performs <see cref="Bitmap.LockBits(System.Drawing.Rectangle,System.Drawing.Imaging.ImageLockMode,System.Drawing.Imaging.PixelFormat)"/>
24+
/// and then copies the data to a new <see cref="NDArray"/> then finally releasing the locked bits.<br></br>
25+
/// If false, It'll call <see cref="Bitmap.LockBits(System.Drawing.Rectangle,System.Drawing.Imaging.ImageLockMode,System.Drawing.Imaging.PixelFormat)"/>
26+
/// , wraps the <see cref="BitmapData.Scan0"/> with an NDArray and call <see cref="Bitmap.UnlockBits"/> only when the NDArray will be collected by the <see cref="GC"/>.
27+
/// </param>
28+
/// <returns>An NDArray that holds the pixel data of the given bitmap</returns>
29+
public static unsafe NDArray ToNDArray(this System.Drawing.Bitmap image, bool flat = false, bool copy = true)
30+
{
31+
if (image == null) throw new ArgumentNullException(nameof(image));
32+
33+
BitmapData bmpData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
34+
if (copy)
35+
try
36+
{
37+
unsafe
38+
{
39+
//Create a 1d vector without filling it's values to zero (similar to np.empty)
40+
var nd = new NDArray(NPTypeCode.Byte, Shape.Vector(bmpData.Stride * image.Height), fillZeros: false);
41+
42+
// Get the respective addresses
43+
byte* src = (byte*)bmpData.Scan0;
44+
byte* dst = (byte*)nd.Unsafe.Address; //we can use unsafe because we just allocated that array and we know for sure it is contagious.
45+
46+
// Copy the RGB values into the array.
47+
Buffer.MemoryCopy(src, dst, nd.size, nd.size); //faster than Marshal.Copy
48+
return nd.reshape(1, image.Height, image.Width, 3);
49+
}
50+
}
51+
finally
52+
{
53+
image.UnlockBits(bmpData);
54+
}
55+
else
56+
{
57+
var nd = new NDArray(new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>((byte*)bmpData.Scan0, bmpData.Stride * bmpData.Height, () => image.UnlockBits(bmpData))));
58+
return flat ? nd : nd.reshape(1, bmpData.Height, bmpData.Width, 3);
59+
}
60+
}
61+
62+
/// <summary>
63+
/// Creates <see cref="NDArray"/> from given <see cref="Image"/>.
64+
/// </summary>
65+
/// <param name="image">The image to load data from.</param>
66+
/// <param name="flat">
67+
/// If true, returns NDArray be 1-d of pixels: `R1G1B1R2G2B2 ... RnGnBn` where n is the amount of pixels in the image.<br></br>
68+
/// If false, returns a 4-d NDArray shaped: (1, bmpData.Height, bmpData.Width, 3)
69+
/// </param>
70+
/// <param name="copy">
71+
/// If true, performs <see cref="Bitmap.LockBits(System.Drawing.Rectangle,System.Drawing.Imaging.ImageLockMode,System.Drawing.Imaging.PixelFormat)"/>
72+
/// and then copies the data to a new <see cref="NDArray"/> then finally releasing the locked bits.<br></br>
73+
/// If false, It'll call <see cref="Bitmap.LockBits(System.Drawing.Rectangle,System.Drawing.Imaging.ImageLockMode,System.Drawing.Imaging.PixelFormat)"/>
74+
/// , wraps the <see cref="BitmapData.Scan0"/> with an NDArray and call <see cref="Bitmap.UnlockBits"/> only when the NDArray will be collected by the <see cref="GC"/>.
75+
/// </param>
76+
/// <returns>An NDArray that holds the pixel data of the given bitmap</returns>
77+
public static NDArray ToNDArray(this Image image, bool flat = false, bool copy = true)
78+
=> ToNDArray(new System.Drawing.Bitmap(image), flat, copy);
79+
80+
/// <summary>
81+
/// Wraps given <see cref="BitmapData"/> as n <see cref="NDArray"/> without performing copy.
82+
/// </summary>
83+
/// <param name="bmpData">Targetted bitmap data with reading capabilities.</param>
84+
/// <param name="flat">
85+
/// If true, returns NDArray be 1-d of pixels: R1G1B1R2G2B2 ... RnGnBn where n is the amount of pixels in the image.<br></br>
86+
/// If false, returns a 4-d NDArray shaped: (1, bmpData.Height, bmpData.Width, 3)
87+
/// </param>
88+
/// <returns>An NDArray that wraps the given bitmap, doesn't copy</returns>
89+
/// <remarks>If the BitmapData is unlocked via <see cref="Bitmap.UnlockBits"/> - the NDArray will point to an invalid address which will cause heap corruption. Use with caution!</remarks>
90+
public static unsafe NDArray AsNDArray(this BitmapData bmpData, bool flat = false)
91+
{
92+
if (bmpData == null)
93+
throw new ArgumentNullException(nameof(bmpData));
94+
95+
var nd = new NDArray(new ArraySlice<byte>(new UnmanagedMemoryBlock<byte>((byte*)bmpData.Scan0, bmpData.Stride * bmpData.Height)));
96+
return flat ? nd : nd.reshape(1, bmpData.Height, bmpData.Width, 3);
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)