/*
    Copyright (C) 2004-2011 Sascha Schmidt (www.vision-cloud.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

*/

package de.vc.jfox.tools;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;

import java.awt.Dimension;

import de.vc.jfo.env.JFOConfig;
import de.vc.jfo.tools.ImageMetaData;
import de.vc.jfo.tools.InternalImageTranscoder;
import de.vc.util.log.LogUtil;


/**
 * Simple transcoder for WMF and EMF files that uses JFOs extension
 * package.
 */

public class WmfImageTranscoder extends InternalImageTranscoder {

  //---- Attributes ------------------------------------------------------------

  //---- statics ---------------------------------------------------------------

  //---- Constructors ----------------------------------------------------------

  //---- public  ---------------------------------------------------------------

  public String[] getInputFormats() {
    return new String[] {
        "wmf", "emf"};
  }

  public String getOutputFormat() {
    return "png";
  }


  //---- protected -------------------------------------------------------------

  protected void doTranscode(InputStream input, OutputStream output, ImageMetaData metaData) {

    Iterator writers = ImageIO.getImageWritersByMIMEType("image/png");
    if (!writers.hasNext())
      throw new RuntimeException("Format PNG unsupported by ImageIO API.");
    javax.imageio.ImageWriter writer = (javax.imageio.ImageWriter) writers.next();
    writers=null;

    WmfDecoder decoder = new WmfDecoder(input);
    if (metaData.getImageSize()!=null)
      decoder.setStandardDimension( checkImageSize(metaData.getImageSize()) );

    try {
      ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
      writer.setOutput(imageOut);
      writer.write(createBufferedImage(decoder, metaData));
    }
    catch (IOException ex) {
      LogUtil.logError(JFOConfig.instance().getLogger(),"Exception while converting image from WMF/EMF to PNG",ex);
    }

  }


  //---- package ---------------------------------------------------------------

  //---- privat ----------------------------------------------------------------


}
