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

BlockSupplier #363

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main/java/net/imglib2/blocks/FallbackPrimitiveBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.NativeType;
import net.imglib2.type.NativeTypeFactory;
import net.imglib2.util.Cast;
import net.imglib2.util.Util;
import net.imglib2.view.Views;

Expand All @@ -66,8 +67,8 @@ public FallbackPrimitiveBlocks( final RandomAccessible< T > source, final T type
if ( type.getEntitiesPerPixel().getRatio() != 1 )
throw new IllegalArgumentException( "Types with entitiesPerPixel != 1 are not supported" );

nativeTypeFactory = ( NativeTypeFactory< T, A > ) type.getNativeTypeFactory();
primitiveTypeProperties = ( PrimitiveTypeProperties< ?, A > ) PrimitiveTypeProperties.get( nativeTypeFactory.getPrimitiveType() );
nativeTypeFactory = Cast.unchecked( type.getNativeTypeFactory() );
primitiveTypeProperties = Cast.unchecked( PrimitiveTypeProperties.get( nativeTypeFactory.getPrimitiveType() ) );
}

@Override
Expand All @@ -85,6 +86,13 @@ public void copy( final long[] srcPos, final Object dest, final int[] size )
LoopBuilder.setImages( Views.interval( source, interval ), img ).forEachPixel( ( a, b ) -> b.set( a ) );
}

@Override
public PrimitiveBlocks< T > independentCopy()
{
// NB FallbackPrimitiveBlocks is stateless
return this;
}

