Skip to content

Commit 902d3c9

Browse files
authored
[rl_gputex] Correctly load mipmaps from DDS files (#4399)
1 parent dc5e6e0 commit 902d3c9

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/external/rl_gputex.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ void *rl_load_dds_from_memory(const unsigned char *file_data, unsigned int file_
185185
if (header->ddspf.flags == 0x40) // No alpha channel
186186
{
187187
int data_size = image_pixel_size*sizeof(unsigned short);
188+
if (header->mipmap_count > 1) data_size = data_size + data_size / 3;
188189
image_data = RL_MALLOC(data_size);
189190

190191
memcpy(image_data, file_data_ptr, data_size);
@@ -196,6 +197,7 @@ void *rl_load_dds_from_memory(const unsigned char *file_data, unsigned int file_
196197
if (header->ddspf.a_bit_mask == 0x8000) // 1bit alpha
197198
{
198199
int data_size = image_pixel_size*sizeof(unsigned short);
200+
if (header->mipmap_count > 1) data_size = data_size + data_size / 3;
199201
image_data = RL_MALLOC(data_size);
200202

201203
memcpy(image_data, file_data_ptr, data_size);
@@ -215,6 +217,7 @@ void *rl_load_dds_from_memory(const unsigned char *file_data, unsigned int file_
215217
else if (header->ddspf.a_bit_mask == 0xf000) // 4bit alpha
216218
{
217219
int data_size = image_pixel_size*sizeof(unsigned short);
220+
if (header->mipmap_count > 1) data_size = data_size + data_size / 3;
218221
image_data = RL_MALLOC(data_size);
219222

220223
memcpy(image_data, file_data_ptr, data_size);
@@ -236,6 +239,7 @@ void *rl_load_dds_from_memory(const unsigned char *file_data, unsigned int file_
236239
else if ((header->ddspf.flags == 0x40) && (header->ddspf.rgb_bit_count == 24)) // DDS_RGB, no compressed
237240
{
238241
int data_size = image_pixel_size*3*sizeof(unsigned char);
242+
if (header->mipmap_count > 1) data_size = data_size + data_size / 3;
239243
image_data = RL_MALLOC(data_size);
240244

241245
memcpy(image_data, file_data_ptr, data_size);
@@ -245,6 +249,7 @@ void *rl_load_dds_from_memory(const unsigned char *file_data, unsigned int file_
245249
else if ((header->ddspf.flags == 0x41) && (header->ddspf.rgb_bit_count == 32)) // DDS_RGBA, no compressed
246250
{
247251
int data_size = image_pixel_size*4*sizeof(unsigned char);
252+
if (header->mipmap_count > 1) data_size = data_size + data_size / 3;
248253
image_data = RL_MALLOC(data_size);
249254

250255
memcpy(image_data, file_data_ptr, data_size);
@@ -265,9 +270,11 @@ void *rl_load_dds_from_memory(const unsigned char *file_data, unsigned int file_
265270
}
266271
else if (((header->ddspf.flags == 0x04) || (header->ddspf.flags == 0x05)) && (header->ddspf.fourcc > 0)) // Compressed
267272
{
268-
// NOTE: This forces only 1 mipmap to be loaded which is not really correct but it works
269-
int data_size = (header->pitch_or_linear_size < file_size - 0x80) ? header->pitch_or_linear_size : file_size - 0x80;
270-
*mips = 1;
273+
int data_size = 0;
274+
275+
// Calculate data size, including all mipmaps
276+
if (header->mipmap_count > 1) data_size = header->pitch_or_linear_size + header->pitch_or_linear_size / 3;
277+
else data_size = header->pitch_or_linear_size;
271278

272279
image_data = RL_MALLOC(data_size*sizeof(unsigned char));
273280

0 commit comments

Comments
 (0)