Skip to content

Commit 1e4cbb6

Browse files
authored
Merge pull request #1529 from KM198912/patch-1
Implement Bitmap Scaling
2 parents cff35bf + 9435168 commit 1e4cbb6

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

Tests/Kernels/GraphicTest/Kernel.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ private void DoTest(Canvas aCanvas)
117117

118118
aCanvas.DrawImage(bitmap, new Point(0, 0));
119119
aCanvas.DrawImage(bitmap2, new Point(200, 0));
120-
120+
//Scale Bitmap
121+
aCanvas.DrawImage(bitmap,0,0,50,50);
122+
121123
aCanvas.DrawImageAlpha(bitmap3, new Point(0, 300));
122124

123125
/* Drawing ellipses */

source/Cosmos.System2/Graphics/Canvas.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,48 @@ public virtual void DrawImage(Image image, int x, int y)
855855
}
856856
}
857857
}
858-
858+
859+
private int[] scaleImage(Image image, int newWidth, int newHeight)
860+
{
861+
int[] pixels = image.rawData;
862+
int w1 = (int)image.Width;
863+
int h1 = (int)image.Height;
864+
int[] temp = new int[newWidth * newHeight];
865+
int x_ratio = (int)((w1 << 16) / newWidth) + 1;
866+
int y_ratio = (int)((h1 << 16) / newHeight) + 1;
867+
int x2, y2;
868+
for (int i = 0; i < newHeight; i++)
869+
{
870+
for (int j = 0; j < newWidth; j++)
871+
{
872+
x2 = ((j * x_ratio) >> 16);
873+
y2 = ((i * y_ratio) >> 16);
874+
temp[(i * newWidth) + j] = pixels[(y2 * w1) + x2];
875+
}
876+
}
877+
return temp;
878+
}
879+
/// <summary>
880+
/// Draw a Scaled Bitmap.
881+
/// </summary>
882+
/// <param name="image">Image to Scale.</param>
883+
/// <param name="x">X coordinate.</param>
884+
/// <param name="y">Y coordinate.</param>
885+
/// <param name="w">Desired Width.</param>
886+
/// <param name="h">Desired Height.</param>
887+
public virtual void DrawImage(Image image, int x, int y,int w,int h)
888+
{
889+
int[] pixels = scaleImage(image, w, h);
890+
for (int _x = 0; _x < w; _x++)
891+
{
892+
for (int _y = 0; _y < h; _y++)
893+
{
894+
Global.mDebugger.SendInternal(pixels[_x + _y * w]);
895+
DrawPoint(new Pen(Color.FromArgb(pixels[_x + _y * w])), x + _x, y + _y);
896+
}
897+
}
898+
}
899+
859900
/// <summary>
860901
/// Draw image with alpha channel.
861902
/// </summary>

0 commit comments

Comments
 (0)