Skip to content
Snippets Groups Projects
Commit 6d6e74ef authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Propagate IOException from reading/writing of QVC files.

parent 28c691c5
No related branches found
No related tags found
No related merge requests found
...@@ -45,10 +45,12 @@ public class SQBenchmark extends BenchmarkBase { ...@@ -45,10 +45,12 @@ public class SQBenchmark extends BenchmarkBase {
if (options.getCodebookType() == CompressionOptions.CodebookType.Global) { if (options.getCodebookType() == CompressionOptions.CodebookType.Global) {
System.out.println("Loading codebook from cache"); System.out.println("Loading codebook from cache");
final QuantizationCacheManager cacheManager = new QuantizationCacheManager(cacheFolder); final QuantizationCacheManager cacheManager = new QuantizationCacheManager(cacheFolder);
final SQCodebook codebook = cacheManager.loadSQCodebook(inputFile, codebookSize); final SQCodebook codebook;
try {
if (codebook == null) { codebook = cacheManager.loadSQCodebook(inputFile, codebookSize);
} catch (final IOException e) {
System.err.println("Failed to read quantization values from cache file."); System.err.println("Failed to read quantization values from cache file.");
e.printStackTrace();
return; return;
} }
......
...@@ -121,7 +121,7 @@ public class QuantizationCacheManager { ...@@ -121,7 +121,7 @@ public class QuantizationCacheManager {
* @param codebookSize Codebook size. * @param codebookSize Codebook size.
* @return SQ cache file. * @return SQ cache file.
*/ */
public SqQvcFile loadSQCacheFile(final String imageFile, final int codebookSize) { public SqQvcFile loadSQCacheFile(final String imageFile, final int codebookSize) throws IOException {
final File fileInfo = getCacheFilePathForSQ(imageFile, codebookSize); final File fileInfo = getCacheFilePathForSQ(imageFile, codebookSize);
return (SqQvcFile) QvcFileReader.readCacheFile(fileInfo.getAbsolutePath()); return (SqQvcFile) QvcFileReader.readCacheFile(fileInfo.getAbsolutePath());
} }
...@@ -134,9 +134,7 @@ public class QuantizationCacheManager { ...@@ -134,9 +134,7 @@ public class QuantizationCacheManager {
* @param vDim Quantization vector dimension. * @param vDim Quantization vector dimension.
* @return VQ cache file. * @return VQ cache file.
*/ */
public VqQvcFile loadVQCacheFile(final String trainFile, public VqQvcFile loadVQCacheFile(final String trainFile, final int codebookSize, final V3i vDim) throws IOException {
final int codebookSize,
final V3i vDim) {
final File fileInfo = getCacheFilePathForVQ(trainFile, codebookSize, vDim); final File fileInfo = getCacheFilePathForVQ(trainFile, codebookSize, vDim);
return (VqQvcFile) QvcFileReader.readCacheFile(fileInfo.getAbsolutePath()); return (VqQvcFile) QvcFileReader.readCacheFile(fileInfo.getAbsolutePath());
} }
...@@ -148,12 +146,8 @@ public class QuantizationCacheManager { ...@@ -148,12 +146,8 @@ public class QuantizationCacheManager {
* @param codebookSize Codebook size. * @param codebookSize Codebook size.
* @return SQ codebook or null. * @return SQ codebook or null.
*/ */
public SQCodebook loadSQCodebook(final String trainFile, final int codebookSize) { public SQCodebook loadSQCodebook(final String trainFile, final int codebookSize) throws IOException {
final SqQvcFile cacheFile = loadSQCacheFile(trainFile, codebookSize); return loadSQCacheFile(trainFile, codebookSize).getCodebook();
if (cacheFile != null)
return cacheFile.getCodebook();
else
return null;
} }
/** /**
...@@ -164,15 +158,8 @@ public class QuantizationCacheManager { ...@@ -164,15 +158,8 @@ public class QuantizationCacheManager {
* @param vDim Quantization vector dimension. * @param vDim Quantization vector dimension.
* @return VQ codebook. * @return VQ codebook.
*/ */
public VQCodebook loadVQCodebook(final String trainFile, public VQCodebook loadVQCodebook(final String trainFile, final int codebookSize, final V3i vDim) throws IOException {
final int codebookSize, return loadVQCacheFile(trainFile, codebookSize, vDim).getCodebook();
final V3i vDim) {
final VqQvcFile cacheFile = loadVQCacheFile(trainFile, codebookSize, vDim);
if (cacheFile != null)
return cacheFile.getCodebook();
else
return null;
} }
/** /**
...@@ -189,7 +176,12 @@ public class QuantizationCacheManager { ...@@ -189,7 +176,12 @@ public class QuantizationCacheManager {
for (int bpci = 2; bpci < 9; bpci++) { // 2 to 8 for (int bpci = 2; bpci < 9; bpci++) { // 2 to 8
compressionParams.setBitsPerCodebookIndex(bpci); compressionParams.setBitsPerCodebookIndex(bpci);
final IQvcFile bpciCacheFile = loadCacheFile(compressionParams); final IQvcFile bpciCacheFile;
try {
bpciCacheFile = loadCacheFile(compressionParams);
} catch (final IOException e) {
continue;
}
if (bpciCacheFile != null) { if (bpciCacheFile != null) {
availableCacheFiles.add(bpciCacheFile); availableCacheFiles.add(bpciCacheFile);
} }
...@@ -207,7 +199,7 @@ public class QuantizationCacheManager { ...@@ -207,7 +199,7 @@ public class QuantizationCacheManager {
* @param compressionParams Parameters used to find cache file. * @param compressionParams Parameters used to find cache file.
* @return Quantization cache file or null if requested file doesn't exist. * @return Quantization cache file or null if requested file doesn't exist.
*/ */
public IQvcFile loadCacheFile(final CompressionOptions compressionParams) { public IQvcFile loadCacheFile(final CompressionOptions compressionParams) throws IOException {
final String path; final String path;
final int codebookSize = (int) Math.pow(2, compressionParams.getBitsPerCodebookIndex()); final int codebookSize = (int) Math.pow(2, compressionParams.getBitsPerCodebookIndex());
switch (compressionParams.getQuantizationType()) { switch (compressionParams.getQuantizationType()) {
...@@ -237,11 +229,15 @@ public class QuantizationCacheManager { ...@@ -237,11 +229,15 @@ public class QuantizationCacheManager {
* @param path Path to cache file. * @param path Path to cache file.
*/ */
public static void inspectCacheFile(final String path, final boolean verbose) { public static void inspectCacheFile(final String path, final boolean verbose) {
final IQvcFile qvcFile = QvcFileReader.readCacheFile(path); final IQvcFile qvcFile;
if (qvcFile == null) { try {
qvcFile = QvcFileReader.readCacheFile(path);
} catch (final IOException e) {
System.err.println("Provided path is not of valid QVC file."); System.err.println("Provided path is not of valid QVC file.");
e.printStackTrace();
return; return;
} }
if (!qvcFile.getHeader().validateHeader()) { if (!qvcFile.getHeader().validateHeader()) {
System.err.println("Provided file is corrupted. Header is not valid."); System.err.println("Provided file is corrupted. Header is not valid.");
return; return;
......
...@@ -17,13 +17,9 @@ public class QvcFileReader { ...@@ -17,13 +17,9 @@ public class QvcFileReader {
* @param path File path. * @param path File path.
* @return Cache file or null if reading fails. * @return Cache file or null if reading fails.
*/ */
public static IQvcFile readCacheFile(final String path) { public static IQvcFile readCacheFile(final String path) throws IOException {
try (final FileInputStream fis = new FileInputStream(path)) { try (final FileInputStream fis = new FileInputStream(path)) {
return readCacheFileImpl(fis); return readCacheFileImpl(fis);
} catch (final IOException e) {
System.err.println(e.getMessage());
e.printStackTrace();
return null;
} }
} }
......
...@@ -90,9 +90,11 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -90,9 +90,11 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
trainAndSaveCodebook(); trainAndSaveCodebook();
} }
final SQCodebook codebook = cacheManager.loadSQCodebook(options.getInputDataInfo().getCacheFileName(), getCodebookSize()); final SQCodebook codebook;
if (codebook == null) { try {
throw new ImageCompressionException("Failed to read quantization values from cache file."); codebook = cacheManager.loadSQCodebook(options.getInputDataInfo().getCacheFileName(), getCodebookSize());
} catch (final IOException e) {
throw new ImageCompressionException("Failed to read quantization values from cache file.", e);
} }
return new ScalarQuantizer(codebook); return new ScalarQuantizer(codebook);
} }
......
...@@ -107,12 +107,13 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -107,12 +107,13 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
trainAndSaveCodebook(); trainAndSaveCodebook();
} }
final VQCodebook codebook = cacheManager.loadVQCodebook(options.getInputDataInfo().getCacheFileName(), final VQCodebook codebook;
getCodebookSize(), try {
options.getQuantizationVector()); codebook = cacheManager.loadVQCodebook(options.getInputDataInfo().getCacheFileName(),
getCodebookSize(),
if (codebook == null) { options.getQuantizationVector());
throw new ImageCompressionException("Failed to read quantization vectors from cache."); } catch (final IOException e) {
throw new ImageCompressionException("Failed to read quantization vectors from cache.", e);
} }
return new VectorQuantizer(codebook); return new VectorQuantizer(codebook);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment