-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvertPictures.java
More file actions
42 lines (35 loc) · 1.24 KB
/
InvertPictures.java
File metadata and controls
42 lines (35 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* InvertPictures lets you choose an image, invert / negate, and then save it.
*
* @author Andreea Teodor
* @version 2017-AUG-08
*/
import java.io.*;
import edu.duke.*;
public class InvertPictures {
public ImageResource invert(ImageResource inImage){
ImageResource outImage = new ImageResource(inImage.getWidth(), inImage.getHeight());
for (Pixel pixel: outImage.pixels()){
Pixel inPixel = inImage.getPixel(pixel.getX(), pixel.getY());
int invRed = (255 - inPixel.getRed());
int invBlue = (255 - inPixel.getBlue());
int invGreen = (255 - inPixel.getGreen());
pixel.setRed(invRed);
pixel.setBlue(invBlue);
pixel.setGreen(invGreen);
}
return outImage;
}
public void selectAndConvert(){
DirectoryResource dr = new DirectoryResource();
for(File f : dr.selectedFiles()){
ImageResource image = new ImageResource(f);
String fname = image.getFileName();
ImageResource inv = invert(image);
String newName = "inverted-" + fname;
inv.setFileName(newName);
inv.draw();
inv.save();
}
}
}