|
| 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