Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- boolean PicLoaded = false;
- boolean Grayscale = false;
- boolean Effect1 = false;
- boolean Effect2 = false;
- boolean Effect3 = false;
- boolean gPressed = false;
- boolean e1Pressed = false;
- boolean e2Pressed = false;
- boolean e3Pressed = false;
- 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("Effect One", 680, 275);
- if (Effect2)
- fill (#FFFF7D); //Effect on means a yellow lighted button
- else
- fill(255);
- rect(660, 300, 130, 40, 10);
- fill(55);
- text("Effect Two", 680, 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)
- {
- loadPixels();
- int i = picStart;
- while (i < picEnd)
- {
- for (int y = 0; y < 480; y++) {
- for (int x = 0; x < 640; x++) {
- int ran = x + y*400;
- float r = red(img.pixels[ran]);
- float g = green(img.pixels[ran]);
- float b = blue(img.pixels[ran]);
- fill(r,g,b);
- noStroke();
- rect (x,y,1,1);
- i= i + 1;
- }
- }
- e1Pressed = true;
- }
- if (Effect2 && !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();
- }
- 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<790 && mouseY>300 && mouseY<340 && PicLoaded)
- {
- Effect2 = 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;
- Effect2 = 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