image.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*************************************************************************/
  2. /* image.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/color.h"
  33. #include "core/math/rect2.h"
  34. #include "core/resource.h"
  35. /**
  36. * @author Juan Linietsky <[email protected]>
  37. *
  38. * Image storage class. This is used to store an image in user memory, as well as
  39. * providing some basic methods for image manipulation.
  40. * Images can be loaded from a file, or registered into the Render object as textures.
  41. */
  42. class Image;
  43. typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img);
  44. typedef Vector<uint8_t> (*SavePNGBufferFunc)(const Ref<Image> &p_img);
  45. typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size);
  46. typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale);
  47. class Image : public Resource {
  48. GDCLASS(Image, Resource);
  49. public:
  50. static SavePNGFunc save_png_func;
  51. static SaveEXRFunc save_exr_func;
  52. static SavePNGBufferFunc save_png_buffer_func;
  53. enum {
  54. MAX_WIDTH = (1 << 24), // force a limit somehow
  55. MAX_HEIGHT = (1 << 24), // force a limit somehow
  56. MAX_PIXELS = 268435456
  57. };
  58. enum Format {
  59. FORMAT_L8, //luminance
  60. FORMAT_LA8, //luminance-alpha
  61. FORMAT_R8,
  62. FORMAT_RG8,
  63. FORMAT_RGB8,
  64. FORMAT_RGBA8,
  65. FORMAT_RGBA4444,
  66. FORMAT_RGB565,
  67. FORMAT_RF, //float
  68. FORMAT_RGF,
  69. FORMAT_RGBF,
  70. FORMAT_RGBAF,
  71. FORMAT_RH, //half float
  72. FORMAT_RGH,
  73. FORMAT_RGBH,
  74. FORMAT_RGBAH,
  75. FORMAT_RGBE9995,
  76. FORMAT_DXT1, //s3tc bc1
  77. FORMAT_DXT3, //bc2
  78. FORMAT_DXT5, //bc3
  79. FORMAT_RGTC_R,
  80. FORMAT_RGTC_RG,
  81. FORMAT_BPTC_RGBA, //btpc bc7
  82. FORMAT_BPTC_RGBF, //float bc6h
  83. FORMAT_BPTC_RGBFU, //unsigned float bc6hu
  84. FORMAT_PVRTC2, //pvrtc
  85. FORMAT_PVRTC2A,
  86. FORMAT_PVRTC4,
  87. FORMAT_PVRTC4A,
  88. FORMAT_ETC, //etc1
  89. FORMAT_ETC2_R11, //etc2
  90. FORMAT_ETC2_R11S, //signed, NOT srgb.
  91. FORMAT_ETC2_RG11,
  92. FORMAT_ETC2_RG11S,
  93. FORMAT_ETC2_RGB8,
  94. FORMAT_ETC2_RGBA8,
  95. FORMAT_ETC2_RGB8A1,
  96. FORMAT_ETC2_RA_AS_RG, //used to make basis universal happy
  97. FORMAT_DXT5_RA_AS_RG, //used to make basis universal happy
  98. FORMAT_MAX
  99. };
  100. static const char *format_names[FORMAT_MAX];
  101. enum Interpolation {
  102. INTERPOLATE_NEAREST,
  103. INTERPOLATE_BILINEAR,
  104. INTERPOLATE_CUBIC,
  105. INTERPOLATE_TRILINEAR,
  106. INTERPOLATE_LANCZOS,
  107. /* INTERPOLATE_TRICUBIC, */
  108. /* INTERPOLATE GAUSS */
  109. };
  110. //this is used for compression
  111. enum UsedChannels {
  112. USED_CHANNELS_L,
  113. USED_CHANNELS_LA,
  114. USED_CHANNELS_R,
  115. USED_CHANNELS_RG,
  116. USED_CHANNELS_RGB,
  117. USED_CHANNELS_RGBA,
  118. };
  119. //some functions provided by something else
  120. static ImageMemLoadFunc _png_mem_loader_func;
  121. static ImageMemLoadFunc _jpg_mem_loader_func;
  122. static ImageMemLoadFunc _webp_mem_loader_func;
  123. static void (*_image_compress_bc_func)(Image *, float, UsedChannels p_channels);
  124. static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, UsedChannels p_channels);
  125. static void (*_image_compress_pvrtc2_func)(Image *);
  126. static void (*_image_compress_pvrtc4_func)(Image *);
  127. static void (*_image_compress_etc1_func)(Image *, float);
  128. static void (*_image_compress_etc2_func)(Image *, float, UsedChannels p_channels);
  129. static void (*_image_decompress_pvrtc)(Image *);
  130. static void (*_image_decompress_bc)(Image *);
  131. static void (*_image_decompress_bptc)(Image *);
  132. static void (*_image_decompress_etc1)(Image *);
  133. static void (*_image_decompress_etc2)(Image *);
  134. static Vector<uint8_t> (*lossy_packer)(const Ref<Image> &p_image, float p_quality);
  135. static Ref<Image> (*lossy_unpacker)(const Vector<uint8_t> &p_buffer);
  136. static Vector<uint8_t> (*lossless_packer)(const Ref<Image> &p_image);
  137. static Ref<Image> (*lossless_unpacker)(const Vector<uint8_t> &p_buffer);
  138. static Vector<uint8_t> (*basis_universal_packer)(const Ref<Image> &p_image, UsedChannels p_channels);
  139. static Ref<Image> (*basis_universal_unpacker)(const Vector<uint8_t> &p_buffer);
  140. _FORCE_INLINE_ Color _get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const;
  141. _FORCE_INLINE_ void _set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color);
  142. protected:
  143. static void _bind_methods();
  144. private:
  145. void _create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  146. create(p_width, p_height, p_use_mipmaps, p_format);
  147. }
  148. void _create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) {
  149. create(p_width, p_height, p_use_mipmaps, p_format, p_data);
  150. }
  151. Format format;
  152. Vector<uint8_t> data;
  153. int width, height;
  154. bool mipmaps;
  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 _put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel);
  166. _FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel);
  167. void _set_data(const Dictionary &p_data);
  168. Dictionary _get_data() const;
  169. Error _load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc p_loader);
  170. 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);
  171. static void average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d);
  172. 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);
  173. 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);
  174. static void renormalize_uint8(uint8_t *p_rgb);
  175. static void renormalize_float(float *p_rgb);
  176. static void renormalize_half(uint16_t *p_rgb);
  177. static void renormalize_rgbe9995(uint32_t *p_rgb);
  178. public:
  179. int get_width() const; ///< Get image width
  180. int get_height() const; ///< Get image height
  181. Vector2 get_size() const;
  182. bool has_mipmaps() const;
  183. int get_mipmap_count() const;
  184. /**
  185. * Convert the image to another format, conversion only to raw byte format
  186. */
  187. void convert(Format p_new_format);
  188. /**
  189. * Get the current image format.
  190. */
  191. Format get_format() const;
  192. int get_mipmap_byte_size(int p_mipmap) const; //get where the mipmap begins in data
  193. int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
  194. void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const; //get where the mipmap begins in data
  195. 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
  196. /**
  197. * Resize the image, using the preferred interpolation method.
  198. */
  199. void resize_to_po2(bool p_square = false);
  200. void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
  201. void shrink_x2();
  202. void expand_x2_hq2x();
  203. bool is_size_po2() const;
  204. /**
  205. * Crop the image to a specific size, if larger, then the image is filled by black
  206. */
  207. void crop_from_point(int p_x, int p_y, int p_width, int p_height);
  208. void crop(int p_width, int p_height);
  209. void flip_x();
  210. void flip_y();
  211. /**
  212. * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1)
  213. */
  214. Error generate_mipmaps(bool p_renormalize = false);
  215. enum RoughnessChannel {
  216. ROUGHNESS_CHANNEL_R,
  217. ROUGHNESS_CHANNEL_G,
  218. ROUGHNESS_CHANNEL_B,
  219. ROUGHNESS_CHANNEL_A,
  220. ROUGHNESS_CHANNEL_L,
  221. };
  222. Error generate_mipmap_roughness(RoughnessChannel p_roughness_channel, const Ref<Image> &p_normal_map);
  223. void clear_mipmaps();
  224. void normalize(); //for normal maps
  225. /**
  226. * Create a new image of a given size and format. Current image will be lost
  227. */
  228. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  229. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data);
  230. void create(const char **p_xpm);
  231. /**
  232. * returns true when the image is empty (0,0) in size
  233. */
  234. bool empty() const;
  235. Vector<uint8_t> get_data() const;
  236. Error load(const String &p_path);
  237. Error save_png(const String &p_path) const;
  238. Vector<uint8_t> save_png_to_buffer() const;
  239. Error save_exr(const String &p_path, bool p_grayscale) const;
  240. /**
  241. * create an empty image
  242. */
  243. Image();
  244. /**
  245. * create an empty image of a specific size and format
  246. */
  247. Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  248. /**
  249. * import an image of a specific size and format from a pointer
  250. */
  251. Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data);
  252. enum AlphaMode {
  253. ALPHA_NONE,
  254. ALPHA_BIT,
  255. ALPHA_BLEND
  256. };
  257. AlphaMode detect_alpha() const;
  258. bool is_invisible() const;
  259. static int get_format_pixel_size(Format p_format);
  260. static int get_format_pixel_rshift(Format p_format);
  261. static int get_format_block_size(Format p_format);
  262. static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h);
  263. static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false);
  264. static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
  265. static Size2i get_image_mipmap_size(int p_width, int p_height, Format p_format, int p_mipmap);
  266. static int get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap);
  267. 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);
  268. enum CompressMode {
  269. COMPRESS_S3TC,
  270. COMPRESS_PVRTC2,
  271. COMPRESS_PVRTC4,
  272. COMPRESS_ETC,
  273. COMPRESS_ETC2,
  274. COMPRESS_BPTC
  275. };
  276. enum CompressSource {
  277. COMPRESS_SOURCE_GENERIC,
  278. COMPRESS_SOURCE_SRGB,
  279. COMPRESS_SOURCE_NORMAL
  280. };
  281. Error compress(CompressMode p_mode, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7);
  282. Error compress_from_channels(CompressMode p_mode, UsedChannels p_channels, float p_lossy_quality = 0.7);
  283. Error decompress();
  284. bool is_compressed() const;
  285. void fix_alpha_edges();
  286. void premultiply_alpha();
  287. void srgb_to_linear();
  288. void normalmap_to_xy();
  289. Ref<Image> rgbe_to_srgb();
  290. Ref<Image> get_image_from_mipmap(int p_mipamp) const;
  291. void bumpmap_to_normalmap(float bump_scale = 1.0);
  292. void blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  293. void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  294. void blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  295. void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  296. void fill(const Color &c);
  297. Rect2 get_used_rect() const;
  298. Ref<Image> get_rect(const Rect2 &p_area) const;
  299. static void set_compress_bc_func(void (*p_compress_func)(Image *, float, UsedChannels));
  300. static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, UsedChannels));
  301. static String get_format_name(Format p_format);
  302. Error load_png_from_buffer(const Vector<uint8_t> &p_array);
  303. Error load_jpg_from_buffer(const Vector<uint8_t> &p_array);
  304. Error load_webp_from_buffer(const Vector<uint8_t> &p_array);
  305. void convert_rg_to_ra_rgba8();
  306. void convert_ra_rgba8_to_rg();
  307. Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
  308. Image(const char **p_xpm);
  309. virtual Ref<Resource> duplicate(bool p_subresources = false) const;
  310. UsedChannels detect_used_channels(CompressSource p_source = COMPRESS_SOURCE_GENERIC);
  311. void optimize_channels();
  312. Color get_pixelv(const Point2 &p_src) const;
  313. Color get_pixel(int p_x, int p_y) const;
  314. void set_pixelv(const Point2 &p_dst, const Color &p_color);
  315. void set_pixel(int p_x, int p_y, const Color &p_color);
  316. void copy_internals_from(const Ref<Image> &p_image) {
  317. ERR_FAIL_COND_MSG(p_image.is_null(), "It's not a reference to a valid Image object.");
  318. format = p_image->format;
  319. width = p_image->width;
  320. height = p_image->height;
  321. mipmaps = p_image->mipmaps;
  322. data = p_image->data;
  323. }
  324. ~Image();
  325. };
  326. VARIANT_ENUM_CAST(Image::Format)
  327. VARIANT_ENUM_CAST(Image::Interpolation)
  328. VARIANT_ENUM_CAST(Image::CompressMode)
  329. VARIANT_ENUM_CAST(Image::CompressSource)
  330. VARIANT_ENUM_CAST(Image::UsedChannels)
  331. VARIANT_ENUM_CAST(Image::AlphaMode)
  332. VARIANT_ENUM_CAST(Image::RoughnessChannel)
  333. #endif // IMAGE_H