1 /**
2 * Copyright (c) 2011, The University of Southampton and the individual contributors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * * Neither the name of the University of Southampton nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.openimaj.image.processing.restoration.inpainting;
31
32 import java.util.Collection;
33
34 import org.openimaj.image.FImage;
35 import org.openimaj.image.Image;
36 import org.openimaj.image.pixel.Pixel;
37 import org.openimaj.image.pixel.PixelSet;
38
39 /**
40 * Abstract base for {@link Inpainter} implementations that consume a mask image
41 * (rather than connected components or pixel sets). Provides the necessary
42 * methods to build the mask image for all the various calls to
43 * <code>setMask</code>.
44 * <p>
45 * All <code>setMask</code> implementations call {@link #initMask()}, which
46 * subclasses should implement to perform any required initialisation.
47 * {@link #processImage(Image)} performs checks on the image dimensions and then
48 * calls {@link #performInpainting(Image)}.
49 *
50 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
51 *
52 * @param <IMAGE>
53 * The type of image that this processor can process
54 */
55 @SuppressWarnings("javadoc")
56 public abstract class AbstractImageMaskInpainter<IMAGE extends Image<?, IMAGE>>
57 implements
58 Inpainter<IMAGE>
59 {
60 /**
61 * The mask image
62 */
63 protected FImage mask;
64
65 @Override
66 public void setMask(FImage mask) {
67 this.mask = mask;
68 initMask();
69 }
70
71 @Override
72 public void setMask(int width, int height, Collection<? extends Iterable<Pixel>> mask) {
73 this.mask = new FImage(width, height);
74
75 for (final Iterable<Pixel> ps : mask) {
76 for (final Pixel p : ps) {
77 if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height)
78 this.mask.pixels[p.y][p.x] = 1;
79 }
80 }
81 initMask();
82 }
83
84 @Override
85 public void setMask(int width, int height, PixelSet... mask) {
86 this.mask = new FImage(width, height);
87
88 for (final Iterable<Pixel> ps : mask) {
89 for (final Pixel p : ps) {
90 if (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height)
91 this.mask.pixels[p.y][p.x] = 1;
92 }
93 }
94 initMask();
95 }
96
97 /**
98 * Perform any initialisation once the mask has been set. Does nothing by
99 * 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 }