Search in sources :

Example 1 with SignatureBox

use of com.sun.media.imageioimpl.plugins.jpeg2000.SignatureBox in project bioformats by openmicroscopy.

the class FileFormatReader method readFileFormat.

/**
 * This method checks whether the given RandomAccessIO is a valid JP2 file
 * and if so finds the first codestream in the file. Currently, the
 * information in the codestream is not used
 *
 * @exception java.io.IOException If an I/O error ocurred.
 *
 * @exception java.io.EOFException If end of file is reached
 */
public void readFileFormat() throws IOException, EOFException {
    int foundCodeStreamBoxes = 0;
    int box;
    int length;
    long longLength = 0;
    int pos;
    short marker;
    boolean jp2HeaderBoxFound = false;
    boolean lastBoxFound = false;
    try {
        // Go through the randomaccessio and find the first
        // contiguous codestream box. Check also that the File Format is
        // correct
        pos = in.getPos();
        // or if not that the first 2 bytes is the SOC marker
        if (in.readInt() != 0x0000000c || in.readInt() != JP2_SIGNATURE_BOX || in.readInt() != 0x0d0a870a) {
            // Not a JP2 file
            in.seek(pos);
            marker = (short) in.readShort();
            if (// Standard syntax marker found
            marker != Markers.SOC)
                throw new Error("File is neither valid JP2 file nor " + "valid JPEG 2000 codestream");
            in.seek(pos);
            if (codeStreamPos == null)
                codeStreamPos = new Vector();
            codeStreamPos.addElement(new Integer(pos));
            return;
        }
        if (metadata != null)
            metadata.addNode(new SignatureBox());
        // Read all remaining boxes
        while (!lastBoxFound) {
            pos = in.getPos();
            length = in.readInt();
            if ((pos + length) == in.length())
                lastBoxFound = true;
            box = in.readInt();
            if (length == 0) {
                lastBoxFound = true;
                length = in.length() - in.getPos();
            } else if (length == 1) {
                longLength = in.readLong();
                throw new IOException("File too long.");
            } else
                longLength = (long) 0;
            pos = in.getPos();
            length -= 8;
            switch(box) {
                case FILE_TYPE_BOX:
                    readFileTypeBox(length + 8, longLength);
                    break;
                case CONTIGUOUS_CODESTREAM_BOX:
                    if (!jp2HeaderBoxFound)
                        throw new Error("Invalid JP2 file: JP2Header box not " + "found before Contiguous codestream " + "box ");
                    readContiguousCodeStreamBox(length + 8, longLength);
                    break;
                case JP2_HEADER_BOX:
                    if (jp2HeaderBoxFound)
                        throw new Error("Invalid JP2 file: Multiple " + "JP2Header boxes found");
                    readJP2HeaderBox(length + 8);
                    jp2HeaderBoxFound = true;
                    length = 0;
                    break;
                case IMAGE_HEADER_BOX:
                    readImageHeaderBox(length);
                    break;
                case INTELLECTUAL_PROPERTY_BOX:
                    readIntPropertyBox(length);
                    break;
                case XML_BOX:
                    readXMLBox(length);
                    break;
                case UUID_INFO_BOX:
                    length = 0;
                    break;
                case UUID_BOX:
                    readUUIDBox(length);
                    break;
                case UUID_LIST_BOX:
                    readUUIDListBox(length);
                    break;
                case URL_BOX:
                    readURLBox(length);
                    break;
                case PALETTE_BOX:
                    readPaletteBox(length + 8);
                    break;
                case BITS_PER_COMPONENT_BOX:
                    readBitsPerComponentBox(length);
                    break;
                case COMPONENT_MAPPING_BOX:
                    readComponentMappingBox(length);
                    break;
                case COLOUR_SPECIFICATION_BOX:
                    readColourSpecificationBox(length);
                    break;
                case CHANNEL_DEFINITION_BOX:
                    readChannelDefinitionBox(length);
                    break;
                case RESOLUTION_BOX:
                    length = 0;
                    break;
                case CAPTURE_RESOLUTION_BOX:
                case DEFAULT_DISPLAY_RESOLUTION_BOX:
                    readResolutionBox(box, length);
                    break;
                default:
                    if (metadata != null) {
                        byte[] data = new byte[length];
                        in.readFully(data, 0, length);
                        metadata.addNode(new Box(length + 8, box, longLength, data));
                    }
            }
            if (!lastBoxFound)
                in.seek(pos + length);
        }
    } catch (EOFException e) {
        throw new Error("EOF reached before finding Contiguous " + "Codestream Box");
    }
    if (codeStreamPos.size() == 0) {
        // Not a valid JP2 file or codestream
        throw new Error("Invalid JP2 file: Contiguous codestream box " + "missing");
    }
    return;
}
Also used : SignatureBox(com.sun.media.imageioimpl.plugins.jpeg2000.SignatureBox) ResolutionBox(com.sun.media.imageioimpl.plugins.jpeg2000.ResolutionBox) ColorSpecificationBox(com.sun.media.imageioimpl.plugins.jpeg2000.ColorSpecificationBox) XMLBox(com.sun.media.imageioimpl.plugins.jpeg2000.XMLBox) UUIDBox(com.sun.media.imageioimpl.plugins.jpeg2000.UUIDBox) SignatureBox(com.sun.media.imageioimpl.plugins.jpeg2000.SignatureBox) HeaderBox(com.sun.media.imageioimpl.plugins.jpeg2000.HeaderBox) ComponentMappingBox(com.sun.media.imageioimpl.plugins.jpeg2000.ComponentMappingBox) BitsPerComponentBox(com.sun.media.imageioimpl.plugins.jpeg2000.BitsPerComponentBox) PaletteBox(com.sun.media.imageioimpl.plugins.jpeg2000.PaletteBox) ChannelDefinitionBox(com.sun.media.imageioimpl.plugins.jpeg2000.ChannelDefinitionBox) Box(com.sun.media.imageioimpl.plugins.jpeg2000.Box) DataEntryURLBox(com.sun.media.imageioimpl.plugins.jpeg2000.DataEntryURLBox) UUIDListBox(com.sun.media.imageioimpl.plugins.jpeg2000.UUIDListBox) FileTypeBox(com.sun.media.imageioimpl.plugins.jpeg2000.FileTypeBox)

