Package org.ngengine.stbimage
Class StbLimits
java.lang.Object
org.ngengine.stbimage.StbLimits
Limits resource usage for every instance of every decoder.
Usually the defaults are fine, but if you wish to change the limits, be sure to set them before any decoder is initialized
as they will be locked afterwards and subsequent calls to the set methods will do nothing.
The limits are applied to all decoders and are not specific to any one format, so they can be used as a global safety net against OOM errors when decoding untrusted content.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intcheckedImageBufferSize(int width, int height, int channels, int bytesPerChannel) Computes byte size for interleaved image buffers with overflow checks.static intcheckedPixelCount(int width, int height) Computes pixel count with overflow checks.static voidcheckMaxSingleAllocationBytes(long requestedBytes) Checks if the requested single allocation bytes exceeds the configured limit.static voidcheckMaxTotalAllocationPerDecodeBytes(long requestedBytes) Checks if the requested cumulative allocation bytes for a decode operation exceeds the configured limit.static voidcheckStalledRounds(int stalledRounds) Checks if stalled rounds have exceeded the configured limit.static voidlock()static voidsetMaxDimensions(int maxDimensions) Sets the maximum allowed image dimensions for decoding.static voidsetMaxSingleAllocationBytes(int maxSingleAllocationBytes) Sets the maximum number of bytes allowed for any single buffer allocation during decode.static voidsetMaxTotalAllocationPerDecodeBytes(long maxTotalAllocationPerDecodeBytes) Sets the maximum cumulative bytes allocated by one decoder instance.static voidsetStalledRoundsLimit(int stalledRoundsLimit) Set how many rounds a loop can stall (make no progress) before we consider it brokenstatic voidvalidateDimensions(int width, int height) Validates image dimensions against stb-style safety limits.
-
Constructor Details
-
StbLimits
public StbLimits()
-
-
Method Details
-
lock
public static void lock() -
setStalledRoundsLimit
public static void setStalledRoundsLimit(int stalledRoundsLimit) Set how many rounds a loop can stall (make no progress) before we consider it broken- Parameters:
stalledRoundsLimit- number of rounds without progress before considering stalled (must be positive)
-
setMaxDimensions
public static void setMaxDimensions(int maxDimensions) Sets the maximum allowed image dimensions for decoding.- Parameters:
maxDimensions- maximum width or height in pixels (must be positive)
-
setMaxTotalAllocationPerDecodeBytes
public static void setMaxTotalAllocationPerDecodeBytes(long maxTotalAllocationPerDecodeBytes) Sets the maximum cumulative bytes allocated by one decoder instance.- Parameters:
maxTotalAllocationPerDecodeBytes- cumulative byte limit (0 for unlimited ie. handled by the allocator itself)
-
setMaxSingleAllocationBytes
public static void setMaxSingleAllocationBytes(int maxSingleAllocationBytes) Sets the maximum number of bytes allowed for any single buffer allocation during decode.- Parameters:
maxSingleAllocationBytes- byte limit (0 for unlimited ie. handled by the allocator itself)
-
validateDimensions
public static void validateDimensions(int width, int height) Validates image dimensions against stb-style safety limits.- Parameters:
width- image widthheight- image height
-
checkedPixelCount
public static int checkedPixelCount(int width, int height) Computes pixel count with overflow checks.- Parameters:
width- image widthheight- image height- Returns:
- checked pixel count
-
checkedImageBufferSize
public static int checkedImageBufferSize(int width, int height, int channels, int bytesPerChannel) Computes byte size for interleaved image buffers with overflow checks.- Parameters:
width- image widthheight- image heightchannels- channel countbytesPerChannel- bytes per channel- Returns:
- checked total buffer size in bytes
-
checkMaxSingleAllocationBytes
public static void checkMaxSingleAllocationBytes(long requestedBytes) Checks if the requested single allocation bytes exceeds the configured limit.- Parameters:
requestedBytes-
-
checkMaxTotalAllocationPerDecodeBytes
public static void checkMaxTotalAllocationPerDecodeBytes(long requestedBytes) Checks if the requested cumulative allocation bytes for a decode operation exceeds the configured limit.- Parameters:
requestedBytes-
-
checkStalledRounds
public static void checkStalledRounds(int stalledRounds) Checks if stalled rounds have exceeded the configured limit.- Parameters:
stalledRounds- current count of stalled rounds
-