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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;

import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.transcoder.image.TIFFTranscoder;
import org.jerrymouse.cc.site.bean.Format;
import org.jerrymouse.cc.site.bean.Type;
import org.jerrymouse.cc.site.convert.AbstractConvertor;

public class SvgConvertor extends AbstractConvertor {
	public byte[] couvert(byte[] src, Type from, Format format) {
		Type to = format.getType();
		Transcoder t = null;
		if (to.equals("jpeg")) {
			t = new JPEGTranscoder();
		} else if (to.equals("png")) {
			t = new PNGTranscoder();
		} else if (to.equals("tiff")) {
			t = new TIFFTranscoder();
		}
		t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));
		TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(
				src));
		ByteArrayOutputStream boutput = new ByteArrayOutputStream();
		TranscoderOutput output = new TranscoderOutput(boutput);
		try {
			t.transcode(input, output);
		} catch (TranscoderException e) {
			e.printStackTrace();
		}
		return boutput.toByteArray();
	}

	@Override
	protected List<Type> input() {
		String[] s = { "svg" };
		return Type.createTypes(s);
	}

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

}
