package org.xbrl.word.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.TranscodingHints;
import org.apache.batik.transcoder.image.ImageTranscoder;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.wmf.tosvg.WMFTranscoder;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang.StringUtils;

public class BatikEMFConvertor {
	public static void main(String[] a) throws Exception {
		String wmfPath = "D:\\image3.wmf";
		FileOutputStream awmfPath = new FileOutputStream("D:\\rr.jpg");
		File wmf = new File(wmfPath);
		FileInputStream wmfStream = new FileInputStream(wmf);
		ByteArrayOutputStream imageOut = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024 * 1024];
		int noOfByteRead = 0;
		while ((noOfByteRead = wmfStream.read()) != -1) {
			imageOut.write(noOfByteRead);
			awmfPath.write(noOfByteRead);
		}
		imageOut.flush();
		awmfPath.flush();
		WMFTranscoder transcoder = new WMFTranscoder();
		TranscodingHints hints = new TranscodingHints();
		hints.put(WMFTranscoder.KEY_HEIGHT, 2000f);
		hints.put(WMFTranscoder.KEY_WIDTH, 2000f);
		transcoder.setTranscodingHints(hints);
		TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(imageOut.toByteArray()));
		ByteArrayOutputStream svg = new ByteArrayOutputStream();
		TranscoderOutput output = new TranscoderOutput(svg);

		transcoder.transcode(input, output);
		String svgFile = StringUtils.replace(wmfPath, "wmf", "svg");
		FileOutputStream fileOut = new FileOutputStream(svgFile);
		fileOut.write(svg.toByteArray());
		fileOut.flush();
		// svg -> jpg
		ImageTranscoder it = new JPEGTranscoder();
		ByteArrayOutputStream jpg = new ByteArrayOutputStream();
		it.transcode(new TranscoderInput(new ByteArrayInputStream(svg.toByteArray())), new TranscoderOutput(jpg));
		String jpgFile = StringUtils.replace(wmfPath, "wmf", "jpg");
		FileOutputStream jpgOut = new FileOutputStream(jpgFile);
		jpgOut.write(jpg.toByteArray());
		jpgOut.flush();
	}
}
