-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_dec_test_had.cpp
More file actions
286 lines (233 loc) · 10.2 KB
/
cpp_dec_test_had.cpp
File metadata and controls
286 lines (233 loc) · 10.2 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "image_encoding_had.hpp"
#include "encoding_library/ArithmeticCoder.hpp"
#include "encoding_library/BitIoStream.hpp"
#include "encoding_library/FrequencyTable.hpp"
#include <fstream>
#include <vector>
#include <iostream>
#include <stdexcept>
#define BLOCK_SIZE 8
using namespace std;
void arithmeticDecodeToFile(const std::string& inputFile, const std::string& outputFile) {
// Open input file for reading
std::ifstream inFile(inputFile, std::ios::binary);
if (!inFile.is_open()) {
throw std::runtime_error("Failed to open input file.");
}
// Read metadata (rows, cols, channels)
int32_t rows, cols, channels;
inFile.read(reinterpret_cast<char*>(&rows), sizeof(rows));
inFile.read(reinterpret_cast<char*>(&cols), sizeof(cols));
inFile.read(reinterpret_cast<char*>(&channels), sizeof(channels));
// Validate metadata
if (rows <= 0 || cols <= 0 || channels <= 0) {
throw std::runtime_error("Invalid image dimensions.");
}
// Read number of unique symbols
uint32_t numSymbols;
inFile.read(reinterpret_cast<char*>(&numSymbols), sizeof(numSymbols));
// Read symbols and frequencies
std::vector<uint16_t> symbols(numSymbols);
std::vector<uint32_t> freqs(numSymbols);
for (size_t i = 0; i < numSymbols; ++i) {
uint32_t symbol, freq;
inFile.read(reinterpret_cast<char*>(&symbol), sizeof(uint32_t));
inFile.read(reinterpret_cast<char*>(&freq), sizeof(uint32_t));
symbols[i] = static_cast<uint16_t>(symbol);
freqs[i] = freq;
}
// Create a frequency table for decoding
SimpleFrequencyTable freqTable(freqs);
// Initialize arithmetic decoder
BitInputStream bitIn(inFile);
ArithmeticDecoder decoder(32, bitIn);
// Decode pixel data
size_t totalPixels = static_cast<size_t>(rows) * cols * channels;
std::vector<int32_t> imgData(totalPixels);
for (size_t i = 0; i < totalPixels; ++i) {
uint32_t idx = decoder.read(freqTable);
imgData[i] = symbols[idx];
}
// Close input file
inFile.close();
// Write decoded data to output binary file
std::ofstream outFile(outputFile, std::ios::binary);
if (!outFile.is_open()) {
throw std::runtime_error("Failed to open output file.");
}
// Write metadata and decoded pixel data
outFile.write(reinterpret_cast<const char*>(&rows), sizeof(rows));
outFile.write(reinterpret_cast<const char*>(&cols), sizeof(cols));
outFile.write(reinterpret_cast<const char*>(&channels), sizeof(channels));
outFile.write(reinterpret_cast<const char*>(imgData.data()), imgData.size() * sizeof(uint16_t));
outFile.close();
std::cout << "Decoding complete. Output written to " << outputFile << std::endl;
}
BigPixelMatrix vectorToMatrix(const vector<int32_t>& data, int32_t rows, int32_t cols, int32_t channels) {
BigPixelMatrix matrix(rows, vector<vector<int32_t>>(cols, vector<int32_t>(channels)));
size_t index = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
for (int k = 0; k < channels; ++k) {
matrix[i][j][k] = data[index++];
}
}
}
return matrix;
}
BigPixelMatrix arithmeticDecodeToMatrix(const std::string& inputFile) {
// Open input file for reading
std::ifstream inFile(inputFile, std::ios::binary);
if (!inFile.is_open()) {
throw std::runtime_error("Failed to open input file.");
}
// Read metadata (rows, cols, channels)
int32_t rows, cols, channels;
inFile.read(reinterpret_cast<char*>(&rows), sizeof(rows));
inFile.read(reinterpret_cast<char*>(&cols), sizeof(cols));
inFile.read(reinterpret_cast<char*>(&channels), sizeof(channels));
// Validate metadata
if (rows <= 0 || cols <= 0 || channels <= 0) {
throw std::runtime_error("Invalid image dimensions.");
}
// Read number of unique symbols
int32_t numSymbols;
inFile.read(reinterpret_cast<char*>(&numSymbols), sizeof(numSymbols));
// Read symbols and frequencies
std::vector<int32_t> symbols(numSymbols);
std::vector<uint32_t> freqs(numSymbols);
for (size_t i = 0; i < numSymbols; ++i) {
uint32_t symbol, freq;
inFile.read(reinterpret_cast<char*>(&symbol), sizeof(int32_t));
inFile.read(reinterpret_cast<char*>(&freq), sizeof(int32_t));
symbols[i] = static_cast<int32_t>(symbol);
freqs[i] = freq;
}
// Create a frequency table for decoding
SimpleFrequencyTable freqTable(freqs);
// Initialize arithmetic decoder
BitInputStream bitIn(inFile);
ArithmeticDecoder decoder(32, bitIn);
// Decode pixel data
size_t totalPixels = static_cast<size_t>(rows) * cols * channels;
std::vector<int32_t> imgData(totalPixels);
for (size_t i = 0; i < totalPixels; ++i) {
uint32_t idx = decoder.read(freqTable);
imgData[i] = symbols[idx];
}
// Close input file
inFile.close();
return vectorToMatrix(imgData, rows, cols, channels);
}
void removeLastRowAndColumn(BigPixelMatrix& matrix) {
if (matrix.empty() || matrix[0].empty()) {
return;
}
// Remove last row
matrix.pop_back();
// Remove last column from each remaining row
for (auto& row : matrix) {
row.pop_back();
}
}
void writeToFile(BigPixelMatrix imgData, const std::string& outputFile) {
// Write decoded data to output binary file
std::ofstream outFile(outputFile, std::ios::binary);
if (!outFile.is_open()) {
throw std::runtime_error("Failed to open output file.");
}
//removeLastRowAndColumn(imgData);
int32_t rows = imgData.size();
int32_t cols = imgData[0].size();
int32_t channels = imgData[0][0].size();
cout << "C++ Size:" << rows << cols << endl;
// Create a flat buffer
vector<int32_t> flatData;
flatData.reserve(rows * cols * channels);
// Flatten the 3D matrix
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
for (int k = 0; k < channels; ++k) {
flatData.push_back(static_cast<int32_t>(imgData[i][j][k]));
}
}
}
// Write metadata and decoded pixel data
outFile.write(reinterpret_cast<const char*>(&rows), sizeof(rows));
outFile.write(reinterpret_cast<const char*>(&cols), sizeof(cols));
outFile.write(reinterpret_cast<const char*>(&channels), sizeof(channels));
outFile.write(reinterpret_cast<const char*>(flatData.data()), flatData.size() * sizeof(int32_t));
outFile.close();
std::cout << "Decoding complete. Output written to " << outputFile << std::endl;
}
BigPixelMatrix reverse_transform_matrix(const BigPixelMatrix& transformedMatrix,
const TransformationMatrix& transformationMatrix,
const TransformationMatrix& transposedTransformationMatrix) {
int padded_height = transformedMatrix.size();
int padded_width = transformedMatrix[0].size();
int channels = transformedMatrix[0][0].size();
int block_size = BLOCK_SIZE;
int orig_height = 940;
int orig_width = 940;
// Create output matrix with original dimensions
BigPixelMatrix pixelMatrix(orig_height, BigPixelRow(orig_width, vector<int32_t>(channels, 0)));
for (int channel = 0; channel < channels; ++channel) {
for (int row = 0; row < padded_height; row += block_size) {
for (int col = 0; col < padded_width; col += block_size) {
vector<vector<int32_t>> segment(block_size, vector<int32_t>(block_size));
// Extract the segment for the current channel
for (int i = 0; i < block_size; ++i) {
for (int j = 0; j < block_size; ++j) {
segment[i][j] = transformedMatrix[row + i][col + j][channel];
}
}
// Apply inverse transformation
segment = matrix_multiply(transposedTransformationMatrix, segment);
//test
if (channel == 0 && row == 0 && col == 0) testVec(segment);
segment = matrix_multiply(segment, transformationMatrix);
//test
if (channel == 0 && row == 0 && col == 0) testVec(segment);
// Store only within original dimensions
for (int i = 0; i < block_size; ++i) {
for (int j = 0; j < block_size; ++j) {
if (row + i < orig_height && col + j < orig_width) {
int32_t value = segment[i][j]/(BLOCK_SIZE*BLOCK_SIZE);360000;
// Clamp to uint16_t range (0 to 65535)
value = std::max(0, std::min(2147483647, value));
pixelMatrix[row + i][col + j][channel] = static_cast<int32_t>(value);
}
}
}
}
}
}
return pixelMatrix;
}
void test (BigPixelMatrix pixelMatrix) {
int rows = pixelMatrix.size();
int cols = pixelMatrix[0].size();
int ch = pixelMatrix[0][0].size();
cout << "Rows: " << rows << ", Cols: " << cols << ", Ch: " << ch << endl;
for (int row = 0; row < 8; row++){
for (int col = 0; col < 8; col++){
cout << pixelMatrix[row][col][0] << " " ;
}cout << endl;}cout << endl;
}
int main(int argc, char *argv[]) {
string src_image_path = argv[1];
//arithmeticDecodeToFile(src_image_path, "/root/tcu/data/results_cluster/tmp_truck/compression/iteration_30000/cmdc_compressed/temp_t.bin");
TransformationMatrix transformationMatrix = get_transformation_matrix(8);
TransformationMatrix transposedTransformationMatrix = get_transposed_transformation_matrix(8);
BigPixelMatrix decodedMatrix = arithmeticDecodeToMatrix(src_image_path);
test(decodedMatrix);
cout << "finished decoding" <<endl;
if(decodedMatrix.size() % BLOCK_SIZE != 0) {
writeToFile(decodedMatrix, "/root/tcu/data/results_cluster/tmp_truck/compression/iteration_30000/cmdc_compressed/temp_t.bin");
return 0;
}
BigPixelMatrix revTransMatrix = reverse_transform_matrix(decodedMatrix, transformationMatrix, transposedTransformationMatrix);
//test(revTransMatrix);
writeToFile(revTransMatrix, "/root/tcu/data/results_cluster/tmp_truck/compression/iteration_30000/cmdc_compressed/temp_t.bin");
return 0;
}