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 java.util.EnumMap;
033import java.util.EnumSet;
034
035import org.openimaj.image.FImage;
036import org.openimaj.image.pixel.ConnectedComponent;
037import org.openimaj.image.pixel.ConnectedComponent.ConnectMode;
038import org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor;
039import org.openimaj.math.geometry.shape.Polygon;
040
041/**
042 *      This renderer encapsulates various other renderers in an easy to
043 *      configure class, allowing multiple renderers to be called in one go.
044 *      This class is designed for greyscale {@link FImage}s. 
045 *      
046 *  @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
047 *      
048 */
049public class ConfigurableRendererMono implements ConnectedComponentProcessor 
050{
051        /** The image to draw into */
052        protected FImage image;
053        
054        /** The options to draw with */
055        protected EnumSet<ConfigurableRenderOptions> options;
056        
057        /** The colour to draw the various options in */
058        EnumMap<ConfigurableRenderOptions, Float> shade = new EnumMap<ConfigurableRenderOptions, Float>(ConfigurableRenderOptions.class);
059        
060        /**
061         *      Protected constructor that takes a set of options to draw.
062         * 
063         *  @param options The options to draw
064         */
065        protected ConfigurableRendererMono(EnumSet<ConfigurableRenderOptions> options) {
066                this.options = options;
067                
068                shade.put(ConfigurableRenderOptions.AXIS, 0.8F);
069                shade.put(ConfigurableRenderOptions.BLOB, 0.5F);
070                shade.put(ConfigurableRenderOptions.BORDER, 0.7F);
071                shade.put(ConfigurableRenderOptions.CENTROID, 0.7F);
072                shade.put(ConfigurableRenderOptions.CH_AXIS, 0.9F);
073                shade.put(ConfigurableRenderOptions.CH_BLOB, 0.2F);
074                shade.put(ConfigurableRenderOptions.CH_BORDER, 1.0F);
075                shade.put(ConfigurableRenderOptions.CH_CENTROID, 1.0F);
076        }
077        
078        /**
079         *      Constructor that takes an image to draw into and a set of options to draw.
080         * 
081         *  @param image The image to draw into
082         *  @param options The options to draw
083         */
084        public ConfigurableRendererMono(FImage image, EnumSet<ConfigurableRenderOptions> options) {
085                this(options);
086                
087                this.image = image;
088        }
089        
090        /**
091         *      Constructor that creates an image to draw into and takes a set of options
092         *      to draw.
093         * @param width The width of the image to create
094         * @param height The height of the image to create
095         * @param options The set of options to draw
096         */
097        public ConfigurableRendererMono(int width, int height, EnumSet<ConfigurableRenderOptions> options) {
098                this(options);
099                
100                image = new FImage(width, height);
101        }
102        
103        /**
104         *      Get the rendered image.
105         * 
106         *  @return The rendered image.
107         */
108        public FImage getImage() {
109                return image;
110        }
111
112        /**
113         *  {@inheritDoc}
114         *  @see org.openimaj.image.processor.connectedcomponent.ConnectedComponentProcessor#process(org.openimaj.image.pixel.ConnectedComponent)
115         */
116        @Override
117        public void process(ConnectedComponent cc) {
118                Polygon ch = cc.calculateConvexHull();
119                ConnectedComponent chcc = new ConnectedComponent(ch);
120                
121                if (options.contains(ConfigurableRenderOptions.CH_BLOB))
122                        chcc.process(new BlobRenderer<Float>(image, shade.get(ConfigurableRenderOptions.CH_BLOB)));
123                
124                if (options.contains(ConfigurableRenderOptions.BLOB))
125                        cc.process(new BlobRenderer<Float>(image, shade.get(ConfigurableRenderOptions.BLOB)));
126                
127                if (options.contains(ConfigurableRenderOptions.CH_BORDER)) 
128                        image.createRenderer().drawPolygon(ch, shade.get(ConfigurableRenderOptions.CH_BORDER));
129                
130                if (options.contains(ConfigurableRenderOptions.BORDER)) 
131                        cc.process(new BorderRenderer<Float>(image, shade.get(ConfigurableRenderOptions.BLOB), ConnectMode.CONNECT_8));
132                
133                if (options.contains(ConfigurableRenderOptions.CH_CENTROID)) 
134                        chcc.process(new CentroidRenderer<Float>(image, shade.get(ConfigurableRenderOptions.CH_CENTROID)));
135                
136                if (options.contains(ConfigurableRenderOptions.CENTROID))
137                        cc.process(new CentroidRenderer<Float>(image, shade.get(ConfigurableRenderOptions.CENTROID)));
138
139                if (options.contains(ConfigurableRenderOptions.CH_AXIS))
140                        chcc.process(new AxisRenderer<Float>(image, shade.get(ConfigurableRenderOptions.CH_AXIS)));
141                
142                if (options.contains(ConfigurableRenderOptions.AXIS))
143                        cc.process(new AxisRenderer<Float>(image, shade.get(ConfigurableRenderOptions.AXIS)));
144        }
145
146        /**
147         *      Set the colour of a specific option to override the default.
148         * 
149         *  @param r The option to set the colour for
150         *  @param colour The colour to draw that option in.
151         */
152        public void setColour(ConfigurableRenderOptions r, Float colour) {
153                shade.put(r, colour);
154        }
155        
156        /**
157         *      Get the colour that a specific option will be drawn in. 
158         * 
159         *  @param r The option to get the colour of.
160         *  @return The colour the option will be drawn in.
161         */
162        public Float getColour(ConfigurableRenderOptions r) {
163                return shade.get(r);
164        }
165}