1
1
package com .github .myibu .algorithm .compress ;
2
2
3
+ import com .github .myibu .algorithm .data .Bits ;
4
+ import com .github .myibu .algorithm .endode .GolombEncoder ;
5
+
6
+ import java .util .ArrayList ;
3
7
import java .util .Arrays ;
8
+ import java .util .List ;
4
9
5
10
/**
6
11
* LZ77 compress algorithm
@@ -48,6 +53,7 @@ public int compress(byte[] in_data, int in_len, byte[] out_data) {
48
53
System .arraycopy (in_data , 0 , out_data , 0 , in_len );
49
54
return in_len ;
50
55
}
56
+ List <List <Integer >> tuples = new ArrayList <>();
51
57
// search buffer
52
58
byte [] sBuf = new byte [s ];
53
59
// look ahead window
@@ -87,6 +93,7 @@ public int compress(byte[] in_data, int in_len, byte[] out_data) {
87
93
// byte[] tuple = String.format("(%d,%d,%s)", minIndex + 1, minMatched, new String(new byte[]{lWindow[minMatched]})).getBytes();
88
94
// System.arraycopy(tuple, 0, out_data, (op++) * tuple.length, tuple.length);
89
95
System .out .println (String .format ("(%d, %d, %s)" , minIndex + 1 , minMatched , new String (new byte []{lWindow [minMatched ]})));
96
+ tuples .add (Arrays .asList ( minIndex + 1 , minMatched , (int )lWindow [minMatched ]));
90
97
sp += (minMatched + 1 );
91
98
// if (sp > s) {
92
99
// sp = s-1;
@@ -101,8 +108,23 @@ public int compress(byte[] in_data, int in_len, byte[] out_data) {
101
108
// byte[] tuple = String.format("(%d,%d,%s)", 0, 0, new String(new byte[]{lWindow[0]})).getBytes();
102
109
// System.arraycopy(tuple, 0, out_data, (op++) * tuple.length, tuple.length);
103
110
System .out .println (String .format ("(%d, %d, %s)" , 0 , 0 , new String (new byte []{lWindow [0 ]})));
111
+ tuples .add (Arrays .asList (0 , 0 , (int )lWindow [0 ]));
104
112
}
105
113
}
114
+ System .out .println (tuples );
115
+ int sum = 0 ;
116
+ GolombEncoder encoder = new GolombEncoder ();
117
+ for (List <Integer > tuple : tuples ) {
118
+ Bits bits = new Bits ();
119
+ bits .append (encoder .encodeToBinary (tuple .get (0 ), (int )(Math .ceil (Math .log (s ) / Math .log (2 )))));
120
+ System .out .println ("1" + bits );
121
+ bits .append (encoder .encode (tuple .get (1 ), 5 ));
122
+ System .out .println ("2" + bits );
123
+ bits .append (Bits .ofByte ((byte )tuple .get (2 ).intValue ()));
124
+ System .out .println ("3" + bits );
125
+ sum += bits .length ();
126
+ }
127
+ System .out .println ("compressed length: " + sum );
106
128
return 0 ;
107
129
}
108
130
0 commit comments