-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblp.cc
More file actions
55 lines (46 loc) · 1.5 KB
/
blp.cc
File metadata and controls
55 lines (46 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "blp.h"
#include <cstring>
BLPHeader::BLPHeader(const void *const buffer)
{
const uint8_t *const bytes = (uint8_t *) buffer;
const uint32_t *const dwords = (uint32_t *) buffer;
type = dwords[1]; // Texture type: 0 = JPG, 1 = S3TC
compression = bytes[8]; // Compression mode: 1 = raw, 2 = DXTC
alpha_depth = bytes[9]; // 0, 1, 4, or 8
alpha_type = bytes[10]; // 0, 1, 7, or 8
has_mips = bytes[11]; // 0 = no mips levels, 1 = has mips (number of levels determined by image size)
width = dwords[3];
height = dwords[4];
std::memcpy(mipmap_offsets, dwords + 5, 64);
std::memcpy(mipmap_lengths, dwords + 21, 64);
std::memcpy(palette, dwords + 37, 1024);
}
BLPFile::BLPFile(const void *const buffer) :
header(buffer)
{
size_t size = 0;
for (unsigned int l = 0; header.mipmap_offsets[l]; l++)
size = header.mipmap_offsets[l] + header.mipmap_lengths[l];
const size_t data_size = size - sizeof(BLPHeader);
data = new uint8_t[data_size];
std::memcpy(data, ((uint8_t *) buffer) + sizeof(BLPHeader), data_size);
}
BLPFile::~BLPFile()
{
delete []data;
}
const void *BLPFile::GetMipMap(const short level, uint32_t &w, uint32_t &h, uint32_t &length) const
{
if (!header.mipmap_offsets[level])
{
w = 0; h = 0; length = 0;
return NULL;
}
w = header.width >> level ? header.width >> level : 1;
h = header.height >> level ? header.height >> level : 1;
length = header.mipmap_lengths[level];
return data + header.mipmap_offsets[level] - sizeof(BLPHeader);
}
void BLPFile::Resize(uint32_t w, uint32_t h)
{
}