texture_loader_pvr.cpp 17 KB

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