Skip to content

Commit e42447d

Browse files
authored
Merge pull request #3087 from Szymekk44/CGS-Update-2-Remastered
✨CGS Update part 2 (Remastered)
2 parents 1f3bb52 + 9f36cb5 commit e42447d

File tree

9 files changed

+587
-57
lines changed

9 files changed

+587
-57
lines changed

Tests/Kernels/GraphicTest/Kernel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ private void DoTest(Canvas aCanvas)
102102
aCanvas.DrawImage(bitmap, 10, 10);
103103
aCanvas.DrawImage(letter, 50, 10);
104104

105+
Bitmap GetImageTest = aCanvas.GetImage(50, 10, (int)letter.Width, (int)letter.Height);
106+
Assert.AreEqual(GetImageTest.RawData, letter.RawData, "GetImage returns correct values");
107+
105108
/* Drawing BitmapHeaderV5 image */
106109
Bitmap v5header = new Bitmap(Convert.FromBase64String(parrot));
107110
aCanvas.DrawImage(v5header,0,0);

source/Cosmos.Core/ManagedMemoryBlock.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ public unsafe void Copy(MemoryBlock block)
166166
MemoryOperations.Copy(xDest, aDataPtr, (int)block.Size);
167167
}
168168

169+
/// <summary>
170+
/// Copies data from the memory block to the specified array.
171+
/// </summary>
172+
/// <param name="aStart">The start index in the memory block from which to begin copying.</param>
173+
/// <param name="aData">The array into which data will be copied.</param>
174+
/// <param name="aIndex">The starting index in the array where data will be copied.</param>
175+
/// <param name="aCount">The number of elements to copy.</param>
176+
public unsafe void Get(int aStart, int[] aData, int aIndex, int aCount)
177+
{
178+
int* xSrc;
179+
fixed (byte* aArrayPtr = memory)
180+
{
181+
xSrc = (int*)aArrayPtr + aStart;
182+
}
183+
fixed (int* aDataPtr = aData)
184+
{
185+
MemoryOperations.Copy(aDataPtr + aIndex, xSrc, aCount);
186+
}
187+
}
188+
169189
/// <summary>
170190
/// Write 8-bit to the memory block.
171191
/// </summary>

source/Cosmos.Core/MemoryBlock.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,38 @@ public unsafe void Copy(ManagedMemoryBlock block)
270270
MemoryOperations.Copy(xDest, aDataPtr, (int)block.Size);
271271
}
272272

273+
/// <summary>
274+
/// Copies data from the memory block to the specified array.
275+
/// </summary>
276+
/// <param name="aByteOffset">The byte offset in the memory block from which to start copying.</param>
277+
/// <param name="aData">The array into which data will be copied.</param>
278+
/// <param name="aIndex">The starting index in the array where data will be copied.</param>
279+
/// <param name="aCount">The number of elements to copy.</param>
280+
public unsafe void Get(int aByteOffset, int[] aData, int aIndex, int aCount)
281+
{
282+
int* xSource = (int*)(Base + aByteOffset);
283+
fixed (int* aDataPtr = aData)
284+
{
285+
MemoryOperations.Copy(aDataPtr + aIndex, xSource, aCount);
286+
}
287+
}
288+
289+
/// <summary>
290+
/// Copies a specified number of bytes from the memory block into an array.
291+
/// </summary>
292+
/// <param name="aByteOffset">The byte offset in the memory block from where the copy starts.</param>
293+
/// <param name="aData">The array where the data will be copied to.</param>
294+
/// <param name="aIndex">The starting index in the destination array.</param>
295+
/// <param name="aCount">The number of bytes to copy.</param>
296+
public unsafe void Get(int aByteOffset, byte[] aData, int aIndex, int aCount)
297+
{
298+
byte* xSource = (byte*)(Base + aByteOffset);
299+
fixed (byte* aDataPtr = aData)
300+
{
301+
MemoryOperations.Copy(aDataPtr + aIndex, xSource, aCount);
302+
}
303+
}
304+
273305
/// <summary>
274306
/// Move bytes array down the memory block.
275307
/// </summary>

source/Cosmos.HAL2/Drivers/Video/VBEDriver.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,18 @@ public uint GetVRAM(uint index)
265265
return (uint)pixel;
266266
}
267267

268+
/// <summary>
269+
/// Get VRAM data.
270+
/// </summary>
271+
/// <param name="aStart">Start position in VRAM.</param>
272+
/// <param name="aData">Array to copy data into.</param>
273+
/// <param name="aIndex">Starting index in the array to begin copying data.</param>
274+
/// <param name="aCount">Number of elements to copy.</param>
275+
public void GetVRAM(int aStart, int[] aData, int aIndex, int aCount)
276+
{
277+
lastbuffer.Get(aStart, aData, aIndex, aCount);
278+
}
279+
268280
/// <summary>
269281
/// Clear VRAM.
270282
/// </summary>

source/Cosmos.System2/Graphics/Canvas.cs

Lines changed: 128 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ public virtual void Clear(Color color)
100100
/// <param name="y">The Y coordinate.</param>
101101
public abstract void DrawPoint(Color color, int x, int y);
102102

103+
/// <summary>
104+
/// Sets the pixel at the given coordinates to the specified <paramref name="color"/>, without unnecessary color operations.
105+
/// </summary>
106+
/// <param name="color">The color to draw with (raw argb).</param>
107+
/// <param name="x">The X coordinate.</param>
108+
/// <param name="y">The Y coordinate.</param>
109+
public abstract void DrawPoint(uint color, int x, int y);
110+
111+
/// <summary>
112+
/// Sets the pixel at the given coordinates to the specified <paramref name="color"/>. without ToArgb()
113+
/// </summary>
114+
/// <param name="color">The color to draw with (raw argb).</param>
115+
/// <param name="x">The X coordinate.</param>
116+
/// <param name="y">The Y coordinate.</param>
117+
public abstract void DrawPoint(int color, int x, int y);
118+
103119
/// <summary>
104120
/// The name of the Canvas implementation.
105121
/// </summary>
@@ -117,11 +133,14 @@ public virtual void Clear(Color color)
117133
/// <param name="x">The X coordinate.</param>
118134
/// <param name="y">The Y coordinate.</param>
119135
public abstract Color GetPointColor(int x, int y);
136+
120137
/// <summary>
121-
/// Gets the index of the pixel at the given coordinates.
138+
/// Gets the color of the pixel at the given coordinates in ARGB.
122139
/// </summary>
123140
/// <param name="x">The X coordinate.</param>
124141
/// <param name="y">The Y coordinate.</param>
142+
public abstract int GetRawPointColor(int x, int y);
143+
125144
internal int GetPointOffset(int x, int y)
126145
{
127146
return (x * Stride) + (y * Pitch);
@@ -147,6 +166,47 @@ public virtual void DrawArray(Color[] colors, int x, int y, int width, int heigh
147166
}
148167
}
149168

