Example usage for javax.imageio.stream ImageInputStream seek

List of usage examples for javax.imageio.stream ImageInputStream seek

Introduction

In this page you can find the example usage for javax.imageio.stream ImageInputStream seek.

Prototype

void seek(long pos) throws IOException;

Source Link

Document

Sets the current stream position to the desired location.

Usage

From source file:org.apache.xmlgraphics.image.loader.impl.ImageLoaderRawCCITTFax.java

/** {@inheritDoc} */
public Image loadImage(ImageInfo info, Map hints, ImageSessionContext session)
        throws ImageException, IOException {
    if (!MimeConstants.MIME_TIFF.equals(info.getMimeType())) {
        throw new IllegalArgumentException(
                "ImageInfo must be from a image with MIME type: " + MimeConstants.MIME_TIFF);
    }//  www  . jav  a  2 s.  com
    int fillOrder = 1;
    int compression = TIFFImage.COMP_NONE;
    long stripOffset;
    long stripLength;
    TIFFDirectory dir;

    Source src = session.needSource(info.getOriginalURI());
    ImageInputStream in = ImageUtil.needImageInputStream(src);
    in.mark();
    try {
        SeekableStream seekable = new SeekableStreamAdapter(in);
        dir = new TIFFDirectory(seekable, 0);
        TIFFField fld;

        fld = dir.getField(TIFFImageDecoder.TIFF_COMPRESSION);
        if (fld != null) {
            compression = fld.getAsInt(0);
            switch (compression) {
            case TIFFImage.COMP_FAX_G3_1D:
            case TIFFImage.COMP_FAX_G3_2D:
            case TIFFImage.COMP_FAX_G4_2D:
                break;
            default:
                log.debug("Unsupported compression " + compression);
                throw new ImageException("ImageLoader doesn't support TIFF compression: " + compression);
            }
        }
        //Read information used for raw embedding
        fld = dir.getField(TIFFImageDecoder.TIFF_FILL_ORDER);
        if (fld != null) {
            fillOrder = fld.getAsInt(0);
        }

        int stripCount;
        fld = dir.getField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP);
        if (fld == null) {
            stripCount = 1;
        } else {
            stripCount = (int) (info.getSize().getHeightPx() / fld.getAsLong(0));
        }
        if (stripCount > 1) {
            log.debug("More than one strip found in TIFF image.");
            throw new ImageException("ImageLoader doesn't support multiple strips");
        }
        stripOffset = dir.getField(TIFFImageDecoder.TIFF_STRIP_OFFSETS).getAsLong(0);
        stripLength = dir.getField(TIFFImageDecoder.TIFF_STRIP_BYTE_COUNTS).getAsLong(0);
    } finally {
        in.reset();
    }

    in.seek(stripOffset);
    InputStream subin = new SubInputStream(ImageUtil.needInputStream(src), stripLength, true);
    if (fillOrder == 2) {
        //Decorate to flip bit order
        subin = new FillOrderChangeInputStream(subin);
    }
    ImageRawCCITTFax rawImage = new ImageRawCCITTFax(info, subin, compression);
    //Strip stream from source as we pass it on internally
    ImageUtil.removeStreams(src);
    return rawImage;
}