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.effects; 31 32 import org.openimaj.image.FImage; 33 import org.openimaj.image.analysis.algorithm.SummedAreaTable; 34 import org.openimaj.image.processor.SinglebandImageProcessor; 35 import org.openimaj.math.geometry.line.Line2d; 36 37 /** 38 * Class to produce a Diorama or "Minature Faking" effect. The effect is 39 * achieved by blurring the image with a kernel that increases size with 40 * distance from a tilt-axis. In this implementation we use a 41 * {@link SummedAreaTable} to efficiently compute box-blurs. 42 * 43 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 44 * 45 */ 46 public class DioramaEffect implements SinglebandImageProcessor<Float, FImage> { 47 Line2d axis; 48 49 /** 50 * Construct with the given tilt axis 51 * 52 * @param axis 53 */ 54 public DioramaEffect(Line2d axis) { 55 this.axis = axis; 56 } 57 58 /** 59 * Get the current tilt axis 60 * 61 * @return the tilt axis 62 */ 63 public Line2d getAxis() { 64 return axis; 65 } 66 67 /** 68 * Set the current tilt axis 69 * 70 * @param axis 71 * the tilt axis 72 */ 73 public void setAxis(Line2d axis) { 74 this.axis = axis; 75 } 76 77 @Override 78 public void processImage(FImage image) { 79 render(image, new SummedAreaTable(image), 80 (int) axis.getBeginPoint().getX(), 81 (int) axis.getBeginPoint().getY(), 82 (int) axis.getEndPoint().getX(), 83 (int) axis.getEndPoint().getY()); 84 } 85 86 private void render(final FImage image, final SummedAreaTable sat, final int x1, final int y1, final int x2, 87 final int y2) 88 { 89 final int w = image.width; 90 final int h = image.height; 91 final double s = (w + h) * 2.0; 92 93 final int dx = x2 - x1; 94 final int dy = y2 - y1; 95 96 final float[][] pixels = image.pixels; 97 98 for (int y = 0; y < h; ++y) 99 { 100 final double yt = y - y1; 101 for (int x = 0; x < w; ++x) 102 { 103 final double xt = x - x1; 104 105 final double r = (dx * xt + dy * yt) / s; 106 final int ri = r < 0 ? (int) -r : (int) r; 107 108 final int yMin = Math.max(0, y - ri); 109 final int yMax = Math.min(h, y + ri); 110 final int bh = yMax - yMin; 111 112 if (bh == 0) 113 continue; 114 115 final int xMin = Math.max(0, x - ri); 116 final int xMax = Math.min(w, x + ri); 117 final float scale = 1.0f / (xMax - xMin) / bh; 118 119 pixels[y][x] = sat.calculateArea(xMin, yMin, xMax, yMax) * scale; 120 } 121 } 122 } 123 }