Saving image with SCIFIO vs ImageJ

Hi everyone,
When saving images as tif, the SCIFIO methods for storing a tiff (like the ImgSaver class or getting a writer) don’t even come close to the save as>tiff menu of FIJI software in terms of speed.

Any idea why?

Thanks

Here are some related issues on GitHub, where poor performance is discussed:

Thanks.

It’s kind of mind boggling that requesting the same save service as the one ImageJ app uses does not yield the same performance.

Saving a tiff should not be this complicated in any case :smiley:

Dear @ctrueden
Any suggestion?

Also,
In the most recent version of the library, the “ImgSaver” methods don’t accept “Writer” objects,
yet this is not the case for “ImgReader” methods.

Any reason why you decided to do this? Because this was an easy way to create a tiff with Metadata and Img interface of imglib2 library.

BTW, I think I’m using the latest version of SCIFIO (version 0.38.3-SNAPSHOT)
and it should be the latest version, because it asks for “Location” objects rather than Strings for the file path.

Unless I’m making some sort of mistake!

@Masoudas Have you seen this page?

I’m not sure what you are asking. It’s true that writing TIFF files with SCIFIO (as of this writing) is very slow.

Sorry, without digging in, I don’t know. Was a method removed that you need? Can you provide a link? If the method was removed as part of the 0.38.0 work (updating to Location API), then I’d definitely like to restore those methods to preserve backwards compatibility. Just let me know exactly which.

1 Like

Thanks for the response @ctrueden,
as always appreciate your work,

So here’s a link to the ImgOpener class, where we have methods that accept Reader objects:
https://javadoc.scijava.org/SCIFIO/io/scif/img/ImgOpener.html

And here’s a link to the ImgSaver class, where there’s no method that accepts Writer objects:
https://javadoc.scijava.org/SCIFIO/io/scif/img/ImgSaver.html

A writer method that I use from the previous version is as follows:
ImgSaver.saveImg(Writer w, Img<?> img, SCIFIOConfig config)

The good thing about accepting writers is that it’s possible to write Metadata.
I’m not exactly sure why you decided to remove them.

I hope this clears things up a bit.

@Masoudas Thanks for the additional information. Checking the git history, it seems the methods in question were removed as part of this commit:

I readded the methods in question on a new imgsaver-api branch:

I didn’t test, though. Can you give it a try and let me know if it works? I’ll push it to master if so.

Thanks @ctrueden

At least with this snippet, it’s not working, and the issue is with the TiffFormat, and not image saver for some reason!

// Write the same image, with the medadata of the original image.
        
        // Image to be read
        File source = new File("/home/masoud/Documents/SampleImages/source.tif");
        FileLocation sourcelocation = new FileLocation(source);

        // Image to be written
        File destination = new File(source.getParent(), "destination.tif");
        FileLocation destLocation = new FileLocation(destination);


        // Get the metadata
        SCIFIO scifio = new SCIFIO();
        Reader reader = scifio.initializer().initializeReader(sourcelocation);
        Metadata meta = reader.getMetadata();
    
        // Get the actual image.
        Img<UnsignedShortType> diskImage = new ImgOpener(scifio.getContext()).openImgs(sourcelocation, new UnsignedShortType()).get(0);


        final TIFFFormat tiffFormat = new TIFFFormat();   
        tiffFormat.setContext(scifio.context()); 
        try {
            // Get writer and set metadata
            Writer writer = tiffFormat.createWriter();
            writer.setDest(destLocation);
            writer.setMetadata(meta);
            
            // Write the image
            ImgSaver save = new ImgSaver(scifio.getContext());
            save.saveImg(writer, diskImage);

        } catch (FormatException e) {
    
        }

Hi @Masoudas,

sorry for the long delay! There are now two PRs to SCIFIO which, at least for me, make your example work (#427, #428). They still need to be reviewed. The next SCIFIO release will include these or better fixes and will soon be available via pom-scijava 29.0.0.

I only had to slightly modify your snippet:

..
        try {
            // Get writer and set metadata
            Writer writer = tiffFormat.createWriter();
            writer.setMetadata(meta);             // call this first..
            writer.setDest(destLocation);        // .. before setting the destination
            
            // Write the image
            ImgSaver save = new ImgSaver(scifio.getContext());
            save.saveImg(writer, diskImage);

        } catch (FormatException e) {
            e.printStackTrace(); 
        }

Apparently the metadata has to be written before setting the destination. In the original snippet this was creating a FormatException which was ignored in the catch block (in the above example it is printed).

Hope this helps, or will help soon, thanks for reporting it to us!

EDIT: The PRs do not address the performance issue, just the issues with SCIFIO 0.38.

2 Likes

Dear Deborah
Thanks for the response.

I’ve run into a couple issues with the SCIFIO library. You might want to take a look at this post as well

But above all, my most pressing issue is to how to save/load ARGB images with SCIFIO.
This was the discussion:

But breaking down ARGB into separate channels poses several issues (at least in my experience). So, is there any way to save an ARGB image directly as a color image using SCIFIO, like for example how ImageJ1 writers do so?

Thanks

1 Like