/// <summary> /// Creates a native <see cref="IBitmapDataInternal"/> by a quantizer session re-using its palette if possible. /// </summary> internal static IBitmapDataInternal CreateBitmapData(Bitmap bitmap, ImageLockMode lockMode, IQuantizingSession quantizingSession) { if (bitmap == null) { throw new ArgumentNullException(nameof(bitmap), PublicResources.ArgumentNull); } var pixelFormat = bitmap.PixelFormat; if (!pixelFormat.IsIndexed() || quantizingSession.Palette == null) { return(CreateBitmapData(bitmap, lockMode, quantizingSession.BackColor, quantizingSession.AlphaThreshold)); } // checking if bitmap and quantizer palette has the same entries if (!quantizingSession.Palette.Equals(bitmap.Palette.Entries)) { return(CreateBitmapData(bitmap, lockMode, quantizingSession.BackColor, quantizingSession.AlphaThreshold)); } if (!lockMode.IsDefined()) { throw new ArgumentOutOfRangeException(nameof(lockMode), PublicResources.EnumOutOfRange(lockMode)); } // here the quantizer and the target bitmap uses the same palette switch (pixelFormat) { case PixelFormat.Format8bppIndexed: return(new NativeBitmapData <NativeBitmapDataRow8I>(bitmap, pixelFormat, lockMode, quantizingSession)); case PixelFormat.Format4bppIndexed: return(new NativeBitmapData <NativeBitmapDataRow4I>(bitmap, pixelFormat, lockMode, quantizingSession)); case PixelFormat.Format1bppIndexed: return(new NativeBitmapData <NativeBitmapDataRow1I>(bitmap, pixelFormat, lockMode, quantizingSession)); default: throw new InvalidOperationException(Res.InternalError($"Unexpected indexed format: {pixelFormat}")); } }
/// <summary> /// Creates a native <see cref="IBitmapDataInternal"/> from a <see cref="Bitmap"/>. /// </summary> internal static IBitmapDataInternal CreateBitmapData(Bitmap bitmap, ImageLockMode lockMode, Color32 backColor = default, byte alphaThreshold = 128, Palette?palette = null) { if (bitmap == null) { throw new ArgumentNullException(nameof(bitmap), PublicResources.ArgumentNull); } if (!lockMode.IsDefined()) { throw new ArgumentOutOfRangeException(nameof(lockMode), PublicResources.EnumOutOfRange(lockMode)); } Debug.Assert(palette == null || backColor.ToOpaque() == palette.BackColor && alphaThreshold == palette.AlphaThreshold); var pixelFormat = bitmap.PixelFormat; switch (pixelFormat) { case PixelFormat.Format32bppArgb: return(new NativeBitmapData <NativeBitmapDataRow32Argb>(bitmap, pixelFormat, lockMode)); case PixelFormat.Format32bppPArgb: return(new NativeBitmapData <NativeBitmapDataRow32PArgb>(bitmap, pixelFormat, lockMode)); case PixelFormat.Format32bppRgb: return(new NativeBitmapData <NativeBitmapDataRow32Rgb>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold)); case PixelFormat.Format24bppRgb: return(new NativeBitmapData <NativeBitmapDataRow24Rgb>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold)); case PixelFormat.Format8bppIndexed: return(new NativeBitmapData <NativeBitmapDataRow8I>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold, palette)); case PixelFormat.Format4bppIndexed: return(new NativeBitmapData <NativeBitmapDataRow4I>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold, palette)); case PixelFormat.Format1bppIndexed: return(new NativeBitmapData <NativeBitmapDataRow1I>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold, palette)); case PixelFormat.Format64bppArgb: return(new NativeBitmapData <NativeBitmapDataRow64Argb>(bitmap, pixelFormat, lockMode)); case PixelFormat.Format64bppPArgb: return(new NativeBitmapData <NativeBitmapDataRow64PArgb>(bitmap, pixelFormat, lockMode)); case PixelFormat.Format48bppRgb: return(new NativeBitmapData <NativeBitmapDataRow48Rgb>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold)); case PixelFormat.Format16bppRgb565: return(OSUtils.IsWindows ? new NativeBitmapData <NativeBitmapDataRow16Rgb565>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold) : new NativeBitmapData <NativeBitmapDataRow16Rgb565Via24Bpp>(bitmap, PixelFormat.Format24bppRgb, lockMode, backColor, alphaThreshold)); case PixelFormat.Format16bppRgb555: return(OSUtils.IsWindows ? new NativeBitmapData <NativeBitmapDataRow16Rgb555>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold) : new NativeBitmapData <NativeBitmapDataRow16Rgb555Via24Bpp>(bitmap, PixelFormat.Format24bppRgb, lockMode, backColor, alphaThreshold)); case PixelFormat.Format16bppArgb1555: return(new NativeBitmapData <NativeBitmapDataRow16Argb1555>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold)); case PixelFormat.Format16bppGrayScale: return(new NativeBitmapData <NativeBitmapDataRow16Gray>(bitmap, pixelFormat, lockMode, backColor, alphaThreshold)); default: throw new InvalidOperationException(Res.InternalError($"Unexpected pixel format {pixelFormat}")); } }