PvrTcDecoder.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //============================================================================
  2. #include "PvrTcDecoder.h"
  3. #include "PvrTcPacket.h"
  4. #include "MortonTable.h"
  5. #include <assert.h>
  6. //============================================================================
  7. using namespace Javelin;
  8. using Data::MORTON_TABLE;
  9. //============================================================================
  10. inline unsigned PvrTcDecoder::GetMortonNumber(int x, int y)
  11. {
  12. return MORTON_TABLE[x >> 8] << 17 | MORTON_TABLE[y >> 8] << 16 | MORTON_TABLE[x & 0xFF] << 1 | MORTON_TABLE[y & 0xFF];
  13. }
  14. //============================================================================
  15. void PvrTcDecoder::DecodeRgb4Bpp(ColorRgb<unsigned char>* result, const Point2<int>& size, const void* data)
  16. {
  17. assert(size.x == size.y);
  18. const int blocks = size.x / 4;
  19. const int blockMask = blocks-1;
  20. const PvrTcPacket* packets = static_cast<const PvrTcPacket*>(data);
  21. for(int y = 0; y < blocks; ++y)
  22. {
  23. for(int x = 0; x < blocks; ++x)
  24. {
  25. const PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  26. unsigned mod = packet->modulationData;
  27. const unsigned char (*weights)[4] = PvrTcPacket::WEIGHTS + 4*packet->usePunchthroughAlpha;
  28. const unsigned char (*factor)[4] = PvrTcPacket::BILINEAR_FACTORS;
  29. for(int py = 0; py < 4; ++py)
  30. {
  31. const int yOffset = (py < 2) ? -1 : 0;
  32. const int y0 = (y + yOffset) & blockMask;
  33. const int y1 = (y0+1) & blockMask;
  34. for(int px = 0; px < 4; ++px)
  35. {
  36. const int xOffset = (px < 2) ? -1 : 0;
  37. const int x0 = (x + xOffset) & blockMask;
  38. const int x1 = (x0+1) & blockMask;
  39. const PvrTcPacket* p0 = packets + GetMortonNumber(x0, y0);
  40. const PvrTcPacket* p1 = packets + GetMortonNumber(x1, y0);
  41. const PvrTcPacket* p2 = packets + GetMortonNumber(x0, y1);
  42. const PvrTcPacket* p3 = packets + GetMortonNumber(x1, y1);
  43. ColorRgb<int> ca = p0->GetColorRgbA() * (*factor)[0] +
  44. p1->GetColorRgbA() * (*factor)[1] +
  45. p2->GetColorRgbA() * (*factor)[2] +
  46. p3->GetColorRgbA() * (*factor)[3];
  47. ColorRgb<int> cb = p0->GetColorRgbB() * (*factor)[0] +
  48. p1->GetColorRgbB() * (*factor)[1] +
  49. p2->GetColorRgbB() * (*factor)[2] +
  50. p3->GetColorRgbB() * (*factor)[3];
  51. const unsigned char* w = weights[mod&3];
  52. ColorRgb<unsigned char> c;
  53. c.r = (ca.r * w[0] + cb.r * w[1]) >> 7;
  54. c.g = (ca.g * w[0] + cb.g * w[1]) >> 7;
  55. c.b = (ca.b * w[0] + cb.b * w[1]) >> 7;
  56. result[(py+y*4)*size.x + (px+x*4)] = c;
  57. mod >>= 2;
  58. factor++;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. void PvrTcDecoder::DecodeRgba4Bpp(ColorRgba<unsigned char>* result, const Point2<int>& size, const void* data)
  65. {
  66. assert(size.x == size.y);
  67. const int blocks = size.x / 4;
  68. const int blockMask = blocks-1;
  69. const PvrTcPacket* packets = static_cast<const PvrTcPacket*>(data);
  70. for(int y = 0; y < blocks; ++y)
  71. {
  72. for(int x = 0; x < blocks; ++x)
  73. {
  74. const PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  75. unsigned mod = packet->modulationData;
  76. const unsigned char (*weights)[4] = PvrTcPacket::WEIGHTS + 4*packet->usePunchthroughAlpha;
  77. const unsigned char (*factor)[4] = PvrTcPacket::BILINEAR_FACTORS;
  78. for(int py = 0; py < 4; ++py)
  79. {
  80. const int yOffset = (py < 2) ? -1 : 0;
  81. const int y0 = (y + yOffset) & blockMask;
  82. const int y1 = (y0+1) & blockMask;
  83. for(int px = 0; px < 4; ++px)
  84. {
  85. const int xOffset = (px < 2) ? -1 : 0;
  86. const int x0 = (x + xOffset) & blockMask;
  87. const int x1 = (x0+1) & blockMask;
  88. const PvrTcPacket* p0 = packets + GetMortonNumber(x0, y0);
  89. const PvrTcPacket* p1 = packets + GetMortonNumber(x1, y0);
  90. const PvrTcPacket* p2 = packets + GetMortonNumber(x0, y1);
  91. const PvrTcPacket* p3 = packets + GetMortonNumber(x1, y1);
  92. ColorRgba<int> ca = p0->GetColorRgbaA() * (*factor)[0] +
  93. p1->GetColorRgbaA() * (*factor)[1] +
  94. p2->GetColorRgbaA() * (*factor)[2] +
  95. p3->GetColorRgbaA() * (*factor)[3];
  96. ColorRgba<int> cb = p0->GetColorRgbaB() * (*factor)[0] +
  97. p1->GetColorRgbaB() * (*factor)[1] +
  98. p2->GetColorRgbaB() * (*factor)[2] +
  99. p3->GetColorRgbaB() * (*factor)[3];
  100. const unsigned char* w = weights[mod&3];
  101. ColorRgba<unsigned char> c;
  102. c.r = (ca.r * w[0] + cb.r * w[1]) >> 7;
  103. c.g = (ca.g * w[0] + cb.g * w[1]) >> 7;
  104. c.b = (ca.b * w[0] + cb.b * w[1]) >> 7;
  105. c.a = (ca.a * w[2] + cb.a * w[3]) >> 7;
  106. result[(py+y*4)*size.x + (px+x*4)] = c;
  107. mod >>= 2;
  108. factor++;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. //============================================================================