CmPixelData.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. // Depth stencil, 32bit depth, 8bit stencil + 24 unused
  119. PF_D32_S8X24 = 42,
  120. // Depth stencil, 24bit depth + 8bit stencil
  121. PF_D24S8 = 43,
  122. // Depth, 32bits
  123. PF_D32 = 44,
  124. // Depth, 16bits
  125. PF_D16 = 45,
  126. // Number of pixel formats currently defined
  127. PF_COUNT = 46
  128. };
  129. typedef vector<PixelFormat>::type PixelFormatList;
  130. /**
  131. * Flags defining some on/off properties of pixel formats
  132. */
  133. enum PixelFormatFlags {
  134. // This format has an alpha channel
  135. PFF_HASALPHA = 0x00000001,
  136. // This format is compressed. This invalidates the values in elemBytes,
  137. // elemBits and the bit counts as these might not be fixed in a compressed format.
  138. PFF_COMPRESSED = 0x00000002,
  139. // This is a floating point format
  140. PFF_FLOAT = 0x00000004,
  141. // This is a depth format (for depth textures)
  142. PFF_DEPTH = 0x00000008,
  143. // Format is in native endian. Generally true for the 16, 24 and 32 bits
  144. // formats which can be represented as machine integers.
  145. PFF_NATIVEENDIAN = 0x00000010,
  146. // This is an intensity format instead of a RGB one. The luminance
  147. // replaces R,G and B. (but not A)
  148. PFF_LUMINANCE = 0x00000020
  149. };
  150. /** Pixel component format */
  151. enum PixelComponentType
  152. {
  153. PCT_BYTE = 0, /// Byte per component (8 bit fixed 0.0..1.0)
  154. PCT_SHORT = 1, /// Short per component (16 bit fixed 0.0..1.0))
  155. PCT_FLOAT16 = 2, /// 16 bit float per component
  156. PCT_FLOAT32 = 3, /// 32 bit float per component
  157. PCT_COUNT = 4 /// Number of pixel types
  158. };
  159. /** A primitive describing a volume (3D), image (2D) or line (1D) of pixels in memory.
  160. In case of a rectangle, depth must be 1.
  161. Pixels are stored as a succession of "depth" slices, each containing "height" rows of
  162. "width" pixels.
  163. */
  164. class CM_UTILITY_EXPORT PixelData: public Box, public IReflectable
  165. {
  166. public:
  167. /// Parameter constructor for setting the members manually
  168. PixelData() {}
  169. ~PixelData()
  170. {
  171. if(ownsData && data != nullptr)
  172. delete[] data;
  173. data = nullptr;
  174. }
  175. /** Constructor providing extents in the form of a Box object. This constructor
  176. assumes the pixel data is laid out consecutively in memory. (this
  177. means row after row, slice after slice, with no space in between)
  178. @param extents Extents of the region defined by data
  179. @param pixelFormat Format of this buffer
  180. @param pixelData Pointer to the actual data
  181. @param ownsPixelData If true then PixelData owns the data buffer and will release it when destroyed.
  182. */
  183. PixelData(const Box &extents, PixelFormat pixelFormat, void *pixelData = 0, bool ownsPixelData = false):
  184. Box(extents), data(pixelData), format(pixelFormat), ownsData(ownsPixelData)
  185. {
  186. setConsecutive();
  187. }
  188. /** Constructor providing width, height and depth. This constructor
  189. assumes the pixel data is laid out consecutively in memory. (this
  190. means row after row, slice after slice, with no space in between)
  191. @param width Width of the region
  192. @param height Height of the region
  193. @param depth Depth of the region
  194. @param pixelFormat Format of this buffer
  195. @param pixelData Pointer to the actual data
  196. @param ownsPixelData If true then PixelData owns the data buffer and will release it when destroyed.
  197. */
  198. PixelData(UINT32 width, UINT32 height, UINT32 depth, PixelFormat pixelFormat,
  199. void *pixelData = 0, bool ownsPixelData = false):
  200. Box(0, 0, 0, width, height, depth),
  201. data(pixelData), format(pixelFormat), ownsData(ownsPixelData)
  202. {
  203. setConsecutive();
  204. }
  205. /// The data pointer
  206. void *data;
  207. /// The pixel format
  208. PixelFormat format;
  209. /** Number of elements between the leftmost pixel of one row and the left
  210. pixel of the next. This value must always be equal to getWidth() (consecutive)
  211. for compressed formats.
  212. */
  213. UINT32 rowPitch;
  214. /** Number of elements between the top left pixel of one (depth) slice and
  215. the top left pixel of the next. This can be a negative value. Must be a multiple of
  216. rowPitch. This value must always be equal to getWidth()*getHeight() (consecutive)
  217. for compressed formats.
  218. */
  219. UINT32 slicePitch;
  220. /**
  221. * @brief If true then PixelData owns the data buffer and will release it when destroyed.
  222. */
  223. bool ownsData;
  224. /** Set the rowPitch and slicePitch so that the buffer is laid out consecutive
  225. in memory.
  226. */
  227. void setConsecutive()
  228. {
  229. rowPitch = getWidth();
  230. slicePitch = getWidth()*getHeight();
  231. }
  232. /** Get the number of elements between one past the rightmost pixel of
  233. one row and the leftmost pixel of the next row. (IE this is zero if rows
  234. are consecutive).
  235. */
  236. UINT32 getRowSkip() const { return rowPitch - getWidth(); }
  237. /** Get the number of elements between one past the right bottom pixel of
  238. one slice and the left top pixel of the next slice. (IE this is zero if slices
  239. are consecutive).
  240. */
  241. UINT32 getSliceSkip() const { return slicePitch - (getHeight() * rowPitch); }
  242. PixelFormat getFormat() const { return format; }
  243. /** Return whether this buffer is laid out consecutive in memory (ie the pitches
  244. are equal to the dimensions)
  245. */
  246. bool isConsecutive() const
  247. {
  248. return rowPitch == getWidth() && slicePitch == getWidth()*getHeight();
  249. }
  250. /** Return the size (in bytes) this image would take if it was
  251. laid out consecutive in memory
  252. */
  253. UINT32 getConsecutiveSize() const;
  254. /** Return a subvolume of this PixelBox.
  255. @param def Defines the bounds of the subregion to return
  256. @returns A pixel box describing the region and the data in it
  257. @remarks This function does not copy any data, it just returns
  258. a PixelBox object with a data pointer pointing somewhere inside
  259. the data of object.
  260. @throws Exception(ERR_INVALIDPARAMS) if def is not fully contained
  261. */
  262. PixelData getSubVolume(const Box &def) const;
  263. /**
  264. * Get colour value from a certain location in the PixelBox. The z coordinate
  265. * is only valid for cubemaps and volume textures. This uses the first (largest)
  266. * mipmap.
  267. */
  268. Color getColourAt(UINT32 x, UINT32 y, UINT32 z);
  269. /**
  270. * Set colour value at a certain location in the PixelBox. The z coordinate
  271. * is only valid for cubemaps and volume textures. This uses the first (largest)
  272. * mipmap.
  273. */
  274. void setColourAt(Color const &cv, UINT32 x, UINT32 y, UINT32 z);
  275. /************************************************************************/
  276. /* SERIALIZATION */
  277. /************************************************************************/
  278. public:
  279. friend class PixelDataRTTI;
  280. static RTTITypeBase* getRTTIStatic();
  281. virtual RTTITypeBase* getRTTI() const;
  282. };
  283. }