Example 2 with SignatureBox

use of com.sun.media.imageioimpl.plugins.jpeg2000.SignatureBox in project bioformats by ome.

the class FileFormatReader method readFileFormat.

/**
 * This method checks whether the given RandomAccessIO is a valid JP2 file
 * and if so finds the first codestream in the file. Currently, the
 * information in the codestream is not used
 *
 * @exception java.io.IOException If an I/O error ocurred.
 *
 * @exception java.io.EOFException If end of file is reached
 */
public void readFileFormat() throws IOException, EOFException {
    int foundCodeStreamBoxes = 0;
    int box;
    int length;
    long longLength = 0;
    int pos;
    short marker;
    boolean jp2HeaderBoxFound = false;
    boolean lastBoxFound = false;
    try {
        // Go through the randomaccessio and find the first
        // contiguous codestream box. Check also that the File Format is
        // correct
        pos = in.getPos();
        // or if not that the first 2 bytes is the SOC marker
        if (in.readInt() != 0x0000000c || in.readInt() != JP2_SIGNATURE_BOX || in.readInt() != 0x0d0a870a) {
            // Not a JP2 file
            in.seek(pos);
            marker = (short) in.readShort();
            if (// Standard syntax marker found
            marker != Markers.SOC)
                throw new Error("File is neither valid JP2 file nor " + "valid JPEG 2000 codestream");
            in.seek(pos);
            if (codeStreamPos == null)
                codeStreamPos = new Vector();
            codeStreamPos.addElement(new Integer(pos));
            return;
        }
        if (metadata != null)
            metadata.addNode(new SignatureBox());
        // Read all remaining boxes
        while (!lastBoxFound) {
            pos = in.getPos();
            length = in.readInt();
            if ((pos + length) == in.length())
                lastBoxFound = true;
            box = in.readInt();
            if (length == 0) {
                lastBoxFound = true;
                length = in.length() - in.getPos();
            } else if (length == 1) {
                longLength = in.readLong();
                throw new IOException("File too long.");
            } else
                longLength = (long) 0;
            pos = in.getPos();
            length -= 8;
            switch(box) {
                case FILE_TYPE_BOX:
                    readFileTypeBox(length + 8, longLength);
                    break;
                case CONTIGUOUS_CODESTREAM_BOX:
                    if (!jp2HeaderBoxFound)
                        throw new Error("Invalid JP2 file: JP2Header box not " + "found before Contiguous codestream " + "box ");
                    readContiguousCodeStreamBox(length + 8, longLength);
                    break;
                case JP2_HEADER_BOX:
                    if (jp2HeaderBoxFound)
                        throw new Error("Invalid JP2 file: Multiple " + "JP2Header boxes found");
                    readJP2HeaderBox(length + 8);
                    jp2HeaderBoxFound = true;
                    length = 0;
                    break;
                case IMAGE_HEADER_BOX:
                    readImageHeaderBox(length);
                    break;
                case INTELLECTUAL_PROPERTY_BOX:
                    readIntPropertyBox(length);
                    break;
                case XML_BOX:
                    readXMLBox(length);
                    break;
                case UUID_INFO_BOX:
                    length = 0;
                    break;
                case UUID_BOX:
                    readUUIDBox(length);
                    break;
                case UUID_LIST_BOX:
                    readUUIDListBox(length);
                    break;
                case URL_BOX:
                    readURLBox(length);
                    break;
                case PALETTE_BOX:
                    readPaletteBox(length + 8);
                    break;
                case BITS_PER_COMPONENT_BOX:
                    readBitsPerComponentBox(length);
                    break;
                case COMPONENT_MAPPING_BOX:
                    readComponentMappingBox(length);
                    break;
                case COLOUR_SPECIFICATION_BOX:
                    readColourSpecificationBox(length);
                    break;
                case CHANNEL_DEFINITION_BOX:
                    readChannelDefinitionBox(length);
                    break;
                case RESOLUTION_BOX:
                    length = 0;
                    break;
                case CAPTURE_RESOLUTION_BOX:
                case DEFAULT_DISPLAY_RESOLUTION_BOX:
                    readResolutionBox(box, length);
                    break;
                default:
                    if (metadata != null) {
                        byte[] data = new byte[length];
                        in.readFully(data, 0, length);
                        metadata.addNode(new Box(length + 8, box, longLength, data));
                    }
            }
            if (!lastBoxFound)
                in.seek(pos + length);
        }
    } catch (EOFException e) {
        throw new Error("EOF reached before finding Contiguous " + "Codestream Box");
    }
    if (codeStreamPos.size() == 0) {
        // Not a valid JP2 file or codestream
        throw new Error("Invalid JP2 file: Contiguous codestream box " + "missing");
    }
    return;
}
Also used : SignatureBox(com.sun.media.imageioimpl.plugins.jpeg2000.SignatureBox) ResolutionBox(com.sun.media.imageioimpl.plugins.jpeg2000.ResolutionBox) ColorSpecificationBox(com.sun.media.imageioimpl.plugins.jpeg2000.ColorSpecificationBox) XMLBox(com.sun.media.imageioimpl.plugins.jpeg2000.XMLBox) UUIDBox(com.sun.media.imageioimpl.plugins.jpeg2000.UUIDBox) SignatureBox(com.sun.media.imageioimpl.plugins.jpeg2000.SignatureBox) HeaderBox(com.sun.media.imageioimpl.plugins.jpeg2000.HeaderBox) ComponentMappingBox(com.sun.media.imageioimpl.plugins.jpeg2000.ComponentMappingBox) BitsPerComponentBox(com.sun.media.imageioimpl.plugins.jpeg2000.BitsPerComponentBox) PaletteBox(com.sun.media.imageioimpl.plugins.jpeg2000.PaletteBox) ChannelDefinitionBox(com.sun.media.imageioimpl.plugins.jpeg2000.ChannelDefinitionBox) Box(com.sun.media.imageioimpl.plugins.jpeg2000.Box) DataEntryURLBox(com.sun.media.imageioimpl.plugins.jpeg2000.DataEntryURLBox) UUIDListBox(com.sun.media.imageioimpl.plugins.jpeg2000.UUIDListBox) FileTypeBox(com.sun.media.imageioimpl.plugins.jpeg2000.FileTypeBox)

