image.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*************************************************************************/
  2. /* image.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef IMAGE_H
  30. #define IMAGE_H
  31. #include "dvector.h"
  32. #include "color.h"
  33. #include "math_2d.h"
  34. /**
  35. * @author Juan Linietsky <[email protected]>
  36. *
  37. * Image storage class. This is used to store an image in user memory, as well as
  38. * providing some basic methods for image manipulation.
  39. * Images can be loaded from a file, or registered into the Render object as textures.
  40. */
  41. class Image;
  42. typedef Error (*SavePNGFunc)(const String &p_path, Image& p_img);
  43. class Image {
  44. enum {
  45. MAX_WIDTH=16384, // force a limit somehow
  46. MAX_HEIGHT=16384// force a limit somehow
  47. };
  48. public:
  49. static SavePNGFunc save_png_func;
  50. enum Format {
  51. FORMAT_GRAYSCALE, ///< one byte per pixel, 0-255
  52. FORMAT_INTENSITY, ///< one byte per pixel, 0-255
  53. FORMAT_GRAYSCALE_ALPHA, ///< two bytes per pixel, 0-255. alpha 0-255
  54. FORMAT_RGB, ///< one byte R, one byte G, one byte B
  55. FORMAT_RGBA, ///< one byte R, one byte G, one byte B, one byte A
  56. FORMAT_INDEXED, ///< index byte 0-256, and after image end, 256*3 bytes of palette
  57. FORMAT_INDEXED_ALPHA, ///< index byte 0-256, and after image end, 256*4 bytes of palette (alpha)
  58. FORMAT_YUV_422,
  59. FORMAT_YUV_444,
  60. FORMAT_BC1, // DXT1
  61. FORMAT_BC2, // DXT3
  62. FORMAT_BC3, // DXT5
  63. FORMAT_BC4, // ATI1
  64. FORMAT_BC5, // ATI2
  65. FORMAT_PVRTC2,
  66. FORMAT_PVRTC2_ALPHA,
  67. FORMAT_PVRTC4,
  68. FORMAT_PVRTC4_ALPHA,
  69. FORMAT_ETC, // regular ETC, no transparency
  70. FORMAT_ATC,
  71. FORMAT_ATC_ALPHA_EXPLICIT,
  72. FORMAT_ATC_ALPHA_INTERPOLATED,
  73. /*FORMAT_ETC2_R, for the future..
  74. FORMAT_ETC2_RG,
  75. FORMAT_ETC2_RGB,
  76. FORMAT_ETC2_RGBA1,
  77. FORMAT_ETC2_RGBA,*/
  78. FORMAT_CUSTOM,
  79. FORMAT_MAX
  80. };
  81. enum Interpolation {
  82. INTERPOLATE_NEAREST,
  83. INTERPOLATE_BILINEAR,
  84. /* INTERPOLATE GAUSS */
  85. };
  86. static Image (*_png_mem_loader_func)(const uint8_t* p_png);
  87. static void (*_image_compress_bc_func)(Image *);
  88. static void (*_image_compress_pvrtc2_func)(Image *);
  89. static void (*_image_compress_pvrtc4_func)(Image *);
  90. static void (*_image_compress_etc_func)(Image *);
  91. static void (*_image_decompress_pvrtc)(Image *);
  92. static void (*_image_decompress_bc)(Image *);
  93. static void (*_image_decompress_etc)(Image *);
  94. Error _decompress_bc();
  95. static DVector<uint8_t> (*lossy_packer)(const Image& p_image,float p_quality);
  96. static Image (*lossy_unpacker)(const DVector<uint8_t>& p_buffer);
  97. static DVector<uint8_t> (*lossless_packer)(const Image& p_image);
  98. static Image (*lossless_unpacker)(const DVector<uint8_t>& p_buffer);
  99. private:
  100. //internal byte based color
  101. struct BColor {
  102. union {
  103. uint8_t col[4];
  104. struct {
  105. uint8_t r,g,b,a;
  106. };
  107. };
  108. bool operator==(const BColor& p_color) const { for(int i=0;i<4;i++) {if (col[i]!=p_color.col[i]) return false; } return true; }
  109. _FORCE_INLINE_ uint8_t gray() const { return (uint16_t(col[0])+uint16_t(col[1])+uint16_t(col[2]))/3; }
  110. _FORCE_INLINE_ BColor() {}
  111. BColor(uint8_t p_r,uint8_t p_g,uint8_t p_b,uint8_t p_a=255) { col[0]=p_r; col[1]=p_g; col[2]=p_b; col[3]=p_a; }
  112. };
  113. //median cut classes
  114. struct BColorPos {
  115. uint32_t index;
  116. BColor color;
  117. struct SortR {
  118. bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.r < cb.color.r; }
  119. };
  120. struct SortG {
  121. bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.g < cb.color.g; }
  122. };
  123. struct SortB {
  124. bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.b < cb.color.b; }
  125. };
  126. struct SortA {
  127. bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.a < cb.color.a; }
  128. };
  129. };
  130. struct SPTree {
  131. bool leaf;
  132. uint8_t split_plane;
  133. uint8_t split_value;
  134. union {
  135. int left;
  136. int color;
  137. };
  138. int right;
  139. SPTree() { leaf=true; left=-1; right=-1;}
  140. };
  141. struct MCBlock {
  142. BColorPos min_color,max_color;
  143. BColorPos *colors;
  144. int sp_idx;
  145. int color_count;
  146. int get_longest_axis_index() const;
  147. int get_longest_axis_length() const;
  148. bool operator<(const MCBlock& p_block) const;
  149. void shrink();
  150. MCBlock();
  151. MCBlock(BColorPos *p_colors,int p_color_count);
  152. };
  153. Format format;
  154. DVector<uint8_t> data;
  155. int width,height,mipmaps;
  156. _FORCE_INLINE_ BColor _get_pixel(int p_x,int p_y,const unsigned char *p_data,int p_data_size) const;
  157. _FORCE_INLINE_ BColor _get_pixelw(int p_x,int p_y,int p_width,const unsigned char *p_data,int p_data_size) const;
  158. _FORCE_INLINE_ void _put_pixelw(int p_x,int p_y, int p_width, const BColor& p_color, unsigned char *p_data);
  159. _FORCE_INLINE_ void _put_pixel(int p_x,int p_y, const BColor& p_color, unsigned char *p_data);
  160. _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
  161. _FORCE_INLINE_ static void _get_format_min_data_size(Format p_format,int &r_w, int &r_h);
  162. static int _get_dst_image_size(int p_width, int p_height, Format p_format,int &r_mipmaps,int p_mipmaps=-1);
  163. bool _can_modify(Format p_format) const;
  164. public:
  165. int get_width() const; ///< Get image width
  166. int get_height() const; ///< Get image height
  167. int get_mipmaps() const;
  168. /**
  169. * Get a pixel from the image. for grayscale or indexed formats, use Color::gray to obtain the actual
  170. * value.
  171. */
  172. Color get_pixel(int p_x,int p_y,int p_mipmap=0) const;
  173. /**
  174. * Set a pixel into the image. for grayscale or indexed formats, a suitable Color constructor.
  175. */
  176. void put_pixel(int p_x,int p_y, const Color& p_color,int p_mipmap=0); /* alpha and index are averaged */
  177. /**
  178. * Convert the image to another format, as close as it can be done.
  179. */
  180. void convert( Format p_new_format );
  181. Image converted(int p_new_format) {
  182. ERR_FAIL_INDEX_V(p_new_format, FORMAT_MAX, Image());
  183. Image ret = *this;
  184. ret.convert((Format)p_new_format);
  185. return ret;
  186. };
  187. /**
  188. * Get the current image format.
  189. */
  190. Format get_format() const;
  191. int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
  192. void get_mipmap_offset_and_size(int p_mipmap,int &r_ofs, int &r_size) const; //get where the mipmap begins in data
  193. 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
  194. /**
  195. * Resize the image, using the prefered interpolation method.
  196. * Indexed-Color images always use INTERPOLATE_NEAREST.
  197. */
  198. void resize_to_po2(bool p_square=false);
  199. void resize( int p_width, int p_height, Interpolation p_interpolation=INTERPOLATE_BILINEAR );
  200. Image resized( int p_width, int p_height, int p_interpolation=INTERPOLATE_BILINEAR );
  201. /**
  202. * Crop the image to a specific size, if larger, then the image is filled by black
  203. */
  204. void crop( int p_width, int p_height );
  205. void flip_x();
  206. void flip_y();
  207. /**
  208. * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1)
  209. */
  210. Error generate_mipmaps(int p_amount=-1,bool p_keep_existing=false);
  211. void clear_mipmaps();
  212. /**
  213. * Generate a normal map from a grayscale image
  214. */
  215. void make_normalmap(float p_height_scale=1.0);
  216. /**
  217. * Create a new image of a given size and format. Current image will be lost
  218. */
  219. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  220. void create(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data);
  221. void create( const char ** p_xpm );
  222. /**
  223. * returns true when the image is empty (0,0) in size
  224. */
  225. bool empty() const;
  226. DVector<uint8_t> get_data() const;
  227. Error load(const String& p_path);
  228. Error save_png(const String& p_path);
  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, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data);
  241. enum AlphaMode {
  242. ALPHA_NONE,
  243. ALPHA_BIT,
  244. ALPHA_BLEND
  245. };
  246. AlphaMode detect_alpha() const;
  247. void put_indexed_pixel(int p_x, int p_y, uint8_t p_idx,int p_mipmap=0);
  248. uint8_t get_indexed_pixel(int p_x, int p_y,int p_mipmap=0) const;
  249. void set_pallete(const DVector<uint8_t>& p_data);
  250. static int get_format_pixel_size(Format p_format);
  251. static int get_format_pixel_rshift(Format p_format);
  252. static int get_format_pallete_size(Format p_format);
  253. static int get_image_data_size(int p_width, int p_height, Format p_format,int p_mipmaps=0);
  254. static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
  255. bool operator==(const Image& p_image) const;
  256. void quantize();
  257. enum CompressMode {
  258. COMPRESS_BC,
  259. COMPRESS_PVRTC2,
  260. COMPRESS_PVRTC4,
  261. COMPRESS_ETC
  262. };
  263. Error compress(CompressMode p_mode=COMPRESS_BC);
  264. Image compressed(int p_mode); /* from the Image::CompressMode enum */
  265. Error decompress();
  266. Image decompressed() const;
  267. void fix_alpha_edges();
  268. void premultiply_alpha();
  269. void srgb_to_linear();
  270. void normalmap_to_xy();
  271. void blit_rect(const Image& p_src, const Rect2& p_src_rect,const Point2& p_dest);
  272. void brush_transfer(const Image& p_src, const Image& p_brush, const Point2& p_dest);
  273. Image brushed(const Image& p_src, const Image& p_brush, const Point2& p_dest) const;
  274. Rect2 get_used_rect() const;
  275. Image get_rect(const Rect2& p_area) const;
  276. static void set_compress_bc_func(void (*p_compress_func)(Image *));
  277. Image(const uint8_t* p_mem_png);
  278. Image(const char **p_xpm);
  279. ~Image();
  280. };
  281. #endif