Advertisement
DumboWumbo

Scramble Code

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