Skip to content
Snippets Groups Projects
Commit e11818e2 authored by Vojtěch Moravec's avatar Vojtěch Moravec
Browse files

Fix benchmark code with PlaneLoader API.

parent 8d464560
No related branches found
No related tags found
No related merge requests found
package azgracompress.benchmark; package azgracompress.benchmark;
import azgracompress.U16;
import azgracompress.cli.ParsedCliOptions; import azgracompress.cli.ParsedCliOptions;
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 { public class Benchmark extends BenchmarkBase {
...@@ -11,90 +24,84 @@ public class Benchmark extends BenchmarkBase { ...@@ -11,90 +24,84 @@ public class Benchmark extends BenchmarkBase {
@Override @Override
public void startBenchmark() { public void startBenchmark() {
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;
}
// TODO(Moravec): Support PlaneLoader API. int[] originalData;
// ParsedCliOptions compressOps; try {
// ParsedCliOptions decompressOps; final IPlaneLoader loader = PlaneLoaderFactory.getPlaneLoaderForInputFile(options.getInputDataInfo());
// try { originalData = loader.loadPlaneData(options.getInputDataInfo().getPlaneIndex());
// compressOps = (ParsedCliOptions) options.clone(); } catch (Exception e) {
// decompressOps = (ParsedCliOptions) options.clone(); System.err.println("Failed to get plane loader. " + e.getMessage());
// } catch (CloneNotSupportedException e) { e.printStackTrace();
// e.printStackTrace(); return;
// return; }
// }
// final int[] quantizedData = TypeConverter.shortArrayToIntArray(decompressedDataset.getPlaneData(0));
// boolean dirCreated = new File(options.getOutputFilePath()).mkdirs();
// //"%d_cb%d.raw.qcmp" final int[] diffArray = Utils.getDifference(originalData, quantizedData);
// final String qcmpFilePath = getFileNamePathIntoOutDir(String.format(COMPRESSED_FILE_TEMPLATE, final double mse = Utils.calculateMse(diffArray);
// options.getPlaneIndex(), final double PSNR = Utils.calculatePsnr(mse, U16.Max);
// codebookSize)); System.out.printf("MSE: %.4f\tPSNR: %.4f(dB)%n", mse, PSNR);
// compressOps.setOutputFilePath(qcmpFilePath);
// ImageCompressor compressor = new ImageCompressor(compressOps); final int[] absDifferenceData = Utils.asAbsoluteValues(diffArray);
// if (!compressor.compress()) {
// System.err.println("Errors occurred during compression."); final String diffFilePath = getFileNamePathIntoOutDir(String.format(DIFFERENCE_FILE_TEMPLATE,
// return; options.getInputDataInfo().getPlaneIndex(),
// } codebookSize));
//
// decompressOps.setInputFileInfo(new InputFileInfo(qcmpFilePath)); final String absDiffFilePath = getFileNamePathIntoOutDir(String.format(ABSOLUTE_DIFFERENCE_FILE_TEMPLATE,
// options.getInputDataInfo().getPlaneIndex(),
// codebookSize));
// final String decompressedFile = getFileNamePathIntoOutDir(String.format(QUANTIZED_FILE_TEMPLATE,
// options.getPlaneIndex(), try {
// codebookSize)); // NOTE(Moravec): Use little endian so that gnuplot can read the array.
// RawDataIO.writeBytesToFile(absDiffFilePath, TypeConverter.unsignedShortArrayToByteArray(absDifferenceData, true));
// decompressOps.setOutputFilePath(decompressedFile); System.out.println("Saved absolute difference to: " + absDiffFilePath);
// ImageDecompressor decompressor = new ImageDecompressor(decompressOps);
// if (!decompressor.decompress()) { RawDataIO.writeDataI32(diffFilePath, diffArray, true);
// System.err.println("Errors occurred during decompression."); System.out.println("Saved difference to: " + absDiffFilePath);
// } } catch (IOException e) {
// System.err.println("Failed to save difference.");
// final int[] originalData; e.printStackTrace();
// try { }
// originalData = RawDataIO.loadImageU16(options.getInputFilePath(),
// options.getImageDimension(),
// options.getPlaneIndex()).getData();
// } catch (IOException e) {
// e.printStackTrace();
// return;
// }
// final int[] quantizedData;
// try {
// quantizedData = RawDataIO.loadImageU16(decompressedFile,
// options.getImageDimension().toV2i().toV3i(), 0).getData();
// } catch (IOException e) {
// e.printStackTrace();
// return;
// }
//
// final int[] diffArray = Utils.getDifference(originalData, quantizedData);
// final double mse = Utils.calculateMse(diffArray);
// final double PSNR = Utils.calculatePsnr(mse, U16.Max);
// System.out.println(String.format("MSE: %.4f\tPSNR: %.4f(dB)", mse, PSNR));
//
// final int[] absDifferenceData = Utils.asAbsoluteValues(diffArray);
//
// final String diffFilePath = getFileNamePathIntoOutDir(String.format(DIFFERENCE_FILE_TEMPLATE,
// options.getPlaneIndex(),
// codebookSize));
//
// final String absDiffFilePath = getFileNamePathIntoOutDir(String.format(ABSOLUTE_DIFFERENCE_FILE_TEMPLATE,
// options.getPlaneIndex(),
// codebookSize));
//
// ImageU16 img = new ImageU16(rawImageDims.getX(),
// rawImageDims.getY(),
// absDifferenceData);
// try {
// // NOTE(Moravec): Use little endian so that gnuplot can read the array.
// RawDataIO.writeImageU16(absDiffFilePath, img, true);
// System.out.println("Saved absolute difference to: " + absDiffFilePath);
//
// RawDataIO.writeDataI32(diffFilePath, diffArray, true);
// System.out.println("Saved difference to: " + absDiffFilePath);
// } catch (Exception e) {
// e.printStackTrace();
// System.err.println("Failed to save difference.");
// return;
// }
} }
} }
...@@ -232,6 +232,7 @@ public class ImageDecompressor extends CompressorDecompressorBase { ...@@ -232,6 +232,7 @@ public class ImageDecompressor extends CompressorDecompressorBase {
return true; return true;
} }
// TODO(Moravec): Return optional to get rid of null check.
public ImageU16Dataset decompressInMemory() { public ImageU16Dataset decompressInMemory() {
try (FileInputStream fileInputStream = new FileInputStream(options.getInputDataInfo().getFilePath()); try (FileInputStream fileInputStream = new FileInputStream(options.getInputDataInfo().getFilePath());
DataInputStream dataInputStream = new DataInputStream(fileInputStream)) { DataInputStream dataInputStream = new DataInputStream(fileInputStream)) {
......
...@@ -15,19 +15,15 @@ public class RawDataIO { ...@@ -15,19 +15,15 @@ public class RawDataIO {
writeBytesToFile(rawFile, buffer); writeBytesToFile(rawFile, buffer);
} }
public static boolean writeDataI32(String rawFile, int[] differenceData, final boolean littleEndian) { public static void writeDataI32(String rawFile,
int[] differenceData,
final boolean littleEndian) throws IOException {
byte[] buffer = TypeConverter.intArrayToByteArray(differenceData, littleEndian); byte[] buffer = TypeConverter.intArrayToByteArray(differenceData, littleEndian);
try {
writeBytesToFile(rawFile, buffer); writeBytesToFile(rawFile, buffer);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
} }
private static void writeBytesToFile(String rawFile, byte[] buffer) throws IOException { public static void writeBytesToFile(String rawFile,
byte[] buffer) throws IOException {
FileOutputStream fileStream = new FileOutputStream(rawFile, false); FileOutputStream fileStream = new FileOutputStream(rawFile, false);
fileStream.write(buffer, 0, buffer.length); fileStream.write(buffer, 0, buffer.length);
fileStream.flush(); fileStream.flush();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment