Skip to content

Commit

Permalink
Flex: fix remaining field indexing bugs
Browse files Browse the repository at this point in the history
Fixes the fixes in 4273aa2.

See #9375.
  • Loading branch information
melissalinkert committed Aug 2, 2012
1 parent 4273aa2 commit 1655396
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions components/bio-formats/src/loci/formats/in/FlexReader.java
Expand Up @@ -117,6 +117,7 @@ public class FlexReader extends FormatReader {
private Vector<String> lightSourceCombinationRefs;
private Vector<String> filterSets;
private HashMap<String, FilterGroup> filterSetMap;
private HashMap<Integer, Timestamp> acquisitionDates;

private Vector<String> measurementFiles;

Expand Down Expand Up @@ -237,8 +238,8 @@ public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
IFD ifd = file.offsets == null ?
file.ifds.get(imageNumber) : firstFile.ifds.get(0);

RandomAccessInputStream s =
(wellRow == 0 && wellCol == 0 && file.field == 0) ? firstStream :
boolean useFirstStream = wellRow == 0 && wellCol == 0 && file.field == 0;
RandomAccessInputStream s = useFirstStream ? firstStream :
new RandomAccessInputStream(getFileHandle(file.file));

int nBytes = ifd.getBitsPerSample()[0] / 8;
Expand All @@ -253,6 +254,9 @@ public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
TiffParser tp = new TiffParser(s);
tp.getSamples(ifd, buf, x, y, w, h);
factor = file.factors[imageNumber];
if (!useFirstStream) {
tp.getStream().close();
}
}
else {
int index = getImageCount() * pos[0] + no;
Expand All @@ -278,6 +282,10 @@ public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
}
}

if (!useFirstStream) {
s.close();
}

return buf;
}

Expand Down Expand Up @@ -309,6 +317,7 @@ public void close(boolean fileOnly) throws IOException {
planePositionZ.clear();
planeExposureTime.clear();
planeDeltaT.clear();
acquisitionDates = null;
}
}

Expand All @@ -319,6 +328,7 @@ protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);

measurementFiles = new Vector<String>();
acquisitionDates = new HashMap<Integer, Timestamp>();

if (checkSuffix(id, FLEX_SUFFIX)) {
initFlexFile(id);
Expand All @@ -328,8 +338,11 @@ else if (checkSuffix(id, RES_SUFFIX)) {
}
else initMeaFile(id);

if ((plateCount % fieldCount) == 0) {
plateCount /= fieldCount;
if (plateCount == flexFiles.size()) {
plateCount /= wellCount;
if ((plateCount % fieldCount) == 0) {
plateCount /= fieldCount;
}
}
}

Expand Down Expand Up @@ -537,6 +550,10 @@ private void populateMetadataStore(MetadataStore store) throws FormatException
store.setImageName("Well " + wellRow + "-" + (wellNumber[pos[1]][1] + 1) +
"; Field #" + (pos[0] + 1), i);

if (acquisitionDates.get(i) != null) {
store.setImageAcquisitionDate(acquisitionDates.get(i), i);
}

if (wellRows == 0 && wellColumns == 0) {
well = pos[1];
NonNegativeInteger row = new NonNegativeInteger(wellNumber[pos[1]][0]);
Expand Down Expand Up @@ -659,6 +676,7 @@ else if (c > 0 && lightSources != null && lightSources.size() == 1)

for (int image=0; image<getImageCount(); image++) {
int plane = i * getImageCount() + image;
int c = getZCTCoords(image)[1];
if (plane < planePositionX.size()) {
store.setPlanePositionX(planePositionX.get(plane), i, image);
}
Expand All @@ -668,8 +686,9 @@ else if (c > 0 && lightSources != null && lightSources.size() == 1)
if (plane < planePositionZ.size()) {
store.setPlanePositionZ(planePositionZ.get(plane), i, image);
}
if (plane < planeExposureTime.size()) {
store.setPlaneExposureTime(planeExposureTime.get(plane), i, image);
if (plane - image + c < planeExposureTime.size()) {
store.setPlaneExposureTime(
planeExposureTime.get(plane - image + c), i, image);
}
if (plane < planeDeltaT.size()) {
store.setPlaneDeltaT(planeDeltaT.get(plane), i, image);
Expand Down Expand Up @@ -778,6 +797,7 @@ private void parseFlexFile(int currentWell, int wellRow, int wellCol,
else {
TiffParser parser = new TiffParser(file.file);
ifd = parser.getFirstIFD();
parser.getStream().close();
}
String xml = XMLTools.sanitizeXML(ifd.getIFDStringValue(FLEX));

Expand Down Expand Up @@ -852,7 +872,7 @@ private void populateCoreMetadata(int wellRow, int wellCol, int field,
LOGGER.info("Populating core metadata for well row " + wellRow +
", column " + wellCol);
if (getSizeC() == 0 && getSizeT() == 0) {
if ((fieldCount % imageNames.size()) != 0) {
if (fieldCount == 0 || (imageNames.size() % fieldCount) != 0) {
fieldCount = 1;
}

Expand Down Expand Up @@ -917,8 +937,8 @@ private void populateCoreMetadata(int wellRow, int wellCol, int field,
core[0].littleEndian = ifd.isLittleEndian();
core[0].pixelType = ifd.getPixelType();

if (fieldCount == 1 && plateCount * wellCount != nFiles) {
fieldCount = nFiles;
if (fieldCount == 1) {
fieldCount *= nFiles;
}

int seriesCount = plateCount * wellCount * fieldCount;
Expand Down Expand Up @@ -1272,7 +1292,7 @@ private void groupFiles(String[] fileList, MetadataStore store)
flexFiles.add(file);
parseFlexFile(currentWell, row, col, field, firstFile, store);
}
if (currentWell != 0) s.close();
if (currentWell != 0 || field != 0) s.close();
if (firstFile) firstFile = false;
}
currentWell++;
Expand Down Expand Up @@ -1304,14 +1324,14 @@ private FlexFile lookupFile(int fileSeries) {
effectiveFieldCount = 1;
}

int[] lengths = new int[] {effectiveFieldCount, wellCount, plateCount};
int[] lengths = new int[] {fieldCount, wellCount, plateCount};
int[] pos = FormatTools.rasterToPosition(lengths, fileSeries);

boolean zeroWell = wellCount == 1 && effectiveFieldCount == 1;
int row = zeroWell ? 0 : wellNumber[pos[1]][0];
int col = zeroWell ? 0 : wellNumber[pos[1]][1];

return lookupFile(row, col, pos[0]);
return lookupFile(row, col, effectiveFieldCount == 1 ? 0 : pos[0]);
}

// -- Helper classes --
Expand Down Expand Up @@ -1401,7 +1421,7 @@ else if ("Image".equals(parentQName)) {

if (qName.equals("DateTime")) {
if (value != null) {
store.setImageAcquisitionDate(new Timestamp(value), currentSeries);
acquisitionDates.put(currentSeries, new Timestamp(value));
}
}
}
Expand Down

0 comments on commit 1655396

Please sign in to comment.