Nirtcpp 2.1.0
Nirtcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
IImage.hpp
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 nirtcpp/nirtcpp.hpp
4
5#ifndef NIRT_I_IMAGE_HPP_INCLUDED
6#define NIRT_I_IMAGE_HPP_INCLUDED
7
8#include <nirtcpp/core/engine/IReferenceCounted.hpp>
9#include <nirtcpp/core/engine/position2d.hpp>
10#include <nirtcpp/core/engine/rect.hpp>
11#include <nirtcpp/core/engine/SColor.hpp>
12#include <nirtcpp/core/engine/irrAllocator.hpp>
13#include <algorithm>
14
15namespace nirt
16{
17namespace video
18{
19
21
25class IImage : public virtual IReferenceCounted
26{
27public:
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#if defined(NIRTCPP_sRGB)
33 ,Format_sRGB(1)
34#endif
35 {
36 BytesPerPixel = getBitsPerPixelFromFormat(Format) / 8;
37 Pitch = BytesPerPixel * Size.Width;
38 }
39
41 virtual ~IImage()
42 {
43 if (DeleteMemory)
44 delete[] Data;
45
46 if (DeleteMipMapsMemory)
47 Allocator.deallocate(MipMapsData);
48 }
49
52 {
53 return Format;
54 }
55
56#if defined(NIRTCPP_sRGB)
58 int get_sRGB() const
59 {
60 return Format_sRGB;
61 }
62 void set_sRGB(int val)
63 {
64 Format_sRGB = val;
65 }
66#endif
67
70 {
71 return Size;
72 }
73
76 {
77 return getBitsPerPixelFromFormat(Format);
78 }
79
82 {
83 return BytesPerPixel;
84 }
85
88 {
89 return getDataSizeFromFormat(Format, Size.Width, Size.Height);
90 }
91
94 {
95 return Size.Width * Size.Height;
96 }
97
99 u32 getPitch() const
100 {
101 return Pitch;
102 }
103
106 {
107 switch (Format)
108 {
109 case ECF_A1R5G5B5:
110 return 0x1F << 10;
111 case ECF_R5G6B5:
112 return 0x1F << 11;
113 case ECF_R8G8B8:
114 return 0x00FF0000;
115 case ECF_A8R8G8B8:
116 return 0x00FF0000;
117 default:
118 return 0x0;
119 }
120 }
121
124 {
125 switch (Format)
126 {
127 case ECF_A1R5G5B5:
128 return 0x1F << 5;
129 case ECF_R5G6B5:
130 return 0x3F << 5;
131 case ECF_R8G8B8:
132 return 0x0000FF00;
133 case ECF_A8R8G8B8:
134 return 0x0000FF00;
135 default:
136 return 0x0;
137 }
138 }
139
142 {
143 switch (Format)
144 {
145 case ECF_A1R5G5B5:
146 return 0x1F;
147 case ECF_R5G6B5:
148 return 0x1F;
149 case ECF_R8G8B8:
150 return 0x000000FF;
151 case ECF_A8R8G8B8:
152 return 0x000000FF;
153 default:
154 return 0x0;
155 }
156 }
157
160 {
161 switch (Format)
162 {
163 case ECF_A1R5G5B5:
164 return 0x1 << 15;
165 case ECF_R5G6B5:
166 return 0x0;
167 case ECF_R8G8B8:
168 return 0x0;
169 case ECF_A8R8G8B8:
170 return 0xFF000000;
171 default:
172 return 0x0;
173 }
174 }
175
177
181 void* getData() const
182 {
183 return Data;
184 }
185
187
192 NIRT_DEPRECATED void* lock()
193 {
194 return getData();
195 }
196
198
200 NIRT_DEPRECATED void unlock()
201 {
202 }
203
205
208 {
209 return getMipMapsSize(Size, mipmapLevel);
210 }
211
212
214
215 static core::dimension2du getMipMapsSize(const core::dimension2du& sizeLevel0, u32 mipmapLevel)
216 {
217 core::dimension2du result(sizeLevel0);
218 u32 i=0;
219 while (i != mipmapLevel)
220 {
221 if (result.Width>1)
222 result.Width >>= 1;
223 if (result.Height>1)
224 result.Height>>=1;
225 ++i;
226
227 if ( result.Width == 1 && result.Height == 1 && i < mipmapLevel )
228 return core::dimension2du(0,0);
229 }
230 return result;
231 }
232
233
235
239 void* getMipMapsData(nirt::u32 mipLevel=1) const
240 {
241 if ( MipMapsData && mipLevel > 0)
242 {
243 size_t dataSize = 0;
244 core::dimension2du mipSize(Size);
245 u32 i = 1; // We want the start of data for this level, not end.
246
247 while (i != mipLevel)
248 {
249 if (mipSize.Width > 1)
250 mipSize.Width >>= 1;
251
252 if (mipSize.Height > 1)
253 mipSize.Height >>= 1;
254
255 dataSize += getDataSizeFromFormat(Format, mipSize.Width, mipSize.Height);
256
257 ++i;
258 if ( mipSize.Width == 1 && mipSize.Height == 1 && i < mipLevel)
259 return 0;
260 }
261
262 return MipMapsData + dataSize;
263 }
264
265 return 0;
266 }
267
269
277 void setMipMapsData(void* data, bool ownForeignMemory, bool deleteMemory)
278 {
279 if (data != MipMapsData)
280 {
281 if (DeleteMipMapsMemory)
282 {
283 Allocator.deallocate(MipMapsData);
284
285 DeleteMipMapsMemory = false;
286 }
287
288 if (data)
289 {
290 if (ownForeignMemory)
291 {
292 MipMapsData = static_cast<u8*>(data);
293
294 DeleteMipMapsMemory = deleteMemory;
295 }
296 else
297 {
298 size_t dataSize = 0;
299 u32 width = Size.Width;
300 u32 height = Size.Height;
301
302 do
303 {
304 if (width > 1)
305 width >>= 1;
306
307 if (height > 1)
308 height >>= 1;
309
310 dataSize += getDataSizeFromFormat(Format, width, height);
311 } while (width != 1 || height != 1);
312
313 MipMapsData = Allocator.allocate(dataSize);
314 std::copy_n((char *)data, dataSize, (char *)MipMapsData);
315
316 DeleteMipMapsMemory = true;
317 }
318 }
319 else
320 {
321 MipMapsData = 0;
322 }
323 }
324 }
325
327 virtual SColor getPixel(u32 x, u32 y) const = 0;
328
330 virtual void setPixel(u32 x, u32 y, const SColor &color, bool blend = false ) = 0;
331
333
334 virtual void copyToScaling(void* target, u32 width, u32 height, ECOLOR_FORMAT format=ECF_A8R8G8B8, u32 pitch=0) =0;
335
337
338 virtual void copyToScaling(IImage* target) =0;
339
341
342 virtual void copyTo(IImage* target, const core::position2d<s32>& pos=core::position2d<s32>(0,0)) =0;
343
345
346 virtual void copyTo(IImage* target, const core::position2d<s32>& pos, const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect=0) =0;
347
349
352 virtual void copyToWithAlpha(IImage* target, const core::position2d<s32>& pos,
353 const core::rect<s32>& sourceRect, const SColor &color,
354 const core::rect<s32>* clipRect = 0,
355 bool combineAlpha=false) =0;
356
358
359 virtual void copyToScalingBoxFilter(IImage* target, s32 bias = 0, bool blend = false) = 0;
360
362
364 virtual void flip(bool topBottom, bool leftRight) = 0;
365
367 virtual void fill(const SColor &color) =0;
368
370 NIRT_DEPRECATED bool isCompressed() const
371 {
372 return IImage::isCompressedFormat(Format);
373 }
374
376
377 NIRT_DEPRECATED bool hasMipMaps() const
378 {
379 return (getMipMapsData() != 0);
380 }
381
384 {
385 switch(format)
386 {
387 case ECF_A1R5G5B5:
388 return 16;
389 case ECF_R5G6B5:
390 return 16;
391 case ECF_R8G8B8:
392 return 24;
393 case ECF_A8R8G8B8:
394 return 32;
395 case ECF_DXT1:
396 return 16;
397 case ECF_DXT2:
398 case ECF_DXT3:
399 case ECF_DXT4:
400 case ECF_DXT5:
401 return 32;
402 case ECF_PVRTC_RGB2:
403 return 12;
404 case ECF_PVRTC_ARGB2:
405 case ECF_PVRTC2_ARGB2:
406 return 16;
407 case ECF_PVRTC_RGB4:
408 return 24;
409 case ECF_PVRTC_ARGB4:
410 case ECF_PVRTC2_ARGB4:
411 return 32;
412 case ECF_ETC1:
413 case ECF_ETC2_RGB:
414 return 24;
415 case ECF_ETC2_ARGB:
416 return 32;
417 case ECF_D16:
418 return 16;
419 case ECF_D32:
420 return 32;
421 case ECF_D24S8:
422 return 32;
423 case ECF_R8:
424 return 8;
425 case ECF_R8G8:
426 return 16;
427 case ECF_R16:
428 return 16;
429 case ECF_R16G16:
430 return 32;
431 case ECF_R16F:
432 return 16;
433 case ECF_G16R16F:
434 return 32;
436 return 64;
437 case ECF_R32F:
438 return 32;
439 case ECF_G32R32F:
440 return 64;
442 return 128;
443 default:
444 return 0;
445 }
446 }
447
449
454 static bool checkDataSizeLimit(size_t dataSize)
455 {
456 // 2gb for now. Could be we could do more on some platforms, but we still will run into
457 // problems right now then for example in then color converter (which currently still uses
458 // s32 for sizes).
459 return (size_t)(s32)(dataSize) == dataSize;
460 }
461
463 static size_t getDataSizeFromFormat(ECOLOR_FORMAT format, u32 width, u32 height)
464 {
465 size_t imageSize = 0;
466
467 switch (format)
468 {
469 case ECF_DXT1:
470 imageSize = (size_t)((width + 3) / 4) * ((height + 3) / 4) * 8;
471 break;
472 case ECF_DXT2:
473 case ECF_DXT3:
474 case ECF_DXT4:
475 case ECF_DXT5:
476 imageSize = (size_t)((width + 3) / 4) * ((height + 3) / 4) * 16;
477 break;
478 case ECF_PVRTC_RGB2:
479 case ECF_PVRTC_ARGB2:
480 imageSize = ((size_t)core::max_<u32>(width, 16) * core::max_<u32>(height, 8) * 2 + 7) / 8;
481 break;
482 case ECF_PVRTC_RGB4:
483 case ECF_PVRTC_ARGB4:
484 imageSize = ((size_t)core::max_<u32>(width, 8) * core::max_<u32>(height, 8) * 4 + 7) / 8;
485 break;
486 case ECF_PVRTC2_ARGB2:
487 imageSize = (size_t)core::ceil32(width / 8.0f) * core::ceil32(height / 4.0f) * 8;
488 break;
489 case ECF_PVRTC2_ARGB4:
490 case ECF_ETC1:
491 case ECF_ETC2_RGB:
492 imageSize = (size_t)core::ceil32(width / 4.0f) * core::ceil32(height / 4.0f) * 8;
493 break;
494 case ECF_ETC2_ARGB:
495 imageSize = (size_t)core::ceil32(width / 4.0f) * core::ceil32(height / 4.0f) * 16;
496 break;
497 default: // uncompressed formats
498 imageSize = (size_t)getBitsPerPixelFromFormat(format) / 8 * width;
499 imageSize *= height;
500 break;
501 }
502
503 return imageSize;
504 }
505
506// Define to check for all compressed image formats cases in a switch
507#define NIRT_CASE_IIMAGE_COMPRESSED_FORMAT\
508 case ECF_DXT1:\
509 case ECF_DXT2:\
510 case ECF_DXT3:\
511 case ECF_DXT4:\
512 case ECF_DXT5:\
513 case ECF_PVRTC_RGB2:\
514 case ECF_PVRTC_ARGB2:\
515 case ECF_PVRTC2_ARGB2:\
516 case ECF_PVRTC_RGB4:\
517 case ECF_PVRTC_ARGB4:\
518 case ECF_PVRTC2_ARGB4:\
519 case ECF_ETC1:\
520 case ECF_ETC2_RGB:\
521 case ECF_ETC2_ARGB:
522
524 static bool isCompressedFormat(const ECOLOR_FORMAT format)
525 {
526 switch(format)
527 {
528 NIRT_CASE_IIMAGE_COMPRESSED_FORMAT
529 return true;
530 default:
531 return false;
532 }
533 }
534
536 static bool isDepthFormat(const ECOLOR_FORMAT format)
537 {
538 switch(format)
539 {
540 case ECF_D16:
541 case ECF_D32:
542 case ECF_D24S8:
543 return true;
544 default:
545 return false;
546 }
547 }
548
550 static bool isFloatingPointFormat(const ECOLOR_FORMAT format)
551 {
552 switch(format)
553 {
554 case ECF_R16F:
555 case ECF_G16R16F:
557 case ECF_R32F:
558 case ECF_G32R32F:
560 return true;
561 default:
562 break;
563 }
564 return false;
565 }
566
567#if defined(PATCH_SUPERTUX_8_0_1_with_1_9_0)
568 static bool isRenderTargetOnlyFormat(const ECOLOR_FORMAT format)
569 {
570 switch (format)
571 {
572 case ECF_A1R5G5B5:
573 case ECF_R5G6B5:
574 case ECF_R8G8B8:
575 case ECF_A8R8G8B8:
576 return false;
577 default:
578 return true;
579 }
580 }
581#endif
582
583protected:
584 ECOLOR_FORMAT Format;
585 core::dimension2d<u32> Size;
586
587 u8* Data;
588 u8* MipMapsData;
589
590 u32 BytesPerPixel;
591 u32 Pitch;
592
593 bool DeleteMemory;
594 bool DeleteMipMapsMemory;
595
596 core::irrAllocator<u8> Allocator;
597#if defined(NIRTCPP_sRGB)
598 int Format_sRGB;
599#endif
600};
601
602
603} // end namespace video
604} // end namespace nirt
605
606#endif
Base class of most objects of the Nirtcpp Engine.
Definition IReferenceCounted.hpp:46
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
T Height
Height of the dimension.
Definition dimension2d.hpp:206
T Width
Width of the dimension.
Definition dimension2d.hpp:204
void deallocate(T *ptr)
Deallocate memory for an array of objects.
Definition irrAllocator.hpp:39
T * allocate(size_t cnt)
Allocate memory for an array of objects.
Definition irrAllocator.hpp:33
Interface for software image data.
Definition IImage.hpp:26
NIRT_DEPRECATED bool isCompressed() const
Inform whether the image is compressed.
Definition IImage.hpp:370
static u32 getBitsPerPixelFromFormat(const ECOLOR_FORMAT format)
get the amount of Bits per Pixel of the given color format
Definition IImage.hpp:383
void * getData() const
Use this to get a pointer to the image data.
Definition IImage.hpp:181
static bool isFloatingPointFormat(const ECOLOR_FORMAT format)
Check if the color format uses floating point values for pixels.
Definition IImage.hpp:550
const core::dimension2d< u32 > & getDimension() const
Returns width and height of image data.
Definition IImage.hpp:69
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.
ECOLOR_FORMAT getColorFormat() const
Returns the color format.
Definition IImage.hpp:51
IImage(ECOLOR_FORMAT format, const core::dimension2d< u32 > &size, bool deleteMemory)
constructor
Definition IImage.hpp:30
static bool isCompressedFormat(const ECOLOR_FORMAT format)
check if this is compressed color format
Definition IImage.hpp:524
static core::dimension2du getMipMapsSize(const core::dimension2du &sizeLevel0, u32 mipmapLevel)
Calculate mipmap size for a certain level.
Definition IImage.hpp:215
u32 getRedMask() const
Returns mask for red value of a pixel.
Definition IImage.hpp:105
NIRT_DEPRECATED void unlock()
Unlock function.
Definition IImage.hpp:200
virtual void copyTo(IImage *target, const core::position2d< s32 > &pos=core::position2d< s32 >(0, 0))=0
copies this surface into another
u32 getBytesPerPixel() const
Returns bytes per pixel.
Definition IImage.hpp:81
u32 getAlphaMask() const
Returns mask for alpha value of a pixel.
Definition IImage.hpp:159
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
virtual ~IImage()
destructor
Definition IImage.hpp:41
u32 getBlueMask() const
Returns mask for blue value of a pixel.
Definition IImage.hpp:141
static bool isDepthFormat(const ECOLOR_FORMAT format)
check if the color format is only viable for depth/stencil textures
Definition IImage.hpp:536
virtual void flip(bool topBottom, bool leftRight)=0
Flips (mirrors) the image in one or two directions.
core::dimension2du getMipMapsSize(u32 mipmapLevel) const
Get the mipmap size for this image for a certain mipmap level.
Definition IImage.hpp:207
u32 getGreenMask() const
Returns mask for green value of a pixel.
Definition IImage.hpp:123
u32 getPitch() const
Returns pitch of image.
Definition IImage.hpp:99
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
void setMipMapsData(void *data, bool ownForeignMemory, bool deleteMemory)
Set mipmaps data.
Definition IImage.hpp:277
NIRT_DEPRECATED void * lock()
Lock function. Use this to get a pointer to the image data.
Definition IImage.hpp:192
virtual void copyToScaling(IImage *target)=0
Copies the image into the target, scaling the image to fit.
virtual void copyTo(IImage *target, const core::position2d< s32 > &pos, const core::rect< s32 > &sourceRect, const core::rect< s32 > *clipRect=0)=0
copies this surface into another
virtual void fill(const SColor &color)=0
fills the surface with given color
static bool checkDataSizeLimit(size_t dataSize)
You should not create images where the result of getDataSizeFromFormat doesn't pass this function.
Definition IImage.hpp:454
virtual void setPixel(u32 x, u32 y, const SColor &color, bool blend=false)=0
Sets a pixel.
static size_t getDataSizeFromFormat(ECOLOR_FORMAT format, u32 width, u32 height)
calculate image data size in bytes for selected format, width and height.
Definition IImage.hpp:463
void * getMipMapsData(nirt::u32 mipLevel=1) const
Get mipmaps data.
Definition IImage.hpp:239
u32 getBitsPerPixel() const
Returns bits per pixel.
Definition IImage.hpp:75
NIRT_DEPRECATED bool hasMipMaps() const
Check whether the image has MipMaps.
Definition IImage.hpp:377
size_t getImageDataSizeInBytes() const
Returns image data size in bytes.
Definition IImage.hpp:87
virtual SColor getPixel(u32 x, u32 y) const =0
Returns a pixel.
u32 getImageDataSizeInPixels() const
Returns image data size in pixels.
Definition IImage.hpp:93
Class representing a 32 bit ARGB color.
Definition SColor.hpp:317
dimension2d< u32 > dimension2du
using type alias for an unsigned integer dimension.
Definition dimension2d.hpp:212
ECOLOR_FORMAT
An enum for the color format of textures used by the Nirtcpp Engine.
Definition SColor.hpp:21
@ ECF_D32
32 bit format using 32 bits for depth.
Definition SColor.hpp:126
@ ECF_PVRTC_ARGB4
PVRTC ARGB 4bpp.
Definition SColor.hpp:67
@ ECF_G32R32F
64 bit format using 32 bits for the red and green channels.
Definition SColor.hpp:101
@ ECF_A16B16G16R16F
64 bit format using 16 bits for the red, green, blue and alpha channels.
Definition SColor.hpp:95
@ ECF_ETC2_RGB
ETC2 RGB.
Definition SColor.hpp:79
@ ECF_A32B32G32R32F
128 bit format using 32 bits for the red, green, blue and alpha channels.
Definition SColor.hpp:104
@ ECF_DXT5
DXT5 color format.
Definition SColor.hpp:55
@ ECF_ETC1
ETC1 RGB.
Definition SColor.hpp:76
@ ECF_R16G16
32 bit format using 16 bits for the red and green channels.
Definition SColor.hpp:118
@ ECF_D16
16 bit format using 16 bits for depth.
Definition SColor.hpp:123
@ ECF_R8G8
16 bit format using 8 bits for the red and green channels.
Definition SColor.hpp:112
@ ECF_A8R8G8B8
Definition SColor.hpp:38
@ ECF_PVRTC_RGB2
PVRTC RGB 2bpp.
Definition SColor.hpp:58
@ ECF_R8G8B8
Definition SColor.hpp:34
@ ECF_DXT1
DXT1 color format.
Definition SColor.hpp:43
@ ECF_PVRTC_ARGB2
PVRTC ARGB 2bpp.
Definition SColor.hpp:61
@ ECF_PVRTC_RGB4
PVRTC RGB 4bpp.
Definition SColor.hpp:64
@ ECF_A1R5G5B5
16 bit color format used by the software driver.
Definition SColor.hpp:26
@ ECF_PVRTC2_ARGB4
PVRTC2 ARGB 4bpp.
Definition SColor.hpp:73
@ ECF_DXT4
DXT4 color format.
Definition SColor.hpp:52
@ ECF_R16F
16 bit format using 16 bits for the red channel.
Definition SColor.hpp:89
@ ECF_G16R16F
32 bit format using 16 bits for the red and green channels.
Definition SColor.hpp:92
@ ECF_R32F
32 bit format using 32 bits for the red channel.
Definition SColor.hpp:98
@ ECF_D24S8
32 bit format using 24 bits for depth and 8 bits for stencil.
Definition SColor.hpp:129
@ ECF_DXT3
DXT3 color format.
Definition SColor.hpp:49
@ ECF_R5G6B5
Standard 16 bit color format.
Definition SColor.hpp:29
@ ECF_R16
16 bit format using 16 bits for the red channel.
Definition SColor.hpp:115
@ ECF_PVRTC2_ARGB2
PVRTC2 ARGB 2bpp.
Definition SColor.hpp:70
@ ECF_ETC2_ARGB
ETC2 ARGB.
Definition SColor.hpp:82
@ ECF_R8
8 bit format using 8 bits for the red channel.
Definition SColor.hpp:109
@ ECF_DXT2
DXT2 color format.
Definition SColor.hpp:46
As of Nirtcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
signed int s32
32 bit signed variable.
Definition irrTypes.hpp:72
unsigned char u8
8 bit unsigned variable.
Definition irrTypes.hpp:24
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.hpp:64

Nirtcpp    @cppfx.xyz

Esvcpp    esv::print