From ab19a9fbefb25a07c749453e246359c664524597 Mon Sep 17 00:00:00 2001 From: yankejustin Date: Sun, 17 May 2015 23:24:13 -0400 Subject: [PATCH 1/6] Make the lock readonly Made the lock for the codec readonly. This makes it impossible for a spot (excluding constructors) from touching the lock. Tampering with the lock means that, if something acquires the lock and enters critical code, the lock can be changed and the critical code's thread safety can be violated. --- Client/Core/Helper/UnsafeStreamCodec.cs | 2 +- Server/Core/Helper/UnsafeStreamCodec.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Client/Core/Helper/UnsafeStreamCodec.cs b/Client/Core/Helper/UnsafeStreamCodec.cs index 16767e5a..b9e72108 100644 --- a/Client/Core/Helper/UnsafeStreamCodec.cs +++ b/Client/Core/Helper/UnsafeStreamCodec.cs @@ -33,7 +33,7 @@ private set private PixelFormat _encodedFormat; private int _encodedWidth; private int _encodedHeight; - private object _imageProcessLock = new object(); + private readonly object _imageProcessLock = new object(); private JpgCompression _jpgCompression; [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] diff --git a/Server/Core/Helper/UnsafeStreamCodec.cs b/Server/Core/Helper/UnsafeStreamCodec.cs index 50b3c000..73ad9801 100644 --- a/Server/Core/Helper/UnsafeStreamCodec.cs +++ b/Server/Core/Helper/UnsafeStreamCodec.cs @@ -31,7 +31,7 @@ private set private PixelFormat _encodedFormat; private int _encodedWidth; private int _encodedHeight; - private object _imageProcessLock = new object(); + private readonly object _imageProcessLock = new object(); private JpgCompression _jpgCompression; [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] From 95a271554b064a8a40e19ada4f16c6a5d6c61574 Mon Sep 17 00:00:00 2001 From: yankejustin Date: Sun, 17 May 2015 23:54:40 -0400 Subject: [PATCH 2/6] Removed unnecessary comment Removed commented-out (archived) code. --- Client/Core/Helper/UnsafeStreamCodec.cs | 29 ------------------------- Server/Core/Helper/UnsafeStreamCodec.cs | 29 ------------------------- 2 files changed, 58 deletions(-) diff --git a/Client/Core/Helper/UnsafeStreamCodec.cs b/Client/Core/Helper/UnsafeStreamCodec.cs index b9e72108..c73ffc7c 100644 --- a/Client/Core/Helper/UnsafeStreamCodec.cs +++ b/Client/Core/Helper/UnsafeStreamCodec.cs @@ -203,19 +203,6 @@ public UnsafeStreamCodec(int imageQuality, int monitor) } } - /*int maxHeight = 0; - int maxWidth = 0; - - for (int i = 0; i < finalUpdates.Count; i++) - { - if (finalUpdates[i].Height > maxHeight) - maxHeight = finalUpdates[i].Height; - maxWidth += finalUpdates[i].Width; - } - - Bitmap bmp = new Bitmap(maxWidth+1, maxHeight+1); - int XOffset = 0;*/ - for (int i = 0; i < finalUpdates.Count; i++) { Rectangle rect = finalUpdates[i]; @@ -233,12 +220,6 @@ public UnsafeStreamCodec(int imageQuality, int monitor) } tmpBmp.UnlockBits(tmpData); - /*using (Graphics g = Graphics.FromImage(bmp)) - { - g.DrawImage(TmpBmp, new Point(XOffset, 0)); - } - XOffset += TmpBmp.Width;*/ - outStream.Write(BitConverter.GetBytes(rect.X), 0, 4); outStream.Write(BitConverter.GetBytes(rect.Y), 0, 4); outStream.Write(BitConverter.GetBytes(rect.Width), 0, 4); @@ -259,16 +240,6 @@ public UnsafeStreamCodec(int imageQuality, int monitor) totalDataLength += (int) length + (4*5); } - /*if (finalUpdates.Count > 0) - { - byte[] lele = base.jpgCompression.Compress(bmp); - byte[] compressed = new SafeQuickLZ().compress(lele, 0, lele.Length, 1); - bool Won = lele.Length < outStream.Length; - bool CompressWon = compressed.Length < outStream.Length; - Console.WriteLine(Won + ", " + CompressWon); - } - bmp.Dispose();*/ - outStream.Position = oldPos; outStream.Write(BitConverter.GetBytes(totalDataLength), 0, 4); blocks.Clear(); diff --git a/Server/Core/Helper/UnsafeStreamCodec.cs b/Server/Core/Helper/UnsafeStreamCodec.cs index 73ad9801..2209e81a 100644 --- a/Server/Core/Helper/UnsafeStreamCodec.cs +++ b/Server/Core/Helper/UnsafeStreamCodec.cs @@ -199,19 +199,6 @@ public UnsafeStreamCodec(int imageQuality = 100) } } - /*int maxHeight = 0; - int maxWidth = 0; - - for (int i = 0; i < finalUpdates.Count; i++) - { - if (finalUpdates[i].Height > maxHeight) - maxHeight = finalUpdates[i].Height; - maxWidth += finalUpdates[i].Width; - } - - Bitmap bmp = new Bitmap(maxWidth+1, maxHeight+1); - int XOffset = 0;*/ - for (int i = 0; i < finalUpdates.Count; i++) { Rectangle rect = finalUpdates[i]; @@ -229,12 +216,6 @@ public UnsafeStreamCodec(int imageQuality = 100) } tmpBmp.UnlockBits(tmpData); - /*using (Graphics g = Graphics.FromImage(bmp)) - { - g.DrawImage(TmpBmp, new Point(XOffset, 0)); - } - XOffset += TmpBmp.Width;*/ - outStream.Write(BitConverter.GetBytes(rect.X), 0, 4); outStream.Write(BitConverter.GetBytes(rect.Y), 0, 4); outStream.Write(BitConverter.GetBytes(rect.Width), 0, 4); @@ -255,16 +236,6 @@ public UnsafeStreamCodec(int imageQuality = 100) totalDataLength += (int) length + (4*5); } - /*if (finalUpdates.Count > 0) - { - byte[] lele = base.jpgCompression.Compress(bmp); - byte[] compressed = new SafeQuickLZ().compress(lele, 0, lele.Length, 1); - bool Won = lele.Length < outStream.Length; - bool CompressWon = compressed.Length < outStream.Length; - Console.WriteLine(Won + ", " + CompressWon); - } - bmp.Dispose();*/ - outStream.Position = oldPos; outStream.Write(BitConverter.GetBytes(totalDataLength), 0, 4); blocks.Clear(); From d1e04a98a87b46d339d5e83d2db162c85627673e Mon Sep 17 00:00:00 2001 From: yankejustin Date: Sun, 17 May 2015 23:58:08 -0400 Subject: [PATCH 3/6] Removed clearing of local lists + setting to null Setting objects to null would be optimized out. Clearing lists probably would not be optimized out. There is no need to explicitly clear a list of items or setting objects to null in C#. We must let the Garbage Collector have its way. --- Client/Core/Helper/UnsafeStreamCodec.cs | 4 ---- Server/Core/Helper/UnsafeStreamCodec.cs | 4 ---- 2 files changed, 8 deletions(-) diff --git a/Client/Core/Helper/UnsafeStreamCodec.cs b/Client/Core/Helper/UnsafeStreamCodec.cs index c73ffc7c..00bdc334 100644 --- a/Client/Core/Helper/UnsafeStreamCodec.cs +++ b/Client/Core/Helper/UnsafeStreamCodec.cs @@ -242,8 +242,6 @@ public UnsafeStreamCodec(int imageQuality, int monitor) outStream.Position = oldPos; outStream.Write(BitConverter.GetBytes(totalDataLength), 0, 4); - blocks.Clear(); - finalUpdates.Clear(); } } @@ -291,7 +289,6 @@ public Bitmap DecodeData(Stream inStream) Rectangle rect = new Rectangle(BitConverter.ToInt32(tempData, 0), BitConverter.ToInt32(tempData, 4), BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); int updateLen = BitConverter.ToInt32(tempData, 16); - tempData = null; byte[] buffer = new byte[updateLen]; inStream.Read(buffer, 0, buffer.Length); @@ -301,7 +298,6 @@ public Bitmap DecodeData(Stream inStream) { g.DrawImage(tmp, rect.Location); } - buffer = null; dataSize -= updateLen + (4*5); } } diff --git a/Server/Core/Helper/UnsafeStreamCodec.cs b/Server/Core/Helper/UnsafeStreamCodec.cs index 2209e81a..72485c38 100644 --- a/Server/Core/Helper/UnsafeStreamCodec.cs +++ b/Server/Core/Helper/UnsafeStreamCodec.cs @@ -238,8 +238,6 @@ public UnsafeStreamCodec(int imageQuality = 100) outStream.Position = oldPos; outStream.Write(BitConverter.GetBytes(totalDataLength), 0, 4); - blocks.Clear(); - finalUpdates.Clear(); } } @@ -287,7 +285,6 @@ public Bitmap DecodeData(Stream inStream) Rectangle rect = new Rectangle(BitConverter.ToInt32(tempData, 0), BitConverter.ToInt32(tempData, 4), BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); int updateLen = BitConverter.ToInt32(tempData, 16); - tempData = null; byte[] buffer = new byte[updateLen]; inStream.Read(buffer, 0, buffer.Length); @@ -297,7 +294,6 @@ public Bitmap DecodeData(Stream inStream) { g.DrawImage(tmp, rect.Location); } - buffer = null; dataSize -= updateLen + (4*5); } } From af591e1d3a588e9b4fa9226ae467a51cd9ccfc8d Mon Sep 17 00:00:00 2001 From: yankejustin Date: Mon, 18 May 2015 00:37:12 -0400 Subject: [PATCH 4/6] Formatted code of UnsafeStreamCodec Formatted the code for UnsafeStreamCodec for readability and consistency with the rest of the code in the program. --- Client/Core/Helper/UnsafeStreamCodec.cs | 177 +++++++++++++++--------- Server/Core/Helper/UnsafeStreamCodec.cs | 173 ++++++++++++++--------- 2 files changed, 216 insertions(+), 134 deletions(-) diff --git a/Client/Core/Helper/UnsafeStreamCodec.cs b/Client/Core/Helper/UnsafeStreamCodec.cs index 00bdc334..f4d0581f 100644 --- a/Client/Core/Helper/UnsafeStreamCodec.cs +++ b/Client/Core/Helper/UnsafeStreamCodec.cs @@ -59,14 +59,16 @@ public UnsafeStreamCodec(int imageQuality, int monitor) this.Monitor = monitor; } - public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, PixelFormat format, - Stream outStream) + public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, PixelFormat format, Stream outStream) { lock (_imageProcessLock) { - byte* pScan0 = (byte*) scan0.ToInt32(); + byte* pScan0 = (byte*)scan0.ToInt32(); + if (!outStream.CanWrite) + { throw new Exception("Must have access to Write in the Stream"); + } int stride = 0; int rawLength = 0; @@ -86,8 +88,8 @@ public UnsafeStreamCodec(int imageQuality, int monitor) throw new NotSupportedException(format.ToString()); } - stride = imageSize.Width*pixelSize; - rawLength = stride*imageSize.Height; + stride = imageSize.Width * pixelSize; + rawLength = stride * imageSize.Height; if (_encodeBuffer == null) { @@ -95,6 +97,7 @@ public UnsafeStreamCodec(int imageQuality, int monitor) this._encodedWidth = imageSize.Width; this._encodedHeight = imageSize.Height; this._encodeBuffer = new byte[rawLength]; + fixed (byte* ptr = _encodeBuffer) { byte[] temp = null; @@ -105,25 +108,28 @@ public UnsafeStreamCodec(int imageQuality, int monitor) outStream.Write(BitConverter.GetBytes(temp.Length), 0, 4); outStream.Write(temp, 0, temp.Length); - memcpy(new IntPtr(ptr), scan0, (uint) rawLength); + memcpy(new IntPtr(ptr), scan0, (uint)rawLength); } return; } + if (this._encodedFormat != format) + { + throw new Exception("PixelFormat is not equal to previous Bitmap"); + } + else if (this._encodedWidth != imageSize.Width || this._encodedHeight != imageSize.Height) + { + throw new Exception("Bitmap width/height are not equal to previous bitmap"); + } + long oldPos = outStream.Position; outStream.Write(new byte[4], 0, 4); - int totalDataLength = 0; - - if (this._encodedFormat != format) - throw new Exception("PixelFormat is not equal to previous Bitmap"); - - if (this._encodedWidth != imageSize.Width || this._encodedHeight != imageSize.Height) - throw new Exception("Bitmap width/height are not equal to previous bitmap"); + long totalDataLength = 0; List blocks = new List(); Size s = new Size(scanArea.Width, CheckBlock.Height); - Size lastSize = new Size(scanArea.Width%CheckBlock.Width, scanArea.Height%CheckBlock.Height); + Size lastSize = new Size(scanArea.Width % CheckBlock.Width, scanArea.Height % CheckBlock.Height); int lasty = scanArea.Height - lastSize.Height; int lastx = scanArea.Width - lastSize.Width; @@ -132,24 +138,30 @@ public UnsafeStreamCodec(int imageQuality, int monitor) List finalUpdates = new List(); s = new Size(scanArea.Width, s.Height); + fixed (byte* encBuffer = _encodeBuffer) { var index = 0; - for (int y = scanArea.Y; y != scanArea.Height;) + for (int y = scanArea.Y; y != scanArea.Height; y += s.Height) { if (y == lasty) + { s = new Size(scanArea.Width, lastSize.Height); + } + cBlock = new Rectangle(scanArea.X, y, scanArea.Width, s.Height); - int offset = (y*stride) + (scanArea.X*pixelSize); - if (memcmp(encBuffer + offset, pScan0 + offset, (uint) stride) != 0) + int offset = (y * stride) + (scanArea.X * pixelSize); + + if (memcmp(encBuffer + offset, pScan0 + offset, (uint)stride) != 0) { index = blocks.Count - 1; + if (blocks.Count != 0 && (blocks[index].Y + blocks[index].Height) == cBlock.Y) { cBlock = new Rectangle(blocks[index].X, blocks[index].Y, blocks[index].Width, - blocks[index].Height + cBlock.Height); + blocks[index].Height + cBlock.Height); blocks[index] = cBlock; } else @@ -157,36 +169,41 @@ public UnsafeStreamCodec(int imageQuality, int monitor) blocks.Add(cBlock); } } - y += s.Height; } for (int i = 0; i < blocks.Count; i++) { s = new Size(CheckBlock.Width, blocks[i].Height); - int x = scanArea.X; - while (x != scanArea.Width) + + for (int x = scanArea.X; x != scanArea.Width; x += s.Width) { if (x == lastx) + { s = new Size(lastSize.Width, blocks[i].Height); + } cBlock = new Rectangle(x, blocks[i].Y, s.Width, blocks[i].Height); bool foundChanges = false; - int blockStride = pixelSize*cBlock.Width; + uint blockStride = (uint)(pixelSize * cBlock.Width); for (int j = 0; j < cBlock.Height; j++) { - int blockOffset = (stride*(cBlock.Y + j)) + (pixelSize*cBlock.X); - if (memcmp(encBuffer + blockOffset, pScan0 + blockOffset, (uint) blockStride) != 0) + int blockOffset = (stride * (cBlock.Y + j)) + (pixelSize * cBlock.X); + + if (memcmp(encBuffer + blockOffset, pScan0 + blockOffset, blockStride) != 0) + { foundChanges = true; - memcpy(encBuffer + blockOffset, pScan0 + blockOffset, (uint) blockStride); - //copy-changes + } + + memcpy(encBuffer + blockOffset, pScan0 + blockOffset, blockStride); + //copy-changes } if (foundChanges) { index = finalUpdates.Count - 1; - if (finalUpdates.Count > 0 && - (finalUpdates[index].X + finalUpdates[index].Width) == cBlock.X) + + if (finalUpdates.Count > 0 && (finalUpdates[index].X + finalUpdates[index].Width) == cBlock.X) { Rectangle rect = finalUpdates[index]; int newWidth = cBlock.Width + rect.Width; @@ -198,7 +215,6 @@ public UnsafeStreamCodec(int imageQuality, int monitor) finalUpdates.Add(cBlock); } } - x += s.Width; } } } @@ -206,38 +222,50 @@ public UnsafeStreamCodec(int imageQuality, int monitor) for (int i = 0; i < finalUpdates.Count; i++) { Rectangle rect = finalUpdates[i]; - int blockStride = pixelSize*rect.Width; + int blockStride = pixelSize * rect.Width; - Bitmap tmpBmp = new Bitmap(rect.Width, rect.Height, format); - BitmapData tmpData = tmpBmp.LockBits(new Rectangle(0, 0, tmpBmp.Width, tmpBmp.Height), - ImageLockMode.ReadWrite, tmpBmp.PixelFormat); - for (int j = 0, offset = 0; j < rect.Height; j++) + Bitmap tmpBmp = null; + BitmapData tmpData = null; + long length; + + try { - int blockOffset = (stride*(rect.Y + j)) + (pixelSize*rect.X); - memcpy((byte*) tmpData.Scan0.ToPointer() + offset, pScan0 + blockOffset, (uint) blockStride); + tmpBmp = new Bitmap(rect.Width, rect.Height, format); + tmpData = tmpBmp.LockBits(new Rectangle(0, 0, tmpBmp.Width, tmpBmp.Height), + ImageLockMode.ReadWrite, tmpBmp.PixelFormat); + + for (int j = 0, offset = 0; j < rect.Height; j++) + { + int blockOffset = (stride * (rect.Y + j)) + (pixelSize * rect.X); + memcpy((byte*)tmpData.Scan0.ToPointer() + offset, pScan0 + blockOffset, (uint)blockStride); //copy-changes - offset += blockStride; + offset += blockStride; + } + + outStream.Write(BitConverter.GetBytes(rect.X), 0, 4); + outStream.Write(BitConverter.GetBytes(rect.Y), 0, 4); + outStream.Write(BitConverter.GetBytes(rect.Width), 0, 4); + outStream.Write(BitConverter.GetBytes(rect.Height), 0, 4); + outStream.Write(new byte[4], 0, 4); + + length = outStream.Length; + long OldPos = outStream.Position; + + _jpgCompression.Compress(tmpBmp, ref outStream); + + length = outStream.Position - length; + + outStream.Position = OldPos - 4; + outStream.Write(BitConverter.GetBytes(length), 0, 4); + outStream.Position += length; + } + finally + { + tmpBmp.UnlockBits(tmpData); + tmpBmp.Dispose(); } - tmpBmp.UnlockBits(tmpData); - outStream.Write(BitConverter.GetBytes(rect.X), 0, 4); - outStream.Write(BitConverter.GetBytes(rect.Y), 0, 4); - outStream.Write(BitConverter.GetBytes(rect.Width), 0, 4); - outStream.Write(BitConverter.GetBytes(rect.Height), 0, 4); - outStream.Write(new byte[4], 0, 4); - - long length = outStream.Length; - long OldPos = outStream.Position; - - _jpgCompression.Compress(tmpBmp, ref outStream); - - length = outStream.Position - length; - - outStream.Position = OldPos - 4; - outStream.Write(BitConverter.GetBytes((int) length), 0, 4); - outStream.Position += length; - tmpBmp.Dispose(); - totalDataLength += (int) length + (4*5); + totalDataLength += length + (4 * 5); } outStream.Position = oldPos; @@ -245,24 +273,32 @@ public UnsafeStreamCodec(int imageQuality, int monitor) } } - public unsafe Bitmap DecodeData(IntPtr CodecBuffer, uint Length) + public unsafe Bitmap DecodeData(IntPtr codecBuffer, uint length) { - if (Length < 4) + if (length < 4) + { return _decodedBitmap; + } + + int dataSize = *(int*)(codecBuffer); - int dataSize = *(int*) (CodecBuffer); if (_decodedBitmap == null) { byte[] temp = new byte[dataSize]; + fixed (byte* tempPtr = temp) { - memcpy(new IntPtr(tempPtr), new IntPtr(CodecBuffer.ToInt32() + 4), (uint) dataSize); + memcpy(new IntPtr(tempPtr), new IntPtr(codecBuffer.ToInt32() + 4), (uint)dataSize); } - this._decodedBitmap = (Bitmap) Bitmap.FromStream(new MemoryStream(temp)); + this._decodedBitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(temp)); + + return _decodedBitmap; + } + else + { return _decodedBitmap; } - return _decodedBitmap; } public Bitmap DecodeData(Stream inStream) @@ -275,7 +311,8 @@ public Bitmap DecodeData(Stream inStream) { temp = new byte[dataSize]; inStream.Read(temp, 0, temp.Length); - this._decodedBitmap = (Bitmap) Bitmap.FromStream(new MemoryStream(temp)); + this._decodedBitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(temp)); + return _decodedBitmap; } @@ -283,24 +320,28 @@ public Bitmap DecodeData(Stream inStream) { while (dataSize > 0) { - byte[] tempData = new byte[4*5]; + byte[] tempData = new byte[4 * 5]; inStream.Read(tempData, 0, tempData.Length); Rectangle rect = new Rectangle(BitConverter.ToInt32(tempData, 0), BitConverter.ToInt32(tempData, 4), - BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); + BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); int updateLen = BitConverter.ToInt32(tempData, 16); byte[] buffer = new byte[updateLen]; inStream.Read(buffer, 0, buffer.Length); using (MemoryStream m = new MemoryStream(buffer)) - using (Bitmap tmp = (Bitmap) Image.FromStream(m)) { - g.DrawImage(tmp, rect.Location); + using (Bitmap tmp = (Bitmap)Image.FromStream(m)) + { + g.DrawImage(tmp, rect.Location); + } } - dataSize -= updateLen + (4*5); + + dataSize -= updateLen + (4 * 5); } } + return _decodedBitmap; } } diff --git a/Server/Core/Helper/UnsafeStreamCodec.cs b/Server/Core/Helper/UnsafeStreamCodec.cs index 72485c38..7799e0ab 100644 --- a/Server/Core/Helper/UnsafeStreamCodec.cs +++ b/Server/Core/Helper/UnsafeStreamCodec.cs @@ -55,14 +55,16 @@ public UnsafeStreamCodec(int imageQuality = 100) this.CheckBlock = new Size(50, 1); } - public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, PixelFormat format, - Stream outStream) + public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, PixelFormat format, Stream outStream) { lock (_imageProcessLock) { - byte* pScan0 = (byte*) scan0.ToInt32(); + byte* pScan0 = (byte*)scan0.ToInt32(); + if (!outStream.CanWrite) + { throw new Exception("Must have access to Write in the Stream"); + } int stride = 0; int rawLength = 0; @@ -82,8 +84,8 @@ public UnsafeStreamCodec(int imageQuality = 100) throw new NotSupportedException(format.ToString()); } - stride = imageSize.Width*pixelSize; - rawLength = stride*imageSize.Height; + stride = imageSize.Width * pixelSize; + rawLength = stride * imageSize.Height; if (_encodeBuffer == null) { @@ -91,6 +93,7 @@ public UnsafeStreamCodec(int imageQuality = 100) this._encodedWidth = imageSize.Width; this._encodedHeight = imageSize.Height; this._encodeBuffer = new byte[rawLength]; + fixed (byte* ptr = _encodeBuffer) { byte[] temp = null; @@ -101,25 +104,28 @@ public UnsafeStreamCodec(int imageQuality = 100) outStream.Write(BitConverter.GetBytes(temp.Length), 0, 4); outStream.Write(temp, 0, temp.Length); - memcpy(new IntPtr(ptr), scan0, (uint) rawLength); + memcpy(new IntPtr(ptr), scan0, (uint)rawLength); } return; } + + if (this._encodedFormat != format) + { + throw new Exception("PixelFormat is not equal to previous Bitmap"); + } + else if (this._encodedWidth != imageSize.Width || this._encodedHeight != imageSize.Height) + { + throw new Exception("Bitmap width/height are not equal to previous bitmap"); + } long oldPos = outStream.Position; outStream.Write(new byte[4], 0, 4); - int totalDataLength = 0; - - if (this._encodedFormat != format) - throw new Exception("PixelFormat is not equal to previous Bitmap"); - - if (this._encodedWidth != imageSize.Width || this._encodedHeight != imageSize.Height) - throw new Exception("Bitmap width/height are not equal to previous bitmap"); + long totalDataLength = 0; List blocks = new List(); Size s = new Size(scanArea.Width, CheckBlock.Height); - Size lastSize = new Size(scanArea.Width%CheckBlock.Width, scanArea.Height%CheckBlock.Height); + Size lastSize = new Size(scanArea.Width % CheckBlock.Width, scanArea.Height % CheckBlock.Height); int lasty = scanArea.Height - lastSize.Height; int lastx = scanArea.Width - lastSize.Width; @@ -128,24 +134,30 @@ public UnsafeStreamCodec(int imageQuality = 100) List finalUpdates = new List(); s = new Size(scanArea.Width, s.Height); + fixed (byte* encBuffer = _encodeBuffer) { var index = 0; - for (int y = scanArea.Y; y != scanArea.Height;) + for (int y = scanArea.Y; y != scanArea.Height; y += s.Height) { if (y == lasty) + { s = new Size(scanArea.Width, lastSize.Height); + } + cBlock = new Rectangle(scanArea.X, y, scanArea.Width, s.Height); - int offset = (y*stride) + (scanArea.X*pixelSize); - if (memcmp(encBuffer + offset, pScan0 + offset, (uint) stride) != 0) + int offset = (y * stride) + (scanArea.X * pixelSize); + + if (memcmp(encBuffer + offset, pScan0 + offset, (uint)stride) != 0) { index = blocks.Count - 1; + if (blocks.Count != 0 && (blocks[index].Y + blocks[index].Height) == cBlock.Y) { cBlock = new Rectangle(blocks[index].X, blocks[index].Y, blocks[index].Width, - blocks[index].Height + cBlock.Height); + blocks[index].Height + cBlock.Height); blocks[index] = cBlock; } else @@ -153,36 +165,41 @@ public UnsafeStreamCodec(int imageQuality = 100) blocks.Add(cBlock); } } - y += s.Height; } for (int i = 0; i < blocks.Count; i++) { s = new Size(CheckBlock.Width, blocks[i].Height); - int x = scanArea.X; - while (x != scanArea.Width) + + for (int x = scanArea.X; x != scanArea.Width; x += s.Width) { if (x == lastx) + { s = new Size(lastSize.Width, blocks[i].Height); + } cBlock = new Rectangle(x, blocks[i].Y, s.Width, blocks[i].Height); bool foundChanges = false; - int blockStride = pixelSize*cBlock.Width; + uint blockStride = (uint)(pixelSize * cBlock.Width); for (int j = 0; j < cBlock.Height; j++) { - int blockOffset = (stride*(cBlock.Y + j)) + (pixelSize*cBlock.X); - if (memcmp(encBuffer + blockOffset, pScan0 + blockOffset, (uint) blockStride) != 0) + int blockOffset = (stride * (cBlock.Y + j)) + (pixelSize * cBlock.X); + + if (memcmp(encBuffer + blockOffset, pScan0 + blockOffset, blockStride) != 0) + { foundChanges = true; - memcpy(encBuffer + blockOffset, pScan0 + blockOffset, (uint) blockStride); - //copy-changes + } + + memcpy(encBuffer + blockOffset, pScan0 + blockOffset, blockStride); + //copy-changes } if (foundChanges) { index = finalUpdates.Count - 1; - if (finalUpdates.Count > 0 && - (finalUpdates[index].X + finalUpdates[index].Width) == cBlock.X) + + if (finalUpdates.Count > 0 && (finalUpdates[index].X + finalUpdates[index].Width) == cBlock.X) { Rectangle rect = finalUpdates[index]; int newWidth = cBlock.Width + rect.Width; @@ -194,7 +211,6 @@ public UnsafeStreamCodec(int imageQuality = 100) finalUpdates.Add(cBlock); } } - x += s.Width; } } } @@ -202,38 +218,50 @@ public UnsafeStreamCodec(int imageQuality = 100) for (int i = 0; i < finalUpdates.Count; i++) { Rectangle rect = finalUpdates[i]; - int blockStride = pixelSize*rect.Width; + int blockStride = pixelSize * rect.Width; - Bitmap tmpBmp = new Bitmap(rect.Width, rect.Height, format); - BitmapData tmpData = tmpBmp.LockBits(new Rectangle(0, 0, tmpBmp.Width, tmpBmp.Height), - ImageLockMode.ReadWrite, tmpBmp.PixelFormat); - for (int j = 0, offset = 0; j < rect.Height; j++) + Bitmap tmpBmp = null; + BitmapData tmpData = null; + long length; + + try { - int blockOffset = (stride*(rect.Y + j)) + (pixelSize*rect.X); - memcpy((byte*) tmpData.Scan0.ToPointer() + offset, pScan0 + blockOffset, (uint) blockStride); + tmpBmp = new Bitmap(rect.Width, rect.Height, format); + tmpData = tmpBmp.LockBits(new Rectangle(0, 0, tmpBmp.Width, tmpBmp.Height), + ImageLockMode.ReadWrite, tmpBmp.PixelFormat); + + for (int j = 0, offset = 0; j < rect.Height; j++) + { + int blockOffset = (stride * (rect.Y + j)) + (pixelSize * rect.X); + memcpy((byte*)tmpData.Scan0.ToPointer() + offset, pScan0 + blockOffset, (uint)blockStride); //copy-changes - offset += blockStride; + offset += blockStride; + } + + outStream.Write(BitConverter.GetBytes(rect.X), 0, 4); + outStream.Write(BitConverter.GetBytes(rect.Y), 0, 4); + outStream.Write(BitConverter.GetBytes(rect.Width), 0, 4); + outStream.Write(BitConverter.GetBytes(rect.Height), 0, 4); + outStream.Write(new byte[4], 0, 4); + + length = outStream.Length; + long OldPos = outStream.Position; + + _jpgCompression.Compress(tmpBmp, ref outStream); + + length = outStream.Position - length; + + outStream.Position = OldPos - 4; + outStream.Write(BitConverter.GetBytes(length), 0, 4); + outStream.Position += length; + } + finally + { + tmpBmp.UnlockBits(tmpData); + tmpBmp.Dispose(); } - tmpBmp.UnlockBits(tmpData); - outStream.Write(BitConverter.GetBytes(rect.X), 0, 4); - outStream.Write(BitConverter.GetBytes(rect.Y), 0, 4); - outStream.Write(BitConverter.GetBytes(rect.Width), 0, 4); - outStream.Write(BitConverter.GetBytes(rect.Height), 0, 4); - outStream.Write(new byte[4], 0, 4); - - long length = outStream.Length; - long OldPos = outStream.Position; - - _jpgCompression.Compress(tmpBmp, ref outStream); - - length = outStream.Position - length; - - outStream.Position = OldPos - 4; - outStream.Write(BitConverter.GetBytes((int) length), 0, 4); - outStream.Position += length; - tmpBmp.Dispose(); - totalDataLength += (int) length + (4*5); + totalDataLength += length + (4 * 5); } outStream.Position = oldPos; @@ -244,21 +272,29 @@ public UnsafeStreamCodec(int imageQuality = 100) public unsafe Bitmap DecodeData(IntPtr codecBuffer, uint length) { if (length < 4) + { return _decodedBitmap; + } + + int dataSize = *(int*)(codecBuffer); - int dataSize = *(int*) (codecBuffer); if (_decodedBitmap == null) { byte[] temp = new byte[dataSize]; + fixed (byte* tempPtr = temp) { - memcpy(new IntPtr(tempPtr), new IntPtr(codecBuffer.ToInt32() + 4), (uint) dataSize); + memcpy(new IntPtr(tempPtr), new IntPtr(codecBuffer.ToInt32() + 4), (uint)dataSize); } - this._decodedBitmap = (Bitmap) Bitmap.FromStream(new MemoryStream(temp)); + this._decodedBitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(temp)); + + return _decodedBitmap; + } + else + { return _decodedBitmap; } - return _decodedBitmap; } public Bitmap DecodeData(Stream inStream) @@ -271,7 +307,8 @@ public Bitmap DecodeData(Stream inStream) { temp = new byte[dataSize]; inStream.Read(temp, 0, temp.Length); - this._decodedBitmap = (Bitmap) Bitmap.FromStream(new MemoryStream(temp)); + this._decodedBitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(temp)); + return _decodedBitmap; } @@ -279,24 +316,28 @@ public Bitmap DecodeData(Stream inStream) { while (dataSize > 0) { - byte[] tempData = new byte[4*5]; + byte[] tempData = new byte[4 * 5]; inStream.Read(tempData, 0, tempData.Length); Rectangle rect = new Rectangle(BitConverter.ToInt32(tempData, 0), BitConverter.ToInt32(tempData, 4), - BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); + BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); int updateLen = BitConverter.ToInt32(tempData, 16); byte[] buffer = new byte[updateLen]; inStream.Read(buffer, 0, buffer.Length); using (MemoryStream m = new MemoryStream(buffer)) - using (Bitmap tmp = (Bitmap) Image.FromStream(m)) { - g.DrawImage(tmp, rect.Location); + using (Bitmap tmp = (Bitmap)Image.FromStream(m)) + { + g.DrawImage(tmp, rect.Location); + } } - dataSize -= updateLen + (4*5); + + dataSize -= updateLen + (4 * 5); } } + return _decodedBitmap; } } From af20dd54e32299efbe458cb49718e7c811a58fff Mon Sep 17 00:00:00 2001 From: yankejustin Date: Mon, 18 May 2015 01:10:25 -0400 Subject: [PATCH 5/6] Disposing IDisposable Resources Dispose of unmanaged resources used by the server's UnsafeStreamCodec and JpgCompression (because it is pretty related to the UnsafeStreamCodec). Made sure that the UnsafeStreamCodec was being disposed of before being re-assigned. Note: I did not reflect these changes to the client's UnsafeStreamCodec because the implementation of the client's codec is a bit different in the CommandHandler. This different implementation caused instability in the client. In the server, I noticed decreased memory consumption and decreased fluctuation in the memory used. --- Server/Core/Commands/SurveillanceHandler.cs | 10 +++++++ Server/Core/Compression/JpgCompression.cs | 23 ++++++++++++-- Server/Core/Helper/UnsafeStreamCodec.cs | 33 ++++++++++++++++++++- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/Server/Core/Commands/SurveillanceHandler.cs b/Server/Core/Commands/SurveillanceHandler.cs index c4f1c781..cca1b8fa 100644 --- a/Server/Core/Commands/SurveillanceHandler.cs +++ b/Server/Core/Commands/SurveillanceHandler.cs @@ -35,6 +35,11 @@ public static void HandleRemoteDesktopResponse(Client client, DesktopResponse pa // we can not dispose all bitmaps here, cause they are later used again in `client.Value.LastDesktop` if (client.Value.LastDesktop == null) { + if (client.Value.StreamCodec != null) + { + client.Value.StreamCodec.Dispose(); + } + client.Value.StreamCodec = new UnsafeStreamCodec(); if (client.Value.LastQuality != packet.Quality || client.Value.LastMonitor != packet.Monitor) { @@ -69,6 +74,11 @@ public static void HandleRemoteDesktopResponse(Client client, DesktopResponse pa { if (client.Value.LastQuality != packet.Quality || client.Value.LastMonitor != packet.Monitor) { + if (client.Value.StreamCodec != null) + { + client.Value.StreamCodec.Dispose(); + } + client.Value.StreamCodec = new UnsafeStreamCodec(); client.Value.LastQuality = packet.Quality; client.Value.LastMonitor = packet.Monitor; diff --git a/Server/Core/Compression/JpgCompression.cs b/Server/Core/Compression/JpgCompression.cs index 879ac9ab..4b52725a 100644 --- a/Server/Core/Compression/JpgCompression.cs +++ b/Server/Core/Compression/JpgCompression.cs @@ -1,10 +1,11 @@ -using System.Drawing; +using System; +using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace xServer.Core.Compression { - public class JpgCompression + public class JpgCompression : IDisposable { private readonly ImageCodecInfo _encoderInfo; private readonly EncoderParameters _encoderParams; @@ -18,6 +19,24 @@ public JpgCompression(long quality) this._encoderParams.Param[1] = new EncoderParameter(Encoder.Compression, (long) EncoderValue.CompressionRle); } + public void Dispose() + { + Dispose(true); + + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + if (_encoderParams != null) + { + _encoderParams.Dispose(); + } + } + } + public byte[] Compress(Bitmap bmp) { using (MemoryStream stream = new MemoryStream()) diff --git a/Server/Core/Helper/UnsafeStreamCodec.cs b/Server/Core/Helper/UnsafeStreamCodec.cs index 7799e0ab..d270bd1a 100644 --- a/Server/Core/Helper/UnsafeStreamCodec.cs +++ b/Server/Core/Helper/UnsafeStreamCodec.cs @@ -8,7 +8,7 @@ namespace xServer.Core.Helper { - public class UnsafeStreamCodec + public class UnsafeStreamCodec : IDisposable { private int _imageQuality; @@ -20,6 +20,12 @@ private set lock (_imageProcessLock) { _imageQuality = value; + + if (_jpgCompression != null) + { + _jpgCompression.Dispose(); + } + _jpgCompression = new JpgCompression(_imageQuality); } } @@ -55,6 +61,31 @@ public UnsafeStreamCodec(int imageQuality = 100) this.CheckBlock = new Size(50, 1); } + public void Dispose() + { + Dispose(true); + + // Tell the Garbage Collector to not waste time finalizing this object + // since we took care of it. + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposing) + { + if (_decodedBitmap != null) + { + _decodedBitmap.Dispose(); + } + + if (_jpgCompression != null) + { + _jpgCompression.Dispose(); + } + } + } + public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, PixelFormat format, Stream outStream) { lock (_imageProcessLock) From 8866103318f26197acc5ba3576eea243f031d7cc Mon Sep 17 00:00:00 2001 From: MaxXor Date: Mon, 18 May 2015 18:07:22 +0200 Subject: [PATCH 6/6] Reformatted UnsafeStreamCodec --- Client/Core/Helper/UnsafeStreamCodec.cs | 56 ++++++++++++------------ Server/Core/Helper/UnsafeStreamCodec.cs | 58 +++++++++++++------------ 2 files changed, 59 insertions(+), 55 deletions(-) diff --git a/Client/Core/Helper/UnsafeStreamCodec.cs b/Client/Core/Helper/UnsafeStreamCodec.cs index f4d0581f..36bde181 100644 --- a/Client/Core/Helper/UnsafeStreamCodec.cs +++ b/Client/Core/Helper/UnsafeStreamCodec.cs @@ -59,11 +59,12 @@ public UnsafeStreamCodec(int imageQuality, int monitor) this.Monitor = monitor; } - public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, PixelFormat format, Stream outStream) + public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, PixelFormat format, + Stream outStream) { lock (_imageProcessLock) { - byte* pScan0 = (byte*)scan0.ToInt32(); + byte* pScan0 = (byte*) scan0.ToInt32(); if (!outStream.CanWrite) { @@ -88,8 +89,8 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P throw new NotSupportedException(format.ToString()); } - stride = imageSize.Width * pixelSize; - rawLength = stride * imageSize.Height; + stride = imageSize.Width*pixelSize; + rawLength = stride*imageSize.Height; if (_encodeBuffer == null) { @@ -108,7 +109,7 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P outStream.Write(BitConverter.GetBytes(temp.Length), 0, 4); outStream.Write(temp, 0, temp.Length); - memcpy(new IntPtr(ptr), scan0, (uint)rawLength); + memcpy(new IntPtr(ptr), scan0, (uint) rawLength); } return; } @@ -129,7 +130,7 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P List blocks = new List(); Size s = new Size(scanArea.Width, CheckBlock.Height); - Size lastSize = new Size(scanArea.Width % CheckBlock.Width, scanArea.Height % CheckBlock.Height); + Size lastSize = new Size(scanArea.Width%CheckBlock.Width, scanArea.Height%CheckBlock.Height); int lasty = scanArea.Height - lastSize.Height; int lastx = scanArea.Width - lastSize.Width; @@ -152,16 +153,16 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P cBlock = new Rectangle(scanArea.X, y, scanArea.Width, s.Height); - int offset = (y * stride) + (scanArea.X * pixelSize); + int offset = (y*stride) + (scanArea.X*pixelSize); - if (memcmp(encBuffer + offset, pScan0 + offset, (uint)stride) != 0) + if (memcmp(encBuffer + offset, pScan0 + offset, (uint) stride) != 0) { index = blocks.Count - 1; if (blocks.Count != 0 && (blocks[index].Y + blocks[index].Height) == cBlock.Y) { cBlock = new Rectangle(blocks[index].X, blocks[index].Y, blocks[index].Width, - blocks[index].Height + cBlock.Height); + blocks[index].Height + cBlock.Height); blocks[index] = cBlock; } else @@ -184,11 +185,11 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P cBlock = new Rectangle(x, blocks[i].Y, s.Width, blocks[i].Height); bool foundChanges = false; - uint blockStride = (uint)(pixelSize * cBlock.Width); + uint blockStride = (uint) (pixelSize*cBlock.Width); for (int j = 0; j < cBlock.Height; j++) { - int blockOffset = (stride * (cBlock.Y + j)) + (pixelSize * cBlock.X); + int blockOffset = (stride*(cBlock.Y + j)) + (pixelSize*cBlock.X); if (memcmp(encBuffer + blockOffset, pScan0 + blockOffset, blockStride) != 0) { @@ -203,7 +204,8 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P { index = finalUpdates.Count - 1; - if (finalUpdates.Count > 0 && (finalUpdates[index].X + finalUpdates[index].Width) == cBlock.X) + if (finalUpdates.Count > 0 && + (finalUpdates[index].X + finalUpdates[index].Width) == cBlock.X) { Rectangle rect = finalUpdates[index]; int newWidth = cBlock.Width + rect.Width; @@ -222,7 +224,7 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P for (int i = 0; i < finalUpdates.Count; i++) { Rectangle rect = finalUpdates[i]; - int blockStride = pixelSize * rect.Width; + int blockStride = pixelSize*rect.Width; Bitmap tmpBmp = null; BitmapData tmpData = null; @@ -232,12 +234,12 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P { tmpBmp = new Bitmap(rect.Width, rect.Height, format); tmpData = tmpBmp.LockBits(new Rectangle(0, 0, tmpBmp.Width, tmpBmp.Height), - ImageLockMode.ReadWrite, tmpBmp.PixelFormat); + ImageLockMode.ReadWrite, tmpBmp.PixelFormat); for (int j = 0, offset = 0; j < rect.Height; j++) { - int blockOffset = (stride * (rect.Y + j)) + (pixelSize * rect.X); - memcpy((byte*)tmpData.Scan0.ToPointer() + offset, pScan0 + blockOffset, (uint)blockStride); + int blockOffset = (stride*(rect.Y + j)) + (pixelSize*rect.X); + memcpy((byte*) tmpData.Scan0.ToPointer() + offset, pScan0 + blockOffset, (uint) blockStride); //copy-changes offset += blockStride; } @@ -249,13 +251,13 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P outStream.Write(new byte[4], 0, 4); length = outStream.Length; - long OldPos = outStream.Position; + long old = outStream.Position; _jpgCompression.Compress(tmpBmp, ref outStream); length = outStream.Position - length; - outStream.Position = OldPos - 4; + outStream.Position = old - 4; outStream.Write(BitConverter.GetBytes(length), 0, 4); outStream.Position += length; } @@ -265,7 +267,7 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P tmpBmp.Dispose(); } - totalDataLength += length + (4 * 5); + totalDataLength += length + (4*5); } outStream.Position = oldPos; @@ -280,7 +282,7 @@ public unsafe Bitmap DecodeData(IntPtr codecBuffer, uint length) return _decodedBitmap; } - int dataSize = *(int*)(codecBuffer); + int dataSize = *(int*) (codecBuffer); if (_decodedBitmap == null) { @@ -288,10 +290,10 @@ public unsafe Bitmap DecodeData(IntPtr codecBuffer, uint length) fixed (byte* tempPtr = temp) { - memcpy(new IntPtr(tempPtr), new IntPtr(codecBuffer.ToInt32() + 4), (uint)dataSize); + memcpy(new IntPtr(tempPtr), new IntPtr(codecBuffer.ToInt32() + 4), (uint) dataSize); } - this._decodedBitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(temp)); + this._decodedBitmap = (Bitmap) Bitmap.FromStream(new MemoryStream(temp)); return _decodedBitmap; } @@ -311,7 +313,7 @@ public Bitmap DecodeData(Stream inStream) { temp = new byte[dataSize]; inStream.Read(temp, 0, temp.Length); - this._decodedBitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(temp)); + this._decodedBitmap = (Bitmap) Bitmap.FromStream(new MemoryStream(temp)); return _decodedBitmap; } @@ -320,11 +322,11 @@ public Bitmap DecodeData(Stream inStream) { while (dataSize > 0) { - byte[] tempData = new byte[4 * 5]; + byte[] tempData = new byte[4*5]; inStream.Read(tempData, 0, tempData.Length); Rectangle rect = new Rectangle(BitConverter.ToInt32(tempData, 0), BitConverter.ToInt32(tempData, 4), - BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); + BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); int updateLen = BitConverter.ToInt32(tempData, 16); byte[] buffer = new byte[updateLen]; @@ -332,13 +334,13 @@ public Bitmap DecodeData(Stream inStream) using (MemoryStream m = new MemoryStream(buffer)) { - using (Bitmap tmp = (Bitmap)Image.FromStream(m)) + using (Bitmap tmp = (Bitmap) Image.FromStream(m)) { g.DrawImage(tmp, rect.Location); } } - dataSize -= updateLen + (4 * 5); + dataSize -= updateLen + (4*5); } } diff --git a/Server/Core/Helper/UnsafeStreamCodec.cs b/Server/Core/Helper/UnsafeStreamCodec.cs index d270bd1a..ce022e42 100644 --- a/Server/Core/Helper/UnsafeStreamCodec.cs +++ b/Server/Core/Helper/UnsafeStreamCodec.cs @@ -86,11 +86,12 @@ protected virtual void Dispose(bool disposing) } } - public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, PixelFormat format, Stream outStream) + public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, PixelFormat format, + Stream outStream) { lock (_imageProcessLock) { - byte* pScan0 = (byte*)scan0.ToInt32(); + byte* pScan0 = (byte*) scan0.ToInt32(); if (!outStream.CanWrite) { @@ -115,8 +116,8 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P throw new NotSupportedException(format.ToString()); } - stride = imageSize.Width * pixelSize; - rawLength = stride * imageSize.Height; + stride = imageSize.Width*pixelSize; + rawLength = stride*imageSize.Height; if (_encodeBuffer == null) { @@ -135,11 +136,11 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P outStream.Write(BitConverter.GetBytes(temp.Length), 0, 4); outStream.Write(temp, 0, temp.Length); - memcpy(new IntPtr(ptr), scan0, (uint)rawLength); + memcpy(new IntPtr(ptr), scan0, (uint) rawLength); } return; } - + if (this._encodedFormat != format) { throw new Exception("PixelFormat is not equal to previous Bitmap"); @@ -156,7 +157,7 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P List blocks = new List(); Size s = new Size(scanArea.Width, CheckBlock.Height); - Size lastSize = new Size(scanArea.Width % CheckBlock.Width, scanArea.Height % CheckBlock.Height); + Size lastSize = new Size(scanArea.Width%CheckBlock.Width, scanArea.Height%CheckBlock.Height); int lasty = scanArea.Height - lastSize.Height; int lastx = scanArea.Width - lastSize.Width; @@ -179,16 +180,16 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P cBlock = new Rectangle(scanArea.X, y, scanArea.Width, s.Height); - int offset = (y * stride) + (scanArea.X * pixelSize); + int offset = (y*stride) + (scanArea.X*pixelSize); - if (memcmp(encBuffer + offset, pScan0 + offset, (uint)stride) != 0) + if (memcmp(encBuffer + offset, pScan0 + offset, (uint) stride) != 0) { index = blocks.Count - 1; if (blocks.Count != 0 && (blocks[index].Y + blocks[index].Height) == cBlock.Y) { cBlock = new Rectangle(blocks[index].X, blocks[index].Y, blocks[index].Width, - blocks[index].Height + cBlock.Height); + blocks[index].Height + cBlock.Height); blocks[index] = cBlock; } else @@ -211,11 +212,11 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P cBlock = new Rectangle(x, blocks[i].Y, s.Width, blocks[i].Height); bool foundChanges = false; - uint blockStride = (uint)(pixelSize * cBlock.Width); + uint blockStride = (uint) (pixelSize*cBlock.Width); for (int j = 0; j < cBlock.Height; j++) { - int blockOffset = (stride * (cBlock.Y + j)) + (pixelSize * cBlock.X); + int blockOffset = (stride*(cBlock.Y + j)) + (pixelSize*cBlock.X); if (memcmp(encBuffer + blockOffset, pScan0 + blockOffset, blockStride) != 0) { @@ -230,7 +231,8 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P { index = finalUpdates.Count - 1; - if (finalUpdates.Count > 0 && (finalUpdates[index].X + finalUpdates[index].Width) == cBlock.X) + if (finalUpdates.Count > 0 && + (finalUpdates[index].X + finalUpdates[index].Width) == cBlock.X) { Rectangle rect = finalUpdates[index]; int newWidth = cBlock.Width + rect.Width; @@ -249,7 +251,7 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P for (int i = 0; i < finalUpdates.Count; i++) { Rectangle rect = finalUpdates[i]; - int blockStride = pixelSize * rect.Width; + int blockStride = pixelSize*rect.Width; Bitmap tmpBmp = null; BitmapData tmpData = null; @@ -259,12 +261,12 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P { tmpBmp = new Bitmap(rect.Width, rect.Height, format); tmpData = tmpBmp.LockBits(new Rectangle(0, 0, tmpBmp.Width, tmpBmp.Height), - ImageLockMode.ReadWrite, tmpBmp.PixelFormat); + ImageLockMode.ReadWrite, tmpBmp.PixelFormat); for (int j = 0, offset = 0; j < rect.Height; j++) { - int blockOffset = (stride * (rect.Y + j)) + (pixelSize * rect.X); - memcpy((byte*)tmpData.Scan0.ToPointer() + offset, pScan0 + blockOffset, (uint)blockStride); + int blockOffset = (stride*(rect.Y + j)) + (pixelSize*rect.X); + memcpy((byte*) tmpData.Scan0.ToPointer() + offset, pScan0 + blockOffset, (uint) blockStride); //copy-changes offset += blockStride; } @@ -276,13 +278,13 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P outStream.Write(new byte[4], 0, 4); length = outStream.Length; - long OldPos = outStream.Position; + long old = outStream.Position; _jpgCompression.Compress(tmpBmp, ref outStream); length = outStream.Position - length; - outStream.Position = OldPos - 4; + outStream.Position = old - 4; outStream.Write(BitConverter.GetBytes(length), 0, 4); outStream.Position += length; } @@ -292,7 +294,7 @@ public unsafe void CodeImage(IntPtr scan0, Rectangle scanArea, Size imageSize, P tmpBmp.Dispose(); } - totalDataLength += length + (4 * 5); + totalDataLength += length + (4*5); } outStream.Position = oldPos; @@ -307,7 +309,7 @@ public unsafe Bitmap DecodeData(IntPtr codecBuffer, uint length) return _decodedBitmap; } - int dataSize = *(int*)(codecBuffer); + int dataSize = *(int*) (codecBuffer); if (_decodedBitmap == null) { @@ -315,10 +317,10 @@ public unsafe Bitmap DecodeData(IntPtr codecBuffer, uint length) fixed (byte* tempPtr = temp) { - memcpy(new IntPtr(tempPtr), new IntPtr(codecBuffer.ToInt32() + 4), (uint)dataSize); + memcpy(new IntPtr(tempPtr), new IntPtr(codecBuffer.ToInt32() + 4), (uint) dataSize); } - this._decodedBitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(temp)); + this._decodedBitmap = (Bitmap) Bitmap.FromStream(new MemoryStream(temp)); return _decodedBitmap; } @@ -338,7 +340,7 @@ public Bitmap DecodeData(Stream inStream) { temp = new byte[dataSize]; inStream.Read(temp, 0, temp.Length); - this._decodedBitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(temp)); + this._decodedBitmap = (Bitmap) Bitmap.FromStream(new MemoryStream(temp)); return _decodedBitmap; } @@ -347,11 +349,11 @@ public Bitmap DecodeData(Stream inStream) { while (dataSize > 0) { - byte[] tempData = new byte[4 * 5]; + byte[] tempData = new byte[4*5]; inStream.Read(tempData, 0, tempData.Length); Rectangle rect = new Rectangle(BitConverter.ToInt32(tempData, 0), BitConverter.ToInt32(tempData, 4), - BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); + BitConverter.ToInt32(tempData, 8), BitConverter.ToInt32(tempData, 12)); int updateLen = BitConverter.ToInt32(tempData, 16); byte[] buffer = new byte[updateLen]; @@ -359,13 +361,13 @@ public Bitmap DecodeData(Stream inStream) using (MemoryStream m = new MemoryStream(buffer)) { - using (Bitmap tmp = (Bitmap)Image.FromStream(m)) + using (Bitmap tmp = (Bitmap) Image.FromStream(m)) { g.DrawImage(tmp, rect.Location); } } - dataSize -= updateLen + (4 * 5); + dataSize -= updateLen + (4*5); } }