Interface PrimitiveBlocks<T extends NativeType<T>>

  • Type Parameters:
    T - pixel type
    All Known Implementing Classes:
    FallbackPrimitiveBlocks, ViewPrimitiveBlocks

    public interface PrimitiveBlocks<T extends NativeType<T>>
    Copy blocks of data out of a NativeType<T> source into primitive arrays (of the appropriate type).

    Use the static method PrimitiveBlocks.of to create a PrimitiveBlocks accessor for an arbitrary RandomAccessible source. Then use the copy(long[], java.lang.Object, int[]) method, to copy blocks out of the source into flat primitive arrays.

    PrimitiveBlocks.of understands a lot of View constructions (that ultimately end in CellImg, ArrayImg, etc) and will try to build an optimized copier. For example, the following will work:

    
     		CellImg< UnsignedByteType, ? > cellImg3D;
     		RandomAccessible< FloatType > view = Converters.convert(
     				Views.extendBorder(
     						Views.hyperSlice(
     								Views.zeroMin(
     										Views.rotate( cellImg3D, 1, 0 )
     								),
     								2, 80 )
     				),
     				new RealFloatConverter<>(),
     				new FloatType()
     		);
     		PrimitiveBlocks< FloatType > blocks = PrimitiveBlocks.of( view );
    
     		final float[] data = new float[ 40 * 50 ];
     		blocks.copy( new int[] { 10, 20 }, data, new int[] { 40, 50 } );
     

    If a source RandomAccessible cannot be understood, PrimitiveBlocks.of will return a fall-back implementation (based on LoopBuilder).

    With the optional OnFallback argument to PrimitiveBlocks.of it can be configured, whether fall-back should be silently accepted (ACCEPT), a warning should be printed (WARN), or an IllegalArgumentException thrown (FAIL). The warning/exception message explains why the input RandomAccessible requires fall-back.

    The only really un-supported case is if the pixel type T does not map one-to-one to a primitive type. (For example, ComplexDoubleType or Unsigned4BitType are not supported.)

    Implementations are not thread-safe in general. Use threadSafe() to obtain a thread-safe instance (implemented using ThreadLocal copies). E.g.,

    
     		PrimitiveBlocks< FloatType > blocks = PrimitiveBlocks.of( view ).threadSafe();
     
    • Method Detail

      • getType

        T getType()
      • copy

        void copy​(long[] srcPos,
                  java.lang.Object dest,
                  int[] size)
        Copy a block from the (T-typed) source into primitive arrays (of the appropriate type).
        Parameters:
        srcPos - min coordinate of the block to copy
        dest - primitive array to copy into. Must correspond to T, for example, if T is UnsignedByteType then dest must be byte[].
        size - the size of the block to copy
      • copy

        default void copy​(int[] srcPos,
                          java.lang.Object dest,
                          int[] size)
        Copy a block from the (T-typed) source into primitive arrays (of the appropriate type).
        Parameters:
        srcPos - min coordinate of the block to copy
        dest - primitive array to copy into. Must correspond to T, for example, if T is UnsignedByteType then dest must be byte[].
        size - the size of the block to copy
      • threadSafe

        PrimitiveBlocks<T> threadSafe()
        Get a thread-safe version of this PrimitiveBlocks. (Implemented as a wrapper that makes ThreadLocal copies).
      • of

        static <T extends NativeType<T>> PrimitiveBlocks<T> of​(RandomAccessible<T> ra)
        Create a PrimitiveBlocks accessor for an arbitrary RandomAccessible source. Many View constructions (that ultimately end in CellImg, ArrayImg, etc.) are understood and will be handled by an optimized copier.

        If the source RandomAccessible cannot be understood, a warning is printed, and a fall-back implementation (based on LoopBuilder) is returned.

        The returned PrimitiveBlocks is not thread-safe in general. Use threadSafe() to obtain a thread-safe instance, e.g., PrimitiveBlocks.of(view).threadSafe().

        Type Parameters:
        T - pixel type
        Parameters:
        ra - the source
        Returns:
        a PrimitiveBlocks accessor for ra.
      • of

        static <T extends NativeType<T>> PrimitiveBlocks<T> of​(RandomAccessible<T> ra,
                                                               PrimitiveBlocks.OnFallback onFallback)
        Create a PrimitiveBlocks accessor for an arbitrary RandomAccessible source. Many View constructions (that ultimately end in CellImg, ArrayImg, etc.) are understood and will be handled by an optimized copier.

        If the source RandomAccessible cannot be understood, a fall-back implementation (based on LoopBuilder) has to be used. The onFallback argument specifies how to handle this case:

        • ACCEPT: silently accept fall-back
        • WARN: accept fall-back, but print a warning explaining why the input ra requires fall-back
        • FAIL: throw IllegalArgumentException explaining why the input ra requires fall-back
        The returned PrimitiveBlocks is not thread-safe in general. Use threadSafe() to obtain a thread-safe instance, e.g., PrimitiveBlocks.of(view).threadSafe().
        Type Parameters:
        T - pixel type
        Parameters:
        ra - the source
        Returns:
        a PrimitiveBlocks accessor for ra.