Newer
Older
import azgracompress.compression.ImageCompressor;
import azgracompress.compression.ImageDecompressor;
import azgracompress.data.ImageU16Dataset;
import azgracompress.io.FileInputData;
import azgracompress.io.RawDataIO;
import azgracompress.io.loader.IPlaneLoader;
import azgracompress.io.loader.PlaneLoaderFactory;
import azgracompress.utilities.TypeConverter;
import azgracompress.utilities.Utils;
import java.io.File;
import java.io.IOException;
public class Benchmark extends BenchmarkBase {
protected Benchmark(ParsedCliOptions options) {
super(options);
}
@Override
public void startBenchmark() {
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
assert (options.getInputDataInfo().isPlaneIndexSet());
ParsedCliOptions compressOps;
ParsedCliOptions decompressOps;
try {
// NOTE: This works, right?
compressOps = (ParsedCliOptions) options.clone();
decompressOps = (ParsedCliOptions) options.clone();
} catch (CloneNotSupportedException e) {
System.err.println(e.getMessage());
e.printStackTrace();
return;
}
// dirCreated is ignored.
boolean dirCreated = new File(options.getOutputFilePath()).mkdirs();
final String qcmpFilePath = getFileNamePathIntoOutDir(String.format(COMPRESSED_FILE_TEMPLATE,
options.getInputDataInfo().getPlaneIndex(),
codebookSize));
compressOps.setOutputFilePath(qcmpFilePath);
ImageCompressor compressor = new ImageCompressor(compressOps);
if (!compressor.compress()) {
System.err.println("Errors occurred during compression.");
return;
}
decompressOps.setInputDataInfo(new FileInputData(qcmpFilePath));
final String decompressedFile = getFileNamePathIntoOutDir(String.format(QUANTIZED_FILE_TEMPLATE,
options.getInputDataInfo().getPlaneIndex(),
codebookSize));
decompressOps.setOutputFilePath(decompressedFile);
ImageDecompressor decompressor = new ImageDecompressor(decompressOps);
final ImageU16Dataset decompressedDataset = decompressor.decompressInMemory();
if (decompressedDataset == null) {
System.err.println("Errors occurred during decompression.");
return;
}
int[] originalData;
try {
final IPlaneLoader loader = PlaneLoaderFactory.getPlaneLoaderForInputFile(options.getInputDataInfo());
originalData = loader.loadPlaneData(options.getInputDataInfo().getPlaneIndex());
} catch (Exception e) {
System.err.println("Failed to get plane loader. " + e.getMessage());
e.printStackTrace();
return;
}
final int[] quantizedData = TypeConverter.shortArrayToIntArray(decompressedDataset.getPlaneData(0));
final int[] diffArray = Utils.getDifference(originalData, quantizedData);
final double mse = Utils.calculateMse(diffArray);
final double PSNR = Utils.calculatePsnr(mse, U16.Max);
System.out.printf("MSE: %.4f\tPSNR: %.4f(dB)%n", mse, PSNR);
final int[] absDifferenceData = Utils.asAbsoluteValues(diffArray);
final String diffFilePath = getFileNamePathIntoOutDir(String.format(DIFFERENCE_FILE_TEMPLATE,
options.getInputDataInfo().getPlaneIndex(),
codebookSize));
final String absDiffFilePath = getFileNamePathIntoOutDir(String.format(ABSOLUTE_DIFFERENCE_FILE_TEMPLATE,
options.getInputDataInfo().getPlaneIndex(),
codebookSize));
try {
// NOTE(Moravec): Use little endian so that gnuplot can read the array.
RawDataIO.writeBytesToFile(absDiffFilePath, TypeConverter.unsignedShortArrayToByteArray(absDifferenceData, true));
System.out.println("Saved absolute difference to: " + absDiffFilePath);
RawDataIO.writeDataI32(diffFilePath, diffArray, true);
System.out.println("Saved difference to: " + absDiffFilePath);
} catch (IOException e) {
System.err.println("Failed to save difference.");
e.printStackTrace();
}