001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.img;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.util.Iterator;
024
025import lucee.commons.io.IOUtil;
026import lucee.commons.io.res.Resource;
027import lucee.commons.lang.ExceptionUtil;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.type.KeyImpl;
030import lucee.runtime.type.Struct;
031import lucee.runtime.type.StructImpl;
032
033import com.drew.imaging.jpeg.JpegMetadataReader;
034import com.drew.imaging.jpeg.JpegProcessingException;
035import com.drew.imaging.tiff.TiffMetadataReader;
036import com.drew.metadata.Directory;
037import com.drew.metadata.Metadata;
038import com.drew.metadata.MetadataException;
039import com.drew.metadata.Tag;
040
041public class ImageMetaDrew {
042
043        /**
044         * adds information about a image to the given struct
045         * @param info
046         * @throws PageException 
047         * @throws IOException 
048         * @throws MetadataException 
049         * @throws JpegProcessingException 
050         */
051        public static void addInfo(String format, Resource res, Struct info)  {
052                if("jpg".equalsIgnoreCase(format))jpg(res, info);
053                else if("tiff".equalsIgnoreCase(format))tiff(res, info);
054                
055        }
056
057        private static void jpg(Resource res,Struct info) {
058                InputStream is=null;
059                try {
060                        is = res.getInputStream();
061                        fill(info,JpegMetadataReader.readMetadata(is));
062                }
063                catch(Throwable t) {
064                        ExceptionUtil.rethrowIfNecessary(t);
065                        //throw Caster.toPageException(t);
066                }
067                finally {
068                        IOUtil.closeEL(is);
069                }
070        }
071        
072        private static void tiff(Resource res,Struct info) {
073                InputStream is=null;
074                try {
075                        is = res.getInputStream();
076                        fill(info,TiffMetadataReader.readMetadata(is,true));
077                }
078                catch(Throwable t) {
079                        ExceptionUtil.rethrowIfNecessary(t);
080                        //throw Caster.toPageException(t);
081                }
082                finally {
083                        IOUtil.closeEL(is);
084                }
085        }
086
087        private static void fill(Struct info,Metadata metadata) {
088                Iterator<Directory> directories = metadata.getDirectories().iterator();
089                while (directories.hasNext()) {
090                    Directory directory = directories.next();
091                    Struct sct=new StructImpl();
092                    info.setEL(KeyImpl.init(directory.getName()), sct);
093                    
094                    Iterator<Tag> tags = directory.getTags().iterator();
095                    while (tags.hasNext()) {
096                        Tag tag = tags.next();
097                        sct.setEL(KeyImpl.init(tag.getTagName()), tag.getDescription());
098                    }
099                }
100        }
101
102        public static void test() {
103                // to not delete, this methd is called to test if the jar exists
104                
105        }
106
107        
108
109}