Skip to content

Commit

Permalink
Update bitmap conversion for API review (dotnet#11101)
Browse files Browse the repository at this point in the history
This updates bitmap conversion and fixed color palette creation to match the final API approval and removes the preview flags.

Fixes dotnet#8827
  • Loading branch information
JeremyKuhne authored Mar 21, 2024
1 parent 4b82e91 commit 0e896ce
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
3 changes: 0 additions & 3 deletions src/System.Drawing.Common/src/System/Drawing/Bitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Drawing.Imaging;
#if NET9_0_OR_GREATER
using System.Drawing.Imaging.Effects;
using System.Runtime.Versioning;
#endif
using System.IO;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -440,7 +439,6 @@ public void ApplyEffect(Effect effect, Rectangle area = default)
/// is complete.
/// </para>
/// </remarks>
[RequiresPreviewFeatures]
public void ConvertFormat(
PixelFormat format,
DitherType ditherType,
Expand Down Expand Up @@ -485,7 +483,6 @@ public void ConvertFormat(
/// The new pixel format. <see cref="PixelFormat.Format16bppGrayScale"/> is not supported.
/// </para>
/// </param>
[RequiresPreviewFeatures]
public void ConvertFormat(PixelFormat format)
{
PixelFormat currentFormat = PixelFormat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if NET9_0_OR_GREATER
using System.Runtime.Versioning;
#endif

namespace System.Drawing.Imaging;

/// <summary>
Expand Down Expand Up @@ -38,17 +34,17 @@ private ColorPalette(int flags, Color[] entries)
/// <summary>
/// Create a custom color palette.
/// </summary>
/// <param name="entries">Color entries for the palette.</param>
public ColorPalette(params Color[] entries) : this(0, entries)
/// <param name="customColors">Color entries for the palette.</param>
public ColorPalette(params Color[] customColors) : this(0, customColors)
{
}

/// <summary>
/// Create a standard color palette.
/// </summary>
public ColorPalette(PaletteType paletteType)
public ColorPalette(PaletteType fixedPaletteType)
{
ColorPalette palette = InitializePalette(paletteType, 0, useTransparentColor: false, bitmap: null);
ColorPalette palette = InitializePalette(fixedPaletteType, 0, useTransparentColor: false, bitmap: null);
_flags = palette.Flags;
_entries = palette.Entries;
}
Expand All @@ -57,9 +53,8 @@ public ColorPalette(PaletteType paletteType)
/// Create an optimal color palette based on the colors in a given bitmap.
/// </summary>
/// <inheritdoc cref="InitializePalette(PaletteType, int, bool, IPointer{GpBitmap}?)"/>
[RequiresPreviewFeatures]
public static ColorPalette CreateOptimalPalette(int colorCount, bool useTransparentColor, Bitmap bitmap) =>
InitializePalette((PaletteType)GdiPlus.PaletteType.PaletteTypeOptimal, colorCount, useTransparentColor, bitmap);
public static ColorPalette CreateOptimalPalette(int colors, bool useTransparentColor, Bitmap bitmap) =>
InitializePalette((PaletteType)GdiPlus.PaletteType.PaletteTypeOptimal, colors, useTransparentColor, bitmap);
#endif

// Memory layout is:
Expand Down Expand Up @@ -91,13 +86,13 @@ internal BufferScope<uint> ConvertToBuffer()
/// <summary>
/// Initializes a standard, optimal, or custom color palette.
/// </summary>
/// <param name="paletteType">The palette type.</param>
/// <param name="fixedPaletteType">The palette type.</param>
/// <param name="colorCount">
/// The number of colors you want to have in an optimal palette based on a the specified bitmap.
/// </param>
/// <param name="useTransparentColor"><see langword="true"/> to include the transparent color in the palette.</param>
internal static ColorPalette InitializePalette(
PaletteType paletteType,
PaletteType fixedPaletteType,
int colorCount,
bool useTransparentColor,
IPointer<GpBitmap>? bitmap)
Expand All @@ -109,7 +104,7 @@ internal static ColorPalette InitializePalette(
{
PInvoke.GdipInitializePalette(
(GdiPlus.ColorPalette*)b,
(GdiPlus.PaletteType)paletteType,
(GdiPlus.PaletteType)fixedPaletteType,
colorCount,
useTransparentColor,
bitmap is null ? null : bitmap.Pointer).ThrowIfFailed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum PaletteType
/// <summary>
/// A palette that has two colors. This palette type is suitable for bitmaps that store 1 bit per pixel.
/// </summary>
FixedBW = GdiPlus.PaletteType.PaletteTypeFixedBW,
FixedBlackAndWhite = GdiPlus.PaletteType.PaletteTypeFixedBW,

/// <summary>
/// A palette based on two intensities each (off or full) for the red, green, and blue channels. Also contains the
Expand Down
4 changes: 2 additions & 2 deletions src/System.Drawing.Common/tests/System/Drawing/BitmapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,8 +1638,8 @@ public void SaveToRestrictiveStream(bool canRead, bool canSeek)
// Format16bppGrayScale is not supported for conversion
{ PixelFormat.Format16bppGrayScale, DitherType.None, PaletteType.FixedHalftone256 },
{ PixelFormat.Format16bppGrayScale, DitherType.ErrorDiffusion, PaletteType.FixedHalftone8 },
{ PixelFormat.Format16bppGrayScale, DitherType.None, PaletteType.FixedBW },
{ PixelFormat.Format16bppGrayScale, DitherType.Solid, PaletteType.FixedBW },
{ PixelFormat.Format16bppGrayScale, DitherType.None, PaletteType.FixedBlackAndWhite },
{ PixelFormat.Format16bppGrayScale, DitherType.Solid, PaletteType.FixedBlackAndWhite },
{ PixelFormat.Format16bppRgb565, (DitherType)(-1), PaletteType.FixedHalftone256 },
{ PixelFormat.Format16bppRgb565, (DitherType)(-1), (PaletteType)(-1) },
};
Expand Down

0 comments on commit 0e896ce

Please sign in to comment.