import io.scif.config.SCIFIOConfig.ImgMode; //导入依赖的package包/类
@Override
public > ImgFactory createFactory(
final Metadata m, final ImgMode[] imgModes, final T type)
throws IncompatibleTypeException
{
ImgFactory tmpFactory = null;
// Max size of a plane of a PlanarImg, or total dataset for ArrayImg.
// 2GB.
final long maxSize = ArrayUtils.safeMultiply64(2, 1024, 1024, 1024);
final long availableMem =
(long) (MemoryTools.totalAvailableMemory() * MEMORY_THRESHOLD);
long datasetSize = m.getDatasetSize();
// check for overflow
if (datasetSize <= 0) datasetSize = Long.MAX_VALUE;
// divide by 1024 to compare to max_size and avoid overflow
final long planeSize =
m.get(0).getAxisLength(Axes.X) * m.get(0).getAxisLength(Axes.Y) *
FormatTools.getBytesPerPixel(m.get(0).getPixelType());
final boolean fitsInMemory = availableMem > datasetSize;
boolean decided = false;
int modeIndex = 0;
// loop over ImgOptions in preferred order
while (!decided) {
// get the current mode, or AUTO if we've exhausted the list of
// modes
final ImgMode mode =
modeIndex >= imgModes.length ? ImgMode.AUTO : imgModes[modeIndex++];
if (mode.equals(ImgMode.AUTO)) {
if (!fitsInMemory) tmpFactory = new SCIFIOCellImgFactory<>();
else if (datasetSize < maxSize) tmpFactory = new ArrayImgFactory<>();
else tmpFactory = new PlanarImgFactory<>();
// FIXME: no CellImgFactory right now.. isn't guaranteed to
// handle all
// images well (e.g. RGB)
// else if (planeSize < maxSize) tmpFactory = new PlanarImgFactory();
// else tmpFactory = new CellImgFactory();
decided = true;
}
else if (mode.equals(ImgMode.ARRAY) && datasetSize < maxSize &&
fitsInMemory)
{
tmpFactory = new ArrayImgFactory<>();
decided = true;
}
else if (mode.equals(ImgMode.PLANAR) && planeSize < maxSize &&
fitsInMemory)
{
tmpFactory = new PlanarImgFactory<>();
decided = true;
}
else if (mode.equals(ImgMode.CELL)) {
// FIXME: no CellImgFactory right now.. isn't guaranteed to
// handle all
// images well (e.g. RGB)
// if (fitsInMemory) tmpFactory = new CellImgFactory();
// else tmpFactory = new SCIFIOCellImgFactory();
tmpFactory = new SCIFIOCellImgFactory<>();
decided = true;
}
}
return tmpFactory.imgFactory(type);
}