public Image loadImage()

in src/main/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawCCITTFax.java [74:152]


    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);
        }
        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 COMPRESSION_CCITT_1D:
                case COMPRESSION_FAX_GROUP4:
                    break;
                case COMPRESSION_FAX_GROUP3:
                    //Note: the TIFFImage compression constants seem to be a bit misleading!
                    compression = TIFFImage.COMP_FAX_G3_1D; //1D is the default for Group3
                    fld = dir.getField(TIFFImageDecoder.TIFF_T4_OPTIONS);
                    if (fld != null) {
                        long t4Options = fld.getAsLong(0);
                        if ((t4Options & 0x01) != 0) {
                            compression = TIFFImage.COMP_FAX_G3_2D; //"Abusing" for 2D signalling
                        }
                    }
                    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(XmlSourceUtil.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
        XmlSourceUtil.removeStreams(src);
        return rawImage;
    }