Utoljára aktív 1 month ago

LRIT Windows Decompressor (using LritRice.lib)

decompress.cpp Eredeti
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
23int PIXELS;
24std::string PREFIX;
25bool debugMode = false;
26
27void 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
58std::string buildFilename(std::string prefix, int n) {
59 std::stringstream ss;
60 ss << prefix << n << ".lrit";
61 return ss.str();
62}
63
64int 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
107int 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}