package dokumenty; import pl.wroc.pwr.IOperator1P; import pl.wroc.pwr.imagechannel.IImageChannel; import pl.wroc.pwr.imagechannel.factory.ImageChannelFactory; public class SzybkiFiltrKrawedziowyPoziomy implements IOperator1P { private IImageChannel imageChannel; private IImageChannel result; private int maskSize; private int fullSize; private float [] cached; public SzybkiFiltrKrawedziowyPoziomy(int maskSize) { this.maskSize = maskSize; this.fullSize = this.maskSize * 2 + 1; } private int getStartIndex(int coord) { if (coord - this.maskSize < 0) { return -(coord - this.maskSize); } return 0; } private int getEndIndex(int coord, int maxCoord) { if (coord + this.fullSize - this.maskSize >= maxCoord) { return maxCoord - (coord - this.maskSize); } return this.fullSize; } private void cacheValues(int x) { for (int y = 0; y < this.imageChannel.getYSize(); y++) { this.cached[y] = 0; int maxMx = this.getEndIndex(x, this.imageChannel.getXSize()); for (int mx = this.getStartIndex(x); mx < maxMx; mx++) { int px = x + mx - this.maskSize; int index = this.imageChannel.getIndex(px, y); float value = this.imageChannel.getValue(index); this.cached[y] += value; } } } private void processSinglePixel(int x, int y) { float mean = 0; int maxMy = this.getEndIndex(y, this.imageChannel.getYSize()); for (int my = this.getStartIndex(y); my < maxMy; my++) { int py = y + my - this.maskSize; if (my == this.maskSize) { mean += this.cached[py]; } else { mean -= this.cached[py]; } } this.result.setValue(x, y, mean); } public IImageChannel apply(IImageChannel imageChannel) { this.imageChannel = imageChannel; this.cached = new float[this.imageChannel.getYSize()]; this.result = new ImageChannelFactory(this.imageChannel).apply(); for (int x = 0; x < this.imageChannel.getXSize(); x++) { this.cacheValues(x); for (int y = 0; y < this.imageChannel.getYSize(); y++) { this.processSinglePixel(x, y); } } return this.result; } }