Image datatype.
    public class Image : Resource

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

Inheritance Chain

Static Fields

    public const Int32 MaxHeight = 16384

    public const Int32 MaxWidth = 16384

Constructors

    public Image()

Properties

    public Collections.Dictionary Data { get; set; }

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

Methods

    public void BlendRect(Image src, Rect2 srcRect, Vector2 dst)

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

    public void BlendRectMask(Image src, Image mask, Rect2 srcRect, Vector2 dst)

Alpha-blends srcRect 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.

    public void BlitRect(Image src, Rect2 srcRect, Vector2 dst)

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

    public void BlitRectMask(Image src, Image mask, Rect2 srcRect, Vector2 dst)

Blits srcRect 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.

    public void BumpmapToNormalmap(float bumpScale = 1f)

    public void ClearMipmaps()

Removes the image’s mipmaps.

    public Error Compress(CompressMode mode, CompressSource source, float lossyQuality)

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.

    public void Convert(Format format)

Converts the image’s format. See format constants.

    public void CopyFrom(Image src)

Copies src image to this image.

    public void Create(int width, int height, bool useMipmaps, Format format)

Creates an empty image of given size and format. See format constants. If useMipmaps is true then generate mipmaps for this image. See the GenerateMipmaps(bool).

    public void CreateFromData(int width, int height, bool useMipmaps, Format format, byte[] data)

Creates a new image of given size and format. See format constants. Fills the image with the given raw data. If useMipmaps is true then generate mipmaps for this image. See the GenerateMipmaps(bool).

    public void Crop(int width, int height)

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.

    public Error Decompress()

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

    public AlphaMode DetectAlpha()

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.

    public void ExpandX2Hq2x()

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

    public void Fill(Color color)

Fills the image with a given Color.

    public void FixAlphaEdges()

Blends low-alpha pixels with nearby pixels.

    public void FlipX()

Flips the image horizontally.

    public void FlipY()

Flips the image vertically.

    public Error GenerateMipmaps(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.

    public byte[] GetData()

Returns the image’s raw data.

    public Format GetFormat()

Returns the image’s format. See FORMAT_* constants.

    public int GetHeight()

Returns the image’s height.

    public int GetMipmapOffset(int mipmap)

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

    public Color GetPixel(int x, int y)

Returns the color of the pixel at (x, y) if the image is locked. If the image is unlocked, it always returns a Color with the value (0, 0, 0, 1.0). This is the same as GetPixelv(Godot.Vector2), but two integer arguments instead of a Vector2 argument.

    public Color GetPixelv(Vector2 src)

Returns the color of the pixel at src if the image is locked. If the image is unlocked, it always returns a Color with the value (0, 0, 0, 1.0). This is the same as GetPixel(int, int), but with a Vector2 argument instead of two integer arguments.

    public Image GetRect(Rect2 rect)

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

    public Vector2 GetSize()

Returns the image’s size (width and height).

    public Rect2 GetUsedRect()

Returns a Rect2 enclosing the visible portion of the image.

    public int GetWidth()

Returns the image’s width.

    public bool HasMipmaps()

Returns true if the image has generated mipmaps.

    public bool IsCompressed()

Returns true if the image is compressed.

    public bool IsEmpty()

Returns true if the image has no data.

    public bool IsInvisible()

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.

    public Error Load(string path)

Loads an image from file path.

    public Error LoadJpgFromBuffer(byte[] buffer)

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

    public Error LoadPngFromBuffer(byte[] buffer)

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

    public Error LoadWebpFromBuffer(byte[] buffer)

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

    public 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.

    public void NormalmapToXy()

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.

    public void PremultiplyAlpha()

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

    public void Resize(int width, int height, Interpolation interpolation = Interpolation.Bilinear)

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

    public void ResizeToPo2(bool 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.

    public Image RgbeToSrgb()

    public Error SaveExr(string path, bool grayscale = false)

Saves the image as an EXR file to path. If grayscale is true and the image has only one channel, it will be saved explicitly as monochrome rather than one red channel. This function will return [constant ERR_UNAVAILABLE] if Godot was compiled without the TinyEXR module.

    public Error SavePng(string path)

Saves the image as a PNG file to path.

    public void SetPixel(int x, int y, Color color)

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

[codeblock]

var img = Image.new()

img.create(img_width, img_height, false, Image.FORMAT_RGBA8)

img.lock()

img.set_pixel(x, y, color) # Works

img.unlock()

img.set_pixel(x, y, color) # Does not have an effect

[/codeblock]

    public void SetPixelv(Vector2 dst, Color color)

    public void ShrinkX2()

Shrinks the image by a factor of 2.

    public void SrgbToLinear()

Converts the raw data from the sRGB colorspace to a linear scale.

    public void Unlock()

Unlocks the data and prevents changes.

Inner Types

AlphaMode

Name Value Description
None 0  
Bit 1  
Blend 2  

CompressMode

Name Value Description
S3tc 0  
Pvrtc2 1  
Pvrtc4 2  
Etc 3  
Etc2 4  

CompressSource

Name Value Description
Generic 0  
Srgb 1  
Normal 2  

Format

Name Value Description
L8 0  
La8 1  
R8 2 OpenGL texture format [code]RED[/code] with a single component and a bitdepth of 8.
Rg8 3 OpenGL texture format [code]RG[/code] with two components and a bitdepth of 8 for each.
Rgb8 4 OpenGL texture format [code]RGB[/code] with three components, each with a bitdepth of 8.
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
Rgba8 5 OpenGL texture format [code]RGBA[/code] with four components, each with a bitdepth of 8.
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
Rgba4444 6 OpenGL texture format [code]RGBA[/code] with four components, each with a bitdepth of 4.
Rgba5551 7 OpenGL texture format [code]GL_RGB5_A1[/code] where 5 bits of depth for each component of RGB and one bit for alpha.
Rf 8 OpenGL texture format [code]GL_R32F[/code] where there’s one component, a 32-bit floating-point value.
Rgf 9 OpenGL texture format [code]GL_RG32F[/code] where there are two components, each a 32-bit floating-point values.
Rgbf 10 OpenGL texture format [code]GL_RGB32F[/code] where there are three components, each a 32-bit floating-point values.
Rgbaf 11 OpenGL texture format [code]GL_RGBA32F[/code] where there are four components, each a 32-bit floating-point values.
Rh 12 OpenGL texture format [code]GL_R32F[/code] where there’s one component, a 16-bit “half-precision” floating-point value.
Rgh 13 OpenGL texture format [code]GL_RG32F[/code] where there are two components, each a 16-bit “half-precision” floating-point value.
Rgbh 14 OpenGL texture format [code]GL_RGB32F[/code] where there are three components, each a 16-bit “half-precision” floating-point value.
Rgbah 15 OpenGL texture format [code]GL_RGBA32F[/code] where there are four components, each a 16-bit “half-precision” floating-point value.
Rgbe9995 16 A special OpenGL texture format where the three color components have 9 bits of precision and all three share a single 5-bit exponent.
Dxt1 17 The [url=https://en.wikipedia.org/wiki/S3_Texture_Compression]S3TC[/url] 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.
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
Dxt3 18 The [url=https://en.wikipedia.org/wiki/S3_Texture_Compression]S3TC[/url] 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.
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
Dxt5 19 The [url=https://en.wikipedia.org/wiki/S3_Texture_Compression]S3TC[/url] 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 transparent gradients compared to DXT3.
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
RgtcR 20 Texture format that uses [url=https://www.khronos.org/opengl/wiki/Red_Green_Texture_Compression]Red Green Texture Compression[/url], normalizing the red channel data using the same compression algorithm that DXT5 uses for the alpha channel.
RgtcRg 21 Texture format that uses [url=https://www.khronos.org/opengl/wiki/Red_Green_Texture_Compression]Red Green Texture Compression[/url], normalizing the red and green channel data using the same compression algorithm that DXT5 uses for the alpha channel.
BptcRgba 22 Texture format that uses [url=https://www.khronos.org/opengl/wiki/BPTC_Texture_Compression]BPTC[/url] compression with unsigned normalized RGBA components.
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
BptcRgbf 23 Texture format that uses [url=https://www.khronos.org/opengl/wiki/BPTC_Texture_Compression]BPTC[/url] compression with signed floating-point RGB components.
BptcRgbfu 24 Texture format that uses [url=https://www.khronos.org/opengl/wiki/BPTC_Texture_Compression]BPTC[/url] compression with unsigned floating-point RGB components.
Pvrtc2 25 Texture format used on PowerVR-supported mobile platforms, uses 2-bit color depth with no alpha. More information can be found [url=https://en.wikipedia.org/wiki/PVRTC]here[/url].
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
Pvrtc2a 26 Same as [url=https://en.wikipedia.org/wiki/PVRTC]PVRTC2[/url], but with an alpha component.
Pvrtc4 27 Similar to [url=https://en.wikipedia.org/wiki/PVRTC]PVRTC2[/url], but with 4-bit color depth and no alpha.
Pvrtc4a 28 Same as [url=https://en.wikipedia.org/wiki/PVRTC]PVRTC4[/url], but with an alpha component.
Etc 29 [url=https://en.wikipedia.org/wiki/Ericsson_Texture_Compression#ETC1]Ericsson Texture Compression format 1[/url], also referred to as “ETC1”, and is part of the OpenGL ES graphics standard. This format cannot store an alpha channel.
Etc2R11 30 [url=https://en.wikipedia.org/wiki/Ericsson_Texture_Compression#ETC2_and_EAC]Ericsson Texture Compression format 2[/url] ([code]R11_EAC[/code] variant), which provides one channel of unsigned data.
Etc2R11s 31 [url=https://en.wikipedia.org/wiki/Ericsson_Texture_Compression#ETC2_and_EAC]Ericsson Texture Compression format 2[/url] ([code]SIGNED_R11_EAC[/code] variant), which provides one channel of signed data.
Etc2Rg11 32 [url=https://en.wikipedia.org/wiki/Ericsson_Texture_Compression#ETC2_and_EAC]Ericsson Texture Compression format 2[/url] ([code]RG11_EAC[/code] variant), which provides two channels of unsigned data.
Etc2Rg11s 33 [url=https://en.wikipedia.org/wiki/Ericsson_Texture_Compression#ETC2_and_EAC]Ericsson Texture Compression format 2[/url] ([code]SIGNED_RG11_EAC[/code] variant), which provides two channels of signed data.
Etc2Rgb8 34 [url=https://en.wikipedia.org/wiki/Ericsson_Texture_Compression#ETC2_and_EAC]Ericsson Texture Compression format 2[/url] ([code]RGB8[/code] variant), which is a follow-up of ETC1 and compresses RGB888 data.
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
Etc2Rgba8 35 [url=https://en.wikipedia.org/wiki/Ericsson_Texture_Compression#ETC2_and_EAC]Ericsson Texture Compression format 2[/url] ([code]RGBA8[/code]variant), which compresses RGBA8888 data with full alpha support.
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
Etc2Rgb8a1 36 [url=https://en.wikipedia.org/wiki/Ericsson_Texture_Compression#ETC2_and_EAC]Ericsson Texture Compression format 2[/url] ([code]RGB8_PUNCHTHROUGH_ALPHA1[/code] variant), which compresses RGBA data to make alpha either fully transparent or fully opaque.
[b]Note:[/b] When creating an [ImageTexture], an sRGB to linear color space conversion is performed.    
Max 37 Represents the size of the [enum Format] enum.

Interpolation

| Name | Value | Description | |——|——-|————-| | Nearest | 0 | Performs nearest-neighbor interpolation. If the image is resized, it will be pixelated.| | Bilinear | 1 | Performs bilinear interpolation. If the image is resized, it will be blurry. This mode is faster than [constant INTERPOLATE_CUBIC], but it results in lower quality.| | Cubic | 2 | Performs cubic interpolation. If the image is resized, it will be blurry. This mode often gives better results compared to [constant INTERPOLATE_BILINEAR], at the cost of being slower.| | Trilinear | 3 | Performs bilinear separately on the two most-suited mipmap levels, then linearly interpolates between them. It’s slower than [constant 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. [b]Note:[/b] If you intend to scale multiple copies of the original image, it’s better to call [method generate_mipmaps]] 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.| | Lanczos | 4 | Performs Lanczos interpolation. This is the slowest image resizing mode, but it typically gives the best results, especially when downscalng images.|

Tags: