-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode.cpp
251 lines (199 loc) · 5.88 KB
/
decode.cpp
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
#include "common.h"
#include "arithmetic_decoder.h"
#include "difference_adaptive_model.h"
#include "jpeg_ls_model.h"
#include "bitmap.h"
#include "adaptive_model.h"
#include "ppm_model.h"
#ifdef WINDOWS
#include "file_map.h"
#include "file_writer.h"
#else
#include "u_file_map.h"
#include "u_file_writer.h"
#endif //WINDOWS
#include <iostream>
using namespace std;
void print_usage()
{
cout << "\nUsage: decode -enc=x input output\n\n"
<< " -enc=x x: modello da utilizzare\n"
<< " 1: modello ppm\n"
<< " 2: modello jpeg-ls per bitmap a 24bpp\n"
<< " 3: modello jpeg-ls per bitmap a 8bpp\n"
<< " 4: modello adattivo per BYTE (generale)\n"
<< " 5: modello differenziale per BYTE\n"
<< "\nCopyright (C) 2001 Lorenzo Dematte'. Tutti i diritti riservati\n";
}
void decode_ls24(char* szInputName, char* szOutputName)
{
DWORD dwFileSize, dwHeaderSize;
{
bitmap_header* bhCompInput;
file_map<bit> mapCompressedInput(szInputName, sizeof(bitmap_header));
bhCompInput = (bitmap_header*)(mapCompressedInput.get_header());
dwHeaderSize = bhCompInput->get_header_size();
dwFileSize = bhCompInput->get_file_size();
file_writer<bit> bmpWriter;
bmpWriter.create(szOutputName);
bmpWriter.write_header((BYTE*)bhCompInput, bhCompInput->get_header_size());
}
file_map<bit> mapCompressedInput(szInputName, dwHeaderSize);
bitmap_true<rgb_color24> bmpFileOut(szOutputName, dwFileSize);
arithmetic_decoder decoder(&mapCompressedInput);
jpeg_ls_24_model model(&decoder, &bmpFileOut);
for (DWORD i = 0; i < bmpFileOut.height(); ++i)
for (DWORD j = 0; j < bmpFileOut.width(); ++j)
{
model.predict5(i, j);
rgb_color24 rgb = model.decode_symbol();
bmpFileOut.set_pixel(i, j, rgb);
}
}
void decode_ppm(char* szInputName, char* szOutputName)
{
file_map<bit> map_comp_input(szInputName);
file_writer<char> file_out;
file_out.create(szOutputName);
arithmetic_decoder dec(&map_comp_input);
ppm_model<5> mdl(&dec);
DWORD* dwBuffer = (DWORD*)map_comp_input.get_header();
DWORD dwExpSize = dwBuffer[1];
if (dwBuffer[0] == g_dwMagic)
{
for(DWORD i = 0; i < (dwExpSize / sizeof(BYTE)); ++i)
{
char c = mdl.decode_symbol();
file_out.send(c);
}
}
}
void decode_ls8(char* szInputName, char* szOutputName)
{
DWORD dwFileSize, dwHeaderSize;
{
bitmap_header* bhCompInput;
file_map<bit> mapCompressedInput(szInputName, sizeof(bitmap_header));
bhCompInput = (bitmap_header*)(mapCompressedInput.get_header());
dwHeaderSize = bhCompInput->get_header_size();
dwFileSize = bhCompInput->get_file_size();
file_writer<bit> bmpWriter;
bmpWriter.create(szOutputName);
bmpWriter.write_header((BYTE*)bhCompInput, bhCompInput->get_header_size());
}
file_map<bit> mapCompressedInput(szInputName, dwHeaderSize);
bitmap8bpp bmpFileOut(szOutputName, dwFileSize);
arithmetic_decoder decoder(&mapCompressedInput);
jpeg_ls_model model(&decoder, &bmpFileOut);
for (DWORD i = 0; i < bmpFileOut.height(); ++i)
for (DWORD j = 0; j < bmpFileOut.width(); ++j)
{
model.predict5(i, j);
BYTE b = model.decode_symbol();
bmpFileOut.set_greyed_pixel(i, j, b);
}
}
void decode_BYTE(char* szInputName, char* szOutputName)
{
file_map<bit> map_comp_input(szInputName);
file_writer<BYTE> file_out;
file_out.create(szOutputName);
arithmetic_decoder dec(&map_comp_input);
adaptive_model<BYTE> mdl(&dec);
DWORD* dwBuffer = (DWORD*)map_comp_input.get_header();
DWORD dwExpSize = dwBuffer[1];
if (dwBuffer[0] == g_dwMagic)
{
for(DWORD i = 0; i < (dwExpSize / sizeof(BYTE)); ++i)
{
BYTE b = mdl.decode_symbol();
file_out.send(b);
}
}
}
void decode_diff_BYTE(char* szInputName, char* szOutputName)
{
file_map<bit> map_comp_input(szInputName);
file_writer<BYTE> file_out;
file_out.create(szOutputName);
arithmetic_decoder dec(&map_comp_input);
difference_adaptive_model<BYTE> mdl(&dec);
DWORD* dwBuffer = (DWORD*)map_comp_input.get_header();
DWORD dwExpSize = dwBuffer[1];
if (dwBuffer[0] == g_dwMagic)
{
for(DWORD i = 0; i < (dwExpSize / sizeof(BYTE)); ++i)
{
BYTE b = mdl.decode_symbol();
file_out.send(b);
}
}
}
int main (int argc, char* argv[])
{
char* szInputName = NULL;
char* szOutputName = NULL;
DWORD dwEncoderType = 0;
//parse the command line
if (argc != 4)
{
print_usage();
return -1;
}
if(strncmp(argv[1], "-enc=", 5) == 0)
{
dwEncoderType = atoi(&(argv[1][5]));
}
else
{
print_usage();
return -1;
}
szInputName = argv[2];
szOutputName = argv[3];
if ((!szInputName) || (!szOutputName))
{
print_usage();
return -1;
}
try
{
switch(dwEncoderType)
{
case 1:
decode_ppm(szInputName, szOutputName);
break;
case 2:
decode_ls24(szInputName, szOutputName);
break;
case 3:
decode_ls8(szInputName, szOutputName);
break;
case 4:
decode_BYTE(szInputName, szOutputName);
break;
case 5:
decode_diff_BYTE(szInputName, szOutputName);
break;
}
}
catch(bad_file*)
{
cout << "\nERRORE:\n"
<< "Impossibile aprire o creare i files necessari.\n"
<< "Controllare che i files esistano, non siano bloccati "
<< "da altri processi e che si abbiano i permessi necessari.\n\n";
}
catch(wrong_bpp*)
{
cout << "\nERRORE:\n"
<< "Il file bitmap specificato ha una profondita' di colore differente "
<< "da quella indicata: provare a specificare un formato diverso\n\n";
}
catch(...)
{
cout << "\nERRORE:\n"
<< "Allocazione memoria fallita!\n\n";
}
return 0;
}