last version


SUBMITTED BY: rickystewart3

DATE: Sept. 20, 2015, 4:01 p.m.

FORMAT: Text only

SIZE: 28.2 kB

HITS: 292

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace first_pr
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void exit()
  20. {
  21. DialogResult rsl = MessageBox.Show("Are you sure that you want to exit?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  22. if (rsl == DialogResult.Yes)
  23. {
  24. Application.Exit();
  25. }
  26. }
  27. private void button3_Click(object sender, EventArgs e)
  28. {
  29. exit();
  30. }
  31. public Image currImage;
  32. static public Image Mem;
  33. private void Load_Image()
  34. {
  35. openFileDialog1.InitialDirectory = "c:";
  36. openFileDialog1.Filter = "image (JPEG) files (*.jpg)|*.jpg|All files (*.*)|*.*|image (BMP) files (*.bmp)|*.bmp|image (PNG) files (*.png)|*.png";
  37. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  38. {
  39. try
  40. {
  41. currImage = Image.FromFile(openFileDialog1.FileName);
  42. Mem = Image.FromFile(openFileDialog1.FileName);
  43. button6.Enabled = true;
  44. if ((currImage.Height <= 256) && (currImage.Width <= 256))
  45. {
  46. pictureBox1.Image = currImage;
  47. }
  48. else
  49. {
  50. DialogResult rsl = MessageBox.Show("Wrong size of picture! Press 'Yes' if you want to continue. Press 'No' if you want to reload image.", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  51. if (rsl == DialogResult.Yes)
  52. {
  53. pictureBox1.Image = currImage;
  54. }
  55. }
  56. }
  57. catch (Exception e)
  58. {
  59. MessageBox.Show("Can't load file.\n" + e.StackTrace, "Warning!",MessageBoxButtons.OK, MessageBoxIcon.Warning);
  60. }
  61. }
  62. }
  63. private void Open_Click(object sender, EventArgs e)
  64. {
  65. Load_Image();
  66. }
  67. private void button2_Click(object sender, EventArgs e)
  68. {
  69. try
  70. {
  71. var bmp = (Bitmap)Mem;
  72. for (int x = 0; x < bmp.Width; ++x)
  73. for (int y = 0; y < bmp.Height; ++y)
  74. {
  75. Color curr = bmp.GetPixel(x, y);
  76. Color next = Color.FromArgb((byte)(1 - curr.R), (byte)(1 - curr.G), (byte)(1 - curr.B)); // RGB -> CMY
  77. bmp.SetPixel(x, y, next);
  78. }
  79. pictureBox2.Image = bmp;
  80. pictureBox3.Image = Mem;
  81. }
  82. catch (Exception ex)
  83. {
  84. MessageBox.Show("Forgot to open file: load it first.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  85. }
  86. }
  87. private void Save_image()
  88. {
  89. if (pictureBox3.Image != null)
  90. {
  91. Bitmap bt = (Bitmap)pictureBox3.Image;
  92. SaveFileDialog sfd = new SaveFileDialog();
  93. sfd.DefaultExt = "bmp";
  94. sfd.Filter = "Image files (*.bmp)|*.bmp|All files (*.*)|*.*";
  95. if (sfd.ShowDialog() == DialogResult.OK)
  96. {
  97. bt.Save(sfd.FileName);
  98. }
  99. }
  100. else
  101. {
  102. throw new Exception("No image");
  103. }
  104. }
  105. private void button1_Click(object sender, EventArgs e)
  106. {
  107. try
  108. {
  109. Save_image();
  110. }
  111. catch (Exception ex)
  112. {
  113. MessageBox.Show(ex.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  114. }
  115. }
  116. private void button4_Click(object sender, EventArgs e)
  117. {
  118. try
  119. {
  120. Bitmap bmp = (Bitmap)Mem;
  121. for (int x = 0; x < bmp.Width; ++x)
  122. for (int y = 0; y < bmp.Height; ++y)
  123. {
  124. Color curr = bmp.GetPixel(x, y);
  125. float r = (11*curr.B + 30*curr.R + 59*curr.G )/100;
  126. Color next = Color.FromArgb((byte) r, (byte) r, (byte) r); // RGB -> BW
  127. bmp.SetPixel(x, y, next);
  128. }
  129. pictureBox2.Image = bmp;
  130. pictureBox3.Image = Mem;
  131. }
  132. catch (Exception ex)
  133. {
  134. MessageBox.Show("Forgot to open file: load it first.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  135. }
  136. }
  137. private void button5_Click(object sender, EventArgs e)
  138. {
  139. try
  140. {
  141. Bitmap bmp = (Bitmap)Mem;
  142. for (int x = 0; x < (bmp.Width); ++x)
  143. for (int y = 0; y < (bmp.Height); ++y)
  144. {
  145. Color curr = bmp.GetPixel(x, y);
  146. float r = (11 * curr.B + 30 * curr.R + 59 * curr.G) / 100;
  147. Color next = Color.FromArgb((byte)r, (byte)r, (byte)r); // RGB -> BW
  148. bmp.SetPixel(x, y, next);
  149. }
  150. pictureBox2.Image = bmp;
  151. pictureBox2.Refresh();
  152. for (int x = 1; x < (bmp.Width - 1); ++x)
  153. for (int y = 1; y < (bmp.Height - 1); ++y)
  154. {
  155. int B1 = bmp.GetPixel(x - 1, y).R + bmp.GetPixel(x + 1, y).R
  156. + bmp.GetPixel(x, y - 1).R + bmp.GetPixel(x, y + 1).R + bmp.GetPixel(x, y).R;
  157. float r = B1 / 5;
  158. Color next = Color.FromArgb((byte)r, (byte)r, (byte)r); // Smooth
  159. bmp.SetPixel(x, y, next);
  160. }
  161. pictureBox3.Image = bmp;
  162. }
  163. catch (Exception ex)
  164. {
  165. MessageBox.Show("Forgot to open file: load it first.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  166. }
  167. }
  168. private void button6_Click(object sender, EventArgs e)
  169. {
  170. Mem = Image.FromFile(openFileDialog1.FileName);
  171. pictureBox2.Image = null;
  172. pictureBox3.Image = null;
  173. pictureBox4.Image = null;
  174. }
  175. public static List<vertex> vertexes = new List<vertex>();
  176. public class vertex
  177. {
  178. public int x;
  179. public int y;
  180. public Color currColor;
  181. public bool free;
  182. public vertex(int _x, int _y, Color _curr)
  183. {
  184. x = _x;
  185. y = _y;
  186. currColor = _curr;
  187. free = true;
  188. }
  189. public bool Equals(vertex other)
  190. {
  191. return ((this.x == other.x) && (this.y == other.y));
  192. }
  193. public override int GetHashCode()
  194. {
  195. return x.GetHashCode() ^ y.GetHashCode();
  196. }
  197. };
  198. public class Triangle
  199. {
  200. public vertex v1;
  201. public vertex v2;
  202. public vertex v3;
  203. public Triangle(vertex _v1, vertex _v2, vertex _v3)
  204. {
  205. v1 = _v1;
  206. v2 = _v2;
  207. v3 = _v3;
  208. }
  209. };
  210. public List<Triangle> triangles = new List<Triangle>();
  211. public static Bitmap bmpLines = new Bitmap(256,256);
  212. public static Graphics graphics = Graphics.FromImage(bmpLines);
  213. public static void initBitmap()
  214. {
  215. for (var i = 0; i < 256; i++)
  216. {
  217. for(var j = 0; j < 256; j++)
  218. {
  219. bmpLines.SetPixel(i, j, Color.White);
  220. }
  221. }
  222. }
  223. public void showTops()
  224. {
  225. Form1.initBitmap();
  226. for (int i = 0; i < vertexes.Count; i++)
  227. {
  228. Form1.bmpLines.SetPixel(vertexes[i].x, vertexes[i].y, vertexes[i].currColor);
  229. }
  230. this.pictureBox4.Image = Form1.bmpLines;
  231. }
  232. /* public static void markRed(int x, int y)
  233. {
  234. bmpLines.SetPixel(x, y, Color.Red);
  235. }*/
  236. abstract class DivideFigure
  237. {
  238. public static int fullSize = 256;
  239. protected int currHight; /// Store hight(Y) of current figure
  240. protected int currWidth; /// Store wigth(X) of current figure
  241. protected int x; /// X coord for upper left point of current figure.
  242. protected int y; /// Y coord for upper left point of current figure.
  243. public Bitmap bmp;
  244. abstract public void divideImage(int bright); /// Divide figure to smaller figures.
  245. public void drawLine(int _x, int _y, int _currWidth, int _currHight, bool vert) ///
  246. {
  247. bmp = (Bitmap)Form1.Mem;
  248. if (vert)
  249. {
  250. for (int i = _y; i < _currHight + y; i++)
  251. {
  252. Form1.bmpLines.SetPixel((_currWidth + x), i, Color.Black);
  253. if ((_currHight == 1 || _currHight == 2) && (_currWidth == 1 || _currWidth == 2))
  254. {
  255. Color curr = bmp.GetPixel((_currWidth + x), i);
  256. vertex M1 = new vertex((_currWidth + x), i, curr);
  257. Form1.vertexes.Add(M1);
  258. //if (_currWidth + x != 256 && _currWidth + x + 1 != 256 && (i + 1) != 256 && (i - 1) >= 0 && (_currWidth + x - 1) >= 0) {
  259. // curr = bmp.GetPixel((_currWidth + x), i + 1);
  260. // Form1.vertexes.Add(new vertex((_currWidth + x), i + 1, curr));
  261. // curr = bmp.GetPixel((_currWidth + x + 1), i + 1);
  262. // Form1.vertexes.Add(new vertex((_currWidth + x + 1), i + 1, curr));
  263. // curr = bmp.GetPixel((_currWidth + x), i - 1);
  264. // Form1.vertexes.Add(new vertex((_currWidth + x), i - 1, curr));
  265. // curr = bmp.GetPixel((_currWidth + x - 1), i - 1);
  266. // Form1.vertexes.Add(new vertex((_currWidth + x - 1), i - 1, curr));
  267. // Form1.bmpLines.SetPixel((_currWidth + x), i, Color.Red);
  268. //}
  269. }
  270. }
  271. }
  272. else
  273. {
  274. for (int i = _x; i < _currWidth + x; i++)
  275. {
  276. Form1.bmpLines.SetPixel(i, _currHight + y, Color.Black);
  277. if ((_currHight == 1 || _currHight == 2) && (_currWidth == 1 || _currWidth == 2))
  278. {
  279. Color curr = Form1.bmpLines.GetPixel(i, _currHight + y);
  280. vertex M1 = new vertex(i, _currHight + y, curr);
  281. Form1.vertexes.Add(M1);
  282. Form1.bmpLines.SetPixel(i, _currHight + y, Color.Red);
  283. if (_currHight + y != 256 && _currHight + y + 1 != 256 && (i + 1) != 256 && (i - 1) >= 0 && (_currHight + y - 1) >= 0)
  284. {
  285. curr = bmp.GetPixel(i + 1, _currHight + y);
  286. Form1.vertexes.Add(new vertex(i + 1, _currHight + y, curr));
  287. curr = bmp.GetPixel((i + 1), _currHight + y + 1);
  288. Form1.vertexes.Add(new vertex((i + 1), _currHight + y + 1, curr));
  289. curr = bmp.GetPixel(i - 1, _currHight + y);
  290. Form1.vertexes.Add(new vertex(i - 1, _currHight + y, curr));
  291. curr = bmp.GetPixel(i - 1, (_currHight + y - 1));
  292. Form1.vertexes.Add(new vertex(i - 1, (_currHight + y - 1), curr));
  293. Form1.bmpLines.SetPixel(i,(_currHight + y), Color.Red);
  294. }
  295. }
  296. }
  297. }
  298. }
  299. public bool checkBrightnes(int x, int y, int currWidth, int currHight, int bright) /// Method that finds two points in figure, that have difference in brightness more than needed.
  300. {
  301. float r = (11 * bmp.GetPixel(x, y).B + 30 * bmp.GetPixel(x, y).R + 59 * bmp.GetPixel(x, y).G) / 100;
  302. for (var i = x; i < (currWidth + x); i++)
  303. {
  304. for (var j = y; j < (currHight + y); j++)
  305. {
  306. Color curr = bmp.GetPixel(i, j);
  307. float r1 = (11 * curr.B + 30 * curr.R + 59 * curr.G) / 100;
  308. if ((r - r1) >= bright)
  309. {
  310. return true;
  311. }
  312. if (r1 > r)
  313. {
  314. r = r1;
  315. }
  316. }
  317. }
  318. return false;
  319. }
  320. };
  321. class Rectangle : DivideFigure
  322. {
  323. private Form1 parent;
  324. private bool lineVert; /// Store if this figure need to divide horisontal or vertical line
  325. public Rectangle(Form1 _parent, int _x, int _y, int _currWidth, int _currHight, bool _lineVert)
  326. {
  327. this.parent = _parent;
  328. this.x = _x;
  329. this.y = _y;
  330. this.currHight = _currHight;
  331. this.currWidth = _currWidth;
  332. this.lineVert = _lineVert;
  333. this.bmp = (Bitmap)Form1.Mem;
  334. }
  335. public override void divideImage(int bright)
  336. {
  337. if (lineVert)
  338. {
  339. currWidth = currWidth / 2;
  340. var R = new Rectangle(parent, x, y, currWidth, currHight, false);
  341. if (R.checkBrightnes(x, y, currWidth, currHight, bright))
  342. {
  343. drawLine(currWidth, y, currWidth, currHight, true);
  344. parent.pictureBox2.Image = Form1.bmpLines;
  345. parent.pictureBox2.Refresh();
  346. R.divideImage(bright);
  347. }
  348. var R1 = new Rectangle(parent, x + currWidth, y, currWidth, currHight, false);
  349. if (R1.checkBrightnes ((x + currWidth), y, currWidth, currHight, bright))
  350. {
  351. drawLine(currWidth, y, currWidth, currHight, true);
  352. // Form1.markRed(x + currWidth, y);
  353. parent.pictureBox2.Refresh();
  354. R1.divideImage(bright);
  355. }
  356. }
  357. else
  358. {
  359. currHight = currHight / 2;
  360. var R = new Rectangle(parent, x, y, currWidth, currHight, true);
  361. if (R.checkBrightnes(x, y, currWidth, currHight, bright))
  362. {
  363. drawLine(x, currHight, currWidth, currHight, false);
  364. parent.pictureBox2.Image = Form1.bmpLines;
  365. parent.pictureBox2.Refresh();
  366. R.divideImage(bright);
  367. }
  368. var R1 = new Rectangle(parent, x, y + currHight, currWidth, currHight, true);
  369. if (R1.checkBrightnes(x, (y + currHight), currWidth, currHight, bright))
  370. {
  371. drawLine(x, currHight + y, currWidth, currHight, false);
  372. //Form1.markRed(x, y + currHight);
  373. parent.pictureBox2.Refresh();
  374. R1.divideImage(bright);
  375. }
  376. }
  377. }
  378. };
  379. private void DivImage_Click(object sender, EventArgs e)
  380. {
  381. Rectangle Rect = new Rectangle(this, 0, 0, 256, 256, true);
  382. initBitmap();
  383. int brightness = (int)numericUpDown1.Value;
  384. if (Rect.checkBrightnes(0, 0, 256, 256, brightness))
  385. {
  386. Rect.divideImage(brightness);
  387. }
  388. vertexes.Add(new vertex(0, 0, ((Bitmap)Mem).GetPixel(0, 0)));
  389. vertexes.Add(new vertex(pictureBox2.Size.Width - 1, 0, ((Bitmap)Mem).GetPixel(pictureBox2.Size.Width - 1, 0)));
  390. vertexes.Add(new vertex(0, pictureBox2.Size.Height - 1, ((Bitmap)Mem).GetPixel(0, pictureBox2.Size.Height - 1)));
  391. vertexes.Add(new vertex(pictureBox2.Size.Width - 1, pictureBox2.Size.Height - 1, ((Bitmap)Mem).GetPixel(pictureBox2.Size.Width - 1, pictureBox2.Size.Height - 1)));
  392. //vertexes.Add(new vertex(100, 100, Color.Black));
  393. //vertexes.Add(new vertex(160, 40, Color.Blue));
  394. //vertexes.Add(new vertex(40, 20, Color.Red));
  395. //vertexes.Add(new vertex(80, 80, Color.Red));
  396. //vertexes.Add(new vertex(60, 130, Color.Black));
  397. //vertexes.Add(new vertex(180, 100, Color.Black));
  398. foreach (vertex v in vertexes)
  399. {
  400. bmpLines.SetPixel(v.x, v.y, v.currColor);
  401. }
  402. pictureBox2.Image = bmpLines;
  403. pictureBox2.Refresh();
  404. showTops();
  405. }
  406. public void drawSide( vertex begin, vertex end)
  407. {
  408. //int k = (begin.y - end.y) / (begin.x - end.x);
  409. //int b = end.y - k * end.x;
  410. //for (int i = begin.x; i < end.x; i++)
  411. //{
  412. // bmpLines.SetPixel(i, k*end.x + b, Color.Black);
  413. //}
  414. int m = vertexes.Count;
  415. graphics.DrawLine(new Pen(Color.Black), new Point(begin.x, begin.y), new Point(end.x, end.y));
  416. this.pictureBox2.Refresh();
  417. }
  418. private int getVertexDir(vertex first, vertex second, vertex third)
  419. {
  420. int turn = (third.x - first.x) * (second.y - first.y) - (third.y - first.y) * (second.x - first.x);
  421. if (turn > 0)
  422. {
  423. return 1;
  424. }
  425. if (turn < 0)
  426. {
  427. return -1;
  428. }
  429. return 0;
  430. }
  431. /// <summary>
  432. /// Choose two points of triangle and add
  433. /// dir: a forbidden one (0 stands for "both allowed")
  434. /// </summary>
  435. public void triangulate(int f, int s, int dir, List<HashSet<int>> matrix)
  436. {
  437. vertex first = vertexes[f];
  438. vertex second = vertexes[s];
  439. first.free = false;
  440. second.free = false;
  441. drawSide(first, second);
  442. bool checkVert = false;
  443. double min = 2;
  444. int n = 0;
  445. for (int m = 0; m < vertexes.Count; m++)
  446. {
  447. if ((dir != 0 && getVertexDir(first, second, vertexes[m]) == dir))
  448. {
  449. continue;
  450. }
  451. if (matrix[f].Contains(m) && matrix[s].Contains(m))
  452. {
  453. triangles.Add(new Triangle(first, second, vertexes[m]));
  454. return;
  455. }
  456. double fs = Math.Pow(first.x - second.x, 2) + Math.Pow(first.y - second.y, 2);
  457. double fm = Math.Pow(first.x - vertexes[m].x, 2) + Math.Pow(first.y - vertexes[m].y, 2);
  458. double ms = Math.Pow(vertexes[m].x - second.x, 2) + Math.Pow(vertexes[m].y - second.y, 2);
  459. double curr = (-fs + fm + ms) / (2 * Math.Sqrt(fm * ms));
  460. if (curr < min)
  461. {
  462. min = curr;
  463. checkVert = true;
  464. n = m;
  465. }
  466. }
  467. if (checkVert)
  468. {
  469. triangles.Add(new Triangle(first, second, vertexes[n]));
  470. matrix[f].Add(n);
  471. matrix[s].Add(n);
  472. matrix[n].Add(f);
  473. matrix[n].Add(s);
  474. drawSide(first, vertexes[n]);
  475. drawSide(second, vertexes[n]);
  476. triangulate(f, n, -getVertexDir(first, second, vertexes[n]), matrix);
  477. triangulate(n, s, -getVertexDir(first, second, vertexes[n]), matrix);
  478. }
  479. }
  480. //void getTriangles(List<HashSet<int>> matrix)
  481. //{
  482. // for (int i = 0; i < matrix.Count ; i++)
  483. // {
  484. // for (int j = 0; j < matrix[i].Count; j++)
  485. // {
  486. // int curr = matrix[i].ElementAt(j);
  487. // if (curr >= i)
  488. // {
  489. // foreach (int elem in matrix[curr])
  490. // {
  491. // for (int m = 0; m < matrix[i].Count ; i++)
  492. // {
  493. // int elem2 = matrix[i].ElementAt(m);
  494. // if (elem == elem2)
  495. // {
  496. // if () // check that we have elem and elem1 in row with number of their common value
  497. // {
  498. // triangles.Add(new Triangle( ))
  499. // }
  500. // }
  501. // }
  502. // }
  503. // }
  504. // }
  505. // }
  506. //}
  507. private bool checkRule()
  508. {
  509. throw new NotImplementedException();
  510. }
  511. public void gradient(Triangle tre)
  512. {
  513. Point[] points = {
  514. new Point(tre.v1.x, tre.v1.y),
  515. new Point(tre.v2.x, tre.v2.y),
  516. new Point(tre.v3.x, tre.v3.y)
  517. };
  518. PathGradientBrush pgbrush = new PathGradientBrush(points);
  519. Color[] mySurroundColor = { tre.v1.currColor, tre.v2.currColor, tre.v3.currColor};
  520. pgbrush.SurroundColors = mySurroundColor;
  521. int averageR = (tre.v1.currColor.R + tre.v2.currColor.R + tre.v3.currColor.R) / 3;
  522. int averageG = (tre.v1.currColor.G + tre.v2.currColor.G + tre.v3.currColor.G) / 3;
  523. int averageB = (tre.v1.currColor.B + tre.v2.currColor.B + tre.v3.currColor.B) / 3;
  524. Color centerCol = Color.FromArgb((byte) averageR, (byte)averageG, (byte)averageB);
  525. pgbrush.CenterColor = centerCol;
  526. pgbrush.SetBlendTriangularShape(1.0f, 1.0f);
  527. graphics.FillPolygon(pgbrush, points);
  528. }
  529. private void Treangulation_Click(object sender, EventArgs e)
  530. {
  531. Bitmap bmpTre = bmpLines;
  532. vertexes = vertexes.Distinct().ToList();
  533. List<HashSet<int>> matrix = new List<HashSet<int>>();
  534. for (int k = 0; k < vertexes.Count; k++)
  535. {
  536. matrix.Add(new HashSet<int>());
  537. }
  538. this.Text = "Triangulation in progress...";
  539. int i = 0, j = 1;
  540. if (vertexes[i].x < vertexes[j].x)
  541. {
  542. int temp = i;
  543. i = j;
  544. j = temp;
  545. }
  546. for (int k = 2; k < vertexes.Count; k++)
  547. {
  548. if (vertexes[k].x > vertexes[i].x)
  549. {
  550. j = i;
  551. i = k;
  552. }
  553. else
  554. {
  555. if (vertexes[k].x > vertexes[j].x)
  556. {
  557. j = k;
  558. }
  559. }
  560. }
  561. triangulate(i, j, 0, matrix);
  562. this.Text = "ImageChanger";
  563. //showTops();
  564. foreach (Triangle curr in triangles)
  565. {
  566. gradient(curr);
  567. pictureBox2.Refresh();
  568. }
  569. }
  570. }
  571. }

comments powered by Disqus