texture_loader_pvr.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/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. f->seek(f->get_position() + 20); // bpp, rmask, gmask, bmask, amask
  66. uint8_t pvrid[5] = { 0, 0, 0, 0, 0 };
  67. f->get_buffer(pvrid, 4);
  68. ERR_FAIL_COND_V(String((char *)pvrid) != "PVR!", RES());
  69. f->get_32(); // surfcount
  70. /*
  71. print_line("height: "+itos(height));
  72. print_line("width: "+itos(width));
  73. print_line("mipmaps: "+itos(mipmaps));
  74. print_line("flags: "+itos(flags));
  75. print_line("surfsize: "+itos(surfsize));
  76. print_line("bpp: "+itos(bpp));
  77. print_line("rmask: "+itos(rmask));
  78. print_line("gmask: "+itos(gmask));
  79. print_line("bmask: "+itos(bmask));
  80. print_line("amask: "+itos(amask));
  81. print_line("surfcount: "+itos(surfcount));
  82. */
  83. PoolVector<uint8_t> data;
  84. data.resize(surfsize);
  85. ERR_FAIL_COND_V(data.size() == 0, RES());
  86. PoolVector<uint8_t>::Write w = data.write();
  87. f->get_buffer(&w[0], surfsize);
  88. err = f->get_error();
  89. ERR_FAIL_COND_V(err != OK, RES());
  90. Image::Format format = Image::FORMAT_MAX;
  91. switch (flags & 0xFF) {
  92. case 0x18:
  93. case 0xC: format = (flags & PVR_HAS_ALPHA) ? Image::FORMAT_PVRTC2A : Image::FORMAT_PVRTC2; break;
  94. case 0x19:
  95. case 0xD: format = (flags & PVR_HAS_ALPHA) ? Image::FORMAT_PVRTC4A : Image::FORMAT_PVRTC4; break;
  96. case 0x16:
  97. format = Image::FORMAT_L8;
  98. break;
  99. case 0x17:
  100. format = Image::FORMAT_LA8;
  101. break;
  102. case 0x20:
  103. case 0x80:
  104. case 0x81:
  105. format = Image::FORMAT_DXT1;
  106. break;
  107. case 0x21:
  108. case 0x22:
  109. case 0x82:
  110. case 0x83:
  111. format = Image::FORMAT_DXT3;
  112. break;
  113. case 0x23:
  114. case 0x24:
  115. case 0x84:
  116. case 0x85:
  117. format = Image::FORMAT_DXT5;
  118. break;
  119. case 0x4:
  120. case 0x15:
  121. format = Image::FORMAT_RGB8;
  122. break;
  123. case 0x5:
  124. case 0x12:
  125. format = Image::FORMAT_RGBA8;
  126. break;
  127. case 0x36:
  128. format = Image::FORMAT_ETC;
  129. break;
  130. default:
  131. ERR_EXPLAIN("Unsupported format in PVR texture: " + itos(flags & 0xFF));
  132. ERR_FAIL_V(RES());
  133. }
  134. w = PoolVector<uint8_t>::Write();
  135. int tex_flags = Texture::FLAG_FILTER | Texture::FLAG_REPEAT;
  136. if (mipmaps)
  137. tex_flags |= Texture::FLAG_MIPMAPS;
  138. Ref<Image> image = memnew(Image(width, height, mipmaps, format, data));
  139. ERR_FAIL_COND_V(image->empty(), RES());
  140. Ref<ImageTexture> texture = memnew(ImageTexture);
  141. texture->create_from_image(image, tex_flags);
  142. if (r_error)
  143. *r_error = OK;
  144. return texture;
  145. }
  146. void ResourceFormatPVR::get_recognized_extensions(List<String> *p_extensions) const {
  147. p_extensions->push_back("pvr");
  148. }
  149. bool ResourceFormatPVR::handles_type(const String &p_type) const {
  150. return ClassDB::is_parent_class(p_type, "Texture");
  151. }
  152. String ResourceFormatPVR::get_resource_type(const String &p_path) const {
  153. if (p_path.get_extension().to_lower() == "pvr")
  154. return "Texture";
  155. return "";
  156. }
  157. static void _compress_pvrtc4(Image *p_img) {
  158. Ref<Image> img = p_img->duplicate();
  159. bool make_mipmaps = false;
  160. if (img->get_width() % 8 || img->get_height() % 8) {
  161. make_mipmaps = img->has_mipmaps();
  162. img->resize(img->get_width() + (8 - (img->get_width() % 8)), img->get_height() + (8 - (img->get_height() % 8)));
  163. }
  164. img->convert(Image::FORMAT_RGBA8);
  165. if (!img->has_mipmaps() && make_mipmaps)
  166. img->generate_mipmaps();
  167. bool use_alpha = img->detect_alpha();
  168. Ref<Image> new_img;
  169. new_img.instance();
  170. new_img->create(img->get_width(), img->get_height(), true, use_alpha ? Image::FORMAT_PVRTC4A : Image::FORMAT_PVRTC4);
  171. PoolVector<uint8_t> data = new_img->get_data();
  172. {
  173. PoolVector<uint8_t>::Write wr = data.write();
  174. PoolVector<uint8_t>::Read r = img->get_data().read();
  175. for (int i = 0; i <= new_img->get_mipmap_count(); i++) {
  176. int ofs, size, w, h;
  177. img->get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h);
  178. Javelin::RgbaBitmap bm(w, h);
  179. copymem(bm.GetData(), &r[ofs], size);
  180. {
  181. Javelin::ColorRgba<unsigned char> *dp = bm.GetData();
  182. for (int j = 0; j < size / 4; j++) {
  183. SWAP(dp[j].r, dp[j].b);
  184. }
  185. }
  186. new_img->get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h);
  187. Javelin::PvrTcEncoder::EncodeRgba4Bpp(&wr[ofs], bm);
  188. }
  189. }
  190. p_img->create(new_img->get_width(), new_img->get_height(), new_img->has_mipmaps(), new_img->get_format(), data);
  191. }
  192. ResourceFormatPVR::ResourceFormatPVR() {
  193. Image::_image_decompress_pvrtc = _pvrtc_decompress;
  194. Image::_image_compress_pvrtc4_func = _compress_pvrtc4;
  195. Image::_image_compress_pvrtc2_func = _compress_pvrtc4;
  196. }
  197. /////////////////////////////////////////////////////////
  198. //PVRTC decompressor, Based on PVRTC decompressor by IMGTEC.
  199. /////////////////////////////////////////////////////////
  200. #define PT_INDEX 2
  201. #define BLK_Y_SIZE 4
  202. #define BLK_X_MAX 8
  203. #define BLK_X_2BPP 8
  204. #define BLK_X_4BPP 4
  205. #define WRAP_COORD(Val, Size) ((Val) & ((Size)-1))
  206. /*
  207. Define an expression to either wrap or clamp large or small vals to the
  208. legal coordinate range
  209. */
  210. #define LIMIT_COORD(Val, Size, p_tiled) \
  211. ((p_tiled) ? WRAP_COORD((Val), (Size)) : CLAMP((Val), 0, (Size)-1))
  212. struct PVRTCBlock {
  213. //blocks are 64 bits
  214. uint32_t data[2];
  215. };
  216. _FORCE_INLINE_ bool is_po2(uint32_t p_input) {
  217. if (p_input == 0)
  218. return 0;
  219. uint32_t minus1 = p_input - 1;
  220. return ((p_input | minus1) == (p_input ^ minus1)) ? 1 : 0;
  221. }
  222. static void unpack_5554(const PVRTCBlock *p_block, int p_ab_colors[2][4]) {
  223. uint32_t raw_bits[2];
  224. raw_bits[0] = p_block->data[1] & (0xFFFE);
  225. raw_bits[1] = p_block->data[1] >> 16;
  226. for (int i = 0; i < 2; i++) {
  227. if (raw_bits[i] & (1 << 15)) {
  228. p_ab_colors[i][0] = (raw_bits[i] >> 10) & 0x1F;
  229. p_ab_colors[i][1] = (raw_bits[i] >> 5) & 0x1F;
  230. p_ab_colors[i][2] = raw_bits[i] & 0x1F;
  231. if (i == 0)
  232. p_ab_colors[0][2] |= p_ab_colors[0][2] >> 4;
  233. p_ab_colors[i][3] = 0xF;
  234. } else {
  235. p_ab_colors[i][0] = (raw_bits[i] >> (8 - 1)) & 0x1E;
  236. p_ab_colors[i][1] = (raw_bits[i] >> (4 - 1)) & 0x1E;
  237. p_ab_colors[i][0] |= p_ab_colors[i][0] >> 4;
  238. p_ab_colors[i][1] |= p_ab_colors[i][1] >> 4;
  239. p_ab_colors[i][2] = (raw_bits[i] & 0xF) << 1;
  240. if (i == 0)
  241. p_ab_colors[0][2] |= p_ab_colors[0][2] >> 3;
  242. else
  243. p_ab_colors[0][2] |= p_ab_colors[0][2] >> 4;
  244. p_ab_colors[i][3] = (raw_bits[i] >> 11) & 0xE;
  245. }
  246. }
  247. }
  248. 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) {
  249. int block_mod_mode = p_block->data[1] & 1;
  250. uint32_t modulation_bits = p_block->data[0];
  251. if (p_2bit && block_mod_mode) {
  252. for (int y = 0; y < BLK_Y_SIZE; y++) {
  253. for (int x = 0; x < BLK_X_2BPP; x++) {
  254. p_modulation_modes[y + p_y][x + p_x] = block_mod_mode;
  255. if (((x ^ y) & 1) == 0) {
  256. p_modulation[y + p_y][x + p_x] = modulation_bits & 3;
  257. modulation_bits >>= 2;
  258. }
  259. }
  260. }
  261. } else if (p_2bit) {
  262. for (int y = 0; y < BLK_Y_SIZE; y++) {
  263. for (int x = 0; x < BLK_X_2BPP; x++) {
  264. p_modulation_modes[y + p_y][x + p_x] = block_mod_mode;
  265. if (modulation_bits & 1)
  266. p_modulation[y + p_y][x + p_x] = 0x3;
  267. else
  268. p_modulation[y + p_y][x + p_x] = 0x0;
  269. modulation_bits >>= 1;
  270. }
  271. }
  272. } else {
  273. for (int y = 0; y < BLK_Y_SIZE; y++) {
  274. for (int x = 0; x < BLK_X_4BPP; x++) {
  275. p_modulation_modes[y + p_y][x + p_x] = block_mod_mode;
  276. p_modulation[y + p_y][x + p_x] = modulation_bits & 3;
  277. modulation_bits >>= 2;
  278. }
  279. }
  280. }
  281. ERR_FAIL_COND(modulation_bits != 0);
  282. }
  283. 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]) {
  284. int u, v, uscale;
  285. int k;
  286. int tmp1, tmp2;
  287. int P[4], Q[4], R[4], S[4];
  288. for (k = 0; k < 4; k++) {
  289. P[k] = p_colorp[k];
  290. Q[k] = p_colorq[k];
  291. R[k] = p_colorr[k];
  292. S[k] = p_colors[k];
  293. }
  294. v = (y & 0x3) | ((~y & 0x2) << 1);
  295. if (p_2bit)
  296. u = (x & 0x7) | ((~x & 0x4) << 1);
  297. else
  298. u = (x & 0x3) | ((~x & 0x2) << 1);
  299. v = v - BLK_Y_SIZE / 2;
  300. if (p_2bit) {
  301. u = u - BLK_X_2BPP / 2;
  302. uscale = 8;
  303. } else {
  304. u = u - BLK_X_4BPP / 2;
  305. uscale = 4;
  306. }
  307. for (k = 0; k < 4; k++) {
  308. tmp1 = P[k] * uscale + u * (Q[k] - P[k]);
  309. tmp2 = R[k] * uscale + u * (S[k] - R[k]);
  310. tmp1 = tmp1 * 4 + v * (tmp2 - tmp1);
  311. r_result[k] = tmp1;
  312. }
  313. if (p_2bit) {
  314. for (k = 0; k < 3; k++) {
  315. r_result[k] >>= 2;
  316. }
  317. r_result[3] >>= 1;
  318. } else {
  319. for (k = 0; k < 3; k++) {
  320. r_result[k] >>= 1;
  321. }
  322. }
  323. for (k = 0; k < 4; k++) {
  324. ERR_FAIL_COND(r_result[k] >= 256);
  325. }
  326. for (k = 0; k < 3; k++) {
  327. r_result[k] += r_result[k] >> 5;
  328. }
  329. r_result[3] += r_result[3] >> 4;
  330. for (k = 0; k < 4; k++) {
  331. ERR_FAIL_COND(r_result[k] >= 256);
  332. }
  333. }
  334. 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) {
  335. static const int rep_vals0[4] = { 0, 3, 5, 8 };
  336. static const int rep_vals1[4] = { 0, 4, 4, 8 };
  337. int mod_val;
  338. y = (y & 0x3) | ((~y & 0x2) << 1);
  339. if (p_2bit)
  340. x = (x & 0x7) | ((~x & 0x4) << 1);
  341. else
  342. x = (x & 0x3) | ((~x & 0x2) << 1);
  343. *p_dopt = 0;
  344. if (p_modulation_modes[y][x] == 0) {
  345. mod_val = rep_vals0[p_modulation[y][x]];
  346. } else if (p_2bit) {
  347. if (((x ^ y) & 1) == 0)
  348. mod_val = rep_vals0[p_modulation[y][x]];
  349. else if (p_modulation_modes[y][x] == 1) {
  350. mod_val = (rep_vals0[p_modulation[y - 1][x]] +
  351. rep_vals0[p_modulation[y + 1][x]] +
  352. rep_vals0[p_modulation[y][x - 1]] +
  353. rep_vals0[p_modulation[y][x + 1]] + 2) /
  354. 4;
  355. } else if (p_modulation_modes[y][x] == 2) {
  356. mod_val = (rep_vals0[p_modulation[y][x - 1]] +
  357. rep_vals0[p_modulation[y][x + 1]] + 1) /
  358. 2;
  359. } else {
  360. mod_val = (rep_vals0[p_modulation[y - 1][x]] +
  361. rep_vals0[p_modulation[y + 1][x]] + 1) /
  362. 2;
  363. }
  364. } else {
  365. mod_val = rep_vals1[p_modulation[y][x]];
  366. *p_dopt = p_modulation[y][x] == PT_INDEX;
  367. }
  368. *r_mod = mod_val;
  369. }
  370. static int disable_twiddling = 0;
  371. static uint32_t twiddle_uv(uint32_t p_height, uint32_t p_width, uint32_t p_y, uint32_t p_x) {
  372. uint32_t twiddled;
  373. uint32_t min_dimension;
  374. uint32_t max_value;
  375. uint32_t scr_bit_pos;
  376. uint32_t dst_bit_pos;
  377. int shift_count;
  378. ERR_FAIL_COND_V(p_y >= p_height, 0);
  379. ERR_FAIL_COND_V(p_x >= p_width, 0);
  380. ERR_FAIL_COND_V(!is_po2(p_height), 0);
  381. ERR_FAIL_COND_V(!is_po2(p_width), 0);
  382. if (p_height < p_width) {
  383. min_dimension = p_height;
  384. max_value = p_x;
  385. } else {
  386. min_dimension = p_width;
  387. max_value = p_y;
  388. }
  389. if (disable_twiddling)
  390. return (p_y * p_width + p_x);
  391. scr_bit_pos = 1;
  392. dst_bit_pos = 1;
  393. twiddled = 0;
  394. shift_count = 0;
  395. while (scr_bit_pos < min_dimension) {
  396. if (p_y & scr_bit_pos) {
  397. twiddled |= dst_bit_pos;
  398. }
  399. if (p_x & scr_bit_pos) {
  400. twiddled |= (dst_bit_pos << 1);
  401. }
  402. scr_bit_pos <<= 1;
  403. dst_bit_pos <<= 2;
  404. shift_count += 1;
  405. }
  406. max_value >>= shift_count;
  407. twiddled |= (max_value << (2 * shift_count));
  408. return twiddled;
  409. }
  410. 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) {
  411. int x, y;
  412. int i, j;
  413. int block_x, blk_y;
  414. int block_xp1, blk_yp1;
  415. int x_block_size;
  416. int block_width, block_height;
  417. int p_x, p_y;
  418. int p_modulation[8][16] = { { 0 } };
  419. int p_modulation_modes[8][16] = { { 0 } };
  420. int Mod, DoPT;
  421. unsigned int u_pos;
  422. // local neighbourhood of blocks
  423. PVRTCBlock *p_blocks[2][2];
  424. PVRTCBlock *prev[2][2] = { { NULL, NULL }, { NULL, NULL } };
  425. struct
  426. {
  427. int Reps[2][4];
  428. } colors5554[2][2];
  429. int ASig[4], BSig[4];
  430. int r_result[4];
  431. if (p_2bit)
  432. x_block_size = BLK_X_2BPP;
  433. else
  434. x_block_size = BLK_X_4BPP;
  435. block_width = MAX(2, p_width / x_block_size);
  436. block_height = MAX(2, p_height / BLK_Y_SIZE);
  437. for (y = 0; y < p_height; y++) {
  438. for (x = 0; x < p_width; x++) {
  439. block_x = (x - x_block_size / 2);
  440. blk_y = (y - BLK_Y_SIZE / 2);
  441. block_x = LIMIT_COORD(block_x, p_width, p_tiled);
  442. blk_y = LIMIT_COORD(blk_y, p_height, p_tiled);
  443. block_x /= x_block_size;
  444. blk_y /= BLK_Y_SIZE;
  445. block_xp1 = LIMIT_COORD(block_x + 1, block_width, p_tiled);
  446. blk_yp1 = LIMIT_COORD(blk_y + 1, block_height, p_tiled);
  447. p_blocks[0][0] = p_comp_img + twiddle_uv(block_height, block_width, blk_y, block_x);
  448. p_blocks[0][1] = p_comp_img + twiddle_uv(block_height, block_width, blk_y, block_xp1);
  449. p_blocks[1][0] = p_comp_img + twiddle_uv(block_height, block_width, blk_yp1, block_x);
  450. p_blocks[1][1] = p_comp_img + twiddle_uv(block_height, block_width, blk_yp1, block_xp1);
  451. if (memcmp(prev, p_blocks, 4 * sizeof(void *)) != 0) {
  452. p_y = 0;
  453. for (i = 0; i < 2; i++) {
  454. p_x = 0;
  455. for (j = 0; j < 2; j++) {
  456. unpack_5554(p_blocks[i][j], colors5554[i][j].Reps);
  457. unpack_modulations(
  458. p_blocks[i][j],
  459. p_2bit,
  460. p_modulation,
  461. p_modulation_modes,
  462. p_x, p_y);
  463. p_x += x_block_size;
  464. }
  465. p_y += BLK_Y_SIZE;
  466. }
  467. memcpy(prev, p_blocks, 4 * sizeof(void *));
  468. }
  469. interpolate_colors(
  470. colors5554[0][0].Reps[0],
  471. colors5554[0][1].Reps[0],
  472. colors5554[1][0].Reps[0],
  473. colors5554[1][1].Reps[0],
  474. p_2bit, x, y,
  475. ASig);
  476. interpolate_colors(
  477. colors5554[0][0].Reps[1],
  478. colors5554[0][1].Reps[1],
  479. colors5554[1][0].Reps[1],
  480. colors5554[1][1].Reps[1],
  481. p_2bit, x, y,
  482. BSig);
  483. get_modulation_value(x, y, p_2bit, (const int(*)[16])p_modulation, (const int(*)[16])p_modulation_modes,
  484. &Mod, &DoPT);
  485. for (i = 0; i < 4; i++) {
  486. r_result[i] = ASig[i] * 8 + Mod * (BSig[i] - ASig[i]);
  487. r_result[i] >>= 3;
  488. }
  489. if (DoPT)
  490. r_result[3] = 0;
  491. u_pos = (x + y * p_width) << 2;
  492. p_dst[u_pos + 0] = (uint8_t)r_result[0];
  493. p_dst[u_pos + 1] = (uint8_t)r_result[1];
  494. p_dst[u_pos + 2] = (uint8_t)r_result[2];
  495. p_dst[u_pos + 3] = (uint8_t)r_result[3];
  496. }
  497. }
  498. }
  499. static void _pvrtc_decompress(Image *p_img) {
  500. ERR_FAIL_COND(p_img->get_format() != Image::FORMAT_PVRTC2 && p_img->get_format() != Image::FORMAT_PVRTC2A && p_img->get_format() != Image::FORMAT_PVRTC4 && p_img->get_format() != Image::FORMAT_PVRTC4A);
  501. bool _2bit = (p_img->get_format() == Image::FORMAT_PVRTC2 || p_img->get_format() == Image::FORMAT_PVRTC2A);
  502. PoolVector<uint8_t> data = p_img->get_data();
  503. PoolVector<uint8_t>::Read r = data.read();
  504. PoolVector<uint8_t> newdata;
  505. newdata.resize(p_img->get_width() * p_img->get_height() * 4);
  506. PoolVector<uint8_t>::Write w = newdata.write();
  507. decompress_pvrtc((PVRTCBlock *)r.ptr(), _2bit, p_img->get_width(), p_img->get_height(), 0, (unsigned char *)w.ptr());
  508. w = PoolVector<uint8_t>::Write();
  509. r = PoolVector<uint8_t>::Read();
  510. bool make_mipmaps = p_img->has_mipmaps();
  511. p_img->create(p_img->get_width(), p_img->get_height(), false, Image::FORMAT_RGBA8, newdata);
  512. if (make_mipmaps)
  513. p_img->generate_mipmaps();
  514. }