Skip to content

Commit

Permalink
OBF: add FLIM support
Browse files Browse the repository at this point in the history
  • Loading branch information
melissalinkert committed May 2, 2018
1 parent 72f7c92 commit 5218ee2
Showing 1 changed file with 64 additions and 9 deletions.
73 changes: 64 additions & 9 deletions components/formats-bsd/src/loci/formats/in/OBFReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ protected void initFile(String id) throws FormatException, IOException {
CoreMetadata meta_data = core.get(image);

final String name = meta_data.seriesMetadata.get("Name").toString();
metadataStore.setImageName(name, image);
metadataStore.setImageName(name.trim(), image);

@SuppressWarnings("unchecked")
final List<Double> lengths = (List<Double>) meta_data.seriesMetadata.get("Lengths");
Expand Down Expand Up @@ -449,6 +449,22 @@ private long initStack(long current) throws FormatException, IOException {
final int length = in.readInt();
final String label = in.readString(length);
labels.add(label);

if (label.endsWith("X") || (dimension == 1 && isFLIMLabel(labels.get(0)))) {
meta_data.sizeX = sizes[dimension];
}
else if (label.endsWith("Y") || (dimension == 2 && isFLIMLabel(labels.get(0)))) {
meta_data.sizeY = sizes[dimension];
}
else if (isFLIMLabel(label)) {
meta_data.sizeZ = sizes[dimension];
meta_data.moduloZ.type = FormatTools.LIFETIME;
meta_data.moduloZ.typeDescription = label;
meta_data.moduloZ.start = 0;
meta_data.moduloZ.step = 1;
meta_data.moduloZ.end = meta_data.sizeZ - 1;
meta_data.imageCount = meta_data.sizeZ * meta_data.sizeC * meta_data.sizeT;
}
}
meta_data.seriesMetadata.put("Labels", labels);

Expand Down Expand Up @@ -486,6 +502,10 @@ private long initStack(long current) throws FormatException, IOException {
}
}

private boolean isFLIMLabel(String label) {
return label.startsWith("SPCM");
}

private int getPixelType(int type) throws FormatException {
switch (type) {
case 0x01: return FormatTools.UINT8;
Expand Down Expand Up @@ -551,11 +571,17 @@ public byte[] openBytes(int no, byte[] buffer, int x, int y, int w, int h)
final int columns = getSizeX();
final int bytesPerPixel = getBitsPerPixel() / 8;

boolean isFLIM = FormatTools.LIFETIME.equals(getModuloZ().type);

final int series = getSeries();
final Stack stack = stacks.get(series);
if (stack.compression) {
if (series != currentInflatedFrame.series) {
currentInflatedFrame.bytes = new byte[rows * columns * bytesPerPixel];
int bufferSize = rows * columns * bytesPerPixel;
if (isFLIM) {
bufferSize *= getSizeZ();
}
currentInflatedFrame.bytes = new byte[bufferSize];
currentInflatedFrame.series = series;
currentInflatedFrame.number = - 1;
}
Expand All @@ -566,7 +592,7 @@ public byte[] openBytes(int no, byte[] buffer, int x, int y, int w, int h)

byte[] bytes = currentInflatedFrame.bytes;
if (no != currentInflatedFrame.number) {
if (no < currentInflatedFrame.number) {
if (no < currentInflatedFrame.number && !isFLIM) {
currentInflatedFrame.number = - 1;
}
if (currentInflatedFrame.number == - 1) {
Expand All @@ -575,7 +601,9 @@ public byte[] openBytes(int no, byte[] buffer, int x, int y, int w, int h)
}

byte[] input = new byte[8192];
while (no != currentInflatedFrame.number) {
int end = isFLIM ? getSizeZ() - 1 : no;

while (currentInflatedFrame.number != end) {
int offset = 0;
while (offset != bytes.length) {
if (inflater.needsInput()) {
Expand All @@ -586,6 +614,10 @@ public byte[] openBytes(int no, byte[] buffer, int x, int y, int w, int h)
in.read(input, 0, length);
inflater.setInput(input, 0, length);
}
else if (isFLIM && remainder == 0 && currentInflatedFrame.number >= 0) {
offset = bytes.length;
continue;
}
else {
throw new FormatException("Corrupted zlib compression");
}
Expand All @@ -603,14 +635,37 @@ else if (inflater.needsDictionary()) {
++ currentInflatedFrame.number;
}
}
for (int row = 0; row != h; ++ row) {
System.arraycopy(bytes, ((row + y) * columns + x) * bytesPerPixel, buffer, row * w * bytesPerPixel, w * bytesPerPixel);
if (isFLIM) {
for (int yy=y; yy<y+h; yy++) {
for (int xx=x; xx<x+w; xx++) {
int src = getSizeZ() * bytesPerPixel * ((yy * getSizeX()) + xx) + no * bytesPerPixel;
int dest = ((yy - y) * w * bytesPerPixel) + ((xx - x) * bytesPerPixel);
System.arraycopy(bytes, src, buffer, dest, bytesPerPixel);
}
}
}
else {
for (int row = 0; row != h; ++ row) {
System.arraycopy(bytes, ((row + y) * columns + x) * bytesPerPixel, buffer, row * w * bytesPerPixel, w * bytesPerPixel);
}
}
}
else {
for (int row = 0; row != h; ++ row) {
in.seek(stack.position + ((no * rows + row + y) * columns + x) * bytesPerPixel);
in.read(buffer, row * w * bytesPerPixel, w * bytesPerPixel);
if (isFLIM) {
for (int yy=y; yy<y+h; yy++) {
for (int xx=x; xx<x+w; xx++) {
long src = stack.position + getSizeZ() * bytesPerPixel * ((yy * getSizeX()) + xx) + no * bytesPerPixel;
int dest = ((yy - y) * w * bytesPerPixel) + ((xx - x) * bytesPerPixel);
in.seek(src);
in.read(buffer, dest, bytesPerPixel);
}
}
}
else {
for (int row = 0; row != h; ++ row) {
in.seek(stack.position + ((no * rows + row + y) * columns + x) * bytesPerPixel);
in.read(buffer, row * w * bytesPerPixel, w * bytesPerPixel);
}
}
}

Expand Down

1 comment on commit 5218ee2

@bjoernthiel
Copy link
Contributor

@bjoernthiel bjoernthiel commented on 5218ee2 Nov 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@melissalinkert Dear Melissa,

unfortunately we receive more and more complaining user feedback saying
"we can no longer open our 3-D images with Fiji" (See attached sample file).

We found this commit to be the reason for the problems and kept answering
"please use an older version". But I think we should repair our reader instead of
dealing with special builds and versions.

So can you please fix or revert this commit?

Best regards

Bjoern

20191106__zstack.zip

Please sign in to comment.