image.h
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 
32 #include "dvector.h"
33 #include "color.h"
34 #include "math_2d.h"
43 class Image;
44 
45 typedef Error (*SavePNGFunc)(const String &p_path, Image& p_img);
46 
47 class Image {
48 
49  enum {
50  MAX_WIDTH=16384, // force a limit somehow
51  MAX_HEIGHT=16384// force a limit somehow
52  };
53 public:
54 
55  static SavePNGFunc save_png_func;
56 
57  enum Format {
65  FORMAT_YUV_422,
66  FORMAT_YUV_444,
67  FORMAT_BC1, // DXT1
68  FORMAT_BC2, // DXT3
69  FORMAT_BC3, // DXT5
70  FORMAT_BC4, // ATI1
71  FORMAT_BC5, // ATI2
72  FORMAT_PVRTC2,
73  FORMAT_PVRTC2_ALPHA,
74  FORMAT_PVRTC4,
75  FORMAT_PVRTC4_ALPHA,
76  FORMAT_ETC, // regular ETC, no transparency
77  FORMAT_ATC,
78  FORMAT_ATC_ALPHA_EXPLICIT,
79  FORMAT_ATC_ALPHA_INTERPOLATED,
80  /*FORMAT_ETC2_R, for the future..
81  FORMAT_ETC2_RG,
82  FORMAT_ETC2_RGB,
83  FORMAT_ETC2_RGBA1,
84  FORMAT_ETC2_RGBA,*/
85  FORMAT_CUSTOM,
86 
87  FORMAT_MAX
88  };
89 
90  static const char* format_names[FORMAT_MAX];
91  enum Interpolation {
92 
93  INTERPOLATE_NEAREST,
94  INTERPOLATE_BILINEAR,
95  INTERPOLATE_CUBIC,
96  /* INTERPOLATE GAUSS */
97  };
98 
99  static Image (*_png_mem_loader_func)(const uint8_t* p_png,int p_size);
100  static void (*_image_compress_bc_func)(Image *);
101  static void (*_image_compress_pvrtc2_func)(Image *);
102  static void (*_image_compress_pvrtc4_func)(Image *);
103  static void (*_image_compress_etc_func)(Image *);
104  static void (*_image_decompress_pvrtc)(Image *);
105  static void (*_image_decompress_bc)(Image *);
106  static void (*_image_decompress_etc)(Image *);
107 
108  Error _decompress_bc();
109 
110  static DVector<uint8_t> (*lossy_packer)(const Image& p_image,float p_quality);
111  static Image (*lossy_unpacker)(const DVector<uint8_t>& p_buffer);
112  static DVector<uint8_t> (*lossless_packer)(const Image& p_image);
113  static Image (*lossless_unpacker)(const DVector<uint8_t>& p_buffer);
114 private:
115 
116  //internal byte based color
117  struct BColor {
118  union {
119  uint8_t col[4];
120  struct {
121  uint8_t r,g,b,a;
122  };
123  };
124 
125  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; }
126  _FORCE_INLINE_ uint8_t gray() const { return (uint16_t(col[0])+uint16_t(col[1])+uint16_t(col[2]))/3; }
127  _FORCE_INLINE_ BColor() {}
128  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; }
129  };
130 
131  //median cut classes
132 
133  struct BColorPos {
134 
135  uint32_t index;
136  BColor color;
137  struct SortR {
138 
139  bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.r < cb.color.r; }
140  };
141 
142  struct SortG {
143 
144  bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.g < cb.color.g; }
145  };
146 
147  struct SortB {
148 
149  bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.b < cb.color.b; }
150  };
151 
152  struct SortA {
153 
154  bool operator()(const BColorPos& ca, const BColorPos& cb) const { return ca.color.a < cb.color.a; }
155  };
156  };
157 
158  struct SPTree {
159 
160  bool leaf;
161  uint8_t split_plane;
162  uint8_t split_value;
163  union {
164  int left;
165  int color;
166  };
167  int right;
168  SPTree() { leaf=true; left=-1; right=-1;}
169  };
170 
171  struct MCBlock {
172 
173  BColorPos min_color,max_color;
174  BColorPos *colors;
175  int sp_idx;
176  int color_count;
177  int get_longest_axis_index() const;
178  int get_longest_axis_length() const;
179  bool operator<(const MCBlock& p_block) const;
180  void shrink();
181  MCBlock();
182  MCBlock(BColorPos *p_colors,int p_color_count);
183  };
184 
185  Format format;
186  DVector<uint8_t> data;
187  int width,height,mipmaps;
188 
189 
190 
191  _FORCE_INLINE_ BColor _get_pixel(int p_x,int p_y,const unsigned char *p_data,int p_data_size) const;
192  _FORCE_INLINE_ BColor _get_pixelw(int p_x,int p_y,int p_width,const unsigned char *p_data,int p_data_size) const;
193  _FORCE_INLINE_ void _put_pixelw(int p_x,int p_y, int p_width, const BColor& p_color, unsigned char *p_data);
194  _FORCE_INLINE_ void _put_pixel(int p_x,int p_y, const BColor& p_color, unsigned char *p_data);
195  _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
196  _FORCE_INLINE_ static void _get_format_min_data_size(Format p_format,int &r_w, int &r_h);
197 
198  static int _get_dst_image_size(int p_width, int p_height, Format p_format,int &r_mipmaps,int p_mipmaps=-1);
199  bool _can_modify(Format p_format) const;
200 
201 
202 
203 public:
204 
205 
206 
207  int get_width() const;
208  int get_height() const;
209  int get_mipmaps() const;
210 
215  Color get_pixel(int p_x,int p_y,int p_mipmap=0) const;
219  void put_pixel(int p_x,int p_y, const Color& p_color,int p_mipmap=0); /* alpha and index are averaged */
220 
224  void convert( Format p_new_format );
225 
226  Image converted(int p_new_format) {
227  ERR_FAIL_INDEX_V(p_new_format, FORMAT_MAX, Image());
228 
229  Image ret = *this;
230  ret.convert((Format)p_new_format);
231  return ret;
232  };
233 
237  Format get_format() const;
238 
239  int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data
240  void get_mipmap_offset_and_size(int p_mipmap,int &r_ofs, int &r_size) const; //get where the mipmap begins in data
241  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
242 
248  void resize_to_po2(bool p_square=false);
249  void resize( int p_width, int p_height, Interpolation p_interpolation=INTERPOLATE_BILINEAR );
250  Image resized( int p_width, int p_height, int p_interpolation=INTERPOLATE_BILINEAR );
251  void shrink_x2();
255  void crop( int p_width, int p_height );
256 
257 
258  void flip_x();
259  void flip_y();
263  Error generate_mipmaps(int p_amount=-1,bool p_keep_existing=false);
264 
265  void clear_mipmaps();
266 
267 
272  void make_normalmap(float p_height_scale=1.0);
273 
277  void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
278  void create(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data);
279 
280  void create( const char ** p_xpm );
284  bool empty() const;
285 
286  DVector<uint8_t> get_data() const;
287 
288  Error load(const String& p_path);
289  Error save_png(const String& p_path);
290 
294  Image();
298  Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format);
302  Image(int p_width, int p_height, int p_mipmaps, Format p_format, const DVector<uint8_t>& p_data);
303 
304  enum AlphaMode {
305  ALPHA_NONE,
306  ALPHA_BIT,
307  ALPHA_BLEND
308  };
309 
310  AlphaMode detect_alpha() const;
311  bool is_invisible() const;
312 
313  void put_indexed_pixel(int p_x, int p_y, uint8_t p_idx,int p_mipmap=0);
314  uint8_t get_indexed_pixel(int p_x, int p_y,int p_mipmap=0) const;
315  void set_pallete(const DVector<uint8_t>& p_data);
316 
317 
318  static int get_format_pixel_size(Format p_format);
319  static int get_format_pixel_rshift(Format p_format);
320  static int get_format_pallete_size(Format p_format);
321  static int get_image_data_size(int p_width, int p_height, Format p_format,int p_mipmaps=0);
322  static int get_image_required_mipmaps(int p_width, int p_height, Format p_format);
323 
324 
325 
326 
327  bool operator==(const Image& p_image) const;
328 
329  void quantize();
330 
331  enum CompressMode {
332  COMPRESS_BC,
333  COMPRESS_PVRTC2,
334  COMPRESS_PVRTC4,
335  COMPRESS_ETC
336  };
337 
338  Error compress(CompressMode p_mode=COMPRESS_BC);
339  Image compressed(int p_mode); /* from the Image::CompressMode enum */
340  Error decompress();
341  Image decompressed() const;
342  bool is_compressed() const;
343 
344  void fix_alpha_edges();
345  void premultiply_alpha();
346  void srgb_to_linear();
347  void normalmap_to_xy();
348 
349  void blit_rect(const Image& p_src, const Rect2& p_src_rect,const Point2& p_dest);
350  void brush_transfer(const Image& p_src, const Image& p_brush, const Point2& p_dest);
351  Image brushed(const Image& p_src, const Image& p_brush, const Point2& p_dest) const;
352 
353  Rect2 get_used_rect() const;
354  Image get_rect(const Rect2& p_area) const;
355 
356  static void set_compress_bc_func(void (*p_compress_func)(Image *));
357  static String get_format_name(Format p_format);
358 
359  Image(const uint8_t* p_mem_png, int p_len=-1);
360  Image(const char **p_xpm);
361  ~Image();
362 
363 };
364 
365 
366 #endif
Definition: image.h:142
void put_pixel(int p_x, int p_y, const Color &p_color, int p_mipmap=0)
Definition: image.cpp:176
Definition: image.h:137
void make_normalmap(float p_height_scale=1.0)
Definition: image.cpp:1095
two bytes per pixel, 0-255. alpha 0-255
Definition: image.h:60
int get_height() const
Get image height.
Definition: image.cpp:335
Format
Definition: image.h:57
Definition: color.h:37
Definition: math_2d.h:204
Definition: image.h:47
Image()
Definition: image.cpp:2478
Color get_pixel(int p_x, int p_y, int p_mipmap=0) const
Definition: image.cpp:346
index byte 0-256, and after image end, 256*3 bytes of palette
Definition: image.h:63
one byte per pixel, 0-255
Definition: image.h:59
Format get_format() const
Definition: image.cpp:425
bool empty() const
Definition: image.cpp:1128
Definition: image.h:147
index byte 0-256, and after image end, 256*4 bytes of palette (alpha)
Definition: image.h:64
one byte per pixel, 0-255
Definition: image.h:58
void convert(Format p_new_format)
Definition: image.cpp:363
one byte R, one byte G, one byte B
Definition: image.h:61
Definition: math_2d.h:65
int get_width() const
Get image width.
Definition: image.cpp:331
Error generate_mipmaps(int p_amount=-1, bool p_keep_existing=false)
Definition: image.cpp:964
Definition: image.h:152
one byte R, one byte G, one byte B, one byte A
Definition: image.h:62
void crop(int p_width, int p_height)
Definition: image.cpp:707
void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format)
Definition: image.cpp:1138
Definition: ustring.h:64
void resize_to_po2(bool p_square=false)
Definition: image.cpp:607