Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working on improving resize #1091

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ public int BitDepth
/// <summary>
/// Did we create the buffer (true) or was it passed in (false)
/// </summary>
readonly bool createdBuffer = true;

bool isDisposed = false;
private readonly bool createdBuffer = true;
private bool isDisposed = false;

/// <summary>
/// Create a new PixelBufferBase object
Expand Down Expand Up @@ -330,7 +329,7 @@ public T Convert<T>()
/// </summary>
/// <typeparam name="T">The buffer type</typeparam>
/// <returns>A new pixel buffer object</returns>
T Clone<T>() where T : PixelBufferBase, new()
private T Clone<T>() where T : PixelBufferBase, new()
{
var newBuffer = new T
{
Expand Down Expand Up @@ -374,6 +373,29 @@ public T Resize<T>(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;
}

/// <summary>
/// Resize the buffer to new dimensions using bilinear interpolation
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,5 +318,57 @@ private int GetShiftCount(uint mask)
}
return count;
}

/// <summary>
/// Creates an Image from the existing with new dimensions and the same color depth
/// </summary>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
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));
}

/// <summary>
/// Creates an Image from the existing with new dimensions and the same color depth
/// </summary>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
/// <param name="newMode"></param>
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());
}
}
}
Loading