
eu.agrosense.client.grid.operations.NineAvgInterpolationOperator Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2008-2012 AgroSense Foundation.
*
* AgroSense is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
* this software, see the FLOSS License Exception
* .
*
* AgroSense is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AgroSense. If not, see .
*
* Contributors:
* Timon Veenstra - initial API and implementation and/or initial documentation
*/
package eu.agrosense.client.grid.operations;
import eu.agrosense.client.grid.CalculationGrid;
import eu.agrosense.client.grid.Grid;
import static eu.agrosense.client.grid.Grid.BAND;
import eu.agrosense.client.grid.GridOperation;
import java.awt.image.RenderedImage;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;
/**
*
* @author Timon Veenstra
*/
@ServiceProvider(service = GridOperation.class)
@NbBundle.Messages({
"Nine average interpolation operator display name=Nine Average Interpolation",
"Nine average interpolation operator description=Calculates average of nine fields as value for the middle point"
})
public class NineAvgInterpolationOperator implements GridOperation {
@Override
public String getDisplayName() {
return Bundle.Nine_average_interpolation_operator_display_name();
}
@Override
public String getDescription() {
return Bundle.Nine_average_interpolation_operator_description();
}
@Override
public void execute(CalculationGrid grid) {
assert grid.getValues() != null;
int w = grid.getWidth();
int h = grid.getHeight();
// even rows and columns
for (int ih = 0; ih < h; ih=ih+2) {
for (int iw = 0; iw < w; iw=iw+2) {
int iv = ih * (int) w + iw;
int value = grid.getValues()[iv];
interpolatePoint(value, iv, w, h, grid);
}
}
// odd rows and columns
for (int ih = 1; ih < h; ih=ih+2) {
for (int iw = 1; iw < w; iw=iw+2) {
int iv = ih * (int) w + iw;
int value = grid.getValues()[iv];
interpolatePoint(value, iv, w, h, grid);
}
}
// even rows and odd columns
for (int ih = 0; ih < h; ih=ih+2) {
for (int iw = 1; iw < w; iw=iw+2) {
int iv = ih * (int) w + iw;
int value = grid.getValues()[iv];
interpolatePoint(value, iv, w, h, grid);
}
}
// odd rows and even columns
for (int ih = 1; ih < h; ih=ih+2) {
for (int iw = 0; iw < w; iw=iw+2) {
int iv = ih * (int) w + iw;
int value = grid.getValues()[iv];
interpolatePoint(value, iv, w, h, grid);
}
}
}
private void interpolatePoint(int value, int iv, int w, int h, CalculationGrid grid) {
// if (Integer.MIN_VALUE == value) {
boolean leftBorder = (iv % w) == 0;
boolean rightBorder = (iv + 1 % w) == 0;
boolean topBorder = iv < w;
boolean bottomBorder = iv > ((h - 1) * w);
int[] square = new int[9];
square[0] = leftBorder | topBorder ? Integer.MIN_VALUE : iv - (int) w - 1;
square[1] = topBorder ? Integer.MIN_VALUE : iv - (int) w;
square[2] = topBorder | rightBorder ? Integer.MIN_VALUE : iv - (int) w + 1;
square[3] = leftBorder ? Integer.MIN_VALUE : iv - 1;
square[4] = iv;
square[5] = rightBorder ? Integer.MIN_VALUE : iv + 1;
square[6] = leftBorder | bottomBorder ? Integer.MIN_VALUE : iv + (int) w - 1;
square[7] = bottomBorder ? Integer.MIN_VALUE : iv + (int) w;
square[8] = rightBorder | bottomBorder ? Integer.MIN_VALUE : iv + (int) w + 1;
int valid = 0;
double avg = 0.0;
for (int i = 0; i < 9; i++) {
// only add to average if:
// - square[i] contains a valid raster coordinate
// - raster has a value on that position
if (square[i] != Integer.MIN_VALUE && square[i] < grid.getValues().length && grid.getValues()[square[i]] != Integer.MIN_VALUE) {
avg = ((avg * valid++) + grid.getValues()[square[i]]) / valid;
}
}
if (valid > 0) {
grid.getValues()[iv] = (int) avg;
}
// }
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy