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

More work in merging data loaders into plugin ready library.

parent e5fea207
No related branches found
No related tags found
No related merge requests found
Showing
with 190 additions and 153 deletions
package azgracompress.benchmark;
import azgracompress.cli.InputFileInfo;
import azgracompress.io.InputDataInfo;
import azgracompress.cli.ParsedCliOptions;
import azgracompress.compression.Interval;
import azgracompress.data.ImageU16;
import azgracompress.data.V3i;
import azgracompress.io.RawDataIO;
......@@ -41,7 +40,7 @@ abstract class BenchmarkBase {
protected BenchmarkBase(final ParsedCliOptions options) {
this.options = options;
final InputFileInfo ifi = options.getInputFileInfo();
final InputDataInfo ifi = options.getInputDataInfo();
this.inputFile = ifi.getFilePath();
this.outputDirectory = options.getOutputFilePath();
......@@ -53,9 +52,8 @@ abstract class BenchmarkBase {
if (ifi.isPlaneIndexSet()) {
this.planes = new int[]{ifi.getPlaneIndex()};
} else if (ifi.isPlaneRangeSet()) {
final int from = ifi.getPlaneRange().getX();
final int to = ifi.getPlaneRange().getY();
final int count = to - from;
final int from = ifi.getPlaneRange().getFrom();
final int count = ifi.getPlaneRange().getInclusiveTo() - from;
this.planes = new int[count + 1];
for (int i = 0; i <= count; i++) {
......
......@@ -26,7 +26,7 @@ public class SQBenchmark extends BenchmarkBase {
public void startBenchmark() {
IPlaneLoader planeLoader;
try {
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(options.getInputFileInfo());
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(options.getInputDataInfo());
} catch (Exception e) {
e.printStackTrace();
System.err.println("Unable to create SCIFIO reader.");
......
......@@ -51,7 +51,7 @@ public class VQBenchmark extends BenchmarkBase {
}
IPlaneLoader planeLoader;
try {
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(options.getInputFileInfo());
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(options.getInputDataInfo());
} catch (Exception e) {
e.printStackTrace();
System.err.println("Unable to create SCIFIO reader.");
......
......@@ -3,10 +3,12 @@ package azgracompress.cli;
import azgracompress.compression.CompressionOptions;
import azgracompress.ScifioWrapper;
import azgracompress.compression.CompressorDecompressorBase;
import azgracompress.compression.Interval;
import azgracompress.data.V2i;
import azgracompress.data.V3i;
import azgracompress.fileformat.FileExtensions;
import azgracompress.fileformat.QuantizationType;
import azgracompress.io.InputDataInfo;
import io.scif.FormatException;
import io.scif.Plane;
import io.scif.Reader;
......@@ -137,7 +139,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
setCodebookCacheFolder(cmd.getOptionValue(CliConstants.CODEBOOK_CACHE_FOLDER_LONG, null));
if (!parseErrorOccurred) {
setOutputFilePath(cmd.getOptionValue(CliConstants.OUTPUT_LONG, getDefaultOutputFilePath(getInputFileInfo().getFilePath())));
setOutputFilePath(cmd.getOptionValue(CliConstants.OUTPUT_LONG, getDefaultOutputFilePath(getInputDataInfo().getFilePath())));
setCodebookCacheFolder(cmd.getOptionValue(CliConstants.CODEBOOK_CACHE_FOLDER_LONG, null));
}
......@@ -160,7 +162,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
return;
}
setInputFileInfo(new InputFileInfo(inputFileArguments[0]));
setInputDataInfo(new InputDataInfo(inputFileArguments[0]));
// Decompress and Inspect methods doesn't require additional file information.
if ((method == ProgramMethod.Decompress) || (method == ProgramMethod.InspectFile)) {
......@@ -168,7 +170,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
}
// Check if input file exists.
if (!new File(getInputFileInfo().getFilePath()).exists()) {
if (!new File(getInputDataInfo().getFilePath()).exists()) {
parseErrorOccurred = true;
errorBuilder.append("Input file doesn't exist.\n");
return;
......@@ -185,11 +187,10 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
private void parseSCIFIOFileArguments(StringBuilder errorBuilder, final String[] inputFileArguments) {
getInputFileInfo().setIsRaw(false);
getInputDataInfo().setDataLoaderType(InputDataInfo.DataLoaderType.SCIFIOLoader);
Reader reader;
try {
reader = ScifioWrapper.getReader(getInputFileInfo().getFilePath());
reader = ScifioWrapper.getReader(getInputDataInfo().getFilePath());
} catch (IOException | FormatException e) {
parseErrorOccurred = true;
errorBuilder.append("Failed to get SCIFIO reader for file.\n");
......@@ -230,7 +231,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
return;
}
getInputFileInfo().setDimension(new V3i(
getInputDataInfo().setDimension(new V3i(
(int) planeWidth,
(int) planeHeight,
(int) planeCount
......@@ -250,8 +251,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
.append("e.g.: 1920x1080x1\n");
return;
}
getInputFileInfo().setIsRaw(true);
getInputDataInfo().setDataLoaderType(InputDataInfo.DataLoaderType.RawDataLoader);
parseImageDims(inputFileArguments[1], errorBuilder);
// User specified plane index or plane range.
......@@ -282,7 +282,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
final ParseResult<Integer> indexToResult = tryParseInt(toIndexString);
if (indexFromResult.isSuccess() && indexToResult.isSuccess()) {
getInputFileInfo().setPlaneRange(new V2i(indexFromResult.getValue(), indexToResult.getValue()));
getInputDataInfo().setPlaneRange(new Interval<>(indexFromResult.getValue(), indexToResult.getValue()));
} else {
parseErrorOccurred = true;
errorBuilder.append("Plane range index is wrong. Expected format D-D, got: ").append(
......@@ -292,7 +292,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
// Here we parse single plane index option.
final ParseResult<Integer> parseResult = tryParseInt(inputFileArguments[inputFileArgumentsOffset]);
if (parseResult.isSuccess()) {
getInputFileInfo().setPlaneIndex(parseResult.getValue());
getInputDataInfo().setPlaneIndex(parseResult.getValue());
} else {
parseErrorOccurred = true;
errorBuilder.append("Failed to parse plane index option, expected integer, got: ")
......@@ -325,7 +325,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
final ParseResult<Integer> n1Result = tryParseInt(num1String);
final ParseResult<Integer> n2Result = tryParseInt(secondPart);
if (n1Result.isSuccess() && n2Result.isSuccess()) {
getInputFileInfo().setDimension(new V3i(n1Result.getValue(), n2Result.getValue(), 1));
getInputDataInfo().setDimension(new V3i(n1Result.getValue(), n2Result.getValue(), 1));
} else {
parseErrorOccurred = true;
errorBuilder.append("Failed to parse image dimensions of format DxD, got: ");
......@@ -340,7 +340,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
final ParseResult<Integer> n3Result = tryParseInt(num3String);
if (n1Result.isSuccess() && n2Result.isSuccess() && n3Result.isSuccess()) {
getInputFileInfo().setDimension(new V3i(n1Result.getValue(), n2Result.getValue(), n3Result.getValue()));
getInputDataInfo().setDimension(new V3i(n1Result.getValue(), n2Result.getValue(), n3Result.getValue()));
} else {
parseErrorOccurred = true;
errorBuilder.append("Failed to parse image dimensions of format DxDxD, got: ");
......@@ -542,7 +542,7 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
}
sb.append("InputFile: ").append(getInputFileInfo().getFilePath()).append('\n');
sb.append("InputFile: ").append(getInputDataInfo().getFilePath()).append('\n');
sb.append("Output: ").append(getOutputFilePath()).append('\n');
sb.append("BitsPerCodebookIndex: ").append(getBitsPerCodebookIndex()).append('\n');
if (hasCodebookCacheFolder()) {
......@@ -551,19 +551,19 @@ public class ParsedCliOptions extends CompressionOptions implements Cloneable {
if (hasQuantizationType(method)) {
sb.append("Input image dims: ").append(getInputFileInfo().getDimensions().toString()).append('\n');
sb.append("Input image dims: ").append(getInputDataInfo().getDimensions().toString()).append('\n');
}
if (getInputFileInfo().isPlaneIndexSet()) {
sb.append("PlaneIndex: ").append(getInputFileInfo().getPlaneIndex()).append('\n');
if (getInputDataInfo().isPlaneIndexSet()) {
sb.append("PlaneIndex: ").append(getInputDataInfo().getPlaneIndex()).append('\n');
}
if (shouldUseMiddlePlane()) {
sb.append("Use middle plane for codebook training\n");
}
if (getInputFileInfo().isPlaneRangeSet()) {
sb.append("FromPlaneIndex: ").append(getInputFileInfo().getPlaneRange().getX()).append('\n');
sb.append("ToPlaneIndex: ").append(getInputFileInfo().getPlaneRange().getY()).append('\n');
if (getInputDataInfo().isPlaneRangeSet()) {
sb.append("FromPlaneIndex: ").append(getInputDataInfo().getPlaneRange().getFrom()).append('\n');
sb.append("ToPlaneIndex: ").append(getInputDataInfo().getPlaneRange().getInclusiveTo()).append('\n');
}
sb.append("Verbose: ").append(isVerbose()).append('\n');
......
package azgracompress.cli.functions;
import azgracompress.cli.CustomFunctionBase;
import azgracompress.cli.InputFileInfo;
import azgracompress.io.InputDataInfo;
import azgracompress.cli.ParsedCliOptions;
import azgracompress.data.ImageU16;
import azgracompress.data.V3i;
......@@ -125,9 +125,9 @@ public class MeasurePlaneErrorFunction extends CustomFunctionBase {
PlaneError[] planeErrors = new PlaneError[dims.getZ()];
InputFileInfo refFileInfo = new InputFileInfo(compFile);
InputDataInfo refFileInfo = new InputDataInfo(compFile);
refFileInfo.setDimension(dims);
InputFileInfo compFileInfo = new InputFileInfo(compressedFile);
InputDataInfo compFileInfo = new InputDataInfo(compressedFile);
compFileInfo.setDimension(dims);
final RawDataLoader refPlaneloader = new RawDataLoader(refFileInfo);
......
package azgracompress.compression;
import azgracompress.cli.InputFileInfo;
import azgracompress.io.InputDataInfo;
import azgracompress.data.V2i;
import azgracompress.data.V3i;
import azgracompress.fileformat.QuantizationType;
......@@ -12,7 +12,7 @@ public class CompressionOptions {
/**
* Input image or compressed file.
*/
private InputFileInfo inputFileInfo;
private InputDataInfo inputDataInfo;
/**
* Output image or compressed file.
......@@ -40,26 +40,11 @@ public class CompressionOptions {
*/
private V2i vectorDimension = new V2i(0);
/**
* Dimensions of the input image data.
*/
private V3i imageDimension = new V3i(0);
/**
* Flag, whether to use middle plane as reference plane for codebook creation.
*/
private boolean useMiddlePlane = false;
/**
* Index of the plane to compress.
*/
private Integer planeIndex = null;
/**
* Range of the planes to compress.
*/
Interval<Integer> planeRange = null;
/**
* Number of workers to be used for different operations.
*/
......@@ -70,13 +55,6 @@ public class CompressionOptions {
*/
private boolean verbose;
public boolean isPlaneIndexSet() {
return (planeIndex != null);
}
public boolean isPlaneRangeSet() {
return (planeRange != null);
}
public boolean hasCodebookCacheFolder() {
return codebookCacheFolder != null;
......@@ -92,12 +70,12 @@ public class CompressionOptions {
return verbose;
}
public InputFileInfo getInputFileInfo() {
return inputFileInfo;
public InputDataInfo getInputDataInfo() {
return inputDataInfo;
}
public void setInputFileInfo(InputFileInfo ifi) {
this.inputFileInfo = ifi;
public void setInputDataInfo(InputDataInfo ifi) {
this.inputDataInfo = ifi;
}
public String getOutputFilePath() {
......@@ -140,14 +118,6 @@ public class CompressionOptions {
this.vectorDimension = vectorDimension;
}
public V3i getImageDimension() {
return imageDimension;
}
public void setImageDimension(V3i imageDimension) {
this.imageDimension = imageDimension;
}
public boolean shouldUseMiddlePlane() {
return useMiddlePlane;
}
......@@ -156,22 +126,6 @@ public class CompressionOptions {
this.useMiddlePlane = useMiddlePlane;
}
public Integer getPlaneIndex() {
return planeIndex;
}
public void setPlaneIndex(Integer planeIndex) {
this.planeIndex = planeIndex;
}
public Interval<Integer> getPlaneRange() {
return planeRange;
}
public void setPlaneRange(Interval<Integer> planeRange) {
this.planeRange = planeRange;
}
public int getWorkerCount() {
return workerCount;
}
......
package azgracompress.compression;
import azgracompress.cli.InputFileInfo;
import azgracompress.cli.ParsedCliOptions;
import azgracompress.io.InputDataInfo;
import azgracompress.compression.exception.ImageCompressionException;
import azgracompress.huffman.Huffman;
import azgracompress.io.OutBitStream;
......@@ -51,13 +50,12 @@ public abstract class CompressorDecompressorBase {
protected int[] getPlaneIndicesForCompression() {
final InputFileInfo ifi = options.getInputFileInfo();
final InputDataInfo ifi = options.getInputDataInfo();
if (ifi.isPlaneIndexSet()) {
return new int[]{ifi.getPlaneIndex()};
} else if (ifi.isPlaneRangeSet()) {
final int from = ifi.getPlaneRange().getX();
final int to = ifi.getPlaneRange().getY();
final int count = to - from;
final int from = ifi.getPlaneRange().getFrom();
final int count = ifi.getPlaneRange().getInclusiveTo() - from;
int[] indices = new int[count + 1];
for (int i = 0; i <= count; i++) {
......@@ -106,7 +104,7 @@ public abstract class CompressorDecompressorBase {
* @return Index of the middle plane.
*/
protected int getMiddlePlaneIndex() {
return (options.getInputFileInfo().getDimensions().getZ() / 2);
return (options.getInputDataInfo().getDimensions().getZ() / 2);
}
/**
......
......@@ -119,16 +119,17 @@ public class ImageCompressor extends CompressorDecompressorBase {
* @return Number of planes for compression.
*/
private int getNumberOfPlanes() {
if (options.isPlaneIndexSet()) {
if (options.getInputDataInfo().isPlaneIndexSet()) {
return 1;
} else if (options.isPlaneRangeSet()) {
final Interval<Integer> planeRange = options.getPlaneRange();
} else if (options.getInputDataInfo().isPlaneRangeSet()) {
final Interval<Integer> planeRange = options.getInputDataInfo().getPlaneRange();
return ((planeRange.getInclusiveTo() + 1) - planeRange.getFrom());
} else {
return options.getImageDimension().getZ();
return options.getInputDataInfo().getDimensions().getZ();
}
}
/**
* Create QCMPFile header for compressed file.
*
......@@ -145,9 +146,9 @@ public class ImageCompressor extends CompressorDecompressorBase {
final boolean oneCodebook = options.shouldUseMiddlePlane() || options.hasCodebookCacheFolder();
header.setCodebookPerPlane(!oneCodebook);
header.setImageSizeX(options.getInputFileInfo().getDimensions().getX());
header.setImageSizeY(options.getInputFileInfo().getDimensions().getY());
header.setImageSizeZ(options.getInputFileInfo().getNumberOfPlanes());
header.setImageSizeX(options.getInputDataInfo().getDimensions().getX());
header.setImageSizeY(options.getInputDataInfo().getDimensions().getY());
header.setImageSizeZ(getNumberOfPlanes());
header.setVectorDimension(options.getVectorDimension());
......
......@@ -61,7 +61,7 @@ public class ImageDecompressor extends CompressorDecompressorBase {
boolean validFile = true;
QCMPFileHeader header = null;
try (FileInputStream fileInputStream = new FileInputStream(options.getInputFileInfo().getFilePath());
try (FileInputStream fileInputStream = new FileInputStream(options.getInputDataInfo().getFilePath());
DataInputStream dataInputStream = new DataInputStream(fileInputStream)) {
header = readQCMPFileHeader(dataInputStream);
} catch (IOException ioEx) {
......@@ -114,7 +114,7 @@ public class ImageDecompressor extends CompressorDecompressorBase {
logBuilder.append("Vector size Z:\t\t").append(header.getVectorSizeZ()).append('\n');
final long headerSize = header.getHeaderSize();
final long fileSize = new File(options.getInputFileInfo().getFilePath()).length();
final long fileSize = new File(options.getInputDataInfo().getFilePath()).length();
final long dataSize = fileSize - header.getHeaderSize();
final IImageDecompressor decompressor = getImageDecompressor(header);
......@@ -165,7 +165,7 @@ public class ImageDecompressor extends CompressorDecompressorBase {
public boolean decompress() {
final Stopwatch decompressionStopwatch = Stopwatch.startNew();
final long decompressedFileSize;
try (FileInputStream fileInputStream = new FileInputStream(options.getInputFileInfo().getFilePath());
try (FileInputStream fileInputStream = new FileInputStream(options.getInputDataInfo().getFilePath());
DataInputStream dataInputStream = new DataInputStream(fileInputStream)) {
final QCMPFileHeader header = readQCMPFileHeader(dataInputStream);
......@@ -185,7 +185,7 @@ public class ImageDecompressor extends CompressorDecompressorBase {
return false;
}
final long fileSize = new File(options.getInputFileInfo().getFilePath()).length();
final long fileSize = new File(options.getInputDataInfo().getFilePath()).length();
final long dataSize = fileSize - header.getHeaderSize();
final long expectedDataSize = imageDecompressor.getExpectedDataSize(header);
if (dataSize != expectedDataSize) {
......
......@@ -2,14 +2,12 @@ package azgracompress.compression;
import azgracompress.U16;
import azgracompress.cache.QuantizationCacheManager;
import azgracompress.cli.InputFileInfo;
import azgracompress.cli.ParsedCliOptions;
import azgracompress.io.InputDataInfo;
import azgracompress.compression.exception.ImageCompressionException;
import azgracompress.data.ImageU16;
import azgracompress.huffman.Huffman;
import azgracompress.io.IPlaneLoader;
import azgracompress.io.PlaneLoaderFactory;
import azgracompress.io.RawDataIO;
import azgracompress.quantization.scalar.LloydMaxU16ScalarQuantization;
import azgracompress.quantization.scalar.SQCodebook;
import azgracompress.quantization.scalar.ScalarQuantizer;
......@@ -75,7 +73,7 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
private ScalarQuantizer loadQuantizerFromCache() throws ImageCompressionException {
QuantizationCacheManager cacheManager = new QuantizationCacheManager(options.getCodebookCacheFolder());
final SQCodebook codebook = cacheManager.loadSQCodebook(options.getInputFileInfo().getFilePath(),
final SQCodebook codebook = cacheManager.loadSQCodebook(options.getInputDataInfo().getFilePath(),
getCodebookSize());
if (codebook == null) {
throw new ImageCompressionException("Failed to read quantization values from cache file.");
......@@ -90,13 +88,13 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
* @throws ImageCompressionException When compress process fails.
*/
public long[] compress(DataOutputStream compressStream) throws ImageCompressionException {
final InputFileInfo inputFileInfo = options.getInputFileInfo();
final InputDataInfo inputDataInfo = options.getInputDataInfo();
Stopwatch stopwatch = new Stopwatch();
final boolean hasGeneralQuantizer = options.hasCodebookCacheFolder() || options.shouldUseMiddlePlane();
final IPlaneLoader planeLoader;
try {
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(inputFileInfo);
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(inputDataInfo);
} catch (Exception e) {
throw new ImageCompressionException("Unable to create SCIFIO reader. " + e.getMessage());
}
......@@ -173,23 +171,23 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
}
private int[] loadConfiguredPlanesData() throws ImageCompressionException {
final InputFileInfo inputFileInfo = options.getInputFileInfo();
final InputDataInfo inputDataInfo = options.getInputDataInfo();
final IPlaneLoader planeLoader;
try {
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(inputFileInfo);
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(inputDataInfo);
} catch (Exception e) {
throw new ImageCompressionException("Unable to create SCIFIO reader. " + e.getMessage());
}
int[] trainData = null;
if (inputFileInfo.isPlaneIndexSet()) {
if (inputDataInfo.isPlaneIndexSet()) {
try {
Log("Loading single plane data.");
trainData = planeLoader.loadPlaneU16(inputFileInfo.getPlaneIndex()).getData();
trainData = planeLoader.loadPlaneU16(inputDataInfo.getPlaneIndex()).getData();
} catch (IOException e) {
throw new ImageCompressionException("Failed to load plane data.", e);
}
} else if (inputFileInfo.isPlaneRangeSet()) {
} else if (inputDataInfo.isPlaneRangeSet()) {
Log("Loading plane range data.");
final int[] planes = getPlaneIndicesForCompression();
try {
......@@ -224,7 +222,7 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
Log(String.format("Saving cache file to %s", options.getOutputFilePath()));
QuantizationCacheManager cacheManager = new QuantizationCacheManager(options.getOutputFilePath());
try {
cacheManager.saveCodebook(options.getInputFileInfo().getFilePath(), codebook);
cacheManager.saveCodebook(options.getInputDataInfo().getFilePath(), codebook);
} catch (IOException e) {
throw new ImageCompressionException("Unable to write cache.", e);
}
......
package azgracompress.compression;
import azgracompress.cache.QuantizationCacheManager;
import azgracompress.cli.InputFileInfo;
import azgracompress.cli.ParsedCliOptions;
import azgracompress.io.InputDataInfo;
import azgracompress.compression.exception.ImageCompressionException;
import azgracompress.data.Chunk2D;
import azgracompress.data.ImageU16;
......@@ -77,7 +76,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
QuantizationCacheManager cacheManager = new QuantizationCacheManager(options.getCodebookCacheFolder());
final VQCodebook codebook = cacheManager.loadVQCodebook(options.getInputFileInfo().getFilePath(),
final VQCodebook codebook = cacheManager.loadVQCodebook(options.getInputDataInfo().getFilePath(),
getCodebookSize(),
options.getVectorDimension().toV3i());
if (codebook == null) {
......@@ -93,13 +92,13 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
* @throws ImageCompressionException When compress process fails.
*/
public long[] compress(DataOutputStream compressStream) throws ImageCompressionException {
final InputFileInfo inputFileInfo = options.getInputFileInfo();
final InputDataInfo inputDataInfo = options.getInputDataInfo();
Stopwatch stopwatch = new Stopwatch();
final boolean hasGeneralQuantizer = options.hasCodebookCacheFolder() || options.shouldUseMiddlePlane();
final IPlaneLoader planeLoader;
final int[] huffmanSymbols = createHuffmanSymbols(getCodebookSize());
try {
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(inputFileInfo);
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(inputDataInfo);
} catch (Exception e) {
throw new ImageCompressionException("Unable to create SCIFIO reader. " + e.getMessage());
}
......@@ -190,30 +189,30 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
private int[][] loadConfiguredPlanesData() throws ImageCompressionException {
final int vectorSize = options.getVectorDimension().getX() * options.getVectorDimension().getY();
final InputFileInfo inputFileInfo = options.getInputFileInfo();
final InputDataInfo inputDataInfo = options.getInputDataInfo();
final IPlaneLoader planeLoader;
try {
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(inputFileInfo);
planeLoader = PlaneLoaderFactory.getPlaneLoaderForInputFile(inputDataInfo);
} catch (Exception e) {
throw new ImageCompressionException("Unable to create SCIFIO reader. " + e.getMessage());
}
int[][] trainData = null;
Stopwatch s = new Stopwatch();
s.start();
if (inputFileInfo.isPlaneIndexSet()) {
if (inputDataInfo.isPlaneIndexSet()) {
Log("VQ: Loading single plane data.");
try {
trainData = loadPlaneQuantizationVectors(planeLoader, inputFileInfo.getPlaneIndex());
trainData = loadPlaneQuantizationVectors(planeLoader, inputDataInfo.getPlaneIndex());
} catch (IOException e) {
throw new ImageCompressionException("Failed to load plane data.", e);
}
} else {
Log(inputFileInfo.isPlaneRangeSet() ? "VQ: Loading plane range data." : "VQ: Loading all planes data.");
Log(inputDataInfo.isPlaneRangeSet() ? "VQ: Loading plane range data." : "VQ: Loading all planes data.");
final int[] planeIndices = getPlaneIndicesForCompression();
final int chunkCountPerPlane = Chunk2D.calculateRequiredChunkCountPerPlane(
inputFileInfo.getDimensions().toV2i(),
inputDataInfo.getDimensions().toV2i(),
options.getVectorDimension());
final int totalChunkCount = chunkCountPerPlane * planeIndices.length;
......@@ -259,7 +258,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
Log("Saving cache file to %s", options.getOutputFilePath());
QuantizationCacheManager cacheManager = new QuantizationCacheManager(options.getOutputFilePath());
try {
cacheManager.saveCodebook(options.getInputFileInfo().getFilePath(), lbgResult.getCodebook());
cacheManager.saveCodebook(options.getInputDataInfo().getFilePath(), lbgResult.getCodebook());
} catch (IOException e) {
throw new ImageCompressionException("Unable to write VQ cache.", e);
}
......
package azgracompress.io;
import azgracompress.cli.InputFileInfo;
import azgracompress.data.ImageU16;
import java.io.IOException;
......
package azgracompress.cli;
package azgracompress.io;
import azgracompress.data.V2i;
import azgracompress.compression.Interval;
import azgracompress.data.V3i;
/**
* Information about the input file.
*/
public class InputFileInfo {
public class InputDataInfo {
public enum DataLoaderType {
RawDataLoader,
SCIFIOLoader,
ImageJBufferLoader
}
/**
* Input file path.
*/
private final String filePath;
private boolean isRAW = true;
/**
* Type of an input data source.
*/
private DataLoaderType dataLoaderType = DataLoaderType.RawDataLoader;
/**
* Image dimension.
*/
private V3i dimension;
private boolean planeIndexSet = false;
private int planeIndex;
private boolean planeRangeSet = false;
private V2i planeRange;
public InputFileInfo(final String filePath) {
this.filePath = filePath;
}
/**
* Index of the plane to compress.
*/
private Integer planeIndex = null;
/**
* Get number of selected planes to be compressed.
*
* @return Number of planes for compression.
* Range of the planes to compress.
*/
public int getNumberOfPlanes() {
if (planeIndexSet) {
return 1;
} else if (planeRangeSet) {
return ((planeRange.getY() + 1) - planeRange.getX());
} else {
return dimension.getZ();
}
Interval<Integer> planeRange = null;
public InputDataInfo(final String filePath) {
this.filePath = filePath;
}
public void setDimension(final V3i dimension) {
this.dimension = dimension;
public boolean isPlaneIndexSet() {
return (planeIndex != null);
}
public void setPlaneIndex(final int planeIndex) {
this.planeIndexSet = true;
this.planeIndex = planeIndex;
public boolean isPlaneRangeSet() {
return (planeRange != null);
}
public void setPlaneRange(final V2i planeRange) {
this.planeRangeSet = true;
this.planeRange = planeRange;
public void setDimension(final V3i dimension) {
this.dimension = dimension;
}
public String getFilePath() {
......@@ -63,27 +62,27 @@ public class InputFileInfo {
return dimension;
}
public boolean isPlaneIndexSet() {
return planeIndexSet;
}
public int getPlaneIndex() {
public Integer getPlaneIndex() {
return planeIndex;
}
public boolean isPlaneRangeSet() {
return planeRangeSet;
public void setPlaneIndex(Integer planeIndex) {
this.planeIndex = planeIndex;
}
public V2i getPlaneRange() {
public Interval<Integer> getPlaneRange() {
return planeRange;
}
public boolean isRAW() {
return isRAW;
public void setPlaneRange(Interval<Integer> planeRange) {
this.planeRange = planeRange;
}
public DataLoaderType getDataLoaderType() {
return dataLoaderType;
}
public void setIsRaw(boolean RAW) {
isRAW = RAW;
public void setDataLoaderType(DataLoaderType dataLoaderType) {
this.dataLoaderType = dataLoaderType;
}
}
package azgracompress.io;
import azgracompress.cli.InputFileInfo;
public final class PlaneLoaderFactory {
/**
* Create concrete plane loader for the input file.
*
* @param inputFileInfo Input file information.
* @param inputDataInfo Input file information.
* @return Concrete plane loader.
* @throws Exception When fails to create plane loader.
*/
public static IPlaneLoader getPlaneLoaderForInputFile(final InputFileInfo inputFileInfo) throws Exception {
if (inputFileInfo.isRAW()) {
return new RawDataLoader(inputFileInfo);
} else {
return new SCIFIOLoader(inputFileInfo);
public static IPlaneLoader getPlaneLoaderForInputFile(final InputDataInfo inputDataInfo) throws Exception {
switch (inputDataInfo.getDataLoaderType()) {
case RawDataLoader:
return new RawDataLoader(inputDataInfo);
case SCIFIOLoader:
return new SCIFIOLoader(inputDataInfo);
case ImageJBufferLoader:
break;
default:
throw new Exception("Unsupported data loader.");
}
return null;
}
}
package azgracompress.io;
import azgracompress.cli.InputFileInfo;
import azgracompress.data.ImageU16;
import azgracompress.data.V3i;
import azgracompress.utilities.TypeConverter;
......@@ -9,18 +8,18 @@ import java.io.*;
import java.util.Arrays;
public class RawDataLoader implements IPlaneLoader {
private final InputFileInfo inputFileInfo;
private final InputDataInfo inputDataInfo;
public RawDataLoader(final InputFileInfo inputFileInfo) {
this.inputFileInfo = inputFileInfo;
public RawDataLoader(final InputDataInfo inputDataInfo) {
this.inputDataInfo = inputDataInfo;
}
@Override
public ImageU16 loadPlaneU16(int plane) throws IOException {
byte[] buffer;
final V3i rawDataDimension = inputFileInfo.getDimensions();
final V3i rawDataDimension = inputDataInfo.getDimensions();
try (FileInputStream fileStream = new FileInputStream(inputFileInfo.getFilePath())) {
try (FileInputStream fileStream = new FileInputStream(inputDataInfo.getFilePath())) {
final long planeSize = (long) rawDataDimension.getX() * (long) rawDataDimension.getY() * 2;
final long expectedFileSize = planeSize * rawDataDimension.getZ();
final long fileSize = fileStream.getChannel().size();
......@@ -56,7 +55,7 @@ public class RawDataLoader implements IPlaneLoader {
return loadPlaneU16(planes[0]).getData();
}
final int planeValueCount = inputFileInfo.getDimensions().getX() * inputFileInfo.getDimensions().getY();
final int planeValueCount = inputDataInfo.getDimensions().getX() * inputDataInfo.getDimensions().getY();
final long planeDataSize = 2 * (long) planeValueCount;
final long totalValueCount = (long) planeValueCount * planes.length;
......@@ -69,7 +68,7 @@ public class RawDataLoader implements IPlaneLoader {
Arrays.sort(planes);
try (FileInputStream fileStream = new FileInputStream(inputFileInfo.getFilePath());
try (FileInputStream fileStream = new FileInputStream(inputDataInfo.getFilePath());
DataInputStream dis = new DataInputStream(new BufferedInputStream(fileStream, 8192))) {
int lastIndex = 0;
......@@ -97,7 +96,7 @@ public class RawDataLoader implements IPlaneLoader {
@Override
public int[] loadAllPlanesU16Data() throws IOException {
final V3i imageDims = inputFileInfo.getDimensions();
final V3i imageDims = inputDataInfo.getDimensions();
final long dataSize = (long) imageDims.getX() * (long) imageDims.getY() * (long) imageDims.getZ();
if (dataSize > (long) Integer.MAX_VALUE) {
......@@ -106,7 +105,7 @@ public class RawDataLoader implements IPlaneLoader {
int[] values = new int[(int) dataSize];
try (FileInputStream fileStream = new FileInputStream(inputFileInfo.getFilePath());
try (FileInputStream fileStream = new FileInputStream(inputDataInfo.getFilePath());
DataInputStream dis = new DataInputStream(new BufferedInputStream(fileStream, 8192))) {
for (int i = 0; i < (int) dataSize; i++) {
......
package azgracompress.io;
import azgracompress.ScifioWrapper;
import azgracompress.cli.InputFileInfo;
import azgracompress.data.ImageU16;
import azgracompress.data.V3i;
import azgracompress.utilities.TypeConverter;
......@@ -13,19 +12,19 @@ import java.util.Arrays;
public class SCIFIOLoader implements IPlaneLoader {
private final InputFileInfo inputFileInfo;
private final InputDataInfo inputDataInfo;
private final Reader reader;
/**
* Create SCIFIO reader from input file.
*
* @param inputFileInfo Input file info.
* @param inputDataInfo Input file info.
* @throws IOException When fails to create SCIFIO reader.
* @throws FormatException When fails to create SCIFIO reader.
*/
public SCIFIOLoader(final InputFileInfo inputFileInfo) throws IOException, FormatException {
this.inputFileInfo = inputFileInfo;
this.reader = ScifioWrapper.getReader(this.inputFileInfo.getFilePath());
public SCIFIOLoader(final InputDataInfo inputDataInfo) throws IOException, FormatException {
this.inputDataInfo = inputDataInfo;
this.reader = ScifioWrapper.getReader(this.inputDataInfo.getFilePath());
}
@Override
......@@ -37,7 +36,7 @@ public class SCIFIOLoader implements IPlaneLoader {
throw new IOException("Unable to open plane with the reader. " + e.getMessage());
}
final int[] data = TypeConverter.unsignedShortBytesToIntArray(planeBytes);
return new ImageU16(inputFileInfo.getDimensions().toV2i(), data);
return new ImageU16(inputDataInfo.getDimensions().toV2i(), data);
}
@SuppressWarnings("DuplicatedCode")
......@@ -49,7 +48,7 @@ public class SCIFIOLoader implements IPlaneLoader {
return loadPlaneU16(planes[0]).getData();
}
final int planeValueCount = inputFileInfo.getDimensions().getX() * inputFileInfo.getDimensions().getY();
final int planeValueCount = inputDataInfo.getDimensions().getX() * inputDataInfo.getDimensions().getY();
final long planeDataSize = 2 * (long) planeValueCount;
final long totalValueCount = (long) planeValueCount * planes.length;
......@@ -81,7 +80,7 @@ public class SCIFIOLoader implements IPlaneLoader {
@Override
public int[] loadAllPlanesU16Data() throws IOException {
final V3i imageDims = inputFileInfo.getDimensions();
final V3i imageDims = inputDataInfo.getDimensions();
final long planePixelCount = (long) imageDims.getX() * (long) imageDims.getY();
final long dataSize = planePixelCount * (long) imageDims.getZ();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment