Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. /** DM Filter Project Starter Code
  2. /* v3.5
  3. /* Original code by: Bradley Beth
  4. /* Update by: Erik Dillaman, January 2016
  5. **/
  6.  
  7. // changelog:
  8. // 3.1 added e*Pressed switches and noLoop();
  9. // 3.2 added img.resize() to line 132, for students with more advanced filters
  10. // 3.25 possible fix for the double-click bug
  11. // 3.5 double-click bug squashed [Shout out to Mr. Purdy for the squashing!]
  12.  
  13. boolean PicLoaded = false;
  14. boolean Grayscale = false;
  15. boolean Effect1 = false;
  16. boolean R = false;
  17. boolean G = false;
  18. boolean B = false;
  19. boolean Effect3 = false;
  20. boolean gPressed = false;
  21. boolean e1Pressed = false;
  22. boolean e2Pressed = false;
  23. boolean e3Pressed = false;
  24. int index = 0;
  25. int picWidth = 0;
  26. int picHeight = 0;
  27. PImage img, resetImg;
  28.  
  29. /***********************************/
  30.  
  31. void setup()
  32. {
  33. noLoop();
  34. size(800, 480);
  35. background(185);
  36. textAlign(LEFT);
  37. textSize(16);
  38. }
  39.  
  40. void draw()
  41. {
  42.  
  43. background(185);
  44. fill(0);
  45. rect(0, 0, 649, 480);
  46. noStroke();
  47. int picStart = 0;
  48. int picEnd = 0;
  49.  
  50.  
  51. /* draw buttons */
  52.  
  53. stroke(0);
  54. fill(0);
  55. textSize(16);
  56. text("File Operations", 665, 30);
  57. line(650, 0, 650, 480);
  58. noStroke();
  59.  
  60. fill(255);
  61. rect(660, 50, 130, 40, 10);
  62. fill(55);
  63. text("Load Picture", 675, 75);
  64.  
  65. fill(255);
  66. rect(660, 100, 130, 40, 10);
  67. fill(55);
  68. text("Save Picture", 675, 125);
  69.  
  70. stroke(0);
  71. line(650, 150, 800, 150);
  72. noStroke();
  73.  
  74. stroke(0);
  75. fill(0);
  76. textSize(16);
  77. text("Filter Effects", 675, 180);
  78. line(650, 0, 650, 480);
  79. noStroke();
  80.  
  81. if (Grayscale)
  82. fill(#FFFF7D); //Effect on means a yellow lighted button
  83. else
  84. fill(255);
  85. rect(660, 200, 130, 40, 10);
  86. fill(55);
  87. text("Grayscale", 680, 225);
  88.  
  89. if (Effect1)
  90. fill(#FFFF7D); //Effect on means a yellow lighted button
  91. else
  92. fill(255);
  93. rect(660, 250, 130, 40, 10);
  94. fill(55);
  95. text("Cool Dots", 680, 275);
  96.  
  97. if (R)
  98. fill (#FFFF7D); //Effect on means a yellow lighted button
  99. else
  100. fill(255);
  101. rect(660, 300, 40, 40, 10);
  102. fill(55);
  103. text("R", 675, 325);
  104.  
  105. if (G)
  106. fill (#FFFF7D); //Effect on means a yellow lighted button
  107. else
  108. fill(255);
  109. rect(705, 300, 40, 40, 10);
  110. fill(55);
  111. text("G", 720, 325);
  112.  
  113. if (B)
  114. fill (#FFFF7D); //Effect on means a yellow lighted button
  115. else
  116. fill(255);
  117. rect(750, 300, 40, 40, 10);
  118. fill(55);
  119. text("B", 765, 325);
  120.  
  121. if (Effect3)
  122. fill (#FFFF7D); //Effect on means a yellow lighted button
  123. else
  124. fill(255);
  125. rect(660, 350, 130, 40, 10);
  126. fill(55);
  127. text("Effect Three", 680, 375);
  128.  
  129. fill(185, 0, 203);
  130. rect(693, 400, 65, 40, 10);
  131. fill(255);
  132. text("Reset", 703, 425);
  133.  
  134. noStroke();
  135. textSize(16);
  136.  
  137. //The following loads and displays an image file.
  138. //The image is resized to best fit in a 640x480 frame.
  139. if (PicLoaded)
  140. {
  141. picWidth = img.width;
  142. picHeight = img.height;
  143.  
  144. if (picWidth > 640)
  145. {
  146. picHeight = (int)(picHeight*(640.0/picWidth));
  147. picWidth = 640;
  148. }
  149. if (picHeight > 480)
  150. {
  151. picWidth = (int)(picWidth*(480.0/picHeight));
  152. picHeight = 480;
  153. }
  154. img.resize(picWidth, picHeight);
  155. // (640-picWidth)/2, (480-picHeight)/2 to CENTER
  156. picStart = 0;
  157. picEnd = picStart+img.width*img.height;
  158.  
  159.  
  160. /***** Effects Code *****/
  161. /* This sample grayscale code may serve as an example */
  162. if (Grayscale && !gPressed)
  163. {
  164. img.loadPixels();
  165. int i = picStart;
  166. while (i < picEnd)
  167. {
  168. color c = img.pixels[i];
  169. float gray = (red(c)+green(c)+blue(c))/3.0; //average the RGB colors
  170. img.pixels[i] = color(gray, gray, gray);
  171. i = i + 1;
  172. }
  173. gPressed = true;
  174. }
  175.  
  176. if (Effect1 && !e1Pressed)
  177. {
  178. img.loadPixels();
  179. int i = picStart;
  180. while (i < picEnd)
  181. {
  182. color c = img.pixels[i];
  183. float gray = (red(c)+green(c)+blue(c))+3.0; //average the RGB colors
  184. img.pixels[i] = color(gray, gray, gray)+6;
  185. i = i + 90;
  186. }
  187.  
  188. //** Your first filter effect code goes here **//
  189.  
  190. e1Pressed = true;
  191. }
  192.  
  193. if (R && !e2Pressed)
  194. {
  195. //** Your second filter effect code goes here **//
  196.  
  197. e2Pressed = true;
  198. }
  199.  
  200. if (Effect3 && !e3Pressed)
  201. {
  202. //** Your third filter effect code goes here **//
  203.  
  204. e3Pressed = true;
  205. }
  206.  
  207. img.updatePixels();
  208. redraw();
  209. }
  210.  
  211. if (img != null) image(img, (640-picWidth)/2, (480-picHeight)/2, picWidth, picHeight);
  212. fill(255);
  213. noStroke();
  214. }
  215.  
  216. void mouseClicked() {
  217. redraw();
  218. }
  219.  
  220. void mousePressed()
  221. {
  222. //The following define the clickable bounding boxes for any buttons used.
  223. //Note that these boundaries should match those drawn in the draw() function.
  224.  
  225. if (mouseX>660 && mouseX<790 && mouseY>50 && mouseY<90)
  226. {
  227. selectInput("Select a file to process:", "infileSelected");
  228. }
  229.  
  230. if (mouseX>660 && mouseX<790 && mouseY>100 && mouseY<140)
  231. {
  232. selectOutput("Select a file to write to:", "outfileSelected");
  233. }
  234.  
  235. if (mouseX>660 && mouseX<790 && mouseY>200 && mouseY<240 && PicLoaded)
  236. {
  237. Grayscale = true;
  238. redraw();
  239. }
  240.  
  241. if (mouseX>660 && mouseX<790 && mouseY>250 && mouseY<290 && PicLoaded)
  242. {
  243. Effect1 = true;
  244. redraw();
  245. }
  246.  
  247. if (mouseX>660 && mouseX<700 && mouseY>300 && mouseY<340 && PicLoaded)
  248. {
  249. R = true;
  250. redraw();
  251. }
  252.  
  253. if (mouseX>705 && mouseX<745 && mouseY>300 && mouseY<340 && PicLoaded)
  254. {
  255. G = true;
  256. redraw();
  257. }
  258.  
  259. if (mouseX>750 && mouseX<790 && mouseY>300 && mouseY<340 && PicLoaded)
  260. {
  261. B = true;
  262. redraw();
  263. }
  264.  
  265. if (mouseX>660 && mouseX<790 && mouseY>350 && mouseY<390 && PicLoaded)
  266. {
  267. Effect3 = true;
  268. redraw();
  269. }
  270.  
  271. if (mouseX>693 && mouseX<758 && mouseY>400 && mouseY<440 && PicLoaded)
  272. {
  273. resetTheImage();
  274. redraw();
  275. }
  276.  
  277.  
  278. }
  279.  
  280. void resetTheImage()
  281. {
  282. Grayscale = false;
  283. Effect1 = false;
  284. R = false;
  285. G = false;
  286. B = false;
  287. Effect3 = false;
  288. gPressed = false;
  289. e1Pressed = false;
  290. e2Pressed = false;
  291. e3Pressed = false;
  292. if (PicLoaded) img = resetImg.get();
  293. }
  294.  
  295. void infileSelected(File selection)
  296. {
  297. if (selection == null)
  298. {
  299. println("IMAGE NOT LOADED: Window was closed or the user hit cancel.");
  300. } else
  301. {
  302. resetTheImage();
  303. println("IMAGE LOADED: User selected " + selection.getAbsolutePath());
  304. img = loadImage(selection.getAbsolutePath());
  305. resetImg = loadImage(selection.getAbsolutePath());
  306. PicLoaded = true;
  307. redraw();
  308. }
  309. }
  310.  
  311. void outfileSelected(File selection)
  312. {
  313. if (selection == null)
  314. {
  315. println("IMAGE NOT SAVED: Window was closed or the user hit cancel.");
  316. } else
  317. {
  318. println("IMAGE SAVED: User selected " + selection.getAbsolutePath());
  319. // updatePixels();
  320. // redraw();
  321. img.save(selection.getAbsolutePath());
  322. redraw();
  323. }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement