diff --git a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroGraphics/Driver/Buffers/PixelBufferBase.cs b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroGraphics/Driver/Buffers/PixelBufferBase.cs index 9424aef08..7b36d9cf6 100644 --- a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroGraphics/Driver/Buffers/PixelBufferBase.cs +++ b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroGraphics/Driver/Buffers/PixelBufferBase.cs @@ -66,9 +66,8 @@ public int BitDepth /// /// Did we create the buffer (true) or was it passed in (false) /// - readonly bool createdBuffer = true; - - bool isDisposed = false; + private readonly bool createdBuffer = true; + private bool isDisposed = false; /// /// Create a new PixelBufferBase object @@ -330,7 +329,7 @@ public T Convert() /// /// The buffer type /// A new pixel buffer object - T Clone() where T : PixelBufferBase, new() + private T Clone() where T : PixelBufferBase, new() { var newBuffer = new T { @@ -374,6 +373,29 @@ public T Resize(int newWidth, int newHeight) return newBuffer; } + internal PixelBufferBase Resize(Type bufferType, int newWidth, int newHeight) + { + var newBuffer = Activator.CreateInstance(bufferType) as PixelBufferBase; + if (newBuffer == null) throw new Exception(); + newBuffer.Width = newWidth; + newBuffer.Height = newHeight; + newBuffer.InitializeBuffer(true); + + float xRatio = (float)Width / newWidth; + float yRatio = (float)Height / newHeight; + + for (int i = 0; i < newWidth; i++) + { + for (int j = 0; j < newHeight; j++) + { + int srcX = (int)(i * xRatio); + int srcY = (int)(j * yRatio); + newBuffer.SetPixel(i, j, GetPixel(srcX, srcY)); + } + } + return newBuffer; + } + /// /// Resize the buffer to new dimensions using bilinear interpolation /// diff --git a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroGraphics/Driver/Image.cs b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroGraphics/Driver/Image.cs index 4e466f701..1a6d70ae6 100644 --- a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroGraphics/Driver/Image.cs +++ b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroGraphics/Driver/Image.cs @@ -318,5 +318,57 @@ private int GetShiftCount(uint mask) } return count; } + + /// + /// Creates an Image from the existing with new dimensions and the same color depth + /// + /// + /// + public Image Resize(int newWidth, int newHeight) + { + if (DisplayBuffer == null) + { + throw new InvalidOperationException("PixelBuffer is null"); + } + + var bufferBase = DisplayBuffer as PixelBufferBase; + + if (bufferBase == null) + { + throw new InvalidOperationException("PixelBuffer must be a PixelBufferBase to resize"); + } + + return LoadFromPixelData(bufferBase.Resize(this.DisplayBuffer.GetType(), newWidth, newHeight)); + } + + /// + /// Creates an Image from the existing with new dimensions and the same color depth + /// + /// + /// + /// + public Image ConvertAndResize(ColorMode newMode, int newWidth, int newHeight) + { + var bufferType = newMode switch + { + ColorMode.Format1bpp => typeof(Buffer1bpp), + ColorMode.Format2bpp => throw new NotSupportedException(), + ColorMode.Format4bppGray => typeof(BufferGray4), + ColorMode.Format4bppIndexed => typeof(BufferIndexed4), + ColorMode.Format8bppGray => typeof(BufferGray8), + ColorMode.Format8bppRgb332 => typeof(BufferRgb332), + ColorMode.Format12bppRgb444 => typeof(BufferRgb444), + ColorMode.Format16bppRgb555 => throw new NotSupportedException(), + ColorMode.Format16bppRgb565 => typeof(BufferRgb565), + ColorMode.Format18bppRgb666 => typeof(BufferRgb666), + ColorMode.Format24bppRgb888 => typeof(BufferRgb888), + ColorMode.Format24bppGrb888 => throw new NotSupportedException(), + ColorMode.Format32bppRgba8888 => typeof(BufferRgba8888), + _ => throw new NotImplementedException($"Unknown or unsupported ColorMode: {newMode}"), + }; + + return LoadFromPixelData((DisplayBuffer as PixelBufferBase)?.Resize(bufferType, newWidth, newHeight) + ?? throw new NotSupportedException()); + } } } \ No newline at end of file