-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorQuantizationCompress.java
More file actions
201 lines (181 loc) · 7.44 KB
/
vectorQuantizationCompress.java
File metadata and controls
201 lines (181 loc) · 7.44 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
import java.util.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class vectorQuantizationCompress {
// Define the tile size (each tile is tileSize x tileSize pixels)
int tileSize = 8; // default, will be set by user
int tilesPerRow;
int tilesPerColumn;
// 3D array to store image tiles (now for color:
// [row][col][tileSize*tileSize*3])
private int[][][] tileGrid;
// Codebook and compressed image
private int[][] codeBook;
private int[][] compressedImage;
// Quality and codebook settings
int qualityOption;
int codeBookSize;
int imageWidth, imageHeight;
// Allow user to choose compression quality and tile size
public void chooseQualityAndTileSize(Scanner sc) {
System.out.print("Enter tile size (e.g., 4, 6, 8): ");
tileSize = sc.nextInt();
System.out.println("Select compression quality option: ");
System.out.println("1. Low Compression (High Quality)");
System.out.println("2. Medium Compression (Medium Quality)");
System.out.println("3. High Compression (Low Quality)");
qualityOption = sc.nextInt();
// Determine codebook size based on quality
switch (qualityOption) {
case 1:
codeBookSize = 256;
break;
case 2:
codeBookSize = 128;
break;
case 3:
codeBookSize = 16;
break;
default:
System.out.println("Invalid option; defaulting to Medium Compression.");
codeBookSize = 128;
break;
}
}
// Load image and split into 8x8 color tiles
public void loadImage(String imgPath) {
try {
BufferedImage image = ImageIO.read(new File(imgPath));
imageWidth = image.getWidth();
imageHeight = image.getHeight();
tilesPerRow = imageHeight / tileSize;
tilesPerColumn = imageWidth / tileSize;
tileGrid = new int[tilesPerRow][tilesPerColumn][tileSize * tileSize * 3];
compressedImage = new int[tilesPerRow][tilesPerColumn];
for (int row = 0; row < tilesPerRow; row++) {
for (int col = 0; col < tilesPerColumn; col++) {
for (int i = 0; i < tileSize; i++) {
for (int j = 0; j < tileSize; j++) {
int pixel = image.getRGB(col * tileSize + j, row * tileSize + i);
int r = (pixel >> 16) & 0xFF;
int g = (pixel >> 8) & 0xFF;
int b = pixel & 0xFF;
int idx = (i * tileSize + j) * 3;
tileGrid[row][col][idx] = r;
tileGrid[row][col][idx + 1] = g;
tileGrid[row][col][idx + 2] = b;
}
}
}
}
System.out.println("Image loaded and split into " + tilesPerRow + " x " + tilesPerColumn + " color tiles.");
} catch (IOException e) {
e.printStackTrace();
}
}
// Improved codebook initialization: sample tiles from across the entire image
// (no change needed)
public void initializeCodebook() {
codeBook = new int[codeBookSize][tileSize * tileSize * 3];
int totalTiles = tilesPerRow * tilesPerColumn;
int step = Math.max(1, totalTiles / codeBookSize);
int count = 0;
for (int row = 0; row < tilesPerRow; row++) {
for (int col = 0; col < tilesPerColumn; col++) {
int flatIndex = row * tilesPerColumn + col;
if (flatIndex % step == 0 && count < codeBookSize) {
codeBook[count++] = tileGrid[row][col].clone();
}
if (count >= codeBookSize)
break;
}
if (count >= codeBookSize)
break;
}
// Fill any remaining codebook vectors if image had fewer tiles
while (count < codeBookSize) {
codeBook[count++] = new int[tileSize * tileSize * 3]; // blank tiles
}
System.out.println("Codebook initialized with " + count + " vectors.");
}
// Quantize the image using the closest codebook vector (multithreaded per row)
public void quantizeImage() {
Thread[] threads = new Thread[tilesPerRow];
for (int row = 0; row < tilesPerRow; row++) {
final int r = row;
threads[row] = new Thread(new Runnable() {
public void run() {
for (int col = 0; col < tilesPerColumn; col++) {
compressedImage[r][col] = findClosestCodebookVector(tileGrid[r][col]);
}
}
});
threads[row].start();
}
// Wait for all threads to finish
for (int row = 0; row < tilesPerRow; row++) {
try {
threads[row].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// Find the closest vector in the codebook using Euclidean distance (for color)
public int findClosestCodebookVector(int[] tile) {
long minDistance = Integer.MAX_VALUE;
int bestIndex = 0;
for (int i = 0; i < codeBookSize; i++) {
long distance = 0;
for (int j = 0; j < tileSize * tileSize * 3; j++) {
int diff = tile[j] - codeBook[i][j];
distance += (long) diff * diff;
}
if (distance < minDistance) {
minDistance = distance;
bestIndex = i;
}
}
return bestIndex;
}
// Save compressed image and codebook to file (update codebook write/read for
// color)
public void saveCompressedFile(String filename) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
// Write metadata: image width, height, tile size, codebook size, color flag
bw.write(imageWidth + " " + imageHeight + " " + tileSize + " " + codeBookSize + " 3");
bw.newLine();
// Write compressed image (tile indices)
for (int i = 0; i < tilesPerRow; i++) {
for (int j = 0; j < tilesPerColumn; j++) {
bw.write(compressedImage[i][j] + " ");
}
bw.newLine();
}
// Write codebook vectors (color)
for (int i = 0; i < codeBookSize; i++) {
for (int j = 0; j < tileSize * tileSize * 3; j++) {
bw.write(codeBook[i][j] + " ");
}
bw.newLine();
}
System.out.println("Compression successful. Data saved to " + filename);
} catch (IOException e) {
e.printStackTrace();
}
}
// Main method
public static void main(String[] args) {
vectorQuantizationCompress compressor = new vectorQuantizationCompress();
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Give file path : ");
String filePath = sc.nextLine();
compressor.chooseQualityAndTileSize(sc);
compressor.loadImage(filePath);
compressor.initializeCodebook();
compressor.quantizeImage();
compressor.saveCompressedFile("Compressed.txt");
}
}
}