Class Raster
Backed by a contiguous byte[] buffer with an optional non-tight
stride (row pitch in bytes). Sub-views share the parent buffer and may have
a different stride than width * bytesPerPixel to span sub-rectangles
cheaply without copying.
底层为连续的 byte[] 缓冲区,可选非紧凑 stride(每行字节跨距)。
子视图共享父缓冲区,且 stride 可与 width * bytesPerPixel 不同,
从而以零拷贝代价跨越子矩形。
Thread safety | 线程安全: Not thread-safe. Synchronize externally if accessing from multiple threads. 非线程安全。如需多线程访问, 请在外部同步。
- Since:
- JDK 25, opencode-base-image V1.0.4
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longMaximum total pixels allowed in a single Raster (256 megapixels). -
Method Summary
Modifier and TypeMethodDescriptionintbyteOffsetOf(int x, int y) copy()Returns a deep copy with tight stride, fresh buffer, only the visible sub-region.static Rastercreate(int width, int height, PixelFormat format) Allocates a fresh Raster of the given dimensions with tight stride (zero-filled).byte[]data()Returns the underlying byte buffer.booleanvoidfill(int argb) Fills every pixel in this raster with the given ARGB color.format()static RasterfromBmp(DecodedBmp bmp) Builds anRGB_8Raster sharing the BMP's RGB buffer.static RasterfromGif(DecodedGif gif) Builds anRGBA_8Raster sharing the GIF frame's RGBA buffer.static RasterfromJpeg(DecodedJpeg jpeg) Builds anRGB_8Raster sharing the JPEG's RGB buffer.static RasterfromPng(DecodedPng png) Builds a Raster from a decoded PNG, choosingRGBA_8when the source has alpha andRGB_8otherwise.static RasterfromWebp(DecodedWebp webp) Builds a Raster sharing the WebP's pixel buffer;RGBA_8when the WebP carries alpha, otherwiseRGB_8.intgetPixelArgb(int x, int y) Reads a single pixel and packs it as0xAARRGGBBint.inthashCode()intheight()booleanisTight()Whether this raster spans the entire underlying buffer with tight stride.voidsetPixelArgb(int x, int y, int argb) Sets a pixel from0xAARRGGBBint (alpha ignored for non-alpha formats).intstride()subView(int x, int y, int w, int h) Returns a sub-view sharing this raster's underlying buffer.toString()byte[]Returns a tight-packedbyte[]of the visible sub-region.intwidth()static Rasterwrap(byte[] data, int width, int height, PixelFormat format) Wraps existing pixel data (no copy) with tight stride.
-
Field Details
-
MAX_PIXELS
public static final long MAX_PIXELSMaximum total pixels allowed in a single Raster (256 megapixels). 单个 Raster 允许的最大总像素数(256 兆像素)。Aligned with the
opencode-base-image-codecdecode-bomb cap to keep the three-layer image stack consistent. Guards against integer overflow in stride/buffer computations and accidental multi-GB allocations from malformed inputs.与
opencode-base-image-codec的 decode-bomb 上限对齐,保持 三层图像栈一致。防止 stride/缓冲计算的整数溢出,以及畸形输入触发的 数 GB 级别意外分配。- See Also:
-
-
Method Details
-
create
Allocates a fresh Raster of the given dimensions with tight stride (zero-filled). 以紧凑 stride 分配指定尺寸的新 Raster(零填充)。- Parameters:
width- image width in pixels (positive) | 图像宽度(像素,正整数)height- image height in pixels (positive) | 图像高度(像素,正整数)format- pixel format | 像素格式- Returns:
- a freshly allocated zero-filled Raster | 新分配的零填充 Raster
- Throws:
IllegalArgumentException- ifwidth/heightis not positive (basic dimension precondition) |width/height非正时抛出(基础维度前置条件)OpenImageCoreException- ifwidth * heightexceedsMAX_PIXELS, or if the resulting buffer length would exceedInteger.MAX_VALUE|width * height超过MAX_PIXELS,或缓冲长度超出Integer.MAX_VALUE时抛出
-
wrap
Wraps existing pixel data (no copy) with tight stride. 包装已有像素数据(不拷贝),使用紧凑 stride。- Parameters:
data- tightly packed pixel buffer | 紧密排列的像素缓冲width- image width in pixels (positive) | 图像宽度(像素,正整数)height- image height in pixels (positive) | 图像高度(像素,正整数)format- pixel format | 像素格式- Returns:
- a Raster sharing the supplied buffer | 共享给定缓冲的 Raster
- Throws:
IllegalArgumentException- ifwidth/heightis not positive |width/height非正时抛出OpenImageCoreException- ifwidth * heightexceedsMAX_PIXELS, the buffer length exceedsInteger.MAX_VALUE, ordata.lengthdoes not match the declared dimensions |width * height超过MAX_PIXELS、 缓冲长度超出Integer.MAX_VALUE,或data.length与声明 维度不符时抛出
-
fromPng
Builds a Raster from a decoded PNG, choosingRGBA_8when the source has alpha andRGB_8otherwise. When alpha is present the RGB and alpha channels are interleaved into a fresh RGBA buffer. 由解码后的 PNG 构造 Raster:源含 alpha 时使用RGBA_8, 否则使用RGB_8。含 alpha 时将 RGB 与 alpha 交错合成新缓冲。- Parameters:
png- decoded PNG | 解码后的 PNG- Returns:
- a Raster containing the PNG's pixels | 包含 PNG 像素的 Raster
- Throws:
IllegalArgumentException- if dimensions are non-positive (basic dimension precondition) | 维度非正时抛出(基础维度前置条件)OpenImageCoreException- if dimensions exceedMAX_PIXELS, or ifrgb/alphabuffer lengths don't match the declared dimensions | 维度超过MAX_PIXELS,或rgb/alpha缓冲长度与声明维度不符时抛出
-
fromJpeg
Builds anRGB_8Raster sharing the JPEG's RGB buffer. 构造与 JPEG 共享 RGB 缓冲的RGB_8Raster。- Parameters:
jpeg- decoded JPEG | 解码后的 JPEG- Returns:
- Raster sharing the JPEG buffer | 共享 JPEG 缓冲的 Raster
-
fromWebp
Builds a Raster sharing the WebP's pixel buffer;RGBA_8when the WebP carries alpha, otherwiseRGB_8. 构造与 WebP 共享像素缓冲的 Raster:含 alpha 时为RGBA_8, 否则为RGB_8。- Parameters:
webp- decoded WebP | 解码后的 WebP- Returns:
- Raster sharing the WebP buffer | 共享 WebP 缓冲的 Raster
-
fromBmp
Builds anRGB_8Raster sharing the BMP's RGB buffer. 构造与 BMP 共享 RGB 缓冲的RGB_8Raster。- Parameters:
bmp- decoded BMP | 解码后的 BMP- Returns:
- Raster sharing the BMP buffer | 共享 BMP 缓冲的 Raster
-
fromGif
Builds anRGBA_8Raster sharing the GIF frame's RGBA buffer. 构造与 GIF 帧共享 RGBA 缓冲的RGBA_8Raster。- Parameters:
gif- decoded GIF first frame | 解码后的 GIF 首帧- Returns:
- Raster sharing the GIF buffer | 共享 GIF 缓冲的 Raster
-
width
public int width()- Returns:
- width in pixels | 像素宽度
-
height
public int height()- Returns:
- height in pixels | 像素高度
-
stride
-
format
- Returns:
- pixel format | 像素格式
-
data
public byte[] data()Returns the underlying byte buffer. 返回底层字节缓冲。Caller MUST NOT mutate when this Raster is a sub-view or shared. 当此 Raster 为子视图或共享时,调用方禁止修改。
- Returns:
- underlying buffer (live reference) | 底层缓冲(活引用)
-
isTight
public boolean isTight()Whether this raster spans the entire underlying buffer with tight stride. 此 raster 是否以紧凑 stride 覆盖整个底层缓冲。- Returns:
trueif tight and full | 既紧凑又完整时返回true
-
byteOffsetOf
-
getPixelArgb
public int getPixelArgb(int x, int y) Reads a single pixel and packs it as0xAARRGGBBint. 读取单个像素并打包为0xAARRGGBB整数。For
RGB_8/BGR_8/GRAY_8the alpha bits are 0xFF (fully opaque). 对RGB_8/BGR_8/GRAY_8, alpha 位为 0xFF(完全不透明)。- Parameters:
x- x coordinate | x 坐标y- y coordinate | y 坐标- Returns:
- packed ARGB int | 打包后的 ARGB 整数
-
setPixelArgb
public void setPixelArgb(int x, int y, int argb) Sets a pixel from0xAARRGGBBint (alpha ignored for non-alpha formats). 由0xAARRGGBB整数设置像素(无 alpha 格式忽略 alpha)。For grayscale formats, the input RGB is converted using the BT.601 luminance formula
Y = 0.299R + 0.587G + 0.114B(integer approximation, half-up rounding). 对于灰度格式,使用 BT.601 亮度公式Y = 0.299R + 0.587G + 0.114B(整数近似,四舍五入)将输入 RGB 转换。- Parameters:
x- x coordinate | x 坐标y- y coordinate | y 坐标argb- packed ARGB int | 打包的 ARGB 整数
-
subView
Returns a sub-view sharing this raster's underlying buffer. 返回与此 raster 共享底层缓冲的子视图。The returned Raster has the same format and stride, but width/height limited to the requested rectangle. Mutations to either side propagate. 返回的 Raster 具有相同的格式和 stride,但宽/高被限制到所请求的矩形。 任一侧的修改都会传播。
- Parameters:
x- sub-region origin x | 子区域起点 xy- sub-region origin y | 子区域起点 yw- sub-region width | 子区域宽度h- sub-region height | 子区域高度- Returns:
- sub-view sharing the buffer | 共享缓冲的子视图
-
copy
Returns a deep copy with tight stride, fresh buffer, only the visible sub-region. 返回深拷贝:紧凑 stride、新缓冲、仅可见子区域。- Returns:
- deep copy | 深拷贝
-
toTightBytes
public byte[] toTightBytes()Returns a tight-packedbyte[]of the visible sub-region. 返回可见子区域的紧密打包byte[]。Length is
width * height * bytesPerPixel. 长度为width * height * bytesPerPixel。- Returns:
- fresh tight-packed buffer | 新的紧密打包缓冲
-
fill
public void fill(int argb) Fills every pixel in this raster with the given ARGB color. 用给定的 ARGB 颜色填充此 raster 中的每个像素。Implementation strategy: for
GRAY_8the whole row is a uniform byte and is cleared viaArrays.fill(long[], long); for multi-byte formats one pixel is encoded into a small template, the first row is built once via byte-doublingSystem.arraycopy, and subsequent rows are produced by row-levelarraycopy. This is typically 5–20× faster than per-pixel encoding for large rasters.实现策略:
GRAY_8整行字节相同,使用Arrays.fill(long[], long); 多字节格式则把一个像素编码到小模板缓冲,首行通过字节倍增System.arraycopy构建,后续行使用行级arraycopy复制。对大尺寸 raster 通常比逐像素 编码快 5–20 倍。- Parameters:
argb- packed ARGB int | 打包的 ARGB 整数
-
equals
-
hashCode
-
toString
-