Skip to content

Commit

Permalink
Apply code review
Browse files Browse the repository at this point in the history
  • Loading branch information
zacsimile committed Dec 10, 2024
1 parent b664a98 commit 59303be
Showing 1 changed file with 92 additions and 59 deletions.
151 changes: 92 additions & 59 deletions components/formats-gpl/src/loci/formats/in/DCIMGReader.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/*
* #%L
* OME Bio-Formats package for reading and converting biological file formats.
* %%
* Copyright (C) 2005 - 2024 Open Microscopy Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

package loci.formats.in;

import java.io.IOException;
Expand Down Expand Up @@ -41,7 +66,6 @@ public class DCIMGReader extends FormatReader {
private long pixelType;
private long bytesPerImage;
private long bytesPerRow;
private int byteFactor;
private long offsetToFooter;
private boolean fourPixelCorrectionInFooter = false;
private long offsetToFourPixels;
Expand Down Expand Up @@ -80,8 +104,6 @@ public String[] getSeriesUsedFiles(boolean noPixels)
{
FormatTools.assertId(currentId, true, 1);

if (!isGroupFiles() || noPixels) return null;

return uniqueFiles;

}
Expand All @@ -91,8 +113,8 @@ public boolean isThisType(RandomAccessInputStream stream)
throws IOException
{
String desc = stream.readString(5);
if (!desc.equals("DCIMG")) return false;
return true;

return desc.equals("DCIMG");
}

@Override
Expand All @@ -104,36 +126,35 @@ public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h)
int zp = no / getSizeT();
int tp = no % getSizeT();

RandomAccessInputStream stream = new RandomAccessInputStream(uniqueFiles[zp]);
stream.order(IS_LITTLE);
try (RandomAccessInputStream stream = new RandomAccessInputStream(uniqueFiles[zp])) {
stream.order(IS_LITTLE);

// DCIMG is stored column major
stream.seek(headerSize + dataOffset + tp*bytesPerImage + byteFactor*y*getSizeX());
for (int row=h-1; row>=0; row--) {
if (fourPixelCorrectionInFooter && (row == fourPixelCorrectionLine) && (x < 4)) {
// DCIMG is stored column major
int byteFactor = FormatTools.getBytesPerPixel(getPixelType());
stream.seek(headerSize + dataOffset + tp*bytesPerImage + byteFactor*y*getSizeX());
for (int row=h-1; row>=0; row--) {
if (fourPixelCorrectionInFooter && (row == fourPixelCorrectionLine) && (x < 4)) {

// mark the current position, as we want to pick up from here
currentStreamPosition = stream.getFilePointer();
// mark the current position, as we want to pick up from here
currentStreamPosition = stream.getFilePointer();

// go get the four pixel offset
stream.seek(fourPixelCorrectionOffset);
stream.skip(byteFactor*x);
stream.read(buf, byteFactor*row*w, byteFactor*(4-x));
// go get the four pixel offset
stream.seek(fourPixelCorrectionOffset + byteFactor*x);
stream.read(buf, byteFactor*row*w, byteFactor*(4-x));

// go back to our current position, plus four pixels
stream.seek(currentStreamPosition);
stream.skipBytes(byteFactor*4);
// go back to our current position, plus four pixels
stream.seek(currentStreamPosition + byteFactor*4);

// continue reading
stream.read(buf, byteFactor*(row*w+4), byteFactor*(w-4));
} else {
stream.skipBytes(byteFactor*x);
stream.read(buf, byteFactor*row*w, byteFactor*w);
// continue reading
stream.read(buf, byteFactor*(row*w+4), byteFactor*(w-4));
} else {
stream.skipBytes(byteFactor*x);
stream.read(buf, byteFactor*row*w, byteFactor*w);
}
stream.skipBytes(byteFactor*(getSizeX() - w - x));
}
stream.skipBytes(byteFactor*(getSizeX() - w - x));
}

stream.close();
return buf;
}

Expand Down Expand Up @@ -161,10 +182,11 @@ protected void initFile(String id)
LOGGER.warn(String.format("Your file is DCAM version %d, but only %d is guaranteed to work.", version, DCIMG_VERSION_1));
}

in.skipBytes(20);
// in.skipBytes(20);

in.skipBytes(4); // long numSessions = in.readUnsignedInt();
in.skipBytes(4); // long numFrames = in.readUnsignedInt();
// in.skipBytes(4); // long numSessions = in.readUnsignedInt();
// in.skipBytes(4); // long numFrames = in.readUnsignedInt();
in.skipBytes(28);
headerSize = in.readUnsignedInt();
in.skipBytes(4);
long fileSize = in.readUnsignedInt();
Expand Down Expand Up @@ -198,10 +220,10 @@ protected void initFile(String id)

if (pixelType == DCIMG_PIXELTYPE_MONO8) {
m.pixelType = FormatTools.UINT8;
byteFactor = 1;
} else if (pixelType == DCIMG_PIXELTYPE_MONO16) {
m.pixelType = FormatTools.UINT16;
byteFactor = 2;
} else {
throw new FormatException(String.format("Unknown pixel type %d.", pixelType));
}

// DCIMG sometimes stores the first 4 pixels of one of its lines somewhere separate
Expand Down Expand Up @@ -253,32 +275,19 @@ private void scanDirectory(Location dir)
private void addFileToList(String file)
throws FormatException, IOException
{
String ext = getExtension(file);
if (!ext.equals("dcimg")) {
LOGGER.warn("File {} with extension {} failed extension check", file, ext);
return;
}
RandomAccessInputStream stream = new RandomAccessInputStream(file);
if (!isThisType(stream)) {
LOGGER.warn("File {} is not DCIMG", file);
stream.close();
if (!checkSuffix(file, "dcimg")) {
LOGGER.warn("File {} failed extension check. Does not end in 'dcimg'.", file);
return;
}
// stream.order(IS_LITTLE);
// TODO: now check width/height
companionFiles.add(file);
stream.close();
}

// TODO: Surely this exists somewhere else?
private String getExtension(String file)
{
String ext = "";
int i = file.lastIndexOf(".");
if (i > 0) {
ext = file.substring(i+1);
try (RandomAccessInputStream stream = new RandomAccessInputStream(file)) {
if (!isThisType(stream)) {
LOGGER.warn("File {} is not DCIMG.", file);
return;
}
// stream.order(IS_LITTLE);
// TODO: now check width/height
companionFiles.add(file);
}
return ext;
}

// The header and footer code feature many commented out properties "for later"
Expand Down Expand Up @@ -309,8 +318,9 @@ private void parseDCAMVersion1Header(RandomAccessInputStream stream)
CoreMetadata m = core.get(0);

stream.seek(headerSize);
stream.skipBytes(8); // long sessionLength = stream.readLong();
stream.skipBytes(52); // unknown numbers 1, 144, 65537
// stream.skipBytes(8); // long sessionLength = stream.readLong();
// stream.skipBytes(52); // unknown numbers 1, 144, 65537
stream.skipBytes(60);
m.sizeT = stream.readInt();
pixelType = stream.readInt();
stream.skipBytes(4); // long mystery1 = stream.readUnsignedInt();
Expand Down Expand Up @@ -359,8 +369,8 @@ private void parseDCAMVersion0Footer(RandomAccessInputStream stream)
// long offsetToFrameCounts = stream.readLong();
// stream.skipBytes(8);
// long offsetToTimestamps = stream.readLong();
stream.skipBytes(72);
stream.skipBytes(16);
// stream.skipBytes(16);
stream.skipBytes(88);
offsetToFourPixels = stream.readLong();
stream.skipBytes(4);
fourPixelOffsetInFrame = stream.readUnsignedInt();
Expand Down Expand Up @@ -405,4 +415,27 @@ private long getFourPixelCorrectionOffset()
return headerSize + dataOffset + bytesPerImage + 12;
}

public void close(boolean fileOnly) throws IOException {
super.close(fileOnly);
if (!fileOnly) {
version = 0;
headerSize = 0;
dataOffset = 0;
pixelType = 0;
bytesPerImage = 0;
bytesPerRow = 0;
offsetToFooter = 0;
fourPixelCorrectionInFooter = false;
offsetToFourPixels = 0;
fourPixelOffsetInFrame = 0;
fourPixelCorrectionLine = 0;
fourPixelCorrectionOffset = 0;
currentStreamPosition = 0;
frameFooterSize = 0;

companionFiles.clear();
uniqueFiles = null;
}
}

}

0 comments on commit 59303be

Please sign in to comment.