Skip to content

Commit

Permalink
When opening "bdv.n5" format, try "bdv.n5.cloud" for non-filesystem URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
tpietzsch committed Jul 31, 2024
1 parent a8280be commit a6cc545
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/main/java/bdv/img/n5/XmlIoN5ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@
*/
package bdv.img.n5;

import static mpicbg.spim.data.XmlKeys.IMGLOADER_FORMAT_ATTRIBUTE_NAME;

import java.io.File;
import java.net.URI;
import java.util.regex.Pattern;

import org.janelia.saalfeldlab.n5.N5FSReader;
import org.jdom2.Element;

import mpicbg.spim.data.SpimDataInstantiationException;
import mpicbg.spim.data.XmlHelpers;
import mpicbg.spim.data.generic.sequence.AbstractSequenceDescription;
import mpicbg.spim.data.generic.sequence.ImgLoaderIo;
import mpicbg.spim.data.generic.sequence.ImgLoaders;
import mpicbg.spim.data.generic.sequence.XmlIoBasicImgLoader;
import org.jdom2.Element;

import static mpicbg.spim.data.XmlHelpers.loadPath;
import static mpicbg.spim.data.XmlKeys.IMGLOADER_FORMAT_ATTRIBUTE_NAME;
import net.imglib2.util.Cast;

@ImgLoaderIo( format = "bdv.n5", type = N5ImageLoader.class )
public class XmlIoN5ImageLoader implements XmlIoBasicImgLoader< N5ImageLoader >
Expand All @@ -64,6 +69,31 @@ public N5ImageLoader fromXml( final Element elem, final URI basePathURI, final A
{
// final String version = elem.getAttributeValue( "version" );
final URI uri = XmlHelpers.loadPathURI( elem, "n5", basePathURI );
return new N5ImageLoader( uri, sequenceDescription );

// try to open with N5FSReader if URI is a file
try
{
final String scheme = uri.getScheme();
final boolean hasScheme = scheme != null;
if ( !hasScheme || FILE_SCHEME.asPredicate().test( scheme ) )
{
final N5FSReader n5 = new N5FSReader( new File( uri ).getAbsolutePath() );
return new N5ImageLoader( n5, uri, sequenceDescription );
}
}
catch ( Exception e ) {}

// try to open with "bdv.n5.cloud" format, if XmlIo for that can be discovered
try
{
final XmlIoBasicImgLoader< ? > io = ImgLoaders.createXmlIoForFormat( "bdv.n5.cloud" );
return Cast.unchecked( io.fromXml( elem, basePathURI, sequenceDescription ) );
}
catch ( SpimDataInstantiationException e )
{
return null;
}
}

private final static Pattern FILE_SCHEME = Pattern.compile("file", Pattern.CASE_INSENSITIVE);
}

0 comments on commit a6cc545

Please sign in to comment.