package imagetools;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
public class Inverter implements Proximity{
private Image image=Image.getInstance();
/**
* Empty Constructor
*/
public Inverter(){
// TODO empty constructor
}
/**
* Constructor with Buffered Image
*/
public Inverter(BufferedImage img) {
// TODO constructor with only buffered image
if (img != null) {
image.setImage(img);
} else {
// without an image provided, throw a new exception
try {
throw new NoImgException();
} catch (NoImgException e) {
e.printStackTrace();
}
}
}
/**
* Constructor with Buffered Image and image path
* @param img
* @param inpath
*/
public Inverter(BufferedImage img, String inpath) {
// TODO set image with BufferedImage and run blur
if (img != null) {
image.setPath(inpath);
image.setImage(img);
} else {
// without an image provided, throw a new exception
try {
throw new NoImgException();
} catch (NoImgException e) {
e.printStackTrace();
}
}
}
/**
* Constructor with file path
*/
public Inverter(String inpath) {
// TODO constructor that takes in the path and attempts to create an
// image and run blur
Pattern p=Pattern.compile("(?mi)(\\.jpg|\\.jpeg|\\.gif|\\.bmp)");
Matcher m=p.matcher(inpath);
if(m.find()){
image.setPath(inpath);
try {
image.setImage(ImageIO.read(new File(image.getPath())));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try{
throw new FileTypeException("Image must be a jpg, gif, bmp, or jpeg");
}catch(FileTypeException e){
e.printStackTrace();
}
}
}
/**
* Set image from file path
*/
@Override
public void setImage(String inpath) {
// TODO set image via string and run blur
try {
image.setImage(ImageIO.read(new File(inpath)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Get the buffered image
*/
@Override
public BufferedImage getBufferedImage() {
// TODO Auto-generated method stub
return image.getImage();
}
/**
* Set Image from Buffered Image
*/
@Override
public void setImage(BufferedImage inimg) {
// TODO set image via buffered image
if (inimg != null) {
image.setImage(inimg);
} else {
try {
throw new NoImgException();
} catch (NoImgException e) {
e.printStackTrace();
}
}
}
/**
* Set the image path
*/
@Override
public void setPath(String inpath){
image.setPath(inpath);
}
/**
* Get the image path
*/
@Override
public String getPath(){
return image.getPath();
}
/**
* Get the Image object
*/
@Override
public Image getImage(){
return image;
}
/**
* Invert the image colors
*/
public void invert() {
//TODO inver the colors
invert_colors();
}
/**
* Private method that inverts the colors
*/
private void invert_colors(){
//TODO inver the image colors
BufferedImage proxyimage=image.getImage();
int width = proxyimage.getWidth();
int height = proxyimage.getHeight();
int r = 0;
int b = 0;
int g = 0;
for (int i = 0; i < width - 1; i++) {
for (int j = 0; j < height - 1; j++) {
Color c = new Color(proxyimage.getRGB(i, j));
b = c.getBlue();
r = c.getRed();
g = c.getGreen();
if (b >= 150) {
b = 0;
} else if (b < 150) {
b = 255;
}
proxyimage.setRGB(i, j, new Color(r, g, b).getRGB());
}
}
}
/**
* Save the image
*/
@Override
public void save(){
image.save();
}
/**
* Delete the image
*/
@Override
public void delete(){
image.delete();
}
}