CmPixelUtil.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "CmPrerequisitesUtil.h"
  27. #include "CmPixelData.h"
  28. namespace CamelotEngine {
  29. /**
  30. * Some utility functions for packing and unpacking pixel data
  31. */
  32. class CM_UTILITY_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. /** Shortcut method to determine if the format is a luminance format. */
  85. static bool isLuminance(PixelFormat format);
  86. /** Return wether a certain image extent is valid for this image format.
  87. @param width
  88. The width of the area
  89. @param height
  90. The height of the area
  91. @param depth
  92. The depth of the area
  93. @param format
  94. The format of the area
  95. @remarks For non-compressed formats, this is always true. For DXT formats,
  96. only sizes with a width and height multiple of 4 and depth 1 are allowed.
  97. */
  98. static bool isValidExtent(size_t width, size_t height, size_t depth, PixelFormat format);
  99. /** Gives the number of bits (RGBA) for a format. See remarks.
  100. @remarks For non-colour formats (dxt, depth) this returns [0,0,0,0].
  101. */
  102. static void getBitDepths(PixelFormat format, int rgba[4]);
  103. /** Gives the masks for the R, G, B and A component
  104. @note Only valid for native endian formats
  105. */
  106. static void getBitMasks(PixelFormat format, UINT32 rgba[4]);
  107. /** Gives the bit shifts for R, G, B and A component
  108. @note Only valid for native endian formats
  109. */
  110. static void getBitShifts(PixelFormat format, unsigned char rgba[4]);
  111. /** Gets the name of an image format
  112. */
  113. static String getFormatName(PixelFormat srcformat);
  114. /** Returns wether the format can be packed or unpacked with the packColour()
  115. and unpackColour() functions. This is generally not true for compressed and
  116. depth formats as they are special. It can only be true for formats with a
  117. fixed element size.
  118. @returns
  119. true if yes, otherwise false
  120. */
  121. static bool isAccessible(PixelFormat srcformat);
  122. /** Returns the component type for a certain pixel format. Returns PCT_BYTE
  123. in case there is no clear component type like with compressed formats.
  124. This is one of PCT_BYTE, PCT_SHORT, PCT_FLOAT16, PCT_FLOAT32.
  125. */
  126. static PixelComponentType getComponentType(PixelFormat fmt);
  127. /** Returns the component count for a certain pixel format. Returns 3(no alpha) or
  128. 4 (has alpha) in case there is no clear component type like with compressed formats.
  129. */
  130. static UINT32 getComponentCount(PixelFormat fmt);
  131. /** Gets the format from given name.
  132. @param name The string of format name
  133. @param accessibleOnly If true, non-accessible format will treat as invalid format,
  134. otherwise, all supported format are valid.
  135. @param caseSensitive Should be set true if string match should use case sensitivity.
  136. @returns The format match the format name, or PF_UNKNOWN if is invalid name.
  137. */
  138. static PixelFormat getFormatFromName(const String& name, bool accessibleOnly = false, bool caseSensitive = false);
  139. /** Gets the BNF expression of the pixel-formats.
  140. @note The string returned by this function is intented to use as a BNF expression
  141. to work with Compiler2Pass.
  142. @param accessibleOnly If true, only accessible pixel format will take into account, otherwise all
  143. pixel formats list in PixelFormat enumeration will being returned.
  144. @returns A string contains the BNF expression.
  145. */
  146. static String getBNFExpressionOfPixelFormats(bool accessibleOnly = false);
  147. /** Returns the similar format but acoording with given bit depths.
  148. @param fmt The original foamt.
  149. @param integerBits Preferred bit depth (pixel bits) for integer pixel format.
  150. Available values: 0, 16 and 32, where 0 (the default) means as it is.
  151. @param floatBits Preferred bit depth (channel bits) for float pixel format.
  152. Available values: 0, 16 and 32, where 0 (the default) means as it is.
  153. @returns The format that similar original format with bit depth according
  154. with preferred bit depth, or original format if no convertion occuring.
  155. */
  156. static PixelFormat getFormatForBitDepths(PixelFormat fmt, UINT16 integerBits, UINT16 floatBits);
  157. /** Pack a colour value to memory
  158. @param colour The colour
  159. @param pf Pixelformat in which to write the colour
  160. @param dest Destination memory location
  161. */
  162. static void packColour(const Color &colour, const PixelFormat pf, void* dest);
  163. /** Pack a colour value to memory
  164. @param r,g,b,a The four colour components, range 0x00 to 0xFF
  165. @param pf Pixelformat in which to write the colour
  166. @param dest Destination memory location
  167. */
  168. static void packColour(const UINT8 r, const UINT8 g, const UINT8 b, const UINT8 a, const PixelFormat pf, void* dest);
  169. /** Pack a colour value to memory
  170. @param r,g,b,a The four colour components, range 0.0f to 1.0f
  171. (an exception to this case exists for floating point pixel
  172. formats, which don't clamp to 0.0f..1.0f)
  173. @param pf Pixelformat in which to write the colour
  174. @param dest Destination memory location
  175. */
  176. static void packColour(const float r, const float g, const float b, const float a, const PixelFormat pf, void* dest);
  177. /** Unpack a colour value from memory
  178. @param colour The colour is returned here
  179. @param pf Pixelformat in which to read the colour
  180. @param src Source memory location
  181. */
  182. static void unpackColour(Color *colour, PixelFormat pf, const void* src);
  183. /** Unpack a colour value from memory
  184. @param r,g,b,a The colour is returned here (as byte)
  185. @param pf Pixelformat in which to read the colour
  186. @param src Source memory location
  187. @remarks This function returns the colour components in 8 bit precision,
  188. this will lose precision when coming from PF_A2R10G10B10 or floating
  189. point formats.
  190. */
  191. static void unpackColour(UINT8 *r, UINT8 *g, UINT8 *b, UINT8 *a, PixelFormat pf, const void* src);
  192. /** Unpack a colour value from memory
  193. @param r,g,b,a The colour is returned here (as float)
  194. @param pf Pixelformat in which to read the colour
  195. @param src Source memory location
  196. */
  197. static void unpackColour(float *r, float *g, float *b, float *a, PixelFormat pf, const void* src);
  198. /** Convert consecutive pixels from one format to another. No dithering or filtering is being done.
  199. Converting from RGB to luminance takes the R channel. In case the source and destination format match,
  200. just a copy is done.
  201. @param src Pointer to source region
  202. @param srcFormat Pixel format of source region
  203. @param dst Pointer to destination region
  204. @param dstFormat Pixel format of destination region
  205. */
  206. static void bulkPixelConversion(void *src, PixelFormat srcFormat, void *dest, PixelFormat dstFormat, unsigned int count);
  207. /** Convert pixels from one format to another. No dithering or filtering is being done. Converting
  208. from RGB to luminance takes the R channel.
  209. @param src PixelBox containing the source pixels, pitches and format
  210. @param dst PixelBox containing the destination pixels, pitches and format
  211. @remarks The source and destination boxes must have the same
  212. dimensions. In case the source and destination format match, a plain copy is done.
  213. */
  214. static void bulkPixelConversion(const PixelData &src, const PixelData &dst);
  215. enum Filter
  216. {
  217. FILTER_NEAREST,
  218. FILTER_LINEAR,
  219. FILTER_BILINEAR,
  220. FILTER_BOX,
  221. FILTER_TRIANGLE,
  222. FILTER_BICUBIC
  223. };
  224. /** Scale a 1D, 2D or 3D image volume.
  225. @param src PixelBox containing the source pointer, dimensions and format
  226. @param dst PixelBox containing the destination pointer, dimensions and format
  227. @param filter Which filter to use
  228. @remarks This function can do pixel format conversion in the process.
  229. @note dst and src can point to the same PixelBox object without any problem
  230. */
  231. static void scale(const PixelData &src, const PixelData &dst, Filter filter = FILTER_BILINEAR);
  232. /** Does gamma adjustment.
  233. @note
  234. Basic algo taken from Titan Engine, copyright (c) 2000 Ignacio
  235. Castano Iguado
  236. */
  237. static void applyGamma(UINT8 *buffer, float gamma, size_t size, UINT8 bpp);
  238. };
  239. /** @} */
  240. /** @} */
  241. }
  242. #endif