package net.sf.jasperreports.engine.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.sf.jasperreports.engine.JRException;

public class JRJdk14ImageEncoder extends JRAbstractImageEncoder {
  public byte[] encode(BufferedImage bi, byte imageType) throws JRException {
    String formatName = null;
    switch (imageType) {
      case 1:
        formatName = "gif";
        break;
      case 3:
        formatName = "png";
        break;
      case 4:
        formatName = "tiff";
        break;
      default:
        formatName = "jpeg";
        break;
    } 
    boolean success = false;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      success = ImageIO.write(bi, formatName, baos);
    } catch (IOException e) {
      throw new JRException(e);
    } 
    if (!success)
      throw new JRException("No appropriate image writer found for the \"" + formatName + "\" format."); 
    return baos.toByteArray();
  }
}
