public static void displayImageInfo(ImageInfo info) {

    ImageSize size = info.getSize();

    Dimension2D dPt = size.getDimensionPt();
    Dimension dPx = size.getDimensionPx();

    log.debug(
        info.getOriginalURI()
            + " "
            + info.getMimeType()
            + " "
            + Math.round(dPx.getWidth())
            + "x"
            + Math.round(dPx.getHeight()));

    log.debug(
        "Resolution:"
            + Math.round(size.getDpiHorizontal())
            + "x"
            + Math.round(size.getDpiVertical()));
    log.debug(
        "Print size: "
            + Math.round(dPt.getWidth() / 72)
            + "\" x"
            + Math.round(dPt.getHeight() / 72)
            + "\"");
  }
    public static CxCy scale(ImageInfo imageInfo, double xEmu, double yEmu) {
      ImageSize size = imageInfo.getSize();
      double iwEmu = toEmu(size.getWidthPx(), size.getDpiHorizontal());
      double ihEmu = toEmu(size.getHeightPx(), size.getDpiVertical());

      return scaleToFit(iwEmu, ihEmu, xEmu, yEmu);
    }
  /**
   * Create a <wp:inline> element suitable for this image, which can be _embedded_ in
   * w:p/w:r/w:drawing.
   *
   * @param filenameHint Any text, for example the original filename
   * @param altText Like HTML's alt text
   * @param id1 An id unique in the document
   * @param id2 Another id unique in the document
   * @param cx Image width in twip
   * @param link true if this is to be linked not embedded None of these things seem to be exposed
   *     in Word 2007's user interface, but Word won't open the document if any of the attributes
   *     these go in (except @ desc) aren't present!
   * @throws Exception
   */
  public Inline createImageInline(
      String filenameHint, String altText, int id1, int id2, long cx, boolean link)
      throws Exception {

    ImageSize size = imageInfo.getSize();

    Dimension2D dPt = size.getDimensionPt();
    double imageWidthTwips = dPt.getWidth() * 20;
    log.debug("imageWidthTwips: " + imageWidthTwips);

    long cy;

    log.debug("Scaling image height to retain aspect ratio");
    cy = UnitsOfMeasurement.twipToEMU(dPt.getHeight() * 20 * cx / imageWidthTwips);

    // Now convert cx to EMU
    cx = UnitsOfMeasurement.twipToEMU(cx);

    log.debug("cx=" + cx + "; cy=" + cy);

    return createImageInline(filenameHint, altText, id1, id2, cx, cy, link);
  }
    public static CxCy scale(ImageInfo imageInfo, PageDimensions page) {

      double writableWidthTwips = page.getWritableWidthTwips();
      log.debug("writableWidthTwips: " + writableWidthTwips);

      ImageSize size = imageInfo.getSize();

      Dimension2D dPt = size.getDimensionPt();
      double imageWidthTwips = dPt.getWidth() * 20;
      log.debug("imageWidthTwips: " + imageWidthTwips);

      long cx;
      long cy;
      boolean scaled = false;
      if (imageWidthTwips > writableWidthTwips) {

        log.debug("Scaling image to fit page width");
        scaled = true;

        cx = UnitsOfMeasurement.twipToEMU(writableWidthTwips);
        cy =
            UnitsOfMeasurement.twipToEMU(
                dPt.getHeight() * 20 * writableWidthTwips / imageWidthTwips);

      } else {

        log.debug("Scaling image - not necessary");

        cx = UnitsOfMeasurement.twipToEMU(imageWidthTwips);
        cy = UnitsOfMeasurement.twipToEMU(dPt.getHeight() * 20);
      }

      log.debug("cx=" + cx + "; cy=" + cy);

      return new CxCy(cx, cy, scaled);
    }
  /** {@inheritDoc} */
  public void activateLayout() {
    initialize();

    FOUserAgent userAgent = pageSeq.getUserAgent();
    ImageManager imageManager = userAgent.getFactory().getImageManager();

    String uri = getExternalDocument().getSrc();
    Integer firstPageIndex = ImageUtil.getPageIndexFromURI(uri);
    boolean hasPageIndex = (firstPageIndex != null);

    try {
      ImageInfo info = imageManager.getImageInfo(uri, userAgent.getImageSessionContext());

      Object moreImages = info.getCustomObjects().get(ImageInfo.HAS_MORE_IMAGES);
      boolean hasMoreImages = moreImages != null && !Boolean.FALSE.equals(moreImages);

      Dimension intrinsicSize = info.getSize().getDimensionMpt();
      ImageLayout layout = new ImageLayout(getExternalDocument(), this, intrinsicSize);

      PageSequence pageSequence = new PageSequence(null);
      transferExtensions(pageSequence);
      areaTreeHandler.getAreaTreeModel().startPageSequence(pageSequence);
      if (log.isDebugEnabled()) {
        log.debug("Starting layout");
      }

      makePageForImage(info, layout);

      if (!hasPageIndex && hasMoreImages) {
        if (log.isTraceEnabled()) {
          log.trace("Starting multi-page processing...");
        }
        URI originalURI;
        try {
          originalURI = new URI(URISpecification.escapeURI(uri));
          int pageIndex = 1;
          while (hasMoreImages) {
            URI tempURI =
                new URI(
                    originalURI.getScheme(),
                    originalURI.getSchemeSpecificPart(),
                    "page=" + Integer.toString(pageIndex + 1));
            if (log.isTraceEnabled()) {
              log.trace("Subimage: " + tempURI.toASCIIString());
            }
            ImageInfo subinfo =
                imageManager.getImageInfo(
                    tempURI.toASCIIString(), userAgent.getImageSessionContext());

            moreImages = subinfo.getCustomObjects().get(ImageInfo.HAS_MORE_IMAGES);
            hasMoreImages = moreImages != null && !Boolean.FALSE.equals(moreImages);

            intrinsicSize = subinfo.getSize().getDimensionMpt();
            layout = new ImageLayout(getExternalDocument(), this, intrinsicSize);

            makePageForImage(subinfo, layout);

            pageIndex++;
          }
        } catch (URISyntaxException e) {
          getResourceEventProducer().uriError(this, uri, e, getExternalDocument().getLocator());
          return;
        }
      }
    } catch (FileNotFoundException fnfe) {
      getResourceEventProducer().imageNotFound(this, uri, fnfe, getExternalDocument().getLocator());
    } catch (IOException ioe) {
      getResourceEventProducer().imageIOError(this, uri, ioe, getExternalDocument().getLocator());
    } catch (ImageException ie) {
      getResourceEventProducer().imageError(this, uri, ie, getExternalDocument().getLocator());
    }
  }