image.h 11 KB

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