dotnet / winforms

Windows Forms is a .NET UI framework for building Windows desktop applications.
MIT License
4.42k stars 985 forks source link

Add histogram support to System.Drawing #8837

Open reflectronic opened 4 years ago

reflectronic commented 4 years ago

Background

GDI+ 1.1 added support for retrieving a histogram of the colors present in a bitmap. A handful of formats can be specified, each of which uses at most four channels of data to return the desired histogram(s). For example, the Gray format converts the bitmap to a grayscale image and returns a single histogram based on those grayscale values. However, the PArgb format uses all four channels to return a histogram for the alpha channel and the premultiplied red, green, and blue channels.

This proposal is one of many to add missing GDI+ 1.1 functionality to System.Drawing.

API Proposal

See the documentation for:

namespace System.Drawing.Imaging
{
+   public sealed class Histogram
+   {
!       All formats use at least one channel.
+       public int[] Channel0 { get; }
+       public int[]? Channel1 { get; }
+       public int[]? Channel2 { get; }
+       public int[]? Channel3 { get; }
+   }

+   public enum HistogramFormat
+   {
+       Argb,
!       'P' means premultiplied. It is consistent with other members in this namespace, like PixelFormat.Format32bppPArgb.
+       PArgb,
+       Rgb,
+       Gray,
+       Blue,
+       Green,
+       Red,
+       Alpha
+   }
}

namespace System.Drawing
{
    public sealed class Bitmap : System.Drawing.Image
    {
+       public Histogram GetHistogram(HistogramFormat histogramFormat);
    }
}

This requires changes to libgdiplus in order to support it on non-Windows platforms.

ghost commented 4 years ago

Tagging subscribers to this area: @safern, @tannergooding See info in area-owners.md if you want to be subscribed.

danmoseley commented 4 years ago

We have to decide on what our road map is for System.Drawing and what kind of changes we want to take in it. For new code, libraries like Imagesharp and SkiaSharp may be better choices.

What are your thoughts about why it is important to extend System.Drawing? Who would use this do you expect?

Also, we would want it to work on Unix: does libgdiplus support this?

reflectronic commented 4 years ago

One thing that System.Drawing has (which no other frameworks do) is very close integration with Winforms. Really, this isn't something important to me personally, but e.g. I originally proposed CachedBitmap when someone asked me online how they could utilize it to speed up their drawing performance. Of course, they linked me to the C++ documentation for it, and there wasn't a way to do that from C#.

I know that System.Drawing isn't .NET's finest work 😛 but these are somewhat low-effort changes. GDI+ was rev'd once; it likely won't be updated again. Most of the work on the .NET side is coming up with a decent API shape; wrapping the right APIs is trivial. I promise I'm not some weird GDI+ fan - it's just low-hanging fruit that can likely be briefly completed in one release and never touched again.

Also, we would want it to work on Unix: does libgdiplus support this?

Not at the moment. However, the same was true of CachedBitmap. I implemented that in libgdiplus, and if this is approved, I'll implement this as well.