-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsizecache.cpp
More file actions
executable file
·48 lines (40 loc) · 1.42 KB
/
Copy pathsizecache.cpp
File metadata and controls
executable file
·48 lines (40 loc) · 1.42 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <unordered_map>
#include "ddsfs.h"
using namespace std;
// Using int will fail with DDS files over 2 GB... but I think that's acceptable for now.
// Not bothering to use any sort of expiry, since this will be able to store thousands of files per MB of RAM.
static unordered_map<string,int> sizecache;
int dds_size(int width, int height, int alpha) {
if (!poweroftwo(width) || !poweroftwo(height)) return width * height * 4;
/*int pixels = 0;
int mips = 0;
// This really seems like it could be done with a single formula.
while ((height >> mips) >= MINSIZE && (width >> mips) >= MINSIZE) {
pixels += (height >> mips) * (width >> mips);
mips++;
}*/
// I *think* this works...
int pixels = (((height<<1)-1)-((MINSIZE<<1)-1)) * (((width<<1)-1)-((MINSIZE<<1)-1));
if (config.compress) {
if (alpha) return sizeof(DDS_HEADER) + (pixels);
return sizeof(DDS_HEADER) + (pixels/2);
} else {
return sizeof(DDS_HEADER) + (pixels*4);
}
}
int sizecache_get(const char* name) {
auto i = sizecache.find(name);
if (i != sizecache.end()) {
if (DEBUG >= 3) printf("SizeCache: Found %d bytes for '%s'\n", i->second, name);
return i->second;
}
if (DEBUG >= 3) printf("SizeCache: No entry for '%s'\n", name);
return -1;
}
void sizecache_set(const char* name, int size) {
if (DEBUG >= 3) printf("SizeCache: Set %d bytes for '%s'\n", size, name);
sizecache[name] = size;
}