001    /*
002     * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: ImageIODetector.java,v 584e9b40f728 2009/04/09 07:41:44 maxberger $ */
018    
019    package net.sourceforge.jeuclid.converter;
020    
021    import java.util.Iterator;
022    import java.util.Set;
023    import java.util.TreeSet;
024    
025    import javax.imageio.ImageIO;
026    import javax.imageio.ImageWriter;
027    
028    /**
029     * Detects and registers the Converters from ImageIO.
030     * 
031     * @version $Revision: 584e9b40f728 $
032     */
033    public final class ImageIODetector implements ConverterDetector {
034    
035        /**
036         * Default constructor.
037         */
038        public ImageIODetector() {
039            // Empty on purpose
040        }
041    
042        /**
043         * Detects and registers all converters available through ImageIO.
044         * 
045         * @param registry
046         *            ConverterRegistry to use.
047         */
048        public void detectConversionPlugins(final ConverterRegistry registry) {
049    
050            final String[] mimeTypes = ImageIO.getWriterMIMETypes();
051    
052            final Set<String> noAlphaMimeTypes = new TreeSet<String>();
053            noAlphaMimeTypes.add("image/jpeg");
054            noAlphaMimeTypes.add("image/bmp");
055    
056            for (final String mimeType : mimeTypes) {
057                final Iterator<ImageWriter> iwit = ImageIO
058                        .getImageWritersByMIMEType(mimeType);
059                if (iwit != null) {
060                    while (iwit.hasNext()) {
061                        final ImageWriter iw = iwit.next();
062                        final String[] suffixes = iw.getOriginatingProvider()
063                                .getFileSuffixes();
064                        if (suffixes != null) {
065                            for (final String suffix : suffixes) {
066                                registry.registerMimeTypeAndSuffix(mimeType,
067                                        suffix, false);
068                            }
069                        }
070                        registry.registerConverter(mimeType,
071                                new ImageIOConverter(iw, noAlphaMimeTypes
072                                        .contains(mimeType)), false);
073                    }
074    
075                }
076    
077            }
078    
079        }
080    }