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

Added implementations for SCIFIOPlaneLoader.

parent fd57e44b
No related branches found
No related tags found
No related merge requests found
......@@ -50,20 +50,23 @@ public class RawDataLoader implements IPlaneLoader {
@Override
public int[] loadPlanesU16Data(int[] planes) throws IOException {
if (planes.length < 1)
if (planes.length < 1) {
return new int[0];
} else if (planes.length == 1) {
return loadPlaneU16(planes[0]).getData();
}
final int planeValueCount = inputFileInfo.getDimensions().getX() * inputFileInfo.getDimensions().getY();
final long planeDataSize = 2 * (long) planeValueCount;
final long totalValueCount = (long) planeValueCount * planes.length;
int[] values = new int[(int) totalValueCount];
if (totalValueCount > (long) Integer.MAX_VALUE) {
throw new IOException("Integer count is too big.");
}
int[] values = new int[(int) totalValueCount];
Arrays.sort(planes);
try (FileInputStream fileStream = new FileInputStream(inputFileInfo.getFilePath());
......@@ -96,12 +99,13 @@ public class RawDataLoader implements IPlaneLoader {
public int[] loadAllPlanesU16Data() throws IOException {
final V3i imageDims = inputFileInfo.getDimensions();
final long dataSize = (long) imageDims.getX() * (long) imageDims.getY() * (long) imageDims.getZ();
int[] values = new int[(int) dataSize];
if (dataSize > (long) Integer.MAX_VALUE) {
throw new IOException("RawFile size is too big.");
}
int[] values = new int[(int) dataSize];
try (FileInputStream fileStream = new FileInputStream(inputFileInfo.getFilePath());
DataInputStream dis = new DataInputStream(new BufferedInputStream(fileStream, 8192))) {
......
......@@ -3,12 +3,13 @@ package azgracompress.io;
import azgracompress.ScifioWrapper;
import azgracompress.cli.InputFileInfo;
import azgracompress.data.ImageU16;
import azgracompress.data.V3i;
import azgracompress.utilities.TypeConverter;
import io.scif.FormatException;
import io.scif.Reader;
import io.scif.jj2000.j2k.NotImplementedError;
import java.io.IOException;
import java.util.Arrays;
public class SCIFIOLoader implements IPlaneLoader {
......@@ -39,13 +40,69 @@ public class SCIFIOLoader implements IPlaneLoader {
return new ImageU16(inputFileInfo.getDimensions().toV2i(), data);
}
@SuppressWarnings("DuplicatedCode")
@Override
public int[] loadPlanesU16Data(int[] planes) throws IOException {
throw new NotImplementedError("NOT IMPLEMENTED");
if (planes.length < 1) {
return new int[0];
} else if (planes.length == 1) {
return loadPlaneU16(planes[0]).getData();
}
final int planeValueCount = inputFileInfo.getDimensions().getX() * inputFileInfo.getDimensions().getY();
final long planeDataSize = 2 * (long) planeValueCount;
final long totalValueCount = (long) planeValueCount * planes.length;
if (totalValueCount > (long) Integer.MAX_VALUE) {
throw new IOException("Integer count is too big.");
}
int[] values = new int[(int) totalValueCount];
Arrays.sort(planes);
byte[] planeBytes;
for (int i = 0; i < planes.length; i++) {
final int plane = planes[i];
try {
planeBytes = reader.openPlane(0, plane).getBytes();
} catch (FormatException e) {
throw new IOException("Unable to open plane.");
}
if (planeBytes.length != planeDataSize) {
throw new IOException("Bad byte count read from plane.");
}
TypeConverter.unsignedShortBytesToIntArray(planeBytes, values, (i * planeValueCount));
}
return values;
}
@Override
public int[] loadAllPlanesU16Data() throws IOException {
throw new NotImplementedError("NOT IMPLEMENTED");
final V3i imageDims = inputFileInfo.getDimensions();
final long planePixelCount = (long) imageDims.getX() * (long) imageDims.getY();
final long dataSize = planePixelCount * (long) imageDims.getZ();
if (dataSize > (long) Integer.MAX_VALUE) {
throw new IOException("FileSize is too big.");
}
int[] values = new int[(int) dataSize];
byte[] planeBytes;
for (int plane = 0; plane < imageDims.getZ(); plane++) {
try {
planeBytes = reader.openPlane(0, plane).getBytes();
} catch (FormatException e) {
throw new IOException("Unable to open plane.");
}
if (planeBytes.length != 2 * planePixelCount) {
throw new IOException("Bad byte count read from plane.");
}
TypeConverter.unsignedShortBytesToIntArray(planeBytes, values, (int) (plane * planePixelCount));
}
return values;
}
}
......@@ -80,4 +80,19 @@ public class TypeConverter {
}
return buffer;
}
/**
* Convert unsigned short bytes to integers. Place integers into values array from offset position.
*
* @param bytes Unsigned short bytes.
* @param values Integer value buffer.
* @param offset Offset into integer array.
*/
public static void unsignedShortBytesToIntArray(final byte[] bytes, final int[] values, final int offset) {
assert (bytes.length % 2 == 0);
for (int i = 0; i < bytes.length; i += 2) {
final int value = (int) (((bytes[i] & 0xff) << 8) | (bytes[i + 1] & 0xff));
values[offset + i] = value;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment