public Composition(int width, int height, ushort bitsPerPixel) { DeviceContext = Gdi32.CreateCompatibleDC(IntPtr.Zero).ThrowWithoutLastErrorAvailableIfInvalid(nameof(Gdi32.CreateCompatibleDC)); bitmap = Gdi32.CreateDIBSection(DeviceContext, new() { bmiHeader = { biSize = Marshal.SizeOf <Gdi32.BITMAPINFOHEADER>(), biWidth = width, biHeight = -height, biPlanes = 1, biBitCount = bitsPerPixel, }, }, Gdi32.DIB.RGB_COLORS, out var compositionPixelDataPointer, hSection: IntPtr.Zero, offset: 0).ThrowLastErrorIfInvalid(); Gdi32.SelectObject(DeviceContext, bitmap).ThrowWithoutLastErrorAvailableIfInvalid(nameof(Gdi32.SelectObject)); unsafe { Pixels = new( (byte *)compositionPixelDataPointer, (uint)width, stride : ((((uint)width * 3) + 3) / 4) * 4, (uint)height); } }
// Referenced http://inis.jinr.ru/sl/vol1/CMC/Graphics_Gems_2,ed_J.Arvo.pdf and // https://github.com/JeremyAnsel/JeremyAnsel.ColorQuant/blob/a025932f7ec361337aaab3057608ed0f71e4e781/JeremyAnsel.ColorQuant/JeremyAnsel.ColorQuant/WuColorQuantizer.cs // to help figure out what was going on. public void Quantize( ColorEnumerable sourceImage, (byte R, byte G, byte B)[] paletteBuffer,