123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- #ifndef IMAGE_H
- #define IMAGE_H
- #include "core/color.h"
- #include "core/dvector.h"
- #include "core/math/rect2.h"
- #include "core/resource.h"
- class Image;
- typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img);
- typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size);
- class Image : public Resource {
- GDCLASS(Image, Resource);
- enum {
- MAX_WIDTH = 16384,
- MAX_HEIGHT = 16384
- };
- public:
- static SavePNGFunc save_png_func;
- enum Format {
- FORMAT_L8,
- FORMAT_LA8,
- FORMAT_R8,
- FORMAT_RG8,
- FORMAT_RGB8,
- FORMAT_RGBA8,
- FORMAT_RGBA4444,
- FORMAT_RGBA5551,
- FORMAT_RF,
- FORMAT_RGF,
- FORMAT_RGBF,
- FORMAT_RGBAF,
- FORMAT_RH,
- FORMAT_RGH,
- FORMAT_RGBH,
- FORMAT_RGBAH,
- FORMAT_RGBE9995,
- FORMAT_DXT1,
- FORMAT_DXT3,
- FORMAT_DXT5,
- FORMAT_RGTC_R,
- FORMAT_RGTC_RG,
- FORMAT_BPTC_RGBA,
- FORMAT_BPTC_RGBF,
- FORMAT_BPTC_RGBFU,
- FORMAT_PVRTC2,
- FORMAT_PVRTC2A,
- FORMAT_PVRTC4,
- FORMAT_PVRTC4A,
- FORMAT_ETC,
- FORMAT_ETC2_R11,
- FORMAT_ETC2_R11S,
- FORMAT_ETC2_RG11,
- FORMAT_ETC2_RG11S,
- FORMAT_ETC2_RGB8,
- FORMAT_ETC2_RGBA8,
- FORMAT_ETC2_RGB8A1,
- FORMAT_MAX
- };
- static const char *format_names[FORMAT_MAX];
- enum Interpolation {
- INTERPOLATE_NEAREST,
- INTERPOLATE_BILINEAR,
- INTERPOLATE_CUBIC,
- INTERPOLATE_TRILINEAR,
-
-
- };
- enum CompressSource {
- COMPRESS_SOURCE_GENERIC,
- COMPRESS_SOURCE_SRGB,
- COMPRESS_SOURCE_NORMAL,
- COMPRESS_SOURCE_LAYERED,
- };
-
- static ImageMemLoadFunc _png_mem_loader_func;
- static ImageMemLoadFunc _jpg_mem_loader_func;
- static ImageMemLoadFunc _webp_mem_loader_func;
- static void (*_image_compress_bc_func)(Image *, float, CompressSource p_source);
- static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, CompressSource p_source);
- static void (*_image_compress_pvrtc2_func)(Image *);
- static void (*_image_compress_pvrtc4_func)(Image *);
- static void (*_image_compress_etc1_func)(Image *, float);
- static void (*_image_compress_etc2_func)(Image *, float, CompressSource p_source);
- static void (*_image_decompress_pvrtc)(Image *);
- static void (*_image_decompress_bc)(Image *);
- static void (*_image_decompress_bptc)(Image *);
- static void (*_image_decompress_etc1)(Image *);
- static void (*_image_decompress_etc2)(Image *);
- static PoolVector<uint8_t> (*lossy_packer)(const Ref<Image> &p_image, float p_quality);
- static Ref<Image> (*lossy_unpacker)(const PoolVector<uint8_t> &p_buffer);
- static PoolVector<uint8_t> (*lossless_packer)(const Ref<Image> &p_image);
- static Ref<Image> (*lossless_unpacker)(const PoolVector<uint8_t> &p_buffer);
- PoolVector<uint8_t>::Write write_lock;
- protected:
- static void _bind_methods();
- private:
- void _create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
- create(p_width, p_height, p_use_mipmaps, p_format);
- }
- void _create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
- create(p_width, p_height, p_use_mipmaps, p_format, p_data);
- }
- Format format;
- PoolVector<uint8_t> data;
- int width, height;
- bool mipmaps;
- void _copy_internals_from(const Image &p_image) {
- format = p_image.format;
- width = p_image.width;
- height = p_image.height;
- mipmaps = p_image.mipmaps;
- data = p_image.data;
- }
- _FORCE_INLINE_ void _get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const;
- static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1);
- bool _can_modify(Format p_format) const;
- _FORCE_INLINE_ void _put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel);
- _FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel);
- void _set_data(const Dictionary &p_data);
- Dictionary _get_data() const;
- Error _load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader);
- static void average_4_uint8(uint8_t &p_out, const uint8_t &p_a, const uint8_t &p_b, const uint8_t &p_c, const uint8_t &p_d);
- static void average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d);
- static void average_4_half(uint16_t &p_out, const uint16_t &p_a, const uint16_t &p_b, const uint16_t &p_c, const uint16_t &p_d);
- static void average_4_rgbe9995(uint32_t &p_out, const uint32_t &p_a, const uint32_t &p_b, const uint32_t &p_c, const uint32_t &p_d);
- static void renormalize_uint8(uint8_t *p_rgb);
- static void renormalize_float(float *p_rgb);
- static void renormalize_half(uint16_t *p_rgb);
- static void renormalize_rgbe9995(uint32_t *p_rgb);
- public:
- int get_width() const;
- int get_height() const;
- Vector2 get_size() const;
- bool has_mipmaps() const;
- int get_mipmap_count() const;
-
- void convert(Format p_new_format);
-
- Format get_format() const;
- int get_mipmap_offset(int p_mipmap) const;
- void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const;
- void get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const;
-
- void resize_to_po2(bool p_square = false);
- void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
- void shrink_x2();
- void expand_x2_hq2x();
-
- void crop_from_point(int p_x, int p_y, int p_width, int p_height);
- void crop(int p_width, int p_height);
- void flip_x();
- void flip_y();
-
- Error generate_mipmaps(bool p_renormalize = false);
- void clear_mipmaps();
- void normalize();
-
- void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
- void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
- void create(const char **p_xpm);
-
- bool empty() const;
- PoolVector<uint8_t> get_data() const;
- Error load(const String &p_path);
- Error save_png(const String &p_path) const;
-
- Image();
-
- Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
-
- Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
- enum AlphaMode {
- ALPHA_NONE,
- ALPHA_BIT,
- ALPHA_BLEND
- };
- AlphaMode detect_alpha() const;
- bool is_invisible() const;
- static int get_format_pixel_size(Format p_format);
- static int get_format_pixel_rshift(Format p_format);
- static int get_format_block_size(Format p_format);
- static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h);
- static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false);
- static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
- static int get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap);
- enum CompressMode {
- COMPRESS_S3TC,
- COMPRESS_PVRTC2,
- COMPRESS_PVRTC4,
- COMPRESS_ETC,
- COMPRESS_ETC2,
- COMPRESS_BPTC
- };
- Error compress(CompressMode p_mode = COMPRESS_S3TC, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7);
- Error decompress();
- bool is_compressed() const;
- void fix_alpha_edges();
- void premultiply_alpha();
- void srgb_to_linear();
- void normalmap_to_xy();
- Ref<Image> rgbe_to_srgb();
- void bumpmap_to_normalmap(float bump_scale = 1.0);
- void blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
- void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
- void blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
- void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
- void fill(const Color &c);
- Rect2 get_used_rect() const;
- Ref<Image> get_rect(const Rect2 &p_area) const;
- static void set_compress_bc_func(void (*p_compress_func)(Image *, float, CompressSource));
- static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, CompressSource));
- static String get_format_name(Format p_format);
- Error load_png_from_buffer(const PoolVector<uint8_t> &p_array);
- Error load_jpg_from_buffer(const PoolVector<uint8_t> &p_array);
- Error load_webp_from_buffer(const PoolVector<uint8_t> &p_array);
- Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
- Image(const char **p_xpm);
- virtual Ref<Resource> duplicate(bool p_subresources = false) const;
- void lock();
- void unlock();
-
- enum DetectChannels {
- DETECTED_L,
- DETECTED_LA,
- DETECTED_R,
- DETECTED_RG,
- DETECTED_RGB,
- DETECTED_RGBA,
- };
- DetectChannels get_detected_channels();
- void optimize_channels();
- Color get_pixelv(const Point2 &p_src) const;
- Color get_pixel(int p_x, int p_y) const;
- void set_pixelv(const Point2 &p_dest, const Color &p_color);
- void set_pixel(int p_x, int p_y, const Color &p_color);
- void copy_internals_from(const Ref<Image> &p_image) {
- ERR_FAIL_COND(p_image.is_null());
- format = p_image->format;
- width = p_image->width;
- height = p_image->height;
- mipmaps = p_image->mipmaps;
- data = p_image->data;
- }
- ~Image();
- };
- VARIANT_ENUM_CAST(Image::Format)
- VARIANT_ENUM_CAST(Image::Interpolation)
- VARIANT_ENUM_CAST(Image::CompressMode)
- VARIANT_ENUM_CAST(Image::CompressSource)
- VARIANT_ENUM_CAST(Image::AlphaMode)
- #endif
|