IImage.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. #ifndef IRR_I_IMAGE_H_INCLUDED
  5. #define IRR_I_IMAGE_H_INCLUDED
  6. #include "IReferenceCounted.h"
  7. #include "position2d.h"
  8. #include "rect.h"
  9. #include "SColor.h"
  10. #include "irrAllocator.h"
  11. namespace irr
  12. {
  13. namespace video
  14. {
  15. //! Interface for software image data.
  16. /** Image loaders create these images from files. IVideoDrivers convert
  17. these images into their (hardware) textures.
  18. NOTE: Floating point formats are not well supported yet. Basically only getData() works for them.
  19. */
  20. class IImage : public virtual IReferenceCounted
  21. {
  22. public:
  23. //! constructor
  24. IImage(ECOLOR_FORMAT format, const core::dimension2d<u32>& size, bool deleteMemory) :
  25. Format(format), Size(size), Data(0), MipMapsData(0), BytesPerPixel(0), Pitch(0), DeleteMemory(deleteMemory), DeleteMipMapsMemory(false)
  26. #if defined(IRRLICHT_sRGB)
  27. ,Format_sRGB(1)
  28. #endif
  29. {
  30. BytesPerPixel = getBitsPerPixelFromFormat(Format) / 8;
  31. Pitch = BytesPerPixel * Size.Width;
  32. }
  33. //! destructor
  34. virtual ~IImage()
  35. {
  36. if (DeleteMemory)
  37. delete[] Data;
  38. if (DeleteMipMapsMemory)
  39. Allocator.deallocate(MipMapsData);
  40. }
  41. //! Returns the color format
  42. ECOLOR_FORMAT getColorFormat() const
  43. {
  44. return Format;
  45. }
  46. #if defined(IRRLICHT_sRGB)
  47. //! Texture is linear/sRGB (should be part of ColorFormat: default yes)
  48. int get_sRGB() const
  49. {
  50. return Format_sRGB;
  51. }
  52. void set_sRGB(int val)
  53. {
  54. Format_sRGB = val;
  55. }
  56. #endif
  57. //! Returns width and height of image data.
  58. const core::dimension2d<u32>& getDimension() const
  59. {
  60. return Size;
  61. }
  62. //! Returns bits per pixel.
  63. u32 getBitsPerPixel() const
  64. {
  65. return getBitsPerPixelFromFormat(Format);
  66. }
  67. //! Returns bytes per pixel
  68. u32 getBytesPerPixel() const
  69. {
  70. return BytesPerPixel;
  71. }
  72. //! Returns image data size in bytes
  73. size_t getImageDataSizeInBytes() const
  74. {
  75. return getDataSizeFromFormat(Format, Size.Width, Size.Height);
  76. }
  77. //! Returns image data size in pixels
  78. u32 getImageDataSizeInPixels() const
  79. {
  80. return Size.Width * Size.Height;
  81. }
  82. //! Returns pitch of image
  83. u32 getPitch() const
  84. {
  85. return Pitch;
  86. }
  87. //! Returns mask for red value of a pixel
  88. u32 getRedMask() const
  89. {
  90. switch (Format)
  91. {
  92. case ECF_A1R5G5B5:
  93. return 0x1F << 10;
  94. case ECF_R5G6B5:
  95. return 0x1F << 11;
  96. case ECF_R8G8B8:
  97. return 0x00FF0000;
  98. case ECF_A8R8G8B8:
  99. return 0x00FF0000;
  100. default:
  101. return 0x0;
  102. }
  103. }
  104. //! Returns mask for green value of a pixel
  105. u32 getGreenMask() const
  106. {
  107. switch (Format)
  108. {
  109. case ECF_A1R5G5B5:
  110. return 0x1F << 5;
  111. case ECF_R5G6B5:
  112. return 0x3F << 5;
  113. case ECF_R8G8B8:
  114. return 0x0000FF00;
  115. case ECF_A8R8G8B8:
  116. return 0x0000FF00;
  117. default:
  118. return 0x0;
  119. }
  120. }
  121. //! Returns mask for blue value of a pixel
  122. u32 getBlueMask() const
  123. {
  124. switch (Format)
  125. {
  126. case ECF_A1R5G5B5:
  127. return 0x1F;
  128. case ECF_R5G6B5:
  129. return 0x1F;
  130. case ECF_R8G8B8:
  131. return 0x000000FF;
  132. case ECF_A8R8G8B8:
  133. return 0x000000FF;
  134. default:
  135. return 0x0;
  136. }
  137. }
  138. //! Returns mask for alpha value of a pixel
  139. u32 getAlphaMask() const
  140. {
  141. switch (Format)
  142. {
  143. case ECF_A1R5G5B5:
  144. return 0x1 << 15;
  145. case ECF_R5G6B5:
  146. return 0x0;
  147. case ECF_R8G8B8:
  148. return 0x0;
  149. case ECF_A8R8G8B8:
  150. return 0xFF000000;
  151. default:
  152. return 0x0;
  153. }
  154. }
  155. //! Use this to get a pointer to the image data.
  156. /**
  157. \return Pointer to the image data. What type of data is pointed to
  158. depends on the color format of the image. For example if the color
  159. format is ECF_A8R8G8B8, it is of u32. */
  160. void* getData() const
  161. {
  162. return Data;
  163. }
  164. //! Lock function. Use this to get a pointer to the image data.
  165. /** Use getData instead.
  166. \return Pointer to the image data. What type of data is pointed to
  167. depends on the color format of the image. For example if the color
  168. format is ECF_A8R8G8B8, it is of u32. Be sure to call unlock() after
  169. you don't need the pointer any more. */
  170. IRR_DEPRECATED void* lock()
  171. {
  172. return getData();
  173. }
  174. //! Unlock function.
  175. /** Should be called after the pointer received by lock() is not
  176. needed anymore. */
  177. IRR_DEPRECATED void unlock()
  178. {
  179. }
  180. //! Get the mipmap size for this image for a certain mipmap level
  181. /** level 0 will be full image size. Every further level is half the size.
  182. Doesn't care if the image actually has mipmaps, just which size would be needed. */
  183. core::dimension2du getMipMapsSize(u32 mipmapLevel) const
  184. {
  185. return getMipMapsSize(Size, mipmapLevel);
  186. }
  187. //! Calculate mipmap size for a certain level
  188. /** level 0 will be full image size. Every further level is half the size. */
  189. static core::dimension2du getMipMapsSize(const core::dimension2du& sizeLevel0, u32 mipmapLevel)
  190. {
  191. core::dimension2du result(sizeLevel0);
  192. u32 i=0;
  193. while (i != mipmapLevel)
  194. {
  195. if (result.Width>1)
  196. result.Width >>= 1;
  197. if (result.Height>1)
  198. result.Height>>=1;
  199. ++i;
  200. if ( result.Width == 1 && result.Height == 1 && i < mipmapLevel )
  201. return core::dimension2du(0,0);
  202. }
  203. return result;
  204. }
  205. //! Get mipmaps data.
  206. /** Note that different mip levels are just behind each other in memory block.
  207. So if you just get level 1 you also have the data for all other levels.
  208. There is no level 0 - use getData to get the original image data.
  209. */
  210. void* getMipMapsData(irr::u32 mipLevel=1) const
  211. {
  212. if ( MipMapsData && mipLevel > 0)
  213. {
  214. size_t dataSize = 0;
  215. core::dimension2du mipSize(Size);
  216. u32 i = 1; // We want the start of data for this level, not end.
  217. while (i != mipLevel)
  218. {
  219. if (mipSize.Width > 1)
  220. mipSize.Width >>= 1;
  221. if (mipSize.Height > 1)
  222. mipSize.Height >>= 1;
  223. dataSize += getDataSizeFromFormat(Format, mipSize.Width, mipSize.Height);
  224. ++i;
  225. if ( mipSize.Width == 1 && mipSize.Height == 1 && i < mipLevel)
  226. return 0;
  227. }
  228. return MipMapsData + dataSize;
  229. }
  230. return 0;
  231. }
  232. //! Set mipmaps data.
  233. /** This method allows you to put custom mipmaps data for
  234. image.
  235. \param data A byte array with pixel color information
  236. \param ownForeignMemory If true, the image will use the data
  237. pointer directly and own it afterward. If false, the memory
  238. will by copied internally.
  239. \param deleteMemory Whether the memory is deallocated upon
  240. destruction. */
  241. void setMipMapsData(void* data, bool ownForeignMemory, bool deleteMemory)
  242. {
  243. if (data != MipMapsData)
  244. {
  245. if (DeleteMipMapsMemory)
  246. {
  247. Allocator.deallocate(MipMapsData);
  248. DeleteMipMapsMemory = false;
  249. }
  250. if (data)
  251. {
  252. if (ownForeignMemory)
  253. {
  254. MipMapsData = static_cast<u8*>(data);
  255. DeleteMipMapsMemory = deleteMemory;
  256. }
  257. else
  258. {
  259. size_t dataSize = 0;
  260. u32 width = Size.Width;
  261. u32 height = Size.Height;
  262. do
  263. {
  264. if (width > 1)
  265. width >>= 1;
  266. if (height > 1)
  267. height >>= 1;
  268. dataSize += getDataSizeFromFormat(Format, width, height);
  269. } while (width != 1 || height != 1);
  270. MipMapsData = Allocator.allocate(dataSize);
  271. memcpy(MipMapsData, data, dataSize);
  272. DeleteMipMapsMemory = true;
  273. }
  274. }
  275. else
  276. {
  277. MipMapsData = 0;
  278. }
  279. }
  280. }
  281. //! Returns a pixel
  282. virtual SColor getPixel(u32 x, u32 y) const = 0;
  283. //! Sets a pixel
  284. virtual void setPixel(u32 x, u32 y, const SColor &color, bool blend = false ) = 0;
  285. //! Copies the image into the target, scaling the image to fit
  286. /** NOTE: mipmaps are ignored */
  287. virtual void copyToScaling(void* target, u32 width, u32 height, ECOLOR_FORMAT format=ECF_A8R8G8B8, u32 pitch=0) =0;
  288. //! Copies the image into the target, scaling the image to fit
  289. /** NOTE: mipmaps are ignored */
  290. virtual void copyToScaling(IImage* target) =0;
  291. //! copies this surface into another
  292. /** NOTE: mipmaps are ignored */
  293. virtual void copyTo(IImage* target, const core::position2d<s32>& pos=core::position2d<s32>(0,0)) =0;
  294. //! copies this surface into another
  295. /** NOTE: mipmaps are ignored */
  296. virtual void copyTo(IImage* target, const core::position2d<s32>& pos, const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect=0) =0;
  297. //! copies this surface into another, using the alpha mask and cliprect and a color to add with
  298. /** NOTE: mipmaps are ignored
  299. \param combineAlpha - When true then combine alpha channels. When false replace target image alpha with source image alpha.
  300. */
  301. virtual void copyToWithAlpha(IImage* target, const core::position2d<s32>& pos,
  302. const core::rect<s32>& sourceRect, const SColor &color,
  303. const core::rect<s32>* clipRect = 0,
  304. bool combineAlpha=false) =0;
  305. //! copies this surface into another, scaling it to fit, applying a box filter
  306. /** NOTE: mipmaps are ignored */
  307. virtual void copyToScalingBoxFilter(IImage* target, s32 bias = 0, bool blend = false) = 0;
  308. //! Flips (mirrors) the image in one or two directions
  309. /** \param topBottom Flip around central x-axis (vertical flipping)
  310. \param leftRight Flip around central y-axis (typical mirror, horizontal flipping) */
  311. virtual void flip(bool topBottom, bool leftRight) = 0;
  312. //! fills the surface with given color
  313. virtual void fill(const SColor &color) =0;
  314. //! Inform whether the image is compressed
  315. IRR_DEPRECATED bool isCompressed() const
  316. {
  317. return IImage::isCompressedFormat(Format);
  318. }
  319. //! Check whether the image has MipMaps
  320. /** \return True if image has MipMaps, else false. */
  321. IRR_DEPRECATED bool hasMipMaps() const
  322. {
  323. return (getMipMapsData() != 0);
  324. }
  325. //! get the amount of Bits per Pixel of the given color format
  326. static u32 getBitsPerPixelFromFormat(const ECOLOR_FORMAT format)
  327. {
  328. switch(format)
  329. {
  330. case ECF_A1R5G5B5:
  331. return 16;
  332. case ECF_R5G6B5:
  333. return 16;
  334. case ECF_R8G8B8:
  335. return 24;
  336. case ECF_A8R8G8B8:
  337. return 32;
  338. case ECF_DXT1:
  339. return 16;
  340. case ECF_DXT2:
  341. case ECF_DXT3:
  342. case ECF_DXT4:
  343. case ECF_DXT5:
  344. return 32;
  345. case ECF_PVRTC_RGB2:
  346. return 12;
  347. case ECF_PVRTC_ARGB2:
  348. case ECF_PVRTC2_ARGB2:
  349. return 16;
  350. case ECF_PVRTC_RGB4:
  351. return 24;
  352. case ECF_PVRTC_ARGB4:
  353. case ECF_PVRTC2_ARGB4:
  354. return 32;
  355. case ECF_ETC1:
  356. case ECF_ETC2_RGB:
  357. return 24;
  358. case ECF_ETC2_ARGB:
  359. return 32;
  360. case ECF_D16:
  361. return 16;
  362. case ECF_D32:
  363. return 32;
  364. case ECF_D24S8:
  365. return 32;
  366. case ECF_R8:
  367. return 8;
  368. case ECF_R8G8:
  369. return 16;
  370. case ECF_R16:
  371. return 16;
  372. case ECF_R16G16:
  373. return 32;
  374. case ECF_R16F:
  375. return 16;
  376. case ECF_G16R16F:
  377. return 32;
  378. case ECF_A16B16G16R16F:
  379. return 64;
  380. case ECF_R32F:
  381. return 32;
  382. case ECF_G32R32F:
  383. return 64;
  384. case ECF_A32B32G32R32F:
  385. return 128;
  386. default:
  387. return 0;
  388. }
  389. }
  390. //! You should not create images where the result of getDataSizeFromFormat doesn't pass this function
  391. /** Note that CImage does not yet check for this, but going beyond this limit is not supported well.
  392. Image loaders should check for this.
  393. If you don't have the format yet then checking width*height*bytes_per_pixel is mostly fine, but make
  394. sure to work with size_t so it doesn't clip the result to u32 too early.
  395. \return true when dataSize is small enough that it should be fine. */
  396. static bool checkDataSizeLimit(size_t dataSize)
  397. {
  398. // 2gb for now. Could be we could do more on some platforms, but we still will run into
  399. // problems right now then for example in then color converter (which currently still uses
  400. // s32 for sizes).
  401. return (size_t)(s32)(dataSize) == dataSize;
  402. }
  403. //! calculate image data size in bytes for selected format, width and height.
  404. static size_t getDataSizeFromFormat(ECOLOR_FORMAT format, u32 width, u32 height)
  405. {
  406. size_t imageSize = 0;
  407. switch (format)
  408. {
  409. case ECF_DXT1:
  410. imageSize = (size_t)((width + 3) / 4) * ((height + 3) / 4) * 8;
  411. break;
  412. case ECF_DXT2:
  413. case ECF_DXT3:
  414. case ECF_DXT4:
  415. case ECF_DXT5:
  416. imageSize = (size_t)((width + 3) / 4) * ((height + 3) / 4) * 16;
  417. break;
  418. case ECF_PVRTC_RGB2:
  419. case ECF_PVRTC_ARGB2:
  420. imageSize = ((size_t)core::max_<u32>(width, 16) * core::max_<u32>(height, 8) * 2 + 7) / 8;
  421. break;
  422. case ECF_PVRTC_RGB4:
  423. case ECF_PVRTC_ARGB4:
  424. imageSize = ((size_t)core::max_<u32>(width, 8) * core::max_<u32>(height, 8) * 4 + 7) / 8;
  425. break;
  426. case ECF_PVRTC2_ARGB2:
  427. imageSize = (size_t)core::ceil32(width / 8.0f) * core::ceil32(height / 4.0f) * 8;
  428. break;
  429. case ECF_PVRTC2_ARGB4:
  430. case ECF_ETC1:
  431. case ECF_ETC2_RGB:
  432. imageSize = (size_t)core::ceil32(width / 4.0f) * core::ceil32(height / 4.0f) * 8;
  433. break;
  434. case ECF_ETC2_ARGB:
  435. imageSize = (size_t)core::ceil32(width / 4.0f) * core::ceil32(height / 4.0f) * 16;
  436. break;
  437. default: // uncompressed formats
  438. imageSize = (size_t)getBitsPerPixelFromFormat(format) / 8 * width;
  439. imageSize *= height;
  440. break;
  441. }
  442. return imageSize;
  443. }
  444. // Define to check for all compressed image formats cases in a switch
  445. #define IRR_CASE_IIMAGE_COMPRESSED_FORMAT\
  446. case ECF_DXT1:\
  447. case ECF_DXT2:\
  448. case ECF_DXT3:\
  449. case ECF_DXT4:\
  450. case ECF_DXT5:\
  451. case ECF_PVRTC_RGB2:\
  452. case ECF_PVRTC_ARGB2:\
  453. case ECF_PVRTC2_ARGB2:\
  454. case ECF_PVRTC_RGB4:\
  455. case ECF_PVRTC_ARGB4:\
  456. case ECF_PVRTC2_ARGB4:\
  457. case ECF_ETC1:\
  458. case ECF_ETC2_RGB:\
  459. case ECF_ETC2_ARGB:
  460. //! check if this is compressed color format
  461. static bool isCompressedFormat(const ECOLOR_FORMAT format)
  462. {
  463. switch(format)
  464. {
  465. IRR_CASE_IIMAGE_COMPRESSED_FORMAT
  466. return true;
  467. default:
  468. return false;
  469. }
  470. }
  471. //! check if the color format is only viable for depth/stencil textures
  472. static bool isDepthFormat(const ECOLOR_FORMAT format)
  473. {
  474. switch(format)
  475. {
  476. case ECF_D16:
  477. case ECF_D32:
  478. case ECF_D24S8:
  479. return true;
  480. default:
  481. return false;
  482. }
  483. }
  484. //! Check if the color format uses floating point values for pixels
  485. static bool isFloatingPointFormat(const ECOLOR_FORMAT format)
  486. {
  487. switch(format)
  488. {
  489. case ECF_R16F:
  490. case ECF_G16R16F:
  491. case ECF_A16B16G16R16F:
  492. case ECF_R32F:
  493. case ECF_G32R32F:
  494. case ECF_A32B32G32R32F:
  495. return true;
  496. default:
  497. break;
  498. }
  499. return false;
  500. }
  501. #if defined(PATCH_SUPERTUX_8_0_1_with_1_9_0)
  502. static bool isRenderTargetOnlyFormat(const ECOLOR_FORMAT format)
  503. {
  504. switch (format)
  505. {
  506. case ECF_A1R5G5B5:
  507. case ECF_R5G6B5:
  508. case ECF_R8G8B8:
  509. case ECF_A8R8G8B8:
  510. return false;
  511. default:
  512. return true;
  513. }
  514. }
  515. #endif
  516. protected:
  517. ECOLOR_FORMAT Format;
  518. core::dimension2d<u32> Size;
  519. u8* Data;
  520. u8* MipMapsData;
  521. u32 BytesPerPixel;
  522. u32 Pitch;
  523. bool DeleteMemory;
  524. bool DeleteMipMapsMemory;
  525. core::irrAllocator<u8> Allocator;
  526. #if defined(IRRLICHT_sRGB)
  527. int Format_sRGB;
  528. #endif
  529. };
  530. } // end namespace video
  531. } // end namespace irr
  532. #endif