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.vis.animators;
031
032import org.openimaj.content.animation.animator.LinearTimeBasedFloatValueAnimator;
033import org.openimaj.content.animation.animator.ValueAnimator;
034import org.openimaj.image.MBFImage;
035import org.openimaj.image.colour.ColourSpace;
036
037import cern.colt.Arrays;
038
039/**
040 *      An animator that moves through a colour space from one colour to another.
041 *      Start and end colours are provided in RGB. The colour space through which the
042 *      animator moves can be defined - the default is RGB. Works only with 3-dimensional
043 *      colour spaces.
044 *
045 *      @author Sina Samangooei (ss@ecs.soton.ac.uk)
046 *      @created 26 Jun 2013
047 */
048public class ColourSpaceAnimator implements ValueAnimator<Float[]>
049{
050        private final LinearTimeBasedFloatValueAnimator c1;
051        private final LinearTimeBasedFloatValueAnimator c2;
052        private final LinearTimeBasedFloatValueAnimator c3;
053
054        /** 1x1 Buffer required for the colour space transform functions */
055        private final MBFImage buffer;
056
057        /** The colour space being used */
058        private ColourSpace colourspace = ColourSpace.RGB;
059
060        /**
061         *      Construct the animator using the start and end colours and a duration (milliseconds)
062         *      @param start Start colour (RGB)
063         *      @param end End colour (RGB)
064         *      @param duration The duration (milliseconds)
065         */
066        public ColourSpaceAnimator( final Float[] start, final Float[] end, final long duration )
067        {
068                final MBFImage startImg = new MBFImage( 1, 1, 3 );
069                startImg.setPixel( 0, 0, start );
070                final MBFImage endImg = new MBFImage( 1, 1, 3 );
071                this.buffer = new MBFImage( 1, 1, 3 );
072                endImg.setPixel( 0, 0, end );
073
074                System.out.println( "In buffer: "+Arrays.toString( end ) );
075                System.out.println( "In buffer: "+Arrays.toString( endImg.getPixel( 0,0 ) ) );
076
077                final Float[] labstart = this.colourspace.convertFromRGB( startImg ).getPixel( 0, 0 );
078                final Float[] labend = this.colourspace.convertFromRGB( endImg ).getPixel( 0, 0 );
079
080                this.c1 = new LinearTimeBasedFloatValueAnimator( labstart[0], labend[0], duration );
081                this.c2 = new LinearTimeBasedFloatValueAnimator( labstart[1], labend[1], duration );
082                this.c3 = new LinearTimeBasedFloatValueAnimator( labstart[2], labend[2], duration );
083        }
084
085        /**
086         *      {@inheritDoc}
087         *      @see org.openimaj.content.animation.animator.ValueAnimator#nextValue()
088         */
089        @Override
090        public Float[] nextValue()
091        {
092                this.buffer.setPixel( 0, 0, new Float[]
093                { this.c1.nextValue(), this.c2.nextValue(), this.c3.nextValue() } );
094                final Float[] retPix = this.colourspace.convertToRGB( this.buffer ).getPixel( 0, 0 );
095                return retPix;
096        }
097
098        /**
099         *      {@inheritDoc}
100         *      @see org.openimaj.content.animation.animator.ValueAnimator#hasFinished()
101         */
102        @Override
103        public boolean hasFinished()
104        {
105                return this.c1.hasFinished() && this.c2.hasFinished() && this.c3.hasFinished();
106        }
107
108        /**
109         *      {@inheritDoc}
110         *      @see org.openimaj.content.animation.animator.ValueAnimator#reset()
111         */
112        @Override
113        public void reset()
114        {
115                this.c1.reset();
116                this.c2.reset();
117                this.c3.reset();
118        }
119
120        /**
121         *      @return the colourspace
122         */
123        public ColourSpace getColourspace()
124        {
125                return this.colourspace;
126        }
127
128        /**
129         *      @param colourspace the colourspace to set
130         */
131        public void setColourspace( final ColourSpace colourspace )
132        {
133                this.colourspace = colourspace;
134        }
135}