/** {@inheritDoc} */ @Override public void handleImage(RenderingContext context, Image image, Rectangle pos) throws IOException { if (log.isDebugEnabled()) { log.debug("Embedding undecoded image data (" + image.getInfo().getMimeType() + ") as data container..."); } super.handleImage(context, image, pos); }
/** * Registers an image with the cache. * @param img the image */ public void putImage(Image img) { String originalURI = img.getInfo().getOriginalURI(); if (originalURI == null || "".equals(originalURI)) { return; //Don't cache if there's no URI } //An already existing Image is replaced. if (!img.isCacheable()) { throw new IllegalArgumentException( "Image is not cacheable! (Flavor: " + img.getFlavor() + ")"); } ImageKey key = new ImageKey(originalURI, img.getFlavor()); images.put(key, img); }
/** {@inheritDoc} */ public Image convert(Image src, Map hints) throws ImageException, IOException { checkSourceFlavor(src); assert src instanceof ImageRendered; ImageRendered rendered = (ImageRendered)src; ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(MimeConstants.MIME_PNG); if (writer == null) { throw new ImageException("Cannot convert image to PNG. No suitable ImageWriter found."); } ByteArrayOutputStream baout = new ByteArrayOutputStream(); ImageWriterParams params = new ImageWriterParams(); params.setResolution((int)Math.round(src.getSize().getDpiHorizontal())); writer.writeImage(rendered.getRenderedImage(), baout, params); return new ImageRawStream(src.getInfo(), getTargetFlavor(), new java.io.ByteArrayInputStream(baout.toByteArray())); }
/** * Checks if the source flavor of the given image is compatible with this ImageConverter. * @param img the image to check */ protected void checkSourceFlavor(Image img) { if (!getSourceFlavor().equals(img.getFlavor())) { throw new IllegalArgumentException("Incompatible image: " + img); } }
/** {@inheritDoc} */ public boolean isCacheable() { Image img = getInfo().getOriginalImage(); if (img == null) { return true; } return img.isCacheable(); }
/** * Returns the image's color space. * @return the color space */ protected ColorSpace getImageColorSpace() { return image.getColorSpace(); }
/** * Returns the effective ICC profile for the image. * @return an ICC profile or null */ protected ICC_Profile getEffectiveICCProfile() { return image.getICCProfile(); }
/** {@inheritDoc} */ public int getHeight() { return image.getSize().getHeightPx(); }
/** {@inheritDoc} */ public boolean isCompatible(RenderingContext targetContext, Image image) { if (targetContext instanceof PSRenderingContext) { PSRenderingContext psContext = (PSRenderingContext)targetContext; return !psContext.isCreateForms() && (image == null || (image instanceof ImageXMLDOM && image.getFlavor().isCompatible(BatikImageFlavors.SVG_DOM))); } return false; }
if (img.isCacheable()) { lastCacheableImage = img; if (img.isCacheable()) { lastCacheableImage = img;
/** {@inheritDoc} */ public PDFDeviceColorSpace getColorSpace() { // DeviceGray, DeviceRGB, or DeviceCMYK return toPDFColorSpace(image.getColorSpace()); }
skipICCProfile = (this.image.getICCProfile() != null);
/** {@inheritDoc} */ public Image convert(Image src, Map hints) { checkSourceFlavor(src); assert src instanceof ImageGraphics2D; ImageGraphics2D g2dImage = (ImageGraphics2D)src; Object formatIntent = hints.get(ImageProcessingHints.BITMAP_TYPE_INTENT); int bitsPerPixel = 24; if (ImageProcessingHints.BITMAP_TYPE_INTENT_GRAY.equals(formatIntent)) { bitsPerPixel = 8; } else if (ImageProcessingHints.BITMAP_TYPE_INTENT_MONO.equals(formatIntent)) { bitsPerPixel = 1; } Object transparencyIntent = hints.get(ImageProcessingHints.TRANSPARENCY_INTENT); boolean withAlpha = true; if (ImageProcessingHints.TRANSPARENCY_INTENT_IGNORE.equals(transparencyIntent)) { withAlpha = false; } int resolution = GraphicsConstants.DEFAULT_SAMPLE_DPI; Number res = (Number)hints.get(ImageProcessingHints.TARGET_RESOLUTION); if (res != null) { resolution = res.intValue(); } boolean cmyk = Boolean.TRUE.equals(hints.get("CMYK")); BufferedImage bi = paintToBufferedImage(g2dImage, bitsPerPixel, withAlpha, resolution, cmyk); ImageBuffered bufImage = new ImageBuffered(src.getInfo(), bi, null); return bufImage; }
/** {@inheritDoc} */ public Image convert(Image src, Map hints) throws ImageException { checkSourceFlavor(src); ImageGraphics2D g2dImage = (ImageGraphics2D)src; DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document Document document = domImpl.createDocument( SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", null); Element root = document.getDocumentElement(); // Create an SVGGeneratorContext to customize SVG generation SVGGeneratorContext genCtx = SVGGeneratorContext.createDefault(document); genCtx.setComment("Generated by Apache Batik's SVGGraphics2D"); genCtx.setEmbeddedFontsOn(true); // Create an instance of the SVG Generator SVGGraphics2D g2d = new SVGGraphics2D(genCtx, true); ImageSize size = src.getSize(); Dimension dim = size.getDimensionMpt(); g2d.setSVGCanvasSize(dim); //SVGGraphics2D doesn't generate the viewBox by itself root.setAttribute("viewBox", "0 0 " + dim.width + " " + dim.height); g2dImage.getGraphics2DImagePainter().paint(g2d, new Rectangle2D.Float(0, 0, dim.width, dim.height)); //Populate the document root with the generated SVG content. g2d.getRoot(root); //Return the generated SVG image ImageXMLDOM svgImage = new ImageXMLDOM(src.getInfo(), document, BatikImageFlavors.SVG_DOM); g2d.dispose(); return svgImage; }
/** {@inheritDoc} */ public int getWidth() { return image.getSize().getWidthPx(); }
/** * Creates and returns an {@link ImageProviderPipeline} that allows to load an image of the * given MIME type and present it in the requested image flavor. * @param originalImage the original image that serves as the origin point of the conversion * @param targetFlavor the requested image flavor * @return an {@link ImageProviderPipeline} or null if no suitable pipeline could be assembled */ public ImageProviderPipeline newImageConverterPipeline( Image originalImage, ImageFlavor targetFlavor) { //Get snapshot to avoid concurrent modification problems (thread-safety) DefaultEdgeDirectory dir = getEdgeDirectory(); ImageRepresentation destination = new ImageRepresentation(targetFlavor); ImageProviderPipeline pipeline = findPipeline(dir, originalImage.getFlavor(), destination); return pipeline; }
throw e; if (info.getOriginalImage() == null || info.getOriginalImage().isCacheable()) { putImageInfo(info);
/** * In some cases the provided Image is not cacheable, nor is any of the intermediate Image * instances (for example, when loading a raw JPEG file). If the image is loaded over a * potentially slow network, it is preferrable to download the whole file and cache it * in memory or in a temporary file. It's not always possible to convert an Image into a * cacheable variant. * @param img the Image to investigate * @return the converted, cacheable Image or null if the Image cannot be converted * @throws IOException if an I/O error occurs */ protected Image forceCaching(Image img) throws IOException { if (img instanceof ImageRawStream) { ImageRawStream raw = (ImageRawStream)img; if (log.isDebugEnabled()) { log.debug("Image is made cacheable: " + img.getInfo()); } //Read the whole stream and hold it in memory so the image can be cached ByteArrayOutputStream baout = new ByteArrayOutputStream(); InputStream in = raw.createInputStream(); try { IOUtils.copy(in, baout); } finally { IOUtils.closeQuietly(in); } final byte[] data = baout.toByteArray(); raw.setInputStreamFactory(new ImageRawStream.ByteArrayStreamFactory(data)); return raw; } return null; }