Skip to content

Commit

Permalink
Merge pull request CosmosOS#1529 from KM198912/patch-1
Browse files Browse the repository at this point in the history
Implement Bitmap Scaling
  • Loading branch information
quajak authored Oct 20, 2020
2 parents cff35bf + 9435168 commit 1e4cbb6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Tests/Kernels/GraphicTest/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ private void DoTest(Canvas aCanvas)

aCanvas.DrawImage(bitmap, new Point(0, 0));
aCanvas.DrawImage(bitmap2, new Point(200, 0));

//Scale Bitmap
aCanvas.DrawImage(bitmap,0,0,50,50);

aCanvas.DrawImageAlpha(bitmap3, new Point(0, 300));

/* Drawing ellipses */
Expand Down
43 changes: 42 additions & 1 deletion source/Cosmos.System2/Graphics/Canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,48 @@ public virtual void DrawImage(Image image, int x, int y)
}
}
}


private int[] scaleImage(Image image, int newWidth, int newHeight)
{
int[] pixels = image.rawData;
int w1 = (int)image.Width;
int h1 = (int)image.Height;
int[] temp = new int[newWidth * newHeight];
int x_ratio = (int)((w1 << 16) / newWidth) + 1;
int y_ratio = (int)((h1 << 16) / newHeight) + 1;
int x2, y2;
for (int i = 0; i < newHeight; i++)
{
for (int j = 0; j < newWidth; j++)
{
x2 = ((j * x_ratio) >> 16);
y2 = ((i * y_ratio) >> 16);
temp[(i * newWidth) + j] = pixels[(y2 * w1) + x2];
}
}
return temp;
}
/// <summary>
/// Draw a Scaled Bitmap.
/// </summary>
/// <param name="image">Image to Scale.</param>
/// <param name="x">X coordinate.</param>
/// <param name="y">Y coordinate.</param>
/// <param name="w">Desired Width.</param>
/// <param name="h">Desired Height.</param>
public virtual void DrawImage(Image image, int x, int y,int w,int h)
{
int[] pixels = scaleImage(image, w, h);
for (int _x = 0; _x < w; _x++)
{
for (int _y = 0; _y < h; _y++)
{
Global.mDebugger.SendInternal(pixels[_x + _y * w]);
DrawPoint(new Pen(Color.FromArgb(pixels[_x + _y * w])), x + _x, y + _y);
}
}
}

/// <summary>
/// Draw image with alpha channel.
/// </summary>
Expand Down

0 comments on commit 1e4cbb6

Please sign in to comment.