Skip to content

Commit

Permalink
TIFFFormat: refactor BigTIFF logic
Browse files Browse the repository at this point in the history
Changed the isBigTIFF flag to be a Boolean, with a null value indicating
the writer should decide whether or not to write BigTIFF data. If the
output dataset is >2GB, BigTIFF is now set automatically.
  • Loading branch information
hinerm committed Apr 17, 2014
1 parent 9b8ae6c commit f7683eb
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions scifio/src/main/java/io/scif/formats/TIFFFormat.java
Expand Up @@ -1047,6 +1047,13 @@ public static class Reader<M extends Metadata> extends

/**
* TiffWriter is the file format writer for TIFF files.
* <p>
* NB: BigTIFF writing can be controlled via the {@link #setBigTiff(boolean)}
* method, or by passing a {@link SCIFIOConfig} with a key of
* {@link Writer#BIG_TIFF_KEY} paired to the desired value. If not explicitly
* turned on or off, BigTIFF will be written if the output dataset is larger
* than 2GB in size.
* </p>
*/
public static class Writer<M extends Metadata> extends AbstractWriter<M> {

Expand All @@ -1067,7 +1074,7 @@ public static class Writer<M extends Metadata> extends AbstractWriter<M> {
// -- Fields --

/** Whether or not the output file is a BigTIFF file. */
private boolean isBigTiff;
private Boolean isBigTIFF = null;

/** The TiffSaver that will do most of the writing. */
private TiffSaver tiffSaver;
Expand All @@ -1078,12 +1085,6 @@ public static class Writer<M extends Metadata> extends AbstractWriter<M> {
/** Whether or not to check the parameters passed to saveBytes. */
private final boolean checkParams = true;

// -- Constructor --

public Writer() {
isBigTiff = false;
}

// -- AbstractWriter Methods --

@Override
Expand All @@ -1099,14 +1100,14 @@ protected String[] makeCompressionTypes() {
* reset when close() is called.
*/
public void setBigTiff(final boolean bigTiff) {
isBigTiff = bigTiff;
isBigTIFF = bigTiff;
}

/**
* @return Whether or not this Writer is configured to write BigTIFF data.
*/
public boolean isBigTiff() {
return isBigTiff;
return isBigTIFF == null ? false : isBigTIFF;
}

/**
Expand Down Expand Up @@ -1178,11 +1179,29 @@ public void setDest(final RandomAccessOutputStream dest,
synchronized (this) {
setupTiffSaver(dest, imageIndex);
}

// Check if a bigTIFF setting was requested
isBigTIFF = null;
if (config.containsKey(BIG_TIFF_KEY)) {
Object o = config.get(BIG_TIFF_KEY);
if (o instanceof Boolean) {
isBigTiff = (Boolean)o;
isBigTIFF = (Boolean)o;
}
else {
String v = String.valueOf(o).toLowerCase();
if (v.startsWith("t")) {
isBigTIFF = true;
}
else if (v.startsWith("f")) {
isBigTIFF = false;
}
}
}

// if isBigTIFF is not explicitly set and the dataset is > 2GB, write
// bigTIFF to be safe.
if (isBigTIFF == null && getMetadata().getDatasetSize() > 2147483648L) {
isBigTIFF = true;
}
}

Expand Down Expand Up @@ -1360,11 +1379,14 @@ private long prepareToWritePlane(final int imageIndex,
ifd.put(IFD.Y_RESOLUTION, new TiffRational(
(long) (physicalSizeY * 1000 * 10000), 1000));

if (!isBigTiff) {
isBigTiff =
if (!isBigTiff()) {
isBigTIFF =
(getStream().length() + 2 * (width * height * c * bytesPerPixel)) >= 4294967296L;
if (isBigTiff) {
throw new FormatException("File is too large; call setBigTiff(true)");
if (isBigTiff()) {
throw new FormatException(
"File is too large for 32-bit TIFF but BigTIFF support was " +
"disabled. Please enable by using setBigTiff(true) or passing a " +
"SCIFIOConfig object with the appropriate BIG_TIFF_KEY,true pair.");
}
}

Expand Down Expand Up @@ -1408,7 +1430,7 @@ private void setupTiffSaver(final RandomAccessOutputStream stream,

tiffSaver.setWritingSequentially(writeSequential());
tiffSaver.setLittleEndian(littleEndian);
tiffSaver.setBigTiff(isBigTiff);
tiffSaver.setBigTiff(isBigTiff());
tiffSaver.setCodecOptions(getCodecOptions());
}

Expand Down

0 comments on commit f7683eb

Please sign in to comment.