-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
486 lines (323 loc) · 12.2 KB
/
Main.java
File metadata and controls
486 lines (323 loc) · 12.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package art;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
public class Main {
private static int length;
private static int lengthOfOutput;
private static FileInputStream file;
static ArrayList<Symbol> symbols;
static double[] cumulativeProbabilities;
static double upperBound = 1;
static double lowerBound = 0;
enum SubsetPosition { UP, DOWN, MIDDLE, ELSE};
static double[] currentPartition;
static boolean endOfMessage;
private static FileOutputStream fileOut;
private static String code;
public static void main(String[] args) throws InterruptedException, IOException {
endOfMessage = false;
if(args[1].equals("kompresja")){
length = 0;
lengthOfOutput = 0;
file = new FileInputStream(args[0]); //otwieramy plik do kompresji
fileOut = new FileOutputStream("E:\\Studia\\Kodowanie i kompresja danych\\Lab\\Lab4\\out");
System.out.println("Plik: "+args[0]);
symbols = new ArrayList<>();
int key;
while( (key = file.read())!=-1){ //tworzymy liste symboli kodowych
length++;
int position = containsSymbol(key);
if (position!=-1){
symbols.get(position).amount ++;
} else {
symbols.add(new Symbol(key));
}
}
//liczenie entropii
double entropy = 0;
for (int i = 0; i < symbols.size(); i++){
double prob = ((double)symbols.get(i).amount)/length;
entropy += prob*Math.log(prob);
}
entropy /=-Math.log(2);
symbols.add(new Symbol(256)); // dodawanie symbolu specjalnego
length++;
code = "";
int counter = 0;
cumputeProbabilities(); //obliczanie prawdopodobieństw symboli
computeCumulativeProbabilities(); //obliczanie skumulowanych prawdopodobieństw
//zapisujemy słownik do pliku
int symbolsAmount = symbols.size()-1; //nie liczymy symbolu specjalnego
System.out.println("Ilosc symboli w slowniku: " + symbolsAmount);
fileOut.write(symbolsAmount-1);
for(int i = 0 ; i < symbolsAmount; i++){
fileOut.write(symbols.get(i).symbol);
byte[] bytes = toByteArray(cumulativeProbabilities[i+1]);
fileOut.write(bytes);
//System.out.println(symbols.get(i).symbol + ", " + cumulativeProbabilities[i+1]);
}
//koniec zapisu słownika do pliku
length--;
System.out.println("Długość pliku wynosi " + length + " bajtów.");
file.close();
file = new FileInputStream(args[0]);
FileChannel channel = file.getChannel();
long fileSize = channel.size();
while( fileSize > -1){ //czytamy symbol z pliku, na koncu dodajemy symbol specjalny
fileSize--;
if (fileSize!=-1){
key = file.read();
} else {
System.out.println("Dodaje znacznik konca");
key = 256; //symbol specjalny, konczacy komunikat
}
double F_lower = cumulativeProbabilities[containsSymbol(key)];
double F_upper = cumulativeProbabilities[containsSymbol(key)+1];
double prevLowerBound = lowerBound;
lowerBound = lowerBound +(upperBound - lowerBound)*F_lower;
upperBound = prevLowerBound + (upperBound - prevLowerBound)*F_upper; //obliczanie biezacego przedzialu
boolean loop = true;
while (loop){
SubsetPosition pos = subPos(lowerBound, upperBound);
if(pos == SubsetPosition.DOWN){
code += "0";
for(int i = 0; i<counter;i++) code +="1";
counter = 0;
} else if (pos == SubsetPosition.UP){
code += "1";
for(int i = 0; i<counter;i++) code +="0";
lowerBound = lowerBound-0.5;
upperBound = upperBound-0.5;
counter = 0;
} else if (pos == SubsetPosition.MIDDLE){
counter++;
lowerBound = lowerBound-0.25;
upperBound = upperBound-0.25;
} else{
break;
}
lowerBound = lowerBound*2;
upperBound = upperBound*2;
}
while(code.length() >=8 ){
lengthOfOutput++;
String toWrite = code.substring(0, 8); // wycinamy jeden bajt
code = code.substring(8); //usuwamy wyciety bajt
int x = StringToInt(toWrite); //zamiana stringa na int
fileOut.write(x); //zapis bajtu do pliku
}
}
counter++;
if(lowerBound < 0.25) {
code += "0";
for(int i = 0; i<counter;i++) code +="1";
counter = 0;
}
else {
code += "1";
for(int i = 0; i<counter;i++) code +="0";
}
while(code.length() >=8 ){ //to na wypadek gdyby tych jedynek lub zero było całkiem sporo
String toWrite = code.substring(0, 8); // wycinamy jeden bajt
code = code.substring(8); //usuwamy wyciety bajt
int x = StringToInt(toWrite); //zamiana stringa na int
fileOut.write(x); //zapis bajtu do pliku
}
int howManyZerosToAttach = 8 - code.length();
for (int i = 0; i < howManyZerosToAttach; i++) code += "0";
//zapisujemy przedostatni bajt
int x = StringToInt(code); //zamiana stringa na int
fileOut.write(x); //zapis bajtu do pliku
fileOut.write(howManyZerosToAttach);
lengthOfOutput+=2;
System.out.println("Entropia kodowanych danych: " + entropy);
double wspKomp = (double)(length - lengthOfOutput)/length;
System.out.println("Współczynnik kompresji: "+ wspKomp );
double av = (double) (lengthOfOutput*8)/length;
System.out.println("Średnia długość kodowania: " + av + " bitow" );
file.close();
fileOut.close();
/////////////********************************************************************/////////////////////////////////////
} else if (args[1].equals("dekompresja")){
file = new FileInputStream("E:\\Studia\\Kodowanie i kompresja danych\\Lab\\Lab4\\out");
fileOut = new FileOutputStream("E:\\Studia\\Kodowanie i kompresja danych\\Lab\\Lab4\\decoded");
FileChannel channel = file.getChannel();
long fileSize = channel.size();
int symbolsAmount = file.read();
symbolsAmount++; // to na wypadek gdyby ktoś sobie zażartował i zapisał 256 symboli w pliku
fileSize--;
cumulativeProbabilities = new double[symbolsAmount+2];
cumulativeProbabilities[0] = 0;
symbols = new ArrayList<>();
//wczytywanie słownika i dystrubuanty
for (int i = 0; i < symbolsAmount; i++){
int symbol = file.read(); fileSize--;
symbols.add(new Symbol(symbol));
byte[] bytes = new byte[8];
file.read(bytes); fileSize -= 8;
double prob = toDouble(bytes);
cumulativeProbabilities[i+1] = prob;
}
symbols.add(new Symbol(256)); //symbol specjalny
cumulativeProbabilities[symbolsAmount+1] = 1;
code ="";
String bufor ="";
double roz;
int rozBufora = 0;
upperBound = 1;
lowerBound = 0;
currentPartition = new double[cumulativeProbabilities.length];
boolean loop = true;
while (loop){
roz = 1;
for(int i=0; i < cumulativeProbabilities.length-1; i++){
double F_lower = cumulativeProbabilities[i];
double F_upper = cumulativeProbabilities[i+1];
currentPartition[i] = lowerBound +(upperBound - lowerBound)*F_lower;
currentPartition[i+1] = lowerBound + (upperBound - lowerBound)*F_upper;
if( currentPartition[i+1]-currentPartition[i] < roz) roz = currentPartition[i+1]-currentPartition[i];
}
rozBufora = (int) Math.ceil( Math.log(1/(0.25*roz))/Math.log(2) );
while(code.length() < rozBufora*2 && fileSize > 0){
if(fileSize == 2){
int x = file.read();
String toWrite = intToString(x);
int zerosAmount = file.read(); fileSize = 0;
toWrite = toWrite.substring(0, 8 - zerosAmount);
code += toWrite;
} else {
int x = file.read(); fileSize--;
code += intToString(x);
}
}
while (bufor.length() < rozBufora*2 && code.length()>0){
bufor += code.charAt(0); //dodajemy "bity" do bufora dopóki nie osągnie wymaganego rozmiaru
code = code.substring(1);
}
double buforBitow = toNumber(bufor);
for(int i = 1; i < currentPartition.length; i++){
if(buforBitow < currentPartition[i] && buforBitow >= currentPartition[i-1] ){
//sprawdzamy do jakiego przedziału wpada aktualny znacznik
lowerBound = currentPartition[i-1];
upperBound = currentPartition[i];
if(symbols.get(i-1).symbol == 256){
endOfMessage = true;
} else {
fileOut.write(symbols.get(i-1).symbol); //zapisuje odpowiedni symbol
}
i = currentPartition.length;
}
}
if(endOfMessage) break;
while(true){
SubsetPosition pos = subPos(lowerBound, upperBound);
if(pos == SubsetPosition.DOWN){
//nic nie rób;
} else if (pos == SubsetPosition.UP){
char [] pom = bufor.toCharArray();
pom[0] = '0'; //odejmujemy 0,5 od liczby bufor
bufor = String.valueOf(pom);
lowerBound -= 0.5;
upperBound -= 0.5;
} else if (pos == SubsetPosition.MIDDLE){
char [] pom = bufor.toCharArray();
if( pom[1] == '1') pom[1]='0'; //odejmujemy 0.25 od bufora
else {
pom[0]='0'; pom[1] = '1';
}
bufor = String.valueOf(pom);
lowerBound -= 0.25;
upperBound -= 0.25;
} else { break; }
lowerBound = lowerBound*2;
upperBound = upperBound*2;
bufor = bufor.substring(1);
if (code.length()>0){
bufor += code.charAt(0);
code = code.substring(1);
} else {
if(fileSize > 0){
if(fileSize == 2){
int x = file.read();
String toWrite = intToString(x);
int zerosAmount = file.read(); fileSize = 0;
toWrite = toWrite.substring(0, 8 - zerosAmount);
code += toWrite;
} else {
int x = file.read(); fileSize--;
code += intToString(x);
}
bufor += code.charAt(0);
code = code.substring(1);
}
}
}
if(bufor.length()==0) loop = false;
}
fileOut.close();
file.close();
}
}
private static SubsetPosition subPos(double lB, double uB){
if (lB >=0 && uB <=0.5) return SubsetPosition.DOWN;
if (lB >= 0.5 && uB<=1) return SubsetPosition.UP;
if (lB >= 0.25 && uB <= 0.75) return SubsetPosition.MIDDLE;
return SubsetPosition.ELSE;
}
private static double toNumber(String bufor){
double res=0;
for(int i=0; i<bufor.length(); i++){
if(bufor.charAt(i) == '1') res += 1/(Math.pow(2, i+1));
}
return res;
}
//zwraca pozycję symbolu
private static int containsSymbol(int symbol){
for(int i = 0; i < symbols.size(); i++){
if (symbols.get(i).symbol == symbol) return i;
}
return -1;
}
private static void cumputeProbabilities(){
for (int i = 0; i < symbols.size(); i++){
symbols.get(i).prob = (double) symbols.get(i).amount/length;
}
}
//obliczanie dystrybuanty rozkładu symboli
private static void computeCumulativeProbabilities(){
cumulativeProbabilities = new double[symbols.size()+1];
cumulativeProbabilities[0] = 0;
for(int i = 1; i < symbols.size()+1; i++) cumulativeProbabilities[i] = symbols.get(i-1).prob + cumulativeProbabilities[i-1];
}
public static byte[] toByteArray(double value) {
byte[] bytes = new byte[8];
ByteBuffer.wrap(bytes).putDouble(value);
return bytes;
}
public static double toDouble(byte[] bytes) {
return ByteBuffer.wrap(bytes).getDouble();
}
public static int StringToInt(String string){
int res=0;
for (int i=7; i >=0; i--){
if(string.charAt(7-i)=='1'){
res += Math.pow(2, i);
}
}
return res;
}
public static String intToString(int number){
String res="";
while(number >0 ){
if(number%2==1) res = "1" + res;
else res = "0"+res;
number /=2;
}
while(res.length()<8) res = "0" + res;
return res;
}
}