Package org.apache.xmlgraphics.image.loader.spi

Examples of org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry$ImageLoaderFactoryComparator


    public PipelineFactory(ImageManager manager) {
        this.manager = manager;
    }
   
    private DefaultEdgeDirectory getEdgeDirectory() {
        ImageImplRegistry registry = manager.getRegistry();
        if (registry.getImageConverterModifications() != converterEdgeDirectoryVersion) {
            Collection converters = registry.getImageConverters();
           
            //Rebuild edge directory
            DefaultEdgeDirectory dir = new DefaultEdgeDirectory();
            Iterator iter = converters.iterator();
            while (iter.hasNext()) {
                ImageConverter converter = (ImageConverter)iter.next();
                dir.addEdge(new ImageConversionEdge(converter));
            }
           
            converterEdgeDirectoryVersion = registry.getImageConverterModifications();
            this.converterEdgeDirectory = dir; //Replace (thread-safe)
        }
        return this.converterEdgeDirectory;
    }
View Full Code Here


     * @return an {@link ImageProviderPipeline} or null if no suitable pipeline could be assembled
     */
    public ImageProviderPipeline newImageConverterPipeline(
                ImageInfo imageInfo, ImageFlavor targetFlavor) {
        String originalMime = imageInfo.getMimeType();
        ImageImplRegistry registry = manager.getRegistry();
        ImageProviderPipeline pipeline = null;
       
        //Get snapshot to avoid concurrent modification problems (thread-safety)
        DefaultEdgeDirectory dir = getEdgeDirectory();
       
        ImageLoaderFactory[] loaderFactories = registry.getImageLoaderFactories(
                imageInfo, targetFlavor);
        if (loaderFactories != null) {
            //Directly load image and return it
            ImageLoader loader;
            if (loaderFactories.length == 1) {
                 loader = loaderFactories[0].newImageLoader(targetFlavor);
            } else {
                int count = loaderFactories.length;
                ImageLoader[] loaders = new ImageLoader[count];
                for (int i = 0; i < count; i++) {
                    loaders[i] = loaderFactories[i].newImageLoader(targetFlavor);
                }
                loader = new CompositeImageLoader(loaders);
            }
            pipeline = new ImageProviderPipeline(manager.getCache(), loader);
        } else {
            //Need to use ImageConverters
            if (log.isTraceEnabled()) {
                log.trace("No ImageLoaderFactory found that can load this format directly."
                        + " Trying ImageConverters instead...");
            }
           
            ImageRepresentation destination = new ImageRepresentation(targetFlavor);
            //Get Loader for originalMIME
            // --> List of resulting flavors, possibly multiple loaders
            loaderFactories = registry.getImageLoaderFactories(originalMime);
            if (loaderFactories != null) {
                SortedSet candidates = new java.util.TreeSet(new PipelineComparator());
                //Find best pipeline -> best loader
                for (int i = 0, ci = loaderFactories.length; i < ci; i++) {
                    ImageLoaderFactory loaderFactory = loaderFactories[i];
View Full Code Here

        MockImageContext imageContext = MockImageContext.newSafeInstance();
        ImageManager manager = imageContext.getImageManager();
        PipelineFactory pFactory = new PipelineFactory(manager);

        //Adding additional penalty for CCITT loading
        ImageImplRegistry registry = imageContext.getImageManager().getRegistry();
        registry.setAdditionalPenalty(ImageLoaderRawCCITTFax.class.getName(),
                Penalty.toPenalty(10000));

        //Input is some TIFF file with CCITT Group 4 compression
        ImageInfo imageInfo = new ImageInfo("test:tiff", "image/tiff");
        imageInfo.getCustomObjects().put("TIFF_STRIP_COUNT", new Integer(1));
        imageInfo.getCustomObjects().put("TIFF_COMPRESSION", new Integer(TIFFImage.COMP_FAX_G4_2D));

        //We want either a G2D image or a raw CCITT image
        ImageFlavor[] targetFlavors = new ImageFlavor[] {
                ImageFlavor.GRAPHICS2D, ImageFlavor.RAW_CCITTFAX};

        ImageProviderPipeline[] candidates = pFactory.determineCandidatePipelines(
                imageInfo, targetFlavors);
        assertNotNull(candidates);
        assertEquals(2, candidates.length);

        ImageProviderPipeline pipeline = manager.choosePipeline(candidates);

        //penalty for internal TIFF implementation (fallback role) is 1000 + 10 for the conversion
        assertEquals(1010, pipeline.getConversionPenalty());
        assertEquals(ImageFlavor.GRAPHICS2D, pipeline.getTargetFlavor());
        if (pipeline.toString().indexOf("LoaderInternalTIFF") < 0) {
            fail("Chose the wrong pipeline: " + pipeline.toString());
        }
        if (pipeline.toString().indexOf("ImageConverterBitmap2G2D") < 0) {
            fail("Chose the wrong pipeline: " + pipeline.toString());
        }

        //Now set an infinite penalty making the solution ineligible
        registry.setAdditionalPenalty(ImageLoaderRawCCITTFax.class.getName(),
                Penalty.INFINITE_PENALTY);

        candidates = pFactory.determineCandidatePipelines(imageInfo, targetFlavors);
        assertNotNull(candidates);
        assertEquals(1, candidates.length);
View Full Code Here

     * environment.
     * @return a new image context
     */
    public static MockImageContext newSafeInstance() {
        MockImageContext ic = new MockImageContext(false);
        ImageImplRegistry registry = ic.getImageManager().getRegistry();
        registry.registerPreloader(new PreloaderTIFF());
        registry.registerPreloader(new PreloaderJPEG());
        registry.registerPreloader(new PreloaderEPS());
        registry.registerLoaderFactory(new ImageLoaderFactoryInternalTIFF());
        registry.registerLoaderFactory(new ImageLoaderFactoryRawCCITTFax());
        registry.registerConverter(new ImageConverterBitmap2G2D());
        registry.registerConverter(new ImageConverterG2D2Bitmap());
        registry.registerConverter(new ImageConverterBuffered2Rendered());
        return ic;
    }
View Full Code Here

    /**
     * Creates a new mock image context.
     * @param discover true to enable plug-in discovery
     */
    public MockImageContext(boolean discover) {
        this.imageManager = new ImageManager(new ImageImplRegistry(discover), this);
    }
View Full Code Here

    public PipelineFactory(ImageManager manager) {
        this.manager = manager;
    }

    private DefaultEdgeDirectory getEdgeDirectory() {
        ImageImplRegistry registry = manager.getRegistry();
        if (registry.getImageConverterModifications() != converterEdgeDirectoryVersion) {
            Collection converters = registry.getImageConverters();

            //Rebuild edge directory
            DefaultEdgeDirectory dir = new DefaultEdgeDirectory();
            Iterator iter = converters.iterator();
            while (iter.hasNext()) {
                ImageConverter converter = (ImageConverter)iter.next();
                Penalty penalty = Penalty.toPenalty(converter.getConversionPenalty());
                penalty = penalty.add(
                        registry.getAdditionalPenalty(converter.getClass().getName()));
                dir.addEdge(new ImageConversionEdge(converter, penalty));
            }

            converterEdgeDirectoryVersion = registry.getImageConverterModifications();
            this.converterEdgeDirectory = dir; //Replace (thread-safe)
        }
        return this.converterEdgeDirectory;
    }
View Full Code Here

     * @return the candidate pipelines
     */
    public ImageProviderPipeline[] determineCandidatePipelines(
                ImageInfo imageInfo, ImageFlavor targetFlavor) {
        String originalMime = imageInfo.getMimeType();
        ImageImplRegistry registry = manager.getRegistry();
        List candidates = new java.util.ArrayList();

        //Get snapshot to avoid concurrent modification problems (thread-safety)
        DefaultEdgeDirectory dir = getEdgeDirectory();

        ImageLoaderFactory[] loaderFactories = registry.getImageLoaderFactories(
                imageInfo, targetFlavor);
        if (loaderFactories != null) {
            //Directly load image and return it
            ImageLoader loader;
            if (loaderFactories.length == 1) {
                 loader = loaderFactories[0].newImageLoader(targetFlavor);
            } else {
                int count = loaderFactories.length;
                ImageLoader[] loaders = new ImageLoader[count];
                for (int i = 0; i < count; i++) {
                    loaders[i] = loaderFactories[i].newImageLoader(targetFlavor);
                }
                loader = new CompositeImageLoader(loaders);
            }
            ImageProviderPipeline pipeline = new ImageProviderPipeline(manager.getCache(), loader);
            candidates.add(pipeline);
        } else {
            //Need to use ImageConverters
            if (log.isTraceEnabled()) {
                log.trace("No ImageLoaderFactory found that can load this format ("
                        + targetFlavor + ") directly. Trying ImageConverters instead...");
            }

            ImageRepresentation destination = new ImageRepresentation(targetFlavor);
            //Get Loader for originalMIME
            // --> List of resulting flavors, possibly multiple loaders
            loaderFactories = registry.getImageLoaderFactories(originalMime);
            if (loaderFactories != null) {

                //Find best pipeline -> best loader
                for (int i = 0, ci = loaderFactories.length; i < ci; i++) {
                    ImageLoaderFactory loaderFactory = loaderFactories[i];
View Full Code Here

    private void configureImageLoading(Configuration parent, boolean strict) throws FOPException {
        if (parent == null) {
            return;
        }
        ImageImplRegistry registry = factory.getImageManager().getRegistry();
        Configuration[] penalties = parent.getChildren("penalty");
        try {
            for (int i = 0, c = penalties.length; i < c; i++) {
                Configuration penaltyCfg = penalties[i];
                String className = penaltyCfg.getAttribute("class");
                String value = penaltyCfg.getAttribute("value");
                Penalty p = null;
                if (value.toUpperCase().startsWith("INF")) {
                    p = Penalty.INFINITE_PENALTY;
                } else {
                    try {
                        p = Penalty.toPenalty(Integer.parseInt(value));
                    } catch (NumberFormatException nfe) {
                        LogUtil.handleException(log, nfe, strict);
                    }
                }
                if (p != null) {
                    registry.setAdditionalPenalty(className, p);
                }
            }
        } catch (ConfigurationException e) {
            LogUtil.handleException(log, e, strict);
        }
View Full Code Here

    private void configureImageLoading(Configuration parent, boolean strict) throws FOPException {
        if (parent == null) {
            return;
        }
        ImageImplRegistry registry = factory.getImageManager().getRegistry();
        Configuration[] penalties = parent.getChildren("penalty");
        try {
            for (int i = 0, c = penalties.length; i < c; i++) {
                Configuration penaltyCfg = penalties[i];
                String className = penaltyCfg.getAttribute("class");
                String value = penaltyCfg.getAttribute("value");
                Penalty p = null;
                if (value.toUpperCase().startsWith("INF")) {
                    p = Penalty.INFINITE_PENALTY;
                } else {
                    try {
                        p = Penalty.toPenalty(Integer.parseInt(value));
                    } catch (NumberFormatException nfe) {
                        LogUtil.handleException(log, nfe, strict);
                    }
                }
                if (p != null) {
                    registry.setAdditionalPenalty(className, p);
                }
            }
        } catch (ConfigurationException e) {
            LogUtil.handleException(log, e, strict);
        }
View Full Code Here

TOP

Related Classes of org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry$ImageLoaderFactoryComparator

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.