decompress.cpp
· 4.2 KiB · C++
原始檔案
#include <stdio.h>
#include <stdint.h>
#include <mutex>
#include <atomic>
#include <thread>
#include <cstdint>
#include <iostream>
#include <vector>
#include <exception>
#include <queue>
#include <sstream>
#include <string>
#include <chrono>
#include <random>
#include <functional>
#include "..\packetDecoder\RiceDecompression.h"
//#define PREFIX "1696_0_"
//#define PIXELS 3463
int PIXELS;
std::string PREFIX;
bool debugMode = false;
void processFile(std::string &filename, std::string &outputname) {
//1696_0_1395.lrit
unsigned char *input;
FILE *f = fopen(filename.c_str(), "rb");
if (f != NULL) {
fseek(f, 0L, SEEK_END);
long sz = ftell(f);
fseek(f, 0L, SEEK_SET);
input = new unsigned char[sz];
fread(input, sz, 1, f);
CRiceDecompression *Rice = new CRiceDecompression(49, 8, 16, PIXELS, 1);
bool Worked = Rice->Decompress(input, sz);
if (!Worked) {
std::cerr << "Failed to decompress" << std::endl;
}
FILE *o = fopen(outputname.c_str(), "ab");
fwrite(Rice->Ptr(), Rice->Size(), 1, o);
fclose(o);
fclose(f);
delete input;
} else {
unsigned char *output = new unsigned char[PIXELS];
memset(output, 0x00, PIXELS);
if (debugMode) std::cout << "Inexistent file " << filename << ". Adding empty frame." << std::endl;
FILE *o = fopen(outputname.c_str(), "ab");
fwrite(output, PIXELS, 1, o);
fclose(o);
delete output;
}
}
std::string buildFilename(std::string prefix, int n) {
std::stringstream ss;
ss << prefix << n << ".lrit";
return ss.str();
}
int doOnlyOne(std::string filename, int Pixels) {
FILE *f;
if (debugMode) std::cout << "Filename: " << filename << " Pixels: " << Pixels << std::endl;
f = fopen(filename.c_str(), "rb");
if (f == NULL) {
std::cerr << "Error Opening file" << std::endl;
int x;
std::cin >> x;
return -1;
}
fseek(f, 0L, SEEK_END);
long sz = ftell(f);
fseek(f, 0L, SEEK_SET);
if (debugMode) std::cout << "File size: " << sz << std::endl;
unsigned char *data = new unsigned char[sz];
fread(data, sz, 1, f);
//Rice Decompression
bool Worked;
CRiceDecompression *Rice = new CRiceDecompression(49, 8, 16, Pixels, 1);
Worked = Rice->Decompress(data, sz);
if (!Worked) {
std::cerr << "Failed to decompress" << std::endl;
}
if (debugMode) std::cout << "Worked: " << Worked << std::endl;
fclose(f);
f = fopen(std::string(filename + ".decomp").c_str(), "wb");
fwrite(Rice->Ptr(), Rice->Size(), 1, f);
fclose(f);
//Frees input memory
int x;
std::cin >> x;
delete[] data;
}
int main(int argc, char *argv[]) {
if (argc < 5) {
int Pixels;
std::string filename;
if (argc >= 3) {
std::istringstream(argv[1]) >> Pixels;
filename = std::string(argv[2]);
} else {
filename = std::string("C:\\Users\\lucas\\Desktop\\Data\\1725_0_10212.lrit");
Pixels = 3463;
}
return doOnlyOne(filename, Pixels);
}
debugMode = argc == 7;
PREFIX = std::string(argv[1]);
std::istringstream(argv[2]) >> PIXELS;
int startNumber = 10212;
int endNumber = 10323;
std::istringstream(argv[3]) >> startNumber;
std::istringstream(argv[4]) >> endNumber;
std::string baseFilename = buildFilename(PREFIX + "_decomp", startNumber - 1);
if (debugMode) std::cout << "Output file: " << baseFilename << std::endl;
if (debugMode) std::cout << "Reading first file with ID " << startNumber - 1 << " filename: " << buildFilename(PREFIX, startNumber - 1) << std::endl;
FILE *f = fopen(baseFilename.c_str(), "wb");
FILE *f2 = fopen(buildFilename(PREFIX, startNumber - 1).c_str(), "rb");
if (f == NULL || f2 == NULL) {
std::cerr << "Error opening base files" << std::endl;
return 1;
}
fseek(f2, 0L, SEEK_END);
long sz = ftell(f2);
fseek(f2, 0L, SEEK_SET);
//sz -= 10;
if (debugMode) std::cout << "Header size: " << sz << std::endl;
char *data = new char[sz];
fread(data, sz, 1, f2);
fwrite(data, sz, 1, f);
fclose(f);
fclose(f2);
if (debugMode) std::cout << "Header wrote. Reading sequence" << std::endl;
for (int i = startNumber; i <= endNumber; i++) {
std::string infile = buildFilename(PREFIX, i);
if (debugMode) std::cout << "Opening file " << infile << std::endl;
processFile(infile, baseFilename);
}
if (debugMode) std::cout << "Finished!" << std::endl;
if (argc < 6) {
int x;
std::cin >> x;
}
return 0;
}
| 1 | #include <stdio.h> |
| 2 | #include <stdint.h> |
| 3 | |
| 4 | #include <mutex> |
| 5 | #include <atomic> |
| 6 | #include <thread> |
| 7 | #include <cstdint> |
| 8 | #include <iostream> |
| 9 | #include <vector> |
| 10 | #include <exception> |
| 11 | #include <queue> |
| 12 | #include <sstream> |
| 13 | #include <string> |
| 14 | #include <chrono> |
| 15 | #include <random> |
| 16 | #include <functional> |
| 17 | |
| 18 | #include "..\packetDecoder\RiceDecompression.h" |
| 19 | |
| 20 | //#define PREFIX "1696_0_" |
| 21 | //#define PIXELS 3463 |
| 22 | |
| 23 | int PIXELS; |
| 24 | std::string PREFIX; |
| 25 | bool debugMode = false; |
| 26 | |
| 27 | void processFile(std::string &filename, std::string &outputname) { |
| 28 | //1696_0_1395.lrit |
| 29 | unsigned char *input; |
| 30 | FILE *f = fopen(filename.c_str(), "rb"); |
| 31 | if (f != NULL) { |
| 32 | fseek(f, 0L, SEEK_END); |
| 33 | long sz = ftell(f); |
| 34 | fseek(f, 0L, SEEK_SET); |
| 35 | input = new unsigned char[sz]; |
| 36 | fread(input, sz, 1, f); |
| 37 | CRiceDecompression *Rice = new CRiceDecompression(49, 8, 16, PIXELS, 1); |
| 38 | bool Worked = Rice->Decompress(input, sz); |
| 39 | if (!Worked) { |
| 40 | std::cerr << "Failed to decompress" << std::endl; |
| 41 | } |
| 42 | FILE *o = fopen(outputname.c_str(), "ab"); |
| 43 | fwrite(Rice->Ptr(), Rice->Size(), 1, o); |
| 44 | fclose(o); |
| 45 | fclose(f); |
| 46 | delete input; |
| 47 | } else { |
| 48 | unsigned char *output = new unsigned char[PIXELS]; |
| 49 | memset(output, 0x00, PIXELS); |
| 50 | if (debugMode) std::cout << "Inexistent file " << filename << ". Adding empty frame." << std::endl; |
| 51 | FILE *o = fopen(outputname.c_str(), "ab"); |
| 52 | fwrite(output, PIXELS, 1, o); |
| 53 | fclose(o); |
| 54 | delete output; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | std::string buildFilename(std::string prefix, int n) { |
| 59 | std::stringstream ss; |
| 60 | ss << prefix << n << ".lrit"; |
| 61 | return ss.str(); |
| 62 | } |
| 63 | |
| 64 | int doOnlyOne(std::string filename, int Pixels) { |
| 65 | FILE *f; |
| 66 | |
| 67 | if (debugMode) std::cout << "Filename: " << filename << " Pixels: " << Pixels << std::endl; |
| 68 | |
| 69 | f = fopen(filename.c_str(), "rb"); |
| 70 | |
| 71 | if (f == NULL) { |
| 72 | std::cerr << "Error Opening file" << std::endl; |
| 73 | int x; |
| 74 | std::cin >> x; |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | fseek(f, 0L, SEEK_END); |
| 79 | long sz = ftell(f); |
| 80 | fseek(f, 0L, SEEK_SET); |
| 81 | |
| 82 | if (debugMode) std::cout << "File size: " << sz << std::endl; |
| 83 | |
| 84 | unsigned char *data = new unsigned char[sz]; |
| 85 | |
| 86 | fread(data, sz, 1, f); |
| 87 | |
| 88 | //Rice Decompression |
| 89 | bool Worked; |
| 90 | CRiceDecompression *Rice = new CRiceDecompression(49, 8, 16, Pixels, 1); |
| 91 | Worked = Rice->Decompress(data, sz); |
| 92 | if (!Worked) { |
| 93 | std::cerr << "Failed to decompress" << std::endl; |
| 94 | } |
| 95 | if (debugMode) std::cout << "Worked: " << Worked << std::endl; |
| 96 | |
| 97 | fclose(f); |
| 98 | f = fopen(std::string(filename + ".decomp").c_str(), "wb"); |
| 99 | fwrite(Rice->Ptr(), Rice->Size(), 1, f); |
| 100 | fclose(f); |
| 101 | //Frees input memory |
| 102 | int x; |
| 103 | std::cin >> x; |
| 104 | delete[] data; |
| 105 | } |
| 106 | |
| 107 | int main(int argc, char *argv[]) { |
| 108 | if (argc < 5) { |
| 109 | int Pixels; |
| 110 | std::string filename; |
| 111 | if (argc >= 3) { |
| 112 | std::istringstream(argv[1]) >> Pixels; |
| 113 | filename = std::string(argv[2]); |
| 114 | } else { |
| 115 | filename = std::string("C:\\Users\\lucas\\Desktop\\Data\\1725_0_10212.lrit"); |
| 116 | Pixels = 3463; |
| 117 | } |
| 118 | return doOnlyOne(filename, Pixels); |
| 119 | } |
| 120 | debugMode = argc == 7; |
| 121 | PREFIX = std::string(argv[1]); |
| 122 | std::istringstream(argv[2]) >> PIXELS; |
| 123 | |
| 124 | int startNumber = 10212; |
| 125 | int endNumber = 10323; |
| 126 | |
| 127 | std::istringstream(argv[3]) >> startNumber; |
| 128 | std::istringstream(argv[4]) >> endNumber; |
| 129 | |
| 130 | std::string baseFilename = buildFilename(PREFIX + "_decomp", startNumber - 1); |
| 131 | if (debugMode) std::cout << "Output file: " << baseFilename << std::endl; |
| 132 | if (debugMode) std::cout << "Reading first file with ID " << startNumber - 1 << " filename: " << buildFilename(PREFIX, startNumber - 1) << std::endl; |
| 133 | |
| 134 | FILE *f = fopen(baseFilename.c_str(), "wb"); |
| 135 | FILE *f2 = fopen(buildFilename(PREFIX, startNumber - 1).c_str(), "rb"); |
| 136 | |
| 137 | if (f == NULL || f2 == NULL) { |
| 138 | std::cerr << "Error opening base files" << std::endl; |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | fseek(f2, 0L, SEEK_END); |
| 143 | long sz = ftell(f2); |
| 144 | fseek(f2, 0L, SEEK_SET); |
| 145 | //sz -= 10; |
| 146 | |
| 147 | if (debugMode) std::cout << "Header size: " << sz << std::endl; |
| 148 | |
| 149 | char *data = new char[sz]; |
| 150 | fread(data, sz, 1, f2); |
| 151 | fwrite(data, sz, 1, f); |
| 152 | fclose(f); |
| 153 | fclose(f2); |
| 154 | |
| 155 | if (debugMode) std::cout << "Header wrote. Reading sequence" << std::endl; |
| 156 | |
| 157 | |
| 158 | for (int i = startNumber; i <= endNumber; i++) { |
| 159 | std::string infile = buildFilename(PREFIX, i); |
| 160 | if (debugMode) std::cout << "Opening file " << infile << std::endl; |
| 161 | processFile(infile, baseFilename); |
| 162 | } |
| 163 | if (debugMode) std::cout << "Finished!" << std::endl; |
| 164 | if (argc < 6) { |
| 165 | int x; |
| 166 | std::cin >> x; |
| 167 | } |
| 168 | return 0; |
| 169 | } |