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.image.processor.connectedcomponent.render;
031
032import org.openimaj.image.FImage;
033import org.openimaj.image.Image;
034import org.openimaj.image.MBFImage;
035import org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor;
036
037/**
038 *      An abstract class that defines top-level methods for objects that can
039 *      render connected components into images.
040 * 
041 *  @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
042 *      
043 *  @param <T>  The type of image the rendered can write to
044 */
045public abstract class AbstractRenderer<T> implements ConnectedComponentProcessor 
046{
047        /** The image that the rendered will write to */
048        protected Image<T,?> image;
049        
050        /** The colour the connected component will be written in */
051        protected T colour;
052        
053        /**
054         *      Default constructor that takes the image to write to and the
055         *      colour to draw in.
056         * 
057         *  @param image The image to write to
058         *  @param colour The colour to write in
059         */
060        public AbstractRenderer(Image<T,?> image, T colour) 
061        {       
062                this.image = image;
063                this.colour = colour;
064        }
065        
066        /**
067         *      Constructor that creates a new image into which to render the
068         *      connected components and a colour to draw in.
069         * @param width The width of the image to create
070         * @param height The height of the image to create
071         * @param colour The colour to draw in
072         */
073        @SuppressWarnings("unchecked")
074        public AbstractRenderer(int width, int height, T colour) 
075        {
076                if (Float.class.isAssignableFrom(colour.getClass())) {
077                        image = (Image<T,?>)(Object)new FImage(width, height);
078                } else if (Float[].class.isAssignableFrom(colour.getClass())) {
079                        image = (Image<T,?>)(Object)new MBFImage(width, height, ((Float[])colour).length);
080                } else {
081                        throw new IllegalArgumentException("Unknown/unsupported type");
082                }
083                this.colour = colour;
084        }
085        
086        /**
087         *      Returns the colour that the components will be drawn in.
088         * 
089         *  @return the colour that the components will be drawn in.
090         */
091        public T getColour() {
092                return colour;
093        }
094        
095        /**
096         *      Set the colour that the components will be drawn in.
097         * 
098         *  @param colour the colour to draw components.
099         */
100        public void setColour(T colour) {
101                this.colour = colour;
102        }
103        
104        /**
105         *      Returns the image that is being rendered on.
106         * 
107         *  @return The image that is being rendered on.
108         */
109        public Image<T,?> getImage() {
110                return image;
111        }
112}