Pixel.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /******************************************************************************
  2. @File Pixel.h
  3. @Title Console Log
  4. @Version @Version Name : etc_conversion.cpp
  5. @Copyright Copyright (C) Imagination Technologies Limited.
  6. @Platform ANSI
  7. @Description Pixel structs.
  8. ******************************************************************************/
  9. #ifndef PIXEL_H
  10. #define PIXEL_H
  11. #include <assert.h>
  12. namespace pvrtexlib
  13. {
  14. #ifdef __APPLE__
  15. /* The classes below are exported */
  16. #pragma GCC visibility push(default)
  17. #endif
  18. // note order of enums reflecta order of members of Pixel struct
  19. enum E_COLOUR_CHANNEL
  20. {
  21. ePIXEL_Red=0,
  22. ePIXEL_Green,
  23. ePIXEL_Blue,
  24. ePIXEL_Alpha,
  25. ePIXEL_None
  26. };
  27. // Pixel structures for standard pixel types
  28. template<typename channelType> struct Pixel
  29. {
  30. typedef channelType chanType;
  31. channelType Red, Green, Blue, Alpha;
  32. Pixel(){}
  33. Pixel(unsigned int u32Colour);
  34. Pixel(channelType nRed, channelType nGreen, channelType nBlue, channelType nAlpha)
  35. :Red(nRed),Green(nGreen),Blue(nBlue),Alpha(nAlpha){}
  36. unsigned int toUnsignedInt() const;
  37. inline channelType getMaxChannelValue() const;
  38. inline int getSizeInBytes() const;
  39. channelType& operator[](const unsigned int channel)
  40. {
  41. assert(channel<4);
  42. return ((channelType*)(&Red))[channel];
  43. }
  44. bool operator==(const Pixel& b)
  45. {
  46. return Red==b.Red && Green==b.Green && Blue==b.Blue && Alpha==b.Alpha;
  47. }
  48. Pixel<channelType> operator+(const Pixel& b)
  49. {
  50. return Pixel<channelType>(Red+b.Red,Green+b.Green,Blue+b.Blue,Alpha+b.Alpha);
  51. }
  52. Pixel<channelType>& operator+=(const Pixel<channelType>& b)
  53. {
  54. Red +=b.Red;Green+=b.Green;Blue+=b.Blue;Alpha+=b.Alpha;
  55. return *this;
  56. }
  57. Pixel<channelType>& operator+=(const channelType b)
  58. {
  59. Red +=b;Green+=b;Blue+=b;Alpha+=b;
  60. return *this;
  61. }
  62. Pixel<channelType>& operator-=(const channelType b)
  63. {
  64. Red -=b;Green-=b;Blue-=b;Alpha-=b;
  65. return *this;
  66. }
  67. Pixel<channelType> operator-(const Pixel& b)
  68. {
  69. return Pixel<channelType>(Red-b.Red,Green-b.Green,Blue-b.Blue,Alpha-b.Alpha);
  70. }
  71. Pixel<channelType> operator*(const Pixel<channelType>& b)
  72. {
  73. return Pixel<channelType>(Red*b.Red,Green*b.Green,Blue*b.Blue,Alpha*b.Alpha);
  74. }
  75. Pixel<channelType> operator*(const channelType b)
  76. {
  77. return Pixel<channelType>(Red*b,Green*b,Blue*b,Alpha*b);
  78. }
  79. Pixel<channelType>& operator*=(const channelType b)
  80. {
  81. Red *=b;Green*=b;Blue*=b;Alpha*=b;
  82. return *this;
  83. }
  84. Pixel<channelType> operator/(const Pixel<channelType>& b)
  85. {
  86. return Pixel<channelType>(Red/b.Red,Green/b.Green,Blue/b.Blue,Alpha/b.Alpha);
  87. }
  88. friend inline Pixel<channelType> operator*(const float f,const Pixel<channelType>& b)
  89. {
  90. return Pixel<channelType>((channelType)(f*(float)b.Red),
  91. (channelType)(f*(float)b.Green),
  92. (channelType)(f*(float)b.Blue),
  93. (channelType)(f*(float)b.Alpha));
  94. }
  95. /*******************************************************************************
  96. * Constructor
  97. * Description : Copy constructor
  98. *******************************************************************************/
  99. template<typename T>
  100. Pixel(const Pixel<T>& original)
  101. {
  102. Red = (channelType)original.Red;
  103. Green = (channelType)original.Green;
  104. Blue = (channelType)original.Blue;
  105. Alpha = (channelType)original.Alpha;
  106. }
  107. /*******************************************************************************
  108. * Constructor
  109. * Description : Assignment operator
  110. *******************************************************************************/
  111. template<typename T>
  112. Pixel& operator=(const Pixel<T>& sRHS)
  113. {
  114. Red = (channelType)sRHS.Red;
  115. Green = (channelType)sRHS.Green;
  116. Blue = (channelType)sRHS.Blue;
  117. Alpha = (channelType)sRHS.Alpha;
  118. return *this;
  119. }
  120. };
  121. template<typename channelType>
  122. inline int Pixel<channelType>::getSizeInBytes() const
  123. {
  124. return sizeof(channelType)*4;
  125. }
  126. // specialisations for uint8
  127. template<>
  128. inline Pixel<uint8>::Pixel(const unsigned int u32Colour)
  129. {
  130. Alpha = uint8(u32Colour>>24);
  131. Blue = uint8((u32Colour>>16)&0xff);
  132. Green = uint8((u32Colour>>8)&0xff);
  133. Red = uint8(u32Colour&0xff);
  134. }
  135. template<>
  136. inline unsigned int Pixel<uint8>::toUnsignedInt() const
  137. {
  138. return ((unsigned int)(Alpha)<<24)
  139. |((unsigned int) (Blue)<<16)
  140. |((unsigned int) (Green)<<8)
  141. |(unsigned int) (Red);
  142. }
  143. template<>
  144. inline uint8 Pixel<uint8>::getMaxChannelValue() const
  145. {
  146. return 0xFF;
  147. }
  148. // specialisations for uint16
  149. template<>
  150. inline uint16 Pixel<uint16>::getMaxChannelValue() const
  151. {
  152. return 0xFFFF;
  153. }
  154. template<>
  155. inline unsigned int Pixel<uint16>::toUnsignedInt() const
  156. {
  157. return ((unsigned int)(Alpha>>8)<<24)
  158. |((unsigned int) (Blue>>8)<<16)
  159. |((unsigned int) (Green>>8)<<8)
  160. |(unsigned int) (Red>>8);
  161. }
  162. // specialisations for uint32
  163. template<>
  164. inline uint32 Pixel<uint32>::getMaxChannelValue() const
  165. {
  166. return 0xFFFFFFFF;
  167. }
  168. template<>
  169. inline unsigned int Pixel<uint32>::toUnsignedInt() const
  170. {
  171. return ((unsigned int)(Alpha>>24)<<24)
  172. |((unsigned int) (Blue>>24)<<16)
  173. |((unsigned int) (Green>>24)<<8)
  174. |(unsigned int) (Red>>24);
  175. }
  176. // specialisations for float
  177. template<>
  178. inline float32 Pixel<float32>::getMaxChannelValue() const
  179. {
  180. return 1.0f;
  181. }
  182. template<>
  183. inline unsigned int Pixel<float32>::toUnsignedInt() const
  184. {
  185. return ((unsigned int)(Alpha*255.f)<<24)
  186. |((unsigned int) (Blue*255.f)<<16)
  187. |((unsigned int) (Green*255.f)<<8)
  188. |(unsigned int) (Red*255.f);
  189. }
  190. // convenience struct for PixelFormats
  191. template<typename channelType> struct PixelRGB
  192. {
  193. channelType Red;
  194. channelType Green;
  195. channelType Blue;
  196. // constructors
  197. PixelRGB(){}
  198. PixelRGB(const channelType tRed,
  199. const channelType tGreen,
  200. const channelType tBlue):
  201. Red(tRed),
  202. Green(tGreen),
  203. Blue(tBlue){}
  204. };
  205. // convenience struct for PixelFormats
  206. template<typename channelType> struct PixelRG
  207. {
  208. channelType Red;
  209. channelType Green;
  210. PixelRG(){}
  211. PixelRG(const channelType tRed,
  212. const channelType tGreen):
  213. Red(tRed),
  214. Green(tGreen){}
  215. };
  216. typedef Pixel<uint8> Pix8;
  217. typedef Pixel<uint16> Pix16;
  218. typedef Pixel<uint32> Pix32;
  219. #ifdef __APPLE__
  220. #pragma GCC visibility pop
  221. #endif
  222. }
  223. #endif // PIXEL_H
  224. /*****************************************************************************
  225. End of file (Pixel.h)
  226. *****************************************************************************/