From 4b97c087d96dd28da5c37e0dc732235b6bb2aaf6 Mon Sep 17 00:00:00 2001 From: tpietzsch Date: Sat, 20 Apr 2024 10:40:38 +0200 Subject: [PATCH] Be defensive in NativeType.copy() methods: Act like createVariable() if no dataAccess is set --- src/main/java/net/imglib2/type/label/BasePairBitType.java | 5 ++++- src/main/java/net/imglib2/type/label/BasePairCharType.java | 5 ++++- src/main/java/net/imglib2/type/logic/BitType.java | 2 +- src/main/java/net/imglib2/type/logic/NativeBoolType.java | 2 +- src/main/java/net/imglib2/type/numeric/ARGBType.java | 2 +- .../net/imglib2/type/numeric/NativeARGBDoubleType.java | 5 ++++- .../imglib2/type/numeric/complex/ComplexDoubleType.java | 5 ++++- .../net/imglib2/type/numeric/complex/ComplexFloatType.java | 5 ++++- .../java/net/imglib2/type/numeric/integer/ByteType.java | 2 +- .../java/net/imglib2/type/numeric/integer/IntType.java | 2 +- .../java/net/imglib2/type/numeric/integer/LongType.java | 2 +- .../java/net/imglib2/type/numeric/integer/ShortType.java | 2 +- .../imglib2/type/numeric/integer/Unsigned128BitType.java | 7 +++++-- .../imglib2/type/numeric/integer/Unsigned12BitType.java | 2 +- .../net/imglib2/type/numeric/integer/Unsigned2BitType.java | 2 +- .../net/imglib2/type/numeric/integer/Unsigned4BitType.java | 2 +- .../net/imglib2/type/numeric/integer/UnsignedByteType.java | 2 +- .../net/imglib2/type/numeric/integer/UnsignedIntType.java | 2 +- .../net/imglib2/type/numeric/integer/UnsignedLongType.java | 2 +- .../imglib2/type/numeric/integer/UnsignedShortType.java | 2 +- .../numeric/integer/UnsignedVariableBitLengthType.java | 5 ++++- .../java/net/imglib2/type/numeric/real/DoubleType.java | 2 +- src/main/java/net/imglib2/type/numeric/real/FloatType.java | 2 +- 23 files changed, 45 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/imglib2/type/label/BasePairBitType.java b/src/main/java/net/imglib2/type/label/BasePairBitType.java index 72d139369..104730ab5 100644 --- a/src/main/java/net/imglib2/type/label/BasePairBitType.java +++ b/src/main/java/net/imglib2/type/label/BasePairBitType.java @@ -179,7 +179,10 @@ public BasePairBitType createVariable() @Override public BasePairBitType copy() { - return new BasePairBitType( this.get() ); + if ( dataAccess != null ) + return new BasePairBitType( this.get() ); + else + return createVariable(); } @Override diff --git a/src/main/java/net/imglib2/type/label/BasePairCharType.java b/src/main/java/net/imglib2/type/label/BasePairCharType.java index 2c29bf392..4f5c9c0c7 100644 --- a/src/main/java/net/imglib2/type/label/BasePairCharType.java +++ b/src/main/java/net/imglib2/type/label/BasePairCharType.java @@ -184,7 +184,10 @@ public BasePairCharType createVariable() @Override public BasePairCharType copy() { - return new BasePairCharType( get() ); + if ( dataAccess != null ) + return new BasePairCharType( get() ); + else + return createVariable(); } @Override diff --git a/src/main/java/net/imglib2/type/logic/BitType.java b/src/main/java/net/imglib2/type/logic/BitType.java index 4bbd212e3..721cc2784 100644 --- a/src/main/java/net/imglib2/type/logic/BitType.java +++ b/src/main/java/net/imglib2/type/logic/BitType.java @@ -309,7 +309,7 @@ public BitType createVariable() @Override public BitType copy() { - return new BitType( get() ); + return new BitType( dataAccess != null ? get() : false ); } @Override diff --git a/src/main/java/net/imglib2/type/logic/NativeBoolType.java b/src/main/java/net/imglib2/type/logic/NativeBoolType.java index 1eefb0ce0..9ad2b4df4 100644 --- a/src/main/java/net/imglib2/type/logic/NativeBoolType.java +++ b/src/main/java/net/imglib2/type/logic/NativeBoolType.java @@ -269,7 +269,7 @@ public NativeBoolType createVariable() @Override public NativeBoolType copy() { - return new NativeBoolType( get() ); + return new NativeBoolType( dataAccess != null ? get() : false ); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/ARGBType.java b/src/main/java/net/imglib2/type/numeric/ARGBType.java index 1ce2cd270..f92212cce 100644 --- a/src/main/java/net/imglib2/type/numeric/ARGBType.java +++ b/src/main/java/net/imglib2/type/numeric/ARGBType.java @@ -254,7 +254,7 @@ public ARGBType createVariable() @Override public ARGBType copy() { - return new ARGBType( get() ); + return new ARGBType( dataAccess != null ? get() : 0 ); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/NativeARGBDoubleType.java b/src/main/java/net/imglib2/type/numeric/NativeARGBDoubleType.java index 2bd30ab18..418249638 100644 --- a/src/main/java/net/imglib2/type/numeric/NativeARGBDoubleType.java +++ b/src/main/java/net/imglib2/type/numeric/NativeARGBDoubleType.java @@ -186,7 +186,10 @@ public NativeARGBDoubleType createVariable() @Override public NativeARGBDoubleType copy() { - return new NativeARGBDoubleType( getA(), getR(), getG(), getB() ); + if ( dataAccess != null ) + return new NativeARGBDoubleType( getA(), getR(), getG(), getB() ); + else + return createVariable(); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/complex/ComplexDoubleType.java b/src/main/java/net/imglib2/type/numeric/complex/ComplexDoubleType.java index 16b0daeda..8d7d46abe 100644 --- a/src/main/java/net/imglib2/type/numeric/complex/ComplexDoubleType.java +++ b/src/main/java/net/imglib2/type/numeric/complex/ComplexDoubleType.java @@ -187,7 +187,10 @@ public ComplexDoubleType createVariable() @Override public ComplexDoubleType copy() { - return new ComplexDoubleType( getRealFloat(), getImaginaryFloat() ); + if ( dataAccess != null ) + return new ComplexDoubleType( getRealFloat(), getImaginaryFloat() ); + else + return createVariable(); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/complex/ComplexFloatType.java b/src/main/java/net/imglib2/type/numeric/complex/ComplexFloatType.java index f6d053281..076ed22f5 100644 --- a/src/main/java/net/imglib2/type/numeric/complex/ComplexFloatType.java +++ b/src/main/java/net/imglib2/type/numeric/complex/ComplexFloatType.java @@ -240,7 +240,10 @@ public ComplexFloatType createVariable() @Override public ComplexFloatType copy() { - return new ComplexFloatType( getRealFloat(), getImaginaryFloat() ); + if ( dataAccess != null ) + return new ComplexFloatType( getRealFloat(), getImaginaryFloat() ); + else + return createVariable(); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/integer/ByteType.java b/src/main/java/net/imglib2/type/numeric/integer/ByteType.java index 509213547..13eb846bd 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/ByteType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/ByteType.java @@ -153,6 +153,6 @@ public ByteType createVariable() @Override public ByteType copy() { - return new ByteType( getByte() ); + return new ByteType( dataAccess != null ? get() : 0 ); } } diff --git a/src/main/java/net/imglib2/type/numeric/integer/IntType.java b/src/main/java/net/imglib2/type/numeric/integer/IntType.java index 0bba38c72..9dc6599ed 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/IntType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/IntType.java @@ -153,6 +153,6 @@ public IntType createVariable() @Override public IntType copy() { - return new IntType( getInt() ); + return new IntType( dataAccess != null ? get() : 0 ); } } diff --git a/src/main/java/net/imglib2/type/numeric/integer/LongType.java b/src/main/java/net/imglib2/type/numeric/integer/LongType.java index 4cfc41d90..387797577 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/LongType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/LongType.java @@ -161,6 +161,6 @@ public LongType createVariable() @Override public LongType copy() { - return new LongType( get() ); + return new LongType( dataAccess != null ? get() : 0 ); } } diff --git a/src/main/java/net/imglib2/type/numeric/integer/ShortType.java b/src/main/java/net/imglib2/type/numeric/integer/ShortType.java index 999a8ea80..acc8dbec7 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/ShortType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/ShortType.java @@ -153,6 +153,6 @@ public ShortType createVariable() @Override public ShortType copy() { - return new ShortType( getShort() ); + return new ShortType( dataAccess != null ? get() : 0 ); } } diff --git a/src/main/java/net/imglib2/type/numeric/integer/Unsigned128BitType.java b/src/main/java/net/imglib2/type/numeric/integer/Unsigned128BitType.java index 4945970f6..f1242bbdd 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/Unsigned128BitType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/Unsigned128BitType.java @@ -308,8 +308,11 @@ public Unsigned128BitType createVariable() public Unsigned128BitType copy() { final Unsigned128BitType copy = new Unsigned128BitType(); - final int k = i.get() * 2; - copy.set( dataAccess.getValue( k ), dataAccess.getValue( k + 1 ) ); + if ( dataAccess != null ) + { + final int k = i.get() * 2; + copy.set( dataAccess.getValue( k ), dataAccess.getValue( k + 1 ) ); + } return copy; } diff --git a/src/main/java/net/imglib2/type/numeric/integer/Unsigned12BitType.java b/src/main/java/net/imglib2/type/numeric/integer/Unsigned12BitType.java index fd2bdce29..0a7fbf6a2 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/Unsigned12BitType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/Unsigned12BitType.java @@ -171,6 +171,6 @@ public Unsigned12BitType createVariable() @Override public Unsigned12BitType copy() { - return new Unsigned12BitType( get() ); + return new Unsigned12BitType( dataAccess != null ? get() : 0 ); } } diff --git a/src/main/java/net/imglib2/type/numeric/integer/Unsigned2BitType.java b/src/main/java/net/imglib2/type/numeric/integer/Unsigned2BitType.java index 2f5ac7485..ada05deec 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/Unsigned2BitType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/Unsigned2BitType.java @@ -140,6 +140,6 @@ public Unsigned2BitType createVariable() @Override public Unsigned2BitType copy() { - return new Unsigned2BitType( get() ); + return new Unsigned2BitType( dataAccess != null ? get() : 0 ); } } diff --git a/src/main/java/net/imglib2/type/numeric/integer/Unsigned4BitType.java b/src/main/java/net/imglib2/type/numeric/integer/Unsigned4BitType.java index 0d3a5ea9f..ca5d3027d 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/Unsigned4BitType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/Unsigned4BitType.java @@ -132,6 +132,6 @@ public Unsigned4BitType createVariable() @Override public Unsigned4BitType copy() { - return new Unsigned4BitType( get() ); + return new Unsigned4BitType( dataAccess != null ? get() : 0 ); } } diff --git a/src/main/java/net/imglib2/type/numeric/integer/UnsignedByteType.java b/src/main/java/net/imglib2/type/numeric/integer/UnsignedByteType.java index d55b41611..8da8bd778 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/UnsignedByteType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/UnsignedByteType.java @@ -213,7 +213,7 @@ public UnsignedByteType createVariable() @Override public UnsignedByteType copy() { - return new UnsignedByteType( get() ); + return new UnsignedByteType( dataAccess != null ? get() : 0 ); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/integer/UnsignedIntType.java b/src/main/java/net/imglib2/type/numeric/integer/UnsignedIntType.java index 5725931d7..d30b618eb 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/UnsignedIntType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/UnsignedIntType.java @@ -248,7 +248,7 @@ public UnsignedIntType createVariable() @Override public UnsignedIntType copy() { - return new UnsignedIntType( get() ); + return new UnsignedIntType( dataAccess != null ? get() : 0 ); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/integer/UnsignedLongType.java b/src/main/java/net/imglib2/type/numeric/integer/UnsignedLongType.java index 357565cf1..139893a89 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/UnsignedLongType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/UnsignedLongType.java @@ -345,7 +345,7 @@ public UnsignedLongType createVariable() @Override public UnsignedLongType copy() { - return new UnsignedLongType( get() ); + return new UnsignedLongType( dataAccess != null ? get() : 0 ); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/integer/UnsignedShortType.java b/src/main/java/net/imglib2/type/numeric/integer/UnsignedShortType.java index b60b841bf..93a1e54ca 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/UnsignedShortType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/UnsignedShortType.java @@ -222,7 +222,7 @@ public UnsignedShortType createVariable() @Override public UnsignedShortType copy() { - return new UnsignedShortType( get() ); + return new UnsignedShortType( dataAccess != null ? get() : 0 ); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/integer/UnsignedVariableBitLengthType.java b/src/main/java/net/imglib2/type/numeric/integer/UnsignedVariableBitLengthType.java index 06829430e..9e8a1e5cf 100644 --- a/src/main/java/net/imglib2/type/numeric/integer/UnsignedVariableBitLengthType.java +++ b/src/main/java/net/imglib2/type/numeric/integer/UnsignedVariableBitLengthType.java @@ -121,7 +121,10 @@ public UnsignedVariableBitLengthType createVariable() @Override public UnsignedVariableBitLengthType copy() { - return new UnsignedVariableBitLengthType( getBits(), nBits ); + if ( dataAccess != null ) + return new UnsignedVariableBitLengthType( getBits(), nBits ); + else + return createVariable(); } /** @see UnsignedLongType#divide(long, long) */ diff --git a/src/main/java/net/imglib2/type/numeric/real/DoubleType.java b/src/main/java/net/imglib2/type/numeric/real/DoubleType.java index 77375ba08..8dbcf22f1 100644 --- a/src/main/java/net/imglib2/type/numeric/real/DoubleType.java +++ b/src/main/java/net/imglib2/type/numeric/real/DoubleType.java @@ -178,7 +178,7 @@ public DoubleType createVariable() @Override public DoubleType copy() { - return new DoubleType( get() ); + return new DoubleType( dataAccess != null ? get() : 0 ); } @Override diff --git a/src/main/java/net/imglib2/type/numeric/real/FloatType.java b/src/main/java/net/imglib2/type/numeric/real/FloatType.java index 2c1cdf0e7..aa20b1490 100644 --- a/src/main/java/net/imglib2/type/numeric/real/FloatType.java +++ b/src/main/java/net/imglib2/type/numeric/real/FloatType.java @@ -247,7 +247,7 @@ public FloatType createVariable() @Override public FloatType copy() { - return new FloatType( get() ); + return new FloatType( dataAccess != null ? get() : 0 ); } @Override