image.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*************************************************************************/
  2. /* image.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/dvector.h"
  34. #include "core/math/rect2.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 Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size);
  46. class Image : public Resource {
  47. GDCLASS(Image, Resource);
  48. enum {
  49. MAX_WIDTH = 16384, // force a limit somehow
  50. MAX_HEIGHT = 16384 // force a limit somehow
  51. };
  52. public:
  53. static SavePNGFunc save_png_func;
  54. enum Format {
  55. FORMAT_L8, //luminance
  56. FORMAT_LA8, //luminance-alpha
  57. FORMAT_R8,
  58. FORMAT_RG8,
  59. FORMAT_RGB8,
  60. FORMAT_RGBA8,
  61. FORMAT_RGBA4444,
  62. FORMAT_RGBA5551,
  63. FORMAT_RF, //float
  64. FORMAT_RGF,
  65. FORMAT_RGBF,
  66. FORMAT_RGBAF,
  67. FORMAT_RH, //half float
  68. FORMAT_RGH,
  69. FORMAT_RGBH,
  70. FORMAT_RGBAH,
  71. FORMAT_RGBE9995,
  72. FORMAT_DXT1, //s3tc bc1
  73. FORMAT_DXT3, //bc2
  74. FORMAT_DXT5, //bc3
  75. FORMAT_RGTC_R,
  76. FORMAT_RGTC_RG,
  77. FORMAT_BPTC_RGBA, //btpc bc7
  78. FORMAT_BPTC_RGBF, //float bc6h
  79. FORMAT_BPTC_RGBFU, //unsigned float bc6hu
  80. FORMAT_PVRTC2, //pvrtc
  81. FORMAT_PVRTC2A,
  82. FORMAT_PVRTC4,
  83. FORMAT_PVRTC4A,
  84. FORMAT_ETC, //etc1
  85. FORMAT_ETC2_R11, //etc2
  86. FORMAT_ETC2_R11S, //signed, NOT srgb.
  87. FORMAT_ETC2_RG11,
  88. FORMAT_ETC2_RG11S,
  89. FORMAT_ETC2_RGB8,
  90. FORMAT_ETC2_RGBA8,
  91. FORMAT_ETC2_RGB8A1,
  92. FORMAT_MAX
  93. };
  94. static const char *format_names[FORMAT_MAX];
  95. enum Interpolation {
  96. INTERPOLATE_NEAREST,
  97. INTERPOLATE_BILINEAR,
  98. INTERPOLATE_CUBIC,
  99. INTERPOLATE_TRILINEAR,
  100. /* INTERPOLATE_TRICUBIC, */
  101. /* INTERPOLATE GAUSS */
  102. };
  103. enum CompressSource {
  104. COMPRESS_SOURCE_GENERIC,
  105. COMPRESS_SOURCE_SRGB,
  106. COMPRESS_SOURCE_NORMAL,
  107. COMPRESS_SOURCE_LAYERED,
  108. };
  109. //some functions provided by something else
  110. static ImageMemLoadFunc _png_mem_loader_func;
  111. static ImageMemLoadFunc _jpg_mem_loader_func;
  112. static ImageMemLoadFunc _webp_mem_loader_func;
  113. static void (*_image_compress_bc_func)(Image *, float, CompressSource p_source);
  114. static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, CompressSource p_source);
  115. static void (*_image_compress_pvrtc2_func)(Image *);
  116. static void (*_image_compress_pvrtc4_func)(Image *);
  117. static void (*_image_compress_etc1_func)(Image *, float);
  118. static void (*_image_compress_etc2_func)(Image *, float, CompressSource p_source);
  119. static void (*_image_decompress_pvrtc)(Image *);
  120. static void (*_image_decompress_bc)(Image *);
  121. static void (*_image_decompress_bptc)(Image *);
  122. static void (*_image_decompress_etc1)(Image *);
  123. static void (*_image_decompress_etc2)(Image *);
  124. static PoolVector<uint8_t> (*lossy_packer)(const Ref<Image> &p_image, float p_quality);
  125. static Ref<Image> (*lossy_unpacker)(const PoolVector<uint8_t> &p_buffer);
  126. static PoolVector<uint8_t> (*lossless_packer)(const Ref<Image> &p_image);
  127. static Ref<Image> (*lossless_unpacker)(const PoolVector<uint8_t> &p_buffer);
  128. PoolVector<uint8_t>::Write write_lock;
  129. protected:
  130. static void _bind_methods();
  131. private:
  132. void _create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) {
  133. create(p_width, p_height, p_use_mipmaps, p_format);
  134. }
  135. void _create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data) {
  136. create(p_width, p_height, p_use_mipmaps, p_format, p_data);
  137. }
  138. Format format;
  139. PoolVector<uint8_t> data;
  140. int width, height;
  141. bool mipmaps;
  142. void _copy_internals_from(const Image &p_image) {
  143. format = p_image.format;
  144. width = p_image.width;
  145. height = p_image.height;
  146. mipmaps = p_image.mipmaps;
  147. data = p_image.data;
  148. }
  149. _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
  150. static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1);
  151. bool _can_modify(Format p_format) const;
  152. _FORCE_INLINE_ void _put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel);
  153. _FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel);
  154. void _set_data(const Dictionary &p_data);
  155. Dictionary _get_data() const;
  156. Error _load_from_buffer(const PoolVector<uint8_t> &p_array, ImageMemLoadFunc p_loader);
  157. 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);
  158. static void average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d);
  159. 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);
  160. 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);
  161. static void renormalize_uint8(uint8_t *p_rgb);
  162. static void renormalize_float(float *p_rgb);
  163. static void renormalize_half(uint16_t *p_rgb);
  164. static void renormalize_rgbe9995(uint32_t *p_rgb);
  165. public:
  166. int get_width() const; ///< Get image width
  167. int get_height() const; ///< Get image height
  168. Vector2 get_size() const;
  169. bool has_mipmaps() const;
  170. int get_mipmap_count() const;
  171. /**
  172. * Convert the image to another format, conversion only to raw byte format
  173. */
  174. void convert(Format p_new_format);
  175. /**
  176. * Get the current image format.
  177. */
  178. Format get_format() const;
  179. int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
  180. void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const; //get where the mipmap begins in data
  181. 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
  182. /**
  183. * Resize the image, using the preferred interpolation method.
  184. * Indexed-Color images always use INTERPOLATE_NEAREST.
  185. */
  186. void resize_to_po2(bool p_square = false);
  187. void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
  188. void shrink_x2();
  189. void expand_x2_hq2x();
  190. /**
  191. * Crop the image to a specific size, if larger, then the image is filled by black
  192. */
  193. void crop_from_point(int p_x, int p_y, int p_width, int p_height);
  194. void crop(int p_width, int p_height);
  195. void flip_x();
  196. void flip_y();
  197. /**
  198. * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1)
  199. */
  200. Error generate_mipmaps(bool p_renormalize = false);
  201. void clear_mipmaps();
  202. void normalize(); //for normal maps
  203. /**
  204. * Create a new image of a given size and format. Current image will be lost
  205. */
  206. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  207. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
  208. void create(const char **p_xpm);
  209. /**
  210. * returns true when the image is empty (0,0) in size
  211. */
  212. bool empty() const;
  213. PoolVector<uint8_t> get_data() const;
  214. Error load(const String &p_path);
  215. Error save_png(const String &p_path) const;
  216. /**
  217. * create an empty image
  218. */
  219. Image();
  220. /**
  221. * create an empty image of a specific size and format
  222. */
  223. Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  224. /**
  225. * import an image of a specific size and format from a pointer
  226. */
  227. Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const PoolVector<uint8_t> &p_data);
  228. enum AlphaMode {
  229. ALPHA_NONE,
  230. ALPHA_BIT,
  231. ALPHA_BLEND
  232. };
  233. AlphaMode detect_alpha() const;
  234. bool is_invisible() const;
  235. static int get_format_pixel_size(Format p_format);
  236. static int get_format_pixel_rshift(Format p_format);
  237. static int get_format_block_size(Format p_format);
  238. static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h);
  239. static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false);
  240. static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
  241. static int get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap);
  242. enum CompressMode {
  243. COMPRESS_S3TC,
  244. COMPRESS_PVRTC2,
  245. COMPRESS_PVRTC4,
  246. COMPRESS_ETC,
  247. COMPRESS_ETC2,
  248. COMPRESS_BPTC
  249. };
  250. Error compress(CompressMode p_mode = COMPRESS_S3TC, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7);
  251. Error decompress();
  252. bool is_compressed() const;
  253. void fix_alpha_edges();
  254. void premultiply_alpha();
  255. void srgb_to_linear();
  256. void normalmap_to_xy();
  257. Ref<Image> rgbe_to_srgb();
  258. void bumpmap_to_normalmap(float bump_scale = 1.0);
  259. void blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  260. void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  261. void blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  262. void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  263. void fill(const Color &c);
  264. Rect2 get_used_rect() const;
  265. Ref<Image> get_rect(const Rect2 &p_area) const;
  266. static void set_compress_bc_func(void (*p_compress_func)(Image *, float, CompressSource));
  267. static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, CompressSource));
  268. static String get_format_name(Format p_format);
  269. Error load_png_from_buffer(const PoolVector<uint8_t> &p_array);
  270. Error load_jpg_from_buffer(const PoolVector<uint8_t> &p_array);
  271. Error load_webp_from_buffer(const PoolVector<uint8_t> &p_array);
  272. Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
  273. Image(const char **p_xpm);
  274. virtual Ref<Resource> duplicate(bool p_subresources = false) const;
  275. void lock();
  276. void unlock();
  277. //this is used for compression
  278. enum DetectChannels {
  279. DETECTED_L,
  280. DETECTED_LA,
  281. DETECTED_R,
  282. DETECTED_RG,
  283. DETECTED_RGB,
  284. DETECTED_RGBA,
  285. };
  286. DetectChannels get_detected_channels();
  287. void optimize_channels();
  288. Color get_pixelv(const Point2 &p_src) const;
  289. Color get_pixel(int p_x, int p_y) const;
  290. void set_pixelv(const Point2 &p_dest, const Color &p_color);
  291. void set_pixel(int p_x, int p_y, const Color &p_color);
  292. void copy_internals_from(const Ref<Image> &p_image) {
  293. ERR_FAIL_COND(p_image.is_null());
  294. format = p_image->format;
  295. width = p_image->width;
  296. height = p_image->height;
  297. mipmaps = p_image->mipmaps;
  298. data = p_image->data;
  299. }
  300. ~Image();
  301. };
  302. VARIANT_ENUM_CAST(Image::Format)
  303. VARIANT_ENUM_CAST(Image::Interpolation)
  304. VARIANT_ENUM_CAST(Image::CompressMode)
  305. VARIANT_ENUM_CAST(Image::CompressSource)
  306. VARIANT_ENUM_CAST(Image::AlphaMode)
  307. #endif