Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toniof enable ignored tests #135

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import org.esa.snap.core.datamodel.RasterDataNode;

import javax.media.jai.RasterAccessor;
import javax.media.jai.RasterFormatTag;
import java.awt.Rectangle;
import java.awt.image.Raster;

/**
* @author Tonio Fincke
Expand All @@ -20,11 +18,11 @@ public void init(RasterDataNode rasterDataNode, RasterAccessor srcAccessor, Rast
this.accessor = DataAccessorFactory.createDoubleDataAccessor(srcAccessor, dstAccessor, noDataValue);
}

protected double getSrcData(int index) {
double getSrcData(int index) {
return accessor.getSrcData(index);
}

protected void setDstData(int index, double value) {
void setDstData(int index, double value) {
accessor.setDstData(index, value);
}

Expand All @@ -50,8 +48,7 @@ int getDstOffset() {

public void dispose(){

};

}

static class NearestNeighbour extends DoubleDataInterpolator {

Expand All @@ -62,11 +59,11 @@ public void interpolate(Rectangle destRect, Rectangle srcRect, double scaleX, do
double subPixelOffsetY = offsetY - (int) offsetY;
double subPixelOffsetX = offsetX - (int) offsetX;
for (int dstY = 0; dstY < destRect.getHeight(); dstY++) {
int srcY = (int) (subPixelOffsetY + scaleY * dstY);
int srcY = (int) Math.round(subPixelOffsetY + scaleY * dstY);
int srcIndexY = getSrcOffset() + srcY * getSrcScalineStride();
boolean yValid = srcY < srcRect.getHeight();
for (int dstX = 0; dstX < destRect.getWidth(); dstX++) {
int srcX = (int) (subPixelOffsetX + scaleX * dstX);
int srcX = (int) Math.round(subPixelOffsetX + scaleX * dstX);
if (yValid && srcX < srcRect.getWidth()) {
setDstData(dstIndexY + dstX, getSrcData(srcIndexY + srcX));
} else {
Expand All @@ -90,13 +87,11 @@ public void interpolate(Rectangle destRect, Rectangle srcRect, double scaleX, do
double subPixelOffsetX = offsetX - (int) offsetX;
for (int dstY = 0; dstY < destRect.getHeight(); dstY++) {
double srcYF = subPixelOffsetY + (scaleY * (dstY + 0.5)) - 0.5;
// double srcYF = subPixelOffsetY + (scaleY * (dstY));
int srcY = (int) srcYF;
int srcIndexY = getSrcOffset() + srcY * getSrcScalineStride();
double wy = srcYF - srcY;
for (int dstX = 0; dstX < destRect.getWidth(); dstX++) {
double srcXF = subPixelOffsetX + (scaleX * (dstX + 0.5)) - 0.5;
// double srcXF = subPixelOffsetX + (scaleX * (dstX));
int srcX = (int) srcXF;
double wx = srcXF - srcX;
boolean withinSrcH = srcY + 1 < srcH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public static Interpolator createInterpolator(InterpolationType type, int dataTy
return new LongDataInterpolator.NearestNeighbour();
case Bilinear:
return new LongDataInterpolator.Bilinear();
case Cubic_Convolution:
return new LongDataInterpolator.CubicConvolution();
}
}
throw new IllegalArgumentException("Interpolation method not supported");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public void init(RasterDataNode rasterDataNode, RasterAccessor srcAccessor, Rast
this.accessor = DataAccessorFactory.createLongDataAccessor(srcAccessor, dstAccessor, noDataValue);
}

protected long getSrcData(int index) {
long getSrcData(int index) {
return accessor.getSrcData(index);
}

protected void setDstData(int index, long value) {
void setDstData(int index, long value) {
accessor.setDstData(index, value);
}

Expand All @@ -44,7 +44,7 @@ int getDstScalineStride() {

public void dispose(){

};
}

static class NearestNeighbour extends LongDataInterpolator {

Expand All @@ -55,11 +55,11 @@ public void interpolate(Rectangle destRect, Rectangle srcRect, double scaleX, do
double subPixelOffsetY = offsetY - (int) offsetY;
double subPixelOffsetX = offsetX - (int) offsetX;
for (int dstY = 0; dstY < destRect.getHeight(); dstY++) {
int srcY = (int) (subPixelOffsetY + scaleY * dstY);
int srcY = (int) Math.round(subPixelOffsetY + (scaleY * (dstY + 0.5)) - 0.5);
int srcIndexY = getSrcOffset() + srcY * getSrcScalineStride();
boolean yValid = srcY < srcRect.getHeight();
for (int dstX = 0; dstX < destRect.getWidth(); dstX++) {
int srcX = (int) (subPixelOffsetX + scaleX * dstX);
int srcX = (int) Math.round(subPixelOffsetX + (scaleX * (dstX + 0.5)) - 0.5);
if (yValid && srcX < srcRect.getWidth()) {
setDstData(dstIndexY + dstX, getSrcData(srcIndexY + srcX));
} else {
Expand All @@ -82,21 +82,19 @@ public void interpolate(Rectangle destRect, Rectangle srcRect, double scaleX, do
double subPixelOffsetX = offsetX - (int) offsetX;
for (int dstY = 0; dstY < destRect.getHeight(); dstY++) {
double srcYF = subPixelOffsetY + (scaleY * (dstY + 0.5)) - 0.5;
// double srcYF = subPixelOffsetY + (scaleY * (dstY));
int srcY = (int) srcYF;
double wy = srcYF - srcY;
int srcIndexY = getSrcOffset() + srcY * getSrcScalineStride();
for (int dstX = 0; dstX < destRect.getWidth(); dstX++) {
double srcXF = subPixelOffsetX + (scaleX * (dstX + 0.5)) - 0.5;
// double srcXF = subPixelOffsetX + (scaleX * (dstX));
int srcX = (int) srcXF;
double wx = srcXF - srcX;
boolean withinSrcH = srcY + 1 < srcH;
boolean withinSrcW = srcX + 1 < srcW;
double v00 = getSrcData(srcIndexY + srcX);
final int dstIndex = dstIndexY + dstX;
if (!withinSrcW && !withinSrcH) {
setDstData(dstIndex, (long) v00);
setDstData(dstIndex, Math.round(v00));
continue;
}
double v01 = withinSrcW ? getSrcData(srcIndexY + srcX + 1) : getNoDataValue();
Expand All @@ -117,36 +115,36 @@ public void interpolate(Rectangle destRect, Rectangle srcRect, double scaleX, do
double v0 = v00 + wx * (v01 - v00);
double v1 = v10 + wx * (v11 - v10);
double v = v0 + wy * (v1 - v0);
setDstData(dstIndex, (long) v);
setDstData(dstIndex, Math.round(v));
} else if (validityCounter == 1) {
if (v00Valid) {
setDstData(dstIndex, (long) v00);
setDstData(dstIndex, Math.round(v00));
} else if (v01Valid) {
setDstData(dstIndex, (long) v01);
setDstData(dstIndex, Math.round(v01));
} else if (v10Valid) {
setDstData(dstIndex, (long) v10);
setDstData(dstIndex, Math.round(v10));
} else {
setDstData(dstIndex, (long) v11);
setDstData(dstIndex, Math.round(v11));
}
} else if (validityCounter == 2) {
if (v00Valid && v01Valid) {
setDstData(dstIndex, (long) ((v00 * (1 - wx)) + (v01 * wx)));
setDstData(dstIndex, Math.round((v00 * (1 - wx)) + (v01 * wx)));
} else if (v10Valid && v11Valid) {
setDstData(dstIndex, (long) ((v10 * (1 - wx)) + (v11 * wx)));
setDstData(dstIndex, Math.round((v10 * (1 - wx)) + (v11 * wx)));
} else if (v00Valid && v10Valid) {
setDstData(dstIndex, (long) ((v00 * (1 - wy)) + (v10 * wy)));
setDstData(dstIndex, Math.round((v00 * (1 - wy)) + (v10 * wy)));
} else if (v01Valid && v11Valid) {
setDstData(dstIndex, (long) ((v01 * (1 - wy)) + (v11 * wy)));
setDstData(dstIndex, Math.round((v01 * (1 - wy)) + (v11 * wy)));
} else if (v00Valid && v11Valid) {
double ws = 1 / ((wx * wy) + ((1 - wx) * (1 - wy)));
double v = ((v00 * (1 - wx)) * (1 - wy)) + (v11 * wx * wy);
v *= ws;
setDstData(dstIndex, (long) v);
setDstData(dstIndex, Math.round(v));
} else {
double ws = 1 / (((1 - wx) * wy) + (wx * (1 - wy)));
double v = (v01 * wx * (1 - wy)) + (v10 * (1 - wx) * wy);
v *= ws;
setDstData(dstIndex, (long) v);
setDstData(dstIndex, Math.round(v));
}
} else { //validityCounter == 3
double w00 = (1 - wx) * (1 - wy);
Expand All @@ -157,22 +155,22 @@ public void interpolate(Rectangle destRect, Rectangle srcRect, double scaleX, do
w01 += (0.5 * w00);
w10 += (0.5 * w00);
double v = v01 * w01 + v10 * w10 + v11 * w11;
setDstData(dstIndex, (long) v);
setDstData(dstIndex, Math.round(v));
} else if (!v01Valid) {
w00 += (0.5 * w01);
w11 += (0.5 * w01);
double v = v00 * w00 + v10 * w10 + v11 * w11;
setDstData(dstIndex, (long) v);
setDstData(dstIndex, Math.round(v));
} else if (!v10Valid) {
w00 += (0.5 * w10);
w11 += (0.5 * w10);
double v = v00 * w00 + v01 * w01 + v11 * w11;
setDstData(dstIndex, (long) v);
setDstData(dstIndex, Math.round(v));
} else {
w01 += (0.5 * w11);
w10 += (0.5 * w11);
double v = v00 * w00 + v01 * w01 + v10 * w10;
setDstData(dstIndex, (long) v);
setDstData(dstIndex, Math.round(v));
}
}
}
Expand All @@ -186,4 +184,166 @@ private boolean isValid(double v) {

}

static class CubicConvolution extends LongDataInterpolator {

@Override
public void interpolate(Rectangle destRect, Rectangle srcRect, double scaleX, double scaleY,
double offsetX, double offsetY) {
final int srcH = (int) srcRect.getHeight();
final int srcW = (int) srcRect.getWidth();
int dstIndexY = getDstOffset();
double subPixelOffsetY = offsetY - (int) offsetY;
double subPixelOffsetX = offsetX - (int) offsetX;
for (int dstY = 0; dstY < destRect.getHeight(); dstY++) {
double srcYF = subPixelOffsetY + (scaleY * (dstY + 0.5)) - 0.5;
int srcY = (int) srcYF;
int srcIndexY = getSrcOffset() + srcY * getSrcScalineStride();
double wy = srcYF - srcY;
for (int dstX = 0; dstX < destRect.getWidth(); dstX++) {
double srcXF = subPixelOffsetX + (scaleX * (dstX + 0.5)) - 0.5;
int srcX = (int) srcXF;
double wx = srcXF - srcX;
final double[][] validRectangle = getValidRectangle(srcX, srcY, srcIndexY, srcW, srcH);
double[] rowMeans = new double[4];
for (int i = 0; i < 4; i++) {
rowMeans[i] =
(validRectangle[i][0] * (4 - 8 * (1 + wx) + 5 * Math.pow(1 + wx, 2) - Math.pow(1 + wx, 3))) +
(validRectangle[i][1] * (1 - 2 * Math.pow(wx, 2) + Math.pow(wx, 3))) +
(validRectangle[i][2] * (1 - 2 * Math.pow(1 - wx, 2) + Math.pow(1 - wx, 3))) +
(validRectangle[i][3] * (4 - 8 * (2 - wx) + 5 * Math.pow(2 - wx, 2) - Math.pow(2 - wx, 3)));
}
double v =
(rowMeans[0] * (4 - 8 * (1 + wy) + 5 * Math.pow(1 + wy, 2) - Math.pow(1 + wy, 3))) +
(rowMeans[1] * (1 - 2 * Math.pow(wy, 2) + Math.pow(wy, 3))) +
(rowMeans[2] * (1 - 2 * Math.pow(1 - wy, 2) + Math.pow(1 - wy, 3))) +
(rowMeans[3] * (4 - 8 * (2 - wy) + 5 * Math.pow(2 - wy, 2) - Math.pow(2 - wy, 3)));
if (Double.isNaN(v) || Math.abs(v - getNoDataValue()) < 1e-8) {
setDstData(dstIndexY + dstX, getNoDataValue());
} else {
setDstData(dstIndexY + dstX, Math.round(v));
}
}
dstIndexY += getDstScalineStride();
}
}

double[][] getValidRectangle(int srcX, int srcY, int srcIndexY, int srcW, int srcH) {
double[][] validRectangle = new double[4][4];
validRectangle[1][1] = getSrcData(srcIndexY + srcX);

final boolean notAtLeftBorder = srcX - 1 >= 0;
final boolean notAtUpperBorder = srcY - 1 >= 0;
final boolean notAtRightBorder = srcX + 1 < srcW;
final boolean notAtRightBorder2 = srcX + 2 < srcW;
final boolean notAtLowerBorder = srcY + 1 < srcH;
final boolean notAtLowerBorder2 = srcY + 2 < srcH;
validRectangle[0][1] = notAtLeftBorder ? getSrcData(srcIndexY, srcX, 0, 1) : validRectangle[1][1];
validRectangle[1][0] = notAtUpperBorder ? getSrcData(srcIndexY, srcX, 1, 0) : validRectangle[1][1];
validRectangle[2][1] = notAtRightBorder ? getSrcData(srcIndexY, srcX, 2, 1) : validRectangle[1][1];
validRectangle[1][2] = notAtLowerBorder ? getSrcData(srcIndexY, srcX, 1, 2) : validRectangle[1][1];
validRectangle[3][1] = notAtRightBorder2 ? getSrcData(srcIndexY, srcX, 3, 1) : validRectangle[2][1];
validRectangle[1][3] = notAtLowerBorder2 ? getSrcData(srcIndexY, srcX, 1, 3) : validRectangle[1][2];

if (notAtLeftBorder) {
if (notAtUpperBorder) {
validRectangle[0][0] = getSrcData(srcIndexY, srcX, 0, 0);
} else {
validRectangle[0][0] = validRectangle[0][1];
}
} else if (notAtUpperBorder) {
validRectangle[0][0] = validRectangle[1][0];
} else {
validRectangle[0][0] = validRectangle[1][1];
}
if (notAtRightBorder) {
if (notAtUpperBorder) {
validRectangle[2][0] = getSrcData(srcIndexY, srcX, 2, 0);
} else {
validRectangle[2][0] = validRectangle[2][1];
}
} else if (notAtUpperBorder) {
validRectangle[2][0] = validRectangle[1][0];
} else {
validRectangle[2][0] = validRectangle[1][1];
}
if (notAtRightBorder) {
if (notAtLowerBorder) {
validRectangle[2][2] = getSrcData(srcIndexY, srcX, 2, 2);
} else {
validRectangle[2][2] = validRectangle[2][1];
}
} else if (notAtLowerBorder) {
validRectangle[2][2] = validRectangle[1][2];
} else {
validRectangle[2][2] = validRectangle[1][1];
}
if (notAtLeftBorder) {
if (notAtLowerBorder) {
validRectangle[0][2] = getSrcData(srcIndexY, srcX, 0, 2);
} else {
validRectangle[0][2] = validRectangle[0][1];
}
} else if (notAtLowerBorder) {
validRectangle[0][2] = validRectangle[1][2];
} else {
validRectangle[0][2] = validRectangle[1][1];
}
if (notAtRightBorder2) {
if (notAtUpperBorder) {
validRectangle[3][0] = getSrcData(srcIndexY, srcX, 3, 0);
} else {
validRectangle[3][0] = validRectangle[3][1];
}
} else {
validRectangle[3][0] = validRectangle[2][0];
}
if (notAtRightBorder2) {
if (notAtLowerBorder) {
validRectangle[3][2] = getSrcData(srcIndexY, srcX, 3, 2);
} else {
validRectangle[3][2] = validRectangle[3][1];
}
} else {
validRectangle[3][2] = validRectangle[2][2];
}
if (notAtRightBorder) {
if (notAtLowerBorder2) {
validRectangle[2][3] = getSrcData(srcIndexY, srcX, 2, 3);
} else {
validRectangle[2][3] = validRectangle[2][2];
}
} else {
validRectangle[2][3] = validRectangle[1][3];
}
if (notAtLeftBorder) {
if (notAtLowerBorder2) {
validRectangle[0][3] = getSrcData(srcIndexY, srcX, 0, 3);
} else {
validRectangle[0][3] = validRectangle[0][2];
}
} else {
validRectangle[0][3] = validRectangle[1][3];
}
if (notAtRightBorder2) {
if (notAtLowerBorder2) {
validRectangle[3][3] = getSrcData(srcIndexY, srcX, 3, 3);
} else {
validRectangle[3][3] = validRectangle[3][2];
}
} else if (notAtLowerBorder2) {
validRectangle[3][3] = validRectangle[2][3];
} else {
validRectangle[3][3] = validRectangle[2][2];
}
return validRectangle;
}

private double getSrcData(int srcIndexY, int srcX, int x, int y) {
int yOffset = (y - 1) * getSrcScalineStride();
int xOffset = srcX + (x - 1);
return getSrcData(srcIndexY + yOffset + xOffset);
}

}

}
Loading