Search in sources :

Example 1 with IISRandomAccessIO

use of com.github.jaiimageio.jpeg2000.impl.IISRandomAccessIO in project ddf by codice.

the class Jpeg2000ThumbnailConverter method process.

@Override
public QueryResponse process(QueryResponse input) throws PluginExecutionException, StopProcessingException {
    for (Result result : input.getResults()) {
        Metacard metacard = result.getMetacard();
        byte[] thumbnailBytes = metacard.getThumbnail();
        if (thumbnailBytes == null) {
            continue;
        }
        try (ByteArrayInputStream original = new ByteArrayInputStream(thumbnailBytes);
            ByteArrayOutputStream converted = new ByteArrayOutputStream()) {
            IISRandomAccessIO in = new IISRandomAccessIO(ImageIO.createImageInputStream(original));
            if (in.length() == 0) {
                continue;
            }
            // extracted from jj2000.j2k.fileformat.reader.FileFormatReader
            if (in.readInt() != OTHER_JP2_SIGNATURE || in.readInt() != JP2_SIGNATURE_BOX || in.readInt() != OFFICIAL_JP2_SIGNATURE) {
                // Not a JP2 file
                in.seek(0);
                if (in.readShort() != START_OF_CODESTREAM_MARKER) {
                    // Standard syntax marker found
                    continue;
                }
            }
            // convert j2k thumbnail to jpeg thumbnail
            original.reset();
            BufferedImage thumbnail = ImageIO.read(original);
            if (thumbnail == null) {
                continue;
            }
            ImageIO.write(thumbnail, "jpeg", converted);
            metacard.setAttribute(new AttributeImpl(Metacard.THUMBNAIL, converted.toByteArray()));
        } catch (IOException e) {
            throw new PluginExecutionException(e);
        }
    }
    return input;
}
Also used : Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IISRandomAccessIO(com.github.jaiimageio.jpeg2000.impl.IISRandomAccessIO) BufferedImage(java.awt.image.BufferedImage) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) Result(ddf.catalog.data.Result)

Aggregations

IISRandomAccessIO (com.github.jaiimageio.jpeg2000.impl.IISRandomAccessIO)1 Metacard (ddf.catalog.data.Metacard)1 Result (ddf.catalog.data.Result)1 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 PluginExecutionException (ddf.catalog.plugin.PluginExecutionException)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1