diff --git a/src/main/java/azgracompress/cache/QuantizationCacheManager.java b/src/main/java/azgracompress/cache/QuantizationCacheManager.java index 77a342731337b5617d02df137db417462ddd4054..5679ccf2bd40decac29afc083c147b970119f9c6 100644 --- a/src/main/java/azgracompress/cache/QuantizationCacheManager.java +++ b/src/main/java/azgracompress/cache/QuantizationCacheManager.java @@ -1,5 +1,6 @@ package azgracompress.cache; +import azgracompress.compression.CompressionOptions; import azgracompress.data.V3i; import azgracompress.fileformat.QuantizationType; import azgracompress.quantization.scalar.SQCodebook; @@ -133,6 +134,7 @@ public class QuantizationCacheManager { return fileName; } + /** * Save VQ codebook to cache. * @@ -206,7 +208,7 @@ public class QuantizationCacheManager { * @param codebookSize Codebook size. * @return SQ cache file. */ - private SQCacheFile loadSQCacheFile(final String imageFile, final int codebookSize) { + public SQCacheFile loadSQCacheFile(final String imageFile, final int codebookSize) { final File path = getCacheFilePathForSQ(imageFile, codebookSize); try { return (SQCacheFile) readCacheFile(path, new SQCacheFile()); @@ -225,9 +227,9 @@ public class QuantizationCacheManager { * @param vDim Quantization vector dimension. * @return VQ cache file. */ - private VQCacheFile loadVQCacheFile(final String trainFile, - final int codebookSize, - final V3i vDim) { + public VQCacheFile loadVQCacheFile(final String trainFile, + final int codebookSize, + final V3i vDim) { final File path = getCacheFilePathForVQ(trainFile, codebookSize, vDim); try { return (VQCacheFile) readCacheFile(path, new VQCacheFile()); @@ -282,9 +284,36 @@ public class QuantizationCacheManager { return null; } - public static ICacheFile readCacheFile(final String path) { - try (FileInputStream fis = new FileInputStream(path); - DataInputStream dis = new DataInputStream(fis)) { + public ICacheFile loadCacheFile(final CompressionOptions compressionParams) { + String path; + int codebookSize = (int) Math.pow(2, compressionParams.getBitsPerCodebookIndex()); + switch (compressionParams.getQuantizationType()) { + + case Scalar: + path = getCacheFilePathForSQ(compressionParams.getInputDataInfo().getCacheFileName(), + codebookSize).getAbsolutePath(); + break; + case Vector1D: + case Vector2D: + case Vector3D: + path = getCacheFilePathForVQ(compressionParams.getInputDataInfo().getCacheFileName(), + codebookSize, + compressionParams.getQuantizationVector()).getAbsolutePath(); + break; + default: + return null; + } + return readCacheFile(path); + } + + /** + * Read cache file by DataInputStream. + * + * @param inputStream Input stream. + * @return Cache file or null, if exception occurs. + */ + private static ICacheFile readCacheFileImpl(final InputStream inputStream) { + try (DataInputStream dis = new DataInputStream(inputStream)) { CacheFileHeader header = new CacheFileHeader(); header.readFromStream(dis); @@ -297,6 +326,31 @@ public class QuantizationCacheManager { } } + /** + * Read cache file from input stream. + * + * @param inputStream Input data stream. + * @return Cache file or null if reading fails. + */ + public static ICacheFile readCacheFile(final InputStream inputStream) { + return readCacheFileImpl(inputStream); + } + + + /** + * Read cache file from file. + * + * @param path File path. + * @return Cache file or null if reading fails. + */ + public static ICacheFile readCacheFile(final String path) { + try (FileInputStream fis = new FileInputStream(path)) { + return readCacheFileImpl(fis); + } catch (IOException e) { + return null; + } + } + /** * Inspect cache file specified by the path. * diff --git a/src/main/java/azgracompress/cache/SQCacheFile.java b/src/main/java/azgracompress/cache/SQCacheFile.java index f7ffe5a1089ec40f4336301f8137a77acaaa1b9c..b6707ad93328e87ec599cce9c8233ed44a54a9f4 100644 --- a/src/main/java/azgracompress/cache/SQCacheFile.java +++ b/src/main/java/azgracompress/cache/SQCacheFile.java @@ -39,6 +39,7 @@ public class SQCacheFile implements ICacheFile { } public void readFromStream(DataInputStream inputStream, CacheFileHeader header) throws IOException { + this.header = header; final int codebookSize = header.getCodebookSize(); final int[] centroids = new int[codebookSize]; final long[] frequencies = new long[codebookSize]; diff --git a/src/main/java/azgracompress/cache/VQCacheFile.java b/src/main/java/azgracompress/cache/VQCacheFile.java index 986a22a035eae4c238c00e6e7697588cd2f5b9ee..9a0df5e751d79911ed144f1c48c9e33456c0d1da 100644 --- a/src/main/java/azgracompress/cache/VQCacheFile.java +++ b/src/main/java/azgracompress/cache/VQCacheFile.java @@ -43,6 +43,7 @@ public class VQCacheFile implements ICacheFile { @Override public void readFromStream(DataInputStream inputStream, CacheFileHeader header) throws IOException { + this.header = header; final int codebookSize = header.getCodebookSize(); final int entrySize = header.getVectorSizeX() * header.getVectorSizeY() * header.getVectorSizeZ();