Package org.apache.xmlgraphics.image.loader

Examples of org.apache.xmlgraphics.image.loader.ImageInfo


    @Test
    public void testLoadImageImageInfoMapImageSessionContext() throws ImageException, IOException {
        ImageContext context = MockImageContext.newSafeInstance();
        ImageSessionContext session = new MockImageSessionContext(context);
        ImageInfo info = new ImageInfo("basn2c08.png", MimeConstants.MIME_PNG);
        Image im = ilpng.loadImage(info, null, session);
        assertTrue(im instanceof ImageRendered);
    }
View Full Code Here


    public void testLoadImage() throws Exception {
        ImageContext context = MockImageContext.newSafeInstance();
        ImageSessionContext session = new MockImageSessionContext(context);
        // This image file is NOT a valid tif! It is the directory table ONLY.
        ImageInfo info = new ImageInfo("dirOnly.tif", MimeConstants.MIME_TIFF);
        ImageSize size = new ImageSize();
        // Size data can be retrieve by parsing the directory table in the TIFF
        size.setSizeInPixels(1728, 2266);
        size.setResolution(203, 192);
        size.calcSizeFromPixels();
        info.setSize(size);

        sut = new ImageLoaderRawCCITTFax();
        ImageRawCCITTFax rawImage = (ImageRawCCITTFax) sut.loadImage(info, null, session);
        assertEquals(2, rawImage.getCompression());
    }
View Full Code Here

        String uri = imageURL.toURI().toASCIIString();

        ImageLoaderImageIO loader = new ImageLoaderImageIO(ImageFlavor.RENDERED_IMAGE);
        ImageContext context = MockImageContext.newSafeInstance();
        ImageSessionContext session = new MockImageSessionContext(context);
        ImageInfo info = new ImageInfo(uri, MimeConstants.MIME_PNG);
        Image im = loader.loadImage(info, null, session);
        assertTrue(im instanceof ImageRendered);
    }
View Full Code Here

                supported = true;
            }
        }

        if (supported) {
            ImageInfo info = createImageInfo(uri, in, context);
            return info;
        } else {
            return null;
        }
    }
View Full Code Here

        }
    }

    private ImageInfo createImageInfo(String uri, ImageInputStream in, ImageContext context)
                throws IOException, ImageException {
        ImageInfo info = null;
        in.mark();
        try {
            int pageIndex = ImageUtil.needPageIndexFromURI(uri);

            SeekableStream seekable = new SeekableStreamAdapter(in);
            TIFFDirectory dir;
            try {
                dir = new TIFFDirectory(seekable, pageIndex);
            } catch (IllegalArgumentException iae) {
                String errorMessage = MessageFormat.format(
                        "Subimage {0} does not exist.", new Object[] {new Integer(pageIndex)});
                throw new SubImageNotFoundException(errorMessage);
            }
            int width = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_IMAGE_WIDTH);
            int height = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_IMAGE_LENGTH);
            ImageSize size = new ImageSize();
            size.setSizeInPixels(width, height);
            int unit = 2; //inch is default
            if (dir.isTagPresent(TIFFImageDecoder.TIFF_RESOLUTION_UNIT)) {
                unit = (int)dir.getFieldAsLong(TIFFImageDecoder.TIFF_RESOLUTION_UNIT);
            }
            if (unit == 2 || unit == 3) {
                float xRes;
                float yRes;
                TIFFField fldx = dir.getField(TIFFImageDecoder.TIFF_X_RESOLUTION);
                TIFFField fldy = dir.getField(TIFFImageDecoder.TIFF_Y_RESOLUTION);
                if (fldx == null || fldy == null) {
                    unit = 2;
                    xRes = context.getSourceResolution();
                    yRes = xRes;
                } else {
                    xRes = fldx.getAsFloat(0);
                    yRes = fldy.getAsFloat(0);
                }
                if (xRes == 0 || yRes == 0) {
                    //Some TIFFs may report 0 here which would lead to problems
                    size.setResolution(context.getSourceResolution());
                } else if (unit == 2) {
                    size.setResolution(xRes, yRes); //Inch
                } else {
                    size.setResolution(
                            UnitConv.in2mm(xRes) / 10,
                            UnitConv.in2mm(yRes) / 10); //Centimeters
                }
            } else {
                size.setResolution(context.getSourceResolution());
            }
            size.calcSizeFromPixels();
            if (log.isTraceEnabled()) {
                log.trace("TIFF image detected: " + size);
            }

            info = new ImageInfo(uri, MimeConstants.MIME_TIFF);
            info.setSize(size);

            TIFFField fld;

            fld = dir.getField(TIFFImageDecoder.TIFF_COMPRESSION);
            if (fld != null) {
                int compression = fld.getAsInt(0);
                if (log.isTraceEnabled()) {
                    log.trace("TIFF compression: " + compression);
                }
                info.getCustomObjects().put("TIFF_COMPRESSION", new Integer(compression));
            }

            fld = dir.getField(TIFFImageDecoder.TIFF_TILE_WIDTH);
            if (fld != null) {
                if (log.isTraceEnabled()) {
                    log.trace("TIFF is tiled");
                }
                info.getCustomObjects().put("TIFF_TILED", Boolean.TRUE);
            }

            int stripCount;
            fld = dir.getField(TIFFImageDecoder.TIFF_ROWS_PER_STRIP);
            if (fld == null) {
                stripCount = 1;
            } else {
                stripCount = (int)Math.ceil(size.getHeightPx() / (double)fld.getAsLong(0));
            }
            if (log.isTraceEnabled()) {
                log.trace("TIFF has " + stripCount + " strips.");
            }
            info.getCustomObjects().put("TIFF_STRIP_COUNT", new Integer(stripCount));

            try {
                //Check if there is a next page
                new TIFFDirectory(seekable, pageIndex + 1);
                info.getCustomObjects().put(ImageInfo.HAS_MORE_IMAGES, Boolean.TRUE);
                if (log.isTraceEnabled()) {
                    log.trace("TIFF is multi-page.");
                }
            } catch (IllegalArgumentException iae) {
                info.getCustomObjects().put(ImageInfo.HAS_MORE_IMAGES, Boolean.FALSE);
            }
        } finally {
            in.reset();
        }
View Full Code Here

    @Test
    public void testCorruptPNG() {
        ImageContext context = MockImageContext.newSafeInstance();
        ImageSessionContext session = new MockImageSessionContext(context);
        ImageInfo info = new ImageInfo("corrupt-image.png", MimeConstants.MIME_PNG);
        ImageLoaderRawPNG ilrpng = new ImageLoaderRawPNG();
        try {
            ImageRawPNG irpng = (ImageRawPNG) ilrpng.loadImage(info, null, session);
            fail("An exception should have been thrown above");
        } catch (Exception e) {
View Full Code Here

    private void testColorTypePNG(String imageName, int colorType, boolean isTransparent)
            throws ImageException, IOException {
        ImageContext context = MockImageContext.newSafeInstance();
        ImageSessionContext session = new MockImageSessionContext(context);
        ImageInfo info = new ImageInfo(imageName, MimeConstants.MIME_PNG);
        ImageLoaderRawPNG ilrpng = new ImageLoaderRawPNG();
        ImageRawPNG irpng = (ImageRawPNG) ilrpng.loadImage(info, null, session);
        ColorModel cm = irpng.getColorModel();
        if (colorType == PNG_COLOR_PALETTE) {
            assertTrue(cm instanceof IndexColorModel);
View Full Code Here

    @Test
    public void testLoadImageBadMime() throws ImageException, IOException {
        ImageContext context = MockImageContext.newSafeInstance();
        ImageSessionContext session = new MockImageSessionContext(context);
        ImageInfo info = new ImageInfo("basn2c08.png", MimeConstants.MIME_JPEG);
        try {
            ImageRawPNG irpng = (ImageRawPNG) ilrpng.loadImage(info, null, session);
            fail("An exception should have been thrown above");
        } catch (IllegalArgumentException e) {
            // do nothing; this was expected
View Full Code Here

    @Test
    public void testLoadImageGoodMime() throws ImageException, IOException {
        ImageContext context = MockImageContext.newSafeInstance();
        ImageSessionContext session = new MockImageSessionContext(context);
        ImageInfo info = new ImageInfo("basn2c08.png", MimeConstants.MIME_PNG);
        Image im = ilrpng.loadImage(info, null, session);
        assertTrue(im instanceof ImageRawPNG);
    }
View Full Code Here

    FileOutputStream fos = new FileOutputStream(tmpImageFile);
    fos.write(bytes);
    fos.close();
        log.debug("created tmp file: " + tmpImageFile.getAbsolutePath());
       
    ImageInfo info = ensureFormatIsSupported(tmpImageFile, bytes, true);
   
    // In the absence of an exception, tmpImageFile now contains an image
    // Word will accept
   
    ContentTypeManager ctm = opcPackage.getContentTypeManager();
   
    // Ensure the relationships part exists
        if (sourcePart.getRelationshipsPart() == null) {
      RelationshipsPart.createRelationshipsPartForPart(sourcePart);
        }

    String proposedRelId = sourcePart.getRelationshipsPart().getNextId();
       
        String ext = info.getMimeType().substring(info.getMimeType().indexOf("/") + 1);
   
//    System.out.println(ext);
   
    BinaryPartAbstractImage imagePart =
                (BinaryPartAbstractImage) ctm.newPartForContentType(
        info.getMimeType(),
                createImageName(opcPackage, sourcePart, proposedRelId, ext), null);
       
        log.debug("created part " + imagePart.getClass().getName()
                + " with name " + imagePart.getPartName().toString());
   
View Full Code Here

TOP

Related Classes of org.apache.xmlgraphics.image.loader.ImageInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.