Aggregations

BitsPerComponentBox (com.sun.media.imageioimpl.plugins.jpeg2000.BitsPerComponentBox)2 Box (com.sun.media.imageioimpl.plugins.jpeg2000.Box)2 ChannelDefinitionBox (com.sun.media.imageioimpl.plugins.jpeg2000.ChannelDefinitionBox)2 ColorSpecificationBox (com.sun.media.imageioimpl.plugins.jpeg2000.ColorSpecificationBox)2 ComponentMappingBox (com.sun.media.imageioimpl.plugins.jpeg2000.ComponentMappingBox)2 DataEntryURLBox (com.sun.media.imageioimpl.plugins.jpeg2000.DataEntryURLBox)2 FileTypeBox (com.sun.media.imageioimpl.plugins.jpeg2000.FileTypeBox)2 HeaderBox (com.sun.media.imageioimpl.plugins.jpeg2000.HeaderBox)2 PaletteBox (com.sun.media.imageioimpl.plugins.jpeg2000.PaletteBox)2 ResolutionBox (com.sun.media.imageioimpl.plugins.jpeg2000.ResolutionBox)2 SignatureBox (com.sun.media.imageioimpl.plugins.jpeg2000.SignatureBox)2 UUIDBox (com.sun.media.imageioimpl.plugins.jpeg2000.UUIDBox)2 UUIDListBox (com.sun.media.imageioimpl.plugins.jpeg2000.UUIDListBox)2 XMLBox (com.sun.media.imageioimpl.plugins.jpeg2000.XMLBox)2