-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage_compression.java
126 lines (108 loc) · 3.11 KB
/
image_compression.java
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.IntStream;
import org.apache.commons.lang3.ArrayUtils;
/**
*
* @author Veeki.yadav
*/
public class image_compression {
static int[] freq2 = new int[256];
/**
*
* @param freq
*/
public static void lossy(int[] freq) {
int n = 9;
for (int i = 0; i < freq.length; i += 10) {
int max = 0;
if (i == 240) {
n = 255;
}
for (int k = i; k <= n; k++) {
if (freq[k] > max) {
max = freq[k];
}
}
makenew(ArrayUtils.indexOf(freq, max), freq, i);
if (n == 255) {
i += 20;
}
n += 10;
}
}
/**
*
* @param maxvalueIndex
* @param freq
* @param start
*/
public static void makenew(int maxvalueIndex, int[] freq, int start) {
int sum = 0;
int end;
if (start == 240) {
end = start + 16;
}
end = start + 10;
// to get the sum
for (int i = start; i < end; i++) {
sum += freq[i];
}
// create new array
for (int i = start; i < end; i++) {
if (i != maxvalueIndex) {
freq2[i] = 0;
} else {
freq2[i] = sum;
}
}
}
/**
*
* @param args
*/
public static void main(String[] args) {
// class object
GrayScale gs = new GrayScale();
priority_queue pq = new priority_queue();
huffman_code hc = new huffman_code();
priority_queue pq2 = new priority_queue();
huffman_code hc2 = new huffman_code();
// to add the frequency elements to the priority queue
int[] freq = gs.freq;
for (int i = 0; i < freq.length; i++) {
pq.add(freq[i], i, null, null);
}
// for lossy compression
lossy(freq);
// to add the frequency elements to the priority queue
for (int i = 0; i < freq2.length; i++) {
pq2.add(freq2[i], i, null, null);
}
// to build the huffman tree
Node root = hc.huffmanTree(freq, pq);
Node root2 = hc2.huffmanTree(freq2, pq2);
// to get the bit code
hc.bitcode(root, "");
hc2.bitcode(root2, "");
// initial bit used
double initbit = gs.width * gs.height * 8;
// sum the compressed bit length and bit code
int sum = 0;
for (int i : hc.str) {
sum += i;
}
// for lossy compression
int sum2 = 0;
for (int i : hc2.str) {
sum2 += i;
}
// to print the compression ratio
double ratio = initbit / sum;
double roundOff = (double) Math.round(ratio * 100) / 100;
System.out.println("Compression ratio for part 1 : " + roundOff);
double ratio2 = initbit / sum2;
double roundOff2 = (double) Math.round(ratio2 * 100) / 100;
System.out.println("Compression ratio for part 2 : " + roundOff2);
}
}