Login  Register

Re: read10bitImage() in ImageReader ?

Posted by Hidenao IWAI on Feb 19, 2019; 8:06am
URL: http://imagej.273.s1.nabble.com/read10bitImage-in-ImageReader-tp5021804p5021816.html

I coded read10bitImage() for ij.io.ImageReader.java as follows,

        short[] read10bitImage(InputStream in) throws IOException {
    int bytesPerLine = (int)(width*1.25);
    if ((width&3)!=0) bytesPerLine += (4-width&3); // add 4-widht%4
        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) {
                pixels[index2+count] = (short)(((buffer[index1]&0xff)*4)   + ((buffer[index1+1]>>6)&0x03));
                count++;
                if (count==width) break;

                pixels[index2+count] = (short)(((buffer[index1+1]&0x3f)*16) + ((buffer[index1+2]>>4)&0x0f));
                count++;
                if (count==width) break;

                pixels[index2+count] = (short)(((buffer[index1+2]&0x0f)*64)  + ((buffer[index1+3]>>2)&0x3f));
                count++;
                if (count==width) break;

                pixels[index2+count] = (short)(((buffer[index1+3]&0x03)*256) + (buffer[index1+4]&0xff));
                count++; index1+=5;
            }
        }
        return pixels;
    }

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html