CmPixelData.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "CmBox.h"
  4. #include "CmIReflectable.h"
  5. namespace CamelotEngine
  6. {
  7. /** The pixel format used for images, textures, and render surfaces */
  8. enum PixelFormat
  9. {
  10. /// Unknown pixel format.
  11. PF_UNKNOWN = 0,
  12. /// 8-bit pixel format, all bits luminace.
  13. PF_L8 = 1,
  14. PF_BYTE_L = PF_L8,
  15. /// 16-bit pixel format, all bits luminace.
  16. PF_L16 = 2,
  17. PF_SHORT_L = PF_L16,
  18. /// 8-bit pixel format, all bits alpha.
  19. PF_A8 = 3,
  20. PF_BYTE_A = PF_A8,
  21. /// 8-bit pixel format, 4 bits alpha, 4 bits luminance.
  22. PF_A4L4 = 4,
  23. /// 2 byte pixel format, 1 byte luminance, 1 byte alpha
  24. PF_BYTE_LA = 5,
  25. /// 16-bit pixel format, 5 bits red, 6 bits green, 5 bits blue.
  26. PF_R5G6B5 = 6,
  27. /// 16-bit pixel format, 5 bits red, 6 bits green, 5 bits blue.
  28. PF_B5G6R5 = 7,
  29. /// 8-bit pixel format, 2 bits blue, 3 bits green, 3 bits red.
  30. PF_R3G3B2 = 31,
  31. /// 16-bit pixel format, 4 bits for alpha, red, green and blue.
  32. PF_A4R4G4B4 = 8,
  33. /// 16-bit pixel format, 5 bits for blue, green, red and 1 for alpha.
  34. PF_A1R5G5B5 = 9,
  35. /// 24-bit pixel format, 8 bits for red, green and blue.
  36. PF_R8G8B8 = 10,
  37. /// 24-bit pixel format, 8 bits for blue, green and red.
  38. PF_B8G8R8 = 11,
  39. /// 32-bit pixel format, 8 bits for alpha, red, green and blue.
  40. PF_A8R8G8B8 = 12,
  41. /// 32-bit pixel format, 8 bits for blue, green, red and alpha.
  42. PF_A8B8G8R8 = 13,
  43. /// 32-bit pixel format, 8 bits for blue, green, red and alpha.
  44. PF_B8G8R8A8 = 14,
  45. /// 32-bit pixel format, 8 bits for red, green, blue and alpha.
  46. PF_R8G8B8A8 = 28,
  47. /// 32-bit pixel format, 8 bits for red, 8 bits for green, 8 bits for blue
  48. /// like PF_A8R8G8B8, but alpha will get discarded
  49. PF_X8R8G8B8 = 26,
  50. /// 32-bit pixel format, 8 bits for blue, 8 bits for green, 8 bits for red
  51. /// like PF_A8B8G8R8, but alpha will get discarded
  52. PF_X8B8G8R8 = 27,
  53. #if CM_ENDIAN == CM_ENDIAN_BIG
  54. /// 3 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue
  55. PF_BYTE_RGB = PF_R8G8B8,
  56. /// 3 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red
  57. PF_BYTE_BGR = PF_B8G8R8,
  58. /// 4 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red and one byte for alpha
  59. PF_BYTE_BGRA = PF_B8G8R8A8,
  60. /// 4 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue, and one byte for alpha
  61. PF_BYTE_RGBA = PF_R8G8B8A8,
  62. #else
  63. /// 3 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue
  64. PF_BYTE_RGB = PF_B8G8R8,
  65. /// 3 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red
  66. PF_BYTE_BGR = PF_R8G8B8,
  67. /// 4 byte pixel format, 1 byte for blue, 1 byte for green, 1 byte for red and one byte for alpha
  68. PF_BYTE_BGRA = PF_A8R8G8B8,
  69. /// 4 byte pixel format, 1 byte for red, 1 byte for green, 1 byte for blue, and one byte for alpha
  70. PF_BYTE_RGBA = PF_A8B8G8R8,
  71. #endif
  72. /// 32-bit pixel format, 2 bits for alpha, 10 bits for red, green and blue.
  73. PF_A2R10G10B10 = 15,
  74. /// 32-bit pixel format, 10 bits for blue, green and red, 2 bits for alpha.
  75. PF_A2B10G10R10 = 16,
  76. /// DDS (DirectDraw Surface) DXT1 format
  77. PF_DXT1 = 17,
  78. /// DDS (DirectDraw Surface) DXT2 format
  79. PF_DXT2 = 18,
  80. /// DDS (DirectDraw Surface) DXT3 format
  81. PF_DXT3 = 19,
  82. /// DDS (DirectDraw Surface) DXT4 format
  83. PF_DXT4 = 20,
  84. /// DDS (DirectDraw Surface) DXT5 format
  85. PF_DXT5 = 21,
  86. // 16-bit pixel format, 16 bits (float) for red
  87. PF_FLOAT16_R = 32,
  88. // 48-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue
  89. PF_FLOAT16_RGB = 22,
  90. // 64-bit pixel format, 16 bits (float) for red, 16 bits (float) for green, 16 bits (float) for blue, 16 bits (float) for alpha
  91. PF_FLOAT16_RGBA = 23,
  92. // 32-bit pixel format, 32 bits (float) for red
  93. PF_FLOAT32_R = 33,
  94. // 96-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue
  95. PF_FLOAT32_RGB = 24,
  96. // 128-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue, 32 bits (float) for alpha
  97. PF_FLOAT32_RGBA = 25,
  98. // 32-bit, 2-channel s10e5 floating point pixel format, 16-bit green, 16-bit red
  99. PF_FLOAT16_GR = 35,
  100. // 64-bit, 2-channel floating point pixel format, 32-bit green, 32-bit red
  101. PF_FLOAT32_GR = 36,
  102. // Depth texture format
  103. PF_DEPTH = 29,
  104. // 64-bit pixel format, 16 bits for red, green, blue and alpha
  105. PF_SHORT_RGBA = 30,
  106. // 32-bit pixel format, 16-bit green, 16-bit red
  107. PF_SHORT_GR = 34,
  108. // 48-bit pixel format, 16 bits for red, green and blue
  109. PF_SHORT_RGB = 37,
  110. /// PVRTC (PowerVR) RGB 2 bpp
  111. PF_PVRTC_RGB2 = 38,
  112. /// PVRTC (PowerVR) RGBA 2 bpp
  113. PF_PVRTC_RGBA2 = 39,
  114. /// PVRTC (PowerVR) RGB 4 bpp
  115. PF_PVRTC_RGB4 = 40,
  116. /// PVRTC (PowerVR) RGBA 4 bpp
  117. PF_PVRTC_RGBA4 = 41,
  118. // Number of pixel formats currently defined
  119. PF_COUNT = 42
  120. };
  121. typedef vector<PixelFormat>::type PixelFormatList;
  122. /**
  123. * Flags defining some on/off properties of pixel formats
  124. */
  125. enum PixelFormatFlags {
  126. // This format has an alpha channel
  127. PFF_HASALPHA = 0x00000001,
  128. // This format is compressed. This invalidates the values in elemBytes,
  129. // elemBits and the bit counts as these might not be fixed in a compressed format.
  130. PFF_COMPRESSED = 0x00000002,
  131. // This is a floating point format
  132. PFF_FLOAT = 0x00000004,
  133. // This is a depth format (for depth textures)
  134. PFF_DEPTH = 0x00000008,
  135. // Format is in native endian. Generally true for the 16, 24 and 32 bits
  136. // formats which can be represented as machine integers.
  137. PFF_NATIVEENDIAN = 0x00000010,
  138. // This is an intensity format instead of a RGB one. The luminance
  139. // replaces R,G and B. (but not A)
  140. PFF_LUMINANCE = 0x00000020
  141. };
  142. /** Pixel component format */
  143. enum PixelComponentType
  144. {
  145. PCT_BYTE = 0, /// Byte per component (8 bit fixed 0.0..1.0)
  146. PCT_SHORT = 1, /// Short per component (16 bit fixed 0.0..1.0))
  147. PCT_FLOAT16 = 2, /// 16 bit float per component
  148. PCT_FLOAT32 = 3, /// 32 bit float per component
  149. PCT_COUNT = 4 /// Number of pixel types
  150. };
  151. /** A primitive describing a volume (3D), image (2D) or line (1D) of pixels in memory.
  152. In case of a rectangle, depth must be 1.
  153. Pixels are stored as a succession of "depth" slices, each containing "height" rows of
  154. "width" pixels.
  155. */
  156. class CM_UTILITY_EXPORT PixelData: public Box, public IReflectable
  157. {
  158. public:
  159. /// Parameter constructor for setting the members manually
  160. PixelData() {}
  161. ~PixelData()
  162. {
  163. if(ownsData && data != nullptr)
  164. delete[] data;
  165. data = nullptr;
  166. }
  167. /** Constructor providing extents in the form of a Box object. This constructor
  168. assumes the pixel data is laid out consecutively in memory. (this
  169. means row after row, slice after slice, with no space in between)
  170. @param extents Extents of the region defined by data
  171. @param pixelFormat Format of this buffer
  172. @param pixelData Pointer to the actual data
  173. @param ownsPixelData If true then PixelData owns the data buffer and will release it when destroyed.
  174. */
  175. PixelData(const Box &extents, PixelFormat pixelFormat, void *pixelData = 0, bool ownsPixelData = false):
  176. Box(extents), data(pixelData), format(pixelFormat), ownsData(ownsPixelData)
  177. {
  178. setConsecutive();
  179. }
  180. /** Constructor providing width, height and depth. This constructor
  181. assumes the pixel data is laid out consecutively in memory. (this
  182. means row after row, slice after slice, with no space in between)
  183. @param width Width of the region
  184. @param height Height of the region
  185. @param depth Depth of the region
  186. @param pixelFormat Format of this buffer
  187. @param pixelData Pointer to the actual data
  188. @param ownsPixelData If true then PixelData owns the data buffer and will release it when destroyed.
  189. */
  190. PixelData(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat,
  191. void *pixelData = 0, bool ownsPixelData = false):
  192. Box(0, 0, 0, width, height, depth),
  193. data(pixelData), format(pixelFormat), ownsData(ownsPixelData)
  194. {
  195. setConsecutive();
  196. }
  197. /// The data pointer
  198. void *data;
  199. /// The pixel format
  200. PixelFormat format;
  201. /** Number of elements between the leftmost pixel of one row and the left
  202. pixel of the next. This value must always be equal to getWidth() (consecutive)
  203. for compressed formats.
  204. */
  205. UINT32 rowPitch;
  206. /** Number of elements between the top left pixel of one (depth) slice and
  207. the top left pixel of the next. This can be a negative value. Must be a multiple of
  208. rowPitch. This value must always be equal to getWidth()*getHeight() (consecutive)
  209. for compressed formats.
  210. */
  211. UINT32 slicePitch;
  212. /**
  213. * @brief If true then PixelData owns the data buffer and will release it when destroyed.
  214. */
  215. bool ownsData;
  216. /** Set the rowPitch and slicePitch so that the buffer is laid out consecutive
  217. in memory.
  218. */
  219. void setConsecutive()
  220. {
  221. rowPitch = getWidth();
  222. slicePitch = getWidth()*getHeight();
  223. }
  224. /** Get the number of elements between one past the rightmost pixel of
  225. one row and the leftmost pixel of the next row. (IE this is zero if rows
  226. are consecutive).
  227. */
  228. UINT32 getRowSkip() const { return rowPitch - getWidth(); }
  229. /** Get the number of elements between one past the right bottom pixel of
  230. one slice and the left top pixel of the next slice. (IE this is zero if slices
  231. are consecutive).
  232. */
  233. UINT32 getSliceSkip() const { return slicePitch - (getHeight() * rowPitch); }
  234. /** Return whether this buffer is laid out consecutive in memory (ie the pitches
  235. are equal to the dimensions)
  236. */
  237. bool isConsecutive() const
  238. {
  239. return rowPitch == getWidth() && slicePitch == getWidth()*getHeight();
  240. }
  241. /** Return the size (in bytes) this image would take if it was
  242. laid out consecutive in memory
  243. */
  244. UINT32 getConsecutiveSize() const;
  245. /** Return a subvolume of this PixelBox.
  246. @param def Defines the bounds of the subregion to return
  247. @returns A pixel box describing the region and the data in it
  248. @remarks This function does not copy any data, it just returns
  249. a PixelBox object with a data pointer pointing somewhere inside
  250. the data of object.
  251. @throws Exception(ERR_INVALIDPARAMS) if def is not fully contained
  252. */
  253. PixelData getSubVolume(const Box &def) const;
  254. /**
  255. * Get colour value from a certain location in the PixelBox. The z coordinate
  256. * is only valid for cubemaps and volume textures. This uses the first (largest)
  257. * mipmap.
  258. */
  259. Color getColourAt(UINT32 x, UINT32 y, UINT32 z);
  260. /**
  261. * Set colour value at a certain location in the PixelBox. The z coordinate
  262. * is only valid for cubemaps and volume textures. This uses the first (largest)
  263. * mipmap.
  264. */
  265. void setColourAt(Color const &cv, UINT32 x, UINT32 y, UINT32 z);
  266. /************************************************************************/
  267. /* SERIALIZATION */
  268. /************************************************************************/
  269. public:
  270. friend class PixelDataRTTI;
  271. static RTTITypeBase* getRTTIStatic();
  272. virtual RTTITypeBase* getRTTI() const;
  273. };
  274. }