image.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*************************************************************************/
  2. /* image.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef IMAGE_H
  31. #define IMAGE_H
  32. #include "core/io/resource.h"
  33. #include "core/math/color.h"
  34. #include "core/math/rect2.h"
  35. /**
  36. * Image storage class. This is used to store an image in user memory, as well as
  37. * providing some basic methods for image manipulation.
  38. * Images can be loaded from a file, or registered into the Render object as textures.
  39. */
  40. class Image;
  41. typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img);
  42. typedef Vector<uint8_t> (*SavePNGBufferFunc)(const Ref<Image> &p_img);
  43. typedef Error (*SaveJPGFunc)(const String &p_path, const Ref<Image> &p_img, float p_quality);
  44. typedef Vector<uint8_t> (*SaveJPGBufferFunc)(const Ref<Image> &p_img, float p_quality);
  45. typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size);
  46. typedef Error (*SaveWebPFunc)(const String &p_path, const Ref<Image> &p_img, const bool p_lossy, const float p_quality);
  47. typedef Vector<uint8_t> (*SaveWebPBufferFunc)(const Ref<Image> &p_img, const bool p_lossy, const float p_quality);
  48. typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale);
  49. typedef Vector<uint8_t> (*SaveEXRBufferFunc)(const Ref<Image> &p_img, bool p_grayscale);
  50. class Image : public Resource {
  51. GDCLASS(Image, Resource);
  52. public:
  53. static SavePNGFunc save_png_func;
  54. static SaveJPGFunc save_jpg_func;
  55. static SaveEXRFunc save_exr_func;
  56. static SavePNGBufferFunc save_png_buffer_func;
  57. static SaveEXRBufferFunc save_exr_buffer_func;
  58. static SaveJPGBufferFunc save_jpg_buffer_func;
  59. static SaveWebPFunc save_webp_func;
  60. static SaveWebPBufferFunc save_webp_buffer_func;
  61. enum {
  62. MAX_WIDTH = (1 << 24), // force a limit somehow
  63. MAX_HEIGHT = (1 << 24), // force a limit somehow
  64. MAX_PIXELS = 268435456
  65. };
  66. enum Format {
  67. FORMAT_L8, //luminance
  68. FORMAT_LA8, //luminance-alpha
  69. FORMAT_R8,
  70. FORMAT_RG8,
  71. FORMAT_RGB8,
  72. FORMAT_RGBA8,
  73. FORMAT_RGBA4444,
  74. FORMAT_RGB565,
  75. FORMAT_RF, //float
  76. FORMAT_RGF,
  77. FORMAT_RGBF,
  78. FORMAT_RGBAF,
  79. FORMAT_RH, //half float
  80. FORMAT_RGH,
  81. FORMAT_RGBH,
  82. FORMAT_RGBAH,
  83. FORMAT_RGBE9995,
  84. FORMAT_DXT1, //s3tc bc1
  85. FORMAT_DXT3, //bc2
  86. FORMAT_DXT5, //bc3
  87. FORMAT_RGTC_R,
  88. FORMAT_RGTC_RG,
  89. FORMAT_BPTC_RGBA, //btpc bc7
  90. FORMAT_BPTC_RGBF, //float bc6h
  91. FORMAT_BPTC_RGBFU, //unsigned float bc6hu
  92. FORMAT_ETC, //etc1
  93. FORMAT_ETC2_R11, //etc2
  94. FORMAT_ETC2_R11S, //signed, NOT srgb.
  95. FORMAT_ETC2_RG11,
  96. FORMAT_ETC2_RG11S,
  97. FORMAT_ETC2_RGB8,
  98. FORMAT_ETC2_RGBA8,
  99. FORMAT_ETC2_RGB8A1,
  100. FORMAT_ETC2_RA_AS_RG, //used to make basis universal happy
  101. FORMAT_DXT5_RA_AS_RG, //used to make basis universal happy
  102. FORMAT_MAX
  103. };
  104. static const char *format_names[FORMAT_MAX];
  105. enum Interpolation {
  106. INTERPOLATE_NEAREST,
  107. INTERPOLATE_BILINEAR,
  108. INTERPOLATE_CUBIC,
  109. INTERPOLATE_TRILINEAR,
  110. INTERPOLATE_LANCZOS,
  111. /* INTERPOLATE_TRICUBIC, */
  112. /* INTERPOLATE GAUSS */
  113. };
  114. //this is used for compression
  115. enum UsedChannels {
  116. USED_CHANNELS_L,
  117. USED_CHANNELS_LA,
  118. USED_CHANNELS_R,
  119. USED_CHANNELS_RG,
  120. USED_CHANNELS_RGB,
  121. USED_CHANNELS_RGBA,
  122. };
  123. //some functions provided by something else
  124. static ImageMemLoadFunc _png_mem_loader_func;
  125. static ImageMemLoadFunc _jpg_mem_loader_func;
  126. static ImageMemLoadFunc _webp_mem_loader_func;
  127. static ImageMemLoadFunc _tga_mem_loader_func;
  128. static ImageMemLoadFunc _bmp_mem_loader_func;
  129. static void (*_image_compress_bc_func)(Image *, float, UsedChannels p_channels);
  130. static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, UsedChannels p_channels);
  131. static void (*_image_compress_etc1_func)(Image *, float);
  132. static void (*_image_compress_etc2_func)(Image *, float, UsedChannels p_channels);
  133. static void (*_image_decompress_bc)(Image *);
  134. static void (*_image_decompress_bptc)(Image *);
  135. static void (*_image_decompress_etc1)(Image *);
  136. static void (*_image_decompress_etc2)(Image *);
  137. static Vector<uint8_t> (*webp_lossy_packer)(const Ref<Image> &p_image, float p_quality);
  138. static Vector<uint8_t> (*webp_lossless_packer)(const Ref<Image> &p_image);
  139. static Ref<Image> (*webp_unpacker)(const Vector<uint8_t> &p_buffer);
  140. static Vector<uint8_t> (*png_packer)(const Ref<Image> &p_image);
  141. static Ref<Image> (*png_unpacker)(const Vector<uint8_t> &p_buffer);
  142. static Vector<uint8_t> (*basis_universal_packer)(const Ref<Image> &p_image, UsedChannels p_channels);
  143. static Ref<Image> (*basis_universal_unpacker)(const Vector<uint8_t> &p_buffer);
  144. static Ref<Image> (*basis_universal_unpacker_ptr)(const uint8_t *p_data, int p_size);
  145. _FORCE_INLINE_ Color _get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const;
  146. _FORCE_INLINE_ void _set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color);
  147. protected:
  148. static void _bind_methods();
  149. private:
  150. Format format = FORMAT_L8;
  151. Vector<uint8_t> data;
  152. int width = 0;
  153. int height = 0;
  154. bool mipmaps = false;
  155. void _copy_internals_from(const Image &p_image) {
  156. format = p_image.format;
  157. width = p_image.width;
  158. height = p_image.height;
  159. mipmaps = p_image.mipmaps;
  160. data = p_image.data;
  161. }
  162. _FORCE_INLINE_ void _get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const; //get where the mipmap begins in data
  163. static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1, int *r_mm_width = nullptr, int *r_mm_height = nullptr);
  164. bool _can_modify(Format p_format) const;
  165. _FORCE_INLINE_ void _get_clipped_src_and_dest_rects(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Point2i &p_dest, Rect2i &r_clipped_src_rect, Rect2i &r_clipped_dest_rect) const;
  166. _FORCE_INLINE_ void _put_pixelb(int p_x, int p_y, uint32_t p_pixel_size, uint8_t *p_data, const uint8_t *p_pixel);
  167. _FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixel_size, const uint8_t *p_data, uint8_t *p_pixel);
  168. _FORCE_INLINE_ void _repeat_pixel_over_subsequent_memory(uint8_t *p_pixel, int p_pixel_size, int p_count);
  169. void _set_data(const Dictionary &p_data);
  170. Dictionary _get_data() const;
  171. Error _load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc p_loader);
  172. 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);
  173. static void average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d);
  174. 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);
  175. 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);
  176. static void renormalize_uint8(uint8_t *p_rgb);
  177. static void renormalize_float(float *p_rgb);
  178. static void renormalize_half(uint16_t *p_rgb);
  179. static void renormalize_rgbe9995(uint32_t *p_rgb);
  180. public:
  181. int get_width() const; ///< Get image width
  182. int get_height() const; ///< Get image height
  183. Vector2i get_size() const;
  184. bool has_mipmaps() const;
  185. int get_mipmap_count() const;
  186. /**
  187. * Convert the image to another format, conversion only to raw byte format
  188. */
  189. void convert(Format p_new_format);
  190. /**
  191. * Get the current image format.
  192. */
  193. Format get_format() const;
  194. int get_mipmap_byte_size(int p_mipmap) const; //get where the mipmap begins in data
  195. int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
  196. void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const; //get where the mipmap begins in data
  197. void get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const; //get where the mipmap begins in data
  198. enum Image3DValidateError {
  199. VALIDATE_3D_OK,
  200. VALIDATE_3D_ERR_IMAGE_EMPTY,
  201. VALIDATE_3D_ERR_MISSING_IMAGES,
  202. VALIDATE_3D_ERR_EXTRA_IMAGES,
  203. VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH,
  204. VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH,
  205. VALIDATE_3D_ERR_IMAGE_HAS_MIPMAPS,
  206. };
  207. static Image3DValidateError validate_3d_image(Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_images);
  208. static String get_3d_image_validation_error_text(Image3DValidateError p_error);
  209. /**
  210. * Resize the image, using the preferred interpolation method.
  211. */
  212. void resize_to_po2(bool p_square = false, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
  213. void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
  214. void shrink_x2();
  215. bool is_size_po2() const;
  216. /**
  217. * Crop the image to a specific size, if larger, then the image is filled by black
  218. */
  219. void crop_from_point(int p_x, int p_y, int p_width, int p_height);
  220. void crop(int p_width, int p_height);
  221. void rotate_90(ClockDirection p_direction);
  222. void rotate_180();
  223. void flip_x();
  224. void flip_y();
  225. /**
  226. * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1)
  227. */
  228. Error generate_mipmaps(bool p_renormalize = false);
  229. enum RoughnessChannel {
  230. ROUGHNESS_CHANNEL_R,
  231. ROUGHNESS_CHANNEL_G,
  232. ROUGHNESS_CHANNEL_B,
  233. ROUGHNESS_CHANNEL_A,
  234. ROUGHNESS_CHANNEL_L,
  235. };
  236. Error generate_mipmap_roughness(RoughnessChannel p_roughness_channel, const Ref<Image> &p_normal_map);
  237. void clear_mipmaps();
  238. void normalize(); //for normal maps
  239. /**
  240. * Create a new image of a given size and format. Current image will be lost
  241. */
  242. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  243. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data);
  244. void create(const char **p_xpm);
  245. /**
  246. * returns true when the image is empty (0,0) in size
  247. */
  248. bool is_empty() const;
  249. Vector<uint8_t> get_data() const;
  250. Error load(const String &p_path);
  251. static Ref<Image> load_from_file(const String &p_path);
  252. Error save_png(const String &p_path) const;
  253. Error save_jpg(const String &p_path, float p_quality = 0.75) const;
  254. Vector<uint8_t> save_png_to_buffer() const;
  255. Vector<uint8_t> save_jpg_to_buffer(float p_quality = 0.75) const;
  256. Vector<uint8_t> save_exr_to_buffer(bool p_grayscale) const;
  257. Error save_exr(const String &p_path, bool p_grayscale) const;
  258. Error save_webp(const String &p_path, const bool p_lossy = false, const float p_quality = 0.75f) const;
  259. Vector<uint8_t> save_webp_to_buffer(const bool p_lossy = false, const float p_quality = 0.75f) const;
  260. void create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  261. create(p_width, p_height, p_use_mipmaps, p_format);
  262. }
  263. void create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) {
  264. create(p_width, p_height, p_use_mipmaps, p_format, p_data);
  265. }
  266. /**
  267. * create an empty image
  268. */
  269. Image() {}
  270. /**
  271. * create an empty image of a specific size and format
  272. */
  273. Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  274. /**
  275. * import an image of a specific size and format from a pointer
  276. */
  277. Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data);
  278. ~Image() {}
  279. enum AlphaMode {
  280. ALPHA_NONE,
  281. ALPHA_BIT,
  282. ALPHA_BLEND
  283. };
  284. AlphaMode detect_alpha() const;
  285. bool is_invisible() const;
  286. static int get_format_pixel_size(Format p_format);
  287. static int get_format_pixel_rshift(Format p_format);
  288. static int get_format_block_size(Format p_format);
  289. static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h);
  290. static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false);
  291. static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
  292. static Size2i get_image_mipmap_size(int p_width, int p_height, Format p_format, int p_mipmap);
  293. static int get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap);
  294. static int get_image_mipmap_offset_and_dimensions(int p_width, int p_height, Format p_format, int p_mipmap, int &r_w, int &r_h);
  295. enum CompressMode {
  296. COMPRESS_S3TC,
  297. COMPRESS_ETC,
  298. COMPRESS_ETC2,
  299. COMPRESS_BPTC,
  300. COMPRESS_MAX,
  301. };
  302. enum CompressSource {
  303. COMPRESS_SOURCE_GENERIC,
  304. COMPRESS_SOURCE_SRGB,
  305. COMPRESS_SOURCE_NORMAL,
  306. COMPRESS_SOURCE_MAX,
  307. };
  308. Error compress(CompressMode p_mode, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7);
  309. Error compress_from_channels(CompressMode p_mode, UsedChannels p_channels, float p_lossy_quality = 0.7);
  310. Error decompress();
  311. bool is_compressed() const;
  312. void fix_alpha_edges();
  313. void premultiply_alpha();
  314. void srgb_to_linear();
  315. void normal_map_to_xy();
  316. Ref<Image> rgbe_to_srgb();
  317. Ref<Image> get_image_from_mipmap(int p_mipamp) const;
  318. void bump_map_to_normal_map(float bump_scale = 1.0);
  319. void blit_rect(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Point2i &p_dest);
  320. void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2i &p_src_rect, const Point2i &p_dest);
  321. void blend_rect(const Ref<Image> &p_src, const Rect2i &p_src_rect, const Point2i &p_dest);
  322. void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2i &p_src_rect, const Point2i &p_dest);
  323. void fill(const Color &p_color);
  324. void fill_rect(const Rect2i &p_rect, const Color &p_color);
  325. Rect2i get_used_rect() const;
  326. Ref<Image> get_rect(const Rect2i &p_area) const;
  327. static void set_compress_bc_func(void (*p_compress_func)(Image *, float, UsedChannels));
  328. static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, UsedChannels));
  329. static String get_format_name(Format p_format);
  330. Error load_png_from_buffer(const Vector<uint8_t> &p_array);
  331. Error load_jpg_from_buffer(const Vector<uint8_t> &p_array);
  332. Error load_webp_from_buffer(const Vector<uint8_t> &p_array);
  333. Error load_tga_from_buffer(const Vector<uint8_t> &p_array);
  334. Error load_bmp_from_buffer(const Vector<uint8_t> &p_array);
  335. void convert_rg_to_ra_rgba8();
  336. void convert_ra_rgba8_to_rg();
  337. Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
  338. Image(const char **p_xpm);
  339. virtual Ref<Resource> duplicate(bool p_subresources = false) const override;
  340. UsedChannels detect_used_channels(CompressSource p_source = COMPRESS_SOURCE_GENERIC) const;
  341. void optimize_channels();
  342. Color get_pixelv(const Point2i &p_point) const;
  343. Color get_pixel(int p_x, int p_y) const;
  344. void set_pixelv(const Point2i &p_point, const Color &p_color);
  345. void set_pixel(int p_x, int p_y, const Color &p_color);
  346. void adjust_bcs(float p_brightness, float p_contrast, float p_saturation);
  347. void set_as_black();
  348. void copy_internals_from(const Ref<Image> &p_image) {
  349. ERR_FAIL_COND_MSG(p_image.is_null(), "It's not a reference to a valid Image object.");
  350. format = p_image->format;
  351. width = p_image->width;
  352. height = p_image->height;
  353. mipmaps = p_image->mipmaps;
  354. data = p_image->data;
  355. }
  356. Dictionary compute_image_metrics(const Ref<Image> p_compared_image, bool p_luma_metric = true);
  357. };
  358. VARIANT_ENUM_CAST(Image::Format)
  359. VARIANT_ENUM_CAST(Image::Interpolation)
  360. VARIANT_ENUM_CAST(Image::CompressMode)
  361. VARIANT_ENUM_CAST(Image::CompressSource)
  362. VARIANT_ENUM_CAST(Image::UsedChannels)
  363. VARIANT_ENUM_CAST(Image::AlphaMode)
  364. VARIANT_ENUM_CAST(Image::RoughnessChannel)
  365. #endif // IMAGE_H