image.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*************************************************************************/
  2. /* image.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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. static const char* format_names[FORMAT_MAX];
  82. enum Interpolation {
  83. INTERPOLATE_NEAREST,
  84. INTERPOLATE_BILINEAR,
  85. INTERPOLATE_CUBIC,
  86. /* INTERPOLATE GAUSS */
  87. };
  88. static Image (*_png_mem_loader_func)(const uint8_t* p_png,int p_size);
  89. static Image (*_jpg_mem_loader_func)(const uint8_t* p_png,int p_size);
  90. static void (*_image_compress_bc_func)(Image *);
  91. static void (*_image_compress_pvrtc2_func)(Image *);
  92. static void (*_image_compress_pvrtc4_func)(Image *);
  93. static void (*_image_compress_etc_func)(Image *);
  94. static void (*_image_decompress_pvrtc)(Image *);
  95. static void (*_image_decompress_bc)(Image *);
  96. static void (*_image_decompress_etc)(Image *);
  97. Error _decompress_bc();
  98. static DVector<uint8_t> (*lossy_packer)(const Image& p_image,float p_quality);
  99. static Image (*lossy_unpacker)(const DVector<uint8_t>& p_buffer);
  100. static DVector<uint8_t> (*lossless_packer)(const Image& p_image);
  101. static Image (*lossless_unpacker)(const DVector<uint8_t>& p_buffer);
  102. private:
  103. //internal byte based color
  104. struct BColor {
  105. union {
  106. uint8_t col[4];
  107. struct {
  108. uint8_t r,g,b,a;
  109. };
  110. };
  111. 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; }
  112. _FORCE_INLINE_ uint8_t gray() const { return (uint16_t(col[0])+uint16_t(col[1])+uint16_t(col[2]))/3; }
  113. _FORCE_INLINE_ BColor() {}
  114. 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; }
  115. };
  116. //median cut classes
  117. struct BColorPos {
  118. uint32_t index;
  119. BColor color;
  120. struct SortR {
  121. bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.r < cb.color.r; }
  122. };
  123. struct SortG {
  124. bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.g < cb.color.g; }
  125. };
  126. struct SortB {
  127. bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.b < cb.color.b; }
  128. };
  129. struct SortA {
  130. bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.a < cb.color.a; }
  131. };
  132. };
  133. struct SPTree {
  134. bool leaf;
  135. uint8_t split_plane;
  136. uint8_t split_value;
  137. union {
  138. int left;
  139. int color;
  140. };
  141. int right;
  142. SPTree() { leaf=true; left=-1; right=-1;}
  143. };
  144. struct MCBlock {
  145. BColorPos min_color,max_color;
  146. BColorPos *colors;
  147. int sp_idx;
  148. int color_count;
  149. int get_longest_axis_index() const;
  150. int get_longest_axis_length() const;
  151. bool operator<(const MCBlock& p_block) const;
  152. void shrink();
  153. MCBlock();
  154. MCBlock(BColorPos *p_colors,int p_color_count);
  155. };
  156. Format format;
  157. DVector<uint8_t> data;
  158. int width,height,mipmaps;
  159. _FORCE_INLINE_ BColor _get_pixel(int p_x,int p_y,const unsigned char *p_data,int p_data_size) const;
  160. _FORCE_INLINE_ BColor _get_pixelw(int p_x,int p_y,int p_width,const unsigned char *p_data,int p_data_size) const;
  161. _FORCE_INLINE_ void _put_pixelw(int p_x,int p_y, int p_width, const BColor& p_color, unsigned char *p_data);
  162. _FORCE_INLINE_ void _put_pixel(int p_x,int p_y, const BColor& p_color, unsigned char *p_data);
  163. _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
  164. _FORCE_INLINE_ static void _get_format_min_data_size(Format p_format,int &r_w, int &r_h);
  165. static int _get_dst_image_size(int p_width, int p_height, Format p_format,int &r_mipmaps,int p_mipmaps=-1);
  166. bool _can_modify(Format p_format) const;
  167. public:
  168. int get_width() const; ///< Get image width
  169. int get_height() const; ///< Get image height
  170. int get_mipmaps() const;
  171. /**
  172. * Get a pixel from the image. for grayscale or indexed formats, use Color::gray to obtain the actual
  173. * value.
  174. */
  175. Color get_pixel(int p_x,int p_y,int p_mipmap=0) const;
  176. /**
  177. * Set a pixel into the image. for grayscale or indexed formats, a suitable Color constructor.
  178. */
  179. void put_pixel(int p_x,int p_y, const Color& p_color,int p_mipmap=0); /* alpha and index are averaged */
  180. /**
  181. * Convert the image to another format, as close as it can be done.
  182. */
  183. void convert( Format p_new_format );
  184. Image converted(int p_new_format) {
  185. ERR_FAIL_INDEX_V(p_new_format, FORMAT_MAX, Image());
  186. Image ret = *this;
  187. ret.convert((Format)p_new_format);
  188. return ret;
  189. };
  190. /**
  191. * Get the current image format.
  192. */
  193. Format get_format() const;
  194. int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
  195. void get_mipmap_offset_and_size(int p_mipmap,int &r_ofs, int &r_size) const; //get where the mipmap begins in data
  196. 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
  197. /**
  198. * Resize the image, using the prefered interpolation method.
  199. * Indexed-Color images always use INTERPOLATE_NEAREST.
  200. */
  201. void resize_to_po2(bool p_square=false);
  202. void resize( int p_width, int p_height, Interpolation p_interpolation=INTERPOLATE_BILINEAR );
  203. Image resized( int p_width, int p_height, int p_interpolation=INTERPOLATE_BILINEAR );
  204. void shrink_x2();
  205. void expand_x2_hq2x();
  206. /**
  207. * Crop the image to a specific size, if larger, then the image is filled by black
  208. */
  209. void crop( int p_width, int p_height );
  210. void flip_x();
  211. void flip_y();
  212. /**
  213. * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1)
  214. */
  215. Error generate_mipmaps(int p_amount=-1,bool p_keep_existing=false);
  216. void clear_mipmaps();
  217. /**
  218. * Generate a normal map from a grayscale image
  219. */
  220. void make_normalmap(float p_height_scale=1.0);
  221. /**
  222. * Create a new image of a given size and format. Current image will be lost
  223. */
  224. void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  225. void create(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data);
  226. void create( const char ** p_xpm );
  227. /**
  228. * returns true when the image is empty (0,0) in size
  229. */
  230. bool empty() const;
  231. DVector<uint8_t> get_data() const;
  232. Error load(const String& p_path);
  233. Error save_png(const String& p_path);
  234. /**
  235. * create an empty image
  236. */
  237. Image();
  238. /**
  239. * create an empty image of a specific size and format
  240. */
  241. Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
  242. /**
  243. * import an image of a specific size and format from a pointer
  244. */
  245. Image(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data);
  246. enum AlphaMode {
  247. ALPHA_NONE,
  248. ALPHA_BIT,
  249. ALPHA_BLEND
  250. };
  251. AlphaMode detect_alpha() const;
  252. bool is_invisible() const;
  253. void put_indexed_pixel(int p_x, int p_y, uint8_t p_idx,int p_mipmap=0);
  254. uint8_t get_indexed_pixel(int p_x, int p_y,int p_mipmap=0) const;
  255. void set_pallete(const DVector<uint8_t>& p_data);
  256. static int get_format_pixel_size(Format p_format);
  257. static int get_format_pixel_rshift(Format p_format);
  258. static int get_format_pallete_size(Format p_format);
  259. static int get_image_data_size(int p_width, int p_height, Format p_format,int p_mipmaps=0);
  260. static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
  261. bool operator==(const Image& p_image) const;
  262. void quantize();
  263. enum CompressMode {
  264. COMPRESS_BC,
  265. COMPRESS_PVRTC2,
  266. COMPRESS_PVRTC4,
  267. COMPRESS_ETC
  268. };
  269. Error compress(CompressMode p_mode=COMPRESS_BC);
  270. Image compressed(int p_mode); /* from the Image::CompressMode enum */
  271. Error decompress();
  272. Image decompressed() const;
  273. bool is_compressed() const;
  274. void fix_alpha_edges();
  275. void premultiply_alpha();
  276. void srgb_to_linear();
  277. void normalmap_to_xy();
  278. void blit_rect(const Image& p_src, const Rect2& p_src_rect,const Point2& p_dest);
  279. void brush_transfer(const Image& p_src, const Image& p_brush, const Point2& p_dest);
  280. Image brushed(const Image& p_src, const Image& p_brush, const Point2& p_dest) const;
  281. Rect2 get_used_rect() const;
  282. Image get_rect(const Rect2& p_area) const;
  283. static void set_compress_bc_func(void (*p_compress_func)(Image *));
  284. static String get_format_name(Format p_format);
  285. Image(const uint8_t* p_mem_png_jpg, int p_len=-1);
  286. Image(const char **p_xpm);
  287. ~Image();
  288. };
  289. #endif