Image

    Category: Core

    Image datatype.

    Properties

    void ( Image src, src_rect, Vector2 dst )
    void ( Image src, mask, Rect2 src_rect, dst )
    voidblit_rect ( src, Rect2 src_rect, dst )
    voidblit_rect_mask ( src, Image mask, src_rect, Vector2 dst )
    void ( float bump_scale=1.0 )
    void ( )
    Error ( CompressMode mode, source, float lossy_quality )
    void ( Format format )
    void ( Image src )
    void ( int width, height, bool use_mipmaps, format )
    voidcreate_from_data ( width, int height, use_mipmaps, Format format, data )
    voidcrop ( width, int height )
    decompress ( )
    detect_alpha ( ) const
    void ( )
    voidfill ( color )
    voidfix_alpha_edges ( )
    void ( )
    voidflip_y ( )
    generate_mipmaps ( renormalize=false )
    PoolByteArray ( ) const
    Format ( ) const
    int ( ) const
    int ( int mipmap ) const
    get_pixel ( x, int y ) const
    get_pixelv ( src ) const
    Image ( Rect2 rect ) const
    get_size ( ) const
    get_used_rect ( ) const
    get_width ( ) const
    has_mipmaps ( ) const
    is_compressed ( ) const
    is_empty ( ) const
    is_invisible ( ) const
    load ( path )
    Error ( PoolByteArray buffer )
    load_png_from_buffer ( buffer )
    Error ( PoolByteArray buffer )
    void ( )
    voidnormalmap_to_xy ( )
    void ( )
    voidresize ( width, int height, interpolation=1 )
    voidresize_to_po2 ( square=false )
    Image ( )
    Error ( String path ) const
    void ( int x, y, Color color )
    void ( Vector2 dst, color )
    voidshrink_x2 ( )
    void ( )
    voidunlock ( )

    Enumerations

    enum Format:

    • FORMAT_L8 = 0
    • FORMAT_LA8 = 1
    • FORMAT_R8 = 2 — OpenGL texture format RED with a single component and a bitdepth of 8.
    • FORMAT_RG8 = 3 — OpenGL texture format RG with two components and a bitdepth of 8 for each.
    • FORMAT_RGB8 = 4 — OpenGL texture format RGB with three components, each with a bitdepth of 8. Note that when creating an , an sRGB to linear color space conversion is performed.
    • FORMAT_RGBA8 = 5 — OpenGL texture format RGBA with four components, each with a bitdepth of 8. Note that when creating an ImageTexture, an sRGB to linear color space conversion is performed.
    • FORMAT_RGBA4444 = 6 — OpenGL texture format RGBA with four components, each with a bitdepth of 4.
    • FORMAT_RGBA5551 = 7 — OpenGL texture format GL_RGB5_A1 where 5 bits of depth for each component of RGB and one bit for alpha.
    • FORMAT_RF = 8 — OpenGL texture format GL_R32F where there’s one component, a 32-bit floating-point value.
    • FORMAT_RGF = 9 — OpenGL texture format GL_RG32F where there are two components, each a 32-bit floating-point values.
    • FORMAT_RGBF = 10 — OpenGL texture format GL_RGB32F where there are three components, each a 32-bit floating-point values.
    • FORMAT_RGBAF = 11 — OpenGL texture format GL_RGBA32F where there are four components, each a 32-bit floating-point values.
    • FORMAT_RH = 12 — OpenGL texture format GL_R32F where there’s one component, a 16-bit “half-precision” floating-point value.
    • FORMAT_RGH = 13 — OpenGL texture format GL_RG32F where there’s two components, each a 16-bit “half-precision” floating-point value.
    • FORMAT_RGBH = 14 — OpenGL texture format GL_RGB32F where there’s three components, each a 16-bit “half-precision” floating-point value.
    • FORMAT_RGBAH = 15 — OpenGL texture format GL_RGBA32F where there’s four components, each a 16-bit “half-precision” floating-point value.
    • FORMAT_RGBE9995 = 16 — A special OpenGL texture format where the three color components have 9 bits of precision and all three share a single exponent.
    • FORMAT_DXT1 = 17 — The S3TC texture format that uses Block Compression 1, and is the smallest variation of S3TC, only providing 1 bit of alpha and color data being premultiplied with alpha. More information can be found at . Note that when creating an ImageTexture, an sRGB to linear color space conversion is performed.
    • FORMAT_DXT3 = 18 — The S3TC texture format that uses Block Compression 2, and color data is interpreted as not having been premultiplied by alpha. Well suited for images with sharp alpha transitions between translucent and opaque areas. Note that when creating an , an sRGB to linear color space conversion is performed.
    • FORMAT_DXT5 = 19 — The S3TC texture format also known as Block Compression 3 or BC3 that contains 64 bits of alpha channel data followed by 64 bits of DXT1-encoded color data. Color data is not premultiplied by alpha, same as DXT3. DXT5 generally produces superior results for transparency gradients than DXT3. Note that when creating an ImageTexture, an sRGB to linear color space conversion is performed.
    • FORMAT_RGTC_R = 20 — Texture format that uses Red Green Texture Compression, normalizing the red channel data using the same compression algorithm that DXT5 uses for the alpha channel. More information can be found here .
    • FORMAT_RGTC_RG = 21 — Texture format that uses Red Green Texture Compression, normalizing the red and green channel data using the same compression algorithm that DXT5 uses for the alpha channel.
    • FORMAT_BPTC_RGBA = 22 — Texture format that uses BPTC compression with unsigned normalized RGBA components. More information can be found at https://www.khronos.org/opengl/wiki/BPTC_Texture_Compression. Note that when creating an , an sRGB to linear color space conversion is performed.
    • FORMAT_BPTC_RGBF = 23 — Texture format that uses BPTC compression with signed floating-point RGB components.
    • FORMAT_BPTC_RGBFU = 24 — Texture format that uses BPTC compression with unsigned floating-point RGB components.
    • FORMAT_PVRTC2 = 25 — Texture format used on PowerVR-supported mobile platforms, uses 2 bit color depth with no alpha. More information on PVRTC can be found here https://en.wikipedia.org/wiki/PVRTC. Note that when creating an , an sRGB to linear color space conversion is performed.
    • FORMAT_PVRTC2A = 26 — Same as PVRTC2, but with an alpha component.
    • FORMAT_PVRTC4 = 27 — Similar to PVRTC2, but with 4 bit color depth and no alpha.
    • FORMAT_PVRTC4A = 28 — Same as PVRTC4, but with an alpha component.
    • FORMAT_ETC2_R11 = 30 — Ericsson Texture Compression format 2 variant R11_EAC, which provides one channel of unsigned data.
    • FORMAT_ETC2_R11S = 31 — Ericsson Texture Compression format 2 variant SIGNED_R11_EAC, which provides one channel of signed data.
    • FORMAT_ETC2_RG11 = 32 — Ericsson Texture Compression format 2 variant RG11_EAC, which provides two channels of unsigned data.
    • FORMAT_ETC2_RG11S = 33 — Ericsson Texture Compression format 2 variant SIGNED_RG11_EAC, which provides two channels of signed data.
    • FORMAT_ETC2_RGB8 = 34 — Ericsson Texture Compression format 2 variant RGB8, which is a followup of ETC1 and compresses RGB888 data. Note that when creating an ImageTexture, an sRGB to linear color space conversion is performed.
    • FORMAT_ETC2_RGBA8 = 35 — Ericsson Texture Compression format 2 variant RGBA8, which compresses RGBA8888 data with full alpha support. Note that when creating an , an sRGB to linear color space conversion is performed.
    • FORMAT_ETC2_RGB8A1 = 36 — Ericsson Texture Compression format 2 variant RGB8_PUNCHTHROUGH_ALPHA1, which compresses RGBA data to make alpha either fully transparent or fully opaque. Note that when creating an ImageTexture, an sRGB to linear color space conversion is performed.
    • FORMAT_MAX = 37 — Represents the size of the enum.

    enum Interpolation:

    • INTERPOLATE_NEAREST = 0
    • INTERPOLATE_BILINEAR = 1
    • INTERPOLATE_CUBIC = 2
    • INTERPOLATE_TRILINEAR = 3 — Performs bilinear separately on the two most suited mipmap levels, then linearly interpolates between them.

    It’s slower than INTERPOLATE_BILINEAR, but produces higher quality results, with much less aliasing artifacts.

    If the image does not have mipmaps, they will be generated and used internally, but no mipmaps will be generated on the resulting image. (Note that if you intend to scale multiple copies of the original image, it’s better to call on it in advance, to avoid wasting processing power in generating them again and again.)

    On the other hand, if the image already has mipmaps, they will be used, and a new set will be generated for the resulting image.


    enum AlphaMode:

    • ALPHA_NONE = 0
    • ALPHA_BIT = 1
    • ALPHA_BLEND = 2

    enum CompressMode:

    • COMPRESS_S3TC = 0
    • COMPRESS_PVRTC2 = 1
    • COMPRESS_PVRTC4 = 2
    • COMPRESS_ETC = 3
    • COMPRESS_ETC2 = 4

    enum CompressSource:

    • COMPRESS_SOURCE_GENERIC = 0
    • COMPRESS_SOURCE_SRGB = 1
    • COMPRESS_SOURCE_NORMAL = 2
    • MAX_WIDTH = 16384 — The maximal width allowed for Image resources.
    • MAX_HEIGHT = 16384 — The maximal height allowed for Image resources.

    Description

    Native image datatype. Contains image data, which can be converted to a , and several functions to interact with it. The maximum width and height for an Image are MAX_WIDTH and .

    Holds all of the image’s color data in a given format. See FORMAT_* constants.

    Method Descriptions

    • void blend_rect ( src, Rect2 src_rect, dst )

    Alpha-blends src_rect from src image to this image at coordinates dest.


    • void blend_rect_mask ( Image src, mask, Rect2 src_rect, dst )

    Alpha-blends src_rect from src image to this image using mask image at coordinates dst. Alpha channels are required for both src and mask. dst pixels and src pixels will blend if the corresponding mask pixel’s alpha value is not 0. src image and mask image must have the same size (width and height) but they can have different formats.


    Copies src_rect from src image to this image at coordinates dst.


    • void blit_rect_mask ( src, Image mask, src_rect, Vector2 dst )

    Blits area from src image to this image at the coordinates given by dst. src pixel is copied onto dst if the corresponding mask pixel’s alpha value is not 0. src image and mask image must have the same size (width and height) but they can have different formats.


    • void bumpmap_to_normalmap ( bump_scale=1.0 )

    • void clear_mipmaps ( )

    Removes the image’s mipmaps.


    Compresses the image to use less memory. Can not directly access pixel data while the image is compressed. Returns error if the chosen compression mode is not available. See COMPRESS_* constants.


    • void convert ( Format format )

    • void copy_from ( src )

    Copies src image to this image.


    • void create ( int width, height, bool use_mipmaps, format )

    Creates an empty image of given size and format. See FORMAT_* constants. If use_mipmaps is true then generate mipmaps for this image. See the generate_mipmaps method.


    Creates a new image of given size and format. See FORMAT_* constants. Fills the image with the given raw data. If use_mipmaps is true then generate mipmaps for this image. See the generate_mipmaps method.


    Crops the image to the given width and height. If the specified size is larger than the current size, the extra area is filled with black pixels.


    • decompress ( )

    Decompresses the image if it is compressed. Returns an error if decompress function is not available.


    Returns ALPHA_BLEND if the image has data for alpha values. Returns ALPHA_BIT if all the alpha values are below a certain threshold or the maximum value. Returns ALPHA_NONE if no data for alpha values is found.


    • void expand_x2_hq2x ( )

    Stretches the image and enlarges it by a factor of 2. No interpolation is done.


    • void fill ( color )

    Fills the image with a given Color.


    • void fix_alpha_edges ( )

    Blends low-alpha pixels with nearby pixels.


    • void flip_x ( )

    Flips the image horizontally.


    • void flip_y ( )

    Flips the image vertically.


    • generate_mipmaps ( bool renormalize=false )

    Generates mipmaps for the image. Mipmaps are pre-calculated and lower resolution copies of the image. Mipmaps are automatically used if the image needs to be scaled down when rendered. This improves image quality and the performance of the rendering. Returns an error if the image is compressed, in a custom format or if the image’s width/height is 0.


    • get_data ( ) const

    Returns the image’s raw data.


    Returns the image’s format. See constants.


    • get_height ( ) const

    Returns the image’s height.


    • int get_mipmap_offset ( mipmap ) const

    Returns the offset where the image’s mipmap with index mipmap is stored in the data dictionary.


    Returns the color of the pixel at (x, y) if the image is locked. If the image is unlocked it always returns a with the value (0, 0, 0, 1.0).


    • Color get_pixelv ( src ) const

    • Image get_rect ( rect ) const

    Returns a new image that is a copy of the image’s area specified with rect.



    • get_used_rect ( ) const

    Returns a Rect2 enclosing the visible portion of the image.


    • get_width ( ) const

    Returns the image’s width.


    • bool has_mipmaps ( ) const

    Returns true if the image has generated mipmaps.


    • is_compressed ( ) const

    Returns true if the image is compressed.


    • bool is_empty ( ) const

    Returns true if the image has no data.


    • is_invisible ( ) const

    Returns true if all the image’s pixels have an alpha value of 0. Returns false if any pixel has an alpha value higher than 0.


    Loads an image from file path.


    • Error load_jpg_from_buffer ( buffer )

    Loads an image from the binary contents of a JPEG file.


    • Error load_png_from_buffer ( buffer )

    Loads an image from the binary contents of a PNG file.


    • Error load_webp_from_buffer ( buffer )

    Loads an image from the binary contents of a WebP file.


    • void lock ( )

    Locks the data for reading and writing access. Sends an error to the console if the image is not locked when reading or writing a pixel.


    • void normalmap_to_xy ( )

    Converts the image’s data to represent coordinates on a 3D plane. This is used when the image represents a normalmap. A normalmap can add lots of detail to a 3D surface without increasing the polygon count.


    • void premultiply_alpha ( )

    Multiplies color values with alpha values. Resulting color values for a pixel are (color * alpha)/256.


    Resizes the image to the given width and height. New pixels are calculated using interpolation. See interpolation constants.


    • void resize_to_po2 ( square=false )

    Resizes the image to the nearest power of 2 for the width and height. If square is true then set width and height to be the same.



    • save_png ( String path ) const

    Saves the image as a PNG file to path.


    • void set_pixel ( x, int y, color )

    Sets the Color of the pixel at (x, y) if the image is locked. Example:


    • void set_pixelv ( dst, Color color )

    • void shrink_x2 ( )

    Shrinks the image by a factor of 2.


    • void srgb_to_linear ( )

    Unlocks the data and prevents changes.