image.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/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. };
  114. //some functions provided by something else
  115. static ImageMemLoadFunc _png_mem_loader_func;
  116. static ImageMemLoadFunc _jpg_mem_loader_func;
  117. static ImageMemLoadFunc _webp_mem_loader_func;
  118. static ImageMemLoadFunc _tga_mem_loader_func;
  119. static void (*_image_compress_bc_func)(Image *, float, CompressSource p_source);
  120. static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, CompressSource p_source);
  121. static void (*_image_compress_pvrtc2_func)(Image *);
  122. static void (*_image_compress_pvrtc4_func)(Image *);
  123. static void (*_image_compress_etc1_func)(Image *, float);
  124. static void (*_image_compress_etc2_func)(Image *, float, CompressSource p_source);
  125. static void (*_image_decompress_pvrtc)(Image *);
  126. static void (*_image_decompress_bc)(Image *);
  127. static void (*_image_decompress_bptc)(Image *);
  128. static void (*_image_decompress_etc1)(Image *);
  129. static void (*_image_decompress_etc2)(Image *);
  130. static PoolVector<uint8_t> (*lossy_packer)(const Ref<Image> &p_image, float p_quality);
  131. static Ref<Image> (*lossy_unpacker)(const PoolVector<uint8_t> &p_buffer);
  132. static PoolVector<uint8_t> (*lossless_packer)(const Ref<Image> &p_image);
  133. static Ref<Image> (*lossless_unpacker)(const PoolVector<uint8_t> &p_buffer);
  134. PoolVector<uint8_t>::Write write_lock;
  135. protected:
  136. static void _bind_methods();
  137. private:
  138. void _create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  139. create(p_width, p_height, p_use_mipmaps, p_format);
  140. }
  141. void _create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  142. create(p_width, p_height, p_use_mipmaps, p_format, p_data);
  143. }
  144. Format format;
  145. PoolVector<uint8_t> data;
  146. int width, height;
  147. bool mipmaps;
  148. void _copy_internals_from(const Image &p_image) {
  149. format = p_image.format;
  150. width = p_image.width;
  151. height = p_image.height;
  152. mipmaps = p_image.mipmaps;
  153. data = p_image.data;
  154. }
  155. _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
  156. static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1);
  157. bool _can_modify(Format p_format) const;
  158. _FORCE_INLINE_ void _put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel);
  159. _FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel);
  160. void _set_data(const Dictionary &p_data);
  161. Dictionary _get_data() const;
  162. Error _load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader);
  163. 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);
  164. static void average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d);
  165. 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);
  166. 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);
  167. static void renormalize_uint8(uint8_t *p_rgb);
  168. static void renormalize_float(float *p_rgb);
  169. static void renormalize_half(uint16_t *p_rgb);
  170. static void renormalize_rgbe9995(uint32_t *p_rgb);
  171. public:
  172. int get_width() const; ///< Get image width
  173. int get_height() const; ///< Get image height
  174. Vector2 get_size() const;
  175. bool has_mipmaps() const;
  176. int get_mipmap_count() const;
  177. /**
  178. * Convert the image to another format, conversion only to raw byte format
  179. */
  180. void convert(Format p_new_format);
  181. /**
  182. * Get the current image format.
  183. */
  184. Format get_format() const;
  185. int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
  186. void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const; //get where the mipmap begins in data
  187. 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
  188. /**
  189. * Resize the image, using the preferred interpolation method.
  190. */
  191. void resize_to_po2(bool p_square = false);
  192. void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
  193. void shrink_x2();
  194. void expand_x2_hq2x();
  195. bool is_size_po2() const;
  196. /**
  197. * Crop the image to a specific size, if larger, then the image is filled by black
  198. */
  199. void crop_from_point(int p_x, int p_y, int p_width, int p_height);
  200. void crop(int p_width, int p_height);
  201. void flip_x();
  202. void flip_y();
  203. /**
  204. * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1)
  205. */
  206. Error generate_mipmaps(bool p_renormalize = false);
  207. void clear_mipmaps();
  208. void normalize(); //for normal maps
  209. /**
  210. * Create a new image of a given size and format. Current image will be lost
  211. */
  212. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  213. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
  214. void create(const char **p_xpm);
  215. /**
  216. * returns true when the image is empty (0,0) in size
  217. */
  218. bool empty() const;
  219. PoolVector<uint8_t> get_data() const;
  220. Error load(const String &p_path);
  221. Error save_png(const String &p_path) const;
  222. PoolVector<uint8_t> save_png_to_buffer() const;
  223. Error save_exr(const String &p_path, bool p_grayscale) const;
  224. /**
  225. * create an empty image
  226. */
  227. Image();
  228. /**
  229. * create an empty image of a specific size and format
  230. */
  231. Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  232. /**
  233. * import an image of a specific size and format from a pointer
  234. */
  235. Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
  236. enum AlphaMode {
  237. ALPHA_NONE,
  238. ALPHA_BIT,
  239. ALPHA_BLEND
  240. };
  241. AlphaMode detect_alpha() const;
  242. bool is_invisible() const;
  243. static int get_format_pixel_size(Format p_format);
  244. static int get_format_pixel_rshift(Format p_format);
  245. static int get_format_block_size(Format p_format);
  246. static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h);
  247. static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false);
  248. static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
  249. static int get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap);
  250. enum CompressMode {
  251. COMPRESS_S3TC,
  252. COMPRESS_PVRTC2,
  253. COMPRESS_PVRTC4,
  254. COMPRESS_ETC,
  255. COMPRESS_ETC2,
  256. COMPRESS_BPTC
  257. };
  258. Error compress(CompressMode p_mode = COMPRESS_S3TC, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7);
  259. Error decompress();
  260. bool is_compressed() const;
  261. void fix_alpha_edges();
  262. void premultiply_alpha();
  263. void srgb_to_linear();
  264. void normalmap_to_xy();
  265. Ref<Image> rgbe_to_srgb();
  266. void bumpmap_to_normalmap(float bump_scale = 1.0);
  267. void blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  268. void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  269. void blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  270. void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  271. void fill(const Color &c);
  272. Rect2 get_used_rect() const;
  273. Ref<Image> get_rect(const Rect2 &p_area) const;
  274. static void set_compress_bc_func(void (*p_compress_func)(Image *, float, CompressSource));
  275. static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, CompressSource));
  276. static String get_format_name(Format p_format);
  277. Error load_png_from_buffer(const PoolVector<uint8_t> &p_array);
  278. Error load_jpg_from_buffer(const PoolVector<uint8_t> &p_array);
  279. Error load_webp_from_buffer(const PoolVector<uint8_t> &p_array);
  280. Error load_tga_from_buffer(const PoolVector<uint8_t> &p_array);
  281. Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
  282. Image(const char **p_xpm);
  283. virtual Ref<Resource> duplicate(bool p_subresources = false) const;
  284. void lock();
  285. void unlock();
  286. //this is used for compression
  287. enum DetectChannels {
  288. DETECTED_L,
  289. DETECTED_LA,
  290. DETECTED_R,
  291. DETECTED_RG,
  292. DETECTED_RGB,
  293. DETECTED_RGBA,
  294. };
  295. DetectChannels get_detected_channels();
  296. void optimize_channels();
  297. Color get_pixelv(const Point2 &p_src) const;
  298. Color get_pixel(int p_x, int p_y) const;
  299. void set_pixelv(const Point2 &p_dst, const Color &p_color);
  300. void set_pixel(int p_x, int p_y, const Color &p_color);
  301. void copy_internals_from(const Ref<Image> &p_image) {
  302. ERR_FAIL_COND_MSG(p_image.is_null(), "It's not a reference to a valid Image object.");
  303. format = p_image->format;
  304. width = p_image->width;
  305. height = p_image->height;
  306. mipmaps = p_image->mipmaps;
  307. data = p_image->data;
  308. }
  309. ~Image();
  310. };
  311. VARIANT_ENUM_CAST(Image::Format)
  312. VARIANT_ENUM_CAST(Image::Interpolation)
  313. VARIANT_ENUM_CAST(Image::CompressMode)
  314. VARIANT_ENUM_CAST(Image::CompressSource)
  315. VARIANT_ENUM_CAST(Image::AlphaMode)
  316. #endif