-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVQCompressUI.java
More file actions
389 lines (328 loc) · 15.9 KB
/
VQCompressUI.java
File metadata and controls
389 lines (328 loc) · 15.9 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
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.DecimalFormat;
import javax.imageio.ImageIO;
public class VQCompressUI extends JFrame {
private JTextField filePathField;
private JButton browseButton;
private JComboBox<Integer> tileSizeBox;
private JComboBox<String> qualityBox;
private JButton compressButton;
private JTextArea statusArea;
private JLabel imageLabel;
private JLabel decompressedImageLabel;
private JProgressBar progressBar;
private JLabel originalSizeLabel;
private JLabel compressedSizeLabel;
private JLabel ratioLabel;
private File selectedFile;
// A simple class to hold the results of the background task
private static class CompressionResult {
final BufferedImage image;
final long originalSize;
final long compressedSize;
CompressionResult(BufferedImage image, long originalSize, long compressedSize) {
this.image = image;
this.originalSize = originalSize;
this.compressedSize = compressedSize;
}
}
public VQCompressUI() {
// 1. Use a modern Look and Feel
try {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ignored) {
// Fallback to system default if Nimbus is not available
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
}
setTitle("VQ Image Compressor");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(800, 700));
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
((JComponent) getContentPane()).setBorder(new EmptyBorder(10, 10, 10, 10));
// --- Top Panel for Settings ---
JPanel settingsPanel = createSettingsPanel();
// --- Center Panel for Image Previews ---
JPanel imagePanel = createImagePreviewPanel();
// --- Bottom Panel for Controls and Status ---
JPanel bottomPanel = createBottomPanel();
add(settingsPanel, BorderLayout.NORTH);
add(imagePanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
addListeners();
}
private JPanel createSettingsPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("1. Compression Settings"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0; gbc.gridy = 0;
panel.add(new JLabel("Image File:"), gbc);
filePathField = new JTextField(30);
filePathField.setEditable(false);
gbc.gridx = 1; gbc.weightx = 1.0;
panel.add(filePathField, gbc);
browseButton = new JButton("Browse...");
gbc.gridx = 2; gbc.weightx = 0;
panel.add(browseButton, gbc);
gbc.gridx = 0; gbc.gridy = 1;
panel.add(new JLabel("Tile Size:"), gbc);
tileSizeBox = new JComboBox<>(new Integer[]{2, 4, 8, 16});
tileSizeBox.setSelectedItem(4);
gbc.gridx = 1;
panel.add(tileSizeBox, gbc);
gbc.gridx = 0; gbc.gridy = 2;
panel.add(new JLabel("Quality:"), gbc);
qualityBox = new JComboBox<>(new String[]{"High Quality", "Medium Quality", "Low Quality"});
qualityBox.setSelectedIndex(1);
gbc.gridx = 1;
panel.add(qualityBox, gbc);
return panel;
}
private JPanel createImagePreviewPanel() {
JPanel panel = new JPanel(new GridLayout(1, 2, 10, 10));
panel.setBorder(BorderFactory.createTitledBorder("Image Preview"));
imageLabel = createPlaceholderLabel("Original Image");
decompressedImageLabel = createPlaceholderLabel("Compressed Result");
panel.add(new JScrollPane(imageLabel));
panel.add(new JScrollPane(decompressedImageLabel));
return panel;
}
private JPanel createBottomPanel() {
JPanel panel = new JPanel(new BorderLayout(10, 10));
// Action Panel
JPanel actionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
compressButton = new JButton("Compress Image");
compressButton.setFont(compressButton.getFont().deriveFont(Font.BOLD, 14f));
progressBar = new JProgressBar();
progressBar.setPreferredSize(new Dimension(150, 20));
progressBar.setStringPainted(true);
progressBar.setString("Ready");
actionPanel.add(compressButton);
actionPanel.add(progressBar);
// Stats Panel
JPanel statsPanel = new JPanel(new GridLayout(1, 3, 10, 0));
statsPanel.setBorder(new TitledBorder("2. Compression Results"));
originalSizeLabel = new JLabel("Original: -", SwingConstants.CENTER);
compressedSizeLabel = new JLabel("Compressed: -", SwingConstants.CENTER);
ratioLabel = new JLabel("Ratio: -", SwingConstants.CENTER);
statsPanel.add(originalSizeLabel);
statsPanel.add(compressedSizeLabel);
statsPanel.add(ratioLabel);
// Status Area
statusArea = new JTextArea(4, 40);
statusArea.setEditable(false);
statusArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(statusArea);
scrollPane.setBorder(BorderFactory.createTitledBorder("Status Log"));
JPanel controlAndStatsPanel = new JPanel(new BorderLayout(5, 5));
controlAndStatsPanel.add(actionPanel, BorderLayout.NORTH);
controlAndStatsPanel.add(statsPanel, BorderLayout.CENTER);
panel.add(controlAndStatsPanel, BorderLayout.NORTH);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private void addListeners() {
browseButton.addActionListener(e -> onBrowse());
compressButton.addActionListener(e -> onCompress());
}
private void onBrowse() {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Images (JPG, PNG, BMP, GIF)", "jpg", "jpeg", "png", "bmp", "gif");
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
filePathField.setText(selectedFile.getAbsolutePath());
try {
BufferedImage img = ImageIO.read(selectedFile);
displayImage(imageLabel, img, "Original Image");
showStatus("Loaded image: " + selectedFile.getName());
resetStats();
} catch (Exception ex) {
showError("Could not read the selected image file.", "Image Load Error");
}
}
}
private void onCompress() {
if (selectedFile == null) {
showError("Please select an image file first.", "No Image Selected");
return;
}
setBusy(true);
// Run compression in the background to keep the UI responsive
SwingWorker<CompressionResult, String> worker = new SwingWorker<>() {
@Override
protected CompressionResult doInBackground() throws Exception {
// --- Get settings from UI ---
int tileSize = (Integer) tileSizeBox.getSelectedItem();
int qualityOption = qualityBox.getSelectedIndex(); // 0=High, 1=Medium, 2=Low
String inputPath = selectedFile.getAbsolutePath();
long originalSize = selectedFile.length();
// --- 1. Choose a location to save the compressed file ---
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Save Compressed File");
fileChooser.setSelectedFile(new File(selectedFile.getName() + ".vq")); // Suggest a name
fileChooser.setFileFilter(new FileNameExtensionFilter("VQ Compressed File (*.vq)", "vq"));
int userSelection = fileChooser.showSaveDialog(VQCompressUI.this);
if (userSelection != JFileChooser.APPROVE_OPTION) {
return null; // User cancelled
}
File compressedFile = fileChooser.getSelectedFile();
// --- 2. Perform Compression ---
publish("Compressing image...");
vectorQuantizationCompress compressor = new vectorQuantizationCompress();
compressor.tileSize = tileSize;
switch (qualityOption) {
case 0: compressor.codeBookSize = 256; break; // High
case 1: compressor.codeBookSize = 128; break; // Medium
case 2: compressor.codeBookSize = 64; break; // Low
}
compressor.loadImage(inputPath);
compressor.initializeCodebook(); // Consider adding a more advanced algorithm here later (LBG)
compressor.quantizeImage();
compressor.saveCompressedFile(compressedFile.getAbsolutePath());
long compressedSize = compressedFile.length();
publish("Compression complete. Decompressing for preview...");
// --- 3. Perform Decompression for preview ---
// We create a temporary file for the decompressed image preview
File tempDecompressedFile = File.createTempFile("decompressed_preview", ".jpg");
tempDecompressedFile.deleteOnExit();
vectorQuantizationDecompress decompressor = new vectorQuantizationDecompress();
// This assumes the decompressor saves the file itself
decompressor.loadCompressedData(compressedFile.getAbsolutePath(), tempDecompressedFile.getAbsolutePath());
BufferedImage decompressedImg = ImageIO.read(tempDecompressedFile);
publish("Decompression complete.");
return new CompressionResult(decompressedImg, originalSize, compressedSize);
}
@Override
protected void process(java.util.List<String> chunks) {
// Update status area with messages from doInBackground
for (String message : chunks) {
showStatus(message);
}
}
@Override
protected void done() {
try {
CompressionResult result = get();
if (result != null) { // Not cancelled
displayImage(decompressedImageLabel, result.image, "Compressed Result");
updateStats(result.originalSize, result.compressedSize);
showStatus("Process finished successfully!");
// --- 4. Ask user to save the final image ---
int choice = JOptionPane.showConfirmDialog(VQCompressUI.this,
"Compression successful! Do you want to save the final decompressed image?",
"Save Image", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
saveFinalImage(result.image);
}
}
} catch (Exception e) {
e.printStackTrace();
showError("An error occurred during the process: " + e.getMessage(), "Error");
resetStats();
} finally {
setBusy(false);
}
}
};
worker.execute();
}
private void saveFinalImage(BufferedImage image) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Save Decompressed Image");
fileChooser.setFileFilter(new FileNameExtensionFilter("JPEG Image (*.jpg)", "jpg"));
fileChooser.setSelectedFile(new File(selectedFile.getName() + "_decompressed.jpg"));
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
File saveFile = fileChooser.getSelectedFile();
try {
ImageIO.write(image, "jpg", saveFile);
showStatus("Decompressed image saved to: " + saveFile.getAbsolutePath());
JOptionPane.showMessageDialog(this, "Image saved successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
showError("Failed to save the image: " + ex.getMessage(), "Save Error");
}
}
}
// --- Helper Methods ---
private void setBusy(boolean busy) {
compressButton.setEnabled(!busy);
browseButton.setEnabled(!busy);
progressBar.setIndeterminate(busy);
progressBar.setString(busy ? "Processing..." : "Ready");
setCursor(busy ? Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR) : Cursor.getDefaultCursor());
}
private JLabel createPlaceholderLabel(String text) {
JLabel label = new JLabel(text, SwingConstants.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setBorder(BorderFactory.createDashedBorder(Color.GRAY));
label.setFont(new Font("SansSerif", Font.ITALIC, 16));
label.setPreferredSize(new Dimension(300, 300));
return label;
}
private void displayImage(JLabel label, BufferedImage img, String defaultText) {
if (img != null) {
int w = label.getWidth();
int h = label.getHeight();
if (w == 0 || h == 0) { // If component not yet sized
w = 350; h = 350;
}
Image scaledImg = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
label.setIcon(new ImageIcon(scaledImg));
label.setText(null);
label.setBorder(BorderFactory.createLineBorder(Color.GRAY));
} else {
label.setIcon(null);
label.setText(defaultText);
label.setBorder(BorderFactory.createDashedBorder(Color.GRAY));
}
}
private void updateStats(long original, long compressed) {
originalSizeLabel.setText("Original: " + formatFileSize(original));
compressedSizeLabel.setText("Compressed: " + formatFileSize(compressed));
if (compressed > 0) {
double ratio = (double) original / compressed;
ratioLabel.setText("Ratio: " + new DecimalFormat("0.00").format(ratio) + " : 1");
}
}
private void resetStats() {
originalSizeLabel.setText("Original: -");
compressedSizeLabel.setText("Compressed: -");
ratioLabel.setText("Ratio: -");
displayImage(decompressedImageLabel, null, "Compressed Result");
}
private String formatFileSize(long size) {
if (size <= 0) return "0 B";
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
private void showStatus(String msg) {
statusArea.append(msg + "\n");
statusArea.setCaretPosition(statusArea.getDocument().getLength());
}
private void showError(String message, String title) {
JOptionPane.showMessageDialog(this, message, title, JOptionPane.ERROR_MESSAGE);
showStatus("ERROR: " + message);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new VQCompressUI().setVisible(true));
}
}