@gov.nasa.jpl.honeycomb/sampler-2d
v0.0.6
Published
Utility for sampling 2D float arrays in row major or column major order.
Readme
Sampler 2D
Utility for sampling 2D float arrays in row major or column major order.
Use
import { Sampler2D } from '@gov.nasa.jpl.honeycomb/sampler-2d';
const data = new Float32Array( [
0, 0, 1, 0,
0, 1, 1, 1
] );
const sampler = new Sampler2D( data, 2, 2 );
const result = new Array();
sampler.sample( 0.5, 0.5, result ); // 0.5, 1
sampler.samplePixel( 0, 1, result ); // 0, 1API
Sampler2D
Texture-like object with an API for sampling image data.
For simplicity image data should be thought about as 0 0 being the bottom left pixel
of the image. Use xInvert or yInvert to flip the image if necessary. Data is assumed to
be interleaved.
channels
channels : NumberReturn the number of channels in the image.
data
data : TypedArrayThe image data.
stride
stride : Number = 4The stride of a single pixel vector in the array.
width
width : NumberThe width of the data grid.
height
height : NumberThe height of the data grid as derived from the data length, stride, and width.
rowMajor
rowMajor : Number = trueWhether the data should be sampled as row major (true) or column major (false).
invertX
invertX : Number = falseWhether the data should be inverted along X.
invertY
invertY : Number = falseWhether the data should be inverted along Y.
interpolate
interpolate : Number = trueIf true, then the result of sampling is bilinearly interpolated between the adjacent pixels.
constructor
constructor( data : TypedArray, width : Number, stride : Number = 4 ) : voidsamplePixelChannel
samplePixelChannel( x : Number, y : Number, channel : Number ) : Number | nullX is expected to be in the range [ 0, width ] and Y within [ 0, height ]. Sets target to the pixel value at
the given pixel. Decimal values are not supported.
Returns null if the provided values are outside a valid range. Returns the sample value otherwise.
samplePixel
samplePixel( x : Number, y : Number, target : Array ) : BooleanX is expected to be in the range [ 0, width ] and Y within [ 0, height ]. Sets target to the pixel value at
the given pixel. Decimal values are not supported.
Returns false if the provided values are outside a valid range. Returns true otherwise.
sampleChannel
sampleChannel( u : Number, v : Number, channel : Number ) : Number | nullU and V are expected to be in the range [ 0, 1 ]. Reads and interpolates the pixels if
interpolate is true.
Returns null if the provided values are outside a valid range. Returns the sample value otherwise.
sample
sample( u : Number, v : Number, target : Array ) : BooleanU and V are expected to be in the range [ 0, 1 ]. Reads and interpolates the pixels if
interpolate is true.
Returns false if the provided values are outside a valid range. Returns true otherwise.
generateMipMap
generateMipMap( target : Sampler2D = null ) : voidCreates a mip map of this Sampler2D. If the dimensions of this sampler are not a factor of two then weighted sampling over multiple pixels are used and a square mipmap is generated.
SpatialSampler2D
A sampler that contains members and functions for sampling with a 2D transform into the samplers texture space.
extends Sampler2D
inverseMatrix
inverseMatrix : Array<Number> = identity matrixA 3x3 matrix that transforms from the given coordinate frame into the local [ 0, 1 ] texture space of the sampler for use in the spatial sample functions.
constructor
constructor( args : ...rest ) : voidspatialSampleChannel
spatialSampleChannel( x : Number, y : Number, channel : Number ) : Number | nullTransforms X and Y by inverseMatrix before sampling the texture using @Sampler2D#sampleChannel and returning the result.
spatialSample
spatialSample( x : Number, y : Number, target : Array ) : BooleanTransforms X and Y by inverseMatrix before sampling the texture using @Sampler2D#sample and returning the result.
setMinMax
setMinMax( minX : Number, minY : Number, maxX : Number, maxY : Number ) : voidSets inverseMatrix to transform from a span of min to max x and y into
the span [ 0, 1 ].
BandSampler2D
A sampler that stores each channel as a separate sampler and samples data accordingly. Use this if image data is not provided as interleaved.
implements BandSampler2D
rowMajor
rowMajor : nullinvertX
invertX : nullinvertY
invertY : nullbands
bands : Array<Sampler2D> = []Array of sampler 2d instances where each band corresponds to a different channel.
constructor
constructor( bands : Array<Sampler2D> = [] ) : void