001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.tools.imagecollection.collection.video.selection;
031
032import java.text.ParseException;
033
034import org.openimaj.image.MBFImage;
035import org.openimaj.tools.imagecollection.collection.ImageCollectionEntrySelection;
036import org.openimaj.tools.imagecollection.collection.config.ImageCollectionConfig;
037import org.openimaj.video.xuggle.XuggleVideo;
038
039public abstract class XuggleVideoFrameSelection implements ImageCollectionEntrySelection<MBFImage> {
040        public static class Proxy extends XuggleVideoFrameSelection {
041                private ImageCollectionEntrySelection<MBFImage> proxy;
042
043                public Proxy(ImageCollectionEntrySelection<MBFImage> proxy) {
044                        this.proxy = proxy;
045                }
046                
047                @Override
048                public boolean acceptEntry(MBFImage image){
049                        return proxy.acceptEntry(image);
050                }
051        }
052        public XuggleVideoFrameSelection(ImageCollectionConfig config){
053                
054        }
055        
056        public XuggleVideoFrameSelection() {
057        }
058
059        @Override
060        public boolean acceptEntry(MBFImage image){
061                return true;
062        }
063
064        public void init(XuggleVideo video){
065                
066        }
067        public static class All extends XuggleVideoFrameSelection{
068
069                public All(ImageCollectionConfig config) {
070                        super(config);
071                }
072        }
073        
074        public static class FramesPerSecond extends XuggleVideoFrameSelection{
075
076                private Integer framesPerSecond;
077                private int framesToSkip;
078                private int framesCount;
079
080                public FramesPerSecond(ImageCollectionConfig config) throws ParseException {
081                        super(config);
082                        this.framesPerSecond = config.read("video.framespersecond");
083                        if(this.framesPerSecond  == null){
084                                this.framesPerSecond = 2;
085                        }
086                }
087                
088                @Override
089                public void init(XuggleVideo video){
090                        this.framesToSkip = (int) (video.getFPS() / this.framesPerSecond);
091                        if(this.framesToSkip < 1) this.framesToSkip = 1;
092                        this.framesCount = 0;
093                }
094                
095                @Override
096                public boolean acceptEntry(MBFImage image){
097                        boolean good = (this.framesCount % this.framesToSkip) == 0;
098                        this.framesCount++;
099                        return good;
100                }
101        }
102        
103        private enum Styles{
104                ALL {
105                        @Override
106                        public XuggleVideoFrameSelection style(ImageCollectionConfig config) {
107                                return new XuggleVideoFrameSelection.All(config);
108                        }
109                },FRAMESPERSECOND {
110                        @Override
111                        public XuggleVideoFrameSelection style(ImageCollectionConfig config) throws ParseException {
112                                return new XuggleVideoFrameSelection.FramesPerSecond(config);
113                        }
114                }
115//              ,KEYFRAMES {
116//                      @Override
117//                      public FrameStyle style(ImageCollectionConfig config) {
118//                              return new FrameStyle.KeyFrames(config);
119//                      }
120//              }
121                ;
122                public abstract XuggleVideoFrameSelection style(ImageCollectionConfig config) throws ParseException;
123        }
124        public static XuggleVideoFrameSelection byName(String read,ImageCollectionConfig config) throws ParseException {
125                return Styles.valueOf(read).style(config);
126        }
127
128}