Skip to content

Commit

Permalink
make general
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Sep 26, 2024
1 parent 8ad2c83 commit b9f76b0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,38 @@ protected AbstractTensorPixelTransformation( final String name)
protected void setFloatUnitaryOperator(final FloatUnaryOperator fun) {
this.fun = fun;
}

protected void setDoubleUnitaryOperator(final DoubleUnaryOperator fun) {
this.dun = fun;
}

protected void setByteUnitaryOperator(final ByteUnaryOperator fun) {
this.bun = fun;
}

protected void setUByteUnitaryOperator(final UByteUnaryOperator fun) {
this.ubun = fun;
}

protected void setShortUnitaryOperator(final ShortUnaryOperator fun) {
this.sun = fun;
}

protected void setUShortUnitaryOperator(final UShortUnaryOperator fun) {
this.usun = fun;
}

protected void setIntUnitaryOperator(final IntUnaryOperator fun) {
this.iun = fun;
}

protected void setUIntUnitaryOperator(final UIntUnaryOperator fun) {
this.uiun = fun;
}

protected void setLongUnitaryOperator(final LongUnaryOperator fun) {
this.lun = fun;
}

@Override
public < R extends RealType< R > & NativeType< R > > Tensor< FloatType > apply( final Tensor< R > input )
Expand All @@ -77,47 +109,47 @@ public < R extends RealType< R > & NativeType< R > > Tensor< FloatType > apply(
public < R extends RealType< R > & NativeType< R > >
void applyInPlace( final Tensor< R > input )
{
if (input.getData().getAt(0) instanceof FloatType) {
if (input.getData().getAt(0) instanceof FloatType && fun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<FloatType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( fun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof DoubleType) {
} else if (input.getData().getAt(0) instanceof DoubleType && dun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<DoubleType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( dun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof ByteType) {
} else if (input.getData().getAt(0) instanceof ByteType && bun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<ByteType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( bun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof UnsignedByteType) {
} else if (input.getData().getAt(0) instanceof UnsignedByteType && ubun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<UnsignedByteType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( ubun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof ShortType) {
} else if (input.getData().getAt(0) instanceof ShortType && sun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<ShortType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( sun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof UnsignedShortType) {
} else if (input.getData().getAt(0) instanceof UnsignedShortType && usun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<UnsignedShortType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( usun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof IntType) {
} else if (input.getData().getAt(0) instanceof IntType && iun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<IntType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( iun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof UnsignedIntType) {
} else if (input.getData().getAt(0) instanceof UnsignedIntType && uiun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<UnsignedIntType>) input.getData() )
.multiThreaded()
.forEachPixel( i -> i.set( uiun.applyAs( i.get() ) ) );
} else if (input.getData().getAt(0) instanceof LongType) {
} else if (input.getData().getAt(0) instanceof LongType && lun != null) {
LoopBuilder
.setImages( (RandomAccessibleInterval<LongType>) input.getData() )
.multiThreaded()
Expand All @@ -136,7 +168,7 @@ public interface FloatUnaryOperator
@FunctionalInterface
public interface DoubleUnaryOperator
{
float applyAs( double in );
double applyAs( double in );
}

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public class BinarizeTransformation extends AbstractTensorPixelTransformation
public BinarizeTransformation()
{
super( name );
super.setFloatUnitaryOperator(v -> ( v >= threshold ) ? 1f : 0f);
super.setDoubleUnitaryOperator(v -> ( v >= threshold ) ? 1d : 0d);
super.setByteUnitaryOperator(v -> ( v >= threshold ) ? (byte) 1 : 0);
super.setUByteUnitaryOperator(v -> ( v >= threshold ) ? 1 : 0);
super.setShortUnitaryOperator(v -> ( v >= threshold ) ? (short) 1 : 0);
super.setUShortUnitaryOperator(v -> ( v >= threshold ) ? 1 : 0);
super.setIntUnitaryOperator(v -> ( v >= threshold ) ? 1 : 0);
super.setUIntUnitaryOperator(v -> ( v >= threshold ) ? 1L : 0L);
super.setLongUnitaryOperator(v -> ( v >= threshold ) ? 1L : 0L);
}

public void setThreshold(Object threshold) {
Expand All @@ -52,21 +61,19 @@ public void setThreshold(Object threshold) {

public void checkRequiredArgs() {
if (threshold == null) {
throw new IllegalArgumentException(String.format(DEFAULT_MISSING_ARG_ERR, "threshold"));
throw new IllegalArgumentException(String.format(DEFAULT_MISSING_ARG_ERR, name, "threshold"));
}
}

public < R extends RealType< R > & NativeType< R > > Tensor< FloatType > apply( final Tensor< R > input )
{
checkRequiredArgs();
super.setFloatUnitaryOperator(v -> ( v >= threshold ) ? 1f : 0f);
return super.apply(input);
}

public void applyInPlace( final Tensor< FloatType > input )
public < R extends RealType< R > & NativeType< R > > void applyInPlace( final Tensor< R > input )
{
checkRequiredArgs();
super.setFloatUnitaryOperator(v -> ( v >= threshold ) ? 1f : 0f);
super.apply(input);
super.applyInPlace(input);
}
}

0 comments on commit b9f76b0

Please sign in to comment.