Skip to content

Commit

Permalink
added bool preventOffBoundPixels
Browse files Browse the repository at this point in the history
yay
  • Loading branch information
Szymekk44 committed May 11, 2024
1 parent ef04aeb commit 111b703
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 32 deletions.
86 changes: 65 additions & 21 deletions source/Cosmos.System2/Graphics/Canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,17 +565,32 @@ public virtual void DrawTriangle(Color color, int v1x, int v1y, int v2x, int v2y
/// <param name="image">The image to draw.</param>
/// <param name="x">The origin X coordinate.</param>
/// <param name="y">The origin Y coordinate.</param>
public virtual void DrawImage(Image image, int x, int y)
public virtual void DrawImage(Image image, int x, int y, bool preventOffBoundPixels = true)
{
Color color;
var maxWidth = Math.Min(image.Width, (int)Mode.Width - x);
var maxHeight = Math.Min(image.Height, (int)Mode.Height - y);
for (int xi = 0; xi < maxWidth; xi++)
if (preventOffBoundPixels)
{
for (int yi = 0; yi < maxHeight; yi++)
var maxWidth = Math.Min(image.Width, (int)Mode.Width - x);
var maxHeight = Math.Min(image.Height, (int)Mode.Height - y);
for (int xi = 0; xi < maxWidth; xi++)
{
color = Color.FromArgb(image.RawData[xi + (yi * image.Width)]);
DrawPoint(color, x + xi, y + yi);
for (int yi = 0; yi < maxHeight; yi++)
{
color = Color.FromArgb(image.RawData[xi + (yi * image.Width)]);
DrawPoint(color, x + xi, y + yi);
}
}
}
else
{
var Width = image.Width;
for (int xi = 0; xi < Width; xi++)
{
for (int yi = 0; yi < image.Height; yi++)
{
color = Color.FromArgb(image.RawData[xi + (yi * Width)]);
DrawPoint(color, x + xi, y + yi);
}
}
}
}
Expand Down Expand Up @@ -611,19 +626,33 @@ static int[] ScaleImage(Image image, int newWidth, int newHeight)
/// <param name="y">The Y coordinate.</param>
/// <param name="w">The desired width to scale the image to before drawing.</param>
/// <param name="h">The desired height to scale the image to before drawing</param>
public virtual void DrawImage(Image image, int x, int y, int w, int h)
public virtual void DrawImage(Image image, int x, int y, int w, int h, bool preventOffBoundPixels = true)
{
Color color;

int[] pixels = ScaleImage(image, w, h);
var maxWidth = Math.Min(w, (int)Mode.Width - x);
var maxHeight = Math.Min(h, (int)Mode.Height - y);
for (int xi = 0; xi < maxWidth; xi++)
if (preventOffBoundPixels)
{
var maxWidth = Math.Min(w, (int)Mode.Width - x);
var maxHeight = Math.Min(h, (int)Mode.Height - y);
for (int xi = 0; xi < maxWidth; xi++)
{
for (int yi = 0; yi < maxHeight; yi++)
{
color = Color.FromArgb(pixels[xi + (yi * w)]);
DrawPoint(color, x + xi, y + yi);
}
}
}
else
{
for (int yi = 0; yi < maxHeight; yi++)
for (int xi = 0; xi < w; xi++)
{
color = Color.FromArgb(pixels[xi + (yi * w)]);
DrawPoint(color, x + xi, y + yi);
for (int yi = 0; yi < h; yi++)
{
color = Color.FromArgb(pixels[xi + (yi * w)]);
DrawPoint(color, x + xi, y + yi);
}
}
}
}
Expand All @@ -634,17 +663,32 @@ public virtual void DrawImage(Image image, int x, int y, int w, int h)
/// <param name="image">The image to draw.</param>
/// <param name="x">The X coordinate.</param>
/// <param name="y">The Y coordinate.</param>
public void DrawImageAlpha(Image image, int x, int y)
public void DrawImageAlpha(Image image, int x, int y, bool preventOffBoundPixels = true)
{
Color color;
var maxWidth = Math.Min(image.Width, (int)Mode.Width - x);
var maxHeight = Math.Min(image.Height, (int)Mode.Height - y);
for (int xi = 0; xi < maxWidth; xi++)
if (preventOffBoundPixels)
{
var maxWidth = Math.Min(image.Width, (int)Mode.Width - x);
var maxHeight = Math.Min(image.Height, (int)Mode.Height - y);
for (int xi = 0; xi < maxWidth; xi++)
{
for (int yi = 0; yi < maxHeight; yi++)
{
color = Color.FromArgb(image.RawData[xi + (yi * image.Width)]);
DrawPoint(color, x + xi, y + yi);
}
}
}
else
{
for (int yi = 0; yi < maxHeight; yi++)
var Width = image.Width;
for (int xi = 0; xi < Width; xi++)
{
color = Color.FromArgb(image.RawData[xi + (yi * image.Width)]);
DrawPoint(color, x + xi, y + yi);
for (int yi = 0; yi < image.Height; yi++)
{
color = Color.FromArgb(image.RawData[xi + (yi * Width)]);
DrawPoint(color, x + xi, y + yi);
}
}
}
}
Expand Down
21 changes: 15 additions & 6 deletions source/Cosmos.System2/Graphics/SVGAIICanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,27 @@ public override void DrawChar(char c, Font font, Color color, int x, int y)
}
}

