Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Text;
  8.  
  9. /*
  10. Made BY : Bouderbala Islam
  11. Contact Info :
  12. email : Bouderbalaislam [at] Gmail.com
  13. skype : kanare007
  14. facebook : fb.com/islambdrbl
  15.  
  16. The MIT License (MIT)
  17. Copyright (c) 2016 AnguisCaptor
  18. Permission is hereby granted, free of charge, to any person obtaining a copy
  19. of this software and associated documentation files (the "Software"), to deal
  20. in the Software without restriction, including without limitation the rights
  21. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. copies of the Software, and to permit persons to whom the Software is
  23. furnished to do so, subject to the following conditions:
  24. The above copyright notice and this permission notice shall be included in all
  25. copies or substantial portions of the Software.
  26. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. SOFTWARE.
  33. */
  34.  
  35. namespace IBCodec
  36. {
  37. class IBcodec
  38. {
  39. Bitmap Old; // the image we are going to use as a refrence
  40. byte[] _Old; // byte array contains data of "Bitmap Old"
  41. Bitmap New; // the seconde image , which will be compared to the first one
  42.  
  43. public List<Rectangle> Blocks; // list of rectangles to be transmeted
  44. public List<Stream> BlocksData;// list of images as streams to be transmeted
  45. public int quality; // compression paremeter
  46.  
  47. public void Code(Bitmap N, int q)
  48. {
  49.  
  50. BitmapData NewData = null;
  51. BitmapData OldData = null;
  52. const int nbpp = 4; // number of bytes per pixel
  53. New = N;
  54. int width = New.Width;
  55. int height = New.Height;
  56. int left = width;
  57. int right = 0;
  58. int top = height;
  59. int bottom = 0;
  60.  
  61. List<Rectangle> horizantals = new List<Rectangle>();
  62. Blocks = new List<Rectangle>();
  63. BlocksData = new List<Stream>();
  64.  
  65. int lastX = -1, lastY = -1;
  66. if (Old == null || quality != q)
  67. {
  68. //if ther is no old image to compare to , just send the new one
  69.  
  70. Old = New;
  71. quality = q;
  72. BlocksData.Add(CaptureScreen.Compress(New, q));
  73. Blocks.Add(new Rectangle(0, 0, width, height));
  74. return;
  75. }
  76.  
  77. else
  78. {
  79. try
  80. {
  81.  
  82. NewData = New.LockBits(
  83. new Rectangle(0, 0, New.Width, New.Height),
  84. ImageLockMode.ReadOnly, New.PixelFormat);
  85. OldData = Old.LockBits(
  86. new Rectangle(0, 0, Old.Width, Old.Height),
  87. ImageLockMode.ReadOnly, Old.PixelFormat);
  88.  
  89.  
  90. int strideNew = NewData.Stride / nbpp;
  91. int strideOld = OldData.Stride / nbpp;
  92.  
  93.  
  94. IntPtr scanNew0 = NewData.Scan0;
  95. IntPtr scanOld0 = OldData.Scan0;
  96. _Old = new byte[Old.Width * Old.Height * nbpp];
  97. unsafe
  98. {
  99. int* pNew = (int*)(void*)scanNew0;
  100. int* pPrev = (int*)(void*)scanOld0;
  101. byte* pScanNew0 = (byte*)scanNew0.ToInt32();
  102.  
  103.  
  104. fixed (byte* ptr = _Old)
  105. {
  106.  
  107. NativeMethods.memcpy(new IntPtr(ptr), scanOld0, (uint)(Old.Width * Old.Height * nbpp));
  108.  
  109.  
  110. // we are going to cutt the image in horizontal pieces each time we finde something has changed
  111. for (int y = 0; y < New.Height; ++y)
  112. {
  113. int offset = (y * Old.Width * nbpp);
  114. if (NativeMethods.memcmp(ptr + offset, pScanNew0 + offset, (uint)(Old.Width * nbpp)) != 0)
  115. {
  116. if (y < top) { top = y; }
  117. if (y > bottom) { bottom = y; }
  118. lastY = y;
  119. }
  120.  
  121.  
  122. if ((y - lastY > 0 && lastY != -1) || (lastY == New.Height - 1 && top == 0))
  123. {
  124. horizantals.Add(new Rectangle(0, top, Old.Width, bottom - top + 1));
  125. top = height;
  126. bottom = 0;
  127. lastY = -1;
  128.  
  129. }
  130.  
  131. }
  132.  
  133. // now we are going to cutt each horizantal piece vertically each time we finde something has changed
  134. for (int i = 0; i < horizantals.Count; i++)
  135. {
  136. left = horizantals[i].X + horizantals[i].Width;
  137. right = horizantals[i].X;
  138. for (int x = horizantals[i].X; x < horizantals[i].X + horizantals[i].Width; x++)
  139. {
  140. pNew = (int*)(void*)scanNew0 + (horizantals[i].Y * strideNew);
  141. pPrev = (int*)(void*)scanOld0 + (horizantals[i].Y * strideOld);
  142. for (int y = horizantals[i].Y; y < horizantals[i].Y + horizantals[i].Height; ++y)
  143. {
  144. if ((pNew + x)[0] != (pPrev + x)[0])
  145. {
  146. if (x <= left) { left = x; }
  147. if (x >= right) { right = x; }
  148. lastX = x;
  149. }
  150.  
  151. pNew += strideNew;
  152. pPrev += strideOld;
  153. }
  154.  
  155. if ((x - lastX > 0 && lastX != -1) || (left == horizantals[i].X && right + 1 == horizantals[i].X + horizantals[i].Width + 1))
  156. {
  157. Blocks.Add(new Rectangle(left, horizantals[i].Top, right - left, horizantals[i].Top + horizantals[i].Height));
  158. left = horizantals[i].X + horizantals[i].Width;
  159. right = horizantals[i].X;
  160. lastX = -1;
  161.  
  162.  
  163.  
  164. }
  165.  
  166. }
  167. }
  168.  
  169.  
  170. }
  171.  
  172.  
  173. }
  174.  
  175. }
  176. catch (Exception e)
  177. {
  178. Console.WriteLine("err : " + e.Message);
  179. }
  180. finally
  181. {
  182. if (NewData != null)
  183. {
  184. New.UnlockBits(NewData);
  185. }
  186. if (OldData != null)
  187. {
  188. Old.UnlockBits(OldData);
  189. }
  190. }
  191. // now we have a list of rectangles where changes has happened , so we are going to creat small images from each rectangle
  192. for (int i = 0; i < Blocks.Count; i++)
  193. {
  194. try
  195. {
  196. Rectangle bounds = Blocks[i];
  197. bounds.Width = bounds.Width == 0 ? 1 : bounds.Width = bounds.Width;
  198. bounds.Height = bounds.Width == 0 ? 1 : bounds.Height = bounds.Height;
  199.  
  200. Bitmap diff = new Bitmap(bounds.Width, bounds.Height);
  201. Graphics _graphics = Graphics.FromImage(diff);
  202.  
  203. _graphics.DrawImage(New, 0, 0, bounds, GraphicsUnit.Pixel);
  204.  
  205. BlocksData.Add(CaptureScreen.Compress(diff, q));
  206. _graphics.Flush();
  207. _graphics.Dispose();
  208. diff.Dispose();
  209.  
  210.  
  211. }
  212. catch (Exception e)
  213. {
  214. Console.WriteLine("adding exception : " + e.Message);
  215. }
  216.  
  217.  
  218. }
  219. Old = New; // now the seconde image becomes the refrence
  220. }
  221.  
  222. }
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement