-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
126 lines (98 loc) · 5.78 KB
/
App.java
File metadata and controls
126 lines (98 loc) · 5.78 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
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import lib.ImagetoPixelConverter;
import lib.MAECalculator;
import lib.MSECalculator;
import lib.PSNRCalculator;
import lib.PixeltoImageConverter;
import src.Utility;
public class App {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Create an instance of Utility
Utility Utility = new Utility(8, 10);
// Define original file directory to loop through
String ImageDirectory = "Original/";
// List all files in the directory
File directory = new File(ImageDirectory);
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
String imageName = file.getName();
// Converting image to pixels
System.out.println("Converting " + imageName + " to pixels");
System.out.println("--------------------------------------------------");
ImagetoPixelConverter ImagetoPixelConverter = new ImagetoPixelConverter(ImageDirectory + imageName);
// Converting the image to pixels
int[][][] pixelData = ImagetoPixelConverter.getPixelData();
int width = ImagetoPixelConverter.getWidth();
int height = ImagetoPixelConverter.getHeight();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int red = pixelData[x][y][0];
int green = pixelData[x][y][1];
int blue = pixelData[x][y][2];
}
}
// Now you have the image data in 'pixelData' that will be taken in by Compress
// Define location and name for the compressed file to be created
String compressed_file_name = "Compressed/" + imageName.substring(0, imageName.lastIndexOf('.'))
+ ".bin";
// start compress timer
long compressStartTime = System.currentTimeMillis();
// call compress function
Utility.Compress(pixelData, compressed_file_name);
// end timer for compress and record the total time passed
long compressEndTime = System.currentTimeMillis();
long compressExecutionTime = compressEndTime - compressStartTime;
System.out.println("Compress Execution Time for " + imageName + " : " + compressExecutionTime
+ " milliseconds");
// Check the original file size
File originalFile = new File(ImageDirectory + imageName);
long originalFileSize = originalFile.length();
System.out
.println("Size of the original file for " + imageName + ": " + originalFileSize + " bytes");
// Check size of the compressed file
File compressedFile = new File(compressed_file_name);
long compressedFileSize = compressedFile.length();
System.out.println(
"Size of the compressed file for " + imageName + ": " + compressedFileSize + " bytes");
// Find the Difference
long differenceInFileSize = originalFileSize - compressedFileSize;
System.out.println(
"Bytes saved from compression of " + imageName + ": " + differenceInFileSize + " bytes");
// start decompress timer
long decompressStartTime = System.currentTimeMillis();
// call decompress function
int[][][] newPixelData = Utility.Decompress(compressed_file_name);
// end timer for decompress and record the total time passed
long decompressEndTime = System.currentTimeMillis();
long decompressExecutionTime = decompressEndTime - decompressStartTime;
System.out.println("Decompress Execution Time for " + imageName + " : " + decompressExecutionTime
+ " milliseconds");
// convert back to image for visualisation
PixeltoImageConverter PixeltoImageConverter = new PixeltoImageConverter(newPixelData);
PixeltoImageConverter.saveImage("Decompressed/" + imageName, "png");
// Get the two bufferedimages for calculations
BufferedImage originalimage = ImageIO.read(new File(ImageDirectory + imageName));
BufferedImage decompressedimage = ImageIO.read(new File("Decompressed/" + imageName));
// calculate MAE
double MAE = MAECalculator.calculateMAE(originalimage, decompressedimage);
System.out.println("Mean Absolute Error of :" + imageName + " is " + MAE);
// calculate MSE
double MSE = MSECalculator.calculateMSE(originalimage, decompressedimage);
System.out.println("Mean Squared Error of :" + imageName + " is " + MSE);
// calculate PSNR
double PSNR = PSNRCalculator.calculatePSNR(originalimage, decompressedimage);
System.out.println("PSNR of :" + imageName + " is " + PSNR);
System.out.println("--------------------------------------------------");
}
}
}
}
}