imagej / ImageJ

Public domain software for processing and analyzing scientific images
http://imagej.org
Other
578 stars 227 forks source link

10 bit tiff support #185

Closed evanspa closed 1 year ago

evanspa commented 2 years ago

Hello - I was asked by Accelerate Diagnostics Inc. to add support to ImageJ for 10-bit TIFF images produced by their equipment. This PR contains this functionality. The meat of the code is a new "read10bitImage()" function inside of ImageReader.java, along with a new file info constant, GRAY10_UNSIGNED. I tried to keep the code consistent with how the other bit-length reader functions are implemented. This code was tested against several 10-bit TIFF files provided by Accelerate and worked well. To accomplish this, the read10bitImage() function reads in 5 bytes of data at a time, and produces 4 pixels of information. The result is that each new pixel contains no data loss, so the fidelity of the image is not reduced with this conversion. Each new 16-bit pixel contains the original 10-bits of information; the remaining unused 6-bits are set to 0 (left-padded).

Accelerate Diagnostics Inc has been a happy user of ImageJ for years, and are excited that this small contribution may make its way into the core product, to benefit other users.

Thank you for your consideration of this PR. -Paul

ctrueden commented 1 year ago

@evanspa That's awesome! Thanks for contributing.

@rasband What do you think? If you want to review the patch without the whitespace changes cluttering the view, you can append ?w=1 to the URL—in this case: https://github.com/imagej/ImageJ/pull/185/files?w=1

rasband commented 1 year ago

Hi @evanspa, It would be helpful if you could provide an example 10 bit tiff file to use for testing.

ctrueden commented 1 year ago

The LibTIFF library has a nice collection of samples, including bit depths from 2 through 32, downloadable from http://www.simplesystems.org/libtiff/images.html. In particular, these files from the archive might be helpful for testing:

rasband commented 1 year ago

@evanspa, @ctrueden 10 bit tiff support is in the ImageJ 1.54c26 daily build but there seems to be a problem with the read10bitImage() method. It enters an infinite loop when you try to open the libtiffpic/depth/flower-minisblack-10.tif sample image.

The commit is at https://github.com/imagej/ImageJ/commit/8355378370b5228c90c2a0b2bd4769bfcdbe5560.

rasband commented 1 year ago

I made 2 small changes to the read10bitImage() method (marked with "//wsr") and it now opens the libtiffpic/depth/flower-minisblack-10.tif sample image. These changes are in the ImageJ 1.54c27 daily build.

-wayne

    short[] read10bitImage(InputStream in) throws IOException {
        int bytesPerLine = (int)(width*1.25); // there are 1.25 bytes of data for each pixel (5 bytes per 4 pixels)
        if ((width&1)==1) bytesPerLine++; // add 1 if odd //wsr
        byte[] buffer = new byte[bytesPerLine*height];
        short[] pixels = new short[nPixels];
        DataInputStream dis = new DataInputStream(in);
        dis.readFully(buffer);
        for (int y=0; y<height; y++) {
            int index1 = y*bytesPerLine;
            int index2 = y*width;
            int count = 0;
            while (count<width) {
                if (index1 + 4 < buffer.length) {
                    final short B0 = (short) (buffer[index1] & 0xFF);
                    final short B1 = (short) (buffer[index1 + 1] & 0xFF);
                    final short B2 = (short) (buffer[index1 + 2] & 0xFF);
                    final short B3 = (short) (buffer[index1 + 3] & 0xFF);
                    final short B4 = (short) (buffer[index1 + 4] & 0xFF);
                    short b0, b1, b2, b3;

                    // Set pixel 1
                    b0 = (short) (B0 << 2);
                    b1 = (short) (B1 >> 6);
                    pixels[index2 + count] = (short) (b0 | b1);
                    count++;

                    // set pixel 2
                    b1 = (short) ((B1 & (0xFF >> 2)) << 4);
                    b2 = (short) (B2 >> 4);
                    pixels[index2 + count] = (short) (b1 | b2);
                    count++;

                    // set pixel 3
                    b2 = (short) ((B2 & (0xFF >> 4)) << 6);
                    b3 = (short) (B3 >> 2);
                    pixels[index2 + count] = (short) (b2 | b3);
                    count++;

                    // set pixel 4
                    b3 = (short) ((B3 & (0xFF >> 6)) << 8);
                    pixels[index2 + count] = (short) (b3 | B4);
                    count++;
                    index1 += 5;
                } else
                    break; //wsr
            }
        }
        return pixels;
    }
evanspa commented 1 year ago

Great, thank you both!

-Paul

On Sun, Feb 19, 2023 at 10:54 PM rasband @.***> wrote:

Closed #185 https://github.com/imagej/ImageJ/pull/185.

— Reply to this email directly, view it on GitHub https://github.com/imagej/ImageJ/pull/185#event-8557203780, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHRE4Y5UR72C25WBG6A6ULWYLTITANCNFSM6AAAAAARZSGPPA . You are receiving this because you were mentioned.Message ID: @.***>