Package org.apache.xmlgraphics.image.loader

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


       
        //Additional processing: obtain the image's intrinsic size and baseline information
        url = URISpecification.getURL(src);
        FOUserAgent userAgent = getUserAgent();
        ImageManager manager = userAgent.getFactory().getImageManager();
        ImageInfo info = null;
        try {
            info = manager.getImageInfo(url, userAgent.getImageSessionContext());
        } catch (ImageException e) {
            log.error("Image not available: " + e.getMessage());
        } catch (FileNotFoundException fnfe) {
            log.error(fnfe.getMessage());
        } catch (IOException ioe) {
            log.error("I/O error while loading image: " + ioe.getMessage());
        }
        if (info != null) {
            this.intrinsicWidth = info.getSize().getWidthMpt();
            this.intrinsicHeight = info.getSize().getHeightMpt();
            int baseline = info.getSize().getBaselinePositionFromBottom();
            if (baseline != 0) {
                this.intrinsicAlignmentAdjust
                    = FixedLength.getInstance(-baseline);
            }
        }
View Full Code Here


        }
        if (name != null) {
            afpDataStream.createIncludePageSegment(name, mpts2units(x), mpts2units(y));
        } else {
            ImageManager manager = getUserAgent().getFactory().getImageManager();
            ImageInfo info = null;
            try {
                ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
                info = manager.getImageInfo(uri, sessionContext);
               
                //Only now fully load/prepare the image
                Map hints = ImageUtil.getDefaultHints(sessionContext);
                org.apache.xmlgraphics.image.loader.Image img = manager.getImage(
                        info, FLAVORS, hints, sessionContext);
               
                //...and process the image
                if (img instanceof ImageGraphics2D) {
                    ImageGraphics2D imageG2D = (ImageGraphics2D)img;
                    RendererContext context = createRendererContext(
                            posInt.x, posInt.y,
                            posInt.width, posInt.height, foreignAttributes);
                    getGraphics2DAdapter().paintImage(imageG2D.getGraphics2DImagePainter(),
                            context,
                            origin.x + posInt.x, origin.y + posInt.y,
                            posInt.width, posInt.height);
                } else if (img instanceof ImageRendered) {
                    ImageRendered imgRend = (ImageRendered)img;
                    RenderedImage ri = imgRend.getRenderedImage();
                   
                    drawBufferedImage(ri, getResolution(),
                            posInt.x + currentIPPosition,
                            posInt.y + currentBPPosition,
                            posInt.width,
                            posInt.height);
                } else if (img instanceof ImageRawCCITTFax) {
                    ImageRawCCITTFax ccitt = (ImageRawCCITTFax)img;
                    int afpx = mpts2units(posInt.x + currentIPPosition);
                    int afpy = mpts2units(posInt.y + currentBPPosition);
                    int afpw = mpts2units(posInt.getWidth());
                    int afph = mpts2units(posInt.getHeight());
                    int afpres = getResolution();
                    ImageObject io = afpDataStream.getImageObject(afpx, afpy, afpw, afph,
                            afpres, afpres);
                    io.setImageParameters(
                            (int) (ccitt.getSize().getDpiHorizontal() * 10),
                            (int) (ccitt.getSize().getDpiVertical() * 10),
                            ccitt.getSize().getWidthPx(),
                            ccitt.getSize().getHeightPx());
                    int compression = ccitt.getCompression();
                    switch (compression) {
                    case TIFFImage.COMP_FAX_G3_1D :
                        io.setImageEncoding((byte) 0x80);
                        break;
                    case TIFFImage.COMP_FAX_G3_2D :
                        io.setImageEncoding((byte) 0x81);
                        break;
                    case TIFFImage.COMP_FAX_G4_2D :
                        io.setImageEncoding((byte) 0x82);
                        break;
                    default:
                        throw new IllegalStateException(
                                "Invalid compression scheme: " + compression);
                    }
                    InputStream in = ccitt.createInputStream();
                    try {
                        byte[] buf = IOUtils.toByteArray(in);
                        io.setImageData(buf);
                    } finally {
                        IOUtils.closeQuietly(in);
                    }
                } else if (img instanceof ImageXMLDOM) {
                    ImageXMLDOM imgXML = (ImageXMLDOM)img;
                    renderDocument(imgXML.getDocument(), imgXML.getRootNamespace(),
                            pos, foreignAttributes);
                } else {
                    throw new UnsupportedOperationException("Unsupported image type: " + img);
                }

            } catch (ImageException ie) {
                log.error("Error while processing image: "
                        + (info != null ? info.toString() : uri), ie);
            } catch (FileNotFoundException fe) {
                log.error(fe.getMessage());
            } catch (IOException ioe) {
                log.error("I/O error while processing image: "
                        + (info != null ? info.toString() : uri), ioe);
            }
           
            /*
            ImageFactory fact = userAgent.getFactory().getImageFactory();
            FopImage fopimage = fact.getImage(url, userAgent);
View Full Code Here

        currentStream.write("" + w + " 0 0 -" + h + " 0 " + h + " cm\n"
                + xObject.getName() + " Do\nQ\n");
    }

    private PDFXObject addRenderedImage(String key, RenderedImage img) {
        ImageInfo info = new ImageInfo(null, "image/unknown");
        ImageSize size = new ImageSize(img.getWidth(), img.getHeight(), 72);
        info.setSize(size);
        ImageRendered imgRend = new ImageRendered(info, img, null);
        ImageRenderedAdapter adapter = new ImageRenderedAdapter(imgRend, key);
        PDFXObject xObject = pdfDoc.addImage(resourceContext, adapter);
        if (outputStream != null) {
            try {
View Full Code Here

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

        ImageGraphics2D g2dImage = null;
        try {
            //Preload image
            ImageInfo info = this.imageManager.getImageInfo(uri, sessionContext);

            //Load image and request Graphics2D image
            g2dImage = (ImageGraphics2D)this.imageManager.getImage(
                    info, ImageFlavor.GRAPHICS2D, sessionContext);
View Full Code Here

        ImageSize size = new ImageSize();
        size.setSizeInMillipoints(dim.width, dim.height);
        size.setResolution(imageManager.getImageContext().getSourceResolution());
        size.calcPixelsFromSize();

        ImageInfo info = new ImageInfo(null, null);
        info.setSize(size);
        return new ImageGraphics2D(info, painter);
    }
View Full Code Here

        String invalid2 = "invalid2.jpg";
        String valid1 = "bgimg300dpi.bmp";
        String valid2 = "big-image.png";


        ImageInfo info1, info2;
        info1 = manager.getImageInfo(valid1, sessionContext);
        assertNotNull(info1);
        assertEquals(valid1, info1.getOriginalURI());

        try {
            manager.getImageInfo(invalid1, sessionContext);
            fail("Expected FileNotFoundException for invalid URI");
        } catch (FileNotFoundException e) {
            //expected
        }

        //2 requests:
        assertEquals(0, statistics.getImageInfoCacheHits());
        assertEquals(2, statistics.getImageInfoCacheMisses());
        assertEquals(0, statistics.getInvalidHits());
        statistics.reset();

        //Cache Hit
        info1 = manager.getImageInfo(valid1, sessionContext);
        assertNotNull(info1);
        assertEquals(valid1, info1.getOriginalURI());

        //Cache Miss
        info2 = manager.getImageInfo(valid2, sessionContext);
        assertNotNull(info2);
        assertEquals(valid2, info2.getOriginalURI());
View Full Code Here

     * @throws Exception if an error occurs
     */
    public void testImageCache1() throws Exception {
        String valid1 = "bgimg72dpi.gif";

        ImageInfo info = manager.getImageInfo(valid1, sessionContext);
        assertNotNull(info);

        ImageBuffered img1 = (ImageBuffered)manager.getImage(
                info, ImageFlavor.BUFFERED_IMAGE, sessionContext);
        assertNotNull(img1);
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, 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 (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

        byte[] header = getHeader(in, BMP_SIG_LENGTH);
        boolean supported = ((header[0] == (byte) 0x42)
                && (header[1] == (byte) 0x4d));

        if (supported) {
            ImageInfo info = new ImageInfo(uri, "image/bmp");
            info.setSize(determineSize(in, context));
            return info;
        } else {
            return null;
        }
    }
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.