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.demos.sandbox.image;
034
035import java.io.IOException;
036import java.net.MalformedURLException;
037
038import org.openimaj.image.DisplayUtilities;
039import org.openimaj.image.FImage;
040import org.openimaj.image.ImageUtilities;
041import org.openimaj.image.MBFImage;
042import org.openimaj.image.processing.mask.MatteGenerator;
043import org.openimaj.image.processing.mask.MatteGenerator.MatteType;
044
045/**
046 *
047 *
048 *      @author David Dupplaw (dpd@ecs.soton.ac.uk)
049 *  @created 31 Jan 2013
050 *      @version $Author$, $Revision$, $Date$
051 */
052public class MatteGeneratorTest
053{
054        /**
055         *      @param args
056         * @throws IOException
057         * @throws MalformedURLException
058         */
059        public static void main( final String args[] ) throws MalformedURLException, IOException
060        {
061                final FImage linearMatteBlackTop = new FImage( 400, 400 );
062                MatteGenerator.generateMatte( linearMatteBlackTop, MatteType.LINEAR_VERTICAL_GRADIENT, true );
063                DisplayUtilities.display( linearMatteBlackTop, "Linear Matte - Black Top" );
064
065                final FImage linearMatteWhiteTop = new FImage( 400, 400 );
066                MatteGenerator.generateMatte( linearMatteWhiteTop, MatteType.LINEAR_VERTICAL_GRADIENT, false );
067                DisplayUtilities.display( linearMatteWhiteTop, "Linear Matte - White Top" );
068
069                final FImage linearMatteBlackLeft = new FImage( 400, 400 );
070                MatteGenerator.generateMatte( linearMatteBlackLeft, MatteType.LINEAR_HORIZONTAL_GRADIENT, true );
071                DisplayUtilities.display( linearMatteBlackLeft, "Linear Matte - Black Left" );
072
073                final FImage linearMatteWhiteLeft = new FImage( 400, 400 );
074                MatteGenerator.generateMatte( linearMatteWhiteLeft, MatteType.LINEAR_HORIZONTAL_GRADIENT, false );
075                DisplayUtilities.display( linearMatteWhiteLeft, "Linear Matte - White Left" );
076
077                final FImage radialBlackMiddle = new FImage( 400, 400 );
078                MatteGenerator.generateMatte( radialBlackMiddle, MatteType.RADIAL_GRADIENT, false );
079                DisplayUtilities.display( radialBlackMiddle, "Radial Matte - Black Middle" );
080
081                FImage radialWhiteMiddle = new FImage( 400, 400 );
082                MatteGenerator.generateMatte( radialWhiteMiddle, MatteType.RADIAL_GRADIENT, true );
083                DisplayUtilities.display( radialWhiteMiddle, "Radial Matte - White Middle" );
084
085                for( double angle = Math.PI/4; angle < Math.PI*2; angle+= Math.PI/4 )
086                {
087                        final FImage angledGrad = new FImage( 400, 400 );
088                        MatteGenerator.generateMatte( angledGrad, MatteType.ANGLED_LINEAR_GRADIENT, angle, 200d, 200d );
089                        DisplayUtilities.display( angledGrad, "Angled Gradient - "+Math.floor(angle*57.3)+" degrees" );
090                }
091
092                // This test should show using the matte generator used to generate image alpha mattes
093
094                // Loads an image, makes a copy and inverts the copy adding an alpha matte to it.
095                // It then plonks the matted image back into the original image, so there should end
096                // up with an image where the inverted version in the centre fades out to the original version.
097
098                // Load an image
099                final String testImage = "/org/openimaj/image/image_for_testing.jpg";
100                final MBFImage i = ImageUtilities.readMBFAlpha( 
101                                MatteGeneratorTest.class.getResource( testImage ) );
102
103                // Clone and invert the image, add an alpha matte using a radial gradient
104                final MBFImage f = i.clone().inverse();
105                radialWhiteMiddle = new FImage( i.getWidth(), i.getHeight() );
106                MatteGenerator.generateMatte( radialWhiteMiddle, 
107                                MatteType.RADIAL_GRADIENT, false );
108                f.bands.set( 3, radialWhiteMiddle );
109
110                // Draw the matted image onto the original image
111                i.drawImage( f, 0, 0 );
112
113                DisplayUtilities.display( i, "Alpha Matte" );
114        }
115}