google / neuroglancer

WebGL-based viewer for volumetric data
Apache License 2.0
1.08k stars 295 forks source link

PNG Support in Precomputed? #369

Closed william-silversmith closed 2 years ago

william-silversmith commented 2 years ago

Hi Jeremy,

I as looking into methods for decreasing the data usage of raw microscopy that is coming off the scope. For understandable reasons, microscopic are very hesitant to employ any form of lossy compression for these images (though my colleague isn't worried about uint8 representation).

I was evaluating lossless jpeg, png, and gzip and I found that on a few samples PNG had superior compression. The lossless jpeg compressor is pretty similar to the png compressor which is just gzip + some predictive coding. The PNG codecs are much more popular and well supported.

Here's an example image that I downloaded from Neuroglancer as raw and converted to the lossless and uncompressed ppm format.

4.0M Jan 31 22:02 em.ppm
 2.5M Jan 31 22:03 em.png # using convert, pngcrush didn't add much if anything
3.4M Jan 31 22:02 em.ppm.gz # gzip -9
3.3M Jan 31 22:10 em.ljpeg # Using libjpeg (https://github.com/thorfdbg/libjpeg)

Advantage gz/png: 1.36x

em.ppm is a section of brain tissue collected under TEM at 4x4x40 resolution. I attempted this with a section of FlyWire data (256x256x128, 8x8x40nm) as well and had similar results.

5.3M Jan 31 21:41 flywire.ljpeg (1.51x)
4.4M Jan 31 21:45 flywire.png (1.81x)
5.9M Jan 31 21:36 flywire.ppm.gz (1.36x)
8.0M Jan 31 21:36 flywire.ppm (1.00x)

Advantage gz/png: 1.34x

Would you be open to including png as a decoder in Neuroglancer? A ~25% lossless reduction in storage for raw images seems pretty enticing. I haven't timed it, but I suspect the higher compression may result in a faster decode time too since I doubt inverting the predictive coding pass is at all a bottleneck (it's probably the deflate part).

I would be willing to make a PR (though not sure of the exact time when I can do so).

Thanks Jeremy!

jbms commented 2 years ago

To be clear you are proposing adding support to the precomputed format?

william-silversmith commented 2 years ago

Yes. Apologies for the imprecision.

jbms commented 2 years ago

Yes, I'd be happy to take a pull request for that. There is some question as to the best PNG library to use for Neuroglancer --- possibly a webassembly one is the way to go.

william-silversmith commented 2 years ago

Thanks Jeremy! I'm in agreement on using a WASM library. Anything else will probably result in complaints of slowness.

william-silversmith commented 2 years ago

Resolved in #370 Thanks!