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.video;
031
032import java.io.File;
033import java.io.IOException;
034import java.util.ArrayList;
035import java.util.List;
036
037import org.openimaj.image.Image;
038
039/**
040 * A video backed by a image files on disk. Each image file is a single frame.
041 * 
042 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
043 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
044 * @author David Dupplaw (dpd@ecs.soton.ac.uk)
045 * 
046 * @param <T>
047 *            the image type of the frames
048 */
049public abstract class FileBackedVideo<T extends Image<?, T>> extends Video<T> {
050        private List<File> files;
051        private T heldCurrentFrame;
052        private int heldCurrentFrameIndex = -1;
053        private boolean loop;
054        private double fps = 30;
055
056        /**
057         * Construct the video from the provided files. Assumes a frame rate of 30
058         * FPS
059         * 
060         * @param files
061         *            the image files
062         */
063        public FileBackedVideo(List<File> files) {
064                this.files = files;
065                this.fps = 30;
066                this.loop = false;
067        }
068
069        /**
070         * Construct the video from the provided files.
071         * 
072         * @param files
073         *            the image files
074         * @param fps
075         *            the frame rate
076         */
077        public FileBackedVideo(List<File> files, double fps) {
078                this.files = files;
079                this.fps = fps;
080                this.loop = false;
081        }
082
083        /**
084         * Construct videos from numbered files using the given format string and
085         * indices. The format string should contain a single %d substitution.
086         * 
087         * @param filenameFormat
088         *            format string
089         * @param start
090         *            starting index (inclusive)
091         * @param stop
092         *            stop index (exclusive)
093         */
094        public FileBackedVideo(String filenameFormat, int start, int stop) {
095                this(getFilesList(filenameFormat, start, stop));
096        }
097
098        @Override
099        public synchronized T getNextFrame() {
100                final T frame = getCurrentFrame();
101                incrementFrame();
102                return frame;
103        }
104
105        private void incrementFrame() {
106                if (this.currentFrame + 1 < this.files.size() || loop) {
107                        this.currentFrame++;
108                }
109        }
110
111        @Override
112        public boolean hasNextFrame() {
113                return loop || this.getCurrentFrameIndex() + 1 < this.files.size();
114        }
115
116        @Override
117        public T getCurrentFrame() {
118                try {
119                        if (this.currentFrame != heldCurrentFrameIndex) {
120                                this.heldCurrentFrame = loadImage(files.get(currentFrame % this.files.size()));
121                                this.heldCurrentFrameIndex = currentFrame;
122                        }
123                } catch (final IOException e) {
124                        this.heldCurrentFrameIndex = currentFrame;
125                        this.heldCurrentFrame = null;
126                }
127                return this.heldCurrentFrame;
128        }
129
130        /**
131         * {@inheritDoc}
132         * 
133         * @see org.openimaj.video.Video#getWidth()
134         */
135        @Override
136        public int getWidth()
137        {
138                return getCurrentFrame().getWidth();
139        }
140
141        /**
142         * {@inheritDoc}
143         * 
144         * @see org.openimaj.video.Video#getHeight()
145         */
146        @Override
147        public int getHeight()
148        {
149                return getCurrentFrame().getHeight();
150        }
151
152        protected abstract T loadImage(File f) throws IOException;
153
154        @Override
155        public long countFrames() {
156                return this.files.size();
157        }
158
159        @Override
160        public void reset()
161        {
162                this.currentFrame = 0;
163        }
164
165        /**
166         * {@inheritDoc}
167         * 
168         * @see org.openimaj.video.Video#getTimeStamp()
169         */
170        @Override
171        public long getTimeStamp()
172        {
173                return (long) (getCurrentFrameIndex() / this.fps) * 1000;
174        }
175
176        /**
177         * {@inheritDoc}
178         * 
179         * @see org.openimaj.video.Video#getFPS()
180         */
181        @Override
182        public double getFPS()
183        {
184                return fps;
185        }
186
187        /**
188         * Get a list of numbered files using the given format string and indices.
189         * The format string should contain a single %d substitution.
190         * 
191         * @param filenameFormat
192         *            format string
193         * @param start
194         *            starting index (inclusive)
195         * @param stop
196         *            stop index (exclusive)
197         * 
198         * @return list of files
199         */
200        public static List<File> getFilesList(String filenameFormat, int start, int stop) {
201                final List<File> files = new ArrayList<File>();
202
203                for (int i = start; i < stop; i++) {
204                        files.add(new File(String.format(filenameFormat, i)));
205                }
206
207                return files;
208        }
209}