Skip to content
This repository was archived by the owner on Jul 13, 2024. It is now read-only.
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
2 changes: 1 addition & 1 deletion RemoteViewing.NoVncExample/DummyFramebufferSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public VncFramebuffer Capture()
|| this.framebuffer.Width != image.Width
|| this.framebuffer.Height != image.Height)
{
this.framebuffer = new VncFramebuffer("Quamotion", image.Width, image.Height, new VncPixelFormat());
this.framebuffer = new VncFramebuffer("Quamotion", image.Width, image.Height, VncPixelFormat.RGB32);
}

lock (this.framebuffer.SyncRoot)
Expand Down
2 changes: 1 addition & 1 deletion RemoteViewing.Tests/Vnc/Server/RawEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void SendTest()
using (MemoryStream stream = new MemoryStream())
{
// The encoder should write the content 'as is' to the stream.
encoder.Send(stream, new VncPixelFormat(), default(VncRectangle), content);
encoder.Send(stream, VncPixelFormat.RGB32, default(VncRectangle), content);

Assert.Equal(content, stream.ToArray());
}
Expand Down
2 changes: 1 addition & 1 deletion RemoteViewing.Tests/Vnc/Server/TightEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void SendSmallRectangleFormat()
using (MemoryStream output = new MemoryStream())
{
var contents = new byte[] { 0x01, 0x02, 0x03, 0x04 };
encoder.Send(output, new VncPixelFormat(), default, contents);
encoder.Send(output, VncPixelFormat.RGB32, default, contents);
raw = output.ToArray();
}

Expand Down
4 changes: 2 additions & 2 deletions RemoteViewing.Tests/Vnc/Server/ZlibEncoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public void SendTest()

// Individual rectangles are compressed using the _same_ zlib stream. Let's send two
// rectangles to make sure this is the case.
encoder.Send(output, new VncPixelFormat(), default(VncRectangle), contents);
encoder.Send(output, VncPixelFormat.RGB32, default(VncRectangle), contents);
raw1 = output.ToArray();

output.SetLength(0);
encoder.Send(output, new VncPixelFormat(), default(VncRectangle), contents);
encoder.Send(output, VncPixelFormat.RGB32, default(VncRectangle), contents);
raw2 = output.ToArray();
}

Expand Down
8 changes: 4 additions & 4 deletions RemoteViewing.Tests/Vnc/VncPixelFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class VncPixelFormatTests
public void ConstructorTest()
{
// Default pixel format should be RGB32.
var pixelFormat = new VncPixelFormat();
var pixelFormat = VncPixelFormat.RGB32;
Assert.Equal(24, pixelFormat.BitDepth);
Assert.Equal(32, pixelFormat.BitsPerPixel);
Assert.Equal(4, pixelFormat.BytesPerPixel);
Expand Down Expand Up @@ -76,9 +76,9 @@ public void ConstuctorInvalidValuesTest()
Assert.Throws<ArgumentOutOfRangeException>(() => new VncPixelFormat(4, 1, 1, 0, 1, 0, 1, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new VncPixelFormat(24, 1, 1, 0, 1, 0, 1, 0));

// Only bit depth of 8 or 24
// Only bit depth of 8 or 16 or 24
Assert.Throws<ArgumentOutOfRangeException>(() => new VncPixelFormat(8, 2, 1, 0, 1, 0, 1, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new VncPixelFormat(16, 16, 1, 0, 1, 0, 1, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => new VncPixelFormat(16, 14, 1, 0, 1, 0, 1, 0));

// Red: negative bits or shift, or bits or shift > bit depth
Assert.Throws<ArgumentOutOfRangeException>(() => new VncPixelFormat(8, 24, -1, 0, 8, 0, 8, 0));
Expand All @@ -105,7 +105,7 @@ public void ConstuctorInvalidValuesTest()
[Fact]
public void EncodeTest()
{
var pixelFormat = new VncPixelFormat();
var pixelFormat = VncPixelFormat.RGB32;
var buffer = new byte[VncPixelFormat.Size];
pixelFormat.Encode(buffer, 0);

Expand Down
6 changes: 3 additions & 3 deletions RemoteViewing.Windows.Forms/VncBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public static unsafe void CopyToFramebuffer(
}

var winformsRect = new Rectangle(sourceRectangle.X, sourceRectangle.Y, sourceRectangle.Width, sourceRectangle.Height);
var data = source.LockBits(winformsRect, ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
var data = source.LockBits(winformsRect, ImageLockMode.ReadOnly, source.PixelFormat);
try
{
fixed (byte* framebufferData = target.GetBuffer())
{
VncPixelFormat.Copy(
data.Scan0,
data.Stride,
new VncPixelFormat(),
data.PixelFormat.ToVncPixelFormat(),
sourceRectangle,
(IntPtr)framebufferData,
target.Stride,
Expand Down Expand Up @@ -113,7 +113,7 @@ public static unsafe void CopyFromFramebuffer(
}

var winformsRect = new Rectangle(targetX, targetY, sourceRectangle.Width, sourceRectangle.Height);
var data = target.LockBits(winformsRect, ImageLockMode.WriteOnly, PixelFormat.Format32bppRgb);
var data = target.LockBits(winformsRect, ImageLockMode.WriteOnly, target.PixelFormat);
try
{
VncPixelFormat.CopyFromFramebuffer(source, sourceRectangle, data.Scan0, data.Stride, targetX, targetY);
Expand Down
4 changes: 3 additions & 1 deletion RemoteViewing.Windows.Forms/VncControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ private void ClearInputState()
this.keysyms.Clear();
}



private void UpdateFramebuffer(bool force, VncFramebuffer framebuffer)
{
if (framebuffer == null)
Expand All @@ -327,7 +329,7 @@ private void UpdateFramebuffer(bool force, VncFramebuffer framebuffer)

if (this.bitmap == null || this.bitmap.Width != w || this.bitmap.Height != h || force)
{
this.bitmap = new Bitmap(w, h, PixelFormat.Format32bppRgb);
this.bitmap = new Bitmap(w, h, framebuffer.PixelFormat.ToSystemDrawingPixelFormat());
VncBitmap.CopyFromFramebuffer(framebuffer, new VncRectangle(0, 0, w, h), this.bitmap, 0, 0);

this.ScaleFactor = this.GetScaleFactor(framebuffer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Drawing.Imaging;
using RemoteViewing.Vnc;

namespace RemoteViewing.Windows.Forms
{
internal static class VncPixelFormatSystemDrawingPixelFormatExtensions
{

internal static PixelFormat ToSystemDrawingPixelFormat(this VncPixelFormat vncPixelFormat)
{
if (vncPixelFormat.Equals(VncPixelFormat.RGB16))
{
return PixelFormat.Format16bppRgb565;
}

if (vncPixelFormat.Equals(VncPixelFormat.RGB32))
{
return PixelFormat.Format32bppRgb;
}

throw new NotSupportedException($"PixelFormat not supported: {vncPixelFormat}");
}

internal static VncPixelFormat ToVncPixelFormat(this PixelFormat pixelFormat)
{
switch (pixelFormat)
{
case PixelFormat.Format16bppRgb565:
return VncPixelFormat.RGB16;

case PixelFormat.Format32bppRgb:
return VncPixelFormat.RGB32;

default:
throw new NotSupportedException($"The pixelformat '{pixelFormat}' is not supported.");
}
}
}
}
23 changes: 9 additions & 14 deletions RemoteViewing/Vnc/VncPixelFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ namespace RemoteViewing.Vnc
/// </summary>
public sealed class VncPixelFormat
{
/// <summary>
/// Initializes a new instance of the <see cref="VncPixelFormat"/> class,
/// with 8 bits each of red, green, and blue channels.
/// </summary>
public VncPixelFormat()
: this(32, 24, 8, 16, 8, 8, 8, 0)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="VncPixelFormat"/> class.
/// </summary>
Expand Down Expand Up @@ -74,7 +65,7 @@ public VncPixelFormat(
throw new ArgumentOutOfRangeException(nameof(bitsPerPixel));
}

if (bitDepth != 6 && bitDepth != 24)
if (bitDepth != 6 && bitDepth != 16 && bitDepth != 24)
{
throw new ArgumentOutOfRangeException(nameof(bitDepth));
}
Expand Down Expand Up @@ -108,10 +99,14 @@ public VncPixelFormat(
}

/// <summary>
/// Gets a <see cref="VncPixelFormat"/> with 8 bits of red, green and blue channels.
/// Gets a <see cref="VncPixelFormat"/> with 32bits per pixel, 8 bits of red, green and blue channels.
/// </summary>
public static VncPixelFormat RGB32 { get; } = new VncPixelFormat();
public static VncPixelFormat RGB32 { get; } = new VncPixelFormat(32, 24, 8, 16, 8, 8, 8, 0);

/// <summary>
/// Gets a <see cref="VncPixelFormat"/> with 16 bits per pixel with 5 bits of red, 6 bits of green and 5 bits of blue channels.
/// </summary>
public static VncPixelFormat RGB16 { get; } = new VncPixelFormat(16, 16, 5, 11, 6, 5, 5, 0);
/// <summary>
/// Gets the number of bits used to store a pixel.
/// </summary>
Expand Down Expand Up @@ -248,7 +243,7 @@ public static unsafe void Copy(
{
throw new ArgumentNullException(nameof(source));
}

if (target == null)
{
throw new ArgumentNullException(nameof(target));
Expand Down Expand Up @@ -412,7 +407,7 @@ public static unsafe void CopyFromFramebuffer(
sourceRectangle,
scan0,
stride,
new VncPixelFormat());
source.PixelFormat);
}
}

Expand Down