Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** DM Filter Project Starter Code
- /* v3.5
- /* Original code by: Bradley Beth
- /* Update by: Erik Dillaman, January 2016
- **/
- // changelog:
- // 3.1 added e*Pressed switches and noLoop();
- // 3.2 added img.resize() to line 132, for students with more advanced filters
- // 3.25 possible fix for the double-click bug
- // 3.5 double-click bug squashed [Shout out to Mr. Purdy for the squashing!]
- boolean PicLoaded = false;
- boolean Grayscale = false;
- boolean Effect1 = false;
- boolean R = false;
- boolean G = false;
- boolean B = false;
- boolean Effect3 = false;
- boolean gPressed = false;
- boolean e1Pressed = false;
- boolean e2Pressed = false;
- boolean e3Pressed = false;
- int index = 0;
- int picWidth = 0;
- int picHeight = 0;
- PImage img, resetImg;
- /***********************************/
- void setup()
- {
- noLoop();
- size(800, 480);
- background(185);
- textAlign(LEFT);
- textSize(16);
- }
- void draw()
- {
- background(185);
- fill(0);
- rect(0, 0, 649, 480);
- noStroke();
- int picStart = 0;
- int picEnd = 0;
- /* draw buttons */
- stroke(0);
- fill(0);
- textSize(16);
- text("File Operations", 665, 30);
- line(650, 0, 650, 480);
- noStroke();
- fill(255);
- rect(660, 50, 130, 40, 10);
- fill(55);
- text("Load Picture", 675, 75);
- fill(255);
- rect(660, 100, 130, 40, 10);
- fill(55);
- text("Save Picture", 675, 125);
- stroke(0);
- line(650, 150, 800, 150);
- noStroke();
- stroke(0);
- fill(0);
- textSize(16);
- text("Filter Effects", 675, 180);
- line(650, 0, 650, 480);
- noStroke();
- if (Grayscale)
- fill(#FFFF7D); //Effect on means a yellow lighted button
- else
- fill(255);
- rect(660, 200, 130, 40, 10);
- fill(55);
- text("Grayscale", 680, 225);
- if (Effect1)
- fill(#FFFF7D); //Effect on means a yellow lighted button
- else
- fill(255);
- rect(660, 250, 130, 40, 10);
- fill(55);
- text("Cool Dots", 680, 275);
- if (R)
- fill (#FFFF7D); //Effect on means a yellow lighted button
- else
- fill(255);
- rect(660, 300, 40, 40, 10);
- fill(55);
- text("R", 675, 325);
- if (G)
- fill (#FFFF7D); //Effect on means a yellow lighted button
- else
- fill(255);
- rect(705, 300, 40, 40, 10);
- fill(55);
- text("G", 720, 325);
- if (B)
- fill (#FFFF7D); //Effect on means a yellow lighted button
- else
- fill(255);
- rect(750, 300, 40, 40, 10);
- fill(55);
- text("B", 765, 325);
- if (Effect3)
- fill (#FFFF7D); //Effect on means a yellow lighted button
- else
- fill(255);
- rect(660, 350, 130, 40, 10);
- fill(55);
- text("Effect Three", 680, 375);
- fill(185, 0, 203);
- rect(693, 400, 65, 40, 10);
- fill(255);
- text("Reset", 703, 425);
- noStroke();
- textSize(16);
- //The following loads and displays an image file.
- //The image is resized to best fit in a 640x480 frame.
- if (PicLoaded)
- {
- picWidth = img.width;
- picHeight = img.height;
- if (picWidth > 640)
- {
- picHeight = (int)(picHeight*(640.0/picWidth));
- picWidth = 640;
- }
- if (picHeight > 480)
- {
- picWidth = (int)(picWidth*(480.0/picHeight));
- picHeight = 480;
- }
- img.resize(picWidth, picHeight);
- // (640-picWidth)/2, (480-picHeight)/2 to CENTER
- picStart = 0;
- picEnd = picStart+img.width*img.height;
- /***** Effects Code *****/
- /* This sample grayscale code may serve as an example */
- if (Grayscale && !gPressed)
- {
- img.loadPixels();
- int i = picStart;
- while (i < picEnd)
- {
- color c = img.pixels[i];
- float gray = (red(c)+green(c)+blue(c))/3.0; //average the RGB colors
- img.pixels[i] = color(gray, gray, gray);
- i = i + 1;
- }
- gPressed = true;
- }
- if (Effect1 && !e1Pressed)
- {
- img.loadPixels();
- int i = picStart;
- while (i < picEnd)
- {
- color c = img.pixels[i];
- float gray = (red(c)+green(c)+blue(c))+3.0; //average the RGB colors
- img.pixels[i] = color(gray, gray, gray)+6;
- i = i + 90;
- }
- //** Your first filter effect code goes here **//
- e1Pressed = true;
- }
- if (R && !e2Pressed)
- {
- //** Your second filter effect code goes here **//
- e2Pressed = true;
- }
- if (Effect3 && !e3Pressed)
- {
- //** Your third filter effect code goes here **//
- e3Pressed = true;
- }
- img.updatePixels();
- redraw();
- }
- if (img != null) image(img, (640-picWidth)/2, (480-picHeight)/2, picWidth, picHeight);
- fill(255);
- noStroke();
- }
- void mouseClicked() {
- redraw();
- }
- void mousePressed()
- {
- //The following define the clickable bounding boxes for any buttons used.
- //Note that these boundaries should match those drawn in the draw() function.
- if (mouseX>660 && mouseX<790 && mouseY>50 && mouseY<90)
- {
- selectInput("Select a file to process:", "infileSelected");
- }
- if (mouseX>660 && mouseX<790 && mouseY>100 && mouseY<140)
- {
- selectOutput("Select a file to write to:", "outfileSelected");
- }
- if (mouseX>660 && mouseX<790 && mouseY>200 && mouseY<240 && PicLoaded)
- {
- Grayscale = true;
- redraw();
- }
- if (mouseX>660 && mouseX<790 && mouseY>250 && mouseY<290 && PicLoaded)
- {
- Effect1 = true;
- redraw();
- }
- if (mouseX>660 && mouseX<700 && mouseY>300 && mouseY<340 && PicLoaded)
- {
- R = true;
- redraw();
- }
- if (mouseX>705 && mouseX<745 && mouseY>300 && mouseY<340 && PicLoaded)
- {
- G = true;
- redraw();
- }
- if (mouseX>750 && mouseX<790 && mouseY>300 && mouseY<340 && PicLoaded)
- {
- B = true;
- redraw();
- }
- if (mouseX>660 && mouseX<790 && mouseY>350 && mouseY<390 && PicLoaded)
- {
- Effect3 = true;
- redraw();
- }
- if (mouseX>693 && mouseX<758 && mouseY>400 && mouseY<440 && PicLoaded)
- {
- resetTheImage();
- redraw();
- }
- }
- void resetTheImage()
- {
- Grayscale = false;
- Effect1 = false;
- R = false;
- G = false;
- B = false;
- Effect3 = false;
- gPressed = false;
- e1Pressed = false;
- e2Pressed = false;
- e3Pressed = false;
- if (PicLoaded) img = resetImg.get();
- }
- void infileSelected(File selection)
- {
- if (selection == null)
- {
- println("IMAGE NOT LOADED: Window was closed or the user hit cancel.");
- } else
- {
- resetTheImage();
- println("IMAGE LOADED: User selected " + selection.getAbsolutePath());
- img = loadImage(selection.getAbsolutePath());
- resetImg = loadImage(selection.getAbsolutePath());
- PicLoaded = true;
- redraw();
- }
- }
- void outfileSelected(File selection)
- {
- if (selection == null)
- {
- println("IMAGE NOT SAVED: Window was closed or the user hit cancel.");
- } else
- {
- println("IMAGE SAVED: User selected " + selection.getAbsolutePath());
- // updatePixels();
- // redraw();
- img.save(selection.getAbsolutePath());
- redraw();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement