1 Star 1 Fork 0

天天编程/godot-docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
class_image.rst 59.85 KB
一键复制 编辑 原始数据 按行查看 历史
github_url: hide

Image

Inherits: :ref:`Resource<class_Resource>` < :ref:`Reference<class_Reference>` < :ref:`Object<class_Object>`

Image datatype.

Description

Native image datatype. Contains image data, which can be converted to a :ref:`Texture2D<class_Texture2D>`, and several functions to interact with it. The maximum width and height for an Image are :ref:`MAX_WIDTH<class_Image_constant_MAX_WIDTH>` and :ref:`MAX_HEIGHT<class_Image_constant_MAX_HEIGHT>`.

Properties

:ref:`Dictionary<class_Dictionary>` :ref:`data<class_Image_property_data>` {"data": PackedByteArray( ),"format": "Lum8","height": 0,"mipmaps": false,"width": 0}

Methods

void :ref:`blend_rect<class_Image_method_blend_rect>` ( :ref:`Image<class_Image>` src, :ref:`Rect2<class_Rect2>` src_rect, :ref:`Vector2<class_Vector2>` dst )
void :ref:`blend_rect_mask<class_Image_method_blend_rect_mask>` ( :ref:`Image<class_Image>` src, :ref:`Image<class_Image>` mask, :ref:`Rect2<class_Rect2>` src_rect, :ref:`Vector2<class_Vector2>` dst )
void :ref:`blit_rect<class_Image_method_blit_rect>` ( :ref:`Image<class_Image>` src, :ref:`Rect2<class_Rect2>` src_rect, :ref:`Vector2<class_Vector2>` dst )
void :ref:`blit_rect_mask<class_Image_method_blit_rect_mask>` ( :ref:`Image<class_Image>` src, :ref:`Image<class_Image>` mask, :ref:`Rect2<class_Rect2>` src_rect, :ref:`Vector2<class_Vector2>` dst )
void :ref:`bumpmap_to_normalmap<class_Image_method_bumpmap_to_normalmap>` ( :ref:`float<class_float>` bump_scale=1.0 )
void :ref:`clear_mipmaps<class_Image_method_clear_mipmaps>` ( )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`compress<class_Image_method_compress>` ( :ref:`CompressMode<enum_Image_CompressMode>` mode, :ref:`CompressSource<enum_Image_CompressSource>` source=0, :ref:`float<class_float>` lossy_quality=0.7 )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`compress_from_channels<class_Image_method_compress_from_channels>` ( :ref:`CompressMode<enum_Image_CompressMode>` mode, :ref:`CompressSource<enum_Image_CompressSource>` channels, :ref:`float<class_float>` lossy_quality=0.7 )
void :ref:`convert<class_Image_method_convert>` ( :ref:`Format<enum_Image_Format>` format )
void :ref:`copy_from<class_Image_method_copy_from>` ( :ref:`Image<class_Image>` src )
void :ref:`create<class_Image_method_create>` ( :ref:`int<class_int>` width, :ref:`int<class_int>` height, :ref:`bool<class_bool>` use_mipmaps, :ref:`Format<enum_Image_Format>` format )
void :ref:`create_from_data<class_Image_method_create_from_data>` ( :ref:`int<class_int>` width, :ref:`int<class_int>` height, :ref:`bool<class_bool>` use_mipmaps, :ref:`Format<enum_Image_Format>` format, :ref:`PackedByteArray<class_PackedByteArray>` data )
void :ref:`crop<class_Image_method_crop>` ( :ref:`int<class_int>` width, :ref:`int<class_int>` height )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`decompress<class_Image_method_decompress>` ( )
:ref:`AlphaMode<enum_Image_AlphaMode>` :ref:`detect_alpha<class_Image_method_detect_alpha>` ( ) const
:ref:`UsedChannels<enum_Image_UsedChannels>` :ref:`detect_used_channels<class_Image_method_detect_used_channels>` ( :ref:`CompressSource<enum_Image_CompressSource>` source=0 )
void :ref:`expand_x2_hq2x<class_Image_method_expand_x2_hq2x>` ( )
void :ref:`fill<class_Image_method_fill>` ( :ref:`Color<class_Color>` color )
void :ref:`fix_alpha_edges<class_Image_method_fix_alpha_edges>` ( )
void :ref:`flip_x<class_Image_method_flip_x>` ( )
void :ref:`flip_y<class_Image_method_flip_y>` ( )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`generate_mipmaps<class_Image_method_generate_mipmaps>` ( :ref:`bool<class_bool>` renormalize=false )
:ref:`PackedByteArray<class_PackedByteArray>` :ref:`get_data<class_Image_method_get_data>` ( ) const
:ref:`Format<enum_Image_Format>` :ref:`get_format<class_Image_method_get_format>` ( ) const
:ref:`int<class_int>` :ref:`get_height<class_Image_method_get_height>` ( ) const
:ref:`int<class_int>` :ref:`get_mipmap_offset<class_Image_method_get_mipmap_offset>` ( :ref:`int<class_int>` mipmap ) const
:ref:`Color<class_Color>` :ref:`get_pixel<class_Image_method_get_pixel>` ( :ref:`int<class_int>` x, :ref:`int<class_int>` y ) const
:ref:`Color<class_Color>` :ref:`get_pixelv<class_Image_method_get_pixelv>` ( :ref:`Vector2<class_Vector2>` src ) const
:ref:`Image<class_Image>` :ref:`get_rect<class_Image_method_get_rect>` ( :ref:`Rect2<class_Rect2>` rect ) const
:ref:`Vector2<class_Vector2>` :ref:`get_size<class_Image_method_get_size>` ( ) const
:ref:`Rect2<class_Rect2>` :ref:`get_used_rect<class_Image_method_get_used_rect>` ( ) const
:ref:`int<class_int>` :ref:`get_width<class_Image_method_get_width>` ( ) const
:ref:`bool<class_bool>` :ref:`has_mipmaps<class_Image_method_has_mipmaps>` ( ) const
:ref:`bool<class_bool>` :ref:`is_compressed<class_Image_method_is_compressed>` ( ) const
:ref:`bool<class_bool>` :ref:`is_empty<class_Image_method_is_empty>` ( ) const
:ref:`bool<class_bool>` :ref:`is_invisible<class_Image_method_is_invisible>` ( ) const
:ref:`Error<enum_@GlobalScope_Error>` :ref:`load<class_Image_method_load>` ( :ref:`String<class_String>` path )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`load_jpg_from_buffer<class_Image_method_load_jpg_from_buffer>` ( :ref:`PackedByteArray<class_PackedByteArray>` buffer )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`load_png_from_buffer<class_Image_method_load_png_from_buffer>` ( :ref:`PackedByteArray<class_PackedByteArray>` buffer )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`load_webp_from_buffer<class_Image_method_load_webp_from_buffer>` ( :ref:`PackedByteArray<class_PackedByteArray>` buffer )
void :ref:`normalmap_to_xy<class_Image_method_normalmap_to_xy>` ( )
void :ref:`premultiply_alpha<class_Image_method_premultiply_alpha>` ( )
void :ref:`resize<class_Image_method_resize>` ( :ref:`int<class_int>` width, :ref:`int<class_int>` height, :ref:`Interpolation<enum_Image_Interpolation>` interpolation=1 )
void :ref:`resize_to_po2<class_Image_method_resize_to_po2>` ( :ref:`bool<class_bool>` square=false )
:ref:`Image<class_Image>` :ref:`rgbe_to_srgb<class_Image_method_rgbe_to_srgb>` ( )
:ref:`Error<enum_@GlobalScope_Error>` :ref:`save_exr<class_Image_method_save_exr>` ( :ref:`String<class_String>` path, :ref:`bool<class_bool>` grayscale=false ) const
:ref:`Error<enum_@GlobalScope_Error>` :ref:`save_png<class_Image_method_save_png>` ( :ref:`String<class_String>` path ) const
void :ref:`set_pixel<class_Image_method_set_pixel>` ( :ref:`int<class_int>` x, :ref:`int<class_int>` y, :ref:`Color<class_Color>` color )
void :ref:`set_pixelv<class_Image_method_set_pixelv>` ( :ref:`Vector2<class_Vector2>` dst, :ref:`Color<class_Color>` color )
void :ref:`shrink_x2<class_Image_method_shrink_x2>` ( )
void :ref:`srgb_to_linear<class_Image_method_srgb_to_linear>` ( )

Enumerations

enum Format:

  • FORMAT_L8 = 0 --- Texture format with a single 8-bit depth representing luminance.
  • FORMAT_LA8 = 1 --- OpenGL texture format with two values, luminance and alpha each stored with 8 bits.
  • 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: When creating an :ref:`ImageTexture<class_ImageTexture>`, 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: When creating an :ref:`ImageTexture<class_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_RGB565 = 7
  • 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 are two components, each a 16-bit "half-precision" floating-point value.
  • FORMAT_RGBH = 14 --- OpenGL texture format GL_RGB32F where there are three components, each a 16-bit "half-precision" floating-point value.
  • FORMAT_RGBAH = 15 --- OpenGL texture format GL_RGBA32F where there are 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 5-bit 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.

Note: When creating an :ref:`ImageTexture<class_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: When creating an :ref:`ImageTexture<class_ImageTexture>`, 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 transparent gradients compared to DXT3.

Note: When creating an :ref:`ImageTexture<class_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.
  • 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.

Note: When creating an :ref:`ImageTexture<class_ImageTexture>`, 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 can be found here.

Note: When creating an :ref:`ImageTexture<class_ImageTexture>`, an sRGB to linear color space conversion is performed.

Note: When creating an :ref:`ImageTexture<class_ImageTexture>`, an sRGB to linear color space conversion is performed.

Note: When creating an :ref:`ImageTexture<class_ImageTexture>`, an sRGB to linear color space conversion is performed.

  • FORMAT_ETC2_RGB8A1 = 36 --- Ericsson Texture Compression format 2 (RGB8_PUNCHTHROUGH_ALPHA1 variant), which compresses RGBA data to make alpha either fully transparent or fully opaque.

Note: When creating an :ref:`ImageTexture<class_ImageTexture>`, an sRGB to linear color space conversion is performed.


enum Interpolation:

  • INTERPOLATE_NEAREST = 0 --- Performs nearest-neighbor interpolation. If the image is resized, it will be pixelated.
  • INTERPOLATE_BILINEAR = 1 --- Performs bilinear interpolation. If the image is resized, it will be blurry. This mode is faster than :ref:`INTERPOLATE_CUBIC<class_Image_constant_INTERPOLATE_CUBIC>`, but it results in lower quality.
  • INTERPOLATE_CUBIC = 2 --- Performs cubic interpolation. If the image is resized, it will be blurry. This mode often gives better results compared to :ref:`INTERPOLATE_BILINEAR<class_Image_constant_INTERPOLATE_BILINEAR>`, at the cost of being slower.
  • INTERPOLATE_TRILINEAR = 3 --- Performs bilinear separately on the two most-suited mipmap levels, then linearly interpolates between them.

It's slower than :ref:`INTERPOLATE_BILINEAR<class_Image_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.

