texture_loader_pvr.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*************************************************************************/
  2. /* texture_loader_pvr.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "texture_loader_pvr.h"
  31. #include "PvrTcEncoder.h"
  32. #include "RgbaBitmap.h"
  33. #include "os/file_access.h"
  34. #include <string.h>
  35. static void _pvrtc_decompress(Image *p_img);
  36. enum PVRFLags {
  37. PVR_HAS_MIPMAPS = 0x00000100,
  38. PVR_TWIDDLED = 0x00000200,
  39. PVR_NORMAL_MAP = 0x00000400,
  40. PVR_BORDER = 0x00000800,
  41. PVR_CUBE_MAP = 0x00001000,
  42. PVR_FALSE_MIPMAPS = 0x00002000,
  43. PVR_VOLUME_TEXTURES = 0x00004000,
  44. PVR_HAS_ALPHA = 0x00008000,
  45. PVR_VFLIP = 0x00010000
  46. };
  47. RES ResourceFormatPVR::load(const String &p_path, const String &p_original_path, Error *r_error) {
  48. if (r_error)
  49. *r_error = ERR_CANT_OPEN;
  50. Error err;
  51. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  52. if (!f)
  53. return RES();
  54. FileAccessRef faref(f);
  55. ERR_FAIL_COND_V(err, RES());
  56. if (r_error)
  57. *r_error = ERR_FILE_CORRUPT;
  58. uint32_t hsize = f->get_32();
  59. ERR_FAIL_COND_V(hsize != 52, RES());
  60. uint32_t height = f->get_32();
  61. uint32_t width = f->get_32();
  62. uint32_t mipmaps = f->get_32();
  63. uint32_t flags = f->get_32();
  64. uint32_t surfsize = f->get_32();
  65. uint32_t bpp = f->get_32();
  66. uint32_t rmask = f->get_32();
  67. uint32_t gmask = f->get_32();
  68. uint32_t bmask = f->get_32();
  69. uint32_t amask = f->get_32();
  70. uint8_t pvrid[5] = { 0, 0, 0, 0, 0 };
  71. f->get_buffer(pvrid, 4);
  72. ERR_FAIL_COND_V(String((char *)pvrid) != "PVR!", RES());
  73. uint32_t surfcount = f->get_32();
  74. /*
  75. print_line("height: "+itos(height));
  76. print_line("width: "+itos(width));
  77. print_line("mipmaps: "+itos(mipmaps));
  78. print_line("flags: "+itos(flags));
  79. print_line("surfsize: "+itos(surfsize));
  80. print_line("bpp: "+itos(bpp));
  81. print_line("rmask: "+itos(rmask));
  82. print_line("gmask: "+itos(gmask));
  83. print_line("bmask: "+itos(bmask));
  84. print_line("amask: "+itos(amask));
  85. print_line("surfcount: "+itos(surfcount));
  86. */
  87. DVector<uint8_t> data;
  88. data.resize(surfsize);
  89. ERR_FAIL_COND_V(data.size() == 0, RES());
  90. DVector<uint8_t>::Write w = data.write();
  91. f->get_buffer(&w[0], surfsize);
  92. err = f->get_error();
  93. ERR_FAIL_COND_V(err != OK, RES());
  94. Image::Format format = Image::FORMAT_MAX;
  95. switch (flags & 0xFF) {
  96. case 0x18:
  97. case 0xC: format = (flags & PVR_HAS_ALPHA) ? Image::FORMAT_PVRTC2_ALPHA : Image::FORMAT_PVRTC2; break;
  98. case 0x19:
  99. case 0xD: format = (flags & PVR_HAS_ALPHA) ? Image::FORMAT_PVRTC4_ALPHA : Image::FORMAT_PVRTC4; break;
  100. case 0x16:
  101. format = Image::FORMAT_GRAYSCALE;
  102. break;
  103. case 0x17:
  104. format = Image::FORMAT_GRAYSCALE_ALPHA;
  105. break;
  106. case 0x20:
  107. case 0x80:
  108. case 0x81:
  109. format = Image::FORMAT_BC1;
  110. break;
  111. case 0x21:
  112. case 0x22:
  113. case 0x82:
  114. case 0x83:
  115. format = Image::FORMAT_BC2;
  116. break;
  117. case 0x23:
  118. case 0x24:
  119. case 0x84:
  120. case 0x85:
  121. format = Image::FORMAT_BC3;
  122. break;
  123. case 0x4:
  124. case 0x15:
  125. format = Image::FORMAT_RGB;
  126. break;
  127. case 0x5:
  128. case 0x12:
  129. format = Image::FORMAT_RGBA;
  130. break;
  131. case 0x36:
  132. format = Image::FORMAT_ETC;
  133. break;
  134. default:
  135. ERR_EXPLAIN("Unsupported format in PVR texture: " + itos(flags & 0xFF));
  136. ERR_FAIL_V(RES());
  137. }
  138. w = DVector<uint8_t>::Write();
  139. int tex_flags = Texture::FLAG_FILTER | Texture::FLAG_REPEAT;
  140. if (mipmaps)
  141. tex_flags |= Texture::FLAG_MIPMAPS;
  142. print_line("flip: " + itos(flags & PVR_VFLIP));
  143. Image image(width, height, mipmaps, format, data);
  144. ERR_FAIL_COND_V(image.empty(), RES());
  145. Ref<ImageTexture> texture = memnew(ImageTexture);
  146. texture->create_from_image(image, tex_flags);
  147. if (r_error)
  148. *r_error = OK;
  149. return texture;
  150. }
  151. void ResourceFormatPVR::get_recognized_extensions(List<String> *p_extensions) const {
  152. p_extensions->push_back("pvr");
  153. }
  154. bool ResourceFormatPVR::handles_type(const String &p_type) const {
  155. return ObjectTypeDB::is_type(p_type, "Texture");
  156. }
  157. String ResourceFormatPVR::get_resource_type(const String &p_path) const {
  158. if (p_path.extension().to_lower() == "pvr")
  159. return "Texture";
  160. return "";
  161. }
  162. static void _compress_pvrtc4(Image *p_img) {
  163. Image img = *p_img;
  164. bool make_mipmaps = false;
  165. if (img.get_width() % 8 || img.get_height() % 8) {
  166. make_mipmaps = img.get_mipmaps() > 0;
  167. img.resize(img.get_width() + (8 - (img.get_width() % 8)), img.get_height() + (8 - (img.get_height() % 8)));
  168. }
  169. img.convert(Image::FORMAT_RGBA);
  170. if (img.get_mipmaps() == 0 && make_mipmaps)
  171. img.generate_mipmaps();
  172. bool use_alpha = img.detect_alpha();
  173. Image new_img;
  174. new_img.create(img.get_width(), img.get_height(), true, use_alpha ? Image::FORMAT_PVRTC4_ALPHA : Image::FORMAT_PVRTC4);
  175. DVector<uint8_t> data = new_img.get_data();
  176. {
  177. DVector<uint8_t>::Write wr = data.write();
  178. DVector<uint8_t>::Read r = img.get_data().read();
  179. for (int i = 0; i <= new_img.get_mipmaps(); i++) {
  180. int ofs, size, w, h;
  181. img.get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h);
  182. Javelin::RgbaBitmap bm(w, h);
  183. copymem(bm.GetData(), &r[ofs], size);
  184. {
  185. Javelin::ColorRgba<unsigned char> *dp = bm.GetData();
  186. for (int j = 0; j < size / 4; j++) {
  187. SWAP(dp[j].r, dp[j].b);
  188. }
  189. }
  190. new_img.get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h);
  191. Javelin::PvrTcEncoder::EncodeRgba4Bpp(&wr[ofs], bm);
  192. }
  193. }
  194. *p_img = Image(new_img.get_width(), new_img.get_height(), new_img.get_mipmaps(), new_img.get_format(), data);
  195. }
  196. ResourceFormatPVR::ResourceFormatPVR() {
  197. Image::_image_decompress_pvrtc = _pvrtc_decompress;
  198. Image::_image_compress_pvrtc4_func = _compress_pvrtc4;
  199. Image::_image_compress_pvrtc2_func = _compress_pvrtc4;
  200. }
  201. /////////////////////////////////////////////////////////
  202. //PVRTC decompressor, Based on PVRTC decompressor by IMGTEC.
  203. /////////////////////////////////////////////////////////
  204. #define PT_INDEX 2
  205. #define BLK_Y_SIZE 4
  206. #define BLK_X_MAX 8
  207. #define BLK_X_2BPP 8
  208. #define BLK_X_4BPP 4
  209. #define WRAP_COORD(Val, Size) ((Val) & ((Size)-1))
  210. /*
  211. Define an expression to either wrap or clamp large or small vals to the
  212. legal coordinate range
  213. */
  214. #define LIMIT_COORD(Val, Size, p_tiled) \
  215. ((p_tiled) ? WRAP_COORD((Val), (Size)) : CLAMP((Val), 0, (Size)-1))
  216. struct PVRTCBlock {
  217. //blocks are 64 bits
  218. uint32_t data[2];
  219. };
  220. _FORCE_INLINE_ bool is_po2(uint32_t p_input) {
  221. if (p_input == 0)
  222. return 0;
  223. uint32_t minus1 = p_input - 1;
  224. return ((p_input | minus1) == (p_input ^ minus1)) ? 1 : 0;
  225. }
  226. static void unpack_5554(const PVRTCBlock *p_block, int p_ab_colors[2][4]) {
  227. uint32_t raw_bits[2];
  228. raw_bits[0] = p_block->data[1] & (0xFFFE);
  229. raw_bits[1] = p_block->data[1] >> 16;
  230. for (int i = 0; i < 2; i++) {
  231. if (raw_bits[i] & (1 << 15)) {
  232. p_ab_colors[i][0] = (raw_bits[i] >> 10) & 0x1F;
  233. p_ab_colors[i][1] = (raw_bits[i] >> 5) & 0x1F;
  234. p_ab_colors[i][2] = raw_bits[i] & 0x1F;
  235. if (i == 0)
  236. p_ab_colors[0][2] |= p_ab_colors[0][2] >> 4;
  237. p_ab_colors[i][3] = 0xF;
  238. } else {
  239. p_ab_colors[i][0] = (raw_bits[i] >> (8 - 1)) & 0x1E;
  240. p_ab_colors[i][1] = (raw_bits[i] >> (4 - 1)) & 0x1E;
  241. p_ab_colors[i][0] |= p_ab_colors[i][0] >> 4;
  242. p_ab_colors[i][1] |= p_ab_colors[i][1] >> 4;
  243. p_ab_colors[i][2] = (raw_bits[i] & 0xF) << 1;
  244. if (i == 0)
  245. p_ab_colors[0][2] |= p_ab_colors[0][2] >> 3;
  246. else
  247. p_ab_colors[0][2] |= p_ab_colors[0][2] >> 4;
  248. p_ab_colors[i][3] = (raw_bits[i] >> 11) & 0xE;
  249. }
  250. }
  251. }
  252. static void unpack_modulations(const PVRTCBlock *p_block, const int p_2bit, int p_modulation[8][16], int p_modulation_modes[8][16], int p_x, int p_y) {
  253. int block_mod_mode = p_block->data[1] & 1;
  254. uint32_t modulation_bits = p_block->data[0];
  255. if (p_2bit && block_mod_mode) {
  256. for (int y = 0; y < BLK_Y_SIZE; y++) {
  257. for (int x = 0; x < BLK_X_2BPP; x++) {
  258. p_modulation_modes[y + p_y][x + p_x] = block_mod_mode;
  259. if (((x ^ y) & 1) == 0) {
  260. p_modulation[y + p_y][x + p_x] = modulation_bits & 3;
  261. modulation_bits >>= 2;
  262. }
  263. }
  264. }
  265. } else if (p_2bit) {
  266. for (int y = 0; y < BLK_Y_SIZE; y++) {
  267. for (int x = 0; x < BLK_X_2BPP; x++) {
  268. p_modulation_modes[y + p_y][x + p_x] = block_mod_mode;
  269. if (modulation_bits & 1)
  270. p_modulation[y + p_y][x + p_x] = 0x3;
  271. else
  272. p_modulation[y + p_y][x + p_x] = 0x0;
  273. modulation_bits >>= 1;
  274. }
  275. }
  276. } else {
  277. for (int y = 0; y < BLK_Y_SIZE; y++) {
  278. for (int x = 0; x < BLK_X_4BPP; x++) {
  279. p_modulation_modes[y + p_y][x + p_x] = block_mod_mode;
  280. p_modulation[y + p_y][x + p_x] = modulation_bits & 3;
  281. modulation_bits >>= 2;
  282. }
  283. }
  284. }
  285. ERR_FAIL_COND(modulation_bits != 0);
  286. }
  287. static void interpolate_colors(const int p_colorp[4], const int p_colorq[4], const int p_colorr[4], const int p_colors[4], bool p_2bit, const int x, const int y, int r_result[4]) {
  288. int u, v, uscale;
  289. int k;
  290. int tmp1, tmp2;
  291. int P[4], Q[4], R[4], S[4];
  292. for (k = 0; k < 4; k++) {
  293. P[k] = p_colorp[k];
  294. Q[k] = p_colorq[k];
  295. R[k] = p_colorr[k];
  296. S[k] = p_colors[k];
  297. }
  298. v = (y & 0x3) | ((~y & 0x2) << 1);
  299. if (p_2bit)
  300. u = (x & 0x7) | ((~x & 0x4) << 1);
  301. else
  302. u = (x & 0x3) | ((~x & 0x2) << 1);
  303. v = v - BLK_Y_SIZE / 2;
  304. if (p_2bit) {
  305. u = u - BLK_X_2BPP / 2;
  306. uscale = 8;
  307. } else {
  308. u = u - BLK_X_4BPP / 2;
  309. uscale = 4;
  310. }
  311. for (k = 0; k < 4; k++) {
  312. tmp1 = P[k] * uscale + u * (Q[k] - P[k]);
  313. tmp2 = R[k] * uscale + u * (S[k] - R[k]);
  314. tmp1 = tmp1 * 4 + v * (tmp2 - tmp1);
  315. r_result[k] = tmp1;
  316. }
  317. if (p_2bit) {
  318. for (k = 0; k < 3; k++) {
  319. r_result[k] >>= 2;
  320. }
  321. r_result[3] >>= 1;
  322. } else {
  323. for (k = 0; k < 3; k++) {
  324. r_result[k] >>= 1;
  325. }
  326. }
  327. for (k = 0; k < 4; k++) {
  328. ERR_FAIL_COND(r_result[k] >= 256);
  329. }
  330. for (k = 0; k < 3; k++) {
  331. r_result[k] += r_result[k] >> 5;
  332. }
  333. r_result[3] += r_result[3] >> 4;
  334. for (k = 0; k < 4; k++) {
  335. ERR_FAIL_COND(r_result[k] >= 256);
  336. }
  337. }
  338. static void get_modulation_value(int x, int y, const int p_2bit, const int p_modulation[8][16], const int p_modulation_modes[8][16], int *r_mod, int *p_dopt) {
  339. static const int rep_vals0[4] = { 0, 3, 5, 8 };
  340. static const int rep_vals1[4] = { 0, 4, 4, 8 };
  341. int mod_val;
  342. y = (y & 0x3) | ((~y & 0x2) << 1);
  343. if (p_2bit)
  344. x = (x & 0x7) | ((~x & 0x4) << 1);
  345. else
  346. x = (x & 0x3) | ((~x & 0x2) << 1);
  347. *p_dopt = 0;
  348. if (p_modulation_modes[y][x] == 0) {
  349. mod_val = rep_vals0[p_modulation[y][x]];
  350. } else if (p_2bit) {
  351. if (((x ^ y) & 1) == 0)
  352. mod_val = rep_vals0[p_modulation[y][x]];
  353. else if (p_modulation_modes[y][x] == 1) {
  354. mod_val = (rep_vals0[p_modulation[y - 1][x]] +
  355. rep_vals0[p_modulation[y + 1][x]] +
  356. rep_vals0[p_modulation[y][x - 1]] +
  357. rep_vals0[p_modulation[y][x + 1]] + 2) /
  358. 4;
  359. } else if (p_modulation_modes[y][x] == 2) {
  360. mod_val = (rep_vals0[p_modulation[y][x - 1]] +
  361. rep_vals0[p_modulation[y][x + 1]] + 1) /
  362. 2;
  363. } else {
  364. mod_val = (rep_vals0[p_modulation[y - 1][x]] +
  365. rep_vals0[p_modulation[y + 1][x]] + 1) /
  366. 2;
  367. }
  368. } else {
  369. mod_val = rep_vals1[p_modulation[y][x]];
  370. *p_dopt = p_modulation[y][x] == PT_INDEX;
  371. }
  372. *r_mod = mod_val;
  373. }
  374. static int disable_twiddling = 0;
  375. static uint32_t twiddle_uv(uint32_t p_height, uint32_t p_width, uint32_t p_y, uint32_t p_x) {
  376. uint32_t twiddled;
  377. uint32_t min_dimension;
  378. uint32_t max_value;
  379. uint32_t scr_bit_pos;
  380. uint32_t dst_bit_pos;
  381. int shift_count;
  382. ERR_FAIL_COND_V(p_y >= p_height, 0);
  383. ERR_FAIL_COND_V(p_x >= p_width, 0);
  384. ERR_FAIL_COND_V(!is_po2(p_height), 0);
  385. ERR_FAIL_COND_V(!is_po2(p_width), 0);
  386. if (p_height < p_width) {
  387. min_dimension = p_height;
  388. max_value = p_x;
  389. } else {
  390. min_dimension = p_width;
  391. max_value = p_y;
  392. }
  393. if (disable_twiddling)
  394. return (p_y * p_width + p_x);
  395. scr_bit_pos = 1;
  396. dst_bit_pos = 1;
  397. twiddled = 0;
  398. shift_count = 0;
  399. while (scr_bit_pos < min_dimension) {
  400. if (p_y & scr_bit_pos) {
  401. twiddled |= dst_bit_pos;
  402. }
  403. if (p_x & scr_bit_pos) {
  404. twiddled |= (dst_bit_pos << 1);
  405. }
  406. scr_bit_pos <<= 1;
  407. dst_bit_pos <<= 2;
  408. shift_count += 1;
  409. }
  410. max_value >>= shift_count;
  411. twiddled |= (max_value << (2 * shift_count));
  412. return twiddled;
  413. }
  414. static void decompress_pvrtc(PVRTCBlock *p_comp_img, const int p_2bit, const int p_width, const int p_height, const int p_tiled, unsigned char *p_dst) {
  415. int x, y;
  416. int i, j;
  417. int block_x, blk_y;
  418. int block_xp1, blk_yp1;
  419. int x_block_size;
  420. int block_width, block_height;
  421. int p_x, p_y;
  422. int p_modulation[8][16];
  423. int p_modulation_modes[8][16];
  424. int Mod, DoPT;
  425. unsigned int u_pos;
  426. // local neighbourhood of blocks
  427. PVRTCBlock *p_blocks[2][2];
  428. PVRTCBlock *prev[2][2] = { { NULL, NULL }, { NULL, NULL } };
  429. struct
  430. {
  431. int Reps[2][4];
  432. } colors5554[2][2];
  433. int ASig[4], BSig[4];
  434. int r_result[4];
  435. if (p_2bit)
  436. x_block_size = BLK_X_2BPP;
  437. else
  438. x_block_size = BLK_X_4BPP;
  439. block_width = MAX(2, p_width / x_block_size);
  440. block_height = MAX(2, p_height / BLK_Y_SIZE);
  441. for (y = 0; y < p_height; y++) {
  442. for (x = 0; x < p_width; x++) {
  443. block_x = (x - x_block_size / 2);
  444. blk_y = (y - BLK_Y_SIZE / 2);
  445. block_x = LIMIT_COORD(block_x, p_width, p_tiled);
  446. blk_y = LIMIT_COORD(blk_y, p_height, p_tiled);
  447. block_x /= x_block_size;
  448. blk_y /= BLK_Y_SIZE;
  449. block_xp1 = LIMIT_COORD(block_x + 1, block_width, p_tiled);
  450. blk_yp1 = LIMIT_COORD(blk_y + 1, block_height, p_tiled);
  451. p_blocks[0][0] = p_comp_img + twiddle_uv(block_height, block_width, blk_y, block_x);
  452. p_blocks[0][1] = p_comp_img + twiddle_uv(block_height, block_width, blk_y, block_xp1);
  453. p_blocks[1][0] = p_comp_img + twiddle_uv(block_height, block_width, blk_yp1, block_x);
  454. p_blocks[1][1] = p_comp_img + twiddle_uv(block_height, block_width, blk_yp1, block_xp1);
  455. if (memcmp(prev, p_blocks, 4 * sizeof(void *)) != 0) {
  456. p_y = 0;
  457. for (i = 0; i < 2; i++) {
  458. p_x = 0;
  459. for (j = 0; j < 2; j++) {
  460. unpack_5554(p_blocks[i][j], colors5554[i][j].Reps);
  461. unpack_modulations(
  462. p_blocks[i][j],
  463. p_2bit,
  464. p_modulation,
  465. p_modulation_modes,
  466. p_x, p_y);
  467. p_x += x_block_size;
  468. }
  469. p_y += BLK_Y_SIZE;
  470. }
  471. memcpy(prev, p_blocks, 4 * sizeof(void *));
  472. }
  473. interpolate_colors(
  474. colors5554[0][0].Reps[0],
  475. colors5554[0][1].Reps[0],
  476. colors5554[1][0].Reps[0],
  477. colors5554[1][1].Reps[0],
  478. p_2bit, x, y,
  479. ASig);
  480. interpolate_colors(
  481. colors5554[0][0].Reps[1],
  482. colors5554[0][1].Reps[1],
  483. colors5554[1][0].Reps[1],
  484. colors5554[1][1].Reps[1],
  485. p_2bit, x, y,
  486. BSig);
  487. get_modulation_value(x, y, p_2bit, (const int(*)[16])p_modulation, (const int(*)[16])p_modulation_modes,
  488. &Mod, &DoPT);
  489. for (i = 0; i < 4; i++) {
  490. r_result[i] = ASig[i] * 8 + Mod * (BSig[i] - ASig[i]);
  491. r_result[i] >>= 3;
  492. }
  493. if (DoPT)
  494. r_result[3] = 0;
  495. u_pos = (x + y * p_width) << 2;
  496. p_dst[u_pos + 0] = (uint8_t)r_result[0];
  497. p_dst[u_pos + 1] = (uint8_t)r_result[1];
  498. p_dst[u_pos + 2] = (uint8_t)r_result[2];
  499. p_dst[u_pos + 3] = (uint8_t)r_result[3];
  500. }
  501. }
  502. }
  503. static void _pvrtc_decompress(Image *p_img) {
  504. // static void decompress_pvrtc(const void *p_comp_img, const int p_2bit, const int p_width, const int p_height, unsigned char* p_dst) {
  505. // decompress_pvrtc((PVRTCBlock*)p_comp_img,p_2bit,p_width,p_height,1,p_dst);
  506. // }
  507. ERR_FAIL_COND(p_img->get_format() != Image::FORMAT_PVRTC2 && p_img->get_format() != Image::FORMAT_PVRTC2_ALPHA && p_img->get_format() != Image::FORMAT_PVRTC4 && p_img->get_format() != Image::FORMAT_PVRTC4_ALPHA);
  508. bool _2bit = (p_img->get_format() == Image::FORMAT_PVRTC2 || p_img->get_format() == Image::FORMAT_PVRTC2_ALPHA);
  509. DVector<uint8_t> data = p_img->get_data();
  510. DVector<uint8_t>::Read r = data.read();
  511. DVector<uint8_t> newdata;
  512. newdata.resize(p_img->get_width() * p_img->get_height() * 4);
  513. DVector<uint8_t>::Write w = newdata.write();
  514. decompress_pvrtc((PVRTCBlock *)r.ptr(), _2bit, p_img->get_width(), p_img->get_height(), 0, (unsigned char *)w.ptr());
  515. //for(int i=0;i<newdata.size();i++) {
  516. // print_line(itos(w[i]));
  517. //}
  518. w = DVector<uint8_t>::Write();
  519. r = DVector<uint8_t>::Read();
  520. bool make_mipmaps = p_img->get_mipmaps() > 0;
  521. Image newimg(p_img->get_width(), p_img->get_height(), 0, Image::FORMAT_RGBA, newdata);
  522. if (make_mipmaps)
  523. newimg.generate_mipmaps();
  524. *p_img = newimg;
  525. }