public override void DrawImage(Image image, int x, int y)
public override void DrawImage(Image image, int x, int y, bool preventOffBoundPixels = true)
{
var width = (int)image.Width;
var height = (int)image.Height;
var maxWidth = Math.Min(width, (int)mode.Width - x);
var maxHeight = Math.Min(height, (int)mode.Height - y);
var frameSize = (int)driver.FrameSize;
var data = image.RawData;

for (int i = 0; i < maxHeight; i++)
if(preventOffBoundPixels)
{
driver.videoMemory.Copy(GetPointOffset(x, y + i) + frameSize, data, i * width, maxWidth);
var maxWidth = Math.Min(width, width - x);
var maxHeight = Math.Min(height, height - y);
for (int i = 0; i < maxHeight; i++)
{
driver.videoMemory.Copy(GetPointOffset(x, y + i) + frameSize, data, i * width, maxWidth);
}
}
else
{
for (int i = 0; i < height; i++)
{
driver.videoMemory.Copy(GetPointOffset(x, y + i) + frameSize, data, i * width, width);
}
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions source/Cosmos.System2/Graphics/VBECanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,27 @@ public override void DrawFilledRectangle(Color aColor, int aX, int aY, int aWidt
}
}

public override void DrawImage(Image aImage, int aX, int aY)
public override void DrawImage(Image aImage, int aX, int aY, bool preventOffBoundPixels = true)
{
var xBitmap = aImage.RawData;
var xWidth = (int)aImage.Width;
var xHeight = (int)aImage.Height;
var maxWidth = Math.Min(xWidth, (int)mode.Width - aX);
var maxHeight = Math.Min(xHeight, (int)mode.Height - aY);
int xOffset = aY * (int)Mode.Width + aX;
for (int i = 0; i < maxHeight; i++)
if (preventOffBoundPixels)
{
driver.CopyVRAM((i * (int)Mode.Width) + xOffset, xBitmap, i * xWidth, maxWidth);
var maxWidth = Math.Min(xWidth, (int)mode.Width - aX);
var maxHeight = Math.Min(xHeight, (int)mode.Height - aY);
for (int i = 0; i < maxHeight; i++)
{
driver.CopyVRAM((i * (int)Mode.Width) + xOffset, xBitmap, i * xWidth, maxWidth);
}
}
else
{
for (int i = 0; i < xHeight; i++)
{
driver.CopyVRAM((i * (int)Mode.Width) + xOffset, xBitmap, i * xWidth, xWidth);
}
}

}
Expand Down

0 comments on commit 111b703

Please sign in to comment.