Skip to content

Commit

Permalink
Add new openPlane function to read pixel data without initializing
Browse files Browse the repository at this point in the history
  • Loading branch information
dgault committed Mar 11, 2024
1 parent fe114ac commit 35a2fef
Showing 1 changed file with 102 additions and 1 deletion.
103 changes: 102 additions & 1 deletion src/loci/formats/in/ZarrReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ private int[] get5DShape(int [] originalShape) {
* @param size of the shape required by jzarr
* @return a 5D shape to be used within the reader
*/
private int[] getOriginalShape(int [] shape5D, int size) {
private static int[] getOriginalShape(int [] shape5D, int size) {
int [] shape = new int[size];
int shape5DIndex = 4;
for (int s = shape.length - 1; s >= 0; s--) {
Expand Down Expand Up @@ -1147,6 +1147,7 @@ protected ArrayList<String> getAvailableOptions() {
optionsList.add(LIST_PIXELS_KEY);
optionsList.add(QUICK_READ_KEY);
optionsList.add(INCLUDE_LABELS_KEY);
optionsList.add(ALT_STORE_KEY);
return optionsList;
}

Expand Down Expand Up @@ -1241,4 +1242,104 @@ private void reloadOptionsFile(String id) {
}
}
}

public static byte[] openPlane(String path, byte[] buf) throws FormatException, IOException {
String rootPath = path.substring(0, path.indexOf(".zarr") + 5);
String altPath = loadAltStoreOption(rootPath);
if (altPath == null) altPath = rootPath;
ZarrService service = new JZarrServiceImpl(altPath);
openZarr(path, service);
int [] shape = {1, 1, 1, 1, 1};
int [] zarrShape = service.getShape();
shape[4] = zarrShape[zarrShape.length - 1];
shape[3] = zarrShape[zarrShape.length - 2];
int zarrArrayShapeSize = zarrShape.length;
if (zarrArrayShapeSize < 5) {
shape = getOriginalShape(shape, zarrArrayShapeSize);
}
int [] offsets = {0, 0, 0, 0, 0};
Object image = service.readBytes(shape, offsets);
int w = shape[4];
int h = shape[3];
System.out.println("Success: Read image");
boolean little = service.isLittleEndian();
int bpp = FormatTools.getBytesPerPixel(service.getPixelType());
if (image instanceof byte[]) {
byte [] data = (byte []) image;
for (int i = 0; i < data.length; i++) {
DataTools.unpackBytes(data[i], buf, i, 1, little);
}
}
else if (image instanceof short[]) {
short[] data = (short[]) image;
for (int row = 0; row < h; row++) {
int base = row * w * bpp;
for (int i = 0; i < w; i++) {
DataTools.unpackBytes(data[(row * w) + i], buf, base + 2 * i, 2, little);
}
}
}
else if (image instanceof int[]) {
int[] data = (int[]) image;
for (int row = 0; row < h; row++) {
int base = row * w * bpp;
for (int i = 0; i < w; i++) {
DataTools.unpackBytes(data[(row * w) + i], buf, base + 4 * i, 4, little);
}
}
}
else if (image instanceof float[]) {
float[] data = (float[]) image;
for (int row = 0; row < h; row++) {
int base = row * w * bpp;
for (int i = 0; i < w; i++) {
int value = Float.floatToIntBits(data[(row * w) + i]);
DataTools.unpackBytes(value, buf, base + 4 * i, 4, little);
}
}
}
else if (image instanceof double[]) {
double[] data = (double[]) image;
for (int row = 0; row < h; row++) {
int base = row * w * bpp;
for (int i = 0; i < w; i++) {
long value = Double.doubleToLongBits(data[(row * w) + i]);
DataTools.unpackBytes(value, buf, base + 8 * i, 8, little);
}
}
}
return buf;
}

private static void openZarr(String path, ZarrService service) {
try {
String canonicalPath = new Location(path).getCanonicalPath();
LOGGER.debug("Opening zarr statically at path: {}", canonicalPath);
service.open(canonicalPath);
} catch (IOException | FormatException e) {
e.printStackTrace();
}
}

/**
* Reloads the bfoptions file statically, only reading the value for the alterntiave file store
*/
private static String loadAltStoreOption(String id) {
String optionsFile = DynamicMetadataOptions.getMetadataOptionsFile(id);
if (optionsFile != null) {
MetadataOptions options = new DynamicMetadataOptions();
if (options != null && options instanceof DynamicMetadataOptions) {
try {
ArrayList<String> optionsList = new ArrayList<String>();
optionsList.add(ALT_STORE_KEY);
((DynamicMetadataOptions) options).loadOptions(optionsFile, optionsList);
return ((DynamicMetadataOptions) options).get(
ALT_STORE_KEY, ALT_STORE_DEFAULT);
} catch (Exception e) {
LOGGER.warn("Exception while attempting to read metadata options file", e);
}
}
}
return ALT_STORE_DEFAULT;
}
}

0 comments on commit 35a2fef

Please sign in to comment.