PvrTcPacket.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //============================================================================
  2. #include "PvrTcPacket.h"
  3. #include "BitScale.h"
  4. //============================================================================
  5. using namespace Javelin;
  6. //============================================================================
  7. const unsigned char PvrTcPacket::BILINEAR_FACTORS[16][4] =
  8. {
  9. { 4, 4, 4, 4 },
  10. { 2, 6, 2, 6 },
  11. { 8, 0, 8, 0 },
  12. { 6, 2, 6, 2 },
  13. { 2, 2, 6, 6 },
  14. { 1, 3, 3, 9 },
  15. { 4, 0, 12, 0 },
  16. { 3, 1, 9, 3 },
  17. { 8, 8, 0, 0 },
  18. { 4, 12, 0, 0 },
  19. { 16, 0, 0, 0 },
  20. { 12, 4, 0, 0 },
  21. { 6, 6, 2, 2 },
  22. { 3, 9, 1, 3 },
  23. { 12, 0, 4, 0 },
  24. { 9, 3, 3, 1 },
  25. };
  26. // Weights are { colorA, colorB, alphaA, alphaB }
  27. const unsigned char PvrTcPacket::WEIGHTS[8][4] =
  28. {
  29. // Weights for Mode=0
  30. { 8, 0, 8, 0 },
  31. { 5, 3, 5, 3 },
  32. { 3, 5, 3, 5 },
  33. { 0, 8, 0, 8 },
  34. // Weights for Mode=1
  35. { 8, 0, 8, 0 },
  36. { 4, 4, 4, 4 },
  37. { 4, 4, 0, 0 },
  38. { 0, 8, 0, 8 },
  39. };
  40. //============================================================================
  41. ColorRgb<int> PvrTcPacket::GetColorRgbA() const
  42. {
  43. if(colorAIsOpaque)
  44. {
  45. unsigned char r = colorA >> 9;
  46. unsigned char g = colorA >> 4 & 0x1f;
  47. unsigned char b = colorA & 0xf;
  48. return ColorRgb<int>(Data::BITSCALE_5_TO_8[r],
  49. Data::BITSCALE_5_TO_8[g],
  50. Data::BITSCALE_4_TO_8[b]);
  51. }
  52. else
  53. {
  54. unsigned char r = (colorA >> 7) & 0xf;
  55. unsigned char g = (colorA >> 3) & 0xf;
  56. unsigned char b = colorA & 7;
  57. return ColorRgb<int>(Data::BITSCALE_4_TO_8[r],
  58. Data::BITSCALE_4_TO_8[g],
  59. Data::BITSCALE_3_TO_8[b]);
  60. }
  61. }
  62. ColorRgb<int> PvrTcPacket::GetColorRgbB() const
  63. {
  64. if(colorBIsOpaque)
  65. {
  66. unsigned char r = colorB >> 10;
  67. unsigned char g = colorB >> 5 & 0x1f;
  68. unsigned char b = colorB & 0x1f;
  69. return ColorRgb<int>(Data::BITSCALE_5_TO_8[r],
  70. Data::BITSCALE_5_TO_8[g],
  71. Data::BITSCALE_5_TO_8[b]);
  72. }
  73. else
  74. {
  75. unsigned char r = colorB >> 8 & 0xf;
  76. unsigned char g = colorB >> 4 & 0xf;
  77. unsigned char b = colorB & 0xf;
  78. return ColorRgb<int>(Data::BITSCALE_4_TO_8[r],
  79. Data::BITSCALE_4_TO_8[g],
  80. Data::BITSCALE_4_TO_8[b]);
  81. }
  82. }
  83. ColorRgba<int> PvrTcPacket::GetColorRgbaA() const
  84. {
  85. if(colorAIsOpaque)
  86. {
  87. unsigned char r = colorA >> 9;
  88. unsigned char g = colorA >> 4 & 0x1f;
  89. unsigned char b = colorA & 0xf;
  90. return ColorRgba<int>(Data::BITSCALE_5_TO_8[r],
  91. Data::BITSCALE_5_TO_8[g],
  92. Data::BITSCALE_4_TO_8[b],
  93. 255);
  94. }
  95. else
  96. {
  97. unsigned char a = colorA >> 11 & 7;
  98. unsigned char r = colorA >> 7 & 0xf;
  99. unsigned char g = colorA >> 3 & 0xf;
  100. unsigned char b = colorA & 7;
  101. return ColorRgba<int>(Data::BITSCALE_4_TO_8[r],
  102. Data::BITSCALE_4_TO_8[g],
  103. Data::BITSCALE_3_TO_8[b],
  104. Data::BITSCALE_3_TO_8[a]);
  105. }
  106. }
  107. ColorRgba<int> PvrTcPacket::GetColorRgbaB() const
  108. {
  109. if(colorBIsOpaque)
  110. {
  111. unsigned char r = colorB >> 10;
  112. unsigned char g = colorB >> 5 & 0x1f;
  113. unsigned char b = colorB & 0x1f;
  114. return ColorRgba<int>(Data::BITSCALE_5_TO_8[r],
  115. Data::BITSCALE_5_TO_8[g],
  116. Data::BITSCALE_5_TO_8[b],
  117. 255);
  118. }
  119. else
  120. {
  121. unsigned char a = colorB >> 12 & 7;
  122. unsigned char r = colorB >> 8 & 0xf;
  123. unsigned char g = colorB >> 4 & 0xf;
  124. unsigned char b = colorB & 0xf;
  125. return ColorRgba<int>(Data::BITSCALE_4_TO_8[r],
  126. Data::BITSCALE_4_TO_8[g],
  127. Data::BITSCALE_4_TO_8[b],
  128. Data::BITSCALE_3_TO_8[a]);
  129. }
  130. }
  131. //============================================================================
  132. void PvrTcPacket::SetColorA(const ColorRgb<unsigned char>& c)
  133. {
  134. int r = Data::BITSCALE_8_TO_5_FLOOR[c.r];
  135. int g = Data::BITSCALE_8_TO_5_FLOOR[c.g];
  136. int b = Data::BITSCALE_8_TO_4_FLOOR[c.b];
  137. colorA = r<<9 | g<<4 | b;
  138. colorAIsOpaque = true;
  139. }
  140. void PvrTcPacket::SetColorB(const ColorRgb<unsigned char>& c)
  141. {
  142. int r = Data::BITSCALE_8_TO_5_CEIL[c.r];
  143. int g = Data::BITSCALE_8_TO_5_CEIL[c.g];
  144. int b = Data::BITSCALE_8_TO_5_CEIL[c.b];
  145. colorB = r<<10 | g<<5 | b;
  146. colorBIsOpaque = true;
  147. }
  148. void PvrTcPacket::SetColorA(const ColorRgba<unsigned char>& c)
  149. {
  150. int a = Data::BITSCALE_8_TO_3_FLOOR[c.a];
  151. if(a == 7)
  152. {
  153. int r = Data::BITSCALE_8_TO_5_FLOOR[c.r];
  154. int g = Data::BITSCALE_8_TO_5_FLOOR[c.g];
  155. int b = Data::BITSCALE_8_TO_4_FLOOR[c.b];
  156. colorA = r<<9 | g<<4 | b;
  157. colorAIsOpaque = true;
  158. }
  159. else
  160. {
  161. int r = Data::BITSCALE_8_TO_4_FLOOR[c.r];
  162. int g = Data::BITSCALE_8_TO_4_FLOOR[c.g];
  163. int b = Data::BITSCALE_8_TO_3_FLOOR[c.b];
  164. colorA = a<<11 | r<<7 | g<<3 | b;
  165. colorAIsOpaque = false;
  166. }
  167. }
  168. void PvrTcPacket::SetColorB(const ColorRgba<unsigned char>& c)
  169. {
  170. int a = Data::BITSCALE_8_TO_3_CEIL[c.a];
  171. if(a == 7)
  172. {
  173. int r = Data::BITSCALE_8_TO_5_CEIL[c.r];
  174. int g = Data::BITSCALE_8_TO_5_CEIL[c.g];
  175. int b = Data::BITSCALE_8_TO_5_CEIL[c.b];
  176. colorB = r<<10 | g<<5 | b;
  177. colorBIsOpaque = true;
  178. }
  179. else
  180. {
  181. int r = Data::BITSCALE_8_TO_4_CEIL[c.r];
  182. int g = Data::BITSCALE_8_TO_4_CEIL[c.g];
  183. int b = Data::BITSCALE_8_TO_4_CEIL[c.b];
  184. colorB = a<<12 | r<<8 | g<<4 | b;
  185. colorBIsOpaque = false;
  186. }
  187. }
  188. //============================================================================