-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressToolsLib.cpp
More file actions
130 lines (114 loc) · 3.64 KB
/
CompressToolsLib.cpp
File metadata and controls
130 lines (114 loc) · 3.64 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "CompressToolsLib.h"
#include "CompressedImage.h"
#include "Logging.h"
#include <fstream>
#include <vector>
#include <Windows.h>
#include <sstream>
#include <mutex>
using namespace CompressToolsLib;
struct CompressToolsLib::CompressedImageFile
{
std::string filename;
std::shared_ptr<CompressedImage> image;
// TODO REMOVE AFTER TESTING used if preloading
std::vector<symbol_t> decodedPixels;
std::mutex lock;
};
__declspec(dllexport) CompressedImageFileHdl CompressToolsLib::OpenImage(const char* filename, ImageMode mode)
{
// try open
std::shared_ptr<CompressedImage> image = CompressedImage::OpenStream(filename);
// error opening
if (!image)
{
CompressTools::ErrorLog(std::string("Error opening image: ") + filename);
return nullptr;
}
// decode
CompressedImageFileHdl imageHdl = new CompressedImageFile();
imageHdl->filename = filename;
imageHdl->image = image;
if (mode == ImageMode::Preload)
imageHdl->decodedPixels = imageHdl->image->GetBottomLevelPixels();
// TODO temporary - delete this later
float memoryUsage = (imageHdl->image->GetMemoryUsage() / 1024) / 1024.0f;
std::cout << "Heightmap memory usage: " << memoryUsage << "MB" << std::endl;
// free up memory
imageHdl->image->ClearBlockCache();
return imageHdl;
}
__declspec(dllexport) uint16_t CompressToolsLib::ReadHeightValue(CompressedImageFileHdl image, uint32_t x, uint32_t y)
{
if (x >= image->image->GetWidth()
|| y >= image->image->GetHeight())
{
//MessageBoxA(0, "Out-of-bounds pixel read!", "Debug", MB_OK);
//std::stringstream msg;
//msg << "Index: " << pixelIndex << " Size: " << image->pixels.size() << std::endl;
//MessageBoxA(0, msg.str().c_str(), "Debug", MB_OK);
return 0;
}
image->lock.lock();
symbol_t val;
// HACK if preloading use preloaded cache
if(image->decodedPixels.size() > 0)
val = image->decodedPixels[y * image->image->GetWidth() + x];
else
val = image->image->GetPixel(x, y);
image->lock.unlock();
return val;
}
__declspec(dllexport) void CompressToolsLib::CloseImage(CompressedImageFileHdl image)
{
delete image;
}
__declspec(dllexport) void CompressToolsLib::SetLoggers(void(*debugLogger)(const char*), void(*errorLogger)(const char*))
{
CompressTools::SetDebugLogger(debugLogger);
CompressTools::SetErrorLogger(errorLogger);
}
__declspec(dllexport) uint32_t CompressToolsLib::GetImageWidthInBlocks(CompressedImageFileHdl image)
{
image->lock.lock();
uint32_t val = image->image->GetWidthInBlocks();
image->lock.unlock();
return val;
}
__declspec(dllexport) uint32_t CompressToolsLib::GetImageHeightInBlocks(CompressedImageFileHdl image)
{
image->lock.lock();
uint32_t val = image->image->GetHeightInBlocks();
image->lock.unlock();
return val;
}
// outputs w
__declspec(dllexport) void CompressToolsLib::GetBlockLODs(CompressedImageFileHdl image, uint8_t* output)
{
image->lock.lock();
std::vector<uint8_t> blockLevels = image->image->GetBlockLevels();
image->lock.unlock();
memcpy(output, &blockLevels[0], sizeof(uint8_t) * blockLevels.size());
}
__declspec(dllexport) uint32_t CompressToolsLib::GetMaxLOD(CompressedImageFileHdl image)
{
image->lock.lock();
uint32_t val = image->image->GetTopLOD();
image->lock.unlock();
return val;
}
__declspec(dllexport) size_t CompressToolsLib::GetMemoryUsage(CompressedImageFileHdl image)
{
image->lock.lock();
size_t val = image->image->GetMemoryUsage();
image->lock.unlock();
return val;
}
__declspec(dllexport) void CompressToolsLib::GetBottomPixels(CompressedImageFileHdl image, uint16_t* values)
{
image->lock.lock();
std::vector<symbol_t> vals = image->image->GetBottomLevelPixels();
memcpy(values, &vals[0], sizeof(symbol_t) * vals.size());
image->lock.unlock();
return;
}