Top Description Fields Constructors Methods
org.lwjgl.stb

public Class STBImage

extends Object
Class Inheritance
Imports
javax.annotation.*, java.nio.*, org.lwjgl.*, org.lwjgl.system.*

Native bindings to stb_image.h from the stb library.

Quick notes

Primarily of interest to game developers and other people who can avoid problematic images and only need the trivial interface. Supported formats:

Animated GIF still needs a proper API, but here's one way to do it.

Features:

Limitations:

Basic usage (see HDR discussion below for HDR usage):


int x,y,n;
unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
// ... process data if not NULL ...
// ... x = width, y = height, n = # 8-bit components per pixel ...
// ... replace '0' with '1'..'4' to force that many components per pixel
// ... but 'n' will always be the number that it would have been if you said 0
stbi_image_free(data)

HDR image support

stb_image supports loading HDR images in general, and currently the Radiance .HDR file format specifically. You can still load any file through the existing interface; if you attempt to load an HDR file, it will be automatically remapped to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1; both of these constants can be reconfigured through this interface:


stbi_hdr_to_ldr_gamma(2.2f);
stbi_hdr_to_ldr_scale(1.0f);

(note, do not use inverse constants; stbi_image will invert them appropriately).

Additionally, there is a new, parallel interface for loading files as (linear) floats to preserve the full dynamic range:


float *data = stbi_loadf(filename, &x, &y, &n, 0);

If you load LDR images through this interface, those images will be promoted to floating point values, run through the inverse of constants corresponding to the above:


stbi_ldr_to_hdr_scale(1.0f);
stbi_ldr_to_hdr_gamma(2.2f);

Finally, given a filename (or an open file or memory block) containing image data, you can query for the "most appropriate" interface to use (that is, whether the image is HDR or not), using:


stbi_is_hdr(char *filename);

iPhone PNG support

We optionally support converting iPhone-formatted PNGs (which store premultiplied BGRA) back to RGB, even though they're internally encoded differently. To enable this conversion, call convert_iphone_png_to_rgb(1).

Call set_unpremultiply_on_load(1) as well to force a divide per pixel to remove any premultiplied alpha *only* if the image file explicitly says there's premultiplied data (currently only happens in iPhone images, and only if iPhone convert-to-rgb processing is on).

Field Summary

Modifier and TypeField and Description
public static final int
STBI_default

Component count.

public static final int
STBI_grey

Component count.

public static final int
STBI_grey_alpha

Component count.

public static final int
STBI_rgb

Component count.

public static final int
STBI_rgb_alpha

Component count.

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public static native void
nstbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)

Unsafe version of: convert_iphone_png_to_rgb

public static native void
nstbi_convert_iphone_png_to_rgb_thread(int flag_true_if_should_convert)

Unsafe version of: convert_iphone_png_to_rgb_thread

public static native long
public static native void
nstbi_image_free(long retval_from_stbi_load)

Unsafe version of: image_free

public static native int
nstbi_info(long filename, long x, long y, long comp)

Unsafe version of: info

public static native int
nstbi_info(long filename, int[] x, int[] y, int[] comp)

Array version of: nstbi_info

public static native int
nstbi_info_from_callbacks(long clbk, long user, long x, long y, long comp)

Unsafe version of: info_from_callbacks

public static native int
nstbi_info_from_callbacks(long clbk, long user, int[] x, int[] y, int[] comp)

Array version of: nstbi_info_from_callbacks

public static native int
nstbi_info_from_memory(long buffer, int
the buffer length, in bytes
len
,
long x, long y, long comp)

Unsafe version of: info_from_memory

public static native int
nstbi_info_from_memory(long buffer, int len, int[] x, int[] y, int[] comp)

Array version of: nstbi_info_from_memory

public static native int
nstbi_is_16_bit(long filename)

Unsafe version of: is_16_bit

public static native int
nstbi_is_16_bit_from_callbacks(long clbk, long user)

Unsafe version of: is_16_bit_from_callbacks

public static native int
nstbi_is_16_bit_from_memory(long buffer, int
the buffer length, in bytes
len
)

Unsafe version of: is_16_bit_from_memory

public static native int
nstbi_is_hdr(long filename)

Unsafe version of: is_hdr

public static native int
nstbi_is_hdr_from_callbacks(long clbk, long user)

Unsafe version of: is_hdr_from_callbacks

public static native int
nstbi_is_hdr_from_memory(long buffer, int
the buffer length, in bytes
len
)

Unsafe version of: is_hdr_from_memory

public static native long
nstbi_load(long filename, long x, long y, long channels_in_file, int desired_channels)

Unsafe version of: load

public static native long
nstbi_load(long filename, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: nstbi_load

public static native long
nstbi_load_16(long filename, long x, long y, long channels_in_file, int desired_channels)

Unsafe version of: load_16

public static native long
nstbi_load_16(long filename, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: nstbi_load_16

public static native long
nstbi_load_16_from_callbacks(long clbk, long user, long x, long y, long channels_in_file, int desired_channels)

Unsafe version of: load_16_from_callbacks

public static native long
nstbi_load_16_from_callbacks(long clbk, long user, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: nstbi_load_16_from_callbacks

public static native long
nstbi_load_16_from_memory(long buffer, int
the buffer length, in bytes
len
,
long x, long y, long channels_in_file, int desired_channels)

Unsafe version of: load_16_from_memory

public static native long
nstbi_load_16_from_memory(long buffer, int len, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: nstbi_load_16_from_memory

public static native long
nstbi_load_from_callbacks(long clbk, long user, long x, long y, long channels_in_file, int desired_channels)

Unsafe version of: load_from_callbacks

public static native long
nstbi_load_from_callbacks(long clbk, long user, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: nstbi_load_from_callbacks

public static native long
nstbi_load_from_memory(long buffer, int
the buffer length, in bytes
len
,
long x, long y, long channels_in_file, int desired_channels)

Unsafe version of: load_from_memory

public static native long
nstbi_load_from_memory(long buffer, int len, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: nstbi_load_from_memory

public static native long
nstbi_load_gif_from_memory(long buffer, int
the buffer length, in bytes
len
,
long delays, long x, long y, long z, long channels_in_file, int desired_channels)

Unsafe version of: load_gif_from_memory

public static native long
nstbi_load_gif_from_memory(long buffer, int len, long delays, int[] x, int[] y, int[] z, int[] channels_in_file, int desired_channels)

Array version of: nstbi_load_gif_from_memory

public static native long
nstbi_loadf(long filename, long x, long y, long channels_in_file, int desired_channels)

Unsafe version of: loadf

public static native long
nstbi_loadf(long filename, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: nstbi_loadf

public static native long
nstbi_loadf_from_callbacks(long clbk, long user, long x, long y, long channels_in_file, int desired_channels)

Unsafe version of: loadf_from_callbacks

public static native long
nstbi_loadf_from_callbacks(long clbk, long user, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: nstbi_loadf_from_callbacks

public static native long
nstbi_loadf_from_memory(long buffer, int
the buffer length, in bytes
len
,
long x, long y, long channels_in_file, int desired_channels)

Unsafe version of: loadf_from_memory

public static native long
nstbi_loadf_from_memory(long buffer, int len, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: nstbi_loadf_from_memory

public static native void
nstbi_set_flip_vertically_on_load(int flag_true_if_should_flip)

Unsafe version of: set_flip_vertically_on_load

public static native void
nstbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)

Unsafe version of: set_unpremultiply_on_load

public static native void
nstbi_set_unpremultiply_on_load_thread(int flag_true_if_should_unpremultiply)

Unsafe version of: set_unpremultiply_on_load_thread

public static native int
nstbi_zlib_decode_buffer(long obuffer, int olen, long ibuffer, int ilen)

Unsafe version of: zlib_decode_buffer

public static native long
nstbi_zlib_decode_malloc(long buffer, int len, long outlen)

Unsafe version of: zlib_decode_malloc

public static native long
nstbi_zlib_decode_malloc_guesssize(long buffer, int len, int initial_size, long outlen)

Unsafe version of: zlib_decode_malloc_guesssize

public static native long
nstbi_zlib_decode_malloc_guesssize_headerflag(long buffer, int len, int initial_size, long outlen, int parse_header)

Unsafe version of: zlib_decode_malloc_guesssize_headerflag

public static native int
nstbi_zlib_decode_noheader_buffer(long obuffer, int olen, long ibuffer, int ilen)

Unsafe version of: zlib_decode_noheader_buffer

public static native long
nstbi_zlib_decode_noheader_malloc(long buffer, int len, long outlen)

Unsafe version of: zlib_decode_noheader_malloc

public static void
stbi_convert_iphone_png_to_rgb(boolean
the convert iPhone PNG to RGB flag
flag_true_if_should_convert
)

Indicate whether we should process iPhone images back to canonical format, or just pass them through "as-is".

public static void
stbi_convert_iphone_png_to_rgb_thread(boolean flag_true_if_should_convert)

As convert_iphone_png_to_rgb, but only applies to images loaded on the thread that calls the function.

public static String
stbi_failure_reason()

Returns a brief reason for failure.

public static native void
stbi_hdr_to_ldr_gamma(float
the gamma value
gamma
)

Changes the gamma value used when converting HDR images to LDR.

public static native void
stbi_hdr_to_ldr_scale(float
the scale factor
scale
)

Changes the scale factor used when converting HDR images to LDR.

public static void
stbi_image_free(ByteBuffer
an stb image
retval_from_stbi_load
)

Frees a loaded image

public static void
stbi_image_free(ShortBuffer
an stb image
retval_from_stbi_load
)

Frees a loaded image

public static void
stbi_image_free(FloatBuffer
an stb image
retval_from_stbi_load
)

Frees a loaded image

public static boolean

Returns:

1 on success, 0 on failure
stbi_info
(ByteBuffer
the file name
filename
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
comp
)

Returns image dimensions & components without fully decoding the image.

public static boolean

Returns:

1 on success, 0 on failure
stbi_info
(CharSequence
the file name
filename
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
comp
)

Returns image dimensions & components without fully decoding the image.

public static boolean
stbi_info(ByteBuffer filename, int[] x, int[] y, int[] comp)

Array version of: info

public static boolean
stbi_info(CharSequence filename, int[] x, int[] y, int[] comp)

Array version of: info

public static boolean
stbi_info_from_callbacks(STBIIOCallbacks
an STBIIOCallbacks struct
clbk
,
long
a pointer to user data
user
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
comp
)

Callback version of info.

public static boolean
stbi_info_from_callbacks(STBIIOCallbacks clbk, long user, int[] x, int[] y, int[] comp)

Array version of: info_from_callbacks

public static boolean
stbi_info_from_memory(ByteBuffer
the buffer from which to load the image data
buffer
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
comp
)

In-memory version of info.

public static boolean
stbi_info_from_memory(ByteBuffer buffer, int[] x, int[] y, int[] comp)

Array version of: info_from_memory

public static boolean
stbi_is_16_bit(ByteBuffer
the file name
filename
)

Returns true if the file contains a 16-bit image.

public static boolean
stbi_is_16_bit(CharSequence
the file name
filename
)

Returns true if the file contains a 16-bit image.

public static boolean
stbi_is_16_bit_from_callbacks(STBIIOCallbacks
an STBIIOCallbacks struct
clbk
,
long
a pointer to user data
user
)

Callback version of is_16_bit.

public static boolean
stbi_is_16_bit_from_memory(ByteBuffer
the buffer from which to load the image data
buffer
)

In-memory version of is_16_bit.

public static boolean

Returns:

1 if the image is HDR, 0 otherwise
stbi_is_hdr
(ByteBuffer
the file name
filename
)

Checks if the specified file contains an HDR image.

public static boolean

Returns:

1 if the image is HDR, 0 otherwise
stbi_is_hdr
(CharSequence
the file name
filename
)

Checks if the specified file contains an HDR image.

public static boolean
stbi_is_hdr_from_callbacks(STBIIOCallbacks
an STBIIOCallbacks struct
clbk
,
long
a pointer to user data
user
)

Callback version of is_hdr.

public static boolean
stbi_is_hdr_from_memory(ByteBuffer
the buffer from which to load the image data
buffer
)

In-memory version of is_hdr.

public static native void
stbi_ldr_to_hdr_gamma(float
the gamma value
gamma
)

Changes the gamma value used when converting LDR images to HDR.

public static native void
stbi_ldr_to_hdr_scale(float
the scale factor
scale
)

Changes the scale value used when converting LDR images to HDR.

public static ByteBuffer
stbi_load(ByteBuffer
the file name
filename
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

Loads an image from the specified file.

public static ByteBuffer
stbi_load(CharSequence
the file name
filename
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

Loads an image from the specified file.

public static ByteBuffer
stbi_load(ByteBuffer filename, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: load

public static ByteBuffer
stbi_load(CharSequence filename, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: load

public static ShortBuffer
stbi_load_16(ByteBuffer
the file name
filename
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

16-bits-per-channel version of load.

public static ShortBuffer
stbi_load_16(CharSequence
the file name
filename
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

16-bits-per-channel version of load.

public static ShortBuffer
stbi_load_16(ByteBuffer filename, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: load_16

public static ShortBuffer
stbi_load_16(CharSequence filename, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: load_16

public static ShortBuffer
stbi_load_16_from_callbacks(STBIIOCallbacks
an STBIIOCallbacks struct
clbk
,
long
a pointer to user data
user
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

Callback version of load_16.

public static ShortBuffer
stbi_load_16_from_callbacks(STBIIOCallbacks clbk, long user, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: load_16_from_callbacks

public static ShortBuffer
stbi_load_16_from_memory(ByteBuffer
the buffer from which to load the image data
buffer
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

In-memory version of load_16.

public static ShortBuffer
stbi_load_16_from_memory(ByteBuffer buffer, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: load_16_from_memory

public static ByteBuffer
stbi_load_from_callbacks(STBIIOCallbacks
an STBIIOCallbacks struct
clbk
,
long
a pointer to user data
user
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

Callback version of load.

public static ByteBuffer
stbi_load_from_callbacks(STBIIOCallbacks clbk, long user, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: load_from_callbacks

public static ByteBuffer
stbi_load_from_memory(ByteBuffer
the buffer from which to load the image data
buffer
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

In-memory version of load.

public static ByteBuffer
stbi_load_from_memory(ByteBuffer buffer, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: load_from_memory

public static ByteBuffer
stbi_load_gif_from_memory(ByteBuffer
the buffer from which to load the image data
buffer
,
PointerBuffer
output the delays of each layer in the image
delays
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs the number of layers in the image
z
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

gif version of load_from_memory.

public static ByteBuffer
stbi_load_gif_from_memory(ByteBuffer buffer, PointerBuffer delays, int[] x, int[] y, int[] z, int[] channels_in_file, int desired_channels)

Array version of: load_gif_from_memory

public static FloatBuffer
stbi_loadf(ByteBuffer
the file name
filename
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

Floating-point version of load.

public static FloatBuffer
stbi_loadf(CharSequence
the file name
filename
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

Floating-point version of load.

public static FloatBuffer
stbi_loadf(ByteBuffer filename, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: loadf

public static FloatBuffer
stbi_loadf(CharSequence filename, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: loadf

public static FloatBuffer
stbi_loadf_from_callbacks(STBIIOCallbacks
an STBIIOCallbacks struct
clbk
,
long
a pointer to user data
user
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

Floating-point version of load_from_callbacks.

public static FloatBuffer
stbi_loadf_from_callbacks(STBIIOCallbacks clbk, long user, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: loadf_from_callbacks

public static FloatBuffer
stbi_loadf_from_memory(ByteBuffer
the buffer from which to load the image data
buffer
,
IntBuffer
outputs the image width in pixels
x
,
IntBuffer
outputs the image height in pixels
y
,
IntBuffer
outputs number of components in image
channels_in_file
,
int
0 or 1..4 to force that many components per pixel. One of:
01234
desired_channels
)

Floating-point version of load_from_memory.

public static FloatBuffer
stbi_loadf_from_memory(ByteBuffer buffer, int[] x, int[] y, int[] channels_in_file, int desired_channels)

Array version of: loadf_from_memory

public static void
stbi_set_flip_vertically_on_load(boolean
the flip vertically on load flag
flag_true_if_should_flip
)

Flips the image vertically, so the first pixel in the output array is the bottom left.

public static native void
stbi_set_flip_vertically_on_load_thread(int
the flip vertically on load flag
flag_true_if_should_flip
)

As set_flip_vertically_on_load, but only applies to images loaded on the thread that calls the function.

public static void
stbi_set_unpremultiply_on_load(boolean
the unpremultiply flag
flag_true_if_should_unpremultiply
)

For image formats that explicitly notate that they have premultiplied alpha, we just return the colors as stored in the file.

public static void
stbi_set_unpremultiply_on_load_thread(boolean flag_true_if_should_unpremultiply)

As set_unpremultiply_on_load, but only applies to images loaded on the thread that calls the function.

public static int
stbi_zlib_decode_buffer(ByteBuffer obuffer, ByteBuffer ibuffer)

ZLIB client - used by PNG, available for other purposes

public static ByteBuffer
stbi_zlib_decode_malloc(ByteBuffer buffer)

ZLIB client - used by PNG, available for other purposes

public static ByteBuffer
stbi_zlib_decode_malloc_guesssize(ByteBuffer buffer, int initial_size)

ZLIB client - used by PNG, available for other purposes

public static ByteBuffer
stbi_zlib_decode_malloc_guesssize_headerflag(ByteBuffer buffer, int initial_size, boolean parse_header)

ZLIB client - used by PNG, available for other purposes

public static int
stbi_zlib_decode_noheader_buffer(ByteBuffer obuffer, ByteBuffer ibuffer)

ZLIB client - used by PNG, available for other purposes

public static ByteBuffer
stbi_zlib_decode_noheader_malloc(ByteBuffer buffer)

ZLIB client - used by PNG, available for other purposes

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait