arsa  2.7
IImage.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #ifndef __I_IMAGE_H_INCLUDED__
6 #define __I_IMAGE_H_INCLUDED__
7 
8 #include "IReferenceCounted.h"
9 #include "position2d.h"
10 #include "rect.h"
11 #include "SColor.h"
12 #include "irrAllocator.h"
13 #include <string.h>
14 
15 namespace irr
16 {
17 namespace video
18 {
19 
21 
25 class IImage : public virtual IReferenceCounted
26 {
27 public:
28 
30  IImage(ECOLOR_FORMAT format, const core::dimension2d<u32>& size, bool deleteMemory) :
31  Format(format), Size(size), Data(0), MipMapsData(0), BytesPerPixel(0), Pitch(0), DeleteMemory(deleteMemory), DeleteMipMapsMemory(false)
32  {
35  }
36 
38  virtual ~IImage()
39  {
40  if (DeleteMemory)
41  delete[] Data;
42 
45  }
46 
49  {
50  return Format;
51  }
52 
55  {
56  return Size;
57  }
58 
61  {
62 
64  }
65 
68  {
69  return BytesPerPixel;
70  }
71 
74  {
76  }
77 
80  {
81  return Size.Width * Size.Height;
82  }
83 
85  u32 getPitch() const
86  {
87  return Pitch;
88  }
89 
91  u32 getRedMask() const
92  {
93  switch (Format)
94  {
95  case ECF_A1R5G5B5:
96  return 0x1F << 10;
97  case ECF_R5G6B5:
98  return 0x1F << 11;
99  case ECF_R8G8B8:
100  return 0x00FF0000;
101  case ECF_A8R8G8B8:
102  return 0x00FF0000;
103  default:
104  return 0x0;
105  }
106  }
107 
110  {
111  switch (Format)
112  {
113  case ECF_A1R5G5B5:
114  return 0x1F << 5;
115  case ECF_R5G6B5:
116  return 0x3F << 5;
117  case ECF_R8G8B8:
118  return 0x0000FF00;
119  case ECF_A8R8G8B8:
120  return 0x0000FF00;
121  default:
122  return 0x0;
123  }
124  }
125 
127  u32 getBlueMask() const
128  {
129  switch (Format)
130  {
131  case ECF_A1R5G5B5:
132  return 0x1F;
133  case ECF_R5G6B5:
134  return 0x1F;
135  case ECF_R8G8B8:
136  return 0x000000FF;
137  case ECF_A8R8G8B8:
138  return 0x000000FF;
139  default:
140  return 0x0;
141  }
142  }
143 
146  {
147  switch (Format)
148  {
149  case ECF_A1R5G5B5:
150  return 0x1 << 15;
151  case ECF_R5G6B5:
152  return 0x0;
153  case ECF_R8G8B8:
154  return 0x0;
155  case ECF_A8R8G8B8:
156  return 0xFF000000;
157  default:
158  return 0x0;
159  }
160  }
161 
163 
167  void* getData() const
168  {
169  return Data;
170  }
171 
173 
179  {
180  return getData();
181  }
182 
184 
187  {
188  }
189 
191 
194  {
195  return getMipMapsSize(Size, mipmapLevel);
196  }
197 
198 
200 
201  static core::dimension2du getMipMapsSize(const core::dimension2du& sizeLevel0, u32 mipmapLevel)
202  {
203  core::dimension2du result(sizeLevel0);
204  u32 i=0;
205  while (i != mipmapLevel)
206  {
207  if (result.Width>1)
208  result.Width >>= 1;
209  if (result.Height>1)
210  result.Height>>=1;
211  ++i;
212 
213  if ( result.Width == 1 && result.Height == 1 && i < mipmapLevel )
214  return core::dimension2du(0,0);
215  }
216  return result;
217  }
218 
219 
221 
225  void* getMipMapsData(irr::u32 mipLevel=1) const
226  {
227  if ( MipMapsData && mipLevel > 0)
228  {
229  size_t dataSize = 0;
230  core::dimension2du mipSize(Size);
231  u32 i = 1; // We want the start of data for this level, not end.
232 
233  while (i != mipLevel)
234  {
235  if (mipSize.Width > 1)
236  mipSize.Width >>= 1;
237 
238  if (mipSize.Height > 1)
239  mipSize.Height >>= 1;
240 
241  dataSize += getDataSizeFromFormat(Format, mipSize.Width, mipSize.Height);
242 
243  ++i;
244  if ( mipSize.Width == 1 && mipSize.Height == 1 && i < mipLevel)
245  return 0;
246  }
247 
248  return MipMapsData + dataSize;
249  }
250 
251  return 0;
252  }
253 
255 
263  void setMipMapsData(void* data, bool ownForeignMemory, bool deleteMemory)
264  {
265  if (data != MipMapsData)
266  {
268  {
270 
271  DeleteMipMapsMemory = false;
272  }
273 
274  if (data)
275  {
276  if (ownForeignMemory)
277  {
278  MipMapsData = static_cast<u8*>(data);
279 
280  DeleteMipMapsMemory = deleteMemory;
281  }
282  else
283  {
284  u32 dataSize = 0;
285  u32 width = Size.Width;
286  u32 height = Size.Height;
287 
288  do
289  {
290  if (width > 1)
291  width >>= 1;
292 
293  if (height > 1)
294  height >>= 1;
295 
297  } while (width != 1 || height != 1);
298 
300  memcpy(MipMapsData, data, dataSize);
301 
302  DeleteMipMapsMemory = true;
303  }
304  }
305  else
306  {
307  MipMapsData = 0;
308  }
309  }
310  }
311 
313  virtual SColor getPixel(u32 x, u32 y) const = 0;
314 
316  virtual void setPixel(u32 x, u32 y, const SColor &color, bool blend = false ) = 0;
317 
319 
320  virtual void copyToScaling(void* target, u32 width, u32 height, ECOLOR_FORMAT format=ECF_A8R8G8B8, u32 pitch=0) =0;
321 
323 
324  virtual void copyToScaling(IImage* target) =0;
325 
327 
328  virtual void copyTo(IImage* target, const core::position2d<s32>& pos=core::position2d<s32>(0,0)) =0;
329 
331 
332  virtual void copyTo(IImage* target, const core::position2d<s32>& pos, const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect=0) =0;
333 
335 
338  virtual void copyToWithAlpha(IImage* target, const core::position2d<s32>& pos,
339  const core::rect<s32>& sourceRect, const SColor &color,
340  const core::rect<s32>* clipRect = 0,
341  bool combineAlpha=false) =0;
342 
344 
345  virtual void copyToScalingBoxFilter(IImage* target, s32 bias = 0, bool blend = false) = 0;
346 
348  virtual void fill(const SColor &color) =0;
349 
352  {
354  }
355 
357 
359  {
360  return (getMipMapsData() != 0);
361  }
362 
365  {
366  switch(format)
367  {
368  case ECF_A1R5G5B5:
369  return 16;
370  case ECF_R5G6B5:
371  return 16;
372  case ECF_R8G8B8:
373  return 24;
374  case ECF_A8R8G8B8:
375  return 32;
376  case ECF_DXT1:
377  return 16;
378  case ECF_DXT2:
379  case ECF_DXT3:
380  case ECF_DXT4:
381  case ECF_DXT5:
382  return 32;
383  case ECF_PVRTC_RGB2:
384  return 12;
385  case ECF_PVRTC_ARGB2:
386  case ECF_PVRTC2_ARGB2:
387  return 16;
388  case ECF_PVRTC_RGB4:
389  return 24;
390  case ECF_PVRTC_ARGB4:
391  case ECF_PVRTC2_ARGB4:
392  return 32;
393  case ECF_ETC1:
394  case ECF_ETC2_RGB:
395  return 24;
396  case ECF_ETC2_ARGB:
397  return 32;
398  case ECF_D16:
399  return 16;
400  case ECF_D32:
401  return 32;
402  case ECF_D24S8:
403  return 32;
404  case ECF_R8:
405  return 8;
406  case ECF_R8G8:
407  return 16;
408  case ECF_R16:
409  return 16;
410  case ECF_R16G16:
411  return 32;
412  case ECF_R16F:
413  return 16;
414  case ECF_G16R16F:
415  return 32;
416  case ECF_A16B16G16R16F:
417  return 64;
418  case ECF_R32F:
419  return 32;
420  case ECF_G32R32F:
421  return 64;
422  case ECF_A32B32G32R32F:
423  return 128;
424  default:
425  return 0;
426  }
427  }
428 
431  {
432  u32 imageSize = 0;
433 
434  switch (format)
435  {
436  case ECF_DXT1:
437  imageSize = ((width + 3) / 4) * ((height + 3) / 4) * 8;
438  break;
439  case ECF_DXT2:
440  case ECF_DXT3:
441  case ECF_DXT4:
442  case ECF_DXT5:
443  imageSize = ((width + 3) / 4) * ((height + 3) / 4) * 16;
444  break;
445  case ECF_PVRTC_RGB2:
446  case ECF_PVRTC_ARGB2:
447  imageSize = (core::max_<u32>(width, 16) * core::max_<u32>(height, 8) * 2 + 7) / 8;
448  break;
449  case ECF_PVRTC_RGB4:
450  case ECF_PVRTC_ARGB4:
451  imageSize = (core::max_<u32>(width, 8) * core::max_<u32>(height, 8) * 4 + 7) / 8;
452  break;
453  case ECF_PVRTC2_ARGB2:
454  imageSize = core::ceil32(width / 8.0f) * core::ceil32(height / 4.0f) * 8;
455  break;
456  case ECF_PVRTC2_ARGB4:
457  case ECF_ETC1:
458  case ECF_ETC2_RGB:
459  imageSize = core::ceil32(width / 4.0f) * core::ceil32(height / 4.0f) * 8;
460  break;
461  case ECF_ETC2_ARGB:
462  imageSize = core::ceil32(width / 4.0f) * core::ceil32(height / 4.0f) * 16;
463  break;
464  default: // uncompressed formats
466  imageSize *= height;
467  break;
468  }
469 
470  return imageSize;
471  }
472 
475  {
476  switch(format)
477  {
478  case ECF_DXT1:
479  case ECF_DXT2:
480  case ECF_DXT3:
481  case ECF_DXT4:
482  case ECF_DXT5:
483  case ECF_PVRTC_RGB2:
484  case ECF_PVRTC_ARGB2:
485  case ECF_PVRTC2_ARGB2:
486  case ECF_PVRTC_RGB4:
487  case ECF_PVRTC_ARGB4:
488  case ECF_PVRTC2_ARGB4:
489  case ECF_ETC1:
490  case ECF_ETC2_RGB:
491  case ECF_ETC2_ARGB:
492  return true;
493  default:
494  return false;
495  }
496  }
497 
499  static bool isDepthFormat(const ECOLOR_FORMAT format)
500  {
501  switch(format)
502  {
503  case ECF_D16:
504  case ECF_D32:
505  case ECF_D24S8:
506  return true;
507  default:
508  return false;
509  }
510  }
511 
514  {
516  return false;
517 
518  switch(format)
519  {
520  case ECF_R16F:
521  case ECF_G16R16F:
522  case ECF_A16B16G16R16F:
523  case ECF_R32F:
524  case ECF_G32R32F:
525  case ECF_A32B32G32R32F:
526  return true;
527  default:
528  break;
529  }
530  return false;
531  }
532 
533 protected:
536 
539 
542 
545 
547 };
548 
549 } // end namespace video
550 } // end namespace irr
551 
552 #endif
553 
_IRR_DEPRECATED_ bool isCompressed() const
Inform whether the image is compressed.
Definition: IImage.h:351
u32 getBytesPerPixel() const
Returns bytes per pixel.
Definition: IImage.h:67
dimension2d< u32 > dimension2du
Typedef for an unsigned integer dimension.
Definition: dimension2d.h:212
static u32 getBitsPerPixelFromFormat(const ECOLOR_FORMAT format)
get the amount of Bits per Pixel of the given color format
Definition: IImage.h:364
#define _IRR_DEPRECATED_
Defines a deprecated macro which generates a warning at compile time.
Definition: irrTypes.h:202
16 bit format using 16 bits for depth.
Definition: SColor.h:123
DXT3 color format.
Definition: SColor.h:49
PVRTC RGB 4bpp.
Definition: SColor.h:64
GLuint64EXT * result
DXT4 color format.
Definition: SColor.h:52
64 bit format using 32 bits for the red and green channels.
Definition: SColor.h:101
virtual SColor getPixel(u32 x, u32 y) const =0
Returns a pixel.
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
64 bit format using 16 bits for the red, green, blue and alpha channels.
Definition: SColor.h:95
32 bit format using 32 bits for the red channel.
Definition: SColor.h:98
const core::dimension2d< u32 > & getDimension() const
Returns width and height of image data.
Definition: IImage.h:54
virtual void copyToScalingBoxFilter(IImage *target, s32 bias=0, bool blend=false)=0
copies this surface into another, scaling it to fit, applying a box filter
core::irrAllocator< u8 > Allocator
Definition: IImage.h:546
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
u32 getPitch() const
Returns pitch of image.
Definition: IImage.h:85
u32 getAlphaMask() const
Returns mask for alpha value of a pixel.
Definition: IImage.h:145
virtual void fill(const SColor &color)=0
fills the surface with given color
ECOLOR_FORMAT
An enum for the color format of textures used by the Irrlicht Engine.
Definition: SColor.h:20
u32 getRedMask() const
Returns mask for red value of a pixel.
Definition: IImage.h:91
Everything in the Irrlicht Engine can be found in this namespace.
Definition: CARSADPad.h:6
16 bit format using 16 bits for the red channel.
Definition: SColor.h:89
REALINLINE s32 ceil32(f32 x)
Definition: irrMath.h:651
GLsizeiptr size
32 bit format using 24 bits for depth and 8 bits for stencil.
Definition: SColor.h:129
PVRTC RGB 2bpp.
Definition: SColor.h:58
Interface for software image data.
Definition: IImage.h:25
virtual void copyTo(IImage *target, const core::position2d< s32 > &pos=core::position2d< s32 >(0, 0))=0
copies this surface into another
unsigned char u8
8 bit unsigned variable.
Definition: irrTypes.h:22
u32 getGreenMask() const
Returns mask for green value of a pixel.
Definition: IImage.h:109
SDL_AudioFormat format
Definition: SDL_audio.h:194
static bool isFloatingPointFormat(const ECOLOR_FORMAT format)
Check if the color format uses floating point values for pixels.
Definition: IImage.h:513
_IRR_DEPRECATED_ void * lock()
Lock function. Use this to get a pointer to the image data.
Definition: IImage.h:178
PVRTC2 ARGB 4bpp.
Definition: SColor.h:73
_IRR_DEPRECATED_ void unlock()
Unlock function.
Definition: IImage.h:186
virtual void copyToWithAlpha(IImage *target, const core::position2d< s32 > &pos, const core::rect< s32 > &sourceRect, const SColor &color, const core::rect< s32 > *clipRect=0, bool combineAlpha=false)=0
copies this surface into another, using the alpha mask and cliprect and a color to add with
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
128 bit format using 32 bits for the red, green, blue and alpha channels.
Definition: SColor.h:104
GLfloat f
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62
16 bit color format used by the software driver.
Definition: SColor.h:26
8 bit format using 8 bits for the red channel.
Definition: SColor.h:109
T * allocate(size_t cnt)
Allocate memory for an array of objects.
Definition: irrAllocator.h:33
_IRR_DEPRECATED_ bool hasMipMaps() const
Check whether the image has MipMaps.
Definition: IImage.h:358
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
DXT2 color format.
Definition: SColor.h:46
core::dimension2du getMipMapsSize(u32 mipmapLevel) const
Get the mipmap size for this image for a certain mipmap level.
Definition: IImage.h:193
void * getData() const
Use this to get a pointer to the image data.
Definition: IImage.h:167
PVRTC2 ARGB 2bpp.
Definition: SColor.h:70
T Width
Width of the dimension.
Definition: dimension2d.h:204
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei imageSize
Definition: SDL_opengl.h:1974
u32 getBitsPerPixel() const
Returns bits per pixel.
Definition: IImage.h:60
ECOLOR_FORMAT getColorFormat() const
Returns the color format.
Definition: IImage.h:48
ECOLOR_FORMAT Format
Definition: IImage.h:534
16 bit format using 16 bits for the red channel.
Definition: SColor.h:115
Class representing a 32 bit ARGB color.
Definition: SColor.h:316
IImage(ECOLOR_FORMAT format, const core::dimension2d< u32 > &size, bool deleteMemory)
constructor
Definition: IImage.h:30
GLenum target
virtual ~IImage()
destructor
Definition: IImage.h:38
32 bit format using 16 bits for the red and green channels.
Definition: SColor.h:118
u32 getImageDataSizeInPixels() const
Returns image data size in pixels.
Definition: IImage.h:79
core::dimension2d< u32 > Size
Definition: IImage.h:535
DXT5 color format.
Definition: SColor.h:55
static u32 getDataSizeFromFormat(ECOLOR_FORMAT format, u32 width, u32 height)
calculate image data size in bytes for selected format, width and height.
Definition: IImage.h:430
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
u32 getImageDataSizeInBytes() const
Returns image data size in bytes.
Definition: IImage.h:73
Standard 16 bit color format.
Definition: SColor.h:29
void setMipMapsData(void *data, bool ownForeignMemory, bool deleteMemory)
Set mipmaps data.
Definition: IImage.h:263
GLuint color
ETC1 RGB.
Definition: SColor.h:76
Base class of most objects of the Irrlicht Engine.
static bool isDepthFormat(const ECOLOR_FORMAT format)
check if the color format is only viable for depth/stencil textures
Definition: IImage.h:499
static core::dimension2du getMipMapsSize(const core::dimension2du &sizeLevel0, u32 mipmapLevel)
Calculate mipmap size for a certain level.
Definition: IImage.h:201
DXT1 color format.
Definition: SColor.h:43
T Height
Height of the dimension.
Definition: dimension2d.h:206
u32 getBlueMask() const
Returns mask for blue value of a pixel.
Definition: IImage.h:127
void deallocate(T *ptr)
Deallocate memory for an array of objects.
Definition: irrAllocator.h:39
virtual void setPixel(u32 x, u32 y, const SColor &color, bool blend=false)=0
Sets a pixel.
32 bit format using 32 bits for depth.
Definition: SColor.h:126
32 bit format using 16 bits for the red and green channels.
Definition: SColor.h:92
16 bit format using 8 bits for the red and green channels.
Definition: SColor.h:112
GLenum GLsizei dataSize
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
virtual void copyToScaling(void *target, u32 width, u32 height, ECOLOR_FORMAT format=ECF_A8R8G8B8, u32 pitch=0)=0
Copies the image into the target, scaling the image to fit.
static bool isCompressedFormat(const ECOLOR_FORMAT format)
check if this is compressed color format
Definition: IImage.h:474
bool DeleteMipMapsMemory
Definition: IImage.h:544
PVRTC ARGB 4bpp.
Definition: SColor.h:67
GLfloat bias
PVRTC ARGB 2bpp.
Definition: SColor.h:61
void * getMipMapsData(irr::u32 mipLevel=1) const
Get mipmaps data.
Definition: IImage.h:225