From f7a78b30d1a7d7d03da6286c371bd8bb6a6e520c Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Thu, 27 Jun 2019 00:32:15 +0200 Subject: [PATCH] System.Drawing: Consolidate BitmapData class (dotnet/corefx#38838) * Add shared BitmapData class * Consolidate BitmapData class * PR feedback Commit migrated from https://github.com/dotnet/corefx/commit/e78cb04d339aa9206b6db8eb8224904ecc4529e1 --- .../src/System.Drawing.Common.csproj | 1 + .../src/System/Drawing/Imaging/BitmapData.Unix.cs | 93 ++------------------- .../System/Drawing/Imaging/BitmapData.Windows.cs | 92 +-------------------- .../src/System/Drawing/Imaging/BitmapData.cs | 96 ++++++++++++++++++++++ .../tests/Imaging/BitmapDataTests.cs | 1 - 5 files changed, 106 insertions(+), 177 deletions(-) create mode 100644 src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.cs diff --git a/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj b/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj index bf6b058..ed383d8 100644 --- a/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj +++ b/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj @@ -121,6 +121,7 @@ + diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.Unix.cs index e7d9b2e..2b6e3ed 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.Unix.cs @@ -37,14 +37,14 @@ namespace System.Drawing.Imaging // MUST BE KEPT IN SYNC WITH gdip.h in libgdiplus! // The first 6 fields MUST also match MS definition [StructLayout(LayoutKind.Sequential)] - public sealed class BitmapData + public sealed partial class BitmapData { - private int width; - private int height; - private int stride; - private PixelFormat pixel_format; // int - private IntPtr scan0; - private int reserved; + private int _width; + private int _height; + private int _stride; + private PixelFormat _pixelFormat; + private IntPtr _scan0; + private int _reserved; #pragma warning disable 169 // *** Warning *** don't depend on those fields in managed // code as they won't exists when using MS @@ -62,84 +62,5 @@ namespace System.Drawing.Imaging private int transparent; // *** Warning *** #pragma warning restore 169 - - public int Height - { - get - { - return height; - } - - set - { - height = value; - } - } - - public int Width - { - get - { - return width; - } - - set - { - width = value; - } - } - - public PixelFormat PixelFormat - { - get - { - - return pixel_format; - } - - set - { - pixel_format = value; - } - } - - public int Reserved - { - get - { - return reserved; - } - - set - { - reserved = value; - } - } - - public IntPtr Scan0 - { - get - { - return scan0; - } - - set - { - scan0 = value; - } - } - - public int Stride - { - get - { - return stride; - } - - set - { - stride = value; - } - } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.Windows.cs index 89df63d..498d7df 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.Windows.cs @@ -10,101 +10,13 @@ namespace System.Drawing.Imaging /// Specifies the attributes of a bitmap image. /// [StructLayout(LayoutKind.Sequential)] - public sealed class BitmapData + public sealed partial class BitmapData { private int _width; private int _height; private int _stride; - private int _pixelFormat; + private PixelFormat _pixelFormat; private IntPtr _scan0; private int _reserved; - - /// - /// Specifies the pixel width of the . - /// - public int Width - { - get { return _width; } - set { _width = value; } - } - - /// - /// Specifies the pixel height of the . - /// - public int Height - { - get { return _height; } - set { _height = value; } - } - - /// - /// Specifies the stride width of the . - /// - public int Stride - { - get { return _stride; } - set { _stride = value; } - } - - /// - /// Specifies the format of the pixel information in this . - /// - public PixelFormat PixelFormat - { - get { return (PixelFormat)_pixelFormat; } - set - { - switch (value) - { - case PixelFormat.DontCare: - // case PixelFormat.Undefined: same as DontCare - case PixelFormat.Max: - case PixelFormat.Indexed: - case PixelFormat.Gdi: - case PixelFormat.Format16bppRgb555: - case PixelFormat.Format16bppRgb565: - case PixelFormat.Format24bppRgb: - case PixelFormat.Format32bppRgb: - case PixelFormat.Format1bppIndexed: - case PixelFormat.Format4bppIndexed: - case PixelFormat.Format8bppIndexed: - case PixelFormat.Alpha: - case PixelFormat.Format16bppArgb1555: - case PixelFormat.PAlpha: - case PixelFormat.Format32bppPArgb: - case PixelFormat.Extended: - case PixelFormat.Format16bppGrayScale: - case PixelFormat.Format48bppRgb: - case PixelFormat.Format64bppPArgb: - case PixelFormat.Canonical: - case PixelFormat.Format32bppArgb: - case PixelFormat.Format64bppArgb: - break; - default: - throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), unchecked((int)value), typeof(PixelFormat)); - } - - - _pixelFormat = (int)value; - } - } - - /// - /// Specifies the address of the pixel data. - /// - public IntPtr Scan0 - { - get { return _scan0; } - set { _scan0 = value; } - } - - /// - /// Reserved. Do not use. - /// - public int Reserved - { - get { return _reserved; } - set { _reserved = value; } - } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.cs new file mode 100644 index 0000000..511f9dd --- /dev/null +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/BitmapData.cs @@ -0,0 +1,96 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace System.Drawing.Imaging +{ + public partial class BitmapData + { + /// + /// Specifies the pixel width of the . + /// + public int Width + { + get { return _width; } + set { _width = value; } + } + + /// + /// Specifies the pixel height of the . + /// + public int Height + { + get { return _height; } + set { _height = value; } + } + + /// + /// Specifies the stride width of the . + /// + public int Stride + { + get { return _stride; } + set { _stride = value; } + } + + /// + /// Specifies the format of the pixel information in this . + /// + public PixelFormat PixelFormat + { + get { return _pixelFormat; } + set + { + switch (value) + { + case PixelFormat.DontCare: + // case PixelFormat.Undefined: same as DontCare + case PixelFormat.Max: + case PixelFormat.Indexed: + case PixelFormat.Gdi: + case PixelFormat.Format16bppRgb555: + case PixelFormat.Format16bppRgb565: + case PixelFormat.Format24bppRgb: + case PixelFormat.Format32bppRgb: + case PixelFormat.Format1bppIndexed: + case PixelFormat.Format4bppIndexed: + case PixelFormat.Format8bppIndexed: + case PixelFormat.Alpha: + case PixelFormat.Format16bppArgb1555: + case PixelFormat.PAlpha: + case PixelFormat.Format32bppPArgb: + case PixelFormat.Extended: + case PixelFormat.Format16bppGrayScale: + case PixelFormat.Format48bppRgb: + case PixelFormat.Format64bppPArgb: + case PixelFormat.Canonical: + case PixelFormat.Format32bppArgb: + case PixelFormat.Format64bppArgb: + break; + default: + throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), unchecked((int)value), typeof(PixelFormat)); + } + + _pixelFormat = value; + } + } + + /// + /// Specifies the address of the pixel data. + /// + public IntPtr Scan0 + { + get { return _scan0; } + set { _scan0 = value; } + } + + /// + /// Reserved. Do not use. + /// + public int Reserved + { + get { return _reserved; } + set { _reserved = value; } + } + } +} diff --git a/src/libraries/System.Drawing.Common/tests/Imaging/BitmapDataTests.cs b/src/libraries/System.Drawing.Common/tests/Imaging/BitmapDataTests.cs index 8b915f8..494ba26 100644 --- a/src/libraries/System.Drawing.Common/tests/Imaging/BitmapDataTests.cs +++ b/src/libraries/System.Drawing.Common/tests/Imaging/BitmapDataTests.cs @@ -106,7 +106,6 @@ namespace System.Drawing.Imaging.Tests Assert.Equal(pixelFormat, bd.PixelFormat); } - [ActiveIssue(20884, TestPlatforms.AnyUnix)] [ConditionalFact(Helpers.IsDrawingSupported)] public void PixelFormat_SetInvalid_ThrowsInvalidEnumException() { -- 2.7.4