169+
/// <summary>
170+
/// Draws an array of pixels to the canvas, starting at the given coordinates,
171+
/// using the given width.
172+
/// </summary>
173+
/// <param name="colors">The pixels to draw.</param>
174+
/// <param name="x">The X coordinate.</param>
175+
/// <param name="y">The Y coordinate.</param>
176+
/// <param name="width">The width of the drawn bitmap.</param>
177+
/// <param name="height">The height of the drawn bitmap.</param>
178+
public virtual void DrawArray(int[] colors, int x, int y, int width, int height)
179+
{
180+
for (int X = 0; X < width; X++)
181+
{
182+
for (int Y = 0; Y < height; Y++)
183+
{
184+
DrawPoint(colors[Y * width + X], x + X, y + Y);
185+
}
186+
}
187+
}
188+
189+
/// <summary>
190+
/// Draws an array of pixels to the canvas, starting at the given coordinates,
191+
/// using the given width.
192+
/// </summary>
193+
/// <param name="colors">The pixels to draw.</param>
194+
/// <param name="x">The X coordinate.</param>
195+
/// <param name="y">The Y coordinate.</param>
196+
/// <param name="width">The width of the drawn bitmap.</param>
197+
/// <param name="height">The height of the drawn bitmap.</param>
198+
/// <param name="startIndex">int[] colors tarting position</param>
199+
public virtual void DrawArray(int[] colors, int x, int y, int width, int height, int startIndex)
200+
{
201+
for (int X = 0; X < width; X++)
202+
{
203+
for (int Y = 0; Y < height; Y++)
204+
{
205+
DrawPoint(colors[Y * width + X + startIndex], x + X, y + Y);
206+
}
207+
}
208+
}
209+
150210
/// <summary>
151211
/// Draws a horizontal line.
152212
/// </summary>
@@ -317,6 +377,7 @@ public virtual void DrawCircle(Color color, int xCenter, int yCenter, int radius
317377
/// <param name="x0">The X center coordinate.</param>
318378
/// <param name="y0">The Y center coordinate.</param>
319379
/// <param name="radius">The radius of the circle to draw.</param>
380+
/// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
320381
public virtual void DrawFilledCircle(Color color, int x0, int y0, int radius)
321382
{
322383
int x = radius;
@@ -485,40 +546,17 @@ public virtual void DrawSquare(Color color, int x, int y, int size)
485546
/// <param name="height">The height of the rectangle.</param>
486547
public virtual void DrawRectangle(Color color, int x, int y, int width, int height)
487548
{
488-
/*
489-
* we must draw four lines connecting any vertex of our rectangle to do this we first obtain the position of these
490-
* vertex (we call these vertexes A, B, C, D as for geometric convention)
491-
*/
492-
493-
/* The check of the validity of x and y are done in DrawLine() */
494-
495-
/* The vertex A is where x,y are */
496-
int xa = x;
497-
int ya = y;
498-
499-
/* The vertex B has the same y coordinate of A but x is moved of width pixels */
500-
int xb = x + width;
501-
int yb = y;
549+
// Draw top edge from (x, y) to (x + width, y)
550+
DrawLine(color, x, y, x + width, y);
502551

503-
/* The vertex C has the same x coordiate of A but this time is y that is moved of height pixels */
504-
int xc = x;
505-
int yc = y + height;
552+
// Draw left edge from (x, y) to (x, y + height)
553+
DrawLine(color, x, y, x, y + height);
506554

507-
/* The Vertex D has x moved of width pixels and y moved of height pixels */
508-
int xd = x + width;
509-
int yd = y + height;
555+
// Draw bottom edge from (x, y + height) to (x + width, y + height)
556+
DrawLine(color, x, y + height, x + width, y + height);
510557

511-
/* Draw a line betwen A and B */
512-
DrawLine(color, xa, ya, xb, yb);
513-
514-
/* Draw a line between A and C */
515-
DrawLine(color, xa, ya, xc, yc);
516-
517-
/* Draw a line between B and D */
518-
DrawLine(color, xb, yb, xd, yd);
519-
520-
/* Draw a line between C and D */
521-
DrawLine(color, xc, yc, xd, yd);
558+
// Draw right edge from (x + width, y) to (x + width, y + height)
559+
DrawLine(color, x + width, y, x + width, y + height);
522560
}
523561

524562
/// <summary>
@@ -569,6 +607,7 @@ public virtual void DrawTriangle(Color color, int v1x, int v1y, int v2x, int v2y
569607
/// <param name="image">The image to draw.</param>
570608
/// <param name="x">The origin X coordinate.</param>
571609
/// <param name="y">The origin Y coordinate.</param>
610+
/// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
572611
public virtual void DrawImage(Image image, int x, int y, bool preventOffBoundPixels = true)
573612
{
574613
Color color;
@@ -598,6 +637,35 @@ public virtual void DrawImage(Image image, int x, int y, bool preventOffBoundPix
598637
}
599638
}
600639

640+
/// <summary>
641+
/// Creates a bitmap by copying a portion of your canvas from the specified coordinates and dimensions.
642+
/// </summary>
643+
/// <param name="x">The starting X coordinate of the region to copy.</param>
644+
/// <param name="y">The starting Y coordinate of the region to copy.</param>
645+
/// <param name="width">The width of the region to copy.</param>
646+
/// <param name="height">The height of the region to copy.</param>
647+
/// <returns>A new <see cref="Bitmap"/> containing the copied region.</returns>
648+
public virtual Bitmap GetImage(int x, int y, int width, int height)
649+
{
650+
Bitmap bitmap = new Bitmap((uint)x, (uint)y, ColorDepth.ColorDepth32);
651+
652+
for (int posy = y, desty = 0; posy < y + y; posy++, desty++)
653+
{
654+
for (int posx = x, destx = 0; posx < x + x; posx++, destx++)
655+
{
656+
bitmap.RawData[desty * x + destx] = GetRawPointColor(posx, posy);
657+
}
658+
}
659+
return bitmap;
660+
}
661+
662+
/// <summary>
663+
/// Scales an image to the specified new width and height.
664+
/// </summary>
665+
/// <param name="image">The image to be scaled.</param>
666+
/// <param name="newWidth">The width of the scaled image.</param>
667+
/// <param name="newHeight">The height of the scaled image.</param>
668+
/// <returns>An array of integers representing the scaled image's pixel data. (Raw bitmap data)</returns>
601669
static int[] ScaleImage(Image image, int newWidth, int newHeight)
602670
{
603671
int[] pixels = image.RawData;
@@ -607,7 +675,6 @@ static int[] ScaleImage(Image image, int newWidth, int newHeight)
607675
int xRatio = (int)((w1 << 16) / newWidth) + 1;
608676
int yRatio = (int)((h1 << 16) / newHeight) + 1;
609677
int x2, y2;
610-
611678
for (int i = 0; i < newHeight; i++)
612679
{
613680
for (int j = 0; j < newWidth; j++)
@@ -617,7 +684,6 @@ static int[] ScaleImage(Image image, int newWidth, int newHeight)
617684
temp[(i * newWidth) + j] = pixels[(y2 * w1) + x2];
618685
}
619686
}
620-
621687
return temp;
622688
}
623689

@@ -629,6 +695,7 @@ static int[] ScaleImage(Image image, int newWidth, int newHeight)
629695
/// <param name="y">The Y coordinate.</param>
630696
/// <param name="w">The desired width to scale the image to before drawing.</param>
631697
/// <param name="h">The desired height to scale the image to before drawing</param>
698+
/// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
632699
public virtual void DrawImage(Image image, int x, int y, int w, int h, bool preventOffBoundPixels = true)
633700
{
634701
Color color;
@@ -660,12 +727,39 @@ public virtual void DrawImage(Image image, int x, int y, int w, int h, bool prev
660727
}
661728
}
662729

730+
/// <summary>
731+
/// Draws the given image at the specified coordinates, cropping the image to fit within the maximum width and height.
732+
/// </summary>
733+
/// <param name="image">The image to draw.</param>
734+
/// <param name="x">The X coordinate where the image will be drawn.</param>
735+
/// <param name="y">The Y coordinate where the image will be drawn.</param>
736+
/// <param name="maxWidth">The maximum width to display the image. If the image exceeds this width, it will be cropped.</param>
737+
/// <param name="maxHeight">The maximum height to display the image. If the image exceeds this height, it will be cropped.</param>
738+
/// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
739+
public virtual void CroppedDrawImage(Image image, int x, int y, int maxWidth, int maxHeight, bool preventOffBoundPixels = true)
740+
{
741+
Color color;
742+
int width = Math.Min((int)image.Width, maxWidth);
743+
int height = Math.Min((int)image.Height, maxHeight);
744+
int[] pixels = image.RawData;
745+
746+
for (int xi = 0; xi < width; xi++)
747+
{
748+
for (int yi = 0; yi < height; yi++)
749+
{
750+
color = Color.FromArgb(pixels[xi + (yi * image.Width)]);
751+
DrawPoint(color, x + xi, y + yi);
752+
}
753+
}
754+
}
755+
663756
/// <summary>
664757
/// Draws an image with alpha blending.
665758
/// </summary>
666759
/// <param name="image">The image to draw.</param>
667760
/// <param name="x">The X coordinate.</param>
668761
/// <param name="y">The Y coordinate.</param>
762+
/// <param name="preventOffBoundPixels">Prevents drawing outside the bounds of the canvas.</param>
669763
public void DrawImageAlpha(Image image, int x, int y, bool preventOffBoundPixels = true)
670764
{
671765
Color color;

source/Cosmos.System2/Graphics/FullScreenCanvas.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private enum VideoDriver
3535
VGADriver
3636
}
3737

38-
static Canvas videoDriver = null;
38+
private static Canvas videoDriver = null;
3939
static readonly PCIDevice svgaIIDevice = PCI.GetDevice(VendorID.VMWare, DeviceID.SVGAIIAdapter);
4040

4141
/// <summary>

0 commit comments

Comments
 (0)