Note: If you intend to scale multiple copies of the original image, it's better to call :ref:`generate_mipmaps<class_Image_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.

  • INTERPOLATE_LANCZOS = 4 --- Performs Lanczos interpolation. This is the slowest image resizing mode, but it typically gives the best results, especially when downscalng images.

enum AlphaMode:

  • ALPHA_NONE = 0 --- Image does not have alpha.
  • ALPHA_BIT = 1 --- Image stores alpha in a single bit.
  • ALPHA_BLEND = 2 --- Image uses alpha.

enum CompressMode:

  • COMPRESS_S3TC = 0 --- Use S3TC compression.
  • COMPRESS_PVRTC2 = 1 --- Use PVRTC2 compression.
  • COMPRESS_PVRTC4 = 2 --- Use PVRTC4 compression.
  • COMPRESS_ETC = 3 --- Use ETC compression.
  • COMPRESS_ETC2 = 4 --- Use ETC2 compression.

enum UsedChannels:

  • USED_CHANNELS_L = 0
  • USED_CHANNELS_LA = 1
  • USED_CHANNELS_R = 2
  • USED_CHANNELS_RG = 3
  • USED_CHANNELS_RGB = 4
  • USED_CHANNELS_RGBA = 5

enum CompressSource:

  • COMPRESS_SOURCE_GENERIC = 0 --- Source texture (before compression) is a regular texture. Default for all textures.
  • COMPRESS_SOURCE_SRGB = 1 --- Source texture (before compression) is in sRGB space.
  • COMPRESS_SOURCE_NORMAL = 2 --- Source texture (before compression) is a normal texture (e.g. it can be compressed into two channels).

Constants

  • MAX_WIDTH = 16777216 --- The maximal width allowed for Image resources.
  • MAX_HEIGHT = 16777216 --- The maximal height allowed for Image resources.

Property Descriptions

Default {"data": PackedByteArray( ),"format": "Lum8","height": 0,"mipmaps": false,"width": 0}

Holds all of the image's color data in a given format. See :ref:`Format<enum_Image_Format>` constants.

Method Descriptions

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


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.


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


Converts a bumpmap to a normalmap. A bumpmap provides a height offset per-pixel, while a normalmap provides a normal direction per pixel.


  • 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 :ref:`CompressMode<enum_Image_CompressMode>` and :ref:`CompressSource<enum_Image_CompressSource>` constants.



Converts the image's format. See :ref:`Format<enum_Image_Format>` constants.


Copies src image to this image.


Creates an empty image of given size and format. See :ref:`Format<enum_Image_Format>` constants. If use_mipmaps is true then generate mipmaps for this image. See the :ref:`generate_mipmaps<class_Image_method_generate_mipmaps>`.


Creates a new image of given size and format. See :ref:`Format<enum_Image_Format>` constants. Fills the image with the given raw data. If use_mipmaps is true then generate mipmaps for this image. See the :ref:`generate_mipmaps<class_Image_method_generate_mipmaps>`.


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.


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


Returns :ref:`ALPHA_BLEND<class_Image_constant_ALPHA_BLEND>` if the image has data for alpha values. Returns :ref:`ALPHA_BIT<class_Image_constant_ALPHA_BIT>` if all the alpha values are stored in a single bit. Returns :ref:`ALPHA_NONE<class_Image_constant_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.


Fills the image with a given :ref:`Color<class_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.


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.


Returns the image's raw data.


Returns the image's format. See :ref:`Format<enum_Image_Format>` constants.


Returns the image's height.


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). This is the same as :ref:`get_pixelv<class_Image_method_get_pixelv>`, but with two integer arguments instead of a :ref:`Vector2<class_Vector2>` argument.


Returns the color of the pixel at src. This is the same as :ref:`get_pixel<class_Image_method_get_pixel>`, but with a :ref:`Vector2<class_Vector2>` argument instead of two integer arguments.


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


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


Returns a :ref:`Rect2<class_Rect2>` enclosing the visible portion of the image, considering each pixel with a non-zero alpha channel as visible.


Returns the image's width.


Returns true if the image has generated mipmaps.


Returns true if the image is compressed.


Returns true if the image has no data.


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.


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


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


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


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


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.


Converts a standard RGBE (Red Green Blue Exponent) image to an sRGB image.


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 :ref:`@GlobalScope.ERR_UNAVAILABLE<class_@GlobalScope_constant_ERR_UNAVAILABLE>` if Godot was compiled without the TinyEXR module.


Saves the image as a PNG file to path.


Sets the :ref:`Color<class_Color>` of the pixel at (x, y). Example:

var img = Image.new()
img.create(img_width, img_height, false, Image.FORMAT_RGBA8)
img.set_pixel(x, y, color)

Sets the :ref:`Color<class_Color>` of the pixel at (dst.x, dst.y). Note that the dst values must be integers. Example:

var img = Image.new()
img.create(img_width, img_height, false, Image.FORMAT_RGBA8)
img.set_pixelv(Vector2(x, y), color)

  • void shrink_x2 ( )

Shrinks the image by a factor of 2.


  • void srgb_to_linear ( )

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

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ttcode-net/godot-docs.git
git@gitee.com:ttcode-net/godot-docs.git
ttcode-net
godot-docs
godot-docs
master

搜索帮助