image.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**************************************************************************/
  2. /* image.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/pool_vector.h"
  35. #include "core/resource.h"
  36. /**
  37. * @author Juan Linietsky <reduzio@gmail.com>
  38. *
  39. * Image storage class. This is used to store an image in user memory, as well as
  40. * providing some basic methods for image manipulation.
  41. * Images can be loaded from a file, or registered into the Render object as textures.
  42. */
  43. class Image;
  44. typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img);
  45. typedef PoolVector<uint8_t> (*SavePNGBufferFunc)(const Ref<Image> &p_img);
  46. typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size);
  47. typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale);
  48. class Image : public Resource {
  49. GDCLASS(Image, Resource);
  50. public:
  51. static SavePNGFunc save_png_func;
  52. static SaveEXRFunc save_exr_func;
  53. static SavePNGBufferFunc save_png_buffer_func;
  54. enum {
  55. MAX_WIDTH = 16384, // force a limit somehow
  56. MAX_HEIGHT = 16384 // force a limit somehow
  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_RGBA5551,
  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_MAX
  97. };
  98. static const char *format_names[FORMAT_MAX];
  99. enum Interpolation {
  100. INTERPOLATE_NEAREST,
  101. INTERPOLATE_BILINEAR,
  102. INTERPOLATE_CUBIC,
  103. INTERPOLATE_TRILINEAR,
  104. INTERPOLATE_LANCZOS,
  105. /* INTERPOLATE_TRICUBIC, */
  106. /* INTERPOLATE GAUSS */
  107. };
  108. enum CompressSource {
  109. COMPRESS_SOURCE_GENERIC,
  110. COMPRESS_SOURCE_SRGB,
  111. COMPRESS_SOURCE_NORMAL,
  112. COMPRESS_SOURCE_LAYERED,
  113. COMPRESS_SOURCE_MAX,
  114. };
  115. //some functions provided by something else
  116. static ImageMemLoadFunc _png_mem_loader_func;
  117. static ImageMemLoadFunc _jpg_mem_loader_func;
  118. static ImageMemLoadFunc _webp_mem_loader_func;
  119. static ImageMemLoadFunc _tga_mem_loader_func;
  120. static ImageMemLoadFunc _bmp_mem_loader_func;
  121. static void (*_image_compress_bc_func)(Image *, float, CompressSource p_source);
  122. static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, CompressSource p_source);
  123. static void (*_image_compress_pvrtc2_func)(Image *);
  124. static void (*_image_compress_pvrtc4_func)(Image *);
  125. static void (*_image_compress_etc1_func)(Image *, float);
  126. static void (*_image_compress_etc2_func)(Image *, float, CompressSource p_source);
  127. static void (*_image_decompress_pvrtc)(Image *);
  128. static void (*_image_decompress_bc)(Image *);
  129. static void (*_image_decompress_bptc)(Image *);
  130. static void (*_image_decompress_etc1)(Image *);
  131. static void (*_image_decompress_etc2)(Image *);
  132. static PoolVector<uint8_t> (*webp_lossy_packer)(const Ref<Image> &p_image, float p_quality);
  133. static PoolVector<uint8_t> (*webp_lossless_packer)(const Ref<Image> &p_image);
  134. static Ref<Image> (*webp_unpacker)(const PoolVector<uint8_t> &p_buffer);
  135. static PoolVector<uint8_t> (*png_packer)(const Ref<Image> &p_image);
  136. static Ref<Image> (*png_unpacker)(const PoolVector<uint8_t> &p_buffer);
  137. PoolVector<uint8_t>::Write write_lock;
  138. protected:
  139. static void _bind_methods();
  140. private:
  141. void _create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  142. create(p_width, p_height, p_use_mipmaps, p_format);
  143. }
  144. void _create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  145. create(p_width, p_height, p_use_mipmaps, p_format, p_data);
  146. }
  147. Format format;
  148. PoolVector<uint8_t> data;
  149. int width, height;
  150. bool mipmaps;
  151. void _copy_internals_from(const Image &p_image) {
  152. format = p_image.format;
  153. width = p_image.width;
  154. height = p_image.height;
  155. mipmaps = p_image.mipmaps;
  156. data = p_image.data;
  157. }
  158. _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
  159. static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1);
  160. bool _can_modify(Format p_format) const;
  161. _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;
  162. _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);
  163. _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);
  164. _FORCE_INLINE_ void _repeat_pixel_over_subsequent_memory(uint8_t *p_pixel, int p_pixel_size, int p_count);
  165. void _set_data(const Dictionary &p_data);
  166. Dictionary _get_data() const;
  167. Error _load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader);
  168. 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);
  169. static void average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d);
  170. 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);
  171. 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);
  172. static void renormalize_uint8(uint8_t *p_rgb);
  173. static void renormalize_float(float *p_rgb);
  174. static void renormalize_half(uint16_t *p_rgb);
  175. static void renormalize_rgbe9995(uint32_t *p_rgb);
  176. public:
  177. int get_width() const; ///< Get image width
  178. int get_height() const; ///< Get image height
  179. Vector2 get_size() const;
  180. bool has_mipmaps() const;
  181. int get_mipmap_count() const;
  182. /**
  183. * Convert the image to another format, conversion only to raw byte format
  184. */
  185. void convert(Format p_new_format);
  186. /**
  187. * Get the current image format.
  188. */
  189. Format get_format() const;
  190. int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
  191. void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const; //get where the mipmap begins in data
  192. 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
  193. /**
  194. * Resize the image, using the preferred interpolation method.
  195. */
  196. void resize_to_po2(bool p_square = false, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
  197. void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
  198. void shrink_x2();
  199. void expand_x2_hq2x();
  200. bool is_size_po2() const;
  201. /**
  202. * Crop the image to a specific size, if larger, then the image is filled by black
  203. */
  204. void crop_from_point(int p_x, int p_y, int p_width, int p_height);
  205. void crop(int p_width, int p_height);
  206. void flip_x();
  207. void flip_y();
  208. /**
  209. * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1)
  210. */
  211. Error generate_mipmaps(bool p_renormalize = false);
  212. void clear_mipmaps();
  213. void normalize(); //for normal maps
  214. /**
  215. * Create a new image of a given size and format. Current image will be lost
  216. */
  217. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  218. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
  219. void create(const char **p_xpm);
  220. /**
  221. * returns true when the image is empty (0,0) in size
  222. */
  223. bool empty() const;
  224. PoolVector<uint8_t> get_data() const;
  225. Error load(const String &p_path);
  226. Error save_png(const String &p_path) const;
  227. PoolVector<uint8_t> save_png_to_buffer() const;
  228. Error save_exr(const String &p_path, bool p_grayscale) const;
  229. /**
  230. * create an empty image
  231. */
  232. Image();
  233. /**
  234. * create an empty image of a specific size and format
  235. */
  236. Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  237. /**
  238. * import an image of a specific size and format from a pointer
  239. */
  240. Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
  241. enum AlphaMode {
  242. ALPHA_NONE,
  243. ALPHA_BIT,
  244. ALPHA_BLEND
  245. };
  246. AlphaMode detect_alpha() const;
  247. bool is_invisible() const;
  248. static int get_format_pixel_size(Format p_format);
  249. static int get_format_pixel_rshift(Format p_format);
  250. static int get_format_block_size(Format p_format);
  251. static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h);
  252. static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false);
  253. static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
  254. static int get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap);
  255. enum CompressMode {
  256. COMPRESS_S3TC,
  257. COMPRESS_PVRTC2,
  258. COMPRESS_PVRTC4,
  259. COMPRESS_ETC,
  260. COMPRESS_ETC2,
  261. COMPRESS_BPTC,
  262. COMPRESS_MAX,
  263. };
  264. Error compress(CompressMode p_mode = COMPRESS_S3TC, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7);
  265. Error decompress();
  266. bool is_compressed() const;
  267. void fix_alpha_edges();
  268. void premultiply_alpha();
  269. void srgb_to_linear();
  270. void normalmap_to_xy();
  271. Ref<Image> rgbe_to_srgb();
  272. void bumpmap_to_normalmap(float bump_scale = 1.0);
  273. void blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  274. void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  275. void blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  276. void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  277. void fill(const Color &p_color);
  278. void fill_rect(const Rect2 &p_rect, const Color &p_color);
  279. Rect2 get_used_rect() const;
  280. Ref<Image> get_rect(const Rect2 &p_area) const;
  281. static void set_compress_bc_func(void (*p_compress_func)(Image *, float, CompressSource));
  282. static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, CompressSource));
  283. static String get_format_name(Format p_format);
  284. Error load_png_from_buffer(const PoolVector<uint8_t> &p_array);
  285. Error load_jpg_from_buffer(const PoolVector<uint8_t> &p_array);
  286. Error load_webp_from_buffer(const PoolVector<uint8_t> &p_array);
  287. Error load_tga_from_buffer(const PoolVector<uint8_t> &p_array);
  288. Error load_bmp_from_buffer(const PoolVector<uint8_t> &p_array);
  289. Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
  290. Image(const char **p_xpm);
  291. virtual Ref<Resource> duplicate(bool p_subresources = false) const;
  292. void lock();
  293. void unlock();
  294. //this is used for compression
  295. enum DetectChannels {
  296. DETECTED_L,
  297. DETECTED_LA,
  298. DETECTED_R,
  299. DETECTED_RG,
  300. DETECTED_RGB,
  301. DETECTED_RGBA,
  302. };
  303. DetectChannels get_detected_channels();
  304. void optimize_channels();
  305. Color get_pixelv(const Point2 &p_src) const;
  306. Color get_pixel(int p_x, int p_y) const;
  307. void set_pixelv(const Point2 &p_dst, const Color &p_color);
  308. void set_pixel(int p_x, int p_y, const Color &p_color);
  309. void copy_internals_from(const Ref<Image> &p_image) {
  310. ERR_FAIL_COND_MSG(p_image.is_null(), "It's not a reference to a valid Image object.");
  311. format = p_image->format;
  312. width = p_image->width;
  313. height = p_image->height;
  314. mipmaps = p_image->mipmaps;
  315. data = p_image->data;
  316. }
  317. ~Image();
  318. };
  319. VARIANT_ENUM_CAST(Image::Format)
  320. VARIANT_ENUM_CAST(Image::Interpolation)
  321. VARIANT_ENUM_CAST(Image::CompressMode)
  322. VARIANT_ENUM_CAST(Image::CompressSource)
  323. VARIANT_ENUM_CAST(Image::AlphaMode)
  324. #endif // IMAGE_H