package ru.makingpages.katto;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.MemoryCacheImageOutputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
/**
* User: greg
*/
public class Katto {
private int targetWidth;
private byte[] originalData;
private float quality;
public String FORMAT_NAME = "jpeg";
public float SOFTEN_FACTOR = 0.05f;
public Katto setTargetWidth(int targetWidth) {
this.targetWidth = targetWidth;
return this;
}
public Katto setOriginalData(byte[] originalData) {
this.originalData = originalData;
return this;
}
public Katto setQuality(float quality) {
this.quality = quality;
return this;
}
private void validateParams() {
if (targetWidth <= 0) {
throw new IllegalArgumentException("TargetWidth should be greater then 0");
}
if (originalData == null) {
throw new IllegalArgumentException("OriginalWidth is null");
}
if (quality < 0 || quality > 1) {
throw new IllegalArgumentException("Quality should be between 0 and 1");
}
}
public byte[] resize() throws IOException {
validateParams();
final ImageIcon ii = new ImageIcon(originalData);
final Image i = ii.getImage();
final Image resizedImage;
final int iWidth = i.getWidth(null);
final int iHeight = i.getHeight(null);
int width;
int height;
if (iWidth < targetWidth) {
resizedImage = i;
} else if (iWidth > iHeight) {
width = targetWidth;
height = (targetWidth * iHeight) / iWidth;
resizedImage = i.getScaledInstance(width, height, Image.SCALE_SMOOTH);
} else {
width = (targetWidth * iWidth) / iHeight;
height = targetWidth;
resizedImage = i.getScaledInstance(width, height, Image.SCALE_SMOOTH);
}
final Image temp = new ImageIcon(resizedImage).getImage();
final BufferedImage bufferedImage = new BufferedImage(
temp.getWidth(null),
temp.getHeight(null),
BufferedImage.TYPE_INT_RGB);
final Graphics g = bufferedImage.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
final float[] softenArray = {0, SOFTEN_FACTOR, 0, SOFTEN_FACTOR, 1 - (SOFTEN_FACTOR * 4), SOFTEN_FACTOR, 0, SOFTEN_FACTOR, 0};
final Kernel kernel = new Kernel(3, 3, softenArray);
final ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
final BufferedImage filteredBufferedImage = cOp.filter(bufferedImage, null);
final Iterator it = ImageIO.getImageWritersByFormatName(FORMAT_NAME);
final ImageWriter writer = (ImageWriter) it.next();
final ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final ImageOutputStream imgOut = new MemoryCacheImageOutputStream(byteArrayOutputStream);
writer.setOutput(imgOut);
final IIOImage imageOut = new IIOImage(filteredBufferedImage, null, null);
writer.write(null, imageOut, iwp);
writer.dispose();
return byteArrayOutputStream.toByteArray();
}
}