image.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 "color.h"
  33. #include "dvector.h"
  34. #include "math_2d.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, Image &p_img);
  44. class Image {
  45. enum {
  46. MAX_WIDTH = 16384, // force a limit somehow
  47. MAX_HEIGHT = 16384 // force a limit somehow
  48. };
  49. public:
  50. static SavePNGFunc save_png_func;
  51. enum Format {
  52. FORMAT_GRAYSCALE, ///< one byte per pixel, 0-255
  53. FORMAT_INTENSITY, ///< one byte per pixel, 0-255
  54. FORMAT_GRAYSCALE_ALPHA, ///< two bytes per pixel, 0-255. alpha 0-255
  55. FORMAT_RGB, ///< one byte R, one byte G, one byte B
  56. FORMAT_RGBA, ///< one byte R, one byte G, one byte B, one byte A
  57. FORMAT_INDEXED, ///< index byte 0-256, and after image end, 256*3 bytes of palette
  58. FORMAT_INDEXED_ALPHA, ///< index byte 0-256, and after image end, 256*4 bytes of palette (alpha)
  59. FORMAT_YUV_422,
  60. FORMAT_YUV_444,
  61. FORMAT_BC1, // DXT1
  62. FORMAT_BC2, // DXT3
  63. FORMAT_BC3, // DXT5
  64. FORMAT_BC4, // ATI1
  65. FORMAT_BC5, // ATI2
  66. FORMAT_PVRTC2,
  67. FORMAT_PVRTC2_ALPHA,
  68. FORMAT_PVRTC4,
  69. FORMAT_PVRTC4_ALPHA,
  70. FORMAT_ETC, // regular ETC, no transparency
  71. FORMAT_ATC,
  72. FORMAT_ATC_ALPHA_EXPLICIT,
  73. FORMAT_ATC_ALPHA_INTERPOLATED,
  74. /*FORMAT_ETC2_R, for the future..
  75. FORMAT_ETC2_RG,
  76. FORMAT_ETC2_RGB,
  77. FORMAT_ETC2_RGBA1,
  78. FORMAT_ETC2_RGBA,*/
  79. FORMAT_CUSTOM,
  80. FORMAT_MAX
  81. };
  82. static const char *format_names[FORMAT_MAX];
  83. enum Interpolation {
  84. INTERPOLATE_NEAREST,
  85. INTERPOLATE_BILINEAR,
  86. INTERPOLATE_CUBIC,
  87. /* INTERPOLATE GAUSS */
  88. };
  89. static Image (*_png_mem_loader_func)(const uint8_t *p_png, int p_size);
  90. static Image (*_jpg_mem_loader_func)(const uint8_t *p_png, int p_size);
  91. static void (*_image_compress_bc_func)(Image *);
  92. static void (*_image_compress_pvrtc2_func)(Image *);
  93. static void (*_image_compress_pvrtc4_func)(Image *);
  94. static void (*_image_compress_etc_func)(Image *);
  95. static void (*_image_decompress_pvrtc)(Image *);
  96. static void (*_image_decompress_bc)(Image *);
  97. static void (*_image_decompress_etc)(Image *);
  98. Error _decompress_bc();
  99. static DVector<uint8_t> (*lossy_packer)(const Image &p_image, float p_quality);
  100. static Image (*lossy_unpacker)(const DVector<uint8_t> &p_buffer);
  101. static DVector<uint8_t> (*lossless_packer)(const Image &p_image);
  102. static Image (*lossless_unpacker)(const DVector<uint8_t> &p_buffer);
  103. private:
  104. //internal byte based color
  105. struct BColor {
  106. union {
  107. uint8_t col[4];
  108. struct {
  109. uint8_t r, g, b, a;
  110. };
  111. };
  112. bool operator==(const BColor &p_color) const {
  113. for (int i = 0; i < 4; i++) {
  114. if (col[i] != p_color.col[i]) return false;
  115. }
  116. return true;
  117. }
  118. _FORCE_INLINE_ uint8_t gray() const { return (uint16_t(col[0]) + uint16_t(col[1]) + uint16_t(col[2])) / 3; }
  119. _FORCE_INLINE_ BColor() {}
  120. BColor(uint8_t p_r, uint8_t p_g, uint8_t p_b, uint8_t p_a = 255) {
  121. col[0] = p_r;
  122. col[1] = p_g;
  123. col[2] = p_b;
  124. col[3] = p_a;
  125. }
  126. };
  127. //median cut classes
  128. struct BColorPos {
  129. uint32_t index;
  130. BColor color;
  131. struct SortR {
  132. bool operator()(const BColorPos &ca, const BColorPos &cb) const { return ca.color.r < cb.color.r; }
  133. };
  134. struct SortG {
  135. bool operator()(const BColorPos &ca, const BColorPos &cb) const { return ca.color.g < cb.color.g; }
  136. };
  137. struct SortB {
  138. bool operator()(const BColorPos &ca, const BColorPos &cb) const { return ca.color.b < cb.color.b; }
  139. };
  140. struct SortA {
  141. bool operator()(const BColorPos &ca, const BColorPos &cb) const { return ca.color.a < cb.color.a; }
  142. };
  143. };
  144. struct SPTree {
  145. bool leaf;
  146. uint8_t split_plane;
  147. uint8_t split_value;
  148. union {
  149. int left;
  150. int color;
  151. };
  152. int right;
  153. SPTree() {
  154. leaf = true;
  155. left = -1;
  156. right = -1;
  157. }
  158. };
  159. struct MCBlock {
  160. BColorPos min_color, max_color;
  161. BColorPos *colors;
  162. int sp_idx;
  163. int color_count;
  164. int get_longest_axis_index() const;
  165. int get_longest_axis_length() const;
  166. bool operator<(const MCBlock &p_block) const;
  167. void shrink();
  168. MCBlock();
  169. MCBlock(BColorPos *p_colors, int p_color_count);
  170. };
  171. Format format;
  172. DVector<uint8_t> data;
  173. int width, height, mipmaps;
  174. _FORCE_INLINE_ BColor _get_pixel(int p_x, int p_y, const unsigned char *p_data, int p_data_size) const;
  175. _FORCE_INLINE_ BColor _get_pixelw(int p_x, int p_y, int p_width, const unsigned char *p_data, int p_data_size) const;
  176. _FORCE_INLINE_ void _put_pixelw(int p_x, int p_y, int p_width, const BColor &p_color, unsigned char *p_data);
  177. _FORCE_INLINE_ void _put_pixel(int p_x, int p_y, const BColor &p_color, unsigned char *p_data);
  178. _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
  179. _FORCE_INLINE_ static void _get_format_min_data_size(Format p_format, int &r_w, int &r_h);
  180. static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1);
  181. bool _can_modify(Format p_format) const;
  182. public:
  183. int get_width() const; ///< Get image width
  184. int get_height() const; ///< Get image height
  185. int get_mipmaps() const;
  186. /**
  187. * Get a pixel from the image. for grayscale or indexed formats, use Color::gray to obtain the actual
  188. * value.
  189. */
  190. Color get_pixel(int p_x, int p_y, int p_mipmap = 0) const;
  191. /**
  192. * Set a pixel into the image. for grayscale or indexed formats, a suitable Color constructor.
  193. */
  194. void put_pixel(int p_x, int p_y, const Color &p_color, int p_mipmap = 0); /* alpha and index are averaged */
  195. /**
  196. * Convert the image to another format, as close as it can be done.
  197. */
  198. void convert(Format p_new_format);
  199. Image converted(int p_new_format) {
  200. ERR_FAIL_INDEX_V(p_new_format, FORMAT_MAX, Image());
  201. Image ret = *this;
  202. ret.convert((Format)p_new_format);
  203. return ret;
  204. };
  205. /**
  206. * Get the current image format.
  207. */
  208. Format get_format() const;
  209. int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
  210. void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const; //get where the mipmap begins in data
  211. 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
  212. /**
  213. * Resize the image, using the prefered interpolation method.
  214. * Indexed-Color images always use INTERPOLATE_NEAREST.
  215. */
  216. void resize_to_po2(bool p_square = false);
  217. void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR);
  218. Image resized(int p_width, int p_height, int p_interpolation = INTERPOLATE_BILINEAR);
  219. void shrink_x2();
  220. void expand_x2_hq2x();
  221. /**
  222. * Crop the image to a specific size, if larger, then the image is filled by black
  223. */
  224. void crop(int p_width, int p_height);
  225. void flip_x();
  226. void flip_y();
  227. /**
  228. * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1)
  229. */
  230. Error generate_mipmaps(int p_amount = -1, bool p_keep_existing = false);
  231. void clear_mipmaps();
  232. /**
  233. * Generate a normal map from a grayscale image
  234. */
  235. void make_normalmap(float p_height_scale = 1.0);
  236. /**
  237. * Create a new image of a given size and format. Current image will be lost
  238. */
  239. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  240. void create(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t> &p_data);
  241. void create(const char **p_xpm);
  242. /**
  243. * returns true when the image is empty (0,0) in size
  244. */
  245. bool empty() const;
  246. DVector<uint8_t> get_data() const;
  247. Error load(const String &p_path);
  248. Error save_png(const String &p_path) const;
  249. /**
  250. * create an empty image
  251. */
  252. Image();
  253. /**
  254. * create an empty image of a specific size and format
  255. */
  256. Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  257. /**
  258. * import an image of a specific size and format from a pointer
  259. */
  260. Image(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t> &p_data);
  261. enum AlphaMode {
  262. ALPHA_NONE,
  263. ALPHA_BIT,
  264. ALPHA_BLEND
  265. };
  266. AlphaMode detect_alpha() const;
  267. bool is_invisible() const;
  268. void put_indexed_pixel(int p_x, int p_y, uint8_t p_idx, int p_mipmap = 0);
  269. uint8_t get_indexed_pixel(int p_x, int p_y, int p_mipmap = 0) const;
  270. void set_pallete(const DVector<uint8_t> &p_data);
  271. static int get_format_pixel_size(Format p_format);
  272. static int get_format_pixel_rshift(Format p_format);
  273. static int get_format_pallete_size(Format p_format);
  274. static int get_image_data_size(int p_width, int p_height, Format p_format, int p_mipmaps = 0);
  275. static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
  276. bool operator==(const Image &p_image) const;
  277. void quantize();
  278. enum CompressMode {
  279. COMPRESS_BC,
  280. COMPRESS_PVRTC2,
  281. COMPRESS_PVRTC4,
  282. COMPRESS_ETC
  283. };
  284. Error compress(CompressMode p_mode = COMPRESS_BC);
  285. Image compressed(int p_mode); /* from the Image::CompressMode enum */
  286. Error decompress();
  287. Image decompressed() const;
  288. bool is_compressed() const;
  289. void fix_alpha_edges();
  290. void premultiply_alpha();
  291. void srgb_to_linear();
  292. void normalmap_to_xy();
  293. void blit_rect(const Image &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  294. void blit_rect_mask(const Image &p_src, const Image &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  295. void blend_rect(const Image &p_src, const Rect2 &p_src_rect, const Point2 &p_dest);
  296. void blend_rect_mask(const Image &p_src, const Image &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest);
  297. void fill(const Color &p_color);
  298. void brush_transfer(const Image &p_src, const Image &p_brush, const Point2 &p_dest);
  299. Image brushed(const Image &p_src, const Image &p_brush, const Point2 &p_dest) const;
  300. Rect2 get_used_rect() const;
  301. Image get_rect(const Rect2 &p_area) const;
  302. static void set_compress_bc_func(void (*p_compress_func)(Image *));
  303. static String get_format_name(Format p_format);
  304. Image(const uint8_t *p_mem_png_jpg, int p_len = -1);
  305. Image(const char **p_xpm);
  306. ~Image();
  307. };
  308. #endif