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

Raw image data load/write.

parent 7d5173af
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_9">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.6.1" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:17.0.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" />
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.6" level="project" />
</component>
</module>
\ No newline at end of file
......@@ -4,6 +4,8 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.scijava</groupId>
<artifactId>DataCompressor</artifactId>
<version>1.0-SNAPSHOT</version>
......@@ -26,6 +28,11 @@
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
......
import com.google.gson.internal.$Gson$Preconditions;
import compression.data.Chunk3D;
import compression.data.ChunkIO;
import compression.data.ImageU16;
import compression.data.V3i;
import compression.data.V3l;
import compression.de.DeException;
import compression.de.DeHistory;
import compression.de.IDESolver;
import compression.de.jade.JadeSolver;
import compression.de.shade.ILShadeSolver;
import compression.de.shade.LShadeSolver;
import compression.quantization.QuantizationValueCache;
import compression.io.RawDataIO;
import compression.quantization.scalar.LloydMaxIteration;
import compression.quantization.scalar.LloydMaxU16ScalarQuantization;
import compression.quantization.vector.CodebookEntry;
import compression.quantization.vector.LBGResult;
import compression.quantization.vector.LBGVectorQuantizer;
import compression.utilities.Utils;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;
import java.util.Random;
public class DataCompressor {
......@@ -35,6 +30,19 @@ public class DataCompressor {
public static void main(String[] args) throws IOException {
ImageU16 img = null;
try {
img = RawDataIO.loadImageU16("D:\\tmp\\tiff_data\\fused_tp_10_ch_1_16bit.raw", new V3i(1041, 996, 946), 359 - 1);
RawDataIO.writeImageU16("D:\\tmp\\tiff_data\\fused_tp_10_ch_1_16bit_p359.raw", img);
} catch (Exception e) {
e.printStackTrace();
}
/*
Chunk3D[] loadedChunks = ChunkIO.loadChunks("D:\\tmp\\server-dump\\chunks.bin");
final int xs = 11;
......@@ -57,24 +65,6 @@ public class DataCompressor {
return;
}
// QuantizationValueCache cache = new QuantizationValueCache("D:\\tmp\\bdv_cache");
// if (!cache.areVectorQuantizationValueCached("initial_load.bin", 3, 4, 1)) {
// CodebookEntry[] codebook = new CodebookEntry[]{
// new CodebookEntry(new int[]{0, 100, 200, 300}),
// new CodebookEntry(new int[]{1212, 5223, 6651, 12300}),
// new CodebookEntry(new int[]{25436, 33333, 44856, 0xffff})
// };
// cache.saveQuantizationValues("initial_load.bin", codebook);
// } else {
// CodebookEntry[] codebook = cache.readCachedValues("initial_load.bin", 3, 4, 1);
// int l = codebook.length;
// }
// int[] centroids = new int[]{0, 125, 355, 400, 500, 700, 2550, 4000, 10000, 50000, 0xffff};
// cache.saveQuantizationValue("test.bin", centroids);
// int[] read = cache.readCachedValues("test.bin", centroids.length);
String sourceFile = "D:\\tmp\\server-dump\\initial_load.bin";
int NumberOfBits = 3;
String output = "lloyd";
......@@ -99,6 +89,7 @@ public class DataCompressor {
//jade(values, Dimension, 5 * Dimension, 500, "JADE-5bits.csv");
//lshade(values, Dimension, 5 * Dimension, 1000, output);
//ilshade(values, Dimension, 100, 800, "iL-SHADE-2bits-800it.csv");
*/
}
private static void appendLineToFile(final String fileName, final String line) {
......
......@@ -4,11 +4,36 @@ public class ImageU16 {
private final int width;
private final int height;
private final short[] data;
private short[] data;
public ImageU16(int width, int height, short[] data) {
this.width = width;
this.height = height;
this.data = data;
}
private int index(final int x, final int y) {
assert ((x >= 0 && x < height) && (y >= 0 && y < width)) : "Index out of bounds";
return (x * width) + y;
}
public short getValueAt(final int x, final int y) {
return data[index(x, y)];
}
public void setValueAt(final int x, final int y, final short value) {
data[index(x, y)] = value;
}
public short[] getData() {
return data;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
package compression.io;
import compression.data.ImageU16;
import compression.data.V3i;
import compression.utilities.Utils;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class RawDataIO {
/**
* Load single U16 image from RAW data file.
*
* @param rawFile Path to the raw file.
* @param rawDataDimension X (Width), Y (Height) of plane and Z(Number of planes)
* @param plane Plane index.
* @return U16 image specified by the plane
*/
public static ImageU16 loadImageU16(final String rawFile, final V3i rawDataDimension, final int plane) throws Exception {
FileInputStream fileStream = new FileInputStream(rawFile);
final long planeSize = (long) rawDataDimension.getX() * (long) rawDataDimension.getY() * 2;
final long expectedFileSize = planeSize * rawDataDimension.getZ();
final long fileSize = fileStream.getChannel().size();
if (expectedFileSize != fileSize) {
throw new Exception("File specified by `rawFile` doesn't contains raw data for image of dimensions `rawDataDimension`");
}
final long planeOffset = plane * planeSize;
byte[] buffer = new byte[(int) planeSize];
if (fileStream.skip(planeOffset) != planeOffset) {
throw new Exception("Failed to skip.");
}
if (fileStream.read(buffer, 0, (int) planeSize) != planeSize) {
throw new Exception("Read wrong number of bytes.");
}
fileStream.close();
ImageU16 image = new ImageU16(rawDataDimension.getX(), rawDataDimension.getY(), Utils.convertU16ByteArrayToShortArray(buffer));
return image;
}
public static void writeImageU16(final String rawFile, final ImageU16 image) throws Exception {
FileOutputStream fileStream = new FileOutputStream(rawFile, false);
byte[] buffer = Utils.convertShortArrayToByteArray(image.getData());
fileStream.write(buffer, 0, buffer.length);
fileStream.flush();
fileStream.close();
}
}
......@@ -17,6 +17,8 @@ public class Utils {
public static byte[] readFileBytes(final String path) throws FileNotFoundException {
FileInputStream fileStream = new FileInputStream(path);
try {
// final byte[] bytes = IOUtils.toByteArray(fileStream);
// return bytes;
return fileStream.readAllBytes();
} catch (IOException e) {
e.printStackTrace();
......@@ -74,15 +76,35 @@ public class Utils {
int index = 0;
for (int i = 0; i < bytes.length; i += 2) {
final int value = (int) (((bytes[i] & 0xff) << 8) | (bytes[i + 1] & 0xff));
if (value > 0) {
values[index++] = value;
continue;
}
values[index++] = value;
}
return values;
}
public static short[] convertU16ByteArrayToShortArray(final byte[] bytes) {
assert (bytes.length % 2 == 0);
short[] values = new short[bytes.length / 2];
int index = 0;
for (int i = 0; i < bytes.length; i += 2) {
final short value = (short) (((bytes[i] & 0xff) << 8) | (bytes[i + 1] & 0xff));
values[index++] = value;
}
return values;
}
public static byte[] convertShortArrayToByteArray(final short[] data) {
byte[] buffer = new byte[data.length * 2];
int j = 0;
for (final short s : data) {
buffer[j++] = (byte) ((s >> 8) & 0xff);
buffer[j++] = (byte) (s & 0xff);
}
return buffer;
}
public static int[] convertShortArrayToIntArray(final short[] src) {
int[] result = new int[src.length];
int intValue;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment