Search in sources :

Example 1 with WlzException

use of uk.ac.mrc.hgu.Wlz.WlzException in project bioformats by openmicroscopy.

the class WlzServiceImpl method saveBytes.

@Override
public void saveBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    if (state == WLZ_SERVICE_WRITE) {
        WlzIVertex3 og = new WlzIVertex3(x + bBox.xMin, y + bBox.yMin, no + bBox.zMin);
        WlzIVertex2 sz = new WlzIVertex2(w, h);
        try {
            wlzObj = WlzObject.WlzBuildObj3B(wlzObj, og, sz, objGType, buf.length, buf);
        } catch (WlzException e) {
            throw new FormatException("Failed save bytes to Woolz object", e);
        }
    }
}
Also used : WlzException(uk.ac.mrc.hgu.Wlz.WlzException) WlzIVertex3(uk.ac.mrc.hgu.Wlz.WlzIVertex3) FormatException(loci.formats.FormatException) WlzIVertex2(uk.ac.mrc.hgu.Wlz.WlzIVertex2)

Example 2 with WlzException

use of uk.ac.mrc.hgu.Wlz.WlzException in project bioformats by openmicroscopy.

the class WlzServiceImpl method readBytes2DDomObj.

private byte[] readBytes2DDomObj(byte[] buf, int x, int y, int w, int h) throws WlzException {
    WlzIVertex2 og = new WlzIVertex2(x + bBox.xMin, y + bBox.yMin);
    WlzIVertex2 sz = new WlzIVertex2(w, h);
    WlzIVertex2[] dstSz = new WlzIVertex2[1];
    dstSz[0] = null;
    switch(objGType) {
        case WlzGreyType.WLZ_GREY_UBYTE:
            {
                byte[][][] dstDat = new byte[1][][];
                WlzObject.WlzToUArray2D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    for (int idX = 0; idX < w; ++idX) {
                        buf[idYW + idX] = dstDat[0][idY][idX];
                    }
                }
            }
            break;
        case WlzGreyType.WLZ_GREY_SHORT:
            {
                short m = 0xff;
                short[][][] dstDat = new short[1][][];
                WlzObject.WlzToSArray2D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    for (int idX = 0; idX < w; ++idX) {
                        short p = dstDat[0][idY][idX];
                        buf[2 * (idYW + idX)] = (byte) ((p >>> 8) & m);
                        buf[2 * (idYW + idX) + 1] = (byte) (p & m);
                    }
                }
            }
            break;
        case WlzGreyType.WLZ_GREY_INT:
            {
                int m = 0xff;
                int[][][] dstDat = new int[1][][];
                WlzObject.WlzToIArray2D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    for (int idX = 0; idX < w; ++idX) {
                        int p = dstDat[0][idY][idX];
                        buf[idYW + (4 * idX)] = (byte) ((p >> 24) & m);
                        buf[idYW + (4 * idX) + 1] = (byte) ((p >> 16) & m);
                        buf[idYW + (4 * idX) + 2] = (byte) ((p >> 8) & m);
                        buf[idYW + (4 * idX) + 3] = (byte) (p & m);
                    }
                }
            }
            break;
        case WlzGreyType.WLZ_GREY_RGBA:
            {
                int m = 0xff;
                int cOff = h * w;
                int[][][] dstDat = new int[1][][];
                WlzObject.WlzToRArray2D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    for (int idX = 0; idX < w; ++idX) {
                        int idYWX = idYW + idX;
                        int p = dstDat[0][idY][idX];
                        buf[idYWX] = (byte) ((p >> 0) & m);
                        buf[idYWX + cOff] = (byte) ((p >> 8) & m);
                        buf[idYWX + (2 * cOff)] = (byte) ((p >> 16) & m);
                        buf[idYWX + (3 * cOff)] = (byte) ((p >> 24) & m);
                    }
                }
            }
            break;
        case WlzGreyType.WLZ_GREY_ERROR:
            /* Indicates no values. */
            {
                int w8 = (w + 7) / 8;
                byte[][][] dstDat = new byte[1][][];
                WlzObject.WlzToBArray2D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    int idYW8 = idY * w8;
                    for (int idX = 0; idX < w; ++idX) {
                        byte p = dstDat[0][idY][idX / 8];
                        byte b = (byte) (p & (0x01 << (idX % 8)));
                        if (b == 0) {
                            buf[idYW + idX] = (byte) 0x00;
                        } else {
                            buf[idYW + idX] = (byte) 0xff;
                        }
                    }
                }
            }
            break;
        default:
            throw new WlzException("Unsupported pixel type");
    }
    return (buf);
}
Also used : WlzException(uk.ac.mrc.hgu.Wlz.WlzException) WlzIVertex2(uk.ac.mrc.hgu.Wlz.WlzIVertex2)

Example 3 with WlzException

use of uk.ac.mrc.hgu.Wlz.WlzException in project bioformats by openmicroscopy.

the class WlzServiceImpl method openRead.

/*
  * Helper methods.
  */
private void openRead(String file) throws FormatException, IOException {
    state = WLZ_SERVICE_READ;
    try {
        wlzFP = new WlzFileInputStream(file);
        wlzObj = WlzObject.WlzReadObj(wlzFP);
        bBox = WlzObject.WlzBoundingBox3I(wlzObj);
        objType = WlzObject.WlzGetObjectType(wlzObj);
        if (objType == WlzObjectType.WLZ_3D_DOMAINOBJ) {
            voxSz = WlzObject.WlzGetVoxelSize(wlzObj);
        } else {
            voxSz = new WlzDVertex3(1.0, 1.0, 1.0);
        }
    } catch (WlzException e) {
        throw new IOException("Failed to read Woolz object (" + e + ")", e);
    }
    try {
        if (objType == WlzObjectType.WLZ_COMPOUND_ARR_1 || objType == WlzObjectType.WLZ_COMPOUND_ARR_2) {
            int count = objType == WlzObjectType.WLZ_COMPOUND_ARR_1 ? 1 : 2;
            WlzObject[][] dest = new WlzObject[1][count];
            WlzObject.WlzExplode(new int[] { count }, dest, wlzObj);
            wlzObj = dest[0][0];
            bBox = WlzObject.WlzBoundingBox3I(wlzObj);
            objType = WlzObject.WlzGetObjectType(wlzObj);
            if (objType == WlzObjectType.WLZ_3D_DOMAINOBJ) {
                voxSz = WlzObject.WlzGetVoxelSize(wlzObj);
            }
        }
        if (WlzObject.WlzObjectValuesIsNull(wlzObj) != 0) {
            /* Here we use WLZ_GREY_ERROR to indicate that the object has no
         * values not an error. */
            objGType = WlzGreyType.WLZ_GREY_ERROR;
        } else {
            // throw an exception here instead of segfaulting during readBytes*
            if (WlzObject.WlzGetObjectValuesType(wlzObj) > WlzObjectType.WLZ_GREY_TAB_TILED) {
                throw new FormatException("Value table data not supported");
            }
            objGType = WlzObject.WlzGreyTypeFromObj(wlzObj);
        }
    } catch (WlzException e) {
        throw new FormatException("Unable to determine Woolz object value type (" + e + ")", e);
    }
    switch(objGType) {
        case WlzGreyType.WLZ_GREY_UBYTE:
            break;
        case WlzGreyType.WLZ_GREY_SHORT:
            break;
        case WlzGreyType.WLZ_GREY_INT:
            break;
        case WlzGreyType.WLZ_GREY_FLOAT:
            break;
        case WlzGreyType.WLZ_GREY_DOUBLE:
            break;
        case WlzGreyType.WLZ_GREY_RGBA:
            break;
        case WlzGreyType.WLZ_GREY_ERROR:
            break;
        default:
            throw new FormatException("Inappropriate Woolz object value type (type = " + objGType + ")");
    }
}
Also used : WlzException(uk.ac.mrc.hgu.Wlz.WlzException) WlzFileInputStream(uk.ac.mrc.hgu.Wlz.WlzFileInputStream) WlzObject(uk.ac.mrc.hgu.Wlz.WlzObject) IOException(java.io.IOException) FormatException(loci.formats.FormatException) WlzDVertex3(uk.ac.mrc.hgu.Wlz.WlzDVertex3)

Example 4 with WlzException

use of uk.ac.mrc.hgu.Wlz.WlzException in project bioformats by openmicroscopy.

the class WlzServiceImpl method readBytes3DDomObj.

private byte[] readBytes3DDomObj(byte[] buf, int x, int y, int z, int w, int h) throws WlzException {
    WlzIVertex3 og = new WlzIVertex3(x + bBox.xMin, y + bBox.yMin, z + bBox.zMin);
    WlzIVertex3 sz = new WlzIVertex3(w, h, 1);
    WlzIVertex3[] dstSz = new WlzIVertex3[1];
    dstSz[0] = null;
    switch(objGType) {
        case WlzGreyType.WLZ_GREY_UBYTE:
            {
                byte[][][][] dstDat = new byte[1][][][];
                WlzObject.WlzToUArray3D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    for (int idX = 0; idX < w; ++idX) {
                        buf[idYW + idX] = dstDat[0][0][idY][idX];
                    }
                }
            }
            break;
        case WlzGreyType.WLZ_GREY_SHORT:
            {
                short m = 0xff;
                short[][][][] dstDat = new short[1][][][];
                WlzObject.WlzToSArray3D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    for (int idX = 0; idX < w; ++idX) {
                        short p = dstDat[0][0][idY][idX];
                        buf[2 * (idYW + idX)] = (byte) ((p >>> 8) & m);
                        buf[2 * (idYW + idX) + 1] = (byte) (p & m);
                    }
                }
            }
            break;
        case WlzGreyType.WLZ_GREY_INT:
            {
                int m = 0xff;
                int[][][][] dstDat = new int[1][][][];
                WlzObject.WlzToIArray3D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    for (int idX = 0; idX < w; ++idX) {
                        int p = dstDat[0][0][idY][idX];
                        buf[idYW + (4 * idX)] = (byte) ((p >> 24) & m);
                        buf[idYW + (4 * idX) + 1] = (byte) ((p >> 16) & m);
                        buf[idYW + (4 * idX) + 2] = (byte) ((p >> 8) & m);
                        buf[idYW + (4 * idX) + 3] = (byte) (p & m);
                    }
                }
            }
            break;
        case WlzGreyType.WLZ_GREY_RGBA:
            {
                int m = 0xff;
                int cOff = h * w;
                int[][][][] dstDat = new int[1][][][];
                WlzObject.WlzToRArray3D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    for (int idX = 0; idX < w; ++idX) {
                        int idYWX = idYW + idX;
                        int p = dstDat[0][0][idY][idX];
                        buf[idYWX] = (byte) ((p >> 0) & m);
                        buf[idYWX + cOff] = (byte) ((p >> 8) & m);
                        buf[idYWX + (2 * cOff)] = (byte) ((p >> 16) & m);
                        buf[idYWX + (3 * cOff)] = (byte) ((p >> 24) & m);
                    }
                }
            }
            break;
        case WlzGreyType.WLZ_GREY_ERROR:
            /* Indicates no values. */
            {
                int w8 = (w + 7) / 8;
                byte[][][][] dstDat = new byte[1][][][];
                WlzObject.WlzToBArray3D(dstSz, dstDat, wlzObj, og, sz, 0);
                for (int idY = 0; idY < h; ++idY) {
                    int idYW = idY * w;
                    int idYW8 = idY * w8;
                    for (int idX = 0; idX < w; ++idX) {
                        byte p = dstDat[0][0][idY][idX / 8];
                        byte b = (byte) (p & (0x01 << (idX % 8)));
                        if (b == 0) {
                            buf[idYW + idX] = (byte) 0x00;
                        } else {
                            buf[idYW + idX] = (byte) 0xff;
                        }
                    }
                }
            }
            break;
        default:
            throw new WlzException("Unsupported pixel type");
    }
    return (buf);
}
Also used : WlzException(uk.ac.mrc.hgu.Wlz.WlzException) WlzIVertex3(uk.ac.mrc.hgu.Wlz.WlzIVertex3)

Example 5 with WlzException

use of uk.ac.mrc.hgu.Wlz.WlzException in project bioformats by openmicroscopy.

the class WlzServiceImpl method close.

@Override
public void close() throws IOException {
    if (wlzObj != null) {
        if (state == WLZ_SERVICE_WRITE) {
            try {
                if (objType == WlzObjectType.WLZ_3D_DOMAINOBJ) {
                    WlzObject.WlzSetVoxelSize(wlzObj, voxSz.vtX, voxSz.vtY, voxSz.vtZ);
                }
                WlzObject.WlzWriteObj(wlzFP, wlzObj);
            } catch (WlzException e) {
                throw new IOException("Failed to write to Woolz object (" + e + ")");
            }
        }
        // Object is freed by garbage collection.
        wlzObj = null;
    }
    if (wlzFP != null) {
        wlzFP.close();
        wlzFP = null;
    }
}
Also used : WlzException(uk.ac.mrc.hgu.Wlz.WlzException) IOException(java.io.IOException)

Aggregations

WlzException (uk.ac.mrc.hgu.Wlz.WlzException)6 FormatException (loci.formats.FormatException)3 IOException (java.io.IOException)2 WlzDVertex3 (uk.ac.mrc.hgu.Wlz.WlzDVertex3)2 WlzIVertex2 (uk.ac.mrc.hgu.Wlz.WlzIVertex2)2 WlzIVertex3 (uk.ac.mrc.hgu.Wlz.WlzIVertex3)2 WlzFileInputStream (uk.ac.mrc.hgu.Wlz.WlzFileInputStream)1 WlzIBox3 (uk.ac.mrc.hgu.Wlz.WlzIBox3)1 WlzObject (uk.ac.mrc.hgu.Wlz.WlzObject)1