Class BmpCodec

java.lang.Object
cloud.opencode.base.image.codec.bmp.BmpCodec

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

Decodes uncompressed Windows BMP byte streams into row-major top-down RGB bytes and encodes RGB bytes back into 24-bit uncompressed BMP files, without any dependency on java.desktop (no AWT, no ImageIO). Designed to be GraalVM native-image friendly and suitable for headless environments.

将未压缩的 Windows BMP 字节流解码为行优先、自上而下的 RGB 字节, 并将 RGB 字节编码回 24-bit 未压缩 BMP 文件,不依赖 java.desktop (不使用 AWT 与 ImageIO)。设计上对 GraalVM native-image 友好, 并适用于无头(headless)环境。

Supported BMP variants | 支持的 BMP 变体:

  • BITMAPFILEHEADER (14 bytes) + BITMAPINFOHEADER (40 bytes) only. 仅支持 14 字节文件头 + 40 字节 BITMAPINFOHEADER。
  • BI_RGB (uncompressed) only - 仅未压缩 (BI_RGB)
  • 24-bit BGR - 24 位 BGR
  • 32-bit BGRA - 32 位 BGRA(alpha 通道在解码时丢弃)
  • 8-bit indexed with 256-entry palette - 8 位索引(256 色调色板)
  • Both top-down (height < 0) and bottom-up (height > 0) row order. 同时支持自上而下(height < 0)和自下而上(height > 0)两种行存储顺序。
  • Row stride padded to 4-byte alignment - 行步长按 4 字节对齐

Not supported | 不支持:

  • RLE compression (BI_RLE8, BI_RLE4) - RLE 压缩
  • 1-bit / 2-bit / 4-bit indexed - 1/2/4 位索引
  • 16-bit BI_BITFIELDS - 16 位(含 BI_BITFIELDS)
  • BITMAPV4HEADER / BITMAPV5HEADER and OS/2 headers - V4/V5 / OS/2 头

Any unsupported variant raises OpenImageCodecException with a message starting with "unsupported BMP variant: ...".

所有不支持的变体均抛出 OpenImageCodecException, 消息以 "unsupported BMP variant: ..." 开头。

Encoding | 编码:

  • encode(byte[], int, int) writes a 24-bit uncompressed BMP (BITMAPINFOHEADER, BI_RGB, bottom-up rows with 4-byte padding). 输出 24 位未压缩 BMP(BITMAPINFOHEADER, BI_RGB, 自下而上 + 4 字节填充)。

Security | 安全性:

  • Thread-safe: Yes - all methods are stateless static. - 线程安全: 是 - 所有方法均为无状态静态方法。
  • Null-safe: throws on null input. - 空值安全: 输入为 null 时抛异常。
  • Bounded by CodecLimits.MAX_DECODED_BYTES to reject pathological dimensions. 通过 CodecLimits.MAX_DECODED_BYTES 限制以拒绝病态尺寸。
Since:
JDK 25, opencode-base-image-codec V1.0.4
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • decode

      public static DecodedBmp decode(byte[] bmpBytes)
      Decodes the supplied BMP byte stream into row-major top-down RGB pixels. 将 BMP 字节流解码为行优先、自上而下的 RGB 像素。
      Parameters:
      bmpBytes - raw BMP bytes including the 14-byte file header | 含 14 字节文件头的原始 BMP 字节
      Returns:
      decoded image | 解码后的图像
      Throws:
      OpenImageCodecException - when the input is not a supported BMP | 当输入不是受支持的 BMP 时抛出
    • encode

      public static byte[] encode(byte[] rgb, int width, int height)
      Encodes RGB pixel data as a 24-bit uncompressed (BI_RGB) BMP. 将 RGB 像素数据编码为 24-bit 未压缩(BI_RGB)BMP。

      The output uses the conventional bottom-up row order with each row padded to a 4-byte boundary. The supplied rgb array is assumed to be top-down row-major (R-G-B per pixel), matching DecodedBmp.

      输出采用约定的自下而上行顺序,每行按 4 字节对齐填充。 输入 rgb 数组按自上而下、行优先(每像素 R-G-B)排列, 与 DecodedBmp 保持一致。

      Parameters:
      rgb - RGB pixel data, length must equal width * height * 3 | RGB 像素数据,长度必须等于 width * height * 3
      width - pixel width (must be > 0) | 像素宽度(必须 > 0)
      height - pixel height (must be > 0) | 像素高度(必须 > 0)
      Returns:
      encoded BMP byte stream | 编码后的 BMP 字节流
      Throws:
      IllegalArgumentException - on null/invalid arguments | 参数为 null 或非法时抛出