package imagetools;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
/*
* Brightens an Image by adding a certain number to more.
*
*
* aevans 8/7/2013
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
public class Brighten implements Proximity{
// the image
private Image image=Image.getInstance();
/**
* Empty Constructor
*/
public Brighten() {
// TODO empty constructor for when the Image object is already set
}
/**
* Constructor with Buffered Image
* @param img
*/
public Brighten(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 fpath and buffered image
* @param img
* @param inpath
*/
public Brighten(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 fpath
* @param inpath
*/
public Brighten(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()){
try {
image.setPath(inpath);
image.setImage(ImageIO.read(new File(inpath)));
} 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 the image from a buffered image
*/
@Override
public void setImage(BufferedImage img) {
// TODO set image from buffered image
image.setImage(img);
}
/**
* Get the Image Object
*/
@Override
public Image getImage(){
return image;
}
/**
* Get the Buffered Image
* @return
*/
public BufferedImage getBufferedImage() {
// TODO returns a buffered image
return image.getImage();
}
/**
* Get the File Path
*/
@Override
public String getPath(){
return image.getPath();
}
/**
* Set the image from a path
*/
@Override
public void setImage(String inpath) {
// TODO set image from path
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 the image path
* @param inpath
*/
@Override
public void setPath(String inpath) {
// TODO set path and get file
image.setPath(inpath);
}
/**
* Set the Image from bytes
* @param bytes
*/
public void setImage(byte[] bytes) {
// TODO set image from bytes (warning: meta data will be stripped)
// byte array to read in image
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
// attempt to read in image
try {
image.setImage(ImageIO.read(bis));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Do brighten
*/
public void brightenImage() {
// TODO calls the brighten script
do_brighten();
}
/**
* Private brighten method
*/
private void do_brighten() {
// TODO brightens the image
BufferedImage proxyimage=image.getImage();
// picture width and height
int width = proxyimage.getWidth();
int height = proxyimage.getHeight();
if (image == null) {
// no image throw exception
try {
throw new NoImgException();
} catch (NoImgException e) {
e.printStackTrace();
System.exit(-1);
}
} else {
// brighten each pixel
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Color c = new Color(proxyimage.getRGB(x, y));
proxyimage.setRGB(x, y, c.brighter().getRGB());
}
}
}
}
/**
* Get the Image as a byte array
* @return
*/
public byte[] getImageBytes() {
// TODO returns a jpeg turned to bytes
// the byte array
byte[] ibytes = null;
// the byte array outputstream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// convert image to byte array
try {
ImageIO.write(image.getImage(), "jpg", baos);
ibytes = baos.toByteArray();
baos.flush();
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return byte array unless failure
return ibytes;
}
/**
* Save the image
*/
@Override
public void save() {
// TODO save the image
image.save();
}
/**
* Delete the image
*/
@Override
public void delete(){
image.delete();
}
}