2
0

texture_loader_pvr.cpp 17 KB

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