texture_loader_pvr.cpp 16 KB

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