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

Choose endianess when writing binary data.

parent 205248a6
Branches
No related tags found
No related merge requests found
......@@ -38,16 +38,27 @@ public class DataCompressor {
// VectorQuantizer vq = new VectorQuantizer(codebook.getCodebook());
ScalarQuantizationBenchmark sqBenchmark = new ScalarQuantizationBenchmark(
"D:\\biology\\tiff_data\\benchmark\\fused_tp_10_ch_1_16bit.raw",
"D:\\biology\\benchmark\\test",
new int[]{358},
// new int[]{234, 294, 365, 463, 593, 682, 696},
ScalarQuantizationBenchmark[] benchmarks = new ScalarQuantizationBenchmark[3];
benchmarks[0] = new ScalarQuantizationBenchmark("D:\\biology\\tiff_data\\benchmark\\fused_tp_10_ch_0_16bit.raw",
"D:\\biology\\benchmark\\fused_tp_10_ch_0_16bit",
new int[]{198, 240, 280, 351, 573, 663, 695},
new V3i(1041, 996, 946));
benchmarks[1] = new ScalarQuantizationBenchmark(
"D:\\biology\\tiff_data\\benchmark\\fused_tp_10_ch_0_16bit_edited.raw",
"D:\\biology\\benchmark\\fused_tp_10_ch_0_16bit_edited",
new int[]{198, 240, 280, 351, 573, 663, 695},
new V3i(1041, 996, 946));
sqBenchmark.startBenchmark();
benchmarks[2] = new ScalarQuantizationBenchmark("D:\\biology\\tiff_data\\benchmark\\fused_tp_10_ch_1_16bit.raw",
"D:\\biology\\benchmark\\fused_tp_10_ch_1_16bit",
new int[]{235, 295, 366, 464, 594, 683, 697},
new V3i(1041, 996, 946));
for (ScalarQuantizationBenchmark benchmark : benchmarks)
{
benchmark.startBenchmark();
}
// ScalarQuantizationBenchmark sqBenchmark = new ScalarQuantizationBenchmark(
// "D:\\biology\\tiff_data\\fused_tp_10_ch_1_16bit_edited.raw",
// "D:\\biology\\benchmark\\scalar_edited",
......
......@@ -47,7 +47,8 @@ public class ScalarQuantizationBenchmark {
private boolean saveQuantizedPlaneData(final short[] data, final String filename) {
ImageU16 img = new ImageU16(rawImageDims.getX(), rawImageDims.getY(), data);
try {
RawDataIO.writeImageU16(getFileNamePath(filename), img);
// NOTE(Moravec): Use big endian so that FIJI can read the image.
RawDataIO.writeImageU16(getFileNamePath(filename), img, false);
System.out.println(String.format("Saved %s", filename));
} catch (Exception e) {
e.printStackTrace();
......@@ -63,7 +64,8 @@ public class ScalarQuantizationBenchmark {
rawImageDims.getY(),
TypeConverter.intArrayToShortArray(differenceData));
try {
RawDataIO.writeImageU16(path, img);
// NOTE(Moravec): Use little endian so that gnuplot can read the array.
RawDataIO.writeImageU16(path, img, true);
System.out.println("Saved difference to: " + path);
} catch (Exception e) {
e.printStackTrace();
......@@ -77,7 +79,8 @@ public class ScalarQuantizationBenchmark {
for (final int planeIndex : planes) {
System.out.println(String.format("Loading plane %d ...", planeIndex));
final short[] planeData = loadPlaneData(planeIndex);
// NOTE(Moravec): Actual planeIndex is zero based.
final short[] planeData = loadPlaneData(planeIndex-1);
if (planeData.length == 0) {
System.err.println(String.format("Failed to load plane %d data. Skipping plane.", planeIndex));
return;
......@@ -106,16 +109,17 @@ public class ScalarQuantizationBenchmark {
codebookSize,
method));
if (!RawDataIO.writeDataI32(centroidsFile, quantizer.getCentroids())) {
// NOTE(Moravec): Centroids are saved in little endian order.
if (!RawDataIO.writeDataI32(centroidsFile, quantizer.getCentroids(), true)) {
System.err.println("Failed to save quantizer centroids.");
return;
}
final String quantizedFile = String.format("p%d_cb%d%s.raw", (planeIndex + 1), codebookSize, method);
final String quantizedFile = String.format("p%d_cb%d%s.raw", planeIndex, codebookSize, method);
final String absoluteDiffFile = String.format("p%d_cb%d%s_diff.raw",
(planeIndex + 1),
planeIndex,
codebookSize,
method);
......
......@@ -50,14 +50,16 @@ public class RawDataIO {
return image;
}
public static void writeImageU16(final String rawFile, final ImageU16 image) throws IOException {
byte[] buffer = TypeConverter.shortArrayToByteArray(image.getData());
public static void writeImageU16(final String rawFile,
final ImageU16 image,
final boolean littleEndian) throws IOException {
byte[] buffer = TypeConverter.shortArrayToByteArray(image.getData(), littleEndian);
writeBytesToFile(rawFile, buffer);
}
public static boolean writeDataI32(String rawFile, int[] differenceData) {
public static boolean writeDataI32(String rawFile, int[] differenceData, final boolean littleEndian) {
byte[] buffer = TypeConverter.intArrayToByteArray(differenceData);
byte[] buffer = TypeConverter.intArrayToByteArray(differenceData, littleEndian);
try {
writeBytesToFile(rawFile, buffer);
} catch (IOException e) {
......
......@@ -53,7 +53,6 @@ public class TypeConverter {
public static short[] intArrayToShortArray(final int[] src) {
short[] result = new short[src.length];
short shortValue;
for (int i = 0; i < src.length; i++) {
if (src[i] < 0 || src[i] > U16.Max) {
throw new RuntimeException("Source value is outside of bounds for 16-bit unsigned integer.");
......@@ -63,27 +62,38 @@ public class TypeConverter {
return result;
}
public static byte[] shortArrayToByteArray(final short[] data) {
public static byte[] shortArrayToByteArray(final short[] data, final boolean littleEndian) {
byte[] buffer = new byte[data.length * 2];
int j = 0;
for (final short s : data) {
// NOTE(Moravec): Use little endian.
buffer[j++] = (byte) (s & 0xff);
buffer[j++] = (byte) ((s >> 8) & 0xff);
if (littleEndian) {
buffer[j++] = (byte) (s & 0xff);
buffer[j++] = (byte) ((s >> 8) & 0xff);
} else {
buffer[j++] = (byte) ((s >> 8) & 0xff);
buffer[j++] = (byte) (s & 0xff);
}
}
return buffer;
}
public static byte[] intArrayToByteArray(final int[] data) {
public static byte[] intArrayToByteArray(final int[] data, final boolean littleEndian) {
byte[] buffer = new byte[data.length * 4];
int j = 0;
for (final int v : data) {
// NOTE(Moravec): Use little endian.
buffer[j++] = (byte) (v & 0xFF);
buffer[j++] = (byte) ((v >>> 8) & 0xFF);
buffer[j++] = (byte) ((v >>> 16) & 0xFF);
buffer[j++] = (byte) ((v >>> 24) & 0xFF);
if (littleEndian) {
buffer[j++] = (byte) (v & 0xFF);
buffer[j++] = (byte) ((v >>> 8) & 0xFF);
buffer[j++] = (byte) ((v >>> 16) & 0xFF);
buffer[j++] = (byte) ((v >>> 24) & 0xFF);
} else {
buffer[j++] = (byte) ((v >>> 24) & 0xFF);
buffer[j++] = (byte) ((v >>> 16) & 0xFF);
buffer[j++] = (byte) ((v >>> 8) & 0xFF);
buffer[j++] = (byte) (v & 0xFF);
}
}
return buffer;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment