Class ImageMetadataUtils

java.lang.Object
uno.anahata.asi.internal.ImageMetadataUtils

public final class ImageMetadataUtils extends Object
High-performance, lightweight utilities for extracting image metadata.

This class uses a header-only image stream reader to parse dimensions without performing a heavy, memory-intensive pixel decode of the entire image array.

Author:
anahata
  • Constructor Details

    • ImageMetadataUtils

      public ImageMetadataUtils()
  • Method Details

    • readMetadata

      public static ImageMetadataUtils.ImageMetadata readMetadata(byte[] data)
      Reads the dimensions and metadata of an image from its raw byte array.

      Utilizes standard JDK ImageReader SPI to parse metadata from headers. This is extremely fast (microsecond latency) and consumes virtually zero CPU compared to full image decoding.

      Parameters:
      data - The raw image file bytes.
      Returns:
      The parsed ImageMetadataUtils.ImageMetadata, or null if reading fails or format is unsupported.
    • calculateOpenAiTileTokens

      public static int calculateOpenAiTileTokens(ImageMetadataUtils.ImageMetadata metadata)
      Calculates the token consumption of an image under OpenAI's vision billing scheme.

      Implements the official OpenAI High-Detail scaling and tiling algorithm: 1. Proportional downscale to fit within a 2048x2048 box. 2. Proportional downscale such that the shortest side is exactly 768px. 3. Count of 512x512 pixel tiles needed to cover the scaled image. 4. Billing of 170 tokens per tile plus a flat 85 tokens base cost.

      Parameters:
      metadata - The parsed image dimensions and metadata.
      Returns:
      The calculated high-detail token count, or 85 as a low-detail fallback.