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.processing.restoration.inpainting;
031
032import java.util.Collection;
033
034import org.openimaj.image.FImage;
035import org.openimaj.image.Image;
036import org.openimaj.image.pixel.Pixel;
037import org.openimaj.image.pixel.PixelSet;
038
039/**
040 * Abstract base for {@link Inpainter} implementations that consume a mask image
041 * (rather than connected components or pixel sets). Provides the necessary
042 * methods to build the mask image for all the various calls to
043 * <code>setMask</code>.
044 * <p>
045 * All <code>setMask</code> implementations call {@link #initMask()}, which
046 * subclasses should implement to perform any required initialisation.
047 * {@link #processImage(Image)} performs checks on the image dimensions and then
048 * calls {@link #performInpainting(Image)}.
049 * 
050 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
051 * 
052 * @param <IMAGE>
053 *            The type of image that this processor can process
054 */
055@SuppressWarnings("javadoc")
056public abstract class AbstractImageMaskInpainter<IMAGE extends Image<?, IMAGE>>
057                implements
058                Inpainter<IMAGE>
059{
060        /**
061         * The mask image
062         */
063        protected FImage mask;
064
065        @Override
066        public void setMask(FImage mask) {
067                this.mask = mask;
068                initMask();
069        }
070
071        @Override
072        public void setMask(int width, int height, Collection<? extends Iterable<Pixel>> mask) {
073                this.mask = new FImage(width, height);
074
075                for (final Iterable<Pixel> ps : mask) {
076                        for (final Pixel p : ps) {
077                                if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height)
078                                        this.mask.pixels[p.y][p.x] = 1;
079                        }
080                }
081                initMask();
082        }
083
084        @Override
085        public void setMask(int width, int height, PixelSet... mask) {
086                this.mask = new FImage(width, height);
087
088                for (final Iterable<Pixel> ps : mask) {
089                        for (final Pixel p : ps) {
090                                if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height)
091                                        this.mask.pixels[p.y][p.x] = 1;
092                        }
093                }
094                initMask();
095        }
096
097        /**
098         * Perform any initialisation once the mask has been set. Does nothing by
099         * default; subclasses should override as required.
100         */
101        protected void initMask() {
102
103        };
104
105        @Override
106        public final void processImage(IMAGE image) {
107                if (mask == null)
108                        throw new IllegalArgumentException("Mask has not been set");
109
110                if (image.getWidth() != mask.getWidth() || image.getHeight() != mask.getHeight())
111                        throw new IllegalArgumentException("Image and mask size do not match");
112
113                performInpainting(image);
114        }
115
116        /**
117         * Perform the inpainting of the given image
118         * 
119         * @param image
120         *            the image to inpaint
121         */
122        protected abstract void performInpainting(IMAGE image);
123}