-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressTools.cpp
More file actions
217 lines (193 loc) · 8.38 KB
/
CompressTools.cpp
File metadata and controls
217 lines (193 loc) · 8.38 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// CompressTools.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#define FREEIMAGE_LIB
#include <FreeImage.h>
#include <iostream>
#include <stdint.h>
#include <vector>
#include <unordered_map>
#include <cmath>
#include "Release_Assert.h"
#include <algorithm>
#include <fstream>
#include "RansEncode.h"
#include "CompressedImage.h"
#include <chrono>
void GetSymbolEntropy(std::vector<uint16_t> symbols)
{
std::cout << "Counting symbols..." << std::endl;
std::unordered_map<uint16_t, uint64_t> symbolCounts;
for (auto symbol : symbols)
{
symbolCounts.try_emplace(symbol, 0);
symbolCounts[symbol] += 1;
}
std::cout << "Getting entropy..." << std::endl;
uint64_t numSymbols = symbols.size();
double totalEntropy = 0.0;
std::vector<uint16_t> symbolValues;
std::vector<double> symbolEntropies;
for (auto symbolCount : symbolCounts)
{
//std::cout << symbolCount.first << " " << symbolCount.second << std::endl;
symbolValues.push_back(symbolCount.first);
double count = (double)symbolCount.second;
double symbolProb = count / numSymbols;
double symbolEntropy = count * -log2(symbolProb);
symbolEntropies.push_back(symbolEntropy);
totalEntropy += symbolEntropy;
}
std::cout << "Total entropy: " << (uint64_t)totalEntropy << " bits " << std::endl;
std::cout << "Total entropy: " << (uint64_t)(totalEntropy/8) << " bytes " << std::endl;
}
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char* message) {
printf("\n*** ");
if (fif != FIF_UNKNOWN) {
printf("%s Format\n", FreeImage_GetFormatFromFIF(fif));
}
printf(message);
printf(" ***\n");
}
int main(int argc, char* argv[])
{
std::string inputFileName = "./data/newland/land/fullmap.tif";
std::string outputFileName = "./data/newland/land/fullmap.cif";
if (argc >= 2)
inputFileName = argv[1];
if (argc >= 3)
outputFileName = argv[2];
std::cout << "Input: " << inputFileName << std::endl;
std::cout << "Output: " << outputFileName << std::endl;
// benchmark test code
if (false)
{
auto start = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "Decode benchmark..." << std::endl;
start = std::chrono::high_resolution_clock::now();
std::shared_ptr<CompressedImage> streamedImage = CompressedImage::OpenStream(outputFileName);
duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "init: " << duration.count() << std::endl;
start = std::chrono::high_resolution_clock::now();
std::vector<symbol_t> decodedPixels = streamedImage->GetBottomLevelPixels();
duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "decode: " << duration.count() << std::endl;
return 0;
}
// normal compressor
std::cout << "Opening image..." << std::endl;
FreeImage_Initialise();
FreeImage_SetOutputMessage(FreeImageErrorHandler);
FIBITMAP* bitmap = FreeImage_Load(FREE_IMAGE_FORMAT::FIF_TIFF, inputFileName.c_str(), TIFF_DEFAULT);
int width = FreeImage_GetWidth(bitmap);
int height = FreeImage_GetHeight(bitmap);
int precision = FreeImage_GetBPP(bitmap);
if (precision == 16)
{
std::cout << "Uncompressed size: " << width*height*sizeof(uint16_t) << " bytes" << std::endl;
std::cout << "Reading pixels..." << std::endl;
std::vector<uint16_t> values;
values.resize(width * height);
// Undo Free_Image transform
// Actually tested it properly this time...
for (int y = 0; y < height; ++y)
{
BYTE* bits = FreeImage_GetScanLine(bitmap, (height-1)-y);
memcpy(&values[y*width], bits, width * sizeof(uint16_t));
}
std::cout << "Generating wavelet image..." << std::endl;
//std::shared_ptr<WaveletLayer> bottomLayer = std::make_shared<WaveletLayer>(values, width, height);
//std::shared_ptr<CompressedImage> compressedImage = std::make_shared<CompressedImage>(bottomLayer);
std::shared_ptr<CompressedImage> compressedImage = std::make_shared<CompressedImage>(values, width, height, 32);
std::cout << "Serializing..." << std::endl;
std::vector<uint8_t> imageBytes = compressedImage->Serialize();
std::cout << "Final encoded bytes: " << imageBytes.size() << std::endl;
// write to disk
std::cout << "Writing bytes..." << std::endl;
std::ofstream compressedFile(outputFileName, std::ios::binary);
if (compressedFile.is_open())
{
compressedFile.write((const char*)&imageBytes[0], imageBytes.size());
compressedFile.close();
}
else
{
std::cerr << "Error opening output file!";
}
auto start = std::chrono::high_resolution_clock::now();
std::shared_ptr<CompressedImage> decodedImage = CompressedImage::OpenStream(outputFileName);
std::chrono::duration<double> duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "File open time: " << duration.count() << std::endl;
/*
std::cout << decodedImage->GetTopLOD() << std::endl;
std::vector<uint8_t> blockLevels = decodedImage->GetBlockLevels();
std::cout << int(blockLevels[0]) << std::endl;
decodedImage->GetPixel(0, 0);
blockLevels = decodedImage->GetBlockLevels();
std::cout << int(blockLevels[0]) << std::endl;
decodedImage->GetPixel(64, 0);
blockLevels = decodedImage->GetBlockLevels();
std::cout << int(blockLevels[0]) << std::endl;
decodedImage->GetPixel(32, 0);
blockLevels = decodedImage->GetBlockLevels();
std::cout << int(blockLevels[0]) << std::endl;
decodedImage->GetPixel(16, 0);
decodedImage->GetPixel(16, 16);
blockLevels = decodedImage->GetBlockLevels();
std::cout << int(blockLevels[0]) << std::endl;
decodedImage->GetPixel(8, 0);
blockLevels = decodedImage->GetBlockLevels();
std::cout << int(blockLevels[0]) << std::endl;
*/
// test parent reads
for (int y = 0; y < height; y += 16)
{
for (int x = 0; x < width; x += 16)
{
uint16_t sourcePixel = values[y * width + x];
uint16_t decodedPixel = decodedImage->GetPixel(x, y);
if (decodedPixel != sourcePixel)
{
std::cout << "Decoded pixel values at (" << x << ", " << y << ") did not match." << std::endl;
}
assert_release(decodedPixel == sourcePixel);
}
}
// test aligned reads
std::cout << "Testing aligned reads..." << std::endl;
start = std::chrono::high_resolution_clock::now();
for (int y = 1024; y < 2048; y += 4)
{
for (int x = 1024; x < 2048; x += 4)
{
uint16_t sourcePixel = values[y * width + x];
uint16_t decodedPixel = decodedImage->GetPixel(x, y);
if (decodedPixel != sourcePixel)
{
std::cout << "Decoded pixel values at (" << x << ", " << y << ") did not match." << std::endl;
}
assert_release(decodedPixel == sourcePixel);
}
}
duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "Test time: " << duration.count() << std::endl;
std::cout << "Decoding bottom-level pixels..." << std::endl;
start = std::chrono::high_resolution_clock::now();
std::vector<uint16_t> decodedPixels = decodedImage->GetBottomLevelPixels();
duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "Decode time: " << duration.count() << std::endl;
for (int i = 0; i < values.size(); ++i)
{
assert_release(values[i] == decodedPixels[i]);
if (values[i] != decodedPixels[i])
{
std::cout << "Decoded wavelet values at " << i << " did not match." << std::endl;
}
}
std::cout << "Values checked!" << std::endl;
decodedImage.reset();
std::cout << "Done!" << std::endl;
}
FreeImage_Unload(bitmap);
FreeImage_DeInitialise();
}