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;
031
032import org.openimaj.image.pixel.Pixel;
033import org.openimaj.image.renderer.ImageRenderer;
034
035/**
036 * A simple 2d plotter
037 * 
038 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
039 *
040 * @param <Q> The pixel type that is processed
041 * @param <I> The image type that is processed
042 */
043public class Plotter<Q, I extends Image<Q, I>> {
044        protected Pixel penPosition = new Pixel(0, 0);
045        protected Q penColour;
046        protected int penThickness;
047        protected I image;
048        protected ImageRenderer<Q,I> renderer;
049        
050        /**
051         * Construct a plotter that writes to the given image.
052         * The pen colour is set to the value of the pixel at (0,0) 
053         * and the pen thickness is set to 1.
054         * 
055         * @param image the image
056         */
057        public Plotter(I image) {
058                this(image, image.getPixel(0, 0), 1);
059        }
060        
061        /**
062         * Construct a plotter that writes to the given image with the
063         * given initial pen colour. The pen thickness is set to 1.
064         * 
065         * @param image the image
066         * @param colour the pen colour
067         */
068        public Plotter(I image, Q colour) {
069                this(image, colour, 1);
070        }
071        
072        /**
073         * Construct a plotter that writes to the given image.
074         * 
075         * @param image the image
076         * @param colour the pen colour
077         * @param thickness the pen thickness
078         */
079        public Plotter(I image, Q colour, int thickness) {
080                this.image = image;
081                this.renderer = image.createRenderer();
082                penColour = colour;
083                penThickness = thickness;
084        }
085        
086        /**
087         * Move to a pixel
088         * @param p the pixel
089         */
090        public void moveTo(Pixel p) {
091                moveTo(p.x, p.y);
092        }
093        
094        /**
095         * Move to a point
096         * @param x x-position
097         * @param y y-position
098         */
099        public void moveTo(int x, int y) {
100                penPosition.x = x;
101                penPosition.y = y;
102        }
103        
104        /**
105         * Draw a line from the current position to a pixel
106         * @param p the pixel to draw to
107         */
108        public void lineTo(Pixel p) {
109                lineTo(p.x, p.y);
110        }
111        
112        /**
113         * Draw a line from the current position to a point
114         * @param x the x position
115         * @param y the y position
116         */
117        public void lineTo(int x, int y) {
118                renderer.drawLine(penPosition.x, penPosition.y, x, y, penThickness, penColour);
119                moveTo(x, y);
120        }
121
122        /**
123         * @return the penPosition
124         */
125        public Pixel getPenPosition() {
126                return penPosition;
127        }
128
129        /**
130         * @param penPosition the penPosition to set
131         */
132        public void setPenPosition(Pixel penPosition) {
133                this.penPosition = penPosition;
134        }
135
136        /**
137         * @return the penColour
138         */
139        public Q getPenColour() {
140                return penColour;
141        }
142
143        /**
144         * @param penColour the penColour to set
145         */
146        public void setPenColour(Q penColour) {
147                this.penColour = penColour;
148        }
149
150        /**
151         * @return the penThickness
152         */
153        public int getPenThickness() {
154                return penThickness;
155        }
156
157        /**
158         * @param penThickness the penThickness to set
159         */
160        public void setPenThickness(int penThickness) {
161                this.penThickness = penThickness;
162        }
163
164        /**
165         * @return the image
166         */
167        public I getImage() {
168                return image;
169        }
170
171        /**
172         * @param image the image to set
173         */
174        public void setImage(I image) {
175                this.image = image;
176                this.renderer = image.createRenderer();
177        }
178}