Form1.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. // This is the code for your desktop app.
  11. // Press Ctrl+F5 (or go to Debug > Start Without Debugging) to run your app.
  12. namespace SketchAssistant
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. /**********************************/
  21. /*** CLASS VARIABLES START HERE ***/
  22. /**********************************/
  23. //Different Program States
  24. public enum ProgramState
  25. {
  26. Idle,
  27. Draw,
  28. Delete
  29. }
  30. //Current Program State
  31. private ProgramState currentState;
  32. //Dialog to select a file.
  33. OpenFileDialog openFileDialogLeft = new OpenFileDialog();
  34. //Image loaded on the left
  35. Image leftImage = null;
  36. //Image on the right
  37. Image rightImage = null;
  38. //Current Line being Drawn
  39. List<Point> currentLine;
  40. //All Lines in the current session
  41. List<Tuple<bool,Line>> lineList = new List<Tuple<bool, Line>>();
  42. //Whether the Mouse is currently pressed in the rightPictureBox
  43. bool mousePressed = false;
  44. //The Position of the Cursor in the right picture box
  45. Point currentCursorPosition;
  46. //The Previous Cursor Position in the right picture box
  47. Point previousCursorPosition;
  48. //Queue for the cursorPositions
  49. Queue<Point> cursorPositions = new Queue<Point>();
  50. //The graphic representation of the right image
  51. Graphics graph = null;
  52. //Deletion Matrixes for checking postions of lines in the image
  53. bool[,] isFilledMatrix;
  54. HashSet<int>[,] linesMatrix;
  55. //Size of deletion area
  56. uint deletionSize = 2;
  57. /******************************************/
  58. /*** FORM SPECIFIC FUNCTIONS START HERE ***/
  59. /******************************************/
  60. private void Form1_Load(object sender, EventArgs e)
  61. {
  62. currentState = ProgramState.Idle;
  63. this.DoubleBuffered = true;
  64. }
  65. //Resize Function connected to the form resize event, will refresh the form when it is resized
  66. private void Form1_Resize(object sender, System.EventArgs e)
  67. {
  68. this.Refresh();
  69. }
  70. //Load button, will open an OpenFileDialog
  71. private void loadToolStripMenuItem_Click(object sender, EventArgs e)
  72. {
  73. openFileDialogLeft.Filter = "Image|*.jpg;*.png;*.jpeg";
  74. if(openFileDialogLeft.ShowDialog() == DialogResult.OK)
  75. {
  76. toolStripLoadStatus.Text = openFileDialogLeft.SafeFileName;
  77. leftImage = Image.FromFile(openFileDialogLeft.FileName);
  78. pictureBoxLeft.Image = leftImage;
  79. //Refresh the left image box when the content is changed
  80. this.Refresh();
  81. }
  82. }
  83. //Changes the state of the program to drawing
  84. private void drawButton_Click(object sender, EventArgs e)
  85. {
  86. if(rightImage != null)
  87. {
  88. if (currentState.Equals(ProgramState.Draw))
  89. {
  90. ChangeState(ProgramState.Idle);
  91. }
  92. else
  93. {
  94. ChangeState(ProgramState.Draw);
  95. }
  96. }
  97. }
  98. //Changes the state of the program to deletion
  99. private void deleteButton_Click(object sender, EventArgs e)
  100. {
  101. if (rightImage != null)
  102. {
  103. if (currentState.Equals(ProgramState.Delete))
  104. {
  105. ChangeState(ProgramState.Idle);
  106. }
  107. else
  108. {
  109. ChangeState(ProgramState.Delete);
  110. }
  111. }
  112. }
  113. //get current Mouse positon within the right picture box
  114. private void pictureBoxRight_MouseMove(object sender, MouseEventArgs e)
  115. {
  116. currentCursorPosition = ConvertCoordinates(new Point(e.X, e.Y));
  117. }
  118. //hold left mouse button to draw.
  119. private void pictureBoxRight_MouseDown(object sender, MouseEventArgs e)
  120. {
  121. mousePressed = true;
  122. if (currentState.Equals(ProgramState.Draw))
  123. {
  124. currentLine = new List<Point>();
  125. }
  126. }
  127. //when the picture box is clicked, add a point to the current line. Occurs f.ex. when using drawing tablets
  128. private void pictureBoxRight_Click(object sender, EventArgs e)
  129. {
  130. if (currentState.Equals(ProgramState.Draw))
  131. {
  132. List<Point> singlePoint = new List<Point> { currentCursorPosition };
  133. Line singlePointLine = new Line(singlePoint, lineList.Count);
  134. singlePointLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  135. singlePointLine.DrawLine(graph);
  136. pictureBoxRight.Image = rightImage;
  137. }
  138. }
  139. //Lift left mouse button to stop drawing and add a new Line.
  140. private void pictureBoxRight_MouseUp(object sender, MouseEventArgs e)
  141. {
  142. mousePressed = false;
  143. if (currentState.Equals(ProgramState.Draw) && currentLine.Count > 0)
  144. {
  145. Line newLine = new Line(currentLine, lineList.Count);
  146. lineList.Add(new Tuple<bool, Line>(true, newLine));
  147. newLine.PopulateMatrixes(isFilledMatrix, linesMatrix);
  148. }
  149. }
  150. //Button to create a new Canvas. Will create an empty image
  151. //which is the size of the left image, if there is one.
  152. //If there is no image loaded the canvas will be the size of the right picture box
  153. private void canvasButton_Click(object sender, EventArgs e)
  154. {
  155. DrawEmptyCanvas();
  156. //The following lines cannot be in DrawEmptyCanvas()
  157. isFilledMatrix = new bool[rightImage.Width, rightImage.Height];
  158. linesMatrix = new HashSet<int>[rightImage.Width, rightImage.Height];
  159. }
  160. //add a Point on every tick to the Drawpath
  161. private void mouseTimer_Tick(object sender, EventArgs e)
  162. {
  163. cursorPositions.Enqueue(currentCursorPosition);
  164. previousCursorPosition = cursorPositions.Dequeue();
  165. if (currentState.Equals(ProgramState.Draw) && mousePressed)
  166. {
  167. currentLine.Add(currentCursorPosition);
  168. Line drawline = new Line(currentLine);
  169. drawline.DrawLine(graph);
  170. pictureBoxRight.Image = rightImage;
  171. }
  172. if (currentState.Equals(ProgramState.Delete) && mousePressed)
  173. {
  174. List<Point> uncheckedPoints = Line.BresenhamLineAlgorithm(previousCursorPosition, currentCursorPosition);
  175. foreach (Point currPoint in uncheckedPoints)
  176. {
  177. HashSet<int> linesToDelete = CheckDeletionMatrixesAroundPoint(currPoint, deletionSize);
  178. if (linesToDelete.Count > 0)
  179. {
  180. foreach (int lineID in linesToDelete)
  181. {
  182. lineList[lineID] = new Tuple<bool, Line>(false, lineList[lineID].Item2);
  183. }
  184. RepopulateDeletionMatrixes();
  185. RedrawRightImage();
  186. }
  187. }
  188. }
  189. }
  190. /***********************************/
  191. /*** HELPER FUNCTIONS START HERE ***/
  192. /***********************************/
  193. /// <summary>
  194. /// Creates an empty Canvas
  195. /// </summary>
  196. private void DrawEmptyCanvas()
  197. {
  198. if (leftImage == null)
  199. {
  200. rightImage = new Bitmap(pictureBoxRight.Width, pictureBoxRight.Height);
  201. graph = Graphics.FromImage(rightImage);
  202. graph.FillRectangle(Brushes.White, 0, 0, pictureBoxRight.Width + 10, pictureBoxRight.Height + 10);
  203. pictureBoxRight.Image = rightImage;
  204. }
  205. else
  206. {
  207. rightImage = new Bitmap(leftImage.Width, leftImage.Height);
  208. graph = Graphics.FromImage(rightImage);
  209. graph.FillRectangle(Brushes.White, 0, 0, leftImage.Width + 10, leftImage.Height + 10);
  210. pictureBoxRight.Image = rightImage;
  211. }
  212. this.Refresh();
  213. pictureBoxRight.Refresh();
  214. }
  215. /// <summary>
  216. /// Redraws all lines in lineList, for which their associated boolean value equals true.
  217. /// </summary>
  218. private void RedrawRightImage()
  219. {
  220. DrawEmptyCanvas();
  221. foreach (Tuple<bool, Line> lineBoolTuple in lineList)
  222. {
  223. if (lineBoolTuple.Item1)
  224. {
  225. lineBoolTuple.Item2.DrawLine(graph);
  226. }
  227. }
  228. pictureBoxRight.Refresh();
  229. }
  230. /// <summary>
  231. /// A helper function which handles tasks associated witch changing states,
  232. /// such as checking and unchecking buttons and changing the state.
  233. /// </summary>
  234. /// <param name="newState">The new state of the program</param>
  235. private void ChangeState(ProgramState newState)
  236. {
  237. switch (currentState)
  238. {
  239. case ProgramState.Draw:
  240. drawButton.CheckState = CheckState.Unchecked;
  241. mouseTimer.Enabled = false;
  242. break;
  243. case ProgramState.Delete:
  244. deleteButton.CheckState = CheckState.Unchecked;
  245. mouseTimer.Enabled = false;
  246. break;
  247. default:
  248. break;
  249. }
  250. switch (newState)
  251. {
  252. case ProgramState.Draw:
  253. drawButton.CheckState = CheckState.Checked;
  254. mouseTimer.Enabled = true;
  255. break;
  256. case ProgramState.Delete:
  257. deleteButton.CheckState = CheckState.Checked;
  258. mouseTimer.Enabled = true;
  259. break;
  260. default:
  261. break;
  262. }
  263. currentState = newState;
  264. pictureBoxRight.Refresh();
  265. }
  266. /// <summary>
  267. /// A function that calculates the coordinates of a point on a zoomed in image.
  268. /// </summary>
  269. /// <param name="">The position of the mouse cursor</param>
  270. /// <returns>The real coordinates of the mouse cursor on the image</returns>
  271. private Point ConvertCoordinates(Point cursorPosition)
  272. {
  273. Point realCoordinates = new Point(5,3);
  274. if(pictureBoxRight.Image == null)
  275. {
  276. return cursorPosition;
  277. }
  278. int widthImage = pictureBoxRight.Image.Width;
  279. int heightImage = pictureBoxRight.Image.Height;
  280. int widthBox = pictureBoxRight.Width;
  281. int heightBox = pictureBoxRight.Height;
  282. float imageRatio = (float)widthImage / (float)heightImage;
  283. float containerRatio = (float)widthBox / (float)heightBox;
  284. if (imageRatio >= containerRatio)
  285. {
  286. //Image is wider than it is high
  287. float zoomFactor = (float)widthImage / (float)widthBox;
  288. float scaledHeight = heightImage / zoomFactor;
  289. float filler = (heightBox - scaledHeight) / 2;
  290. realCoordinates.X = (int)(cursorPosition.X * zoomFactor);
  291. realCoordinates.Y = (int)((cursorPosition.Y - filler) * zoomFactor);
  292. }
  293. else
  294. {
  295. //Image is higher than it is wide
  296. float zoomFactor = (float)heightImage / (float)heightBox;
  297. float scaledWidth = widthImage / zoomFactor;
  298. float filler = (widthBox - scaledWidth) / 2;
  299. realCoordinates.X = (int)((cursorPosition.X - filler) * zoomFactor);
  300. realCoordinates.Y = (int)(cursorPosition.Y * zoomFactor);
  301. }
  302. return realCoordinates;
  303. }
  304. /// <summary>
  305. /// A function that populates the matrixes needed for deletion detection with line data.
  306. /// </summary>
  307. private void RepopulateDeletionMatrixes()
  308. {
  309. if(rightImage != null)
  310. {
  311. isFilledMatrix = new bool[rightImage.Width,rightImage.Height];
  312. linesMatrix = new HashSet<int>[rightImage.Width, rightImage.Height];
  313. foreach(Tuple<bool,Line> lineTuple in lineList)
  314. {
  315. if (lineTuple.Item1)
  316. {
  317. lineTuple.Item2.PopulateMatrixes(isFilledMatrix, linesMatrix);
  318. }
  319. }
  320. }
  321. }
  322. /// <summary>
  323. /// A function that checks the deletion matrixes at a certain point
  324. /// and returns all Line ids at that point and in a square around it in a certain range.
  325. /// </summary>
  326. /// <param name="p">The point around which to check.</param>
  327. /// <param name="range">The range around the point. If range is 0, only the point is checked.</param>
  328. /// <returns>A List of all lines.</returns>
  329. private HashSet<int> CheckDeletionMatrixesAroundPoint(Point p, uint range)
  330. {
  331. HashSet<int> returnSet = new HashSet<int>();
  332. if (p.X >= 0 && p.Y >= 0 && p.X < rightImage.Width && p.Y < rightImage.Height)
  333. {
  334. if (isFilledMatrix[p.X, p.Y])
  335. {
  336. returnSet.UnionWith(linesMatrix[p.X, p.Y]);
  337. }
  338. }
  339. for (int x_mod = (int)range*(-1); x_mod < range; x_mod++)
  340. {
  341. for (int y_mod = (int)range * (-1); y_mod < range; y_mod++)
  342. {
  343. if (p.X + x_mod >= 0 && p.Y + y_mod >= 0 && p.X + x_mod < rightImage.Width && p.Y + y_mod < rightImage.Height)
  344. {
  345. if (isFilledMatrix[p.X + x_mod, p.Y + y_mod])
  346. {
  347. returnSet.UnionWith(linesMatrix[p.X + x_mod, p.Y + y_mod]);
  348. }
  349. }
  350. }
  351. }
  352. return returnSet;
  353. }
  354. }
  355. }