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
public SQImageCompressor(ParsedCliOptions options) {
super(options);
}
/**
......@@ -43,6 +42,9 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
for (final int quantizationValue : centroids) {
compressStream.writeShort(quantizationValue);
}
if (options.isVerbose()) {
Log("Wrote quantization values to compressed stream.");
}
}
/**
......@@ -58,34 +60,36 @@ public class SQImageCompressor extends CompressorDecompressorBase implements IIm
options.getImageDimension(),
options.getReferencePlaneIndex());
Log("Creating codebook from reference plane...");
Log(String.format("Training scalar quantizer from reference plane %d.", options.getReferencePlaneIndex()));
quantizer = trainScalarQuantizerFromData(referencePlane.getData());
writeCodebookToOutputStream(quantizer, compressStream);
Log("Wrote reference codebook.");
}
final int[] planeIndices = getPlaneIndicesForCompression();
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(),
options.getImageDimension(),
planeIndex);
if (!options.hasReferencePlaneIndex()) {
Log("Creating plane codebook...");
Log(String.format("Training scalar quantizer from plane %d.", planeIndex));
quantizer = trainScalarQuantizerFromData(plane.getData());
writeCodebookToOutputStream(quantizer, compressStream);
Log("Wrote plane codebook.");
}
assert (quantizer != null);
Log("Writing quantization indices...");
Log("Compressing plane...");
final int[] indices = quantizer.quantizeIntoIndices(plane.getData());
OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048);
outBitStream.write(indices);
outBitStream.flush();
try (OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048)) {
outBitStream.write(indices);
} catch (IOException ioEx) {
ioEx.printStackTrace();
}
Log(String.format("Finished processing of plane %d", planeIndex));
}
}
......
......@@ -66,6 +66,9 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
compressStream.writeShort(vecVal);
}
}
if (options.isVerbose()) {
Log("Wrote quantization vectors to compressed stream.");
}
}
/**
......@@ -81,7 +84,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
options.getImageDimension(),
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);
quantizer = trainVectorQuantizerFromPlaneVectors(refPlaneVectors);
writeQuantizerToCompressStream(quantizer, compressStream);
......@@ -91,7 +94,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
final int[] planeIndices = getPlaneIndicesForCompression();
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(),
options.getImageDimension(),
planeIndex);
......@@ -99,7 +102,7 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
final int[][] planeVectors = getPlaneVectors(plane);
if (!options.hasReferencePlaneIndex()) {
Log("Creating plane codebook...");
Log(String.format("Training vector quantizer from plane %d.", planeIndex));
quantizer = trainVectorQuantizerFromPlaneVectors(planeVectors);
writeQuantizerToCompressStream(quantizer, compressStream);
Log("Wrote plane codebook.");
......@@ -107,13 +110,19 @@ public class VQImageCompressor extends CompressorDecompressorBase implements IIm
assert (quantizer != null);
Log("Writing quantization indices...");
Log("Compression plane...");
final int[] indices = quantizer.quantizeIntoIndices(planeVectors);
OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048);
outBitStream.write(indices);
outBitStream.flush();
Log(String.format("Finished processing of plane %d", planeIndex));
try (OutBitStream outBitStream = new OutBitStream(compressStream, options.getBitsPerPixel(), 2048)) {
outBitStream.write(indices);
} catch (IOException ioEx) {
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;
import java.io.IOException;
import java.io.OutputStream;
public class OutBitStream {
public class OutBitStream implements AutoCloseable {
private OutputStream outStream;
private byte[] buffer;
......@@ -103,4 +103,14 @@ public class OutBitStream {
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