CmPixelUtil.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef _PixelFormat_H__
  25. #define _PixelFormat_H__
  26. #include "CmPrerequisites.h"
  27. #include "CmPixelData.h"
  28. namespace CamelotFramework {
  29. /**
  30. * Some utility functions for packing and unpacking pixel data
  31. */
  32. class CM_EXPORT PixelUtil {
  33. public:
  34. /** Returns the size in bytes of an element of the given pixel format.
  35. @returns
  36. The size in bytes of an element. See Remarks.
  37. @remarks
  38. Passing PF_UNKNOWN will result in returning a size of 0 bytes.
  39. */
  40. static UINT32 getNumElemBytes( PixelFormat format );
  41. /** Returns the size in bits of an element of the given pixel format.
  42. @returns
  43. The size in bits of an element. See Remarks.
  44. @remarks
  45. Passing PF_UNKNOWN will result in returning a size of 0 bits.
  46. */
  47. static UINT32 getNumElemBits( PixelFormat format );
  48. /** Returns the size in memory of a region with the given extents and pixel
  49. format with consecutive memory layout.
  50. @param width
  51. The width of the area
  52. @param height
  53. The height of the area
  54. @param depth
  55. The depth of the area
  56. @param format
  57. The format of the area
  58. @returns
  59. The size in bytes
  60. @remarks
  61. In case that the format is non-compressed, this simply returns
  62. width*height*depth*PixelUtil::getNumElemBytes(format). In the compressed
  63. case, this does serious magic.
  64. */
  65. static UINT32 getMemorySize(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  66. /** Returns the property flags for this pixel format
  67. @returns
  68. A bitfield combination of PFF_HASALPHA, PFF_ISCOMPRESSED,
  69. PFF_FLOAT, PFF_DEPTH, PFF_NATIVEENDIAN, PFF_LUMINANCE
  70. @remarks
  71. This replaces the seperate functions for formatHasAlpha, formatIsFloat, ...
  72. */
  73. static unsigned int getFlags( PixelFormat format );
  74. /** Shortcut method to determine if the format has an alpha component */
  75. static bool hasAlpha(PixelFormat format);
  76. /** Shortcut method to determine if the format is floating point */
  77. static bool isFloatingPoint(PixelFormat format);
  78. /** Shortcut method to determine if the format is compressed */
  79. static bool isCompressed(PixelFormat format);
  80. /** Shortcut method to determine if the format is a depth format. */
  81. static bool isDepth(PixelFormat format);
  82. /** Shortcut method to determine if the format is in native endian format. */
  83. static bool isNativeEndian(PixelFormat format);
  84. /** Return wether a certain image extent is valid for this image format.
  85. @param width
  86. The width of the area
  87. @param height
  88. The height of the area
  89. @param depth
  90. The depth of the area
  91. @param format
  92. The format of the area
  93. @remarks For non-compressed formats, this is always true. For DXT formats,
  94. only sizes with a width and height multiple of 4 and depth 1 are allowed.
  95. */
  96. static bool isValidExtent(size_t width, size_t height, size_t depth, PixelFormat format);
  97. /** Gives the number of bits (RGBA) for a format. See remarks.
  98. @remarks For non-colour formats (dxt, depth) this returns [0,0,0,0].
  99. */
  100. static void getBitDepths(PixelFormat format, int rgba[4]);
  101. /** Gives the masks for the R, G, B and A component
  102. @note Only valid for native endian formats
  103. */
  104. static void getBitMasks(PixelFormat format, UINT32 rgba[4]);
  105. /** Gives the bit shifts for R, G, B and A component
  106. @note Only valid for native endian formats
  107. */
  108. static void getBitShifts(PixelFormat format, unsigned char rgba[4]);
  109. /** Gets the name of an image format
  110. */
  111. static String getFormatName(PixelFormat srcformat);
  112. /** Returns wether the format can be packed or unpacked with the packColour()
  113. and unpackColour() functions. This is generally not true for compressed and
  114. depth formats as they are special. It can only be true for formats with a
  115. fixed element size.
  116. @returns
  117. true if yes, otherwise false
  118. */
  119. static bool isAccessible(PixelFormat srcformat);
  120. /** Returns the component type for a certain pixel format. Returns PCT_BYTE
  121. in case there is no clear component type like with compressed formats.
  122. This is one of PCT_BYTE, PCT_SHORT, PCT_FLOAT16, PCT_FLOAT32.
  123. */
  124. static PixelComponentType getComponentType(PixelFormat fmt);
  125. /** Returns the component count for a certain pixel format. Returns 3(no alpha) or
  126. 4 (has alpha) in case there is no clear component type like with compressed formats.
  127. */
  128. static UINT32 getComponentCount(PixelFormat fmt);
  129. /** Gets the format from given name.
  130. @param name The string of format name
  131. @param accessibleOnly If true, non-accessible format will treat as invalid format,
  132. otherwise, all supported format are valid.
  133. @param caseSensitive Should be set true if string match should use case sensitivity.
  134. @returns The format match the format name, or PF_UNKNOWN if is invalid name.
  135. */
  136. static PixelFormat getFormatFromName(const String& name, bool accessibleOnly = false, bool caseSensitive = false);
  137. /** Gets the BNF expression of the pixel-formats.
  138. @note The string returned by this function is intented to use as a BNF expression
  139. to work with Compiler2Pass.
  140. @param accessibleOnly If true, only accessible pixel format will take into account, otherwise all
  141. pixel formats list in PixelFormat enumeration will being returned.
  142. @returns A string contains the BNF expression.
  143. */
  144. static String getBNFExpressionOfPixelFormats(bool accessibleOnly = false);
  145. /** Returns the maximum number of Mipmaps that can be generated until we reach
  146. the mininum format possible. This does not count the base level.
  147. @param width
  148. The width of the area
  149. @param height
  150. The height of the area
  151. @param depth
  152. The depth of the area
  153. @param format
  154. The format of the area
  155. @remarks
  156. In case that the format is non-compressed, this simply returns
  157. how many times we can divide this texture in 2 until we reach 1x1.
  158. For compressed formats, constraints apply on minimum size and alignment
  159. so this might differ.
  160. */
  161. static UINT32 getMaxMipmaps(UINT32 width, UINT32 height, UINT32 depth, PixelFormat format);
  162. /** Pack a colour value to memory
  163. @param colour The colour
  164. @param pf Pixelformat in which to write the colour
  165. @param dest Destination memory location
  166. */
  167. static void packColour(const Color &colour, const PixelFormat pf, void* dest);
  168. /** Pack a colour value to memory
  169. @param r,g,b,a The four colour components, range 0x00 to 0xFF
  170. @param pf Pixelformat in which to write the colour
  171. @param dest Destination memory location
  172. */
  173. static void packColour(const UINT8 r, const UINT8 g, const UINT8 b, const UINT8 a, const PixelFormat pf, void* dest);
  174. /** Pack a colour value to memory
  175. @param r,g,b,a The four colour components, range 0.0f to 1.0f
  176. (an exception to this case exists for floating point pixel
  177. formats, which don't clamp to 0.0f..1.0f)
  178. @param pf Pixelformat in which to write the colour
  179. @param dest Destination memory location
  180. */
  181. static void packColour(const float r, const float g, const float b, const float a, const PixelFormat pf, void* dest);
  182. /** Unpack a colour value from memory
  183. @param colour The colour is returned here
  184. @param pf Pixelformat in which to read the colour
  185. @param src Source memory location
  186. */
  187. static void unpackColour(Color *colour, PixelFormat pf, const void* src);
  188. /** Unpack a colour value from memory
  189. @param r,g,b,a The colour is returned here (as byte)
  190. @param pf Pixelformat in which to read the colour
  191. @param src Source memory location
  192. @remarks This function returns the colour components in 8 bit precision,
  193. this will lose precision when coming from PF_A2R10G10B10 or floating
  194. point formats.
  195. */
  196. static void unpackColour(UINT8 *r, UINT8 *g, UINT8 *b, UINT8 *a, PixelFormat pf, const void* src);
  197. /** Unpack a colour value from memory
  198. @param r,g,b,a The colour is returned here (as float)
  199. @param pf Pixelformat in which to read the colour
  200. @param src Source memory location
  201. */
  202. static void unpackColour(float *r, float *g, float *b, float *a, PixelFormat pf, const void* src);
  203. /** Convert pixels from one format to another. No dithering or filtering is being done. Converting
  204. from RGB to luminance takes the R channel.
  205. @param src PixelBox containing the source pixels, pitches and format
  206. @param dst PixelBox containing the destination pixels, pitches and format
  207. @remarks The source and destination boxes must have the same
  208. dimensions. In case the source and destination format match, a plain copy is done.
  209. */
  210. static void bulkPixelConversion(const PixelData &src, const PixelData &dst);
  211. enum Filter
  212. {
  213. FILTER_NEAREST,
  214. FILTER_LINEAR,
  215. FILTER_BILINEAR,
  216. FILTER_BOX,
  217. FILTER_TRIANGLE,
  218. FILTER_BICUBIC
  219. };
  220. /** Scale a 1D, 2D or 3D image volume.
  221. @param src PixelBox containing the source pointer, dimensions and format
  222. @param dst PixelBox containing the destination pointer, dimensions and format
  223. @param filter Which filter to use
  224. @remarks This function can do pixel format conversion in the process.
  225. @note dst and src can point to the same PixelBox object without any problem
  226. */
  227. static void scale(const PixelData &src, const PixelData &dst, Filter filter = FILTER_BILINEAR);
  228. /** Does gamma adjustment.
  229. @note
  230. Basic algo taken from Titan Engine, copyright (c) 2000 Ignacio
  231. Castano Iguado
  232. */
  233. static void applyGamma(UINT8 *buffer, float gamma, size_t size, UINT8 bpp);
  234. };
  235. /** @} */
  236. /** @} */
  237. }
  238. #endif