@Override
public PrimitiveBlocks< T > threadSafe()
{
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/net/imglib2/blocks/PrimitiveBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
*/
package net.imglib2.blocks;

import static net.imglib2.blocks.PrimitiveBlocks.OnFallback.FAIL;
import static net.imglib2.blocks.PrimitiveBlocks.OnFallback.WARN;

import net.imglib2.RandomAccessible;
import net.imglib2.type.NativeType;
import net.imglib2.util.Util;

import static net.imglib2.blocks.PrimitiveBlocks.OnFallback.FAIL;
import static net.imglib2.blocks.PrimitiveBlocks.OnFallback.WARN;


/**
* Copy blocks of data out of a {@code NativeType<T>} source into primitive
Expand Down Expand Up @@ -77,7 +77,7 @@
* If a source {@code RandomAccessible} cannot be understood, {@link
* PrimitiveBlocks#of(RandomAccessible) PrimitiveBlocks.of} will return a
* fall-back implementation (based on {@code LoopBuilder}).
*
* <p>
* With the optional {@link OnFallback OnFallback} argument to {@link
* PrimitiveBlocks#of(RandomAccessible, OnFallback) PrimitiveBlocks.of} it can
* be configured, whether
Expand Down Expand Up @@ -144,6 +144,8 @@ default void copy( int[] srcPos, Object dest, int[] size )
*/
PrimitiveBlocks< T > threadSafe();

PrimitiveBlocks< T > independentCopy();

enum OnFallback
{
ACCEPT,
Expand Down Expand Up @@ -197,11 +199,11 @@ static < T extends NativeType< T > > PrimitiveBlocks< T > of(
* @return a {@code PrimitiveBlocks} accessor for {@code ra}.
* @param <T> pixel type
*/
static < T extends NativeType< T >, R extends NativeType< R > > PrimitiveBlocks< T > of(
static < T extends NativeType< T > > PrimitiveBlocks< T > of(
RandomAccessible< T > ra,
OnFallback onFallback )
{
final ViewPropertiesOrError< T, R > props = ViewAnalyzer.getViewProperties( ra );
final ViewPropertiesOrError< T, ? > props = ViewAnalyzer.getViewProperties( ra );
if ( props.isFullySupported() )
{
return new ViewPrimitiveBlocks<>( props.getViewProperties() );
Expand Down
41 changes: 1 addition & 40 deletions src/main/java/net/imglib2/blocks/PrimitiveBlocksUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,15 @@
package net.imglib2.blocks;

import java.util.Arrays;
import java.util.function.Supplier;
import net.imglib2.Interval;
import net.imglib2.Point;
import net.imglib2.RandomAccessible;

import net.imglib2.img.array.ArrayImg;
import net.imglib2.img.array.ArrayImgFactory;
import net.imglib2.img.basictypeaccess.array.ArrayDataAccess;
import net.imglib2.transform.integer.MixedTransform;
import net.imglib2.type.NativeType;
import net.imglib2.type.Type;
import net.imglib2.util.CloseableThreadLocal;

class PrimitiveBlocksUtils
{
static < T extends NativeType< T > > PrimitiveBlocks< T > threadSafe( final Supplier< PrimitiveBlocks< T > > supplier )
{
final CloseableThreadLocal< PrimitiveBlocks< T > > tl = CloseableThreadLocal.withInitial( supplier );
return new PrimitiveBlocks< T >()
{
@Override
public T getType()
{
return tl.get().getType();
}

@Override
public void copy( final long[] srcPos, final Object dest, final int[] size )
{
tl.get().copy( srcPos, dest, size );
}

@Override
public PrimitiveBlocks< T > threadSafe()
{
return this;
}
};
}

static < T extends NativeType< T > > Object extractOobValue( final T type, final Extension extension )
{
if ( extension.type() == Extension.Type.CONSTANT )
Expand All @@ -86,15 +56,6 @@ static < T extends NativeType< T > > Object extractOobValue( final T type, final
return null;
}

// TODO replace with ra.getType() when that is available in imglib2 core
static < T extends Type< T > > T getType( RandomAccessible< T > ra )
{
final Point p = new Point( ra.numDimensions() );
if ( ra instanceof Interval )
( ( Interval ) ra ).min( p );
return ra.getAt( p ).createVariable();
}

/**
* Computes the inverse of (@code transform}. The {@code MixedTransform
* transform} is a pure axis permutation followed by inversion of some axes,
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/net/imglib2/blocks/ViewAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@
import net.imglib2.transform.integer.MixedTransform;
import net.imglib2.type.NativeType;
import net.imglib2.type.Type;
import net.imglib2.util.Cast;
import net.imglib2.util.Intervals;
import net.imglib2.view.ExtendedRandomAccessibleInterval;
import net.imglib2.view.IntervalView;
import net.imglib2.view.MixedTransformView;

import static net.imglib2.blocks.PrimitiveBlocksUtils.getType;

class ViewAnalyzer
{
/**
Expand Down Expand Up @@ -90,7 +89,7 @@ private ViewAnalyzer( final RandomAccessible< ? > ra )
*/
private < T extends Type< T > > boolean checkViewTypeSupported()
{
final T type = getType( ( RandomAccessible< T > ) ra );
final T type = Cast.unchecked( ra.getType() );
if ( type instanceof NativeType
&& ( ( NativeType ) type ).getEntitiesPerPixel().getRatio() == 1 )
{
Expand Down Expand Up @@ -589,16 +588,16 @@ private boolean splitTransform()

private < T extends NativeType< T >, R extends NativeType< R > > ViewProperties< T, R > getViewProperties()
{
final T viewType = getType( ( RandomAccessible< T > ) ra );
final NativeImg< R, ? > root = ( NativeImg< R, ? > ) nodes.get( nodes.size() - 1 ).view();
final T viewType = Cast.unchecked( ra.getType() );
final NativeImg< R, ? > root = Cast.unchecked( nodes.get( nodes.size() - 1 ).view() );
final R rootType = root.createLinkedType();
return new ViewProperties<>( viewType, root, rootType, oobExtension, transform, permuteInvertTransform, converterSupplier );
}

private < T extends NativeType< T > > FallbackProperties< T > getFallbackProperties()
{
final RandomAccessible< T > view = ( RandomAccessible< T > ) ra;
return new FallbackProperties<>( getType( view ), view );
final RandomAccessible< T > view = Cast.unchecked( ra );
return new FallbackProperties<>( view.getType(), view );
}

public static < T extends NativeType< T >, R extends NativeType< R > > ViewPropertiesOrError< T, R > getViewProperties( RandomAccessible< T > view )
Expand Down
45 changes: 39 additions & 6 deletions src/main/java/net/imglib2/blocks/ViewPrimitiveBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -33,14 +33,17 @@
*/
package net.imglib2.blocks;

import static net.imglib2.blocks.PrimitiveBlocksUtils.extractOobValue;

import java.util.function.Supplier;

import net.imglib2.transform.integer.MixedTransform;
import net.imglib2.type.NativeType;
import net.imglib2.type.PrimitiveType;
import net.imglib2.util.Cast;
import net.imglib2.util.CloseableThreadLocal;
import net.imglib2.util.Intervals;

import static net.imglib2.blocks.PrimitiveBlocksUtils.extractOobValue;

class ViewPrimitiveBlocks< T extends NativeType< T >, R extends NativeType< R > > implements PrimitiveBlocks< T >
{
private final ViewProperties< T, R > props;
Expand All @@ -58,6 +61,8 @@ class ViewPrimitiveBlocks< T extends NativeType< T >, R extends NativeType< R >

private final Convert convert;

private Supplier< PrimitiveBlocks< T > > threadSafeSupplier;

public ViewPrimitiveBlocks( final ViewProperties< T, R > props )
{
this.props = props;
Expand Down Expand Up @@ -156,10 +161,38 @@ else if ( doConvert )
@Override
public PrimitiveBlocks< T > threadSafe()
{
return PrimitiveBlocksUtils.threadSafe( this::newInstance );
if ( threadSafeSupplier == null )
threadSafeSupplier = CloseableThreadLocal.withInitial( this::independentCopy )::get;
return new PrimitiveBlocks< T >()
{
@Override
public T getType()
{
return props.getViewType();
}

@Override
public void copy( final long[] srcPos, final Object dest, final int[] size )
{
threadSafeSupplier.get().copy( srcPos, dest, size );
}

@Override
public PrimitiveBlocks< T > independentCopy()
{
return ViewPrimitiveBlocks.this.independentCopy().threadSafe();
}

@Override
public PrimitiveBlocks< T > threadSafe()
{
return this;
}
};
}

ViewPrimitiveBlocks< T, R > newInstance()
@Override
public PrimitiveBlocks< T > independentCopy()
{
return new ViewPrimitiveBlocks<>( this );
}
Expand Down
Loading