PvrTcEncoder.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //============================================================================
  2. #include "PvrTcEncoder.h"
  3. #include "AlphaBitmap.h"
  4. #include "PvrTcPacket.h"
  5. #include "RgbBitmap.h"
  6. #include "RgbaBitmap.h"
  7. #include "MortonTable.h"
  8. #include "BitUtility.h"
  9. #include "Interval.h"
  10. #include <assert.h>
  11. #include <math.h>
  12. #include <stdint.h>
  13. //============================================================================
  14. using namespace Javelin;
  15. using Data::MORTON_TABLE;
  16. //============================================================================
  17. static const unsigned char MODULATION_LUT[16] =
  18. {
  19. 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3
  20. };
  21. //============================================================================
  22. inline unsigned PvrTcEncoder::GetMortonNumber(int x, int y)
  23. {
  24. return MORTON_TABLE[x >> 8] << 17 | MORTON_TABLE[y >> 8] << 16 | MORTON_TABLE[x & 0xFF] << 1 | MORTON_TABLE[y & 0xFF];
  25. }
  26. //============================================================================
  27. void PvrTcEncoder::EncodeAlpha2Bpp(void* result, const AlphaBitmap& bitmap)
  28. {
  29. int size = bitmap.GetBitmapWidth();
  30. assert(size == bitmap.GetBitmapHeight());
  31. assert(BitUtility::IsPowerOf2(size));
  32. // Blocks in each dimension.
  33. int xBlocks = size/8;
  34. int yBlocks = size/4;
  35. const unsigned char* bitmapData = bitmap.GetRawData();
  36. PvrTcPacket* packets = static_cast<PvrTcPacket*>(result);
  37. for(int y = 0; y < yBlocks; ++y)
  38. {
  39. for(int x = 0; x < xBlocks; ++x)
  40. {
  41. PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  42. packet->usePunchthroughAlpha = 0;
  43. packet->colorAIsOpaque = 0;
  44. packet->colorA = 0x7ff; // White, with 0 alpha
  45. packet->colorBIsOpaque = 1;
  46. packet->colorB = 0x7fff; // White with full alpha
  47. const unsigned char* blockBitmapData = &bitmapData[y*4*size + x*8];
  48. uint32_t modulationData = 0;
  49. for(int py = 0; py < 4; ++py)
  50. {
  51. const unsigned char* rowBitmapData = blockBitmapData;
  52. for(int px = 0; px < 8; ++px)
  53. {
  54. unsigned char pixel = *rowBitmapData++;
  55. modulationData = BitUtility::RotateRight(modulationData | (pixel >> 7), 1);
  56. }
  57. blockBitmapData += size;
  58. }
  59. packet->modulationData = modulationData;
  60. }
  61. }
  62. }
  63. void PvrTcEncoder::EncodeAlpha4Bpp(void* result, const AlphaBitmap& bitmap)
  64. {
  65. int size = bitmap.GetBitmapWidth();
  66. assert(size == bitmap.GetBitmapHeight());
  67. assert(BitUtility::IsPowerOf2(size));
  68. // Blocks in each dimension.
  69. int blocks = size/4;
  70. const unsigned char* bitmapData = bitmap.GetRawData();
  71. PvrTcPacket* packets = static_cast<PvrTcPacket*>(result);
  72. for(int y = 0; y < blocks; ++y)
  73. {
  74. for(int x = 0; x < blocks; ++x)
  75. {
  76. PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  77. packet->usePunchthroughAlpha = 0;
  78. packet->colorAIsOpaque = 0;
  79. packet->colorA = 0x7ff; // White, with 0 alpha
  80. packet->colorBIsOpaque = 1;
  81. packet->colorB = 0x7fff; // White with full alpha
  82. const unsigned char* blockBitmapData = &bitmapData[(y*size + x)*4];
  83. uint32_t modulationData = 0;
  84. for(int py = 0; py < 4; ++py)
  85. {
  86. const unsigned char* rowBitmapData = blockBitmapData;
  87. for(int px = 0; px < 4; ++px)
  88. {
  89. unsigned char pixel = *rowBitmapData++;
  90. modulationData = BitUtility::RotateRight(modulationData | MODULATION_LUT[pixel>>4], 2);
  91. }
  92. blockBitmapData += size;
  93. }
  94. packet->modulationData = modulationData;
  95. }
  96. }
  97. }
  98. //============================================================================
  99. typedef Interval<ColorRgb<unsigned char> > ColorRgbBoundingBox;
  100. static void CalculateBoundingBox(ColorRgbBoundingBox& cbb, const RgbBitmap& bitmap, int blockX, int blockY)
  101. {
  102. int size = bitmap.GetBitmapWidth();
  103. const ColorRgb<unsigned char>* data = bitmap.GetData() + blockY * 4 * size + blockX * 4;
  104. cbb.min = data[0];
  105. cbb.max = data[0];
  106. cbb |= data[1];
  107. cbb |= data[2];
  108. cbb |= data[3];
  109. cbb |= data[size];
  110. cbb |= data[size+1];
  111. cbb |= data[size+2];
  112. cbb |= data[size+3];
  113. cbb |= data[2*size];
  114. cbb |= data[2*size+1];
  115. cbb |= data[2*size+2];
  116. cbb |= data[2*size+3];
  117. cbb |= data[3*size];
  118. cbb |= data[3*size+1];
  119. cbb |= data[3*size+2];
  120. cbb |= data[3*size+3];
  121. }
  122. void PvrTcEncoder::EncodeRgb4Bpp(void* result, const RgbBitmap& bitmap)
  123. {
  124. assert(bitmap.GetBitmapWidth() == bitmap.GetBitmapHeight());
  125. assert(BitUtility::IsPowerOf2(bitmap.GetBitmapWidth()));
  126. const int size = bitmap.GetBitmapWidth();
  127. const int blocks = size / 4;
  128. const int blockMask = blocks-1;
  129. PvrTcPacket* packets = static_cast<PvrTcPacket*>(result);
  130. for(int y = 0; y < blocks; ++y)
  131. {
  132. for(int x = 0; x < blocks; ++x)
  133. {
  134. ColorRgbBoundingBox cbb;
  135. CalculateBoundingBox(cbb, bitmap, x, y);
  136. PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  137. packet->usePunchthroughAlpha = 0;
  138. packet->SetColorA(cbb.min);
  139. packet->SetColorB(cbb.max);
  140. }
  141. }
  142. for(int y = 0; y < blocks; ++y)
  143. {
  144. for(int x = 0; x < blocks; ++x)
  145. {
  146. const unsigned char (*factor)[4] = PvrTcPacket::BILINEAR_FACTORS;
  147. const ColorRgb<unsigned char>* data = bitmap.GetData() + y * 4 * size + x * 4;
  148. uint32_t modulationData = 0;
  149. for(int py = 0; py < 4; ++py)
  150. {
  151. const int yOffset = (py < 2) ? -1 : 0;
  152. const int y0 = (y + yOffset) & blockMask;
  153. const int y1 = (y0+1) & blockMask;
  154. for(int px = 0; px < 4; ++px)
  155. {
  156. const int xOffset = (px < 2) ? -1 : 0;
  157. const int x0 = (x + xOffset) & blockMask;
  158. const int x1 = (x0+1) & blockMask;
  159. const PvrTcPacket* p0 = packets + GetMortonNumber(x0, y0);
  160. const PvrTcPacket* p1 = packets + GetMortonNumber(x1, y0);
  161. const PvrTcPacket* p2 = packets + GetMortonNumber(x0, y1);
  162. const PvrTcPacket* p3 = packets + GetMortonNumber(x1, y1);
  163. ColorRgb<int> ca = p0->GetColorRgbA() * (*factor)[0] +
  164. p1->GetColorRgbA() * (*factor)[1] +
  165. p2->GetColorRgbA() * (*factor)[2] +
  166. p3->GetColorRgbA() * (*factor)[3];
  167. ColorRgb<int> cb = p0->GetColorRgbB() * (*factor)[0] +
  168. p1->GetColorRgbB() * (*factor)[1] +
  169. p2->GetColorRgbB() * (*factor)[2] +
  170. p3->GetColorRgbB() * (*factor)[3];
  171. const ColorRgb<unsigned char>& pixel = data[py*size + px];
  172. ColorRgb<int> d = cb - ca;
  173. ColorRgb<int> p;
  174. p.r=pixel.r*16;
  175. p.g=pixel.g*16;
  176. p.b=pixel.b*16;
  177. ColorRgb<int> v = p - ca;
  178. // PVRTC uses weightings of 0, 3/8, 5/8 and 1
  179. // The boundaries for these are 3/16, 1/2 (=8/16), 13/16
  180. int projection = (v % d) * 16;
  181. int lengthSquared = d % d;
  182. if(projection > 3*lengthSquared) modulationData++;
  183. if(projection > 8*lengthSquared) modulationData++;
  184. if(projection > 13*lengthSquared) modulationData++;
  185. modulationData = BitUtility::RotateRight(modulationData, 2);
  186. factor++;
  187. }
  188. }
  189. PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  190. packet->modulationData = modulationData;
  191. }
  192. }
  193. }
  194. //============================================================================
  195. static void CalculateBoundingBox(ColorRgbBoundingBox& cbb, const RgbaBitmap& bitmap, int blockX, int blockY)
  196. {
  197. int size = bitmap.GetBitmapWidth();
  198. const ColorRgba<unsigned char>* data = bitmap.GetData() + blockY * 4 * size + blockX * 4;
  199. cbb.min = data[0];
  200. cbb.max = data[0];
  201. cbb |= data[1];
  202. cbb |= data[2];
  203. cbb |= data[3];
  204. cbb |= data[size];
  205. cbb |= data[size+1];
  206. cbb |= data[size+2];
  207. cbb |= data[size+3];
  208. cbb |= data[2*size];
  209. cbb |= data[2*size+1];
  210. cbb |= data[2*size+2];
  211. cbb |= data[2*size+3];
  212. cbb |= data[3*size];
  213. cbb |= data[3*size+1];
  214. cbb |= data[3*size+2];
  215. cbb |= data[3*size+3];
  216. }
  217. void PvrTcEncoder::EncodeRgb4Bpp(void* result, const RgbaBitmap& bitmap)
  218. {
  219. assert(bitmap.GetBitmapWidth() == bitmap.GetBitmapHeight());
  220. assert(BitUtility::IsPowerOf2(bitmap.GetBitmapWidth()));
  221. const int size = bitmap.GetBitmapWidth();
  222. const int blocks = size / 4;
  223. const int blockMask = blocks-1;
  224. PvrTcPacket* packets = static_cast<PvrTcPacket*>(result);
  225. for(int y = 0; y < blocks; ++y)
  226. {
  227. for(int x = 0; x < blocks; ++x)
  228. {
  229. ColorRgbBoundingBox cbb;
  230. CalculateBoundingBox(cbb, bitmap, x, y);
  231. PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  232. packet->usePunchthroughAlpha = 0;
  233. packet->SetColorA(cbb.min);
  234. packet->SetColorB(cbb.max);
  235. }
  236. }
  237. for(int y = 0; y < blocks; ++y)
  238. {
  239. for(int x = 0; x < blocks; ++x)
  240. {
  241. const unsigned char (*factor)[4] = PvrTcPacket::BILINEAR_FACTORS;
  242. const ColorRgba<unsigned char>* data = bitmap.GetData() + y * 4 * size + x * 4;
  243. uint32_t modulationData = 0;
  244. for(int py = 0; py < 4; ++py)
  245. {
  246. const int yOffset = (py < 2) ? -1 : 0;
  247. const int y0 = (y + yOffset) & blockMask;
  248. const int y1 = (y0+1) & blockMask;
  249. for(int px = 0; px < 4; ++px)
  250. {
  251. const int xOffset = (px < 2) ? -1 : 0;
  252. const int x0 = (x + xOffset) & blockMask;
  253. const int x1 = (x0+1) & blockMask;
  254. const PvrTcPacket* p0 = packets + GetMortonNumber(x0, y0);
  255. const PvrTcPacket* p1 = packets + GetMortonNumber(x1, y0);
  256. const PvrTcPacket* p2 = packets + GetMortonNumber(x0, y1);
  257. const PvrTcPacket* p3 = packets + GetMortonNumber(x1, y1);
  258. ColorRgb<int> ca = p0->GetColorRgbA() * (*factor)[0] +
  259. p1->GetColorRgbA() * (*factor)[1] +
  260. p2->GetColorRgbA() * (*factor)[2] +
  261. p3->GetColorRgbA() * (*factor)[3];
  262. ColorRgb<int> cb = p0->GetColorRgbB() * (*factor)[0] +
  263. p1->GetColorRgbB() * (*factor)[1] +
  264. p2->GetColorRgbB() * (*factor)[2] +
  265. p3->GetColorRgbB() * (*factor)[3];
  266. const ColorRgb<unsigned char>& pixel = data[py*size + px];
  267. ColorRgb<int> d = cb - ca;
  268. ColorRgb<int> p;
  269. p.r=pixel.r*16;
  270. p.g=pixel.g*16;
  271. p.b=pixel.b*16;
  272. ColorRgb<int> v = p - ca;
  273. // PVRTC uses weightings of 0, 3/8, 5/8 and 1
  274. // The boundaries for these are 3/16, 1/2 (=8/16), 13/16
  275. int projection = (v % d) * 16;
  276. int lengthSquared = d % d;
  277. if(projection > 3*lengthSquared) modulationData++;
  278. if(projection > 8*lengthSquared) modulationData++;
  279. if(projection > 13*lengthSquared) modulationData++;
  280. modulationData = BitUtility::RotateRight(modulationData, 2);
  281. factor++;
  282. }
  283. }
  284. PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  285. packet->modulationData = modulationData;
  286. }
  287. }
  288. }
  289. //============================================================================
  290. typedef Interval<ColorRgba<unsigned char> > ColorRgbaBoundingBox;
  291. static void CalculateBoundingBox(ColorRgbaBoundingBox& cbb, const RgbaBitmap& bitmap, int blockX, int blockY)
  292. {
  293. int size = bitmap.GetBitmapWidth();
  294. const ColorRgba<unsigned char>* data = bitmap.GetData() + blockY * 4 * size + blockX * 4;
  295. cbb.min = data[0];
  296. cbb.max = data[0];
  297. cbb |= data[1];
  298. cbb |= data[2];
  299. cbb |= data[3];
  300. cbb |= data[size];
  301. cbb |= data[size+1];
  302. cbb |= data[size+2];
  303. cbb |= data[size+3];
  304. cbb |= data[2*size];
  305. cbb |= data[2*size+1];
  306. cbb |= data[2*size+2];
  307. cbb |= data[2*size+3];
  308. cbb |= data[3*size];
  309. cbb |= data[3*size+1];
  310. cbb |= data[3*size+2];
  311. cbb |= data[3*size+3];
  312. }
  313. void PvrTcEncoder::EncodeRgba4Bpp(void* result, const RgbaBitmap& bitmap)
  314. {
  315. assert(bitmap.GetBitmapWidth() == bitmap.GetBitmapHeight());
  316. assert(BitUtility::IsPowerOf2(bitmap.GetBitmapWidth()));
  317. const int size = bitmap.GetBitmapWidth();
  318. const int blocks = size / 4;
  319. const int blockMask = blocks-1;
  320. PvrTcPacket* packets = static_cast<PvrTcPacket*>(result);
  321. for(int y = 0; y < blocks; ++y)
  322. {
  323. for(int x = 0; x < blocks; ++x)
  324. {
  325. ColorRgbaBoundingBox cbb;
  326. CalculateBoundingBox(cbb, bitmap, x, y);
  327. PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  328. packet->usePunchthroughAlpha = 0;
  329. packet->SetColorA(cbb.min);
  330. packet->SetColorB(cbb.max);
  331. }
  332. }
  333. for(int y = 0; y < blocks; ++y)
  334. {
  335. for(int x = 0; x < blocks; ++x)
  336. {
  337. const unsigned char (*factor)[4] = PvrTcPacket::BILINEAR_FACTORS;
  338. const ColorRgba<unsigned char>* data = bitmap.GetData() + y * 4 * size + x * 4;
  339. uint32_t modulationData = 0;
  340. for(int py = 0; py < 4; ++py)
  341. {
  342. const int yOffset = (py < 2) ? -1 : 0;
  343. const int y0 = (y + yOffset) & blockMask;
  344. const int y1 = (y0+1) & blockMask;
  345. for(int px = 0; px < 4; ++px)
  346. {
  347. const int xOffset = (px < 2) ? -1 : 0;
  348. const int x0 = (x + xOffset) & blockMask;
  349. const int x1 = (x0+1) & blockMask;
  350. const PvrTcPacket* p0 = packets + GetMortonNumber(x0, y0);
  351. const PvrTcPacket* p1 = packets + GetMortonNumber(x1, y0);
  352. const PvrTcPacket* p2 = packets + GetMortonNumber(x0, y1);
  353. const PvrTcPacket* p3 = packets + GetMortonNumber(x1, y1);
  354. ColorRgba<int> ca = p0->GetColorRgbaA() * (*factor)[0] +
  355. p1->GetColorRgbaA() * (*factor)[1] +
  356. p2->GetColorRgbaA() * (*factor)[2] +
  357. p3->GetColorRgbaA() * (*factor)[3];
  358. ColorRgba<int> cb = p0->GetColorRgbaB() * (*factor)[0] +
  359. p1->GetColorRgbaB() * (*factor)[1] +
  360. p2->GetColorRgbaB() * (*factor)[2] +
  361. p3->GetColorRgbaB() * (*factor)[3];
  362. const ColorRgba<unsigned char>& pixel = data[py*size + px];
  363. ColorRgba<int> d = cb - ca;
  364. ColorRgba<int> p;
  365. p.r=pixel.r*16;
  366. p.g=pixel.g*16;
  367. p.b=pixel.b*16;
  368. p.a=pixel.a*16;
  369. ColorRgba<int> v = p - ca;
  370. // PVRTC uses weightings of 0, 3/8, 5/8 and 1
  371. // The boundaries for these are 3/16, 1/2 (=8/16), 13/16
  372. int projection = (v % d) * 16;
  373. int lengthSquared = d % d;
  374. if(projection > 3*lengthSquared) modulationData++;
  375. if(projection > 8*lengthSquared) modulationData++;
  376. if(projection > 13*lengthSquared) modulationData++;
  377. modulationData = BitUtility::RotateRight(modulationData, 2);
  378. factor++;
  379. }
  380. }
  381. PvrTcPacket* packet = packets + GetMortonNumber(x, y);
  382. packet->modulationData = modulationData;
  383. }
  384. }
  385. }
  386. //============================================================================