Skip to content

Commit

Permalink
ND2: reduce memory requirements
Browse files Browse the repository at this point in the history
See #6929.
  • Loading branch information
melissalinkert committed Aug 14, 2012
1 parent 051ce66 commit 42ea883
Showing 1 changed file with 44 additions and 49 deletions.
93 changes: 44 additions & 49 deletions components/bio-formats/src/loci/formats/in/NativeND2Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,54 +389,42 @@ protected void initFile(String id) throws FormatException, IOException {
int skip = len - 12 - lenOne * 2;
if (skip <= 0) skip += lenOne * 2;



// Image calibration for newer nd2 files

if (blockType.endsWith("Calibra"))
{
long veryStart = in.getFilePointer();
in.skipBytes(12); // ImageCalibra|tionLV

long endFP = in.getFilePointer() + lenOne + lenTwo - 24;
while (in.read() == 0);

while (in.getFilePointer() < endFP) {

int nameLen = in.read();

if (nameLen == 0) {
in.seek(in.getFilePointer() - 3);
nameLen = in.read();
}

if (nameLen < 0)
break;

// Get data
String attributeName = DataTools.stripString(in.readString(nameLen * 2));
double valueOrLength = in.readDouble();


if (attributeName.equals("dCalibration"))
{
if ( valueOrLength > 0) {
addGlobalMeta(attributeName, valueOrLength);
trueSizeX = valueOrLength;
}

break; // Done with calibration
}



} // while

in.seek(veryStart); // For old nd2 files
} // elseif


if (blockType.startsWith("ImageDataSeq")) {
// Image calibration for newer nd2 files

if (blockType.endsWith("Calibra")) {
long veryStart = in.getFilePointer();
in.skipBytes(12); // ImageCalibra|tionLV

long endFP = in.getFilePointer() + lenOne + lenTwo - 24;
while (in.read() == 0);

while (in.getFilePointer() < endFP) {
int nameLen = in.read();
if (nameLen == 0) {
in.seek(in.getFilePointer() - 3);
nameLen = in.read();
}
if (nameLen < 0) {
break;
}

// Get data
String attributeName =
DataTools.stripString(in.readString(nameLen * 2));
double valueOrLength = in.readDouble();

if (attributeName.equals("dCalibration")) {
if (valueOrLength > 0) {
addGlobalMeta(attributeName, valueOrLength);
trueSizeX = valueOrLength;
}
break; // Done with calibration
}
}
in.seek(veryStart); // For old nd2 files
}

if (blockType.startsWith("ImageDataSeq")) {
imageOffsets.add(new Long(fp));
imageLengths.add(new int[] {lenOne, lenTwo});
char b = (char) in.readByte();
Expand All @@ -462,6 +450,7 @@ else if (blockType.startsWith("ImageText")) {
try {
ND2Handler handler = new ND2Handler(core, imageOffsets.size());
XMLTools.parseXML(xmlString, handler);
xmlString = null;
core = handler.getCoreMetadata();
if (backupHandler == null ||
backupHandler.getChannelNames().size() == 0)
Expand Down Expand Up @@ -536,6 +525,7 @@ else if (attributeName.startsWith("l version")) {
ND2Handler handler =
new ND2Handler(core, imageOffsets.size());
XMLTools.parseXML(xmlString, handler);
xmlString = null;
core = handler.getCoreMetadata();
if (backupHandler == null) {
backupHandler = handler;
Expand Down Expand Up @@ -681,6 +671,7 @@ else if (blockType.startsWith("CustomData|Y")) {

String xmlString =
new String(xml.getBytes(), 0, (int) xml.length(), Constants.ENCODING);
xml = null;
xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ND2>" +
xmlString + "</ND2>";
xmlString = XMLTools.sanitizeXML(xmlString);
Expand All @@ -690,6 +681,7 @@ else if (blockType.startsWith("CustomData|Y")) {
ND2Handler handler =
new ND2Handler(core, getSizeX() == 0, imageOffsets.size());
XMLTools.parseXML(xmlString, handler);
xmlString = null;

channelColors = handler.getChannelColors();
if (!isLossless) {
Expand Down Expand Up @@ -1210,6 +1202,7 @@ else if (box == 0x6a703268) {
sb.append(s);
}
}
s = null;

sb.append("</NIKON>");

Expand All @@ -1230,9 +1223,11 @@ else if (box == 0x6a703268) {
core[0].dimensionOrder = "";

String xml = sb.substring(offset, len - offset);
sb = null;
handler = new ND2Handler(core, vs.size());
try {
XMLTools.parseXML(XMLTools.sanitizeXML(xml), handler);
xml = XMLTools.sanitizeXML(xml);
XMLTools.parseXML(xml, handler);
}
catch (IOException e) { }
xml = null;
Expand Down

0 comments on commit 42ea883

Please sign in to comment.