001    /*
002      Copyright (C) 2004 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
017    
018    package org.objectweb.jac.util;
019    
020    import java.awt.Dimension;
021    import java.io.File;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.util.Arrays;
025    import org.apache.log4j.Logger;
026    
027    /**
028     * Contains various methods related to image files
029     */
030    
031    public class Images {
032        static Logger logger  = Logger.getLogger("images");
033    
034        protected static final byte[] 
035                PNG_SIG = new byte[] {-119, 'P', 'N', 'G', 13, 10, 26, 10};
036        protected static final byte[] 
037                GIF87A_SIG = new byte[] {'G', 'I', 'F', '8', '7', 'a' };
038        protected static final byte[] 
039                GIF89A_SIG = new byte[] {'G', 'I', 'F', '8', '9', 'a' };
040        protected static final byte[] 
041                JFIF_SIG = new byte[] {-1, -40, -1, -32, 0, 0, 'J', 'F', 'I', 'F'};
042    
043    
044        /**
045         * Tells the size of an image. Supported formats are GIF and PNG.
046         *
047         * @param img the file of the image
048         * @return The size of the image, null if the image format is unhandled 
049         * 
050         * @see #getImageSize(InputStream)
051         */
052        public static Dimension getImageFileSize(File img) 
053            throws IOException 
054        {
055            return getImageSize(
056                Images.class.getClassLoader().getResourceAsStream(
057                    img.getPath()));
058        }
059    
060        /**
061         * Tells the size of an image. Supported formats are GIF and PNG.
062         *
063         * @param img the data of the image
064         * @return The size of the image, null if the image format is unhandled 
065         * 
066         * @see #getImageFileSize(File)
067         */
068        public static Dimension getImageSize(InputStream img) 
069            throws IOException 
070        {
071    
072            int read;
073            byte [] buf = new byte[24];
074            if ((read=img.read(buf,0,24))>=10) {
075                if (ExtArrays.equals(buf, 0, GIF87A_SIG, 0, 6) ||
076                    ExtArrays.equals(buf, 0, GIF89A_SIG, 0, 6)) {
077                    return new Dimension(
078                        buf[7]*(2^8)+buf[6],
079                        buf[9]*(2^8)+buf[8]);
080                } else if (ExtArrays.equals(buf,0,PNG_SIG,0,8)) {
081                    if (read==24) {
082                        return new Dimension(
083                            buf[16]*2^24+buf[17]*2^16+buf[18]*2^8+buf[19],
084                            buf[20]*2^24+buf[21]*2^16+buf[22]*2^8+buf[23]);
085                    } else {
086                        throw new RuntimeException(
087                            "getImageSize: not enough data");
088                    }
089                } else if (ExtArrays.equals(buf,0,JFIF_SIG,0,4) &&
090                           ExtArrays.equals(buf,6,JFIF_SIG,6,4)) {
091                    logger.debug("getImageSize: JPEG is not supported yet");
092                    return null;
093                }
094                logger.warn("Unrecognized signature"+ExtArrays.asList(buf));
095                return null;
096            } else {
097                throw new RuntimeException("getImageSize: not enough data");
098            }
099        }
100    }