package org.jerrymouse.cc.site.convert.image;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;

import org.jerrymouse.cc.site.bean.Format;
import org.jerrymouse.cc.site.bean.Type;
import org.jerrymouse.cc.site.convert.AbstractConvertor;

public class ImageConvertor extends AbstractConvertor {

	@Override
	public byte[] couvert(byte[] src, Type from, Format format) {
		Type to = format.getType();
		ByteArrayOutputStream out = null;
		try {
			out = new ByteArrayOutputStream();
			BufferedImage image = ImageIO.read(new ByteArrayInputStream(src));
			BufferedImage toImage = new BufferedImage(image.getWidth(), image
					.getHeight(), BufferedImage.TYPE_INT_RGB);
			toImage.createGraphics().drawImage(image, 0, 0, null);
			ImageIO.write(toImage, to.getSuffix(), out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toByteArray();
	}

	@Override
	protected List<Type> input() {
		String[] s = { "jpeg", "png", "bmp", "gif", "wbmp", "tiff", "xpm",
				"tga" };
		return Type.createTypes(s);
	}

	@Override
	protected List<Type> output() {
		String[] s = { "jpeg", "png", "bmp", "gif", "wbmp", "tiff", "xpm",
				"tga" };
		return Type.createTypes(s);
	}

}
