Class PngDecoder

java.lang.Object
cloud.opencode.base.image.codec.png.PngDecoder

public final class PngDecoder extends Object
Pure-Java PNG decoder for image embedding / general use. 用于图像嵌入与通用场景的纯 Java PNG 解码器。

Decodes a non-interlaced 8-bit PNG into row-major RGB bytes (and an optional alpha plane) without any dependency on java.desktop (no AWT, no ImageIO). Designed to be GraalVM native-image friendly and suitable for headless environments.

将非隔行 8-bit PNG 解码为行优先的 RGB 字节(以及可选的 alpha 平面), 不依赖 java.desktop(不使用 AWT 与 ImageIO)。设计上对 GraalVM native-image 友好,并适用于无头(headless)环境。

Supported PNG variants | 支持的 PNG 变体:

  • Bit depth 8 only - 仅 8 位深度
  • colorType 0 (Greyscale) - 灰度 → 展开为 RGB
  • colorType 2 (RGB) - RGB
  • colorType 3 (Indexed/PLTE, optional tRNS) - 索引调色板(可选 tRNS)
  • colorType 4 (Greyscale + Alpha) - 灰度 + alpha
  • colorType 6 (RGBA) - RGBA
  • Filters: None(0), Sub(1), Up(2), Average(3), Paeth(4) - 5 种行滤波器
  • Non-interlaced (interlace=0) only - 仅支持非隔行

Any other variant (16-bit depth, Adam7 interlacing, unknown colorType, malformed CRC, missing chunks, etc.) raises OpenImageCodecException with an "unsupported PNG variant: ..." message.

其它变体(16 位深度、Adam7 隔行、未知 colorType、CRC 错、缺少块等) 抛出 OpenImageCodecException,消息以 "unsupported PNG variant: ..." 开头。

Security | 安全性:

  • Thread-safe: Yes - all methods are stateless static. - 线程安全: 是 - 所有方法均为无状态静态方法。
  • Null-safe: throws on null input. - 空值安全: 输入为 null 时抛异常。
  • Validates CRC32 of every chunk to reject corrupted streams. - 校验每个块的 CRC32 以拒绝损坏数据。
Since:
JDK 25, opencode-base-image-codec V1.0.4
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • decode

      public static DecodedPng decode(byte[] pngBytes)
      Decodes the supplied PNG byte stream. 解码给定的 PNG 字节流。
      Parameters:
      pngBytes - raw PNG bytes including the 8-byte signature | 包含 8 字节签名的原始 PNG 字节
      Returns:
      decoded image | 解码后的图像
      Throws:
      OpenImageCodecException - when the input is not a supported PNG | 当输入不是支持的 PNG 时抛出