/**
   * Convert image formats which are not supported by Word (eg EPS, PDF), into ones which are. This
   * requires ImageMagick to be on your system's path; for EPS and PDF images, Ghostscript is also
   * required.
   *
   * @param is
   * @param os
   * @param density PixelsPerInch
   * @throws IOException
   * @throws InterruptedException
   */
  public static void convertToPNG(InputStream is, OutputStream os, int density)
      throws IOException, InterruptedException {

    /*
     * See http://www.eichberger.de/2006/05/imagemagick-in-servlets.html
     *
     * "Calling 'convert - png:-' as an external command and feeding it the
     * source image as standard input and reading the converted image
     * (in this case png) as standard output"
     *
     */

    log.info("Start ImageMagick...");
    Process p =
        Runtime.getRuntime()
            .exec("imconvert -density " + density + " -units PixelsPerInch - png:-");

    // GraphicsMagick is a little quicker than ImageMagick,
    // but v1.3.3 (of Dec 2008) still has the now fixed in GM bug
    // whereby the right most ~10% of the resulting image is chopped off
    // Process p = Runtime.getRuntime().exec("gm convert -density " + density + " -units
    // PixelsPerInch - png:-");

    /* On Windows, if this results in "Invalid Parameter",
     * then either ImageMagick is not installed,
     * or exec is finding the wrong convert
     * program.  See http://studio.imagemagick.org/pipermail/magick-users/2005-October/016464.html
     * and http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=8324&start=0
     *
     * Rather than use full path, rename convert to imconvert (which Alfresco and others do)
     *
     */

    // initialize Gobblers
    StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), os);
    StreamGobbler errGobbler = new StreamGobbler(p.getErrorStream(), System.err);
    // start them
    inGobbler.start();
    errGobbler.start();

    // p.getOutputStream() is the _output stream_ of the subprocess, so
    // this copies is into the standard input stream of the process
    try {
      copy2(is, new BufferedOutputStream(p.getOutputStream()));
      p.getOutputStream().close();
      log.debug("Image copied...");
    } catch (IOException ioe) {

      ioe.printStackTrace();
      // debug
      copy2(p.getErrorStream(), System.err);
    }

    if (p.waitFor() != 0) {
      log.error("Error");
    }
    log.debug("End Process...");
  }