/**
   * Reads image meta data.
   *
   * @param oimage
   * @return
   */
  public static Map<String, String> readImageData(IIOImage oimage) {
    Map<String, String> dict = new HashMap<String, String>();

    IIOMetadata imageMetadata = oimage.getMetadata();
    if (imageMetadata != null) {
      IIOMetadataNode dimNode = (IIOMetadataNode) imageMetadata.getAsTree("javax_imageio_1.0");
      NodeList nodes = dimNode.getElementsByTagName("HorizontalPixelSize");
      int dpiX;
      if (nodes.getLength() > 0) {
        float dpcWidth = Float.parseFloat(nodes.item(0).getAttributes().item(0).getNodeValue());
        dpiX = (int) Math.round(25.4f / dpcWidth);
      } else {
        dpiX = Toolkit.getDefaultToolkit().getScreenResolution();
      }
      dict.put("dpiX", String.valueOf(dpiX));

      nodes = dimNode.getElementsByTagName("VerticalPixelSize");
      int dpiY;
      if (nodes.getLength() > 0) {
        float dpcHeight = Float.parseFloat(nodes.item(0).getAttributes().item(0).getNodeValue());
        dpiY = (int) Math.round(25.4f / dpcHeight);
      } else {
        dpiY = Toolkit.getDefaultToolkit().getScreenResolution();
      }
      dict.put("dpiY", String.valueOf(dpiY));
    }

    return dict;
  }
Esempio n. 2
0
  /**
   * Writes any extension blocks, the Image Descriptor, and the image data
   *
   * @param iioimage The image and image metadata.
   * @param param The write parameters.
   * @param globalColorTable The Global Color Table.
   * @param sourceBounds The source region.
   * @param destSize The destination dimensions.
   */
  private void writeImage(
      RenderedImage image,
      GIFWritableImageMetadata imageMetadata,
      ImageWriteParam param,
      byte[] globalColorTable,
      Rectangle sourceBounds,
      Dimension destSize)
      throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
      // Create default metadata.
      imageMetadata =
          (GIFWritableImageMetadata) getDefaultImageMetadata(new ImageTypeSpecifier(image), param);

      // Set GraphicControlExtension flag only if there is
      // transparency.
      writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
      // Check for GraphicControlExtension element.
      NodeList list = null;
      try {
        IIOMetadataNode root = (IIOMetadataNode) imageMetadata.getAsTree(IMAGE_METADATA_NAME);
        list = root.getElementsByTagName("GraphicControlExtension");
      } catch (IllegalArgumentException iae) {
        // Should never happen.
      }

      // Set GraphicControlExtension flag if element present.
      writeGraphicsControlExtension = list != null && list.getLength() > 0;

      // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
      // the interlacing is set per the ImageWriteParam mode setting.
      if (param != null && param.canWriteProgressive()) {
        if (param.getProgressiveMode() == ImageWriteParam.MODE_DISABLED) {
          imageMetadata.interlaceFlag = false;
        } else if (param.getProgressiveMode() == ImageWriteParam.MODE_DEFAULT) {
          imageMetadata.interlaceFlag = true;
        }
      }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
      imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
      writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(
            imageMetadata.localColorTable == null
                ? (globalColorTable == null
                    ? sampleModel.getSampleSize(0)
                    : globalColorTable.length / 3)
                : imageMetadata.localColorTable.length / 3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize, param, imageMetadata.interlaceFlag);
  }