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.renderer;
031
032import org.openimaj.image.MultiBandImage;
033import org.openimaj.image.SingleBandImage;
034import org.openimaj.math.geometry.point.Point2d;
035import org.openimaj.math.geometry.shape.Polygon;
036
037/**
038 * Abstract base for {@link ImageRenderer}s that work on {@link MultiBandImage}
039 * s.
040 * 
041 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
042 * 
043 * @param <T>
044 *            The pixel type
045 * @param <I>
046 *            The concrete subclass type
047 * @param <S>
048 *            The concrete subclass type of each band
049 */
050public abstract class MultiBandRenderer<T extends Comparable<T>, I extends MultiBandImage<T, I, S>, S extends SingleBandImage<T, S>>
051                extends
052                ImageRenderer<T[], I>
053{
054        /**
055         * Construct with given target image.
056         * 
057         * @param targetImage
058         *            the target image.
059         */
060        public MultiBandRenderer(final I targetImage) {
061                super(targetImage);
062        }
063
064        /**
065         * Construct with given target image and rendering hints.
066         * 
067         * @param targetImage
068         *            the target image.
069         * @param hints
070         *            the render hints
071         */
072        public MultiBandRenderer(final I targetImage, final RenderHints hints) {
073                super(targetImage, hints);
074        }
075
076        /**
077         * Draws the given single band image onto each band at the given position.
078         * Side-affects this image. The single band image must be of the same type
079         * as the bands within this image.
080         * 
081         * @param image
082         *            A {@link SingleBandImage} to draw
083         * @param x
084         *            The x-coordinate for the top-left of the drawn image
085         * @param y
086         *            The y-coordinate for the top-left of the drawn image
087         */
088        public void drawImage(final S image, final int x, final int y) {
089                for (final S band : this.targetImage.bands)
090                        band.createRenderer(this.hints).drawImage(image, x, y);
091        }
092
093        /**
094         * Draws the given single band image onto the specific band at the given
095         * position. Side-affects this image. The single band image must be of the
096         * same type as the bands within this image.
097         * 
098         * @param image
099         *            A {@link SingleBandImage} to draw
100         * @param band
101         *            The band onto which the image will be drawn
102         * @param x
103         *            The x-coordinate for the top-left of the drawn image
104         * @param y
105         *            The y-coordinate for the top-left of the drawn image
106         */
107        public void drawImage(final S image, final int band, final int x, final int y) {
108                this.targetImage.bands.get(band).createRenderer(this.hints).drawImage(image, x, y);
109        }
110
111        /**
112         * {@inheritDoc}
113         * 
114         * @see org.openimaj.image.renderer.ImageRenderer#drawLine(int, int, double,
115         *      int, int, java.lang.Object)
116         */
117        @Override
118        public void drawLine(final int x1, final int y1, final double theta, final int length, final int thickness, T[] grey)
119        {
120                grey = this.sanitise(grey);
121
122                for (int i = 0; i < this.targetImage.bands.size(); i++) {
123                        this.targetImage.bands.get(i).createRenderer(this.hints).drawLine(x1, y1, theta, length, thickness, grey[i]);
124                }
125        }
126
127        /**
128         * {@inheritDoc}
129         * 
130         * @see org.openimaj.image.renderer.ImageRenderer#drawLine(int, int, int,
131         *      int, int, java.lang.Object)
132         */
133        @Override
134        public void drawLine(final int x0, final int y0, final int x1, final int y1, final int thickness, T[] grey) {
135                grey = this.sanitise(grey);
136
137                for (int i = 0; i < this.targetImage.bands.size(); i++) {
138                        this.targetImage.bands.get(i).createRenderer(this.hints).drawLine(x0, y0, x1, y1, thickness, grey[i]);
139                }
140        }
141
142        /**
143         * {@inheritDoc}
144         * 
145         * @see org.openimaj.image.renderer.ImageRenderer#drawLine(int, int, int,
146         *      int, int, java.lang.Object)
147         */
148        @Override
149        public void drawLine(final float x0, final float y0, final float x1, final float y1, final int thickness, T[] grey) {
150                grey = this.sanitise(grey);
151
152                for (int i = 0; i < this.targetImage.bands.size(); i++) {
153                        this.targetImage.bands.get(i).createRenderer(this.hints).drawLine(x0, y0, x1, y1, thickness, grey[i]);
154                }
155        }
156
157        /**
158         * {@inheritDoc}
159         * 
160         * @see org.openimaj.image.renderer.ImageRenderer#drawPoint(org.openimaj.math.geometry.point.Point2d,
161         *      java.lang.Object, int)
162         */
163        @Override
164        public void drawPoint(final Point2d p, T[] col, final int size) {
165                col = this.sanitise(col);
166                for (int i = 0; i < this.targetImage.bands.size(); i++)
167                        this.targetImage.bands.get(i).createRenderer(this.hints).drawPoint(p, col[i], size);
168        }
169
170        /**
171         * {@inheritDoc}
172         * 
173         * @see org.openimaj.image.renderer.ImageRenderer#drawPolygon(org.openimaj.math.geometry.shape.Polygon,
174         *      int, java.lang.Object)
175         */
176        @Override
177        public void drawPolygon(final Polygon p, final int thickness, T[] grey) {
178
179                grey = this.sanitise(grey);
180
181                for (int i = 0; i < this.targetImage.bands.size(); i++) {
182                        this.targetImage.bands.get(i).createRenderer(this.hints).drawPolygon(p, thickness, grey[i]);
183                }
184        }
185}