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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
/**
* Sharpen an Image
*
* Sharpens an Image. Useful in testing, reading data, or after denoise.
*
* aevans 8/7/2013
*/
public class Sharpen implements Proximity{
// the buffered image
private Image image=Image.getInstance();
// the path for saving
private String path;
// the sharpen weight (auto is 2)
private int weight = 2;
/**
* Empty Constructor
*/
public Sharpen() {
// TODO empty constructor
}
/**
* Constructor with Buffered Image
* @param img
*/
public Sharpen(BufferedImage img) {
// TODO constructor with 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 the Buffered Image and the file path
* @param img
* @param inpath
*/
public Sharpen(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
* @param inpath
*/
public Sharpen(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 the image from a buffered image
* @param img
*/
@Override
public void setImage(BufferedImage img) {
// TODO set image from buffered image
image.setImage(img);
}
/**
* Set the image from a file 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 file path
*/
public void setPath(String inpath) {
// TODO set path and get file
image.setPath(inpath);
}
/**
* Set the Image froma byte array
* @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();
}
}
/**
* Get the Buffered Image
*/
@Override
public BufferedImage getBufferedImage() {
// TODO returns a buffered image
return image.getImage();
}
/**
* Set the Sharpen weight (int)
* @param inweight
*/
public void setSharpWeight(int inweight) {
// TODO sets the sharpen weight
weight = inweight;
}
/**
* Get the sharpen weight
* @return
*/
public int getSharpWeight() {
// TODO return sharpen weight
return weight;
}
/**
* Get the Image object
*/
@Override
public Image getImage(){
return image;
}
/**
* Return the image path
*/
@Override
public String getPath(){
return image.getPath();
}
/**
* Perform the image sharpening
*/
public void sharpenImage() {
// TODO sharpen an image using a convulution kernel with a weight in the
// center
do_sharpen();
}
/**
* Private method that performs the sharpen
*/
private void do_sharpen() {
// TODO sharpen using a convulution kernel
/*
* Kernel is |-1|-1|-1| |-1|weight|-1||-1|-1|-1|
*/
BufferedImage proxyimage=image.getImage();
// a secondary image for storing new outcomes
BufferedImage image2 = new BufferedImage(proxyimage.getWidth(),proxyimage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
// image width and height
int width = proxyimage.getWidth();
int height = proxyimage.getHeight();
int r = 0;
int g = 0;
int b = 0;
Color c = null;
for (int x = 1; x < width - 1; x++) {
for (int y = 1; y < height - 1; y++) {
// sharpen the image using the kernel (center is c5)
Color c00 = new Color(proxyimage.getRGB(x - 1, y - 1));
Color c01 = new Color(proxyimage.getRGB(x - 1, y));
Color c02 = new Color(proxyimage.getRGB(x - 1, y + 1));
Color c10 = new Color(proxyimage.getRGB(x, y - 1));
Color c11 = new Color(proxyimage.getRGB(x, y));
Color c12 = new Color(proxyimage.getRGB(x, y + 1));
Color c20 = new Color(proxyimage.getRGB(x + 1, y - 1));
Color c21 = new Color(proxyimage.getRGB(x + 1, y));
Color c22 = new Color(proxyimage.getRGB(x + 1, y + 1));
// apply the kernel for r
r = -c00.getRed() - c01.getRed() - c02.getRed() - c10.getRed()
+ (weight * c11.getRed()) - c12.getRed() - c20.getRed()
- c21.getRed() - c22.getRed();
// apply the kernel for g
g = c00.getGreen() - c01.getGreen() - c02.getGreen()
- c10.getGreen() + (weight * c11.getGreen())
- c12.getGreen() - c20.getGreen() - c21.getGreen()
- c22.getGreen();
// apply the transformation for b
b = c00.getBlue() - c01.getBlue() - c02.getBlue()
- c10.getBlue() + (weight * c11.getBlue())
- c12.getBlue() - c20.getBlue() - c21.getBlue()
- c22.getBlue();
// set the new rgb values
r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));
c = new Color(r, g, b);
// set the new mask colors in the new image
image2.setRGB(x, y, c.getRGB());
}
}
// add the new values back to the original image
Color cmask = null;
Color corig = null;
for (int x = 1; x < width - 1; x++) {
for (int y = 1; y < height - 1; y++) {
// get the 2 colors
cmask = new Color(image2.getRGB(x, y));
corig = new Color(proxyimage.getRGB(x, y));
// add the new values
r = cmask.getRed() + corig.getRed();
g = cmask.getGreen() + corig.getGreen();
b = cmask.getBlue() + corig.getBlue();
// set the new rgb values
r = Math.min(255, Math.max(0, r));
g = Math.min(255, Math.max(0, g));
b = Math.min(255, Math.max(0, b));
proxyimage.setRGB(x, y, new Color(r, g, b).getRGB());
}
}
}
/**
* Get the byte array behind the image
* @return
*/
public byte[] getImageBytes() {
// TODO returns a jpeg turned to bytes
BufferedImage proxyimage=image.getImage();
// the byte array
byte[] ibytes = null;
// the byte array outputstream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// convert image to byte array
try {
ImageIO.write(proxyimage, "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
*/
public void save() {
// TODO save the image
image.save();
}
/**
* Delete the image
*/
@Override
public void delete(){
}
}