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 */
030/**
031 * 
032 */
033package org.openimaj.video.timecode;
034
035/**
036 *      This class allows video timecodes to be stored as frame numbers.
037 * 
038 *  @author David Dupplaw (dpd@ecs.soton.ac.uk)
039 *      
040 *      @created 1 Jun 2011
041 */
042public class FrameNumberVideoTimecode extends VideoTimecode
043{
044        /** The frame number */
045        private long frameNumber = -1;
046        
047        /** The number of frames per second */
048        protected double fps = 0;
049        
050        /**
051         *      Default constructor that takes the frame number.
052         *  @param number The frame number
053         *  @param fps The frame rate 
054         */
055        public FrameNumberVideoTimecode( final long number, final double fps )
056        {
057                this.frameNumber = number;
058                this.fps = fps;
059        }
060
061        /**
062         *      {@inheritDoc}
063         *      @see org.openimaj.video.timecode.VideoTimecode#getFrameNumber()
064         */
065        @Override
066        public long getFrameNumber()
067        {
068                return this.frameNumber;
069        }
070        
071        /**
072         *      Set the frame number.
073         *  @param frame The frame number
074         */
075        public void setFrameNumber( final long frame )
076        {
077                this.frameNumber = frame;
078        }
079
080        /**
081         *  {@inheritDoc}
082         *  @see java.lang.Comparable#compareTo(java.lang.Object)
083         */
084        @Override
085    public int compareTo( final VideoTimecode o )
086    {
087                if( o instanceof FrameNumberVideoTimecode ) {
088                        final long diff = (((FrameNumberVideoTimecode)o).getFrameNumber() - this.frameNumber);
089                        
090                        if (diff == 0) return 0;
091                        return (diff < 0) ? 1 : -1;
092                }
093                
094            return 0;
095    }
096
097        /**
098         *  {@inheritDoc}
099         *  @see java.lang.Object#hashCode()
100         */
101        @Override
102        public int hashCode()
103        {
104                return (int)this.frameNumber;
105        }
106
107        /**
108         *  {@inheritDoc}
109         *  @see java.lang.Object#equals(java.lang.Object)
110         */
111        @Override
112        public boolean equals( final Object obj )
113        {
114                if( obj instanceof FrameNumberVideoTimecode )
115                        return this.frameNumber == ((FrameNumberVideoTimecode)obj).getFrameNumber();
116                return false;
117        }
118
119        /**
120         *  {@inheritDoc}
121         *  @see java.lang.Object#toString()
122         */
123        @Override
124        public String toString()
125        {
126                return "Frame "+this.frameNumber;
127        }
128
129        /**
130         *  {@inheritDoc}
131         *  @see org.openimaj.time.Timecode#getTimecodeInMilliseconds()
132         */
133        @Override
134    public long getTimecodeInMilliseconds()
135    {
136            return (long)(this.frameNumber * 1000 / this.fps);
137    }
138
139        /**
140         *  {@inheritDoc}
141         *  @see org.openimaj.time.Timecode#setTimecodeInMilliseconds(long)
142         */
143        @Override
144    public void setTimecodeInMilliseconds( final long timeInMilliseconds )
145    {
146                this.frameNumber = (long)(timeInMilliseconds * this.fps / 1000d);
147    }
148        
149        /**
150         *      {@inheritDoc}
151         */
152        @Override
153        public FrameNumberVideoTimecode clone()
154        {
155                return new FrameNumberVideoTimecode( this.frameNumber, this.fps );
156        }
157}