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

Improved logging of compressor and utilized try-resource.

parent 2adcefdd
No related branches found
No related tags found
No related merge requests found
...@@ -15,7 +15,6 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -15,7 +15,6 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
public SQImageCompressor(ParsedCliOptions options) { public SQImageCompressor(ParsedCliOptions options) {
super(options); super(options);
} }
/** /**
...@@ -43,6 +42,9 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -43,6 +42,9 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
for (final int quantizationValue : centroids) { for (final int quantizationValue : centroids) {
compressStream.writeShort(quantizationValue); compressStream.writeShort(quantizationValue);
} }
if (options.isVerbose()) {
Log("Wrote quantization values to compressed stream.");
}
} }
/** /**
...@@ -58,34 +60,36 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -58,34 +60,36 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
options.getImageDimension(), options.getImageDimension(),
options.getReferencePlaneIndex()); options.getReferencePlaneIndex());
Log("Creating codebook from reference plane..."); Log(String.format("Training scalar quantizer from reference plane %d.", options.getReferencePlaneIndex()));
quantizer = trainScalarQuantizerFromData(referencePlane.getData()); quantizer = trainScalarQuantizerFromData(referencePlane.getData());
writeCodebookToOutputStream(quantizer, compressStream); writeCodebookToOutputStream(quantizer, compressStream);
Log("Wrote reference codebook.");
} }
final int[] planeIndices = getPlaneIndicesForCompression(); final int[] planeIndices = getPlaneIndicesForCompression();
for (final int planeIndex : planeIndices) { for (final int planeIndex : planeIndices) {
Log(String.format("Loading plane %d...", planeIndex)); Log(String.format("Loading plane %d.", planeIndex));
final ImageU16 plane = RawDataIO.loadImageU16(options.getInputFile(), final ImageU16 plane = RawDataIO.loadImageU16(options.getInputFile(),
options.getImageDimension(), options.getImageDimension(),
planeIndex); planeIndex);
if (!options.hasReferencePlaneIndex()) { if (!options.hasReferencePlaneIndex()) {
Log("Creating plane codebook..."); Log(String.format("Training scalar quantizer from plane %d.", planeIndex));
quantizer = trainScalarQuantizerFromData(plane.getData()); quantizer = trainScalarQuantizerFromData(plane.getData());
writeCodebookToOutputStream(quantizer, compressStream); writeCodebookToOutputStream(quantizer, compressStream);
Log("Wrote plane codebook.");
} }
assert (quantizer != null); assert (quantizer != null);
Log("Writing quantization indices..."); Log("Compressing plane...");
final int[] indices = quantizer.quantizeIntoIndices(plane.getData()); final int[] indices = quantizer.quantizeIntoIndices(plane.getData());
OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048);
outBitStream.write(indices); try (OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048)) {
outBitStream.flush(); outBitStream.write(indices);
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
Log(String.format("Finished processing of plane %d", planeIndex)); Log(String.format("Finished processing of plane %d", planeIndex));
} }
} }
......
...@@ -66,6 +66,9 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -66,6 +66,9 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
compressStream.writeShort(vecVal); compressStream.writeShort(vecVal);
} }
} }
if (options.isVerbose()) {
Log("Wrote quantization vectors to compressed stream.");
}
} }
/** /**
...@@ -81,7 +84,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -81,7 +84,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
options.getImageDimension(), options.getImageDimension(),
options.getReferencePlaneIndex()); options.getReferencePlaneIndex());
Log("Creating codebook from reference plane..."); Log(String.format("Training vector quantizer from reference plane %d.", options.getReferencePlaneIndex()));
final int[][] refPlaneVectors = getPlaneVectors(referencePlane); final int[][] refPlaneVectors = getPlaneVectors(referencePlane);
quantizer = trainVectorQuantizerFromPlaneVectors(refPlaneVectors); quantizer = trainVectorQuantizerFromPlaneVectors(refPlaneVectors);
writeQuantizerToCompressStream(quantizer, compressStream); writeQuantizerToCompressStream(quantizer, compressStream);
...@@ -91,7 +94,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -91,7 +94,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
final int[] planeIndices = getPlaneIndicesForCompression(); final int[] planeIndices = getPlaneIndicesForCompression();
for (final int planeIndex : planeIndices) { for (final int planeIndex : planeIndices) {
Log(String.format("Loading plane %d...", planeIndex)); Log(String.format("Loading plane %d.", planeIndex));
final ImageU16 plane = RawDataIO.loadImageU16(options.getInputFile(), final ImageU16 plane = RawDataIO.loadImageU16(options.getInputFile(),
options.getImageDimension(), options.getImageDimension(),
planeIndex); planeIndex);
...@@ -99,7 +102,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -99,7 +102,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
final int[][] planeVectors = getPlaneVectors(plane); final int[][] planeVectors = getPlaneVectors(plane);
if (!options.hasReferencePlaneIndex()) { if (!options.hasReferencePlaneIndex()) {
Log("Creating plane codebook..."); Log(String.format("Training vector quantizer from plane %d.", planeIndex));
quantizer = trainVectorQuantizerFromPlaneVectors(planeVectors); quantizer = trainVectorQuantizerFromPlaneVectors(planeVectors);
writeQuantizerToCompressStream(quantizer, compressStream); writeQuantizerToCompressStream(quantizer, compressStream);
Log("Wrote plane codebook."); Log("Wrote plane codebook.");
...@@ -107,13 +110,19 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm ...@@ -107,13 +110,19 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
assert (quantizer != null); assert (quantizer != null);
Log("Writing quantization indices..."); Log("Compression plane...");
final int[] indices = quantizer.quantizeIntoIndices(planeVectors); final int[] indices = quantizer.quantizeIntoIndices(planeVectors);
OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048); try (OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048)) {
outBitStream.write(indices); outBitStream.write(indices);
outBitStream.flush(); } catch (IOException ioEx) {
Log(String.format("Finished processing of plane %d", planeIndex)); ioEx.printStackTrace();
}
Log(String.format("Finished processing of plane %d.", planeIndex));
// OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048);
// outBitStream.write(indices);
// outBitStream.flush();
// Log(String.format("Finished processing of plane %d.", planeIndex));
} }
} }
......
...@@ -3,7 +3,7 @@ package azgracompress.io; ...@@ -3,7 +3,7 @@ package azgracompress.io;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
public class OutBitStream { public class OutBitStream implements AutoCloseable {
private OutputStream outStream; private OutputStream outStream;
private byte[] buffer; private byte[] buffer;
...@@ -103,4 +103,14 @@ public class OutBitStream { ...@@ -103,4 +103,14 @@ public class OutBitStream {
write(value); write(value);
} }
} }
/**
* Flush the bitsteam on close.
* @throws Exception when flush fails.
*/
@Override
public void close() throws Exception {
flush();
System.out.println("Autoclosing OutBitStream");
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment